import type { ModelOverrideSelection } from "./registry-types.ts"; import { isNativeModelToken } from "./model-token.ts"; import { randomUUID } from "node:crypto"; import { lstatSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { BasePeer, type OwnedToolsLaunch } from "../hub/peers.ts"; import type { DeliveryFailureCause } from "../hub/delivery-failure.ts"; import { frame, type Envelope } from "../hub/envelope.ts"; import { realPath } from "../hub/project.ts"; import { IMPLEMENTER_HUB_TOOL_NAMES } from "../hub/hub-tools.ts"; import { claudeStateFile } from "../hub/claude-state.ts"; import { sessionSettings } from "../cli/launch.ts"; import { assessVersion, COMPAT } from "./compat.ts"; import { OwnedPrintChild, PrintChildCleanupError, type OwnedInputContext, type OwnedInputObserver, type OwnedPrintChildOptions } from "./claude-print-child.ts"; import { turnAttribution } from "../hub/turn-attribution.ts"; import { ClaudeTaskSessions } from "./claude-task-sessions.ts"; import { formatClaudeFailure, type ClaudeFailureTrace } from "../hub/claude-failure-stage.ts"; import type { PermissionMode } from "../hub/permission-mode.ts"; import type { ReviewerPrintUsage } from "./reviewer-print.ts"; import { resultModel } from "./print-result-model.ts"; const provenVersions = new WeakMap<() => boolean, string>(); function provenVersion(binding: () => boolean): string | undefined { return provenVersions.get(binding); } function sealFailureTrace(trace: ClaudeFailureTrace | undefined): void { try { trace?.seal(); } catch { /* diagnostics never affect owned cleanup */ } } function discardFailureTrace(trace: ClaudeFailureTrace | undefined): void { try { trace?.discard(); } catch { /* diagnostics never affect owned cleanup */ } } function readFailureTrace(trace: ClaudeFailureTrace | undefined): unknown { if (!trace) return undefined; // Every returned trace is sealed by onLeaderExit before authority is revoked. try { return trace.take(); } catch { discardFailureTrace(trace); return undefined; } } export interface ClaudeImplementerOptions { cwd: string; stateDir: string; projectRoot: string; instanceId: string; server: string; factsHook: string; systemPrompt: string; watchdogMs: number; /** Declared `model` of this instance (#844): one validated word passed as `--model`; unset leaves the native default and the argv unchanged. */ model?: string; /** Host-only choice sampled once at the next turn/review boundary. */ getModelOverride?: () => ModelOverrideSelection; mode?: "ask" | "ask-when-needed"; /** Exact host-reported transcript evidence only; unknown/read errors retain the recorded UUID. */ transcriptPersisted?(task: number, sessionId: string): boolean | "unknown"; /** Host-only lifecycle test seam, never project configuration. */ stopChild?: OwnedPrintChildOptions["stop"]; command?: readonly string[]; /** Host-owned policy and board reads, never native or model-selected authority. */ admit(env: Envelope): boolean; chatTask(): number | undefined; /** Host-only schedule provenance; unknown is not an assignment trigger. */ scheduledTask?(task: number): boolean | undefined; /** Host-only privacy and current-owner check for the task-id hint; absent or unknown withholds it. */ taskHintAllowed?(task: number): boolean; onUsage?(usage: ReviewerPrintUsage, task?: number): void; notify?(message: string): void; /** Test/embedding-only proof port; project configuration cannot select it. */ prove?(): Promise<() => boolean>; /** Host-observed failure evidence bound to one admitted native launch. */ failureTrace?(launch: Readonly, nativeVersion?: string): ClaudeFailureTrace; /** Actual task-session input provenance only; never a peer/native authority port. */ beforeInput?: OwnedInputObserver; inputReleased?: OwnedInputObserver; /** Resolve current privacy at final rendering; unknown or a throw is private. */ failurePrivate?(task?: number): boolean; /** Optional host observer of the fixed, already privacy-screened failure message. */ failureLog?(message: string): void; } export interface ImplementerTurn { sessionId: string; resume: boolean; persistent: boolean } export function claudeImplementerArgs(turn: ImplementerTurn, mcp: string, settings: string, prompt: string, model?: string): string[] { return ["-p", "--output-format", "stream-json", "--verbose", turn.resume ? "--resume" : "--session-id", turn.sessionId, ...(!turn.persistent ? ["--no-session-persistence"] : []), "--strict-mcp-config", "--mcp-config", mcp, "--setting-sources", "", "--settings", settings, ...(model ? ["--model", model] : []), "--permission-mode", "default", "--permission-prompts", "host", "--permission-prompt-tool", "mcp__agent-hub__hub_permission_request", "--allowedTools", ...IMPLEMENTER_HUB_TOOL_NAMES.map(name => `mcp__agent-hub__${name}`), "--append-system-prompt", prompt]; } /** A receipt-backed CLI version and a per-start binary fence. Version reads use an isolated, disposable HOME. * ponytail: native prompt behavior is certified by the dated deterministic receipt, not re-inferred at startup; * the upgrade is another isolated native permission probe before extending COMPAT. */ export async function proveClaudeImplementer(peer: string, command: readonly string[]): Promise<() => boolean> { const binary = Bun.which(command[0]!, { PATH: process.env.PATH }) ?? command[0]!; const fingerprint = () => { const file = realPath(binary), stat = lstatSync(file); return [file, stat.dev, stat.ino, stat.size, stat.mtimeMs, stat.ctimeMs].join(":"); }; const binding = fingerprint(), home = mkdtempSync(join(tmpdir(), "ahub-claude-proof-")); let version = "", timedOut = false; const child = new OwnedPrintChild({ peer, command, args: ["--version"], cwd: home, input: "", envMode: "replace", env: { PATH: process.env.PATH, HOME: home, CLAUDE_CONFIG_DIR: join(home, ".claude") }, maxFrameBytes: 4096, maxOutputBytes: 4096, onLine: line => { version += line; }, onLimit: () => { timedOut = true; void child.stop().catch(() => {}); } }); const timer = setTimeout(() => { timedOut = true; void child.stop().catch(() => {}); }, 3000); try { const code = await child.done; if (code !== 0 || timedOut) throw new Error("Claude implementer permission proof unavailable"); const observed = COMPAT["claude-implementer"].parse(version), verdict = assessVersion("claude-implementer", observed); if (!verdict.ok) throw new Error(verdict.reason); if (fingerprint() !== binding) throw new Error("Claude implementer binary changed during proof"); const proof = () => { try { return fingerprint() === binding; } catch { return false; } }; provenVersions.set(proof, verdict.version); return proof; } finally { clearTimeout(timer); await child.stop(); rmSync(home, { recursive: true, force: true }); } } /** One owned print child at a time. Idle holds no native process; task continuity is ids-only on disk. */ export class ClaudeImplementerPeer extends BasePeer { /** #1402: the model the last `result` row's modelUsage named; read once at the turn end. */ private servedModel: string | undefined; observedModel(): import("../hub/events.ts").ModelObservation | undefined { const model = this.servedModel; this.servedModel = undefined; return model ? { model, evidence: "served" } : undefined; } readonly hubNative = true; readonly hubOwned: boolean = true; private generation = 0; private owned: OwnedPrintChild | undefined; private running: Promise | undefined; private active: OwnedToolsLaunch | undefined; private childAbort: AbortController | undefined; private abort: AbortController | undefined; private cleanup: (() => void) | undefined; private binding: (() => boolean) | undefined; private command: readonly string[] | undefined; private deliveryId: string | undefined; private inputReleased = false; private failureTrace: ClaudeFailureTrace | undefined; private sessions: ClaudeTaskSessions; private stopping: Promise | undefined; private stoppingTarget: "idle" | "offline" | undefined; private observerSessionIdValue: string | undefined; private currentInputContext: OwnedInputContext | undefined; private mode: "ask" | "ask-when-needed"; constructor(id: string, private readonly options: ClaudeImplementerOptions) { super(id, options.watchdogMs); if (options.mode !== undefined && options.mode !== "ask" && options.mode !== "ask-when-needed") throw new Error("Claude implementer refuses unsupported permission mode"); this.mode = options.mode ?? "ask"; this.sessions = new ClaudeTaskSessions(join(options.stateDir, claudeStateFile("claude-task-sessions", id))); } get permissionModeState(): "ask" | "ask-when-needed" { return this.mode; } async setPermissionMode(mode: PermissionMode): Promise { if (mode !== "ask" && mode !== "ask-when-needed") throw new Error("Claude implementer refuses never-ask"); this.mode = mode; // Native remains default: the host facts gate applies this choice to the current child. } get sessionGeneration(): string | undefined { return this.active?.generation; } /** Privacy-only observer identity; never exposed as PeerAdapter.nativeSession or used for budget/resume authority. */ get observerSessionId(): string | undefined { return this.observerSessionIdValue; } ownedTools(launchId: unknown): OwnedToolsLaunch | undefined { return typeof launchId === "string" && this.active?.launchId === launchId && !this.abort?.signal.aborted ? this.active : undefined; } keepApprovalAlive(): void { if (this.state === "busy" && this.active) this.touch(); } recoveryMetadata(): Record { return { launch: { kind: "claude-implementer", peer: this.id }, ...(this.active ? { sessionId: this.active.sessionId } : {}) }; } async start(): Promise { if (this.owned || this.running || this.stopping) throw new Error("Claude implementer cleanup is not complete"); if (this.state !== "offline") return; const generation = ++this.generation; const selected = this.options.command ?? ["claude"]; const command = this.options.prove ? selected : [realPath(Bun.which(selected[0]!, { PATH: process.env.PATH }) ?? selected[0]!), ...selected.slice(1)]; const binding = await (this.options.prove?.() ?? proveClaudeImplementer(this.id, command)); if (generation !== this.generation) return; this.command = command; this.binding = binding; this.setState("idle"); } async stop(reason = "Claude implementer stopped before settlement"): Promise { return this.cancel(reason, "offline"); } protected override onWatchdog(): void { void this.cancel("Claude implementer watchdog timeout", "idle", "timeout").catch(() => {}); } private settle(state: "completed" | "failed_safe" | "needs_review", reason?: string, failureCause?: DeliveryFailureCause): void { const id = this.deliveryId; this.deliveryId = undefined; if (id) this.delivery({ id, state, ...(reason ? { reason } : {}), ...(failureCause ? { failureCause } : {}) }); } private cancel(reason: string, next: "idle" | "offline", failureCause?: DeliveryFailureCause): Promise { if (this.stopping) { if (next === "offline") this.stoppingTarget = "offline"; return this.stopping; } this.stoppingTarget = next; ++this.generation; this.active = undefined; this.childAbort?.abort(); this.childAbort = undefined; this.abort?.abort(); this.clearWatchdog(); discardFailureTrace(this.failureTrace); this.failureTrace = undefined; this.settle(this.inputReleased ? "needs_review" : "failed_safe", reason, failureCause); if (this.owned || this.running) this.setState("busy"); const work: Promise = Promise.resolve().then(async () => { if (this.owned) await this.owned.stop(); // Retain handle/busy if group cleanup fails. const running = this.running; await running?.catch(() => {}); if (this.running === running) this.running = undefined; this.cleanup?.(); this.cleanup = undefined; this.owned = undefined; // An idle callback can synchronously drain the next Bus delivery. const target = this.stoppingTarget ?? next; if (this.stopping === work) { this.stopping = undefined; this.stoppingTarget = undefined; } this.setState(target); }); this.stopping = work; void work.finally(() => { if (this.stopping === work) { this.stopping = undefined; this.stoppingTarget = undefined; } }).catch(() => {}); return work; } async deliver(envs: Envelope[], deliveryId?: string): Promise { if (this.state !== "idle" || this.owned || this.running || this.stopping) throw new Error("Claude implementer is not idle"); if (envs.some(env => env.private)) throw new Error("Claude implementer refuses private delivery"); const groups: { task?: number; envs: Envelope[] }[] = []; for (const env of envs) { // Completion/cancellation notices and foreign task refs do not veto ordinary messages in the digest. if (!this.options.admit(env)) continue; const raw = env.refs?.task, task = raw === undefined ? this.options.chatTask() : Number(raw); if (task !== undefined && (!Number.isSafeInteger(task) || task <= 0)) continue; let group = groups.find(item => item.task === task); if (!group) { group = { task, envs: [] }; groups.push(group); } group.envs.push(env); } const generation = this.generation; this.deliveryId = deliveryId; this.inputReleased = false; this.abort = new AbortController(); this.setState("busy"); let selection: ModelOverrideSelection | undefined, modelFailure: Error | undefined; try { selection = this.options.getModelOverride?.(); if (selection?.model !== undefined && !isNativeModelToken(selection.model)) throw new Error("invalid model"); } catch { modelFailure = new Error("runtime model selection unavailable; check the peer model setting and retry"); } if (deliveryId) this.delivery({ id: deliveryId, state: "accepted" }); // Register the run identity before any synchronous failure can publish idle. const running: Promise = Promise.resolve().then(() => this.run(groups, generation, running, selection, modelFailure)); this.running = running; void running.finally(() => { if (this.running === running) this.running = undefined; }).catch(() => {}); } private async run(groups: { task?: number; envs: Envelope[] }[], generation: number, running: Promise, selection?: ModelOverrideSelection, modelFailure?: Error): Promise { const current = () => generation === this.generation && !this.abort?.signal.aborted; let failureReason: string | undefined; try { if (modelFailure) throw modelFailure; const model = selection?.model ?? this.options.model; let modelApplied = false; for (let group of groups) { failureReason = undefined; if (!current() || !this.binding?.()) throw new Error("Claude implementer admission changed"); group = { ...group, envs: group.envs.filter(env => !env.private && this.options.admit(env)) }; if (!group.envs.length) continue; let turn = this.sessions.session(group.task); if (turn.resume && group.task !== undefined) { let persisted: boolean | "unknown" = "unknown"; try { persisted = this.options.transcriptPersisted?.(group.task, turn.sessionId) ?? "unknown"; } catch { /* unknown is never missing */ } if (persisted === false) { this.sessions.forgetMissing(group.task, turn.sessionId); turn = this.sessions.session(group.task); this.options.notify?.(`${this.id}: task #${group.task} session fresh; recorded transcript is missing`); } } const inputContext: OwnedInputContext = Object.freeze({ sessionId: turn.sessionId, inputId: randomUUID() }); this.observerSessionIdValue = turn.sessionId; this.currentInputContext = inputContext; this.options.beforeInput?.(inputContext, group.envs); const launchId = randomUUID(), directory = mkdtempSync(join(this.options.stateDir, "claude-child-")), settings = join(directory, "settings.json"); let launchFailureTrace: ClaudeFailureTrace | undefined; this.cleanup = () => rmSync(directory, { recursive: true, force: true }); writeFileSync(settings, sessionSettings(undefined, { script: this.options.factsHook, stateDir: this.options.stateDir, purpose: "permission" }), { mode: 0o600, flag: "wx" }); const env = { AGENTHUB_MODE: "tools", AGENTHUB_PEER_ID: this.id, AGENTHUB_STATE_DIR: this.options.stateDir, AGENTHUB_PROJECT_DIR: this.options.projectRoot, AGENTHUB_INSTANCE_ID: this.options.instanceId, AGENTHUB_LAUNCH_ID: launchId, AGENTHUB_HOOK_PURPOSE: "permission", AGENTHUB_PERMISSION_TOOL: "1" }; const mcp = JSON.stringify({ mcpServers: { "agent-hub": { command: process.execPath, args: ["run", this.options.server], env } } }); let result = false, failed = false; if (!current() || !this.binding?.() || group.envs.some(env => env.private || !this.options.admit(env))) throw new Error("Claude implementer admission changed before child start"); let taskPrivate = group.task !== undefined; try { if (group.task !== undefined) taskPrivate = this.options.taskHintAllowed?.(group.task) !== true; } catch { /* Unknown is private. */ } const attribution = turnAttribution(group.envs, { task: group.task, cloud: true, scheduled: this.options.scheduledTask }); if (taskPrivate) attribution.private = true; const child = new OwnedPrintChild({ peer: this.id, command: this.command!, args: claudeImplementerArgs(turn, mcp, settings, this.options.systemPrompt, model), cwd: this.options.cwd, input: group.envs.map(frame).join("\n\n"), env, attribution, current, stop: this.options.stopChild, onSpawn: () => { try { if (!this.binding?.() || group.envs.some(env => env.private || !this.options.admit(env))) throw new Error("Claude implementer admission changed before child start"); } catch { // Fence synchronously before OwnedPrintChild can release stdin; cleanup is asynchronous and owned. void this.cancel("Claude implementer admission changed before child start", "idle").catch(() => {}); return; } if (!modelApplied) { modelApplied = true; try { selection?.applied(); } catch { /* Status acknowledgement never grants child authority. */ } } this.childAbort = new AbortController(); const launch: OwnedToolsLaunch = Object.freeze({ permissionHook: true, launchId, generation: `${generation}:${launchId}`, sessionId: turn.sessionId, signal: this.childAbort.signal, ...(group.task !== undefined ? { task: group.task } : {}) }); this.active = launch; try { launchFailureTrace = this.options.failureTrace?.(Object.freeze({ ...launch }), provenVersion(this.binding!)); } catch { launchFailureTrace = undefined; } this.failureTrace = launchFailureTrace; // Admission is synchronous and fenced; settlement must treat the child as having received input // even if cancellation interrupts a later stdin flush callback. this.inputReleased = true; }, onInputReleased: () => { if (!current() || this.currentInputContext !== inputContext) return; this.options.inputReleased?.(inputContext, group.envs); }, onLeaderExit: () => { sealFailureTrace(launchFailureTrace); this.active = undefined; this.childAbort?.abort(); this.childAbort = undefined; }, onActivity: () => this.touch(), onLimit: () => { void this.cancel("Claude implementer output limit exceeded", "idle").catch(() => {}); }, onFrame: event => { const row = event as { type?: unknown; subtype?: unknown; is_error?: unknown; usage?: ReviewerPrintUsage; session_id?: unknown } | null; if (row?.session_id === turn.sessionId && group.task !== undefined) { try { if (!this.sessions.markStarted(group.task, turn.sessionId)) throw new Error("session changed"); } catch { void this.cancel("Claude implementer session publication failed", "idle").catch(() => {}); return; } } if (row?.type !== "result" || result) return; result = true; failed = row.is_error === true || row.subtype !== "success"; if (row.usage) this.options.onUsage?.(row.usage, group.task); { const model = resultModel(row); if (model) this.servedModel = model; } } }); this.owned = child; let code: number | null; try { code = await child.done; } catch (error) { // spawn ENOENT has no owned group. A started child with failed group cleanup must keep its handle. if (child.process.pid === undefined) this.owned = undefined; if (error instanceof PrintChildCleanupError) failureReason = this.failureReason(group.task, launchFailureTrace); else discardFailureTrace(launchFailureTrace); if (this.failureTrace === launchFailureTrace) this.failureTrace = undefined; throw error; } this.active = undefined; this.owned = undefined; this.cleanup?.(); this.cleanup = undefined; if (!current()) { discardFailureTrace(launchFailureTrace); if (this.failureTrace === launchFailureTrace) this.failureTrace = undefined; return; } if (code !== 0 || !result || failed) { failureReason = this.failureReason(group.task, launchFailureTrace) ?? "Claude implementer turn failed"; if (this.failureTrace === launchFailureTrace) this.failureTrace = undefined; throw new Error("Claude implementer turn failed"); } discardFailureTrace(launchFailureTrace); if (this.failureTrace === launchFailureTrace) this.failureTrace = undefined; } if (current()) { if (this.running === running) this.running = undefined; this.settle(this.inputReleased ? "completed" : "failed_safe", this.inputReleased ? undefined : "Claude implementer has no admissible envelopes"); this.setState("idle"); } } catch (error) { this.active = undefined; this.childAbort?.abort(); this.childAbort = undefined; if (current()) { const reason = error instanceof PrintChildCleanupError ? failureReason === "Claude implementer turn failed (details withheld)" ? "Claude implementer cleanup incomplete; details withheld" : failureReason ? `Claude implementer cleanup incomplete; ${failureReason}` : "Claude implementer cleanup incomplete" : failureReason ?? "Claude implementer turn failed"; this.settle(this.inputReleased ? "needs_review" : "failed_safe", reason); if (this.owned) this.setState("busy"); else { this.cleanup?.(); this.cleanup = undefined; if (this.running === running) this.running = undefined; this.setState("idle"); } } } } private failureReason(task: number | undefined, trace: ClaudeFailureTrace | undefined): string | undefined { if (!trace) return undefined; const evidence = readFailureTrace(trace); if (!evidence) return undefined; let privateTurn = true; try { privateTurn = this.options.failurePrivate?.(task) !== false; } catch { /* unknown privacy is private */ } const message = formatClaudeFailure(evidence, privateTurn); try { this.options.failureLog?.(message); } catch { /* host observation never changes settlement */ } return message; } } export function isClaudeImplementerPeer(value: unknown): value is ClaudeImplementerPeer { return value instanceof ClaudeImplementerPeer; }