Skip to content
Open
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
170 changes: 170 additions & 0 deletions src/workbench/extensions/agent/crdt/docOpMinter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,176 @@ describe('attachDocOpMinter', () => {
expect(minted).toEqual([])
})

it.for([
{
name: 'live true and stored false',
liveSerialize: true,
storedSerialize: false,
expectedOps: ['set_widget', 'set_widget']
},
{
name: 'live undefined and stored false',
liveSerialize: undefined,
storedSerialize: false,
expectedOps: []
},
{
name: 'live false and stored true',
liveSerialize: false,
storedSerialize: true,
expectedOps: []
}
])('reports $name once across separate writes', async ({
liveSerialize,
storedSerialize,
expectedOps
}) => {
const { source } = seedGraph(graph)
const widget = source.widgets![0]
const stored = useWidgetValueStore().getWidget(
widgetId(graph.id, source.id, widget.name)
)
assert.exists(stored)
stored.serialize = storedSerialize
widget.serialize = liveSerialize

emitGraphIntent({
type: 'set_widget',
graphId: graph.id,
nodeId: source.id,
name: 'steps',
value: 21,
previous: 20
})
await afterFlush()
emitGraphIntent({
type: 'set_widget',
graphId: graph.id,
nodeId: source.id,
name: 'steps',
value: 22,
previous: 21
})
await afterFlush()

expect(minted.map(({ op }) => op)).toEqual(expectedOps)
const reports = vi
.mocked(reportError)
.mock.calls.filter(
([, metadata]) =>
metadata.errorType === 'agent_crdt_widget_serialize_drift'
)
expect(reports).toHaveLength(1)
expect(reports[0]?.[1].context).toEqual({
graphId: graph.id,
nodeId: source.id,
widget: 'steps',
liveSerialize: liveSerialize ?? null,
storedSerialize
})
})

it('reports a serialize disagreement while filtering an add-node snapshot', async () => {
const source = new TestSource()
graph.add(source)
const widget = source.widgets![0]
const stored = useWidgetValueStore().getWidget(
widgetId(graph.id, source.id, widget.name)
)
assert.exists(stored)
stored.serialize = false
widget.serialize = undefined

await afterFlush()

expect(minted).toEqual([
expect.objectContaining({
op: 'add_node',
node: expect.objectContaining({ widgets_values: {} })
})
])
const reports = vi
.mocked(reportError)
.mock.calls.filter(
([, metadata]) =>
metadata.errorType === 'agent_crdt_widget_serialize_drift'
)
expect(reports).toHaveLength(1)
expect(reports[0]?.[1].context).toEqual({
graphId: graph.id,
nodeId: source.id,
widget: 'steps',
liveSerialize: null,
storedSerialize: false
})
})

it('reports a live serialize:false widget omitted by add-node serialization', async () => {
const source = new TestSource()
graph.add(source)
const widget = source.widgets![0]
const stored = useWidgetValueStore().getWidget(
widgetId(graph.id, source.id, widget.name)
)
assert.exists(stored)
stored.serialize = true
widget.serialize = false

await afterFlush()

expect(minted).toEqual([
expect.objectContaining({
op: 'add_node',
node: expect.objectContaining({ widgets_values: {} })
})
])
const reports = vi
.mocked(reportError)
.mock.calls.filter(
([, metadata]) =>
metadata.errorType === 'agent_crdt_widget_serialize_drift'
)
expect(reports).toHaveLength(1)
expect(reports[0]?.[1].context).toEqual({
graphId: graph.id,
nodeId: source.id,
widget: 'steps',
liveSerialize: false,
storedSerialize: true
})
})

it('does not report when live and stored serialize flags agree', async () => {
const { source } = seedGraph(graph)
const widget = source.widgets![0]
const stored = useWidgetValueStore().getWidget(
widgetId(graph.id, source.id, widget.name)
)
assert.exists(stored)
stored.serialize = false
widget.serialize = false

emitGraphIntent({
type: 'set_widget',
graphId: graph.id,
nodeId: source.id,
name: 'steps',
value: 21,
previous: 20
})
await afterFlush()

expect(minted).toEqual([])
expect(
vi
.mocked(reportError)
.mock.calls.filter(
([, metadata]) =>
metadata.errorType === 'agent_crdt_widget_serialize_drift'
)
).toEqual([])
})

