From 39b7be07d5bb39eff11f41ddeb13a078ffe25447 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 04:47:06 +0000 Subject: [PATCH] fix(agent): show inserted groups that carry no id (#22958) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An agent turn that asks for a group can report "Done, both KSamplers are in one group" while no group ever appears on the canvas, also after a save and reload (FE 1.60 QA REG04, on 1.59, 1.60 and 1.61.0.dev1). Part of FRONT-3833. Cause: `insert_workflow` keeps a group's `id` only when the inserted workflow gave it one (`remap` only rewrites a present `id`), so the host accepts a group written as just `{ title, bounding }` and stores it in `meta.groups` with no id. `readDocGroups` required `id: z.number()` and dropped that record silently, in both the change collector and the applier, so the group never reached the canvas or the saved workflow. Fix: a `meta.groups` record without a numeric id gets a stable id derived from its content (FNV-1a over key-sorted JSON). The collector and the applier name the same canvas group on every read, and a later `clear` removes it. Records with a numeric id are unchanged. - Integration (`agentCrdtProjection.integration.test.ts`): "creates and clears an inserted group that carries no id" — red on main (`expected [] to deeply equal [ 'KSamplers' ]`), green here. crdt suite: 1415 pass. - Browser (`agentStageFramesFromInsertedWorkflow.spec.ts`): "shows stage frames the agent inserted without group ids, and saves them" — the same PM-947 stage-frames insertion with the group ids stripped must show both frames and save them. Related: #22945 (a group drawn around existing nodes was moved off them) is the other REG04 cause. (cherry picked from commit 723f62848af0d3c739c591b905c12612cc76a331) --- .../agentCrdtProjection.integration.test.ts | 65 +++++++++++++++++++ .../agent/crdt/liveGraphApplier.test.ts | 31 ++++++++- .../extensions/agent/crdt/liveGraphApplier.ts | 47 +++++++++++++- 3 files changed, 139 insertions(+), 4 deletions(-) diff --git a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts index 57bae5bc35e..b51ad87f98b 100644 --- a/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts +++ b/src/workbench/extensions/agent/crdt/agentCrdtProjection.integration.test.ts @@ -509,6 +509,71 @@ describe('AgentCrdtProjection catch-up over a live graph', () => { ]).toEqual([-20, -20]) expect([...group.size]).toEqual([300, 200]) }) + it('creates and clears id-less inserted groups whose titles differ only by emoji', () => { + const { graph, source, note } = buildLiveGraph() + source.setSize([200, 100]) + note.setSize([200, 100]) + const { host, hostEdit, destroy } = bindAndCatchUp( + graph, + structuredClone(graph.serialize()) + ) + onTestFinished(destroy) + + hostEdit( + () => + applyHostOp(host, { + ...HOST_OP, + op_id: '1a6a'.padEnd(32, '0'), + op: 'insert_workflow', + workflow: { + nodes: [ + { + id: 1, + type: 'TestSource', + pos: [20, 30], + size: [200, 100], + widgets_values: [20, 7] + } + ], + groups: [ + { title: 'Stage 😀', bounding: [0, 10, 300, 200] }, + { title: 'Stage 😁', bounding: [0, 10, 300, 200] } + ] + } + }), + { actor: HOST_OP.actor, catchUp: false } + ) + + const groupTitles = graph.groups.map(({ title }) => title) + expect(groupTitles).toHaveLength(2) + expect(groupTitles).toEqual( + expect.arrayContaining(['Stage 😀', 'Stage 😁']) + ) + const inserted = graph._nodes.find( + (node) => node !== source && node !== note + ) + assert(inserted, 'the insertion reached the canvas') + expect( + graph.groups.map((group) => [ + group.pos[0] - inserted.pos[0], + group.pos[1] - inserted.pos[1] + ]) + ).toEqual([ + [-20, -20], + [-20, -20] + ]) + + hostEdit(() => + applyHostOp(host, { + ...HOST_OP, + op_id: 'c1eb'.padEnd(32, '0'), + op: 'clear', + removed_nodes: graph._nodes.map(({ id }) => Number(id)) + }) + ) + + expect(graph.groups).toEqual([]) + }) it('keeps a group-only insertion on the existing nodes it frames', () => { const { graph, source, note } = buildLiveGraph() source.setSize([200, 100]) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts index d3afdf52e0b..79c16a4ad14 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts @@ -44,7 +44,7 @@ import type { WidgetValue } from '@/types/simplifiedWidget' import { followedDoc } from './__fixtures__/followedDoc' import { attachDocOpMinter } from './docOpMinter' import type { GraphOperation } from './graphOperations' -import { LiveGraphApplier } from './liveGraphApplier' +import { LiveGraphApplier, readDocGroups } from './liveGraphApplier' import type { LiveGraphApplierDeps, RemoteApplyContext @@ -2846,3 +2846,32 @@ describe('LiveGraphApplier change bracket', () => { expect(events).toHaveLength(4) }) }) + +describe('readDocGroups', () => { + const frame = { title: 'Stage', bounding: [0, 0, 100, 100] } + + it('keeps a stored positive integer id', () => { + expect([...readDocGroups([{ ...frame, id: 7 }]).keys()]).toEqual(['7']) + }) + + it('names a record without a usable id the same way on every read', () => { + const records = [frame, { ...frame, id: -1 }, { ...frame, id: Number.NaN }] + const first = [...readDocGroups(records).values()].map(({ id }) => id) + const second = [...readDocGroups(records).values()].map(({ id }) => id) + expect(first).toHaveLength(3) + expect(second).toEqual(first) + expect(first.every((id) => Number.isSafeInteger(id) && id > 0)).toBe(true) + }) + + it('tells apart id-less records whose titles differ only in an astral character', () => { + const groups = readDocGroups([ + { ...frame, title: 'Stage \u{1F600}' }, + { ...frame, title: 'Stage \u{1F601}' } + ]) + expect(groups.size).toBe(2) + }) + + it('reads an id-less record that carries a bigint field', () => { + expect(readDocGroups([{ ...frame, extra: 1n }]).size).toBe(1) + }) +}) diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts index 970ab15d6fb..e25f97e1421 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.ts @@ -230,19 +230,60 @@ const zDocNodeFields = zComfyNode [OPAQUE_WIDGETS_KEY]: zWidgetRecord.optional() }) -const zDocGroup = zGroup.extend({ id: z.number() }) +const zDocGroup = zGroup.extend({ id: z.unknown().optional() }) -/** The `meta.groups` records that configure a canvas group, keyed by id. */ +/** + * The `meta.groups` records that configure a canvas group, keyed by id; a + * record without a usable id is keyed by a hash of its content. + */ export function readDocGroups(groups: unknown): Map { const parsed = new Map() if (!Array.isArray(groups)) return parsed for (const record of groups) { const group = zDocGroup.safeParse(structuredClone(record)) - if (group.success) parsed.set(String(group.data.id), group.data) + if (!group.success) continue + const id = isGroupId(group.data.id) + ? group.data.id + : contentGroupId(group.data) + parsed.set(String(id), { ...group.data, id }) } return parsed } +function isGroupId(id: unknown): id is number { + return typeof id === 'number' && Number.isSafeInteger(id) && id > 0 +} + +function contentGroupId(record: Record): number { + const json = canonicalJson(record) + let h1 = 0xdeadbeef + let h2 = 0x41c6ce57 + for (let i = 0; i < json.length; i++) { + const code = json.charCodeAt(i) + h1 = Math.imul(h1 ^ code, 2654435761) + h2 = Math.imul(h2 ^ code, 1597334677) + } + h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) + h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909) + h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) + h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909) + return 4294967296 * (2097151 & h2) + (h1 >>> 0) + 1 +} + +function canonicalJson(value: unknown): string { + if (typeof value === 'bigint') return `${value}n` + if (value === undefined) return 'null' + if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]` + if (value !== null && typeof value === 'object') { + const entries = Object.entries(value) + .filter(([, item]) => item !== undefined) + .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)) + .map(([key, item]) => `${JSON.stringify(key)}:${canonicalJson(item)}`) + return `{${entries.join(',')}}` + } + return JSON.stringify(value) +} + /** * Only the actor's kind is reported: the segments after it in * `agent::` and `human::` identify a user.