UNPKG

@code-yeongyu/senpi

Version:

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

151 lines • 7.05 kB
/** * The `session_status` record and the ONE pure function that derives it. * * Every status a subscriber sees - the snapshot in the `subscribe_sessions` answer and every delta after * it - is computed by `deriveSessionStatus` from three inputs the host already holds: the registry entry * (lifecycle state, attachments, identity), the session's own runtime view (streaming, compacting, model, * queues, pending questions) and the fanout's pending question frames. Nothing here reads a clock, a * socket or a registry, so the derivation table is pinned by unit tests rather than by timing. */ import type { SessionKind } from "../../core/extensions/types.ts"; import type { RetryWait } from "../../core/retry-wait.ts"; import type { SettledFields, ThreadSettledBy } from "../../core/session-settled.ts"; import type { RpcSessionState } from "./session-registry-types.ts"; export type SessionStatusValue = "opening" | "idle" | "running" | "waiting_input" | "compacting" | "unloaded" | "closed"; export type SessionStatusCause = "open" | "prompt" | "agent_end" | "agent_idle" | "abort" | "question" | "compaction" | "set_model" | "queue" | "attach" | "detach" | "replace" | "park" | "close" | "context" | "rename" | "archive" | "settle" | "retry"; export type SessionStatusActor = "client" | "agent" | "host"; /** A session's published labels: the open-time context overlaid with every `set_session_context` patch. */ export type PublishedSessionContext = Readonly<Record<string, string | number | boolean>>; /** Why a session's routing handle ended; set once, from the record that sealed it. */ export interface SessionStatusTerminal { readonly status: "unloaded" | "closed"; readonly reason?: string; } /** What the registry entry contributes. */ export interface SessionStatusEntryView { readonly state: RpcSessionState; readonly kind: SessionKind; readonly cwd: string; readonly attachments: number; /** Milliseconds since the Unix epoch of the holder's last observed activity, if known. */ readonly lastActivityAt?: number; readonly durableSessionId?: string; readonly sessionPath?: string; readonly name?: string; /** The session file's archived flag; absent reads as not archived. */ readonly archived?: boolean; /** The session file's Done flag; absent reads as not settled. */ readonly settled?: SettledFields; readonly context: PublishedSessionContext; /** * The context the session was opened with, before any `set_session_context` patch. `childrenOf` * subscription matching reads this, so a patch can never move a session into or out of a * supervisor's children view; the published `context` above is what subscribers read. */ readonly openContext?: PublishedSessionContext; } /** What the live runtime contributes; absent while the session is opening or after it was torn down. */ export interface SessionStatusRuntimeView { readonly isStreaming: boolean; readonly isCompacting: boolean; readonly model?: { readonly provider: string; readonly id: string; }; readonly thinkingLevel?: string; readonly steering: number; readonly followUp: number; readonly pendingQuestions: number; readonly waiting?: RetryWait; } /** The fanout's pending `question` frames for the session (replayed on attach); undefined when the count is unknown. */ export interface SessionStatusFanoutView { readonly pendingQuestions: number | undefined; } /** The derived half of a `session_status` record: everything except subscription, sequence and change. */ export interface DerivedSessionStatus { readonly status: SessionStatusValue; readonly reason?: string; readonly kind: SessionKind; readonly cwd: string; readonly attachments: number; readonly durableSessionId?: string; readonly sessionPath?: string; readonly name?: string; readonly archived: boolean; readonly settled: boolean; readonly settledBy?: ThreadSettledBy; readonly model?: { readonly provider: string; readonly id: string; }; readonly thinkingLevel?: string; readonly pendingQueue: { readonly steering: number; readonly followUp: number; }; readonly waiting?: RetryWait; readonly context: PublishedSessionContext; } /** The state an effect ledger reports for a session's most recent effect (filled by a later provider). */ export interface SessionStatusLastEffect { readonly effectId: string; readonly kind: string; readonly state: string; } /** One `session_status` record as it travels on the wire. */ export interface SessionStatusRecord { readonly type: "session_status"; readonly subscriptionId: string; readonly seq: number; readonly sessionId: string; readonly durableSessionId?: string; readonly sessionPath?: string; readonly kind: SessionKind; readonly cwd: string; readonly name?: string; readonly archived: boolean; readonly settled: boolean; readonly settledBy?: ThreadSettledBy; readonly status: SessionStatusValue; readonly reason?: string; readonly attachments: number; readonly model?: { readonly provider: string; readonly id: string; }; readonly thinkingLevel?: string; readonly pendingQueue: { readonly steering: number; readonly followUp: number; }; /** senpi#7025: the auto-retry wait the running session sleeps through; absent when it is not waiting. */ readonly waiting?: RetryWait; readonly context?: PublishedSessionContext; readonly lastEffect?: SessionStatusLastEffect; /** The last published status change. With no recorded change, snapshots use last known activity, or subscription time if unknown. */ readonly lastChange: { readonly at: string; readonly cause: SessionStatusCause; readonly by: SessionStatusActor; }; } /** * Derive a session's status. `terminal` is the seal the writer recorded (park, close, release); `cause` * is the change being published, used only for the `reason` of a run that an abort ended. */ export declare function deriveSessionStatus(entry: SessionStatusEntryView, runtime: SessionStatusRuntimeView | undefined, fanout: SessionStatusFanoutView, terminal?: SessionStatusTerminal, cause?: SessionStatusCause): DerivedSessionStatus; /** * The terminal a lifecycle record seals a session with. A park and a handoff park leave a session that * reopens by path (`unloaded`); every other close ends it (`closed`) with today's `session_closed.reason`. */ export declare function terminalOfLifecycle(record: { readonly type: string; readonly reason?: unknown; }): SessionStatusTerminal | undefined; /** The status change a session record publishes, or undefined when the record changes no status. */ export declare function causeOfRecord(record: { readonly type?: unknown; readonly method?: unknown; }): SessionStatusCause | undefined; //# sourceMappingURL=session-status.d.ts.map