Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: expire completed agent operations
Richie McIlroydc8f876324be
diff --git a/apps/web/__tests__/e2e/agent-local-e2e.test.ts b/apps/web/__tests__/e2e/agent-local-e2e.test.tsindex 216f21324..f33482564 100644--- a/apps/web/__tests__/e2e/agent-local-e2e.test.ts+++ b/apps/web/__tests__/e2e/agent-local-e2e.test.ts@@ -1675,6 +1675,21 @@ agentE2e("Cap agent local Docker E2E", () => {"4".repeat(64),],);+ await connection.execute(+ `INSERT INTO agent_api_operations+ (id, userId, kind, resourceId, state, payload, updatedAt, completedAt)+ VALUES+ ('op_e2e_old', ?, 'duplicate_cap', ?, 'succeeded', ?, DATE_SUB(NOW(), INTERVAL 31 DAY), DATE_SUB(NOW(), INTERVAL 31 DAY)),+ ('op_e2e_run', ?, 'duplicate_cap', ?, 'running', ?, DATE_SUB(NOW(), INTERVAL 31 DAY), NULL)`,+ [+ userId,+ videoId,+ JSON.stringify({ videoId }),+ userId,+ videoId,+ JSON.stringify({ videoId }),+ ],+ );await connection.execute(`INSERT INTO agent_api_keys(id, userId, tokenHash, name, scopes, expiresAt, revokedAt)@@ -1719,24 +1734,31 @@ agentE2e("Cap agent local Docker E2E", () => {const deleted = asObject(cleanupBody.deleted);expect(Number(deleted.authorizationCodes)).toBeGreaterThanOrEqual(1);expect(Number(deleted.idempotencyRecords)).toBeGreaterThanOrEqual(1);+ expect(Number(deleted.operations)).toBeGreaterThanOrEqual(1);expect(Number(deleted.accessTokens)).toBeGreaterThanOrEqual(2);const [remaining] = await connection.execute<RowDataPacket[]>(`SELECT(SELECT COUNT(*) FROM agent_api_authorization_codes WHERE id = 'cod_e2e_live') AS liveCodes,(SELECT COUNT(*) FROM agent_api_idempotency WHERE id = 'ide_e2e_live') AS liveIdempotency,+ (SELECT COUNT(*) FROM agent_api_operations WHERE id = 'op_e2e_ready') AS recentOperations,+ (SELECT COUNT(*) FROM agent_api_operations WHERE id = 'op_e2e_run') AS runningOperations,(SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_live2') AS liveKeys,(SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_rvnew') AS recentRevokedKeys,(SELECT COUNT(*) FROM agent_api_authorization_codes WHERE id = 'cod_e2e_expired') AS expiredCodes,(SELECT COUNT(*) FROM agent_api_idempotency WHERE id = 'ide_e2e_expired') AS expiredIdempotency,+ (SELECT COUNT(*) FROM agent_api_operations WHERE id = 'op_e2e_old') AS oldOperations,(SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_old') AS expiredKeys,(SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_rvold') AS oldRevokedKeys`,);expect(Number(remaining[0]?.liveCodes)).toBe(1);expect(Number(remaining[0]?.liveIdempotency)).toBe(1);+ expect(Number(remaining[0]?.recentOperations)).toBe(1);+ expect(Number(remaining[0]?.runningOperations)).toBe(1);expect(Number(remaining[0]?.liveKeys)).toBe(1);expect(Number(remaining[0]?.recentRevokedKeys)).toBe(1);expect(Number(remaining[0]?.expiredCodes)).toBe(0);expect(Number(remaining[0]?.expiredIdempotency)).toBe(0);+ expect(Number(remaining[0]?.oldOperations)).toBe(0);expect(Number(remaining[0]?.expiredKeys)).toBe(0);expect(Number(remaining[0]?.oldRevokedKeys)).toBe(0);});diff --git a/apps/web/lib/agent-api-cleanup.ts b/apps/web/lib/agent-api-cleanup.tsindex 907526e0a..2928b0c24 100644--- a/apps/web/lib/agent-api-cleanup.ts+++ b/apps/web/lib/agent-api-cleanup.ts@@ -5,8 +5,9 @@ import {agentApiAuthorizationCodes,agentApiIdempotency,agentApiKeys,+ agentApiOperations,} from "@cap/database/schema";-import { lt, or } from "drizzle-orm";+import { and, inArray, lt, or } from "drizzle-orm";const affectedRows = (result: unknown) =>Array.isArray(result)@@ -32,6 +33,7 @@ export const cleanupExpiredAgentApiRecords = async (input?: {batchSize?: number;maxBatches?: number;keyRetentionMs?: number;+ operationRetentionMs?: number;}) => {const now = input?.now ?? new Date();const batchSize = Math.min(Math.max(input?.batchSize ?? 1_000, 1), 5_000);@@ -40,7 +42,14 @@ export const cleanupExpiredAgentApiRecords = async (input?: {input?.keyRetentionMs ?? 30 * 24 * 60 * 60 * 1_000,0,);+ const operationRetentionMs = Math.max(+ input?.operationRetentionMs ?? 30 * 24 * 60 * 60 * 1_000,+ 0,+ );const keyRetentionCutoff = new Date(now.getTime() - keyRetentionMs);+ const operationRetentionCutoff = new Date(+ now.getTime() - operationRetentionMs,+ );const database = db();const authorizationCodes = await deleteInBatches(@@ -61,6 +70,20 @@ export const cleanupExpiredAgentApiRecords = async (input?: {batchSize,maxBatches,);+ const operations = await deleteInBatches(+ () =>+ database+ .delete(agentApiOperations)+ .where(+ and(+ inArray(agentApiOperations.state, ["succeeded", "failed"]),+ lt(agentApiOperations.updatedAt, operationRetentionCutoff),+ ),+ )+ .limit(batchSize),+ batchSize,+ maxBatches,+ );const accessTokens = await deleteInBatches(() =>database@@ -76,5 +99,5 @@ export const cleanupExpiredAgentApiRecords = async (input?: {maxBatches,);- return { authorizationCodes, idempotencyRecords, accessTokens };+ return { authorizationCodes, idempotencyRecords, operations, accessTokens };};