Skip to content

Commit 034787c

Browse files
committed
fix(heartbeat): allow reasoning/thinking blocks in heartbeat ack filtering
1 parent f354889 commit 034787c

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,51 @@ describe("isHeartbeatOkResponse", () => {
135135
),
136136
).toBe(false);
137137
});
138+
139+
it("matches heartbeat acks that include reasoning blocks", () => {
140+
// Model-internal reasoning blocks should not prevent heartbeat filtering.
141+
expect(
142+
isHeartbeatOkResponse({
143+
role: "assistant",
144+
content: [
145+
{ type: "thinking", thinking: "Checking for new tasks..." },
146+
{ type: "text", text: "HEARTBEAT_OK" },
147+
],
148+
}),
149+
).toBe(true);
150+
151+
expect(
152+
isHeartbeatOkResponse({
153+
role: "assistant",
154+
content: [
155+
{ type: "reasoning", reasoning: "Nothing to do" },
156+
{ type: "text", text: "HEARTBEAT_OK" },
157+
],
158+
}),
159+
).toBe(true);
160+
161+
expect(
162+
isHeartbeatOkResponse({
163+
role: "assistant",
164+
content: [
165+
{ type: "redacted_thinking", data: "redacted..." },
166+
{ type: "text", text: "HEARTBEAT_OK" },
167+
],
168+
}),
169+
).toBe(true);
170+
});
171+
172+
it("does not match heartbeat when reasoning is mixed with non-text content", () => {
173+
expect(
174+
isHeartbeatOkResponse({
175+
role: "assistant",
176+
content: [
177+
{ type: "thinking", thinking: "Checking..." },
178+
{ type: "tool_use", id: "tool-1", name: "search", input: {} },
179+
],
180+
}),
181+
).toBe(false);
182+
});
138183
});
139184

140185
describe("filterHeartbeatTranscriptArtifacts", () => {
@@ -189,6 +234,30 @@ describe("filterHeartbeatTranscriptArtifacts", () => {
189234
}
190235
});
191236

237+
it("removes heartbeat pairs where the assistant response includes reasoning blocks", () => {
238+
const messages = [
239+
{ role: "user", content: "Hello" },
240+
{ role: "assistant", content: "Hi there!" },
241+
{ role: "user", content: HEARTBEAT_PROMPT },
242+
{
243+
role: "assistant",
244+
content: [
245+
{ type: "thinking", thinking: "Checking for due tasks..." },
246+
{ type: "text", text: "HEARTBEAT_OK" },
247+
],
248+
},
249+
{ role: "user", content: "What time is it?" },
250+
{ role: "assistant", content: "It is 3pm." },
251+
];
252+
253+
expect(filterHeartbeatTranscriptArtifacts(messages, undefined, HEARTBEAT_PROMPT)).toEqual([
254+
{ role: "user", content: "Hello" },
255+
{ role: "assistant", content: "Hi there!" },
256+
{ role: "user", content: "What time is it?" },
257+
{ role: "assistant", content: "It is 3pm." },
258+
]);
259+
});
260+
192261
it("removes prompt-only interrupted heartbeat spans", () => {
193262
const messages = [
194263
{ role: "user", content: HEARTBEAT_TRANSCRIPT_PROMPT },

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,18 @@ function resolveMessageText(content: unknown): { text: string; hasNonTextContent
269269
continue;
270270
}
271271
if (block.type !== "text" && block.type !== "input_text" && block.type !== "output_text") {
272+
// Model-internal reasoning/thinking blocks are invisible to the user and should
273+
// not prevent heartbeat filtering. They are ephemeral model artifacts, not
274+
// content that the user sees or responds to. Skipping them here lets
275+
// isHeartbeatOkResponse correctly classify short HEARTBEAT_OK acks even when
276+
// the model emits reasoning alongside the text response.
277+
if (
278+
block.type === "reasoning" ||
279+
block.type === "thinking" ||
280+
block.type === "redacted_thinking"
281+
) {
282+
continue;
283+
}
272284
hasNonTextContent = true;
273285
continue;
274286
}

0 commit comments

Comments
 (0)