it('uses the live serialize flag for direct store-path intents', async () => {
const { source } = seedGraph(graph)
source.widgets![0].serialize = false
Expand Down
74 changes: 68 additions & 6 deletions src/workbench/extensions/agent/crdt/docOpMinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ type PendingOp =
| { kind: 'add_node'; graph: LGraph; node: LGraphNode }
| { kind: 'op'; operation: GraphOperation }

type SerializeDriftReporter = (
name: string,
liveSerialize: boolean | undefined,
storedSerialize: boolean | undefined
) => void

/**
* Serialized save-format node. `widgets_values` is NAME-KEYED via the node's
* own `widgets_values_named` minus non-value widgets (FE-1904: the doc host's
Expand All @@ -103,7 +109,10 @@ type PendingOp =
* `flags.ghost` is a placement-in-progress marker the placement click clears
* without minting, so the document must not record it.
*/
export function wireNodeSnapshot(node: LGraphNode): WorkflowNode | null {
export function wireNodeSnapshot(
node: LGraphNode,
onSerializeDrift?: SerializeDriftReporter
): WorkflowNode | null {
let serialized: ISerialisedNode
try {
serialized = node.serialize()
Expand All @@ -118,6 +127,21 @@ export function wireNodeSnapshot(node: LGraphNode): WorkflowNode | null {
...rest
} = wireSerialized
const snapshot = { ...rest, flags } satisfies WorkflowNode

// serialize() omits widgets marked serialize:false from its named payload.
// Inspect the live widgets first so those omissions still report drift.
const rootGraphId = node.graph?.rootGraph.id
if (rootGraphId && onSerializeDrift) {
const widgetValueStore = useWidgetValueStore()
for (const widget of node.widgets ?? []) {
const stored = widgetValueStore.getWidget(
widgetId(rootGraphId, node.id, widget.name)
)
if (stored && widget.serialize !== stored.serialize)
onSerializeDrift(widget.name, widget.serialize, stored.serialize)
}
}

return named && !node.isVirtualNode
? { ...snapshot, widgets_values: valueWidgetsOnly(node, named) }
: snapshot
Expand All @@ -142,9 +166,15 @@ function valueWidgetsOnly(

function isValueWidget(
widget: IBaseWidget | undefined,
stored?: WidgetState
stored?: WidgetState,
onSerializeDrift?: (
liveSerialize: boolean | undefined,
storedSerialize: boolean | undefined
) => void
): boolean {
if (!widget && !stored) return false
if (widget && stored && widget.serialize !== stored.serialize)
onSerializeDrift?.(widget.serialize, stored.serialize)
// A destructuring default fires on absent AND present-but-undefined alike,
// which is what `IBaseWidget`'s own optional fields mean: the live widget
// did not state this, so the registration the intent was keyed by answers.
Expand Down Expand Up @@ -176,7 +206,8 @@ function reachableIntentGraph(
*/
function isValueWidgetWrite(
owner: LGraphNode | null,
event: IntentOf<'set_widget'>
event: IntentOf<'set_widget'>,
onSerializeDrift?: SerializeDriftReporter
): boolean {
const rootGraphId = owner?.graph?.rootGraph.id ?? event.graphId
const stored = useWidgetValueStore().getWidget(
Expand All @@ -185,7 +216,9 @@ function isValueWidgetWrite(
const widget = owner?.widgets?.find(
(candidate) => candidate.name === event.name
)
return isValueWidget(widget, stored)
return isValueWidget(widget, stored, (liveSerialize, storedSerialize) =>
onSerializeDrift?.(event.name, liveSerialize, storedSerialize)
)
}

/**
Expand Down Expand Up @@ -400,7 +433,9 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter {
}
const { graph, node } = entry
if (node.graph !== graph) continue
const snapshot = wireNodeSnapshot(node)
const snapshot = wireNodeSnapshot(node, (name, live, stored) =>
reportSerializeDrift(graph.id, node.id, name, live, stored)
)
if (!snapshot) {
console.error(
'[agent-crdt] add_node mint dropped: no snapshot for node',
Expand Down Expand Up @@ -431,6 +466,28 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter {
reportError(new Error(message), { surface: 'agent', errorType, context })
}

function reportSerializeDrift(
graphId: string,
nodeId: NodeId,
name: string,
liveSerialize: boolean | undefined,
storedSerialize: boolean | undefined
): void {
reportOnce(
`widget_serialize:${graphId}:${String(nodeId)}:${name}`,
`Widget ${name} on node ${String(nodeId)} has a live serialize flag that disagrees with its registered metadata`,
'agent_crdt_widget_serialize_drift',
{
graphId,
nodeId,
widget: name,
liveSerialize: liveSerialize ?? null,
storedSerialize: storedSerialize ?? null
},
reportedDrift
)
}

/** True when `graph` is the bound document's root graph. */
function isMintableRootScope(
graph: LGraph,
Expand Down Expand Up @@ -468,7 +525,12 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter {
const owner = eventGraph
? findNodeInHierarchy(eventGraph, event.nodeId)
: null
if (!isValueWidgetWrite(owner, event)) return
if (
!isValueWidgetWrite(owner, event, (name, live, stored) =>
reportSerializeDrift(event.graphId, event.nodeId, name, live, stored)
)
)
return
const rootGraphId = deps.boundRootGraphId() ?? graph.id
const operation = routedWidgetOperation(
graph,
Expand Down
Loading