fix(anthropic): guard invalid timestamps in history imports (#110536)

* fix(anthropic): guard invalid timestamps in history imports

* fix(anthropic): reject pre-epoch history timestamps

* fix(anthropic): preserve valid transcript dates

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
wahaha1223
2026-07-18 18:45:10 -07:00
committed by GitHub
co-authored by Peter Steinberger
parent 7e1aca92b4
commit ecc56749e1
2 changed files with 34 additions and 1 deletions
@@ -20,6 +20,38 @@ vi.mock("openclaw/plugin-sdk/session-transcript-runtime", () => ({
}));
describe("importClaudeHistory", () => {
it("falls back for invalid timestamps while preserving valid pre-epoch dates", async () => {
appended.length = 0;
const fallbackTimestamp = new Date("2026-07-18T12:00:00.000Z").getTime();
vi.useFakeTimers();
vi.setSystemTime(fallbackTimestamp);
try {
await importClaudeHistory({
items: [
{ type: "userMessage", text: "invalid", timestamp: "not-a-date", uuid: "u-1" },
{
type: "userMessage",
text: "pre-epoch",
timestamp: "1969-12-31T23:59:59.000Z",
uuid: "u-2",
},
],
threadId: "thread-1",
sessionFile: "/tmp/unused.jsonl",
sessionId: "session-1",
sessionKey: "agent:main:catalog-adopt",
agentId: "main",
config: {} as OpenClawConfig,
});
} finally {
vi.useRealTimers();
}
expect(appended).toHaveLength(2);
expect(appended.map((message) => message.timestamp)).toEqual([-1_000, fallbackTimestamp + 1]);
expect(JSON.stringify(appended)).not.toContain('"timestamp":null');
});
it("tags imported native user rows so self-echo provenance excludes them", async () => {
appended.length = 0;
await importClaudeHistory({
@@ -8,7 +8,8 @@ function importedClaudeMessage(
item: ClaudeTranscriptItem,
fallbackTimestamp: number,
): AgentMessage {
const timestamp = item.timestamp ? Date.parse(item.timestamp) : fallbackTimestamp;
const parsedTimestamp = item.timestamp ? Date.parse(item.timestamp) : Number.NaN;
const timestamp = Number.isFinite(parsedTimestamp) ? parsedTimestamp : fallbackTimestamp;
const text = item.text?.trim() || "[Unsupported Claude transcript item]";
if (item.type === "userMessage") {
// Imported native rows are not OpenClaw-authored; mirrorOrigin excludes them