@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
119 lines • 6.86 kB
TypeScript
/**
* Atomic, idempotent admission of a message another session sent this one.
*
* One synchronous call decides and acts: a delivery either enters the runtime (a started turn, the
* follow-up queue or the steering queue - the same queues a user's queued input uses) exactly once,
* or nothing happens. The ledger is the process-lifetime answer to "does this runtime hold, or has it
* written, delivery X": a delivery is `pending` from admission until its transcript entry is
* persisted, then `emitted`. A second admission of an id in either state is `already_admitted`.
* A delivery whose entry the session file refused is `failed` (with the error): it is no longer held,
* so it blocks nothing, and it stays with its sender. It is admitted again only once the run that
* refused it has settled and the file's last write succeeded: a redelivery then starts or joins a
* later run, whose start drops the refused copy from the model context, and it never loops against
* a file that still refuses writes.
*/
import type { AgentMessage } from "@earendil-works/pi-agent-core";
import { type AdmissionHoldReason, type AdmitExternalMessageInput, type AdmittedDeliveries, type ExternalAdmissionResult, type ExternalDeliverAs, type SessionAdmissionGate, type SessionControlDeliveryDetails, type SessionControlRemoteOrigin } from "./extensions/session-control-types.ts";
import type { HoldKind } from "./engine-turn-hold.ts";
import type { CustomMessage } from "./messages.ts";
import type { HistoryReducer } from "./session-history-reduce.ts";
import { type InputTurnMark } from "./turn-origin.ts";
export type SessionControlDeliveryMessage = CustomMessage<SessionControlDeliveryDetails>;
export interface ExternalAdmissionPort {
/** A run is active, or a prompt has claimed its start: new input queues behind it. */
readonly isBusy: () => boolean;
/** The holds a delivery cannot release, so a start is kept as a held notice instead (senpi#6490); empty when none. */
readonly heldBy: () => readonly HoldKind[];
/** Synchronously enqueues into the runtime's own queue for `lane`. */
readonly enqueue: (message: SessionControlDeliveryMessage, lane: ExternalDeliverAs) => void;
/** Starts a turn carrying `message`; settles when that run is over, whether or not it wrote the entry. */
readonly start: (message: SessionControlDeliveryMessage) => Promise<void>;
}
export interface EditorHoldState {
readonly hold_reason?: AdmissionHoldReason;
readonly revision: number;
}
/**
* What the engine itself adds to a delivery an RPC connection admits (senpi#6233): the remote device that connection
* speaks for, the thread-chain hop it was admitted at, and the origin mark its turn takes. `pi.session` never passes
* one, so neither an extension nor a caller's record can write these.
*/
export interface DeliveryStamp {
readonly remoteOrigin?: SessionControlRemoteOrigin;
readonly chainCount?: number;
readonly turnMark?: InputTurnMark;
}
export declare class ExternalAdmission {
private turnEpochValue;
private readonly pending;
private readonly emitted;
private readonly failed;
private fileTakesWrites;
private readonly emittedListeners;
private editorSource;
private closedReason;
private readonly port;
private readonly inputHolds;
private readonly settledListeners;
constructor(port: ExternalAdmissionPort);
get turnEpoch(): number;
/** Why admission is closed (a release or a graceful close took the session); `undefined` while it admits. */
get closed(): string | undefined;
get hasInputWork(): boolean;
/** Called when an agent run begins; a steer names the epoch it was meant for. */
beginTurn(): void;
/** The terminal whose submitted-but-untaken input holds admissions; `undefined` detaches it (never held). */
setEditorSource(source: (() => EditorHoldState) | undefined): void;
/**
* Holds admission while user input is on its way into the runtime: from `prompt()` entry until the
* prompt reports a disposition or returns. A `command` hold (an extension or built-in command that
* may submit text after an await) also ends as soon as a prompt the command submitted - one from
* an extension (`submittedByCommand`) that began while it was open - is accepted. Ending a hold is
* idempotent; the last one to end notifies `onInputsSettled`.
*/
beginInput(options?: {
readonly command?: boolean;
readonly submittedByCommand?: boolean;
}): InputHold;
onInputsSettled(listener: () => void): () => void;
onEmitted(listener: (deliveryId: string) => void): () => void;
/** Durable custody will replay this delivery: reserve its id before asynchronous recovery or gateway admission. */
restoreQueued(deliveryId: string, isOwned: () => boolean): void;
/**
* From here on every `admit` throws `reason` and changes nothing: the runtime is being handed to
* another writer, so the delivery stays with its sender and is delivered there instead.
*/
close(reason: string): void;
/** Undoes `close` when the hand-over it was closed for did not happen. */
reopen(): void;
gate(): SessionAdmissionGate;
admit(input: AdmitExternalMessageInput, stamp?: DeliveryStamp): ExternalAdmissionResult;
list(): AdmittedDeliveries;
/** A message's transcript entry was written: the file takes writes; a delivery's own entry makes it emitted. */
observePersisted(message: AgentMessage): void;
/**
* The session file refused a delivery's entry: the delivery is settled as failed with `error` and is
* no longer held, so its start stops counting as busy and later deliveries start. It stays with its sender.
*/
observeRefused(message: AgentMessage, error: string): void;
/**
* A run settled: its refused messages leave the model context at the next run's start, so the failed
* deliveries may be admitted again - unless the file's last write was refused too.
*/
observeRunSettled(): void;
/** Cleared carriers are no longer held; custody not yet enqueued for recovery still owns its delivery. */
dropQueued(): void;
private releaseRestored;
private endHold;
private isBusy;
}
export interface InputHold {
/** The runtime took the input: it started a turn, was queued, or was handled. */
accepted(): void;
/** The submission ended without being accepted (it threw, was cancelled, or never submitted). */
end(): void;
}
/** Every delivery id the transcript holds an entry for: what answers for the process-lifetime ledger after a restart. */
export declare const writtenDeliveryIds: HistoryReducer<Set<string>>;
export declare function deliveryIdOf(message: AgentMessage): string | undefined;
//# sourceMappingURL=external-admission.d.ts.map