Skip to content
7 changes: 7 additions & 0 deletions docs/architecture/tla/TaskHistoryMigration.NoBackup.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions docs/architecture/tla/TaskHistoryMigration.NoSweep.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions docs/architecture/tla/TaskHistoryMigration.NoVerify.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/architecture/tla/TaskHistoryMigration.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

418 changes: 418 additions & 0 deletions docs/architecture/tla/TaskHistoryMigration.tla

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions docs/architecture/tla/TaskHistoryMigrationMC.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---- MODULE TaskHistoryMigrationMC ----
\* Sabotage operator values for the variant configs.
EXTENDS TaskHistoryMigration
NoSweepEnabled == FALSE
NoVerifyEnabled == FALSE
NoBackupEnabled == FALSE
StaleResurrectEnabled == TRUE
NoCompareGuardEnabled == FALSE
PeerOverwriteEnabled == FALSE
====
20 changes: 20 additions & 0 deletions src/core/task-persistence/TaskHistoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,26 @@ export class TaskHistoryStore {

// ────────────────────────────── Private: Path helpers ──────────────────────────────

/**
* Check whether a task directory exists without reading its files.
*/
async hasTaskDir(taskId: string): Promise<boolean> {
const tasksDir = await this.getTasksDir()
try {
await fs.access(path.join(tasksDir, taskId))
return true
} catch (error) {
// Absence makes the entry an orphan. Any other failure (e.g.
// EACCES, EIO) must not make the sweep treat an existing
// directory as gone, so the caller keeps the blob instead.
const code = getErrorCode(error)
if (code === "ENOENT" || code === "ENOTDIR") {
return false
}
throw error
}
}

/**
* Get the tasks base directory path, resolving custom storage paths.
*/
Expand Down
129 changes: 129 additions & 0 deletions src/core/task-persistence/__tests__/legacyTaskHistoryBlob.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions src/core/task-persistence/legacyTaskHistoryBlob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { HistoryItem } from "@roo-code/types"

export const LEGACY_TASK_HISTORY_BACKUP_FILE = "legacy-task-history.backup.json"

export type LegacyBlobSweepDecision =
{ action: "keep"; reason: "unverified-entries" | "backup-unwritten"; unverifiedIds: string[] } | { action: "clear" }

/**
* Decide whether the legacy globalState taskHistory blob can be removed.
*
* The file-backed store must hold every entry whose task directory still
* exists, and a backup must hold the full array before the delete. Entries
* without an id were never migrated, so they count like orphans here.
*/
export function planLegacyBlobSweep(
entries: HistoryItem[],
storeHeldIds: ReadonlySet<string>,
orphanIds: ReadonlySet<string>,
backupWritten: boolean,
): LegacyBlobSweepDecision {
const unverifiedIds = entries
.map((item) => item?.id)
.filter((id): id is string => Boolean(id))
.filter((id) => !orphanIds.has(id) && !storeHeldIds.has(id))

if (unverifiedIds.length > 0) {
return { action: "keep", reason: "unverified-entries", unverifiedIds }
}

if (!backupWritten) {
return { action: "keep", reason: "backup-unwritten", unverifiedIds: [] }
}

return { action: "clear" }
}

/**
* Merge the current legacy array with an earlier sweep's backup.
*
* Entries only the earlier backup holds (e.g. orphans with no task
* directory) must survive the write; current entries win on an id clash.
* Identical ID-less records match one-for-one, so a repeated deferred
* sweep does not duplicate them; unmatched prior-only records keep their
* existing multiplicity.
*/
export function mergeLegacyBackup(current: HistoryItem[], prior: HistoryItem[]): HistoryItem[] {
const currentIds = new Set(current.map((item) => item?.id).filter((id): id is string => Boolean(id)))
// An ID-less entry has no merge key, so the id filter below would
// append the prior copy again on every deferred sweep and grow the
// backup. Match identical ID-less records one-for-one and keep only
// the prior excess (CR 2873 thread 4814737822).
const currentIdless = new Map<string, number>()
for (const item of current) {
if (!item?.id) {
const key = JSON.stringify(item)
currentIdless.set(key, (currentIdless.get(key) ?? 0) + 1)
}
}
const keptPrior: HistoryItem[] = []
for (const item of prior) {
if (!item?.id) {
const key = JSON.stringify(item)
const matched = currentIdless.get(key) ?? 0
if (matched > 0) {
currentIdless.set(key, matched - 1)
continue
}
} else if (currentIds.has(item.id)) {
continue
}
keptPrior.push(item)
}
return [...current, ...keptPrior]
}
Loading
Loading