Skip to content

fix(switch): stamp the incoming store when two stores share one mtime - #175

Merged
uwuclxdy merged 4 commits into
uwuclxdy:mommyfrom
xingfanxia:contrib/global-switch-equal-mtime
Nov 13, 2026
Merged

uwuclxdy merged 4 commits into
uwuclxdy:mommyfrom
xingfanxia:contrib/global-switch-equal-mtime

Conversation

@xingfanxia

Copy link
Copy Markdown
Contributor

the problem: my operator asked for clauth to auto-switch Claude accounts on a remote Linux host the way it does on their Mac. While testing that I reproduced a silent failure: a global clauth switch reported success, but a running Claude Code session kept authenticating as the account it had been switched off. The ask then became fixing that, and this PR ports the fix from our fork onto current mommy.

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.json resolves to is not equal to the one it memoized. runtime::touch_store documents this, and the per-session swap stamps the incoming store before it repoints. The global relinks, force_link_profile_credentials and the guarded link_profile_credentials, call publish_credential_link with 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.

  • A new keep_repoint_visible in src/claude.rs runs in both relinks, after the Keychain mirror and before publish_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.
  • It stamps through the swap's own runtime::stamp_store, made pub(crate) with its doc updated. There is no second stamp function, and the touch receipt is written the same way, so readers going through profile_cache::effective_write_time still see the store's real write time.
  • precondition_with documents that a stamp ahead of a legacy .pending sidecar's settlement discards that sidecar. The switch path reads the profile with read_profile, which does not land a .pending. So before the stamp it runs load_profile the way a swap's SwapPlan does, 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 existing with_coarse_mtime override. The load re-enters the state flock the relink already holds. A re-entrant frame takes no flock and no lock rank (lock.rs acquire_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 through force_link_profile_credentials. The first link does not stamp, each later switch moves the live mtime, and B's effective_write_time still reads its pre-stamp write time (the receipt landed).

  • guarded_relink_between_equal_mtime_stores_stays_visible_to_running_sessions: the same through link_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 to a_truncating_filesystem_gets_no_receipt_at_all, because with_coarse_mtime is private to runtime): the incoming store carries a credentials.json.pending that 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_visible calls removed, the first two fail on equal mtimes and the third passes, since it is the guard. With only the load_profile line 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

  • Claude Code (claude-opus-5-5), operated by @xingfanxia
  • ran (macOS, aarch64, rustc 1.99.1, the current stable that CI installs): cargo fmt --all -- --check clean; cargo clippy --locked --all-targets --all-features --release -- -D warnings clean; cargo clippy --locked --all-targets --all-features -- -D warnings clean; 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 under cfg(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, then clauth switch A→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: unknown
  • unsure: (1) a failed stamp or load_profile now fails the switch instead of repointing anyway; I kept "a failure moves nothing" to match the swap. (2) The load_profile before 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 whose config.toml fails 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 Windows create_symlink may fall back to a copy, and I could not run that leg.

🤖 Generated with Claude Code

xingfanxia and others added 2 commits November 10, 2026 15:23
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 uwuclxdy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/claude.rs Outdated
return Ok(());
}
match (mtime(&live), mtime(&incoming)) {
(Some(memoized), Some(next)) if memoized == next => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/claude.rs Outdated
/// 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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: on macOS the Keychain item has already moved when the stamp or the load fails. say the failure leaves the link unmoved.

Comment thread tests/inline/claude.rs Outdated
assert_eq!(
live_mtime(),
earlier,
"an mtime that already differs is a real write time and must be kept"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/inline/claude.rs

#[cfg(unix)]
#[test]
fn guarded_relink_between_equal_mtime_stores_stays_visible_to_running_sessions() {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this covers one switch, without the switch back or the receipt assert the forcing test has. consider adding both.

Comment thread tests/inline/runtime.rs
assert_ne!(
file_mtime(&link),
Some(same),
"a running session memoized a's mtime; b must not resolve to the same one"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

xingfanxia and others added 2 commits November 13, 2026 02:22
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>
@xingfanxia

Copy link
Copy Markdown
Contributor Author

Thanks. All of it is in 22cd0350 and f7a3e833.

  • src/claude.rs (major, stamp on every switch). keep_repoint_visible now stamps on every relink onto a different store, the way the swap stamps every move. The same-store exit and the first-link exit (a live path that resolves to nothing) stay. The two current mtimes are no longer compared. The stamp gets the outgoing store's mtime as its memoized, as the swap passes file_mtime(&current), so the coarse-tick fallback still works.
    • Where the load_profile goes. It still runs right before the stamp, but now only when a legacy .pending stands beside the incoming store (profile::rotation_staged_beside(target)). My first try loaded before every stamp, the way the swap's install branch does. That broke an existing test, tui::app::tests::a_divergence_overwrite_over_a_login_another_account_stores_is_not_refused: left: [(Danger, "relink failed\nfailed to parse work/config.toml at line 4, column 9")], right: [(Warning, "nothing saved\nthis login is already saved in 'other'")]. The swap refuses an unreadable profile before its load runs. The relinks also serve an account whose config.toml cannot be read, and the TUI's divergence overwrite relinks one on purpose. An unconditional load would fail those relinks. With the condition, the load runs only when it has a sidecar to settle. So the consequence I flagged in the PR body gets narrower instead of wider. A torn config.toml now fails a relink only when a .pending also sits beside that account's credentials.json. Before, it failed one only in the equal-mtime case, whether or not a sidecar was there. When the load does fail, the relink still fails with the link unmoved, as you said to keep. This also means a relink does not run load_profile's config.toml normalization unless a sidecar is there.
  • src/claude.rs (minor, failure wording). The doc now says: "A failed load or stamp fails the relink with the link unmoved; on macOS the Keychain item has already moved to the incoming account by then." No error text claimed anything about this, so only the doc changed. The stamp still runs after the Keychain mirror, because keychain_mirror_source and carry_live_extra_best_effort may write the incoming store, and a stamp before them would be followed by a real write. The runtime::stamp_store doc line about this caller now says the load runs when a .pending stands beside the store.
  • tests/inline/claude.rs:9603 (minor). That leg is now global_switch_stamps_a_store_whose_mtime_already_differs, and it is the positive case. It poses the idle-session story: both stores at earlier, link a, then a write moves a to same while b stays at earlier. After the switch to b it asserts the live mtime moved off earlier and effective_write_time(&b) still reads earlier. Then it relinks b onto itself and asserts the live mtime is unchanged.
  • tests/inline/claude.rs:9569 (nit). The guarded test now asserts b's receipt after the relink to b, then switches back to a and asserts the live mtime moved again and a's receipt holds.
  • tests/inline/runtime.rs:13979 (nit). The link's identity and mtime are now read before the test's own load_profile. The test is renamed global_switch_settles_a_pending_sidecar_before_its_stamp, since the stamp is no longer equal-mtime only. One limit: the switch's own load_profile adopts the sidecar through atomic_write_600, and that write already moves the mtime. So this assert catches a removed helper, but not a removed stamp with the load kept. The claude.rs tests catch that one.
  • Optional (log line). Done. The daemon's boot relink and the TUI's three (the two in the boot worker and the resumed BootRelink) now log the error and the account instead of dropping it. The TUI's three go through one small relink_at_boot helper. The text is ASCII: clauth daemon: <error>; .credentials.json was not relinked to '<account>', and clauth: ... in the TUI, which goes to clauth.log there. This is new user-visible log output. The guard's existing refusal (a live file that differs from the profile) now gets a line too, once per start, where before it said nothing. I added no test for the log lines.

Commands run (macOS, aarch64):

  • cargo fmt --all -- --check: clean
  • cargo clippy --locked --all-targets --all-features -- -D warnings: clean
  • cargo clippy --locked --all-targets --all-features --release -- -D warnings: clean
  • cargo nextest run --locked --all-features -E 'test(/claude::|runtime::|profile_cache::|actions::/)': 1494 passed, 0 failed
  • cargo nextest run --locked --all-features -E 'test(/daemon::|tui::/)': 4295 passed, 0 failed

Mutations, each run against five tests (the two equal-mtime tests, the differing-mtime test, the .pending test, and the TUI divergence-overwrite test above):

  • stamp call removed, load kept: the three claude.rs tests fail, each on its first "must not resolve to the same one" assert. The .pending test passes, for the reason above.
  • whole helper removed (return at entry): all four switch tests fail. The .pending test fails on the mtime assert. With the same mutation and the test as it was on c3171a5f, it passes, which is the gap you pointed out.
  • load_profile removed: only the .pending test fails, left: Some("rt-pend-b"), right: Some("rt-rotated").
  • load_profile made unconditional: only the TUI divergence-overwrite test fails, with the toast above.
  • the PR's earlier equal-mtime guard restored: only global_switch_stamps_a_store_whose_mtime_already_differs fails, on the live-mtime assert.
  • same-store exit removed: only that test fails, on the same-store relink assert.
  • the stamp swapped for a bare set_times(now) with no receipt: the three claude.rs tests fail on their effective_write_time asserts. The .pending test passes, since it runs with no receipt anyway.

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 needless_range_loop in src/gateway_edit.rs:2919-2920 and tests/inline/tui_render_overview.rs:4458 and :4475, which this PR does not touch (#180's run shows it). The clippy runs above were on 1.99.1.

Not done, or unsure:

  • No mutation isolates the switch-back asserts (the guarded test's a-side receipt and second visibility check). Each mutation above fails earlier in the same test.
  • The full suite, Linux and Windows were not run here. I'm leaving those to CI. The new tests are #[cfg(unix)] like the rest.
  • I did not repeat the end-to-end check with a real Claude Code process on this branch.

xingfanxia added a commit to xingfanxia/clauth that referenced this pull request Nov 12, 2026
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>
@uwuclxdy
uwuclxdy merged commit 3baaf8d into uwuclxdy:mommy Nov 13, 2026
3 of 6 checks passed
xingfanxia added a commit to xingfanxia/clauth that referenced this pull request Nov 16, 2026
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>
xingfanxia added a commit to xingfanxia/clauth that referenced this pull request Nov 16, 2026
.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>
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