1. The 4-Line Core Engine: executeStep

In `v4`, the core step execution function has been reduced to four clean, expressive lines with zero defensive boilerplate:

export const executeStep = (key, state, showLog = false) => {
    const instruction = instructions[key];
    const result = types[instruction.type](instruction, state);
    writeResult(state, instruction.destinationPath, result, showLog);
    return result;
};
1
Instruction Lookup:

Fetches the declarative instruction blueprint directly from declarative-instructions using its unique string identifier (e.g., "buildColgroup").

2
Dimensional Type Dispatch:

Maps instruction.type (single, rowOnColumn, or columnOnRow) to the appropriate mathematical transformer without touching DOM nodes.

3
In-Place Canvas Mounting:

writeResult locates the destination mount point inside the target canvas specification and commits the computed node AST.

4
Synchronous Return:

Returns the transformed subtree result for optional intermediate validation or chaining.

2. The Dimension Dispatcher Table

The types registry routes instructions based on their geometric dimensionality:

const types = {
    single: (instruction, state) => {
        const template = spec[instruction.templateKey];
        const source = resolveSource(instruction.sourcePath, state);
        const fn = instruction.func === "fromCollection" ? fromCollection : applyTemplate;
        return fn(source, template);
    },
    rowOnColumn: (instruction, { data }) => {
        return rowOnColumn({
            columns: data?.columns,
            data,
            columnSpec: spec[instruction.templateKey.columnKey],
            rowSpec: spec[instruction.templateKey.rowKey]
        });
    },
    columnOnRow: (instruction, { data }) => {
        return columnOnRow({
            columns: data?.columns,
            data,
            columnSpec: spec[instruction.templateKey.columnKey],
            rowSpec: spec[instruction.templateKey.rowKey]
        });
    }
};

3. Path Navigation Helpers (helpers.js)

Two companion helper functions handle input data resolution and output canvas insertion:

A. resolveSource(sourcePath, state)

Extracts the required data subset from the state object. Supports dual navigation modes:

B. writeResult(state, destinationPath, result, showLog)

Applies the generated spec directly into the canvas. Uses json-write-path to find target branches. Includes safe-skipping so missing optional containers won't crash execution.

Critical Contract

4. The Canvas Pattern: Expect base, NEVER baseKey

json-renderers-run-instructions-steps is designed to accept an initialized base canvas object, mutate it sequentially in-place, and return that base. It must never accept or expect baseKey.

Anti-Pattern: Accepting baseKey

Forces the steps runner to perform catalog resolution, introduces heavy dependencies on declarative-spec, isolates canvas state, and breaks composability with parent orchestrators.

// FORBIDDEN BLUNDER
runInstructions({ instructions, baseKey, data })
Correct Architecture: Expecting base

The runner receives the in-memory canvas AST (base), runs instructions against state = { base, data }, and returns the populated base to the pipeline.

// THE ARCHITECTURAL STANDARD
runInstructions({ instructions, base, data })
Why This Architecture Is Non-Negotiable:
  1. Pipelining in json-renderers-run-instructions: The master orchestrator resolves the definition, clones the base template once via const base = structuredClone(spec[definition.baseFromSpec]), passes base into Task 1 (Steps) to populate core tables/forms, and then passes the exact same canvas to Task 2 (Options) to inject search bars, buttons, and pagination. If steps required baseKey, the shared canvas pipeline would break completely.
  2. Symmetry of Return Value: The runner returns base. It receives base, mutates its sub-trees, and returns the modified base. Receiving baseKey while returning base violates interface predictability.
  3. Zero Catalog Coupling & Pollution: Expecting base frees the runner from needing declarative-spec as an internal dependency. It also prevents catastrophic in-memory catalog mutation where multiple runs would poison a shared static spec.

5. Ecosystem Dependencies & Integration

Each package in the stack has a single, strictly bounded responsibility:

Package Role in Pipeline
declarative-instructions Provides instruction metadata, template keys, and destination paths.
spec-repo Central catalog for raw element specifications (tags, attributes, shells).
json-transformer-funcs-primitive 0D primitive template interpolator.
json-transformer-funcs-one-dimension 1D linear collection array mapper (fromCollection).
json-transformer-funcs-two-dimensions 2D Cartesian matrix engine (rowOnColumn, columnOnRow).
jsonpathly High-speed JSONPath query engine for input data resolution.
json-write-path In-place AST tree mutator for target specification insertion.