@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
69 lines • 3.6 kB
TypeScript
/**
* File-per-job store for durable scheduled prompts.
*
* Layout under `<agentDir>/schedule/` (the directory a file sits in IS its state):
*
* pending/<id>.json the job, waiting for its next due time
* firing/<id>@<n>~<owner>.json occurrence <n> claimed by runner <owner> (`<pid>-<processStartMs>`)
* failed/<id>@<n>.json occurrence <n> whose delivery failed or whose runner died mid-delivery
* cancelled/<id> tombstone: the job was cancelled; nothing may re-arm or deliver it
* runners/<pid>.json lease + heartbeat of each live `senpi schedule run --watch`
*
* State transitions (claim, re-arm, settle, cancel) live in `occurrences.ts`.
*
* Every write is atomic (0600 temp file + rename, directories 0700). A runner claims an occurrence
* by renaming `pending/<id>.json` to its occurrence record: rename is atomic on one filesystem, so
* among racing runners exactly one wins and the others see ENOENT. A recurring job is re-armed in
* `pending/` right after the claim, BEFORE delivery, so a runner crash can lose at most the one
* in-flight occurrence, never the schedule. Cancellation writes the tombstone first and every
* claim and re-arm re-checks it afterwards, so a cancelled job can never come back.
*/
import { type ScheduledJob } from "./types.ts";
export type ScheduledJobState = "pending" | "firing" | "failed";
export declare function scheduleDir(agentDir: string): string;
export declare function isScheduledJobId(id: string): boolean;
export declare function isFileSystemError(error: unknown, code: string): boolean;
export declare function ensureDir(dir: string): Promise<void>;
/** Atomic replace: a hidden 0600 temp file next to the target, then rename over it. */
export declare function writeAtomic(filePath: string, contents: string): Promise<void>;
export declare function serialize(job: ScheduledJob): string;
/** Identity of the runner process that owns an occurrence record. */
export interface RunnerIdentity {
readonly pid: number;
readonly processStartedAtMs: number;
}
export interface JobRecord {
readonly state: ScheduledJobState;
readonly job: ScheduledJob;
/** Store-relative path, e.g. `firing/sch_x@2~123-456.json`. */
readonly file: string;
/** Occurrence number for `firing` and `failed` records. */
readonly occurrence?: number;
/** Claiming runner of a `firing` record. */
readonly owner?: RunnerIdentity;
}
export interface InvalidJobFile {
readonly state: ScheduledJobState;
readonly file: string;
readonly error: string;
}
export interface JobListing {
readonly jobs: readonly JobRecord[];
readonly invalid: readonly InvalidJobFile[];
}
export declare function readJobFile(path: string, expectedId: string): Promise<ScheduledJob>;
/** Every record in every state, sorted by due time. Unreadable files are reported, never dropped. */
export declare function listScheduledJobs(dir: string): Promise<JobListing>;
export interface NewScheduledJob {
readonly sessionId: string;
readonly sessionFile: string | null;
readonly cwd: string;
readonly prompt: string;
readonly dueAt: number;
readonly everyMs: number | null;
}
export declare function createScheduledJob(dir: string, input: NewScheduledJob, now: number): Promise<ScheduledJob>;
export declare function tombstonePath(dir: string, id: string): string;
export declare function isCancelled(dir: string, id: string): Promise<boolean>;
export declare function runnersDir(dir: string): string;
//# sourceMappingURL=store.d.ts.map