Skip to content

Commit 1ea84e3

Browse files
fix(heartbeat): detect HEARTBEAT_OK through reasoning/thinking blocks
isHeartbeatOkResponse previously rejected any assistant message with non-text content, so a HEARTBEAT_OK ack paired with a reasoning or thinking block was preserved as a normal turn. Treat thinking and redacted_thinking blocks as silent internal content so the text ack can still be recognized and the heartbeat span stripped. Fixes #95796
1 parent f65aca6 commit 1ea84e3

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎src/auto-reply/heartbeat-filter.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,38 @@ describe("isHeartbeatOkResponse", () => {
124124
expect(isHeartbeatOkResponse(toolCallOnlyMessage)).toBe(false);
125125
});
126126

127+
it("ignores silent reasoning/thinking blocks when checking for heartbeat acknowledgements", () => {
128+
expect(
129+
isHeartbeatOkResponse({
130+
role: "assistant",
131+
content: [
132+
{ type: "thinking", thinking: "I should confirm the heartbeat." },
133+
{ type: "text", text: "HEARTBEAT_OK" },
134+
],
135+
}),
136+
).toBe(true);
137+
138+
expect(
139+
isHeartbeatOkResponse({
140+
role: "assistant",
141+
content: [
142+
{ type: "redacted_thinking", data: "opaque" },
143+
{ type: "text", text: "HEARTBEAT_OK" },
144+
],
145+
}),
146+
).toBe(true);
147+
148+
expect(
149+
isHeartbeatOkResponse({
150+
role: "assistant",
151+
content: [
152+
{ type: "thinking", thinking: "thinking" },
153+
{ type: "tool_use", id: "tool-1", name: "search", input: {} },
154+
],
155+
}),
156+
).toBe(false);
157+
});
158+
127159
it("respects ackMaxChars overrides", () => {
128160
expect(
129161
isHeartbeatOkResponse(

‎src/auto-reply/heartbeat-filter.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MESSAGE_TOOL_DELIVERY_HINTS } from "./reply/delivery-hints.js";
1414
const HEARTBEAT_TASK_PROMPT_PREFIX =
1515
"Run the following periodic tasks (only those due based on their intervals):";
1616
const HEARTBEAT_TASK_PROMPT_ACK = "After completing all due tasks, reply HEARTBEAT_OK.";
17+
const REASONING_BLOCK_TYPES = new Set(["thinking", "redacted_thinking"]);
1718
const TOOL_CALL_BLOCK_TYPES = new Set([
1819
"toolCall",
1920
"functionCall",
@@ -254,7 +255,10 @@ function matchesHeartbeatPromptText(text: string, prompt: string | undefined): b
254255
return Boolean(normalized) && (text === normalized || text.startsWith(`${normalized}\n`));
255256
}
256257

257-
function resolveMessageText(content: unknown): { text: string; hasNonTextContent: boolean } {
258+
function resolveMessageText(
259+
content: unknown,
260+
ignoredNonTextTypes?: ReadonlySet<string>,
261+
): { text: string; hasNonTextContent: boolean } {
258262
if (typeof content === "string") {
259263
return { text: content, hasNonTextContent: false };
260264
}
@@ -268,16 +272,21 @@ function resolveMessageText(content: unknown): { text: string; hasNonTextContent
268272
hasNonTextContent = true;
269273
continue;
270274
}
271-
if (block.type !== "text" && block.type !== "input_text" && block.type !== "output_text") {
272-
hasNonTextContent = true;
275+
const type = readString(block.type);
276+
if (type === "text" || type === "input_text" || type === "output_text") {
277+
const blockText = (block as { text?: unknown }).text;
278+
if (typeof blockText !== "string") {
279+
hasNonTextContent = true;
280+
continue;
281+
}
282+
text += blockText;
283+
} else if (ignoredNonTextTypes?.has(type ?? "")) {
284+
// Silent internal content (e.g. reasoning/thinking blocks) does not prevent
285+
// a message from being treated as a plain heartbeat acknowledgement.
273286
continue;
274-
}
275-
const blockText = (block as { text?: unknown }).text;
276-
if (typeof blockText !== "string") {
287+
} else {
277288
hasNonTextContent = true;
278-
continue;
279289
}
280-
text += blockText;
281290
}
282291
return { text, hasNonTextContent };
283292
}
@@ -336,7 +345,7 @@ export function isHeartbeatOkResponse(
336345
if (hasAssistantToolCall(message)) {
337346
return false;
338347
}
339-
const { text, hasNonTextContent } = resolveMessageText(message.content);
348+
const { text, hasNonTextContent } = resolveMessageText(message.content, REASONING_BLOCK_TYPES);
340349
if (hasNonTextContent) {
341350
return false;
342351
}

0 commit comments

Comments
 (0)