diff --git a/packages/server/src/server/agent/providers/claude/agent.test.ts b/packages/server/src/server/agent/providers/claude/agent.test.ts index ff76e5186..fbb7907b2 100644 --- a/packages/server/src/server/agent/providers/claude/agent.test.ts +++ b/packages/server/src/server/agent/providers/claude/agent.test.ts @@ -1544,6 +1544,60 @@ describe("ClaudeAgentSession features", () => { } }); + test("accepts a steer while Claude compacts, so the compaction is not interrupted", async () => { + const { queryFactory, push } = createStreamingQueryMock(); + const client = new ClaudeAgentClient({ + logger, + queryFactory, + resolveBinary: async () => "/test/claude/bin", + }); + const session = await client.createSession({ provider: "claude", cwd: process.cwd() }); + const events: AgentStreamEvent[] = []; + const unsubscribe = session.subscribe((event) => events.push(event)); + try { + const { turnId } = await session.startTurn("/compact"); + const input = queryFactory.mock.calls[0]?.[0].prompt as AsyncIterable; + const iterator = input[Symbol.asyncIterator](); + await expect(iterator.next()).resolves.toMatchObject({ value: { type: "user" } }); + await vi.waitFor(() => + expect(events).toContainEqual( + expect.objectContaining({ + type: "timeline", + item: expect.objectContaining({ type: "user_message" }), + }), + ), + ); + push({ + type: "system", + subtype: "status", + status: "compacting", + session_id: "session-1", + } as unknown as SDKMessage); + await vi.waitFor(() => + expect(events).toContainEqual( + expect.objectContaining({ + type: "timeline", + item: { type: "compaction", status: "loading" }, + }), + ), + ); + + const admission = await session.steerActiveTurn?.("message during compaction", { + expectedTurnId: turnId, + }); + + // Claude Code holds a `next` message until the compaction finishes. Refusing it makes the + // manager replace the turn, and the interrupt cancels the compaction. + expect(admission).toEqual({ status: "accepted", providerMessageId: expect.any(String) }); + await expect(iterator.next()).resolves.toMatchObject({ + value: { type: "user", priority: "next" }, + }); + } finally { + unsubscribe(); + await session.close(); + } + }); + test("a human steer supersedes blocking permissions until Claude reads it", async () => { const { queryFactory } = createQueryMock(); const client = new ClaudeAgentClient({ diff --git a/packages/server/src/server/agent/providers/claude/agent.ts b/packages/server/src/server/agent/providers/claude/agent.ts index 1087e6dfe..ba559dedf 100644 --- a/packages/server/src/server/agent/providers/claude/agent.ts +++ b/packages/server/src/server/agent/providers/claude/agent.ts @@ -2637,7 +2637,10 @@ class ClaudeAgentSession implements AgentSession { return { status: "unavailable" }; } const activeTurnId = this.activeForegroundTurnId ?? this.autonomousTurn?.id; - if (this.compacting || activeTurnId !== options.expectedTurnId) { + // A compacting turn still takes a steer: Claude Code holds a `next` message until the + // compaction finishes. Refusing it makes the manager replace the turn, and that interrupt + // cancels the compaction. + if (activeTurnId !== options.expectedTurnId) { return { status: "unavailable" }; } diff --git a/packages/server/src/server/daemon-e2e/claude-steer-during-compaction.real.e2e.test.ts b/packages/server/src/server/daemon-e2e/claude-steer-during-compaction.real.e2e.test.ts new file mode 100644 index 000000000..85fc3547b --- /dev/null +++ b/packages/server/src/server/daemon-e2e/claude-steer-during-compaction.real.e2e.test.ts @@ -0,0 +1,122 @@ +import { beforeAll, beforeEach, describe, expect, test } from "vitest"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import pino from "pino"; + +import { ClaudeAgentClient } from "../agent/providers/claude/agent.js"; +import { DaemonClient } from "../test-utils/daemon-client.js"; +import { createTestPaseoDaemon } from "../test-utils/paseo-daemon.js"; +import { canRunRealProvider, getRealProviderConfig } from "./real-provider-test-config.js"; + +function tmpCwd(): string { + return mkdtempSync(path.join(tmpdir(), "daemon-real-claude-steer-compaction-")); +} + +type TimelineItem = Awaited< + ReturnType +>["entries"][number]["item"]; + +// The steer must land while the compaction is still running, or it takes the idle path. +function isCompacting(items: TimelineItem[]): boolean { + const statuses = new Set( + items.flatMap((item) => (item.type === "compaction" ? [item.status] : [])), + ); + return statuses.has("loading") && !statuses.has("completed"); +} + +function hasAssistantText(items: TimelineItem[], text: string): boolean { + return items.some((item) => item.type === "assistant_message" && item.text.includes(text)); +} + +async function timelineItems(client: DaemonClient, agentId: string): Promise { + const timeline = await client.fetchAgentTimeline(agentId, { + direction: "tail", + limit: 200, + }); + return timeline.entries.map((entry) => entry.item); +} + +async function waitForTimeline( + client: DaemonClient, + agentId: string, + check: (items: TimelineItem[]) => boolean, + timeoutMs: number, + label: string, +): Promise { + const deadline = Date.now() + timeoutMs; + for (;;) { + const items = await timelineItems(client, agentId); + if (check(items)) return items; + if (Date.now() > deadline) { + throw new Error(`Timed out: ${label}: ${JSON.stringify(items.slice(-6)).slice(0, 2000)}`); + } + await new Promise((resolve) => setTimeout(resolve, 100)); + } +} + +describe("daemon E2E (real claude) - steer during compaction", () => { + let canRun = false; + + beforeAll(async () => { + canRun = await canRunRealProvider("claude"); + }); + + beforeEach((context) => { + if (!canRun) context.skip(); + }); + + test("a steered message waits for a running /compact instead of cancelling it", async () => { + const logger = pino({ level: "silent" }); + const cwd = tmpCwd(); + const daemon = await createTestPaseoDaemon({ + agentClients: { claude: new ClaudeAgentClient({ logger }) }, + logger, + }); + const client = new DaemonClient({ + url: `ws://127.0.0.1:${daemon.port}/ws`, + }); + try { + await client.connect(); + await client.fetchAgents({ subscribe: {} }); + const agent = await client.createAgent({ + cwd, + title: "claude-steer-during-compaction", + ...getRealProviderConfig("claude"), + }); + await client.sendAgentMessage( + agent.id, + `Remember this list for later and reply exactly: NOTED. ${Array.from( + { length: 400 }, + (_, index) => `item-${index}`, + ).join(" ")}`, + ); + const firstFinish = await client.waitForFinish(agent.id, 120_000); + expect(firstFinish.status, firstFinish.error).toBe("idle"); + + await client.sendAgentMessage(agent.id, "/compact"); + await waitForTimeline(client, agent.id, isCompacting, 60_000, "Claude reports compacting"); + + await client.sendAgentMessage(agent.id, "Reply exactly: AFTER_COMPACT", { + activeTurnBehavior: "steer", + }); + await waitForTimeline( + client, + agent.id, + (items) => hasAssistantText(items, "AFTER_COMPACT"), + 180_000, + "Claude answers the steered message", + ); + await client.waitForFinish(agent.id, 120_000); + const finalItems = await timelineItems(client, agent.id); + expect( + finalItems.filter((item) => item.type === "compaction").map((item) => item.status), + ).toContain("completed"); + expect(hasAssistantText(finalItems, "Compaction canceled")).toBe(false); + } finally { + await client.close().catch(() => undefined); + await daemon.close().catch(() => undefined); + rmSync(cwd, { recursive: true, force: true }); + } + }, 420_000); +});