From 53472a92c2ee241529eb6246b017d68c8a25bb89 Mon Sep 17 00:00:00 2001 From: Turtle Date: Mon, 9 Nov 2026 15:11:11 +0800 Subject: [PATCH] fix(session-persistence-jsonl): detach listed headers from the listing cache Document why revision-guarded header reuse is safe, return a copy of each cached header, and cover cached unreadable logs and exact re-read counts. Refs #8166 --- .../session-persistence-jsonl/src/index.ts | 9 ++++++- .../tests/jsonl.spec.ts | 24 +++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/session/session-persistence-jsonl/src/index.ts b/packages/session/session-persistence-jsonl/src/index.ts index bf805bce1a..02234f17f4 100644 --- a/packages/session/session-persistence-jsonl/src/index.ts +++ b/packages/session/session-persistence-jsonl/src/index.ts @@ -336,6 +336,12 @@ class JsonlSessionPersistence extends SessionPersistence { * source path and guarded by the stat-derived revision, so a listing reads * only the first frame of logs whose file changed. Each complete listing * replaces the map, which drops deleted paths. + * + * Reuse relies on this backend never rewriting a generation's header line + * in place: publication creates a new file, append grows it, and repair + * truncates its tail, so every content change moves the revision. The + * revision is file metadata, not a byte checksum. A reused entry also + * reuses the path-identity check its first read passed. */ private listedHeaders = new Map() /** One joinable decode/migration operation per selected historical Session file revision. */ @@ -562,7 +568,8 @@ class JsonlSessionPersistence extends SessionPersistence { const { identity } = artifact listed.add(artifact.header.id) snapshots.push({ - header: artifact.header, + // Detached from the header the listing cache retains. + header: { ...artifact.header }, formatStatus: artifact.formatStatus, revision: artifact.sourceVersion < SESSION_FORMAT_VERSION ? SessionPersistenceRevision(`${fileRevision(identity)}:${corpusRevision}`) diff --git a/packages/session/session-persistence-jsonl/tests/jsonl.spec.ts b/packages/session/session-persistence-jsonl/tests/jsonl.spec.ts index b64306806a..4632e3f7dd 100644 --- a/packages/session/session-persistence-jsonl/tests/jsonl.spec.ts +++ b/packages/session/session-persistence-jsonl/tests/jsonl.spec.ts @@ -2641,11 +2641,18 @@ describe('JsonlSessionPersistence: edge cases', () => { const kept = meta('memo-kept', '/memo') const changed = meta('memo-changed', '/memo') const removed = meta('memo-removed', '/memo') + const corrupt = meta('memo-corrupt', '/memo') for (const m of [kept, changed, removed]) await writeLog(ctx.sessionPersistence, m, oneTurnLog()) + const corruptPath = rawLogPath(root, corrupt.cwd, corrupt.id) + await mkdir(dirname(corruptPath), { recursive: true }) + await writeFile(corruptPath, 'not json\n') const created = (ids: readonly { header: SessionHeader }[]) => Object.fromEntries(ids.map(({ header }) => [header.id, header.createdAt])) - expect(created(await ctx.sessionPersistence.list())) + const first = await ctx.sessionPersistence.list() + expect(created(first)) .toEqual({ [kept.id]: kept.createdAt, [changed.id]: changed.createdAt, [removed.id]: removed.createdAt }) + // A caller's copy is detached from the headers later listings reuse. + mutableHeader(first.find(({ header }) => header.id === kept.id)!.header).createdAt = 0 const path = rawLogPath(root, changed.cwd, changed.id) const probe = await open(path, 'r') @@ -2656,17 +2663,24 @@ describe('JsonlSessionPersistence: edge cases', () => { .toEqual({ [kept.id]: kept.createdAt, [changed.id]: changed.createdAt, [removed.id]: removed.createdAt }) expect(reads).not.toHaveBeenCalled() + // An explicit mtime keeps each revision distinct on coarse-timestamp filesystems. + const later = new Date(Date.now() + 60_000) await writeFile(path, [ JSON.stringify(toHeaderLine({ ...changed, createdAt: changed.createdAt + 1 })), ...oneTurnLog().map(event => JSON.stringify(event)), '', ].join('\n')) - // An explicit mtime keeps the revision distinct on coarse-timestamp filesystems. - await utimes(path, new Date(), new Date(Date.now() + 60_000)) + await utimes(path, later, later) + await writeFile(corruptPath, [ + JSON.stringify(toHeaderLine(corrupt)), + ...oneTurnLog().map(event => JSON.stringify(event)), + '', + ].join('\n')) + await utimes(corruptPath, later, later) await rm(sessionDir(root, removed.cwd, removed.id), { recursive: true }) expect(created(await ctx.sessionPersistence.list())) - .toEqual({ [kept.id]: kept.createdAt, [changed.id]: changed.createdAt + 1 }) - expect(reads).toHaveBeenCalled() + .toEqual({ [kept.id]: kept.createdAt, [changed.id]: changed.createdAt + 1, [corrupt.id]: corrupt.createdAt }) + expect(reads).toHaveBeenCalledTimes(2) reads.mockRestore() })