/** * Per-run stdout event log — the durable spine of a headless `claude -p` run. * * Owns `sessions/{normalizedId}.run-{runId}.events.jsonl`: every byte the child * writes to stdout lands here VERBATIM, one file line per stdout line, BEFORE * anything parses it. That ordering is the whole point — the log is the source * of truth and the runner's NDJSON reader is merely its first consumer, so a * line the reader cannot parse (drifted wire format, truncated JSON, an event * type that did not exist when this shipped) is still on disk, and a process * that dies mid-run still leaves behind everything that arrived. * * Writes are synchronous (`writeSync` against an fd opened with "a") for * exactly that reason: an async queue would only ever have *issued* the write * before the parse, and a hard kill would lose whatever was still in flight. * The chunks are small and few (one per stdout chunk of a single child), so the * event-loop cost is a rounding error against a run measured in minutes. * * The `runId` in the filename is the SAME id the route persists as * `currentRunId`, so the log's name and the session record can never disagree * about which run produced it. * * Total by construction, like the reader it feeds: nothing here throws. A log * that cannot be opened or written reports itself through `RunEventLogStats` * and the run proceeds untouched — a logging failure must never fail a run. * Reporting is the whole point: a log that hit its size cap or lost bytes to a * short write says so on `truncated`, which rides all the way out to * `metadata.run.eventLogTruncated`. A silent fallback here would make an empty * log and a capped one look identical to every consumer downstream. * * At settle the log folds down into the transcript sidecar (assistant text plus * one turn per `tool_use`) through `appendSessionTurn` (run-transcript.ts), and * older logs for the same segment are pruned so the sessions dir cannot grow * without limit. */ import type { SessionRuntimeType } from "../utils/storage-utils.js"; /** * Ceiling on one run's log — a runaway child cannot fill the disk. * * The finished file is never LARGER than this, terminator included: the writer * refuses a chunk that would take it TO the cap (`>=`, not `>`), leaving the one * byte `flush()` may still need. The off-by-one that leaves matters because * `foldRunEventLog` refuses to read anything past this size — a writer allowed * to land on exactly the cap and then add a terminator produces a file its own * fold silently discards. * * Reaching it does not close the log: only the crossing chunk is refused, later * chunks that still fit are still written, and `RunEventLogStats.truncated` * reports that bytes are missing. The file always ends on a line boundary, so an * offset-based tail never reads half a record. */ export declare const RUN_EVENT_LOG_MAX_BYTES: number; /** * How many event logs a single session keeps. Count-based rather than * age-based: a count is a hard bound (N logs per session, at most * RUN_EVENT_LOG_MAX_BYTES each), while an age window still admits unbounded * growth from a session that runs in a tight loop. */ export declare const RUN_EVENT_LOG_RETENTION = 5; /** Which run's stdout a log belongs to. */ export interface RunEventLogTarget { /** Project data dir — the log lives under its `sessions/`. */ projectDir: string; /** Canonical `normalizedId` of the write-target session. */ sessionId: string; /** The run's id — the SAME one persisted as `currentRunId`. */ runId: string; } /** What a finished log wrote, for the run record's diagnostics. */ export interface RunEventLogStats { /** Complete lines appended (the terminator added at flush counts its line). */ lines: number; /** Bytes appended — always exactly the size of the file on disk. */ bytes: number; /** * The file is INCOMPLETE: bytes the child wrote are not in it, because a chunk * crossed RUN_EVENT_LOG_MAX_BYTES or a write landed short. What IS there is * still whole lines in stream order — but this file is no longer the whole * stream, so nothing may read it (or tail it by offset) as if it were. * * Surfaced all the way to `metadata.run.eventLogTruncated`: without it a * capped log reports `eventLogLines: 0` and is indistinguishable from a child * that said nothing at all. * * `truncated: false` is the strong claim and holds even when `error` is set: * every byte the child wrote is in the file AND the file ends on a line * boundary. An error beside it belongs to a failure that cost no bytes — the * log never opened (`bytes: 0`, nothing was ever written) or the fd failed to * close after everything had landed. Anything that loses bytes or leaves the * last line open, a failed terminator write included, sets `truncated`. */ truncated: boolean; /** First open/write failure, reported rather than thrown. */ error?: string; } export interface RunEventLogHandle { /** Where the log lives; empty string for the no-op handle. */ readonly path: string; /** Appends a raw stdout chunk verbatim. Never throws. */ push(chunk: Buffer | string): void; /** Terminates a final line that arrived without its newline. Never throws. */ flush(): void; /** Closes the fd and reports what was written. Never throws. */ close(): RunEventLogStats; } /** * Filename-safe form of a run id. The id reaches a path, so it is sanitized * rather than trusted: anything outside `[A-Za-z0-9_-]` (`/`, `.`, `..`) becomes * `-` before it can traverse out of the sessions dir. */ export declare function runEventLogSegment(runId: string): string; /** * Log path for one run. Keys on the session's canonical `normalizedId` exactly * as the transcript sidecar does, so both files for a session sort together and * neither can be derived from a raw route id. */ export declare function runEventLogPath(projectDir: string, sessionId: string, runId: string): string; /** * Opens (creating) the run's log and returns a write-through handle. * * The file is created up front, so a run that produces no stdout at all — a * child killed at its deadline before it spoke, an immediate non-zero exit — * still leaves the log behind. `undefined` target yields the no-op handle, which * is how the runner stays usable without a project dir (tests, and any caller * that does not want a log). */ export declare function openRunEventLog(target: RunEventLogTarget | undefined): RunEventLogHandle; export interface RunFoldResult { /** Turns this call appended to the sidecar. */ appended: number; /** The sidecar already carried this run's turns — nothing was written. */ alreadyFolded: boolean; /** An assistant TEXT turn for this run is in the sidecar (from either call). */ assistantTextFolded: boolean; /** * The runtime-native session id a driver normalizer harvested from the log, * when one folded it and any line carried an id. This is what lets a first * turn lazily mint the session's `runtimeSessionId` — the caller persists it * after the run settles. Absent for the built-in claude parser (claude ids * reach the record through metadata, not this fold) and when no line had one. */ runtimeSessionId?: string; } /** * Options for one fold. Everything is optional: absent, the fold behaves * exactly as it always has — the built-in claude stream-json parser. */ export interface FoldRunEventLogOptions { /** * The write-target's runtime type. When a one-shot driver adapter is * registered for it (run-driver.ts), the log folds through that adapter's * normalizer instead of the claude parser — an opencode log's lines are * `{type, sessionID, part}` objects no `assistant` event parser can read. */ runtimeType?: SessionRuntimeType; } /** * Folds a run's event log down into the segment's transcript sidecar: assistant * text plus one turn per `tool_use`, appended through `appendSessionTurn` in * stream order, each tagged with the run id. * * Idempotence mechanism: the fold's OWN OUTPUT is the marker. Every appended * turn carries `run: `, and a fold refuses as soon as the sidecar holds * any turn tagged with that id. This was chosen over a separate marker (a * `foldedAt` on the run record, a `.folded` sentinel file) precisely because a * separate marker has a window: a crash between "turns appended" and "marker * written" would re-fold and duplicate the whole run on the next settle. Here * there is no window — the evidence and the mark are the same bytes. The cost * is at-most-once rather than exactly-once under a crash MID-fold (the turns * already appended are tagged, so the remainder is never completed), which is * the safe direction: a short transcript, never a doubled one. * * Never throws — a fold that cannot read the log or write the sidecar reports * an empty result and the settle continues. */ export declare function foldRunEventLog(projectDir: string, sessionId: string, runId: string, options?: FoldRunEventLogOptions): Promise; /** * Keeps the newest `keep` RUNS for one session and deletes all their files — * event log AND the snapshot/changes sidecars that live beside it. * * Called at EVERY settle (after the fold), so the run that just settled is * always among the survivors and the sessions dir is bounded at `keep` runs per * session no matter how many runs a session accumulates. "Every settle" means * the startup orphan sweep too (`settleOrphanedRuns`): a server that dies * mid-run never reaches the route's write-back, so without that call site a * session whose runs are always interrupted grows one capped log per run * forever. `keep: 0` drops them all — what session deletion uses. * * Preservation unit is the RUN, not the file: one run's log + snapshot + * changes manifest are born, age and die together (grouped by the run segment * in the filename), so the review surface for a retained run always has its * baseline and its diff, and a pruned run takes all three with it. * * Never throws; returns how many files it removed. */ export declare function pruneRunEventLogs(projectDir: string, sessionId: string, keep?: number): Promise; //# sourceMappingURL=run-event-log.d.ts.map