:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(agent): track background turns that start without a fresh echo (#116872)
posthog[bot]04af17948907
diff --git a/packages/agent/packages/agent/src/adapters/claude/claude-agent.task-notification.test.ts b/packages/agent/packages/agent/src/adapters/claude/claude-agent.task-notification.test.tsindex e872ab5f78c5..5a34e9211766 100644--- a/packages/agent/packages/agent/src/adapters/claude/claude-agent.task-notification.test.ts+++ b/packages/agent/packages/agent/src/adapters/claude/claude-agent.task-notification.test.ts@@ -247,6 +247,40 @@ describe("ClaudeAcpAgent — task-notification results", () => {});});+ it("accepts a steer in a background turn whose notification echoed before the previous result", async () => {+ const { agent } = makeAgent();+ const sessionId = "s-task-notification-early-echo";+ const { query, input } = installFakeSession(agent, sessionId);++ const promptPromise = agent.prompt({+ sessionId,+ prompt: [{ type: "text", text: "hi" }],+ });+ await tick();+ await echoUserMessage(query, input);+ await send(query, assistantMessage(sessionId, "msg_1", "hello"));+ await send(query, resultSuccess(sessionId, "result-1"));+ await promptPromise;++ await send(query, taskNotificationUserMessage(sessionId, "ping 1"));+ await send(query, assistantMessage(sessionId, "msg_2", "ping 1 received."));+ await send(query, taskNotificationUserMessage(sessionId, "ping 2"));+ await send(query, taskNotificationResult(sessionId, "result-2"));+ await send(query, assistantMessage(sessionId, "msg_3", "ping 2 received."));++ const steerPromise = agent.prompt({+ sessionId,+ prompt: [{ type: "text", text: "wrap up now" }],+ _meta: { steer: true },+ });+ await tick();+ await echoUserMessage(query, input);+ await send(query, assistantMessage(sessionId, "msg_4", "wrapping up"));+ await expect(steerPromise).resolves.toMatchObject({+ _meta: { steer: true },+ });+ });+it("declines a background steer the SDK never echoes after the background turn ends", async () => {vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });try {diff --git a/packages/agent/packages/agent/src/adapters/claude/claude-agent.ts b/packages/agent/packages/agent/src/adapters/claude/claude-agent.tsindex e43ba6208daf..b07f15c38c68 100644--- a/packages/agent/packages/agent/src/adapters/claude/claude-agent.ts+++ b/packages/agent/packages/agent/src/adapters/claude/claude-agent.ts@@ -1821,6 +1821,18 @@ export class ClaudeAcpAgent extends BaseAcpAgent {) {session.backgroundTurnActive = true;}+ // The SDK can echo a task notification before the previous+ // background result, so that result clears the flag first. Model+ // output with no tracked turn still means a background turn runs.+ if (+ message.type === "assistant" &&+ message.parent_tool_use_id === null &&+ !session.cancelled &&+ !session.activeTurn &&+ !session.turnQueue.some((turn) => !turn.settled)+ ) {+ session.backgroundTurnActive = true;+ }// A user echo promotes its queued turn (handing off any still-// active one first), then drops from the feed. Runs before the// cancelled guard so a turn enqueued after a cancel still starts.