Skip to content

Commit 601d369

Browse files
committed
fix(api): return 0 instead of throwing for unavailable task history length
1 parent 46c1889 commit 601d369

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

apps/vscode-e2e/src/fixtures/subtasks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export function addSubtaskFixtures(mock: InstanceType<typeof LLMock>) {
332332
toolCalls: [
333333
{
334334
name: "attempt_completion",
335-
arguments: JSON.stringify({ result: "9" }),
335+
arguments: JSON.stringify({ result: SUBTASK_INTERRUPT_CHILD_FOLLOWUP_ANSWER }),
336336
id: "call_interrupt_child_completion_002",
337337
},
338338
],
@@ -348,7 +348,7 @@ export function addSubtaskFixtures(mock: InstanceType<typeof LLMock>) {
348348
toolCalls: [
349349
{
350350
name: "attempt_completion",
351-
arguments: JSON.stringify({ result: "Interrupted parent resumed" }),
351+
arguments: JSON.stringify({ result: SUBTASK_INTERRUPT_PARENT_RESULT }),
352352
id: "call_interrupt_parent_completion_003",
353353
},
354354
],
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, it, expect, vi, beforeEach } from "vitest"
2+
import * as vscode from "vscode"
3+
4+
import { API } from "../api"
5+
import { ClineProvider } from "../../core/webview/ClineProvider"
6+
7+
vi.mock("vscode")
8+
vi.mock("../../core/webview/ClineProvider")
9+
10+
describe("API#getTaskApiConversationHistoryLength", () => {
11+
let api: API
12+
let mockOutputChannel: vscode.OutputChannel
13+
let mockProvider: ClineProvider
14+
let mockGetTaskWithId: ReturnType<typeof vi.fn>
15+
16+
beforeEach(() => {
17+
mockOutputChannel = {
18+
appendLine: vi.fn(),
19+
} as unknown as vscode.OutputChannel
20+
21+
mockGetTaskWithId = vi.fn()
22+
23+
mockProvider = {
24+
context: {} as vscode.ExtensionContext,
25+
getTaskWithId: mockGetTaskWithId,
26+
on: vi.fn(),
27+
} as unknown as ClineProvider
28+
29+
api = new API(mockOutputChannel, mockProvider, undefined, true)
30+
})
31+
32+
it("returns the persisted api conversation history length", async () => {
33+
mockGetTaskWithId.mockResolvedValue({
34+
apiConversationHistory: [{ role: "user" }, { role: "assistant" }],
35+
})
36+
37+
await expect(api.getTaskApiConversationHistoryLength("task-1")).resolves.toBe(2)
38+
})
39+
40+
it("returns 0 instead of throwing when the task is unavailable", async () => {
41+
mockGetTaskWithId.mockRejectedValue(new Error("Task not found"))
42+
43+
await expect(api.getTaskApiConversationHistoryLength("missing-task")).resolves.toBe(0)
44+
})
45+
})

src/extension/api.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,13 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
240240
return item ? structuredClone(item) : undefined
241241
}
242242

243-
public async getTaskApiConversationHistoryLength(taskId: string) {
244-
const { apiConversationHistory } = await this.sidebarProvider.getTaskWithId(taskId)
245-
return apiConversationHistory.length
243+
public async getTaskApiConversationHistoryLength(taskId: string): Promise<number> {
244+
try {
245+
const { apiConversationHistory } = await this.sidebarProvider.getTaskWithId(taskId)
246+
return apiConversationHistory.length
247+
} catch {
248+
return 0
249+
}
246250
}
247251

248252
public getCurrentTaskStack() {

0 commit comments

Comments
 (0)