UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

112 lines • 7.65 kB
import { type LandingOutcome, type StampClockOptions } from "./move-landing.ts"; import { type DeclineReason, type FailReason, type MoveAnswer, type MoveRecord } from "./move-record.ts"; import type { MoveId } from "./ownership.ts"; export { type DeclineReason, type FailReason, type MoveAnswer, MoveFencedError, type MoveRecord, type MoveStatus, parseMoveStatus, } from "./move-record.ts"; /** How long a ready move waits for its bundle before it is declined as expired. */ export declare const READY_TTL_MS: number; export type MoveLedgerOptions = StampClockOptions; /** What a receive brings to `beginReceive`. */ export interface BeginReceiveOptions { /** The folder the import lands in, resolved as the import resolves it. The record keeps it as `cwd`. */ readonly cwd?: string; } /** * The receiving device's record of every move it was offered. Asking about a move is itself a fence: a move this * device never heard of is recorded as declined BEFORE the answer leaves, so a later import of it is refused and the * sender may safely take its session back. Every writer and the import's final rename share one lock per move. */ export declare class MoveLedger { #private; readonly dir: string; private readonly clock; constructor(dir: string, options?: MoveLedgerOptions); /** * Says this device can take the move, for `READY_TTL_MS`. Sending ready again keeps the first window; a move already * receiving or accepted stays so, and one already answered otherwise is refused. */ prepare(moveId: MoveId, folder?: string): Promise<MoveRecord>; /** * Refuses an import into a folder other than the one a ready move was readied for, before the import's own * repository check can answer that folder's repository instead (senpi#6435). It writes nothing and takes no lock: * a record is replaced whole, so one read of it is consistent, and an import must not wait here behind another * import's commit of the same move (senpi#6413). `beginReceive` decides again under the lock. */ requireReadiedFor(moveId: MoveId, cwd: string): Promise<void>; /** * Starts receiving a verified bundle into `target`, the session file it will land as. Only a move this device readied * (and whose window is still open) may start; a bundle for a move it never readied is declined for good * (`chunk_without_ready`). The first receive records its target and the move lands only there: a receive for another * target is fenced while the move is receiving, and an accepted move never lands again, at any target (a deleted copy * is gone). A receiving record with no target (an older build wrote it) is fenced too: where it landed is unknown, * and a file it already landed is confirmed by the import's same-file check, which runs before this. */ beginReceive(moveId: MoveId, target: string, options?: BeginReceiveOptions): Promise<void>; /** Declines a ready move whose window has passed; any other record is returned unchanged. */ expire(moveId: MoveId): Promise<MoveRecord | undefined>; /** * The signed answer a sender gets. `movedIn` is whether the session file here records the move, `revoked` whether it * records its revoke too (a revoke whose record write was lost still answers revoked). `landed` reads the file at the * recorded target, so a step a crash interrupted is settled from the files first (`reconcile`). */ answerStatus(moveId: MoveId, here: { readonly movedIn: boolean; readonly revoked?: boolean; readonly landed?: (target: string) => LandingOutcome | Promise<LandingOutcome>; }): Promise<MoveAnswer>; /** * Throws unless this process still holds the move's lock. For what a commit's `prepare` removes or copies: each is a * write under this lock, and a holder that lost it must stop before the next one. */ assertHeld(moveId: MoveId): void; /** Cancel, expiry or a refused chunk: only before the bundle was verified and receiving began, never after. */ decline(moveId: MoveId, reason: DeclineReason): Promise<MoveRecord>; /** * A verified import that could not finish; it may end a receiving move but never an accepted one. `unlessLanded` is * asked under the move's lock, right before the write: when this move's session file landed (an overlapping import's * commit may land it while this call waits for the lock), nothing is recorded and the current record is returned. */ fail(moveId: MoveId, reason: FailReason, options?: { readonly unlessLanded?: () => boolean | Promise<boolean>; }): Promise<MoveRecord | undefined>; /** * Runs the import's commit (the atomic rename into `target`) under the move's lock. The record is stamped * `committing` before the rename, so a crash or failure on either side of it never lets the move land again; the * file at the target then decides (`confirmLanded`, `reconcile`). Only the recorded target may be renamed into. * `prepare` runs before the stamp: whatever it does or refuses lands nothing, so a refusal there still records failed. * `rename` is handed `stampStands`, to call right before the step that lands the file: whatever comes before that * step (waiting for the session's lock, rewriting the file) can outlast the lock's stale bound too. */ commit<T>(moveId: MoveId, target: string, rename: (stampStands: () => Promise<void>) => T | Promise<T>, prepare?: () => void | Promise<void>): Promise<T>; /** * The import found this move's file already landed at `target`: that file proves the commit, so a record a crash * left `receiving`, committing or lost becomes accepted there, and revoked when the file is (`revoked`). A target * other than the recorded one is fenced; an accepted record only gains a revoke mark it was missing. */ confirmLanded(moveId: MoveId, target: string, revoked: boolean): Promise<void>; /** * Settles a step a crash interrupted, from the file at the recorded target: a committing record becomes accepted * (revoked if the file is) when its file landed and failed when it did not, once its stamp is old enough that no * stalled commit can still land it (`COMMIT_SETTLE_AFTER_MS`), and an accepted record whose file is sealed gains its * revoke mark. Without `landed`, or without a recorded target, the record is returned as it is. */ private reconcile; /** * Records the revoke of this move's import on its accepted record (`revoked: true`; the state stays `accepted`). The * import committed, so a record lost or left `receiving` by a crash becomes accepted and revoked; a repeat changes * nothing, and any other record is left alone. */ markRevoked(moveId: MoveId): Promise<void>; /** * Records a final answer unless one is already there: once a sender may have heard `accepted`, `declined` or * `failed`, it never changes. Reads through `current()`, so a ready move whose window passed answers `expired`. * `keeps` names the open states this transition must also leave alone. A record stamped `committing` is never * settled here: its rename may have landed a copy, so only the file at its target settles it (`reconcile`). */ private settle; /** `settle` for a caller that already holds the move's lock. */ private settleLocked; /** The record, with a ready move whose window has passed declined as expired first (on disk, under the lock). */ private current; private locked; } //# sourceMappingURL=move-ledger.d.ts.map