Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **fix(db):** Add `conversation_turn_nodes` and orphaned `agentic_conversations` to the auto-cleanup cycle under the existing `retention.callLogs` window, so identity nodes whose call-log content has already been purged no longer accumulate without bound in `storage.sqlite` (#12548 — thanks @pacocartones)
2 changes: 2 additions & 0 deletions src/app/api/settings/purge-usage-history/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export async function POST(request: Request) {
deletedRoutingDecisions: result.deletedRoutingDecisions,
deletedQuotaConsumption: result.deletedQuotaConsumption,
deletedTokenLedger: result.deletedTokenLedger,
deletedConversationTurnNodes: result.deletedConversationTurnNodes,
deletedAgenticConversations: result.deletedAgenticConversations,
errors: result.errors,
},
{ status: result.errors > 0 ? 500 : 200 }
Expand Down
128 changes: 125 additions & 3 deletions src/lib/db/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
deleteAllFromTable,
deleteCallLogArtifacts,
deleteFromTableBefore,
deleteFromTableBeforeInBatches,
tableExists,
type DeleteByPeriodTarget,
} from "./cleanup/usagePurge";
Expand Down Expand Up @@ -430,6 +431,103 @@ export async function cleanupCcrBlocks(): Promise<CleanupResult> {
return result;
}

/**
* Clean up conversation_turn_nodes older than the call-log retention window (#12453).
*
* The nodes are identity-only: the transcript view resolves each turn's display
* content from the call_logs row `last_correlation_id` points at. Once
* cleanupCallLogs purges that row the node can never render again, so the two
* tables share the dashboard database setting `retention.callLogs` instead of
* a knob of their own; `CALL_LOG_RETENTION_DAYS` configures the separate
* compliance cleanup path and does not override this window. Deleting an old
* node only affects reconnect anchors: a conversation resumed after the window
* mints a new id, which is already the documented anchor-miss behavior of
* resolveConversationId. `last_seen_at` has no index (migration 156), so
* each DELETE is a table scan. Bounded batches yield between writes so an
* existing large table cannot park the event loop for the whole cleanup pass.
*/
export async function cleanupConversationTurnNodes(): Promise<CleanupResult> {
const retention = getRetentionSettings();

const retentionDays = retention.callLogs;
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
const cutoffISO = cutoffDate.toISOString();

const result: CleanupResult = { deleted: 0, errors: 0 };

try {
result.deleted = await deleteFromTableBeforeInBatches(
{ table: "conversation_turn_nodes", column: "last_seen_at", cutoff: "iso" },
cutoffISO
);

console.log(
`[Cleanup] Deleted ${result.deleted} conversation_turn_nodes older than ${retentionDays} days`
);
} catch (err: unknown) {
console.error("[Cleanup] Error cleaning conversation_turn_nodes:", err);
result.errors++;
}

return result;
}

/**
* Sweep agentic_conversations left without any conversation_turn_nodes (#12453).
*
* Runs after cleanupConversationTurnNodes so a root whose whole chain just
* expired goes in the same pass. The indexed `last_seen_at` predicate bounds
* the NOT EXISTS probe to roots that are already past the retention window.
* Deletion is batched for the same event-loop fairness guarantee as the
* preceding node cleanup.
*/
export async function cleanupAgenticConversations(): Promise<CleanupResult> {
const db = getDbInstance();
const retention = getRetentionSettings();

const retentionDays = retention.callLogs;
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
const cutoffISO = cutoffDate.toISOString();

const result: CleanupResult = { deleted: 0, errors: 0 };

try {
if (!tableExists("agentic_conversations") || !tableExists("conversation_turn_nodes")) {
return result;
}

const stmt = db.prepare(
`DELETE FROM agentic_conversations
WHERE rowid IN (
SELECT rowid FROM agentic_conversations
WHERE last_seen_at < ?
AND NOT EXISTS (
SELECT 1 FROM conversation_turn_nodes n
WHERE n.conversation_id = agentic_conversations.id
)
LIMIT 10000
)`
);
while (true) {
const batch = stmt.run(cutoffISO).changes;
result.deleted += batch;
if (batch < 10_000) break;
await new Promise<void>((resolve) => setImmediate(resolve));
}

console.log(
`[Cleanup] Deleted ${result.deleted} orphaned agentic_conversations older than ${retentionDays} days`
);
} catch (err: unknown) {
console.error("[Cleanup] Error cleaning agentic_conversations:", err);
result.errors++;
}

return result;
}

/**
* Run all cleanup functions if auto-cleanup is enabled.
*/
Expand Down Expand Up @@ -463,6 +561,8 @@ export async function runAutoCleanup(): Promise<{
compressionRunTelemetry: await cleanupCompressionRunTelemetry(),
proxyLogs: await cleanupProxyLogs(),
ccrBlocks: await cleanupCcrBlocks(),
conversationTurnNodes: await cleanupConversationTurnNodes(),
agenticConversations: await cleanupAgenticConversations(),
};

const totalDeleted = Object.values(results).reduce((sum, r) => sum + r.deleted, 0);
Expand Down Expand Up @@ -588,6 +688,8 @@ export interface ResetUsageHistoryResult extends CleanupResult {
deletedRoutingDecisions: number;
deletedQuotaConsumption: number;
deletedTokenLedger: number;
deletedConversationTurnNodes: number;
deletedAgenticConversations: number;
}

