Read graph identities and metadata without decoding atom payloads
of shared·codex-capacity-chair··Not checked
Version 147 · Files at version 148
2 files, 42 lines added, 7 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -495,7 +495,7 @@
496496 /** lamport(a) = 1 + max lamport of its deps; 0 for an atom with no deps (such as a0). */ 497497 lamport(id: AtomId): number { 498− this.atom(id);
498+ if (!this.has(id)) throw new EngineError("unknown-atom", `no atom ${id}`); 499499 return this.#lamport[this.#seq(id)]!; @@ -510,7 +510,7 @@
510510 const seq = this.#lineAtom.at(n); 513− atom: this.#atoms[seq]!.id,
513+ atom: this.idOfSeq(seq), 514514 k: n - this.#lineBase[seq]!, 515515 file: this.#paths[this.#lineFile.at(n)]!, 516516 parent: this.#nodeName(this.#lineParent.at(n)), @@ -527,7 +527,7 @@
527527 /** The atom that wrote a line, or undefined: line(id).atom without making the record. */ 528528 lineAtom(id: LineId): AtomId | undefined { 529529 const n = this.#lineN(id); 530− return n < 0 ? undefined : this.#atoms[this.#lineAtom.at(n)]!.id;
530+ return n < 0 ? undefined : this.idOfSeq(this.#lineAtom.at(n)); 533533 /** The file a line was written in, or undefined: line(id).file without making the record. */ @@ -586,7 +586,9 @@
587587 /** The atom id with this seq, as the graph keeps it. */ 588588 idOfSeq(seq: number): AtomId { 589− return this.#atoms[seq]!.id;
589+ if (!Number.isSafeInteger(seq) || seq < 0 || seq >= this.#atoms.length) 590+ throw new EngineError("unknown-atom", `no atom at sequence ${seq}`); 592594 lamportOfSeq(seq: number): number { @@ -741,7 +743,7 @@
741743 /** A line's id from its n. */ 742744 #lineName(n: number): LineId { 743745 const seq = this.#lineAtom.at(n); 744− return `${this.#atoms[seq]!.id}.${n - this.#lineBase[seq]!}`;
746+ return `${this.idOfSeq(seq)}.${n - this.#lineBase[seq]!}`; 747749 /** A node's code: a line's n, or -1 - a path's index for a start node; undefined when unknown. */ @@ -781,7 +783,7 @@
781783 deletersOf(line: LineId): readonly AtomId[] { 782784 const n = this.#lineN(line); 783785 if (n < 0) return EMPTY; 784− return [...this.eachDeleterSeqOf(n)].map((seq) => this.#atoms[seq]!.id);
786+ return [...this.eachDeleterSeqOf(n)].map((seq) => this.idOfSeq(seq)); 787789 writersOf(key: Key): readonly AtomId[] { @@ -837,7 +839,7 @@
837839 * start node, that file): a set's moves can have taken them elsewhere. 839841 filesOf(id: AtomId): readonly string[] { 840− this.atom(id);
842+ if (!this.has(id)) throw new EngineError("unknown-atom", `no atom ${id}`); 841843 return this.#atomFiles[this.#seq(id)]!; --- /dev/null
+++ b/src/engine/test/graph-metadata-indexed.test.ts
@@ -0,0 +1,33 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { Graph } from "../graph.ts"; 4+import { Repo } from "./helpers.ts"; 5+import { SqlState } from "./storage-helper.ts"; 7+test("indexed identities and metadata do not decode atom payloads", () => { 8+ const db = new SqlState(); 10+ const repo = Repo.init({ "a.txt": "one\ntwo\n" }); 11+ const graph = new Graph({ storage: db }); 12+ graph.add(repo.graph.atom("a0")); 13+ const read = db.read.bind(db); 14+ db.read = (collection, key) => { 15+ if (collection === "atoms") throw Error("atom payload read forbidden"); 16+ return read(collection, key); 18+ assert.equal(graph.idOfSeq(0), "a0"); 19+ assert.equal(graph.lamport("a0"), 0); 20+ assert.deepEqual(graph.filesOf("a0"), ["a.txt"]); 21+ assert.equal(graph.line("a0.0")?.atom, "a0"); 22+ assert.equal(graph.lineIdOf(0), "a0.0"); 23+ for (const seq of [-1, 1, 0.5, NaN, Infinity]) assert.throws(() => graph.idOfSeq(seq), /no atom/); 24+ for (const id of ["a1", "a00", "a-1", "invalid"]) { 25+ assert.throws(() => graph.lamport(id), /no atom/); 26+ assert.throws(() => graph.filesOf(id), /no atom/); 28+ assert.equal(graph.line("a1.0"), undefined); 29+ assert.throws(() => graph.atom("a0"), /payload read forbidden/);