/** * The bus as its owner and its callers name it: the shapes, and nothing that runs. * * `runtime.ts` is the lifecycle and the operations, and it was carrying the two interfaces * that describe them as well — a hundred lines of declaration over a module already past the * line bar. They live here for the same reason the store's shapes live in `types.ts`: a * shape is read by `commands.ts`, by the request modules, and by a test, and none of them * needs the runtime to be loaded to say what the bus answers. * * Types only: no value, no import that is not a type, and so nothing on the registration * graph. */ import type { ExtensionContext, SessionShutdownEvent, SessionStartEvent } from "@earendil-works/pi-coding-agent"; import type { HostIdentity } from "../../lib/session-files.ts"; import type { LivenessFn, StartTokenFn } from "../../lib/spawn.ts"; import type { RepeatTimer } from "../../lib/timer.ts"; import type { WatchingState } from "./mail-watch.ts"; import type { BusStore } from "./store.ts"; import type { BusMessage, ClaimConflict, CleanResult, DeliveryFailure, DropResult, ForgetResult, InboxPeek, SessionStatus, } from "./types.ts"; export interface SessionBusOptions { now?: () => number; /** Test seam: use this bus directory instead of the agent-dir layout. */ dir?: string; /** * Test seam: the store this runtime reads and writes. * * The real one is built from `dir`. A case that needs a failure *inside* a call — the removal in * `leave`, rather than the door — hands a store that delegates to the real one and throws there. */ store?: BusStore; /** Test seam: the process id this runtime registers as, so two can share one session id. */ pid?: number; /** Test seam: whether a pid is running, so a suite never depends on the host's pids. */ alive?: LivenessFn; /** Test seam: the start-time seam the record is stamped with. */ startTokenFn?: StartTokenFn; /** * Test seam: this machine's identity, as `presence/host.json` holds it. * * The product reads it, making it on first use. A caller that names its own `dir` names its host * too; one that names neither writes records and claims an older build would have written. */ host?: HostIdentity; /** Test seam: skip git and use this repository identity, saying whether git would have resolved it. */ repo?: { root: string; key: string; fromGit?: boolean }; /** Heartbeat interval; 0 disables it (tests). */ heartbeatMs?: number; /** Inbox-watch poll interval; 0 disables it (tests). */ watchMs?: number; /** * Test seam: where the heartbeat's failure and recovery lines go. * * The default is `process.stderr` — the one place a session with no tool call open can read them. */ stderr?: (line: string) => void; /** Test seam: the repeating timer behind the heartbeat and the poll. */ interval?: RepeatTimer; sessionTtlMs?: number; messageTtlMs?: number; } /** * What leaving the bus gave up: the name this session was addressed by, and the claims it held. * * Both are answers rather than nulls because a caller reports them: a person who left should read * which paths went back, and no label is a session that was on no bus to leave. */ export interface LeaveOutcome { /** The label peers addressed it by; absent when it was on no bus to leave. */ label?: string; /** The paths that went back to nobody. */ released: string[]; } export interface SessionBus { /** * Load this session's records, adopting the row this session already has, or serving the one a * `/new` left behind. * * The event is optional because a caller that is not the host's lifecycle has no reason to * give: a join request, a suite, a command. Without one, nothing is served and a record is * minted, which is what every one of those callers wants. */ start(ctx: ExtensionContext, event?: SessionStartEvent): Promise; /** * Release this session's resources. * * `"new"` and `"reload"` keep the record and its claims, because the process goes on serving them * under a new transcript or a re-evaluated runtime; every other reason ends the session and removes * what it held. */ stop(event?: SessionShutdownEvent): Promise; /** * Leave the team without ending the process: the row goes, its claims go, the watch stops. * * The answer says what was given up. The start is kept — the store, the record's id, and what a * card names — so a later `join` re-adds the same row under the same id rather than a stranger's; * `stop` stays the host's door and does this at the end of a process. * * Refused when another pane with this session id owns the row — a `/resume` in a second pane * registers one id twice, and the one leaving must not strip the one still working — and a failed * write leaves the session as it was rather than as a row nobody beats. */ leave(): Promise; status(): Promise; /** The live bus, read only: no record refresh, no prune. */ snapshot(): Promise; /** This session's unread mail without consuming it, and the files this build cannot read. */ peek(): Promise; /** * The files still in this inbox, as the last poll read them; 0 while the watch is off. * * A turn-start line reads this rather than the inbox: the count is already paid for by the * watch, and a session that is not listening has nothing to say. */ inboxDepth(): number; /** * Remove unaccounted mail by message id — the one door `/sessions drop` opens. * * No tool action calls this: a person clears evidence, a session does not. The store refuses any * id a record's inbox holds, so a mistaken id costs a sentence rather than somebody's mail. */ dropUnaccounted(ids: readonly string[]): Promise; /** * Clear this session's own inbox of the mail older than `olderThanMs` — the door `clean` opens. * * No tool action calls this: the mail reached this session and went unread, so a person clears * it. It never joins, and it walks one inbox — this one — so a peer's mail is out of reach by * construction rather than by a guard. */ cleanInbox(olderThanMs: number): Promise; /** * Free the names of records the bus reads as `gone`: their claims go, their inbox stays. * * A person's door — no session's tool call may take a name a person has not released — and the * answer is one line per name: what went, or why it stayed. */ forget(names: readonly string[]): Promise; /** Release a claim by its stored repository-relative path. */ releaseStored(path: string): Promise; send(input: { to: string; text: string; kind?: string; /** A name for this message: a resend of the same body folds into the copy still waiting. */ key?: unknown; /** The message or ask this one answers, as one word. */ replyTo?: unknown; /** * What the message is about: the sender's own spelling for it — a card's `#673`, a branch, a * phrase — in one line of at most 64 characters. */ about?: unknown; paths?: unknown; }): Promise<{ delivered: string[]; failed: DeliveryFailure[]; messageId: string }>; inbox(limit?: number): Promise; /** * Show up to `limit` messages without deleting them, marking what it shows as told. * * The door for a burst a notice only previewed: the count falls because the mail reached the * model, and `inbox` stays the one call that removes a body. */ read(limit?: number): Promise; claim(paths: unknown): Promise<{ claimed: string[]; conflicts: ClaimConflict[] }>; /** * Release this session's claims, or — with `holder` — move a live peer's claims to this session. * * The second form is the seat's move, and it answers in the same two lists: what changed hands * and what the named peer did not hold (#835). */ release(paths: unknown, holder?: string): Promise<{ released: string[]; notOwned: string[] }>; /** * Join the team: join the bus, name this session when given a label, and listen for * mail unless `listen` is false. One action, because they are one act — a session on * the bus that will not hear mail is on it, but not on the team. */ join(input: { label?: string; wake?: boolean; listen?: boolean; }): Promise<{ label: string; watching: WatchingState }>; /** Whether the armed watch asked for a triggered turn; the settle retry reads it. */ wantsWake(): boolean; /** A run began: stamp the record, so a peer reads this session as at work. */ turnStart(): Promise; /** The run settled: take the stamp off, aborted or not. */ turnSettled(): Promise; /** Read the inbox without consuming it, queueing what is new; no-op when unarmed. */ poll(): Promise; /** Mail that arrived and has not been reported yet, oldest first. */ pending(): BusMessage[]; /** Drop mail from the pending queue, once reported or consumed. */ markReported(ids: readonly string[]): void; /** Set what to call when new mail arrives and this session asked to be woken. */ set onWake(callback: (() => void) | undefined); }