Skip to content

Commit 4723d3f

Browse files
committed
fix(api): skip tool_result messages at generation boundary
1 parent a28f3c5 commit 4723d3f

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎src/extension/__tests__/api-task-conversation-history-length.spec.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,78 @@ describe("API#getTaskApiConversationHistoryLength", () => {
235235
await expect(api.hasTaskApiConversationHistorySequence("task-1", expectedSequence)).resolves.toBe(true)
236236
})
237237

238+
it("accepts attempt_completion after a persisted tool_result user message in the same generation", async () => {
239+
mockGetTaskWithId.mockResolvedValue({
240+
apiConversationHistory: [
241+
{ role: "user", content: [{ type: "text", text: "RESTART_PERSISTENCE_SMOKE" }] },
242+
{
243+
role: "assistant",
244+
content: [{ type: "tool_use", id: "read-1", name: "read_file", input: { path: "f.ts" } }],
245+
},
246+
// tool_result is role:"user" — must not terminate the generation search
247+
{ role: "user", content: [{ type: "tool_result", tool_use_id: "read-1", content: "file contents" }] },
248+
{
249+
role: "assistant",
250+
content: [
251+
{ type: "tool_use", id: "completion", name: "attempt_completion", input: { result: "done" } },
252+
],
253+
},
254+
],
255+
})
256+
257+
await expect(api.hasTaskApiConversationHistorySequence("task-1", expectedSequence)).resolves.toBe(true)
258+
})
259+
260+
it("rejects attempt_completion that follows a later genuine user text turn", async () => {
261+
mockGetTaskWithId.mockResolvedValue({
262+
apiConversationHistory: [
263+
{ role: "user", content: [{ type: "text", text: "RESTART_PERSISTENCE_SMOKE" }] },
264+
{
265+
role: "assistant",
266+
content: [{ type: "tool_use", id: "read-1", name: "read_file", input: { path: "f.ts" } }],
267+
},
268+
// A genuine user text turn ends the generation — completion below is in the next generation
269+
{ role: "user", content: [{ type: "text", text: "follow-up question" }] },
270+
{
271+
role: "assistant",
272+
content: [
273+
{ type: "tool_use", id: "completion", name: "attempt_completion", input: { result: "done" } },
274+
],
275+
},
276+
],
277+
})
278+
279+
await expect(api.hasTaskApiConversationHistorySequence("task-1", expectedSequence)).resolves.toBe(false)
280+
})
281+
282+
it("treats a user turn with mixed text and tool_result content as a genuine boundary", async () => {
283+
mockGetTaskWithId.mockResolvedValue({
284+
apiConversationHistory: [
285+
{ role: "user", content: [{ type: "text", text: "RESTART_PERSISTENCE_SMOKE" }] },
286+
{
287+
role: "assistant",
288+
content: [{ type: "tool_use", id: "read-1", name: "read_file", input: { path: "f.ts" } }],
289+
},
290+
// Mixed content: has both a tool_result and a text block — counts as a genuine user turn
291+
{
292+
role: "user",
293+
content: [
294+
{ type: "tool_result", tool_use_id: "read-1", content: "file contents" },
295+
{ type: "text", text: "what do you think?" },
296+
],
297+
},
298+
{
299+
role: "assistant",
300+
content: [
301+
{ type: "tool_use", id: "completion", name: "attempt_completion", input: { result: "done" } },
302+
],
303+
},
304+
],
305+
})
306+
307+
await expect(api.hasTaskApiConversationHistorySequence("task-1", expectedSequence)).resolves.toBe(false)
308+
})
309+
238310
it("rejects an assistant completion that does not follow the expected user turn", async () => {
239311
mockGetTaskWithId.mockResolvedValue({
240312
apiConversationHistory: [

‎src/extension/api.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,17 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
274274
if (userTurnIndex < 0) return false
275275

276276
// Search all assistant turns that belong to the same generation: between
277-
// this user turn and the next user turn (or end of history). A match in a
278-
// later generation would be a false positive.
279-
const nextUserIndex = apiConversationHistory.findIndex((m, i) => i > userTurnIndex && m.role === "user")
277+
// this user turn and the next genuine user text turn (or end of history).
278+
// tool_result messages also use role:"user", so we must skip them —
279+
// stopping at a tool_result would cut the search short before a later
280+
// attempt_completion in the same generation.
281+
const nextUserIndex = apiConversationHistory.findIndex(
282+
(m, i) =>
283+
i > userTurnIndex &&
284+
m.role === "user" &&
285+
Array.isArray(m.content) &&
286+
m.content.some((block) => block.type === "text"),
287+
)
280288
const generationSlice = apiConversationHistory.slice(
281289
userTurnIndex + 1,
282290
nextUserIndex < 0 ? undefined : nextUserIndex,

0 commit comments

Comments
 (0)