Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/session-history-completion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"roo-cline": minor
---

Mark sessions completed from their root task. Completed sessions are kept and can be reopened, but hidden from history unless Show completed is on. Resume continues the deepest unfinished task on the active delegation path instead of the parent, or opens the root with a warning if task relationships are damaged.
1 change: 1 addition & 0 deletions packages/types/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const historyItemSchema = z.object({
mode: z.string().optional(),
apiConfigName: z.string().optional(), // Provider profile name for sticky profile feature
status: z.enum(["active", "completed", "delegated"]).optional(),
sessionCompletedAt: z.number().optional(),
delegatedToId: z.string().optional(), // Last child this parent delegated to
childIds: z.array(z.string()).optional(), // All children spawned by this task
awaitingChildId: z.string().optional(), // Child currently awaited (set when delegated)
Expand Down
5 changes: 5 additions & 0 deletions packages/types/src/vscode-extension-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface ExtensionMessage {
| "fileSearchResults"
| "toggleApiConfigPin"
| "acceptInput"
| "resumeSessionInputUsed"
| "setHistoryPreviewCollapsed"
| "commandExecutionStatus"
| "mcpExecutionStatus"
Expand Down Expand Up @@ -400,6 +401,9 @@ export interface WebviewMessage {
| "selectImages"
| "exportCurrentTask"
| "showTaskWithId"
| "markSessionCompleted"
| "reopenSession"
| "resumeSession"
| "deleteTaskWithId"
| "exportTaskWithId"
| "importSettings"
Expand Down Expand Up @@ -541,6 +545,7 @@ export interface WebviewMessage {
askResponse?: ClineAskResponse
apiConfiguration?: ProviderSettings
images?: string[]
input?: string
bool?: boolean
value?: number
stepIndex?: number
Expand Down
286 changes: 19 additions & 267 deletions src/__tests__/removeClineFromStack-delegation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,281 +1,33 @@
// npx vitest run __tests__/removeClineFromStack-delegation.spec.ts

import { describe, it, expect, vi } from "vitest"
import { ClineProvider } from "../core/webview/ClineProvider"

describe("ClineProvider.removeClineFromStack() delegation awareness", () => {
/**
* Helper to build a minimal mock provider with a single task on the stack.
* The task's parentTaskId and taskId are configurable.
*/
function buildMockProvider(opts: {
childTaskId: string
parentTaskId?: string
parentHistoryItem?: Record<string, any>
getTaskWithIdError?: Error
}) {
const childTask = {
taskId: opts.childTaskId,
instanceId: "inst-1",
parentTaskId: opts.parentTaskId,
emit: vi.fn(),
abortTask: vi.fn().mockResolvedValue(undefined),
}

const updateTaskHistory = vi.fn().mockResolvedValue([])
const getTaskWithId = opts.getTaskWithIdError
? vi.fn().mockRejectedValue(opts.getTaskWithIdError)
: vi.fn().mockImplementation(async (id: string) => {
if (id === opts.parentTaskId && opts.parentHistoryItem) {
return { historyItem: { ...opts.parentHistoryItem } }
}
throw new Error("Task not found")
})

describe("ClineProvider.removeClineFromStack", () => {
it.each([undefined, "parent-1"])("SH-07: never repairs metadata (parent %s)", async (parentTaskId) => {
const cleanup = vi.fn()
const task = { taskId: "child", parentTaskId, emit: vi.fn(), abortTask: vi.fn() }
const provider = {
clineStack: [childTask] as any[],
taskEventListeners: new Map(),
log: vi.fn(),
getTaskWithId,
updateTaskHistory,
}

return { provider, childTask, updateTaskHistory, getTaskWithId }
}

it("repairs parent metadata (delegated → active) when a delegated child is removed", async () => {
const { provider, updateTaskHistory, getTaskWithId } = buildMockProvider({
childTaskId: "child-1",
parentTaskId: "parent-1",
parentHistoryItem: {
id: "parent-1",
task: "Parent task",
ts: 1000,
number: 1,
tokensIn: 0,
tokensOut: 0,
totalCost: 0,
status: "delegated",
awaitingChildId: "child-1",
delegatedToId: "child-1",
childIds: ["child-1"],
},
})

await (ClineProvider.prototype as any).removeClineFromStack.call(provider)

// Stack should be empty after pop
expect(provider.clineStack).toHaveLength(0)

// Parent lookup should have been called
expect(getTaskWithId).toHaveBeenCalledWith("parent-1")

// Parent metadata should be repaired
expect(updateTaskHistory).toHaveBeenCalledTimes(1)
const updatedParent = updateTaskHistory.mock.calls[0][0]
expect(updatedParent).toEqual(
expect.objectContaining({
id: "parent-1",
status: "active",
awaitingChildId: undefined,
}),
)

// Log the repair
expect(provider.log).toHaveBeenCalledWith(expect.stringContaining("Repaired parent parent-1 metadata"))
})

it("does NOT modify parent metadata when the task has no parentTaskId (non-delegated)", async () => {
const { provider, updateTaskHistory, getTaskWithId } = buildMockProvider({
childTaskId: "standalone-1",
// No parentTaskId — this is a top-level task
})

await (ClineProvider.prototype as any).removeClineFromStack.call(provider)

// Stack should be empty
expect(provider.clineStack).toHaveLength(0)

// No parent lookup or update should happen
expect(getTaskWithId).not.toHaveBeenCalled()
expect(updateTaskHistory).not.toHaveBeenCalled()
})

it("does NOT modify parent metadata when awaitingChildId does not match the popped child", async () => {
const { provider, updateTaskHistory, getTaskWithId } = buildMockProvider({
childTaskId: "child-1",
parentTaskId: "parent-1",
parentHistoryItem: {
id: "parent-1",
task: "Parent task",
ts: 1000,
number: 1,
tokensIn: 0,
tokensOut: 0,
totalCost: 0,
status: "delegated",
awaitingChildId: "child-OTHER", // different child
delegatedToId: "child-OTHER",
childIds: ["child-OTHER"],
},
})

await (ClineProvider.prototype as any).removeClineFromStack.call(provider)

// Parent was looked up but should NOT be updated
expect(getTaskWithId).toHaveBeenCalledWith("parent-1")
expect(updateTaskHistory).not.toHaveBeenCalled()
})

it("does NOT modify parent metadata when parent status is not 'delegated'", async () => {
const { provider, updateTaskHistory, getTaskWithId } = buildMockProvider({
childTaskId: "child-1",
parentTaskId: "parent-1",
parentHistoryItem: {
id: "parent-1",
task: "Parent task",
ts: 1000,
number: 1,
tokensIn: 0,
tokensOut: 0,
totalCost: 0,
status: "completed", // already completed
awaitingChildId: "child-1",
childIds: ["child-1"],
},
})

await (ClineProvider.prototype as any).removeClineFromStack.call(provider)

expect(getTaskWithId).toHaveBeenCalledWith("parent-1")
expect(updateTaskHistory).not.toHaveBeenCalled()
})

it("catches and logs errors during parent metadata repair without blocking the pop", async () => {
const { provider, childTask, updateTaskHistory, getTaskWithId } = buildMockProvider({
childTaskId: "child-1",
parentTaskId: "parent-1",
getTaskWithIdError: new Error("Storage unavailable"),
})

// Should NOT throw
await (ClineProvider.prototype as any).removeClineFromStack.call(provider)

// Stack should still be empty (pop was not blocked)
expect(provider.clineStack).toHaveLength(0)

// The abort should still have been called
expect(childTask.abortTask).toHaveBeenCalledWith(true)

// Error should be logged as non-fatal
expect(provider.log).toHaveBeenCalledWith(
expect.stringContaining("Failed to repair parent metadata for parent-1 (non-fatal)"),
)

// No update should have been attempted
expect(updateTaskHistory).not.toHaveBeenCalled()
})

it("handles empty stack gracefully", async () => {
const provider = {
clineStack: [] as any[],
taskEventListeners: new Map(),
log: vi.fn(),
clineStack: [task],
taskEventListeners: new Map([[task, [cleanup]]]),
getTaskWithId: vi.fn(),
updateTaskHistory: vi.fn(),
log: vi.fn(),
}

// Should not throw
await (ClineProvider.prototype as any).removeClineFromStack.call(provider)

expect(provider.clineStack).toHaveLength(0)
await ClineProvider.prototype.removeClineFromStack.call(provider as any)
expect(provider.clineStack).toEqual([])
expect(task.abortTask).toHaveBeenCalledWith(true)
expect(cleanup).toHaveBeenCalledOnce()
expect(provider.getTaskWithId).not.toHaveBeenCalled()
expect(provider.updateTaskHistory).not.toHaveBeenCalled()
})

it("skips delegation repair when skipDelegationRepair option is true", async () => {
const { provider, updateTaskHistory, getTaskWithId } = buildMockProvider({
childTaskId: "child-1",
parentTaskId: "parent-1",
parentHistoryItem: {
id: "parent-1",
task: "Parent task",
ts: 1000,
number: 1,
tokensIn: 0,
tokensOut: 0,
totalCost: 0,
status: "delegated",
awaitingChildId: "child-1",
delegatedToId: "child-1",
childIds: ["child-1"],
},
})

// Call with skipDelegationRepair: true (as delegateParentAndOpenChild would)
await (ClineProvider.prototype as any).removeClineFromStack.call(provider, { skipDelegationRepair: true })

// Stack should be empty after pop
expect(provider.clineStack).toHaveLength(0)

// Parent lookup should NOT have been called — repair was skipped entirely
expect(getTaskWithId).not.toHaveBeenCalled()
expect(updateTaskHistory).not.toHaveBeenCalled()
it("still cleans up when abort fails", async () => {
const task = { taskId: "child", emit: vi.fn(), abortTask: vi.fn().mockRejectedValue(new Error("abort")) }
const provider = { clineStack: [task], taskEventListeners: new Map(), log: vi.fn() }
await ClineProvider.prototype.removeClineFromStack.call(provider as any)
expect(provider.clineStack).toEqual([])
expect(provider.log).toHaveBeenCalledOnce()
})

it("does NOT reset grandparent during A→B→C nested delegation transition", async () => {
// Scenario: A delegated to B, B is now delegating to C.
// delegateParentAndOpenChild() pops B via removeClineFromStack({ skipDelegationRepair: true }).
// Grandparent A should remain "delegated" — its metadata must not be repaired.
const grandparentHistory = {
id: "task-A",
task: "Grandparent task",
ts: 1000,
number: 1,
tokensIn: 0,
tokensOut: 0,
totalCost: 0,
status: "delegated",
awaitingChildId: "task-B",
delegatedToId: "task-B",
childIds: ["task-B"],
}

const taskB = {
taskId: "task-B",
instanceId: "inst-B",
parentTaskId: "task-A",
emit: vi.fn(),
abortTask: vi.fn().mockResolvedValue(undefined),
}

const getTaskWithId = vi.fn().mockImplementation(async (id: string) => {
if (id === "task-A") {
return { historyItem: { ...grandparentHistory } }
}
throw new Error("Task not found")
})
const updateTaskHistory = vi.fn().mockResolvedValue([])

const provider = {
clineStack: [taskB] as any[],
taskEventListeners: new Map(),
log: vi.fn(),
getTaskWithId,
updateTaskHistory,
}

// Simulate what delegateParentAndOpenChild does: pop B with skipDelegationRepair
await (ClineProvider.prototype as any).removeClineFromStack.call(provider, { skipDelegationRepair: true })

// B was popped
expect(provider.clineStack).toHaveLength(0)

// Grandparent A should NOT have been looked up or modified
expect(getTaskWithId).not.toHaveBeenCalled()
expect(updateTaskHistory).not.toHaveBeenCalled()

// Grandparent A's metadata remains intact (delegated, awaitingChildId: task-B)
// The caller (delegateParentAndOpenChild) will update A to point to C separately.
it("handles an empty stack", async () => {
await ClineProvider.prototype.removeClineFromStack.call({ clineStack: [] } as any)
})
})
22 changes: 22 additions & 0 deletions src/core/task-persistence/TaskHistoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ export class TaskHistoryStore {
})
}

async updateIfPresent(
taskId: string,
update: (item: HistoryItem) => Partial<Omit<HistoryItem, "id">> | undefined,
): Promise<HistoryItem | undefined> {
return this.withLock(async () => {
const existing = this.cache.get(taskId)
if (!existing) {
return undefined
}
const changes = update(existing)
if (!changes) {
return undefined
}
const merged = { ...existing, ...changes, id: taskId }
await this.writeTaskFile(merged)
this.cache.set(taskId, merged)
this.scheduleIndexWrite()
await this.onWrite?.(this.getAll())
return merged
})
}

/**
* Delete a single task's history item.
*/
Expand Down
Loading