From df4d6d3b9dbd75acf575aedfdb6115a9a8780e16 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 24 Sep 2026 17:58:47 -0700 Subject: [PATCH 01/12] test(agent): extract durable coverage from the three parked PR stacks The CRDT-STORES-0036, human-op outbox and PM-1293 node-replacement stacks are parked to the post-refactor world rather than landing. Each solved a real problem; this carries the behavioral half of that knowledge onto main so the problems stay visible after the remote-apply refactor, and leaves the implementation half behind. Tests only. No production file is touched. - semanticLayoutSeparation.guard.test.ts: the surviving half of #18435's architecture guard -- layout lives in its own frontend-owned document and the semantic stores never reach into it. Written against mint() and plain Yjs rather than the follower adapter, so it survives the refactor. The two assertions in #18435 that described the parked design are dropped. Also characterizes, without judging it, that a minted node payload still carries pos and size verbatim. - crossWorkflowPending.test.ts: the outbox stack's one user-facing property -- an edit made while the connection is down is not silently lost. Pinned as both halves: what main does now (abandons it once the retry budget runs out) and what it should do (replays once after reconnect, under the original op_id), the latter as an expected failure. - agentNodeMaterializer.test.ts: PM-1293's invariant rather than its state machine -- a replacement under the same id keeps the node title. Widget values and links were already covered by a sibling test; the title was not. - widgetValueStore.test.ts: the stores stack's question about the user -- a remote write racing a local edit leaves the fresher value showing and clears the local-dirty mark, including when the value is unchanged. Refs #18435 #18436 #18440 #18445 #18447 #18502 #18506 #18510 #18519 #18533 #18537 #18538 #18545 #18150 --- src/stores/widgetValueStore.test.ts | 75 ++++++++++ .../agent/crdt/agentNodeMaterializer.test.ts | 37 +++++ .../agent/crdt/crossWorkflowPending.test.ts | 120 +++++++++++++++ .../semanticLayoutSeparation.guard.test.ts | 139 ++++++++++++++++++ 4 files changed, 371 insertions(+) create mode 100644 src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts diff --git a/src/stores/widgetValueStore.test.ts b/src/stores/widgetValueStore.test.ts index 4f6afde8bc7..d818dff1da9 100644 --- a/src/stores/widgetValueStore.test.ts +++ b/src/stores/widgetValueStore.test.ts @@ -757,6 +757,81 @@ describe('useWidgetValueStore', () => { expect(store.isLocallyDirty(seedA)).toBe(true) }) }) + + /** + * Extracted from the CRDT-STORES-0036 stack (PRs #18435..#18506), which is + * parked: it made the semantic stores projections of a per-root `Y.Doc`, + * deepening store-first apply rather than removing it. Its tests are almost + * all about that projection -- document key order, owner resolution, root + * membership -- and none of that is carried over. + * + * What is carried over is the question the stack answered about the user: a + * remote write and a local edit racing on one widget must not leave the user + * looking at the staler of the two. Written against the store's own public + * API rather than any document shape, so it keeps its meaning once remote + * ops apply through the graph API. + */ + describe('a remote write racing a local edit on the same widget', () => { + const remote: RemoteMutationContext = { + source: 'agent-remote', + actor: 'agent:test', + opId: 'op-1' + } + + it('shows the agent value and drops the local-dirty mark', () => { + const store = useWidgetValueStore() + const registered = store.registerWidget(seedA, state('number', 1))! + + // The user types while a frame is in flight. + registered.value = 2 + expect(store.isLocallyDirty(seedA)).toBe(true) + + expect(store.setValue(seedA, 42, remote)).toBe(true) + + expect(store.getWidget(seedA)?.value).toBe(42) + expect(store.isLocallyDirty(seedA)).toBe(false) + }) + + it('a local edit after the remote write is the fresher value and is dirty again', () => { + const store = useWidgetValueStore() + const registered = store.registerWidget(seedA, state('number', 1))! + + store.setValue(seedA, 42, remote) + registered.value = 7 + + expect(store.getWidget(seedA)?.value).toBe(7) + expect(store.isLocallyDirty(seedA)).toBe(true) + }) + + it('a remote write landing on the value already shown still clears the mark', () => { + // The short-circuit case: the user typed the same value the agent was + // about to send. The write reports nothing changed, but the mark must + // still clear, or the widget stays dirty forever against a value the + // host already agrees with. + const store = useWidgetValueStore() + const registered = store.registerWidget(seedA, state('number', 1))! + + registered.value = 42 + expect(store.isLocallyDirty(seedA)).toBe(true) + + store.setValue(seedA, 42, remote) + + expect(store.getWidget(seedA)?.value).toBe(42) + expect(store.isLocallyDirty(seedA)).toBe(false) + }) + + it('does not touch the same widget name on another graph', () => { + const store = useWidgetValueStore() + store.registerWidget(seedA, state('number', 1)) + const other = store.registerWidget(seedB, state('number', 1))! + other.value = 5 + + store.setValue(seedA, 42, remote) + + expect(store.getWidget(seedB)?.value).toBe(5) + expect(store.isLocallyDirty(seedB)).toBe(true) + }) + }) }) describe('stripGraphPrefix', () => { diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 82ca293de22..2b1d78403b1 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -703,6 +703,43 @@ describe('reconcileAgentAdapters', () => { expect(graph.serialize().nodes).toHaveLength(1) }) + /** + * Extracted from the PM-1293 node-replacement stack (PR #18150), which is + * parked. That stack modelled replacement as a state machine inside the + * materializer and its tests assert that machine's transition table -- + * an implementation that is not landing, so the table is not carried over. + * + * The invariant the machine existed to enforce is carried over: replacing + * a node under the same id must not lose what the user can see on it. The + * sibling test below already covers widget values and links; the title did + * not have an assertion, and it is the field a user is most likely to have + * set by hand. + */ + it('keeps the title across a replacement under the same id', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + const mutations = remoteMutations(scope) + mutations.addNode( + { ...nodePayload(1), title: 'Upscale pass' }, + { ...REMOTE, opId: 'op-1' } + ) + reconcileAgentAdapters(graph) + const stale = graph.getNodeById(toNodeId(1)) + expect(stale?.title).toBe('Upscale pass') + + mutations.deleteNode(toNodeId(1), [], REMOTE) + mutations.addNode( + { ...nodePayload(1), title: 'Upscale pass' }, + { ...REMOTE, opId: 'op-1-again' } + ) + + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + + const replacement = graph.getNodeById(toNodeId(1)) + expect(replacement).not.toBe(stale) + expect(replacement?.title).toBe('Upscale pass') + }) + it('runs stale-node lifecycle without clearing successor-owned state', () => { const graph = new LGraph() const scope = graphScopeOf(graph) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 97c6587e511..48750727678 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -411,3 +411,123 @@ describe('abortIfUnbound settles delivered ops as undeliverable', () => { expect(settlement?.[1].state).not.toBe('undeliverable') }) }) + +/** + * The FakeBridge's `resubscribe` is a bare vi.fn, so a test that wants the + * post-reconnect subscribe ack must play the host's part itself: mark the + * workflow subscribed again and forward the `doc_subscribed` ok frame the + * bridge would have re-emitted. + */ +function ackResubscribe(workflowId: string): void { + bridge().subscribedWorkflowId = workflowId + bridge().dispatchEvent( + new CustomEvent('doc_subscribed', { + detail: { workflowId, ok: true, seq: 0 } + }) + ) +} + +/** + * Extracted from the human-op outbox stack (PRs #18510, #18519, #18533, + * #18545), which is parked rather than landing: it was built on the + * store-first mint path the remote-apply refactor replaces. + * + * The stack's own unit tests -- the outbox's entry-state table, its + * write-through persistence structure, its session-storage TTL -- assert that + * parked design and are deliberately not carried over. What is carried over is + * the one property that is about the user rather than about the mechanism, and + * that is true of any design that solves this: an edit I made while the + * connection was down is not silently lost. + * + * Today it is. The sender's five-retry budget is a delivery budget, not a + * retention budget; once it runs out the batch settles `undeliverable` and + * nothing holds it. The two tests below pin both halves -- what happens now, + * and what should -- so the refactor can flip the second one green and the + * first one goes red in the same change. + */ +describe('a human edit made while the document connection is down', () => { + // The sender gives an in-flight batch five retries 500 ms apart before it + // retires the batch `undeliverable`. + const RETRY_BUDGET_MS = 5 * 500 + + beforeEach(() => { + useAgentPanelStore().enabled = true + bridgeState.current = null + bridgeState.transport.up = true + clientState.transportUp = true + clientState.attempts = [] + clientState.sent = [] + clientState.sendOps.mockClear() + devLogState.recordDevEvent.mockClear() + vi.useFakeTimers() + }) + + it('is abandoned once the retry budget runs out, and nothing retains it', async () => { + const { enqueue } = mountFollower('wf-a') + clientState.transportUp = false + + await enqueue([deleteNode('edited-during-outage')]) + // First send plus five retries, every one refused by the transport. + vi.advanceTimersByTime(RETRY_BUDGET_MS) + expect(clientState.attempts).toHaveLength(6) + expect(clientState.sent).toHaveLength(0) + + const operationId = clientState.attempts[0].ops[0].op_id + expect(devLogState.recordDevEvent).toHaveBeenCalledWith( + 'human_ops_settled', + expect.objectContaining({ + state: 'undeliverable', + ops: [expect.objectContaining({ op_id: operationId })] + }) + ) + + // The socket comes back and the host confirms the document is bound + // again. The edit does not go with it: it is gone. + clientState.transportUp = true + apiState.target.dispatchEvent(new Event('reconnected')) + expect(bridge().resubscribe).toHaveBeenCalledTimes(1) + ackResubscribe('wf-a') + expect(clientState.sent).toHaveLength(0) + }) + + it.fails('KNOWN GAP: reaches the host once after reconnect, under its original op_id', async () => { + const { enqueue } = mountFollower('wf-a') + clientState.transportUp = false + + await enqueue([deleteNode('edited-during-outage')]) + vi.advanceTimersByTime(RETRY_BUDGET_MS) + expect(clientState.sent).toHaveLength(0) + const operationId = clientState.attempts[0].ops[0].op_id + + // `reconnected` alone only re-drives the subscribe; nothing may go out + // until the host acks that the document is bound again. + clientState.transportUp = true + apiState.target.dispatchEvent(new Event('reconnected')) + expect(clientState.sent).toHaveLength(0) + + ackResubscribe('wf-a') + + // Delivered, toward the workflow it was minted against, carrying the id + // it was minted with. Re-minting would defeat the applier's op_id dedupe + // and let a replay apply the edit a second time. + expect(clientState.sent).toHaveLength(1) + expect(clientState.sent[0]).toMatchObject({ workflowId: 'wf-a' }) + expect(clientState.sent[0].ops[0]).toMatchObject({ + op_id: operationId, + op: 'delete_node', + node_id: 'edited-during-outage' + }) + + // And exactly once: after the host applies the replay, a later reconnect + // must not send it a third time. + dispatchOpsResult({ + workflowId: 'wf-a', + ok: true, + applied: [operationId], + skipped: [] + }) + apiState.target.dispatchEvent(new Event('reconnected')) + ackResubscribe('wf-a') + expect(clientState.sent).toHaveLength(1) + }) +}) diff --git a/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts new file mode 100644 index 00000000000..ab8d715d53e --- /dev/null +++ b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts @@ -0,0 +1,139 @@ +/** + * Layout stays out of the shared semantic document. + * + * Extracted from the architecture guard in PR #18435 (CRDT-STORES-0036), a + * stack that is parked rather than landing. The two assertions in that guard + * which described the parked design -- "the semantic stores are projections of + * a Yjs document" and "a remote update merges without a GraphMutations round + * trip" -- are deliberately not carried over: they assert an implementation + * that is not shipping. + * + * What is carried over is the boundary that guard protected, and that boundary + * is independent of how a remote op reaches the graph. Layout and view state + * live in a separate frontend-owned Yjs document; the semantic document carries + * nodes and links and nothing positional. Restated in the program's CRDT + * invariant list as KEEP-ALIVE #8, and explicitly preserved by the remote-apply + * pivot ("layout stays in its own FE-owned doc"). + * + * Deliberately written against the shared `mint()` and plain Yjs rather than + * against the follower adapter, so it keeps its meaning once remote apply moves + * onto the graph API and the reconciliation layer is deleted. + */ +import { mint } from '@comfyorg/comfy-multi-player' +import { readFileSync } from 'node:fs' +import path from 'node:path' +import { describe, expect, it } from 'vitest' +import * as Y from 'yjs' + +const SRC_DIR = path.resolve(__dirname, '../../../../') +const STORES_DIR = path.join(SRC_DIR, 'stores') +const LAYOUT_STORE = path.join( + SRC_DIR, + 'renderer/core/layout/store/layoutStore.ts' +) + +/** The three stores that hold semantic graph state. */ +const SEMANTIC_STORES = [ + 'nodeDataStore.ts', + 'linkStore.ts', + 'widgetValueStore.ts' +] as const + +const YJS_IMPORT = /from\s+['"]yjs['"]/ +const LAYOUT_STORE_IMPORT = /layout\/store\/layoutStore/ +const POSITIONAL_ROOT = /layout|viewport|position|pan|zoom/i + +function readSource(file: string): string { + return readFileSync(file, 'utf8') +} + +/** Roots a peer would actually receive, not the roots the sender declared. */ +function deliveredRoots(host: Y.Doc): string[] { + const peer = new Y.Doc() + Y.applyUpdate(peer, Y.encodeStateAsUpdate(host)) + const roots = [...peer.share.keys()] + peer.destroy() + return roots +} + +describe('layout stays out of the semantic document', () => { + it('layoutStore owns its own Yjs document', () => { + expect(readSource(LAYOUT_STORE)).toMatch(YJS_IMPORT) + }) + + it.for(SEMANTIC_STORES)('%s does not reach into layoutStore', (file) => { + expect(readSource(path.join(STORES_DIR, file))).not.toMatch( + LAYOUT_STORE_IMPORT + ) + }) + + it('a minted workflow ships nodes and links, and nothing positional', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + pos: [640, 480], + inputs: [], + outputs: [{ name: 'out', type: 'IMAGE', links: [9] }] + }, + { + id: 2, + type: 'Sink', + pos: [1200, 480], + inputs: [{ name: 'image', type: 'IMAGE', link: 9 }], + outputs: [] + } + ], + links: [[9, 1, 0, 2, 0, 'IMAGE']] + }, + { types: {} } + ) + + const roots = deliveredRoots(host) + host.destroy() + + // Yjs only ships roots that carry content, so assert the semantic roots + // arrived before concluding anything from the absence of the others. + expect(roots).toEqual(expect.arrayContaining(['nodes', 'links'])) + expect(roots.filter((root) => POSITIONAL_ROOT.test(root))).toEqual([]) + }) + + /** + * Characterization, not an aspiration. The separation above holds at the + * root level, but a minted node payload still carries `pos` and `size` + * verbatim, so geometry is present inside the semantic document even though + * `layoutStore` is what the canvas actually reads. + * + * Two defensible readings, and this test deliberately does not pick one: + * the payload is copied verbatim so a peer can place a node it has never + * seen (never re-derived from a schema), or geometry does not belong in the + * semantic document at all. If the second reading wins, this test goes red + * and the message is the reason why. + */ + it('a minted node payload still carries pos and size verbatim', () => { + const host = mint( + { + nodes: [ + { + id: 1, + type: 'Source', + pos: [640, 480], + size: [210, 60], + inputs: [], + outputs: [] + } + ], + links: [] + }, + { types: {} } + ) + + const node = host.getMap('nodes').get('1') + const payload = node instanceof Y.Map ? node.toJSON() : node + host.destroy() + + expect(payload).toMatchObject({ pos: [640, 480], size: [210, 60] }) + }) +}) From 7b51bf0771392f957f0762cb42627b6c7f8a1b26 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 25 Sep 2026 16:40:04 -0700 Subject: [PATCH 02/12] test(agent): split the outbox known-gap pin so neither gap masks the other CodeRabbit review r4104... on crossWorkflowPending.test.ts:493: it.fails passes on the first failure and abandons the rest of the body, so the delivery assertion failing meant the op_id-retention and exactly-once assertions never ran. An implementation that delivered the op but re-minted its op_id would have kept reporting as an expected failure, hiding a FORECLOSE #7 violation behind a green run. Split into two it.fails pins, one per property. Both still fail today (nothing is delivered at all), but once delivery lands each property fails on its own name instead of one masking the other. Verified: 6 passed, 2 expected fail. --- .../agent/crdt/crossWorkflowPending.test.ts | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 48750727678..bdef76b9606 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -490,14 +490,27 @@ describe('a human edit made while the document connection is down', () => { expect(clientState.sent).toHaveLength(0) }) - it.fails('KNOWN GAP: reaches the host once after reconnect, under its original op_id', async () => { + /** + * These two gaps are split rather than asserted in one `it.fails`, because + * `it.fails` passes on the FIRST failure and abandons the rest of the body. + * Bundled, the delivery assertion below fails while `clientState.sent` is + * still empty, so the `op_id`-retention and exactly-once assertions never + * execute -- and an implementation that delivers the op but re-mints its + * `op_id` would still report as an expected failure, hiding a FORECLOSE #7 + * violation behind a green run. + * + * Both remain `it.fails` today: nothing is delivered at all, so the second + * test cannot reach its own subject either. The point of the split is that + * once delivery lands, each property fails -- and gets fixed -- on its own + * name instead of one masking the other. + */ + it.fails('KNOWN GAP: reaches the host after reconnect', async () => { const { enqueue } = mountFollower('wf-a') clientState.transportUp = false await enqueue([deleteNode('edited-during-outage')]) vi.advanceTimersByTime(RETRY_BUDGET_MS) expect(clientState.sent).toHaveLength(0) - const operationId = clientState.attempts[0].ops[0].op_id // `reconnected` alone only re-drives the subscribe; nothing may go out // until the host acks that the document is bound again. @@ -507,11 +520,25 @@ describe('a human edit made while the document connection is down', () => { ackResubscribe('wf-a') - // Delivered, toward the workflow it was minted against, carrying the id - // it was minted with. Re-minting would defeat the applier's op_id dedupe - // and let a replay apply the edit a second time. + // Delivered, toward the workflow it was minted against. expect(clientState.sent).toHaveLength(1) expect(clientState.sent[0]).toMatchObject({ workflowId: 'wf-a' }) + }) + + it.fails('KNOWN GAP: replays under its original op_id, exactly once', async () => { + const { enqueue } = mountFollower('wf-a') + clientState.transportUp = false + + await enqueue([deleteNode('edited-during-outage')]) + vi.advanceTimersByTime(RETRY_BUDGET_MS) + const operationId = clientState.attempts[0].ops[0].op_id + + clientState.transportUp = true + apiState.target.dispatchEvent(new Event('reconnected')) + ackResubscribe('wf-a') + + // Carrying the id it was minted with. Re-minting would defeat the + // applier's op_id dedupe and let a replay apply the edit a second time. expect(clientState.sent[0].ops[0]).toMatchObject({ op_id: operationId, op: 'delete_node', From 9ec558130f3479c566baf5e2b2477f9852b7a362 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 25 Sep 2026 20:30:14 -0700 Subject: [PATCH 03/12] test(agent): stop three pins passing for the wrong reason MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three assertions that could each report green while the property they name was broken. - `crossWorkflowPending`: the pre-ack invariants sat inside the `it.fails` for delivery. `it.fails` passes on the FIRST failure, so a regression that transmitted before the host acked would have been reported as the expected failure and the delivery assertion would never have run — the exact masking this file's own block comment splits tests to avoid. Moved into a passing test; the `it.fails` now holds only its subject. - `semanticLayoutSeparation`: `POSITIONAL_ROOT` matched `position` but not a root named `pos` or `size`, which are the exact keys a node payload carries — the likeliest shape for geometry to reach a peer, and the one KEEP-ALIVE #8 is about. Both now match as whole names. - `widgetValueStore`: the cross-graph isolation test asserted only that the OTHER graph was untouched, so a `setValue` that silently did nothing satisfied it. Confirm the write landed first. Verified: 69 passed | 2 expected fail. --- src/stores/widgetValueStore.test.ts | 5 ++++ .../agent/crdt/crossWorkflowPending.test.ts | 24 +++++++++++++++++-- .../semanticLayoutSeparation.guard.test.ts | 9 ++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/stores/widgetValueStore.test.ts b/src/stores/widgetValueStore.test.ts index d818dff1da9..d831b50425a 100644 --- a/src/stores/widgetValueStore.test.ts +++ b/src/stores/widgetValueStore.test.ts @@ -828,6 +828,11 @@ describe('useWidgetValueStore', () => { store.setValue(seedA, 42, remote) + // Confirm the write landed before concluding anything from the other + // graph being untouched: a `setValue` that silently did nothing would + // satisfy the isolation assertions below without isolating anything. + expect(store.getWidget(seedA)?.value).toBe(42) + expect(store.getWidget(seedB)?.value).toBe(5) expect(store.isLocallyDirty(seedB)).toBe(true) }) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index bdef76b9606..5624170950c 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -504,7 +504,16 @@ describe('a human edit made while the document connection is down', () => { * once delivery lands, each property fails -- and gets fixed -- on its own * name instead of one masking the other. */ - it.fails('KNOWN GAP: reaches the host after reconnect', async () => { + /** + * The holding half of the same story, and it belongs in a PASSING test for + * the reason the block comment above gives. Both assertions here describe + * behaviour that works today, so inside the `it.fails` below they would be + * indistinguishable from the gap it pins: a regression that transmitted + * before the ack would fail, `it.fails` would report that as the expected + * failure, and the delivery assertion would never run. Held separately, a + * regression in either half is unmissable. + */ + it('holds the queue until the host acks the resubscribe', async () => { const { enqueue } = mountFollower('wf-a') clientState.transportUp = false @@ -517,10 +526,21 @@ describe('a human edit made while the document connection is down', () => { clientState.transportUp = true apiState.target.dispatchEvent(new Event('reconnected')) expect(clientState.sent).toHaveLength(0) + }) + it.fails('KNOWN GAP: reaches the host after reconnect', async () => { + const { enqueue } = mountFollower('wf-a') + clientState.transportUp = false + + await enqueue([deleteNode('edited-during-outage')]) + vi.advanceTimersByTime(RETRY_BUDGET_MS) + + clientState.transportUp = true + apiState.target.dispatchEvent(new Event('reconnected')) ackResubscribe('wf-a') - // Delivered, toward the workflow it was minted against. + // Delivered, toward the workflow it was minted against. The ONLY + // assertion in this body, so this is the property that fails. expect(clientState.sent).toHaveLength(1) expect(clientState.sent[0]).toMatchObject({ workflowId: 'wf-a' }) }) diff --git a/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts index ab8d715d53e..636a57ab87f 100644 --- a/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts +++ b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts @@ -41,7 +41,14 @@ const SEMANTIC_STORES = [ const YJS_IMPORT = /from\s+['"]yjs['"]/ const LAYOUT_STORE_IMPORT = /layout\/store\/layoutStore/ -const POSITIONAL_ROOT = /layout|viewport|position|pan|zoom/i +/** + * Root names that describe where or how big something is. `pos` and `size` + * are matched as whole names as well as through the longer words that contain + * them: those two are the exact keys a node payload carries, so they are the + * most likely shape for geometry to reach a peer under, and the substring + * alternatives below do not match either on its own. + */ +const POSITIONAL_ROOT = /layout|viewport|position|pan|zoom|^(?:pos|size)$/i function readSource(file: string): string { return readFileSync(file, 'utf8') From 35aa724100a8dfe5136892f1d004a9f0c785734b Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 25 Sep 2026 22:36:16 -0700 Subject: [PATCH 04/12] test(agent): stop two more pins passing for the wrong reason Both findings from the CodeRabbit review, both legitimate, and both the same defect this PR is named after. `holds the queue until the host acks the resubscribe` advanced the full retry budget before reconnecting. Past the budget the sender has already settled the batch `undeliverable` and dropped it -- the test directly above asserts exactly that -- so `sent` was empty because nothing was left to send, not because the queue waited for the ack. An implementation that flushed the moment `reconnected` fired would have passed. Reconnect at three of the six allowed attempts instead, with the batch provably still live: assert the attempt count and that no `undeliverable` settle has been recorded, then check the pre-ack hold. Verified by mutation: restoring the old timing under the new liveness assertions fails, which is the claim. The remaining limit is stated in the test rather than papered over. Nothing is delivered after the ack either -- the `it.fails` below is that gap -- so an empty `sent` is not yet proof the ack is what releases the queue. What is checkable today is that `reconnected` alone does not flush a live batch. `layoutStore owns its own Yjs document` asserted that layoutStore.ts imports yjs. That shows it has *a* document; it cannot show the document is a different one, which is the property KEEP-ALIVE #8 is actually about -- the same assert-the-claim-not-a-proxy gap the program's own invariant list flags. Added a behavioural test that drives a real `createNode` through `layoutStore` and requires the bytes a semantic peer would receive to be unchanged. Byte equality rather than a root-name check, because a leak into an existing semantic root would add no new root name. Geometry chosen not to collide with the minted payload. Verified by mutation: a stray write into the semantic doc fails it. The two renderer imports take the same eslint-disable that AgentPanelRoot.vue already takes for the same two modules -- a test of that boundary has to reach the store the boundary is about. --- .../agent/crdt/crossWorkflowPending.test.ts | 31 +++++++++- .../semanticLayoutSeparation.guard.test.ts | 61 +++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 5624170950c..425644cb1b6 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -447,8 +447,10 @@ function ackResubscribe(workflowId: string): void { */ describe('a human edit made while the document connection is down', () => { // The sender gives an in-flight batch five retries 500 ms apart before it - // retires the batch `undeliverable`. - const RETRY_BUDGET_MS = 5 * 500 + // retires the batch `undeliverable`. Named separately because one test has to + // stop part-way through the budget, while the batch is still retained. + const RETRY_INTERVAL_MS = 500 + const RETRY_BUDGET_MS = 5 * RETRY_INTERVAL_MS beforeEach(() => { useAgentPanelStore().enabled = true @@ -512,19 +514,42 @@ describe('a human edit made while the document connection is down', () => { * before the ack would fail, `it.fails` would report that as the expected * failure, and the delivery assertion would never run. Held separately, a * regression in either half is unmissable. + * + * This test must reconnect while the batch is still PENDING, and that is the + * whole reason the timer only advances part of the budget. Past the budget + * the sender has already settled the batch `undeliverable` and dropped it -- + * which is exactly what the test above asserts -- so `sent` would then be + * empty because there is nothing left to send, not because the queue waited + * for the ack. An implementation that flushed the moment `reconnected` fired + * would have passed. With the batch still alive that explanation is gone. + * + * One limit, stated rather than papered over: because nothing is delivered + * after the ack either -- the `it.fails` below is exactly that gap -- an empty + * `sent` is not yet proof that the ack is what releases the queue. What this + * test does establish is the half that is checkable today, that a live batch + * is not flushed by `reconnected` alone. When the delivery gap closes, assert + * the batch goes out after `ackResubscribe` here and the pair is complete. */ it('holds the queue until the host acks the resubscribe', async () => { const { enqueue } = mountFollower('wf-a') clientState.transportUp = false await enqueue([deleteNode('edited-during-outage')]) - vi.advanceTimersByTime(RETRY_BUDGET_MS) + // First send plus two refused retries: three of the six attempts the + // budget allows, so the batch is still in flight and still retained. + vi.advanceTimersByTime(2 * RETRY_INTERVAL_MS) + expect(clientState.attempts).toHaveLength(3) + expect(devLogState.recordDevEvent).not.toHaveBeenCalledWith( + 'human_ops_settled', + expect.objectContaining({ state: 'undeliverable' }) + ) expect(clientState.sent).toHaveLength(0) // `reconnected` alone only re-drives the subscribe; nothing may go out // until the host acks that the document is bound again. clientState.transportUp = true apiState.target.dispatchEvent(new Event('reconnected')) + expect(bridge().resubscribe).toHaveBeenCalledTimes(1) expect(clientState.sent).toHaveLength(0) }) diff --git a/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts index 636a57ab87f..5abb0cad476 100644 --- a/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts +++ b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts @@ -25,6 +25,17 @@ import path from 'node:path' import { describe, expect, it } from 'vitest' import * as Y from 'yjs' +// The layer rule forbids workbench/ -> renderer/, and the two lines below are +// the same exception `AgentPanelRoot.vue` already takes for the same two +// modules: the agent's layout path legitimately drives the renderer's layout +// store. A test of that boundary has to reach the store the boundary is about. +// eslint-disable-next-line import-x/no-restricted-paths +import { layoutStore } from '@/renderer/core/layout/store/layoutStore' +// eslint-disable-next-line import-x/no-restricted-paths +import { LayoutSource } from '@/renderer/core/layout/types' +import { toNodeId } from '@/types/nodeId' +import { createUuidv4 } from '@/utils/uuid' + const SRC_DIR = path.resolve(__dirname, '../../../../') const STORES_DIR = path.join(SRC_DIR, 'stores') const LAYOUT_STORE = path.join( @@ -64,6 +75,56 @@ function deliveredRoots(host: Y.Doc): string[] { } describe('layout stays out of the semantic document', () => { + /** + * The behavioural half of "layoutStore owns its own document". Asserting that + * `layoutStore.ts` imports yjs only shows it has *a* document; it cannot show + * the document is a different one, which is the property KEEP-ALIVE #8 is + * about. So drive a real layout mutation through the store and require the + * bytes a semantic peer would receive to be unchanged by it. + * + * The geometry below is deliberately unlike anything in the minted payload: + * if the numbers collided, an implementation that did leak layout into the + * semantic document could still produce an identical update. + */ + it('a layout mutation changes nothing a semantic peer receives', () => { + const host = mint( + { + nodes: [ + { id: 1, type: 'Source', pos: [0, 0], inputs: [], outputs: [] } + ], + links: [] + }, + { types: {} } + ) + const before = Y.encodeStateAsUpdate(host) + + const nodeId = toNodeId('layout-only-node') + const position = { x: 7331, y: 1337 } + const size = { width: 313, height: 131 } + layoutStore.applyOperation({ + type: 'createNode', + graphId: createUuidv4(), + nodeId, + layout: { + id: nodeId, + position, + size, + bounds: { x: position.x, y: position.y, ...size }, + zIndex: 0, + visible: true + }, + source: LayoutSource.Canvas, + timestamp: 0 + }) + + const after = Y.encodeStateAsUpdate(host) + host.destroy() + + // Byte equality rather than a root-name check: a leak that wrote into an + // existing semantic root would add no new root name at all. + expect(after).toEqual(before) + }) + it('layoutStore owns its own Yjs document', () => { expect(readSource(LAYOUT_STORE)).toMatch(YJS_IMPORT) }) From 8426200d23f83485e4aee34bb8b63a9ef221fd79 Mon Sep 17 00:00:00 2001 From: Amp Date: Sun, 27 Sep 2026 02:49:51 +0000 Subject: [PATCH 05/12] test(agent): keep extracted coverage durable Amp-Thread-ID: https://ampcode.com/threads/T-01a0e09e-980c-73d8-a42a-17dbc2a5f472 --- src/stores/widgetValueStore.test.ts | 35 +--- .../agent/crdt/agentNodeMaterializer.test.ts | 37 ---- .../agent/crdt/crossWorkflowPending.test.ts | 124 +------------- .../semanticLayoutSeparation.guard.test.ts | 158 ++---------------- 4 files changed, 26 insertions(+), 328 deletions(-) diff --git a/src/stores/widgetValueStore.test.ts b/src/stores/widgetValueStore.test.ts index d831b50425a..275dc6bedae 100644 --- a/src/stores/widgetValueStore.test.ts +++ b/src/stores/widgetValueStore.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from 'vitest' +import { assert, describe, expect, it, vi } from 'vitest' import type { UUID } from '@/utils/uuid' import type { RemoteMutationContext } from '@/types/graphMutationContext' @@ -758,19 +758,6 @@ describe('useWidgetValueStore', () => { }) }) - /** - * Extracted from the CRDT-STORES-0036 stack (PRs #18435..#18506), which is - * parked: it made the semantic stores projections of a per-root `Y.Doc`, - * deepening store-first apply rather than removing it. Its tests are almost - * all about that projection -- document key order, owner resolution, root - * membership -- and none of that is carried over. - * - * What is carried over is the question the stack answered about the user: a - * remote write and a local edit racing on one widget must not leave the user - * looking at the staler of the two. Written against the store's own public - * API rather than any document shape, so it keeps its meaning once remote - * ops apply through the graph API. - */ describe('a remote write racing a local edit on the same widget', () => { const remote: RemoteMutationContext = { source: 'agent-remote', @@ -780,9 +767,9 @@ describe('useWidgetValueStore', () => { it('shows the agent value and drops the local-dirty mark', () => { const store = useWidgetValueStore() - const registered = store.registerWidget(seedA, state('number', 1))! + const registered = store.registerWidget(seedA, state('number', 1)) + assert.exists(registered) - // The user types while a frame is in flight. registered.value = 2 expect(store.isLocallyDirty(seedA)).toBe(true) @@ -794,7 +781,8 @@ describe('useWidgetValueStore', () => { it('a local edit after the remote write is the fresher value and is dirty again', () => { const store = useWidgetValueStore() - const registered = store.registerWidget(seedA, state('number', 1))! + const registered = store.registerWidget(seedA, state('number', 1)) + assert.exists(registered) store.setValue(seedA, 42, remote) registered.value = 7 @@ -804,12 +792,9 @@ describe('useWidgetValueStore', () => { }) it('a remote write landing on the value already shown still clears the mark', () => { - // The short-circuit case: the user typed the same value the agent was - // about to send. The write reports nothing changed, but the mark must - // still clear, or the widget stays dirty forever against a value the - // host already agrees with. const store = useWidgetValueStore() - const registered = store.registerWidget(seedA, state('number', 1))! + const registered = store.registerWidget(seedA, state('number', 1)) + assert.exists(registered) registered.value = 42 expect(store.isLocallyDirty(seedA)).toBe(true) @@ -823,14 +808,12 @@ describe('useWidgetValueStore', () => { it('does not touch the same widget name on another graph', () => { const store = useWidgetValueStore() store.registerWidget(seedA, state('number', 1)) - const other = store.registerWidget(seedB, state('number', 1))! + const other = store.registerWidget(seedB, state('number', 1)) + assert.exists(other) other.value = 5 store.setValue(seedA, 42, remote) - // Confirm the write landed before concluding anything from the other - // graph being untouched: a `setValue` that silently did nothing would - // satisfy the isolation assertions below without isolating anything. expect(store.getWidget(seedA)?.value).toBe(42) expect(store.getWidget(seedB)?.value).toBe(5) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 2b1d78403b1..82ca293de22 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -703,43 +703,6 @@ describe('reconcileAgentAdapters', () => { expect(graph.serialize().nodes).toHaveLength(1) }) - /** - * Extracted from the PM-1293 node-replacement stack (PR #18150), which is - * parked. That stack modelled replacement as a state machine inside the - * materializer and its tests assert that machine's transition table -- - * an implementation that is not landing, so the table is not carried over. - * - * The invariant the machine existed to enforce is carried over: replacing - * a node under the same id must not lose what the user can see on it. The - * sibling test below already covers widget values and links; the title did - * not have an assertion, and it is the field a user is most likely to have - * set by hand. - */ - it('keeps the title across a replacement under the same id', () => { - const graph = new LGraph() - const scope = graphScopeOf(graph) - const mutations = remoteMutations(scope) - mutations.addNode( - { ...nodePayload(1), title: 'Upscale pass' }, - { ...REMOTE, opId: 'op-1' } - ) - reconcileAgentAdapters(graph) - const stale = graph.getNodeById(toNodeId(1)) - expect(stale?.title).toBe('Upscale pass') - - mutations.deleteNode(toNodeId(1), [], REMOTE) - mutations.addNode( - { ...nodePayload(1), title: 'Upscale pass' }, - { ...REMOTE, opId: 'op-1-again' } - ) - - expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) - - const replacement = graph.getNodeById(toNodeId(1)) - expect(replacement).not.toBe(stale) - expect(replacement?.title).toBe('Upscale pass') - }) - it('runs stale-node lifecycle without clearing successor-owned state', () => { const graph = new LGraph() const scope = graphScopeOf(graph) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 425644cb1b6..98b52c5babc 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -412,12 +412,6 @@ describe('abortIfUnbound settles delivered ops as undeliverable', () => { }) }) -/** - * The FakeBridge's `resubscribe` is a bare vi.fn, so a test that wants the - * post-reconnect subscribe ack must play the host's part itself: mark the - * workflow subscribed again and forward the `doc_subscribed` ok frame the - * bridge would have re-emitted. - */ function ackResubscribe(workflowId: string): void { bridge().subscribedWorkflowId = workflowId bridge().dispatchEvent( @@ -427,28 +421,7 @@ function ackResubscribe(workflowId: string): void { ) } -/** - * Extracted from the human-op outbox stack (PRs #18510, #18519, #18533, - * #18545), which is parked rather than landing: it was built on the - * store-first mint path the remote-apply refactor replaces. - * - * The stack's own unit tests -- the outbox's entry-state table, its - * write-through persistence structure, its session-storage TTL -- assert that - * parked design and are deliberately not carried over. What is carried over is - * the one property that is about the user rather than about the mechanism, and - * that is true of any design that solves this: an edit I made while the - * connection was down is not silently lost. - * - * Today it is. The sender's five-retry budget is a delivery budget, not a - * retention budget; once it runs out the batch settles `undeliverable` and - * nothing holds it. The two tests below pin both halves -- what happens now, - * and what should -- so the refactor can flip the second one green and the - * first one goes red in the same change. - */ describe('a human edit made while the document connection is down', () => { - // The sender gives an in-flight batch five retries 500 ms apart before it - // retires the batch `undeliverable`. Named separately because one test has to - // stop part-way through the budget, while the batch is still retained. const RETRY_INTERVAL_MS = 500 const RETRY_BUDGET_MS = 5 * RETRY_INTERVAL_MS @@ -469,7 +442,6 @@ describe('a human edit made while the document connection is down', () => { clientState.transportUp = false await enqueue([deleteNode('edited-during-outage')]) - // First send plus five retries, every one refused by the transport. vi.advanceTimersByTime(RETRY_BUDGET_MS) expect(clientState.attempts).toHaveLength(6) expect(clientState.sent).toHaveLength(0) @@ -483,8 +455,6 @@ describe('a human edit made while the document connection is down', () => { }) ) - // The socket comes back and the host confirms the document is bound - // again. The edit does not go with it: it is gone. clientState.transportUp = true apiState.target.dispatchEvent(new Event('reconnected')) expect(bridge().resubscribe).toHaveBeenCalledTimes(1) @@ -492,51 +462,11 @@ describe('a human edit made while the document connection is down', () => { expect(clientState.sent).toHaveLength(0) }) - /** - * These two gaps are split rather than asserted in one `it.fails`, because - * `it.fails` passes on the FIRST failure and abandons the rest of the body. - * Bundled, the delivery assertion below fails while `clientState.sent` is - * still empty, so the `op_id`-retention and exactly-once assertions never - * execute -- and an implementation that delivers the op but re-mints its - * `op_id` would still report as an expected failure, hiding a FORECLOSE #7 - * violation behind a green run. - * - * Both remain `it.fails` today: nothing is delivered at all, so the second - * test cannot reach its own subject either. The point of the split is that - * once delivery lands, each property fails -- and gets fixed -- on its own - * name instead of one masking the other. - */ - /** - * The holding half of the same story, and it belongs in a PASSING test for - * the reason the block comment above gives. Both assertions here describe - * behaviour that works today, so inside the `it.fails` below they would be - * indistinguishable from the gap it pins: a regression that transmitted - * before the ack would fail, `it.fails` would report that as the expected - * failure, and the delivery assertion would never run. Held separately, a - * regression in either half is unmissable. - * - * This test must reconnect while the batch is still PENDING, and that is the - * whole reason the timer only advances part of the budget. Past the budget - * the sender has already settled the batch `undeliverable` and dropped it -- - * which is exactly what the test above asserts -- so `sent` would then be - * empty because there is nothing left to send, not because the queue waited - * for the ack. An implementation that flushed the moment `reconnected` fired - * would have passed. With the batch still alive that explanation is gone. - * - * One limit, stated rather than papered over: because nothing is delivered - * after the ack either -- the `it.fails` below is exactly that gap -- an empty - * `sent` is not yet proof that the ack is what releases the queue. What this - * test does establish is the half that is checkable today, that a live batch - * is not flushed by `reconnected` alone. When the delivery gap closes, assert - * the batch goes out after `ackResubscribe` here and the pair is complete. - */ - it('holds the queue until the host acks the resubscribe', async () => { + it('does not flush a pending batch on reconnect before the resubscribe ack', async () => { const { enqueue } = mountFollower('wf-a') clientState.transportUp = false await enqueue([deleteNode('edited-during-outage')]) - // First send plus two refused retries: three of the six attempts the - // budget allows, so the batch is still in flight and still retained. vi.advanceTimersByTime(2 * RETRY_INTERVAL_MS) expect(clientState.attempts).toHaveLength(3) expect(devLogState.recordDevEvent).not.toHaveBeenCalledWith( @@ -545,61 +475,9 @@ describe('a human edit made while the document connection is down', () => { ) expect(clientState.sent).toHaveLength(0) - // `reconnected` alone only re-drives the subscribe; nothing may go out - // until the host acks that the document is bound again. clientState.transportUp = true apiState.target.dispatchEvent(new Event('reconnected')) expect(bridge().resubscribe).toHaveBeenCalledTimes(1) expect(clientState.sent).toHaveLength(0) }) - - it.fails('KNOWN GAP: reaches the host after reconnect', async () => { - const { enqueue } = mountFollower('wf-a') - clientState.transportUp = false - - await enqueue([deleteNode('edited-during-outage')]) - vi.advanceTimersByTime(RETRY_BUDGET_MS) - - clientState.transportUp = true - apiState.target.dispatchEvent(new Event('reconnected')) - ackResubscribe('wf-a') - - // Delivered, toward the workflow it was minted against. The ONLY - // assertion in this body, so this is the property that fails. - expect(clientState.sent).toHaveLength(1) - expect(clientState.sent[0]).toMatchObject({ workflowId: 'wf-a' }) - }) - - it.fails('KNOWN GAP: replays under its original op_id, exactly once', async () => { - const { enqueue } = mountFollower('wf-a') - clientState.transportUp = false - - await enqueue([deleteNode('edited-during-outage')]) - vi.advanceTimersByTime(RETRY_BUDGET_MS) - const operationId = clientState.attempts[0].ops[0].op_id - - clientState.transportUp = true - apiState.target.dispatchEvent(new Event('reconnected')) - ackResubscribe('wf-a') - - // Carrying the id it was minted with. Re-minting would defeat the - // applier's op_id dedupe and let a replay apply the edit a second time. - expect(clientState.sent[0].ops[0]).toMatchObject({ - op_id: operationId, - op: 'delete_node', - node_id: 'edited-during-outage' - }) - - // And exactly once: after the host applies the replay, a later reconnect - // must not send it a third time. - dispatchOpsResult({ - workflowId: 'wf-a', - ok: true, - applied: [operationId], - skipped: [] - }) - apiState.target.dispatchEvent(new Event('reconnected')) - ackResubscribe('wf-a') - expect(clientState.sent).toHaveLength(1) - }) }) diff --git a/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts index 5abb0cad476..40585ca8e45 100644 --- a/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts +++ b/src/workbench/extensions/agent/crdt/semanticLayoutSeparation.guard.test.ts @@ -1,71 +1,17 @@ -/** - * Layout stays out of the shared semantic document. - * - * Extracted from the architecture guard in PR #18435 (CRDT-STORES-0036), a - * stack that is parked rather than landing. The two assertions in that guard - * which described the parked design -- "the semantic stores are projections of - * a Yjs document" and "a remote update merges without a GraphMutations round - * trip" -- are deliberately not carried over: they assert an implementation - * that is not shipping. - * - * What is carried over is the boundary that guard protected, and that boundary - * is independent of how a remote op reaches the graph. Layout and view state - * live in a separate frontend-owned Yjs document; the semantic document carries - * nodes and links and nothing positional. Restated in the program's CRDT - * invariant list as KEEP-ALIVE #8, and explicitly preserved by the remote-apply - * pivot ("layout stays in its own FE-owned doc"). - * - * Deliberately written against the shared `mint()` and plain Yjs rather than - * against the follower adapter, so it keeps its meaning once remote apply moves - * onto the graph API and the reconciliation layer is deleted. - */ import { mint } from '@comfyorg/comfy-multi-player' -import { readFileSync } from 'node:fs' -import path from 'node:path' import { describe, expect, it } from 'vitest' import * as Y from 'yjs' -// The layer rule forbids workbench/ -> renderer/, and the two lines below are -// the same exception `AgentPanelRoot.vue` already takes for the same two -// modules: the agent's layout path legitimately drives the renderer's layout -// store. A test of that boundary has to reach the store the boundary is about. -// eslint-disable-next-line import-x/no-restricted-paths -import { layoutStore } from '@/renderer/core/layout/store/layoutStore' -// eslint-disable-next-line import-x/no-restricted-paths -import { LayoutSource } from '@/renderer/core/layout/types' -import { toNodeId } from '@/types/nodeId' -import { createUuidv4 } from '@/utils/uuid' +const POSITIONAL_ROOTS = [ + 'layout', + 'viewport', + 'position', + 'pan', + 'zoom', + 'pos', + 'size' +] -const SRC_DIR = path.resolve(__dirname, '../../../../') -const STORES_DIR = path.join(SRC_DIR, 'stores') -const LAYOUT_STORE = path.join( - SRC_DIR, - 'renderer/core/layout/store/layoutStore.ts' -) - -/** The three stores that hold semantic graph state. */ -const SEMANTIC_STORES = [ - 'nodeDataStore.ts', - 'linkStore.ts', - 'widgetValueStore.ts' -] as const - -const YJS_IMPORT = /from\s+['"]yjs['"]/ -const LAYOUT_STORE_IMPORT = /layout\/store\/layoutStore/ -/** - * Root names that describe where or how big something is. `pos` and `size` - * are matched as whole names as well as through the longer words that contain - * them: those two are the exact keys a node payload carries, so they are the - * most likely shape for geometry to reach a peer under, and the substring - * alternatives below do not match either on its own. - */ -const POSITIONAL_ROOT = /layout|viewport|position|pan|zoom|^(?:pos|size)$/i - -function readSource(file: string): string { - return readFileSync(file, 'utf8') -} - -/** Roots a peer would actually receive, not the roots the sender declared. */ function deliveredRoots(host: Y.Doc): string[] { const peer = new Y.Doc() Y.applyUpdate(peer, Y.encodeStateAsUpdate(host)) @@ -75,67 +21,7 @@ function deliveredRoots(host: Y.Doc): string[] { } describe('layout stays out of the semantic document', () => { - /** - * The behavioural half of "layoutStore owns its own document". Asserting that - * `layoutStore.ts` imports yjs only shows it has *a* document; it cannot show - * the document is a different one, which is the property KEEP-ALIVE #8 is - * about. So drive a real layout mutation through the store and require the - * bytes a semantic peer would receive to be unchanged by it. - * - * The geometry below is deliberately unlike anything in the minted payload: - * if the numbers collided, an implementation that did leak layout into the - * semantic document could still produce an identical update. - */ - it('a layout mutation changes nothing a semantic peer receives', () => { - const host = mint( - { - nodes: [ - { id: 1, type: 'Source', pos: [0, 0], inputs: [], outputs: [] } - ], - links: [] - }, - { types: {} } - ) - const before = Y.encodeStateAsUpdate(host) - - const nodeId = toNodeId('layout-only-node') - const position = { x: 7331, y: 1337 } - const size = { width: 313, height: 131 } - layoutStore.applyOperation({ - type: 'createNode', - graphId: createUuidv4(), - nodeId, - layout: { - id: nodeId, - position, - size, - bounds: { x: position.x, y: position.y, ...size }, - zIndex: 0, - visible: true - }, - source: LayoutSource.Canvas, - timestamp: 0 - }) - - const after = Y.encodeStateAsUpdate(host) - host.destroy() - - // Byte equality rather than a root-name check: a leak that wrote into an - // existing semantic root would add no new root name at all. - expect(after).toEqual(before) - }) - - it('layoutStore owns its own Yjs document', () => { - expect(readSource(LAYOUT_STORE)).toMatch(YJS_IMPORT) - }) - - it.for(SEMANTIC_STORES)('%s does not reach into layoutStore', (file) => { - expect(readSource(path.join(STORES_DIR, file))).not.toMatch( - LAYOUT_STORE_IMPORT - ) - }) - - it('a minted workflow ships nodes and links, and nothing positional', () => { + it('ships nodes and links without known positional roots', () => { const host = mint( { nodes: [ @@ -162,25 +48,11 @@ describe('layout stays out of the semantic document', () => { const roots = deliveredRoots(host) host.destroy() - // Yjs only ships roots that carry content, so assert the semantic roots - // arrived before concluding anything from the absence of the others. expect(roots).toEqual(expect.arrayContaining(['nodes', 'links'])) - expect(roots.filter((root) => POSITIONAL_ROOT.test(root))).toEqual([]) + expect(roots.filter((root) => POSITIONAL_ROOTS.includes(root))).toEqual([]) }) - /** - * Characterization, not an aspiration. The separation above holds at the - * root level, but a minted node payload still carries `pos` and `size` - * verbatim, so geometry is present inside the semantic document even though - * `layoutStore` is what the canvas actually reads. - * - * Two defensible readings, and this test deliberately does not pick one: - * the payload is copied verbatim so a peer can place a node it has never - * seen (never re-derived from a schema), or geometry does not belong in the - * semantic document at all. If the second reading wins, this test goes red - * and the message is the reason why. - */ - it('a minted node payload still carries pos and size verbatim', () => { + it.fails('omits pos and size from a minted node payload', () => { const host = mint( { nodes: [ @@ -199,9 +71,11 @@ describe('layout stays out of the semantic document', () => { ) const node = host.getMap('nodes').get('1') - const payload = node instanceof Y.Map ? node.toJSON() : node + const payload = node instanceof Y.Map ? node.toJSON() : {} host.destroy() - expect(payload).toMatchObject({ pos: [640, 480], size: [210, 60] }) + expect( + Object.keys(payload).filter((key) => key === 'pos' || key === 'size') + ).toEqual([]) }) }) From d03d9e5314a9b80979e6a241aa7f707e7fe9d608 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 26 Sep 2026 20:06:11 -0700 Subject: [PATCH 06/12] test(agent): restore the replay pins under the ruling, and split them three ways Christian, 2026-09-26: "yes we definitely want queued and replay edits. edits while connection is down should definitely still be pushed eventually. if adrs are saying otherwise, change the adrs as well." That reverses the premise of the P1 that removed these pins. Queue-and-replay is wanted product behaviour, so the `it.fails` pins are pinning a real gap, not arguing with a decision -- and the recorded clause they collide with is the thing that changes. Exactly one sentence collides: CRDT-AUTHORITY-0035 decision 3, line 67, "The frontend does not persist or re-send operations dropped as `undeliverable`". It is `Status: Proposed` and was agent-authored in #18064. An amendment is being drafted for ratification; it is not landed here. CRDT-WRITE-0035 needs no reversal and is honoured rather than amended. It already holds a delete minted before a tab switch and delivers it on return, which is the same direction. What it rejects is narrower than it reads: "a retry queue that resends undeliverable ops when the workflow rebinds", because that "duplicates the sender's ordering and never-re-mint rules outside the module that owns them". That is a placement objection protecting FORECLOSE #7 (never regenerate op_id on retry), not a product one. So the change this pins is small: the sender's five retries 500 ms apart are a DELIVERY budget where the ruling wants a RETENTION budget. Stop `undeliverable` being terminal, inside opSender. Do not add a queue beside it. Split three ways, one property per name, because `it.fails` passes on the FIRST failing assertion and abandons the body. That was DrJKL's finding 1, confirmed still open in his second pass: bundled, an implementation that delivered the op while re-minting its `op_id` reported as an expected failure and hid a FORECLOSE #7 violation. Proven fixed by mutation -- a build that retains the batch and replays it with re-minted ids flips the delivery pin and the exactly-once pin to "expected failure but passed" while the id-retention pin stays red, which is the discrimination the bundled version could not make. The same mutation correctly reddens the characterization test above them, so retention landing turns that one red and the pins green in one change; its docstring now says so, since it describes today's defect and not a desired property. Scope stated on the describe, because the pins are narrower than the product property: all three live inside one page session -- socket drop, budget exhaustion, socket return. None asserts survival across a RELOAD, which needs persistence and is what the parked human-op outbox stack was built for. That distinction is a design question, not something these pins settle. Also finding 3, fixed rather than deleted. `keeps the title across a replacement under the same id` supplied `title: 'Upscale pass'` on both the incumbent and the replacement, so it could not tell preservation from overwriting. Making the incumbent a hand-typed rename turns it red: the replacement payload's title wins and the user's name is lost. So it is restored as `it.fails`, joining the two `keeps a live rename ...` pins already in this file under different triggers -- this is the same-id replacement trigger the parked PM-1293 stack existed to fix. --- .../agent/crdt/agentNodeMaterializer.test.ts | 51 +++++++ .../agent/crdt/crossWorkflowPending.test.ts | 131 ++++++++++++++++++ 2 files changed, 182 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 82ca293de22..6cdffa220c1 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -703,6 +703,57 @@ describe('reconcileAgentAdapters', () => { expect(graph.serialize().nodes).toHaveLength(1) }) + /** + * Extracted from the PM-1293 node-replacement stack (PR #18150), which is + * parked. That stack modelled replacement as a state machine inside the + * materializer and its tests assert that machine's transition table -- an + * implementation that is not landing, so the table is not carried over. + * + * The invariant the machine existed to enforce is carried over: replacing a + * node under the same id must not lose what the user can see on it. The + * sibling test below already covers widget values and links; the title did + * not have an assertion, and it is the field a user is most likely to have + * set by hand. + * + * The incumbent title and the replacement payload's title DIFFER on + * purpose. An earlier version of this test supplied 'Upscale pass' on both + * sides, so a materializer that simply took the replacement payload's title + * produced the expected string and the test could not tell preservation from + * overwriting -- it passed while proving nothing. + * + * Making them differ turned it red, so this is `it.fails`: the live title is + * NOT preserved, the replacement payload's title wins, and a user who + * renamed the node loses the name. That is the same loss the two + * `keeps a live rename ...` pins in this file already record under different + * triggers (a tab reload, an unrelated reconcile); this one is the + * same-id replacement trigger the PM-1293 stack existed to fix. + */ + it.fails('KNOWN GAP: keeps the live title when a replacement payload carries another', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + const mutations = remoteMutations(scope) + mutations.addNode( + { ...nodePayload(1), title: 'Upscale pass' }, + { ...REMOTE, opId: 'op-1' } + ) + reconcileAgentAdapters(graph) + const stale = graph.getNodeById(toNodeId(1)) + // The user renames the node by hand after it was materialized. + if (stale) stale.title = 'My hand-named node' + + mutations.deleteNode(toNodeId(1), [], REMOTE) + mutations.addNode( + { ...nodePayload(1), title: 'Upscale pass' }, + { ...REMOTE, opId: 'op-1-again' } + ) + + expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) + + const replacement = graph.getNodeById(toNodeId(1)) + expect(replacement).not.toBe(stale) + expect(replacement?.title).toBe('My hand-named node') + }) + it('runs stale-node lifecycle without clearing successor-owned state', () => { const graph = new LGraph() const scope = graphScopeOf(graph) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 98b52c5babc..99087caceae 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -412,6 +412,12 @@ describe('abortIfUnbound settles delivered ops as undeliverable', () => { }) }) +/** + * The FakeBridge's `resubscribe` is a bare vi.fn, so a test that wants the + * post-reconnect subscribe ack must play the host's part itself: mark the + * workflow subscribed again and forward the `doc_subscribed` ok frame the + * bridge would have re-emitted. + */ function ackResubscribe(workflowId: string): void { bridge().subscribedWorkflowId = workflowId bridge().dispatchEvent( @@ -421,6 +427,41 @@ function ackResubscribe(workflowId: string): void { ) } +/** + * Queue-and-replay of an edit made while the connection was down is WANTED + * product behaviour, ruled 2026-09-26: "yes we definitely want queued and + * replay edits. edits while connection is down should definitely still be + * pushed eventually. if adrs are saying otherwise, change the adrs as well." + * + * That ruling is why the expected failures below are pins on desired behaviour + * rather than an argument with a decision. Exactly ONE recorded sentence is in + * the way -- CRDT-AUTHORITY-0035 decision 3, "The frontend does not persist or + * re-send operations dropped as `undeliverable`" -- and it is being amended + * rather than obeyed. It is `Status: Proposed` and agent-authored (#18064). + * Do not delete these pins to reconcile the suite with that clause. + * + * CRDT-WRITE-0035 is NOT in the way and needs no reversal: it already holds a + * delete minted before a tab switch and delivers it on return, which is this + * same direction. What it rejects is "a retry queue that resends undeliverable + * ops when the workflow rebinds", because that "duplicates the sender's + * ordering and never-re-mint rules outside the module that owns them" -- a + * PLACEMENT objection protecting FORECLOSE #7 (never regenerate `op_id` on + * retry) and KEEP-ALIVE #2 (the creator mints `op_id` once). It stands, and the + * pin below on id retention is what holds us to it: retention belongs inside + * `opSender`, not in a second queue beside it. + * + * What has to change is smaller than "add a queue": the sender's five retries + * 500 ms apart are a DELIVERY budget where the ruling wants a RETENTION budget. + * Stop `undeliverable` being terminal. + * + * SCOPE, because the pins are narrower than the product property and the two + * are easy to conflate. Everything below lives inside ONE page session: the + * socket drops, the retry budget runs out, the socket returns. Surviving a + * RELOAD is a strictly stronger property that needs persistence, which is what + * the parked human-op outbox stack (#18510, #18519, #18533, #18537, #18538, + * #18545) was actually built for. Nothing here asserts it, and no pin here + * should be read as covering it. + */ describe('a human edit made while the document connection is down', () => { const RETRY_INTERVAL_MS = 500 const RETRY_BUDGET_MS = 5 * RETRY_INTERVAL_MS @@ -437,6 +478,13 @@ describe('a human edit made while the document connection is down', () => { vi.useFakeTimers() }) + /** + * Characterization of today, NOT a desired property. Under the ruling above + * this behaviour is the defect: the budget is a delivery budget, so the batch + * is dropped rather than retained. When retention lands this test goes red and + * the three pins below go green, in the same change. Do not "fix" it by + * relaxing the assertion. + */ it('is abandoned once the retry budget runs out, and nothing retains it', async () => { const { enqueue } = mountFollower('wf-a') clientState.transportUp = false @@ -480,4 +528,87 @@ describe('a human edit made while the document connection is down', () => { expect(bridge().resubscribe).toHaveBeenCalledTimes(1) expect(clientState.sent).toHaveLength(0) }) + + /** + * Three pins, one property each, because `it.fails` passes on the FIRST + * failing assertion and abandons the rest of the body. Bundled, the delivery + * assertion fails while `sent` is still empty, so an id or duplicate check + * behind it never executes -- and an implementation that delivered the op + * while re-minting its `op_id` would still report as an expected failure, + * hiding a FORECLOSE #7 violation behind a green run. Split, each property + * fails and gets fixed on its own name. + * + * Stated rather than papered over: nothing is delivered at all today, so pins + * 2 and 3 currently fail for pin 1's reason and cannot yet reach their own + * subject. That is why they are separate names now -- when delivery lands, + * each one starts reporting on the property it is named for instead of one + * masking the other two. + * + * All three are same-page-session: socket drop, budget exhaustion, socket + * return. None asserts survival across a reload; see the describe docstring. + */ + it.fails('KNOWN GAP: still reaches the host after reconnect, in the same session', async () => { + const { enqueue } = mountFollower('wf-a') + clientState.transportUp = false + + await enqueue([deleteNode('edited-during-outage')]) + vi.advanceTimersByTime(RETRY_BUDGET_MS) + + clientState.transportUp = true + apiState.target.dispatchEvent(new Event('reconnected')) + ackResubscribe('wf-a') + + // Delivered, toward the workflow it was minted against. The ONLY + // assertion in this body, so this is the property that fails. + expect(clientState.sent).toMatchObject([{ workflowId: 'wf-a' }]) + }) + + it.fails('KNOWN GAP: that replay carries the op_id it was minted with', async () => { + const { enqueue } = mountFollower('wf-a') + clientState.transportUp = false + + await enqueue([deleteNode('edited-during-outage')]) + vi.advanceTimersByTime(RETRY_BUDGET_MS) + const operationId = clientState.attempts[0].ops[0].op_id + + clientState.transportUp = true + apiState.target.dispatchEvent(new Event('reconnected')) + ackResubscribe('wf-a') + + // Re-minting would defeat the applier's op_id dedupe and let a replay + // apply the edit a second time (FORECLOSE #7). Retention has to live in + // the module that owns minting, which is why CRDT-WRITE-0035's rejection + // of a queue beside the sender still stands. + expect(clientState.sent[0]?.ops[0]).toMatchObject({ + op_id: operationId, + op: 'delete_node', + node_id: 'edited-during-outage' + }) + }) + + it.fails('KNOWN GAP: that replay happens exactly once, not on every later reconnect', async () => { + const { enqueue } = mountFollower('wf-a') + clientState.transportUp = false + + await enqueue([deleteNode('edited-during-outage')]) + vi.advanceTimersByTime(RETRY_BUDGET_MS) + const operationId = clientState.attempts[0].ops[0].op_id + + clientState.transportUp = true + apiState.target.dispatchEvent(new Event('reconnected')) + ackResubscribe('wf-a') + + // The host applies the replay, then the socket drops again. A retained + // batch that is never retired would go out a second time. + dispatchOpsResult({ + workflowId: 'wf-a', + ok: true, + applied: [operationId], + skipped: [] + }) + apiState.target.dispatchEvent(new Event('reconnected')) + ackResubscribe('wf-a') + + expect(clientState.sent).toHaveLength(1) + }) }) From 13ef724e9d3c548ac458476bbd87a329f2a46c5a Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 26 Sep 2026 20:34:46 -0700 Subject: [PATCH 07/12] test(agent): close expected-failure vacuity --- .../agent/crdt/agentNodeMaterializer.test.ts | 109 +++++++++++++----- .../agent/crdt/crossWorkflowPending.test.ts | 3 +- 2 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 6cdffa220c1..4c64635592f 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -341,8 +341,7 @@ describe('reconcileAgentAdapters', () => { reconcileAgentAdapters(graph) const live = graph.getNodeById(toNodeId(1)) - assert.exists(live) - live.title = 'My Custom Prompt' + if (live) live.title = 'My Custom Prompt' graph.configure(graph.serialize()) remoteMutations(scope).batch({ ...REMOTE, opId: 'op-2' }, (batch) => { @@ -534,6 +533,15 @@ describe('reconcileAgentAdapters', () => { ).toBe(9) }) + it('materializes the node used by the canonical-layout gap pin', () => { + const graph = new LGraph() + seedAgentAddedNode(graph, 1) + + reconcileAgentAdapters(graph) + + expect(graph.getNodeById(toNodeId(1))).toBeDefined() + }) + it.fails('keeps canonical layout geometry when configuring a materialized node', () => { const graph = new LGraph() const scope = seedAgentAddedNode(graph, 1) @@ -549,9 +557,8 @@ describe('reconcileAgentAdapters', () => { reconcileAgentAdapters(graph) const live = graph.getNodeById(toNodeId(1)) - assert.exists(live) expect({ - live: [...live.pos], + live: live ? [...live.pos] : undefined, stored: layoutStore.getNodeLayout(scope.rootGraphId, toNodeId(1)) ?.position }).toEqual({ live: [400, 500], stored: { x: 400, y: 500 } }) @@ -607,6 +614,26 @@ describe('reconcileAgentAdapters', () => { expect(graph.getNodeById(toNodeId(1))).toBe(live) }) + it('materializes node, state, and widgets used by the canonical-state gap pin', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + remoteMutations(scope).addNode( + { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, + REMOTE + ) + const addedState = useNodeDataStore().getNode( + scope.rootGraphId, + toNodeId(1) + ) + + reconcileAgentAdapters(graph) + const live = graph.getNodeById(toNodeId(1)) + + expect(addedState).toBeDefined() + expect(live).toBeDefined() + expect(live?.widgets).toBeDefined() + }) + it.fails('adopts canonical node and widget state across materialization and reconcile', () => { const graph = new LGraph() const scope = graphScopeOf(graph) @@ -619,11 +646,9 @@ describe('reconcileAgentAdapters', () => { scope.rootGraphId, toNodeId(1) ) - assert.exists(addedState) reconcileAgentAdapters(graph) const live = graph.getNodeById(toNodeId(1)) - assert.exists(live) - const materializedState = live._state + const materializedState = live?._state mutations.batch({ ...REMOTE, opId: 'reconcile-value' }, (batch) => { batch.reconcileNode({ @@ -633,27 +658,40 @@ describe('reconcileAgentAdapters', () => { }) reconcileAgentAdapters(graph) + const liveWidgetBeforeLocalEdit = live?.widgets?.[0].value + const storedWidgetBeforeLocalEdit = useWidgetValueStore().getWidget( + widgetId(scope.rootGraphId, toNodeId(1), 'value') + )?.value + const widgets = live?.widgets + if (widgets) widgets[0].value = 10 expect({ adoptedAddedState: materializedState === addedState, - keptMaterializedState: live._state === materializedState, - liveWidget: live.widgets?.[0].value, - storedWidget: useWidgetValueStore().getWidget( + keptMaterializedState: live?._state === materializedState, + liveWidgetBeforeLocalEdit, + storedWidgetBeforeLocalEdit, + propagatedWidget: useWidgetValueStore().getWidget( widgetId(scope.rootGraphId, toNodeId(1), 'value') )?.value }).toEqual({ adoptedAddedState: true, keptMaterializedState: true, - liveWidget: 9, - storedWidget: 9 + liveWidgetBeforeLocalEdit: 9, + storedWidgetBeforeLocalEdit: 9, + propagatedWidget: 10 }) - const widgets = live.widgets - assert.exists(widgets) - widgets[0].value = 10 - expect( - useWidgetValueStore().getWidget( - widgetId(scope.rootGraphId, toNodeId(1), 'value') - )?.value - ).toBe(10) + }) + + it('materializes the live node used by the reload-and-reconcile gap pin', () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + remoteMutations(scope).addNode( + { ...nodePayload(1), title: 'Positive prompt' }, + REMOTE + ) + + reconcileAgentAdapters(graph) + + expect(graph.getNodeById(toNodeId(1))).toBeDefined() }) it.fails('keeps a live rename after a workflow reload and unrelated reconcile', () => { @@ -667,8 +705,7 @@ describe('reconcileAgentAdapters', () => { reconcileAgentAdapters(graph) const live = graph.getNodeById(toNodeId(1)) - assert.exists(live) - live.title = 'My Custom Prompt' + if (live) live.title = 'My Custom Prompt' graph.configure(graph.serialize()) remoteMutations(scope).batch( @@ -728,7 +765,7 @@ describe('reconcileAgentAdapters', () => { * triggers (a tab reload, an unrelated reconcile); this one is the * same-id replacement trigger the PM-1293 stack existed to fix. */ - it.fails('KNOWN GAP: keeps the live title when a replacement payload carries another', () => { + const replaceRenamedNode = () => { const graph = new LGraph() const scope = graphScopeOf(graph) const mutations = remoteMutations(scope) @@ -747,10 +784,30 @@ describe('reconcileAgentAdapters', () => { { ...REMOTE, opId: 'op-1-again' } ) - expect(reconcileAgentAdapters(graph)).toEqual([toNodeId(1)]) - + const materialized = reconcileAgentAdapters(graph) const replacement = graph.getNodeById(toNodeId(1)) - expect(replacement).not.toBe(stale) + return { graph, materialized, replacement, stale } + } + + it('sets up and replaces the renamed node before checking title preservation', () => { + const { graph, materialized, replacement, stale } = replaceRenamedNode() + + expect(stale).toBeDefined() + expect(replacement).toBeDefined() + expect({ + materialized, + nodeCount: graph._nodes.length, + replacementChanged: replacement !== stale + }).toEqual({ + materialized: [toNodeId(1)], + nodeCount: 1, + replacementChanged: true + }) + }) + + it.fails('KNOWN GAP: keeps the live title when a replacement payload carries another', () => { + const { replacement } = replaceRenamedNode() + expect(replacement?.title).toBe('My hand-named node') }) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 99087caceae..415eade7e99 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -597,6 +597,7 @@ describe('a human edit made while the document connection is down', () => { clientState.transportUp = true apiState.target.dispatchEvent(new Event('reconnected')) ackResubscribe('wf-a') + const sentAfterFirstAck = clientState.sent.length // The host applies the replay, then the socket drops again. A retained // batch that is never retired would go out a second time. @@ -609,6 +610,6 @@ describe('a human edit made while the document connection is down', () => { apiState.target.dispatchEvent(new Event('reconnected')) ackResubscribe('wf-a') - expect(clientState.sent).toHaveLength(1) + expect([sentAfterFirstAck, clientState.sent.length]).toEqual([1, 1]) }) }) From 930d3de339f958038e5e1137843a52df58705cfe Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:53:16 -0700 Subject: [PATCH 08/12] test(agent): await replay before settling result Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18754#discussion_r4117678714 --- .../extensions/agent/crdt/crossWorkflowPending.test.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 415eade7e99..69f5109393b 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -592,24 +592,22 @@ describe('a human edit made while the document connection is down', () => { await enqueue([deleteNode('edited-during-outage')]) vi.advanceTimersByTime(RETRY_BUDGET_MS) - const operationId = clientState.attempts[0].ops[0].op_id clientState.transportUp = true apiState.target.dispatchEvent(new Event('reconnected')) ackResubscribe('wf-a') - const sentAfterFirstAck = clientState.sent.length + await vi.waitFor(() => expect(clientState.sent).toHaveLength(1)) + const replayedOperationId = clientState.sent[0].ops[0].op_id - // The host applies the replay, then the socket drops again. A retained - // batch that is never retired would go out a second time. dispatchOpsResult({ workflowId: 'wf-a', ok: true, - applied: [operationId], + applied: [replayedOperationId], skipped: [] }) apiState.target.dispatchEvent(new Event('reconnected')) ackResubscribe('wf-a') - expect([sentAfterFirstAck, clientState.sent.length]).toEqual([1, 1]) + expect(clientState.sent).toHaveLength(1) }) }) From 7cf5de9dc05a7fa03fbfce0e02711baf03f3dcc0 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 08:53:28 -0700 Subject: [PATCH 09/12] test(agent): make gap setup independently observable Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18754#discussion_r4117678707 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18754#discussion_r4117678711 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18754#discussion_r4117678718 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18754#discussion_r4117678726 https://github.com/Comfy-Org/ComfyUI_frontend/pull/18754#discussion_r4117678733 --- .../agent/crdt/agentNodeMaterializer.test.ts | 187 +++++++----------- 1 file changed, 75 insertions(+), 112 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 4c64635592f..3064d17cf5c 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -227,6 +227,26 @@ function seedAgentAddedNode(graph: LGraph, id: number, type = 'dummy') { return scope } +function reloadRenamedNode() { + const graph = new LGraph() + const scope = graphScopeOf(graph) + const docPayload = { + ...nodePayload(1), + title: 'Positive prompt' + } + remoteMutations(scope).addNode(docPayload, REMOTE) + reconcileAgentAdapters(graph) + + const live = graph.getNodeById(toNodeId(1)) + assert.exists(live) + live.title = 'My Custom Prompt' + graph.configure(graph.serialize()) + + const reloaded = graph.getNodeById(toNodeId(1)) + assert.exists(reloaded) + return { docPayload, graph, reloaded, scope } +} + beforeEach(() => { LiteGraph.registerNodeType('dummy', DummyNode) LiteGraph.registerNodeType('widget-node', WidgetNode) @@ -326,23 +346,7 @@ describe('reconcileAgentAdapters', () => { }) it.fails('keeps a live rename after the workflow tab reloads and an unrelated reconcile runs', () => { - const graph = new LGraph() - const scope = graphScopeOf(graph) - const docPayload = { - id: 1, - type: 'dummy', - title: 'Positive prompt', - pos: [0, 0], - size: [100, 80], - inputs: [], - outputs: [] - } - remoteMutations(scope).addNode(docPayload, { ...REMOTE, opId: 'op-1' }) - reconcileAgentAdapters(graph) - - const live = graph.getNodeById(toNodeId(1)) - if (live) live.title = 'My Custom Prompt' - graph.configure(graph.serialize()) + const { docPayload, graph, scope } = reloadRenamedNode() remoteMutations(scope).batch({ ...REMOTE, opId: 'op-2' }, (batch) => { batch.reconcileNode(docPayload) @@ -533,16 +537,7 @@ describe('reconcileAgentAdapters', () => { ).toBe(9) }) - it('materializes the node used by the canonical-layout gap pin', () => { - const graph = new LGraph() - seedAgentAddedNode(graph, 1) - - reconcileAgentAdapters(graph) - - expect(graph.getNodeById(toNodeId(1))).toBeDefined() - }) - - it.fails('keeps canonical layout geometry when configuring a materialized node', () => { + const arrangeCanonicalLayoutNode = () => { const graph = new LGraph() const scope = seedAgentAddedNode(graph, 1) layoutStore.applyOperation({ @@ -553,6 +548,19 @@ describe('reconcileAgentAdapters', () => { source: LayoutSource.AgentRemote, timestamp: Date.now() }) + return { graph, scope } + } + + it('stores the remote layout used by the canonical-layout gap pin', () => { + const { scope } = arrangeCanonicalLayoutNode() + + expect( + layoutStore.getNodeLayout(scope.rootGraphId, toNodeId(1))?.position + ).toEqual({ x: 400, y: 500 }) + }) + + it.fails('keeps canonical layout geometry when configuring a materialized node', () => { + const { graph, scope } = arrangeCanonicalLayoutNode() reconcileAgentAdapters(graph) @@ -599,6 +607,27 @@ describe('reconcileAgentAdapters', () => { }) describe('remote update of a live node', () => { + const arrangeCanonicalWidgetState = () => { + const graph = new LGraph() + const scope = graphScopeOf(graph) + const mutations = remoteMutations(scope) + mutations.addNode( + { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, + REMOTE + ) + const addedState = useNodeDataStore().getNode( + scope.rootGraphId, + toNodeId(1) + ) + assert.exists(addedState) + reconcileAgentAdapters(graph) + const live = graph.getNodeById(toNodeId(1)) + assert.exists(live) + const widget = live.widgets?.[0] + assert.exists(widget) + return { addedState, graph, live, mutations, scope, widget } + } + it('keeps the same live node when the record is updated in place', () => { const graph = new LGraph() const scope = seedAgentAddedNode(graph, 1) @@ -615,40 +644,16 @@ describe('reconcileAgentAdapters', () => { }) it('materializes node, state, and widgets used by the canonical-state gap pin', () => { - const graph = new LGraph() - const scope = graphScopeOf(graph) - remoteMutations(scope).addNode( - { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, - REMOTE - ) - const addedState = useNodeDataStore().getNode( - scope.rootGraphId, - toNodeId(1) - ) - - reconcileAgentAdapters(graph) - const live = graph.getNodeById(toNodeId(1)) + const { live, widget } = arrangeCanonicalWidgetState() - expect(addedState).toBeDefined() - expect(live).toBeDefined() - expect(live?.widgets).toBeDefined() + expect(live.id).toBe(toNodeId(1)) + expect(widget.value).toBe(7) }) it.fails('adopts canonical node and widget state across materialization and reconcile', () => { - const graph = new LGraph() - const scope = graphScopeOf(graph) - const mutations = remoteMutations(scope) - mutations.addNode( - { ...nodePayload(1, 'widget-node'), widgets_values: { value: 7 } }, - REMOTE - ) - const addedState = useNodeDataStore().getNode( - scope.rootGraphId, - toNodeId(1) - ) - reconcileAgentAdapters(graph) - const live = graph.getNodeById(toNodeId(1)) - const materializedState = live?._state + const { addedState, graph, live, mutations, scope, widget } = + arrangeCanonicalWidgetState() + const materializedState = live._state mutations.batch({ ...REMOTE, opId: 'reconcile-value' }, (batch) => { batch.reconcileNode({ @@ -658,15 +663,14 @@ describe('reconcileAgentAdapters', () => { }) reconcileAgentAdapters(graph) - const liveWidgetBeforeLocalEdit = live?.widgets?.[0].value + const liveWidgetBeforeLocalEdit = widget.value const storedWidgetBeforeLocalEdit = useWidgetValueStore().getWidget( widgetId(scope.rootGraphId, toNodeId(1), 'value') )?.value - const widgets = live?.widgets - if (widgets) widgets[0].value = 10 + widget.value = 10 expect({ adoptedAddedState: materializedState === addedState, - keptMaterializedState: live?._state === materializedState, + keptMaterializedState: live._state === materializedState, liveWidgetBeforeLocalEdit, storedWidgetBeforeLocalEdit, propagatedWidget: useWidgetValueStore().getWidget( @@ -681,32 +685,14 @@ describe('reconcileAgentAdapters', () => { }) }) - it('materializes the live node used by the reload-and-reconcile gap pin', () => { - const graph = new LGraph() - const scope = graphScopeOf(graph) - remoteMutations(scope).addNode( - { ...nodePayload(1), title: 'Positive prompt' }, - REMOTE - ) + it('keeps the live rename used by the reload-and-reconcile gap pin through reload', () => { + const { reloaded } = reloadRenamedNode() - reconcileAgentAdapters(graph) - - expect(graph.getNodeById(toNodeId(1))).toBeDefined() + expect(reloaded.title).toBe('My Custom Prompt') }) it.fails('keeps a live rename after a workflow reload and unrelated reconcile', () => { - const graph = new LGraph() - const scope = graphScopeOf(graph) - const docPayload = { - ...nodePayload(1), - title: 'Positive prompt' - } - remoteMutations(scope).addNode(docPayload, REMOTE) - reconcileAgentAdapters(graph) - - const live = graph.getNodeById(toNodeId(1)) - if (live) live.title = 'My Custom Prompt' - graph.configure(graph.serialize()) + const { docPayload, graph, scope } = reloadRenamedNode() remoteMutations(scope).batch( { ...REMOTE, opId: 'unrelated-reconcile' }, @@ -740,31 +726,6 @@ describe('reconcileAgentAdapters', () => { expect(graph.serialize().nodes).toHaveLength(1) }) - /** - * Extracted from the PM-1293 node-replacement stack (PR #18150), which is - * parked. That stack modelled replacement as a state machine inside the - * materializer and its tests assert that machine's transition table -- an - * implementation that is not landing, so the table is not carried over. - * - * The invariant the machine existed to enforce is carried over: replacing a - * node under the same id must not lose what the user can see on it. The - * sibling test below already covers widget values and links; the title did - * not have an assertion, and it is the field a user is most likely to have - * set by hand. - * - * The incumbent title and the replacement payload's title DIFFER on - * purpose. An earlier version of this test supplied 'Upscale pass' on both - * sides, so a materializer that simply took the replacement payload's title - * produced the expected string and the test could not tell preservation from - * overwriting -- it passed while proving nothing. - * - * Making them differ turned it red, so this is `it.fails`: the live title is - * NOT preserved, the replacement payload's title wins, and a user who - * renamed the node loses the name. That is the same loss the two - * `keeps a live rename ...` pins in this file already record under different - * triggers (a tab reload, an unrelated reconcile); this one is the - * same-id replacement trigger the PM-1293 stack existed to fix. - */ const replaceRenamedNode = () => { const graph = new LGraph() const scope = graphScopeOf(graph) @@ -775,8 +736,8 @@ describe('reconcileAgentAdapters', () => { ) reconcileAgentAdapters(graph) const stale = graph.getNodeById(toNodeId(1)) - // The user renames the node by hand after it was materialized. - if (stale) stale.title = 'My hand-named node' + assert.exists(stale) + stale.title = 'My hand-named node' mutations.deleteNode(toNodeId(1), [], REMOTE) mutations.addNode( @@ -797,11 +758,13 @@ describe('reconcileAgentAdapters', () => { expect({ materialized, nodeCount: graph._nodes.length, - replacementChanged: replacement !== stale + replacementChanged: replacement !== stale, + staleTitle: stale.title }).toEqual({ materialized: [toNodeId(1)], nodeCount: 1, - replacementChanged: true + replacementChanged: true, + staleTitle: 'My hand-named node' }) }) From 921e4496416b1a0e39d06fa9abcae2b89d035e99 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 10:07:47 -0700 Subject: [PATCH 10/12] test(agent): prove canonical layout materialization --- .../agent/crdt/agentNodeMaterializer.test.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts index 3064d17cf5c..eddb2c75e7b 100644 --- a/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts +++ b/src/workbench/extensions/agent/crdt/agentNodeMaterializer.test.ts @@ -552,24 +552,26 @@ describe('reconcileAgentAdapters', () => { } it('stores the remote layout used by the canonical-layout gap pin', () => { - const { scope } = arrangeCanonicalLayoutNode() + const { graph, scope } = arrangeCanonicalLayoutNode() expect( layoutStore.getNodeLayout(scope.rootGraphId, toNodeId(1))?.position ).toEqual({ x: 400, y: 500 }) + + reconcileAgentAdapters(graph) + const live = graph.getNodeById(toNodeId(1)) + assert.exists(live) + expect(live.id).toBe(toNodeId(1)) }) it.fails('keeps canonical layout geometry when configuring a materialized node', () => { - const { graph, scope } = arrangeCanonicalLayoutNode() + const { graph } = arrangeCanonicalLayoutNode() reconcileAgentAdapters(graph) const live = graph.getNodeById(toNodeId(1)) - expect({ - live: live ? [...live.pos] : undefined, - stored: layoutStore.getNodeLayout(scope.rootGraphId, toNodeId(1)) - ?.position - }).toEqual({ live: [400, 500], stored: { x: 400, y: 500 } }) + assert.exists(live) + expect([...live.pos]).toEqual([400, 500]) }) it('is idempotent once the node is live', () => { From 05c0317e82ab2087ffaf41d19f10dc781acfc19b Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 15:08:44 -0700 Subject: [PATCH 11/12] test(agent): link replay pins to accepted policy --- .../agent/crdt/crossWorkflowPending.test.ts | 44 ++++--------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index 69f5109393b..b498910d357 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -428,39 +428,15 @@ function ackResubscribe(workflowId: string): void { } /** - * Queue-and-replay of an edit made while the connection was down is WANTED - * product behaviour, ruled 2026-09-26: "yes we definitely want queued and - * replay edits. edits while connection is down should definitely still be - * pushed eventually. if adrs are saying otherwise, change the adrs as well." + * These pins cover one observable same-page-session scenario: the socket + * drops, the retry budget expires, the suspended tab resumes, and the socket + * returns. After the resubscribe acknowledgment, `opSender` must replay the + * retained edit with its original `op_id`; retry exhaustion is not a terminal + * discard. Persistence across a full page reload is a separate decision and is + * not asserted here. * - * That ruling is why the expected failures below are pins on desired behaviour - * rather than an argument with a decision. Exactly ONE recorded sentence is in - * the way -- CRDT-AUTHORITY-0035 decision 3, "The frontend does not persist or - * re-send operations dropped as `undeliverable`" -- and it is being amended - * rather than obeyed. It is `Status: Proposed` and agent-authored (#18064). - * Do not delete these pins to reconcile the suite with that clause. - * - * CRDT-WRITE-0035 is NOT in the way and needs no reversal: it already holds a - * delete minted before a tab switch and delivers it on return, which is this - * same direction. What it rejects is "a retry queue that resends undeliverable - * ops when the workflow rebinds", because that "duplicates the sender's - * ordering and never-re-mint rules outside the module that owns them" -- a - * PLACEMENT objection protecting FORECLOSE #7 (never regenerate `op_id` on - * retry) and KEEP-ALIVE #2 (the creator mints `op_id` once). It stands, and the - * pin below on id retention is what holds us to it: retention belongs inside - * `opSender`, not in a second queue beside it. - * - * What has to change is smaller than "add a queue": the sender's five retries - * 500 ms apart are a DELIVERY budget where the ruling wants a RETENTION budget. - * Stop `undeliverable` being terminal. - * - * SCOPE, because the pins are narrower than the product property and the two - * are easy to conflate. Everything below lives inside ONE page session: the - * socket drops, the retry budget runs out, the socket returns. Surviving a - * RELOAD is a strictly stronger property that needs persistence, which is what - * the parked human-op outbox stack (#18510, #18519, #18533, #18537, #18538, - * #18545) was actually built for. Nothing here asserts it, and no pin here - * should be read as covering it. + * Accepted policy (ADR-012, clarification dated 2026-09-28): + * https://github.com/christian-byrne/in-app-agent-program/blob/955f4dd3eab7e3a6992e409239463ddc5fa5c296/decisions/ADR-012-lifecycle-and-reconnect-semantics.md#clarification-2026-09-28 */ describe('a human edit made while the document connection is down', () => { const RETRY_INTERVAL_MS = 500 @@ -576,9 +552,7 @@ describe('a human edit made while the document connection is down', () => { ackResubscribe('wf-a') // Re-minting would defeat the applier's op_id dedupe and let a replay - // apply the edit a second time (FORECLOSE #7). Retention has to live in - // the module that owns minting, which is why CRDT-WRITE-0035's rejection - // of a queue beside the sender still stands. + // apply the edit a second time. expect(clientState.sent[0]?.ops[0]).toMatchObject({ op_id: operationId, op: 'delete_node', From 624368af29c9a58ed5472e3bc8b593972313e275 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 28 Sep 2026 15:49:50 -0700 Subject: [PATCH 12/12] test(agent): describe replay pins precisely Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/18754#discussion_r4127551237 --- .../agent/crdt/crossWorkflowPending.test.ts | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts index b498910d357..a1955e86d8e 100644 --- a/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts +++ b/src/workbench/extensions/agent/crdt/crossWorkflowPending.test.ts @@ -428,15 +428,12 @@ function ackResubscribe(workflowId: string): void { } /** - * These pins cover one observable same-page-session scenario: the socket - * drops, the retry budget expires, the suspended tab resumes, and the socket - * returns. After the resubscribe acknowledgment, `opSender` must replay the - * retained edit with its original `op_id`; retry exhaustion is not a terminal - * discard. Persistence across a full page reload is a separate decision and is - * not asserted here. - * - * Accepted policy (ADR-012, clarification dated 2026-09-28): - * https://github.com/christian-byrne/in-app-agent-program/blob/955f4dd3eab7e3a6992e409239463ddc5fa5c296/decisions/ADR-012-lifecycle-and-reconnect-semantics.md#clarification-2026-09-28 + * These pins exercise one observable same-page-session transport sequence: + * mark the transport down, exhaust the retry budget, mark it up, emit + * `reconnected`, and acknowledge resubscription. The three separate pins cover + * eventual delivery, retention of the original `op_id`, and exactly-once + * delivery after the matching host result. They do not exercise tab suspension + * or persistence across a full page reload. */ describe('a human edit made while the document connection is down', () => { const RETRY_INTERVAL_MS = 500 @@ -455,11 +452,10 @@ describe('a human edit made while the document connection is down', () => { }) /** - * Characterization of today, NOT a desired property. Under the ruling above - * this behaviour is the defect: the budget is a delivery budget, so the batch - * is dropped rather than retained. When retention lands this test goes red and - * the three pins below go green, in the same change. Do not "fix" it by - * relaxing the assertion. + * Characterization of today, NOT a desired property: the retry budget is a + * delivery budget, so the batch is dropped rather than retained. If retention + * lands, this test goes red and the three pins below go green in the same + * change. Do not "fix" it by relaxing the assertion. */ it('is abandoned once the retry budget runs out, and nothing retains it', async () => { const { enqueue } = mountFollower('wf-a') @@ -520,8 +516,9 @@ describe('a human edit made while the document connection is down', () => { * each one starts reporting on the property it is named for instead of one * masking the other two. * - * All three are same-page-session: socket drop, budget exhaustion, socket - * return. None asserts survival across a reload; see the describe docstring. + * All three use the same transport-down, budget-exhaustion, reconnect-event, + * and resubscribe-ack sequence. None asserts tab lifecycle behavior or + * survival across a reload; see the describe docstring. */ it.fails('KNOWN GAP: still reaches the host after reconnect, in the same session', async () => { const { enqueue } = mountFollower('wf-a')