diff --git a/src/tests/v1/acp-agent.test.ts b/src/tests/v1/acp-agent.test.ts index d0cb4856..a5a7f34d 100644 --- a/src/tests/v1/acp-agent.test.ts +++ b/src/tests/v1/acp-agent.test.ts @@ -28392,6 +28392,64 @@ describe("turn steering (_session/steering)", () => { expect(agent.sessions["test-session"].owedTrailingIdles).toBe(0); }); + // The same guard when the autonomous cycle's result is stamped with the + // original prompt: answersPendingPrompt then routes it to the user lane, and + // its idle must still not settle the turn while the steered work runs. + it.each(["end_turn", "refusal"] as const)( + "does not settle a steered turn on an autonomous %s result naming the original prompt", + async (stopReason) => { + const timeline: string[] = []; + const agent = new ClaudeAcpAgent(timelineClient(timeline), { + log: () => {}, + error: () => {}, + }); + + injectGeneratorSession(agent, (input) => { + async function* messageGenerator() { + const iter = input[Symbol.asyncIterator](); + const u1 = await iter.next(); + yield userEcho(u1.value); + yield createAssistantText("working on it"); + const steered = await iter.next(); + yield { ...interruptedCycleResult(), user_message_uuid: u1.value.uuid }; + yield userEcho(steered.value); + // A background cycle's result, stamped with the original prompt only. + yield { + ...createResultMessage(), + stop_reason: stopReason, + origin: { kind: "task-notification" }, + user_message_uuid: u1.value.uuid, + user_message_uuids: [u1.value.uuid], + }; + yield idleMessage(); // that cycle's trailer + yield createAssistantText("STEERED-OK"); + yield createResultMessage(); + yield idleMessage(); + } + return messageGenerator(); + }); + + const turn = agent + .prompt({ sessionId: "test-session", prompt: [{ type: "text", text: "start" }] }) + .then((response) => { + timeline.push(`prompt:${response.stopReason}`); + return response; + }); + await waitFor(() => !!agent.sessions["test-session"]?.activeTurn); + await agent.steer({ + sessionId: "test-session", + prompt: [{ type: "text", text: "also handle X" }], + }); + + await turn; + await agent.sessions["test-session"]?.consumer; + + // The turn keeps one stop reason across its cycles, so a refusal trailer's + // reason is reported; what matters here is that the prompt ends last. + expect(timeline).toEqual(["working on it", "STEERED-OK", `prompt:${stopReason}`]); + }, + ); + it("always injects at 'now' priority, ignoring any client-supplied priority", async () => { const agent = createMockAgent(); const captured: any[] = []; diff --git a/src/v1/acp-agent.ts b/src/v1/acp-agent.ts index af0e48f9..e7bd3cbf 100644 --- a/src/v1/acp-agent.ts +++ b/src/v1/acp-agent.ts @@ -797,7 +797,8 @@ type Turn = { * `steeredSettle`. `steeredSettle` can already hold the interrupted cycle's * outcome (or a held turn's, moved over by `steer()`) when the echo lands, so * while this is set the recorded outcome predates the steered work and an - * idle must not settle on it. */ + * idle must not settle on it. A result of an autonomous cycle that names + * none of the turn's steers is not steered work, so it does not clear it. */ steeredAwaitingResult?: boolean; /** What a steered turn settles with once its steered work has run: the outcome * of its latest result, so its usage covers every cycle the turn ran. */ @@ -3907,14 +3908,19 @@ export class ClaudeAcpAgent { * calling settleActive directly bypasses the hold and re-opens the * out-of-turn permission deadlock (issue #866) through its lane. For a * client with `backgroundSubagents` this always settles now. */ - const settleOrDefer = async (outcome: TurnOutcome) => { + const settleOrDefer = async ( + outcome: TurnOutcome, + { steeredWork = true }: { steeredWork?: boolean } = {}, + ) => { // No result ends a steered turn: the steer aborted the cycle this result // may belong to, and the steered one is still to come. Record the outcome // for the idle lane (see Turn.steeredEchoes); later cycles overwrite it, // so the last result and its usage win. if (isSteering(session.activeTurn)) { session.activeTurn.steeredSettle = outcome; - session.activeTurn.steeredAwaitingResult = false; + // Only a result of the steered work ends the wait for it (see + // Turn.steeredAwaitingResult). + if (steeredWork) session.activeTurn.steeredAwaitingResult = false; return; } if ( @@ -5612,6 +5618,10 @@ export class ClaudeAcpAgent { // commands whose shared or late result this is, even when the // result is the ACTIVE turn's (ensureActiveTurn never looks at // the map in that case). + // Whether this result names one of the turn's steers. The settle + // calls below read it too: a result of a cycle Claude Code started + // that names none of them is not the steered work's result. + let answersSteer = false; if (!isAutonomousResult) { recordResultForOrphanCommands(); await ensureActiveTurn(message.user_message_uuid, message.user_message_uuids); @@ -5629,7 +5639,6 @@ export class ClaudeAcpAgent { const consumed = message.user_message_uuids ?? (message.user_message_uuid !== undefined ? [message.user_message_uuid] : []); - let answersSteer = false; for (const uuid of consumed) { steeredTurn.steeredEchoes.delete(uuid); if (steeredTurn.steeredUuids?.has(uuid)) answersSteer = true; @@ -6070,7 +6079,9 @@ export class ClaudeAcpAgent { // settling it out from under them would strand their output // and permission requests out-of-turn (issue #866's deadlock, // through the refusal lane). - await settleOrDefer(turnOutcome(session, "refusal")); + await settleOrDefer(turnOutcome(session, "refusal"), { + steeredWork: !startedByClaudeCode || answersSteer, + }); break; } @@ -6258,7 +6269,9 @@ export class ClaudeAcpAgent { await compaction.reset(); } if (!session.cancelled) { - await settleOrDefer(turnOutcome(session, stopReason)); + await settleOrDefer(turnOutcome(session, stopReason), { + steeredWork: !startedByClaudeCode || answersSteer, + }); } } finally { if (!isAutonomousResult) {