-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Auto-delete task history on plugin load (retention + checkpoint cleanup) #9262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hannesrudolph
wants to merge
23
commits into
main
Choose a base branch
from
chore/retention-purge-checkpoint-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,858
−29
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
99362f8
feat: task history retention purge uses provider delete path; checkpo…
hannesrudolph d073305
fix: address race condition in task purge by processing deletions seq…
hannesrudolph b4d3767
fix: update translations for task history retention settings
hannesrudolph 6a49a20
fix: update settings.json translations for task history retention
hannesrudolph 6e208d0
fix: update task history retention descriptions for multiple languages
hannesrudolph 73ebc19
fix: remove unused import and fix translation issues
hannesrudolph 943186a
fix: add missing props to About.spec.tsx tests
hannesrudolph b6d60b2
fix: remove 'as any' type casts in retention feature
hannesrudolph fb151aa
refactor(retention): narrow RetentionSetting type and optimize deleti…
hannesrudolph ab6d3ab
test(webview): align About component test props with new retention union
hannesrudolph 82df612
test: stabilize checkpoint service temp dir cleanup
hannesrudolph 32a6639
Update src/extension.ts
hannesrudolph 3a5d03e
Update src/__tests__/task-history-retention.spec.ts
hannesrudolph 48666c4
fix: clarify aboutRetention text - runs on extension activation, not …
daniel-lxs 7d20cc0
feat: add task storage size display to settings
daniel-lxs 2423706
i18n: add taskHistoryStorage translations to all locales
daniel-lxs db6b195
fix: add background purgeOldTasks call and remove duplicate NLS keys
hannesrudolph f109eb7
fix: resolve merge conflict, use startBackgroundRetentionPurge() for …
daniel-lxs d50d6a7
fix: handle non-existent workspace repo in checkpoint cleanup
daniel-lxs 4729908
chore: move task history retention to app state
hannesrudolph 51e1ab1
fix: address mrubens review feedback for task history retention
hannesrudolph f5f7272
fix: add missing task history retention translations
hannesrudolph 69efab6
test(core): increase timeout for CustomToolRegistry clearCache test
hannesrudolph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| // npx vitest run __tests__/task-history-retention.spec.ts | ||
| import * as fs from "fs/promises" | ||
| import * as path from "path" | ||
| import * as os from "os" | ||
|
|
||
| import { describe, it, expect } from "vitest" | ||
|
|
||
| // Ensure purge uses the provided base path without touching VS Code config | ||
| vi.mock("../utils/storage", () => ({ | ||
| getStorageBasePath: (p: string) => Promise.resolve(p), | ||
| })) | ||
|
|
||
| import { purgeOldTasks } from "../utils/task-history-retention" | ||
| import { GlobalFileNames } from "../shared/globalFileNames" | ||
|
|
||
| // Helpers | ||
| async function exists(p: string): Promise<boolean> { | ||
| try { | ||
| await fs.access(p) | ||
| return true | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| async function mkTempBase(): Promise<string> { | ||
| const base = await fs.mkdtemp(path.join(os.tmpdir(), "roo-retention-")) | ||
| // Ensure <base>/tasks exists | ||
| await fs.mkdir(path.join(base, "tasks"), { recursive: true }) | ||
| return base | ||
| } | ||
|
|
||
| async function createTask(base: string, id: string, ts?: number | "invalid"): Promise<string> { | ||
| const dir = path.join(base, "tasks", id) | ||
| await fs.mkdir(dir, { recursive: true }) | ||
| const metadataPath = path.join(dir, GlobalFileNames.taskMetadata) | ||
| const metadata = ts === "invalid" ? "{ invalid json" : JSON.stringify({ ts: ts ?? Date.now() }, null, 2) | ||
| await fs.writeFile(metadataPath, metadata, "utf8") | ||
| return dir | ||
| } | ||
|
|
||
| describe("utils/task-history-retention.ts purgeOldTasks()", () => { | ||
| it("purges tasks older than 7 days when retention is '7'", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| const old = await createTask(base, "task-8d", now - days(8)) | ||
| const recent = await createTask(base, "task-6d", now - days(6)) | ||
|
|
||
| const { purgedCount } = await purgeOldTasks("7", base, () => {}, false) | ||
| expect(purgedCount).toBe(1) | ||
| expect(await exists(old)).toBe(false) | ||
| expect(await exists(recent)).toBe(true) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("purges tasks older than 3 days when retention is '3'", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| const old = await createTask(base, "task-4d", now - days(4)) | ||
| const recent = await createTask(base, "task-2d", now - days(2)) | ||
|
|
||
| const { purgedCount } = await purgeOldTasks("3", base, () => {}, false) | ||
| expect(purgedCount).toBe(1) | ||
| expect(await exists(old)).toBe(false) | ||
| expect(await exists(recent)).toBe(true) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("does not delete anything in dry run mode but still reports purgedCount", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| const old = await createTask(base, "task-8d", now - days(8)) | ||
| const recent = await createTask(base, "task-6d", now - days(6)) | ||
|
|
||
| const { purgedCount } = await purgeOldTasks("7", base, () => {}, true) | ||
| expect(purgedCount).toBe(1) | ||
| // In dry run, nothing is deleted | ||
| expect(await exists(old)).toBe(true) | ||
| expect(await exists(recent)).toBe(true) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("does nothing when retention is 'never'", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const oldTs = now - 45 * 24 * 60 * 60 * 1000 // 45 days ago | ||
| const t1 = await createTask(base, "task-old", oldTs) | ||
| const t2 = await createTask(base, "task-new", now) | ||
|
|
||
| const { purgedCount, cutoff } = await purgeOldTasks("never", base, () => {}) | ||
|
|
||
| expect(purgedCount).toBe(0) | ||
| expect(cutoff).toBeNull() | ||
| expect(await exists(t1)).toBe(true) | ||
| expect(await exists(t2)).toBe(true) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("purges tasks older than 30 days and keeps newer or invalid-metadata ones", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| // One older than 30 days => delete | ||
| const old = await createTask(base, "task-31d", now - days(31)) | ||
| // One newer than 30 days => keep | ||
| const recent = await createTask(base, "task-29d", now - days(29)) | ||
| // Invalid metadata => skipped (kept) | ||
| const invalid = await createTask(base, "task-invalid", "invalid") | ||
|
|
||
| const { purgedCount, cutoff } = await purgeOldTasks("30", base, () => {}) | ||
|
|
||
| expect(typeof cutoff).toBe("number") | ||
| expect(purgedCount).toBe(1) | ||
| expect(await exists(old)).toBe(false) | ||
| expect(await exists(recent)).toBe(true) | ||
| expect(await exists(invalid)).toBe(true) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("deletes orphan checkpoint-only directories regardless of age", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| // Create a normal task that is recent (should be kept) | ||
| const normalTask = await createTask(base, "task-normal", now - days(1)) | ||
|
|
||
| // Create an orphan checkpoint-only directory (only has checkpoints/ subdirectory, no metadata) | ||
| const orphanDir = path.join(base, "tasks", "task-orphan-checkpoints") | ||
| await fs.mkdir(orphanDir, { recursive: true }) | ||
| const checkpointsDir = path.join(orphanDir, "checkpoints") | ||
| await fs.mkdir(checkpointsDir, { recursive: true }) | ||
| // Add a dummy file inside checkpoints to make it realistic | ||
| await fs.writeFile(path.join(checkpointsDir, "checkpoint-1.json"), "{}", "utf8") | ||
|
|
||
| // Create another orphan with just checkpoints (no other files) | ||
| const orphanDir2 = path.join(base, "tasks", "task-orphan-empty") | ||
| await fs.mkdir(orphanDir2, { recursive: true }) | ||
| const checkpointsDir2 = path.join(orphanDir2, "checkpoints") | ||
| await fs.mkdir(checkpointsDir2, { recursive: true }) | ||
|
|
||
| // Run purge with 7 day retention - orphans should be deleted regardless of age | ||
| const { purgedCount } = await purgeOldTasks("7", base, () => {}) | ||
|
|
||
| // Orphan directories should be deleted even though they're "recent" | ||
| expect(await exists(orphanDir)).toBe(false) | ||
| expect(await exists(orphanDir2)).toBe(false) | ||
| // Normal task should still exist (it's recent) | ||
| expect(await exists(normalTask)).toBe(true) | ||
| // Should have deleted 2 orphan directories | ||
| expect(purgedCount).toBe(2) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("does not delete directories with checkpoints AND other content", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| // Create a task directory with both checkpoints and other files (but recent, so should be kept) | ||
| const taskDir = path.join(base, "tasks", "task-with-content") | ||
| await fs.mkdir(taskDir, { recursive: true }) | ||
| const checkpointsDir = path.join(taskDir, "checkpoints") | ||
| await fs.mkdir(checkpointsDir, { recursive: true }) | ||
| await fs.writeFile(path.join(checkpointsDir, "checkpoint-1.json"), "{}", "utf8") | ||
| // Add other files (not just checkpoints) | ||
| await fs.writeFile(path.join(taskDir, "some-file.txt"), "content", "utf8") | ||
| // Note: No metadata file, so it's technically invalid but has content | ||
|
|
||
| const { purgedCount } = await purgeOldTasks("7", base, () => {}) | ||
|
|
||
| // Should NOT be deleted because it has content besides checkpoints | ||
| expect(await exists(taskDir)).toBe(true) | ||
| expect(purgedCount).toBe(0) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("falls back to directory mtime for legacy tasks without metadata", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| // Create a legacy task directory without any metadata file | ||
| const oldLegacyDir = path.join(base, "tasks", "task-legacy-old") | ||
| await fs.mkdir(oldLegacyDir, { recursive: true }) | ||
| // Add some content file | ||
| await fs.writeFile(path.join(oldLegacyDir, "content.txt"), "old task", "utf8") | ||
| // Manually set mtime to 10 days ago by touching the directory | ||
| const oldTime = new Date(now - days(10)) | ||
| await fs.utimes(oldLegacyDir, oldTime, oldTime) | ||
|
|
||
| // Create another legacy task that is recent | ||
| const recentLegacyDir = path.join(base, "tasks", "task-legacy-recent") | ||
| await fs.mkdir(recentLegacyDir, { recursive: true }) | ||
| await fs.writeFile(path.join(recentLegacyDir, "content.txt"), "recent task", "utf8") | ||
| // This one has recent mtime (now) | ||
|
|
||
| // Run purge with 7 day retention | ||
| const { purgedCount } = await purgeOldTasks("7", base, () => {}) | ||
|
|
||
| // Old legacy task should be deleted based on mtime | ||
| expect(await exists(oldLegacyDir)).toBe(false) | ||
| // Recent legacy task should be kept | ||
| expect(await exists(recentLegacyDir)).toBe(true) | ||
| expect(purgedCount).toBe(1) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("prioritizes metadata timestamp over mtime when both exist", async () => { | ||
| const base = await mkTempBase() | ||
| try { | ||
| const now = Date.now() | ||
| const days = (n: number) => n * 24 * 60 * 60 * 1000 | ||
|
|
||
| // Create task with old metadata ts but recent mtime | ||
| const taskDir = path.join(base, "tasks", "task-priority-test") | ||
| await fs.mkdir(taskDir, { recursive: true }) | ||
| const metadataPath = path.join(taskDir, GlobalFileNames.taskMetadata) | ||
| // Metadata says it's 10 days old (should be deleted with 7 day retention) | ||
| const metadata = JSON.stringify({ ts: now - days(10) }, null, 2) | ||
| await fs.writeFile(metadataPath, metadata, "utf8") | ||
| // But directory mtime is recent (could happen after editing) | ||
| // (Directory mtime is automatically recent from mkdir/writeFile) | ||
|
|
||
| const { purgedCount } = await purgeOldTasks("7", base, () => {}) | ||
|
|
||
| // Should be deleted based on metadata ts, not mtime | ||
| expect(await exists(taskDir)).toBe(false) | ||
| expect(purgedCount).toBe(1) | ||
| } finally { | ||
| await fs.rm(base, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.