fix(memory): bound session export timestamps

This commit is contained in:
Peter Steinberger
2026-05-30 18:20:56 -04:00
parent b26f89213e
commit 66a4410095
2 changed files with 20 additions and 1 deletions

View File

@@ -295,4 +295,22 @@ describe("buildSessionEntry", () => {
expect(entry.content).toBe("Assistant: User-facing summary.\nUser: Actual user follow-up.");
expect(entry.lineMap).toStrictEqual([2, 3]);
});
it("drops Date-invalid numeric message timestamps", async () => {
const jsonlLines = [
JSON.stringify({
type: "message",
message: {
role: "user",
content: "Hello",
timestamp: 8_640_000_000_000_001,
},
}),
];
const filePath = path.join(tmpDir, "invalid-timestamp-session.jsonl");
fsSync.writeFileSync(filePath, jsonlLines.join("\n"));
const entry = requireSessionEntry(await buildSessionEntry(filePath));
expect(entry.messageTimestampsMs).toStrictEqual([0]);
});
});

View File

@@ -28,6 +28,7 @@ const DREAMING_NARRATIVE_RUN_PREFIX = "dreaming-narrative-";
// This limit applies to content only; the role label adds up to 11 chars.
const SESSION_EXPORT_CONTENT_WRAP_CHARS = 800;
const SESSION_ENTRY_PARSE_YIELD_LINES = 250;
const MAX_DATE_TIMESTAMP_MS = 8_640_000_000_000_000;
const DIRECT_CRON_PROMPT_RE = /^\[cron:[^\]]+\]\s*/;
export type SessionFileEntry = {
@@ -509,7 +510,7 @@ function parseSessionTimestampMs(
for (const value of candidates) {
if (typeof value === "number" && Number.isFinite(value)) {
const ms = value > 0 && value < 1e11 ? value * 1000 : value;
if (Number.isFinite(ms) && ms > 0) {
if (Number.isFinite(ms) && ms > 0 && ms <= MAX_DATE_TIMESTAMP_MS) {
return ms;
}
}