Skip to content

Commit 248c835

Browse files
Jasmine ZhangJasmine Zhang
authored andcommitted
fix: ignore reasoning-only heartbeat ack blocks
Fixes #95796
1 parent f80f4a8 commit 248c835

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,27 @@ describe("isHeartbeatOkResponse", () => {
108108
}),
109109
).toBe(false);
110110

111+
expect(
112+
isHeartbeatOkResponse({
113+
role: "assistant",
114+
content: [
115+
{ type: "reasoning", text: "internal draft" },
116+
{ type: "output_text", text: "HEARTBEAT_OK" },
117+
],
118+
}),
119+
).toBe(true);
120+
121+
expect(
122+
isHeartbeatOkResponse({
123+
role: "assistant",
124+
content: [
125+
{ type: "thinking", thinking: "internal draft" },
126+
{ type: "output_text", text: "HEARTBEAT_OK" },
127+
{ type: "image", image_url: "https://example.com/a.png" },
128+
],
129+
}),
130+
).toBe(false);
131+
111132
const toolCallOnlyMessage = {
112133
role: "assistant",
113134
content: null,
@@ -189,6 +210,24 @@ describe("filterHeartbeatTranscriptArtifacts", () => {
189210
}
190211
});
191212

213+
it("removes heartbeat pairs when the ack also contains reasoning blocks", () => {
214+
const messages = [
215+
{ role: "user", content: HEARTBEAT_TRANSCRIPT_PROMPT },
216+
{
217+
role: "assistant",
218+
content: [
219+
{ type: "reasoning", text: "thinking..." },
220+
{ type: "output_text", text: "HEARTBEAT_OK" },
221+
],
222+
},
223+
{ role: "user", content: "what model are you" },
224+
];
225+
226+
expect(filterHeartbeatTranscriptArtifacts(messages, undefined, HEARTBEAT_PROMPT)).toEqual([
227+
{ role: "user", content: "what model are you" },
228+
]);
229+
});
230+
192231
it("removes prompt-only interrupted heartbeat spans", () => {
193232
const messages = [
194233
{ role: "user", content: HEARTBEAT_TRANSCRIPT_PROMPT },

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,28 @@ function resolveMessageText(content: unknown): { text: string; hasNonTextContent
282282
return { text, hasNonTextContent };
283283
}
284284

285+
function hasOnlyIgnorableHeartbeatAckBlocks(content: unknown): boolean {
286+
if (!Array.isArray(content) || content.length === 0) {
287+
return false;
288+
}
289+
let sawTextBlock = false;
290+
for (const block of content) {
291+
if (!isRecord(block)) {
292+
return false;
293+
}
294+
const type = readString(block.type) ?? "";
295+
if (type === "text" || type === "input_text" || type === "output_text") {
296+
sawTextBlock = true;
297+
continue;
298+
}
299+
if (type === "reasoning" || type === "thinking") {
300+
continue;
301+
}
302+
return false;
303+
}
304+
return sawTextBlock;
305+
}
306+
285307
/** Return whether a user message is an internal heartbeat prompt. */
286308
export function isHeartbeatUserMessage(
287309
message: { role: string; content?: unknown },
@@ -337,7 +359,7 @@ export function isHeartbeatOkResponse(
337359
return false;
338360
}
339361
const { text, hasNonTextContent } = resolveMessageText(message.content);
340-
if (hasNonTextContent) {
362+
if (hasNonTextContent && !hasOnlyIgnorableHeartbeatAckBlocks(message.content)) {
341363
return false;
342364
}
343365
return stripHeartbeatToken(text, { mode: "heartbeat", maxAckChars: ackMaxChars }).shouldSkip;

0 commit comments

Comments
 (0)