Web Platform + Edge Systems Prototype

Reactive DOM Signals for Prompt Pipelines

Signal-driven DAG dependency scheduling binds DOM MutationObserver events to a structuredClone memoization cache, edge ReadableStream execution, and immediate AbortController cancellation for superseded streams.

Problem & Hypothesis

Re-running a full multi-step prompt pipeline on every keystroke wastes compute and lets slow out-of-order responses overwrite newer edits. Modeling the pipeline as a MutationObserver-driven dependency DAG preserves unaffected sibling outputs via structuredClone caching and cancels superseded fetch() streams via AbortController.

Confirm / Falsify Signals

Confirm: Rapid leaf edits abort in-flight NDJSON streams (abortedCount >= 2) while node-compliance resolves from cache. Falsify: Downstream nodes re-run redundantly on unchanged inputs or abort storms cause stale writes.

What To Try First

Click 1. Simulate Rapid Multi-Edit Burst below and watch In-Flight Streams Aborted and Cached Sibling Hits update in the telemetry strip and execution timeline.

3 Things To Try

Trigger deterministic signal schedules to observe mid-stream aborts, sibling cache hits, and DOM mutation capture.

Dispatches 3 topic updates 180ms apart via #topic-surface. Triggers automatic AbortController cancellation (abortedCount >= 2) while keeping node-compliance cached.

Cycles the audience signal. Recomputes node-outline and node-final-draft while serving node-compliance from cache (cacheHits++).

Mutates the contenteditable DOM node and places the caret inside so MutationObserver drives incremental streaming without resetting untouched branches.

In-Flight Streams Aborted 0
Cached Sibling Hits 0
Total Edge Requests 0
Cache Entries / Wave 0 / #0

Source Signals

Edit any signal below to trigger topological DAG invalidation.

MutationObserver: watching
Feeds node-outline
Incremental DOM invalidation for streaming prompt pipelines
Feeds node-outline
Feeds node-compliance only
8 chunks per node

Live DAG Topology

node-compliance remains isolated from topic and audience edits.

Reactive Prompt Pipeline Directed Acyclic Graph Three source signals (topic, audience, constraints) connect to Stage 1 compute nodes (node-outline and node-compliance), which join into Stage 2 node-final-draft. topic Signal · MutationObserver Incremental DOM invalid... audience Signal · select browser-engineers constraints Signal · select strict-csp-zero-innerhtml node-outline deps: [topic, audience] status: idle (0/8) hash: #-------- node-compliance deps: [constraints] status: idle (0/8) hash: #-------- node-final-draft deps: [outline, compliance] status: idle (0/8) hash: #--------
idle streaming cached aborted completed

Derived Compute Node Outputs

Incremental NDJSON chunks rendered via textContent (zero innerHTML).

node-outline

Depends on: topic, audience

idle
0/8 chunks · 0 ms #--------
Waiting for initial wave...

node-compliance

Depends on: constraints only

idle
0/8 chunks · 0 ms #--------
Waiting for initial wave...

node-final-draft

Depends on: node-outline, node-compliance

idle
0/8 chunks · 0 ms #--------
Waiting for initial wave...

Execution Timeline, Cache & Plan Inspector

Exclusive <details name="wire"> disclosures tracing abort signals, memoized entries, and compute plans.

