Repository navigation
fix(switch): stamp the incoming store when two stores share one mtime - #175
Conversation
Claude Code re-reads its login only when the mtime of what .credentials.json resolves to differs from the one it memoized. The per-session swap stamps the incoming store before it repoints; the global relinks (force_link_profile_credentials and the guarded link_profile_credentials) did not. A switch between two stores with the same mtime therefore moved the link, reported success, and left running sessions on the account they had been switched off. Both relinks now call runtime::stamp_store, receipt included, when the live path resolves to a different store with an equal mtime. The call runs after the Keychain mirror and before the repoint, so a failure moves nothing. As a swap's SwapPlan does, it runs load_profile first, so a legacy .pending sidecar is settled before a stamp that lands without its receipt (a truncating filesystem) could discard it; a test poses that case with the existing coarse-mtime override. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The three tests that use them are #[cfg(unix)], so on Windows the helpers were dead code and -D warnings failed the build. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
uwuclxdy
left a comment
There was a problem hiding this comment.
The memo check is the right cause and the stamp fixes the equal-mtime case: both visibility tests fail here with the helper off, and the .pending one fails without the load. Failing the switch on a failed stamp or load is right, keep it.
Optional: the four relinks that drop their error (let _ = link_profile_credentials(..) at daemon and TUI start) would skip silently on a failed stamp once every switch stamps. A log line there would cover it.
| return Ok(()); | ||
| } | ||
| match (mtime(&live), mtime(&incoming)) { | ||
| (Some(memoized), Some(next)) if memoized == next => { |
There was a problem hiding this comment.
major: this compares the two stores as they are now, while a session compares against the mtime it saw at its last request. a session idle across a write to the outgoing store still holds the old value, so a switch onto a store carrying that value stays invisible even though the two current mtimes differ. stamp on every switch onto a different store instead, as the per-session swap does, and keep the same-store and first-link exits.
| /// is the swap's own ([`crate::runtime::stamp_store`], receipt included), keyed | ||
| /// the way a swap keys it: the `load_profile` first lands a legacy `.pending` | ||
| /// sidecar, which a stamp ahead of it would discard. Runs after the Keychain | ||
| /// mirror and before the repoint, so a failure moves nothing. |
There was a problem hiding this comment.
minor: on macOS the Keychain item has already moved when the stamp or the load fails. say the failure leaves the link unmoved.
| assert_eq!( | ||
| live_mtime(), | ||
| earlier, | ||
| "an mtime that already differs is a real write time and must be kept" |
There was a problem hiding this comment.
minor: with a stamp on every switch this leg becomes the positive case. assert the live mtime moved and effective_write_time(&b) still reads earlier.
|
|
||
| #[cfg(unix)] | ||
| #[test] | ||
| fn guarded_relink_between_equal_mtime_stores_stays_visible_to_running_sessions() { |
There was a problem hiding this comment.
nit: this covers one switch, without the switch back or the receipt assert the forcing test has. consider adding both.
| assert_ne!( | ||
| file_mtime(&link), | ||
| Some(same), | ||
| "a running session memoized a's mtime; b must not resolve to the same one" |
There was a problem hiding this comment.
nit: the load_profile above rewrites the store, so this passes with the stamp turned off. consider reading the link's mtime before that load.
The equal-mtime check compared the two stores as they are now, but a running Claude Code session compares against the mtime it saw at its last request. A session idle across a write to the outgoing store still holds the older value, so a switch onto a store carrying that value stayed invisible even though the two current mtimes differed. keep_repoint_visible now stamps on every relink onto a different store, as the per-session swap stamps every move, and keeps the same-store and first-link exits. The load_profile that settles a legacy .pending sidecar now runs only when one stands beside the incoming store. The swap refuses an unreadable profile before its own load, but the relinks also serve an account whose config.toml cannot be read (the TUI's divergence overwrite relinks one on purpose), and an unconditional load would fail them. The doc now says what a failed load or stamp leaves behind: the link unmoved, and on macOS the Keychain item already moved. Tests: the differing-mtime leg becomes the positive case and poses the idle-session story; the guarded relink test switches back and asserts both receipts; the .pending test reads the link's mtime before its own verifying load, which would otherwise move it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The daemon's and the TUI's start relinks discarded the result of link_profile_credentials. With every switch onto a different store now stamping, a failed stamp or profile load there would skip the relink with nothing said. Each of the four call sites now writes a log line naming the error and the account. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks. All of it is in
Commands run (macOS, aarch64):
Mutations, each run against five tests (the two equal-mtime tests, the differing-mtime test, the
CI: the run this push starts will likely be red for a reason outside this diff. Upstream CI's stable moved from rustc 1.99.1 to 1.100.0 today, and its clippy reports Not done, or unsure:
|
Takes the sync ledger's amended text (the three upstream commits left for the next sync, the 1.99.1 gate against upstream CI's 1.100.0) and fills in the deploy record, the live keep_unset_models confirmation, and the state of PR uwuclxdy#175, uwuclxdy#176 and uwuclxdy#180. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
upstream/mommy 9ca6de2. Lands: banked limit resets listed and spent by id from CLI and TUI (e14af42; a spend now names its id, `<name> --yes` alone is a usage error), the daemon's hourly codex reset-list read, the TUI Overview `no limit` 5h cell, cleanupPeriodDays put-back (settings-retention.json), phone pairing from a link/QR and the status.json `listen` slot, delegates pane, mascot animation, pool usage for proxy profiles, and our PR uwuclxdy#175 (3baaf8d) and uwuclxdy#176 (6e4bb60, e16b5f1) in their reviewed forms. Conflict resolutions (21 files): - src/claude.rs: keep_repoint_visible taken as upstream merged it (every relink onto a different store is stamped); the fork's RESCUE-2 archive_live_credentials kept beside it. No uwuclxdy#175 delta left. - src/runtime.rs, tests/inline/runtime.rs: upstream's files whole (stamp_store doc; a stray fork blank line and comment rewraps dropped). - src/oauth.rs, tests/inline/oauth.rs, tests/inline/claude.rs: the degrade-to-mint and equal-mtime hunks taken as upstream merged them (uwuclxdy#176/uwuclxdy#175), so the fork's 88aaabe / 40ebca4 / faad403 versions are gone; fork-only tests elsewhere in the files kept. - src/usage/scheduler.rs: the fork's kill-switch + codex_poll_due filter kept; codex_poll_due now reads upstream's CodexPolled memo (.stamp). The ROLLING_DEGRADE_LEAD_MS assert takes upstream's wording. - src/usage/mod.rs, src/daemon/mod.rs, src/tui/app.rs: upstream's import lists, with the fork's PendingSwitchTarget kept test-only and select_switch_winner imported in the daemon. - src/daemon/status_json.rs: upstream's build_codex_entries doc (with fetches_here); the fork's codex_fallback / codex_store_mtime_ms moved ABOVE that doc, which a past merge had glued onto codex_fallback. - src/main.rs: the fork's cmd_codex_proxy, cmd_switch_codex, cmd_fallback and run_migrate_codex kept; cmd_limit_reset and the old limit_reset_claude body taken as upstream's (src/limit_reset.rs). - src/completions.rs: upstream's `devices pair --no-qr` line, then the fork's `migrate-codex --dry-run` branch. - src/tui/render/overview.rs: the fork's email argument on the claude row, upstream's wave_phase_ms on the codex row. - src/tui/render/usage.rs: imports union (active_pill + spans_cells); HeaderState keeps the fork's is_active beside upstream's activity. - tests/inline/scheduler.rs: upstream's new_codex_resets_memo() plus the fork's VecDeque queue in four fixtures; codex_poll_due test on CodexPolled. - tests/inline/proxy_p6_feed.rs, tests/inline/daemon_status_json.rs: both sides (the fork's last_error/last_switch beside upstream's listen); the two wire fixtures carry `listen` after binary_removed. - wiki/Codex.md: upstream's limit-reset text; the fork's ccsbar sentence (`limit-reset <name> --yes`) dropped, no longer a valid invocation. - wiki/Daemon.md, wiki/REST-API.md: upstream's text plus the fork's no_five_hour_limit row / mentions. Compile fallout: the fork's has_email / email arguments on upstream's new overview tests, is_active / account_email on upstream's new HeaderState literals, last_error / last_switch on two LiveSignals literals; the fork's feature-coverage row names upstream's limit-reset CLI-shape test in place of the retired auto-pick test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
.agent/PROGRESS.md UPS-26 entry; SYNC.md inventory: the equal-mtime relink stamp and the rolling degrade leave the fork delta (merged upstream as uwuclxdy#175 / uwuclxdy#176), the limit-reset line names the by-id spend, the kill-switch line names upstream's new codex reset-list read, no_five_hour_limit stays fork-only. docs/ccsbar/DESIGN.md: the limit-reset client contract after upstream e14af42. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This makes a global switch reach sessions that are already running when the outgoing and incoming credential stores have the same mtime.
Cause. Claude Code re-reads its login only when the mtime of the file
~/.claude/.credentials.jsonresolves to is not equal to the one it memoized.runtime::touch_storedocuments this, and the per-session swap stamps the incoming store before it repoints. The global relinks,force_link_profile_credentialsand the guardedlink_profile_credentials, callpublish_credential_linkwith no stamp. So when the two stores carry the same mtime, the link moves, the command reports success, and every running session stays on the old account. This happens whenever both stores were last written in the same tick, or a tool or restore left them with the same timestamp.Fix.
keep_repoint_visibleinsrc/claude.rsruns in both relinks, after the Keychain mirror and beforepublish_credential_link, so a failure moves nothing. It acts only when the live path resolves (canonical::identity) to a different store whose mtime is equal to the incoming store's. A relink onto the same store, a first link, and a switch between stores whose mtimes already differ are left as they were.runtime::stamp_store, madepub(crate)with its doc updated. There is no second stamp function, and the touch receipt is written the same way, so readers going throughprofile_cache::effective_write_timestill see the store's real write time.precondition_withdocuments that a stamp ahead of a legacy.pendingsidecar's settlement discards that sidecar. The switch path reads the profile withread_profile, which does not land a.pending. So before the stamp it runsload_profilethe way a swap'sSwapPlandoes, and only in the equal-mtime branch. When the touch receipt lands, it already keeps the sidecar's adoption right, so the load matters only when the stamp lands without one: a filesystem that truncates mtimes (or a receipt write that fails). The new test poses that case with the existingwith_coarse_mtimeoverride. The load re-enters the state flock the relink already holds. A re-entrant frame takes no flock and no lock rank (lock.rsacquire_inner), so this adds no lock-order edge for any caller.No user-visible wording, keys or defaults change. The wiki already says a running session adopts the new account at its next request (Quickstart, Claude Code Plugin). This makes that true in the equal-mtime case, so I left the wiki alone.
Tests (
tests/inline/claude.rs,HomeSandbox, synthetic profiles,#[cfg(unix)]):global_switch_between_equal_mtime_stores_stays_visible_to_running_sessions: A→B→A throughforce_link_profile_credentials. The first link does not stamp, each later switch moves the live mtime, and B'seffective_write_timestill reads its pre-stamp write time (the receipt landed).guarded_relink_between_equal_mtime_stores_stays_visible_to_running_sessions: the same throughlink_profile_credentials.global_switch_leaves_a_store_alone_when_its_mtime_already_differs: a store whose mtime already differs is not stamped, and neither is a relink onto the same store.global_switch_settles_a_pending_sidecar_before_its_equal_mtime_stamp(tests/inline/runtime.rs, next toa_truncating_filesystem_gets_no_receipt_at_all, becausewith_coarse_mtimeis private toruntime): the incoming store carries acredentials.json.pendingthat ties it, and both stores share one mtime. The switch runs under the coarse-mtime override, so the stamp gets no receipt. Afterwards the store holds the sidecar's rotation (the rule adopts a tie), the sidecar is gone, the link resolves to B, and its mtime differs from the memoized one.With both
keep_repoint_visiblecalls removed, the first two fail on equal mtimes and the third passes, since it is the guard. With only theload_profileline removed, the fourth fails:left: Some("rt-pend-b"),right: Some("rt-rotated"), so the rotation is discarded. The same mutation without the coarse override passes, which shows the receipt alone already protects the sidecar on a filesystem that keeps exact mtimes.agent
cargo fmt --all -- --checkclean;cargo clippy --locked --all-targets --all-features --release -- -D warningsclean;cargo clippy --locked --all-targets --all-features -- -D warningsclean;cargo test --locked --all-features --bin clauth -- claude:: runtime:: profile_cache:: actions::1494 passed, 0 failed. The full suite was not run here, and neither Linux nor Windows was built; I'm leaving those to CI. The Keychain mirror is compiled out undercfg(test), so the ordering against it is covered only by reading the code. On our fork, the earlier version of this change was checked end to end on Linux: an isolated home, synthetic profiles with fake bearers and a local HTTP sink, one long-lived Claude Code 2.1.289 process, both stores forced to one mtime, thenclauth switchA→B→A. The sink saw A, A, A before the change and A, B, A with it. I did not repeat that on this branch. operator verification: unknownload_profilenow fails the switch instead of repointing anyway; I kept "a failure moves nothing" to match the swap. (2) Theload_profilebefore the stamp is needed only on the receipt-less path. The test reaches that path through the read-back override, not a real one-second filesystem, so the stamp-ahead (memoized + 1s) branch is not exercised there. It also means a profile whoseconfig.tomlfails to load now fails the switch in the equal-mtime case, where before it would have relinked. (3) The tests are unix-only because on Windowscreate_symlinkmay fall back to a copy, and I could not run that leg.🤖 Generated with Claude Code