@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
58 lines • 3.09 kB
TypeScript
/**
* File-level primitives for the terminal lease. On a filesystem with hard links a lease file is
* never visible half-written: it is written to a private temp file first and then published with
* `link` (exclusive: fails with EEXIST when a lease exists) or `rename` (replace). Every removal of
* a lease, by an acquire or by the GC, goes through `reclaimInspected`: under an exclusive
* `<lease>.lock`, and only while the file still holds exactly the record that was judged stale, so
* a fresh lease another process just published is never deleted. A reclaim lock records the
* holder's identity (pid, boot and start instant); it is broken only when that process is gone or its
* pid now belongs to another process, and only if it still holds the record that was judged stale.
*/
export declare function errorCode(error: unknown): string | undefined;
export declare function unlinkIfPresent(path: string): Promise<void>;
export declare function readLeaseText(path: string): Promise<string | undefined>;
/** Options for publishing a lease file. */
export interface PublishOptions {
/** File mode of the published file (before umask); defaults to 0o666 like `open`. */
readonly mode?: number;
}
/** Publish `content` at `path` only if nothing is there; throws EEXIST otherwise. Never half-written. */
export declare function publishExclusive(path: string, content: string, options?: PublishOptions): Promise<void>;
export declare function publishReplace(path: string, content: string, options?: PublishOptions): Promise<void>;
export interface LockProbes {
readonly isProcessAlive?: (pid: number) => boolean;
readonly readProcessStartMs?: (pid: number) => Promise<number | undefined>;
}
/** Who holds a reclaim lock: the same identity a lease records, so a reused pid is never mistaken for it. */
export interface ReclaimLockHolder {
readonly pid: number;
readonly bootAtMs: number;
readonly processStartedAtMs: number;
}
type LockState = {
readonly state: "free";
} | {
readonly state: "held";
readonly holder?: ReclaimLockHolder;
readonly unproven?: true;
} | {
readonly state: "stale";
readonly raw: string;
};
/** Whether a reclaim lock is held by the very process that wrote it; a dead or reused pid is stale. */
export declare function reclaimLockState(lock: string, probes?: LockProbes): Promise<LockState>;
/** Remove a stale lock only if it still holds `raw`; a lock someone took meanwhile is put back. */
export declare function breakStaleLock(lock: string, raw: string): Promise<void>;
export type ReclaimOutcome = {
readonly outcome: "removed" | "changed";
} | {
readonly outcome: "busy";
readonly holder?: ReclaimLockHolder;
};
/**
* Remove the lease at `path` only if it still holds `inspected`: `removed`, `changed` (another
* process replaced it meanwhile; left alone), or `busy` with the live process reclaiming it now.
*/
export declare function reclaimInspected(path: string, inspected: string, probes?: LockProbes): Promise<ReclaimOutcome>;
export {};
//# sourceMappingURL=lease-file.d.ts.map