Execution Timeline & HTTP Wire Inspector
POST /api/stream-step · application/x-ndjson · request.signal active
    Memoization Cache Snapshot (structuredClone)
    {}
    Latest planRecompute() Output
    {}
    How This Is Set Up

    Implementation & Platform Contract

    Ad-hoc input listeners chained to sequential fetch() calls re-execute unchanged upstream steps on every keystroke and allow slow responses to overwrite newer edits. Binding MutationObserver to a topological DAG scheduler with structuredClone sibling caches and AbortController cancellation isolates dirty subgraphs and terminates stale edge streams.

    Imperative fetch chain (baseline)
    // Imperative listeners re-run full chains and race on out-of-order responses
    topicInput.addEventListener("input", async () => {
      const outline = await (await fetch("/api/outline", { method: "POST", body: topicInput.value })).text();
      const compliance = await (await fetch("/api/compliance", { method: "POST", body: constraints.value })).text();
      const draft = await (await fetch("/api/draft", { method: "POST", body: outline + compliance })).text();
      // An earlier slow fetch can resolve last and overwrite newer DOM output
      outputPre.textContent = draft;
    });
    Reactive DOM signal DAG (this lab)
    // public/app.js — MutationObserver drives SignalDagScheduler with abort & cache reuse
    const scheduler = new SignalDagScheduler({
      initialSignals: { topic: readTopicText(), audience: audienceSelect.value, constraints: constraintsSelect.value },
      executor: edgeStreamExecutor // Streams NDJSON from POST /api/stream-step with AbortSignal
    });
    const topicObserver = new MutationObserver(() => {
      void scheduler.updateSignal("topic", readTopicText());
    });
    topicObserver.observe(topicSurface, { characterData: true, childList: true, subtree: true });
    // Only dirty nodes (node-outline -> node-final-draft) re-run; node-compliance stays cached
    Complete Page Setup: HTML Markup, DAG Spec & Edge Stream Contract

    Live DOM signal elements in public/index.html, the DAG_SPEC dependency graph and planRecompute() planner in public/lib/signal-dag.js, MutationObserver + AbortController NDJSON reader in public/app.js, and createStepStreamResponse() in src/worker.ts.

    <!-- 1. Page HTML Setup (public/index.html) -->
    <div id="topic-surface" class="topic-surface" contenteditable="true" role="textbox">Incremental DOM invalidation for streaming prompt pipelines</div>
    <select id="audience-select"><option value="browser-engineers">browser-engineers</option></select>
    <select id="constraints-select"><option value="strict-csp-zero-innerhtml">strict-csp-zero-innerhtml</option></select>
    <pre tabindex="0" class="node-output"><code id="out-node-outline"></code></pre>
    <pre tabindex="0" class="node-output"><code id="out-node-compliance"></code></pre>
    <pre tabindex="0" class="node-output"><code id="out-node-final-draft"></code></pre>
    <script type="module" src="/app.js"></script>
    
    // 2. Concrete DAG_SPEC & planRecompute() Contract (public/lib/signal-dag.js)
    export const COMPUTE_ORDER = Object.freeze(["node-outline", "node-compliance", "node-final-draft"]);
    export const DAG_SPEC = Object.freeze({
      topic: Object.freeze({ id: "topic", kind: "signal", deps: Object.freeze([]), label: "Source: topic" }),
      audience: Object.freeze({ id: "audience", kind: "signal", deps: Object.freeze([]), label: "Source: audience" }),
      constraints: Object.freeze({ id: "constraints", kind: "signal", deps: Object.freeze([]), label: "Source: constraints" }),
      "node-outline": Object.freeze({ id: "node-outline", kind: "compute", deps: Object.freeze(["topic", "audience"]), label: "node-outline" }),
      "node-compliance": Object.freeze({ id: "node-compliance", kind: "compute", deps: Object.freeze(["constraints"]), label: "node-compliance" }),
      "node-final-draft": Object.freeze({ id: "node-final-draft", kind: "compute", deps: Object.freeze(["node-outline", "node-compliance"]), label: "node-final-draft" })
    });
    const plan = planRecompute({ generation, signals, cache, nodeStates, activeNodeIds }, changedKey, newValues);
    // Returns { generation, dirtyNodes, preservedNodes, abortTargets, steps }
    // Step actions: "reuse-sibling" | "cache-hit" | "await-inflight" | "recompute"
    
    // 3. MutationObserver, AbortController NDJSON Consumer & Worker Stream (public/app.js + src/worker.ts)
    const topicObserver = new MutationObserver(() => { void scheduler.updateSignal("topic", readTopicText()); });
    topicObserver.observe(topicSurface, { characterData: true, childList: true, subtree: true });
    const response = await fetch("/api/stream-step", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ nodeId, inputs: context.inputs, depHash: context.depHash, generation: context.generation, chunkDelayMs }),
      signal
    });
    const reader = response.body.getReader(); // Decodes NDJSON frames into context.onChunk() -> outEl.textContent
    // src/worker.ts createStepStreamResponse(payload, request.signal):
    // Streams 8 NDJSON chunks via ReadableStream and attaches signal?.addEventListener("abort", onSignalAbort, { once: true })
    // plus ReadableStream.cancel(reason) so superseded HTTP connections clear timers immediately on the edge.
    Core Plumbing Required Today (Browser + Edge / Protocol Contract)
    // public/lib/signal-dag.js & public/app.js — topological planRecompute + AbortController NDJSON
    const plan = planRecompute({ generation, signals, cache, nodeStates, activeNodeIds }, changedKey, newValues);
    // plan.abortTargets lists in-flight dirty nodes; scheduler aborts each via AbortController("superseded")
    // Preserved sibling inputs use structuredClone + computeDependencyHash(nodeId, inputs) for cache hits
    const response = await fetch("/api/stream-step", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ nodeId, inputs: context.inputs, depHash: context.depHash, generation }),
      signal // Aborts client reader and fires Worker request.signal teardown immediately
    });
    const reader = response.body.getReader(); // Incrementally decodes NDJSON deltas into .textContent
    What disappears

    Manual debounce timers, redundant re-fetches of unchanged sibling prompt branches, and race conditions where late responses overwrite newer DOM edits.

    What becomes explicit

    Topologically ordered dependencies (DAG_SPEC), deterministic FNV-1a depHash keys, structuredClone cache snapshots, and explicit AbortController lifecycles.

    Platform gaps remaining

    The web platform lacks native reactive DOM signal primitives and dependency-aware fetch schedulers, requiring userland MutationObserver + AbortController + ReadableStream plumbing.