diff --git a/app/api/agent/[id]/route.ts b/app/api/agent/[id]/route.ts index 892d71293..c2e864616 100644 --- a/app/api/agent/[id]/route.ts +++ b/app/api/agent/[id]/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { readSessionHeader } from "@/lib/session-reader"; import { apiErrorResponse, resolveSessionPathOr404 } from "@/lib/api-utils"; -import { startRpcSession, getRpcSession, resolveSpawnCwdResult, WebRpcError } from "@/lib/rpc-manager"; +import { startRpcSession, getRpcSession, launchConfigForCwd, resolveSpawnCwdResult, WebRpcError } from "@/lib/rpc-manager"; import { RpcCommandError } from "@/lib/omp/rpc-process"; import { parseJsonWithinLimit, RequestBodyTooLargeError } from "@/lib/bounded-form-data"; import { MAX_AGENT_COMMAND_REQUEST_BYTES } from "@/lib/image-attachments"; @@ -69,7 +69,12 @@ export async function POST( const noSpawn = Object.hasOwn(NO_SPAWN_REPLIES, body.type); const liveOnly = LIVE_SESSION_ONLY.has(body.type); if (existing?.isAlive()) { - if (noSpawn || liveOnly || existing.advisorSpawned === advisor || existing.isRunning() || existing.isEditingHistory()) { + // Apply the same effective-advisor rule as startRpcSession: a + // workspace-forced advisor child must not be compared against the raw + // per-chat toggle, or every toggle-off prompt would destroy+respawn an + // idle child for no change (the replacement spawns with the same flag). + const effectiveAdvisor = advisor || launchConfigForCwd(existing.cwd)?.advisor === true; + if (noSpawn || liveOnly || existing.advisorSpawned === effectiveAdvisor || existing.isRunning() || existing.isEditingHistory()) { const result = await existing.send(body); return NextResponse.json({ success: true, data: result }); } diff --git a/lib/rpc-manager.test.mjs b/lib/rpc-manager.test.mjs index a153e829d..dda9f80ea 100644 --- a/lib/rpc-manager.test.mjs +++ b/lib/rpc-manager.test.mjs @@ -882,6 +882,24 @@ test("replace gate compares the union of toggle and workspace advisor", async () assert.match(source, /existing\.advisorSpawned === effectiveAdvisor/); }); +test("agent route fast path compares the effective advisor flag", async () => { + const source = await readFile(new URL("../app/api/agent/[id]/route.ts", import.meta.url), "utf8"); + // The route previously compared the live child's flag against the raw + // ?advisor= query, so a workspace-forced advisor child was destroyed and + // respawned on every idle toggle-off POST even though the replacement + // spawned with the same effective flag. + assert.match(source, /const effectiveAdvisor = advisor \|\| launchConfigForCwd\(existing\.cwd\)\?\.advisor === true;/); + assert.match(source, /existing\.advisorSpawned === effectiveAdvisor/); +}); + +test("live-owner scan is not skipped by a dead keyed wrapper", async () => { + const source = await readFile(new URL("./rpc-manager.ts", import.meta.url), "utf8"); + // Skipping the scan whenever `existing` was truthy let startRpcSession + // spawn a second --resume child for a file a live wrapper already owned + // (under another key) once the keyed wrapper had died. + assert.match(source, /if \(sessionFile && \(!existing \|\| !existing\.isAlive\(\)\)\) \{/); +}); + test("fresh spawns force a new session when omp resumes the cwd's latest session", async () => { const source = await readFile(new URL("./rpc-manager.ts", import.meta.url), "utf8"); // startRpcSession: a bare spawn (no --resume) must never let omp's startup diff --git a/lib/rpc-manager.ts b/lib/rpc-manager.ts index f8e23cee8..3b1f1df5a 100644 --- a/lib/rpc-manager.ts +++ b/lib/rpc-manager.ts @@ -2428,8 +2428,8 @@ export async function resumeInterruptedSessions(): Promise { })); } -/** Look up the workspace-registered launch config; unregistered projects attach none. */ -function launchConfigForCwd(cwd: string): ProjectLaunchConfig | undefined { +/** Look up the workspace-registered launch config; unregistered projects attach none. Exported so the agent route's fast-path gate can apply the same effective-advisor rule as startRpcSession. */ +export function launchConfigForCwd(cwd: string): ProjectLaunchConfig | undefined { let canonical = cwd; try { canonical = realpathSync(cwd); } catch {} const key = comparableProjectPath(canonical); @@ -2587,7 +2587,10 @@ export async function startRpcSession( // omp >= 18.5 lets only one process own a session file; a second `--resume` // child would fork into a new nested session on its first write. Reuse the // live wrapper that already reports this file (e.g. after it moved files). - if (sessionFile && !existing) { + // A dead keyed wrapper must not skip this scan: it owns nothing anymore, + // and a live wrapper under another key may already own this file. Spawning + // a second --resume child would fork into a new nested session on first write. + if (sessionFile && (!existing || !existing.isAlive())) { for (const candidate of new Set(registry.values())) { if (candidate.isAlive() && candidate.sessionFile && samePath(candidate.sessionFile, sessionFile)) { registry.set(sessionId, candidate);