@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
55 lines • 3.89 kB
TypeScript
/**
* The brain's `egress_call` ledger (row CK9; custody design 4.5). Every write is one admitted mutation supplied by the
* host wrapper, which fences it (epoch, admission seq, instance generation) and commits it atomically; the ledger only
* builds the writes. W4 (row + request chunks) precedes any dial, W4' (`hooked`) replaces the request and sets
* `hooksApplied` in one mutation, `ended` (row + the assembled message in result chunks) is durable before the terminal
* event, and the step commit (W9) closes the row and deletes its chunks with `closeCallWrites` inside its own mutation.
* A cancelled step (W14) is never dialed again, so W14 deletes the live row's chunks too: nothing else would.
*/
import type { DeviceId, ProviderId } from "@earendil-works/pi-agent-core";
import type { DurableStore, StoreWrite } from "@earendil-works/pi-agent-core/conformance";
import type { AssistantMessage } from "@earendil-works/pi-ai";
import { type EgressCallEnd, type EgressCallRow, type VariantRecord } from "./ledger-schema.ts";
/** Runs `build` inside the host's admission slot and commits the writes it returns as one fenced, atomic mutation. */
export type AdmittedMutation = (build: () => Promise<readonly StoreWrite[]>) => Promise<void>;
export type NewCallRow = Omit<EgressCallRow, "state" | "requestChunks" | "result">;
export interface VariantMismatch {
readonly providerId: ProviderId;
readonly deviceId: DeviceId;
readonly stepId: string;
readonly headDigest: string;
readonly liveAtMismatch: string | undefined;
}
export interface EgressLedger {
/** Every row of the step, oldest attempt first. */
rowsForStep(stepId: string): Promise<EgressCallRow[]>;
/** The stored request text, byte-identical to what was admitted (or what `hooked` replaced it with). */
readRequest(row: EgressCallRow): Promise<string>;
/** The assembled message of an `ended` row whose result is a message. */
readMessage(row: EgressCallRow): Promise<AssistantMessage>;
/** W4. With `previous`, the previous row is retired (`ended` -> `closed`) and its request chunks deleted, atomically. */
dispatch(next: NewCallRow, requestJson: string, previous?: EgressCallRow): Promise<EgressCallRow>;
/** W4': the post-hook request replaces the stored one and `hooksApplied` becomes true, in one mutation. */
recordHooked(row: EgressCallRow, requestJson: string): Promise<EgressCallRow>;
end(row: EgressCallRow, result: EgressCallEnd): Promise<EgressCallRow>;
/**
* The hand refused `row`'s start, so nothing ran: the row and its chunks are replaced by the same call under a new
* `callId` (same attempt, same stored request) in one mutation. A duplicate `start{callId}` is a result query (4.3),
* so the refused callId can never be started again.
*/
replaceRefused(row: EgressCallRow, callId: string): Promise<EgressCallRow>;
/** The row is superseded; a definition mismatch records the device variant in the same mutation. */
supersede(row: EgressCallRow, variant?: VariantMismatch): Promise<EgressCallRow>;
/** W14: the step is cancelled (and its live row with it); a cancelled step is never dialed again. */
cancelStep(stepId: string, row: EgressCallRow | undefined): Promise<void>;
isCancelled(stepId: string): Promise<boolean>;
recordVariant(variant: VariantMismatch): Promise<void>;
variants(providerId: ProviderId): Promise<ReadonlyMap<DeviceId, VariantRecord>>;
}
/** The W9 part the ledger owns: the step's row becomes `closed` and its request chunks are deleted (4.5 retention). */
export declare function closeCallWrites(row: EgressCallRow): StoreWrite[];
export declare function createEgressLedger(options: {
readonly store: Pick<DurableStore, "getDocument" | "scanDocuments">;
readonly mutate: AdmittedMutation;
}): EgressLedger;
//# sourceMappingURL=ledger.d.ts.map