UNPKG

@code-yeongyu/senpi

Version:

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

135 lines • 6.5 kB
/** * `/loop` extension factory and lifecycle wiring. * * This module owns the only impure parts of the feature: real timers, the session store, * the extension event wiring, and message dispatch. Every decision it makes is delegated * to the pure modules beside it (`scheduler.ts`, `tick-prompt.ts`, `cron-planner.ts`, * `loopfile.ts`, `status.ts`), so the runtime here is a thin, testable adapter. * * Load-bearing rules implemented here: * - A tick STEERS. Idle dispatch goes through `sendUserMessage(..., { * expandPromptTemplates: true })` so a slash payload reaches the real command path; * a busy session receives the tick as a steer, delivered into the running turn at its * next boundary, never parked as a follow-up behind it. * - Shutdown SUSPENDS: timers are cancelled, the ticker disposed, and the snapshot is * persisted without a terminal reason (every senpi shutdown reason is resumable). * - Keepalive is applied only to an attributed dynamic iteration, never after an ordinary * user turn, and never after a user abort (an abort PAUSES the loop instead). * - A store failure is not swallowed: the affected loops end with `error` and the user is * told, because a schedule that cannot be persisted must not keep running. */ import type { EntryRenderer, ExtensionContext, ExtensionFactory } from "../../types.ts"; import { type LoopFileResult } from "./loopfile.ts"; import { type LoopClock, type LoopIdFactory, type LoopTimerPort } from "./scheduler.ts"; import { type LoopMode, type TickDelivery } from "./tick-prompt.ts"; import type { DeliveryId, EpochMs, LoopEndReason, LoopId, LoopSentinel, LoopState, LoopStoreRef, RequestedInterval } from "./types.ts"; /** Custom entry type carrying one dispatched tick, used for attribution and folding. */ export declare const LOOP_TICK_ENTRY_TYPE = "loop-tick"; export interface LoopTickEntryData { readonly loopId: LoopId; readonly deliveryId: DeliveryId; readonly scheduledForAt: EpochMs; readonly mode: LoopMode; readonly delivery: TickDelivery; readonly sentinel?: LoopSentinel; /** Consecutive quiet dynamic iterations at the moment this tick was dispatched. */ readonly noopStreak: number; /** True when this tick continues a quiet streak and should render folded. */ readonly folded: boolean; } export interface LoopCreateOk { readonly ok: true; readonly loopId: LoopId; /** Present when a new dynamic loop replaced a previous one. */ readonly supersededLoopId?: LoopId; /** Human-readable rounding notice when the requested cadence was normalized. */ readonly roundingNotice?: string; readonly cronExpression?: string; readonly effectiveCadence?: string; readonly expiresAt?: EpochMs; } export interface LoopCreateRejected { readonly ok: false; readonly message: string; } export type LoopCreateOutcome = LoopCreateOk | LoopCreateRejected; export interface LoopStoreFailure { readonly endReason: LoopEndReason; readonly message: string; readonly loopIds: readonly LoopId[]; } export interface StartFixedRequest { readonly originalArgs: string; readonly prompt: string; readonly requestedInterval: RequestedInterval; } export interface StartDynamicRequest { readonly originalArgs: string; readonly prompt: string; } export interface StartBareRequest { readonly originalArgs: string; readonly interval?: RequestedInterval; } export interface ControllerScheduleWakeupRequest { readonly loopId: LoopId; readonly requestedDelaySeconds: number; readonly delaySeconds: number; readonly reason: string; readonly prompt: string; readonly noop: boolean; } /** * Runtime surface of a live loop extension. The `/loop` command module drives the * extension exclusively through this interface, so command parsing never reaches into * scheduler or store internals. */ export interface LoopController { startFixed(request: StartFixedRequest): Promise<LoopCreateOutcome>; startDynamic(request: StartDynamicRequest): Promise<LoopCreateOutcome>; startBare(request: StartBareRequest): Promise<LoopCreateOutcome>; /** Runs the due decision for a loop at the current clock reading and dispatches it. */ fireDue(loopId: LoopId): Promise<void>; scheduleWakeup(request: ControllerScheduleWakeupRequest): Promise<void>; stop(target: LoopId | "all", detail: string): Promise<readonly LoopId[]>; pause(target: LoopId | "all"): Promise<readonly LoopId[]>; resume(target: LoopId | "all"): Promise<readonly LoopId[]>; getState(): LoopState; statusLine(): string | undefined; /** Set when a store read/write failed and the affected loops were ended with `error`. */ lastStoreFailure(): LoopStoreFailure | undefined; /** True when this loop was retired because its state could not be read or written. */ isEndedWithError(loopId: LoopId): boolean; } export interface LoopExtensionDeps { readonly clock?: LoopClock; /** Factory so each session gets its own timer port; defaults to real timeouts. */ readonly timers?: () => LoopTimerPort; readonly ids?: LoopIdFactory; readonly storeRef?: (ctx: ExtensionContext) => LoopStoreRef; readonly resolveLoopFile?: (options: { cwd: string; homeDir: string; }) => Promise<LoopFileResult>; readonly readState?: (ref: LoopStoreRef) => Promise<LoopState | null>; readonly writeState?: (ref: LoopStoreRef, state: LoopState) => Promise<void>; /** * Published at factory time so a command module (or a test) can drive this instance. * The controller is bound to the extension's own session state, so it stays valid across * session starts within one extension generation. */ readonly onControllerReady?: (controller: LoopController) => void; } export declare const renderLoopTickEntry: EntryRenderer<LoopTickEntryData>; /** * Real timer port: exactly one live timeout per key, replaced on re-arm. * * Follows the generation-counter discipline of `cache-keepalive/index.ts`: cancelling or * re-arming a key bumps its generation, so a callback whose generation is stale returns * without firing even if it was already scheduled on the event loop. */ export declare function createNodeTimerPort(): LoopTimerPort; export declare function createLoopExtension(deps?: LoopExtensionDeps): ExtensionFactory; declare const _default: ExtensionFactory; export default _default; //# sourceMappingURL=index.d.ts.map