← Back to Pull requests

(feat) swap: settle Claude Code's stale plaintext credential fallback after each canonical write (issue #1540) #1541

Closed
unknown closed this pull request without merging on Oct 30, 2026 18:52 UTC
View on GitHub
unknown unknown commented on Oct 20, 2026 21:06 UTC
Author

Closes #1540.

Picked up on 2026-10-30 by the maintainer after the review below the fold. The contributor's commit stands, rebased onto main, with three lines of its message brought in line with what lands (the default, the ADR number, the follow-up issue); a second commit on top applies the review. An independent fresh-context judge graded the result MERGEABLE-WITH-FIXES, and both fixes are in. The two notable changes: the default is now touch, and the ADR is 0039. This body is updated to match; the original text is in its edit history.

On macOS, Claude Code writes <config dir>/.credentials.json only when a keychain save fails. On 2.1.285 its per-request change check is file-first: while that file exists, a live session compares only the file's mtime and never re-reads the keychain. So once one stale fallback file is left behind, every swap is invisible to every running session. Issue #1540 has the field measurements and the decode.

The change

FallbackSettlingStore (new, src/plaintext_fallback.rs) wraps the canonical CredentialStore. After every successful write, it settles the file according to a new [credential].plaintext_fallback key:

| Mode | Effect |
|---|---|
| touch (default) | bump its mtime, so live sessions re-read storage (keychain first) once; the file stays |
| remove | delete the file |
| off | leave it |

Why touch is the default (review finding 2; ADR-0039 § Context). The file is not always stale. Claude Code writes it when a keychain save fails (build/version-compat.md § AC-6), so at that moment it holds a credential newer than the keychain item, and refresh tokens rotate on exchange (ADR-0007), so the keychain's copy may already be dead. The account being refreshed is the active one, which the next swap moves away from, and swap.rs re-stashes it from the keychain without seeing the file. remove could therefore delete the only working copy of that login. touch fixes the measured harm and deletes nothing; remove is the opt-in for an operator who would rather have that account need a fresh login than keep a plaintext copy on disk.

All four production writers of the canonical item (swap, adopt-target, activating login, keep-warm promotion) go through three store construction sites: the daemon in cli.rs, use, and login's reconcile_login. Wrapping at those sites covers all four, plus any writer added later.

Two source-inspection tests guard the wiring:

  • every_canonical_writer_settles_the_fallback_with_the_configured_mode pins the three sites: the wrap is present, it wraps RealCredentialStore::new(), its mode comes from plaintext_fallback, and no bare store reaches the site ahead of the wrap.

  • every_bare_store_construction_is_wrapped_or_registered_read_only walks src/ and requires every other non-test RealCredentialStore::new() to be registered as read-only with a reason, keyed by file and enclosing fn, so a writer built on a fourth site cannot slip past the first test.
  • Safety properties, each with a test:

  • macOS only. fallback_applies_on(os) is a pure function of std::env::consts::OS, so the Linux and Windows arms are asserted on the macOS runner (the_settlement_applies_on_macos_and_nowhere_else). Elsewhere the file is the store (#26) and the wrapper passes straight through.

  • Paired with the item sessiometer writes. The path resolves like the keychain service suffix: CLAUDE_SECURESTORAGE_CONFIG_DIR (defined-empty means the default), then a non-empty CLAUDE_CONFIG_DIR, then ~/.claude. It must be absolute and ASCII.

  • Never follows a link. symlink_metadata first; a symlink or a directory is refused in every mode. The touch arm then opens with O_NOFOLLOW, so a link swapped in after the stat is refused by the kernel (the_re_stamp_open_refuses_a_link_on_its_own).

  • The default deletes nothing (the_default_mode_re_stamps_and_deletes_nothing).

  • Never reads the file, and never fails the write. The keychain write has already succeeded, so settlement is best-effort, like the ~/.claude.json co-write.

  • A failed write settles nothing.

  • One durable line per outcome except "absent": event=plaintext_fallback action=removed|touched|ignored|refused|failed. It is registered in every_event_variant and carries no path and no secret.
  • The config key follows the [migration].conflict_policy pattern (ADR-0005): validated against its vocabulary, rendered with an inline comment, reported by config show --origin. origin_report_reports_every_key_render_writes pins render and origin agreement.

    Docs

  • README [credential]: a table row and a #### Claude Code's plaintext fallback subsection, including why remove is not the default and what a live session does while the file exists (it keeps the token it already holds in memory; no restart is needed).

  • build/version-compat.md: a # Issue #1540 entry recording the file-first change check, on CC 2.1.285, outside the verified range and not widening it. The measured removal is ✅; the predicted touch pickup is its own ⚠️ finding, unmeasured. The "what would falsify this" list includes the newer-file window.

  • ADR-0039 (renumbered from 0038, which main took in #1527), plus its index row. § Context records the window, § Decision 3 the default, § Alternatives the remove default and the mtime-comparison variant.
  • Follow-ups

    1. The import version floor has moved — #1589, open. [credential].plaintext_fallback is rendered unconditionally, so CONFIG_BLOCK_VERSION_FLOOR must name this PR's merge commit, which exists only after the squash. a_build_at_the_current_floor_commit_still_rejects_a_current_render witnesses the gap until then; the constant's doc comment and the README point at #1589.
    2. A status / menubar warning while the file is present. It touches the status wire and the Swift fixtures, so it stays separate (as proposed in #1540).
    3. The mode is read once at daemon start-up, like the tunables. A roster reload does not re-read it.
    4. The touch arm is unmeasured live. The ledger entry says what would promote it.

    Gates

    The diff touches src/, README.md, build/, and docs/, so test, msrv, deny, and doc-gates are owed. No golden, apps/menubar/, or gate path is touched, so no rebaseline or Gate-Change-Acknowledged trailer is owed.

    Run locally on the pickup commit, in the order ci.yml runs them, with the line each printed:

    cargo fmt --all --check                                                   exit 0
    cargo clippy --all-targets --all-features -- -D warnings                  exit 0 ("Checking sessiometer v0.1.0")
    RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --document-private-items   exit 0 ("Documenting sessiometer v0.1.0")
    cargo build --verbose                                                     exit 0
    cargo test --verbose              test result: ok. 2438 passed; 0 failed; 14 ignored; 0 measured
    cargo +1.87.0 build && cargo +1.87.0 test
                                      test result: ok. 2438 passed; 0 failed; 14 ignored; 0 measured
    ./scripts/check-no-security-framework.sh                                  exit 0
    cargo deny check advisories sources licenses                              advisories ok, licenses ok, sources ok  (cargo-deny 0.20.2)
    ./scripts/check-doc-citations.sh                                          exit 0
    ./scripts/check-readme-cc-range.sh                                        exit 0
    ./scripts/check-test-runner-env-form.sh                                   exit 0
    ./scripts/check-doc-citation-rot.sh <origin/main> <HEAD>                  ok: no source citations added or modified in docs/
    

    The contributor's own CI run failed only on deny: RUSTSEC-2026-0386 on tokio 1.52.3, which main fixed in c9ec668 and the rebase picks up.

    Provenance

    This is the third cause behind the same "sessions don't follow swaps" symptom as #1535. Moving the file aside by hand put the same running sessions back on the swapped-to account on their next request, with no restart.

    🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: alexey-pelykh Review duration: 14266m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #1541"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%