diff --git a/doc/AGENT-LIFECYCLE.md b/doc/AGENT-LIFECYCLE.md index 96767c83131..87e0967316f 100644 --- a/doc/AGENT-LIFECYCLE.md +++ b/doc/AGENT-LIFECYCLE.md @@ -54,6 +54,19 @@ An old result cannot complete a newer operation. Deletion requires completed termination or a rejected hire. The existing restrictions for built-in agents and accounting still apply. +Messages accepted during setup remain queued until verification completes. +After the worker commits `ready`, its driver immediately asks the scheduler to +dispatch that agent's saved work. The scheduler rechecks company scope, +invokability, budgets, concurrency, and task ownership through normal admission. +If dispatch fails, readiness remains committed and the periodic queue sweep +retries; setup is not repeated because of a scheduler failure. + +Native OpenCode verification checks the selected model with the real hello +probe. It validates the model identifier but does not first enumerate the full model +catalog. Authentication failures, unavailable models, and probe timeouts still +prevent readiness. Without an explicit local working directory, the native +probe uses a temporary empty directory instead of the server's checkout. + ## Commands and transactions `requestHire` writes the agent record before preparation starts. diff --git a/doc/DEVELOPING.md b/doc/DEVELOPING.md index 117cfd61819..6e1a053fd4d 100644 --- a/doc/DEVELOPING.md +++ b/doc/DEVELOPING.md @@ -1351,6 +1351,16 @@ trace finalization immediately so a later error cannot lose cleanup state. Existing public failure helpers remain re-exported by `heartbeat.ts`. Keep completion policy changes separate from this extraction and dispatch. +Runtime selection is in `server/src/services/heartbeat/runtime-selection.ts`. +`selectHeartbeatRuntime` constructs or restores native execution inputs, selects +completion contracts and sessions, and persists native or legacy runtime choices. +Native selection retains the row lock that serializes it with cancellation and +controller lease renewal. The executor receives an explicit selection result; +lifecycle and ownership callbacks update its teardown state immediately, including +when preparation later throws. Public sandbox lifecycle and model fallback helpers +remain re-exported by `heartbeat.ts`. Provider dispatch and final cleanup stay in +the executor. + ## Wake Context Delivery Built-in adapters deliver wake context through the run prompt, including structured diff --git a/doc/connections/BROWSER-USE.md b/doc/connections/BROWSER-USE.md index 72104d93981..1a1a3c7769e 100644 --- a/doc/connections/BROWSER-USE.md +++ b/doc/connections/BROWSER-USE.md @@ -72,7 +72,12 @@ runs and browser instances. Each hosted run records its invocation, parent run, event cursor and cost checkpoint. A leased reconciler polls minimal status, drains bounded event pages, discovers browser instances by conversation, and checks each instance's authoritative conversation association. Restarting the -server resumes reconciliation. A lost paid-create response becomes an explicit +server resumes reconciliation. The app schedules the next check from saved +session deadlines and cleanup leases. Session writes wake it after commit. +With no live sessions it performs no recurring browser database scan. Existing +sessions still poll provider state (about three seconds while running, fifteen +while idle), enforce expiry and access changes, and retry failed cleanup. +A lost paid-create response becomes an explicit unknown outcome and is never submitted again automatically; the operator must inspect Browser Use before starting replacement work. diff --git a/doc/plans/2026-10-08-instance-sleep-and-scheduler-inventory.md b/doc/plans/2026-10-08-instance-sleep-and-scheduler-inventory.md index 0646390908a..24db42b22ac 100644 --- a/doc/plans/2026-10-08-instance-sleep-and-scheduler-inventory.md +++ b/doc/plans/2026-10-08-instance-sleep-and-scheduler-inventory.md @@ -438,10 +438,14 @@ Longer polling intervals alone do not complete an item. catch-up remain the recovery boundaries. Source: [email-channels.ts](../../server/src/services/email-channels.ts). -- [ ] **JOB-03 — Browser-use cleanup.** Current: every **3s** with due-session - queries even when empty. Change: register actual session reconciliation/expiry - deadlines. **Complete when:** no sessions means no queries, while active leases, - expiry, and failed cleanup remain recoverable. Sources: +- [x] **JOB-03 — Browser-use cleanup.** Replaced the empty **3s** scan with + transaction-aware session signals and deadlines from existing `nextPollAt` and + `leaseUntil` columns. Startup restores unfinished work; no live sessions means + no recurring browser SQL or timer. Active and idle sessions retain provider + polling, expiry checks, lease renewal, and cleanup retries. Credential-removal + sweeps signal lease release so background cleanup resumes promptly. Standby + suppresses SQL; idle drain defers dispatch with a memory-only wake. No new + tables. Independent DB roots/direct SQL require restart recovery. Sources: [app.ts](../../server/src/app.ts), [browser-use.ts](../../server/src/services/browser-use.ts). diff --git a/packages/adapters/opencode-local/src/server/test.remote.test.ts b/packages/adapters/opencode-local/src/server/test.remote.test.ts index aebe6cfa0a9..b9a63a48b97 100644 --- a/packages/adapters/opencode-local/src/server/test.remote.test.ts +++ b/packages/adapters/opencode-local/src/server/test.remote.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, rm } from "node:fs/promises"; +import { mkdtemp, rm, stat } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; @@ -72,6 +72,13 @@ vi.mock("@paperclipai/adapter-utils/execution-target", async () => { }); import { testEnvironment } from "./test.js"; +import { discoverOpenCodeModels, ensureOpenCodeModelConfiguredAndAvailable } from "./models.js"; + +vi.mock("./models.js", async () => ({ + ...await vi.importActual("./models.js"), + discoverOpenCodeModels: vi.fn().mockRejectedValue(new Error("catalog should not be needed")), + ensureOpenCodeModelConfiguredAndAvailable: vi.fn().mockRejectedValue(new Error("catalog should not be needed")), +})); describe("opencode remote environment diagnostics", () => { const configHomes: string[] = []; @@ -89,6 +96,56 @@ describe("opencode remote environment diagnostics", () => { await Promise.all(configHomes.splice(0).map(dir => rm(dir, { recursive: true, force: true }))); }); + it.each(["passed", "auth_required", "model_unavailable", "timed_out"])( + "validates a native model with the real probe path and no catalog scan (%s)", async outcome => { + const successfulProbe = await runAdapterExecutionTargetProcess.getMockImplementation()!(); + runAdapterExecutionTargetProcess.mockResolvedValueOnce(outcome === "passed" ? successfulProbe : { + ...successfulProbe, exitCode: 1, stdout: "", timedOut: outcome === "timed_out", + stderr: outcome === "auth_required" ? "invalid API key" : "ProviderModelNotFoundError", + }); + const result = await testEnvironment({ companyId: "company-1", adapterType: "paperclip_runner", + config: { model: "openrouter/deepseek/deepseek-v4-flash-0731", env: { XDG_CONFIG_HOME: configHome } } }); + expect(discoverOpenCodeModels).not.toHaveBeenCalled(); + expect(ensureOpenCodeModelConfiguredAndAvailable).not.toHaveBeenCalled(); + expect(runAdapterExecutionTargetProcess).toHaveBeenCalledExactlyOnceWith( + expect.any(String), null, "opencode", ["run", "--format", "json", "--model", "openrouter/deepseek/deepseek-v4-flash-0731"], + expect.objectContaining({ stdin: "Respond with hello." }), + ); + expect(result.status).toBe(outcome === "passed" ? "pass" : "warn"); + expect(result.checks).toContainEqual(expect.objectContaining({ code: `opencode_hello_probe_${outcome}` })); + const call = runAdapterExecutionTargetProcess.mock.calls[0] as unknown as [unknown, unknown, unknown, unknown, { cwd: string }]; + expect(path.basename(call[4].cwd)).toMatch(/^paperclip-opencode-native-probe-/); + await expect(stat(call[4].cwd)).rejects.toMatchObject({ code: "ENOENT" }); + }, + ); + + it("rejects a malformed native model before starting the probe", async () => { + const result = await testEnvironment({ companyId: "company-1", adapterType: "paperclip_runner", + config: { model: "missing-provider", env: { XDG_CONFIG_HOME: configHome } } }); + expect(result.status).toBe("fail"); + expect(runAdapterExecutionTargetProcess).not.toHaveBeenCalled(); + expect(discoverOpenCodeModels).not.toHaveBeenCalled(); + }); + + it("preserves an explicitly configured native probe directory", async () => { + await testEnvironment({ companyId: "company-1", adapterType: "paperclip_runner", + config: { model: "provider/model", cwd: configHome, env: { XDG_CONFIG_HOME: configHome } } }); + const call = runAdapterExecutionTargetProcess.mock.calls[0] as unknown as [unknown, unknown, unknown, unknown, { cwd: string }]; + expect(call[4].cwd).toBe(configHome); + expect((await stat(configHome)).isDirectory()).toBe(true); + }); + + it("keeps catalog validation for the legacy local adapter", async () => { + vi.mocked(discoverOpenCodeModels).mockResolvedValueOnce([{ id: "provider/model", label: "Model" }]); + vi.mocked(ensureOpenCodeModelConfiguredAndAvailable).mockResolvedValueOnce([{ id: "provider/model", label: "Model" }]); + const result = await testEnvironment({ companyId: "company-1", adapterType: "opencode_local", + config: { model: "provider/model", env: { XDG_CONFIG_HOME: configHome } } }); + expect(result.status).toBe("pass"); + expect(discoverOpenCodeModels).toHaveBeenCalledOnce(); + expect(ensureOpenCodeModelConfiguredAndAvailable).toHaveBeenCalledOnce(); + expect(runAdapterExecutionTargetProcess).toHaveBeenCalledOnce(); + }); + it.each([false, true])("stages remote runtime config assets for sandbox hello probes (managed=%s)", async (managed) => { const configHome = await mkdtemp(path.join(os.tmpdir(), "opencode-remote-test-config-")); configHomes.push(configHome); diff --git a/packages/adapters/opencode-local/src/server/test.ts b/packages/adapters/opencode-local/src/server/test.ts index 7f2480b9c90..628745e9772 100644 --- a/packages/adapters/opencode-local/src/server/test.ts +++ b/packages/adapters/opencode-local/src/server/test.ts @@ -25,7 +25,7 @@ import { prepareAdapterExecutionTargetRuntime, overrideAdapterExecutionTargetRemoteCwd, } from "@paperclipai/adapter-utils/execution-target"; -import { discoverOpenCodeModels, ensureOpenCodeModelConfiguredAndAvailable } from "./models.js"; +import { discoverOpenCodeModels, ensureOpenCodeModelConfiguredAndAvailable, requireOpenCodeModelId } from "./models.js"; import { parseOpenCodeJsonl } from "./parse.js"; import { SANDBOX_INSTALL_COMMAND } from "../index.js"; import { prepareOpenCodeRuntimeConfig, prepareManagedOpenCodeRemoteHomes } from "./runtime-config.js"; @@ -141,9 +141,16 @@ export async function testEnvironment( // still has the path available for cleanup in `finally` — otherwise the // `fs.mkdtemp` directory leaks on the early-throw path. let preparedRuntimeWorkspaceLocalDir: string | null = null; + let nativeProbeWorkspaceLocalDir: string | null = null; try { let runtimeTarget: AdapterExecutionTarget | null = target ?? null; let runtimeCwd = cwd; + // Native workspaces are assigned at execution time. A configuration probe + // without an explicit workspace must not run in the server's checkout. + if (!targetIsRemote && ctx.adapterType === "paperclip_runner" && !asString(config.cwd, "").trim()) { + nativeProbeWorkspaceLocalDir = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-opencode-native-probe-")); + runtimeCwd = nativeProbeWorkspaceLocalDir; + } if (targetIsRemote) { preparedRuntimeWorkspaceLocalDir = await fs.mkdtemp(path.join(os.tmpdir(), `paperclip-opencode-envtest-${runId}-`)); const preparedExecutionTargetRuntime = await prepareAdapterExecutionTargetRuntime({ @@ -223,12 +230,30 @@ export async function testEnvironment( let modelValidationPassed = false; const configuredModel = asString(config.model, "").trim(); + // The native runner already selects a concrete model. Its real hello probe + // verifies model access and credentials; a full catalog scan adds another + // cold CLI launch (and discovery retries) without verifying that request. + const nativeModelProbe = ctx.adapterType === "paperclip_runner" && Boolean(configuredModel); + // Model discovery and validation use local child processes against // OpenCode's `models` subcommand and JSON config; these are not yet // wired through the execution target. When probing a remote env, skip // discovery/validation and rely on the remote hello probe to surface // model/auth issues directly. - if (targetIsRemote && configuredModel) { + if (nativeModelProbe) { + try { + requireOpenCodeModelId(configuredModel); + modelValidationPassed = true; + checks.push({ + code: "opencode_model_validation_via_probe", + level: "info", + message: "The configured native-runner model will be validated by the hello probe.", + }); + } catch (error) { + checks.push({ code: "opencode_model_invalid", level: "error", + message: error instanceof Error ? error.message : "Configured model is invalid." }); + } + } else if (targetIsRemote && configuredModel) { checks.push({ code: "opencode_model_validation_skipped_remote", level: "info", @@ -305,7 +330,7 @@ export async function testEnvironment( const modelUnavailable = checks.some((check) => check.code === "opencode_hello_probe_model_unavailable"); if (!configuredModel && !modelUnavailable) { // No model configured – skip model requirement if no model-related checks exist - } else if (!targetIsRemote && configuredModel && canRunProbe) { + } else if (!nativeModelProbe && !targetIsRemote && configuredModel && canRunProbe) { try { await ensureOpenCodeModelConfiguredAndAvailable({ model: configuredModel, @@ -430,6 +455,9 @@ export async function testEnvironment( } } } finally { + if (nativeProbeWorkspaceLocalDir) { + await fs.rm(nativeProbeWorkspaceLocalDir, { recursive: true, force: true }).catch(() => {}); + } await restoreWorkspace?.(); if (!restoreWorkspace && preparedRuntimeWorkspaceLocalDir) { // Reached when `prepareAdapterExecutionTargetRuntime` threw before diff --git a/packages/paperclip-runner/README.md b/packages/paperclip-runner/README.md index b4df933cfe0..38c61877418 100644 --- a/packages/paperclip-runner/README.md +++ b/packages/paperclip-runner/README.md @@ -238,6 +238,37 @@ provider-native operations. Full auto is resolved consistently for agent creation, adapter conversion, direct driver launches, and fresh/resumed turns. Explicitly stored restrictive modes still apply. +Native OpenCode forwards incremental text and reasoning parts as they arrive; +the final full snapshot selects the final response without repeating streamed +text. Delta parts must match an observed message and part identity. Identical +chunks without event IDs remain distinct tokens. Each isolated launch disables +automatic updates, remote model-catalog refreshes, and default plugins; the +runner supplies the pinned executable, selected model, and assigned MCP tools. +Provider inference and reasoning still contribute to time to first text. These +startup settings do not disable model reasoning. + +On macOS, the verified OpenCode executable keeps its private launch pathname +until the provider process exits. Removing that pathname immediately after +spawn can kill or stall the signed binary before its health check succeeds. +Ownership, permissions, and file-identity checks remain enforced; cleanup also +runs on failed launches, and retries create a fresh launch snapshot. + +Call `session.startTurn({ message, reasoningMode: "disabled" })` to disable +reasoning for one OpenCode/OpenRouter turn. `CapabilityLiveSession.sendMessage` +accepts the same option in its second argument. The choice travels in the durable +`turn.start` command and selects an OpenCode model variant for that prompt only. +Omitting it (or passing `"default"`) on the next turn restores provider defaults, +including on a warm or recovered session. It is not an environment variable or +agent-wide setting. No automatic decision policy selects it. The model must +support disabling reasoning; other runner providers reject an explicit selection +before starting work. Native sessions expose support through the +`perTurnReasoning` capability. + +OpenCode's proxy sends agent instructions once per turn through the system prompt. The +task envelope retains task-specific constraints and the completion contract, +without repeating the system instructions. This applies to all users of the +proxy; already-recorded conversation history is not rewritten. + The runner's authenticated bridge and controller still enforce company access, action claims, task modes, and governed approvals. Provider permission defaults do not change workspace isolation or grant credentials or connection access. diff --git a/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs b/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs index 29494a4e21a..61432b9dbe0 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs @@ -386,6 +386,13 @@ pub struct CodexSkillInput { pub path: String, } +/// Optional inputs for one turn; omitted fields preserve provider defaults. +#[derive(Clone, Copy, Debug, Default)] +pub struct CodexTurnOptions<'a> { + pub skills: &'a [CodexSkillInput], + pub reasoning_mode: Option<&'a str>, +} + impl CodexSkillInput { pub fn validate(&self) -> Result<(), LocalRunnerError> { if self.input_type != "skill" @@ -1670,15 +1677,26 @@ impl CodexProvider { } pub fn start_turn(&mut self, message: &str, cwd: &str) -> Result { - self.start_turn_with_skills(message, cwd, &[]) + self.start_turn_with_options(message, cwd, CodexTurnOptions::default()) } - pub fn start_turn_with_skills( + pub fn start_turn_with_options( &mut self, message: &str, cwd: &str, - skills: &[CodexSkillInput], + options: CodexTurnOptions<'_>, ) -> Result { + let CodexTurnOptions { + skills, + reasoning_mode, + } = options; + if reasoning_mode.is_some_and(|mode| { + self.config.provider != "opencode" || !matches!(mode, "default" | "disabled") + }) { + return Err(LocalRunnerError::invalid( + "turn.reasoningMode requires OpenCode and must be default or disabled", + )); + } if skills.len() > 64 || (self.config.provider != "codex" && !skills.is_empty()) { return Err(LocalRunnerError::invalid( "explicit skills require Codex and at most 64 selections", @@ -1727,6 +1745,9 @@ impl CodexProvider { let turn_params_object = turn_params .as_object_mut() .expect("Codex turn parameters are an object"); + if let Some(mode) = reasoning_mode { + turn_params_object.insert("reasoningMode".to_owned(), json!(mode)); + } if self.permission_profile == "paperclip-runner-external-sandbox" { turn_params_object.insert( "sandboxPolicy".to_owned(), diff --git a/packages/paperclip-runner/runner/crates/runner-core/src/provider_backend.rs b/packages/paperclip-runner/runner/crates/runner-core/src/provider_backend.rs index 23268296bd6..329c3dc7bf4 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/src/provider_backend.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/src/provider_backend.rs @@ -13,7 +13,7 @@ use serde_json::{json, Value}; use sha2::{Digest, Sha256}; use crate::codex_provider::{ - CodexProvider, CodexProviderConfig, CodexProviderEvent, CodexSkillInput, + CodexProvider, CodexProviderConfig, CodexProviderEvent, CodexSkillInput, CodexTurnOptions, ProviderStartupObservation, ProviderStartupStage, RejectedAcceptedTurn, MAX_SETTLED_PROVIDER_TURN_IDS, }; @@ -2903,7 +2903,24 @@ impl CodexCommandExecutor { .validate() .map_err(|error| DurableRunnerError::invalid(error.to_string()))?; } + let reasoning_mode = match payload.get("reasoningMode") { + None => None, + Some(Value::String(mode)) if matches!(mode.as_str(), "default" | "disabled") => { + Some(mode.as_str()) + } + Some(_) => return Err(DurableRunnerError::invalid("invalid turn.reasoningMode")), + }; self.restore_provider_if_needed()?; + if reasoning_mode.is_some() + && self + .state + .as_ref() + .is_none_or(|state| state.config.provider != "opencode") + { + return Err(DurableRunnerError::invalid( + "turn.reasoningMode requires OpenCode", + )); + } if self .state .as_ref() @@ -2986,7 +3003,14 @@ impl CodexCommandExecutor { rejected_accepted_turn, ) = { let provider = self.ensure_provider()?; - let result = provider.start_turn_with_skills(text, &cwd, &skills); + let result = provider.start_turn_with_options( + text, + &cwd, + CodexTurnOptions { + skills: &skills, + reasoning_mode, + }, + ); ( result, provider.completed_turn_authority().is_some(), @@ -6068,6 +6092,27 @@ mod tests { executor.state = Some(state); executor.restore_checked = true; + for mode in [ + json!(null), + json!(false), + json!("bad"), + json!("disabled"), + json!("default"), + ] { + let error = executor + .start_turn(&json!({"text": "must not reach the provider", "reasoningMode": mode})) + .unwrap_err(); + assert!(error.to_string().contains("reasoningMode")); + assert!(executor.provider.is_none()); + assert!( + !executor + .state + .as_ref() + .unwrap() + .ambiguous_turn_start_pending + ); + } + let error = executor .start_turn(&json!({"text": "must not reach the provider"})) .expect_err("the exact regular backlog limit must reject admission"); diff --git a/packages/paperclip-runner/src/backends/harness-driver-backend.test.ts b/packages/paperclip-runner/src/backends/harness-driver-backend.test.ts index e1542606a24..a5cc423e60e 100644 --- a/packages/paperclip-runner/src/backends/harness-driver-backend.test.ts +++ b/packages/paperclip-runner/src/backends/harness-driver-backend.test.ts @@ -97,6 +97,34 @@ const driver: HarnessDriver = { }; describe("HarnessDriverBackend", () => { + it.each(["default", "disabled"] as const)("rejects unsupported reasoning %s before starting provider work", async reasoningMode => { + const startTurn = vi.fn(async () => ({ turnId: "turn-1" })); + const session = Object.assign(new FakeHarnessSession(), { startTurn }); + const backend = new HarnessDriverBackend({ ...driver, openSession: async () => session }); + const opened = await backend.openSession({ identity: { + runId: "run-1", sessionId: "session-1", companyId: "company-1", issueId: "issue-1", agentId: "agent-1", + } }); + expect((await opened.capabilities()).perTurnReasoning).toBe(false); + const message = { role: "user" as const, text: "Hi" }; + await expect(opened.startTurn({ message, reasoningMode })).rejects.toThrow("Per-turn reasoning is not supported"); + expect(startTurn).not.toHaveBeenCalled(); + await expect(opened.startTurn({ message })).resolves.toEqual({ turnId: "turn-1" }); + expect(startTurn).toHaveBeenCalledExactlyOnceWith({ message }); + }); + + it("forwards reasoning when the provider explicitly supports it", async () => { + const startTurn = vi.fn(async () => ({ turnId: "turn-1" })); + const session = Object.assign(new FakeHarnessSession(), { startTurn, supportsTurnReasoning: () => true }); + const backend = new HarnessDriverBackend({ ...driver, openSession: async () => session }); + const opened = await backend.openSession({ identity: { + runId: "run-1", sessionId: "session-1", companyId: "company-1", issueId: "issue-1", agentId: "agent-1", + } }); + expect((await opened.capabilities()).perTurnReasoning).toBe(true); + const input = { message: { role: "user" as const, text: "Hi" }, reasoningMode: "disabled" as const }; + await opened.startTurn(input); + expect(startTurn).toHaveBeenCalledExactlyOnceWith(input); + }); + it("refreshes negotiated controls after a warm handshake and separates follow-ups", async () => { let controls = { steering: false, queuedFollowUp: false }; const steer = vi.fn(async () => {}); diff --git a/packages/paperclip-runner/src/backends/harness-driver-backend.ts b/packages/paperclip-runner/src/backends/harness-driver-backend.ts index 6e3897e4e57..fcdb43e16b7 100644 --- a/packages/paperclip-runner/src/backends/harness-driver-backend.ts +++ b/packages/paperclip-runner/src/backends/harness-driver-backend.ts @@ -454,6 +454,7 @@ class HarnessNativeSession implements NativeSession { return { resume: true, typedEvents: true, + perTurnReasoning: this.#session.supportsTurnReasoning?.() === true, steering: (negotiated?.steering ?? this.#steeringSupported) && this.#session.steer !== undefined, queuedFollowUp: negotiated?.queuedFollowUp === true && this.#session.steer !== undefined, interruption: this.#session.interrupt !== undefined, @@ -710,6 +711,9 @@ class HarnessNativeSession implements NativeSession { // The provider has no active turn to interrupt yet. Do not launch work // whose events cancellation would suppress and leave the owner waiting. if (this.#explicitlyCancelled) throw new Error("native_session_cancelled"); + if (input.reasoningMode !== undefined && this.#session.supportsTurnReasoning?.() !== true) { + throw new Error("Per-turn reasoning is not supported by this provider"); + } this.#terminal = null; try { const started = await this.#session.startTurn(input); diff --git a/packages/paperclip-runner/src/backends/native-instruction-measurement.test.ts b/packages/paperclip-runner/src/backends/native-instruction-measurement.test.ts index 98507b24338..2c5592af47e 100644 --- a/packages/paperclip-runner/src/backends/native-instruction-measurement.test.ts +++ b/packages/paperclip-runner/src/backends/native-instruction-measurement.test.ts @@ -190,11 +190,10 @@ describe("direct OpenCode HTTP instruction boundary", () => { const request = requests.at(-1)!; expect(request.providerID).toBe("openrouter"); expect(request.modelID).toBe("deepseek/deepseek-v4-flash-0731"); const text = request.parts[0].text as string; + expect(request.system).toBe(nativeSystemInstructions(value)); if (schema === "v4" && phase === "start") { - expect(request.system).toBe(nativeSystemInstructions(input)); expect(JSON.parse(text).task.constraints).toEqual(nativeTaskConstraints(input)); } else { - expect(request.system).toBeUndefined(); expect(JSON.parse(text)).toEqual(envelope); } directOpenCodeReceipts.push({ provider: "opencode", schema, phase, request: measure(request), diff --git a/packages/paperclip-runner/src/cli/opencode-app-server-proxy.test.ts b/packages/paperclip-runner/src/cli/opencode-app-server-proxy.test.ts index 0c672b356af..65250a0f8d5 100644 --- a/packages/paperclip-runner/src/cli/opencode-app-server-proxy.test.ts +++ b/packages/paperclip-runner/src/cli/opencode-app-server-proxy.test.ts @@ -9,7 +9,7 @@ import { rmSync, writeFileSync, } from "node:fs"; -import { spawnSync } from "node:child_process"; +import { spawn, spawnSync } from "node:child_process"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { @@ -28,7 +28,7 @@ describe("OpenCode runnerd proxy executable", () => { ])).toEqual({ command: "/proc/self/fd/7", commandFd: 7 }); }); - it("executes the inherited artifact through the nested child fd mapping", () => { + it("executes the inherited artifact through the nested child fd mapping", async () => { if (process.platform === "win32") return; if (process.platform === "darwin") { const root = mkdtempSync(join(tmpdir(), "paperclip-opencode-nested-")); @@ -45,22 +45,27 @@ describe("OpenCode runnerd proxy executable", () => { TRUSTED_OPENCODE_EXECUTABLE_ARG, command, ]); - binding.commandLifecycle?.beforeSpawn(); - const child = spawnSync(binding.command, [], { encoding: "utf8" }); - expect(child.error).toBeUndefined(); - expect(child.status).toBe(0); - expect(child.stdout).toBe("verified-nested-spawn"); - binding.commandLifecycle?.afterSpawn(); - expect(() => lstatSync(command)).toThrow(); - expect(() => lstatSync(directory)).toThrow(); - binding.commandLifecycle?.beforeSpawn(); - const retriedChild = spawnSync(binding.command, [], { encoding: "utf8" }); - expect(retriedChild.error).toBeUndefined(); - expect(retriedChild.status).toBe(0); - expect(retriedChild.stdout).toBe("verified-nested-spawn"); - binding.commandLifecycle?.afterSpawn(); - expect(() => lstatSync(command)).toThrow(); - expect(() => lstatSync(directory)).toThrow(); + for (let attempt = 0; attempt < 2; attempt += 1) { + binding.commandLifecycle!.beforeSpawn(); + const child = spawn(binding.command, [], { stdio: ["ignore", "pipe", "pipe"] }); + let stdout = ""; + child.stdout!.on("data", chunk => { stdout += chunk; }); + const exited = new Promise((resolve, reject) => { + child.once("error", reject); + child.once("close", resolve); + }); + binding.commandLifecycle!.afterSpawn(); + // Exercise the production async boundary, not spawnSync followed by + // cleanup after the executable has already finished. + expect(lstatSync(command).isFile()).toBe(true); + expect(() => binding.commandLifecycle!.beforeSpawn()).toThrow("still in use"); + expect(await exited).toBe(0); + expect(stdout).toBe("verified-nested-spawn"); + binding.commandLifecycle!.afterExit(); + binding.commandLifecycle!.afterExit(); + expect(() => lstatSync(command)).toThrow(); + expect(() => lstatSync(directory)).toThrow(); + } } finally { rmSync(root, { recursive: true, force: true }); } @@ -131,6 +136,7 @@ describe("OpenCode runnerd proxy task envelope", () => { criterionIds: ["objective", "verification"], }, })).toMatchObject({ + constraints: ["Use Paperclip MCP tools for semantic operations."], completionContract: { revision: "17", criteria: [ diff --git a/packages/paperclip-runner/src/cli/opencode-app-server-proxy.ts b/packages/paperclip-runner/src/cli/opencode-app-server-proxy.ts index 11cc112dea7..0e60ac1f245 100644 --- a/packages/paperclip-runner/src/cli/opencode-app-server-proxy.ts +++ b/packages/paperclip-runner/src/cli/opencode-app-server-proxy.ts @@ -1,4 +1,5 @@ #!/usr/bin/env node +import { parseOpenCodeReasoningMode } from "../drivers/opencode/reasoning-mode.js"; import { createInterface } from "node:readline"; import { readFileSync } from "node:fs"; import { resolve } from "node:path"; @@ -365,6 +366,7 @@ async function handle(message: RpcMessage): Promise { .join("\n"); const turn = await session.startTurn({ message: { role: "user", text: messageText }, + ...(params.reasoningMode === undefined ? {} : { reasoningMode: parseOpenCodeReasoningMode(params.reasoningMode) }), }); activeTurnId = turn.turnId; // OpenCode normally publishes session/turn startup over SSE, but a fast diff --git a/packages/paperclip-runner/src/cli/opencode-proxy-command.ts b/packages/paperclip-runner/src/cli/opencode-proxy-command.ts index 2da3349bf34..3d73ce8d22b 100644 --- a/packages/paperclip-runner/src/cli/opencode-proxy-command.ts +++ b/packages/paperclip-runner/src/cli/opencode-proxy-command.ts @@ -39,6 +39,7 @@ export function trustedOpenCodeLaunchBinding( commandLifecycle?: { beforeSpawn(): void; afterSpawn(): void; + afterExit(): void; }; } { const command = args.length === 2 && args[0] === TRUSTED_OPENCODE_EXECUTABLE_ARG @@ -119,8 +120,7 @@ export function trustedOpenCodeLaunchBinding( let materializedIdentity: { dev: number; ino: number } | undefined; const materializeForSpawn = () => { if (materialized) { - validateMacSnapshot(materializedIdentity); - return; + throw new Error("Verified OpenCode executable is still in use"); } let destinationFd: number | undefined; try { @@ -184,10 +184,15 @@ export function trustedOpenCodeLaunchBinding( // attackers are outside the documented local-host trust boundary in // docs/durable-recovery.md. Keep the verified source on an unlinked // descriptor, rematerialize only at the syscall boundary (including - // retries), then remove the executable pathname immediately. + // retries). Keep its private pathname until the child exits: deleting + // a signed executable during macOS startup can kill or stall the child. beforeSpawn: materializeForSpawn, afterSpawn() { validateMacSnapshot(materializedIdentity); + }, + afterExit() { + if (!materialized) return; + validateMacSnapshot(materializedIdentity); unlinkSync(command); rmdirSync(dirname(command)); materialized = false; diff --git a/packages/paperclip-runner/src/cli/opencode-proxy-task-envelope.ts b/packages/paperclip-runner/src/cli/opencode-proxy-task-envelope.ts index dd30c666d90..535b64d6d30 100644 --- a/packages/paperclip-runner/src/cli/opencode-proxy-task-envelope.ts +++ b/packages/paperclip-runner/src/cli/opencode-proxy-task-envelope.ts @@ -30,7 +30,8 @@ export function openCodeProxyTaskEnvelope(params: Record) { } : {}), constraints: [ - text(params.baseInstructions, "Complete only the supplied task."), + // baseInstructions already travels in the system prompt. Keep task + // constraints here without duplicating the agent instruction bundle. "Use Paperclip MCP tools for semantic operations.", ], }); diff --git a/packages/paperclip-runner/src/contracts/harness-driver.ts b/packages/paperclip-runner/src/contracts/harness-driver.ts index 410496afce5..a520a146431 100644 --- a/packages/paperclip-runner/src/contracts/harness-driver.ts +++ b/packages/paperclip-runner/src/contracts/harness-driver.ts @@ -498,6 +498,7 @@ export interface HarnessSessionRecoveryResult { } export interface HarnessSession { + supportsTurnReasoning?(): boolean; turnControlCapabilities?(): NativeTurnControlCapabilities | null; ids(): { driverSessionId: string; @@ -511,6 +512,8 @@ export interface HarnessSession { /** Set by orchestration only after successful provider-session recovery. */ continuation?: true; requestedCollaborationMode?: "default" | "plan"; + /** OpenCode/OpenRouter only. Applies to this turn, never subsequent turns. */ + reasoningMode?: "default" | "disabled"; }): Promise<{ turnId: string; effectiveCollaborationMode?: "default" | "plan"; diff --git a/packages/paperclip-runner/src/contracts/native-session-backend.ts b/packages/paperclip-runner/src/contracts/native-session-backend.ts index f473b56a03e..251d10748e7 100644 --- a/packages/paperclip-runner/src/contracts/native-session-backend.ts +++ b/packages/paperclip-runner/src/contracts/native-session-backend.ts @@ -193,6 +193,8 @@ export interface NativeSession { /** Set by orchestration only after successful provider-session recovery. */ continuation?: true; requestedCollaborationMode?: "default" | "plan"; + /** OpenCode/OpenRouter only. Applies to this turn, never subsequent turns. */ + reasoningMode?: "default" | "disabled"; }): Promise<{ turnId: string; effectiveCollaborationMode?: "default" | "plan"; diff --git a/packages/paperclip-runner/src/contracts/types.ts b/packages/paperclip-runner/src/contracts/types.ts index 81bc3d24723..8a5dd2a49bd 100644 --- a/packages/paperclip-runner/src/contracts/types.ts +++ b/packages/paperclip-runner/src/contracts/types.ts @@ -41,6 +41,8 @@ export interface NativeSessionCapabilities { runtimeRequestHandoff?: boolean; goals?: boolean; threadLineage?: boolean; + /** Can select reasoning for one turn without changing subsequent turns. */ + perTurnReasoning?: boolean; collaborationModes?: Array<"default" | "plan">; unsupported?: string[]; } diff --git a/packages/paperclip-runner/src/drivers/codex/app-server-transport.ts b/packages/paperclip-runner/src/drivers/codex/app-server-transport.ts index 3e08de1f22f..a65138a435b 100644 --- a/packages/paperclip-runner/src/drivers/codex/app-server-transport.ts +++ b/packages/paperclip-runner/src/drivers/codex/app-server-transport.ts @@ -35,6 +35,7 @@ export type CodexServerRequestHandler = ( ) => Promise>; export interface CodexAppServerTransport { + supportsTurnReasoning?(): boolean; /** Runner-owned live capability projection; absent on native Codex transports. */ turnControlCapabilities?(): NativeTurnControlCapabilities | null; request( diff --git a/packages/paperclip-runner/src/drivers/codex/codex-app-server-driver.context-control.test.ts b/packages/paperclip-runner/src/drivers/codex/codex-app-server-driver.context-control.test.ts index cd9ee34129d..021c0ab99f7 100644 --- a/packages/paperclip-runner/src/drivers/codex/codex-app-server-driver.context-control.test.ts +++ b/packages/paperclip-runner/src/drivers/codex/codex-app-server-driver.context-control.test.ts @@ -45,6 +45,19 @@ import { import { rehydrateRunnerdItemNotification } from "../../live/runnerd-codex-transport.js"; describe("Codex app-server Codex driver", () => { + it.each(["default", "disabled"] as const)("rejects unsupported reasoning %s without sending a turn RPC", async reasoningMode => { + const transport = new FakeCodexTransport(); + const session = await makeDriver([transport]).openSession({ + runId: "run-reasoning", normalizedSessionId: "normalized-reasoning", workingDirectory: WORKSPACE, + }); + const message = { role: "user" as const, text: "Hi" }; + await expect(session.startTurn({ message, reasoningMode })).rejects.toThrow("Per-turn reasoning is not supported"); + expect(transport.calls.filter(call => call.method === "turn/start")).toHaveLength(0); + await session.startTurn({ message }); + expect(transport.calls.filter(call => call.method === "turn/start")).toHaveLength(1); + await session.close({ reason: "fixture complete" }); + }); + it("retains one correlation-bound steering acknowledgement after the rehydrated runnerd echo", async () => { const transport = new FakeCodexTransport("thread-root"); const session = await makeDriver([transport]).openSession({ diff --git a/packages/paperclip-runner/src/drivers/codex/codex-harness-session.ts b/packages/paperclip-runner/src/drivers/codex/codex-harness-session.ts index 4efc9eec23d..983d9322eee 100644 --- a/packages/paperclip-runner/src/drivers/codex/codex-harness-session.ts +++ b/packages/paperclip-runner/src/drivers/codex/codex-harness-session.ts @@ -82,6 +82,10 @@ export class CodexHarnessSession } } + supportsTurnReasoning(): boolean { + return this.transport.supportsTurnReasoning?.() === true; + } + turnControlCapabilities() { if (this.driverKind === "acpx_runtime") { return this.transport.turnControlCapabilities?.() ?? { steering: false, queuedFollowUp: false }; @@ -157,11 +161,16 @@ export class CodexHarnessSession /** Set by orchestration only after successful provider-session recovery. */ continuation?: true; requestedCollaborationMode?: "default" | "plan"; + /** OpenCode/OpenRouter only. Applies to this turn, never subsequent turns. */ + reasoningMode?: "default" | "disabled"; }): Promise<{ turnId: string; effectiveCollaborationMode: "default" | "plan"; }> { this.assertProtocolIntegrity(); + if (input.reasoningMode !== undefined && !this.supportsTurnReasoning()) { + throw new Error("Per-turn reasoning is not supported by this provider"); + } if (this.protocolFailed && this.protocolFailureCode) { throw new NativeProviderTerminalFailure(this.protocolFailureCode, false, this.protocolFailureMessage ?? undefined); } @@ -231,6 +240,7 @@ export class CodexHarnessSession try { response = await this.transport.request("turn/start", { threadId: this.opened.threadId, + ...(input.reasoningMode === undefined ? {} : { reasoningMode: input.reasoningMode }), ...(this.reasoningEffort ? { effort: this.reasoningEffort } : {}), cwd: this.opened.context.workingDirectory, permissions: diff --git a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts index 59d740d9940..75032bd82c7 100644 --- a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts +++ b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts @@ -906,6 +906,8 @@ describe("OpenCodeServerDriver", () => { expect(environment.keys).not.toContain("UNRELATED_SECRET"); expect(environment.keys).not.toContain("PAPERCLIP_PROVIDER_TRACE_PATH"); expect(environment.projectConfigDisabled).toBe("true"); + expect(environment.modelsFetchDisabled).toBe("true"); + expect(environment.defaultPluginsDisabled).toBe("true"); expect(mcpEvidence.tools).toEqual( expect.arrayContaining(["paperclip_finish", "paperclip_block"]), ); @@ -920,6 +922,46 @@ describe("OpenCodeServerDriver", () => { expect(diagnostics.join("\n")).toContain("[REDACTED]"); }); + it.each(["role-first", "role-last", "role-last-long"])("preserves incremental text and completion with identity checks (%s)", async (roleOrder) => { + await chmod(fixture, 0o755); + const root = await mkdtemp(join(tmpdir(), "paperclip-opencode-stream-")); + const workspace = await mkdtemp(join(tmpdir(), "paperclip-opencode-stream-workspace-")); + roots.push(root, workspace); + const driver = new OpenCodeServerDriver({ + model: "openrouter/deepseek/deepseek-v4-flash-0731", + runtimeDirectory: root, + command: fixture, + environment: { PATH: process.env.PATH }, + }); + const session = await driver.openSession({ runId: "stream", normalizedSessionId: "stream", workingDirectory: workspace }); + try { + await session.startTurn({ message: { role: "user", text: `incremental-stream ${roleOrder}` } }); + const deltas: string[] = []; + const reasoning: string[] = []; + const events: PrpEvent[] = []; + for await (const event of session.events()) { + events.push(event); + if (event.eventType === "item.delta" && event.payload.kind === "reasoning") reasoning.push(String(event.payload.text)); + if (event.eventType === "item.delta" && event.payload.kind === "agentMessage") { + deltas.push(String(event.payload.text)); + // The fixture cannot finish its turn until a chunk reaches us. + if (deltas.length === 1) { + expect(await readFile(join(root, "stream", "data", "stream-released"), "utf8").catch(() => null)).toBeNull(); + } + await writeFile(join(root, "stream", "data", "release-stream"), "ready"); + } + if (TURN_TERMINAL_EVENT_TYPES.has(event.eventType)) break; + } + const expectedText = `Hi ${"👋".repeat(roleOrder === "role-last-long" ? 120 : 2)}`; + expect(deltas).toEqual(roleOrder === "role-first" ? ["Hi ", "👋", "👋"] : [expectedText]); + expect(reasoning).toEqual(["Thinking"]); + expect(events.filter(event => event.eventType === "item.completed" && event.payload.kind === "agentMessage") + .map(event => event.payload.text)).toEqual([expectedText]); + } finally { + await session.close({ reason: "stream test complete" }); + } + }); + it("maps OpenCode's normal abort error to cancellation without a false provider failure notice", async () => { await chmod(fixture, 0o755); const root = await mkdtemp(join(tmpdir(), "paperclip-opencode-abort-")); @@ -1497,7 +1539,7 @@ describe("OpenCodeServerDriver", () => { await session.close({ reason: "test" }); }); - it("sends only the authoritative wake envelope when resuming an existing provider session", async () => { + it("resends system instructions without repeating the task envelope on recovery", async () => { await chmod(fixture, 0o755); const root = await mkdtemp( join(tmpdir(), "paperclip-opencode-resume-context-"), @@ -1553,7 +1595,7 @@ describe("OpenCodeServerDriver", () => { ], }); expect(submittedPrompt).not.toHaveProperty("tools"); - expect(submittedPrompt).not.toHaveProperty("system"); + expect(submittedPrompt).toHaveProperty("system", "large original system context"); await recovered!.session!.close({ reason: "recovery-test" }); }); @@ -1942,9 +1984,11 @@ describe("OpenCodeServerDriver", () => { "*": permissionMode, external_directory: { "*": "deny", [`${workspace}/**`]: "allow" }, }); + expect(config.autoupdate).toBe(false); expect(config.provider.openrouter.models).toHaveProperty( "deepseek/deepseek-v4-flash-0731", ); + expect(config.provider.openrouter.models["deepseek/deepseek-v4-flash-0731"].options).toBeUndefined(); expect(config.permission.paperclip_finish).toBeUndefined(); expect(config.permission["paperclip_*"]).toBe("allow"); await session.close({ reason: "permission mode test complete" }); @@ -2440,6 +2484,9 @@ describe("OpenCodeServerDriver", () => { afterSpawn: () => { commandLifecycle.push("after"); }, + afterExit: () => { + commandLifecycle.push("exit"); + }, }, fetch: async (input, init) => { if ( @@ -2465,9 +2512,31 @@ describe("OpenCodeServerDriver", () => { expect(spawns.length).toBeGreaterThanOrEqual(2); expect(spawns.length).toBeLessThanOrEqual(3); expect(commandLifecycle).toEqual( - Array.from({ length: spawns.length }, () => ["before", "after"]).flat(), + [...Array.from({ length: spawns.length - 1 }, () => ["before", "after", "exit"]).flat(), "before", "after"], ); await session.close({ reason: "test" }); + expect(commandLifecycle).toEqual( + Array.from({ length: spawns.length }, () => ["before", "after", "exit"]).flat(), + ); + }); + + it("releases the executable binding when spawning fails before health checks", async () => { + const root = await mkdtemp(join(tmpdir(), "paperclip-opencode-spawn-error-")); + roots.push(root); + const lifecycle: string[] = []; + const driver = new OpenCodeServerDriver({ + model: "openrouter/deepseek/deepseek-v4-flash-0731", + runtimeDirectory: root, + command: join(root, "missing-executable"), + commandLifecycle: { + beforeSpawn: () => { lifecycle.push("before"); }, + afterSpawn: () => { lifecycle.push("spawned"); }, + afterExit: () => { lifecycle.push("released"); }, + }, + }); + await expect(driver.openSession({ runId: "spawn-error", normalizedSessionId: "spawn-error", + workingDirectory: root })).rejects.toThrow("ENOENT"); + expect(lifecycle).toEqual(["before", "released"]); }); it("reports startup exit details with stderr captured after spawn and redacted", async () => { diff --git a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts index 81abbfd3f06..4ce6a5b9dd9 100644 --- a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts +++ b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts @@ -19,6 +19,8 @@ import { getCACertificates } from "node:tls"; import { pipeline } from "node:stream/promises"; import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; +import { parseOpenCodeReasoningMode, type OpenCodeReasoningMode } from "./reasoning-mode.js"; + import { CODEX_SKILLLESS_BASE_INSTRUCTIONS, createCodexTaskEnvelope, @@ -176,6 +178,7 @@ export interface OpenCodeServerDriverOptions { commandLifecycle?: { beforeSpawn(): void; afterSpawn(): void; + afterExit?(): void; }; runtimeDirectory: string; systemInstructions?: string; @@ -479,6 +482,7 @@ class OpenCodeHarnessSession implements HarnessSession { Array> >(); readonly #partText = new Map(); + readonly #streamingParts = new Map>(); readonly #completedTextPartIds = new Set(); readonly #completedReasoningPartIds = new Set(); readonly #completedTextParts: Array<{ @@ -640,12 +644,22 @@ class OpenCodeHarnessSession implements HarnessSession { .finally(() => this.#pumpEvents()); } + supportsTurnReasoning(): boolean { + return this.#model.startsWith("openrouter/"); + } + async startTurn(input: { message: NativeUserMessage; + reasoningMode?: OpenCodeReasoningMode; }): Promise<{ turnId: string }> { if (this.#activeTurnId !== null) throw new Error("OpenCode session already has an active turn"); + const reasoningMode = parseOpenCodeReasoningMode(input.reasoningMode); + if (input.reasoningMode !== undefined && !this.#model.startsWith("openrouter/")) { + throw new Error("Per-turn OpenCode reasoning is supported only for OpenRouter models"); + } const turnId = `turn-${randomBytes(12).toString("hex")}`; + this.#streamingParts.clear(); this.#activeTurnId = turnId; this.#emit("turn.submitted", { envelopeSchema: this.#taskEnvelope.schema, @@ -655,12 +669,9 @@ class OpenCodeHarnessSession implements HarnessSession { this.#emit("turn.started", { status: "inProgress" }, { turnId }); const [providerID, ...modelParts] = this.#model.split("/"); const modelID = modelParts.join("/"); - // A resumed OpenCode provider session already retains the original system - // instructions and task envelope in its conversation. Repeating both on - // every Paperclip continuation can overflow smaller context windows and - // OpenCode then completes with `finish: unknown` and zero tokens. The - // native model envelope still carries the authoritative wake delta, - // interaction responses, completion contract, and current issue context. + // Keep the task envelope only on the initial task-mode wake. OpenCode + // rebuilds system instructions from the latest user message, so system + // instructions must still accompany every prompt (including recovery). const prompt = this.#sendFullContext ? JSON.stringify({ task: this.#taskEnvelope, @@ -682,12 +693,15 @@ class OpenCodeHarnessSession implements HarnessSession { // at this HTTP boundary. providerID, modelID, + // Always select the empty default variant on an ordinary turn so a + // previous disabled turn cannot change this turn's provider defaults. + ...(providerID === "openrouter" + ? { variant: reasoningMode === "disabled" ? "paperclip-no-reasoning" : "paperclip-default" } + : {}), // Keep question enabled in the isolated config. OpenCode 1.18.32 // turns a prompt's deprecated `tools` map into replacement session // permissions, so a sparse override here discards the session policy. - ...(this.#sendFullContext - ? { system: this.#systemInstructions } - : {}), + system: this.#systemInstructions, parts: [{ type: "text", text: prompt }], }), }, @@ -1470,9 +1484,11 @@ class OpenCodeHarnessSession implements HarnessSession { const event = record(value); const properties = record(event.properties); const type = text(event.type); - const eventId = text(event.id, canonicalJson(value)); - if (this.#seenProviderEvents.has(eventId)) return; - this.#seenProviderEvents.add(eventId); + // Id-less deltas are additive: two identical chunks can be legitimate + // adjacent tokens. Content deduplication would silently lose the second. + const eventId = text(event.id) || (type === "message.part.delta" ? null : canonicalJson(value)); + if (eventId !== null && this.#seenProviderEvents.has(eventId)) return; + if (eventId !== null) this.#seenProviderEvents.add(eventId); if (this.#seenProviderEvents.size > 10_000) this.#seenProviderEvents.delete( this.#seenProviderEvents.values().next().value!, @@ -1565,8 +1581,16 @@ class OpenCodeHarnessSession implements HarnessSession { ); return; } - if (type === "message.part.updated") { - const part = record(properties.part); + if (type === "message.part.updated" || type === "message.part.delta") { + let part = record(properties.part); + if (type === "message.part.delta") { + const previous = this.#streamingParts.get(text(properties.partID)); + // A delta has no role or part type. Only stream text for an observed + // part with matching message identity; never infer assistant identity. + if (!previous || properties.field !== "text" || typeof properties.delta !== "string" + || text(previous.messageID, text(previous.messageId)) !== text(properties.messageID)) return; + part = { ...previous, text: text(previous.text) + properties.delta }; + } const messageId = text(part.messageID, text(part.messageId)); if (!messageId) return; // Resolve the turn this message actually belongs to, not whichever @@ -1574,11 +1598,20 @@ class OpenCodeHarnessSession implements HarnessSession { // must stay attributed to the turn that created that message. const owningTurnId = this.#messageTurnIds.get(messageId) ?? turnId; if (!owningTurnId) return; + if (owningTurnId === turnId && text(part.id) && ["text", "reasoning"].includes(text(part.type))) { + this.#streamingParts.set(text(part.id), part); + } const role = this.#messageRoles.get(messageId); if (role === "assistant") this.#emitAssistantPart(part, owningTurnId); else if (role === undefined) { const pending = this.#pendingMessageParts.get(messageId) ?? []; - if (pending.length < 100) pending.push(part); + // Text deltas are cumulative by this point. Keep the newest snapshot + // (including completion metadata), not the first 100 token chunks. + const previousIndex = ["text", "reasoning"].includes(text(part.type)) && text(part.id) + ? pending.findIndex(candidate => candidate.id === part.id && candidate.type === part.type) + : -1; + if (previousIndex >= 0) pending[previousIndex] = part; + else if (pending.length < 100) pending.push(part); this.#pendingMessageParts.set(messageId, pending); } return; @@ -2233,12 +2266,23 @@ async function startRuntime(input: { input.trace?.addSensitiveValues([providerProxy.token]); } let child: ChildProcess | undefined; + let launchPrepared = false; + const releaseExecutable = () => { + if (!launchPrepared) return; + launchPrepared = false; + try { + input.options.commandLifecycle?.afterExit?.(); + } catch (error) { + input.options.onDiagnostic?.(redact(`OpenCode executable cleanup failed: ${String(error)}`, sensitiveValues)); + } + }; try { const config = { $schema: "https://opencode.ai/config.json", model: input.options.model, small_model: input.options.model, share: "disabled", + autoupdate: false, // The configured entry is already composed exactly once into the session // system prompt; siblings remain available through the read-only root. instructions: [], @@ -2257,7 +2301,15 @@ async function startRuntime(input: { }, } : {}), models: { - [providerModelId]: { name: providerModelId }, + [providerModelId]: { + name: providerModelId, + ...(modelProvider === "openrouter" + ? { variants: { + "paperclip-default": {}, + "paperclip-no-reasoning": { reasoning: { enabled: false } }, + } } + : {}), + }, }, }, }, @@ -2307,6 +2359,11 @@ async function startRuntime(input: { XDG_DATA_HOME: dataHome, XDG_CACHE_HOME: cacheHome, OPENCODE_DISABLE_PROJECT_CONFIG: "true", + // The runner pins the executable, model and assigned MCP tools. A + // fresh isolated cache must not fetch a catalog or install unrelated + // default plugins before it can submit the first prompt. + OPENCODE_DISABLE_MODELS_FETCH: "true", + OPENCODE_DISABLE_DEFAULT_PLUGINS: "true", OPENCODE_SERVER_USERNAME: username, OPENCODE_SERVER_PASSWORD: password, ...(providerProxy ? { NO_PROXY: [input.options.environment?.no_proxy ?? input.options.environment?.NO_PROXY, "127.0.0.1", "localhost"].filter(Boolean).join(",") } : {}), @@ -2319,6 +2376,7 @@ async function startRuntime(input: { stdio[input.options.commandFd] = input.options.commandFd; } input.options.commandLifecycle?.beforeSpawn(); + launchPrepared = true; child = spawn( input.options.command ?? resolvePinnedOpenCodeCommand(), ["serve", "--hostname", "127.0.0.1", "--port", String(port)], @@ -2329,6 +2387,7 @@ async function startRuntime(input: { detached: globalThis.process.platform !== "win32" && isolateProcessGroup, }, ); + child.once("exit", releaseExecutable); if (child.pid !== undefined) { try { input.options.commandLifecycle?.afterSpawn(); @@ -2425,6 +2484,7 @@ async function startRuntime(input: { } catch { providerChild.kill("SIGKILL"); } + await waitForExit(providerChild, 2_000); } await rm(join(configHome, "opencode", "opencode.json"), { force: true, @@ -2441,6 +2501,8 @@ async function startRuntime(input: { await providerProxy?.close(); await bridge.close().catch(() => {}); child?.kill("SIGKILL"); + if (child?.pid) await waitForExit(child, 2_000); + if (!child?.pid || child.exitCode !== null || child.signalCode !== null) releaseExecutable(); await rm(join(configHome, "opencode", "opencode.json"), { force: true, }).catch(() => undefined); diff --git a/packages/paperclip-runner/src/drivers/opencode/prepared-context-transport.test.ts b/packages/paperclip-runner/src/drivers/opencode/prepared-context-transport.test.ts index dd6f34e8677..c4710a971e9 100644 --- a/packages/paperclip-runner/src/drivers/opencode/prepared-context-transport.test.ts +++ b/packages/paperclip-runner/src/drivers/opencode/prepared-context-transport.test.ts @@ -30,6 +30,7 @@ describe("prepared OpenCode context transport", () => { runtimeDirectory: root, command: fixture, conversationMode: "prepared", + systemInstructions: "Prepared agent instructions.", environment: { PATH: process.env.PATH, OPENROUTER_API_KEY: "fixture-key" }, fetch: async (input, init) => { if (String(input).endsWith("/prompt_async")) @@ -45,6 +46,7 @@ describe("prepared OpenCode context transport", () => { }); await session.startTurn({ message: { role: "user", text: "prepared OpenCode initial wake" }, + reasoningMode: "disabled", }); for await (const event of session.events()) { if (event.eventType === "turn.completed") break; @@ -67,6 +69,8 @@ describe("prepared OpenCode context transport", () => { [{ type: "text", text: "completion-only OpenCode continuation" }], ]); expect(JSON.stringify(submitted)).not.toContain('"task"'); + expect(submitted.map(body => body.variant)).toEqual(["paperclip-no-reasoning", "paperclip-default"]); + expect(submitted.map(body => body.system)).toEqual(["Prepared agent instructions.", "Prepared agent instructions."]); await recovered!.session!.close({ reason: "prepared-test-recovery" }); }); }); diff --git a/packages/paperclip-runner/src/drivers/opencode/reasoning-mode.test.ts b/packages/paperclip-runner/src/drivers/opencode/reasoning-mode.test.ts new file mode 100644 index 00000000000..db5e747d46a --- /dev/null +++ b/packages/paperclip-runner/src/drivers/opencode/reasoning-mode.test.ts @@ -0,0 +1,63 @@ +import { chmod, mkdtemp, readFile, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { describe, expect, it } from "vitest"; +import { OpenCodeServerDriver } from "./opencode-server-driver.js"; +import { parseOpenCodeReasoningMode } from "./reasoning-mode.js"; +import { openCodeProxyTaskEnvelope } from "../../cli/opencode-proxy-task-envelope.js"; + +describe("per-turn OpenCode reasoning", () => { + it.each([undefined, "default"])("preserves provider defaults for %s", (value) => { + expect(parseOpenCodeReasoningMode(value)).toBe("default"); + }); + it.each(["", "false", null, false, 0])("rejects malformed mode %s", (value) => { + expect(() => parseOpenCodeReasoningMode(value)).toThrow("turn.reasoningMode"); + }); + it("selects reasoning independently on successive turns and sends instructions once", async () => { + const root = await mkdtemp(join(tmpdir(), "paperclip-turn-reasoning-")); + const fixture = resolve("test/fixtures/fake-opencode-server.mjs"); + await chmod(fixture, 0o755); + const submitted: Array<{ variant?: string; system?: string; parts: Array<{ text: string }> }> = []; + const instructions = "Unique shared agent instructions. Keep the requested scope."; + const driver = new OpenCodeServerDriver({ + model: "openrouter/deepseek/deepseek-v4-flash-0731", + runtimeDirectory: root, + command: fixture, + systemInstructions: instructions, + taskEnvelope: openCodeProxyTaskEnvelope({ baseInstructions: instructions, + completionContract: { revision: "17", criterionIds: ["objective"] } }), + environment: { PATH: process.env.PATH, OPENROUTER_API_KEY: "fixture-key" }, + fetch: async (input, init) => { + if (String(input).endsWith("/prompt_async")) submitted.push(JSON.parse(String(init?.body))); + return fetch(input, init); + }, + }); + let session: Awaited> | undefined; + try { + session = await driver.openSession({ runId: "turn-reasoning", normalizedSessionId: "reasoning", workingDirectory: root }); + await expect(session.startTurn({ message: { role: "user", text: "invalid" }, reasoningMode: "bad" as never })).rejects.toThrow("turn.reasoningMode"); + expect(submitted).toHaveLength(0); + for (const reasoningMode of [undefined, "disabled", undefined] as const) { + await session.startTurn({ message: { role: "user", text: "Reply Hi." }, reasoningMode }); + for await (const event of session.events()) if (event.eventType === "turn.completed") break; + } + expect(submitted.map(body => body.variant)).toEqual(["paperclip-default", "paperclip-no-reasoning", "paperclip-default"]); + expect(submitted[0]!.system).toBe(instructions); + for (const body of submitted) { + expect(body.system).toBe(instructions); + expect(JSON.stringify(body).split(instructions)).toHaveLength(2); + } + const envelope = JSON.parse(submitted[0]!.parts[0].text); + expect(envelope.task.completionContract.revision).toBe("17"); + expect(envelope.task.constraints).toEqual(["Use Paperclip MCP tools for semantic operations."]); + const config = JSON.parse(await readFile(join(root, "reasoning/config/opencode/opencode.json"), "utf8")); + expect(config.provider.openrouter.models["deepseek/deepseek-v4-flash-0731"]).toMatchObject({ + variants: { "paperclip-default": {}, "paperclip-no-reasoning": { reasoning: { enabled: false } } }, + }); + expect(config.provider.openrouter.models["deepseek/deepseek-v4-flash-0731"].options).toBeUndefined(); + } finally { + await session?.close(); + await rm(root, { recursive: true, force: true }); + } + }); +}); diff --git a/packages/paperclip-runner/src/drivers/opencode/reasoning-mode.ts b/packages/paperclip-runner/src/drivers/opencode/reasoning-mode.ts new file mode 100644 index 00000000000..003589ead39 --- /dev/null +++ b/packages/paperclip-runner/src/drivers/opencode/reasoning-mode.ts @@ -0,0 +1,8 @@ +export type OpenCodeReasoningMode = "default" | "disabled"; + +/** Opt-in only. Omitting the setting preserves the provider's own defaults. */ +export function parseOpenCodeReasoningMode(value: unknown): OpenCodeReasoningMode { + if (value === undefined || value === "default") return "default"; + if (value === "disabled") return "disabled"; + throw new Error("turn.reasoningMode must be default or disabled"); +} diff --git a/packages/paperclip-runner/src/live/live-session.ts b/packages/paperclip-runner/src/live/live-session.ts index b79755a6685..c36b8036346 100644 --- a/packages/paperclip-runner/src/live/live-session.ts +++ b/packages/paperclip-runner/src/live/live-session.ts @@ -1514,7 +1514,7 @@ export class CapabilityLiveSession { async sendMessage( message: string, /** Launch-only diagnostics may opt out; qualification campaigns must not. */ - options: { allowMissingUsage?: boolean } = {}, + options: { allowMissingUsage?: boolean; reasoningMode?: "default" | "disabled" } = {}, ): Promise { const value = message.trim(); if (value.length === 0) throw new Error("Capability live messages cannot be empty"); @@ -1617,6 +1617,7 @@ export class CapabilityLiveSession { permissions: CODEX_PERMISSION_PROFILE, runtimeWorkspaceRoots: [this.#config.workingDirectory], input: [userInput(value)], + ...(options.reasoningMode === undefined ? {} : { reasoningMode: options.reasoningMode }), }); } catch (error) { if (this.#pendingTurnAdmission !== admission) { diff --git a/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts b/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts index a024eb20d92..debd0c6e3fe 100644 --- a/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts +++ b/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts @@ -7951,7 +7951,10 @@ it.each([true, false])("preserves prepared OpenCode cleanup errors (primary fail expect((failure as Error).message).not.toContain("fixture-secret"); }); -it("preserves prepared input and completion feedback through runnerd and the real OpenCode proxy boundary", async () => { +it.each([ + { model: "openrouter/deepseek/deepseek-v4-flash-0731", supportsReasoning: true }, + { model: "anthropic/claude-sonnet-4", supportsReasoning: false }, +])("preserves prepared input and completion feedback through runnerd and the real OpenCode proxy boundary ($model)", async ({ model, supportsReasoning }) => { const root = await mkdtemp(join(tmpdir(), "runnerd-prepared-opencode-")); // GitHub-hosted Linux toolcache Node can be group-writable, unlike the AWS // fleet. Qualify an owned copy with strict permissions, never chmod the host @@ -8006,7 +8009,7 @@ it("preserves prepared input and completion feedback through runnerd and the rea const driver = new CodexAppServerDriver({ taskEnvelope: task, conversationMode: "prepared", - model: "openrouter/deepseek/deepseek-v4-flash-0731", + model, transportFactory: () => bundle.transport, workingDirectoryAuthority: "remote_runner", environment: { PAPERCLIP_WORKSPACE_CWD: root }, @@ -8025,7 +8028,15 @@ it("preserves prepared input and completion feedback through runnerd and the rea await withPreparedOpenCodeCleanup({ run: async () => { session = await driver.openSession({ runId: "prepared-opencode", normalizedSessionId: "prepared-opencode", workingDirectory: root }); - await session.startTurn({ message: { role: "user", text: prepared } }); + expect(bundle.transport.supportsTurnReasoning?.()).toBe(supportsReasoning); + const firstTurn = { message: { role: "user" as const, text: prepared } }; + if (!supportsReasoning) { + await expect(session.startTurn({ ...firstTurn, reasoningMode: "disabled" })) + .rejects.toThrow("Per-turn reasoning is not supported"); + await expect(bundle.transport.request("turn/start", { input: [{ type: "text", text: prepared }], reasoningMode: "disabled" })) + .rejects.toThrow("requires an OpenCode/OpenRouter model"); + } + await session.startTurn({ ...firstTurn, ...(supportsReasoning ? { reasoningMode: "disabled" as const } : {}) }); const events: PrpEvent[] = []; for await (const event of session.events()) { events.push(event); @@ -8037,10 +8048,16 @@ it("preserves prepared input and completion feedback through runnerd and the rea expect(sessionRoots).toHaveLength(1); const requests = (await readFile(join(runtime, sessionRoots[0]!.name, "data/fake-prompt-requests.ndjson"), "utf8")).trim().split("\n").map((line) => JSON.parse(line)); expect(requests.map((request) => request.parts)).toEqual([[{ type: "text", text: prepared }]]); + expect(requests[0].variant).toBe(supportsReasoning ? "paperclip-no-reasoning" : undefined); const outcomes = JSON.parse(await readFile(join(runtime, sessionRoots[0]!.name, "data/fake-completion-feedback.json"), "utf8")); expect(outcomes).toHaveLength(2); expect(outcomes[0].result).toMatchObject({ isError: true, content: [{ text: expect.stringContaining("required document link") }] }); expect(outcomes[1].result).toMatchObject({ content: [{ text: expect.stringContaining(feedback) }] }); + await session.startTurn({ message: { role: "user", text: "Reply Hi." } }); + for await (const event of session.events()) if (event.eventType === "turn.completed") break; + const allRequests = (await readFile(join(runtime, sessionRoots[0]!.name, "data/fake-prompt-requests.ndjson"), "utf8")).trim().split("\n").map((line) => JSON.parse(line)); + expect(allRequests.map(request => request.variant)).toEqual(supportsReasoning + ? ["paperclip-no-reasoning", "paperclip-default"] : [undefined, undefined]); }, closeSession: async () => { await session?.close(); }, closeTransport: () => bundle.transport.close(), diff --git a/packages/paperclip-runner/src/live/runnerd-codex-transport.ts b/packages/paperclip-runner/src/live/runnerd-codex-transport.ts index 08a8886f3b9..71103812f2a 100644 --- a/packages/paperclip-runner/src/live/runnerd-codex-transport.ts +++ b/packages/paperclip-runner/src/live/runnerd-codex-transport.ts @@ -3538,6 +3538,20 @@ class DurablePrpCodexTransport implements CodexAppServerTransport { #providerIdentity: Record | null = null; #turnControls: NativeTurnControlCapabilities = { steering: false, queuedFollowUp: false }; + supportsTurnReasoning(): boolean { + if (this.options.provider !== "opencode") return false; + // Use the durable selected model on both fresh and recovered sessions. + // Older states may retain the provider only in the preparation command. + const state = this.#core?.store.state; + const template = this.#runAttachTemplate ?? state?.runAttachTemplate + ?? state?.commands.findLast(command => + (command.type === "run.prepare" || command.type === "run.attach") + && record(command.payload).provider !== undefined, + )?.payload; + const model = record(record(template).provider).model; + return typeof model === "string" && model.startsWith("openrouter/"); + } + turnControlCapabilities(): NativeTurnControlCapabilities | null { if (this.options.provider !== "acpx") return null; if (this.#closed || this.#failure) return { steering: false, queuedFollowUp: false }; @@ -5883,6 +5897,11 @@ class DurablePrpCodexTransport implements CodexAppServerTransport { .filter((item) => item.type !== "skill") .map((item) => (typeof item.text === "string" ? item.text : "")) .join("\n"); + const reasoningMode = params.reasoningMode; + if (reasoningMode !== undefined && ( + !this.supportsTurnReasoning() + || (reasoningMode !== "default" && reasoningMode !== "disabled") + )) throw new Error("turn.reasoningMode requires an OpenCode/OpenRouter model and must be default or disabled"); const skills = resolveRunnerdCodexSkillInputs( input.filter((item) => item.type === "skill"), this.options.runtimeContext ?? null, @@ -5916,6 +5935,7 @@ class DurablePrpCodexTransport implements CodexAppServerTransport { "turn.start", { text: message, + ...(reasoningMode === undefined ? {} : { reasoningMode }), ...(skills.length ? { skills } : {}), turnId: pendingTurnId, }, diff --git a/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs b/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs index 69bd35a8e4e..692fd9c2ebd 100644 --- a/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs +++ b/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs @@ -99,6 +99,8 @@ await writeFile( home: process.env.HOME, configHome: process.env.XDG_CONFIG_HOME, projectConfigDisabled: process.env.OPENCODE_DISABLE_PROJECT_CONFIG, + modelsFetchDisabled: process.env.OPENCODE_DISABLE_MODELS_FETCH, + defaultPluginsDisabled: process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS, }), ); @@ -400,8 +402,7 @@ const server = createServer(async (request, response) => { const promptPayload = JSON.parse(Buffer.concat(chunks).toString("utf8")); await appendFile(join(process.env.XDG_DATA_HOME, "fake-prompt-requests.ndjson"), JSON.stringify(promptPayload) + "\n"); if ( - promptPayload.providerID !== "openrouter" || - promptPayload.modelID !== "deepseek/deepseek-v4-flash-0731" || + `${promptPayload.providerID}/${promptPayload.modelID}` !== runtimeConfig.model || "model" in promptPayload ) { return json(response, 400, { @@ -427,11 +428,56 @@ const server = createServer(async (request, response) => { }, }, }); + emit({ + type: "message.part.delta", + properties: { + sessionID: session.id, + messageID: "message-late-source", + partID: "part-late-straggler", + field: "text", + delta: "late straggler text must not reach the next turn", + }, + }); } } setTimeout(async () => { await callFirstPaperclipTool(); const parsedPrompt = parsedPromptText(promptPayload); + if (String(parsedPrompt.message ?? "").includes("incremental-stream")) { + const messageID = `stream-message-${promptTurnSeq}`; + const part = { id: `stream-part-${promptTurnSeq}`, messageID, sessionID: session.id, type: "text", text: "" }; + const reasoning = { ...part, id: `${part.id}-reasoning`, type: "reasoning" }; + const update = (value) => emit({ type: "message.part.updated", properties: { sessionID: session.id, part: value } }); + const delta = (value, text, extra = {}) => emit({ type: "message.part.delta", properties: { sessionID: session.id, messageID, partID: value.id, field: "text", delta: text, ...extra } }); + const announceRole = () => emit({ type: "message.updated", properties: { sessionID: session.id, info: { id: messageID, role: "assistant" } } }); + const roleFirst = String(parsedPrompt.message).includes("role-first"); + const longResponse = String(parsedPrompt.message).includes("role-last-long"); + if (roleFirst) announceRole(); + // Parts and deltas can precede the message's role announcement. + update(part); + update(reasoning); + delta(reasoning, "Thinking"); + delta(part, "Hi "); + const emojiCount = longResponse ? 120 : 2; + for (let i = 0; i < emojiCount; i++) delta(part, "👋"); + delta(part, "wrong message", { messageID: "unrelated-message" }); + delta(part, "wrong session", { sessionID: "unrelated-session" }); + delta(part, "wrong field", { field: "output" }); + delta(part, "unknown part", { partID: "unknown-part" }); + const finalPart = { ...part, text: `Hi ${"👋".repeat(emojiCount)}`, time: { start: 1, end: 2 } }; + if (longResponse) update(finalPart); + if (!roleFirst) announceRole(); + // The consumer must observe a delta before the completed snapshot + // exists. This handshake makes the streaming regression deterministic. + for (let i = 0; i < 500; i++) { + if (await readFile(join(process.env.XDG_DATA_HOME, "release-stream"), "utf8").catch(() => "") === "ready") break; + await new Promise(resolve => setTimeout(resolve, 10)); + } + await writeFile(join(process.env.XDG_DATA_HOME, "stream-released"), "ready"); + update(finalPart); + emit({ type: "session.idle", properties: { sessionID: session.id } }); + return; + } if (String(parsedPrompt.message ?? "").includes("native-question")) { pendingQuestion = nativeQuestion(); emit({ diff --git a/server/src/__tests__/browser-use-connection.test.ts b/server/src/__tests__/browser-use-connection.test.ts index b206fabe93c..f9282b9f082 100644 --- a/server/src/__tests__/browser-use-connection.test.ts +++ b/server/src/__tests__/browser-use-connection.test.ts @@ -34,6 +34,8 @@ import { import { toolAccessService } from "../services/tool-access.js"; import { createToolGatewayService } from "../services/tool-gateway.js"; import { browserUseViewports } from "../services/browser-use-viewport.js"; +import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js"; +import { registerBrowserUseCleanup } from "../services/browser-use-work.js"; import { browserUseService } from "../services/browser-use.js"; import { applyConnectorSkills, prepareConnectorSkillDelivery, resolveConnectorAssignments } from "../services/connector-runtime.js"; import { registerAssignedMcpGateway } from "../services/native-runtime/assigned-mcp-tools.js"; @@ -313,6 +315,47 @@ const actor = { actorType: "user" as const, actorId: "browser-reviewer" }; }, }; } + it("wakes the app worker from request-service commits and restores durable poll and lease deadlines", async () => { + const f = await fixture(); + const background = browserUseService(db, f.request); + const sweep = vi.spyOn(background, "sweep"); + const onError = vi.fn(); + const coordinator = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, onError }); + try { + await registerBrowserUseCleanup(coordinator, background, () => true).ready; + expect(coordinator.nextWakeAt()).toBeNull(); + expect(await background.nextSweepAt()).toBeNull(); + const started = await f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read example.com" }) as { sessionId: string }; + await vi.waitFor(() => expect(sweep.mock.calls.length).toBeGreaterThan(1)); + await coordinator.stop(); + expect(onError).not.toHaveBeenCalled(); + + const poll = new Date(Date.now() + 60_000); + const lease = new Date(Date.now() + 180_000); + await db.update(browserUseSessions).set({ nextPollAt: poll, leaseUntil: lease }) + .where(eq(browserUseSessions.id, started.sessionId)); + expect(await background.nextSweepAt()).toBe(lease.getTime()); + const restarted = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, onError }); + try { + await registerBrowserUseCleanup(restarted, browserUseService(db, f.request), () => true).ready; + expect(restarted.nextWakeAt()).toBe(lease.getTime()); + } finally { await restarted.stop(); } + await db.update(browserUseSessions).set({ leaseUntil: new Date(0) }) + .where(eq(browserUseSessions.id, started.sessionId)); + expect(await background.nextSweepAt()).toBe(poll.getTime()); + await db.update(browserUseSessions).set({ leaseUntil: null }) + .where(eq(browserUseSessions.id, started.sessionId)); + expect(await background.nextSweepAt()).toBe(poll.getTime()); + const [session] = await db.select().from(browserUseSessions).where(eq(browserUseSessions.id, started.sessionId)); + await f.service.control(session, "end"); + expect(await background.nextSweepAt()).toBeLessThan(poll.getTime()); + await db.update(browserUseSessions).set({ status: "closed" }).where(eq(browserUseSessions.id, started.sessionId)); + expect(await background.nextSweepAt()).toBeNull(); + } finally { + await coordinator.stop(); + await db.update(browserUseSessions).set({ status: "closed" }).where(eq(browserUseSessions.companyId, f.company.id)); + } + }); it("keeps Cloud instructions out of universal skills and unassigned runtime overlays", async () => { const skills = await listPaperclipSkillEntries(fileURLToPath(new URL("../", import.meta.url)), [fileURLToPath(new URL("../../../skills", import.meta.url))]); expect(skills.some(skill => skill.runtimeName === "paperclip")).toBe(true); diff --git a/server/src/__tests__/browser-use-work.test.ts b/server/src/__tests__/browser-use-work.test.ts new file mode 100644 index 00000000000..ae8a2dfbec2 --- /dev/null +++ b/server/src/__tests__/browser-use-work.test.ts @@ -0,0 +1,108 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { installDatabaseWorkSignals } from "../../../packages/db/src/work-signals.js"; +import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js"; +import { DELIVERY_QUEUES, notifyDeliveryWork } from "../services/delivery-work-notifications.js"; +import { registerBrowserUseCleanup } from "../services/browser-use-work.js"; + +const coordinators: ReturnType[] = []; +afterEach(async () => { + for (const coordinator of coordinators.splice(0)) await coordinator.stop(); + vi.useRealTimers(); +}); +function setup() { + vi.useFakeTimers(); + const execute = vi.fn(async () => [{ xid: "42", status: "committed" }]); + const owner = installDatabaseWorkSignals({ execute, async transaction(callback: (tx: any) => Promise) { + return callback({ execute }); + } }); + const enabled = vi.fn(() => true), primary = vi.fn(() => true), onError = vi.fn(); + const coordinator = createDeliveryWorkCoordinator({ + owner, canRun: () => enabled() && primary(), canReconcile: primary, onError, + }); + coordinators.push(coordinator); + const service = { sweep: vi.fn(async () => {}), nextSweepAt: vi.fn(async (): Promise => null) }; + const start = () => registerBrowserUseCleanup(coordinator, service, enabled); + const commit = () => owner.transaction(tx => notifyDeliveryWork(tx, DELIVERY_QUEUES.browser)); + return { service, coordinator, start, commit, enabled, primary, onError }; +} +describe("browser cleanup scheduling", () => { + it("stays quiet after an empty startup, wakes on commit, and stops after the final session closes", async () => { + const s = setup(); + await s.start().ready; + await vi.advanceTimersByTimeAsync(3_600_000); + expect(s.service.sweep).toHaveBeenCalledTimes(1); + expect(s.service.nextSweepAt).toHaveBeenCalledTimes(1); + expect(vi.getTimerCount()).toBe(0); + const due = Date.now() + 15_000; + s.service.nextSweepAt.mockResolvedValue(due); + await s.commit(); + await vi.advanceTimersByTimeAsync(1); + expect(s.service.sweep).toHaveBeenCalledTimes(2); + expect(s.coordinator.nextWakeAt()).toBe(due); + await vi.advanceTimersByTimeAsync(14_998); + expect(s.service.sweep).toHaveBeenCalledTimes(2); + s.service.nextSweepAt.mockResolvedValue(null); + await vi.advanceTimersByTimeAsync(1); + expect(s.service.sweep).toHaveBeenCalledTimes(3); + expect(vi.getTimerCount()).toBe(0); + }); + it("restores a future lease/retry deadline at startup and lets a control commit preempt it", async () => { + const s = setup(); + const due = Date.now() + 180_000; + s.service.nextSweepAt.mockResolvedValue(due); + await s.start().ready; + await vi.advanceTimersByTimeAsync(120_000); + expect(s.service.sweep).toHaveBeenCalledTimes(1); + await s.commit(); + await vi.advanceTimersByTimeAsync(1); + expect(s.service.sweep).toHaveBeenCalledTimes(2); + expect(s.coordinator.nextWakeAt()).toBe(due); + }); + it("retries a failed scan and bounds already-due batches", async () => { + const s = setup(); + s.service.sweep.mockRejectedValueOnce(new Error("database unavailable")); + await s.start().ready; + expect(s.onError).toHaveBeenCalledTimes(1); + s.service.nextSweepAt.mockResolvedValue(Date.now() - 1000); + await vi.advanceTimersByTimeAsync(3000); + expect(s.service.sweep).toHaveBeenCalledTimes(2); + expect(s.coordinator.nextWakeAt()).toBe(Date.now() + 1000); + s.service.nextSweepAt.mockResolvedValue(null); + await vi.advanceTimersByTimeAsync(1000); + expect(vi.getTimerCount()).toBe(0); + }); + it("does no session SQL during standby or drain and recovers a first commit after drain", async () => { + const s = setup(); + s.primary.mockReturnValue(false); + await s.start().ready; + await vi.advanceTimersByTimeAsync(6000); + expect(s.service.sweep).not.toHaveBeenCalled(); + expect(s.service.nextSweepAt).not.toHaveBeenCalled(); + s.primary.mockReturnValue(true); + s.enabled.mockReturnValue(false); + await s.commit(); + await vi.advanceTimersByTimeAsync(6000); + expect(s.service.sweep).not.toHaveBeenCalled(); + expect(s.service.nextSweepAt).not.toHaveBeenCalled(); + s.enabled.mockReturnValue(true); + await vi.advanceTimersByTimeAsync(3000); + expect(s.service.sweep).toHaveBeenCalledTimes(1); + expect(vi.getTimerCount()).toBe(0); + }); + it("waits for in-flight cleanup on shutdown without rearming its deadline", async () => { + const s = setup(); + let finish!: () => void; + s.service.sweep.mockImplementationOnce(() => new Promise(resolve => { finish = resolve; })); + const worker = s.start(); + await vi.advanceTimersByTimeAsync(1); + let stopped = false; + const stop = s.coordinator.stop().then(() => { stopped = true; }); + await Promise.resolve(); + expect(stopped).toBe(false); + finish(); + await stop; + await worker.ready; + expect(s.service.nextSweepAt).not.toHaveBeenCalled(); + expect(vi.getTimerCount()).toBe(0); + }); +}); diff --git a/server/src/__tests__/heartbeat-responsible-user-invariant.test.ts b/server/src/__tests__/heartbeat-responsible-user-invariant.test.ts index 8c4633a6ca4..39592ac031e 100644 --- a/server/src/__tests__/heartbeat-responsible-user-invariant.test.ts +++ b/server/src/__tests__/heartbeat-responsible-user-invariant.test.ts @@ -25,6 +25,7 @@ import { import { heartbeatService } from "../services/heartbeat.ts"; import { runningProcesses } from "../adapters/index.ts"; import { drainHeartbeatRunsToQuiescence } from "./helpers/drain-heartbeat-runs.js"; +import { createLifecycleDriver } from "../services/agent-lifecycle-driver.js"; const mockAdapterExecute = vi.hoisted(() => vi.fn(async () => ({ @@ -166,6 +167,27 @@ describeEmbeddedPostgres("heartbeat responsible-user invariant", () => { return { companyId, ownerUserId, agentId }; } + it("dispatches saved work on readiness with company scoping and normal admission checks", async () => { + const { companyId, agentId, ownerUserId } = await seedCompany(); + await db.update(agents).set({ status: "paused", lifecycleState: "verifying" }).where(eq(agents.id, agentId)); + const [run] = await db.insert(heartbeatRuns).values({ companyId, agentId, status: "queued", + invocationSource: "on_demand", contextSnapshot: { responsibleUserId: ownerUserId } }).returning(); + const driver = createLifecycleDriver(db, {} as never); + const currentAgent = async () => (await db.select().from(agents).where(eq(agents.id, agentId)))[0]; + await driver.onReady!(await currentAgent()); + expect(mockAdapterExecute).not.toHaveBeenCalled(); + expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id)))[0].status).toBe("queued"); + + await db.update(agents).set({ status: "idle", lifecycleState: "ready" }).where(eq(agents.id, agentId)); + await heartbeat.resumeQueuedRunsForAgent(randomUUID(), agentId); + expect(mockAdapterExecute).not.toHaveBeenCalled(); + expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id)))[0].status).toBe("queued"); + + await driver.onReady!(await currentAgent()); + expect(await waitForRun(db, run.id)).toMatchObject({ status: "succeeded", responsibleUserId: ownerUserId }); + expect(mockAdapterExecute).toHaveBeenCalledOnce(); + }); + it("dispatches an interrupted queue under the clicking operator through the real startup path", async () => { const { companyId, agentId, ownerUserId } = await seedCompany(); const operatorId = `operator-${randomUUID()}`, issueId = randomUUID(), commentId = randomUUID(), queueId = randomUUID(); diff --git a/server/src/app.ts b/server/src/app.ts index 5e0cb3d8e12..45a68a6a6b2 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -9,6 +9,7 @@ import { customerSuccessRoutes } from "./routes/customer-success.js"; import { cloudWarmStandbyMiddleware } from "./middleware/cloud-warm-standby.js"; import type { CloudWarmStandby } from "./services/cloud-warm-standby.js"; import { browserUseRoutes } from "./routes/browser-use.js"; +import { registerBrowserUseCleanup } from "./services/browser-use-work.js"; import { browserUseService } from "./services/browser-use.js"; import { slackToolRoutes } from "./routes/slack-tools.js"; import { createPublicMcpOAuth, publicMcpConfig } from "./services/public-mcp/oauth.js"; @@ -1292,12 +1293,7 @@ export async function createApp( ); }); }; - const browserUseTimer = setInterval(() => { - if (isWarmStandby() || isIdleTaskDrainActive()) return; - void browserUse.sweep().catch(() => logger.warn("Browser Use reconciliation failed; retrying.")); - }, 3000); - browserUseTimer.unref?.(); - if (!isWarmStandby() && !isIdleTaskDrainActive()) void browserUse.sweep().catch(() => logger.warn("Browser Use startup reconciliation failed; retrying.")); + registerBrowserUseCleanup(deliveryWork, browserUse, () => !isIdleTaskDrainActive()); let importTransferSweepTimer: ReturnType | null = setInterval( sweepImportTransferSpools, @@ -1410,7 +1406,6 @@ export async function createApp( chatPublicationTimer = null; } await chatReconciliation.drain(); - clearInterval(browserUseTimer); if (importTransferSweepTimer) { clearInterval(importTransferSweepTimer); importTransferSweepTimer = null; diff --git a/server/src/modules/agent-lifecycle/__tests__/diagnostics.test.ts b/server/src/modules/agent-lifecycle/__tests__/diagnostics.test.ts index 4f7a8db7214..06b7e246d49 100644 --- a/server/src/modules/agent-lifecycle/__tests__/diagnostics.test.ts +++ b/server/src/modules/agent-lifecycle/__tests__/diagnostics.test.ts @@ -13,14 +13,58 @@ function fixture() { const store = { get: vi.fn(), claim: vi.fn().mockResolvedValue(agent), renew: vi.fn().mockResolvedValue(true), setRequiredPlugins: vi.fn().mockResolvedValue(true), completeHost: vi.fn().mockResolvedValue(true), completePlugin: vi.fn().mockResolvedValue(true), defer: vi.fn().mockResolvedValue(true), - change: vi.fn().mockResolvedValue({ ...agent, lifecycleState: "ready" }), pending: vi.fn().mockResolvedValue([agent.id]) } satisfies LifecycleStore; + change: vi.fn().mockResolvedValue({ ...agent, lifecycleState: "ready", lifecycleVersion: 4, lifecycleOperation: null }), pending: vi.fn().mockResolvedValue([agent.id]) } satisfies LifecycleStore; const driver = { requiredPluginIds: vi.fn().mockResolvedValue(["plugin"]), runHost: vi.fn().mockResolvedValue("complete"), - runPlugin: vi.fn().mockResolvedValue("complete") } satisfies LifecycleDriver; + runPlugin: vi.fn().mockResolvedValue("complete"), onReady: vi.fn().mockResolvedValue(undefined) } satisfies LifecycleDriver; const report = vi.fn(); return { store, driver, report, worker: createLifecycleWorker(store, driver, report) }; } afterEach(() => { vi.restoreAllMocks(); vi.useRealTimers(); }); +describe("lifecycle readiness handoff", () => { + it("notifies the scheduler only after host, plugins, and readiness have committed", async () => { + const { store, driver, worker } = fixture(); + let commit!: (ready: LifecycleAgent) => void; + store.change.mockReturnValue(new Promise(resolve => { commit = resolve; })); + driver.onReady.mockImplementation(async ready => { + expect(store.completeHost).toHaveBeenCalledOnce(); + expect(store.completePlugin).toHaveBeenCalledOnce(); + expect(store.change).toHaveBeenCalledOnce(); + expect(ready).toMatchObject({ lifecycleState: "ready", lifecycleVersion: 4, lifecycleOperation: null }); + }); + const work = worker.process(agent.id); + await vi.waitFor(() => expect(store.change).toHaveBeenCalledOnce()); + expect(driver.onReady).not.toHaveBeenCalled(); + commit({ ...agent, lifecycleState: "ready", lifecycleVersion: 4, lifecycleOperation: null }); + await work; + expect(driver.onReady).toHaveBeenCalledOnce(); + await worker.stop(); + }); + + it.each(["host", "plugin", "transition", "hold"])("does not dispatch when %s prevents readiness", async stage => { + const { store, driver, worker } = fixture(); + if (stage === "host") driver.runHost.mockResolvedValue("pending"); + if (stage === "plugin") driver.runPlugin.mockResolvedValue("pending"); + if (stage === "transition") store.change.mockResolvedValue(null); + if (stage === "hold") store.change.mockResolvedValue({ ...agent, lifecycleState: "paused" }); + await worker.process(agent.id); + expect(driver.onReady).not.toHaveBeenCalled(); + await worker.stop(); + }); + + it("preserves committed readiness after a dispatch failure for the queue sweep to recover", async () => { + const { store, driver, report, worker } = fixture(); + const error = new Error("queue unavailable"); + driver.onReady.mockRejectedValue(error); + await worker.process(agent.id); + expect(report).toHaveBeenCalledWith(expect.objectContaining({ stage: "ready", phase: "ready", version: 4 }), error); + expect(store.defer).not.toHaveBeenCalled(); + expect(driver.runHost).toHaveBeenCalledOnce(); + expect(store.change).toHaveBeenCalledOnce(); + await worker.stop(); + }); +}); + describe("lifecycle failure diagnostics", () => { it.each(["host", "plugin", "claim", "renew", "scan", "defer"] as const)("reports %s failures with the available context", async stage => { const { store, driver, report, worker } = fixture(); diff --git a/server/src/modules/agent-lifecycle/application/ports.ts b/server/src/modules/agent-lifecycle/application/ports.ts index 1845ad8b7d1..11442b70b88 100644 --- a/server/src/modules/agent-lifecycle/application/ports.ts +++ b/server/src/modules/agent-lifecycle/application/ports.ts @@ -27,11 +27,13 @@ export interface LifecycleDriver { requiredPluginIds(agent: LifecycleAgent): Promise; runHost(agent: LifecycleAgent): Promise<"complete" | "pending">; runPlugin(agent: LifecycleAgent, pluginId: string): Promise<"complete" | "pending">; + /** Notify the scheduler only after readiness has committed. */ + onReady?(agent: LifecycleAgent): Promise; } export interface LifecycleFailure { stage: "claim" | "renew" | "select_plugins" | "save_plugins" | "host" | "plugin" - | "complete_host" | "complete_plugin" | "transition" | "defer" | "scan" | "policy_scan"; + | "complete_host" | "complete_plugin" | "transition" | "defer" | "scan" | "policy_scan" | "ready"; agentId?: string; companyId?: string; phase?: AgentLifecycleState; diff --git a/server/src/modules/agent-lifecycle/application/worker.ts b/server/src/modules/agent-lifecycle/application/worker.ts index ad1c143dd96..3b90b227673 100644 --- a/server/src/modules/agent-lifecycle/application/worker.ts +++ b/server/src/modules/agent-lifecycle/application/worker.ts @@ -56,6 +56,14 @@ export function createLifecycleWorker(store: LifecycleStore, driver: LifecycleDr if (!await runStep("complete_plugin", () => store.completePlugin(agent, owner, pluginId), pluginId)) continue phase; } const changed = await runStep("transition", () => store.change(id, "complete", { version: agent.lifecycleVersion, owner })); + if (changed?.lifecycleState === "ready" && !stopped && canRun()) { + // Accepted messages may already be waiting for verification. Do not + // leave them until the periodic queue sweep. Readiness is committed; + // a dispatch failure must not replay setup or defer the old phase. + await attempt({ ...context, stage: "ready", phase: changed.lifecycleState, + version: changed.lifecycleVersion, operationId: changed.lifecycleOperation?.id }, + async () => { await driver.onReady?.(changed); }).catch(() => {}); + } if (changed && !isLifecycleWorkPending(changed.lifecycleState)) return; } catch { if (!await runStep("defer", () => store.defer(agent, owner, "The lifecycle step failed. Retry the operation.", new Date()))) continue; diff --git a/server/src/services/agent-lifecycle-driver.ts b/server/src/services/agent-lifecycle-driver.ts index 68cbea29999..11963d997fa 100644 --- a/server/src/services/agent-lifecycle-driver.ts +++ b/server/src/services/agent-lifecycle-driver.ts @@ -10,6 +10,9 @@ export function createLifecycleDriver(db: Db, manager: PluginWorkerManager): Lif const { verify } = agentHarnessVerificationService(db, manager); const heartbeat = heartbeatService(db, { pluginWorkerManager: manager }); return { + async onReady(agent) { + await heartbeat.resumeQueuedRunsForAgent(agent.companyId, agent.id); + }, async requiredPluginIds(agent) { if (agent.lifecycleRequiredPluginIds !== null) return agent.lifecycleRequiredPluginIds; const installed = await db.select().from(plugins).where(inArray(plugins.status, ["ready", "error", "installed", "upgrade_pending"])); diff --git a/server/src/services/browser-use-work.ts b/server/src/services/browser-use-work.ts new file mode 100644 index 00000000000..ae8c7a36244 --- /dev/null +++ b/server/src/services/browser-use-work.ts @@ -0,0 +1,23 @@ +import type { createDeliveryWorkCoordinator } from "./delivery-work-coordinator.js"; +import { DELIVERY_QUEUES } from "./delivery-work-notifications.js"; + +/** The app owns this worker; request-scoped services only signal durable writes. */ +export function registerBrowserUseCleanup( + coordinator: ReturnType, + service: { sweep(): Promise; nextSweepAt(): Promise }, + canRun: () => boolean, +) { + let next: number | null = null; + return coordinator.register(DELIVERY_QUEUES.browser, { + retryMs: 3000, + run: () => service.sweep(), + hasPending: async () => { + // During idle drain retain a memory-only wake, including startup or a + // newly committed first session. Warm standby is gated by the coordinator. + next = canRun() ? await service.nextSweepAt() : Date.now() + 3000; + return false; + }, + // Bound overdue batches without spinning on a busy database. + nextRunAt: () => next === null ? null : Math.max(Date.now() + 1000, next), + }); +} diff --git a/server/src/services/browser-use.ts b/server/src/services/browser-use.ts index 849b1d8bb08..683fe107ca8 100644 --- a/server/src/services/browser-use.ts +++ b/server/src/services/browser-use.ts @@ -43,6 +43,7 @@ import { createCostEventInTransaction } from "./costs.js"; import { createFinanceEventInTransaction } from "./finance.js"; import { withAccountingTransaction } from "./accounting-transaction.js"; import { logActivity } from "./activity-log.js"; +import { DELIVERY_QUEUES, notifyDeliveryWork } from "./delivery-work-notifications.js"; import { browserUseViewports } from "./browser-use-viewport.js"; import { forbidden, notFound, conflict } from "../errors.js"; @@ -64,6 +65,7 @@ const argsSchema = z }) .strict(); const terminalSessions = ["closed", "failed"]; +const liveSessions = ["starting", "running", "idle", "stopping"]; const requestMarker = (invocationId: string) => `\n\n[Paperclip request: ${invocationId}]`; export function browserUseService( @@ -422,6 +424,7 @@ export function browserUseService( ); const sid = args.sessionId; const s = await db.transaction(async (tx) => { + await notifyDeliveryWork(tx, DELIVERY_QUEUES.browser); let row: Session; if (sid) { await owned(binding, grant, sid); @@ -429,6 +432,7 @@ export function browserUseService( .update(sessions) .set({ status: "starting", + nextPollAt: new Date(), idleDeadline: null, updatedAt: new Date(), }) @@ -486,6 +490,7 @@ export function browserUseService( ...(maxCostUsd === undefined ? {} : { maxCostUsd }), }); await db.transaction(async (tx) => { + await notifyDeliveryWork(tx, DELIVERY_QUEUES.browser); await tx .update(runs) .set({ providerRunId: result.id, status: result.status }) @@ -507,6 +512,7 @@ export function browserUseService( const message = rejected ? error.message : "Browser run creation could not be confirmed. Paperclip is locating and stopping possible provider work. Do not start another run yet."; await db.transaction(async tx => { + await notifyDeliveryWork(tx, DELIVERY_QUEUES.browser); const [current] = await tx.select().from(sessions).where(eq(sessions.id, s.id)).for("update"); // A restart must not leave a drained rejection attached to a starting // session: that combination has neither recovery work nor an idle timer. @@ -566,25 +572,28 @@ export function browserUseService( throw conflict("Only an idle browser can be kept open."); } else { if (terminalSessions.includes(s.status)) return; - await db - .update(sessions) - .set({ - stopRequested: action, - ...(action === "end" ? { status: "stopping" } : {}), - nextPollAt: new Date(), - updatedAt: new Date(), - }) - .where( - and( - eq(sessions.id, s.id), - action === "cancel" - ? or( - isNull(sessions.stopRequested), - ne(sessions.stopRequested, "end"), - ) - : undefined, - ), - ); + await db.transaction(async tx => { + await notifyDeliveryWork(tx, DELIVERY_QUEUES.browser); + await tx + .update(sessions) + .set({ + stopRequested: action, + ...(action === "end" ? { status: "stopping" } : {}), + nextPollAt: new Date(), + updatedAt: new Date(), + }) + .where( + and( + eq(sessions.id, s.id), + action === "cancel" + ? or( + isNull(sessions.stopRequested), + ne(sessions.stopRequested, "end"), + ) + : undefined, + ), + ); + }); } await signal( s, @@ -1218,97 +1227,109 @@ export function browserUseService( ); } } - let sweeping = false; - async function sweep(connectionId?: string) { - if (sweeping) return; - sweeping = true; - try { - const due = await db - .select() - .from(sessions) + // Wait for a concurrent sweep rather than reporting completion while its + // lease is still held. A filtered cleanup must still inspect its own scope. + let sweeping: Promise | null = null; + async function sweep(connectionId?: string): Promise { + if (sweeping) { + await sweeping; + return sweep(connectionId); + } + sweeping = reconcileDue(connectionId); + try { await sweeping; } finally { sweeping = null; } + } + async function reconcileDue(connectionId?: string) { + const due = await db + .select() + .from(sessions) + .where( + and( + connectionId ? eq(sessions.connectionId, connectionId) : undefined, + inArray(sessions.status, liveSessions), + lte(sessions.nextPollAt, new Date()), + or( + isNull(sessions.leaseUntil), + lte(sessions.leaseUntil, new Date()), + ), + ), + ) + .limit(30); + for (const s of due) { + const token = randomUUID(); + const lease = await db + .update(sessions) + .set({ + leaseToken: token, + leaseUntil: new Date(Date.now() + 180_000), + }) .where( and( - connectionId ? eq(sessions.connectionId, connectionId) : undefined, - inArray(sessions.status, [ - "starting", - "running", - "idle", - "stopping", - ]), - lte(sessions.nextPollAt, new Date()), + eq(sessions.id, s.id), or( isNull(sessions.leaseUntil), lte(sessions.leaseUntil, new Date()), ), ), ) - .limit(30); - for (const s of due) { - const token = randomUUID(); - const lease = await db + .returning(); + if (!lease.length) continue; + // Renew while paginating provider events or stopping multiple browsers. + const renewal = setInterval(() => { + void db + .update(sessions) + .set({ leaseUntil: new Date(Date.now() + 180_000) }) + .where(and(eq(sessions.id, s.id), eq(sessions.leaseToken, token))) + .catch(() => {}); + }, 30_000); + renewal.unref?.(); + let delay = s.status === "idle" ? 15000 : 3000; + try { + await reconcile(s); + } catch (e) { + delay = Math.max( + 10000, + e instanceof BrowserUseError ? e.retryAfterMs : 0, + ); + await db .update(sessions) .set({ - leaseToken: token, - leaseUntil: new Date(Date.now() + 180_000), + error: + s.stopRequested === "end" + ? "Browser shutdown is not confirmed. Paperclip will retry." + : "Browser synchronization failed. Paperclip will retry.", }) - .where( - and( - eq(sessions.id, s.id), - or( - isNull(sessions.leaseUntil), - lte(sessions.leaseUntil, new Date()), - ), - ), - ) - .returning(); - if (!lease.length) continue; - // Renew while paginating provider events or stopping multiple browsers. - const renewal = setInterval(() => { - void db - .update(sessions) - .set({ leaseUntil: new Date(Date.now() + 180_000) }) - .where(and(eq(sessions.id, s.id), eq(sessions.leaseToken, token))) - .catch(() => {}); - }, 30_000); - renewal.unref?.(); - let delay = s.status === "idle" ? 15000 : 3000; - try { - await reconcile(s); - } catch (e) { - delay = Math.max( - 10000, - e instanceof BrowserUseError ? e.retryAfterMs : 0, - ); - await db - .update(sessions) - .set({ - error: - s.stopRequested === "end" - ? "Browser shutdown is not confirmed. Paperclip will retry." - : "Browser synchronization failed. Paperclip will retry.", - }) - .where(eq(sessions.id, s.id)); - } finally { - clearInterval(renewal); - await db - .update(sessions) - .set({ - leaseToken: null, - leaseUntil: null, - nextPollAt: new Date( - Date.now() + delay + Math.floor(Math.random() * 500), - ), - }) - .where(and(eq(sessions.id, s.id), eq(sessions.leaseToken, token))); + .where(eq(sessions.id, s.id)); + } finally { + clearInterval(renewal); + const release = (tx: Pick) => tx.update(sessions).set({ + leaseToken: null, + leaseUntil: null, + nextPollAt: new Date(Date.now() + delay + Math.floor(Math.random() * 500)), + }).where(and(eq(sessions.id, s.id), eq(sessions.leaseToken, token))); + if (connectionId) { + // Credential-removal sweeps can race the app worker. Wake it when + // their lease ends so it does not wait for the old lease expiry. + await db.transaction(async tx => { + await notifyDeliveryWork(tx, DELIVERY_QUEUES.browser); + await release(tx); + }); + } else { + await release(db); } } - } finally { - sweeping = false; } } + async function nextSweepAt(): Promise { + const [row] = await db.select({ + // An overdue row owned by another sweep cannot run before its lease ends. + at: sql`min(greatest(${sessions.nextPollAt}, ${sessions.leaseUntil}))`, + }).from(sessions).where(inArray(sessions.status, liveSessions)); + return row?.at == null ? null : new Date(row.at).getTime(); + } return { execute, sweep, + nextSweepAt, list, viewer, resize, @@ -1322,16 +1343,19 @@ export function browserUseService( const scope = and( eq(sessions.companyId, companyId), eq(sessions.connectionId, connectionId), - inArray(sessions.status, ["starting", "running", "idle", "stopping"]), + inArray(sessions.status, liveSessions), ); - await db - .update(sessions) - .set({ - stopRequested: "end", - nextPollAt: new Date(), - updatedAt: new Date(), - }) - .where(scope); + await db.transaction(async tx => { + await notifyDeliveryWork(tx, DELIVERY_QUEUES.browser); + await tx + .update(sessions) + .set({ + stopRequested: "end", + nextPollAt: new Date(), + updatedAt: new Date(), + }) + .where(scope); + }); await sweep(connectionId); const [pending] = await db .select({ id: sessions.id }) diff --git a/server/src/services/delivery-work-notifications.ts b/server/src/services/delivery-work-notifications.ts index d9696da5cf2..42bed21ae4d 100644 --- a/server/src/services/delivery-work-notifications.ts +++ b/server/src/services/delivery-work-notifications.ts @@ -2,6 +2,7 @@ import { signalDatabaseWork, subscribeDatabaseWork } from "@paperclipai/db"; /** Topics for existing durable queues; register intent before their writes. */ export const DELIVERY_QUEUES = { + browser: "browser-use", email: "email-channels", feedback: "feedback-exports", chatCompletion: "chat-completions", diff --git a/server/src/services/heartbeat.ts b/server/src/services/heartbeat.ts index f75cf91ee33..4b072ea25d8 100644 --- a/server/src/services/heartbeat.ts +++ b/server/src/services/heartbeat.ts @@ -1,3 +1,4 @@ +import { selectHeartbeatRuntime } from "./heartbeat/runtime-selection.js"; import { createHeartbeatRunCompletion } from "./heartbeat/run-completion.js"; export { MAX_TURN_CONTINUATION_WAKE_REASON, @@ -246,7 +247,6 @@ export { import { buildPaperclipTaskMarkdown } from "./heartbeat/task-markdown.js"; export { buildPaperclipTaskMarkdown } from "./heartbeat/task-markdown.js"; - import { recordLegacyWorkspaceRestoreFailure } from "./legacy-execution-recovery.js"; import { configuredEnvironmentProjection, @@ -261,7 +261,6 @@ import { prepareConnectionInstructionDelivery } from "./connection-instructions. import { resolveAssignedConnectionInstructionsForRun } from "./native-runtime/assigned-mcp-tools.js"; import { externalObjectService } from "./external-objects.js"; import { resolvePaperclipInstanceRoot } from "../home-paths.js"; -import { dotRunnerBroker } from "./dot-runner-broker.js"; import { startupCancellationFence, @@ -275,7 +274,6 @@ import { AgentDirectoryReuseInvalidatedError, isAgentDirectoryCopy } from "./age import type { PaperclipTurnContext } from "@paperclipai/adapter-utils/server-utils"; import { restoreNativeWorkspaceBestEffort } from "./native-runtime/native-workspace-best-effort.js"; - import { withNativeWorkspaceFinalizationOwnership, NativeWorkspaceFinalizationBusyError, @@ -308,7 +306,6 @@ import { import { legacyControllerBootId, - renewLegacyControllerLease, watchLegacyControllerLease, } from "./legacy-controller-lease.js"; @@ -335,9 +332,6 @@ import { claimedAdapterType, } from "./conversation-continuation.js"; -import { getNativeReviewAssignment, readNativeReviewAssignmentContext } from "./native-runtime/native-review-participant.js"; - -import { buildNativeReviewRequest } from "./native-runtime/native-review-prompt.js"; import { settleInterruptedNativeBootstrap, } from "./legacy-execution-recovery.js"; @@ -352,8 +346,6 @@ export { buildHeartbeatRunStatusLiveEventPayload } from "./heartbeat-run-status- import { buildExecutionContinuation, StaleExecutionContinuationError } from "./execution-continuation.js"; import { renderPaperclipWakePrompt } from "@paperclipai/adapter-utils/server-utils"; - - import { initializeRunIdentity, } from "./run-identity.js"; @@ -374,7 +366,9 @@ import fs from "node:fs/promises"; import path from "node:path"; import { execFile as execFileCallback } from "node:child_process"; import { promisify } from "node:util"; -import { createHash, randomUUID } from "node:crypto"; +import { + createHash, +} from "node:crypto"; import { and, asc, @@ -400,8 +394,6 @@ import { agentConfigRevisions, agentWakeupRequests, chatActions, - completionContracts, - heartbeatRunEvents, heartbeatRuns, environmentLeases, issueComments, @@ -432,50 +424,28 @@ import { publishLiveEvent } from "./live-events.js"; import { queuedCommentIdsFromRunContext, } from "./issue-queued-comment-queue.js"; -import { documentService } from "./documents.js"; + import { getTaskPlanContext } from "./task-plan-context.js"; -import { managedAgentProfileService } from "./managed-agent-profiles.js"; -import { remoteAgentProfileService } from "./remote-agent-profiles.js"; + import { buildNativeProviderEnvironment, - buildNativeExecutionInput, - buildNativeExecutionWithCheckpoint, - buildNativeRuntimeContext, claimWarmNativeInstructionCopy, nativeSessionWorkspaceScope, reserveWarmNativeInstructionDirectory, dispatchNativeSessionResumptions, - ensureNativeCompletionContract, executePaperclipNativeSession, finalizeNativeRun, - findNativeSessionResumeRun, - isNativeSessionId, - isUnusedNativeSessionBootstrap, - isUnusedLegacyNativeRetryReplacement, isRunnerIngressAuthorized, materializeLegacyQuestionResponseWakeProjection, - materializeNativeInteractionResponses, - nativeCompletionRequestsWithSources, - nativeCompletionSource, - nativeImmediateObjectiveSource, NativeCancellationPendingRecoveryError, NativeControllerDetachedForRestartError, - nativeToolContractFingerprintForTarget, - prepareNativeSessionBootstrapPersistence, prepareNativeWorkspaceSync, readNativeWorkspaceSyncReference, recordNativeFinalizationFailure, type NativeRestartRecoveryClaim, - rebindNativeSessionCheckpoint, resolveHeartbeatNativeRuntimeMode, } from "./native-runtime/index.js"; -import { - assertAgentCoreProfileRecoveryBinding, - assertManagedProfileRecoveryBinding, - projectPaperclipRunnerTaskConfig, - resolvePaperclipRunnerNativeProviderInput, -} from "./native-runtime/provider-profile.js"; -import { readRemoteCodexModelCliVersion } from "./native-runtime/codex-model-fallback.js"; + import { buildNativeHeartbeatPreparationSpans, buildNativeWakeIngressSpan, @@ -483,7 +453,6 @@ import { type NativeRunHistoricalSpan, } from "./native-runtime/native-run-trace.js"; import { - describeRunnerdNativeSessionBackend, parseNativeExecutionInput, type NativeExecutionInput, type NativeSessionBackend, @@ -511,7 +480,7 @@ import { import { EXTERNAL_CHAT_QUESTION_RESPONSE_KEY, } from "./native-runtime/external-chat-question-response.js"; -import { materializeExternalChatQuestionResponseInput } from "./native-runtime/external-chat-question-response-input.js"; + import { NativeRunnerOwnershipUnverifiedError, isNativeRunnerOwnershipHeld, @@ -548,10 +517,6 @@ import { buildHeartbeatRunIssueComment, } from "./heartbeat-run-summary.js"; -import { - CHAT_CONTROL_RECOVERY_ADMISSION_KEY, -} from "./chat-control-recovery-stop.js"; - import { logActivity, type LogActivityInput, @@ -586,7 +551,6 @@ import { settleLiveRunnerGoalBeforeInterrupt, } from "./runner-goals.js"; - import { parseIssueExecutionState, } from "./issue-execution-policy.js"; @@ -597,9 +561,7 @@ import { continuationSummaryParksExecutor, getIssueContinuationSummaryDocument, } from "./issue-continuation-summary.js"; -import { - buildPlanReviewContext, -} from "./plan-review-context.js"; + import { executionWorkspaceService, type TaskWorkspaceBindingPatch, @@ -673,7 +635,6 @@ import { type RunSnapshot as WakeQueueRunSnapshot, } from "../modules/wake-queue/index.js"; - import { taskWatchdogService } from "./task-watchdogs.js"; import { @@ -690,15 +651,11 @@ import { } from "../log-redaction.js"; import { createRunSecretRedactionRegistry } from "./run-secret-redaction.js"; -import { - resolvePaperclipRunnerIdleTimeoutMs, -} from "@paperclipai/adapter-utils"; + import { readPaperclipSkillSyncPreference, - selectPaperclipTaskMarkdown, } from "@paperclipai/adapter-utils/server-utils"; - import { environmentService } from "./environments.js"; import { parseExecutionPolicyBootstrapEnv } from "./execution-policy-bootstrap.js"; @@ -839,64 +796,11 @@ export function getTaskDrainStatus(): { }; } -export interface NativeSandboxLifecycle { - runnerProcess: "per_turn" | "warm"; - sandboxResource: "keep_running" | "stop_and_reuse" | "destroy_after_turn"; - failoverBackup: "verified"; -} +export { type NativeSandboxLifecycle } from "./heartbeat/runtime-selection.js"; -export function resolveReusableSandboxLifecycle(input: { - lifecyclePolicy: - | { mode: "per_turn"; idleTimeoutMs: null } - | { mode: "warm"; idleTimeoutMs: number }; - target: { - kind: "local" | "remote"; - transport?: string; - reusableLeaseConfigured?: boolean; - effectiveCapabilities?: { reusableLeases: boolean } | null; - } | null; -}): NativeSandboxLifecycle | null { - if (input.target?.kind !== "remote" || input.target.transport !== "sandbox") { - return null; - } - const reusableLease = - input.target.reusableLeaseConfigured === true && - input.target.effectiveCapabilities?.reusableLeases === true; - if (input.lifecyclePolicy.mode === "warm" && !reusableLease) { - throw new Error("runner_warm_lifecycle_requires_reusable_provider_lease"); - } - return { - runnerProcess: input.lifecyclePolicy.mode, - sandboxResource: - input.lifecyclePolicy.mode === "warm" - ? "keep_running" - : reusableLease - ? "stop_and_reuse" - : "destroy_after_turn", - failoverBackup: "verified", - }; -} +export { resolveReusableSandboxLifecycle } from "./heartbeat/runtime-selection.js"; -export function resolveNativeSandboxLifecycle(input: { - adapterType: string; - lifecyclePolicy: - | { mode: "per_turn"; idleTimeoutMs: null } - | { mode: "warm"; idleTimeoutMs: number }; - target: { - kind: "local" | "remote"; - transport?: string; - reusableLeaseConfigured?: boolean; - effectiveCapabilities?: { reusableLeases: boolean } | null; - } | null; -}): NativeSandboxLifecycle | null { - if ( - input.adapterType !== "paperclip_runner" || - input.target?.kind !== "remote" || - input.target.transport !== "sandbox" - ) - return null; - return resolveReusableSandboxLifecycle(input); -} +export { resolveNativeSandboxLifecycle } from "./heartbeat/runtime-selection.js"; interface ParsedIssueAssigneeAdapterOverrides { adapterConfig: Record | null; @@ -1115,38 +1019,7 @@ export function postWorkspaceReadyComment(input: { ); } -export async function postNativeModelFallbackWarning(input: { - issuesSvc: Pick, "addComment">; - onEvent: (event: AdapterRuntimeEvent) => Promise; - issueId: string; - runId: string; - requestedModel: string | null; - effectiveModel: string | null; - codexCliVersion: string; -}): Promise { - const message = `Using ${input.effectiveModel} because the sandbox's Codex ${input.codexCliVersion} does not support ${input.requestedModel}. Work will continue with the compatible model. Update the sandbox's Codex CLI to use the requested model.`; - await input.onEvent({ - eventType: "runner.model_fallback", - stream: "system", - level: "warn", - message, - payload: { - requestedModel: input.requestedModel, - effectiveModel: input.effectiveModel, - codexCliVersion: input.codexCliVersion, - }, - }); - await input.issuesSvc.addComment(input.issueId, message, { runId: input.runId }, { - authorType: "system", - presentation: { - kind: "system_notice", - tone: "warning", - title: `Using ${input.effectiveModel}`, - density: "compact", - detailsDefaultOpen: false, - }, - }); -} +export { postNativeModelFallbackWarning } from "./heartbeat/runtime-selection.js"; function isTruthyRuntimeEnvValue(value: string | undefined) { return value === "true" || value === "1" || value === "yes" || value === "on"; @@ -5797,759 +5670,36 @@ export function heartbeatService( } } } - let nativeExecution: NativeExecutionInput | null = null; - let getNativeFreshSessionHandoff: (() => Promise) | undefined; - let nativeRunnerInstanceId: string | null = null; - if (nativeRuntimeResolution.kind === "native") { - if (!issueRef) { - throw new Error("native_runtime_ineligible: issue is required"); - } - const nativeExecutionWorkspaceId = - persistedExecutionWorkspace?.id ?? run.id; - const nativeReviewContext = readNativeReviewAssignmentContext(context); - const nativeReview = nativeReviewContext ? await getNativeReviewAssignment(db, { - companyId: agent.companyId, issueId: issueRef.id, agentId: agent.id, - contextSnapshot: nativeReviewContext, - }) : null; - if (nativeReviewContext && !nativeReview) throw new Error("native_review_assignment_no_longer_available"); - const nativeReviewRequest = nativeReview - ? buildNativeReviewRequest({ - title: nativeReview.interaction.title, - summary: nativeReview.interaction.summary, - payload: nativeReview.interaction.payload, - }) - : null; - const persistedContract = run.completionContractId - ? await db - .select() - .from(completionContracts) - .where( - and( - eq(completionContracts.id, run.completionContractId), - eq(completionContracts.companyId, agent.companyId), - eq(completionContracts.issueId, issueRef.id), - ), - ) - .limit(1) - .then((rows) => rows[0] ?? null) - : null; - // Only a server-verified human resolution may supply a current answer - // reference. Tool/agent results and generated summaries stay evidence. - const currentHumanResponseId = !nativeReviewRequest - ? executionContinuation?.humanResponses?.find( - (response) => response.id === executionContinuation.trigger.interactionId, - )?.id - : undefined; - const immediateCompletion = (() => { - if (nativeReviewRequest) return { requests: [nativeReviewRequest], sources: [null] }; - const { requests, sources } = nativeCompletionRequestsWithSources( - safeWakeComments.length > 0 - ? safeWakeComments - : safeWakeCommentContext?.body - ? [safeWakeCommentContext] - : [], - { - requiredFullWakeCommentCount: - paperclipWakePayload?.fallbackFetchNeeded === true && - CHAT_PROVIDERS.some( - (provider) => - provider === - paperclipWakePayload.externalChatProvider, - ) && - Array.isArray(paperclipWakePayload.commentIds) - ? paperclipWakePayload.commentIds.length - : undefined, - }, - ); - // Preserve every admitted pending chat request while also - // retaining newer user direction materialized by recovery. - // A file-only wake must not inherit an old task objective. - const latestComment = - executionContinuation?.messages.findLast( - (message) => - message.authorType === "user" && - !message.createdByRunId && - !message.deleted && - message.body.trim().length > 0, - ); - const latestRequest = latestComment?.body; - if ( - latestRequest && - !requests.some( - (request) => request === latestRequest.trim(), - ) - ) { - requests.push(latestRequest.trim()); - sources.push(nativeCompletionSource("comment", latestComment!.id, latestRequest)); - } - return { requests: requests.length > 0 ? requests : undefined, sources }; - })(); - // Rebuilding a default contract is not a change in user direction. - // In particular, an upgraded checkpoint may have an intentionally - // authored contract and no continuation envelope yet. - const completionContract = - persistedContract && persistedNativeExecutionInput - ? { - row: persistedContract, - contract: persistedContract.contractJson as never, - } - : await ensureNativeCompletionContract({ - db, - companyId: agent.companyId, - issue: issueRef, - actorId: agent.id, - immediateRequest: - nativeReviewRequest ?? (currentHumanResponseId - ? null - : executionContinuation?.objective ?? safeWakeCommentContext?.body ?? null), - // The ordinary initial objective is selected by the server-owned - // continuation envelope. Carry its explicit description source - // through the singular-request compatibility path; do not infer - // provenance for review requests, answers, or wake fallbacks. - immediateRequestSource: nativeImmediateObjectiveSource({ - issueId: issueRef.id, - objectiveSource: executionContinuation?.objectiveSource, - excluded: Boolean(nativeReviewRequest || currentHumanResponseId), - }), - humanResponseId: currentHumanResponseId, - immediateRequests: immediateCompletion.requests, - immediateRequestSources: immediateCompletion.sources, - }); - const taskNativeSessionId = !taskSessionCredentialCompatible ? null : readNonEmptyString( - taskSessionDecodedParams?.sessionId, - ); - // Compatibility for native retry rows created before same-run restart - // recovery existed. Only an entirely unused replacement row may - // inherit its source checkpoint; any process/provider evidence on the - // replacement makes the ownership ambiguous and therefore ineligible. - const legacyRetrySource = - run.retryOfRunId && !isFailedChatRunRetry - ? await db - .select({ - id: heartbeatRuns.id, - companyId: heartbeatRuns.companyId, - agentId: heartbeatRuns.agentId, - runnerInstanceId: heartbeatRuns.runnerInstanceId, - nativeSessionId: heartbeatRuns.nativeSessionId, - runnerProfileJson: heartbeatRuns.runnerProfileJson, - runtimeMode: heartbeatRuns.runtimeMode, - status: heartbeatRuns.status, - }) - .from(heartbeatRuns) - .where( - and( - eq(heartbeatRuns.id, run.retryOfRunId), - eq(heartbeatRuns.companyId, agent.companyId), - eq(heartbeatRuns.agentId, agent.id), - ), - ) - .limit(1) - .then((rows) => rows[0] ?? null) - : null; - const nativeBootstrapHasProviderEvidence = - legacyRetrySource || - run.nativeSessionId || - persistedNativeExecutionInput - ? await db - .select({ id: heartbeatRunEvents.id }) - .from(heartbeatRunEvents) - .where( - and( - eq(heartbeatRunEvents.runId, run.id), - inArray(heartbeatRunEvents.eventType, [ - "harness.ready", - "session.started", - "session.resumed", - "session.updated", - "turn.started", - "provider.event", - "provider.rpc_result", - ]), - ), - ) - .limit(1) - .then((rows) => rows.length > 0) - : false; - const compatibleLegacyRetrySource = - !managedAiRuntime && !isConversation(issueContext) && context.forceFreshSession !== true && isUnusedLegacyNativeRetryReplacement({ - replacement: run, - source: legacyRetrySource, - hasProviderEvents: nativeBootstrapHasProviderEvidence, - }) - ? legacyRetrySource - : null; - const legacyRetrySessionId = - compatibleLegacyRetrySource?.nativeSessionId; - const taskResumeRunId = - taskSessionForRun?.lastRunId && - taskSessionForRun.lastRunId !== run.id && - isNativeSessionId(taskNativeSessionId) - ? taskSessionForRun.lastRunId - : null; - const resumableTaskSessionId = isDotRun ? null : taskResumeRunId - ? taskNativeSessionId - : (legacyRetrySessionId ?? null); - const requestedNativeSessionId = - run.nativeSessionId ?? resumableTaskSessionId; - // A task-session lastRunId can lag a failed turn or point at an older - // normalized session. Find the newest exact-session authority instead. - // Rows that already acquired provider authority are progress barriers, - // even when they do not contain a usable checkpoint. - const previousNativeRun = - requestedNativeSessionId && - isUnusedNativeSessionBootstrap( - run, - nativeBootstrapHasProviderEvidence, - ) - ? await findNativeSessionResumeRun(db, { - companyId: agent.companyId, - agentId: agent.id, - issueId: issueRef.id, - normalizedSessionId: requestedNativeSessionId, - currentRunId: run.id, - beforeCreatedAt: run.createdAt, - }) - : null; - nativeRunnerInstanceId = - previousNativeRun?.runnerInstanceId && - previousNativeRun.nativeSessionId === - (run.nativeSessionId ?? resumableTaskSessionId) - ? previousNativeRun.runnerInstanceId - : (run.runnerInstanceId ?? randomUUID()); - let nativeSessionId = - run.nativeSessionId ?? resumableTaskSessionId ?? randomUUID(); - let nativeResumeCheckpoint: ReturnType< - typeof rebindNativeSessionCheckpoint - > = null; - const agentLifecyclePolicy = - parseObject(agent.adapterConfig).lifecycleMode === "warm" - ? { - mode: "warm" as const, - idleTimeoutMs: resolvePaperclipRunnerIdleTimeoutMs( - parseObject(agent.adapterConfig).idleTimeoutMs, - ), - } - : { mode: "per_turn" as const, idleTimeoutMs: null }; - const environmentLifecyclePolicy = - executionTarget?.kind === "remote" && - executionTarget.transport === "sandbox" - ? (executionTarget.runnerLifecyclePolicy ?? null) - : null; - // Native Codex owns a durable, session-scoped home. It flushes refreshed - // auth into each invocation's private home before that home is removed. - // Other managed harnesses still require per-turn credential cleanup. - const supportsManagedWarmSession = agent.adapterType === "paperclip_runner" && - nativeRuntimeResolution.profile.backend === "codex_app_server"; - const effectiveLifecyclePolicy = persistedNativeExecutionInput?.session.lifecyclePolicy ?? - (nativeRuntimeResolution.profile.backend === "openai_dot_mcp" || managedAiRuntime && !supportsManagedWarmSession - ? { mode: "per_turn" as const, idleTimeoutMs: null } - : environmentLifecyclePolicy ?? agentLifecyclePolicy); - if ( - effectiveLifecyclePolicy.mode === "warm" && - executionTarget?.kind === "remote" && - executionTarget.transport === "sandbox" && - (executionTarget.reusableLeaseConfigured !== true || - executionTarget.effectiveCapabilities?.reusableLeases !== true) - ) { - throw new Error("runner_warm_environment_requires_reusable_lease"); - } - const persistedProfile = persistedRunnerProfile; - if (persistedNativeExecutionInput) { - nativeExecution = persistedNativeExecutionInput; - if ( - nativeExecution.binding.companyId !== agent.companyId || - nativeExecution.binding.runId !== run.id || - nativeExecution.binding.issueId !== issueRef.id || - nativeExecution.binding.agentId !== agent.id || - nativeExecution.binding.executionWorkspaceId !== - nativeExecutionWorkspaceId || - nativeExecution.completionContract.id !== - completionContract.row.id || - nativeExecution.completionContract.sha256 !== - completionContract.row.canonicalSha256 - ) - throw new Error( - "native_execution_input_persisted_binding_mismatch", - ); - // Recover only the originally admitted request. A stored idle - // checkpoint can precede an already-started provider turn, so even - // apparent idleness is not authority to rewrite its contract. - // Newer user direction retains its separate durable wakeup cause. - // A failed pre-bootstrap attempt may have persisted its immutable - // input before discovering that lastRunId no longer names this - // session. Restore only an exactly compatible prior checkpoint; - // never rewrite the admitted input or skip current provider work. - if ( - previousNativeRun && - isUnusedNativeSessionBootstrap( - run, - nativeBootstrapHasProviderEvidence, - ) - ) { - nativeResumeCheckpoint = rebindNativeSessionCheckpoint({ - previousRun: previousNativeRun, - currentExecution: nativeExecution, - executionTargetKind: executionTarget?.kind ?? "local", - }); - } - if (nativeExecution.provider.kind === "claude_managed") { - const recoveryProfile = await managedAgentProfileService( - db, - ).requireQualified( - agent.companyId, - nativeExecution.provider.managedProfile.profileId, - ); - assertManagedProfileRecoveryBinding({ - adapterConfig: agent.adapterConfig, - snapshot: nativeExecution.provider.managedProfile, - stored: recoveryProfile, - }); - } - if (nativeExecution.provider.kind === "aws_agentcore") { - const recoveryProfile = await remoteAgentProfileService( - db, - ).requireQualified( - agent.companyId, - nativeExecution.provider.agentCoreProfile.profileId, - "aws_bedrock_agentcore_harness", - ); - assertAgentCoreProfileRecoveryBinding({ - snapshot: nativeExecution.provider.agentCoreProfile, - stored: recoveryProfile, - }); - } - } else { - const interactionId = readNonEmptyString(context.interactionId); - const interactionResponses = context[ - EXTERNAL_CHAT_QUESTION_RESPONSE_KEY - ] - ? await materializeExternalChatQuestionResponseInput({ - db, - binding: { - companyId: agent.companyId, - issueId: issueRef.id, - runId: run.id, - agentId: agent.id, - }, - contextSnapshot: context, - }) - : await materializeNativeInteractionResponses({ - db, - companyId: agent.companyId, - issueId: issueRef.id, - runId: run.id, - agentId: agent.id, - interactionIds: Array.isArray(context.interactionIds) - ? [ - ...new Set([ - ...(interactionId ? [interactionId] : []), - ...context.interactionIds.filter( - (id): id is string => typeof id === "string", - ), - ]), - ] - : interactionId - ? [interactionId] - : [], - }); - const runnerAdapterConfig = parseObject(agent.adapterConfig); - const managedProfile = - nativeRuntimeResolution.profile.backend === - "claude_managed_agents_api" - ? await managedAgentProfileService(db).requireQualified( - agent.companyId, - readNonEmptyString(runnerAdapterConfig.managedProfileId) ?? - "", - ) - : null; - const agentCoreProfile = - nativeRuntimeResolution.profile.backend === - "aws_agentcore_harness_api" - ? await remoteAgentProfileService(db).requireQualified( - agent.companyId, - readNonEmptyString( - runnerAdapterConfig.agentCoreProfileId, - ) ?? "", - "aws_bedrock_agentcore_harness", - ) - : null; - if (managedProfile) { - const rawApiKeyBinding = parseObject( - runnerAdapterConfig.env, - ).ANTHROPIC_API_KEY; - const boundSecretId = - typeof rawApiKeyBinding === "object" && - rawApiKeyBinding !== null - ? readNonEmptyString( - (rawApiKeyBinding as Record).secretId, - ) - : null; - if (boundSecretId !== managedProfile.apiKeySecretId) { - throw new ConfigurationIncompleteFailure( - "configuration incomplete: Claude Managed profile API key is not bound at env.ANTHROPIC_API_KEY", - { - configurationIncomplete: { - reason: "managed_agent_profile_secret_binding_mismatch", - companyId: agent.companyId, - agentId: agent.id, - profileId: managedProfile.id, - requiredEnvKeys: ["ANTHROPIC_API_KEY"], - }, - }, - ); - } - } - const executionMode = - issueRef.workMode === "planning" && !isConversation(issueContext) && !acceptedPlanContinuationWake - ? ("plan" as const) - : ("default" as const); - const pinnedPlan = - executionMode === "plan" - ? await documentService(db).getIssueDocumentByKey( - issueRef.id, - "plan", - ) - : null; - const pinnedReviewContext = - executionMode === "plan" - ? await buildPlanReviewContext({ - db, - companyId: agent.companyId, - issueId: issueRef.id, - issueWorkMode: issueRef.workMode, - interactionId: readNonEmptyString(context.interactionId), - }) - : null; - const pinnedPlanMarkdown = pinnedPlan?.body ?? ""; - const dotBinding = nativeRuntimeResolution.profile.backend === "openai_dot_mcp" - ? await dotRunnerBroker(db).snapshot(agent.companyId, agent.id, String(parseObject(agent.adapterConfig).dotBindingId ?? "")) : undefined; - const nativeRuntimeContext = await buildNativeRuntimeContext({ - db, - agent, - runId: run.id, - runtimeConfig, - runtimeSkillEntries, - instructionWorkingCopy: instructionCopy ? { rootPath: instructionCopy.executionRoot, entryPath: instructionCopy.entryFile, ...(isAgentDirectoryCopy(instructionCopy) ? { kind: "agent_files" as const } : {}) } : undefined, - }); - getNativeFreshSessionHandoff = nativeReviewRequest ? undefined : getFreshSessionHandoff; - const nativeProviderConfig = nativeRuntimeResolution.profile.backend === "codex_app_server" - || nativeRuntimeResolution.profile.backend === "opencode_server" - ? projectPaperclipRunnerTaskConfig( - nativeRuntimeResolution.profile.backend, - agent.adapterConfig, - issueAssigneeOverrides?.adapterConfig, - managedAiRuntime ? readNonEmptyString(resolvedConfig.model) ?? undefined : undefined, - ) - : agent.adapterConfig; - const requestedNativeProvider = resolvePaperclipRunnerNativeProviderInput({ - backend: nativeRuntimeResolution.profile.backend, - adapterConfig: nativeProviderConfig, managedProfile, agentCoreProfile, dotBinding, - }); - const codexCliVersion = agent.adapterType === "paperclip_runner" && requestedNativeProvider.provider === "codex" - ? await readRemoteCodexModelCliVersion({ - model: requestedNativeProvider.model, - target: executionTarget, - remoteCodexPath: runtimeEnv.PAPERCLIP_RUNNER_REMOTE_CODEX_PATH, - remoteCodexNpmSpec: runtimeEnv.PAPERCLIP_RUNNER_REMOTE_CODEX_NPM_SPEC, - }) : null; - const nativeProvider = codexCliVersion - ? resolvePaperclipRunnerNativeProviderInput({ - backend: nativeRuntimeResolution.profile.backend, - adapterConfig: nativeProviderConfig, codexCliVersion, managedProfile, agentCoreProfile, dotBinding, - }) : requestedNativeProvider; - if (nativeProvider.model !== requestedNativeProvider.model) { - await postNativeModelFallbackWarning({ - issuesSvc, onEvent: onAdapterEvent, issueId: issueRef.id, runId: run.id, - requestedModel: requestedNativeProvider.model, effectiveModel: nativeProvider.model, - codexCliVersion: codexCliVersion!, - }); - } - const buildExecution = ({ normalizedSessionId, resumedSession }: { normalizedSessionId: string; resumedSession: boolean }) => - buildNativeExecutionInput({ - agentKeyId: agentIdentity?.keyId, - companyId: agent.companyId, - runId: run.id, - issue: nativeReviewRequest ? { ...issueRef, title: `Review: ${issueRef.title}`, description: nativeReviewRequest } : issueRef, - taskPrompt: [ - nativeReviewRequest ?? readNonEmptyString( - selectPaperclipTaskMarkdown(context, { - resumedSession: false, - includeCommunicationGuidance: false, - }), - ) ?? - `# ${issueRef.identifier ?? issueRef.id}: ${issueRef.title}`, - nativeRuntimeResolution.profile.backend !== "openai_dot_mcp" && projectRepositoryPaths.length > 0 - ? `## Project repositories\nThe task workspace also contains these editable Git repositories:\n${projectRepositoryPaths.map((repo) => `- ${repo}`).join("\n")}` - : null, - ].filter(Boolean).join("\n\n"), - initialCommunicationGuidance: nativeReviewRequest ? null : readNonEmptyString(context.paperclipTaskCommunicationGuidance), - wakePayload: context.paperclipWake, - turnContext: context.paperclipTurnContext, - githubInstructionSkillKeys: runScopedMentionedSkillKeys, - resumedSession, - previousTurn: (() => { - if (!previousNativeRun || nativeReviewRequest) return null; - try { - const previousTask = parseNativeExecutionInput(parseObject(previousNativeRun.runnerProfileJson).nativeExecutionInput).task; - if (paperclipWakePayload?.externalChatProvider) { - // External native inputs use a neutral task title. Compare - // the saved canonical brief so old provider text is not - // repeated as a change, while genuine edits still arrive. - const savedIssue = parseObject(parseObject(previousNativeRun.contextSnapshot).paperclipIssue); - if (savedIssue.id !== issueRef.id || typeof savedIssue.title !== "string" || - (savedIssue.description !== null && typeof savedIssue.description !== "string")) return null; - return { runId: previousNativeRun.id, task: { title: savedIssue.title, description: savedIssue.description } }; - } - return { runId: previousNativeRun.id, task: previousTask }; - } catch { - // An invalid prior snapshot must use the fresh bootstrap. - return null; - } - })(), - conversationMode: context.conversationMode === true, - agentId: agent.id, - workspace: { - // Projectless paperclip_runner tasks still have a resolved local cwd. Bind that - // transient workspace to the run id so the native input remains durable and replayable - // without fabricating a project-scoped execution_workspaces row. - id: nativeExecutionWorkspaceId, - cwd: executionWorkspace.cwd, - repoUrl: executionWorkspace.repoUrl, - repoRef: executionWorkspace.repoRef, - branchName: executionWorkspace.branchName, - }, - normalizedSessionId, - executionMode, - planningContext: - executionMode === "plan" - ? { - documentId: pinnedPlan?.id ?? null, - baseRevisionId: - pinnedPlan?.latestRevisionId ?? null, - baseRevisionNumber: - pinnedPlan?.latestRevisionNumber ?? 0, - markdown: pinnedPlanMarkdown, - sha256: createHash("sha256") - .update(pinnedPlanMarkdown) - .digest("hex"), - reviewContext: pinnedReviewContext - ? (structuredClone( - pinnedReviewContext, - ) as unknown as Record) - : {}, - } - : null, - ...nativeProvider, - lifecyclePolicy: effectiveLifecyclePolicy, - interactionResponses, - completionContract: { - id: completionContract.row.id, - sha256: completionContract.row.canonicalSha256, - schemaVersion: completionContract.row.schemaVersion, - contract: completionContract.contract, - sources: "sources" in completionContract ? completionContract.sources : undefined, - }, - runtimeContext: nativeRuntimeContext, - }); - const backendDescriptor = await describeRunnerdNativeSessionBackend(buildExecution({ - normalizedSessionId: nativeSessionId, resumedSession: previousNativeRun !== null, - })); - const nativeExecutionWithCheckpoint = buildNativeExecutionWithCheckpoint({ - previousRun: previousNativeRun, - normalizedSessionId: nativeSessionId, - executionTargetKind: executionTarget?.kind ?? "local", - toolRefreshOnResume: backendDescriptor.capabilities.toolRefreshOnResume === true, - refreshTools: context.refreshTools === true, - buildExecution, - }); - nativeExecution = nativeExecutionWithCheckpoint.execution; - nativeResumeCheckpoint = nativeExecutionWithCheckpoint.checkpoint; - if ( - nativeSessionId !== - nativeExecutionWithCheckpoint.normalizedSessionId - ) { - nativeRunnerInstanceId = randomUUID(); - } - nativeSessionId = nativeExecutionWithCheckpoint.normalizedSessionId; - } - const nativeSandboxLifecycle = resolveNativeSandboxLifecycle({ - adapterType: agent.adapterType, - lifecyclePolicy: nativeExecution.session.lifecyclePolicy, - target: executionTarget, - }); - if (nativeSandboxLifecycle) { - nativeLifecycleTelemetryForRun = { - provider: nativeExecution.provider.kind, - harness: nativeExecution.session.driverKind, - lifecycleMode: nativeExecution.session.lifecyclePolicy.mode, - sandboxResource: nativeSandboxLifecycle.sandboxResource, - }; - const selectedLifecycleSpan = getStartupTracer( - "paperclip.environment-lifecycle", - ).startSpan("sandbox.lifecycle.selected", { - attributes: { - "paperclip.native.span.provider": nativeExecution.provider.kind, - "paperclip.native.span.harness": - nativeExecution.session.driverKind, - "paperclip.native.span.lifecycle_mode": - nativeExecution.session.lifecyclePolicy.mode, - "paperclip.native.span.sandbox_resource": - nativeSandboxLifecycle.sandboxResource, - "paperclip.native.span.outcome": "selected", - "paperclip.native.span.bytes_transferred": 0, - }, - }); - selectedLifecycleSpan.end(); - } - providerResourceDispositionForRun = - nativeSandboxLifecycle?.sandboxResource === "keep_running" - ? "keep_running" - : nativeSandboxLifecycle?.sandboxResource === "stop_and_reuse" - ? "stop_and_retain" - : nativeSandboxLifecycle?.sandboxResource === - "destroy_after_turn" - ? "destroy" - : undefined; - await options.beforeNativeRuntimeSelection?.(run.id); - const nativeSelected = await db.transaction(async (tx) => { - const lockedRun = await tx - .select() - .from(heartbeatRuns) - .where(eq(heartbeatRuns.id, run.id)) - .for("update") - .limit(1) - .then((rows) => rows[0] ?? null); - if (!lockedRun) throw new Error("native_runtime_run_missing"); - // Cancellation and runtime selection serialize on this row. A - // stopped preparation must never create a new native coordinator. - if (lockedRun.status !== "running" || lockedRun.resultJson?.startupCancellation) return false; - if (lockedRun.runtimeMode === "legacy" && lockedRun.controllerBootId && - !(await renewLegacyControllerLease(tx as unknown as Db, lockedRun))) { - nativeOwnershipHeld = true; - return false; - } - if ( - lockedRun.runtimeModeResolvedAt && - lockedRun.runtimeMode !== "native" - ) { - throw new Error("native_runtime_mode_conflict"); - } - const lockedProfile = parseObject(lockedRun.runnerProfileJson); - const persistedNativeSessionId = - await prepareNativeSessionBootstrapPersistence(tx, { - run: lockedRun, - selectedSessionId: nativeSessionId, - execution: nativeExecution!, - restoringCheckpoint: nativeResumeCheckpoint !== null, - }); - await tx - .update(heartbeatRuns) - .set({ - runtimeMode: "native", - runtimeModeResolverVersion: - lockedRun.runtimeModeResolverVersion ?? - nativeRuntimeResolution.resolverVersion, - runtimeModeReason: - lockedRun.runtimeModeReason ?? nativeRuntimeResolution.reason, - runtimeModeResolvedAt: - lockedRun.runtimeModeResolvedAt ?? new Date(), - runnerProfileJson: { - ...nativeRuntimeResolution.profile, - ...lockedProfile, - ...(lockedProfile.nativeExecutionInput - ? {} - : { recoveryEventInventoryVersion: 1 }), - ...(providerTraceRequested - ? { - providerTrace: { - mode: "raw", - traceId: providerTraceCapture?.metadata.id ?? null, - maxBytes: PROVIDER_TRACE_MAX_BYTES, - }, - } - : {}), - nativeExecutionInput: - lockedProfile.nativeExecutionInput ?? nativeExecution, - nativeToolContractFingerprint: - nativeToolContractFingerprintForTarget( - executionTarget?.kind ?? "local", - ), - ...(lockedProfile.sessionCheckpoint != null - ? { sessionCheckpoint: lockedProfile.sessionCheckpoint } - : nativeResumeCheckpoint - ? { - sessionCheckpoint: - nativeResumeCheckpoint as unknown as Record< - string, - unknown - >, - } - : {}), - }, - runnerInstanceId: - previousNativeRun?.runnerInstanceId && - persistedNativeSessionId === previousNativeRun.nativeSessionId - ? previousNativeRun.runnerInstanceId - : lockedRun.nativeSessionId !== persistedNativeSessionId - ? nativeRunnerInstanceId - : (lockedRun.runnerInstanceId ?? nativeRunnerInstanceId), - nativeSessionId: persistedNativeSessionId, - processPid: - lockedRun.processPid ?? - (previousNativeRun?.nativeSessionId === nativeSessionId - ? previousNativeRun.processPid - : null), - processGroupId: - lockedRun.processGroupId ?? - (previousNativeRun?.nativeSessionId === nativeSessionId - ? previousNativeRun.processGroupId - : null), - processStartedAt: - lockedRun.processStartedAt ?? - (previousNativeRun?.nativeSessionId === nativeSessionId - ? previousNativeRun.processStartedAt - : null), - nativeIssueId: lockedRun.nativeIssueId ?? issueRef.id, - driverKind: - lockedRun.driverKind ?? - nativeExecution?.session.driverKind ?? - "codex_app_server", - driverVersion: lockedRun.driverVersion ?? "phase6-v1", - completionContractId: - lockedRun.completionContractId ?? completionContract.row.id, - completionContractSha256: - lockedRun.completionContractSha256 ?? - completionContract.row.canonicalSha256, - nativePhase: lockedRun.nativePhase ?? "observed", - nativePhaseUpdatedAt: - lockedRun.nativePhaseUpdatedAt ?? new Date(), - updatedAt: new Date(), - }) - .where(eq(heartbeatRuns.id, run.id)); - await tx - .insert(nativeRunFinalizations) - .values({ - runId: run.id, - companyId: agent.companyId, - issueId: issueRef.id, - phase: "observed", - }) - .onConflictDoNothing(); - return true; - }); - if (!nativeSelected) return; - controllerLease.stop(); - nativeWorkspaceSync = await prepareNativeWorkspaceSync({ - db, - runId: run.id, - companyId: agent.companyId, - workspaceId: nativeExecutionWorkspaceId, - workspaceLocalDir: executionWorkspace.cwd, - target: executionTarget, - lease: activeEnvironmentLease.lease, - restartRecovery: runOptions.nativeRestartRecovery, - sameRunRecovery: Boolean(runOptions.nativeLeaseOwner), - resourceDisposition: providerResourceDispositionForRun, + const runtimeSelection = await selectHeartbeatRuntime(db, { + run, + agent, + nativeRuntimeResolution, + task: { + issueRef, + issueContext, + context, + executionContinuation, + safeWakeComments, + safeWakeCommentContext, + paperclipWakePayload, + acceptedPlanContinuationWake, + }, + session: { + persistedNativeExecutionInput, + persistedRunnerProfile, + taskSessionCredentialCompatible, + taskSessionDecodedParams, + taskSessionForRun, + isFailedChatRunRetry, + getFreshSessionHandoff, + }, + workspace: { + persistedExecutionWorkspace, + executionWorkspace, + executionTarget, + activeEnvironmentLease, + isDotRun, + projectRepositoryPaths, onCheckpoint: async (metrics) => { await appendRunEvent(run, { eventType: "lifecycle", stream: "system", level: "info", message: "workspace_checkpoint", payload: { phase: "workspace_checkpoint", ...metrics } }); @@ -6560,40 +5710,37 @@ export function heartbeatService( finally { await appendRunEvent(run, { eventType: "lifecycle", stream: "system", level: "info", message: name, payload: { phase: name, durationMs: Date.now() - started } }); } }, - }); - } else { - const legacyWarmLifecycle = - executionTarget?.kind === "remote" && - executionTarget.transport === "sandbox" && - executionTarget.runnerLifecyclePolicy?.mode === "warm" - ? resolveReusableSandboxLifecycle({ - lifecyclePolicy: executionTarget.runnerLifecyclePolicy, - target: executionTarget, - }) - : null; - if (legacyWarmLifecycle?.sandboxResource === "keep_running") { - providerResourceDispositionForRun = "keep_running"; - } - await db - .update(heartbeatRuns) - .set({ - runtimeMode: "legacy", - runtimeModeResolverVersion: - nativeRuntimeResolution.resolverVersion, - runtimeModeReason: nativeRuntimeResolution.reason, - runtimeModeResolvedAt: run.runtimeModeResolvedAt ?? new Date(), - // Preserve server-owned admission and dispatch evidence on this - // row; never copy another run's execution profile. - runnerProfileJson: sql`(case when ${heartbeatRuns.runnerProfileJson} ? ${CHAT_CONTROL_RECOVERY_ADMISSION_KEY} - then jsonb_build_object(${CHAT_CONTROL_RECOVERY_ADMISSION_KEY}::text, ${heartbeatRuns.runnerProfileJson}->${CHAT_CONTROL_RECOVERY_ADMISSION_KEY}) - else '{}'::jsonb end) - || (case when ${heartbeatRuns.runnerProfileJson} ? 'adapterDispatch' - then jsonb_build_object('adapterDispatch', ${heartbeatRuns.runnerProfileJson}->'adapterDispatch') - else '{}'::jsonb end) || ${JSON.stringify(providerTraceRequested ? { providerTrace: { mode: "raw", traceId: providerTraceCapture?.metadata.id ?? null, maxBytes: PROVIDER_TRACE_MAX_BYTES } } : {})}::jsonb`, - updatedAt: new Date(), - }) - .where(eq(heartbeatRuns.id, run.id)); - } + }, + config: { + runtimeConfig, + runtimeSkillEntries, + runScopedMentionedSkillKeys, + instructionCopy, + agentIdentity, + issueAssigneeOverrides, + resolvedConfig, + managedAiRuntime, + }, + recovery: runOptions, + trace: { providerTraceRequested, providerTraceCapture }, + issuesSvc, + runtimeEnv, + beforeNativeRuntimeSelection: options.beforeNativeRuntimeSelection, + onAdapterEvent, + onNativeLifecycleSelected: (telemetry) => { + nativeLifecycleTelemetryForRun = telemetry; + }, + onProviderResourceDisposition: (disposition) => { + providerResourceDispositionForRun = disposition; + }, + onNativeOwnershipHeld: () => { + nativeOwnershipHeld = true; + }, + stopControllerLease: () => controllerLease.stop(), + }); + if (!runtimeSelection.selected) return; + const { nativeExecution, nativeRunnerInstanceId, getNativeFreshSessionHandoff } = runtimeSelection; + nativeWorkspaceSync = runtimeSelection.nativeWorkspaceSync; const localAgentJwtScope = issueRef?.workMode === "skill_test" ? { kind: "skill_test" as const, issueId: issueRef.id } @@ -8174,6 +7321,12 @@ export function heartbeatService( resumeQueuedRuns, + resumeQueuedRunsForAgent: async (companyId: string, agentId: string) => { + const agent = await getAgent(agentId); + if (!agent || agent.companyId !== companyId) return []; + return startNextQueuedRunForAgent(agentId); + }, + scheduleBoundedRetry, reconcileStrandedAssignedIssues, diff --git a/server/src/services/heartbeat/runtime-selection.test.ts b/server/src/services/heartbeat/runtime-selection.test.ts new file mode 100644 index 00000000000..f5f2c8bb706 --- /dev/null +++ b/server/src/services/heartbeat/runtime-selection.test.ts @@ -0,0 +1,233 @@ +import { randomUUID } from "node:crypto"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { agents, companies, completionContracts, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, heartbeatRuns, issues, nativeRunFinalizations, type Db } from "@paperclipai/db"; +import { eq, sql } from "drizzle-orm"; +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js"; +import { CHAT_CONTROL_RECOVERY_ADMISSION_KEY } from "../chat-control-recovery-stop.js"; +import { buildNativeRuntimeContext } from "../native-runtime/runtime-context.js"; +import { nativeRuntimeContextFixture } from "../native-runtime/runtime-context.test-fixture.js"; +import { prepareNativeWorkspaceSync } from "../native-runtime/native-workspace-sync.js"; +import { resolveHeartbeatNativeRuntimeMode } from "../native-runtime/runtime-mode.js"; +import { canonicalNativeRuntimeContextDigest } from "../../vendor/paperclip-runner/index.js"; +import { selectHeartbeatRuntime, type HeartbeatRuntimeSelectionInput } from "./runtime-selection.js"; + +// Keep the real contract, session, and runtime-selection transactions. Replace only +// filesystem materialization and sandbox synchronization at their external boundaries. +vi.mock("../native-runtime/runtime-context.js", async (original) => ({ + ...await original(), + buildNativeRuntimeContext: vi.fn(async () => nativeRuntimeContextFixture()), +})); +vi.mock("../native-runtime/native-workspace-sync.js", async (original) => ({ + ...await original(), + prepareNativeWorkspaceSync: vi.fn(async () => null), +})); + +const support = await getEmbeddedPostgresTestSupport(); +describe.skipIf(!support.supported)("heartbeat runtime selection boundary", () => { + let database: Awaited>; + let db: Db; + let instructionRoot: string; + beforeAll(async () => { + instructionRoot = await mkdtemp(join(tmpdir(), "paperclip-selection-instructions-")); + await writeFile(join(instructionRoot, "AGENTS.md"), "Complete the assigned task.\n"); + database = await startEmbeddedPostgresTestDatabase("paperclip-runtime-selection"); + db = createDb(database.connectionString); + await db.execute(sql`set client_min_messages = warning`); + }, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS); + afterAll(async () => { await db?.$client.end(); await database?.cleanup(); if (instructionRoot) await rm(instructionRoot, { recursive: true, force: true }); }); + beforeEach(async () => { + await db.execute(sql`truncate table companies restart identity cascade`); + vi.clearAllMocks(); + vi.mocked(prepareNativeWorkspaceSync).mockResolvedValue(null); + vi.mocked(buildNativeRuntimeContext).mockImplementation(async () => { + const context = nativeRuntimeContextFixture(); + context.instructions.bundle.rootPath = instructionRoot; + return { ...context, aggregateDigest: canonicalNativeRuntimeContextDigest(context) }; + }); + }); + + async function fixture(adapterType = "paperclip_runner") { + const [company] = await db.insert(companies).values({ name: "Runtime selection", issuePrefix: "SEL" }).returning(); + const [agent] = await db.insert(agents).values({ companyId: company.id, name: "Selecting agent", status: "running", adapterType, adapterConfig: { provider: "codex", model: "gpt-5.6-luna" } }).returning(); + const [issue] = await db.insert(issues).values({ companyId: company.id, title: "Complete this task", description: "Preserve the requested work", assigneeAgentId: agent.id }).returning(); + const [run] = await db.insert(heartbeatRuns).values({ companyId: company.id, agentId: agent.id, issueId: issue.id, scopeKind: "issue", status: "running" }).returning(); + const now = new Date(); + const input: HeartbeatRuntimeSelectionInput = { + run, agent, + nativeRuntimeResolution: resolveHeartbeatNativeRuntimeMode({ persisted: run, enabled: true, runtimeConfig: {}, adapterConfig: agent.adapterConfig, agent, issue, target: null, workspaceId: null }), + task: { issueRef: issue, issueContext: null, context: { paperclipTaskMarkdown: "Complete this task" }, executionContinuation: null, safeWakeComments: [], safeWakeCommentContext: null, paperclipWakePayload: null, acceptedPlanContinuationWake: false }, + session: { persistedNativeExecutionInput: null, persistedRunnerProfile: {}, taskSessionCredentialCompatible: true, taskSessionDecodedParams: null, taskSessionForRun: null, isFailedChatRunRetry: false, getFreshSessionHandoff: vi.fn(async () => "Earlier context") }, + workspace: { persistedExecutionWorkspace: null, executionWorkspace: { cwd: "/tmp/paperclip-runtime-selection", repoUrl: null, repoRef: null, branchName: null }, executionTarget: null, isDotRun: false, projectRepositoryPaths: [], + activeEnvironmentLease: { lease: { id: randomUUID(), companyId: company.id, environmentId: null, executionWorkspaceId: null, issueId: issue.id, heartbeatRunId: run.id, status: "active", leasePolicy: "ephemeral", provider: null, providerLeaseId: null, acquiredAt: now, lastUsedAt: now, expiresAt: null, releasedAt: null, failureReason: null, cleanupStatus: null, metadata: null, createdAt: now, updatedAt: now } }, + }, + config: { runtimeConfig: {}, runtimeSkillEntries: [], runScopedMentionedSkillKeys: [], instructionCopy: null, agentIdentity: undefined, issueAssigneeOverrides: null, resolvedConfig: {}, managedAiRuntime: undefined }, + recovery: {}, trace: { providerTraceRequested: false, providerTraceCapture: null }, + issuesSvc: { addComment: vi.fn() }, runtimeEnv: {}, onAdapterEvent: vi.fn(async () => {}), + onNativeLifecycleSelected: vi.fn(), onProviderResourceDisposition: vi.fn(), onNativeOwnershipHeld: vi.fn(), stopControllerLease: vi.fn(), + }; + return { input, issue, run, agent }; + } + const read = (id: string) => db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, id)).then(rows => rows[0]!); + const coordinators = (id: string) => db.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, id)); + const sandbox = { kind: "remote" as const, transport: "sandbox" as const, remoteCwd: "/workspace", reusableLeaseConfigured: false }; + + it("persists the native input, completion contract, and coordinator before transferring cleanup ownership", async () => { + const { input, run } = await fixture(); + input.workspace.onCheckpoint = vi.fn(async () => {}); + input.workspace.onPhase = async (_name, work) => work(); + vi.mocked(prepareNativeWorkspaceSync).mockImplementationOnce(async args => { + expect(await read(run.id)).toMatchObject({ runtimeMode: "native", nativeIssueId: input.task.issueRef!.id }); + expect(await coordinators(run.id)).toHaveLength(1); + expect(input.stopControllerLease).toHaveBeenCalledOnce(); + expect(args).toMatchObject({ runId: run.id, workspaceId: run.id, workspaceLocalDir: input.workspace.executionWorkspace.cwd }); + expect(args.onCheckpoint).toBe(input.workspace.onCheckpoint); + expect(args.onPhase).toBe(input.workspace.onPhase); + return null; + }); + const selected = await selectHeartbeatRuntime(db, input); + expect(selected.selected).toBe(true); + if (!selected.selected) throw new Error("Expected selection"); + const saved = await read(run.id); + expect(saved).toMatchObject({ runtimeMode: "native", driverKind: "codex_app_server", nativePhase: "observed", nativeSessionId: expect.any(String), runnerInstanceId: selected.nativeRunnerInstanceId, + runnerProfileJson: { nativeExecutionInput: selected.nativeExecution, recoveryEventInventoryVersion: 1 } }); + expect(await db.select().from(completionContracts)).toHaveLength(1); + expect(selected.getNativeFreshSessionHandoff).toBe(input.session.getFreshSessionHandoff); + expect(input.onNativeOwnershipHeld).not.toHaveBeenCalled(); + }); + + it("persists configured GitHub instruction skills without admitting provider-mentioned skills", async () => { + const { input, issue, run } = await fixture(); + input.config.runScopedMentionedSkillKeys = ["company/test/configured"]; + input.task.context.paperclipWake = { + reason: "External chat message received", externalChatProvider: "github", checkedOutByHarness: true, + issue: { id: issue.id, workMode: "standard" }, + comments: [{ id: "review-comment", body: "Please use /provider-mentioned" }], + commentIds: ["review-comment"], latestCommentId: "review-comment", + commentWindow: { requestedCount: 1, includedCount: 1, missingCount: 0 }, fallbackFetchNeeded: false, + }; + vi.mocked(buildNativeRuntimeContext).mockImplementationOnce(async () => { + const context = nativeRuntimeContextFixture(); + context.instructions.bundle.rootPath = instructionRoot; + context.skills = ["configured", "provider-mentioned"].map(name => ({ + key: `company/test/${name}`, runtimeName: name, versionId: null, + bundle: { ...context.instructions.bundle }, + })); + return { ...context, aggregateDigest: canonicalNativeRuntimeContextDigest(context) }; + }); + expect(await selectHeartbeatRuntime(db, input)).toMatchObject({ + selected: true, nativeExecution: { task: { description: "Configured GitHub instruction skills:\n/configured" } }, + }); + expect((await read(run.id)).runnerProfileJson).toMatchObject({ + nativeExecutionInput: { task: { description: "Configured GitHub instruction skills:\n/configured" } }, + }); + }); + + it("restores the immutable admitted input and saved checkpoint when newer context arrives", async () => { + const { input, run } = await fixture(); + const selected = await selectHeartbeatRuntime(db, input); + if (!selected.selected || !selected.nativeExecution) throw new Error("Expected native selection"); + const saved = await read(run.id); + const profile = { ...saved.runnerProfileJson, sessionCheckpoint: { retained: "checkpoint" } }; + await db.update(heartbeatRuns).set({ runnerProfileJson: profile }).where(eq(heartbeatRuns.id, run.id)); + input.run = await read(run.id); + input.session.persistedRunnerProfile = profile; + input.session.persistedNativeExecutionInput = selected.nativeExecution; + input.task.context.paperclipTaskMarkdown = "A newer unrelated request"; + input.task.safeWakeComments = [{ id: randomUUID(), body: "New direction belongs to a separate wake" }]; + vi.mocked(buildNativeRuntimeContext).mockClear(); + const resumed = await selectHeartbeatRuntime(db, input); + expect(resumed).toMatchObject({ selected: true, nativeExecution: selected.nativeExecution, nativeRunnerInstanceId: selected.nativeRunnerInstanceId }); + expect((await read(run.id)).runnerProfileJson).toMatchObject(profile); + expect(await db.select().from(completionContracts)).toHaveLength(1); + expect(await coordinators(run.id)).toHaveLength(1); + expect(buildNativeRuntimeContext).not.toHaveBeenCalled(); + }); + + it("rejects an admitted input bound to another run without replacing the saved runtime", async () => { + const { input, run } = await fixture(); + const selected = await selectHeartbeatRuntime(db, input); + if (!selected.selected || !selected.nativeExecution) throw new Error("Expected native selection"); + const saved = await read(run.id); + input.run = saved; + input.session.persistedNativeExecutionInput = { ...selected.nativeExecution, binding: { ...selected.nativeExecution.binding, runId: randomUUID() } }; + await expect(selectHeartbeatRuntime(db, input)).rejects.toThrow("native_execution_input_persisted_binding_mismatch"); + expect((await read(run.id)).runnerProfileJson).toEqual(saved.runnerProfileJson); + }); + + it.each(["cancelled", "startup_cancellation"])("does not create a native coordinator when %s wins preparation", async winner => { + const { input, run } = await fixture(); + input.beforeNativeRuntimeSelection = async () => { + await db.update(heartbeatRuns).set(winner === "cancelled" ? { status: "cancelled" } : { resultJson: { startupCancellation: { beforeNativeSelection: true } } }).where(eq(heartbeatRuns.id, run.id)); + }; + expect(await selectHeartbeatRuntime(db, input)).toEqual({ selected: false }); + expect(await read(run.id)).toMatchObject({ runtimeMode: "legacy", nativeSessionId: null, completionContractId: null }); + expect(await coordinators(run.id)).toHaveLength(0); + expect(input.stopControllerLease).not.toHaveBeenCalled(); + expect(prepareNativeWorkspaceSync).not.toHaveBeenCalled(); + }); + + it("reports a lost controller lease before returning without native selection", async () => { + const { input, run } = await fixture(); + await db.update(heartbeatRuns).set({ controllerBootId: randomUUID(), controllerLeaseExpiresAt: new Date(Date.now() + 60_000) }).where(eq(heartbeatRuns.id, run.id)); + expect(await selectHeartbeatRuntime(db, input)).toEqual({ selected: false }); + expect(input.onNativeOwnershipHeld).toHaveBeenCalledOnce(); + expect(input.stopControllerLease).not.toHaveBeenCalled(); + expect(await coordinators(run.id)).toHaveLength(0); + }); + + it("rejects a competing persisted legacy selection atomically", async () => { + const { input, run } = await fixture(); + input.beforeNativeRuntimeSelection = async () => { + await db.update(heartbeatRuns).set({ runtimeModeResolvedAt: new Date(), runtimeMode: "legacy" }).where(eq(heartbeatRuns.id, run.id)); + }; + await expect(selectHeartbeatRuntime(db, input)).rejects.toThrow("native_runtime_mode_conflict"); + expect((await read(run.id)).runtimeMode).toBe("legacy"); + expect(await coordinators(run.id)).toHaveLength(0); + expect(input.stopControllerLease).not.toHaveBeenCalled(); + }); + + it("reports sandbox teardown policy even when a later preparation step throws", async () => { + const { input, run } = await fixture(); + input.workspace.executionTarget = sandbox; + input.beforeNativeRuntimeSelection = async () => { throw new Error("selection interrupted"); }; + await expect(selectHeartbeatRuntime(db, input)).rejects.toThrow("selection interrupted"); + expect(input.onNativeLifecycleSelected).toHaveBeenCalledWith({ provider: "codex", harness: "codex_app_server", lifecycleMode: "per_turn", sandboxResource: "destroy_after_turn" }); + expect(input.onProviderResourceDisposition).toHaveBeenCalledWith("destroy"); + expect(input.stopControllerLease).not.toHaveBeenCalled(); + expect(await coordinators(run.id)).toHaveLength(0); + }); + + it("retains native admission and teardown state if workspace sync preparation fails", async () => { + const { input, run } = await fixture(); + input.workspace.executionTarget = sandbox; + vi.mocked(prepareNativeWorkspaceSync).mockRejectedValueOnce(new Error("sync preparation failed")); + await expect(selectHeartbeatRuntime(db, input)).rejects.toThrow("sync preparation failed"); + expect(input.onProviderResourceDisposition).toHaveBeenCalledWith("destroy"); + expect(input.stopControllerLease).toHaveBeenCalledOnce(); + expect((await read(run.id)).runtimeMode).toBe("native"); + expect(await coordinators(run.id)).toHaveLength(1); + }); + + it("rejects a warm native sandbox that cannot reuse its lease", async () => { + const { input, run } = await fixture(); + input.workspace.executionTarget = { ...sandbox, runnerLifecyclePolicy: { mode: "warm", idleTimeoutMs: 300_000 } }; + await expect(selectHeartbeatRuntime(db, input)).rejects.toThrow("runner_warm_environment_requires_reusable_lease"); + expect(input.onNativeLifecycleSelected).not.toHaveBeenCalled(); + expect(await coordinators(run.id)).toHaveLength(0); + }); + + it("preserves only server-owned admission and dispatch evidence when recording legacy selection", async () => { + const { input, run } = await fixture("process"); + const admission = { version: 1, kind: "fixture" }, dispatch = { entered: true }; + await db.update(heartbeatRuns).set({ runnerProfileJson: { [CHAT_CONTROL_RECOVERY_ADMISSION_KEY]: admission, adapterDispatch: dispatch, unrelated: "drop" } }).where(eq(heartbeatRuns.id, run.id)); + input.trace = { providerTraceRequested: true, providerTraceCapture: { metadata: { id: "trace-id" } } }; + expect(await selectHeartbeatRuntime(db, input)).toMatchObject({ selected: true, nativeExecution: null, nativeWorkspaceSync: null }); + expect((await read(run.id)).runnerProfileJson).toEqual({ [CHAT_CONTROL_RECOVERY_ADMISSION_KEY]: admission, adapterDispatch: dispatch, providerTrace: { mode: "raw", traceId: "trace-id", maxBytes: 64 * 1024 * 1024 } }); + expect(await coordinators(run.id)).toHaveLength(0); + expect(input.stopControllerLease).not.toHaveBeenCalled(); + expect(buildNativeRuntimeContext).not.toHaveBeenCalled(); + }); +}); diff --git a/server/src/services/heartbeat/runtime-selection.ts b/server/src/services/heartbeat/runtime-selection.ts new file mode 100644 index 00000000000..c179e948bcf --- /dev/null +++ b/server/src/services/heartbeat/runtime-selection.ts @@ -0,0 +1,1057 @@ +import { + ConfigurationIncompleteFailure, + type buildPaperclipWakePayload, + type createHeartbeatRunPreparation, +} from "./run-preparation.js"; +import { dotRunnerBroker } from "../dot-runner-broker.js"; +import { isAgentDirectoryCopy } from "../agent-directory-working-copies.js"; +import { isConversation } from "../agent-conversations.js"; +import { renewLegacyControllerLease } from "../legacy-controller-lease.js"; +import type { prepareManagedAiRuntime } from "../ai-connection-runtime.js"; +import { + getNativeReviewAssignment, + readNativeReviewAssignmentContext, +} from "../native-runtime/native-review-participant.js"; +import { buildNativeReviewRequest } from "../native-runtime/native-review-prompt.js"; +import type { buildExecutionContinuation } from "../execution-continuation.js"; +import type { agentInstructionWorkingCopyService } from "../agent-instruction-working-copies.js"; +import { createHash, randomUUID } from "node:crypto"; +import { and, eq, inArray, sql } from "drizzle-orm"; +import type { Db } from "@paperclipai/db"; +import { CHAT_PROVIDERS } from "@paperclipai/shared"; +import { + type agents, + completionContracts, + heartbeatRunEvents, + heartbeatRuns, + nativeRunFinalizations, +} from "@paperclipai/db"; +import { getStartupTracer } from "../../instrumentation.js"; +import { documentService } from "../documents.js"; +import { managedAgentProfileService } from "../managed-agent-profiles.js"; +import { remoteAgentProfileService } from "../remote-agent-profiles.js"; +import { + buildNativeExecutionInput, + buildNativeExecutionWithCheckpoint, + buildNativeRuntimeContext, + ensureNativeCompletionContract, + findNativeSessionResumeRun, + isNativeSessionId, + isUnusedNativeSessionBootstrap, + isUnusedLegacyNativeRetryReplacement, + materializeNativeInteractionResponses, + nativeCompletionRequestsWithSources, + nativeCompletionSource, + nativeImmediateObjectiveSource, + nativeToolContractFingerprintForTarget, + prepareNativeSessionBootstrapPersistence, + prepareNativeWorkspaceSync, + type NativeRestartRecoveryClaim, + rebindNativeSessionCheckpoint, + type resolveHeartbeatNativeRuntimeMode, +} from "../native-runtime/index.js"; +import { + assertAgentCoreProfileRecoveryBinding, + assertManagedProfileRecoveryBinding, + projectPaperclipRunnerTaskConfig, + resolvePaperclipRunnerNativeProviderInput, +} from "../native-runtime/provider-profile.js"; +import { readRemoteCodexModelCliVersion } from "../native-runtime/codex-model-fallback.js"; +import { + describeRunnerdNativeSessionBackend, + parseNativeExecutionInput, + type NativeExecutionInput, +} from "../../vendor/paperclip-runner/index.js"; +import { PROVIDER_TRACE_MAX_BYTES } from "../provider-trace-store.js"; +import type { AdapterRuntimeEvent } from "../../adapters/index.js"; +import { parseObject } from "../../adapters/utils.js"; +import { EXTERNAL_CHAT_QUESTION_RESPONSE_KEY } from "../native-runtime/external-chat-question-response.js"; +import { + materializeExternalChatQuestionResponseInput, +} from "../native-runtime/external-chat-question-response-input.js"; +import { CHAT_CONTROL_RECOVERY_ADMISSION_KEY } from "../chat-control-recovery-stop.js"; +import type { issueService } from "../issues.js"; +import { buildPlanReviewContext } from "../plan-review-context.js"; +import { resolvePaperclipRunnerIdleTimeoutMs } from "@paperclipai/adapter-utils"; +import { selectPaperclipTaskMarkdown } from "@paperclipai/adapter-utils/server-utils"; +import type { ProviderResourceDisposition } from "../environment-runtime.js"; + +type Run = typeof heartbeatRuns.$inferSelect; +type Agent = typeof agents.$inferSelect; +type IssueContext = Awaited["getIssueExecutionContext"]>> | null; +type IssueRef = Parameters[0]["issue"] & Parameters[0]["issue"]; + +export interface NativeLifecycleTelemetry { + provider: string; + harness: string; + lifecycleMode: "per_turn" | "warm"; + sandboxResource: "keep_running" | "stop_and_reuse" | "destroy_after_turn"; +} + +export interface HeartbeatRuntimeSelectionInput { + run: Run; + agent: Agent; + nativeRuntimeResolution: ReturnType; + task: { + issueRef: IssueRef | null; + issueContext: IssueContext; + context: Record; + executionContinuation: Awaited> | null; + safeWakeComments: Parameters[0]; + safeWakeCommentContext: { id: string; body: string } | null; + paperclipWakePayload: Awaited>; + acceptedPlanContinuationWake: boolean; + }; + session: { + persistedNativeExecutionInput: NativeExecutionInput | null; + persistedRunnerProfile: Record; + taskSessionCredentialCompatible: boolean; + taskSessionDecodedParams: Record | null; + taskSessionForRun: { lastRunId: string | null } | null; + isFailedChatRunRetry: boolean; + getFreshSessionHandoff: (() => Promise) | undefined; + }; + workspace: { + persistedExecutionWorkspace: { id: string } | null; + executionWorkspace: Omit[0]["workspace"], "id">; + executionTarget: Parameters[0]["target"]; + activeEnvironmentLease: Pick[0], "lease">; + isDotRun: boolean; + projectRepositoryPaths: string[]; + onCheckpoint?: Parameters[0]["onCheckpoint"]; + onPhase?: Parameters[0]["onPhase"]; + }; + config: { + runtimeConfig: Record; + runtimeSkillEntries: Parameters[0]["runtimeSkillEntries"]; + runScopedMentionedSkillKeys: string[]; + instructionCopy: Awaited["prepare"]>>; + agentIdentity: { keyId: string } | undefined; + issueAssigneeOverrides: { adapterConfig: Record | null } | null; + resolvedConfig: Record; + managedAiRuntime: Awaited> | undefined; + }; + recovery: { nativeLeaseOwner?: string; nativeRestartRecovery?: NativeRestartRecoveryClaim }; + trace: { providerTraceRequested: boolean; providerTraceCapture: { metadata: { id: string } } | null }; + issuesSvc: Pick, "addComment">; + runtimeEnv: Record; + beforeNativeRuntimeSelection?: (runId: string) => Promise; + onAdapterEvent: (event: AdapterRuntimeEvent) => Promise; + // Teardown must observe these updates even when a later preparation step throws. + onNativeLifecycleSelected: (telemetry: NativeLifecycleTelemetry) => void; + onProviderResourceDisposition: (disposition: ProviderResourceDisposition | undefined) => void; + onNativeOwnershipHeld: () => void; + stopControllerLease: () => void; +} + +export type HeartbeatRuntimeSelectionResult = { selected: false } | { + selected: true; + nativeExecution: NativeExecutionInput | null; + nativeRunnerInstanceId: string | null; + getNativeFreshSessionHandoff: (() => Promise) | undefined; + nativeWorkspaceSync: Awaited>; +}; + +export interface NativeSandboxLifecycle { + runnerProcess: "per_turn" | "warm"; + sandboxResource: "keep_running" | "stop_and_reuse" | "destroy_after_turn"; + failoverBackup: "verified"; +} + +export function resolveReusableSandboxLifecycle(input: { + lifecyclePolicy: + | { mode: "per_turn"; idleTimeoutMs: null } + | { mode: "warm"; idleTimeoutMs: number }; + target: { + kind: "local" | "remote"; + transport?: string; + reusableLeaseConfigured?: boolean; + effectiveCapabilities?: { reusableLeases: boolean } | null; + } | null; +}): NativeSandboxLifecycle | null { + if (input.target?.kind !== "remote" || input.target.transport !== "sandbox") { + return null; + } + const reusableLease = + input.target.reusableLeaseConfigured === true && + input.target.effectiveCapabilities?.reusableLeases === true; + if (input.lifecyclePolicy.mode === "warm" && !reusableLease) { + throw new Error("runner_warm_lifecycle_requires_reusable_provider_lease"); + } + return { + runnerProcess: input.lifecyclePolicy.mode, + sandboxResource: + input.lifecyclePolicy.mode === "warm" + ? "keep_running" + : reusableLease + ? "stop_and_reuse" + : "destroy_after_turn", + failoverBackup: "verified", + }; +} + +export function resolveNativeSandboxLifecycle(input: { + adapterType: string; + lifecyclePolicy: + | { mode: "per_turn"; idleTimeoutMs: null } + | { mode: "warm"; idleTimeoutMs: number }; + target: { + kind: "local" | "remote"; + transport?: string; + reusableLeaseConfigured?: boolean; + effectiveCapabilities?: { reusableLeases: boolean } | null; + } | null; +}): NativeSandboxLifecycle | null { + if ( + input.adapterType !== "paperclip_runner" || + input.target?.kind !== "remote" || + input.target.transport !== "sandbox" + ) + return null; + return resolveReusableSandboxLifecycle(input); +} + +export async function postNativeModelFallbackWarning(input: { + issuesSvc: Pick, "addComment">; + onEvent: (event: AdapterRuntimeEvent) => Promise; + issueId: string; + runId: string; + requestedModel: string | null; + effectiveModel: string | null; + codexCliVersion: string; +}): Promise { + const message = `Using ${input.effectiveModel} because the sandbox's Codex ${input.codexCliVersion} does not support ${input.requestedModel}. Work will continue with the compatible model. Update the sandbox's Codex CLI to use the requested model.`; + await input.onEvent({ + eventType: "runner.model_fallback", + stream: "system", + level: "warn", + message, + payload: { + requestedModel: input.requestedModel, + effectiveModel: input.effectiveModel, + codexCliVersion: input.codexCliVersion, + }, + }); + await input.issuesSvc.addComment(input.issueId, message, { runId: input.runId }, { + authorType: "system", + presentation: { + kind: "system_notice", + tone: "warning", + title: `Using ${input.effectiveModel}`, + density: "compact", + detailsDefaultOpen: false, + }, + }); +} + +function readNonEmptyString(value: unknown): string | null { + return typeof value === "string" && value.trim().length > 0 ? value : null; +} + +/** Prepare and persist the selected runtime before the executor dispatches provider work. */ +export async function selectHeartbeatRuntime(db: Db, input: HeartbeatRuntimeSelectionInput): Promise { + const { run, agent, nativeRuntimeResolution, issuesSvc, runtimeEnv, beforeNativeRuntimeSelection, onAdapterEvent, + onNativeLifecycleSelected, onProviderResourceDisposition, onNativeOwnershipHeld, stopControllerLease } = input; + const { issueRef, issueContext, context, executionContinuation, safeWakeComments, safeWakeCommentContext, + paperclipWakePayload, acceptedPlanContinuationWake } = input.task; + const { persistedNativeExecutionInput, persistedRunnerProfile, taskSessionCredentialCompatible, taskSessionDecodedParams, + taskSessionForRun, isFailedChatRunRetry, getFreshSessionHandoff } = input.session; + const { persistedExecutionWorkspace, executionWorkspace, executionTarget, activeEnvironmentLease, isDotRun, projectRepositoryPaths } = input.workspace; + const { runtimeConfig, runtimeSkillEntries, runScopedMentionedSkillKeys, instructionCopy, agentIdentity, issueAssigneeOverrides, resolvedConfig, managedAiRuntime } = input.config; + const runOptions = input.recovery; + const { providerTraceRequested, providerTraceCapture } = input.trace; + let nativeExecution: NativeExecutionInput | null = null; + let getNativeFreshSessionHandoff: (() => Promise) | undefined; + let nativeRunnerInstanceId: string | null = null; + let nativeWorkspaceSync: Awaited> = null; + let providerResourceDispositionForRun: ProviderResourceDisposition | undefined; + if (nativeRuntimeResolution.kind === "native") { + if (!issueRef) { + throw new Error("native_runtime_ineligible: issue is required"); + } + const nativeExecutionWorkspaceId = + persistedExecutionWorkspace?.id ?? run.id; + const nativeReviewContext = readNativeReviewAssignmentContext(context); + const nativeReview = nativeReviewContext ? await getNativeReviewAssignment(db, { + companyId: agent.companyId, issueId: issueRef.id, agentId: agent.id, + contextSnapshot: nativeReviewContext, + }) : null; + if (nativeReviewContext && !nativeReview) throw new Error("native_review_assignment_no_longer_available"); + const nativeReviewRequest = nativeReview + ? buildNativeReviewRequest({ + title: nativeReview.interaction.title, + summary: nativeReview.interaction.summary, + payload: nativeReview.interaction.payload, + }) + : null; + const persistedContract = run.completionContractId + ? await db + .select() + .from(completionContracts) + .where( + and( + eq(completionContracts.id, run.completionContractId), + eq(completionContracts.companyId, agent.companyId), + eq(completionContracts.issueId, issueRef.id), + ), + ) + .limit(1) + .then((rows) => rows[0] ?? null) + : null; + // Only a server-verified human resolution may supply a current answer + // reference. Tool/agent results and generated summaries stay evidence. + const currentHumanResponseId = !nativeReviewRequest + ? executionContinuation?.humanResponses?.find( + (response) => response.id === executionContinuation.trigger.interactionId, + )?.id + : undefined; + const immediateCompletion = (() => { + if (nativeReviewRequest) return { requests: [nativeReviewRequest], sources: [null] }; + const { requests, sources } = nativeCompletionRequestsWithSources( + safeWakeComments.length > 0 + ? safeWakeComments + : safeWakeCommentContext?.body + ? [safeWakeCommentContext] + : [], + { + requiredFullWakeCommentCount: + paperclipWakePayload?.fallbackFetchNeeded === true && + CHAT_PROVIDERS.some( + (provider) => + provider === + paperclipWakePayload.externalChatProvider, + ) && + Array.isArray(paperclipWakePayload.commentIds) + ? paperclipWakePayload.commentIds.length + : undefined, + }, + ); + // Preserve every admitted pending chat request while also + // retaining newer user direction materialized by recovery. + // A file-only wake must not inherit an old task objective. + const latestComment = + executionContinuation?.messages.findLast( + (message) => + message.authorType === "user" && + !message.createdByRunId && + !message.deleted && + message.body.trim().length > 0, + ); + const latestRequest = latestComment?.body; + if ( + latestRequest && + !requests.some( + (request) => request === latestRequest.trim(), + ) + ) { + requests.push(latestRequest.trim()); + sources.push(nativeCompletionSource("comment", latestComment!.id, latestRequest)); + } + return { requests: requests.length > 0 ? requests : undefined, sources }; + })(); + // Rebuilding a default contract is not a change in user direction. + // In particular, an upgraded checkpoint may have an intentionally + // authored contract and no continuation envelope yet. + const completionContract = + persistedContract && persistedNativeExecutionInput + ? { + row: persistedContract, + contract: persistedContract.contractJson as never, + } + : await ensureNativeCompletionContract({ + db, + companyId: agent.companyId, + issue: issueRef, + actorId: agent.id, + immediateRequest: + nativeReviewRequest ?? (currentHumanResponseId + ? null + : executionContinuation?.objective ?? safeWakeCommentContext?.body ?? null), + // The ordinary initial objective is selected by the server-owned + // continuation envelope. Carry its explicit description source + // through the singular-request compatibility path; do not infer + // provenance for review requests, answers, or wake fallbacks. + immediateRequestSource: nativeImmediateObjectiveSource({ + issueId: issueRef.id, + objectiveSource: executionContinuation?.objectiveSource, + excluded: Boolean(nativeReviewRequest || currentHumanResponseId), + }), + humanResponseId: currentHumanResponseId, + immediateRequests: immediateCompletion.requests, + immediateRequestSources: immediateCompletion.sources, + }); + const taskNativeSessionId = !taskSessionCredentialCompatible ? null : readNonEmptyString( + taskSessionDecodedParams?.sessionId, + ); + // Compatibility for native retry rows created before same-run restart + // recovery existed. Only an entirely unused replacement row may + // inherit its source checkpoint; any process/provider evidence on the + // replacement makes the ownership ambiguous and therefore ineligible. + const legacyRetrySource = + run.retryOfRunId && !isFailedChatRunRetry + ? await db + .select({ + id: heartbeatRuns.id, + companyId: heartbeatRuns.companyId, + agentId: heartbeatRuns.agentId, + runnerInstanceId: heartbeatRuns.runnerInstanceId, + nativeSessionId: heartbeatRuns.nativeSessionId, + runnerProfileJson: heartbeatRuns.runnerProfileJson, + runtimeMode: heartbeatRuns.runtimeMode, + status: heartbeatRuns.status, + }) + .from(heartbeatRuns) + .where( + and( + eq(heartbeatRuns.id, run.retryOfRunId), + eq(heartbeatRuns.companyId, agent.companyId), + eq(heartbeatRuns.agentId, agent.id), + ), + ) + .limit(1) + .then((rows) => rows[0] ?? null) + : null; + const nativeBootstrapHasProviderEvidence = + legacyRetrySource || + run.nativeSessionId || + persistedNativeExecutionInput + ? await db + .select({ id: heartbeatRunEvents.id }) + .from(heartbeatRunEvents) + .where( + and( + eq(heartbeatRunEvents.runId, run.id), + inArray(heartbeatRunEvents.eventType, [ + "harness.ready", + "session.started", + "session.resumed", + "session.updated", + "turn.started", + "provider.event", + "provider.rpc_result", + ]), + ), + ) + .limit(1) + .then((rows) => rows.length > 0) + : false; + const compatibleLegacyRetrySource = + !managedAiRuntime && !isConversation(issueContext) && context.forceFreshSession !== true && isUnusedLegacyNativeRetryReplacement({ + replacement: run, + source: legacyRetrySource, + hasProviderEvents: nativeBootstrapHasProviderEvidence, + }) + ? legacyRetrySource + : null; + const legacyRetrySessionId = + compatibleLegacyRetrySource?.nativeSessionId; + const taskResumeRunId = + taskSessionForRun?.lastRunId && + taskSessionForRun.lastRunId !== run.id && + isNativeSessionId(taskNativeSessionId) + ? taskSessionForRun.lastRunId + : null; + const resumableTaskSessionId = isDotRun ? null : taskResumeRunId + ? taskNativeSessionId + : (legacyRetrySessionId ?? null); + const requestedNativeSessionId = + run.nativeSessionId ?? resumableTaskSessionId; + // A task-session lastRunId can lag a failed turn or point at an older + // normalized session. Find the newest exact-session authority instead. + // Rows that already acquired provider authority are progress barriers, + // even when they do not contain a usable checkpoint. + const previousNativeRun = + requestedNativeSessionId && + isUnusedNativeSessionBootstrap( + run, + nativeBootstrapHasProviderEvidence, + ) + ? await findNativeSessionResumeRun(db, { + companyId: agent.companyId, + agentId: agent.id, + issueId: issueRef.id, + normalizedSessionId: requestedNativeSessionId, + currentRunId: run.id, + beforeCreatedAt: run.createdAt, + }) + : null; + nativeRunnerInstanceId = + previousNativeRun?.runnerInstanceId && + previousNativeRun.nativeSessionId === + (run.nativeSessionId ?? resumableTaskSessionId) + ? previousNativeRun.runnerInstanceId + : (run.runnerInstanceId ?? randomUUID()); + let nativeSessionId = + run.nativeSessionId ?? resumableTaskSessionId ?? randomUUID(); + let nativeResumeCheckpoint: ReturnType< + typeof rebindNativeSessionCheckpoint + > = null; + const agentLifecyclePolicy = + parseObject(agent.adapterConfig).lifecycleMode === "warm" + ? { + mode: "warm" as const, + idleTimeoutMs: resolvePaperclipRunnerIdleTimeoutMs( + parseObject(agent.adapterConfig).idleTimeoutMs, + ), + } + : { mode: "per_turn" as const, idleTimeoutMs: null }; + const environmentLifecyclePolicy = + executionTarget?.kind === "remote" && + executionTarget.transport === "sandbox" + ? (executionTarget.runnerLifecyclePolicy ?? null) + : null; + // Native Codex owns a durable, session-scoped home. It flushes refreshed + // auth into each invocation's private home before that home is removed. + // Other managed harnesses still require per-turn credential cleanup. + const supportsManagedWarmSession = agent.adapterType === "paperclip_runner" && + nativeRuntimeResolution.profile.backend === "codex_app_server"; + const effectiveLifecyclePolicy = persistedNativeExecutionInput?.session.lifecyclePolicy ?? + (nativeRuntimeResolution.profile.backend === "openai_dot_mcp" || managedAiRuntime && !supportsManagedWarmSession + ? { mode: "per_turn" as const, idleTimeoutMs: null } + : environmentLifecyclePolicy ?? agentLifecyclePolicy); + if ( + effectiveLifecyclePolicy.mode === "warm" && + executionTarget?.kind === "remote" && + executionTarget.transport === "sandbox" && + (executionTarget.reusableLeaseConfigured !== true || + executionTarget.effectiveCapabilities?.reusableLeases !== true) + ) { + throw new Error("runner_warm_environment_requires_reusable_lease"); + } + const persistedProfile = persistedRunnerProfile; + if (persistedNativeExecutionInput) { + nativeExecution = persistedNativeExecutionInput; + if ( + nativeExecution.binding.companyId !== agent.companyId || + nativeExecution.binding.runId !== run.id || + nativeExecution.binding.issueId !== issueRef.id || + nativeExecution.binding.agentId !== agent.id || + nativeExecution.binding.executionWorkspaceId !== + nativeExecutionWorkspaceId || + nativeExecution.completionContract.id !== + completionContract.row.id || + nativeExecution.completionContract.sha256 !== + completionContract.row.canonicalSha256 + ) + throw new Error( + "native_execution_input_persisted_binding_mismatch", + ); + // Recover only the originally admitted request. A stored idle + // checkpoint can precede an already-started provider turn, so even + // apparent idleness is not authority to rewrite its contract. + // Newer user direction retains its separate durable wakeup cause. + // A failed pre-bootstrap attempt may have persisted its immutable + // input before discovering that lastRunId no longer names this + // session. Restore only an exactly compatible prior checkpoint; + // never rewrite the admitted input or skip current provider work. + if ( + previousNativeRun && + isUnusedNativeSessionBootstrap( + run, + nativeBootstrapHasProviderEvidence, + ) + ) { + nativeResumeCheckpoint = rebindNativeSessionCheckpoint({ + previousRun: previousNativeRun, + currentExecution: nativeExecution, + executionTargetKind: executionTarget?.kind ?? "local", + }); + } + if (nativeExecution.provider.kind === "claude_managed") { + const recoveryProfile = await managedAgentProfileService( + db, + ).requireQualified( + agent.companyId, + nativeExecution.provider.managedProfile.profileId, + ); + assertManagedProfileRecoveryBinding({ + adapterConfig: agent.adapterConfig, + snapshot: nativeExecution.provider.managedProfile, + stored: recoveryProfile, + }); + } + if (nativeExecution.provider.kind === "aws_agentcore") { + const recoveryProfile = await remoteAgentProfileService( + db, + ).requireQualified( + agent.companyId, + nativeExecution.provider.agentCoreProfile.profileId, + "aws_bedrock_agentcore_harness", + ); + assertAgentCoreProfileRecoveryBinding({ + snapshot: nativeExecution.provider.agentCoreProfile, + stored: recoveryProfile, + }); + } + } else { + const interactionId = readNonEmptyString(context.interactionId); + const interactionResponses = context[ + EXTERNAL_CHAT_QUESTION_RESPONSE_KEY + ] + ? await materializeExternalChatQuestionResponseInput({ + db, + binding: { + companyId: agent.companyId, + issueId: issueRef.id, + runId: run.id, + agentId: agent.id, + }, + contextSnapshot: context, + }) + : await materializeNativeInteractionResponses({ + db, + companyId: agent.companyId, + issueId: issueRef.id, + runId: run.id, + agentId: agent.id, + interactionIds: Array.isArray(context.interactionIds) + ? [ + ...new Set([ + ...(interactionId ? [interactionId] : []), + ...context.interactionIds.filter( + (id): id is string => typeof id === "string", + ), + ]), + ] + : interactionId + ? [interactionId] + : [], + }); + const runnerAdapterConfig = parseObject(agent.adapterConfig); + const managedProfile = + nativeRuntimeResolution.profile.backend === + "claude_managed_agents_api" + ? await managedAgentProfileService(db).requireQualified( + agent.companyId, + readNonEmptyString(runnerAdapterConfig.managedProfileId) ?? + "", + ) + : null; + const agentCoreProfile = + nativeRuntimeResolution.profile.backend === + "aws_agentcore_harness_api" + ? await remoteAgentProfileService(db).requireQualified( + agent.companyId, + readNonEmptyString( + runnerAdapterConfig.agentCoreProfileId, + ) ?? "", + "aws_bedrock_agentcore_harness", + ) + : null; + if (managedProfile) { + const rawApiKeyBinding = parseObject( + runnerAdapterConfig.env, + ).ANTHROPIC_API_KEY; + const boundSecretId = + typeof rawApiKeyBinding === "object" && + rawApiKeyBinding !== null + ? readNonEmptyString( + (rawApiKeyBinding as Record).secretId, + ) + : null; + if (boundSecretId !== managedProfile.apiKeySecretId) { + throw new ConfigurationIncompleteFailure( + "configuration incomplete: Claude Managed profile API key is not bound at env.ANTHROPIC_API_KEY", + { + configurationIncomplete: { + reason: "managed_agent_profile_secret_binding_mismatch", + companyId: agent.companyId, + agentId: agent.id, + profileId: managedProfile.id, + requiredEnvKeys: ["ANTHROPIC_API_KEY"], + }, + }, + ); + } + } + const executionMode = + issueRef.workMode === "planning" && !isConversation(issueContext) && !acceptedPlanContinuationWake + ? ("plan" as const) + : ("default" as const); + const pinnedPlan = + executionMode === "plan" + ? await documentService(db).getIssueDocumentByKey( + issueRef.id, + "plan", + ) + : null; + const pinnedReviewContext = + executionMode === "plan" + ? await buildPlanReviewContext({ + db, + companyId: agent.companyId, + issueId: issueRef.id, + issueWorkMode: issueRef.workMode, + interactionId: readNonEmptyString(context.interactionId), + }) + : null; + const pinnedPlanMarkdown = pinnedPlan?.body ?? ""; + const dotBinding = nativeRuntimeResolution.profile.backend === "openai_dot_mcp" + ? await dotRunnerBroker(db).snapshot(agent.companyId, agent.id, String(parseObject(agent.adapterConfig).dotBindingId ?? "")) : undefined; + const nativeRuntimeContext = await buildNativeRuntimeContext({ + db, + agent, + runId: run.id, + runtimeConfig, + runtimeSkillEntries, + instructionWorkingCopy: instructionCopy ? { rootPath: instructionCopy.executionRoot, entryPath: instructionCopy.entryFile, ...(isAgentDirectoryCopy(instructionCopy) ? { kind: "agent_files" as const } : {}) } : undefined, + }); + getNativeFreshSessionHandoff = nativeReviewRequest ? undefined : getFreshSessionHandoff; + const nativeProviderConfig = nativeRuntimeResolution.profile.backend === "codex_app_server" + || nativeRuntimeResolution.profile.backend === "opencode_server" + ? projectPaperclipRunnerTaskConfig( + nativeRuntimeResolution.profile.backend, + agent.adapterConfig, + issueAssigneeOverrides?.adapterConfig, + managedAiRuntime ? readNonEmptyString(resolvedConfig.model) ?? undefined : undefined, + ) + : agent.adapterConfig; + const requestedNativeProvider = resolvePaperclipRunnerNativeProviderInput({ + backend: nativeRuntimeResolution.profile.backend, + adapterConfig: nativeProviderConfig, managedProfile, agentCoreProfile, dotBinding, + }); + const codexCliVersion = agent.adapterType === "paperclip_runner" && requestedNativeProvider.provider === "codex" + ? await readRemoteCodexModelCliVersion({ + model: requestedNativeProvider.model, + target: executionTarget, + remoteCodexPath: runtimeEnv.PAPERCLIP_RUNNER_REMOTE_CODEX_PATH, + remoteCodexNpmSpec: runtimeEnv.PAPERCLIP_RUNNER_REMOTE_CODEX_NPM_SPEC, + }) : null; + const nativeProvider = codexCliVersion + ? resolvePaperclipRunnerNativeProviderInput({ + backend: nativeRuntimeResolution.profile.backend, + adapterConfig: nativeProviderConfig, codexCliVersion, managedProfile, agentCoreProfile, dotBinding, + }) : requestedNativeProvider; + if (nativeProvider.model !== requestedNativeProvider.model) { + await postNativeModelFallbackWarning({ + issuesSvc, onEvent: onAdapterEvent, issueId: issueRef.id, runId: run.id, + requestedModel: requestedNativeProvider.model, effectiveModel: nativeProvider.model, + codexCliVersion: codexCliVersion!, + }); + } + const buildExecution = ({ normalizedSessionId, resumedSession }: { normalizedSessionId: string; resumedSession: boolean }) => + buildNativeExecutionInput({ + agentKeyId: agentIdentity?.keyId, + companyId: agent.companyId, + runId: run.id, + issue: nativeReviewRequest ? { ...issueRef, title: `Review: ${issueRef.title}`, description: nativeReviewRequest } : issueRef, + taskPrompt: [ + nativeReviewRequest ?? readNonEmptyString( + selectPaperclipTaskMarkdown(context, { + resumedSession: false, + includeCommunicationGuidance: false, + }), + ) ?? + `# ${issueRef.identifier ?? issueRef.id}: ${issueRef.title}`, + nativeRuntimeResolution.profile.backend !== "openai_dot_mcp" && projectRepositoryPaths.length > 0 + ? `## Project repositories\nThe task workspace also contains these editable Git repositories:\n${projectRepositoryPaths.map((repo) => `- ${repo}`).join("\n")}` + : null, + ].filter(Boolean).join("\n\n"), + initialCommunicationGuidance: nativeReviewRequest ? null : readNonEmptyString(context.paperclipTaskCommunicationGuidance), + wakePayload: context.paperclipWake, + turnContext: context.paperclipTurnContext, + githubInstructionSkillKeys: runScopedMentionedSkillKeys, + resumedSession, + previousTurn: (() => { + if (!previousNativeRun || nativeReviewRequest) return null; + try { + const previousTask = parseNativeExecutionInput(parseObject(previousNativeRun.runnerProfileJson).nativeExecutionInput).task; + if (paperclipWakePayload?.externalChatProvider) { + // External native inputs use a neutral task title. Compare + // the saved canonical brief so old provider text is not + // repeated as a change, while genuine edits still arrive. + const savedIssue = parseObject(parseObject(previousNativeRun.contextSnapshot).paperclipIssue); + if (savedIssue.id !== issueRef.id || typeof savedIssue.title !== "string" || + (savedIssue.description !== null && typeof savedIssue.description !== "string")) return null; + return { runId: previousNativeRun.id, task: { title: savedIssue.title, description: savedIssue.description } }; + } + return { runId: previousNativeRun.id, task: previousTask }; + } catch { + // An invalid prior snapshot must use the fresh bootstrap. + return null; + } + })(), + conversationMode: context.conversationMode === true, + agentId: agent.id, + workspace: { + // Projectless paperclip_runner tasks still have a resolved local cwd. Bind that + // transient workspace to the run id so the native input remains durable and replayable + // without fabricating a project-scoped execution_workspaces row. + id: nativeExecutionWorkspaceId, + cwd: executionWorkspace.cwd, + repoUrl: executionWorkspace.repoUrl, + repoRef: executionWorkspace.repoRef, + branchName: executionWorkspace.branchName, + }, + normalizedSessionId, + executionMode, + planningContext: + executionMode === "plan" + ? { + documentId: pinnedPlan?.id ?? null, + baseRevisionId: + pinnedPlan?.latestRevisionId ?? null, + baseRevisionNumber: + pinnedPlan?.latestRevisionNumber ?? 0, + markdown: pinnedPlanMarkdown, + sha256: createHash("sha256") + .update(pinnedPlanMarkdown) + .digest("hex"), + reviewContext: pinnedReviewContext + ? (structuredClone( + pinnedReviewContext, + ) as unknown as Record) + : {}, + } + : null, + ...nativeProvider, + lifecyclePolicy: effectiveLifecyclePolicy, + interactionResponses, + completionContract: { + id: completionContract.row.id, + sha256: completionContract.row.canonicalSha256, + schemaVersion: completionContract.row.schemaVersion, + contract: completionContract.contract, + sources: "sources" in completionContract ? completionContract.sources : undefined, + }, + runtimeContext: nativeRuntimeContext, + }); + const backendDescriptor = await describeRunnerdNativeSessionBackend(buildExecution({ + normalizedSessionId: nativeSessionId, resumedSession: previousNativeRun !== null, + })); + const nativeExecutionWithCheckpoint = buildNativeExecutionWithCheckpoint({ + previousRun: previousNativeRun, + normalizedSessionId: nativeSessionId, + executionTargetKind: executionTarget?.kind ?? "local", + toolRefreshOnResume: backendDescriptor.capabilities.toolRefreshOnResume === true, + refreshTools: context.refreshTools === true, + buildExecution, + }); + nativeExecution = nativeExecutionWithCheckpoint.execution; + nativeResumeCheckpoint = nativeExecutionWithCheckpoint.checkpoint; + if ( + nativeSessionId !== + nativeExecutionWithCheckpoint.normalizedSessionId + ) { + nativeRunnerInstanceId = randomUUID(); + } + nativeSessionId = nativeExecutionWithCheckpoint.normalizedSessionId; + } + const nativeSandboxLifecycle = resolveNativeSandboxLifecycle({ + adapterType: agent.adapterType, + lifecyclePolicy: nativeExecution.session.lifecyclePolicy, + target: executionTarget, + }); + if (nativeSandboxLifecycle) { + onNativeLifecycleSelected({ + provider: nativeExecution.provider.kind, + harness: nativeExecution.session.driverKind, + lifecycleMode: nativeExecution.session.lifecyclePolicy.mode, + sandboxResource: nativeSandboxLifecycle.sandboxResource, + }); + const selectedLifecycleSpan = getStartupTracer( + "paperclip.environment-lifecycle", + ).startSpan("sandbox.lifecycle.selected", { + attributes: { + "paperclip.native.span.provider": nativeExecution.provider.kind, + "paperclip.native.span.harness": + nativeExecution.session.driverKind, + "paperclip.native.span.lifecycle_mode": + nativeExecution.session.lifecyclePolicy.mode, + "paperclip.native.span.sandbox_resource": + nativeSandboxLifecycle.sandboxResource, + "paperclip.native.span.outcome": "selected", + "paperclip.native.span.bytes_transferred": 0, + }, + }); + selectedLifecycleSpan.end(); + } + providerResourceDispositionForRun = + nativeSandboxLifecycle?.sandboxResource === "keep_running" + ? "keep_running" + : nativeSandboxLifecycle?.sandboxResource === "stop_and_reuse" + ? "stop_and_retain" + : nativeSandboxLifecycle?.sandboxResource === + "destroy_after_turn" + ? "destroy" + : undefined; + onProviderResourceDisposition(providerResourceDispositionForRun); + await beforeNativeRuntimeSelection?.(run.id); + const nativeSelected = await db.transaction(async (tx) => { + const lockedRun = await tx + .select() + .from(heartbeatRuns) + .where(eq(heartbeatRuns.id, run.id)) + .for("update") + .limit(1) + .then((rows) => rows[0] ?? null); + if (!lockedRun) throw new Error("native_runtime_run_missing"); + // Cancellation and runtime selection serialize on this row. A + // stopped preparation must never create a new native coordinator. + if (lockedRun.status !== "running" || lockedRun.resultJson?.startupCancellation) return false; + if (lockedRun.runtimeMode === "legacy" && lockedRun.controllerBootId && + !(await renewLegacyControllerLease(tx as unknown as Db, lockedRun))) { + onNativeOwnershipHeld(); + return false; + } + if ( + lockedRun.runtimeModeResolvedAt && + lockedRun.runtimeMode !== "native" + ) { + throw new Error("native_runtime_mode_conflict"); + } + const lockedProfile = parseObject(lockedRun.runnerProfileJson); + const persistedNativeSessionId = + await prepareNativeSessionBootstrapPersistence(tx, { + run: lockedRun, + selectedSessionId: nativeSessionId, + execution: nativeExecution!, + restoringCheckpoint: nativeResumeCheckpoint !== null, + }); + await tx + .update(heartbeatRuns) + .set({ + runtimeMode: "native", + runtimeModeResolverVersion: + lockedRun.runtimeModeResolverVersion ?? + nativeRuntimeResolution.resolverVersion, + runtimeModeReason: + lockedRun.runtimeModeReason ?? nativeRuntimeResolution.reason, + runtimeModeResolvedAt: + lockedRun.runtimeModeResolvedAt ?? new Date(), + runnerProfileJson: { + ...nativeRuntimeResolution.profile, + ...lockedProfile, + ...(lockedProfile.nativeExecutionInput + ? {} + : { recoveryEventInventoryVersion: 1 }), + ...(providerTraceRequested + ? { + providerTrace: { + mode: "raw", + traceId: providerTraceCapture?.metadata.id ?? null, + maxBytes: PROVIDER_TRACE_MAX_BYTES, + }, + } + : {}), + nativeExecutionInput: + lockedProfile.nativeExecutionInput ?? nativeExecution, + nativeToolContractFingerprint: + nativeToolContractFingerprintForTarget( + executionTarget?.kind ?? "local", + ), + ...(lockedProfile.sessionCheckpoint != null + ? { sessionCheckpoint: lockedProfile.sessionCheckpoint } + : nativeResumeCheckpoint + ? { + sessionCheckpoint: + nativeResumeCheckpoint as unknown as Record< + string, + unknown + >, + } + : {}), + }, + runnerInstanceId: + previousNativeRun?.runnerInstanceId && + persistedNativeSessionId === previousNativeRun.nativeSessionId + ? previousNativeRun.runnerInstanceId + : lockedRun.nativeSessionId !== persistedNativeSessionId + ? nativeRunnerInstanceId + : (lockedRun.runnerInstanceId ?? nativeRunnerInstanceId), + nativeSessionId: persistedNativeSessionId, + processPid: + lockedRun.processPid ?? + (previousNativeRun?.nativeSessionId === nativeSessionId + ? previousNativeRun.processPid + : null), + processGroupId: + lockedRun.processGroupId ?? + (previousNativeRun?.nativeSessionId === nativeSessionId + ? previousNativeRun.processGroupId + : null), + processStartedAt: + lockedRun.processStartedAt ?? + (previousNativeRun?.nativeSessionId === nativeSessionId + ? previousNativeRun.processStartedAt + : null), + nativeIssueId: lockedRun.nativeIssueId ?? issueRef.id, + driverKind: + lockedRun.driverKind ?? + nativeExecution?.session.driverKind ?? + "codex_app_server", + driverVersion: lockedRun.driverVersion ?? "phase6-v1", + completionContractId: + lockedRun.completionContractId ?? completionContract.row.id, + completionContractSha256: + lockedRun.completionContractSha256 ?? + completionContract.row.canonicalSha256, + nativePhase: lockedRun.nativePhase ?? "observed", + nativePhaseUpdatedAt: + lockedRun.nativePhaseUpdatedAt ?? new Date(), + updatedAt: new Date(), + }) + .where(eq(heartbeatRuns.id, run.id)); + await tx + .insert(nativeRunFinalizations) + .values({ + runId: run.id, + companyId: agent.companyId, + issueId: issueRef.id, + phase: "observed", + }) + .onConflictDoNothing(); + return true; + }); + if (!nativeSelected) return { selected: false }; + stopControllerLease(); + nativeWorkspaceSync = await prepareNativeWorkspaceSync({ + db, + runId: run.id, + companyId: agent.companyId, + workspaceId: nativeExecutionWorkspaceId, + workspaceLocalDir: executionWorkspace.cwd, + target: executionTarget, + lease: activeEnvironmentLease.lease, + restartRecovery: runOptions.nativeRestartRecovery, + sameRunRecovery: Boolean(runOptions.nativeLeaseOwner), + resourceDisposition: providerResourceDispositionForRun, + onCheckpoint: input.workspace.onCheckpoint, + onPhase: input.workspace.onPhase, + }); + } else { + const legacyWarmLifecycle = + executionTarget?.kind === "remote" && + executionTarget.transport === "sandbox" && + executionTarget.runnerLifecyclePolicy?.mode === "warm" + ? resolveReusableSandboxLifecycle({ + lifecyclePolicy: executionTarget.runnerLifecyclePolicy, + target: executionTarget, + }) + : null; + if (legacyWarmLifecycle?.sandboxResource === "keep_running") { + providerResourceDispositionForRun = "keep_running"; + onProviderResourceDisposition(providerResourceDispositionForRun); + } + await db + .update(heartbeatRuns) + .set({ + runtimeMode: "legacy", + runtimeModeResolverVersion: + nativeRuntimeResolution.resolverVersion, + runtimeModeReason: nativeRuntimeResolution.reason, + runtimeModeResolvedAt: run.runtimeModeResolvedAt ?? new Date(), + // Preserve server-owned admission and dispatch evidence on this + // row; never copy another run's execution profile. + runnerProfileJson: sql`(case when ${heartbeatRuns.runnerProfileJson} ? ${CHAT_CONTROL_RECOVERY_ADMISSION_KEY} + then jsonb_build_object(${CHAT_CONTROL_RECOVERY_ADMISSION_KEY}::text, ${heartbeatRuns.runnerProfileJson}->${CHAT_CONTROL_RECOVERY_ADMISSION_KEY}) + else '{}'::jsonb end) + || (case when ${heartbeatRuns.runnerProfileJson} ? 'adapterDispatch' + then jsonb_build_object('adapterDispatch', ${heartbeatRuns.runnerProfileJson}->'adapterDispatch') + else '{}'::jsonb end) || ${JSON.stringify(providerTraceRequested ? { providerTrace: { mode: "raw", traceId: providerTraceCapture?.metadata.id ?? null, maxBytes: PROVIDER_TRACE_MAX_BYTES } } : {})}::jsonb`, + updatedAt: new Date(), + }) + .where(eq(heartbeatRuns.id, run.id)); + } + return { selected: true, nativeExecution, nativeRunnerInstanceId, getNativeFreshSessionHandoff, nativeWorkspaceSync }; +} diff --git a/server/src/services/native-runtime/native-session-resume.test.ts b/server/src/services/native-runtime/native-session-resume.test.ts index fedc1628476..d486db139f4 100644 --- a/server/src/services/native-runtime/native-session-resume.test.ts +++ b/server/src/services/native-runtime/native-session-resume.test.ts @@ -461,7 +461,7 @@ describe("capability-gated connection tool refresh", () => { it("wires exact-session recovery and guarded selected identity into heartbeat persistence", () => { const source = readFileSync( - new URL("../heartbeat.ts", import.meta.url), + new URL("../heartbeat/runtime-selection.ts", import.meta.url), "utf8", ); expect(source).toContain("await findNativeSessionResumeRun(db,");