Advanced

Under the Hood: The Nodestr Prompt Compiler

Graph traversal algorithm, spatial ordering, tabular token interpolation, full-table Markdown serialization, and Output Hub export.

# Under the Hood: The Nodestr Prompt Compiler

Nodestr's prompt and document compiler converts visual node graphs into coherent LLM prompts, structured Markdown manuscripts, and multi-format export payloads in real time.

---

## 1. Upstream Graph Traversal

When an **Output Hub** (or downstream consumer) compiles text:
1. It identifies all incoming noodles connected to its Input handles.
2. It recursively traverses upstream nodes to collect text and tabular content from ancestor nodes.
3. It evaluates template placeholders (e.g. `{context}`) and injects connected upstream text in place.

---

## 2. Spatial Top-to-Bottom Ordering

When multiple nodes connect to a single input or Group Output:
- Nodestr sorts node text snippets by vertical canvas position and explicit port order.
- This guarantees predictable, unscrambled prompt concatenation regardless of the chronological order in which nodes were created.

---

## 3. Tabular Token Interpolation (Consumer [ 📄 Record | 📊 Table ] Modes)

When nodes connect to Table Nodes via the Royal Blue `All Records` port, the compiler detects the chosen consumer mode:

### A. Single-Record Mode (`📄 Record` Mode)
- **Active Record Resolution**: The compiler detects each consumer node's active record index (or the master index from a connected **Record Selector Node**) and interpolates column tokens (`{{First Name}}`, `{{Profession}}`) directly against that record's data map.
- **Schema Label Formatting**: If `Field/Value Pair` mode is selected on the table footer, the compiler formats outputs as `Column Name: Record Value` (e.g. `Name: Samantha`). If `Value Only` is active, it emits raw values (`Samantha`).
- **Null Safety**: Cells with empty or null values cleanly resolve to empty strings (`""`), preventing `null` leaks or syntax errors in downstream prompt text.

### B. Full-Table Markdown Mode (`📊 Table` Mode)
- **Markdown Pipe Formatting**: When a consumer node is switched to **`📊 Table`** mode—or when a Table Node connects directly into an **Output Hub**—the compiler formats the entire dataset as a clean Markdown table.
- **Token Injection**: Any `{{Table}}` token inside the consumer node's text field is replaced with the complete GitHub-Flavored Markdown pipe table (`| Col 1 | Col 2 | \n | :--- | :--- | \n ...`), with internal pipe characters (`\|`) and newlines safely escaped. The `{{TableTitle}}` token resolves to the upstream Table Node's title.

---

## 4. Output Hub `Out` Port Passthrough & Downstream Export

The **Output Hub** not only aggregates upstream streams for on-canvas preview, but also exposes a right-hand **`Out`** port:
- When a downstream node—such as the **Document Exporter Node**—connects to an Output Hub's `Out` port, it receives the Output Hub's complete compiled Markdown stream.
- Workspaces created in earlier versions are automatically updated when opened so the `Out` port is always available.

---

## 5. The Output Hub Batch Compiler & Pre-Flight Audit Engine

When downstream workflows require generating individual personalized copies, running parameterized test cases, or compiling mail merges across entire datasets, the **Output Hub** serves as a deterministic **Batch Compiler Engine**:

```
[ Table Node (50 rows) ] ───> [ Text Node ] ───> [ Output Hub ]
                                  (or)                 │
                              [ Find & Replace ]       ▼
                                                  [ Run Batch ]
```

### 1. Single Action Trigger
- Output Hub nodes connected to upstream Table Nodes display a prominent **`Run Batch (N rows)`** button inside the node's body.
- Unlike previous iterations, no batch triggers clutter the canvas header or individual Table Node toolbars—the Output Hub remains the single, authoritative point of document compilation and export.

### 2. Instant Pre-Flight Audit
Clicking **`Run Batch`** launches the batch compilation modal, which performs an instantaneous all-row dry-run in memory (<20ms for 100 rows):
- **Missing Cell Audit**: Scans all columns referenced by downstream text nodes or Find & Replace rules, highlighting rows with empty cells before you generate content.
- **Unresolved Token Detection**: Flags broken or mistyped placeholders (e.g. `{{FristName}}`) so you can fix errors upfront.
- **Exact Output Budgeting**: Calculates the exact compiled character count and memory footprint across all rows.

### 3. Human-Verified Safety Guardrail (Zero Blind AI Loops)
If an **AI Agent Node** is connected upstream in the active Output Hub stream, the batch engine **actively locks batch execution**:
- Blind automated AI loops risk depleting provider quotas, running up unexpected token charges, or generating uninspected responses.
- To run a batch with AI content, use the AI Node's **`Extract to Text`** action first. This commits the generated prompt or draft into a standard Text Node where you can inspect the copy, wire in your Table Node's column keys (or use Find & Replace), and run the batch deterministically with full human verification.

### 4. Versatile Output Destinations
The Batch Compiler supports six specialized destinations tailored to your output goals:
- **Merged Text Node**: Compiles all evaluated rows into a single text document with your choice of separator (Horizontal rule, Double newline, Markdown headers, or Custom text).
- **Linked Table**: Creates a new Table Node on your canvas with an added **`Output`** column containing each row's evaluated text, wired directly to your original data.
- **New Table Column**: Appends the compiled output as a new column on your existing upstream Driver Table (subject to the 12/16 column cap).
- **Direct Export**: Downloads the entire compiled batch directly to your machine as **Plain Text (.txt)**, **Markdown (.md)**, **HTML (.html)**, or **Microsoft Word (.docx)**, cleanly bypassing text box size caps for large multi-page reports.
- **Spawn Individual Text Nodes**: Generates up to 20 individual Text Nodes arranged in an organized grid on your canvas, protected by a hard 20-node safety cap.

### 5. Multi-Table Graphs: Driver vs. Reference Tables
In complex graphs containing multiple Table Nodes:
- The compiler classifies tables into **Driver Tables** (which iterate row-by-row) and **Reference Tables** (which remain in Full-Table `📊 Table` mode across all iterations).
- A table role selector allows you to pick which dataset drives the iteration loop, while reference matrices (e.g. pricing sheets or style guides) are injected intact into every row's compiled output.

---

## 6. Execution Safety & Compiler Isolation

Non-text canvas elements—such as **Sticky Notes**, **Frames**, **Record Selector Nodes**, canvas decorations (stickers, shapes, arrows), and **Document Exporter Nodes**—are automatically excluded during prompt compilation. Only content-bearing nodes contribute to your final output.