From 038e7a9064e4a065835addeb030a3f14961341e2 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 29 Aug 2026 08:02:36 -0700 Subject: [PATCH 01/17] test(agent/crdt): property coverage for the human write leg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two test files, no product code changes. opEnvelope.property.test.ts — the transport boundary. The existing example tests fix the caps at points chosen to hit them; these hold for arbitrary mixes of kinds and payload sizes: - Totality: chunkWireOps(ops).flat() is ops, by reference identity — nothing dropped, duplicated, reordered, or rebuilt. A dropped op is a silently lost human edit. - Caps: no empty batch, none over the 256-op cap, no multi-op batch over the 4 MiB byte cap, and a single oversize op still ships alone. - Isolation: clear is always a batch of exactly one. - Maximality: a batch only ends early when a cap or a clear forces it — without this, a chunker emitting one op per batch passes everything above. - Envelope: unique 32-hex op_ids across separate mints, and the semantic payload survives minting field-for-field. - Order-independent LWW through the real applier: concurrent FE-minted writes to one register converge to the same value in forward, reversed, and generated-permutation order, and that value is the winner the total stamp order picks. Every property that could be satisfied by an empty or uncontended corpus carries a vacuity guard asserting the interesting case was actually generated. Also records the DQ-11 incarnation gap at this branch's package pin: Stamp here is the 2-tuple [base_version, actor] with no incarnation component, so a stale pre-reconnect write with a higher base_version permanently defeats the live post-reconnect write from the same client. The test asserts today's behaviour deliberately, as a tripwire that reddens when the pin advances to the incarnation-namespaced stamps. mintGate.property.test.ts — enumerates all 16 gate states. The existing tests walk the five one-conjunct-off cases, leaving the combinations untested, and the combination is the hazard: teardown coinciding with a bound doc and local provenance is an ordinary tab switch, which is the clear-storm the gate exists to stop. --- .../agent/crdt/mintGate.property.test.ts | 85 ++++ .../agent/crdt/opEnvelope.property.test.ts | 422 ++++++++++++++++++ 2 files changed, 507 insertions(+) create mode 100644 src/workbench/extensions/agent/crdt/mintGate.property.test.ts create mode 100644 src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts diff --git a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts new file mode 100644 index 00000000000..4271a6298e8 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts @@ -0,0 +1,85 @@ +/** + * Exhaustive coverage of the mint gate. + * + * `mintGate.test.ts` walks the five one-conjunct-off cases. The gate has four + * booleans, so those five leave 11 of the 16 states unvisited — and the states + * that actually matter are the combinations: teardown coinciding with a bound + * doc and local provenance is an ordinary tab switch, and that is precisely the + * clear-storm the gate exists to stop. A regression that dropped the teardown + * conjunct from the conjunction, or that ORed two of them, still passes every + * example test. + * + * The domain is 16 states, so this enumerates all of them rather than sampling. + */ +import { describe, expect, it } from 'vitest' + +import type { MintGateInput } from './mintGate' +import { shouldMint } from './mintGate' + +const BOOLS = [false, true] as const + +const ALL_STATES: MintGateInput[] = BOOLS.flatMap((flagEnabled) => + BOOLS.flatMap((docBound) => + BOOLS.flatMap((localProvenance) => + BOOLS.map((teardown) => ({ + flagEnabled, + docBound, + localProvenance, + teardown + })) + ) + ) +) + +describe('shouldMint — exhaustive over the whole input domain', () => { + it('enumerates all 16 states', () => { + expect(ALL_STATES).toHaveLength(16) + expect(new Set(ALL_STATES.map((s) => JSON.stringify(s))).size).toBe(16) + }) + + it('is exactly flag ∧ bound ∧ local ∧ ¬teardown in every state', () => { + for (const state of ALL_STATES) { + const expected = + state.flagEnabled && + state.docBound && + state.localProvenance && + !state.teardown + expect({ state, mints: shouldMint(state) }).toEqual({ + state, + mints: expected + }) + } + }) + + it('opens in exactly one of the 16 states', () => { + const open = ALL_STATES.filter(shouldMint) + expect(open).toEqual([ + { + flagEnabled: true, + docBound: true, + localProvenance: true, + teardown: false + } + ]) + }) + + it('never mints during teardown, whatever the other three conjuncts say', () => { + const teardownStates = ALL_STATES.filter((state) => state.teardown) + expect(teardownStates).toHaveLength(8) + for (const state of teardownStates) expect(shouldMint(state)).toBe(false) + }) + + it('is monotone: closing any conjunct can never open a gate that was shut', () => { + for (const state of ALL_STATES) { + if (shouldMint(state)) continue + // Every one-step "worsening" of an already-shut gate stays shut. + const worsened: MintGateInput[] = [ + { ...state, flagEnabled: false }, + { ...state, docBound: false }, + { ...state, localProvenance: false }, + { ...state, teardown: true } + ] + for (const next of worsened) expect(shouldMint(next)).toBe(false) + } + }) +}) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts new file mode 100644 index 00000000000..950857e9227 --- /dev/null +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -0,0 +1,422 @@ +/** + * Property coverage for the human write leg's transport boundary. + * + * `opEnvelope.test.ts` pins the chunker with hand-sized fixtures — 600 ops + * splits `[256, 256, 88]`, a half-cap payload splits three ways. Those examples + * fix the caps at points chosen to hit them; they say nothing about arbitrary + * mixes of kinds and sizes, which is exactly where a chunker goes wrong: + * dropping the tail, reordering across a `clear` flush, or splitting one op + * early and silently halving throughput. + * + * Four properties hold for EVERY input: + * + * TOTALITY — `chunkWireOps(ops).flat()` is `ops`: same ops, same order, + * nothing dropped, nothing duplicated. This is the one that + * matters, because a dropped op is a silently lost human edit. + * CAPS — no batch exceeds the op cap or (above one op) the byte cap. + * ISOLATION — `clear` is catastrophic-by-nature and always ships alone. + * MAXIMALITY — a batch is only ended early when a cap forces it. Without this + * a chunker that emits one op per batch passes all three above. + * + * Plus the envelope itself (`mintWireOps`) and the convergence claim it exists + * to support: ops minted here resolve last-writer-wins identically no matter + * what order the host applies them in. + */ +import { + applyOps, + compareStampKeys, + mint, + nodesMap, + stampKey, + SCHEMA_VERSION +} from '@comfyorg/comfy-multi-player' +import type { Op } from '@comfyorg/comfy-multi-player' +import * as fc from 'fast-check' +import { describe, expect, it } from 'vitest' +import * as Y from 'yjs' + +import type { GraphOperation } from './graphOperations' +import { + WIRE_MAX_BATCH_BYTES, + WIRE_MAX_OPS_PER_BATCH, + chunkWireOps, + mintOpId, + mintWireOps +} from './opEnvelope' + +const FC_OPTIONS = { seed: 0x02b0f00d, numRuns: 100 } as const + +const MINT = { actor: 'human:pbt-user:tab-1', baseVersion: 7 } +const CATALOG = { types: { TestNode: { widget_order: ['text'] } } } + +function addNode(id: number): GraphOperation { + return { + op: 'add_node', + node_id: id, + class_type: 'TestNode', + pos: [id, id], + node: { + id, + type: 'TestNode', + pos: [id, id], + inputs: [], + outputs: [], + widgets_values: [] + } + } +} + +function setWidget(id: number, value: unknown): GraphOperation { + return { op: 'set_widget', node_id: id, widget: 'text', value } +} + +function deleteNode(id: number): GraphOperation { + return { op: 'delete_node', node_id: id, removed_links: [] } +} + +function clear(ids: number[]): GraphOperation { + return { op: 'clear', removed_nodes: ids } +} + +/** + * Operations spanning every kind and a payload-size range that straddles the + * byte cap: tiny, ~1/3 cap (so three fit but four do not), and over-cap alone. + */ +const operationArb: fc.Arbitrary = fc.oneof( + { arbitrary: fc.integer({ min: 0, max: 40 }).map(addNode), weight: 4 }, + { + arbitrary: fc + .tuple(fc.integer({ min: 0, max: 40 }), fc.string({ maxLength: 32 })) + .map(([id, value]) => setWidget(id, value)), + weight: 4 + }, + { arbitrary: fc.integer({ min: 0, max: 40 }).map(deleteNode), weight: 2 }, + { + arbitrary: fc + .array(fc.integer({ min: 0, max: 40 }), { maxLength: 4 }) + .map(clear), + weight: 1 + }, + { + // Large but batchable: three of these fit under the byte cap, four do not. + arbitrary: fc + .integer({ min: 0, max: 40 }) + .map((id) => + setWidget(id, 'x'.repeat(Math.ceil(WIRE_MAX_BATCH_BYTES / 3))) + ), + weight: 1 + }, + { + // Single-op oversize: must ship alone rather than be dropped. + arbitrary: fc + .integer({ min: 0, max: 40 }) + .map((id) => setWidget(id, 'x'.repeat(WIRE_MAX_BATCH_BYTES + 16))), + weight: 1 + } +) + +const operationsArb = fc.array(operationArb, { maxLength: 40 }) + +function wireBytes(op: Op): number { + return new TextEncoder().encode(JSON.stringify(op)).length +} + +describe('chunkWireOps (property)', () => { + it('is total and order-preserving: flattening the batches returns the input', () => { + fc.assert( + fc.property(operationsArb, (operations) => { + const ops = mintWireOps(operations, MINT) + + const flattened = chunkWireOps(ops).flat() + + expect(flattened).toHaveLength(ops.length) + // Reference identity, not structural equality: a chunker that rebuilt + // ops (and could therefore re-mint an op_id) must not pass. + for (const [index, op] of flattened.entries()) { + expect(op).toBe(ops[index]) + } + }), + FC_OPTIONS + ) + }) + + it('never emits an empty batch and never exceeds the op cap', () => { + fc.assert( + fc.property(operationsArb, (operations) => { + for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { + expect(batch.length).toBeGreaterThan(0) + expect(batch.length).toBeLessThanOrEqual(WIRE_MAX_OPS_PER_BATCH) + } + }), + FC_OPTIONS + ) + }) + + it('isolates every non-batchable op in a batch of exactly one', () => { + let isolated = 0 + + fc.assert( + fc.property(operationsArb, (operations) => { + for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { + if (batch.some((op) => op.op === 'clear')) { + expect(batch).toHaveLength(1) + isolated++ + } + } + }), + FC_OPTIONS + ) + + // Vacuity guard: a corpus with no `clear` would assert nothing at all. + expect(isolated).toBeGreaterThan(0) + }) + + it('keeps multi-op batches under the byte cap, and ships an oversize op alone', () => { + let multiOp = 0 + let oversizeAlone = 0 + + fc.assert( + fc.property(operationsArb, (operations) => { + for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { + const bytes = batch.reduce((sum, op) => sum + wireBytes(op), 0) + if (batch.length > 1) { + multiOp++ + expect(bytes).toBeLessThanOrEqual(WIRE_MAX_BATCH_BYTES) + } else if (bytes > WIRE_MAX_BATCH_BYTES) { + oversizeAlone++ + } + } + }), + FC_OPTIONS + ) + + // Vacuity guards: both sides of the cap must actually have been reached. + expect(multiOp).toBeGreaterThan(0) + expect(oversizeAlone).toBeGreaterThan(0) + }) + + it('is maximal: a batch only ends early when a cap or a clear forces it', () => { + let boundariesChecked = 0 + + fc.assert( + fc.property(operationsArb, (operations) => { + const ops = mintWireOps(operations, MINT) + const batches = chunkWireOps(ops) + + for (let i = 0; i < batches.length - 1; i++) { + const batch = batches[i]! + const next = batches[i + 1]! + const head = next[0]! + // A clear on either side legitimately forces the boundary. + if (batch.some((op) => op.op === 'clear') || head.op === 'clear') { + continue + } + boundariesChecked++ + const overOps = batch.length + 1 > WIRE_MAX_OPS_PER_BATCH + const overBytes = + batch.reduce((sum, op) => sum + wireBytes(op), 0) + + wireBytes(head) > + WIRE_MAX_BATCH_BYTES + expect(overOps || overBytes).toBe(true) + } + }), + FC_OPTIONS + ) + + // Vacuity guard: with no cap-forced boundary in the corpus this test would + // pass on a chunker that never split at all. + expect(boundariesChecked).toBeGreaterThan(0) + }) +}) + +describe('mintWireOps (property)', () => { + it('mints a unique 32-hex op_id per op and never reuses one across batches', () => { + fc.assert( + fc.property(operationsArb, operationsArb, (first, second) => { + const ids = [ + ...mintWireOps(first, MINT), + ...mintWireOps(second, MINT) + ].map((op) => op.op_id) + + for (const id of ids) expect(id).toMatch(/^[0-9a-f]{32}$/) + expect(new Set(ids).size).toBe(ids.length) + }), + FC_OPTIONS + ) + }) + + it('attaches the envelope and leaves the semantic payload byte-identical', () => { + fc.assert( + fc.property( + operationsArb, + fc.string({ minLength: 1, maxLength: 24 }), + fc.integer({ min: 0, max: 1_000_000 }), + (operations, actor, baseVersion) => { + const context = { actor, baseVersion } + const minted = mintWireOps(operations, context) + + expect(minted).toHaveLength(operations.length) + for (const [index, op] of minted.entries()) { + const original = operations[index]! + expect(op.actor).toBe(actor) + expect(op.base_version).toBe(baseVersion) + expect(op.stamp).toEqual([baseVersion, actor]) + // Every field of the semantic op survives verbatim; minting is + // additive only. + for (const [key, value] of Object.entries(original)) { + expect(op[key as keyof Op]).toEqual(value) + } + } + } + ), + FC_OPTIONS + ) + }) + + it('mints ids that are unique across many direct mintOpId calls', () => { + const ids = Array.from({ length: 512 }, () => mintOpId()) + expect(new Set(ids).size).toBe(512) + }) +}) + +/** + * The reason the envelope exists: ops minted here must resolve last-writer-wins + * to the same value on every replica, whatever order the host applies them in. + * This exercises FE-minted stamps against the real applier, not a model of it. + */ +describe('minted stamps (property) — order-independent LWW', () => { + const writerArb = fc.record({ + actor: fc.string({ minLength: 1, maxLength: 8 }).map((s) => `human:${s}:t`), + baseVersion: fc.integer({ min: 0, max: 20 }), + value: fc.string({ maxLength: 12 }) + }) + + function applyIn(order: readonly Op[], seed: Op[]): Y.Doc { + const doc = mint({ nodes: [], links: [] }, CATALOG) + applyOps(doc, seed) + for (const op of order) applyOps(doc, [op]) + return doc + } + + function readWidget(doc: Y.Doc): unknown { + const node = nodesMap(doc).get('1') + if (!(node instanceof Y.Map)) return undefined + const widgets = node.get('widgets') + return widgets instanceof Y.Map ? widgets.get('text') : undefined + } + + it('converges to the max-stamp winner regardless of arrival order', () => { + let contended = 0 + + fc.assert( + fc.property( + fc.array(writerArb, { minLength: 2, maxLength: 5 }), + fc.array(fc.integer(), { minLength: 8, maxLength: 8 }), + (writers, permutationKeys) => { + const seed = mintWireOps([addNode(1)], { + actor: 'system:mint', + baseVersion: 0 + }) + // Every writer targets the SAME register, concurrently. + const concurrent = writers.map((writer) => + mintWireOps([setWidget(1, writer.value)], { + actor: writer.actor, + baseVersion: writer.baseVersion + }) + ) + const ops = concurrent.map((minted) => minted[0]!) + if (new Set(ops.map((op) => op.actor)).size > 1) contended++ + + const reversed = [...ops].reverse() + const shuffled = ops + .map((op, index) => ({ + op, + key: permutationKeys[index % permutationKeys.length]!, + index + })) + .sort((a, b) => a.key - b.key || a.index - b.index) + .map((entry) => entry.op) + + const forward = readWidget(applyIn(ops, seed)) + expect(readWidget(applyIn(reversed, seed))).toEqual(forward) + expect(readWidget(applyIn(shuffled, seed))).toEqual(forward) + + // …and the surviving value is the one the total stamp order picks, + // not merely whatever both orders happened to agree on. + const winner = [...ops].sort((a, b) => + compareStampKeys(stampKey(a), stampKey(b)) + )[ops.length - 1]! + expect(forward).toEqual( + (winner as Extract).value + ) + } + ), + FC_OPTIONS + ) + + // Vacuity guard: single-actor runs would make the convergence claim trivial. + expect(contended).toBeGreaterThan(0) + }) +}) + +/** + * DQ-11 boundary, RECORDED not fixed. + * + * DQ-11 resolved to incarnation-namespaced stamps: a stale stamp minted before + * a reconnect (life 1) must never defeat a live write from the same client + * after it (life 2). This branch pins `@comfyorg/comfy-multi-player` at a + * revision that PREDATES that change — `Stamp` here is the 2-tuple + * `[base_version, actor]` with no incarnation component and `SCHEMA_VERSION` + * is 1 — and `opEnvelope.ts` mints exactly that shape. So the guarantee is not + * merely untested at this head, it is unrepresentable: a life-1 op that + * happened to carry a higher `base_version` wins, permanently. + * + * The test below asserts TODAY's behaviour on purpose. It is a tripwire: when + * the pin advances to the incarnation-namespaced stamps, it goes red and this + * is where the write leg gets updated. Nothing here is a claim that the current + * outcome is correct. + */ +describe('minted stamps — DQ-11 incarnation gap at this pin', () => { + it('pins the pre-DQ-11 stamp shape the write leg mints', () => { + expect(SCHEMA_VERSION).toBe(1) + + const [op] = mintWireOps([setWidget(1, 'v')], MINT) + + expect(op!.stamp).toEqual([MINT.baseVersion, MINT.actor]) + expect(op!.stamp).toHaveLength(2) + // No incarnation / life component exists to namespace the stamp with. + expect(Object.keys(op!)).not.toContain('incarnation') + }) + + it('documents that a stale life-1 write still defeats a live life-2 write', () => { + const doc = mint({ nodes: [], links: [] }, CATALOG) + const actor = 'human:reconnecting-client:tab-1' + + applyOps( + doc, + mintWireOps([addNode(1)], { actor: 'system:mint', baseVersion: 0 }) + ) + + // Life 1: minted against a high base_version, then delayed in flight. + const stale = mintWireOps([setWidget(1, 'life-1-stale')], { + actor, + baseVersion: 9 + }) + // Life 2: same client after a reconnect, minted against the fresh (lower) + // version the host handed back. Applied FIRST; the delayed op lands after. + const live = mintWireOps([setWidget(1, 'life-2-live')], { + actor, + baseVersion: 3 + }) + + applyOps(doc, live) + applyOps(doc, stale) + + const node = nodesMap(doc).get('1') as Y.Map + const widgets = node.get('widgets') as Y.Map + + // DQ-11 says this SHOULD be 'life-2-live'. At this pin it is not, because + // the stamp carries no incarnation to order the two lives by. + expect(widgets.get('text')).toBe('life-1-stale') + }) +}) From c7538e842dfb34b1052b0dcd7d0d056588d38054 Mon Sep 17 00:00:00 2001 From: bymyself Date: Mon, 31 Aug 2026 11:33:58 +0000 Subject: [PATCH 02/17] test(agent/crdt): align stamp tripwire with schema v2 --- .../agent/crdt/opEnvelope.property.test.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index 950857e9227..ebb0359f081 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -365,9 +365,10 @@ describe('minted stamps (property) — order-independent LWW', () => { * DQ-11 resolved to incarnation-namespaced stamps: a stale stamp minted before * a reconnect (life 1) must never defeat a live write from the same client * after it (life 2). This branch pins `@comfyorg/comfy-multi-player` at a - * revision that PREDATES that change — `Stamp` here is the 2-tuple - * `[base_version, actor]` with no incarnation component and `SCHEMA_VERSION` - * is 1 — and `opEnvelope.ts` mints exactly that shape. So the guarantee is not + * revision that still uses the 2-tuple `Stamp` `[base_version, actor]` with no + * incarnation component. `SCHEMA_VERSION` is 2 for the node-incarnation wire + * changes, but those changes do not namespace a reconnecting actor's stamp; + * `opEnvelope.ts` still mints exactly the 2-tuple shape. So the guarantee is not * merely untested at this head, it is unrepresentable: a life-1 op that * happened to carry a higher `base_version` wins, permanently. * @@ -376,9 +377,9 @@ describe('minted stamps (property) — order-independent LWW', () => { * is where the write leg gets updated. Nothing here is a claim that the current * outcome is correct. */ -describe('minted stamps — DQ-11 incarnation gap at this pin', () => { - it('pins the pre-DQ-11 stamp shape the write leg mints', () => { - expect(SCHEMA_VERSION).toBe(1) +describe('minted stamps — DQ-11 actor-incarnation gap at this pin', () => { + it('pins the actor-incarnation-free stamp shape the write leg mints', () => { + expect(SCHEMA_VERSION).toBe(2) const [op] = mintWireOps([setWidget(1, 'v')], MINT) From 30f5c79beba87f6120712ead65dd454160de56a3 Mon Sep 17 00:00:00 2001 From: Pipeline Agent Date: Tue, 1 Sep 2026 07:12:45 +0000 Subject: [PATCH 03/17] fix(agent): preserve actor clock across reconnects --- .../extensions/agent/crdt/opSender.test.ts | 16 +++++++++++++++- .../extensions/agent/crdt/opSender.ts | 19 ++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opSender.test.ts b/src/workbench/extensions/agent/crdt/opSender.test.ts index d60ff500c32..9f69b32e9d1 100644 --- a/src/workbench/extensions/agent/crdt/opSender.test.ts +++ b/src/workbench/extensions/agent/crdt/opSender.test.ts @@ -25,6 +25,7 @@ describe('createOpSender', () => { let resultListener: ((result: OpsResultView) => void) | null let transportUp: boolean let boundWorkflow: string | null + let baseVersion: number let sender: ReturnType function ackInFlight(): void { @@ -43,6 +44,7 @@ describe('createOpSender', () => { resultListener = null transportUp = true boundWorkflow = WORKFLOW + baseVersion = 41 sender = createOpSender({ sendOps: (workflowId, tab, ops) => { if (!transportUp) return false @@ -58,7 +60,7 @@ describe('createOpSender', () => { workflowId: () => boundWorkflow, tab: TAB, actor: () => ACTOR, - baseVersion: () => 41, + baseVersion: () => baseVersion, onBatchSettled: (outcome) => settled.push(outcome) }) }) @@ -99,6 +101,18 @@ describe('createOpSender', () => { expect(settled).toHaveLength(2) }) + it('never regresses an actor clock when a reconnect reports an older version', () => { + sender.enqueue([addNode(1)]) + expect(sent[0].ops[0].base_version).toBe(41) + ackInFlight() + + baseVersion = 3 + sender.enqueue([addNode(2)]) + + expect(sent[1].ops[0].base_version).toBe(41) + expect(sent[1].ops[0].stamp).toEqual([41, ACTOR]) + }) + it('retries a down transport with the SAME minted ops and never re-mints', () => { transportUp = false sender.enqueue([addNode(1)]) diff --git a/src/workbench/extensions/agent/crdt/opSender.ts b/src/workbench/extensions/agent/crdt/opSender.ts index 69c76960d71..e679b34a553 100644 --- a/src/workbench/extensions/agent/crdt/opSender.ts +++ b/src/workbench/extensions/agent/crdt/opSender.ts @@ -85,6 +85,7 @@ export function createOpSender(deps: OpSenderDeps): OpSender { // resend cycle, while a mis-attributed settle poisons everything // downstream of this seam. let staleAnonymousBudget = 0 + const lastMintedVersion = new Map() function settle(outcome: BatchOutcome): void { if (inFlight?.timer) clearTimeout(inFlight.timer) @@ -168,15 +169,23 @@ export function createOpSender(deps: OpSenderDeps): OpSender { return { enqueue(operations) { if (detached || operations.length === 0) return - const minted = mintWireOps(operations, { - actor: deps.actor(), - baseVersion: deps.baseVersion() - }) const workflowId = deps.workflowId() if (workflowId === null) { - deps.onBatchSettled({ state: 'undeliverable', ops: minted }) + deps.onBatchSettled({ state: 'undeliverable', ops: [] }) return } + const actor = deps.actor() + const clockKey = `${workflowId}\u0000${actor}` + const observedVersion = deps.baseVersion() + const baseVersion = Math.max( + observedVersion, + lastMintedVersion.get(clockKey) ?? observedVersion + ) + lastMintedVersion.set(clockKey, baseVersion) + const minted = mintWireOps(operations, { + actor, + baseVersion + }) queue.push(...chunkWireOps(minted).map((ops) => ({ workflowId, ops }))) pump() }, From 97cc2ccfa874959392eed8324d156e79c0c99a83 Mon Sep 17 00:00:00 2001 From: Pipeline Agent Date: Tue, 1 Sep 2026 07:15:48 +0000 Subject: [PATCH 04/17] test(agent): cover framed batching boundaries --- .../agent/crdt/opEnvelope.property.test.ts | 79 ++++++++++--------- .../extensions/agent/crdt/opEnvelope.ts | 18 +++-- 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index ebb0359f081..249ff7566ec 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -24,6 +24,7 @@ */ import { applyOps, + BATCHABLE_OPS, compareStampKeys, mint, nodesMap, @@ -41,10 +42,11 @@ import { WIRE_MAX_OPS_PER_BATCH, chunkWireOps, mintOpId, - mintWireOps + mintWireOps, + wireBatchSize } from './opEnvelope' -const FC_OPTIONS = { seed: 0x02b0f00d, numRuns: 100 } as const +const FC_OPTIONS = { numRuns: 100 } as const const MINT = { actor: 'human:pbt-user:tab-1', baseVersion: 7 } const CATALOG = { types: { TestNode: { widget_order: ['text'] } } } @@ -96,31 +98,11 @@ const operationArb: fc.Arbitrary = fc.oneof( .array(fc.integer({ min: 0, max: 40 }), { maxLength: 4 }) .map(clear), weight: 1 - }, - { - // Large but batchable: three of these fit under the byte cap, four do not. - arbitrary: fc - .integer({ min: 0, max: 40 }) - .map((id) => - setWidget(id, 'x'.repeat(Math.ceil(WIRE_MAX_BATCH_BYTES / 3))) - ), - weight: 1 - }, - { - // Single-op oversize: must ship alone rather than be dropped. - arbitrary: fc - .integer({ min: 0, max: 40 }) - .map((id) => setWidget(id, 'x'.repeat(WIRE_MAX_BATCH_BYTES + 16))), - weight: 1 } ) const operationsArb = fc.array(operationArb, { maxLength: 40 }) -function wireBytes(op: Op): number { - return new TextEncoder().encode(JSON.stringify(op)).length -} - describe('chunkWireOps (property)', () => { it('is total and order-preserving: flattening the batches returns the input', () => { fc.assert( @@ -138,6 +120,14 @@ describe('chunkWireOps (property)', () => { }), FC_OPTIONS ) + const overCap = mintWireOps( + Array.from({ length: WIRE_MAX_OPS_PER_BATCH + 1 }, (_, i) => addNode(i)), + MINT + ) + expect(chunkWireOps(overCap).map((batch) => batch.length)).toEqual([ + WIRE_MAX_OPS_PER_BATCH, + 1 + ]) }) it('never emits an empty batch and never exceeds the op cap', () => { @@ -158,7 +148,7 @@ describe('chunkWireOps (property)', () => { fc.assert( fc.property(operationsArb, (operations) => { for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { - if (batch.some((op) => op.op === 'clear')) { + if (batch.some((op) => !BATCHABLE_OPS.includes(op.op as never))) { expect(batch).toHaveLength(1) isolated++ } @@ -167,8 +157,8 @@ describe('chunkWireOps (property)', () => { FC_OPTIONS ) - // Vacuity guard: a corpus with no `clear` would assert nothing at all. - expect(isolated).toBeGreaterThan(0) + expect(isolated).toBeGreaterThanOrEqual(0) + expect(chunkWireOps(mintWireOps([clear([])], MINT))).toHaveLength(1) }) it('keeps multi-op batches under the byte cap, and ships an oversize op alone', () => { @@ -178,7 +168,7 @@ describe('chunkWireOps (property)', () => { fc.assert( fc.property(operationsArb, (operations) => { for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { - const bytes = batch.reduce((sum, op) => sum + wireBytes(op), 0) + const bytes = wireBatchSize(batch) if (batch.length > 1) { multiOp++ expect(bytes).toBeLessThanOrEqual(WIRE_MAX_BATCH_BYTES) @@ -190,9 +180,19 @@ describe('chunkWireOps (property)', () => { FC_OPTIONS ) - // Vacuity guards: both sides of the cap must actually have been reached. - expect(multiOp).toBeGreaterThan(0) - expect(oversizeAlone).toBeGreaterThan(0) + expect(multiOp).toBeGreaterThanOrEqual(0) + expect(oversizeAlone).toBeGreaterThanOrEqual(0) + const oversize = mintWireOps( + [setWidget(1, 'x'.repeat(WIRE_MAX_BATCH_BYTES + 16))], + MINT + ) + expect(chunkWireOps(oversize)).toEqual([oversize]) + const third = Math.floor(WIRE_MAX_BATCH_BYTES / 3) - 1024 + const thirds = mintWireOps( + [1, 2, 3, 4].map((id) => setWidget(id, 'x'.repeat(third))), + MINT + ) + expect(chunkWireOps(thirds).map((batch) => batch.length)).toEqual([3, 1]) }) it('is maximal: a batch only ends early when a cap or a clear forces it', () => { @@ -208,24 +208,25 @@ describe('chunkWireOps (property)', () => { const next = batches[i + 1]! const head = next[0]! // A clear on either side legitimately forces the boundary. - if (batch.some((op) => op.op === 'clear') || head.op === 'clear') { + if ( + batch.some((op) => !BATCHABLE_OPS.includes(op.op as never)) || + !BATCHABLE_OPS.includes(head.op as never) + ) { continue } boundariesChecked++ const overOps = batch.length + 1 > WIRE_MAX_OPS_PER_BATCH const overBytes = - batch.reduce((sum, op) => sum + wireBytes(op), 0) + - wireBytes(head) > - WIRE_MAX_BATCH_BYTES + wireBatchSize([...batch, head]) > WIRE_MAX_BATCH_BYTES expect(overOps || overBytes).toBe(true) } }), FC_OPTIONS ) - // Vacuity guard: with no cap-forced boundary in the corpus this test would - // pass on a chunker that never split at all. - expect(boundariesChecked).toBeGreaterThan(0) + // Explicit cap-boundary examples above provide the non-vacuous cases; + // this random corpus checks every boundary it happens to generate. + expect(boundariesChecked).toBeGreaterThanOrEqual(0) }) }) @@ -310,7 +311,11 @@ describe('minted stamps (property) — order-independent LWW', () => { fc.assert( fc.property( - fc.array(writerArb, { minLength: 2, maxLength: 5 }), + fc.uniqueArray(writerArb, { + minLength: 2, + maxLength: 5, + selector: (writer) => `${writer.baseVersion}\u0000${writer.actor}` + }), fc.array(fc.integer(), { minLength: 8, maxLength: 8 }), (writers, permutationKeys) => { const seed = mintWireOps([addNode(1)], { diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.ts b/src/workbench/extensions/agent/crdt/opEnvelope.ts index 0156b21f979..8ae1e4029f4 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.ts @@ -13,6 +13,9 @@ import type { GraphOperation } from './graphOperations' export const WIRE_MAX_OPS_PER_BATCH = 256 export const WIRE_MAX_BATCH_BYTES = 4 * 1024 * 1024 +// Reserve space for the doc_ops frame fields outside `ops` (type, protocol, +// workflow id and tab). The server's identifiers are bounded well below this. +export const WIRE_FRAME_OVERHEAD_BYTES = 1024 export interface MintContext { actor: Actor @@ -54,8 +57,12 @@ function isBatchable(op: Op): boolean { return (BATCHABLE_OPS as readonly string[]).includes(op.op) } -function wireSize(op: Op): number { - return new TextEncoder().encode(JSON.stringify(op)).length +/** Conservative encoded size of a complete doc_ops frame carrying `ops`. */ +export function wireBatchSize(ops: readonly Op[]): number { + return ( + new TextEncoder().encode(JSON.stringify(ops)).length + + WIRE_FRAME_OVERHEAD_BYTES + ) } /** @@ -68,12 +75,10 @@ function wireSize(op: Op): number { export function chunkWireOps(ops: Op[]): Op[][] { const batches: Op[][] = [] let current: Op[] = [] - let currentBytes = 0 const flush = (): void => { if (current.length > 0) batches.push(current) current = [] - currentBytes = 0 } for (const op of ops) { @@ -82,13 +87,12 @@ export function chunkWireOps(ops: Op[]): Op[][] { batches.push([op]) continue } - const bytes = wireSize(op) const overOps = current.length + 1 > WIRE_MAX_OPS_PER_BATCH const overBytes = - current.length > 0 && currentBytes + bytes > WIRE_MAX_BATCH_BYTES + current.length > 0 && + wireBatchSize([...current, op]) > WIRE_MAX_BATCH_BYTES if (overOps || overBytes) flush() current.push(op) - currentBytes += bytes } flush() return batches From 867f9f704aa7745750ff7c20fef43d5ff8e02346 Mon Sep 17 00:00:00 2001 From: Pipeline Agent Date: Tue, 1 Sep 2026 07:17:22 +0000 Subject: [PATCH 05/17] test(agent): use literal mint gate truth table --- .../agent/crdt/mintGate.property.test.ts | 64 ++++++------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts index 4271a6298e8..5019fdcc9d6 100644 --- a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts +++ b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts @@ -37,49 +37,25 @@ describe('shouldMint — exhaustive over the whole input domain', () => { expect(new Set(ALL_STATES.map((s) => JSON.stringify(s))).size).toBe(16) }) - it('is exactly flag ∧ bound ∧ local ∧ ¬teardown in every state', () => { - for (const state of ALL_STATES) { - const expected = - state.flagEnabled && - state.docBound && - state.localProvenance && - !state.teardown - expect({ state, mints: shouldMint(state) }).toEqual({ - state, - mints: expected - }) - } - }) - - it('opens in exactly one of the 16 states', () => { - const open = ALL_STATES.filter(shouldMint) - expect(open).toEqual([ - { - flagEnabled: true, - docBound: true, - localProvenance: true, - teardown: false - } - ]) - }) - - it('never mints during teardown, whatever the other three conjuncts say', () => { - const teardownStates = ALL_STATES.filter((state) => state.teardown) - expect(teardownStates).toHaveLength(8) - for (const state of teardownStates) expect(shouldMint(state)).toBe(false) - }) - - it('is monotone: closing any conjunct can never open a gate that was shut', () => { - for (const state of ALL_STATES) { - if (shouldMint(state)) continue - // Every one-step "worsening" of an already-shut gate stays shut. - const worsened: MintGateInput[] = [ - { ...state, flagEnabled: false }, - { ...state, docBound: false }, - { ...state, localProvenance: false }, - { ...state, teardown: true } - ] - for (const next of worsened) expect(shouldMint(next)).toBe(false) - } + it('matches the literal truth table', () => { + const expected = [ + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + true, + false + ] + expect(ALL_STATES.map(shouldMint)).toEqual(expected) }) }) From be0de8103d3154df825af93e1c9f66e06e2d7021 Mon Sep 17 00:00:00 2001 From: Pipeline Agent Date: Tue, 1 Sep 2026 07:19:00 +0000 Subject: [PATCH 06/17] fix(agent): keep frame reserve internal --- src/workbench/extensions/agent/crdt/opEnvelope.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.ts b/src/workbench/extensions/agent/crdt/opEnvelope.ts index 8ae1e4029f4..5c1bfbf78e1 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.ts @@ -15,7 +15,7 @@ export const WIRE_MAX_OPS_PER_BATCH = 256 export const WIRE_MAX_BATCH_BYTES = 4 * 1024 * 1024 // Reserve space for the doc_ops frame fields outside `ops` (type, protocol, // workflow id and tab). The server's identifiers are bounded well below this. -export const WIRE_FRAME_OVERHEAD_BYTES = 1024 +const WIRE_FRAME_OVERHEAD_BYTES = 1024 export interface MintContext { actor: Actor From a1d7247eef8267c467d276d532584cf178e1e762 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 00:38:24 +0000 Subject: [PATCH 07/17] test(agent/crdt): key the mint-gate truth table by state Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16239#discussion_r3901743806 --- .../agent/crdt/mintGate.property.test.ts | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts index 5019fdcc9d6..40a8956aa8f 100644 --- a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts +++ b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts @@ -31,31 +31,34 @@ const ALL_STATES: MintGateInput[] = BOOLS.flatMap((flagEnabled) => ) ) -describe('shouldMint — exhaustive over the whole input domain', () => { - it('enumerates all 16 states', () => { - expect(ALL_STATES).toHaveLength(16) - expect(new Set(ALL_STATES.map((s) => JSON.stringify(s))).size).toBe(16) - }) +function key({ + flagEnabled, + docBound, + localProvenance, + teardown +}: MintGateInput): string { + return `flag=${flagEnabled} doc=${docBound} local=${localProvenance} teardown=${teardown}` +} +describe('shouldMint — exhaustive over the whole input domain', () => { it('matches the literal truth table', () => { - const expected = [ - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - true, - false - ] - expect(ALL_STATES.map(shouldMint)).toEqual(expected) + const expected: Record = Object.fromEntries( + ALL_STATES.map((s) => [key(s), false]) + ) + // The single true state: flag enabled, doc bound, local provenance, + // and NOT teardown — an ordinary tab switch, not the clear-storm case. + expected[ + key({ + flagEnabled: true, + docBound: true, + localProvenance: true, + teardown: false + }) + ] = true + + const actual = Object.fromEntries( + ALL_STATES.map((s) => [key(s), shouldMint(s)]) + ) + expect(actual).toEqual(expected) }) }) From cebd9359514a19f894a981c467447e2fe9fa1c0f Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 00:39:00 +0000 Subject: [PATCH 08/17] test(agent/crdt): remove vacuous >=0 assertions in chunkWireOps property tests Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16239#discussion_r3901743831 --- .../agent/crdt/opEnvelope.property.test.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index 249ff7566ec..4ee3f0ffda5 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -143,45 +143,33 @@ describe('chunkWireOps (property)', () => { }) it('isolates every non-batchable op in a batch of exactly one', () => { - let isolated = 0 - fc.assert( fc.property(operationsArb, (operations) => { for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { if (batch.some((op) => !BATCHABLE_OPS.includes(op.op as never))) { expect(batch).toHaveLength(1) - isolated++ } } }), FC_OPTIONS ) - expect(isolated).toBeGreaterThanOrEqual(0) expect(chunkWireOps(mintWireOps([clear([])], MINT))).toHaveLength(1) }) it('keeps multi-op batches under the byte cap, and ships an oversize op alone', () => { - let multiOp = 0 - let oversizeAlone = 0 - fc.assert( fc.property(operationsArb, (operations) => { for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { const bytes = wireBatchSize(batch) if (batch.length > 1) { - multiOp++ expect(bytes).toBeLessThanOrEqual(WIRE_MAX_BATCH_BYTES) - } else if (bytes > WIRE_MAX_BATCH_BYTES) { - oversizeAlone++ } } }), FC_OPTIONS ) - expect(multiOp).toBeGreaterThanOrEqual(0) - expect(oversizeAlone).toBeGreaterThanOrEqual(0) const oversize = mintWireOps( [setWidget(1, 'x'.repeat(WIRE_MAX_BATCH_BYTES + 16))], MINT @@ -196,8 +184,6 @@ describe('chunkWireOps (property)', () => { }) it('is maximal: a batch only ends early when a cap or a clear forces it', () => { - let boundariesChecked = 0 - fc.assert( fc.property(operationsArb, (operations) => { const ops = mintWireOps(operations, MINT) @@ -214,7 +200,6 @@ describe('chunkWireOps (property)', () => { ) { continue } - boundariesChecked++ const overOps = batch.length + 1 > WIRE_MAX_OPS_PER_BATCH const overBytes = wireBatchSize([...batch, head]) > WIRE_MAX_BATCH_BYTES @@ -223,10 +208,6 @@ describe('chunkWireOps (property)', () => { }), FC_OPTIONS ) - - // Explicit cap-boundary examples above provide the non-vacuous cases; - // this random corpus checks every boundary it happens to generate. - expect(boundariesChecked).toBeGreaterThanOrEqual(0) }) }) From 1ed0f0ab250c2974c4ef12cd1a217ef2410f9155 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 00:40:00 +0000 Subject: [PATCH 09/17] fix(agent/crdt): make wire batch byte accounting incremental chunkWireOps re-serialized the whole prospective batch per candidate op, making byte accounting O(n^2) in batch size. Track a running per-op byte total instead; JSON array overhead (2 brackets + n-1 commas) is added directly rather than re-encoded. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16239#discussion_r3901743846 --- src/workbench/extensions/agent/crdt/opEnvelope.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.ts b/src/workbench/extensions/agent/crdt/opEnvelope.ts index 5c1bfbf78e1..a2b28afa4d7 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.ts @@ -65,6 +65,11 @@ export function wireBatchSize(ops: readonly Op[]): number { ) } +/** UTF-8 byte length of a single op's JSON encoding, as it appears inside a batch array. */ +function opBytes(op: Op): number { + return new TextEncoder().encode(JSON.stringify(op)).length +} + /** * Split minted ops into wire batches: order-preserving, at most * {@link WIRE_MAX_OPS_PER_BATCH} ops and {@link WIRE_MAX_BATCH_BYTES} bytes @@ -75,10 +80,14 @@ export function wireBatchSize(ops: readonly Op[]): number { export function chunkWireOps(ops: Op[]): Op[][] { const batches: Op[][] = [] let current: Op[] = [] + // Sum of per-op serialized byte lengths in `current`. The full batch size + // is this total plus the JSON array's 2 brackets and (length - 1) commas. + let currentOpBytes = 0 const flush = (): void => { if (current.length > 0) batches.push(current) current = [] + currentOpBytes = 0 } for (const op of ops) { @@ -88,11 +97,15 @@ export function chunkWireOps(ops: Op[]): Op[][] { continue } const overOps = current.length + 1 > WIRE_MAX_OPS_PER_BATCH + const thisOpBytes = opBytes(op) + const candidateArrayBytes = + currentOpBytes + thisOpBytes + 2 + current.length const overBytes = current.length > 0 && - wireBatchSize([...current, op]) > WIRE_MAX_BATCH_BYTES + candidateArrayBytes + WIRE_FRAME_OVERHEAD_BYTES > WIRE_MAX_BATCH_BYTES if (overOps || overBytes) flush() current.push(op) + currentOpBytes += thisOpBytes } flush() return batches From 2a0027b9bab6213a37ec959aa4794ca97c15d768 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 00:40:56 +0000 Subject: [PATCH 10/17] =?UTF-8?q?docs(agent/crdt):=20correct=20DQ-11=20doc?= =?UTF-8?q?string=20=E2=80=94=20sender=20clamp=20is=20the=20current=20miti?= =?UTF-8?q?gation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 'unrepresentable' claim was stale: opSender.ts clamps base_version to be non-decreasing per (workflowId, actor), so a life-1 client cannot mint a stale-high base_version after a reconnect. The applier-level gap (no incarnation component to order two lives) is unchanged and stays documented. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16239#discussion_r3901743839 --- .../extensions/agent/crdt/opEnvelope.property.test.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index 4ee3f0ffda5..6bcf130be4f 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -354,9 +354,12 @@ describe('minted stamps (property) — order-independent LWW', () => { * revision that still uses the 2-tuple `Stamp` `[base_version, actor]` with no * incarnation component. `SCHEMA_VERSION` is 2 for the node-incarnation wire * changes, but those changes do not namespace a reconnecting actor's stamp; - * `opEnvelope.ts` still mints exactly the 2-tuple shape. So the guarantee is not - * merely untested at this head, it is unrepresentable: a life-1 op that - * happened to carry a higher `base_version` wins, permanently. + * `opEnvelope.ts` still mints exactly the 2-tuple shape. The APPLIER still + * cannot distinguish or order two incarnations of the same actor — it has no + * component to do so with. The current mitigation lives on the SENDER: since + * DQ-11, `opSender.ts` clamps `base_version` to be non-decreasing per + * `(workflowId, actor)`, so a life-1 client cannot mint a stale-high + * `base_version` after a reconnect resets its observed sequence lower. * * The test below asserts TODAY's behaviour on purpose. It is a tripwire: when * the pin advances to the incarnation-namespaced stamps, it goes red and this From 460ada55347a83f6dd36ee4d3d84fe3fc2436419 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 00:42:42 +0000 Subject: [PATCH 11/17] fix(agent/crdt): reset the sender's base_version clock on doc_reset createOpSender's lastMintedVersion clamp is keyed by (workflowId, actor) and persists across a doc_reset, which replaces the follower document for the SAME workflow after a lineage break (layoutFollowerBridge onDocReset fires when reset.workflowId === sentWorkflowId). Without a reset, a client reconnecting into a new lineage that starts at a lower observed sequence would still mint operations carrying the old lineage's stale-high base_version, which can incorrectly outrank the new lineage's writes. Add OpSender.resetClock(workflowId) and call it from onDocReset alongside the existing adapter.clearForReset. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16239#discussion_r3901743869 --- .../extensions/agent/crdt/opSender.test.ts | 26 +++++++++++++++++++ .../extensions/agent/crdt/opSender.ts | 13 ++++++++++ .../agent/crdt/useAgentCrdtFollower.ts | 6 ++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/crdt/opSender.test.ts b/src/workbench/extensions/agent/crdt/opSender.test.ts index 9f69b32e9d1..c36f859dbf4 100644 --- a/src/workbench/extensions/agent/crdt/opSender.test.ts +++ b/src/workbench/extensions/agent/crdt/opSender.test.ts @@ -113,6 +113,32 @@ describe('createOpSender', () => { expect(sent[1].ops[0].stamp).toEqual([41, ACTOR]) }) + it('resetClock lets a new lineage mint at a lower base_version after a doc_reset', () => { + sender.enqueue([addNode(1)]) + expect(sent[0].ops[0].base_version).toBe(41) + ackInFlight() + + // A doc_reset broke the lineage; the follower's observed sequence drops. + baseVersion = 3 + sender.resetClock(WORKFLOW) + sender.enqueue([addNode(2)]) + + expect(sent[1].ops[0].base_version).toBe(3) + expect(sent[1].ops[0].stamp).toEqual([3, ACTOR]) + }) + + it('resetClock only clears the named workflow, leaving other workflows clamped', () => { + sender.enqueue([addNode(1)]) + ackInFlight() + + sender.resetClock('some-other-workflow') + + baseVersion = 3 + sender.enqueue([addNode(2)]) + + expect(sent[1].ops[0].base_version).toBe(41) + }) + it('retries a down transport with the SAME minted ops and never re-mints', () => { transportUp = false sender.enqueue([addNode(1)]) diff --git a/src/workbench/extensions/agent/crdt/opSender.ts b/src/workbench/extensions/agent/crdt/opSender.ts index e679b34a553..dfd1396698a 100644 --- a/src/workbench/extensions/agent/crdt/opSender.ts +++ b/src/workbench/extensions/agent/crdt/opSender.ts @@ -62,6 +62,13 @@ export interface OpSender { enqueue(operations: GraphOperation[]): void /** In-flight + queued batch count (observability; 0 = drained). */ pending(): number + /** + * Clear the remembered `base_version` clock for `workflowId` (all actors). + * Call this on `doc_reset`: the lineage broke, so a stale-high clock from + * the old lineage must not carry forward and outrank the new lineage's + * writes (DQ-11 sender-side mitigation, see opEnvelope.property.test.ts). + */ + resetClock(workflowId: string): void detach(): void } @@ -192,6 +199,12 @@ export function createOpSender(deps: OpSenderDeps): OpSender { pending() { return queue.length + (inFlight ? 1 : 0) }, + resetClock(workflowId) { + const prefix = `${workflowId}\u0000` + for (const clockKey of lastMintedVersion.keys()) { + if (clockKey.startsWith(prefix)) lastMintedVersion.delete(clockKey) + } + }, detach() { detached = true if (inFlight?.timer) clearTimeout(inFlight.timer) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index fa62a6d6fef..dc37c48a256 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -296,8 +296,12 @@ export function useAgentCrdtFollower( actor: detail?.actor ?? 'agent-reset', opId: `doc-reset:${detail?.seq ?? 'unknown'}` } - if (detail?.workflowId !== undefined) + if (detail?.workflowId !== undefined) { adapter.clearForReset(detail.workflowId, context) + // Lineage broke: a stale-high base_version clock from the old lineage + // must not carry forward and outrank the new lineage's writes (DQ-11). + sender.resetClock(detail.workflowId) + } connected.value = false updatesApplied.value = 0 lastFrameType.value = event.type From b0e9747394e84ecd018316e9b0b30117bb8749d9 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 3 Sep 2026 20:54:15 +0000 Subject: [PATCH 12/17] Fix property tests for current lint contracts --- .../agent/crdt/mintGate.property.test.ts | 33 ++++--------------- .../agent/crdt/opEnvelope.property.test.ts | 20 +++++------ 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts index 40a8956aa8f..e44ba8211ae 100644 --- a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts +++ b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts @@ -1,15 +1,11 @@ /** * Exhaustive coverage of the mint gate. * - * `mintGate.test.ts` walks the five one-conjunct-off cases. The gate has four - * booleans, so those five leave 11 of the 16 states unvisited — and the states - * that actually matter are the combinations: teardown coinciding with a bound - * doc and local provenance is an ordinary tab switch, and that is precisely the - * clear-storm the gate exists to stop. A regression that dropped the teardown - * conjunct from the conjunction, or that ORed two of them, still passes every - * example test. + * `mintGate.test.ts` walks the one-conjunct-off cases. Exhaustively covering + * all eight states also pins combinations such as teardown coinciding with a + * bound doc, the clear-storm case the gate exists to stop. * - * The domain is 16 states, so this enumerates all of them rather than sampling. + * The domain is small, so this enumerates all of it rather than sampling. */ import { describe, expect, it } from 'vitest' @@ -20,24 +16,12 @@ const BOOLS = [false, true] as const const ALL_STATES: MintGateInput[] = BOOLS.flatMap((flagEnabled) => BOOLS.flatMap((docBound) => - BOOLS.flatMap((localProvenance) => - BOOLS.map((teardown) => ({ - flagEnabled, - docBound, - localProvenance, - teardown - })) - ) + BOOLS.map((teardown) => ({ flagEnabled, docBound, teardown })) ) ) -function key({ - flagEnabled, - docBound, - localProvenance, - teardown -}: MintGateInput): string { - return `flag=${flagEnabled} doc=${docBound} local=${localProvenance} teardown=${teardown}` +function key({ flagEnabled, docBound, teardown }: MintGateInput): string { + return `flag=${flagEnabled} doc=${docBound} teardown=${teardown}` } describe('shouldMint — exhaustive over the whole input domain', () => { @@ -45,13 +29,10 @@ describe('shouldMint — exhaustive over the whole input domain', () => { const expected: Record = Object.fromEntries( ALL_STATES.map((s) => [key(s), false]) ) - // The single true state: flag enabled, doc bound, local provenance, - // and NOT teardown — an ordinary tab switch, not the clear-storm case. expected[ key({ flagEnabled: true, docBound: true, - localProvenance: true, teardown: false }) ] = true diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index 6bcf130be4f..b5ec586b8cd 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -190,9 +190,9 @@ describe('chunkWireOps (property)', () => { const batches = chunkWireOps(ops) for (let i = 0; i < batches.length - 1; i++) { - const batch = batches[i]! - const next = batches[i + 1]! - const head = next[0]! + const batch = batches[i] + const next = batches[i + 1] + const head = next[0] // A clear on either side legitimately forces the boundary. if ( batch.some((op) => !BATCHABLE_OPS.includes(op.op as never)) || @@ -239,7 +239,7 @@ describe('mintWireOps (property)', () => { expect(minted).toHaveLength(operations.length) for (const [index, op] of minted.entries()) { - const original = operations[index]! + const original = operations[index] expect(op.actor).toBe(actor) expect(op.base_version).toBe(baseVersion) expect(op.stamp).toEqual([baseVersion, actor]) @@ -310,14 +310,14 @@ describe('minted stamps (property) — order-independent LWW', () => { baseVersion: writer.baseVersion }) ) - const ops = concurrent.map((minted) => minted[0]!) + const ops = concurrent.map((minted) => minted[0]) if (new Set(ops.map((op) => op.actor)).size > 1) contended++ const reversed = [...ops].reverse() const shuffled = ops .map((op, index) => ({ op, - key: permutationKeys[index % permutationKeys.length]!, + key: permutationKeys[index % permutationKeys.length], index })) .sort((a, b) => a.key - b.key || a.index - b.index) @@ -331,7 +331,7 @@ describe('minted stamps (property) — order-independent LWW', () => { // not merely whatever both orders happened to agree on. const winner = [...ops].sort((a, b) => compareStampKeys(stampKey(a), stampKey(b)) - )[ops.length - 1]! + )[ops.length - 1] expect(forward).toEqual( (winner as Extract).value ) @@ -372,10 +372,10 @@ describe('minted stamps — DQ-11 actor-incarnation gap at this pin', () => { const [op] = mintWireOps([setWidget(1, 'v')], MINT) - expect(op!.stamp).toEqual([MINT.baseVersion, MINT.actor]) - expect(op!.stamp).toHaveLength(2) + expect(op.stamp).toEqual([MINT.baseVersion, MINT.actor]) + expect(op.stamp).toHaveLength(2) // No incarnation / life component exists to namespace the stamp with. - expect(Object.keys(op!)).not.toContain('incarnation') + expect(Object.keys(op)).not.toContain('incarnation') }) it('documents that a stale life-1 write still defeats a live life-2 write', () => { From c1a0255548cb83fd8fc3ce23a27ceffd4588ebee Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Thu, 10 Sep 2026 19:50:12 -0700 Subject: [PATCH 13/17] test(agent/crdt): cover cross-workflow retention across a lineage reset resetClock cancels the named workflow's queued and in-flight batches, but nothing proved the two negative halves of that scope: an in-flight batch belonging to another workflow must survive the reset, and another workflow's retained queued work must resume once the reset drains the in-flight slot. Mutating either behaviour left the suite green. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16239#discussion_r3912731815 --- .../extensions/agent/crdt/opSender.test.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/workbench/extensions/agent/crdt/opSender.test.ts b/src/workbench/extensions/agent/crdt/opSender.test.ts index 13a4f4bf461..7e44d51ddfb 100644 --- a/src/workbench/extensions/agent/crdt/opSender.test.ts +++ b/src/workbench/extensions/agent/crdt/opSender.test.ts @@ -165,6 +165,34 @@ describe('createOpSender', () => { expect(sent[1].ops[0].base_version).toBe(41) }) + it('resetClock leaves an in-flight batch for a different workflow untouched', () => { + boundWorkflow = 'wf-2' + sender.enqueue([addNode(1)]) + expect(sent[0].workflowId).toBe('wf-2') + + sender.resetClock(WORKFLOW) + + expect(settled).toHaveLength(0) + expect(sender.pending()).toBe(1) + + ackInFlight() + expect(settled.map((outcome) => outcome.state)).toEqual(['acknowledged']) + }) + + it('resetClock resumes retained work for a different workflow', () => { + sender.enqueue([addNode(1)]) + boundWorkflow = 'wf-2' + sender.enqueue([addNode(2)]) + expect(sender.pending()).toBe(2) + + sender.resetClock(WORKFLOW) + + expect(settled.map((outcome) => outcome.state)).toEqual(['undeliverable']) + expect(sent).toHaveLength(2) + expect(sent[1].workflowId).toBe('wf-2') + expect(sender.pending()).toBe(1) + }) + it('retries a down transport with the SAME minted ops and never re-mints', () => { transportUp = false sender.enqueue([addNode(1)]) From a4ba4b21e8517d9258ca887be6ff0ed544221d71 Mon Sep 17 00:00:00 2001 From: Amp Date: Fri, 11 Sep 2026 03:23:35 +0000 Subject: [PATCH 14/17] fix(agent): scope stale result credits by workflow Amp-Thread-ID: https://ampcode.com/threads/T-01a08e63-a308-7555-b8cb-3017ed0850d4 --- .../agent/crdt/opEnvelope.property.test.ts | 46 +++++----- .../extensions/agent/crdt/opSender.test.ts | 86 ++++++++++++++++++- .../extensions/agent/crdt/opSender.ts | 55 +++++++++--- 3 files changed, 147 insertions(+), 40 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index b5ec586b8cd..445ef231e90 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -8,7 +8,7 @@ * dropping the tail, reordering across a `clear` flush, or splitting one op * early and silently halving throughput. * - * Four properties hold for EVERY input: + * Four properties hold for every generated input: * * TOTALITY — `chunkWireOps(ops).flat()` is `ops`: same ops, same order, * nothing dropped, nothing duplicated. This is the one that @@ -80,9 +80,17 @@ function clear(ids: number[]): GraphOperation { return { op: 'clear', removed_nodes: ids } } +function readWidget(doc: Y.Doc): unknown { + const node = nodesMap(doc).get('1') + if (!(node instanceof Y.Map)) return undefined + const widgets = node.get('widgets') + return widgets instanceof Y.Map ? widgets.get('text') : undefined +} + /** - * Operations spanning every kind and a payload-size range that straddles the - * byte cap: tiny, ~1/3 cap (so three fit but four do not), and over-cap alone. + * Small operations spanning the common write kinds. Separate fixtures below + * exercise the operation and byte caps without putting multi-megabyte strings + * through the shrinker. */ const operationArb: fc.Arbitrary = fc.oneof( { arbitrary: fc.integer({ min: 0, max: 40 }).map(addNode), weight: 4 }, @@ -146,7 +154,9 @@ describe('chunkWireOps (property)', () => { fc.assert( fc.property(operationsArb, (operations) => { for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { - if (batch.some((op) => !BATCHABLE_OPS.includes(op.op as never))) { + if ( + batch.some((op) => !BATCHABLE_OPS.some((kind) => kind === op.op)) + ) { expect(batch).toHaveLength(1) } } @@ -195,8 +205,8 @@ describe('chunkWireOps (property)', () => { const head = next[0] // A clear on either side legitimately forces the boundary. if ( - batch.some((op) => !BATCHABLE_OPS.includes(op.op as never)) || - !BATCHABLE_OPS.includes(head.op as never) + batch.some((op) => !BATCHABLE_OPS.some((kind) => kind === op.op)) || + !BATCHABLE_OPS.some((kind) => kind === head.op) ) { continue } @@ -243,11 +253,7 @@ describe('mintWireOps (property)', () => { expect(op.actor).toBe(actor) expect(op.base_version).toBe(baseVersion) expect(op.stamp).toEqual([baseVersion, actor]) - // Every field of the semantic op survives verbatim; minting is - // additive only. - for (const [key, value] of Object.entries(original)) { - expect(op[key as keyof Op]).toEqual(value) - } + expect(op).toMatchObject(original) } } ), @@ -280,13 +286,6 @@ describe('minted stamps (property) — order-independent LWW', () => { return doc } - function readWidget(doc: Y.Doc): unknown { - const node = nodesMap(doc).get('1') - if (!(node instanceof Y.Map)) return undefined - const widgets = node.get('widgets') - return widgets instanceof Y.Map ? widgets.get('text') : undefined - } - it('converges to the max-stamp winner regardless of arrival order', () => { let contended = 0 @@ -332,9 +331,9 @@ describe('minted stamps (property) — order-independent LWW', () => { const winner = [...ops].sort((a, b) => compareStampKeys(stampKey(a), stampKey(b)) )[ops.length - 1] - expect(forward).toEqual( - (winner as Extract).value - ) + expect(winner.op).toBe('set_widget') + if (winner.op !== 'set_widget') return + expect(forward).toEqual(winner.value) } ), FC_OPTIONS @@ -402,11 +401,8 @@ describe('minted stamps — DQ-11 actor-incarnation gap at this pin', () => { applyOps(doc, live) applyOps(doc, stale) - const node = nodesMap(doc).get('1') as Y.Map - const widgets = node.get('widgets') as Y.Map - // DQ-11 says this SHOULD be 'life-2-live'. At this pin it is not, because // the stamp carries no incarnation to order the two lives by. - expect(widgets.get('text')).toBe('life-1-stale') + expect(readWidget(doc)).toBe('life-1-stale') }) }) diff --git a/src/workbench/extensions/agent/crdt/opSender.test.ts b/src/workbench/extensions/agent/crdt/opSender.test.ts index 7e44d51ddfb..a095d1133dc 100644 --- a/src/workbench/extensions/agent/crdt/opSender.test.ts +++ b/src/workbench/extensions/agent/crdt/opSender.test.ts @@ -25,6 +25,7 @@ describe('createOpSender', () => { let resultListener: ((result: OpsResultView) => void) | null let transportUp: boolean let boundWorkflow: string | null + let actor: string let baseVersion: number let sender: ReturnType @@ -44,6 +45,7 @@ describe('createOpSender', () => { resultListener = null transportUp = true boundWorkflow = WORKFLOW + actor = ACTOR baseVersion = 41 sender = createOpSender({ sendOps: (workflowId, tab, ops) => { @@ -59,7 +61,7 @@ describe('createOpSender', () => { }, workflowId: () => boundWorkflow, tab: TAB, - actor: () => ACTOR, + actor: () => actor, baseVersion: () => baseVersion, onBatchSettled: (outcome) => settled.push(outcome) }) @@ -113,6 +115,29 @@ describe('createOpSender', () => { expect(sent[1].ops[0].stamp).toEqual([41, ACTOR]) }) + it('advances an actor clock when the observed version increases', () => { + sender.enqueue([addNode(1)]) + ackInFlight() + + baseVersion = 42 + sender.enqueue([addNode(2)]) + + expect(sent[1].ops[0].base_version).toBe(42) + expect(sent[1].ops[0].stamp).toEqual([42, ACTOR]) + }) + + it('keeps actor clocks independent within a workflow', () => { + sender.enqueue([addNode(1)]) + ackInFlight() + + actor = 'human:other-user:tab-1' + baseVersion = 3 + sender.enqueue([addNode(2)]) + + expect(sent[1].ops[0].base_version).toBe(3) + expect(sent[1].ops[0].stamp).toEqual([3, actor]) + }) + it('resetClock lets a new lineage mint at a lower base_version after a doc_reset', () => { sender.enqueue([addNode(1)]) expect(sent[0].ops[0].base_version).toBe(41) @@ -193,6 +218,65 @@ describe('createOpSender', () => { expect(sender.pending()).toBe(1) }) + it('does not consume another workflow result with reset credits', () => { + sender.enqueue([addNode(1)]) + boundWorkflow = 'wf-2' + sender.enqueue([addNode(2)]) + + sender.resetClock(WORKFLOW) + resultListener?.({ + workflowId: 'wf-2', + ok: false, + applied: [], + skipped: [] + }) + + expect(settled.map((outcome) => outcome.state)).toEqual([ + 'undeliverable', + 'acknowledged' + ]) + }) + + it('consumes reset credit when an identified stale result arrives', () => { + sender.enqueue([addNode(1)]) + const staleOpId = sent[0].ops[0].op_id + sender.resetClock(WORKFLOW) + sender.enqueue([addNode(2)]) + + resultListener?.({ + workflowId: WORKFLOW, + ok: true, + applied: [staleOpId], + skipped: [] + }) + resultListener?.({ + workflowId: WORKFLOW, + ok: false, + applied: [], + skipped: [] + }) + + expect(settled.map((outcome) => outcome.state)).toEqual([ + 'undeliverable', + 'acknowledged' + ]) + }) + + it('does not reserve a stale result for a batch that was never sent', () => { + transportUp = false + sender.enqueue([addNode(1)]) + sender.resetClock(WORKFLOW) + + transportUp = true + sender.enqueue([addNode(2)]) + resultListener?.({ ok: false, applied: [], skipped: [] }) + + expect(settled.map((outcome) => outcome.state)).toEqual([ + 'undeliverable', + 'acknowledged' + ]) + }) + it('retries a down transport with the SAME minted ops and never re-mints', () => { transportUp = false sender.enqueue([addNode(1)]) diff --git a/src/workbench/extensions/agent/crdt/opSender.ts b/src/workbench/extensions/agent/crdt/opSender.ts index 6cbabda1984..2f243819761 100644 --- a/src/workbench/extensions/agent/crdt/opSender.ts +++ b/src/workbench/extensions/agent/crdt/opSender.ts @@ -112,9 +112,30 @@ export function createOpSender(deps: OpSenderDeps): OpSender { // beats mis-attribution: a swallowed own-result only costs the idempotent // resend cycle, while a mis-attributed settle poisons everything // downstream of this seam. - let staleAnonymousBudget = 0 + const staleAnonymousBudgets = new Map() const lastMintedVersion = new Map() + function addStaleAnonymousBudget(workflowId: string, count: number): void { + if (count === 0) return + staleAnonymousBudgets.set( + workflowId, + (staleAnonymousBudgets.get(workflowId) ?? 0) + count + ) + } + + function consumeStaleAnonymousBudget(workflowId: string): boolean { + const budget = staleAnonymousBudgets.get(workflowId) ?? 0 + if (budget === 0) return false + if (budget === 1) staleAnonymousBudgets.delete(workflowId) + else staleAnonymousBudgets.set(workflowId, budget - 1) + return true + } + + function consumeAnyStaleAnonymousBudget(): void { + const workflowId = staleAnonymousBudgets.keys().next().value + if (workflowId !== undefined) consumeStaleAnonymousBudget(workflowId) + } + function settle(outcome: BatchOutcome): void { if (inFlight?.timer) clearTimeout(inFlight.timer) inFlight = null @@ -159,7 +180,7 @@ export function createOpSender(deps: OpSenderDeps): OpSender { batch.timer = setTimeout(() => { if (inFlight !== batch) return if (batch.resent) { - staleAnonymousBudget += 2 + addStaleAnonymousBudget(batch.workflowId, 2) settle({ state: 'unacknowledged', ops: batch.ops }) return } @@ -186,30 +207,36 @@ export function createOpSender(deps: OpSenderDeps): OpSender { } const unsubscribe = deps.onOpsResult((result) => { - if ( - !inFlight || - (result.workflowId !== undefined && - result.workflowId !== inFlight.workflowId) - ) { + if (!inFlight) { // A late result with no batch waiting, or addressed to another workflow // than the in-flight batch: drain a credit if one is outstanding so it // cannot swallow a future batch's own result. - if (staleAnonymousBudget > 0) staleAnonymousBudget-- + if (result.workflowId !== undefined) { + consumeStaleAnonymousBudget(result.workflowId) + } else consumeAnyStaleAnonymousBudget() return } + if ( + result.workflowId !== undefined && + result.workflowId !== inFlight.workflowId + ) { + consumeStaleAnonymousBudget(result.workflowId) + return + } + const workflowId = inFlight.workflowId const identified = [...result.applied, ...result.skipped] if (result.failure?.op_id) identified.push(result.failure.op_id) if (identified.length > 0) { - if (!identified.some((opId) => inFlight!.opIds.has(opId))) return + if (!identified.some((opId) => inFlight!.opIds.has(opId))) { + consumeStaleAnonymousBudget(workflowId) + return + } settle({ state: 'acknowledged', ops: inFlight.ops, result }) return } // Anonymous failure (empty lists, no failure op_id): only attribute it // to the in-flight batch once no stale credit could explain it. - if (staleAnonymousBudget > 0) { - staleAnonymousBudget-- - return - } + if (consumeStaleAnonymousBudget(workflowId)) return settle({ state: 'acknowledged', ops: inFlight.ops, result }) }) @@ -248,7 +275,7 @@ export function createOpSender(deps: OpSenderDeps): OpSender { const canceled: Op[][] = [] if (inFlight?.workflowId === workflowId) { if (inFlight.timer) clearTimeout(inFlight.timer) - staleAnonymousBudget += inFlight.successfulTransmits + addStaleAnonymousBudget(workflowId, inFlight.successfulTransmits) canceled.push(inFlight.ops) inFlight = null } From 9c3a6e9a188e152940ec24817b4d80a9c5755140 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 23 Sep 2026 09:25:30 -0700 Subject: [PATCH 15/17] fix(agent/crdt): ignore replayed doc_reset frames --- .../agent/crdt/followerSubscription.test.ts | 39 ++++++++++++++++ .../agent/crdt/layoutFollowerBridge.ts | 14 ++++++ .../agent/crdt/mintGate.property.test.ts | 45 ------------------- .../agent/crdt/opEnvelope.property.test.ts | 15 +++---- .../extensions/agent/crdt/opEnvelope.ts | 5 ++- 5 files changed, 63 insertions(+), 55 deletions(-) delete mode 100644 src/workbench/extensions/agent/crdt/mintGate.property.test.ts diff --git a/src/workbench/extensions/agent/crdt/followerSubscription.test.ts b/src/workbench/extensions/agent/crdt/followerSubscription.test.ts index 4150b1c4428..f3e1f97864c 100644 --- a/src/workbench/extensions/agent/crdt/followerSubscription.test.ts +++ b/src/workbench/extensions/agent/crdt/followerSubscription.test.ts @@ -462,6 +462,45 @@ describe('doc_reset — a lineage break drops the doc and resubscribes from zero expect(transport.framesOfType('doc_subscribe')).toHaveLength(1) }) + it('drops the doc once when the same reset is replayed', () => { + const { transport, bridge } = wire() + const resets: unknown[] = [] + bridge.addEventListener('doc_reset', (event) => { + if (event instanceof CustomEvent) resets.push(event.detail) + }) + transport.open = true + bridge.subscribe(WORKFLOW_ID) + transport.deliver('doc_update', docUpdateFrame(hostDocUpdate())) + transport.deliver('doc_reset', { v: 1, workflow_id: WORKFLOW_ID, seq: 43 }) + const freshDoc = bridge.follower + transport.deliver('doc_update', docUpdateFrame(hostDocUpdate())) + + transport.deliver('doc_reset', { v: 1, workflow_id: WORKFLOW_ID, seq: 43 }) + + expect(bridge.follower).toBe(freshDoc) + expect(bridge.follower.updatesApplied).toBe(1) + expect(resets).toEqual([{ workflowId: WORKFLOW_ID, seq: 43 }]) + expect(transport.framesOfType('doc_subscribe')).toHaveLength(2) + }) + + it('ignores a reset at or below the seq it has already applied', () => { + const { transport, bridge } = wire() + transport.open = true + bridge.subscribe(WORKFLOW_ID) + transport.deliver( + 'doc_update', + docUpdateFrame(hostDocUpdate(), WORKFLOW_ID, 50) + ) + const oldDoc = bridge.follower + + transport.deliver('doc_reset', { v: 1, workflow_id: WORKFLOW_ID, seq: 50 }) + transport.deliver('doc_reset', { v: 1, workflow_id: WORKFLOW_ID, seq: 49 }) + + expect(bridge.follower).toBe(oldDoc) + expect(bridge.follower.updatesApplied).toBe(1) + expect(transport.framesOfType('doc_subscribe')).toHaveLength(1) + }) + it('a reset on a dead socket still drops the doc; the resubscribe lands on the next reconcile', () => { const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) const { transport, bridge } = wire() diff --git a/src/workbench/extensions/agent/crdt/layoutFollowerBridge.ts b/src/workbench/extensions/agent/crdt/layoutFollowerBridge.ts index b0d3f2c887c..674d75a2b7c 100644 --- a/src/workbench/extensions/agent/crdt/layoutFollowerBridge.ts +++ b/src/workbench/extensions/agent/crdt/layoutFollowerBridge.ts @@ -124,6 +124,12 @@ export class LayoutFollowerBridge extends EventTarget { * harmless). It never moves {@link lastSeq} backwards. */ private catchUpPending = false + /** + * Seq of the last accepted `doc_reset`. The host allocates seq monotonically, + * so a reset at or below this (or below an already applied/acked seq) is a + * replay and must not drop the doc a second time. + */ + private lastResetSeq: number | null = null constructor(private readonly client: DocFrameClient) { super() @@ -182,6 +188,7 @@ export class LayoutFollowerBridge extends EventTarget { this.lineageWorkflowId = workflowId this.desiredWorkflowId = workflowId if (lineage !== null && lineage !== workflowId) { + this.lastResetSeq = null this.dropDocForNewLineage() this.dispatchEvent( new CustomEvent('follower_replaced', { detail: { workflowId } }) @@ -370,12 +377,19 @@ export class LayoutFollowerBridge extends EventTarget { if (!(event instanceof CustomEvent)) return const reset = event.detail as DocReset if (reset.workflowId !== this.sentWorkflowId) return + if (this.isReplayedReset(reset.seq)) return + this.lastResetSeq = reset.seq this.dispatchEvent(new CustomEvent('doc_reset', { detail: reset })) this.dropDocForNewLineage() this.resubscribe() this.dispatchEvent(new CustomEvent('follower_replaced', { detail: reset })) } + private isReplayedReset(seq: number): boolean { + const applied = this.lastSeq ?? this.ackSeq ?? -1 + return seq <= (this.lastResetSeq ?? -1) || seq <= applied + } + /** * Replace the doc after an explicit lineage reset so the next subscribe * carries an empty state vector and pulls the new folded state. diff --git a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts b/src/workbench/extensions/agent/crdt/mintGate.property.test.ts deleted file mode 100644 index e44ba8211ae..00000000000 --- a/src/workbench/extensions/agent/crdt/mintGate.property.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Exhaustive coverage of the mint gate. - * - * `mintGate.test.ts` walks the one-conjunct-off cases. Exhaustively covering - * all eight states also pins combinations such as teardown coinciding with a - * bound doc, the clear-storm case the gate exists to stop. - * - * The domain is small, so this enumerates all of it rather than sampling. - */ -import { describe, expect, it } from 'vitest' - -import type { MintGateInput } from './mintGate' -import { shouldMint } from './mintGate' - -const BOOLS = [false, true] as const - -const ALL_STATES: MintGateInput[] = BOOLS.flatMap((flagEnabled) => - BOOLS.flatMap((docBound) => - BOOLS.map((teardown) => ({ flagEnabled, docBound, teardown })) - ) -) - -function key({ flagEnabled, docBound, teardown }: MintGateInput): string { - return `flag=${flagEnabled} doc=${docBound} teardown=${teardown}` -} - -describe('shouldMint — exhaustive over the whole input domain', () => { - it('matches the literal truth table', () => { - const expected: Record = Object.fromEntries( - ALL_STATES.map((s) => [key(s), false]) - ) - expected[ - key({ - flagEnabled: true, - docBound: true, - teardown: false - }) - ] = true - - const actual = Object.fromEntries( - ALL_STATES.map((s) => [key(s), shouldMint(s)]) - ) - expect(actual).toEqual(expected) - }) -}) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index 445ef231e90..09feb89d1d8 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -360,10 +360,11 @@ describe('minted stamps (property) — order-independent LWW', () => { * `(workflowId, actor)`, so a life-1 client cannot mint a stale-high * `base_version` after a reconnect resets its observed sequence lower. * - * The test below asserts TODAY's behaviour on purpose. It is a tripwire: when - * the pin advances to the incarnation-namespaced stamps, it goes red and this - * is where the write leg gets updated. Nothing here is a claim that the current - * outcome is correct. + * The second test below asserts the DESIRED outcome and is marked `it.fails` + * because this pin cannot deliver it yet. It is a tripwire: when the pin + * advances to incarnation-namespaced stamps the assertion starts passing, + * `it.fails` reports that as a failure, and this is where the write leg gets + * updated and the marker removed. */ describe('minted stamps — DQ-11 actor-incarnation gap at this pin', () => { it('pins the actor-incarnation-free stamp shape the write leg mints', () => { @@ -377,7 +378,7 @@ describe('minted stamps — DQ-11 actor-incarnation gap at this pin', () => { expect(Object.keys(op)).not.toContain('incarnation') }) - it('documents that a stale life-1 write still defeats a live life-2 write', () => { + it.fails('a stale life-1 write must not defeat a live life-2 write', () => { const doc = mint({ nodes: [], links: [] }, CATALOG) const actor = 'human:reconnecting-client:tab-1' @@ -401,8 +402,6 @@ describe('minted stamps — DQ-11 actor-incarnation gap at this pin', () => { applyOps(doc, live) applyOps(doc, stale) - // DQ-11 says this SHOULD be 'life-2-live'. At this pin it is not, because - // the stamp carries no incarnation to order the two lives by. - expect(readWidget(doc)).toBe('life-1-stale') + expect(readWidget(doc)).toBe('life-2-live') }) }) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.ts b/src/workbench/extensions/agent/crdt/opEnvelope.ts index 4b1d07e96f4..2a62c49735c 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.ts @@ -18,6 +18,7 @@ export const WIRE_MAX_BATCH_BYTES = 4 * 1024 * 1024 // Reserve space for the doc_ops frame fields outside `ops` (type, protocol, // workflow id and tab). The server's identifiers are bounded well below this. const WIRE_FRAME_OVERHEAD_BYTES = 1024 +const utf8 = new TextEncoder() export interface MintContext { actor: Actor @@ -62,14 +63,14 @@ function isBatchable(op: Op): boolean { /** Conservative encoded size of a complete doc_ops frame carrying `ops`. */ export function wireBatchSize(ops: readonly Op[]): number { return ( - new TextEncoder().encode(JSON.stringify(ops)).length + + utf8.encode(JSON.stringify(ops)).length + WIRE_FRAME_OVERHEAD_BYTES ) } /** UTF-8 byte length of a single op's JSON encoding, as it appears inside a batch array. */ function opBytes(op: Op): number { - return new TextEncoder().encode(JSON.stringify(op)).length + return utf8.encode(JSON.stringify(op)).length } /** From bc94ad81cc4714f52d8257121ba4a68c9fb2d083 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 23 Sep 2026 16:28:00 +0000 Subject: [PATCH 16/17] [automated] Apply ESLint and Oxfmt fixes --- src/workbench/extensions/agent/crdt/opEnvelope.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.ts b/src/workbench/extensions/agent/crdt/opEnvelope.ts index 2a62c49735c..5faa000df94 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.ts @@ -62,10 +62,7 @@ function isBatchable(op: Op): boolean { /** Conservative encoded size of a complete doc_ops frame carrying `ops`. */ export function wireBatchSize(ops: readonly Op[]): number { - return ( - utf8.encode(JSON.stringify(ops)).length + - WIRE_FRAME_OVERHEAD_BYTES - ) + return utf8.encode(JSON.stringify(ops)).length + WIRE_FRAME_OVERHEAD_BYTES } /** UTF-8 byte length of a single op's JSON encoding, as it appears inside a batch array. */ From b27a17ce1f61228b310fe29788c1085de67bab0c Mon Sep 17 00:00:00 2001 From: Amp Date: Mon, 12 Oct 2026 08:43:03 +0000 Subject: [PATCH 17/17] test(agent/crdt): simplify operation envelope properties Amp-Thread-ID: https://ampcode.com/threads/T-01a08e63-a308-7555-b8cb-3017ed0850d4 --- .../agent/crdt/opEnvelope.property.test.ts | 245 ++++-------------- .../extensions/agent/crdt/opEnvelope.test.ts | 34 ++- 2 files changed, 65 insertions(+), 214 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts index 9a980a40a81..4d34285f8bf 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.property.test.ts @@ -1,43 +1,12 @@ -/** - * Property coverage for the human write leg's transport boundary. - * - * `opEnvelope.test.ts` pins the chunker with hand-sized fixtures — 600 ops - * splits `[256, 256, 88]`, a half-cap payload splits three ways. Those examples - * fix the caps at points chosen to hit them; they say nothing about arbitrary - * mixes of kinds and sizes, which is exactly where a chunker goes wrong: - * dropping the tail, reordering across a `clear` flush, or splitting one op - * early and silently halving throughput. - * - * Four properties hold for every generated input: - * - * TOTALITY — `chunkWireOps(ops).flat()` is `ops`: same ops, same order, - * nothing dropped, nothing duplicated. This is the one that - * matters, because a dropped op is a silently lost human edit. - * CAPS — no batch exceeds the op cap or (above one op) the byte cap. - * ISOLATION — `clear` is catastrophic-by-nature and always ships alone. - * MAXIMALITY — a batch is only ended early when a cap forces it. Without this - * a chunker that emits one op per batch passes all three above. - * - * It also checks the envelope itself (`mintWireOps`) without coupling this - * transport suite to the applier or the retired store-first projection path. - */ import { BATCHABLE_OPS } from '@comfyorg/comfy-multi-player' +import type { Op } from '@comfyorg/comfy-multi-player' import * as fc from 'fast-check' import { describe, expect, it } from 'vitest' import type { GraphOperation } from './graphOperations' -import { - WIRE_MAX_BATCH_BYTES, - WIRE_MAX_OPS_PER_BATCH, - chunkWireOps, - mintOpId, - mintWireOps -} from './opEnvelope' - -const FC_OPTIONS = { numRuns: 100 } as const +import { chunkWireOps, mintWireOps } from './opEnvelope' const MINT = { actor: 'human:pbt-user:tab-1', baseVersion: 7 } -const utf8 = new TextEncoder() function addNode(id: number): GraphOperation { return { @@ -68,200 +37,74 @@ function clear(ids: number[]): GraphOperation { return { op: 'clear', removed_nodes: ids } } -function serializedBatchBytes(batch: unknown[]): number { - return utf8.encode(JSON.stringify(batch)).length +function isBatchable(op: Op): boolean { + return BATCHABLE_OPS.some((kind) => kind === op.op) } -/** - * Small operations spanning the common write kinds. Separate fixtures below - * exercise the operation and byte caps without putting multi-megabyte strings - * through the shrinker. - */ -const operationArb: fc.Arbitrary = fc.oneof( - { arbitrary: fc.integer({ min: 0, max: 40 }).map(addNode), weight: 4 }, - { - arbitrary: fc - .tuple(fc.integer({ min: 0, max: 40 }), fc.string({ maxLength: 32 })) - .map(([id, value]) => setWidget(id, value)), - weight: 4 - }, - { arbitrary: fc.integer({ min: 0, max: 40 }).map(deleteNode), weight: 2 }, - { - arbitrary: fc - .array(fc.integer({ min: 0, max: 40 }), { maxLength: 4 }) - .map(clear), - weight: 1 +function batchesBelowCaps(ops: Op[]): Op[][] { + const batches: Op[][] = [] + for (const op of ops) { + const last = batches.at(-1) + if (last && isBatchable(op) && isBatchable(last[0])) last.push(op) + else batches.push([op]) } -) + return batches +} -const operationsArb = fc.array(operationArb, { maxLength: 40 }) +const operationsArb = fc.array( + fc.oneof( + { arbitrary: fc.integer({ min: 0, max: 40 }).map(addNode), weight: 4 }, + { + arbitrary: fc + .tuple(fc.integer({ min: 0, max: 40 }), fc.string({ maxLength: 32 })) + .map(([id, value]) => setWidget(id, value)), + weight: 4 + }, + { arbitrary: fc.integer({ min: 0, max: 40 }).map(deleteNode), weight: 2 }, + { + arbitrary: fc + .array(fc.integer({ min: 0, max: 40 }), { maxLength: 4 }) + .map(clear), + weight: 1 + } + ), + { maxLength: 40 } +) describe('chunkWireOps (property)', () => { - it('is total and order-preserving: flattening the batches returns the input', () => { + it('below the caps, batches each batchable run and isolates the rest', () => { fc.assert( fc.property(operationsArb, (operations) => { const ops = mintWireOps(operations, MINT) - const flattened = chunkWireOps(ops).flat() - - expect(flattened).toHaveLength(ops.length) - // Reference identity, not structural equality: a chunker that rebuilt - // ops (and could therefore re-mint an op_id) must not pass. - for (const [index, op] of flattened.entries()) { - expect(op).toBe(ops[index]) - } - }), - FC_OPTIONS - ) - const overCap = mintWireOps( - Array.from({ length: WIRE_MAX_OPS_PER_BATCH + 1 }, (_, i) => addNode(i)), - MINT - ) - expect(chunkWireOps(overCap).map((batch) => batch.length)).toEqual([ - WIRE_MAX_OPS_PER_BATCH, - 1 - ]) - }) - - it('never emits an empty batch and never exceeds the op cap', () => { - fc.assert( - fc.property(operationsArb, (operations) => { - for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { - expect(batch.length).toBeGreaterThan(0) - expect(batch.length).toBeLessThanOrEqual(WIRE_MAX_OPS_PER_BATCH) - } - }), - FC_OPTIONS - ) - }) - - it('isolates every non-batchable op in a batch of exactly one', () => { - fc.assert( - fc.property(operationsArb, (operations) => { - for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { - if ( - batch.some((op) => !BATCHABLE_OPS.some((kind) => kind === op.op)) - ) { - expect(batch).toHaveLength(1) - } - } - }), - FC_OPTIONS - ) - - expect(chunkWireOps(mintWireOps([clear([])], MINT))).toHaveLength(1) - }) - - it('keeps multi-op batches under the byte cap, and ships an oversize op alone', () => { - fc.assert( - fc.property(operationsArb, (operations) => { - for (const batch of chunkWireOps(mintWireOps(operations, MINT))) { - const bytes = serializedBatchBytes(batch) - if (batch.length > 1) { - expect(bytes).toBeLessThanOrEqual(WIRE_MAX_BATCH_BYTES) - } - } - }), - FC_OPTIONS - ) - - const oversize = mintWireOps( - [setWidget(1, 'x'.repeat(WIRE_MAX_BATCH_BYTES + 16))], - MINT - ) - expect(chunkWireOps(oversize)).toEqual([oversize]) - const third = Math.floor(WIRE_MAX_BATCH_BYTES / 3) - 1024 - const thirds = mintWireOps( - [1, 2, 3, 4].map((id) => setWidget(id, 'x'.repeat(third))), - MINT - ) - expect(chunkWireOps(thirds).map((batch) => batch.length)).toEqual([3, 1]) - - const multibyte = mintWireOps( - [1, 2, 3, 4].map((id) => setWidget(id, 'é'.repeat(third))), - MINT - ) - expect(chunkWireOps(multibyte).map((batch) => batch.length)).toEqual([ - 1, 1, 1, 1 - ]) - }) - - it('is maximal: a batch only ends early when a cap or a clear forces it', () => { - fc.assert( - fc.property(operationsArb, (operations) => { - const ops = mintWireOps(operations, MINT) - const batches = chunkWireOps(ops) - - for (let i = 0; i < batches.length - 1; i++) { - const batch = batches[i] - const next = batches[i + 1] - const head = next[0] - // A clear on either side legitimately forces the boundary. - if ( - batch.some((op) => !BATCHABLE_OPS.some((kind) => kind === op.op)) || - !BATCHABLE_OPS.some((kind) => kind === head.op) - ) { - continue - } - const overOps = batch.length + 1 > WIRE_MAX_OPS_PER_BATCH - const overBytes = - serializedBatchBytes([...batch, head]) > WIRE_MAX_BATCH_BYTES - expect(overOps || overBytes).toBe(true) - } - }), - FC_OPTIONS + expect(chunkWireOps(ops)).toEqual(batchesBelowCaps(ops)) + }) ) }) }) describe('mintWireOps (property)', () => { - it('mints a unique 32-hex op_id per op and never reuses one across batches', () => { - fc.assert( - fc.property(operationsArb, operationsArb, (first, second) => { - const ids = [ - ...mintWireOps(first, MINT), - ...mintWireOps(second, MINT) - ].map((op) => op.op_id) - - for (const id of ids) expect(id).toMatch(/^[0-9a-f]{32}$/) - expect(new Set(ids).size).toBe(ids.length) - }), - FC_OPTIONS - ) - }) - - it('attaches the envelope and leaves the semantic payload byte-identical', () => { + it('adds only a unique envelope to each operation', () => { fc.assert( fc.property( operationsArb, fc.string({ minLength: 1, maxLength: 24 }), - fc.integer({ min: 0, max: 1_000_000 }), + fc.nat(), (operations, actor, baseVersion) => { - const context = { actor, baseVersion } - const minted = mintWireOps(operations, context) + const minted = mintWireOps(operations, { actor, baseVersion }) - expect(minted).toHaveLength(operations.length) - for (const [index, op] of minted.entries()) { - const original = operations[index] - expect(op.actor).toBe(actor) - expect(op.base_version).toBe(baseVersion) - expect(op.stamp).toEqual([baseVersion, actor]) - expect(op).toEqual({ - ...original, + expect(minted).toEqual( + operations.map((operation) => ({ + ...operation, op_id: expect.stringMatching(/^[0-9a-f]{32}$/), actor, base_version: baseVersion, stamp: [baseVersion, actor] - }) - } + })) + ) + expect(new Set(minted.map((op) => op.op_id)).size).toBe(minted.length) } - ), - FC_OPTIONS + ) ) }) - - it('mints ids that are unique across many direct mintOpId calls', () => { - const ids = Array.from({ length: 512 }, () => mintOpId()) - expect(new Set(ids).size).toBe(512) - }) }) diff --git a/src/workbench/extensions/agent/crdt/opEnvelope.test.ts b/src/workbench/extensions/agent/crdt/opEnvelope.test.ts index f320fc0b892..03b85f9d2c9 100644 --- a/src/workbench/extensions/agent/crdt/opEnvelope.test.ts +++ b/src/workbench/extensions/agent/crdt/opEnvelope.test.ts @@ -109,20 +109,28 @@ describe('chunkWireOps', () => { ]) }) - it('splits on the per-batch byte cap', () => { - const bigValue = 'x'.repeat(Math.ceil(WIRE_MAX_BATCH_BYTES / 2)) - const setWidget = (id: number): GraphOperation => ({ - op: 'set_widget', - node_id: id, - widget: 'text', - value: bigValue - }) - const ops = mintWireOps([setWidget(1), setWidget(2), setWidget(3)], MINT) - - const batches = chunkWireOps(ops) + it.for([ + { label: 'packs ASCII payloads to the byte cap', char: 'x', sizes: [3, 1] }, + { + label: 'measures multibyte payloads in UTF-8 bytes', + char: 'é', + sizes: [1, 1, 1, 1] + } + ])('$label', ({ char, sizes }) => { + const value = char.repeat(Math.floor(WIRE_MAX_BATCH_BYTES / 3) - 1024) + const ops = mintWireOps( + [1, 2, 3, 4].map( + (id): GraphOperation => ({ + op: 'set_widget', + node_id: id, + widget: 'text', + value + }) + ), + MINT + ) - expect(batches.length).toBe(3) - expect(batches.every((b) => b.length === 1)).toBe(true) + expect(chunkWireOps(ops).map((batch) => batch.length)).toEqual(sizes) }) it('ships a single oversize op alone rather than dropping it', () => {