-
Notifications
You must be signed in to change notification settings - Fork 87
M5: Retention Cleanup + Tests + Documentation #6919
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
Merged
Jasonnnz
merged 1 commit into
feature/qa-video-automation
from
swarm/qa-video-auto/task-8
Feb 23, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| import { describe, test, expect, beforeEach, afterAll, mock } from 'bun:test'; | ||
| import { mkdtempSync, rmSync, writeFileSync, existsSync, mkdirSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| const testDir = mkdtempSync(join(tmpdir(), 'recording-cleanup-test-')); | ||
|
|
||
| mock.module('../util/platform.js', () => ({ | ||
| getDataDir: () => testDir, | ||
| isMacOS: () => process.platform === 'darwin', | ||
| isLinux: () => process.platform === 'linux', | ||
| isWindows: () => process.platform === 'win32', | ||
| getSocketPath: () => join(testDir, 'test.sock'), | ||
| getPidPath: () => join(testDir, 'test.pid'), | ||
| getDbPath: () => join(testDir, 'test.db'), | ||
| getLogPath: () => join(testDir, 'test.log'), | ||
| ensureDataDir: () => {}, | ||
| getRootDir: () => testDir, | ||
| })); | ||
|
|
||
| mock.module('../util/logger.js', () => ({ | ||
| getLogger: () => new Proxy({} as Record<string, unknown>, { | ||
| get: () => () => {}, | ||
| }), | ||
| })); | ||
|
|
||
| mock.module('../config/loader.js', () => ({ | ||
| getConfig: () => ({ | ||
| model: 'test', | ||
| provider: 'test', | ||
| apiKeys: {}, | ||
| memory: { enabled: false }, | ||
| rateLimit: { maxRequestsPerMinute: 0, maxTokensPerSession: 0 }, | ||
| }), | ||
| })); | ||
|
|
||
| import { initializeDb, getDb, resetDb } from '../memory/db.js'; | ||
| import { | ||
| uploadAttachment, | ||
| createFileBackedAttachment, | ||
| getAttachmentById, | ||
| getExpiredFileAttachments, | ||
| } from '../memory/attachments-store.js'; | ||
| import { runCleanupPass } from '../daemon/recording-cleanup.js'; | ||
|
|
||
| initializeDb(); | ||
|
|
||
| afterAll(() => { | ||
| resetDb(); | ||
| try { rmSync(testDir, { recursive: true }); } catch { /* best effort */ } | ||
| }); | ||
|
|
||
| function resetTables() { | ||
| const db = getDb(); | ||
| db.run('DELETE FROM message_attachments'); | ||
| db.run('DELETE FROM attachments'); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Cleanup pass tests | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('runCleanupPass', () => { | ||
| beforeEach(resetTables); | ||
|
|
||
| test('deletes expired file-backed attachments and their files', () => { | ||
| const now = Date.now(); | ||
| const recordingsDir = join(testDir, 'recordings'); | ||
| mkdirSync(recordingsDir, { recursive: true }); | ||
|
|
||
| // Create a file on disk | ||
| const filePath = join(recordingsDir, 'expired-recording.mp4'); | ||
| writeFileSync(filePath, Buffer.alloc(1024, 0)); // 1 KB dummy file | ||
|
|
||
| // Create expired file-backed attachment | ||
| const expired = createFileBackedAttachment({ | ||
| filename: 'expired-recording.mp4', | ||
| mimeType: 'video/mp4', | ||
| sizeBytes: 1024, | ||
| filePath, | ||
| expiresAt: now - 10000, // expired 10 seconds ago | ||
| }); | ||
|
|
||
| // Verify file exists before cleanup | ||
| expect(existsSync(filePath)).toBe(true); | ||
|
|
||
| const result = runCleanupPass(); | ||
|
|
||
| expect(result.cleaned).toBe(1); | ||
| expect(result.bytesFreed).toBe(1024); | ||
|
|
||
| // File should be removed from disk | ||
| expect(existsSync(filePath)).toBe(false); | ||
|
|
||
| // DB row should be removed | ||
| expect(getAttachmentById(expired.id)).toBeNull(); | ||
| }); | ||
|
|
||
| test('does not touch non-expired file-backed attachments', () => { | ||
| const now = Date.now(); | ||
| const recordingsDir = join(testDir, 'recordings'); | ||
| mkdirSync(recordingsDir, { recursive: true }); | ||
|
|
||
| const filePath = join(recordingsDir, 'fresh-recording.mp4'); | ||
| writeFileSync(filePath, Buffer.alloc(512, 0)); | ||
|
|
||
| const fresh = createFileBackedAttachment({ | ||
| filename: 'fresh-recording.mp4', | ||
| mimeType: 'video/mp4', | ||
| sizeBytes: 512, | ||
| filePath, | ||
| expiresAt: now + 86400000, // expires tomorrow | ||
| }); | ||
|
|
||
| const result = runCleanupPass(); | ||
|
|
||
| expect(result.cleaned).toBe(0); | ||
| expect(result.bytesFreed).toBe(0); | ||
|
|
||
| // File should still exist | ||
| expect(existsSync(filePath)).toBe(true); | ||
|
|
||
| // DB row should still exist | ||
| expect(getAttachmentById(fresh.id)).not.toBeNull(); | ||
| }); | ||
|
|
||
| test('never touches inline_base64 attachments', () => { | ||
| // Create an inline base64 attachment | ||
| const inline = uploadAttachment('chart.png', 'image/png', 'iVBORw0K'); | ||
|
|
||
| const result = runCleanupPass(); | ||
|
|
||
| expect(result.cleaned).toBe(0); | ||
|
|
||
| // Inline attachment should still exist | ||
| expect(getAttachmentById(inline.id)).not.toBeNull(); | ||
| }); | ||
|
|
||
| test('handles missing files gracefully (file already deleted)', () => { | ||
| const now = Date.now(); | ||
|
|
||
| // Create expired attachment pointing to a non-existent file | ||
| const expired = createFileBackedAttachment({ | ||
| filename: 'ghost-recording.mp4', | ||
| mimeType: 'video/mp4', | ||
| sizeBytes: 2048, | ||
| filePath: join(testDir, 'nonexistent', 'ghost.mp4'), | ||
| expiresAt: now - 10000, | ||
| }); | ||
|
|
||
| const result = runCleanupPass(); | ||
|
|
||
| // Should still clean up the DB row even if the file is missing | ||
| expect(result.cleaned).toBe(1); | ||
| expect(result.bytesFreed).toBe(0); // no file to measure | ||
|
|
||
| expect(getAttachmentById(expired.id)).toBeNull(); | ||
| }); | ||
|
|
||
| test('cleans up multiple expired recordings in one pass', () => { | ||
| const now = Date.now(); | ||
| const recordingsDir = join(testDir, 'recordings-multi'); | ||
| mkdirSync(recordingsDir, { recursive: true }); | ||
|
|
||
| const fileA = join(recordingsDir, 'a.mp4'); | ||
| const fileB = join(recordingsDir, 'b.mp4'); | ||
| writeFileSync(fileA, Buffer.alloc(2048, 0)); | ||
| writeFileSync(fileB, Buffer.alloc(4096, 0)); | ||
|
|
||
| createFileBackedAttachment({ | ||
| filename: 'a.mp4', | ||
| mimeType: 'video/mp4', | ||
| sizeBytes: 2048, | ||
| filePath: fileA, | ||
| expiresAt: now - 5000, | ||
| }); | ||
|
|
||
| createFileBackedAttachment({ | ||
| filename: 'b.mp4', | ||
| mimeType: 'video/mp4', | ||
| sizeBytes: 4096, | ||
| filePath: fileB, | ||
| expiresAt: now - 3000, | ||
| }); | ||
|
|
||
| // Also add a non-expired one | ||
| const fileC = join(recordingsDir, 'c.mp4'); | ||
| writeFileSync(fileC, Buffer.alloc(1024, 0)); | ||
| createFileBackedAttachment({ | ||
| filename: 'c.mp4', | ||
| mimeType: 'video/mp4', | ||
| sizeBytes: 1024, | ||
| filePath: fileC, | ||
| expiresAt: now + 86400000, | ||
| }); | ||
|
|
||
| const result = runCleanupPass(); | ||
|
|
||
| expect(result.cleaned).toBe(2); | ||
| expect(result.bytesFreed).toBe(2048 + 4096); | ||
|
|
||
| // Expired files gone | ||
| expect(existsSync(fileA)).toBe(false); | ||
| expect(existsSync(fileB)).toBe(false); | ||
|
|
||
| // Non-expired file still present | ||
| expect(existsSync(fileC)).toBe(true); | ||
| }); | ||
|
|
||
| test('returns zeros when no expired attachments exist', () => { | ||
| const result = runCleanupPass(); | ||
| expect(result.cleaned).toBe(0); | ||
| expect(result.bytesFreed).toBe(0); | ||
| }); | ||
|
|
||
| test('file-backed attachments without expiresAt are never cleaned', () => { | ||
| const recordingsDir = join(testDir, 'recordings-no-expiry'); | ||
| mkdirSync(recordingsDir, { recursive: true }); | ||
|
|
||
| const filePath = join(recordingsDir, 'permanent.mp4'); | ||
| writeFileSync(filePath, Buffer.alloc(256, 0)); | ||
|
|
||
| const permanent = createFileBackedAttachment({ | ||
| filename: 'permanent.mp4', | ||
| mimeType: 'video/mp4', | ||
| sizeBytes: 256, | ||
| filePath, | ||
| // No expiresAt — should never be cleaned | ||
| }); | ||
|
|
||
| const result = runCleanupPass(); | ||
|
|
||
| expect(result.cleaned).toBe(0); | ||
| expect(existsSync(filePath)).toBe(true); | ||
| expect(getAttachmentById(permanent.id)).not.toBeNull(); | ||
| }); | ||
| }); |
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
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.