Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/workbench/extensions/agent/crdt/docFrameClient.reseed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
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', () => {
Expand Down
23 changes: 13 additions & 10 deletions src/workbench/extensions/agent/crdt/docFrameClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
const MAX_WORKFLOW_ID_LENGTH = 128
const MAX_ACTOR_LENGTH = 256
const MAX_AWARENESS_STATE_BYTES = 8 << 10
Expand Down Expand Up @@ -470,21 +472,22 @@ export class DocFrameClient extends EventTarget {
expectedSeq: number,
workflow: Record<string, unknown>
): 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)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
if (workflowJson.length > MAX_DOC_RESEED_WORKFLOW_BYTES)
return 'too_large'
if (utf8.encode(workflowJson).length > MAX_DOC_RESEED_WORKFLOW_BYTES)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
return 'too_large'
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
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'
}

Expand Down
67 changes: 66 additions & 1 deletion src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -587,6 +590,68 @@ describe('useAgentCrdtFollower', () => {
expect(telemetryState.reportError).not.toHaveBeenCalled()
})

it('keeps the reseed-attempt budget across inactive transitions', async () => {
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
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(
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
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')
Expand Down
5 changes: 4 additions & 1 deletion src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Loading