Read Artifacts snapshots in version-pinned batches; cleared in review ask 45
of shared·codex-capacity-chair··Not checked
Version 176 · Files at version 177
5 files, 175 lines added, 1 removed.
--- a/server/src/engine-project.ts
+++ b/server/src/engine-project.ts
@@ -9,6 +9,7 @@
99import { type Entry, type ProjectStore as LogStore, MemoryStore, Project } from "../../worker/project/project.ts"; 1010import type { EditBody, ProjectCalls, ProjectHistory } from "../../worker/project/project-api.ts"; 1111import { where } from "../../worker/project/where.ts"; 12+import type { PushSnapshotMetadata } from "./push-snapshot.ts"; 1314/** The parts of a Durable Object's storage the log and blob store use, declared structurally so tests can stand in. */ 1415export interface SqliteStorage { @@ -807,6 +808,38 @@
807808 filesAt(version: number) { 808809 return this.project.viewAt(version).files(); 811+ fileTextAt(version: number, path: string): string { 812+ return this.project.viewAt(version).text(path); 814+ /** One scoped batch: at most 64 files and 64 Ki characters plus one whole file. */ 815+ fileTextsAt(version: number, paths: readonly string[]): Record<string, string> { 816+ const view = this.project.viewAt(version); 817+ const files: Record<string, string> = Object.create(null); 819+ for (const path of paths.slice(0, 64)) { 820+ const text = view.attrs(path).link !== null ? "" : view.text(path); 822+ characters += text.length; 823+ if (characters >= 65536) break; 827+ /** Capture metadata now; each text read reopens the captured version in a fresh host scope. */ 828+ sharedPushMetadata(): PushSnapshotMetadata { 829+ const version = this.project.version; 830+ const view = this.project.viewAt(version); 831+ const paths: string[] = []; 832+ const modes: PushSnapshotMetadata["modes"] = Object.create(null); 833+ const blobs: Record<string, string> = Object.create(null); 834+ for (const path of view.paths()) { 835+ const attrs = view.attrs(path); 836+ modes[path] = { exec: attrs.exec, link: attrs.link }; 837+ if (attrs.blob !== null) blobs[path] = attrs.blob; 838+ else paths.push(path); 840+ const by = version > 0 ? this.project.published(version) : undefined; 841+ return { version, paths, modes, blobs, ...(by ? { last: { agent: by.agent, message: by.message } } : {}) }; 810843 blobsAt(version: number) { 811844 return this.project.viewAt(version).blobs(); --- a/server/src/project-object.ts
+++ b/server/src/project-object.ts
@@ -26,6 +26,7 @@
2828import { type PushResult, pushShared, repoName } from "./push.ts"; 29+import { pushSnapshot } from "./push-snapshot.ts"; 2930import { SqliteWriterState } from "./writer-host.ts"; 3132const READ_OPS = new Set([ @@ -605,7 +606,11 @@
605606 error: e instanceof Error ? e.message : String(e), 608− const shared = this.useProject((p) => p.shared());
609+ const shared = pushSnapshot( 610+ this.useProject((p) => p.sharedPushMetadata()), 611+ (version, path) => this.useProject((current) => current.fileTextAt(version, path)), 612+ (version, paths) => this.useProject((current) => current.fileTextsAt(version, paths)), 609614 const done = await this.ctx.storage.get<PushResult>("pushed"); 610615 if (done && done.version >= shared.version) return; 611616 const pushedAt = (await this.ctx.storage.get<number>("pushedAt")) ?? 0; --- /dev/null
+++ b/server/src/push-snapshot.ts
@@ -0,0 +1,35 @@
1+import type { FileModes } from "../../worker/project/project-api.ts"; 3+export interface PushSnapshotMetadata { 7+ blobs: Record<string, string>; 8+ last?: { agent: string; message: string }; 11+/** Construct outside the read scope so detaching metadata never evaluates file text. */ 12+export function pushSnapshot( 13+ metadata: PushSnapshotMetadata, 14+ readText: (version: number, path: string) => string, 15+ readBatch?: (version: number, paths: readonly string[]) => Record<string, string>, 17+ const { paths, ...shared } = metadata; 18+ const files: Record<string, string> = Object.create(null); 19+ let cache: Record<string, string> = Object.create(null); 20+ for (let index = 0; index < paths.length; index++) { 21+ const path = paths[index]!; 22+ Object.defineProperty(files, path, { 25+ if (!readBatch) return readText(shared.version, path); 26+ if (!Object.hasOwn(cache, path)) cache = readBatch(shared.version, paths.slice(index, index + 64)); 27+ if (!Object.hasOwn(cache, path)) throw Error("push batch omitted requested file"); 28+ const text = cache[path]!; 34+ return { ...shared, files }; --- /dev/null
+++ b/server/test/push-snapshot-batch.test.ts
@@ -0,0 +1,34 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { answer } from "../../worker/project/http.ts"; 4+import { EngineProject } from "../src/engine-project.ts"; 5+import { pushSnapshot } from "../src/push-snapshot.ts"; 6+import { memoryStorage } from "./helpers.ts"; 8+test("push batches cap file count and text while preserving exact lazy contents", (t) => { 9+ const storage = memoryStorage(); 10+ t.after(() => storage.db.close()); 11+ const project = EngineProject.open(storage); 12+ const files = Object.fromEntries(Array.from({ length: 130 }, (_, i) => [`f${i}`, `${i}\n`])); 13+ assert.equal(answer(project, "init", undefined, { files }).status, 200); 15+ const snapshot = pushSnapshot( 16+ project.sharedPushMetadata(), 18+ throw Error("single read forbidden"); 20+ (version, paths) => { 22+ return project.fileTextsAt(version, paths); 25+ assert.equal(batches, 0); 26+ assert.deepEqual({ ...snapshot.files }, files); 27+ assert.equal(batches, 3); 28+ const large = "x".repeat(70000) + "\n"; 29+ assert.equal(answer(project, "record", "writer", { files: { large } }).status, 200); 30+ assert.equal(answer(project, "publish", "writer", { message: "Large file" }).status, 200); 31+ const batch = project.fileTextsAt(1, ["large", "f0"]); 32+ assert.deepEqual(Object.keys(batch), ["large"]); 33+ assert.equal(batch.large, large); --- /dev/null
+++ b/server/test/push-snapshot.test.ts
@@ -0,0 +1,67 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { answer } from "../../worker/project/http.ts"; 4+import { MemoryStore, Project } from "../../worker/project/project.ts"; 5+import { DiskFirstRuntime } from "../src/disk-first-runtime.ts"; 6+import { SqliteLog } from "../src/engine-project.ts"; 7+import { pushSnapshot } from "../src/push-snapshot.ts"; 8+import { memoryStorage } from "./helpers.ts"; 10+test("push snapshot reads a pinned version through fresh disk scopes after another publish", (t) => { 11+ const storage = memoryStorage(); 12+ t.after(() => storage.db.close()); 13+ const seed = new MemoryStore(); 14+ new Project(seed).init({ "a.txt": "old\n", "b.txt": "stable\n" }, 0); 15+ new SqliteLog(storage).append(seed.entries); 16+ t.mock.method(Project, "load", () => { 17+ throw Error("full-history replay forbidden"); 19+ const runtime = () => new DiskFirstRuntime(storage, "push-snapshot", "push-snapshot"); 20+ while (!runtime().preparePage(() => {}).ready) {} 22+ const snapshot = pushSnapshot( 24+ (calls) => calls.sharedPushMetadata(), 29+ return runtime().read( 30+ (current) => current.fileTextAt(version, path), 34+ (version, paths) => { 36+ return runtime().read( 37+ (current) => current.fileTextsAt(version, paths), 42+ assert.equal(reads, 0); 43+ assert.equal(snapshot.files["a.txt"], "old\n"); 44+ for (const [op, body] of [ 46+ ["record", { files: { "a.txt": "new", "b.txt": "changed" } }], 47+ ["publish", { message: "Changed during push" }], 51+ (calls) => answer(calls, op, "alice", body), 56+ assert.equal(snapshot.files["b.txt"], "stable\n"); 57+ assert.equal(snapshot.files["a.txt"], "old\n"); 58+ assert.equal(snapshot.version, 0); 59+ assert.equal(reads, 2); 62+ (calls) => calls.shared().version,