Skip to content

fix: preserve session createdAt/lastActivityAt across a daemon restart - #306

Merged
saucam merged 2 commits into
mainfrom
fix/preserve-session-timestamps-on-resume
Aug 29, 2026
Merged

fix: preserve session createdAt/lastActivityAt across a daemon restart#306
saucam merged 2 commits into
mainfrom
fix/preserve-session-timestamps-on-resume

Conversation

@saucam

@saucam saucam commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Session ordering was not surviving restarts. Root cause is two unconditional lines in the Session constructor:

this.createdAt = new Date().toISOString();
this.#lastActivityAt = this.createdAt;

They run on resume too, so every restart re-dates every session to the restart moment.

Two consequences

  • Age resets. A session created weeks ago reports as brand new.
  • Ordering collapses. The session list's recency key is lastActivityAt ?? createdAt, so after a restart every session ties on the same instant — and the attention ordering from feat: order the session list by attention, not by when you created it #300 falls back to insertion order, which is precisely what it was built to replace.

The data was already there

TranscriptMeta persists both fields, and they were already being read: SessionManager.resumeSortKey sorts the resume pass by meta.lastActivityAt.

So the manager iterated resume in the correct order, and each Session constructor then overwrote the timestamps a moment later. The right values were on disk, read, and discarded.

The fix

  1. SessionCreateOptions gains optional createdAt / lastActivityAt
  2. Constructor prefers them: opts.createdAt ?? new Date().toISOString()
  3. The resume call site passes meta.createdAt / meta.lastActivityAt

New sessions are unaffected — no opts means stamped fresh, with activity falling back to creation.

One deliberate edge case: meta written before lastActivityAt existed degrades to createdAt, not to now. Falling back to now would sort the most ancient session as the most recently active one — the inverse of the intent, and worse than the bug being fixed.

Verification

lint + typecheck clean, 2386 pass / 0 fail. Four new tests: both timestamps preserved on resume, both stamped fresh for a new session, the partial-meta fallback, and a simulated restart confirming two sessions stay distinguishable by recency (the actual regression).

🤖 Generated with Claude Code

Both timestamps were stamped unconditionally in the Session constructor, so
every restart re-dated every resumed session to the restart moment:

  this.createdAt = new Date().toISOString();
  this.#lastActivityAt = this.createdAt;

Two consequences. A weeks-old session reported as brand new. And because the
session list's recency key is `lastActivityAt ?? createdAt`, every session tied
on the same instant — so the attention ordering added in #300 collapsed back to
insertion order on the first restart, which is exactly what it was built to
replace.

The values were already persisted in TranscriptMeta (createdAt,
lastActivityAt) and already read: SessionManager's `resumeSortKey` sorts the
resume pass by `meta.lastActivityAt`. So the manager iterated in the right
order and each Session then overwrote the timestamps a moment later — the
correct data was on disk, read, and discarded.

SessionCreateOptions now carries both as optional fields, the constructor
prefers them over `now`, and the resume call site passes the meta values. A new
session is unaffected (no opts -> stamped fresh, activity falls back to
creation), and meta written before `lastActivityAt` existed degrades to
`createdAt` rather than to `now` — an ancient session must not sort as the most
recently active one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@saucam
saucam merged commit 213205e into main Aug 29, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants