{"file":"server/src/project-object.ts","version":161,"total":698,"from":1,"to":698,"lines":[{"n":1,"text":"// One project: its append-only log and blobs in the Durable Object's SQLite, the engine loaded from them, and the push","atom":"a1454","author":"speed","version":150},{"n":2,"text":"// out to the project's Artifacts repo after each new version of shared. Ported from the platform's ProjectObject","atom":"a1454","author":"speed","version":150},{"n":3,"text":"// (worker/src/index.ts), trimmed to the core path: no CI, merge queue checker, boxes, pipelines, members, visibility,","atom":"a1454","author":"speed","version":150},{"n":4,"text":"// R2 copies, edge cache, probes or disk-first writer.","atom":"a1454","author":"speed","version":150},{"n":5,"text":"","atom":"a1454","author":"speed","version":150},{"n":6,"text":"import { DurableObject } from \"cloudflare:workers\";","atom":"a1454","author":"speed","version":150},{"n":7,"text":"import { type Answer, answer, type Readers } from \"../../worker/project/http.ts\";","atom":"a1454","author":"speed","version":150},{"n":8,"text":"import type { ProjectCalls, ProjectHistory } from \"../../worker/project/project-api.ts\";","atom":"a1454","author":"speed","version":150},{"n":9,"text":"import { BLOB_MAX, BlobStore } from \"./blobs.ts\";","atom":"a1454","author":"speed","version":150},{"n":10,"text":"import { DiskFirstRuntime } from \"./disk-first-runtime.ts\";","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":11,"text":"import { EngineProject, SqliteLog } from \"./engine-project.ts\";","atom":"a1454","author":"speed","version":150},{"n":12,"text":"import { artifactsOf, type Env } from \"./env.ts\";","atom":"a1487","author":"speed","version":150},{"n":13,"text":"import { pushedSince, remoteHead } from \"./ingest.ts\";","atom":"a1454","author":"speed","version":150},{"n":14,"text":"import {","atom":"a1454","author":"speed","version":150},{"n":15,"text":"\tcheckKey,","atom":"a1454","author":"speed","version":150},{"n":16,"text":"\temptyKeyState,","atom":"a1454","author":"speed","version":150},{"n":17,"text":"\texpiresAt,","atom":"a1454","author":"speed","version":150},{"n":18,"text":"\thmac,","atom":"a1454","author":"speed","version":150},{"n":19,"text":"\tissueKey,","atom":"a1487","author":"speed","version":150},{"n":20,"text":"\ttype KeyRole,","atom":"a1454","author":"speed","version":150},{"n":21,"text":"\ttype KeyState,","atom":"a1454","author":"speed","version":150},{"n":22,"text":"\tkeyAllows,","atom":"a1454","author":"speed","version":150},{"n":23,"text":"\tlistKeys,","atom":"a1454","author":"speed","version":150},{"n":24,"text":"\trevokeKey,","atom":"a1454","author":"speed","version":150},{"n":25,"text":"} from \"./keys.ts\";","atom":"a1454","author":"speed","version":150},{"n":26,"text":"import { type PushResult, pushShared, repoName } from \"./push.ts\";","atom":"a1454","author":"speed","version":150},{"n":27,"text":"import { SqliteWriterState } from \"./writer-host.ts\";","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":28,"text":"","atom":"a1454","author":"speed","version":150},{"n":29,"text":"const READ_OPS = new Set([","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":30,"text":"\t\"status\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":31,"text":"\t\"blame\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":32,"text":"\t\"why\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":33,"text":"\t\"tree\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":34,"text":"\t\"path\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":35,"text":"\t\"follow\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":36,"text":"\t\"collisions\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":37,"text":"\t\"draft-state\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":38,"text":"\t\"refusals\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":39,"text":"\t\"labels\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":40,"text":"\t\"log\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":41,"text":"\t\"show\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":42,"text":"\t\"file\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":43,"text":"\t\"files\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":44,"text":"\t\"compare\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":45,"text":"\t\"where\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":46,"text":"\t\"feed\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":47,"text":"\t\"drafts\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":48,"text":"\t\"diff\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":49,"text":"\t\"ribbon\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":50,"text":"\t\"position\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":51,"text":"\t\"changes\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":52,"text":"\t\"notes\",","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":53,"text":"]);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":54,"text":"","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":55,"text":"/** At most one push out a minute: each one clones the repo, so publishes in a burst share one. */","atom":"a1454","author":"speed","version":150},{"n":56,"text":"export const PUSH_EVERY_MS = 60_000;","atom":"a1454","author":"speed","version":150},{"n":57,"text":"/** While agents are working, how often a call looks for plain-git pushes to the repo. */","atom":"a1454","author":"speed","version":150},{"n":58,"text":"const GIT_CHECK_MS = 15_000;","atom":"a1454","author":"speed","version":150},{"n":59,"text":"","atom":"a1454","author":"speed","version":150},{"n":60,"text":"/**","atom":"a1454","author":"speed","version":150},{"n":61,"text":" * The load guard (platform issue 46): an estimate of a loaded project's memory from its log entries, lines and claim","atom":"a1454","author":"speed","version":150},{"n":62,"text":" * keys, all read from SQLite without loading. A Durable Object has 128 MB. Past the hard cap a project refuses to","atom":"a1454","author":"speed","version":150},{"n":63,"text":" * load (export still reads it); past the soft cap, new work is held so the hard cap is never reached.","atom":"a1454","author":"speed","version":150},{"n":64,"text":" */","atom":"a1454","author":"speed","version":150},{"n":65,"text":"const LOAD_BYTES_PER_ENTRY = 1_200;","atom":"a1454","author":"speed","version":150},{"n":66,"text":"const LOAD_BYTES_PER_LINE = 20;","atom":"a1454","author":"speed","version":150},{"n":67,"text":"const LOAD_BYTES_PER_KEY = 20;","atom":"a1454","author":"speed","version":150},{"n":68,"text":"const LOAD_BASE_BYTES = 10_000_000;","atom":"a1454","author":"speed","version":150},{"n":69,"text":"const LOAD_MAX_BYTES = 114_000_000;","atom":"a1454","author":"speed","version":150},{"n":70,"text":"const LOAD_SOFT_BYTES = 94_800_000;","atom":"a1454","author":"speed","version":150},{"n":71,"text":"/** Before its lines are counted, a log over this many characters refuses to load. */","atom":"a1454","author":"speed","version":150},{"n":72,"text":"const LOG_LOAD_MAX_CHARS = 12_000_000;","atom":"a1454","author":"speed","version":150},{"n":73,"text":"/** The ops the soft cap holds: the ones that bring new work into the history. */","atom":"a1454","author":"speed","version":150},{"n":74,"text":"const SOFT_HELD_OPS: ReadonlySet<string> = new Set([\"record\", \"edit\", \"publish\", \"land\", \"amend\", \"pluck\", \"drop\"]);","atom":"a1454","author":"speed","version":150},{"n":75,"text":"","atom":"a1454","author":"speed","version":150},{"n":76,"text":"export function loadEstimate(entries: number, lines: number, keys: number): number {","atom":"a1454","author":"speed","version":150},{"n":77,"text":"\treturn entries * LOAD_BYTES_PER_ENTRY + lines * LOAD_BYTES_PER_LINE + keys * LOAD_BYTES_PER_KEY + LOAD_BASE_BYTES;","atom":"a1454","author":"speed","version":150},{"n":78,"text":"}","atom":"a1454","author":"speed","version":150},{"n":79,"text":"","atom":"a1454","author":"speed","version":150},{"n":80,"text":"/** A presented agent key, checked inside the call so the check and the call are one trip. */","atom":"a1454","author":"speed","version":150},{"n":81,"text":"export interface KeyForCall {","atom":"a1454","author":"speed","version":150},{"n":82,"text":"\tpresented: string;","atom":"a1454","author":"speed","version":150},{"n":83,"text":"\taddress: string;","atom":"a1454","author":"speed","version":150},{"n":84,"text":"\twrite: boolean;","atom":"a1454","author":"speed","version":150},{"n":85,"text":"\troute: string | undefined;","atom":"a1454","author":"speed","version":150},{"n":86,"text":"}","atom":"a1454","author":"speed","version":150},{"n":87,"text":"","atom":"a1454","author":"speed","version":150},{"n":88,"text":"/** A news item from the issues code: delivered as an announcement from its writer. */","atom":"a1454","author":"speed","version":150},{"n":89,"text":"export interface IssueNews {","atom":"a1454","author":"speed","version":150},{"n":90,"text":"\tfrom: string;","atom":"a1454","author":"speed","version":150},{"n":91,"text":"\tissue: number | null;","atom":"a1454","author":"speed","version":150},{"n":92,"text":"\ttext: string;","atom":"a1454","author":"speed","version":150},{"n":93,"text":"\tto?: string[];","atom":"a1454","author":"speed","version":150},{"n":94,"text":"}","atom":"a1454","author":"speed","version":150},{"n":95,"text":"","atom":"a1454","author":"speed","version":150},{"n":96,"text":"/** What the issues Durable Object hears when a start note naming an issue begins and ends. Each is optional. */","atom":"a1454","author":"speed","version":150},{"n":97,"text":"interface IssueHooks {","atom":"a1454","author":"speed","version":150},{"n":98,"text":"\tclaim?(agent: string, issue: number, change: number): Promise<unknown>;","atom":"a1454","author":"speed","version":150},{"n":99,"text":"\tunclaim?(agent: string, issue: number): Promise<unknown>;","atom":"a1454","author":"speed","version":150},{"n":100,"text":"\tdone?(agent: string, issue: number, version: number): Promise<unknown>;","atom":"a1454","author":"speed","version":150},{"n":101,"text":"}","atom":"a1454","author":"speed","version":150},{"n":102,"text":"","atom":"a1454","author":"speed","version":150},{"n":103,"text":"export class ProjectObject extends DurableObject<Env> {","atom":"a1454","author":"speed","version":150},{"n":104,"text":"\tprivate blobs: BlobStore;","atom":"a1454","author":"speed","version":150},{"n":105,"text":"\tprivate loaded: ProjectCalls | undefined;","atom":"a1454","author":"speed","version":150},{"n":106,"text":"\t/** Why the log didn't load, if it didn't. */","atom":"a1454","author":"speed","version":150},{"n":107,"text":"\tprivate loadError: unknown;","atom":"a1454","author":"speed","version":150},{"n":108,"text":"\tprivate tried = false;","atom":"a1454","author":"speed","version":150},{"n":109,"text":"\tprivate authorityRevision = 0;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":110,"text":"\t/** Feed long-polls waiting for the log to grow; woken after every call that wrote. */","atom":"a1454","author":"speed","version":150},{"n":111,"text":"\tprivate waiting: (() => void)[] = [];","atom":"a1454","author":"speed","version":150},{"n":112,"text":"\t/** When this object last asked the repo for pushes made with plain git. */","atom":"a1454","author":"speed","version":150},{"n":113,"text":"\tprivate gitCheckedAt = 0;","atom":"a1454","author":"speed","version":150},{"n":114,"text":"\tprivate gitSyncing: Promise<{ head: string; commits: number; answer: unknown } | null> | undefined;","atom":"a1454","author":"speed","version":150},{"n":115,"text":"","atom":"a1454","author":"speed","version":150},{"n":116,"text":"\tconstructor(ctx: DurableObjectState, env: Env) {","atom":"a1454","author":"speed","version":150},{"n":117,"text":"\t\tsuper(ctx, env);","atom":"a1454","author":"speed","version":150},{"n":118,"text":"\t\tthis.blobs = new BlobStore(ctx.storage);","atom":"a1454","author":"speed","version":150},{"n":119,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":120,"text":"","atom":"a1454","author":"speed","version":150},{"n":121,"text":"\t/** Where files kept whole go: the project's blob store. */","atom":"a1454","author":"speed","version":150},{"n":122,"text":"\tprivate wholeFiles() {","atom":"a1454","author":"speed","version":150},{"n":123,"text":"\t\treturn { keep: (text: string) => this.blobs.putText(text), read: (sha: string) => this.blobs.textOf(sha) };","atom":"a1454","author":"speed","version":150},{"n":124,"text":"\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":125,"text":"","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":126,"text":"\tprivate diskRuntime(): DiskFirstRuntime | undefined {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":127,"text":"\t\treturn this.env.ABSO_DISK_FIRST === \"on\"","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":128,"text":"\t\t\t? new DiskFirstRuntime(this.ctx.storage, this.ctx.id.toString(), \"server-disk-v1\", {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":129,"text":"\t\t\t\t\thasBlob: (sha) => this.blobs.has(sha),","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":130,"text":"\t\t\t\t\twhole: this.wholeFiles(),","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":131,"text":"\t\t\t\t})","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":132,"text":"\t\t\t: undefined;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":133,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":134,"text":"\tprivate async prepareDisk(validate: () => void = () => {}) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":135,"text":"\t\tconst runtime = this.diskRuntime();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":136,"text":"\t\tif (!runtime) return undefined;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":137,"text":"\t\tconst progress = runtime.preparePage(validate);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":138,"text":"\t\tif (!progress.ready && progress.phase !== \"empty\") await this.soon(Date.now() + 100);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":139,"text":"\t\treturn progress;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":140,"text":"\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":141,"text":"\t/** Once indexing starts, legacy writes would invalidate the persistent root. */","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":142,"text":"\tprivate legacyWritesHeld(): boolean {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":143,"text":"\t\tif (this.env.ABSO_DISK_FIRST === \"on\") return false;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":144,"text":"\t\tconst tables = this.ctx.storage.sql","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":145,"text":"\t\t\t.exec(\"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'writer_state'\")","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":146,"text":"\t\t\t.toArray();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":147,"text":"\t\treturn (","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":148,"text":"\t\t\ttables.length > 0 &&","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":149,"text":"\t\t\tnew SqliteWriterState(this.ctx.storage, \"server-disk-v1\").read(\"runtime\", \"format\") !== undefined","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":150,"text":"\t\t);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":151,"text":"\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":152,"text":"\tprivate useProject<T>(operation: (calls: EngineProject) => T, write = false): T {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":153,"text":"\t\tconst runtime = this.diskRuntime();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":154,"text":"\t\tif (runtime) return write ? runtime.write(operation, () => {}) : runtime.read(operation, () => {});","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":155,"text":"\t\tif (write && this.legacyWritesHeld())","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":156,"text":"\t\t\tthrow Error(\"disk-first indexes exist; enable ABSO_DISK_FIRST before writing\");","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":157,"text":"\t\treturn operation(this.project as EngineProject);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":158,"text":"\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":159,"text":"\tprivate async diskCall(","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":160,"text":"\t\tname: string,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":161,"text":"\t\top: string,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":162,"text":"\t\tagent: string | undefined,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":163,"text":"\t\tbody: unknown,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":164,"text":"\t\thow: string | undefined,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":165,"text":"\t\trevision: number,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":166,"text":"\t): Promise<Answer> {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":167,"text":"\t\tconst runtime = this.diskRuntime()!;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":168,"text":"\t\tconst validate = () => {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":169,"text":"\t\t\tif (revision !== this.authorityRevision)","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":170,"text":"\t\t\t\tthrow Object.assign(Error(\"project authorization changed during call setup\"), { code: \"authority-changed\" });","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":171,"text":"\t\t};","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":172,"text":"\t\tconst progress = await this.prepareDisk(validate);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":173,"text":"\t\tconst known = new Set((await this.ctx.storage.get<string[]>(\"readers\")) ?? []);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":174,"text":"\t\tvalidate();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":175,"text":"\t\tlet readersChanged = false;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":176,"text":"\t\tconst readers: Readers = {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":177,"text":"\t\t\thas: (a) => known.has(a),","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":178,"text":"\t\t\tlist: () => [...known].sort(),","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":179,"text":"\t\t\tset: (a, on) => {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":180,"text":"\t\t\t\tif (on === known.has(a)) return;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":181,"text":"\t\t\t\tif (on) known.add(a);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":182,"text":"\t\t\t\telse known.delete(a);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":183,"text":"\t\t\t\treadersChanged = true;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":184,"text":"\t\t\t},","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":185,"text":"\t\t};","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":186,"text":"\t\tif (progress?.phase === \"empty\" && op !== \"init\")","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":187,"text":"\t\t\treturn { status: 404, body: { error: \"no such project; run init first\" } };","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":188,"text":"\t\tif (!progress?.ready && !(progress?.phase === \"empty\" && op === \"init\"))","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":189,"text":"\t\t\treturn { status: 503, body: { error: \"project indexes are being prepared\", retryAfter: 1, progress } };","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":190,"text":"\t\tconst before = new SqliteLog(this.ctx.storage).seq();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":191,"text":"\t\tconst action = (calls: EngineProject) => {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":192,"text":"\t\t\tconst result = answer(calls, op, agent, body, readers, how);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":193,"text":"\t\t\tif (result.status >= 500) throw Error(`project operation failed: ${JSON.stringify(result.body)}`);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":194,"text":"\t\t\treturn result;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":195,"text":"\t\t};","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":196,"text":"","atom":"a1454","author":"speed","version":150},{"n":197,"text":"\t\tconst result =","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":198,"text":"\t\t\tprogress.phase === \"empty\"","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":199,"text":"\t\t\t\t? runtime.initialize(action, validate)","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":200,"text":"\t\t\t\t: READ_OPS.has(op) || op.startsWith(\"change/\")","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":201,"text":"\t\t\t\t\t? runtime.read(action, validate)","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":202,"text":"\t\t\t\t\t: runtime.write(action, validate);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":203,"text":"\t\tif (readersChanged) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":204,"text":"\t\t\tthis.authorityRevision++;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":205,"text":"\t\t\tawait this.ctx.storage.put(\"readers\", readers.list());","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":206,"text":"\t\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":207,"text":"\t\tconst log = new SqliteLog(this.ctx.storage);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":208,"text":"\t\tif (log.seq() !== before) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":209,"text":"\t\t\tthis.wake();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":210,"text":"\t\t\tawait this.ctx.storage.put(\"name\", name);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":211,"text":"\t\t\tawait this.soon(Date.now() + 100);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":212,"text":"\t\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":213,"text":"\t\tif (","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":214,"text":"\t\t\tresult.status === 200 &&","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":215,"text":"\t\t\tartifactsOf(this.env) &&","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":216,"text":"\t\t\tDate.now() - this.gitCheckedAt > GIT_CHECK_MS &&","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":217,"text":"\t\t\t(await this.ctx.storage.get(\"name\")) !== undefined &&","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":218,"text":"\t\t\t(await this.ctx.storage.getAlarm()) === null","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":219,"text":"\t\t)","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":220,"text":"\t\t\tawait this.ctx.storage.setAlarm(Date.now());","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":221,"text":"\t\tif (result.status === 200 && op === \"starting\") result.body = await this.startingHooks(name, result.body);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":222,"text":"\t\tif (result.status === 200) result.body = this.withBlobSizes(op, result.body);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":223,"text":"\t\treturn result;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":224,"text":"\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":225,"text":"","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":226,"text":"\t/** The project, loaded from its log on first use. A log too big for memory is refused, not loaded. */","atom":"a1454","author":"speed","version":150},{"n":227,"text":"\tprivate ensure(): ProjectCalls | undefined {","atom":"a1454","author":"speed","version":150},{"n":228,"text":"\t\tif (this.diskRuntime()) throw Error(\"legacy project load forbidden in disk-first mode\");","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":229,"text":"\t\tif (this.loaded || this.tried) return this.loaded;","atom":"a1454","author":"speed","version":150},{"n":230,"text":"\t\tthis.tried = true;","atom":"a1454","author":"speed","version":150},{"n":231,"text":"\t\ttry {","atom":"a1454","author":"speed","version":150},{"n":232,"text":"\t\t\tconst why = this.tooBigToLoad();","atom":"a1454","author":"speed","version":150},{"n":233,"text":"\t\t\tif (why) throw new Error(`${why}, too big to load in a Durable Object's memory; GET export still reads it`);","atom":"a1454","author":"speed","version":150},{"n":234,"text":"\t\t\tthis.loaded = EngineProject.open(this.ctx.storage, (sha) => this.blobs.has(sha), this.wholeFiles());","atom":"a1454","author":"speed","version":150},{"n":235,"text":"\t\t} catch (e) {","atom":"a1454","author":"speed","version":150},{"n":236,"text":"\t\t\tthis.loadError = e;","atom":"a1454","author":"speed","version":150},{"n":237,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":238,"text":"\t\treturn this.loaded;","atom":"a1454","author":"speed","version":150},{"n":239,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":240,"text":"","atom":"a1454","author":"speed","version":150},{"n":241,"text":"\tprivate get project(): ProjectCalls {","atom":"a1454","author":"speed","version":150},{"n":242,"text":"\t\tconst p = this.ensure();","atom":"a1454","author":"speed","version":150},{"n":243,"text":"\t\tif (!p) throw this.loadError;","atom":"a1454","author":"speed","version":150},{"n":244,"text":"\t\treturn p;","atom":"a1454","author":"speed","version":150},{"n":245,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":246,"text":"","atom":"a1454","author":"speed","version":150},{"n":247,"text":"\tprivate logChars(): { chars: number; entries: number } {","atom":"a1454","author":"speed","version":150},{"n":248,"text":"\t\tconst tables = this.ctx.storage.sql","atom":"a1454","author":"speed","version":150},{"n":249,"text":"\t\t\t.exec(\"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'log'\")","atom":"a1454","author":"speed","version":150},{"n":250,"text":"\t\t\t.toArray();","atom":"a1454","author":"speed","version":150},{"n":251,"text":"\t\treturn tables.length === 0 ? { chars: 0, entries: 0 } : new SqliteLog(this.ctx.storage).logSize();","atom":"a1454","author":"speed","version":150},{"n":252,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":253,"text":"","atom":"a1454","author":"speed","version":150},{"n":254,"text":"\tprivate sizes(): { lines: number; keys: number } | undefined {","atom":"a1454","author":"speed","version":150},{"n":255,"text":"\t\tif (this.logChars().entries === 0) return undefined;","atom":"a1454","author":"speed","version":150},{"n":256,"text":"\t\ttry {","atom":"a1454","author":"speed","version":150},{"n":257,"text":"\t\t\treturn new SqliteLog(this.ctx.storage).countSizes();","atom":"a1454","author":"speed","version":150},{"n":258,"text":"\t\t} catch {","atom":"a1454","author":"speed","version":150},{"n":259,"text":"\t\t\treturn undefined;","atom":"a1454","author":"speed","version":150},{"n":260,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":261,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":262,"text":"","atom":"a1454","author":"speed","version":150},{"n":263,"text":"\t/** Why recording new work is held: the estimate is past the soft cap; else null. */","atom":"a1454","author":"speed","version":150},{"n":264,"text":"\tprivate softHeld(): string | null {","atom":"a1454","author":"speed","version":150},{"n":265,"text":"\t\tconst counted = this.sizes();","atom":"a1454","author":"speed","version":150},{"n":266,"text":"\t\tif (counted === undefined) return null;","atom":"a1454","author":"speed","version":150},{"n":267,"text":"\t\tconst bytes = loadEstimate(this.logChars().entries, counted.lines, counted.keys);","atom":"a1454","author":"speed","version":150},{"n":268,"text":"\t\treturn bytes > LOAD_SOFT_BYTES","atom":"a1454","author":"speed","version":150},{"n":269,"text":"\t\t\t? `this project is near the most a project can hold (an estimated ${(bytes / 1e6).toFixed(0)} MB of ${LOAD_MAX_BYTES / 1e6} MB), so recording and publishing are held; status and reads keep working`","atom":"a1454","author":"speed","version":150},{"n":270,"text":"\t\t\t: null;","atom":"a1454","author":"speed","version":150},{"n":271,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":272,"text":"","atom":"a1454","author":"speed","version":150},{"n":273,"text":"\t/** Why this project mustn't load: its estimated memory, or before its lines are counted, its log's size; else null. */","atom":"a1454","author":"speed","version":150},{"n":274,"text":"\tprivate tooBigToLoad(): string | null {","atom":"a1454","author":"speed","version":150},{"n":275,"text":"\t\tconst counted = this.sizes();","atom":"a1454","author":"speed","version":150},{"n":276,"text":"\t\tif (counted !== undefined) {","atom":"a1454","author":"speed","version":150},{"n":277,"text":"\t\t\tconst bytes = loadEstimate(this.logChars().entries, counted.lines, counted.keys);","atom":"a1454","author":"speed","version":150},{"n":278,"text":"\t\t\treturn bytes > LOAD_MAX_BYTES","atom":"a1454","author":"speed","version":150},{"n":279,"text":"\t\t\t\t? `this project holds ${counted.lines} lines and ${this.logChars().entries} entries, an estimated ${(bytes / 1e6).toFixed(0)} MB once loaded`","atom":"a1454","author":"speed","version":150},{"n":280,"text":"\t\t\t\t: null;","atom":"a1454","author":"speed","version":150},{"n":281,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":282,"text":"\t\tconst { chars } = this.logChars();","atom":"a1454","author":"speed","version":150},{"n":283,"text":"\t\treturn chars > LOG_LOAD_MAX_CHARS ? `this project's log is ${(chars / 1e6).toFixed(1)} MB` : null;","atom":"a1454","author":"speed","version":150},{"n":284,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":285,"text":"","atom":"a1454","author":"speed","version":150},{"n":286,"text":"\t// Blobs: binary files' bytes, named by their SHA-256.","atom":"a1454","author":"speed","version":150},{"n":287,"text":"","atom":"a1454","author":"speed","version":150},{"n":288,"text":"\tasync putBlob(sha: string, body: ReadableStream<Uint8Array>): Promise<Answer> {","atom":"a1454","author":"speed","version":150},{"n":289,"text":"\t\tconst a = await this.blobs.put(sha, body);","atom":"a1454","author":"speed","version":150},{"n":290,"text":"\t\t// A push out waiting on missing blobs goes again now.","atom":"a1454","author":"speed","version":150},{"n":291,"text":"\t\tif (a.status === 200 && (await this.ctx.storage.get(\"pushError\"))) await this.ctx.storage.setAlarm(Date.now());","atom":"a1454","author":"speed","version":150},{"n":292,"text":"\t\treturn a;","atom":"a1454","author":"speed","version":150},{"n":293,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":294,"text":"","atom":"a1454","author":"speed","version":150},{"n":295,"text":"\tasync getBlob(sha: string): Promise<ReadableStream<Uint8Array> | null> {","atom":"a1454","author":"speed","version":150},{"n":296,"text":"\t\treturn this.blobs.stream(sha);","atom":"a1454","author":"speed","version":150},{"n":297,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":298,"text":"","atom":"a1454","author":"speed","version":150},{"n":299,"text":"\t/** Shared's newest version, from storage alone, never loading the project. Null when there's no project. */","atom":"a1454","author":"speed","version":150},{"n":300,"text":"\tasync head(): Promise<{ version: number; at: string } | null> {","atom":"a1454","author":"speed","version":150},{"n":301,"text":"\t\tif (this.logChars().entries === 0) return null;","atom":"a1454","author":"speed","version":150},{"n":302,"text":"\t\treturn { version: new SqliteLog(this.ctx.storage).sharedVersion(), at: new Date().toISOString() };","atom":"a1454","author":"speed","version":150},{"n":303,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":304,"text":"","atom":"a1454","author":"speed","version":150},{"n":305,"text":"\t/** Backups: one page of the log as JSON lines. It works even when the log won't load. */","atom":"a1454","author":"speed","version":150},{"n":306,"text":"\tasync exportLog(from: number, limit: number): Promise<{ lines: string; next: number | null } | null> {","atom":"a1454","author":"speed","version":150},{"n":307,"text":"\t\tif (this.logChars().entries === 0) return null;","atom":"a1454","author":"speed","version":150},{"n":308,"text":"\t\tconst { texts, next } = new SqliteLog(this.ctx.storage).page(from, limit);","atom":"a1454","author":"speed","version":150},{"n":309,"text":"\t\treturn { lines: texts.map((t) => `${t}\\n`).join(\"\"), next };","atom":"a1454","author":"speed","version":150},{"n":310,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":311,"text":"","atom":"a1454","author":"speed","version":150},{"n":312,"text":"\tprivate wake(): void {","atom":"a1454","author":"speed","version":150},{"n":313,"text":"\t\tconst all = this.waiting;","atom":"a1454","author":"speed","version":150},{"n":314,"text":"\t\tthis.waiting = [];","atom":"a1454","author":"speed","version":150},{"n":315,"text":"\t\tfor (const w of all) w();","atom":"a1454","author":"speed","version":150},{"n":316,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":317,"text":"","atom":"a1454","author":"speed","version":150},{"n":318,"text":"\t/**","atom":"a1454","author":"speed","version":150},{"n":319,"text":"\t * GET feed?from=&wait=<seconds, up to 25>: when nothing is new, hold the call until an entry is written or the wait","atom":"a1454","author":"speed","version":150},{"n":320,"text":"\t * ends, then answer as usual.","atom":"a1454","author":"speed","version":150},{"n":321,"text":"\t */","atom":"a1454","author":"speed","version":150},{"n":322,"text":"\tprivate async feed(name: string, body: unknown): Promise<Answer> {","atom":"a1454","author":"speed","version":150},{"n":323,"text":"\t\tconst q = (body ?? {}) as Record<string, string | undefined>;","atom":"a1454","author":"speed","version":150},{"n":324,"text":"\t\tconst wait = Math.min(25, Math.max(0, Number(q.wait) || 0));","atom":"a1454","author":"speed","version":150},{"n":325,"text":"\t\tconst first = await this.call(name, \"feed\", undefined, body, true);","atom":"a1454","author":"speed","version":150},{"n":326,"text":"\t\tconst from = Number(q.from) || 0;","atom":"a1454","author":"speed","version":150},{"n":327,"text":"\t\tconst empty = first.status === 200 && (first.body as { next: number }).next <= from;","atom":"a1454","author":"speed","version":150},{"n":328,"text":"\t\tif (!empty || wait === 0) return first;","atom":"a1454","author":"speed","version":150},{"n":329,"text":"\t\tawait new Promise<void>((resolve) => {","atom":"a1454","author":"speed","version":150},{"n":330,"text":"\t\t\tconst timer = setTimeout(resolve, wait * 1000);","atom":"a1454","author":"speed","version":150},{"n":331,"text":"\t\t\tthis.waiting.push(() => {","atom":"a1454","author":"speed","version":150},{"n":332,"text":"\t\t\t\tclearTimeout(timer);","atom":"a1454","author":"speed","version":150},{"n":333,"text":"\t\t\t\tresolve();","atom":"a1454","author":"speed","version":150},{"n":334,"text":"\t\t\t});","atom":"a1454","author":"speed","version":150},{"n":335,"text":"\t\t});","atom":"a1454","author":"speed","version":150},{"n":336,"text":"\t\treturn this.call(name, \"feed\", undefined, body, true);","atom":"a1454","author":"speed","version":150},{"n":337,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":338,"text":"","atom":"a1454","author":"speed","version":150},{"n":339,"text":"\t/** One project call: the agent key (if any), the size caps, then answer() from worker/project/http.ts. */","atom":"a1454","author":"speed","version":150},{"n":340,"text":"\tasync call(","atom":"a1454","author":"speed","version":150},{"n":341,"text":"\t\tname: string,","atom":"a1454","author":"speed","version":150},{"n":342,"text":"\t\top: string,","atom":"a1454","author":"speed","version":150},{"n":343,"text":"\t\tagent: string | undefined,","atom":"a1454","author":"speed","version":150},{"n":344,"text":"\t\tbody: unknown,","atom":"a1454","author":"speed","version":150},{"n":345,"text":"\t\tinFeed = false,","atom":"a1454","author":"speed","version":150},{"n":346,"text":"\t\thow?: string,","atom":"a1454","author":"speed","version":150},{"n":347,"text":"\t\tkey?: KeyForCall,","atom":"a1454","author":"speed","version":150},{"n":348,"text":"\t): Promise<Answer> {","atom":"a1454","author":"speed","version":150},{"n":349,"text":"\t\tconst revision = this.authorityRevision;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":350,"text":"\t\tif (key) {","atom":"a1454","author":"speed","version":150},{"n":351,"text":"\t\t\tconst decided = await this.checkAgentKey(key.presented, agent, key.address);","atom":"a1454","author":"speed","version":150},{"n":352,"text":"\t\t\tif (!decided.ok) return { status: decided.status, body: { error: decided.error } };","atom":"a1454","author":"speed","version":150},{"n":353,"text":"\t\t\tconst allowed = keyAllows(decided.role, { write: key.write, agent, route: key.route });","atom":"a1454","author":"speed","version":150},{"n":354,"text":"\t\t\tif (!allowed.ok) return { status: allowed.status, body: { error: allowed.error } };","atom":"a1454","author":"speed","version":150},{"n":355,"text":"\t\t\thow = decided.as;","atom":"a1454","author":"speed","version":150},{"n":356,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":357,"text":"\t\tif (op === \"feed\" && !inFeed) return this.feed(name, body);","atom":"a1454","author":"speed","version":150},{"n":358,"text":"\t\tif (this.diskRuntime()) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":359,"text":"\t\t\ttry {","atom":"a1454","author":"speed","version":150},{"n":360,"text":"\t\t\t\treturn await this.diskCall(name, op, agent, body, how, revision);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":361,"text":"\t\t\t} catch (error) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":362,"text":"\t\t\t\tif ((error as { code?: string })?.code === \"authority-changed\")","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":363,"text":"\t\t\t\t\treturn { status: 409, body: { error: \"project authorization changed; retry the request\", retryable: true } };","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":364,"text":"\t\t\t\tthrow error;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":365,"text":"\t\t\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":366,"text":"\t\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":367,"text":"\t\tif (this.legacyWritesHeld() && !READ_OPS.has(op) && !op.startsWith(\"change/\"))","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":368,"text":"\t\t\treturn { status: 409, body: { error: \"disk-first indexes exist; enable ABSO_DISK_FIRST before writing\" } };","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":369,"text":"\t\tif (SOFT_HELD_OPS.has(op)) {","atom":"a1454","author":"speed","version":150},{"n":370,"text":"\t\t\tconst held = this.softHeld();","atom":"a1454","author":"speed","version":150},{"n":371,"text":"\t\t\tif (held) return { status: 507, body: { error: held } };","atom":"a1454","author":"speed","version":150},{"n":372,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":373,"text":"\t\tconst loaded = this.ensure();","atom":"a1454","author":"speed","version":150},{"n":374,"text":"\t\tif (!loaded) {","atom":"a1454","author":"speed","version":150},{"n":375,"text":"\t\t\tconst why = this.loadError instanceof Error ? this.loadError.message : String(this.loadError);","atom":"a1454","author":"speed","version":150},{"n":376,"text":"\t\t\treturn { status: 507, body: { error: `the project's log did not load: ${why}` } };","atom":"a1454","author":"speed","version":150},{"n":377,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":378,"text":"\t\t// Readers live in storage, not the log: a refused call writes nothing, so replay never needs them.","atom":"a1454","author":"speed","version":150},{"n":379,"text":"\t\tconst list = new Set((await this.ctx.storage.get<string[]>(\"readers\")) ?? []);","atom":"a1454","author":"speed","version":150},{"n":380,"text":"\t\tlet changed = false;","atom":"a1454","author":"speed","version":150},{"n":381,"text":"\t\tconst readers: Readers = {","atom":"a1454","author":"speed","version":150},{"n":382,"text":"\t\t\thas: (a) => list.has(a),","atom":"a1454","author":"speed","version":150},{"n":383,"text":"\t\t\tset: (a, on) => {","atom":"a1454","author":"speed","version":150},{"n":384,"text":"\t\t\t\tif (on === list.has(a)) return;","atom":"a1454","author":"speed","version":150},{"n":385,"text":"\t\t\t\tif (on) list.add(a);","atom":"a1454","author":"speed","version":150},{"n":386,"text":"\t\t\t\telse list.delete(a);","atom":"a1454","author":"speed","version":150},{"n":387,"text":"\t\t\t\tchanged = true;","atom":"a1454","author":"speed","version":150},{"n":388,"text":"\t\t\t},","atom":"a1454","author":"speed","version":150},{"n":389,"text":"\t\t\tlist: () => [...list].sort(),","atom":"a1454","author":"speed","version":150},{"n":390,"text":"\t\t};","atom":"a1454","author":"speed","version":150},{"n":391,"text":"\t\tconst before = loaded.exists() ? loaded.shared().version : -1;","atom":"a1454","author":"speed","version":150},{"n":392,"text":"\t\tconst logLength = () => (this.loaded as { logLength?: () => number } | undefined)?.logLength?.() ?? 0;","atom":"a1454","author":"speed","version":150},{"n":393,"text":"\t\tconst entriesBefore = logLength();","atom":"a1454","author":"speed","version":150},{"n":394,"text":"\t\tconst a = answer(loaded, op, agent, body, readers, how);","atom":"a1454","author":"speed","version":150},{"n":395,"text":"\t\tif (changed) await this.ctx.storage.put(\"readers\", readers.list());","atom":"a1454","author":"speed","version":150},{"n":396,"text":"\t\tif (logLength() !== entriesBefore) this.wake();","atom":"a1454","author":"speed","version":150},{"n":397,"text":"\t\tif (a.status === 200 && op === \"starting\") a.body = await this.startingHooks(name, a.body);","atom":"a1454","author":"speed","version":150},{"n":398,"text":"\t\tif (a.status === 200) a.body = this.withBlobSizes(op, a.body);","atom":"a1454","author":"speed","version":150},{"n":399,"text":"\t\tconst after = loaded.exists() ? loaded.shared().version : -1;","atom":"a1454","author":"speed","version":150},{"n":400,"text":"\t\tif (a.status === 200 && after >= 0 && (op === \"init\" || after > before)) {","atom":"a1454","author":"speed","version":150},{"n":401,"text":"\t\t\t// A new version of shared: push it out to Artifacts in the background. The alarm retries; the call never waits.","atom":"a1454","author":"speed","version":150},{"n":402,"text":"\t\t\tawait this.ctx.storage.put(\"name\", name);","atom":"a1454","author":"speed","version":150},{"n":403,"text":"\t\t\tif (artifactsOf(this.env)) await this.ctx.storage.setAlarm(Date.now());","atom":"a1487","author":"speed","version":150},{"n":404,"text":"\t\t} else if (","atom":"a1454","author":"speed","version":150},{"n":405,"text":"\t\t\ta.status === 200 &&","atom":"a1454","author":"speed","version":150},{"n":406,"text":"\t\t\tartifactsOf(this.env) &&","atom":"a1487","author":"speed","version":150},{"n":407,"text":"\t\t\tDate.now() - this.gitCheckedAt > GIT_CHECK_MS &&","atom":"a1454","author":"speed","version":150},{"n":408,"text":"\t\t\t(await this.ctx.storage.get(\"name\")) !== undefined &&","atom":"a1454","author":"speed","version":150},{"n":409,"text":"\t\t\t(await this.ctx.storage.getAlarm()) === null","atom":"a1454","author":"speed","version":150},{"n":410,"text":"\t\t) {","atom":"a1454","author":"speed","version":150},{"n":411,"text":"\t\t\t// While agents are working, the alarm also looks for plain-git pushes.","atom":"a1454","author":"speed","version":150},{"n":412,"text":"\t\t\tawait this.ctx.storage.setAlarm(Date.now());","atom":"a1454","author":"speed","version":150},{"n":413,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":414,"text":"\t\treturn a;","atom":"a1454","author":"speed","version":150},{"n":415,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":416,"text":"","atom":"a1454","author":"speed","version":150},{"n":417,"text":"\t/**","atom":"a1454","author":"speed","version":150},{"n":418,"text":"\t * The engine's starting answer carries hooks for the issues code (a start note naming an issue began or ended). They","atom":"a1454","author":"speed","version":150},{"n":419,"text":"\t * go to the project's IssueProject, and never to the client. A failed hook is logged and never fails the call.","atom":"a1454","author":"speed","version":150},{"n":420,"text":"\t */","atom":"a1454","author":"speed","version":150},{"n":421,"text":"\tprivate async startingHooks(name: string, body: unknown): Promise<unknown> {","atom":"a1454","author":"speed","version":150},{"n":422,"text":"\t\tconst { hooks, ...rest } = body as {","atom":"a1454","author":"speed","version":150},{"n":423,"text":"\t\t\thooks?: {","atom":"a1454","author":"speed","version":150},{"n":424,"text":"\t\t\t\tclaim?: { issue: number; agent: string; change: number };","atom":"a1454","author":"speed","version":150},{"n":425,"text":"\t\t\t\tunclaim?: { issue: number; agent: string; change: number; done?: boolean; version?: number };","atom":"a1454","author":"speed","version":150},{"n":426,"text":"\t\t\t};","atom":"a1454","author":"speed","version":150},{"n":427,"text":"\t\t};","atom":"a1454","author":"speed","version":150},{"n":428,"text":"\t\tif (!hooks) return body;","atom":"a1454","author":"speed","version":150},{"n":429,"text":"\t\tconst issues = this.env.ISSUES_DO.get(this.env.ISSUES_DO.idFromName(name)) as unknown as IssueHooks;","atom":"a1454","author":"speed","version":150},{"n":430,"text":"\t\tconst tell = async (what: string, call: () => Promise<unknown> | undefined) => {","atom":"a1454","author":"speed","version":150},{"n":431,"text":"\t\t\ttry {","atom":"a1454","author":"speed","version":150},{"n":432,"text":"\t\t\t\tawait call();","atom":"a1454","author":"speed","version":150},{"n":433,"text":"\t\t\t} catch (e) {","atom":"a1454","author":"speed","version":150},{"n":434,"text":"\t\t\t\tconsole.log(JSON.stringify({ event: \"issues-hook\", what, project: name, error: String(e).slice(0, 200) }));","atom":"a1454","author":"speed","version":150},{"n":435,"text":"\t\t\t}","atom":"a1454","author":"speed","version":150},{"n":436,"text":"\t\t};","atom":"a1454","author":"speed","version":150},{"n":437,"text":"\t\tconst end = hooks.unclaim;","atom":"a1454","author":"speed","version":150},{"n":438,"text":"\t\tif (end?.done && end.version !== undefined) {","atom":"a1454","author":"speed","version":150},{"n":439,"text":"\t\t\tconst version = end.version;","atom":"a1454","author":"speed","version":150},{"n":440,"text":"\t\t\tawait tell(\"done\", () => issues.done?.(end.agent, end.issue, version));","atom":"a1454","author":"speed","version":150},{"n":441,"text":"\t\t} else if (end) await tell(\"unclaim\", () => issues.unclaim?.(end.agent, end.issue));","atom":"a1454","author":"speed","version":150},{"n":442,"text":"\t\tconst claim = hooks.claim;","atom":"a1454","author":"speed","version":150},{"n":443,"text":"\t\tif (claim) await tell(\"claim\", () => issues.claim?.(claim.agent, claim.issue, claim.change));","atom":"a1454","author":"speed","version":150},{"n":444,"text":"\t\treturn rest;","atom":"a1454","author":"speed","version":150},{"n":445,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":446,"text":"","atom":"a1454","author":"speed","version":150},{"n":447,"text":"\t/** A folder's binary files count by their bytes, and a binary file says its size: the blob store knows both. */","atom":"a1454","author":"speed","version":150},{"n":448,"text":"\tprivate withBlobSizes(op: string, body: unknown): unknown {","atom":"a1454","author":"speed","version":150},{"n":449,"text":"\t\tif (op === \"tree\" && Array.isArray((body as { entries?: unknown }).entries)) {","atom":"a1454","author":"speed","version":150},{"n":450,"text":"\t\t\tconst b = body as { entries: { bytes?: number; blobs?: string[] }[] };","atom":"a1454","author":"speed","version":150},{"n":451,"text":"\t\t\treturn {","atom":"a1454","author":"speed","version":150},{"n":452,"text":"\t\t\t\t...b,","atom":"a1454","author":"speed","version":150},{"n":453,"text":"\t\t\t\tentries: b.entries.map(({ blobs, ...e }) =>","atom":"a1454","author":"speed","version":150},{"n":454,"text":"\t\t\t\t\tblobs ? { ...e, bytes: (e.bytes ?? 0) + blobs.reduce((n, sha) => n + this.blobs.size(sha), 0) } : e,","atom":"a1454","author":"speed","version":150},{"n":455,"text":"\t\t\t\t),","atom":"a1454","author":"speed","version":150},{"n":456,"text":"\t\t\t};","atom":"a1454","author":"speed","version":150},{"n":457,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":458,"text":"\t\tif (op === \"file\" && typeof (body as { blob?: unknown }).blob === \"string\")","atom":"a1454","author":"speed","version":150},{"n":459,"text":"\t\t\treturn { ...(body as object), size: this.blobs.size((body as { blob: string }).blob) };","atom":"a1454","author":"speed","version":150},{"n":460,"text":"\t\treturn body;","atom":"a1454","author":"speed","version":150},{"n":461,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":462,"text":"","atom":"a1454","author":"speed","version":150},{"n":463,"text":"\t// The push out to Artifacts, and plain-git pushes back in.","atom":"a1454","author":"speed","version":150},{"n":464,"text":"","atom":"a1454","author":"speed","version":150},{"n":465,"text":"\t/**","atom":"a1454","author":"speed","version":150},{"n":466,"text":"\t * Takes in commits pushed to the project's repo with plain git: the ones above the server's last commit that","atom":"a1454","author":"speed","version":150},{"n":467,"text":"\t * haven't been taken in yet. One run at a time; a call while a run is going gets that run's answer.","atom":"a1454","author":"speed","version":150},{"n":468,"text":"\t */","atom":"a1454","author":"speed","version":150},{"n":469,"text":"\tgitSync(): Promise<{ head: string; commits: number; answer: unknown } | null> {","atom":"a1454","author":"speed","version":150},{"n":470,"text":"\t\tthis.gitSyncing ??= this.gitSyncOnce().finally(() => {","atom":"a1454","author":"speed","version":150},{"n":471,"text":"\t\t\tthis.gitSyncing = undefined;","atom":"a1454","author":"speed","version":150},{"n":472,"text":"\t\t});","atom":"a1454","author":"speed","version":150},{"n":473,"text":"\t\treturn this.gitSyncing;","atom":"a1454","author":"speed","version":150},{"n":474,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":475,"text":"","atom":"a1454","author":"speed","version":150},{"n":476,"text":"\tprivate async gitSyncOnce(): Promise<{ head: string; commits: number; answer: unknown } | null> {","atom":"a1454","author":"speed","version":150},{"n":477,"text":"\t\tif (this.legacyWritesHeld()) return null;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":478,"text":"\t\tconst artifacts = artifactsOf(this.env);","atom":"a1487","author":"speed","version":150},{"n":479,"text":"\t\tif (!artifacts) return null;","atom":"a1454","author":"speed","version":150},{"n":480,"text":"\t\tthis.gitCheckedAt = Date.now();","atom":"a1454","author":"speed","version":150},{"n":481,"text":"\t\tconst name = await this.ctx.storage.get<string>(\"name\");","atom":"a1454","author":"speed","version":150},{"n":482,"text":"\t\tif (!name) return null;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":483,"text":"\t\tconst progress = await this.prepareDisk();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":484,"text":"\t\tif (progress && !progress.ready) return null;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":485,"text":"\t\tif (!this.useProject((p) => p.exists())) return null;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":486,"text":"\t\tconst seen = await this.ctx.storage.get<string>(\"git:head\");","atom":"a1454","author":"speed","version":150},{"n":487,"text":"\t\tconst pushed = await this.ctx.storage.get<PushResult>(\"pushed\");","atom":"a1454","author":"speed","version":150},{"n":488,"text":"\t\tconst head = await remoteHead(artifacts, name);","atom":"a1454","author":"speed","version":150},{"n":489,"text":"\t\tif (!head || head === seen || head === pushed?.commit) return null;","atom":"a1454","author":"speed","version":150},{"n":490,"text":"\t\tconst sharedBlobs = this.useProject((p) => p.shared().blobs ?? {});","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":491,"text":"\t\tconst found = await pushedSince(","atom":"a1454","author":"speed","version":150},{"n":492,"text":"\t\t\tartifacts,","atom":"a1454","author":"speed","version":150},{"n":493,"text":"\t\t\tname,","atom":"a1454","author":"speed","version":150},{"n":494,"text":"\t\t\tseen,","atom":"a1454","author":"speed","version":150},{"n":495,"text":"\t\t\tasync (bytes) => (bytes.byteLength > BLOB_MAX ? null : this.blobs.putBytes(bytes)),","atom":"a1454","author":"speed","version":150},{"n":496,"text":"\t\t\t(path) => sharedBlobs[path],","atom":"a1454","author":"speed","version":150},{"n":497,"text":"\t\t);","atom":"a1454","author":"speed","version":150},{"n":498,"text":"\t\tif (!found || found.commits.length === 0) {","atom":"a1454","author":"speed","version":150},{"n":499,"text":"\t\t\tif (found) await this.ctx.storage.put(\"git:head\", found.head);","atom":"a1454","author":"speed","version":150},{"n":500,"text":"\t\t\treturn null;","atom":"a1454","author":"speed","version":150},{"n":501,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":502,"text":"\t\tlet result: unknown;","atom":"a1454","author":"speed","version":150},{"n":503,"text":"\t\ttry {","atom":"a1454","author":"speed","version":150},{"n":504,"text":"\t\t\tresult = this.useProject(","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":505,"text":"\t\t\t\t(p) => p.ingest({ author: found.author, base: found.base, commits: found.commits }, Date.now()),","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":506,"text":"\t\t\t\ttrue,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":507,"text":"\t\t\t);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":508,"text":"\t\t} catch (e) {","atom":"a1454","author":"speed","version":150},{"n":509,"text":"\t\t\tresult = { error: e instanceof Error ? e.message : String(e) };","atom":"a1454","author":"speed","version":150},{"n":510,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":511,"text":"\t\tconst taken = {","atom":"a1454","author":"speed","version":150},{"n":512,"text":"\t\t\tat: new Date().toISOString(),","atom":"a1454","author":"speed","version":150},{"n":513,"text":"\t\t\thead: found.head,","atom":"a1454","author":"speed","version":150},{"n":514,"text":"\t\t\tcommits: found.commits.length,","atom":"a1454","author":"speed","version":150},{"n":515,"text":"\t\t\tanswer: result,","atom":"a1454","author":"speed","version":150},{"n":516,"text":"\t\t\t...(found.tooBig.length ? { tooBig: found.tooBig } : {}),","atom":"a1454","author":"speed","version":150},{"n":517,"text":"\t\t};","atom":"a1454","author":"speed","version":150},{"n":518,"text":"\t\tawait this.ctx.storage.put({ \"git:head\": found.head, \"git:last\": taken });","atom":"a1454","author":"speed","version":150},{"n":519,"text":"\t\tthis.wake();","atom":"a1454","author":"speed","version":150},{"n":520,"text":"\t\tif ((result as { landed?: boolean }).landed) await this.ctx.storage.setAlarm(Date.now());","atom":"a1454","author":"speed","version":150},{"n":521,"text":"\t\treturn taken;","atom":"a1454","author":"speed","version":150},{"n":522,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":523,"text":"","atom":"a1454","author":"speed","version":150},{"n":524,"text":"\t/** Sets the alarm for `at` unless one is due sooner. */","atom":"a1454","author":"speed","version":150},{"n":525,"text":"\tprivate async soon(at: number): Promise<void> {","atom":"a1454","author":"speed","version":150},{"n":526,"text":"\t\tconst now = await this.ctx.storage.getAlarm();","atom":"a1454","author":"speed","version":150},{"n":527,"text":"\t\tif (now === null || now > at) await this.ctx.storage.setAlarm(at);","atom":"a1454","author":"speed","version":150},{"n":528,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":529,"text":"","atom":"a1454","author":"speed","version":150},{"n":530,"text":"\t/**","atom":"a1454","author":"speed","version":150},{"n":531,"text":"\t * Plain-git pushes in first, so the push out builds on them; then shared out to the project's Artifacts repo, at","atom":"a1454","author":"speed","version":150},{"n":532,"text":"\t * most once a minute, backing off on failure. Without an ARTIFACTS binding (local dev) it does nothing.","atom":"a1454","author":"speed","version":150},{"n":533,"text":"\t */","atom":"a1454","author":"speed","version":150},{"n":534,"text":"\tasync alarm(): Promise<void> {","atom":"a1454","author":"speed","version":150},{"n":535,"text":"\t\tif (!this.diskRuntime() && !artifactsOf(this.env)) return;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":536,"text":"\t\tif (!this.diskRuntime() && !this.ensure()) return;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":537,"text":"\t\tconst progress = await this.prepareDisk();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":538,"text":"\t\tif (progress && !progress.ready) return;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":539,"text":"\t\tconst artifacts = artifactsOf(this.env);","atom":"a1487","author":"speed","version":150},{"n":540,"text":"\t\tif (!artifacts) return;","atom":"a1454","author":"speed","version":150},{"n":541,"text":"\t\tconst name = await this.ctx.storage.get<string>(\"name\");","atom":"a1454","author":"speed","version":150},{"n":542,"text":"\t\tif (!name || !this.useProject((p) => p.exists())) return;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":543,"text":"\t\tawait this.gitSync().then(","atom":"a1454","author":"speed","version":150},{"n":544,"text":"\t\t\t() => this.ctx.storage.delete(\"git:error\"),","atom":"a1454","author":"speed","version":150},{"n":545,"text":"\t\t\t(e) =>","atom":"a1454","author":"speed","version":150},{"n":546,"text":"\t\t\t\tthis.ctx.storage.put(\"git:error\", {","atom":"a1487","author":"speed","version":150},{"n":547,"text":"\t\t\t\t\tat: new Date().toISOString(),","atom":"a1487","author":"speed","version":150},{"n":548,"text":"\t\t\t\t\terror: e instanceof Error ? e.message : String(e),","atom":"a1487","author":"speed","version":150},{"n":549,"text":"\t\t\t\t}),","atom":"a1487","author":"speed","version":150},{"n":550,"text":"\t\t);","atom":"a1454","author":"speed","version":150},{"n":551,"text":"\t\tconst shared = this.useProject((p) => p.shared());","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":552,"text":"\t\tconst done = await this.ctx.storage.get<PushResult>(\"pushed\");","atom":"a1454","author":"speed","version":150},{"n":553,"text":"\t\tif (done && done.version >= shared.version) return;","atom":"a1454","author":"speed","version":150},{"n":554,"text":"\t\tconst pushedAt = (await this.ctx.storage.get<number>(\"pushedAt\")) ?? 0;","atom":"a1454","author":"speed","version":150},{"n":555,"text":"\t\tif (Date.now() - pushedAt < PUSH_EVERY_MS) {","atom":"a1454","author":"speed","version":150},{"n":556,"text":"\t\t\tawait this.soon(pushedAt + PUSH_EVERY_MS);","atom":"a1454","author":"speed","version":150},{"n":557,"text":"\t\t\treturn;","atom":"a1454","author":"speed","version":150},{"n":558,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":559,"text":"\t\tlet result: PushResult;","atom":"a1454","author":"speed","version":150},{"n":560,"text":"\t\ttry {","atom":"a1454","author":"speed","version":150},{"n":561,"text":"\t\t\tresult = await pushShared(artifacts, name, shared, (sha) => this.blobs.read(sha));","atom":"a1454","author":"speed","version":150},{"n":562,"text":"\t\t} catch (e) {","atom":"a1454","author":"speed","version":150},{"n":563,"text":"\t\t\t// Kept for /out, and tried again with a backoff of our own (a thrown alarm is retried only six times).","atom":"a1454","author":"speed","version":150},{"n":564,"text":"\t\t\tconst was = await this.ctx.storage.get<{ attempts?: number }>(\"pushError\");","atom":"a1454","author":"speed","version":150},{"n":565,"text":"\t\t\tconst attempts = (was?.attempts ?? 0) + 1;","atom":"a1454","author":"speed","version":150},{"n":566,"text":"\t\t\tawait this.ctx.storage.put(\"pushError\", {","atom":"a1454","author":"speed","version":150},{"n":567,"text":"\t\t\t\tversion: shared.version,","atom":"a1454","author":"speed","version":150},{"n":568,"text":"\t\t\t\tat: new Date().toISOString(),","atom":"a1454","author":"speed","version":150},{"n":569,"text":"\t\t\t\tattempts,","atom":"a1454","author":"speed","version":150},{"n":570,"text":"\t\t\t\terror: e instanceof Error ? e.message : String(e),","atom":"a1454","author":"speed","version":150},{"n":571,"text":"\t\t\t});","atom":"a1454","author":"speed","version":150},{"n":572,"text":"\t\t\tawait this.soon(Date.now() + Math.min(10 * 60_000, 15_000 * 2 ** Math.min(attempts - 1, 6)));","atom":"a1454","author":"speed","version":150},{"n":573,"text":"\t\t\treturn;","atom":"a1454","author":"speed","version":150},{"n":574,"text":"\t\t}","atom":"a1454","author":"speed","version":150},{"n":575,"text":"\t\tawait this.ctx.storage.put({ pushed: result, \"git:head\": result.commit, pushedAt: Date.now() });","atom":"a1454","author":"speed","version":150},{"n":576,"text":"\t\tawait this.ctx.storage.delete(\"pushError\");","atom":"a1454","author":"speed","version":150},{"n":577,"text":"\t\t// Another publish may have landed while this push ran.","atom":"a1454","author":"speed","version":150},{"n":578,"text":"\t\tif (this.useProject((p) => p.shared().version) > result.version) await this.soon(Date.now() + PUSH_EVERY_MS);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":579,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":580,"text":"","atom":"a1454","author":"speed","version":150},{"n":581,"text":"\t/** The push out: the repo, the last version pushed, and plain-git intake. */","atom":"a1454","author":"speed","version":150},{"n":582,"text":"\tasync out() {","atom":"a1454","author":"speed","version":150},{"n":583,"text":"\t\tconst name = await this.ctx.storage.get<string>(\"name\");","atom":"a1454","author":"speed","version":150},{"n":584,"text":"\t\treturn {","atom":"a1454","author":"speed","version":150},{"n":585,"text":"\t\t\trepo: name ? repoName(name) : null,","atom":"a1454","author":"speed","version":150},{"n":586,"text":"\t\t\tartifacts: artifactsOf(this.env) ? \"bound\" : \"unbound\",","atom":"a1487","author":"speed","version":150},{"n":587,"text":"\t\t\tversion: this.logChars().entries ? new SqliteLog(this.ctx.storage).sharedVersion() : null,","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":588,"text":"\t\t\tpushed: (await this.ctx.storage.get<PushResult>(\"pushed\")) ?? null,","atom":"a1454","author":"speed","version":150},{"n":589,"text":"\t\t\terror: (await this.ctx.storage.get(\"pushError\")) ?? null,","atom":"a1454","author":"speed","version":150},{"n":590,"text":"\t\t\tgit: {","atom":"a1454","author":"speed","version":150},{"n":591,"text":"\t\t\t\thead: (await this.ctx.storage.get<string>(\"git:head\")) ?? null,","atom":"a1454","author":"speed","version":150},{"n":592,"text":"\t\t\t\tlast: (await this.ctx.storage.get(\"git:last\")) ?? null,","atom":"a1454","author":"speed","version":150},{"n":593,"text":"\t\t\t\terror: (await this.ctx.storage.get(\"git:error\")) ?? null,","atom":"a1454","author":"speed","version":150},{"n":594,"text":"\t\t\t},","atom":"a1454","author":"speed","version":150},{"n":595,"text":"\t\t};","atom":"a1454","author":"speed","version":150},{"n":596,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":597,"text":"","atom":"a1454","author":"speed","version":150},{"n":598,"text":"\t// Per-agent keys: the HMACs live here, one project's keys per object.","atom":"a1454","author":"speed","version":150},{"n":599,"text":"","atom":"a1454","author":"speed","version":150},{"n":600,"text":"\tprivate async keyState(): Promise<KeyState> {","atom":"a1454","author":"speed","version":150},{"n":601,"text":"\t\treturn (await this.ctx.storage.get<KeyState>(\"agent-keys\")) ?? emptyKeyState();","atom":"a1454","author":"speed","version":150},{"n":602,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":603,"text":"","atom":"a1454","author":"speed","version":150},{"n":604,"text":"\tasync checkAgentKey(presented: string, agent: string | undefined, address: string) {","atom":"a1454","author":"speed","version":150},{"n":605,"text":"\t\tconst state = await this.keyState();","atom":"a1454","author":"speed","version":150},{"n":606,"text":"\t\tconst decided = await checkKey(state, this.env.KEY_HMAC_SECRET, presented, agent, Date.now(), address);","atom":"a1454","author":"speed","version":150},{"n":607,"text":"\t\tif (decided.dirty) await this.ctx.storage.put(\"agent-keys\", state);","atom":"a1454","author":"speed","version":150},{"n":608,"text":"\t\tif (!decided.ok) return decided;","atom":"a1454","author":"speed","version":150},{"n":609,"text":"\t\tconst row = state.rows.find((r) => r.id === decided.id);","atom":"a1454","author":"speed","version":150},{"n":610,"text":"\t\treturn { ...decided, agent: row?.agent ?? \"\" };","atom":"a1454","author":"speed","version":150},{"n":611,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":612,"text":"","atom":"a1454","author":"speed","version":150},{"n":613,"text":"\t/** Issues a key. The returned key is the only copy. */","atom":"a1454","author":"speed","version":150},{"n":614,"text":"\tasync issueAgentKey(","atom":"a1454","author":"speed","version":150},{"n":615,"text":"\t\tagent: string,","atom":"a1454","author":"speed","version":150},{"n":616,"text":"\t\taddress: string,","atom":"a1454","author":"speed","version":150},{"n":617,"text":"\t\texpiresRaw: string | null,","atom":"a1454","author":"speed","version":150},{"n":618,"text":"\t\trole: KeyRole,","atom":"a1454","author":"speed","version":150},{"n":619,"text":"\t): Promise<{ id: string; key: string; agent: string } | { error: string }> {","atom":"a1454","author":"speed","version":150},{"n":620,"text":"\t\tconst secret = this.env.KEY_HMAC_SECRET;","atom":"a1454","author":"speed","version":150},{"n":621,"text":"\t\tif (!secret) return { error: \"the key secret (KEY_HMAC_SECRET) is not set\" };","atom":"a1454","author":"speed","version":150},{"n":622,"text":"\t\tconst when = expiresAt(Date.now(), expiresRaw);","atom":"a1454","author":"speed","version":150},{"n":623,"text":"\t\tif (typeof when !== \"number\") return { error: when.error };","atom":"a1454","author":"speed","version":150},{"n":624,"text":"\t\tconst state = await this.keyState();","atom":"a1454","author":"speed","version":150},{"n":625,"text":"\t\tconst issued = await issueKey(state, secret, agent, \"admin\", Date.now(), address, when, role);","atom":"a1454","author":"speed","version":150},{"n":626,"text":"\t\tthis.authorityRevision++;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":627,"text":"\t\tawait this.ctx.storage.put(\"agent-keys\", state);","atom":"a1454","author":"speed","version":150},{"n":628,"text":"\t\treturn issued;","atom":"a1454","author":"speed","version":150},{"n":629,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":630,"text":"","atom":"a1454","author":"speed","version":150},{"n":631,"text":"\tasync revokeAgentKey(id: string, address: string): Promise<{ revoked: boolean }> {","atom":"a1454","author":"speed","version":150},{"n":632,"text":"\t\tconst secret = this.env.KEY_HMAC_SECRET;","atom":"a1454","author":"speed","version":150},{"n":633,"text":"\t\tif (!secret) return { revoked: false };","atom":"a1454","author":"speed","version":150},{"n":634,"text":"\t\tconst state = await this.keyState();","atom":"a1454","author":"speed","version":150},{"n":635,"text":"\t\tconst revoked = revokeKey(state, id, Date.now(), await hmac(secret, address));","atom":"a1454","author":"speed","version":150},{"n":636,"text":"\t\tthis.authorityRevision++;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":637,"text":"\t\tawait this.ctx.storage.put(\"agent-keys\", state);","atom":"a1454","author":"speed","version":150},{"n":638,"text":"\t\treturn { revoked };","atom":"a1454","author":"speed","version":150},{"n":639,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":640,"text":"","atom":"a1454","author":"speed","version":150},{"n":641,"text":"\tasync listAgentKeys(agent?: string) {","atom":"a1454","author":"speed","version":150},{"n":642,"text":"\t\treturn listKeys(await this.keyState(), agent);","atom":"a1454","author":"speed","version":150},{"n":643,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":644,"text":"","atom":"a1454","author":"speed","version":150},{"n":645,"text":"\t// For the issues code (server/src/issues/).","atom":"a1454","author":"speed","version":150},{"n":646,"text":"","atom":"a1454","author":"speed","version":150},{"n":647,"text":"\t/** The project's agent names, for the ask seat check. None when there's no project. */","atom":"a1454","author":"speed","version":150},{"n":648,"text":"\tasync agents(_project: string): Promise<string[]> {","atom":"a1454","author":"speed","version":150},{"n":649,"text":"\t\tif (!this.diskRuntime() && !this.ensure()) return [];","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":650,"text":"\t\tconst progress = await this.prepareDisk();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":651,"text":"\t\tif (progress?.phase === \"empty\") return [];","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":652,"text":"\t\tif (progress && !progress.ready) throw Error(\"project indexes are being prepared\");","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":653,"text":"\t\treturn this.useProject((p) => (p.exists() ? p.status().agents.map((a) => a.agent) : []));","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":654,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":655,"text":"","atom":"a1454","author":"speed","version":150},{"n":656,"text":"\t/**","atom":"a1454","author":"speed","version":150},{"n":657,"text":"\t * Issues news, delivered through the engine's announce path: each item goes to the agents whose start note names","atom":"a1454","author":"speed","version":150},{"n":658,"text":"\t * its issue and to the known agents in `to`, never back to its writer.","atom":"a1454","author":"speed","version":150},{"n":659,"text":"\t */","atom":"a1454","author":"speed","version":150},{"n":660,"text":"\tasync news(_project: string, items: IssueNews[]): Promise<{ to: string[] }[]> {","atom":"a1454","author":"speed","version":150},{"n":661,"text":"\t\tif (!this.diskRuntime() && !this.ensure()) return items.map(() => ({ to: [] }));","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":662,"text":"\t\tconst progress = await this.prepareDisk();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":663,"text":"\t\tif (progress?.phase === \"empty\") return items.map(() => ({ to: [] }));","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":664,"text":"\t\tif (progress && !progress.ready) throw Error(\"project indexes are being prepared\");","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":665,"text":"\t\treturn this.useProject((p) => {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":666,"text":"\t\t\tif (!p.exists()) return items.map(() => ({ to: [] }));","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":667,"text":"\t\t\tconst engine = p as ProjectCalls & ProjectHistory;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":668,"text":"\t\t\tconst out: { to: string[] }[] = [];","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":669,"text":"\t\t\tfor (const item of items) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":670,"text":"\t\t\t\tconst status = p.status() as { agents: { agent: string; starting?: { issue?: number } }[] };","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":671,"text":"\t\t\t\tconst known = new Set(status.agents.map((a) => a.agent));","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":672,"text":"\t\t\t\tconst to = new Set([","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":673,"text":"\t\t\t\t\t...(item.issue !== null","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":674,"text":"\t\t\t\t\t\t? status.agents.filter((a) => a.starting?.issue === item.issue).map((a) => a.agent)","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":675,"text":"\t\t\t\t\t\t: []),","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":676,"text":"\t\t\t\t\t...(item.to ?? []).filter((t) => known.has(t)),","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":677,"text":"\t\t\t\t]);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":678,"text":"\t\t\t\tto.delete(item.from);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":679,"text":"\t\t\t\tif (to.size === 0) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":680,"text":"\t\t\t\t\tout.push({ to: [] });","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":681,"text":"\t\t\t\t\tcontinue;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":682,"text":"\t\t\t\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":683,"text":"\t\t\t\ttry {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":684,"text":"\t\t\t\t\tengine.announce(item.from, item.text, [...to]);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":685,"text":"\t\t\t\t\tout.push({ to: [...to] });","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":686,"text":"\t\t\t\t} catch (e) {","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":687,"text":"\t\t\t\t\t// Persistent writer state may already have changed. Let the outer","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":688,"text":"\t\t\t\t\t// transaction undo the whole batch before reporting a failure.","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":689,"text":"\t\t\t\t\tif (this.env.ABSO_DISK_FIRST === \"on\") throw e;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":690,"text":"\t\t\t\t\tconsole.log(JSON.stringify({ event: \"issues-news\", error: String(e).slice(0, 200) }));","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":691,"text":"\t\t\t\t\tout.push({ to: [] });","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":692,"text":"\t\t\t\t}","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":693,"text":"\t\t\t}","atom":"a1454","author":"speed","version":150},{"n":694,"text":"\t\t\tthis.wake();","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":695,"text":"\t\t\treturn out;","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":696,"text":"\t\t}, true);","atom":"a1616","author":"codex-capacity-chair","version":158},{"n":697,"text":"\t}","atom":"a1454","author":"speed","version":150},{"n":698,"text":"}","atom":"a1454","author":"speed","version":150}],"atoms":[{"atom":"a1454","author":"speed","message":"","time":1791906824142,"timeIso":"2026-10-13T15:53:44.142Z","publish":{"draft":null,"version":150,"agent":"speed","message":"Open server: the project Worker, Durable Object and Artifacts (issue 17)","time":1791907522179,"timeIso":"2026-10-13T16:05:22.179Z"}},{"atom":"a1487","author":"speed","message":"","time":1791907425388,"timeIso":"2026-10-13T16:03:45.388Z","publish":{"draft":null,"version":150,"agent":"speed","message":"Open server: the project Worker, Durable Object and Artifacts (issue 17)","time":1791907522179,"timeIso":"2026-10-13T16:05:22.179Z"}},{"atom":"a1616","author":"codex-capacity-chair","message":"","time":1791917879117,"timeIso":"2026-10-13T18:57:59.117Z","publish":{"draft":null,"version":158,"agent":"codex-capacity-chair","message":"Integrate opt-in disk-first project runtime with cold-object transactions","time":1791917879117,"timeIso":"2026-10-13T18:57:59.117Z"}}]}