diff --git a/src/workbench/extensions/agent/crdt/docFrameClient.reseed.test.ts b/src/workbench/extensions/agent/crdt/docFrameClient.reseed.test.ts index 68de945fbac..2df5b2110b8 100644 --- a/src/workbench/extensions/agent/crdt/docFrameClient.reseed.test.ts +++ b/src/workbench/extensions/agent/crdt/docFrameClient.reseed.test.ts @@ -95,24 +95,49 @@ describe('stale-schema reseed wire protocol', () => { ]) }) - it('accepts an empty canvas and rejects an oversized one', () => { + it('accepts an empty canvas and rejects an oversized workflow field', () => { const empty = refusedBridge() expect(empty.bridge.reseed('wf-1', { nodes: [] })).toBe('sent') const oversized = refusedBridge() expect( - oversized.bridge.reseed('wf-1', { value: 'x'.repeat((8 << 20) + 1) }) + oversized.bridge.reseed('wf-1', { value: 'x'.repeat((2 << 20) + 1) }) ).toBe('too_large') expect(oversized.transport.frames('doc_reseed')).toHaveLength(0) }) - it('counts the complete UTF-8 frame against the transport limit', () => { - const multibyte = refusedBridge() + it('enforces the backend workflow-field byte boundary for UTF-8 JSON', () => { + const limit = 2 << 20 + const wrapperBytes = new TextEncoder().encode('{"value":""}').length + const remaining = limit - wrapperBytes + const exactValue = + '界'.repeat(Math.floor(remaining / 3)) + 'x'.repeat(remaining % 3) + const exact = refusedBridge() + const oversized = refusedBridge() - expect( - multibyte.bridge.reseed('wf-1', { value: '界'.repeat(3_000_000) }) - ).toBe('too_large') - expect(multibyte.transport.frames('doc_reseed')).toHaveLength(0) + expect(exact.bridge.reseed('wf-1', { value: exactValue })).toBe('sent') + expect(exact.transport.frames('doc_reseed')).toHaveLength(1) + expect(oversized.bridge.reseed('wf-1', { value: `${exactValue}界` })).toBe( + 'too_large' + ) + expect(oversized.transport.frames('doc_reseed')).toHaveLength(0) + }) + + it('measures and sends the same workflow serialization', () => { + let serializations = 0 + const workflow = { + toJSON() { + serializations += 1 + return { value: serializations === 1 ? 'measured' : 'changed' } + } + } + const { transport, bridge } = refusedBridge() + + expect(bridge.reseed('wf-1', workflow)).toBe('sent') + expect(serializations).toBe(1) + expect(transport.frames('doc_reseed')).toEqual([ + expect.objectContaining({ workflow: { value: 'measured' } }) + ]) }) it('reports serialization failures separately from oversized frames', () => { diff --git a/src/workbench/extensions/agent/crdt/docFrameClient.ts b/src/workbench/extensions/agent/crdt/docFrameClient.ts index a78a506c0b9..f9b5f93cd42 100644 --- a/src/workbench/extensions/agent/crdt/docFrameClient.ts +++ b/src/workbench/extensions/agent/crdt/docFrameClient.ts @@ -5,6 +5,8 @@ import { reportError } from '@/platform/telemetry/reportError' export const DOC_PROTOCOL_VERSION = 1 /** Keep this encoded-field cap aligned with cloud's `MaxDocFrameB64Len`. */ const MAX_DOC_UPDATE_B64_LENGTH = 8 << 20 +/** Keep this inline JSON-field cap aligned with cloud's `MaxDocReseedWorkflowBytes`. */ +const MAX_DOC_RESEED_WORKFLOW_BYTES = 2 << 20 const MAX_WORKFLOW_ID_LENGTH = 128 const MAX_ACTOR_LENGTH = 256 const MAX_AWARENESS_STATE_BYTES = 8 << 10 @@ -470,21 +472,22 @@ export class DocFrameClient extends EventTarget { expectedSeq: number, workflow: Record ): DocReseedSendResult { - const data = { - v: DOC_PROTOCOL_VERSION, - workflow_id: workflowId, - expected_seq: expectedSeq, - workflow - } let frame: string try { - frame = JSON.stringify({ type: 'doc_reseed', data }) + const workflowJson = JSON.stringify(workflow) + if (workflowJson.length > MAX_DOC_RESEED_WORKFLOW_BYTES) + return 'too_large' + if (utf8.encode(workflowJson).length > MAX_DOC_RESEED_WORKFLOW_BYTES) + return 'too_large' + const metadataJson = JSON.stringify({ + v: DOC_PROTOCOL_VERSION, + workflow_id: workflowId, + expected_seq: expectedSeq + }) + frame = `{"type":"doc_reseed","data":${metadataJson.slice(0, -1)},"workflow":${workflowJson}}}` } catch { return 'serialization_failed' } - if (frame.length > MAX_DOC_UPDATE_B64_LENGTH) return 'too_large' - if (utf8.encode(frame).length > MAX_DOC_UPDATE_B64_LENGTH) - return 'too_large' return this.transport.send(frame) ? 'sent' : 'unavailable' } diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 80e1ae3d0b4..144c1830b58 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -526,7 +526,10 @@ describe('useAgentCrdtFollower', () => { expect(clientState.sendOps).toHaveBeenCalledExactlyOnceWith( 'wf-1', expect.any(String), - [expect.objectContaining({ node_id: 'after-failure' })] + [ + expect.objectContaining({ node_id: 'held' }), + expect.objectContaining({ node_id: 'after-failure' }) + ] ) }) it('replays post-snapshot edits after the same workflow tab is reactivated', async () => { @@ -587,6 +590,68 @@ describe('useAgentCrdtFollower', () => { expect(telemetryState.reportError).not.toHaveBeenCalled() }) + it('keeps the reseed-attempt budget across inactive transitions', async () => { + const { isTargetActive, unmount } = mountFollower( + 'wf-1', + true, + () => null, + {}, + () => ({ nodes: [] }) + ) + bridge().canReseed.mockReturnValue(true) + bridge().reseed.mockReturnValue('sent') + + for (let attempt = 0; attempt < 6; attempt++) { + dispatchFrame('doc_subscribed', { + workflowId: 'wf-1', + ok: false, + code: 'stale_schema_reseed_required', + expectedSeq: attempt + }) + dispatchFrame('doc_reseed_result', { + workflowId: 'wf-1', + ok: false, + code: 'retry' + }) + isTargetActive.value = false + await nextTick() + isTargetActive.value = true + await nextTick() + } + + dispatchFrame('doc_subscribed', { + workflowId: 'wf-1', + ok: false, + code: 'stale_schema_reseed_required', + expectedSeq: 6 + }) + + expect(bridge().reseed).toHaveBeenCalledTimes(6) + expect(telemetryState.reportError).toHaveBeenCalledExactlyOnceWith( + expect.any(Error), + { + errorType: 'failure_reseeding_agent_cloud_workflow', + level: 'warning', + tags: { feature_area: 'agent', operation: 'sync', outcome: 'gave_up' } + } + ) + + isTargetActive.value = false + await nextTick() + isTargetActive.value = true + await nextTick() + dispatchFrame('doc_subscribed', { + workflowId: 'wf-1', + ok: false, + code: 'stale_schema_reseed_required', + expectedSeq: 7 + }) + + expect(bridge().reseed).toHaveBeenCalledTimes(6) + expect(telemetryState.reportError).toHaveBeenCalledTimes(1) + unmount() + }) + it('does not construct a follower when the product gate is disabled', () => { useAgentPanelStore().enabled = false const { status, unmount } = mountFollower('wf-1') diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 3082e0d172d..02bd5959438 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -610,7 +610,9 @@ function startAgentCrdtFollower( clearSubscribeRetry() clearSubscribeAckTimer() subscribeGaveUp = true + const heldOperations = pendingReseed?.heldOperations ?? [] pendingReseed = null + if (heldOperations.length > 0) coalescer.enqueue(heldOperations) connected.value = false reportError(new Error(`agent document reseed stopped: ${reason}`), { errorType: 'failure_reseeding_agent_cloud_workflow', @@ -758,6 +760,7 @@ function startAgentCrdtFollower( const onSubscribed: EventListener = (event) => { if (!(event instanceof CustomEvent)) return if (!isTargetActive.value) return + if (subscribeGaveUp) return const ok = event.detail?.ok === true connected.value = ok lastFrameType.value = event.type @@ -1135,8 +1138,8 @@ function startAgentCrdtFollower( const workflowChanged = previous === undefined || next !== previous[0] clearSubscribeAckTimer() clearSubscribeRetry() - subscribeGaveUp = false if (workflowChanged) { + subscribeGaveUp = false reseedAttempts = 0 pendingReseed = null }