Skip to content

fix: sessions sorted by time are deterministic, in the code and in its test - #143

Merged
endrix merged 2 commits into
mainfrom
test/session-order-clock
Nov 9, 2026
Merged

endrix merged 2 commits into
mainfrom
test/session-order-clock

Conversation

@endrix

@endrix endrix commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Main's CI went red after #142, which is changelog-only, in extension-core/test/session-manager.test.ts > getSessionsForWorkflow: expected ['Session 1', 'Session 3'], got ['Session 3', 'Session 1'].

Cause

A flaky test, older than this cleanup work. The code sorts sessions by updatedAt, most recent first, and the test's comment says so too, but the test expected insertion order. It passed only when all three sessions were created within one millisecond, because the sort then kept insertion order. On a runner where the clock ticked, the code did what its comment says and the test failed.

Fix

  • Each session is now created at its own fixed time: fake Date only, so no timers change.
  • The test expects the most recent first.
  • With distinct times the old expectation fails every time, and the new one passes every time.

Review (/code-review medium), addressed

The review confirmed the diagnosis. It also confirmed that nothing in createSession waits on a timer, that real timers come back even on failure, and that no other test depends on two calls landing in one millisecond. It found that the code itself had no tie-breaker, so two sessions created or restored in the same millisecond came back oldest first, and the chat's delete fallback took the older one as "the most recent sibling".

  • Fixed in the code: equal times now fall back to createdAt, then to the later one added.
  • New test for equal times; it fails without the tie-breaker.
  • Comment: it now says what the test needs, and the history is in the commit.

Local results

  • session-manager.test.ts: 3/3 runs green
  • extension-core: 336/336

https://claude.ai/code/session_01EToj6RJRMSSHQiqGNMU9wv

session-manager.test.ts expected ['Session 1', 'Session 3'] from a sort
by updatedAt, most recent first, as its own comment says. That held only
when all three sessions were created within one millisecond: the sort
then kept insertion order. On a runner where the clock ticked between
them, Session 3 came first and the test failed, as on main's CI after
#142 (a changelog-only change).

The test now creates each session at its own fixed time (fake Date only)
and expects the most recent first. With distinct times the old
expectation fails every time; the new one passes every time.

Claude-Session: https://claude.ai/code/session_01EToj6RJRMSSHQiqGNMU9wv
Review of #143: the sort by updatedAt had no tie-breaker, so two sessions
created (or restored) in the same millisecond came back oldest first, and
the chat's delete fallback took the older one as the most recent sibling.
Equal times now fall back to createdAt, then to the later one added. A
test pins equal times (it fails without the tie-breaker); the first
test's comment says what it needs rather than its history.

Claude-Session: https://claude.ai/code/session_01EToj6RJRMSSHQiqGNMU9wv
@endrix endrix changed the title test: sessions sorted by time are created at distinct times fix: sessions sorted by time are deterministic, in the code and in its test Nov 9, 2026
@endrix
endrix merged commit 5508095 into main Nov 9, 2026
1 check 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.

1 participant