Line data Source code
1 : /**
2 : * The follower's read-only view of its own document.
3 : *
4 : * Split out of `crdtDebugReport.ts` so the composable — which every agent
5 : * panel loads eagerly — does not pull the report collector, its redaction
6 : * helpers and the extension store into that bundle. Only the debug panel,
7 : * behind its async boundary, needs those.
8 : */
9 : import {
10 : appliedOpIds,
11 : readGraph,
12 : readMeta,
13 : readStamps
14 : } from '@comfyorg/comfy-multi-player'
15 : import type * as Y from 'yjs'
16 :
17 : import type { AgentCrdtStatus } from './agentCrdtStatus'
18 :
19 : /** Live CRDT internals, read from the follower at the moment Copy is pressed. */
20 : export interface CrdtDebugSnapshot {
21 : status: AgentCrdtStatus
22 : tabId: string | null
23 : lastSeq: number | null
24 : schemaError: string | null
25 : meta: Readonly<Record<string, unknown>>
26 : nodeIds: readonly string[]
27 : linkIds: readonly string[]
28 : appliedOpIds: readonly string[]
29 : stamps: Readonly<Record<string, unknown>>
30 : }
31 :
32 : /**
33 : * Read every CRDT-observable fact out of a follower doc.
34 : *
35 : * Uses the package's read-only snapshot surface rather than the live Y types:
36 : * those accessors never materialize a root on an empty document and never hand
37 : * back a writable handle, so building a report can never perturb the thing it
38 : * is reporting on.
39 : */
40 0 : export function readCrdtSnapshot(
41 : doc: Y.Doc | null,
42 : base: Omit<
43 : CrdtDebugSnapshot,
44 : 'meta' | 'nodeIds' | 'linkIds' | 'appliedOpIds' | 'stamps'
45 : >
46 : ): CrdtDebugSnapshot {
47 0 : if (doc === null) {
48 0 : return {
49 : ...base,
50 : meta: {},
51 : nodeIds: [],
52 : linkIds: [],
53 : appliedOpIds: [],
54 : stamps: {}
55 : }
56 : }
57 0 : try {
58 0 : const graph = readGraph(doc)
59 0 : return {
60 : ...base,
61 : meta: readMeta(doc),
62 : nodeIds: Object.keys(graph.nodes),
63 : linkIds: Object.keys(graph.links),
64 : appliedOpIds: appliedOpIds(doc),
65 : stamps: readStamps(doc)
66 : }
67 : } catch (error) {
68 : // A doc this build cannot read (KA-11) still has to produce a report —
69 : // that failure IS the bug being reported.
70 0 : return {
71 : ...base,
72 : schemaError: base.schemaError ?? String(error),
73 : meta: {},
74 : nodeIds: [],
75 : linkIds: [],
76 : appliedOpIds: [],
77 : stamps: {}
78 : }
79 : }
80 : }
|