Skip to content
Closed
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
25 changes: 25 additions & 0 deletions src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ describe('CrdtDevPanel', () => {
expect(log).not.toContain('ws_out')
})

it('filters mint and projection diagnostics independently', async () => {
const user = userEvent.setup()
recordDevEvent('op_minted', { workflowId: 'mint-workflow' })
recordDevEvent('doc_effects', {
workflowId: 'effect-workflow',
committed: false
})
renderPanel()
await user.click(chip()!)
await user.click(screen.getByTestId('crdt-dev-panel-tab-log'))

for (const [kind, absent] of [
['op_minted', 'doc_effects'],
['doc_effects', 'op_minted']
]) {
await user.selectOptions(
screen.getByTestId('crdt-dev-panel-filter'),
kind
)
const log = screen.getByTestId('crdt-dev-panel-log').textContent
expect(log).toContain(kind)
expect(log).not.toContain(absent)
}
})

it('shows the sensitive-source opt-ins as off, and lets them be turned on', async () => {
const user = userEvent.setup()
renderPanel()
Expand Down
2 changes: 2 additions & 0 deletions src/workbench/extensions/agent/crdt/CrdtDevPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ const EVENT_KINDS: readonly DevEventKind[] = [
'human_ops_settled',
'doc_reset',
'doc_nodes_changed',
'doc_effects',
'op_minted',
'schema_error',
'reconnected',
'subscribe_retry',
Expand Down
14 changes: 9 additions & 5 deletions src/workbench/extensions/agent/crdt/agentCrdtProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
readSubgraphDefinitionIds,
readSubgraphDefinitions
} from './agentSubgraphDefinitions'
import { recordDevEvent } from './devPanelLog'
import { docLog } from './crdtLog'
import type { DocUpdate } from './docFrameClient'
import type { MutationsForTarget } from './ecsFollowerAdapter'
import { EcsFollowerAdapter } from './ecsFollowerAdapter'
Expand Down Expand Up @@ -76,10 +76,14 @@ export class AgentCrdtProjection {
// else asks the canvas to paint the new links.
graph.setDirtyCanvas(true, true)
if (nodeIds.length > 0) {
recordDevEvent('agent_node_adapters_materialized', {
workflowId,
nodeIds
})
docLog.debug(
'agent_node_adapters_materialized',
'live node adapters materialized',
{
workflowId,
nodeIds
}
)
}
}

Expand Down
28 changes: 26 additions & 2 deletions src/workbench/extensions/agent/crdt/crdtLog.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { setCrdtDebugEnabled } from './crdtDebugGate'
import { wireLog } from './crdtLog'
import { setCrdtDebugEnabled, setCrdtLogLevel } from './crdtDebugGate'
import { docLog, wireLog } from './crdtLog'
import { clearDevEvents, devEvents } from './devPanelLog'

describe('crdtLog', () => {
Expand All @@ -10,6 +10,30 @@ describe('crdtLog', () => {
clearDevEvents()
})

it('retains document detail below console verbosity and prints it once when enabled', () => {
const debug = vi.spyOn(console, 'debug').mockImplementation(() => {})
const logDocument = docLog.debug
setCrdtLogLevel('info')
logDocument('doc_effects', 'projection outcome', { committed: false })
expect(debug).not.toHaveBeenCalled()
expect(devEvents.value).toHaveLength(1)
expect(devEvents.value[0]).toMatchObject({
scope: 'doc',
level: 'debug',
detail: { committed: false }
})

setCrdtLogLevel('debug')
logDocument('doc_effects', 'projection outcome', { committed: true })
expect(debug).toHaveBeenCalledExactlyOnceWith(
'%c[crdt:doc]%c doc_effects — projection outcome',
'color:#a5b4fc',
'',
{ committed: true }
)
expect(devEvents.value).toHaveLength(2)
})

it('keeps warnings visible when the debug instrument is opted out', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
setCrdtDebugEnabled(false)
Expand Down
5 changes: 3 additions & 2 deletions src/workbench/extensions/agent/crdt/crdtLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Before this module the two were wired independently — a handful of
* `recordDevEvent` calls that never printed, and a handful of bare
* `console.warn`s the panel never saw — so the console and the copied report
* disagreed about what happened. New structured wire events use
* {@link crdtLog}; existing document events remain in the shared ring buffer.
* disagreed about what happened. Wire and document events use
* {@link crdtLog} to share the debug gate and console level selection.
*/
import type { CrdtLogLevel } from './crdtDebugGate'
import {
Expand Down Expand Up @@ -85,3 +85,4 @@ function scopedCrdtLog(scope: CrdtLogScope) {
}

export const wireLog = scopedCrdtLog('wire')
export const docLog = scopedCrdtLog('doc')
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ describe('R-73 cross-workflow pending operation characterization', () => {
{
state: 'undeliverable',
ops: [expect.objectContaining({ op_id: operationAId })]
}
},
{ scope: 'doc', level: 'debug' }
)
enqueue([deleteNode('b-pending')])
expect(clientState.sent).toHaveLength(2)
Expand Down Expand Up @@ -272,7 +273,8 @@ describe('R-73 cross-workflow pending operation characterization', () => {
ok: true,
applied: [operationAId],
skipped: []
}
},
{ scope: 'doc', level: 'debug' }
)
expect(operationBId).not.toBe(operationAId)
})
Expand Down
2 changes: 2 additions & 0 deletions src/workbench/extensions/agent/crdt/devPanelLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type DevEventKind =
| 'reconnected'
| 'subscribe_retry'
| 'doc_nodes_changed'
| 'doc_effects'
| 'op_minted'
| 'rebind'
| 'stale_probe'
| 'doc_gap'
Expand Down
45 changes: 45 additions & 0 deletions src/workbench/extensions/agent/crdt/docFrameClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as Y from 'yjs'

import { reportError } from '@/platform/telemetry/reportError'

import { setCrdtDebugEnabled } from './crdtDebugGate'
import { clearDevEvents, devEvents } from './devPanelLog'
import type { DocFrameTransport } from './docFrameClient'
import {
DocFrameClient,
Expand Down Expand Up @@ -36,6 +38,49 @@ function updateAfter(doc: Y.Doc, mutate: () => void): Uint8Array {
}

describe('doc frame client', () => {
it('reports false sends for every frame type without changing delivery results', () => {
setCrdtDebugEnabled(true)
clearDevEvents()
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const transport = new TestTransport()
const send = vi.spyOn(transport, 'send').mockReturnValue(false)
const client = new DocFrameClient(transport)

expect(client.subscribe('wf-private', new Uint8Array())).toBe(false)
expect(client.sendOps('wf-private', 'tab-private', [])).toBe(false)
expect(client.unsubscribe('wf-private')).toBe(false)
expect(
devEvents.value.map(({ kind, scope, detail }) => ({
kind,
scope,
detail
}))
).toEqual(
['doc_subscribe', 'doc_ops', 'doc_unsubscribe'].map((frameType) => ({
kind: 'frame_send_failed',
scope: 'wire',
detail: {
frameType,
workflowId: 'wf-private',
reason: 'transport_unavailable'
}
}))
)

send.mockReturnValue(true)
expect(client.subscribe('wf-private', new Uint8Array())).toBe(true)
expect(devEvents.value).toHaveLength(3)
clearDevEvents()
setCrdtDebugEnabled(false)
warn.mockClear()
send.mockReturnValue(false)
expect(client.unsubscribe('wf-private')).toBe(false)
expect(warn).toHaveBeenCalledOnce()
expect(JSON.stringify(warn.mock.calls)).not.toContain('wf-private')
expect(devEvents.value).toHaveLength(0)
client.destroy()
})

it('decodes a base64 doc_update that converges through Y.applyUpdate', () => {
const source = new Y.Doc()
source.getMap('nodes').set('one', { x: 10 })
Expand Down
12 changes: 11 additions & 1 deletion src/workbench/extensions/agent/crdt/docFrameClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Op } from '@comfyorg/comfy-multi-player'

import { reportError } from '@/platform/telemetry/reportError'

import { wireLog } from './crdtLog'

export const DOC_PROTOCOL_VERSION = 1
/** Keep this encoded-field cap aligned with cloud's `MaxDocFrameB64Len`. */
const MAX_DOC_UPDATE_B64_LENGTH = 8 << 20
Expand Down Expand Up @@ -450,6 +452,14 @@ export class DocFrameClient extends EventTarget {
}

private send(type: string, data: Record<string, unknown>): boolean {
return this.transport.send(JSON.stringify({ type, data }))
const delivered = this.transport.send(JSON.stringify({ type, data }))
if (!delivered) {
wireLog.warn('frame_send_failed', 'outbound doc frame not sent', {
frameType: type,
workflowId: data.workflow_id,
reason: 'transport_unavailable'
})
}
return delivered
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type { NodeId } from '@/types/nodeId'
import { toNodeId } from '@/types/nodeId'
import { widgetId } from '@/types/widgetId'

import { setCrdtDebugEnabled } from './crdtDebugGate'
import { clearDevEvents, devEvents } from './devPanelLog'
import type { DocUpdate } from './docFrameClient'
import { EcsFollowerAdapter } from './ecsFollowerAdapter'
import { FollowerDoc } from './followerDoc'
Expand Down Expand Up @@ -45,6 +47,8 @@ function op(id: string, baseVersion: number, payload: object) {

describe('EcsFollowerAdapter integration', () => {
it('reconciles a full seeded snapshot with existing and server-ahead entities', () => {
setCrdtDebugEnabled(true)
clearDevEvents()
const layouts = new Map<NodeId, TestLayout>()
const createLayout = vi.fn(
(_scope: typeof scope, nodeId: NodeId, layout: TestLayout) => {
Expand Down Expand Up @@ -144,6 +148,22 @@ describe('EcsFollowerAdapter integration', () => {
})
expect(deleteLayouts).not.toHaveBeenCalled()
expect(createLayout).toHaveBeenCalledOnce()
expect(
devEvents.value.find(({ kind }) => kind === 'doc_effects')?.detail
).toEqual({
workflowId: 'wf',
seq: 1,
reconcile: true,
committed: true,
nodeActions: [
['1', 'add'],
['2', 'add']
],
changedWidgets: [],
replacedWidgetMaps: [],
changedLinkIds: ['9'],
removedLinkIds: []
})

adapter.destroy()
follower.destroy()
Expand Down Expand Up @@ -244,6 +264,8 @@ describe('EcsFollowerAdapter integration', () => {
})

it('retries authoritative reconciliation after a rejected first batch', () => {
setCrdtDebugEnabled(true)
clearDevEvents()
const deleteLayouts = vi.fn()
let scopeAvailable = false
const mutations = createGraphMutations({
Expand Down Expand Up @@ -291,6 +313,14 @@ describe('EcsFollowerAdapter integration', () => {
// incremental handling.
scopeAvailable = true
expect(adapter.applyFrame({ workflowId: 'wf', seq: 2, update })).toBe(true)
expect(
devEvents.value
.filter(({ kind }) => kind === 'doc_effects')
.map(({ detail }) => detail)
).toEqual([
expect.objectContaining({ seq: 1, reconcile: true, committed: false }),
expect.objectContaining({ seq: 2, reconcile: true, committed: true })
])
expect(useNodeDataStore().getGraphNodesFor('root', 'root')).toEqual([])
expect(deleteLayouts).toHaveBeenCalledWith(
scope,
Expand Down Expand Up @@ -791,7 +821,9 @@ describe('EcsFollowerAdapter integration', () => {
})

it('keeps the targeted update path for value changes and same-frame re-adds', () => {
setCrdtDebugEnabled(true)
const { widgets, deliver, widgetValue, destroy } = bindSeededHost()
clearDevEvents()

deliver(2, () => widgets.set('seed', 5))
deliver(3, () => {
Expand All @@ -802,6 +834,22 @@ describe('EcsFollowerAdapter integration', () => {
expect(widgetValue('seed')).toBe(5)
expect(widgetValue('stale')).toBe(11)
expect(useWidgetValueStore().clearNode).not.toHaveBeenCalled()
expect(
devEvents.value
.filter(({ kind }) => kind === 'doc_effects')
.map(({ detail }) => detail)
).toEqual([
expect.objectContaining({
seq: 2,
reconcile: false,
changedWidgets: [['1', ['seed']]]
}),
expect.objectContaining({
seq: 3,
reconcile: false,
changedWidgets: [['1', ['stale']]]
})
])
destroy()
})

Expand Down
18 changes: 18 additions & 0 deletions src/workbench/extensions/agent/crdt/ecsFollowerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
import type { RemoteMutationContext } from '@/types/graphMutationContext'
import { toNodeId } from '@/types/nodeId'

import { isCrdtDebugEnabled } from './crdtDebugGate'
import { docLog } from './crdtLog'
import type { DocUpdate } from './docFrameClient'
import type { FollowerDoc } from './followerDoc'

Expand Down Expand Up @@ -307,6 +309,22 @@ export class EcsFollowerAdapter {
// cleanup instead of falling through to incremental handling with
// stale local-only graph state still present.
if (committed) session.reconcileNextFrame = false
if (isCrdtDebugEnabled()) {
docLog.debug('doc_effects', 'document projection batch outcome', {
workflowId: session.workflowId,
seq: update.seq,
reconcile,
committed,
nodeActions: [...nodeActions],
changedWidgets: [...changedWidgets].map(([id, names]) => [
id,
[...names]
]),
replacedWidgetMaps: [...replacedWidgetMaps],
changedLinkIds: [...changedLinkIds],
removedLinkIds
})
}
return committed
}

Expand Down
Loading
Loading