@amalgm/live 0.2.81-main.43715442758 → 0.2.81-main.43756402472

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/dist/entities/index.d.ts +3 -0
  2. package/dist/entities/index.d.ts.map +1 -1
  3. package/dist/entities/index.js +3 -0
  4. package/dist/entities/index.js.map +1 -1
  5. package/dist/entities/registry-graph.d.ts +119 -0
  6. package/dist/entities/registry-graph.d.ts.map +1 -0
  7. package/dist/entities/registry-graph.js +478 -0
  8. package/dist/entities/registry-graph.js.map +1 -0
  9. package/dist/files/accepted-files.d.ts +66 -0
  10. package/dist/files/accepted-files.d.ts.map +1 -0
  11. package/dist/files/accepted-files.js +126 -0
  12. package/dist/files/accepted-files.js.map +1 -0
  13. package/dist/files/index.d.ts +13 -0
  14. package/dist/files/index.d.ts.map +1 -1
  15. package/dist/files/index.js +7 -0
  16. package/dist/files/index.js.map +1 -1
  17. package/dist/files/media-type.d.ts +2 -0
  18. package/dist/files/media-type.d.ts.map +1 -0
  19. package/dist/files/media-type.js +50 -0
  20. package/dist/files/media-type.js.map +1 -0
  21. package/dist/files/plans.d.ts +29 -0
  22. package/dist/files/plans.d.ts.map +1 -0
  23. package/dist/files/plans.js +36 -0
  24. package/dist/files/plans.js.map +1 -0
  25. package/dist/files/usage-categories.d.ts +17 -0
  26. package/dist/files/usage-categories.d.ts.map +1 -0
  27. package/dist/files/usage-categories.js +33 -0
  28. package/dist/files/usage-categories.js.map +1 -0
  29. package/dist/files/usage-chain.d.ts +37 -0
  30. package/dist/files/usage-chain.d.ts.map +1 -0
  31. package/dist/files/usage-chain.js +44 -0
  32. package/dist/files/usage-chain.js.map +1 -0
  33. package/dist/files/usage-effect.d.ts +56 -0
  34. package/dist/files/usage-effect.d.ts.map +1 -0
  35. package/dist/files/usage-effect.js +29 -0
  36. package/dist/files/usage-effect.js.map +1 -0
  37. package/dist/files/usage.d.ts +80 -0
  38. package/dist/files/usage.d.ts.map +1 -0
  39. package/dist/files/usage.js +132 -0
  40. package/dist/files/usage.js.map +1 -0
  41. package/dist/git/artifact.d.ts +12 -3
  42. package/dist/git/artifact.d.ts.map +1 -1
  43. package/dist/git/artifact.js +30 -23
  44. package/dist/git/artifact.js.map +1 -1
  45. package/dist-cjs/entities/index.js +3 -0
  46. package/dist-cjs/entities/index.js.map +1 -1
  47. package/dist-cjs/entities/registry-graph.js +483 -0
  48. package/dist-cjs/entities/registry-graph.js.map +1 -0
  49. package/dist-cjs/files/accepted-files.js +132 -0
  50. package/dist-cjs/files/accepted-files.js.map +1 -0
  51. package/dist-cjs/files/index.js +28 -1
  52. package/dist-cjs/files/index.js.map +1 -1
  53. package/dist-cjs/files/media-type.js +53 -0
  54. package/dist-cjs/files/media-type.js.map +1 -0
  55. package/dist-cjs/files/plans.js +40 -0
  56. package/dist-cjs/files/plans.js.map +1 -0
  57. package/dist-cjs/files/usage-categories.js +37 -0
  58. package/dist-cjs/files/usage-categories.js.map +1 -0
  59. package/dist-cjs/files/usage-chain.js +49 -0
  60. package/dist-cjs/files/usage-chain.js.map +1 -0
  61. package/dist-cjs/files/usage-effect.js +34 -0
  62. package/dist-cjs/files/usage-effect.js.map +1 -0
  63. package/dist-cjs/files/usage.js +140 -0
  64. package/dist-cjs/files/usage.js.map +1 -0
  65. package/dist-cjs/git/artifact.js +30 -23
  66. package/dist-cjs/git/artifact.js.map +1 -1
  67. package/package.json +4 -4
@@ -20,6 +20,8 @@
20
20
  * committed head
21
21
  * - graph-pages.ts a root's immutable record pages and parent index
22
22
  * - graph-head.ts the committed graph head codec
23
+ * - registry-graph.ts a committed head read lazily, a page at a time, as
24
+ * the accepted view's points
23
25
  * - package-pages.ts one new subtree as one record's canonical pages
24
26
  */
25
27
  export * from './types.js';
