Avoid line projection for file attribute queries
of shared·codex-capacity-chair··Not checked
Version 139 · Files at version 140
2 files, 73 lines added, 10 removed.
--- /dev/null
+++ b/src/engine/test/attribute-projection.test.ts
@@ -0,0 +1,57 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { Graph } from "../graph.ts"; 4+import { CachedStateStorage, StableOverlaySet } from "../state-source.ts"; 5+import { View } from "../view.ts"; 6+import { Repo } from "./helpers.ts"; 7+import { SqlState } from "./storage-helper.ts"; 9+test("attribute queries and explicit file existence do not traverse line history", (t) => { 10+ const repo = Repo.init({ "file.txt": "one\ntwo\n" }); 11+ const base = new Set(repo.shared); 14+ { file: "file.txt", name: "exec", value: "1" }, 15+ { file: "empty.txt", name: "exists", value: "1" }, 16+ { file: "link.txt", name: "link", value: "target" }, 19+ const first = new Set(repo.shared); 22+ { file: "file.txt", name: "exec", value: null }, 23+ { file: "empty.txt", name: "exists", value: null }, 24+ { file: "link.txt", name: "link", value: null }, 27+ const source = new SqlState(); 29+ const disk = new Graph({ storage: new CachedStateStorage(source) }); 30+ for (const id of repo.shared) disk.add(repo.graph.atom(id)); 31+ for (const graph of [repo.graph, new Graph({ storage: new CachedStateStorage(source) })]) { 32+ for (const set of [base, first, repo.shared]) { 33+ const view = new View(graph, new StableOverlaySet(set)); 34+ const before = view.weight(); 35+ const guard = t.mock.method(graph, "startCodeOf", () => { 36+ throw new Error("line traversal forbidden"); 38+ assert.equal(view.attrs("file.txt").exec, set === first); 39+ assert.equal(view.attrs("empty.txt").exists, set === first); 40+ assert.equal(view.attrs("link.txt").link, set === first ? "target" : null); 41+ assert.deepEqual(view.attrs("missing.txt"), { exists: false, exec: false, link: null, blob: null }); 43+ assert.equal(view.hasFile("empty.txt"), true); 44+ assert.equal(view.hasFile("link.txt"), true); 46+ assert.equal(view.weight(), before); 47+ guard.mock.restore(); 48+ assert.equal(view.hasFile("file.txt"), true, "text-only files retain line fallback"); 49+ assert.equal(view.hasFile("missing.txt"), false); 50+ assert.equal(view.hasFile("empty.txt"), set === first); 51+ assert.equal(view.text("file.txt"), "one\ntwo\n"); --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -420,7 +420,7 @@
420420 return this.#layoutResolved(path, complete); 423− #layoutResolved(path: string, complete: boolean, nodes?: Iterable<number>): FileState {
423+ #attributes(path: string): FileAttrs { 424424 const graph = this.graph; 425425 // For each attribute, the highest-priority setter in the set wins. 426426 const setters = new Map<string, { id: AtomId; value: string | null }>(); @@ -433,6 +433,17 @@
437+ exists: setters.get("exists")?.value === "1", 438+ exec: setters.get("exec")?.value === "1", 439+ link: setters.get("link")?.value ?? null, 440+ blob: setters.get("blob")?.value ?? null, 444+ #layoutResolved(path: string, complete: boolean, nodes?: Iterable<number>): FileState { 445+ const graph = this.graph; 446+ const attrs = this.#attributes(path); 436447 const seq: number[] = []; 437448 const vis: number[] = []; 438449 const deletedBy = new Map<number, readonly number[]>(); @@ -453,12 +464,7 @@
453464 } else vis.push(n); 454465 if (complete) seq.push(n); 456− const attrs: FileAttrs = {
457− exists: setters.get("exists")?.value === "1",
458− exec: setters.get("exec")?.value === "1",
459− link: setters.get("link")?.value ?? null,
460− blob: setters.get("blob")?.value ?? null,
461− };
462468 return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy }; @@ -496,13 +502,13 @@
497503 /** Whether the file exists in this projection (has a visible line). */ 498504 hasFile(path: string): boolean { 499− const f = this.#file(path);
500− return f.vis.length > 0 || f.attrs.exists || f.attrs.link !== null || f.attrs.blob !== null;
505+ const attrs = this.attrs(path); 506+ return attrs.exists || attrs.link !== null || attrs.blob !== null || this.#file(path).vis.length > 0; 503509 /** A file's attributes: kept while empty, executable, a link's target. */ 504510 attrs(path: string): FileAttrs { 505− return this.#file(path).attrs;
511+ return this.#files.get(path)?.attrs ?? this.#attributes(path); 508514 /** The projected text of a file ("" if it has no visible lines). */