diff --git a/src/workbench/extensions/agent/crdt/docFrameClient.numeric.test.ts b/src/workbench/extensions/agent/crdt/docFrameClient.numeric.test.ts new file mode 100644 index 00000000000..98db007801d --- /dev/null +++ b/src/workbench/extensions/agent/crdt/docFrameClient.numeric.test.ts @@ -0,0 +1,110 @@ +import { describe, expect, it } from 'vitest' + +import { encodeBase64, parseServerDocFrame } from './docFrameClient' + +const updateFrame = (seq: unknown) => ({ + type: 'doc_update', + data: { + v: 1, + workflow_id: 'wf-1', + seq, + update_b64: encodeBase64(new Uint8Array()) + } +}) + +const awarenessFrame = (expiresAt: unknown) => ({ + type: 'awareness', + data: { + v: 1, + workflow_id: 'wf-1', + actor: 'human:user:tab-a', + expires_at: expiresAt + } +}) + +const sequencedFrame = ( + type: 'doc_subscribed' | 'doc_reset', + seq: unknown +) => ({ + type, + data: { + v: 1, + workflow_id: 'wf-1', + seq, + ...(type === 'doc_subscribed' && { ok: true }) + } +}) + +const docOpsResultFrame = (seq?: unknown) => ({ + type: 'doc_ops_result', + data: { + v: 1, + workflow_id: 'wf-1', + ok: true, + applied: [], + skipped: [], + ...(seq !== undefined && { seq }) + } +}) + +const sequencedFrameTypes: ReadonlyArray<'doc_subscribed' | 'doc_reset'> = [ + 'doc_subscribed', + 'doc_reset' +] + +describe('doc frame numeric domains', () => { + it.for([-1, 1.5, Number.POSITIVE_INFINITY, Number.NaN])( + 'rejects an invalid doc_update seq: %s', + (seq) => { + expect(parseServerDocFrame(updateFrame(seq))).toBeNull() + } + ) + + it.for([-1, 1.5, Number.POSITIVE_INFINITY, Number.NaN])( + 'rejects an invalid awareness expiry: %s', + (expiresAt) => { + expect(parseServerDocFrame(awarenessFrame(expiresAt))).toBeNull() + } + ) + + describe.for(sequencedFrameTypes)('%s seq', (type) => { + it.for([-1, 1.5, Number.POSITIVE_INFINITY, Number.NaN, '1'])( + 'rejects an invalid value: %s', + (seq) => { + expect(parseServerDocFrame(sequencedFrame(type, seq))).toBeNull() + } + ) + + it('accepts zero', () => { + expect(parseServerDocFrame(sequencedFrame(type, 0))?.data).toMatchObject({ + seq: 0 + }) + }) + }) + + it.for([-1, 1.5, Number.POSITIVE_INFINITY, Number.NaN, '1'])( + 'rejects an invalid doc_ops_result seq: %s', + (seq) => { + expect(parseServerDocFrame(docOpsResultFrame(seq))).toBeNull() + } + ) + + it('accepts a valid doc_ops_result seq', () => { + expect(parseServerDocFrame(docOpsResultFrame(0))?.data).toMatchObject({ + seq: 0 + }) + }) + + it('accepts doc_ops_result without seq', () => { + expect(parseServerDocFrame(docOpsResultFrame())?.data).not.toHaveProperty( + 'seq' + ) + }) + + it('accepts finite non-negative integer sequence and expiry values', () => { + expect(parseServerDocFrame(updateFrame(0))?.data).toMatchObject({ seq: 0 }) + expect(parseServerDocFrame(awarenessFrame(0))?.data).toMatchObject({ + expiresAt: 0 + }) + }) +}) diff --git a/src/workbench/extensions/agent/crdt/docFrameClient.ts b/src/workbench/extensions/agent/crdt/docFrameClient.ts index 9917b4a4254..b13ecdd9fd7 100644 --- a/src/workbench/extensions/agent/crdt/docFrameClient.ts +++ b/src/workbench/extensions/agent/crdt/docFrameClient.ts @@ -168,7 +168,7 @@ export function parseServerDocFrame(value: unknown): ServerDocFrame | null { if ( frame.type === 'doc_update' && - typeof data.seq === 'number' && + isNonNegativeInteger(data.seq) && typeof data.update_b64 === 'string' ) { return { @@ -187,20 +187,28 @@ export function parseServerDocFrame(value: unknown): ServerDocFrame | null { } } - if (frame.type === 'doc_subscribed' && typeof data.ok === 'boolean') { + if ( + frame.type === 'doc_subscribed' && + typeof data.ok === 'boolean' && + (data.seq === undefined || isNonNegativeInteger(data.seq)) + ) { return { type: frame.type, data: { workflowId: data.workflow_id, ok: data.ok, - ...(typeof data.seq === 'number' && { seq: data.seq }), + ...(data.seq !== undefined && { seq: data.seq }), ...(typeof data.code === 'string' && { code: data.code }), ...(typeof data.message === 'string' && { message: data.message }) } } } - if (frame.type === 'doc_ops_result' && typeof data.ok === 'boolean') { + if ( + frame.type === 'doc_ops_result' && + typeof data.ok === 'boolean' && + (data.seq === undefined || isNonNegativeInteger(data.seq)) + ) { return { type: frame.type, data: { @@ -216,7 +224,7 @@ export function parseServerDocFrame(value: unknown): ServerDocFrame | null { (item): item is string => typeof item === 'string' ) : [], - ...(typeof data.seq === 'number' && { seq: data.seq }), + ...(isNonNegativeInteger(data.seq) && { seq: data.seq }), ...(typeof data.code === 'string' && { code: data.code }), ...(typeof data.message === 'string' && { message: data.message }), // PoC diagnostics: surface the failure verbatim (object, not array). @@ -225,7 +233,7 @@ export function parseServerDocFrame(value: unknown): ServerDocFrame | null { } } - if (frame.type === 'doc_reset' && typeof data.seq === 'number') { + if (frame.type === 'doc_reset' && isNonNegativeInteger(data.seq)) { return { type: frame.type, data: {