Compare commits

...
Author SHA1 Message Date
monarchmaisuriya 9f80c5e1a0 fix(claude): settle the first tool call after a compaction
Claude Code now sends the compaction summary as a user message marked
isSynthetic. The synthetic check returned before the one-shot compacting
flag was cleared, so the flag stayed set and swallowed the next user
message: the first tool result after the compaction. That call stayed
running and was later shown as Canceled when the idle runtime was
collected.

Skip the summary by what it is (synthetic, or isCompactSummary in older
streams) instead of by position, and drop the compacting flag, which
nothing else reads.

Co-authored-by: monarchmaisuriya <monarchmaisuriya7600@gmail.com>
2026-11-29 08:16:26 +00:00
2 changed files with 68 additions and 7 deletions
@@ -4508,6 +4508,70 @@ describe("ClaudeAgentSession context window usage", () => {
}
});
test("settles the first tool call after a compaction whose summary is synthetic", async () => {
// Claude Code 2.1.330 sends the compaction summary as a user message marked isSynthetic
// (seen live after /compact), so it is skipped before it can consume the compaction state.
const session = await createSessionForTurns([
[
createCompactingStatus(),
createCompactBoundary({ compact_metadata: { trigger: "auto", pre_tokens: 167_000 } }),
{
type: "user",
parent_tool_use_id: null,
isSynthetic: true,
message: {
role: "user",
content: "This session is being continued from a previous conversation.",
},
uuid: "compact-summary-1",
session_id: "session-1",
},
{
type: "assistant",
parent_tool_use_id: null,
message: {
role: "assistant",
content: [
{
type: "tool_use",
id: "toolu-after-compact",
name: "Bash",
input: { command: "ls" },
},
],
},
uuid: "assistant-after-compact",
session_id: "session-1",
},
{
type: "user",
parent_tool_use_id: null,
message: {
role: "user",
content: [{ type: "tool_result", tool_use_id: "toolu-after-compact", content: "a.go" }],
},
uuid: "tool-result-after-compact",
session_id: "session-1",
},
createSuccessResult(),
],
]);
try {
const events = await collectStreamEvents(session, "keep working");
const statuses = events.flatMap((event) =>
event.type === "timeline" &&
event.item.type === "tool_call" &&
event.item.callId === "toolu-after-compact"
? [event.item.status]
: [],
);
expect(statuses).toEqual(["running", "completed"]);
} finally {
await session.close();
}
});
test("reports the plan mode Claude enters on its own with EnterPlanMode", async () => {
// Claude Code announces a mode it switched to itself as a status message
// carrying the new permissionMode, right after the EnterPlanMode tool call.
@@ -2698,7 +2698,6 @@ class ClaudeAgentSession implements AgentSession {
private lastOptionsModel: string | null = null;
private lastRuntimeModel: string | null = null;
private readonly resultMeter = new ClaudeResultMeter();
private compacting = false;
private compactionMarkerOpen = false;
private queryPumpPromise: Promise<void> | null = null;
private queryRestartNeeded = false;
@@ -5468,7 +5467,6 @@ class ClaudeAgentSession implements AgentSession {
}
const status = toObjectRecord(message)?.status;
if (status === "compacting") {
this.compacting = true;
// Claude Code repeats this status every 30 seconds until the compaction
// finishes. Each repeat used to open its own marker, and the app only ever
// resolves one of them, so the rest stayed on "Compacting..." forever.
@@ -5554,11 +5552,10 @@ class ClaudeAgentSession implements AgentSession {
message: Extract<SDKMessage, { type: "user" }>,
events: AgentStreamEvent[],
): Promise<void> {
if (isSyntheticUserEntry(message)) {
return;
}
if (this.compacting) {
this.compacting = false;
// The compaction summary is skipped by what it is, never by being the next user message:
// Claude marks it synthetic (isCompactSummary in older streams), and a position-based skip
// swallowed the first tool result after every compaction instead.
if (isSyntheticUserEntry(message) || toObjectRecord(message)?.isCompactSummary === true) {
return;
}
const messageId =