Lines 4–13 at 162 (were 4–11 at 161)
44// R2 copies, edge cache, probes or disk-first writer. 66import { DurableObject } from "cloudflare:workers"; 7−import { type Answer, answer, type Readers } from "../../worker/project/http.ts";
7+import { createHash } from "node:crypto"; 8+import { type Answer, answer, type Readers, readerAnswer } from "../../worker/project/http.ts"; 89import type { ProjectCalls, ProjectHistory } from "../../worker/project/project-api.ts"; 10+import { ProjectError } from "../../worker/project/project-error.ts"; 911import { BLOB_MAX, BlobStore } from "./blobs.ts"; 1012import { DiskFirstRuntime } from "./disk-first-runtime.ts"; 1113import { EngineProject, SqliteLog } from "./engine-project.ts"; Lines 165–171 at 162 (were 163–168 at 161)
164166 how: string | undefined, 168+ request?: { id: string; principal: string }, 166169 ): Promise<Answer> { 167170 const runtime = this.diskRuntime()!; 168171 const validate = () => { Lines 173–183 at 162 (were 170–176 at 161)
170173 throw Object.assign(Error("project authorization changed during call setup"), { code: "authority-changed" }); 172175 const progress = await this.prepareDisk(validate); 173− const known = new Set((await this.ctx.storage.get<string[]>("readers")) ?? []);
176+ const legacyReaders = (await this.ctx.storage.get<string[]>("readers")) ?? []; 177+ const storedReaders = progress?.ready 178+ ? (new SqliteWriterState(this.ctx.storage, "server-disk-v1").read("runtime", "readers") as string[] | undefined) 180+ const known = new Set(storedReaders ?? legacyReaders); 175182 let readersChanged = false; 176183 const readers: Readers = { Lines 194–237 at 162 (were 187–211 at 161)
187194 return { status: 404, body: { error: "no such project; run init first" } }; 188195 if (!progress?.ready && !(progress?.phase === "empty" && op === "init")) 189196 return { status: 503, body: { error: "project indexes are being prepared", retryAfter: 1, progress } }; 197+ if (agent && op !== "reader") { 198+ const refused = readerAnswer({ status: () => undefined }, op, agent, body, readers); 199+ if (refused) return refused; 190201 const before = new SqliteLog(this.ctx.storage).seq(); 191202 const action = (calls: EngineProject) => { 192203 const result = answer(calls, op, agent, body, readers, how); 193204 if (result.status >= 500) throw Error(`project operation failed: ${JSON.stringify(result.body)}`); 206+ new SqliteWriterState(this.ctx.storage, "server-disk-v1").write("runtime", "readers", readers.list()); 210+ const receipt = request 212+ id: `http:${createHash("sha256").update(request.principal).digest("hex")}:${request.id}`, 215+ principal: request.principal, 216+ agent: agent ?? null, 198224 progress.phase === "empty" 199− ? runtime.initialize(action, validate)
225+ ? runtime.initialize(action, validate, receipt) 200226 : READ_OPS.has(op) || op.startsWith("change/") 201227 ? runtime.read(action, validate) 202− : runtime.write(action, validate);
228+ : runtime.write(action, validate, receipt); 203230 if (readersChanged) { 204231 this.authorityRevision++; 205− await this.ctx.storage.put("readers", readers.list());
207233 const log = new SqliteLog(this.ctx.storage); 208− if (log.seq() !== before) {
234+ if (log.seq() !== before || (request && !READ_OPS.has(op))) { 210236 await this.ctx.storage.put("name", name); 211237 await this.soon(Date.now() + 100); Lines 371–408 at 162 (were 345–364 at 161)
348375 ): Promise<Answer> { 349376 const revision = this.authorityRevision; 377+ let principal = "shared-token"; 379+ requestId !== undefined && 380+ !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(requestId) 382+ return { status: 400, body: { error: "Idempotency-Key must be a UUID" } }; 351384 const decided = await this.checkAgentKey(key.presented, agent, key.address); 352385 if (!decided.ok) return { status: decided.status, body: { error: decided.error } }; 353386 const allowed = keyAllows(decided.role, { write: key.write, agent, route: key.route }); 354387 if (!allowed.ok) return { status: allowed.status, body: { error: allowed.error } }; 389+ principal = `key:${decided.id}`; 357391 if (op === "feed" && !inFeed) return this.feed(name, body); 358392 if (this.diskRuntime()) { 360− return await this.diskCall(name, op, agent, body, how, revision);
394+ return await this.diskCall( 401+ requestId ? { id: requestId, principal } : undefined, 404+ if ((error as { code?: string })?.code === "request-reused") 405+ return { status: 409, body: { error: "Idempotency-Key was reused for a different request" } }; 362406 if ((error as { code?: string })?.code === "authority-changed") 363407 return { status: 409, body: { error: "project authorization changed; retry the request", retryable: true } }; Lines 543–569 at 162 (were 499–512 at 161)
499543 if (found) await this.ctx.storage.put("git:head", found.head); 546+ // Unexpected failures leave the cursor unacknowledged for retry. Engine 547+ // refusals are recorded and acknowledged so a later fixing commit can advance. 548+ const ingest = (p: EngineProject) => 549+ p.ingest({ author: found.author, base: found.base, commits: found.commits }, Date.now()); 550+ const runtime = this.diskRuntime(); 502551 let result: unknown; 504− result = this.useProject(
505− (p) => p.ingest({ author: found.author, base: found.base, commits: found.commits }, Date.now()),
506− true,
507− );
508− } catch (e) {
509− result = { error: e instanceof Error ? e.message : String(e) };
554+ ? runtime.write(ingest, () => {}, { 555+ id: `git:${found.head}`, 560+ commits: found.commits.map((commit) => commit.sha), 563+ : this.useProject(ingest, true); 565+ if (!(error instanceof ProjectError)) throw error; 566+ result = { error: error.message }; 512569 at: new Date().toISOString(),