Lines 4–21 at 318 (were 4–15 at 158)
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"; 1214import { artifactsOf, type Env } from "./env.ts"; 15+import { GitDiskIntake } from "./git-disk-intake.ts"; 16+import { GitFileContents } from "./git-file-contents.ts"; 17+import { GitIntakeLimitError } from "./git-intake-limit.ts"; 18+import { GitIntakeTrees } from "./git-intake-trees.ts"; 1319import { pushedSince, remoteHead } from "./ingest.ts"; Lines 30–36 at 318 (were 24–29 at 158)
2632import { type PushResult, pushShared, repoName } from "./push.ts"; 33+import { pushSnapshot } from "./push-snapshot.ts"; 2734import { SqliteWriterState } from "./writer-host.ts"; 2936const READ_OPS = new Set([ Lines 49–55 at 318 (were 42–47 at 158)
Lines 64–74 at 318 (were 56–61 at 158)
5664export const PUSH_EVERY_MS = 60_000; 5765/** While agents are working, how often a call looks for plain-git pushes to the repo. */ 5866const GIT_CHECK_MS = 15_000; 67+// Disk scratch admission limits, independent of the Worker's resident memory. 68+const GIT_PACK_MAX_BYTES = 128 * 1024 * 1024; 69+const GIT_OBJECT_MAX_BYTES = 64 * 1024 * 1024; 70+const GIT_DECODED_MAX_BYTES = 256 * 1024 * 1024; 71+const GIT_LIMIT_POLICY = `expanded-v2:${GIT_PACK_MAX_BYTES}:${GIT_OBJECT_MAX_BYTES}:${GIT_DECODED_MAX_BYTES}`; 6174 * The load guard (platform issue 46): an estimate of a loaded project's memory from its log entries, lines and claim Lines 129–139 at 318 (were 116–121 at 158)
116129 constructor(ctx: DurableObjectState, env: Env) { 118131 this.blobs = new BlobStore(ctx.storage); 132+ if (env.ABSO_DISK_FIRST === "on") { 133+ GitDiskIntake.recover(ctx.storage); 134+ GitFileContents.recover(ctx.storage); 135+ GitIntakeTrees.recover(ctx.storage); 121139 /** Where files kept whole go: the project's blob store. */ Lines 181–187 at 318 (were 163–168 at 158)
164182 how: string | undefined, 184+ request?: { id: string; principal: string }, 166185 ): Promise<Answer> { 167186 const runtime = this.diskRuntime()!; 168187 const validate = () => { Lines 189–199 at 318 (were 170–176 at 158)
170189 throw Object.assign(Error("project authorization changed during call setup"), { code: "authority-changed" }); 172191 const progress = await this.prepareDisk(validate); 173− const known = new Set((await this.ctx.storage.get<string[]>("readers")) ?? []);
192+ const legacyReaders = (await this.ctx.storage.get<string[]>("readers")) ?? []; 193+ const storedReaders = progress?.ready 194+ ? (new SqliteWriterState(this.ctx.storage, "server-disk-v1").read("runtime", "readers") as string[] | undefined) 196+ const known = new Set(storedReaders ?? legacyReaders); 175198 let readersChanged = false; 176199 const readers: Readers = { Lines 210–253 at 318 (were 187–211 at 158)
187210 return { status: 404, body: { error: "no such project; run init first" } }; 188211 if (!progress?.ready && !(progress?.phase === "empty" && op === "init")) 189212 return { status: 503, body: { error: "project indexes are being prepared", retryAfter: 1, progress } }; 213+ if (agent && op !== "reader") { 214+ const refused = readerAnswer({ status: () => undefined }, op, agent, body, readers); 215+ if (refused) return refused; 190217 const before = new SqliteLog(this.ctx.storage).seq(); 191218 const action = (calls: EngineProject) => { 192219 const result = answer(calls, op, agent, body, readers, how); 193220 if (result.status >= 500) throw Error(`project operation failed: ${JSON.stringify(result.body)}`); 222+ new SqliteWriterState(this.ctx.storage, "server-disk-v1").write("runtime", "readers", readers.list()); 226+ const receipt = request 228+ id: `http:${createHash("sha256").update(request.principal).digest("hex")}:${request.id}`, 231+ principal: request.principal, 232+ agent: agent ?? null, 198240 progress.phase === "empty" 199− ? runtime.initialize(action, validate)
241+ ? runtime.initialize(action, validate, receipt) 200242 : READ_OPS.has(op) || op.startsWith("change/") 201243 ? runtime.read(action, validate) 202− : runtime.write(action, validate);
244+ : runtime.write(action, validate, receipt); 203246 if (readersChanged) { 204247 this.authorityRevision++; 205− await this.ctx.storage.put("readers", readers.list());
207249 const log = new SqliteLog(this.ctx.storage); 208− if (log.seq() !== before) {
250+ if (log.seq() !== before || (request && !READ_OPS.has(op))) { 210252 await this.ctx.storage.put("name", name); 211253 await this.soon(Date.now() + 100); Lines 387–424 at 318 (were 345–364 at 158)
348391 ): Promise<Answer> { 349392 const revision = this.authorityRevision; 393+ let principal = "shared-token"; 395+ requestId !== undefined && 396+ !/^[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) 398+ return { status: 400, body: { error: "Idempotency-Key must be a UUID" } }; 351400 const decided = await this.checkAgentKey(key.presented, agent, key.address); 352401 if (!decided.ok) return { status: decided.status, body: { error: decided.error } }; 353402 const allowed = keyAllows(decided.role, { write: key.write, agent, route: key.route }); 354403 if (!allowed.ok) return { status: allowed.status, body: { error: allowed.error } }; 405+ principal = `key:${decided.id}`; 357407 if (op === "feed" && !inFeed) return this.feed(name, body); 358408 if (this.diskRuntime()) { 360− return await this.diskCall(name, op, agent, body, how, revision);
410+ return await this.diskCall( 417+ requestId ? { id: requestId, principal } : undefined, 420+ if ((error as { code?: string })?.code === "request-reused") 421+ return { status: 409, body: { error: "Idempotency-Key was reused for a different request" } }; 362422 if ((error as { code?: string })?.code === "authority-changed") 363423 return { status: 409, body: { error: "project authorization changed; retry the request", retryable: true } }; Lines 546–625 at 318 (were 486–512 at 158)
486546 const seen = await this.ctx.storage.get<string>("git:head"); 487547 const pushed = await this.ctx.storage.get<PushResult>("pushed"); 488548 const head = await remoteHead(artifacts, name); 549+ const refusal = this.diskRuntime() 550+ ? await this.ctx.storage.get<{ head: string; policy: string; kind: GitIntakeLimitError["kind"]; error: string }>( 558+ head !== pushed?.commit && 559+ refusal.head === head && 560+ refusal.policy === GIT_LIMIT_POLICY 562+ throw new GitIntakeLimitError(refusal.kind, refusal.error); 563+ await this.ctx.storage.delete("git:size-refusal"); 489565 if (!head || head === seen || head === pushed?.commit) return null; 490566 const sharedBlobs = this.useProject((p) => p.shared().blobs ?? {}); 567+ using trees = this.diskRuntime() ? new GitIntakeTrees(this.ctx.storage) : undefined; 491568 const found = await pushedSince( 495572 async (bytes) => (bytes.byteLength > BLOB_MAX ? null : this.blobs.putBytes(bytes)), 496573 (path) => sharedBlobs[path], 497− );
574+ trees ? (files, path, body, oid) => trees.retainFile(files, path, body, oid) : undefined, 577+ storage: this.ctx.storage, 578+ maxPackBytes: GIT_PACK_MAX_BYTES, 579+ maxObjectBytes: GIT_OBJECT_MAX_BYTES, 580+ maxDecodedBytes: GIT_DECODED_MAX_BYTES, 581+ maxBinaryBytes: BLOB_MAX, 582+ retainFileChunks: (files, path, chunks, oid) => trees.retainChunks(files, path, chunks, oid), 585+ ).catch(async (error: unknown) => { 586+ if (trees && error instanceof GitIntakeLimitError) { 589+ policy: GIT_LIMIT_POLICY, 591+ error: error.message, 592+ at: new Date().toISOString(), 594+ await this.ctx.storage.put({ "git:size-refusal": refusal, "git:error": refusal }); 498598 if (!found || found.commits.length === 0) { 499599 if (found) await this.ctx.storage.put("git:head", found.head); 602+ // Unexpected failures leave the cursor unacknowledged for retry. Engine 603+ // refusals are recorded and acknowledged so a later fixing commit can advance. 604+ const ingest = (p: EngineProject) => 605+ p.ingest({ author: found.author, base: found.base, commits: found.commits }, Date.now()); 606+ const runtime = this.diskRuntime(); 502607 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) };
610+ ? runtime.write(ingest, () => {}, { 611+ id: `git:${found.head}`, 616+ commits: found.commits.map((commit) => commit.sha), 619+ : this.useProject(ingest, true); 621+ if (!(error instanceof ProjectError)) throw error; 622+ result = { error: error.message }; 512625 at: new Date().toISOString(), Lines 653–682 at 318 (were 540–554 at 158)
540653 if (!artifacts) return; 541654 const name = await this.ctx.storage.get<string>("name"); 542655 if (!name || !this.useProject((p) => p.exists())) return; 656+ let intakeFailed = false; 543657 await this.gitSync().then( 544− () => this.ctx.storage.delete("git:error"),
545− (e) =>
546− this.ctx.storage.put("git:error", {
659+ await this.ctx.storage.delete("git:error"); 660+ await this.ctx.storage.delete("git:retry-attempts"); 663+ intakeFailed = true; 664+ const attempts = ((await this.ctx.storage.get<number>("git:retry-attempts")) ?? 0) + 1; 665+ await this.ctx.storage.put("git:retry-attempts", attempts); 666+ await this.ctx.storage.put("git:error", { 547667 at: new Date().toISOString(), 548668 error: e instanceof Error ? e.message : String(e), 549− }),
670+ await this.soon(Date.now() + (attempts === 1 ? 1 : attempts === 2 ? 5 : 15) * 60_000); 551− const shared = this.useProject((p) => p.shared());
673+ // Never export over a remote head whose changes could not be taken in. 674+ if (intakeFailed) return; 675+ const shared = pushSnapshot( 676+ this.useProject((p) => p.sharedPushMetadata()), 677+ (version, path) => this.useProject((current) => current.fileTextAt(version, path)), 678+ (version, paths) => this.useProject((current) => current.fileTextsAt(version, paths)), 552680 const done = await this.ctx.storage.get<PushResult>("pushed"); 553681 if (done && done.version >= shared.version) return; 554682 const pushedAt = (await this.ctx.storage.get<number>("pushedAt")) ?? 0; Lines 686–705 at 318 (were 558–564 at 158)
559687 let result: PushResult; 561− result = await pushShared(artifacts, name, shared, (sha) => this.blobs.read(sha));
689+ result = await pushShared( 693+ (sha) => this.blobs.read(sha), 694+ this.env.ABSO_DISK_FIRST === "on" 696+ storage: this.ctx.storage, 697+ maxPackBytes: GIT_PACK_MAX_BYTES, 698+ maxObjectBytes: GIT_OBJECT_MAX_BYTES, 699+ maxDecodedBytes: GIT_DECODED_MAX_BYTES, 563704 // Kept for /out, and tried again with a backoff of our own (a thrown alarm is retried only six times). 564705 const was = await this.ctx.storage.get<{ attempts?: number }>("pushError");