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
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
31 changes: 30 additions & 1 deletion src/workbench/extensions/agent/crdt/liveGraphApplier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
})
47 changes: 44 additions & 3 deletions src/workbench/extensions/agent/crdt/liveGraphApplier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ISerialisedGroup> {
const parsed = new Map<string, ISerialisedGroup>()
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<string, unknown>): 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:<thread>:<turn>` and `human:<user>:<tab>` identify a user.
Expand Down
Loading