/** * Per-run claims — the durable spine of the stateless Ask-AI turn surface. * * Replaces the sessions-entity run claims (session-store's `currentRunId` + * `metadata.run`) with a store keyed by RUN, not by session: per-project * `runs/index.json` records every run a project spawned, and a record with no * `outcome` IS the live claim. There is no thread record anywhere — a run is * the only unit of persistence, and everything a client needs to continue a * conversation (the end frame's `runtimeSessionId`, harvested by the fold) * reaches it out of band. * * Concurrency: ONE live run per project. `beginRun` refuses (DagError * `RUN_IN_PROGRESS`, mapped to HTTP 409 by respond.ts) while an unsettled * claim holds the index — the same single overlap signal the sessions route * used, renamed. * * Liveness survives a restart the same way it always did: the claim persists * the child's pid, and `settleOrphanedRuns` (the startup sweep) probes it. * Deliberately narrowed from the old reconciler: a dead pid alone no longer * settles — the run's own deadline must also have passed, so a claim written * by a previous server process whose child outlived the restart is left alone, * and a claim whose child died mid-run settles `interrupted` exactly once. */ /** How a run ended. A superset of the runner's own `RunOutcome`: `interrupted` * is never produced BY a run, it is written FOR a run whose process vanished * without settling — a restart's orphan, or a cancel. */ export type RunOutcome = "success" | "error" | "timeout" | "interrupted"; /** * One run record. `outcome` absent ⇢ the record IS the live claim; present ⇢ * the run has settled and `endedAt` is stamped. * * All timestamps are epoch ms (`Date.now()`), matching the runner's own record * unit. Fields are validated on read — a hand-edited index can carry anything. */ export interface RunRecord { /** UUID the route minted — the claim's identity and the log's filename key. */ runId: string; /** OS pid of the spawned child; absent when the spawn produced none. */ pid?: number; /** Epoch ms of the claim. */ startedAt: number; /** Epoch ms the runner's kill timer is armed for (resolveTimeoutMs + now). */ deadlineAt: number; /** The driver's runtime type ("pi" | "opencode" | "claude-code" | "codex"). */ runtimeType: string; /** The binary the runner spawned. */ runner: string; /** The runtime session id this run continued, when it was a continuation. */ continueSessionId?: string; /** * Directory segment the run's event log is keyed under (`sessions/` * filename prefix) — the project slug for the ask route. Retention and the * startup sweep prune logs by this segment, so it rides the claim rather * than being re-derived by a process that was not the spawner. */ logSegment: string; outcome?: RunOutcome; error?: string; /** * Epoch ms the CHILD exited — stamped from the runner's record, so a settle * that runs after a restart (the sweep) stamps its own now. */ endedAt?: number; /** Length of the run's reply text, as the runner measured it. */ replyChars?: number; /** The runtime-native session id the fold harvested from the run log — the * continuation handle the end frame carries to the client. */ runtimeSessionId?: string; /** Typed failure the client can act on (e.g. "CONTINUATION_LOST"). */ errorCode?: string; /** The run's event log hit its cap or lost bytes — the end frame reports it * so a consumer knows it reached a hole, never a silent end. */ eventLogTruncated?: boolean; /** Epoch ms the run's workspace changes were reverted (POST /revert). Once * stamped, a second revert is refused — one revert per run, whatever it * found to revert. Lives on the record (not the sidecars) so it survives * their retention pruning. */ revertedAt?: number; } export interface RunIndex { runs: RunRecord[]; } /** Index path — the pattern session-store used for `sessions/index.json`. */ export declare function runsIndexPath(projectDir: string): string; export interface BeginRunInput { /** Caller-minted id for this run — the claim's identity. */ runId: string; /** OS pid of the spawned child; absent/`null` when the spawn produced none. */ pid?: number | null; /** Epoch ms the runner will kill the child at — persisted WITH the claim so * the startup sweep can tell "still within its window" from "orphaned". */ deadlineAt: number; /** Driver runtime type that shaped the run's argv. */ runtimeType: string; /** Binary the runner spawns. */ runner: string; /** The runtime session id this run continues, when it is a continuation. */ continueSessionId?: string; /** Directory segment the run's event log is keyed under (see RunRecord). */ logSegment: string; } /** * Claims the project's one live-run slot for `runId`. * * Refuses with `RUN_IN_PROGRESS` (409 on the wire) while ANY unsettled claim * holds the index — the atomic single-live-run gate, checked under the same * lock the settle releases it under, so two overlapping POSTs cannot both win. * The claim lands BEFORE the child exists: from here on, a server that dies * mid-run leaves a claim behind rather than an invisible orphan, and the * startup sweep is what settles it. */ export declare function beginRun(projectDir: string, input: BeginRunInput): Promise; /** Records the child's pid on the claim AFTER the spawn produced one. No-op * when the claim is already gone (the run settled in the gap) — a pid write * must never resurrect a settled run. */ export declare function updateRunPid(projectDir: string, input: { runId: string; pid: number; }): Promise; export interface SettleRunInput { /** Settles only when it matches the persisted claim — a run that has already * settled (or was superseded) is never settled twice. */ runId: string; outcome: RunOutcome; error?: string; /** Epoch ms the run ended — defaults to now. */ endedAt?: number; replyChars?: number; /** The runtime-native session id the fold harvested — the continuation handle. */ runtimeSessionId?: string; /** Typed failure code the client can act on (CONTINUATION_LOST, …). */ errorCode?: string; eventLogTruncated?: boolean; } /** * Stamps the outcome onto the run's claim in ONE lock acquisition — the same * write that releases the claim, so "claim gone" and "outcome readable" can * never disagree (the stream's end frame depends on that). * * Keyed on the run id: a settle whose run has already settled (a cancel that * won the race, or a newer claim) is a byte-identical no-op. */ export declare function settleRun(projectDir: string, input: SettleRunInput): Promise; /** One run record, or `undefined` when the index does not list the run id. */ export declare function getRun(projectDir: string, runId: string): Promise; /** * Stamps the run's workspace changes as reverted (POST /revert), once. * * Refuses with `RUN_ALREADY_REVERTED` (409 on the wire) when the run has * already been reverted, `RUN_NOT_FOUND` when the run is not in the index, and * `RUN_NOT_SETTLED` while the run is still live — a claim in flight may still * be writing, so its changes are not final and must not be reverted. * Under the same lock every other mutation uses, so a second revert racing * the first can never both win. */ export declare function markRunReverted(projectDir: string, runId: string): Promise; /** The project's current in-flight run claim, or `undefined`. */ export declare function liveRun(projectDir: string): Promise; /** * Whether a pid is a live process, via signal 0 (no signal delivered). * * SHORTCUT: pids are reused, so a recycled pid reads alive and keeps an orphan * claim standing; upgrade to a start-time check (`/proc//stat` field 22 or * `ps -o lstart`) when a stuck claim is ever actually observed. */ export declare function isProcessAlive(pid: number): boolean; export interface SweepOptions { /** Injectable pid liveness (tests). */ isAlive?: (pid: number) => boolean; /** Clock in epoch ms; defaults to `Date.now()`. */ now?: number; } /** * Settles every run claim in a project whose process is gone AND whose * deadline has passed, as `interrupted`. * * A claim only stands while its pid is still alive within its own deadline — a * run spawned by a previous server process either outlived the restart (leave * it be; it still holds the project's slot) or died with it, and a dead claim * that is never cleared is a project stuck on "a run is in progress" forever. * The deadline condition is what keeps a claim whose child merely outlived a * crash from being settled out from under a live process. * * A settle here also prunes the project's run event logs, exactly as the * route's own write-back does — this is the ONLY settle an interrupted run * ever gets, so it is the only place that can bound the logs of a project * whose runs never settle normally. * * Never throws: a project whose index cannot be read is skipped, and a record * deleted mid-sweep is simply not settled. */ export declare function settleOrphanedRuns(projectDir: string, options?: SweepOptions): Promise; /** * Startup sweep across every registered project, keyed by RUN. Composition-time * work (app.ts calls it once, fire-and-forget) — one pass, never a poller. * * Never throws — a data dir without a readable meta.json settles nothing, * rather than taking the server down at boot. */ export declare function settleOrphanedRunsOnStartup(dataDir?: string, options?: SweepOptions): Promise; //# sourceMappingURL=run-store.d.ts.map