@@ -32,5 +34,6 @@ export * from './reducer.js';
32
34
  export * from './accepted-view.js';
33
35
  export * from './graph-pages.js';
34
36
  export * from './graph-head.js';
37
+ export * from './registry-graph.js';
35
38
  export * from './package-pages.js';
36
39
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entities/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entities/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC"}
@@ -20,6 +20,8 @@
20
20
  * committed head
21
21
  * - graph-pages.ts a root's immutable record pages and parent index
22
22
  * - graph-head.ts the committed graph head codec
23
+ * - registry-graph.ts a committed head read lazily, a page at a time, as
24
+ * the accepted view's points
23
25
  * - package-pages.ts one new subtree as one record's canonical pages
24
26
  */
25
27
  export * from './types.js';
@@ -32,5 +34,6 @@ export * from './reducer.js';
32
34
  export * from './accepted-view.js';
33
35
  export * from './graph-pages.js';
34
36
  export * from './graph-head.js';
37
+ export * from './registry-graph.js';
35
38
  export * from './package-pages.js';
36
39
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/entities/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/entities/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * The registry graph as a reader holds it: a computer, or the authority
3
+ * answering one admitted read.
4
+ *
5
+ * The authority stores the graph as a small head naming each root's immutable
6
+ * pages, and a reader holds exactly that: the head, plus the records of
7
+ * only the roots the current operation materializes. Nothing ever
8
+ * materializes the whole account — and most reads materialize nothing.
9
+ *
10
+ * Two page lists per root (graph-pages.ts) make every point read cheap:
11
+ * - Record pages hold the root in (parentUUID, uuid) order, so one folder's
12
+ * children are found from the references alone and read from the few pages
13
+ * that hold them.
14
+ * - The parent index names each record's parent in uuid order, so an entity
15
+ * whose parent is unknown costs one parent-index page, then one record page.
16
+ *
17
+ * A reader proves each page it reads against its reference and believes the
18
+ * head for the rest; the authority proved every root whole before it
19
+ * committed it. Pages are parsed transiently and dropped, so a point read's
20
+ * memory is O(page), never O(root). Where H holds an entity never changes
21
+ * under one graph, so each entity's root and parent are read once.
22
+ *
23
+ * A resource whose authority snapshot is still records-shaped (a login's
24
+ * initial publication, before any head has committed) is held with a null
25
+ * head and every record in memory — such graphs are one machine's first
26
+ * publication and small by construction.
27
+ */
28
+ import { type Steps } from '../steps.js';
29
+ import { type CloudEntityRecord, type CloudSnapshot } from './cloud.js';
30
+ import { type CloudGraphHead } from './graph-head.js';
31
+ import { type GraphPageRef } from './graph-pages.js';
32
+ import type { Sha256Hex } from './types.js';
33
+ /** What names one immutable page's bytes: a record page and a parent-index
34
+ * page alike. */
35
+ export type GraphPageIdentity = Pick<GraphPageRef, 'sha256' | 'bytes'>;
36
+ /** Read one immutable page's bytes by identity. The reader owns caching and
37
+ * transport; the graph owns every proof over what comes back. */
38
+ export type GraphPageReader = (ref: GraphPageIdentity) => Promise<Uint8Array>;
39
+ /** Run a long synchronous pass to its answer. A host sharing its thread
40
+ * runs it across its own turns; the default runs it straight through. */
41
+ export type StepsFinisher = <R>(steps: Steps<R>) => Promise<R>;
42
+ export declare class RegistryGraph {
43
+ /** The committed head, or null while the authority still holds a
44
+ * records-shaped snapshot. */
45
+ readonly head: CloudGraphHead | null;
46
+ private readonly roots;
47
+ private readonly readPage;
48
+ private readonly sha256Hex;
49
+ private readonly finish;
50
+ /** Where H holds each uuid already asked about: its holder, or null once
51
+ * every root has said it holds none. */
52
+ private readonly placed;
53
+ private constructor();
54
+ /** Hold a records-shaped snapshot fully materialized: it is a first
55
+ * publication's own graph and carries no page identities to defer to. */
56
+ static fromRecords(records: readonly CloudEntityRecord[], sha256Hex: Sha256Hex): RegistryGraph;
57
+ /** Hold a committed head lazily: every root starts as page identities. */
58
+ static fromHead(head: CloudGraphHead, readPage: GraphPageReader, sha256Hex: Sha256Hex, finish?: StepsFinisher): RegistryGraph;
59
+ rootIds(): readonly string[];
60
+ /** The records of one root as this machine has already materialized them,
61
+ * or null when that root is still page identities. */
62
+ loadedRecords(rootId: string): readonly CloudEntityRecord[] | null;
63
+ /** Materialize one root: every record page proven and parsed in order with
64
+ * a bounded prefetch, bytes dropped as they are consumed, then the complete
65
+ * subtree law over what they hold. An absent root answers empty rather than
66
+ * faulting — reading a root that is not there is how callers learn it is
67
+ * not there. */
68
+ recordsFor(rootId: string): Promise<readonly CloudEntityRecord[]>;
69
+ /** One root's own record — the discovery unit. It opens the root's first
70
+ * record page, since no parent sorts before every parent. */
71
+ rootRecord(rootId: string): Promise<CloudEntityRecord>;
72
+ /** Every root's own record, for discovery over the whole account — which
73
+ * root is the core, which roots are workspaces — at one page per root. */
74
+ rootRecords(): Promise<readonly CloudEntityRecord[]>;
75
+ /** H's chain of each uuid the graph holds: the uuid, then each parent up
76
+ * to its root. Each level climbs by the parent index, reading each page
77
+ * that could name that level's ancestors once. */
78
+ chains(uuids: readonly string[]): Promise<ReadonlyMap<string, readonly string[]>>;
79
+ /** H's row of each uuid the graph holds: its parent from the parent index,
80
+ * then the one record page its key falls in, each page read once. */
81
+ records(uuids: readonly string[]): Promise<ReadonlyMap<string, CloudEntityRecord>>;
82
+ /** H's rows directly beneath each parent the graph holds, any status, in
83
+ * uuid order: read from the record pages that can hold them, each once.
84
+ * Where H holds each row is then known. */
85
+ children(parents: readonly string[]): Promise<ReadonlyMap<string, readonly CloudEntityRecord[]>>;
86
+ /** Which root holds each uuid the graph holds. */
87
+ rootsOf(uuids: readonly string[]): Promise<ReadonlyMap<string, string>>;
88
+ /** The checksum the authority holds for this graph right now. */
89
+ checksum(): string;
90
+ /** The records shape of a headless graph — a first publication's own small
91
+ * snapshot. A committed head never flattens: readers are root-scoped. */
92
+ recordsShapedSnapshot(): CloudSnapshot;
93
+ private byUuid;
94
+ private byParent;
95
+ /** Where H holds each uuid: its root and its parent. A uuid naming a root
96
+ * is answered directly, one already placed from memory, and a materialized
97
+ * root from its records; every other root reads each parent-index page that
98
+ * could name a uuid still unplaced once. */
99
+ private holders;
100
+ /** Where H is already known to hold `uuid`: its holder, null for nowhere,
101
+ * or undefined while unasked. */
102
+ private heldAt;
103
+ /** The holders one root has for `uuids`, remembered. */
104
+ private holdersIn;
105
+ /** The parent one root names for each uuid it holds — null for the root
106
+ * itself. */
107
+ private parentsIn;
108
+ /** One page proven against its reference and parsed. */
109
+ private rows;
110
+ /** Prove, parse, and visit pages in order, fetching a bounded window ahead
111
+ * so the network pipeline stays full while transient memory stays at a few
112
+ * page budgets. */
113
+ private scanPages;
114
+ }
115
+ /** Hold the authority's stored snapshot bytes as this machine's graph. A head
116
+ * is held lazily — no page is read here — and a records-shaped snapshot is
117
+ * grouped in place. */
118
+ export declare function registryGraphFromSnapshot(bytes: Uint8Array, readPage: GraphPageReader, sha256Hex: Sha256Hex, finish?: StepsFinisher): RegistryGraph;
119
+ //# sourceMappingURL=registry-graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry-graph.d.ts","sourceRoot":"","sources":["../../src/entities/registry-graph.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAe,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AAEtD,OAAO,EAML,KAAK,iBAAiB,EACtB,KAAK,aAAa,EACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAKL,KAAK,cAAc,EACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAOL,KAAK,YAAY,EAGlB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C;iBACiB;AACjB,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC,YAAY,EAAE,QAAQ,GAAG,OAAO,CAAC,CAAC;AAEvE;iEACiE;AACjE,MAAM,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,iBAAiB,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;AAE9E;yEACyE;AACzE,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;AAkF/D,qBAAa,aAAa;IACxB;kCAC8B;IAC9B,QAAQ,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI,CAAC;IAErC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAE/C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkB;IAE3C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IAEtC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IAEvC;4CACwC;IACxC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoC;IAE3D,OAAO;IAcP;6EACyE;IACzE,MAAM,CAAC,WAAW,CAChB,OAAO,EAAE,SAAS,iBAAiB,EAAE,EACrC,SAAS,EAAE,SAAS,GACnB,aAAa;IAkBhB,0EAA0E;IAC1E,MAAM,CAAC,QAAQ,CACb,IAAI,EAAE,cAAc,EACpB,QAAQ,EAAE,eAAe,EACzB,SAAS,EAAE,SAAS,EACpB,MAAM,GAAE,aAAyB,GAChC,aAAa;IAehB,OAAO,IAAI,SAAS,MAAM,EAAE;IAI5B;0DACsD;IACtD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,iBAAiB,EAAE,GAAG,IAAI;IAIlE;;;;oBAIgB;IACV,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,iBAAiB,EAAE,CAAC;IAuBvE;iEAC6D;IACvD,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAe5D;8EAC0E;IACpE,WAAW,IAAI,OAAO,CAAC,SAAS,iBAAiB,EAAE,CAAC;IAI1D;;sDAEkD;IAC5C,MAAM,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;IAiCvF;yEACqE;IAC/D,OAAO,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;IA6BxF;;+CAE2C;IACrC,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,iBAAiB,EAAE,CAAC,CAAC;IAkCtG,kDAAkD;IAC5C,OAAO,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAI7E,iEAAiE;IACjE,QAAQ,IAAI,MAAM;IAKlB;6EACyE;IACzE,qBAAqB,IAAI,aAAa;IAatC,OAAO,CAAC,MAAM;IAId,OAAO,CAAC,QAAQ;IAYhB;;;gDAG4C;YAC9B,OAAO;IAkBrB;qCACiC;IACjC,OAAO,CAAC,MAAM;IAId,wDAAwD;YAC1C,SAAS;IAUvB;iBACa;YACC,SAAS;IAyBvB,wDAAwD;YAC1C,IAAI;IAIlB;;uBAEmB;YACL,SAAS;CAmBxB;AAED;;uBAEuB;AACvB,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,eAAe,EACzB,SAAS,EAAE,SAAS,EACpB,MAAM,GAAE,aAAyB,GAChC,aAAa,CAKf"}
@@ -0,0 +1,478 @@
1
+ /**
2
+ * The registry graph as a reader holds it: a computer, or the authority
3
+ * answering one admitted read.
4
+ *
5
+ * The authority stores the graph as a small head naming each root's immutable
6
+ * pages, and a reader holds exactly that: the head, plus the records of
7
+ * only the roots the current operation materializes. Nothing ever
8
+ * materializes the whole account — and most reads materialize nothing.
9
+ *
10
+ * Two page lists per root (graph-pages.ts) make every point read cheap:
11
+ * - Record pages hold the root in (parentUUID, uuid) order, so one folder's
12
+ * children are found from the references alone and read from the few pages
13
+ * that hold them.
14
+ * - The parent index names each record's parent in uuid order, so an entity
15
+ * whose parent is unknown costs one parent-index page, then one record page.
16
+ *
17
+ * A reader proves each page it reads against its reference and believes the
18
+ * head for the rest; the authority proved every root whole before it
19
+ * committed it. Pages are parsed transiently and dropped, so a point read's
20
+ * memory is O(page), never O(root). Where H holds an entity never changes
21
+ * under one graph, so each entity's root and parent are read once.
22
+ *
23
+ * A resource whose authority snapshot is still records-shaped (a login's
24
+ * initial publication, before any head has committed) is held with a null
25
+ * head and every record in memory — such graphs are one machine's first
26
+ * publication and small by construction.
27
+ */
28
+ import { finishSteps } from '../steps.js';
29
+ import { utf8Decode } from './bytes.js';
30
+ import { completeRootsFromRecordsSteps, parseSnapshot, snapshotFromRecords, stableJson, ENTITY_CLOUD_SCHEMA_VERSION, } from './cloud.js';
31
+ import { graphHeadChecksum, parseGraphHead, recordsByRoot, GRAPH_HEAD_KIND, } from './graph-head.js';
32
+ import { compareGraphKeys, pageHolding, pagesHoldingChildren, parentPageHolding, parseGraphPage, } from './graph-pages.js';
33
+ const finishNow = async (steps) => finishSteps(steps);
34
+ /** How many pages a scan fetches ahead of its in-order proof. Bounds
35
+ * transient page bytes at PAGE_PREFETCH page budgets. */
36
+ const PAGE_PREFETCH = 4;
37
+ const NO_PAGES = { pages: [], parents: [] };
38
+ function rawUuid(row) {
39
+ return String(row?.uuid ?? '');
40
+ }
41
+ function rawKey(row) {
42
+ const parent = row?.parentUUID;
43
+ return [typeof parent === 'string' ? parent : null, rawUuid(row)];
44
+ }
45
+ /** One raw page row through the canonical record law, before anyone reads it. */
46
+ function assertedRecord(row) {
47
+ return snapshotFromRecords([row]).records[0];
48
+ }
49
+ /** The index of `target` in ascending `rows`, or -1. */
50
+ function search(rows, target, compare) {
51
+ let low = 0;
52
+ let high = rows.length - 1;
53
+ while (low <= high) {
54
+ const middle = (low + high) >> 1;
55
+ const order = compare(rows[middle], target);
56
+ if (order === 0)
57
+ return middle;
58
+ if (order < 0)
59
+ low = middle + 1;
60
+ else
61
+ high = middle - 1;
62
+ }
63
+ return -1;
64
+ }
65
+ function parentPair(row) {
66
+ if (!Array.isArray(row) || row.length !== 2 || typeof row[0] !== 'string'
67
+ || !(row[1] === null || typeof row[1] === 'string')) {
68
+ throw new Error('cloud graph parent index holds an invalid entry');
69
+ }
70
+ return [row[0], row[1]];
71
+ }
72
+ const byPairUuid = (row, uuid) => {
73
+ const named = parentPair(row)[0];
74
+ return named < uuid ? -1 : named > uuid ? 1 : 0;
75
+ };
76
+ function grouped(groups, key, open) {
77
+ const held = groups.get(key);
78
+ if (held !== undefined)
79
+ return held;
80
+ const opened = open();
81
+ groups.set(key, opened);
82
+ return opened;
83
+ }
84
+ function misplaced(rootId, uuid) {
85
+ return new Error(`cloud graph root ${rootId}'s parent index names ${uuid}, but its pages do not hold it`);
86
+ }
87
+ export class RegistryGraph {
88
+ /** The committed head, or null while the authority still holds a
89
+ * records-shaped snapshot. */
90
+ head;
91
+ roots;
92
+ readPage;
93
+ sha256Hex;
94
+ finish;
95
+ /** Where H holds each uuid already asked about: its holder, or null once
96
+ * every root has said it holds none. */
97
+ placed = new Map();
98
+ constructor(head, roots, readPage, sha256Hex, finish) {
99
+ this.head = head;
100
+ this.roots = roots;
101
+ this.readPage = readPage;
102
+ this.sha256Hex = sha256Hex;
103
+ this.finish = finish;
104
+ }
105
+ /** Hold a records-shaped snapshot fully materialized: it is a first
106
+ * publication's own graph and carries no page identities to defer to. */
107
+ static fromRecords(records, sha256Hex) {
108
+ const roots = new Map();
109
+ for (const [rootId, group] of recordsByRoot(records)) {
110
+ roots.set(rootId, {
111
+ refs: NO_PAGES,
112
+ records: group,
113
+ loading: null,
114
+ rootRecord: group.find((record) => record.uuid === rootId) ?? null,
115
+ byUuid: null,
116
+ byParent: null,
117
+ });
118
+ }
119
+ const reader = () => {
120
+ throw new Error('a records-shaped graph has no pages to read');
121
+ };
122
+ return new RegistryGraph(null, roots, reader, sha256Hex, finishNow);
123
+ }
124
+ /** Hold a committed head lazily: every root starts as page identities. */
125
+ static fromHead(head, readPage, sha256Hex, finish = finishNow) {
126
+ const roots = new Map();
127
+ for (const root of head.roots) {
128
+ roots.set(root.rootId, {
129
+ refs: { pages: root.pages, parents: root.parents },
130
+ records: null,
131
+ loading: null,
132
+ rootRecord: null,
133
+ byUuid: null,
134
+ byParent: null,
135
+ });
136
+ }
137
+ return new RegistryGraph(head, roots, readPage, sha256Hex, finish);
138
+ }
139
+ rootIds() {
140
+ return [...this.roots.keys()];
141
+ }
142
+ /** The records of one root as this machine has already materialized them,
143
+ * or null when that root is still page identities. */
144
+ loadedRecords(rootId) {
145
+ return this.roots.get(rootId.toLowerCase())?.records ?? null;
146
+ }
147
+ /** Materialize one root: every record page proven and parsed in order with
148
+ * a bounded prefetch, bytes dropped as they are consumed, then the complete
149
+ * subtree law over what they hold. An absent root answers empty rather than
150
+ * faulting — reading a root that is not there is how callers learn it is
151
+ * not there. */
152
+ async recordsFor(rootId) {
153
+ const normalized = rootId.toLowerCase();
154
+ const state = this.roots.get(normalized);
155
+ if (!state)
156
+ return [];
157
+ if (state.records)
158
+ return state.records;
159
+ state.loading ??= (async () => {
160
+ const assembled = [];
161
+ await this.scanPages(state.refs.pages, (rows) => {
162
+ assembled.push(...rows);
163
+ });
164
+ const records = await this.finish(completeRootsFromRecordsSteps([normalized], assembled));
165
+ state.records = records;
166
+ state.rootRecord = records.find((record) => record.uuid === normalized) ?? null;
167
+ return records;
168
+ })();
169
+ try {
170
+ return await state.loading;
171
+ }
172
+ catch (error) {
173
+ state.loading = null;
174
+ throw error;
175
+ }
176
+ }
177
+ /** One root's own record — the discovery unit. It opens the root's first
178
+ * record page, since no parent sorts before every parent. */
179
+ async rootRecord(rootId) {
180
+ const normalized = rootId.toLowerCase();
181
+ const state = this.roots.get(normalized);
182
+ if (!state)
183
+ throw new Error(`cloud graph has no root ${normalized}`);
184
+ if (state.rootRecord)
185
+ return state.rootRecord;
186
+ const first = state.records ? null : state.refs.pages[0];
187
+ const rows = first ? await this.rows(first) : [];
188
+ const found = rows.length > 0 ? assertedRecord(rows[0]) : null;
189
+ if (!found || found.uuid !== normalized || found.parentUUID !== null) {
190
+ throw new Error(`cloud graph root ${normalized} does not hold its own record`);
191
+ }
192
+ state.rootRecord = found;
193
+ return found;
194
+ }
195
+ /** Every root's own record, for discovery over the whole account — which
196
+ * root is the core, which roots are workspaces — at one page per root. */
197
+ async rootRecords() {
198
+ return Promise.all([...this.roots.keys()].map((rootId) => this.rootRecord(rootId)));
199
+ }
200
+ /** H's chain of each uuid the graph holds: the uuid, then each parent up
201
+ * to its root. Each level climbs by the parent index, reading each page
202
+ * that could name that level's ancestors once. */
203
+ async chains(uuids) {
204
+ const placed = await this.holders(uuids);
205
+ const parentOf = new Map();
206
+ for (let level = placed; level.size > 0;) {
207
+ for (const [uuid, { parent }] of level)
208
+ parentOf.set(uuid, parent);
209
+ const climbing = new Map();
210
+ for (const { root, parent } of level.values()) {
211
+ if (parent !== null && !parentOf.has(parent))
212
+ grouped(climbing, root, () => new Set()).add(parent);
213
+ }
214
+ const next = new Map();
215
+ for (const [root, ancestors] of climbing) {
216
+ const unknown = [...ancestors].filter((ancestor) => this.heldAt(ancestor) === undefined);
217
+ if (unknown.length > 0)
218
+ await this.holdersIn(root, unknown);
219
+ for (const ancestor of ancestors) {
220
+ const holder = this.heldAt(ancestor);
221
+ if (!holder || holder.root !== root)
222
+ throw misplaced(root, ancestor);
223
+ next.set(ancestor, holder);
224
+ }
225
+ }
226
+ level = next;
227
+ }
228
+ const chains = new Map();
229
+ for (const uuid of placed.keys()) {
230
+ const chain = [uuid];
231
+ for (let at = parentOf.get(uuid); at !== null; at = parentOf.get(at)) {
232
+ if (chain.length > parentOf.size)
233
+ throw new Error(`cloud graph entity ${uuid} has a parent cycle`);
234
+ chain.push(at);
235
+ }
236
+ chains.set(uuid, chain);
237
+ }
238
+ return chains;
239
+ }
240
+ /** H's row of each uuid the graph holds: its parent from the parent index,
241
+ * then the one record page its key falls in, each page read once. */
242
+ async records(uuids) {
243
+ const found = new Map();
244
+ const asked = new Map();
245
+ for (const [uuid, { root, parent }] of await this.holders(uuids)) {
246
+ const state = this.roots.get(root);
247
+ if (state.records)
248
+ found.set(uuid, this.byUuid(state).get(uuid));
249
+ else if (parent === null)
250
+ found.set(uuid, await this.rootRecord(root));
251
+ else {
252
+ const key = [parent, uuid];
253
+ const index = pageHolding(state.refs.pages, key);
254
+ if (index < 0)
255
+ throw misplaced(root, uuid);
256
+ grouped(grouped(asked, root, () => new Map()), index, () => []).push(key);
257
+ }
258
+ }
259
+ for (const [root, pages] of asked) {
260
+ const refs = this.roots.get(root).refs.pages;
261
+ const indexes = [...pages.keys()].sort((left, right) => left - right);
262
+ await this.scanPages(indexes.map((index) => refs[index]), (rows, position) => {
263
+ for (const key of pages.get(indexes[position])) {
264
+ const at = search(rows, key, (row, target) => compareGraphKeys(rawKey(row), target));
265
+ const record = at < 0 ? null : assertedRecord(rows[at]);
266
+ if (!record || record.uuid !== key[1] || record.parentUUID !== key[0])
267
+ throw misplaced(root, key[1]);
268
+ found.set(key[1], record);
269
+ }
270
+ });
271
+ }
272
+ return found;
273
+ }
274
+ /** H's rows directly beneath each parent the graph holds, any status, in
275
+ * uuid order: read from the record pages that can hold them, each once.
276
+ * Where H holds each row is then known. */
277
+ async children(parents) {
278
+ const found = new Map();
279
+ const asked = new Map();
280
+ for (const [parent, { root }] of await this.holders(parents)) {
281
+ const state = this.roots.get(root);
282
+ if (state.records)
283
+ found.set(parent, this.byParent(state).get(parent) ?? []);
284
+ else
285
+ grouped(asked, root, () => new Set()).add(parent);
286
+ }
287
+ for (const [root, named] of asked) {
288
+ const refs = this.roots.get(root).refs.pages;
289
+ const indexes = new Set();
290
+ for (const parent of named) {
291
+ const [from, to] = pagesHoldingChildren(refs, parent);
292
+ for (let index = from; index <= to; index += 1)
293
+ indexes.add(index);
294
+ }
295
+ const held = new Map([...named].map((parent) => [parent, []]));
296
+ const sorted = [...indexes].sort((left, right) => left - right);
297
+ await this.scanPages(sorted.map((index) => refs[index]), (rows) => {
298
+ for (const row of rows) {
299
+ const parent = rawKey(row)[0];
300
+ if (parent !== null)
301
+ held.get(parent)?.push(row);
302
+ }
303
+ });
304
+ for (const [parent, rows] of held) {
305
+ const records = snapshotFromRecords(rows).records;
306
+ // A row read says where H holds it, so a walk beneath it reads no
307
+ // parent index.
308
+ for (const record of records)
309
+ this.placed.set(record.uuid, { root, parent });
310
+ found.set(parent, records);
311
+ }
312
+ }
313
+ return found;
314
+ }
315
+ /** Which root holds each uuid the graph holds. */
316
+ async rootsOf(uuids) {
317
+ return new Map([...await this.holders(uuids)].map(([uuid, { root }]) => [uuid, root]));
318
+ }
319
+ /** The checksum the authority holds for this graph right now. */
320
+ checksum() {
321
+ if (this.head !== null)
322
+ return graphHeadChecksum(this.head, this.sha256Hex);
323
+ return this.sha256Hex(stableJson(this.recordsShapedSnapshot()));
324
+ }
325
+ /** The records shape of a headless graph — a first publication's own small
326
+ * snapshot. A committed head never flattens: readers are root-scoped. */
327
+ recordsShapedSnapshot() {
328
+ if (this.head !== null) {
329
+ throw new Error('a committed graph head is never flattened to records');
330
+ }
331
+ const records = [];
332
+ for (const state of this.roots.values())
333
+ for (const record of state.records ?? [])
334
+ records.push(record);
335
+ return {
336
+ schemaVersion: ENTITY_CLOUD_SCHEMA_VERSION,
337
+ kind: 'amalgm-entity-registry',
338
+ records: snapshotFromRecords(records).records,
339
+ };
340
+ }
341
+ byUuid(state) {
342
+ return state.byUuid ??= new Map(state.records.map((record) => [record.uuid, record]));
343
+ }
344
+ byParent(state) {
345
+ if (state.byParent)
346
+ return state.byParent;
347
+ const byParent = new Map();
348
+ for (const record of state.records) {
349
+ if (record.parentUUID === null)
350
+ continue;
351
+ const siblings = byParent.get(record.parentUUID);
352
+ if (siblings)
353
+ siblings.push(record);
354
+ else
355
+ byParent.set(record.parentUUID, [record]);
356
+ }
357
+ return state.byParent = byParent;
358
+ }
359
+ /** Where H holds each uuid: its root and its parent. A uuid naming a root
360
+ * is answered directly, one already placed from memory, and a materialized
361
+ * root from its records; every other root reads each parent-index page that
362
+ * could name a uuid still unplaced once. */
363
+ async holders(uuids) {
364
+ const found = new Map();
365
+ let unplaced = [];
366
+ for (const uuid of new Set(uuids)) {
367
+ const holder = this.heldAt(uuid);
368
+ if (holder)
369
+ found.set(uuid, holder);
370
+ else if (holder === undefined)
371
+ unplaced.push(uuid);
372
+ }
373
+ const materializedFirst = [...this.roots].sort(([, left], [, right]) => Number(!left.records) - Number(!right.records));
374
+ for (const [root] of materializedFirst) {
375
+ if (unplaced.length === 0)
376
+ break;
377
+ for (const [uuid, holder] of await this.holdersIn(root, unplaced))
378
+ found.set(uuid, holder);
379
+ unplaced = unplaced.filter((uuid) => !found.has(uuid));
380
+ }
381
+ for (const uuid of unplaced)
382
+ this.placed.set(uuid, null);
383
+ return found;
384
+ }
385
+ /** Where H is already known to hold `uuid`: its holder, null for nowhere,
386
+ * or undefined while unasked. */
387
+ heldAt(uuid) {
388
+ return this.roots.has(uuid) ? { root: uuid, parent: null } : this.placed.get(uuid);
389
+ }
390
+ /** The holders one root has for `uuids`, remembered. */
391
+ async holdersIn(root, uuids) {
392
+ const found = new Map();
393
+ for (const [uuid, parent] of await this.parentsIn(this.roots.get(root), uuids)) {
394
+ const holder = { root, parent };
395
+ this.placed.set(uuid, holder);
396
+ found.set(uuid, holder);
397
+ }
398
+ return found;
399
+ }
400
+ /** The parent one root names for each uuid it holds — null for the root
401
+ * itself. */
402
+ async parentsIn(state, uuids) {
403
+ const found = new Map();
404
+ if (state.records) {
405
+ const held = this.byUuid(state);
406
+ for (const uuid of uuids) {
407
+ const row = held.get(uuid);
408
+ if (row)
409
+ found.set(uuid, row.parentUUID);
410
+ }
411
+ return found;
412
+ }
413
+ const asked = new Map();
414
+ for (const uuid of uuids) {
415
+ const index = parentPageHolding(state.refs.parents, uuid);
416
+ if (index >= 0)
417
+ grouped(asked, index, () => []).push(uuid);
418
+ }
419
+ const indexes = [...asked.keys()].sort((left, right) => left - right);
420
+ await this.scanPages(indexes.map((index) => state.refs.parents[index]), (pairs, position) => {
421
+ for (const uuid of asked.get(indexes[position])) {
422
+ const at = search(pairs, uuid, byPairUuid);
423
+ if (at >= 0)
424
+ found.set(uuid, parentPair(pairs[at])[1]);
425
+ }
426
+ });
427
+ return found;
428
+ }
429
+ /** One page proven against its reference and parsed. */
430
+ async rows(ref) {
431
+ return parseGraphPage(ref, await this.readPage(ref), this.sha256Hex);
432
+ }
433
+ /** Prove, parse, and visit pages in order, fetching a bounded window ahead
434
+ * so the network pipeline stays full while transient memory stays at a few
435
+ * page budgets. */
436
+ async scanPages(refs, visit) {
437
+ const pending = [];
438
+ let ahead = 0;
439
+ try {
440
+ for (let index = 0; index < refs.length; index += 1) {
441
+ while (ahead < refs.length && pending.length < PAGE_PREFETCH) {
442
+ pending.push(this.readPage(refs[ahead]));
443
+ ahead += 1;
444
+ }
445
+ visit(parseGraphPage(refs[index], await pending.shift(), this.sha256Hex), index);
446
+ }
447
+ }
448
+ finally {
449
+ // A fault must strand no in-flight read unobserved.
450
+ await Promise.allSettled(pending);
451
+ }
452
+ }
453
+ }
454
+ /** Hold the authority's stored snapshot bytes as this machine's graph. A head
455
+ * is held lazily — no page is read here — and a records-shaped snapshot is
456
+ * grouped in place. */
457
+ export function registryGraphFromSnapshot(bytes, readPage, sha256Hex, finish = finishNow) {
458
+ if (!snapshotIsGraphHead(bytes)) {
459
+ return RegistryGraph.fromRecords(parseSnapshot(bytes).records, sha256Hex);
460
+ }
461
+ return RegistryGraph.fromHead(parseGraphHead(bytes), readPage, sha256Hex, finish);
462
+ }
463
+ /** Which shape the authority's snapshot bytes carry. Both shapes declare their
464
+ * kind ahead of their payload, and no record carries a `kind` of its own, so
465
+ * the first declaration in the opening bytes is the snapshot's — the shape is
466
+ * decided from a few bytes rather than by parsing a whole graph. */
467
+ function snapshotIsGraphHead(bytes) {
468
+ const declared = /"kind":"([^"]*)"/
469
+ .exec(utf8Decode(bytes.subarray(0, SNAPSHOT_KIND_PREFIX_BYTES)));
470
+ if (!declared)
471
+ throw new Error('cloud registry snapshot declares no kind');
472
+ return declared[1] === GRAPH_HEAD_KIND;
473
+ }
474
+ /** Enough opening bytes to hold the kind declaration of either shape, whatever
475
+ * small members precede it. The authority reads the same prefix to decide the
476
+ * same question. */
477
+ const SNAPSHOT_KIND_PREFIX_BYTES = 128;
478
+ //# sourceMappingURL=registry-graph.js.map