diff --git a/src/workbench/extensions/agent/crdt/devPanelLog.ts b/src/workbench/extensions/agent/crdt/devPanelLog.ts index 0f25aa70dbd..115541be084 100644 --- a/src/workbench/extensions/agent/crdt/devPanelLog.ts +++ b/src/workbench/extensions/agent/crdt/devPanelLog.ts @@ -28,6 +28,7 @@ export const DEV_EVENT_KINDS = [ 'doc_subscribed', 'doc_update', 'doc_ops_result', + 'doc_ops_result_dropped', 'human_ops_settled', 'doc_reset', 'doc_nodes_changed', @@ -106,6 +107,7 @@ const STRUCTURAL_STRING_KEYS = new Set([ 'outcome', 'phase', 'prompt_id', + 'reason', 'removed', 'scope', 'seq', @@ -114,6 +116,7 @@ const STRUCTURAL_STRING_KEYS = new Set([ 'source', 'state', 'status', + 'subscribed_workflow_id', 'tab', 'tab_id', 'target', diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 8d17c20d4fd..dc582a78730 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -143,10 +143,7 @@ vi.mock(import('./agentCrdtProjection'), () => ({ } })) -vi.mock(import('./devPanelLog'), () => ({ - recordDevEvent: vi.fn(), - sanitizeDevEventDetail: vi.fn((detail: unknown) => detail) -})) +vi.mock(import('./devPanelLog'), { spy: true }) vi.mock(import('@/platform/telemetry/reportError'), () => ({ reportError: telemetryState.reportError @@ -158,6 +155,7 @@ vi.mock(import('@/scripts/app'), () => ({ })) import { SUBSCRIBE_ACK_TIMEOUT_MS } from './agentCrdtDocLifecycle' +import { clearDevEvents, devEvents } from './devPanelLog' import { STALE_AFTER_MS, SUBSCRIBE_CATCHUP_GRACE_MS, @@ -650,6 +648,49 @@ describe('useAgentCrdtFollower', () => { unmount() }) + it('logs each dropped op result with its reason, subscribed target, and frame', async () => { + clearDevEvents() + const { workflowId, isTargetActive, unmount } = mountFollower('wf-1') + dispatchFrame('doc_subscribed', { ok: true }) + const staleFailure = { + workflowId: 'wf-2', + ok: false, + skipped: ['op-2'], + failed: { op_id: 'op-2', code: 'unknown_node' } + } + + dispatchFrame('doc_ops_result', staleFailure) + workflowId.value = 'wf-2' + await nextTick() + dispatchFrame('doc_ops_result', { workflowId: 'wf-1', ok: true }) + isTargetActive.value = false + await nextTick() + dispatchFrame('doc_ops_result', { workflowId: 'wf-2', ok: true }) + + expect( + devEvents.value + .filter((event) => event.kind === 'doc_ops_result_dropped') + .map((event) => event.detail) + ).toEqual([ + { + reason: 'workflow_mismatch', + subscribedWorkflowId: 'wf-1', + frame: staleFailure + }, + { + reason: 'workflow_mismatch', + subscribedWorkflowId: 'wf-2', + frame: { workflowId: 'wf-1', ok: true } + }, + { + reason: 'inactive_target', + subscribedWorkflowId: 'wf-2', + frame: { workflowId: 'wf-2', ok: true } + } + ]) + unmount() + }) + it('FEC-5: an idle doc still expires', () => { vi.useFakeTimers() const setup = mountFollower('wf-1') diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 6a955165bdc..0eb65341e1a 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -200,6 +200,17 @@ export interface AgentCrdtStatus { outcomes: AgentCrdtOutcomeCounters } +type OpsResultDropReason = 'inactive_target' | 'workflow_mismatch' + +function opsResultDropReason( + active: boolean, + workflowId: unknown, + subscribedWorkflowId: string | null +): OpsResultDropReason | null { + if (!active) return 'inactive_target' + return workflowId === subscribedWorkflowId ? null : 'workflow_mismatch' +} + export interface AgentCrdtFollowerEvents { onMaterialized?: (event: { workflowId: string @@ -618,11 +629,19 @@ function startAgentCrdtFollower( const onOpsResult: EventListener = (event) => { if (!(event instanceof CustomEvent)) return const detail = event.detail as { workflowId?: unknown } | null - if ( - !isTargetActive.value || - detail?.workflowId !== subscribedWorkflowId.value + const dropReason = opsResultDropReason( + isTargetActive.value, + detail?.workflowId, + subscribedWorkflowId.value ) + if (dropReason !== null) { + recordDevEvent('doc_ops_result_dropped', { + reason: dropReason, + subscribedWorkflowId: subscribedWorkflowId.value, + frame: event.detail ?? null + }) return + } lifecycle.onDocumentResult() lastFrameType.value = event.type recordDevEvent('doc_ops_result', event.detail ?? null)