Skip to content

Commit 7d3fa68

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

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,28 @@ 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+
238260
it("rejects an assistant completion that does not follow the expected user turn", async () => {
239261
mockGetTaskWithId.mockResolvedValue({
240262
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)