{"draft":null,"from":161,"to":162,"files":[{"path":"server/src/project-object.ts","added":70,"removed":13,"diff":"--- a/server/src/project-object.ts\n+++ b/server/src/project-object.ts\n@@ -4,8 +4,10 @@\n // R2 copies, edge cache, probes or disk-first writer.\n \n import { DurableObject } from \"cloudflare:workers\";\n-import { type Answer, answer, type Readers } from \"../../worker/project/http.ts\";\n+import { createHash } from \"node:crypto\";\n+import { type Answer, answer, type Readers, readerAnswer } from \"../../worker/project/http.ts\";\n import type { ProjectCalls, ProjectHistory } from \"../../worker/project/project-api.ts\";\n+import { ProjectError } from \"../../worker/project/project-error.ts\";\n import { BLOB_MAX, BlobStore } from \"./blobs.ts\";\n import { DiskFirstRuntime } from \"./disk-first-runtime.ts\";\n import { EngineProject, SqliteLog } from \"./engine-project.ts\";\n@@ -163,6 +165,7 @@\n \t\tbody: unknown,\n \t\thow: string | undefined,\n \t\trevision: number,\n+\t\trequest?: { id: string; principal: string },\n \t): Promise<Answer> {\n \t\tconst runtime = this.diskRuntime()!;\n \t\tconst validate = () => {\n@@ -170,7 +173,11 @@\n \t\t\t\tthrow Object.assign(Error(\"project authorization changed during call setup\"), { code: \"authority-changed\" });\n \t\t};\n \t\tconst progress = await this.prepareDisk(validate);\n-\t\tconst known = new Set((await this.ctx.storage.get<string[]>(\"readers\")) ?? []);\n+\t\tconst legacyReaders = (await this.ctx.storage.get<string[]>(\"readers\")) ?? [];\n+\t\tconst storedReaders = progress?.ready\n+\t\t\t? (new SqliteWriterState(this.ctx.storage, \"server-disk-v1\").read(\"runtime\", \"readers\") as string[] | undefined)\n+\t\t\t: undefined;\n+\t\tconst known = new Set(storedReaders ?? legacyReaders);\n \t\tvalidate();\n \t\tlet readersChanged = false;\n \t\tconst readers: Readers = {\n@@ -187,25 +194,44 @@\n \t\t\treturn { status: 404, body: { error: \"no such project; run init first\" } };\n \t\tif (!progress?.ready && !(progress?.phase === \"empty\" && op === \"init\"))\n \t\t\treturn { status: 503, body: { error: \"project indexes are being prepared\", retryAfter: 1, progress } };\n+\t\tif (agent && op !== \"reader\") {\n+\t\t\tconst refused = readerAnswer({ status: () => undefined }, op, agent, body, readers);\n+\t\t\tif (refused) return refused;\n+\t\t}\n \t\tconst before = new SqliteLog(this.ctx.storage).seq();\n \t\tconst action = (calls: EngineProject) => {\n \t\t\tconst result = answer(calls, op, agent, body, readers, how);\n \t\t\tif (result.status >= 500) throw Error(`project operation failed: ${JSON.stringify(result.body)}`);\n+\t\t\tif (readersChanged)\n+\t\t\t\tnew SqliteWriterState(this.ctx.storage, \"server-disk-v1\").write(\"runtime\", \"readers\", readers.list());\n \t\t\treturn result;\n \t\t};\n \n+\t\tconst receipt = request\n+\t\t\t? {\n+\t\t\t\t\tid: `http:${createHash(\"sha256\").update(request.principal).digest(\"hex\")}:${request.id}`,\n+\t\t\t\t\tidentity: {\n+\t\t\t\t\t\tproject: name,\n+\t\t\t\t\t\tprincipal: request.principal,\n+\t\t\t\t\t\tagent: agent ?? null,\n+\t\t\t\t\t\toperation: op,\n+\t\t\t\t\t\tbody: body ?? null,\n+\t\t\t\t\t\thow: how ?? null,\n+\t\t\t\t\t},\n+\t\t\t\t}\n+\t\t\t: undefined;\n \t\tconst result =\n \t\t\tprogress.phase === \"empty\"\n-\t\t\t\t? runtime.initialize(action, validate)\n+\t\t\t\t? runtime.initialize(action, validate, receipt)\n \t\t\t\t: READ_OPS.has(op) || op.startsWith(\"change/\")\n \t\t\t\t\t? runtime.read(action, validate)\n-\t\t\t\t\t: runtime.write(action, validate);\n+\t\t\t\t\t: runtime.write(action, validate, receipt);\n+\n \t\tif (readersChanged) {\n \t\t\tthis.authorityRevision++;\n-\t\t\tawait this.ctx.storage.put(\"readers\", readers.list());\n \t\t}\n \t\tconst log = new SqliteLog(this.ctx.storage);\n-\t\tif (log.seq() !== before) {\n+\t\tif (log.seq() !== before || (request && !READ_OPS.has(op))) {\n \t\t\tthis.wake();\n \t\t\tawait this.ctx.storage.put(\"name\", name);\n \t\t\tawait this.soon(Date.now() + 100);\n@@ -345,20 +371,38 @@\n \t\tinFeed = false,\n \t\thow?: string,\n \t\tkey?: KeyForCall,\n+\t\trequestId?: string,\n \t): Promise<Answer> {\n \t\tconst revision = this.authorityRevision;\n+\t\tlet principal = \"shared-token\";\n+\t\tif (\n+\t\t\trequestId !== undefined &&\n+\t\t\t!/^[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)\n+\t\t)\n+\t\t\treturn { status: 400, body: { error: \"Idempotency-Key must be a UUID\" } };\n \t\tif (key) {\n \t\t\tconst decided = await this.checkAgentKey(key.presented, agent, key.address);\n \t\t\tif (!decided.ok) return { status: decided.status, body: { error: decided.error } };\n \t\t\tconst allowed = keyAllows(decided.role, { write: key.write, agent, route: key.route });\n \t\t\tif (!allowed.ok) return { status: allowed.status, body: { error: allowed.error } };\n \t\t\thow = decided.as;\n+\t\t\tprincipal = `key:${decided.id}`;\n \t\t}\n \t\tif (op === \"feed\" && !inFeed) return this.feed(name, body);\n \t\tif (this.diskRuntime()) {\n \t\t\ttry {\n-\t\t\t\treturn await this.diskCall(name, op, agent, body, how, revision);\n+\t\t\t\treturn await this.diskCall(\n+\t\t\t\t\tname,\n+\t\t\t\t\top,\n+\t\t\t\t\tagent,\n+\t\t\t\t\tbody,\n+\t\t\t\t\thow,\n+\t\t\t\t\trevision,\n+\t\t\t\t\trequestId ? { id: requestId, principal } : undefined,\n+\t\t\t\t);\n \t\t\t} catch (error) {\n+\t\t\t\tif ((error as { code?: string })?.code === \"request-reused\")\n+\t\t\t\t\treturn { status: 409, body: { error: \"Idempotency-Key was reused for a different request\" } };\n \t\t\t\tif ((error as { code?: string })?.code === \"authority-changed\")\n \t\t\t\t\treturn { status: 409, body: { error: \"project authorization changed; retry the request\", retryable: true } };\n \t\t\t\tthrow error;\n@@ -499,14 +543,27 @@\n \t\t\tif (found) await this.ctx.storage.put(\"git:head\", found.head);\n \t\t\treturn null;\n \t\t}\n+\t\t// Unexpected failures leave the cursor unacknowledged for retry. Engine\n+\t\t// refusals are recorded and acknowledged so a later fixing commit can advance.\n+\t\tconst ingest = (p: EngineProject) =>\n+\t\t\tp.ingest({ author: found.author, base: found.base, commits: found.commits }, Date.now());\n+\t\tconst runtime = this.diskRuntime();\n \t\tlet result: unknown;\n \t\ttry {\n-\t\t\tresult = this.useProject(\n-\t\t\t\t(p) => p.ingest({ author: found.author, base: found.base, commits: found.commits }, Date.now()),\n-\t\t\t\ttrue,\n-\t\t\t);\n-\t\t} catch (e) {\n-\t\t\tresult = { error: e instanceof Error ? e.message : String(e) };\n+\t\t\tresult = runtime\n+\t\t\t\t? runtime.write(ingest, () => {}, {\n+\t\t\t\t\t\tid: `git:${found.head}`,\n+\t\t\t\t\t\tidentity: {\n+\t\t\t\t\t\t\tproject: name,\n+\t\t\t\t\t\t\thead: found.head,\n+\t\t\t\t\t\t\tbase: found.base,\n+\t\t\t\t\t\t\tcommits: found.commits.map((commit) => commit.sha),\n+\t\t\t\t\t\t},\n+\t\t\t\t\t})\n+\t\t\t\t: this.useProject(ingest, true);\n+\t\t} catch (error) {\n+\t\t\tif (!(error instanceof ProjectError)) throw error;\n+\t\t\tresult = { error: error.message };\n \t\t}\n \t\tconst taken = {\n \t\t\tat: new Date().toISOString(),"}],"unmatched":[]}