import type { LocalAgentResolvedConfiguration } from '@khorsheed/dsh-local-agent/types'; import type { Context } from '@deepseek-ai/cordis'; import type { Session } from '@deepseek-ai/dsh-session'; import { type SubagentRun, type SubagentStartRequest } from '@deepseek-ai/dsh-subagent'; import type { Config } from './index.ts'; import type { ClaudeEndpointAuth } from './endpoint-auth.ts'; import { ClaudeSubagentMirror } from './subagent-mirror.ts'; /** Default idle lifetime of an unused resident runtime before reclaim. */ export declare const DEFAULT_LIVE_IDLE_MS: number; /** Default timeout for the turn's system/init ack (a cold boot can be slow). */ export declare const DEFAULT_LIVE_INIT_TIMEOUT_MS = 60000; /** Bounded wait for an interrupted turn to converge while a run disposes. */ export declare const DEFAULT_LIVE_DISPOSE_CONVERGE_MS = 5000; /** How long a tripped channel breaker stays on the exec fallback. */ export declare const DEFAULT_LIVE_CHANNEL_RETRY_MS: number; /** Default minimum interval between one streaming kind's snapshot messages. */ export declare const DEFAULT_SNAPSHOT_MIN_INTERVAL_MS = 50; /** Default minimum text growth between one streaming kind's snapshot messages. */ export declare const DEFAULT_SNAPSHOT_MIN_CHARS = 0; export declare class LiveChannelUnavailableError extends Error { constructor(message: string); } /** Internal timeouts; tests inject small values. */ export interface ClaudeLiveDriverTimeouts { readonly initMs: number; readonly convergeMs: number; readonly channelRetryMs: number; /** Coalescing delay for the member's subagent transcript tails (subagent-mirror.ts). */ readonly subagentTailMs?: number; /** Re-read interval for open background subagents between rounds. */ readonly subagentPollMs?: number; } /** How much of the live event stream crosses into the child session. */ export type ClaudeLiveMirrorGranularity = 'event' | 'token'; /** Fully resolved inputs for one live round. */ export interface ClaudeLiveRoundSpec { /** Parent Session workspace; also the runtime process cwd. */ readonly cwd: string; /** The `claude-code` harness's scoped home, injected as `CLAUDE_CONFIG_DIR`. */ readonly homeDir: string; /** dsh subagent session recording this delegation (always live on this path). */ readonly childSession: Session; /** The delegating session (member-channel registration). */ readonly parentSessionId: string; /** * Resume round: continue the session recorded as `cliSessionId` (also the * reattach path after a runtime crash — the respawn passes `--resume`). */ readonly resume?: { readonly cliSessionId: string; readonly turn: number; } | undefined; /** * The delegation layer's model: the model this DELEGATION requested on its * first round, re-requested by every resume round from the record. The * spawn binds it when no session-level override outranks it — a delegation * naming a model is no longer exec-only; its model binds at spawn like any * other layer. */ readonly model?: string | undefined; readonly configuration?: LocalAgentResolvedConfiguration; /** Fresh round: called with the session id from the turn's system/init. */ readonly onSessionId?: ((sessionId: string) => void) | undefined; } /** * The claude live driver owns every resident stream-json runtime of this * provider generation. Created only when `live: true`; disposed with the * plugin. */ export declare class ClaudeLiveDriver { private readonly ctx; private readonly config; private readonly timeouts; private readonly runtimes; private readonly ensuring; private readonly idleTimers; /** Per-member round serialization (the resume lock covers resume-vs-resume only). */ private readonly roundChains; /** Rounds accepted and not yet settled, per member (a reload asked meanwhile waits for them). */ private readonly roundsInFlight; /** The spec each member's runtime was last ensured with: a reload respawns from it. */ private readonly lastSpecs; /** * Each member's subagent mirror: it outlives a round (a background subagent * keeps running after the member's turn) and ends with the member's * process (see subagent-mirror.ts). */ private readonly subagentMirrors; private channelBrokenAt; private disposed; /** * Set by drain() (a settings-driven generation handoff): new rounds are * refused so the provider falls back to exec, while in-flight rounds finish * on their runtime undisturbed. Unlike `disposed`, the driver still serves * what it already accepted. */ private draining; private readonly disposeController; constructor(ctx: Context, config: Pick & { /** * Resolver for the spawn's endpoint credential (endpoint-auth.ts: the * member's scoped endpoint-auth.json over the plugin config triple), * read per RUNTIME SPAWN — a key rotation reaches the resident process * at its next respawn. Absent, no `ANTHROPIC_*` variables are set. */ endpoint?: (homeDir: string) => Promise; liveIdleMs?: number; /** Maximum batching wait for incremental streaming messages. */ snapshotMinIntervalMs?: number; /** @deprecated Character growth no longer gates live publication. */ snapshotMinChars?: number; /** * Resolver for the member's effective model, read at each RUNTIME SPAWN * (the resident process is where a live round's CLI starts). Receives * the member and the round's delegation-layer model (the delegation's * own request, re-requested on resume from the record) and answers in * the family's order — session-level override, delegation, settings. * Absent, or resolving to nothing, leaves the spawn argv unchanged. */ model?: (childSessionId: string, delegationModel?: string) => string | undefined; /** * Scratch the spawn's effective model into the scoped settings.json * before the process starts (a `--resume` respawn restores the * session's stored model over the `--model` flag). index.ts wires the * shared {@link ClaudeScopedModelMemory}; absent, the file is untouched. */ provisionModel?: (homeDir: string, model: string | undefined) => Promise; }, timeouts?: ClaudeLiveDriverTimeouts); /** Whether the channel is in its post-failure cooldown (rounds fall back to exec). */ get disabled(): boolean; /** Count of resident runtimes currently registered (zombie accounting in tests). */ get liveCount(): number; /** * True while the member's runtime exists or is being spawned. The settings * controller gates a new generation on this: a fresh driver must not serve * a member whose retiring generation still hosts the (same) claude session. */ hasRuntime(key: string): boolean; /** * Drain for a settings-driven generation handoff: refuse new rounds (the * provider's catch falls back to exec), let every in-flight round finish on * its runtime, then reclaim. Resolves when no runtime or spawn remains. * Unlike disposeAll, in-flight work is never interrupted. */ drain(): Promise; private clearIdleTimer; private armIdleTimer; private reclaim; /** Reclaim every runtime (plugin unload); no process survives the profile. */ disposeAll(): Promise; /** The member's subagent mirror (created on its first live round). */ private subagentMirror; /** The member's subagent mirror, if it has one (tests and diagnostics). */ subagentsOf(childSessionId: string): ClaudeSubagentMirror | undefined; private ensureRuntime; /** The model a spawn for this round would bind (the argv `--model` value). */ private spawnModel; /** * The model the member's resident runtime was spawned with, or undefined * when the member has no live runtime (or it binds none). The model broker * compares this against a switch's new effective model. */ runtimeModel(childSessionId: string): string | undefined; /** * Round-start self-heal: a resident process whose CLI lacks the member's * mode tools (its bridge served the placeholder, or an empty list while the * mode has tools) is retired here, between rounds, so this round respawns * a fresh `claude -p` stream-json process that resumes the same CLI session and lists again. * claude registers a `notifications/tools/list_changed` handler, but a placeholder it was served is still replaced here so the round never depends on the notification. The registry decides (`memberToolsStale`); an older core * without it keeps the process. */ private retireIfToolsStale; /** * Reload a member's process on request (the room's edit dialog): retire the * resident runtime and spawn a fresh one that resumes the same CLI session, * without a prompt. Whether `claude -p` lists the * bridge before its first input is not verified; a process that has not * listed reads as a pending reload until its next round. * Chained on the member's round queue, so a running round finishes first * (`after-round`) and the next round waits for the reload. Undefined when * this driver hosts no process for the member. */ reloadMember(key: string, hooks: { readonly onStart: () => void; }): { readonly when: 'now' | 'after-round'; readonly done: Promise<'respawned' | 'no-process' | 'failed'>; } | undefined; /** The reload body, between rounds. */ private reloadLocked; /** * Whether {@link startMember} would start the member's process now: the * driver is live and holds no process, spawn or round for the member. */ canStartMember(key: string): boolean; /** * Start a member's resident process with no round: the room's 立即载入 * for a member that has none (after a host restart, or an idle reclaim). * `prepare` runs on a resume spec the core built from durable state, so * `claude -p` connects the bridge and lists its tools at startup, before any input (measured on claude 2.1.317), with no prompt and no model call. * Chained on the member's round queue; `no-process` when a process or a * round appeared meanwhile. */ startMember(spec: ClaudeLiveRoundSpec): Promise<'respawned' | 'no-process' | 'failed'>; /** * Retire the member's resident runtime so the next round respawns with its * new effective model (the model broker's switch path). The caller refuses * switches while a round is in flight, so the reclaim never kills a run; * the CLI session survives on disk and the respawn resumes it. */ retireRuntime(childSessionId: string): Promise; /** * Spawn the member's resident stream-json process. There is no handshake * message — the channel proves itself with the first turn's system/init — * so spawn failure is the only breaker trip here; the init wait lives in * the round's accept window. */ private spawnRuntime; private markChannelBroken; /** * Drive one delegation round on the member's resident stream-json process. * Mirrors the exec run's settlement contract exactly (settleRunResult + * turn/end bookkeeping); cancel is the interrupt control request and the * process survives. */ /** Initialize native controls without sending a user prompt. Native history starts on its first real turn. */ prepare(spec: ClaudeLiveRoundSpec, signal: AbortSignal): Promise; startRound(request: SubagentStartRequest, spec: ClaudeLiveRoundSpec): Promise; /** The serialized round body. */ private startRoundLocked; } //# sourceMappingURL=live-driver.d.ts.map