{"draft":null,"from":161,"to":162,"files":[{"path":"worker/project/project.ts","added":151,"removed":9,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -228,6 +228,19 @@\n \tready: boolean;\n }\n \n+/** A decoded entry is supplied by a host that binds its bytes to canonical authority. */\n+export type WriterAdoptionFrame = { seq: number; json: string } | { seq: number; bytes: number; entry: Entry };\n+function adoptionFrameBytes(frame: WriterAdoptionFrame): number {\n+\treturn \"json\" in frame\n+\t\t? typeof frame.json === \"string\"\n+\t\t\t? new TextEncoder().encode(frame.json).byteLength\n+\t\t\t: NaN\n+\t\t: frame.bytes;\n+}\n+function adoptionFrameEntry(frame: WriterAdoptionFrame): Entry {\n+\treturn \"json\" in frame ? (JSON.parse(frame.json) as Entry) : frame.entry;\n+}\n+\n export class Project {\n \treadonly #store: ProjectStore;\n \treadonly #writerStorage?: GraphStateStorage;\n@@ -480,6 +493,46 @@\n \t\treturn new Project(store, { ...options, writerStorage: storage }, WRITER_REOPEN);\n \t}\n \n+\t/** Open a host-verified historical prefix. Its state must be immutable and\n+\t * scoped to canonical authority; adoption readiness describes the live writer,\n+\t * not whether this individual prefix has a complete checkpoint. */\n+\tstatic openHistorical(\n+\t\tstorage: GraphStateStorage,\n+\t\tlogLength: number,\n+\t\tscratch: GraphStateStorage,\n+\t\tassertScope: () => void = () => {\n+\t\t\tstorage.read(\"project:meta\", \"checkpoint\");\n+\t\t},\n+\t\treadViewProvider?: ProjectOptions[\"readViewProvider\"],\n+\t): Project {\n+\t\tconst refuse = (): never => {\n+\t\t\tthrow new Error(\"historical project is read-only\");\n+\t\t};\n+\t\tconst selected = (collection: string) => {\n+\t\t\t// Revalidate the historical scope even when accessing derived scratch.\n+\t\t\tassertScope();\n+\t\t\treturn collection.startsWith(\"projection-scratch:\") ? scratch : storage;\n+\t\t};\n+\t\treturn Project.#openWriter(\n+\t\t\t{ append: refuse },\n+\t\t\t{\n+\t\t\t\tread: (collection, key) => selected(collection).read(collection, key),\n+\t\t\t\tentries: (collection) => selected(collection).entries(collection),\n+\t\t\t\twrite: (collection, key, value) => {\n+\t\t\t\t\tif (selected(collection) !== scratch) refuse();\n+\t\t\t\t\tscratch.write(collection, key, value);\n+\t\t\t\t},\n+\t\t\t\tremove: (collection, key) => {\n+\t\t\t\t\tif (selected(collection) !== scratch) refuse();\n+\t\t\t\t\tscratch.remove(collection, key);\n+\t\t\t\t},\n+\t\t\t\ttransaction: (body) => storage.transaction(() => scratch.transaction(body)),\n+\t\t\t},\n+\t\t\tlogLength,\n+\t\t\t{ readViewProvider },\n+\t\t);\n+\t}\n+\n \t/**\n \t * Derive one bounded page from the host's canonical frames. The host reads\n \t * those frames from its own log and revalidates the source inside this same\n@@ -490,10 +543,15 @@\n \t\tinput: {\n \t\t\tsource: WriterSource;\n \t\t\tfrom: number;\n-\t\t\tframes: Iterable<{ seq: number; json: string }>;\n+\t\t\tframes: Iterable<WriterAdoptionFrame>;\n \t\t\tmaxEntries: number;\n \t\t\tmaxBytes: number;\n+\t\t\t/** The host supplies disk-backed decoded entries and verified byte counts. */\n+\t\t\tdecodedFrames?: boolean;\n \t\t\tcurrentSource(): WriterSource;\n+\t\t\t/** Select the host's derived historical revision before applying a frame.\n+\t\t\t * Each selected revision receives a complete Project checkpoint. */\n+\t\t\tbeginRevision?(logLength: number): void;\n \t\t},\n \t\toptions: Omit<ProjectOptions, \"writerStorage\"> = {},\n \t): { next: number; ready: boolean; entries: number; bytes: number } {\n@@ -509,7 +567,7 @@\n \t\t\tinput.maxEntries > 256 ||\n \t\t\t!Number.isSafeInteger(input.maxBytes) ||\n \t\t\tinput.maxBytes < 1 ||\n-\t\t\tinput.maxBytes > 8_000_000\n+\t\t\t(input.maxBytes > 8_000_000 && input.decodedFrames !== true)\n \t\t)\n \t\t\tthrow new Error(\"invalid writer adoption bounds or source\");\n \t\treturn storage.transaction(() => {\n@@ -533,19 +591,24 @@\n \t\t\tlet count = 0;\n \t\t\tlet bytes = 0;\n \t\t\tfor (const frame of input.frames) {\n+\t\t\t\tif (input.decodedFrames && \"json\" in frame) throw new Error(\"decoded adoption requires decoded frames\");\n+\t\t\t\tconst size = adoptionFrameBytes(frame);\n \t\t\t\tif (\n \t\t\t\t\tcount >= input.maxEntries ||\n \t\t\t\t\tframe.seq !== input.from + count ||\n \t\t\t\t\tframe.seq >= input.source.logLength ||\n-\t\t\t\t\ttypeof frame.json !== \"string\" ||\n-\t\t\t\t\tframe.json.length > input.maxBytes - bytes\n+\t\t\t\t\t!Number.isSafeInteger(size) ||\n+\t\t\t\t\tsize < 1 ||\n+\t\t\t\t\tsize > input.maxBytes - bytes\n \t\t\t\t)\n \t\t\t\t\tthrow new Error(\"writer adoption page exceeds bounds or has a noncontiguous frame\");\n-\t\t\t\tbytes += new TextEncoder().encode(frame.json).byteLength;\n+\t\t\t\tbytes += size;\n \t\t\t\tif (bytes > input.maxBytes) throw new Error(\"writer adoption page exceeds byte bound\");\n-\t\t\t\tproject.#apply(JSON.parse(frame.json) as Entry);\n+\t\t\t\tinput.beginRevision?.(frame.seq + 1);\n+\t\t\t\tproject.#apply(adoptionFrameEntry(frame));\n \t\t\t\tproject.#stored++;\n \t\t\t\tproject.#persistMutable();\n+\t\t\t\tif (input.beginRevision) project.#writeCheckpoint();\n \t\t\t\tcount++;\n \t\t\t}\n \t\t\tif (count === 0) throw new Error(\"writer adoption page must advance\");\n@@ -563,9 +626,81 @@\n \t}\n \n \t/**\n-\t * Apply more stored entries to a copy loaded for reading (#41, replay):\n-\t * scrubbing forward applies only what's new. Never for the live project.\n+\t * Extend derived historical indexes from an already checkpointed prefix.\n+\t * The host supplies only new canonical frames inside its commit transaction.\n \t */\n+\tstatic extendHistoricalPage(\n+\t\tstorage: GraphStateStorage,\n+\t\tinput: {\n+\t\t\tfrom: number;\n+\t\t\tframes: Iterable<WriterAdoptionFrame>;\n+\t\t\tmaxEntries: number;\n+\t\t\tmaxBytes: number;\n+\t\t\t/** The host supplies disk-backed decoded entries and verified byte counts. */\n+\t\t\tdecodedFrames?: boolean;\n+\t\t\tbeginRevision(logLength: number): void;\n+\t\t\t/** Verify both the saved prefix and the new frames against canonical authority. */\n+\t\t\tvalidateAuthority(): void;\n+\t\t},\n+\t): { next: number; entries: number; bytes: number } {\n+\t\tif (\n+\t\t\t!Number.isSafeInteger(input.from) ||\n+\t\t\tinput.from < 1 ||\n+\t\t\t!Number.isSafeInteger(input.maxEntries) ||\n+\t\t\tinput.maxEntries < 1 ||\n+\t\t\tinput.maxEntries > 256 ||\n+\t\t\t!Number.isSafeInteger(input.maxBytes) ||\n+\t\t\tinput.maxBytes < 1 ||\n+\t\t\t(input.maxBytes > 8_000_000 && input.decodedFrames !== true)\n+\t\t)\n+\t\t\tthrow new Error(\"invalid historical extension bounds\");\n+\t\tconst validate = () => {\n+\t\t\tconst result: unknown = input.validateAuthority();\n+\t\t\tif (result && (typeof result === \"object\" || typeof result === \"function\") && \"then\" in result)\n+\t\t\t\tthrow new Error(\"historical authority must be synchronous\");\n+\t\t};\n+\t\treturn storage.transaction(() => {\n+\t\t\tvalidate();\n+\t\t\tconst project = Project.#openWriter(\n+\t\t\t\t{\n+\t\t\t\t\tappend: () => {\n+\t\t\t\t\t\tthrow new Error(\"history derivation cannot append canonical entries\");\n+\t\t\t\t\t},\n+\t\t\t\t},\n+\t\t\t\tstorage,\n+\t\t\t\tinput.from,\n+\t\t\t\t{},\n+\t\t\t);\n+\t\t\tproject.#loading = true;\n+\t\t\tlet entries = 0,\n+\t\t\t\tbytes = 0;\n+\t\t\tfor (const frame of input.frames) {\n+\t\t\t\tif (input.decodedFrames && \"json\" in frame) throw new Error(\"decoded adoption requires decoded frames\");\n+\t\t\t\tconst size = adoptionFrameBytes(frame);\n+\t\t\t\tif (\n+\t\t\t\t\tentries >= input.maxEntries ||\n+\t\t\t\t\tframe.seq !== input.from + entries ||\n+\t\t\t\t\t!Number.isSafeInteger(size) ||\n+\t\t\t\t\tsize < 1 ||\n+\t\t\t\t\tsize > input.maxBytes - bytes\n+\t\t\t\t)\n+\t\t\t\t\tthrow new Error(\"historical extension exceeds bounds or is not contiguous\");\n+\t\t\t\tbytes += size;\n+\t\t\t\tif (bytes > input.maxBytes) throw new Error(\"historical extension exceeds byte bound\");\n+\t\t\t\tinput.beginRevision(frame.seq + 1);\n+\t\t\t\tproject.#apply(adoptionFrameEntry(frame));\n+\t\t\t\tproject.#stored++;\n+\t\t\t\tproject.#persistMutable();\n+\t\t\t\tproject.#writeCheckpoint();\n+\t\t\t\tentries++;\n+\t\t\t}\n+\t\t\tif (!entries) throw new Error(\"historical extension must advance\");\n+\t\t\tvalidate();\n+\t\t\treturn { next: input.from + entries, entries, bytes };\n+\t\t});\n+\t}\n+\n+\t/** Apply more entries to a replay copy. Never for the live project. */\n \tcatchUp(entries: Iterable<Entry>): void {\n \t\tthis.#loading = true;\n \t\ttry {\n@@ -896,6 +1031,7 @@\n \n \t/** The project's reviewers per area (#44): avcs.json's `\"review\": [{\"paths\": [...], \"reviewer\": \"...\"}]` in shared. */\n \t#reviewRules(): ReviewRule[] {\n+\t\tif ([\"abso.json\", \"avcs.json\"].every((path) => this.#graph.startCodeOf(path) === undefined)) return [];\n \t\treturn reviewRulesIn(projectFileIn(this.#sharedView())?.text);\n \t}\n \n@@ -1325,7 +1461,7 @@\n \t\tspace: Space,\n \t\tchanges: AtomId[],\n \t\tpick: boolean,\n-\t): { collisions: Collision[]; newer: Set<AtomId>; union: Set<AtomId> } {\n+\t): { collisions: Collision[]; newer: ReadonlySet<AtomId>; union: ReadonlySet<AtomId> } {\n \t\treturn hostedLandCollisions(this.#landHost, space, changes, pick);\n \t}\n \n@@ -2043,6 +2179,12 @@\n \t#landers?: LandHost;\n \tget #landHost(): LandHost {\n \t\tthis.#landers ??= {\n+\t\t\tviewAt: (space, version, changes) =>\n+\t\t\t\tchanges?.length\n+\t\t\t\t\t? this.#viewKeyed(`a ${space.name}@${version}+${changes.join(\",\")}`, () =>\n+\t\t\t\t\t\t\tunionOverlay(this.#at(space, version), changes),\n+\t\t\t\t\t\t)\n+\t\t\t\t\t: this.#viewAt(space, version),\n \t\t\tagents: this.#agents,\n \t\t\tgraph: this.#graph,\n \t\t\tspaces: this.#spaces,"}],"unmatched":[]}