Skip to content

fix: stable secondary sort for equal-timestamp session fallback#60

Merged
levifig merged 2 commits into
mainfrom
fix/session-fallback-stable-tiebreak
May 31, 2026
Merged

fix: stable secondary sort for equal-timestamp session fallback#60
levifig merged 2 commits into
mainfrom
fix/session-fallback-stable-tiebreak

Conversation

@levifig

@levifig levifig commented May 31, 2026

Copy link
Copy Markdown
Owner

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

  • `npm run typecheck` clean
  • `npm run test -- cli/lib/session/find.test.ts cli/lib/session/resolve.test.ts` — 47/47 pass
  • New test: two active sessions with byte-identical timestamps + lexically-distinct filenames; assert the same one returns across multiple invocations and that it's the alphabetically-first by `filePath`

Out of scope

  • Other comparators with the same pattern (`findSessionByClaudeId`, `pickCandidate`) — same risk, already filed as a separate follow-up spawn_task.
  • The branch-fallback tier itself — STRATEGY notes Tier 3 is being phased out in a future spec.

levifig added 2 commits May 31, 2026 02:08
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 levifig merged commit dcaf00a into main May 31, 2026
1 check failed
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.
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