function isResetUsageHistoryPeriod(period: string): period is ResetUsageHistoryPeriod {
Expand All @@ -604,10 +706,13 @@ function isResetUsageHistoryPeriod(period: string): period is ResetUsageHistoryP
* first, since the whole point is to wipe the data the user selected.
*
* @param period - One of {@link RESET_USAGE_HISTORY_PERIODS}. `"all"` wipes
* every row in all three tables; any other value deletes rows strictly
* older than `now - period`. Throws on an invalid period.
* every reset target, including conversation identity metadata; any other
* value deletes only time-scoped usage/log rows older than `now - period`.
* Throws on an invalid period.
*/
const RESET_TARGETS: Array<DeleteByPeriodTarget & { resultKey: keyof ResetUsageHistoryResult }> = [
const RESET_TARGETS: Array<
DeleteByPeriodTarget & { resultKey: keyof ResetUsageHistoryResult; allOnly?: boolean }
> = [
{ table: "usage_history", column: "timestamp", cutoff: "iso", resultKey: "deletedUsageHistory" },
{
table: "daily_usage_summary",
Expand Down Expand Up @@ -660,6 +765,20 @@ const RESET_TARGETS: Array<DeleteByPeriodTarget & { resultKey: keyof ResetUsageH
resultKey: "deletedQuotaConsumption",
},
{ table: "token_ledger", column: "created_at", cutoff: "iso", resultKey: "deletedTokenLedger" },
{
table: "conversation_turn_nodes",
column: "last_seen_at",
cutoff: "iso",
resultKey: "deletedConversationTurnNodes",
allOnly: true,
},
{
table: "agentic_conversations",
column: "last_seen_at",
cutoff: "iso",
resultKey: "deletedAgenticConversations",
allOnly: true,
},
];

export async function resetUsageHistory(period: string): Promise<ResetUsageHistoryResult> {
Expand All @@ -684,6 +803,8 @@ export async function resetUsageHistory(period: string): Promise<ResetUsageHisto
deletedRoutingDecisions: 0,
deletedQuotaConsumption: 0,
deletedTokenLedger: 0,
deletedConversationTurnNodes: 0,
deletedAgenticConversations: 0,
deletedArtifacts: 0,
errors: 0,
};
Expand All @@ -702,6 +823,7 @@ export async function resetUsageHistory(period: string): Promise<ResetUsageHisto
const cutoffIso = new Date(Date.now() - RESET_USAGE_HISTORY_PERIOD_MS[period]).toISOString();
artifactsToDelete = collectCallLogArtifactsBefore(cutoffIso);
for (const target of RESET_TARGETS) {
if (target.allOnly) continue;
(result[target.resultKey] as number) = deleteFromTableBefore(target, cutoffIso);
}
});
Expand Down
61 changes: 44 additions & 17 deletions src/lib/db/cleanup/usagePurge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ export type DeleteByPeriodTarget = {
cutoff: "iso" | "date" | "dateHour" | "epochMs" | "epochSeconds";
};

const DELETE_BATCH_SIZE = 10_000;

function cutoffValue(target: DeleteByPeriodTarget, cutoffIso: string): string | number {
switch (target.cutoff) {
case "date":
return cutoffIso.slice(0, 10);
case "dateHour":
return `${cutoffIso.slice(0, 10)} ${cutoffIso.slice(11, 13)}:00:00`;
case "epochMs":
return new Date(cutoffIso).getTime();
case "epochSeconds":
return Math.floor(new Date(cutoffIso).getTime() / 1000);
case "iso":
default:
return cutoffIso;
}
}

export function tableExists(table: string): boolean {
const row = getDbInstance()
.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?")
Expand All @@ -31,25 +49,34 @@ export function deleteAllFromTable(table: string): number {
export function deleteFromTableBefore(target: DeleteByPeriodTarget, cutoffIso: string): number {
if (!tableExists(target.table)) return 0;

const cutoff = (() => {
switch (target.cutoff) {
case "date":
return cutoffIso.slice(0, 10);
case "dateHour":
return `${cutoffIso.slice(0, 10)} ${cutoffIso.slice(11, 13)}:00:00`;
case "epochMs":
return new Date(cutoffIso).getTime();
case "epochSeconds":
return Math.floor(new Date(cutoffIso).getTime() / 1000);
case "iso":
default:
return cutoffIso;
}
})();

return getDbInstance()
.prepare(`DELETE FROM ${target.table} WHERE ${target.column} < ?`)
.run(cutoff).changes;
.run(cutoffValue(target, cutoffIso)).changes;
}

export async function deleteFromTableBeforeInBatches(
target: DeleteByPeriodTarget,
cutoffIso: string
): Promise<number> {
if (!tableExists(target.table)) return 0;

const statement = getDbInstance().prepare(
`DELETE FROM ${target.table}
WHERE rowid IN (
SELECT rowid FROM ${target.table}
WHERE ${target.column} < ?
LIMIT ?
)`
);
const cutoff = cutoffValue(target, cutoffIso);
let deleted = 0;

while (true) {
const batch = statement.run(cutoff, DELETE_BATCH_SIZE).changes;
deleted += batch;
if (batch < DELETE_BATCH_SIZE) return deleted;
await new Promise<void>((resolve) => setImmediate(resolve));
}
}

export function collectCallLogArtifactsBefore(cutoffIso: string): string[] {
Expand Down
Loading
Loading