refactor(cli): group subagent trajectory deletion and use native filesystem testing (#22890)

This commit is contained in:
Abhi
2026-03-18 10:42:15 -04:00
committed by GitHub
parent 1311e8c480
commit 81a97e78f1
3 changed files with 1081 additions and 1552 deletions

View File

@@ -252,4 +252,154 @@ describe('Session Cleanup Integration', () => {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
it('should delete subagent files and their artifacts when parent expires', async () => {
// Create a temporary directory with test sessions
const fs = await import('node:fs/promises');
const path = await import('node:path');
const os = await import('node:os');
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'gemini-test-'));
const chatsDir = path.join(tempDir, 'chats');
const logsDir = path.join(tempDir, 'logs');
const toolOutputsDir = path.join(tempDir, 'tool-outputs');
await fs.mkdir(chatsDir, { recursive: true });
await fs.mkdir(logsDir, { recursive: true });
await fs.mkdir(toolOutputsDir, { recursive: true });
const now = new Date();
const oldDate = new Date(now.getTime() - 5 * 24 * 60 * 60 * 1000); // 5 days ago
// The shortId that ties them together
const sharedShortId = 'abcdef12';
const parentSessionId = 'parent-uuid-123';
const parentFile = path.join(
chatsDir,
`${SESSION_FILE_PREFIX}2024-01-01T10-00-00-${sharedShortId}.json`,
);
await fs.writeFile(
parentFile,
JSON.stringify({
sessionId: parentSessionId,
messages: [],
startTime: oldDate.toISOString(),
lastUpdated: oldDate.toISOString(),
}),
);
const subagentSessionId = 'subagent-uuid-456';
const subagentFile = path.join(
chatsDir,
`${SESSION_FILE_PREFIX}2024-01-01T10-05-00-${sharedShortId}.json`,
);
await fs.writeFile(
subagentFile,
JSON.stringify({
sessionId: subagentSessionId,
messages: [],
startTime: oldDate.toISOString(),
lastUpdated: oldDate.toISOString(),
}),
);
const parentLogFile = path.join(
logsDir,
`session-${parentSessionId}.jsonl`,
);
await fs.writeFile(parentLogFile, '{"log": "parent"}');
const parentToolOutputsDir = path.join(
toolOutputsDir,
`session-${parentSessionId}`,
);
await fs.mkdir(parentToolOutputsDir, { recursive: true });
await fs.writeFile(
path.join(parentToolOutputsDir, 'some-output.txt'),
'data',
);
const subagentLogFile = path.join(
logsDir,
`session-${subagentSessionId}.jsonl`,
);
await fs.writeFile(subagentLogFile, '{"log": "subagent"}');
const subagentToolOutputsDir = path.join(
toolOutputsDir,
`session-${subagentSessionId}`,
);
await fs.mkdir(subagentToolOutputsDir, { recursive: true });
await fs.writeFile(
path.join(subagentToolOutputsDir, 'some-output.txt'),
'data',
);
const currentShortId = 'current1';
const currentFile = path.join(
chatsDir,
`${SESSION_FILE_PREFIX}2025-01-20T10-00-00-${currentShortId}.json`,
);
await fs.writeFile(
currentFile,
JSON.stringify({
sessionId: 'current-session',
messages: [
{
type: 'user',
content: [{ type: 'text', text: 'hello' }],
timestamp: now.toISOString(),
},
],
startTime: now.toISOString(),
lastUpdated: now.toISOString(),
}),
);
// Configure test
const config: Config = {
storage: {
getProjectTempDir: () => tempDir,
},
getSessionId: () => 'current-session', // Mock CLI instance ID
getDebugMode: () => false,
initialize: async () => undefined,
} as unknown as Config;
const settings: Settings = {
general: {
sessionRetention: {
enabled: true,
maxAge: '1d', // Expire things older than 1 day
},
},
};
try {
const result = await cleanupExpiredSessions(config, settings);
// Verify the cleanup result object
// It scanned 3 files. It should delete 2 (parent + subagent), and keep 1 (current)
expect(result.disabled).toBe(false);
expect(result.scanned).toBe(3);
expect(result.deleted).toBe(2);
expect(result.skipped).toBe(1);
// Verify on-disk file states
const chats = await fs.readdir(chatsDir);
expect(chats).toHaveLength(1);
expect(chats).toContain(
`${SESSION_FILE_PREFIX}2025-01-20T10-00-00-${currentShortId}.json`,
); // Only current is left
const logs = await fs.readdir(logsDir);
expect(logs).toHaveLength(0); // Both parent and subagent logs were deleted
const tools = await fs.readdir(toolOutputsDir);
expect(tools).toHaveLength(0); // Both parent and subagent tool output dirs were deleted
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
});