/** * The task record file: one session's Task records — both kinds in one * document — written atomically and read with a previous-generation fallback. * * The file is the only place the panel's history comes from after a restart, so * it is written for machines and read defensively: anything that does not parse * as a record set is corruption, not an exception to throw at the user. * * @author mystery4f <40482524+mystery4f@users.noreply.github.com> */ import { rename } from "node:fs/promises"; import type { Task } from "./task.js"; /** * On-disk schema version. One version covers every kind; a change that older * code could not read any more bumps it. * * Version 2 added `updatedAt` (ADR-0010). Version 3 split the old `loop` kind * into `prompt-loop` and reserved `cmd-loop`, so a version 1 or 2 document still * loads: a record from before recency takes its own `createdAt`, and an old * `loop` reads as an prompt loop. Version 4 implemented the `cmd-loop` arm with its * fire history and added the parent link a fire child carries. Version 5 adds * the `chain` arm and the three statuses its steps use (`waiting`, `skipped`, * `stopped`), and separates the parent link from the fire link, so a step can * name its chain without claiming to be a fire. Every older version still loads * — each one only added arms, fields and statuses, so nothing a reader knew * before is spelled differently now — and saving writes version 5. */ export declare const TASK_FILE_VERSION = 5; /** One session's task records as they are stored. */ export interface TaskFile { readonly version: number; readonly tasks: readonly Task[]; } /** * The records of one generation, or `undefined` when the document is unusable. * Exported for the retention sweep, which must tell an empty session from an * unreadable one but must not repair what it reads. */ export declare function parseTaskFile(text: string): Task[] | undefined; export interface RenameRetryOptions { readonly attempts?: number; readonly delayMs?: number; /** Seams for the retry's own test; production uses the real rename and timer. */ readonly renameImpl?: typeof rename; readonly sleep?: (ms: number) => Promise; } /** * Rename a file into place, waiting out a Windows share that is still open. * * POSIX renames over a file another process is reading without complaint; Windows * refuses with `EPERM` or `EBUSY` until every handle on that file closes — a * reader polling the record, a virus scanner, another pi session. Without a retry * the writer sees a generation that never lands, which is how `task-persistence` * timed out on the Windows runner. The delay is short because the holder is * usually a read that started microseconds ago. * * Exported for the retry's own test; no caller outside this module needs it. */ export declare function renameWithRetry(from: string, to: string, options?: RenameRetryOptions): Promise; /** Everything a caller may tune about the store; production uses the defaults. */ export interface TaskStoreOptions { /** Clock seam for tests; dates the records found still running at load. */ now?: () => number; } /** * The records of one session as files next to its output: `tasks.json` plus the * previous generation beside it. */ export declare class TaskStore { private readonly directory; private readonly now; /** * The last generation this store read or wrote. On the next write it becomes * `.prev`, so the fallback copy is always a document that once parsed. */ private lastGeneration; /** Queued writes; each update waits for the one before it. */ /** The write in flight, if any; a save joins it instead of queueing behind it. */ private writes; /** The newest generation waiting to be written; older ones are superseded. */ private pending; /** * The directory, made on first use. A session that never records anything * leaves the filesystem exactly as it was, so merely opening pi in a project * does not create `.pi/tasks/` for nobody to read. */ private directoryReady; constructor(directory: string, options?: TaskStoreOptions); /** * Every record the file holds, oldest first, or an empty list before the first * write. A corrupt current file falls back to the previous generation; when * neither is readable the session simply has no history, and pi keeps running. */ load(): Promise; /** * Make the session directory, once. The write paths call this themselves, so * a caller only needs it when it writes into the directory by another route — * a process's output file is opened by the spawn, not by {@link save}. * * A failure is not remembered: the next attempt tries again, and the records * in memory are already correct. */ ensureDirectory(): Promise; /** * Replace the file with this set of records, keeping the last good generation * as `.prev`. Updates are queued, so two writes in a row cannot interleave * their temp files and hand each other a half-written document. * * Persistence is best effort: a full disk must not fail a task that is already * running, so a write error is swallowed rather than thrown at the session. */ save(tasks: readonly Task[]): Promise; /** Write the newest pending generation until none is left. */ private drain; private writeGeneration; private readGeneration; }