fix: stable secondary sort for equal-timestamp session fallback#60
Merged
Conversation
When two active sessions have byte-identical `last_updated`, `last_entry`, and `created` fields, the most-recent-active fallback in `findActiveSessionForBranch` previously resolved ties via `readdirSync` collection order, which is filesystem- and platform-dependent. Add an alphabetical `filePath` tiebreaker so the comparator is fully deterministic. Session filenames already encode their creation timestamp (`YYYYMMDD-HHMMSS-…md`), so the secondary key still favors recency in the realistic case while removing the non-determinism.
…path) Codex review on PR #60 caught that `pickCandidate` — the sorter shared by the direct branch-match path in `findActiveSessionForBranch` — had the same non-determinism bug as the no-match most-recent-active fallback: candidates with identical effective timestamps (`last_updated` || `last_entry`) ordered by `readdirSync` collection order, which is filesystem-/platform-dependent. - Add `filePath.localeCompare` as the tertiary key in `pickCandidate`, after status priority and effective timestamp. - Tighten the most-recent-active comparator comment to match actual behavior (single effective timestamp with documented fallbacks, not "every timestamp field"). - Add a branch-match tie test mirroring the existing no-match tie test: asserts repeated calls return the alphabetically-first session.
levifig
added a commit
that referenced
this pull request
May 31, 2026
Release v2.0.0-dev.49 covering the deterministic session-fallback tiebreaker fix from PR #60.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up from Codex review on PR #55 (SPEC-042 Track B). The most-recent-active branch-fallback comparator in `findActiveSessionForBranch` used `last_updated || last_entry || created || "0"` with `localeCompare`, then picked index 0. When two active sessions had byte-identical values for all three timestamp fields, the result depended on `readdirSync` collection order — filesystem-dependent and non-deterministic across platforms.
Fix
Add an alphabetical `filePath` tiebreaker to the comparator. Session filenames already encode their creation timestamp (`YYYYMMDD-HHMMSS-…md`), so the secondary key still favors recency in the realistic case while removing the non-determinism.
8-line source change in `cli/lib/session/find.ts`. The SQLite refactor (`994f1740`) did not touch this file; `findActiveSessionForBranch` remains actively imported by `cli/commands/session.ts` and exercised by the e2e session-routing tests.
Test plan
Out of scope