fix: preserve Grok session metadata when history exceeds retention limits

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-06 11:12:17 -05:00
1 parent 797e578452
commit 31da0558d1
4 files changed
+55 -8

No files matched your search

+12 -5
View File
@@ -10,6 +10,7 @@ import type { StoredSecretVersionMaterial } from "../secrets/types.js";
const MAX_BYTES = 16 * 1024 * 1024;
const MAX_ENTRIES = 5000;
const FIELD = "paperclipGrokHistory";
class GrokHistoryLimitError extends Error {}
type Entry = { name: string; bytes: string };
function validName(name: string) {
@@ -59,7 +60,7 @@ export async function prepareGrokSessionHistory(db: Db, input: {
let count = 0;
async function collect(relative = "") {
for (const entry of await readdir(path.join(root, relative), { withFileTypes: true })) {
if (++count > MAX_ENTRIES) throw new Error("Grok history exceeds its entry bound");
if (++count > MAX_ENTRIES) throw new GrokHistoryLimitError("Grok history exceeds its entry bound");
const name = relative ? `${relative}/${entry.name}` : entry.name;
if (!validName(name) || entry.isSymbolicLink()) continue;
const source = path.join(root, name);
@@ -69,19 +70,25 @@ export async function prepareGrokSessionHistory(db: Db, input: {
try {
const stat = await file.stat();
if (!stat.isFile() || stat.nlink !== 1) continue;
if (size + stat.size > MAX_BYTES) throw new Error("Grok history exceeds its storage bound");
if (size + stat.size > MAX_BYTES) throw new GrokHistoryLimitError("Grok history exceeds its storage bound");
const bytes = await file.readFile();
size += bytes.length;
if (size > MAX_BYTES) throw new Error("Grok history exceeds its storage bound");
if (size > MAX_BYTES) throw new GrokHistoryLimitError("Grok history exceeds its storage bound");
entries.push({ name, bytes: bytes.toString("base64") });
} finally { await file.close(); }
}
}
await collect();
try { await collect(); }
catch (error) {
if (!(error instanceof GrokHistoryLimitError)) throw error;
// Save provider metadata normally, but explicitly discard the unusable
// transcript. The adapter starts with a fresh task handoff next time.
return { [FIELD]: null, paperclipGrokHistoryStatus: "fresh_session_required" };
}
if (entries.length === 0) return;
const prepared = await localEncryptedProvider.createVersion({ value: JSON.stringify({ scope, entries }) });
// The heartbeat writes this with the provider session's checkpoint before
// releasing execution ownership. Cleanup must not race a subsequent resume.
return { [FIELD]: { scope, material: prepared.material } };
return { [FIELD]: { scope, material: prepared.material }, paperclipGrokHistoryStatus: "retained" };
};
}
+10 -1
View File
@@ -26064,6 +26064,7 @@ export function heartbeatService(
expectedRunId: finalizedRun.id,
});
} else {
const historyCheckpoint = await managedAiRuntime?.checkpointSessionHistory?.();
await upsertTaskSession({
companyId: agent.companyId,
agentId: agent.id,
@@ -26075,12 +26076,20 @@ export function heartbeatService(
configuredModel,
sessionConfigMetadata,
),
...await managedAiRuntime?.checkpointSessionHistory?.(),
...historyCheckpoint,
},
sessionDisplayId: nextSessionState.displayId,
lastRunId: finalizedRun.id,
lastError: runErrorMessage,
});
if (historyCheckpoint?.paperclipGrokHistoryStatus === "fresh_session_required") {
await appendRunEvent(finalizedRun, {
eventType: "session_history_limit",
stream: "system",
level: "warn",
message: "Grok history exceeded its retention limit. The next run will start a fresh session with the task handoff.",
}).catch((error) => logger.warn({ error, runId: finalizedRun.id }, "Could not record Grok history limit"));
}
}
}
}