Skip to content

fix(models): a switch removes only the model values clauth wrote - #180

Open
xingfanxia wants to merge 6 commits into
uwuclxdy:mommyfrom
xingfanxia:contrib/keep-unset-models
Open

xingfanxia wants to merge 6 commits into
uwuclxdy:mommyfrom
xingfanxia:contrib/keep-unset-models

Conversation

@xingfanxia

@xingfanxia xingfanxia commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

the problem: my operator asked "why did this session turn into Opus 5.5 by itself?" about a running Claude Code session they had put on another model with /model. The cause turned out to be a clauth account switch, and the ask became: "add a config to clauth that can turn this baffling automatic change off". (Both translated from Chinese.) Following your review, this round drops the config and fixes the switch instead.

A switch or a start now removes a model key the landing account does not set only while the settings file still holds the value clauth itself last wrote there. A /model pick, an /advisor pick or a hand edit survives. A row an account sets is still written over whatever the file holds. There is no new key.

Cause. On every switch and every clauth start, build_settings in src/claude.rs writes each model key: the top-level model and advisorModel, plus ANTHROPIC_DEFAULT_{OPUS,SONNET,HAIKU,FABLE}_MODEL and CLAUDE_CODE_SUBAGENT_MODEL in env. It writes the account's row when the account has one and removes the key when it does not. Claude Code's /model saves its choice in that same top-level model key, and a running session reloads settings.json when it changes. So on accounts with no [models] rows, every automatic switch moved a running session off the model the operator picked, and deleted a subagent model set by hand. The removal could not tell a value clauth wrote from one the operator wrote.

Fix. It extends the base-env record (~/.clauth/base-env.json), which already tells clauth's values from the user's for custom [env] keys, to the model keys.

  • SavedBaseEnv gains models: per model key, the SHA-256 digest of the value clauth last wrote there (wrote), plus pending with the same meaning as on a BaseEnvEntry. It keeps no base: a row writes over the key as before, and nothing is put back when a row goes away, so no model value of the user's is ever stored. The field is None until the first live apply of this build, and a fresh stamp holds {}.
  • settle_model_keys decides each key the landing account leaves unset. The key is removed only when the record's entry names the live value. Where the record cannot speak for the live file, it uses the judges the record already uses for covers (Judge). With no model entries yet (no record, or one written before this change) it runs the migration's test: a value equal to the outgoing account's own value is clauth's (the active account, or every account while none is active). After an older clauth moved the marker (Unseen), any account's own value counts. Every other value is the user's and stays. "An account's own value" is its [env] entry for an env key, else its [models] row, since the build's "profile env last" pass writes the [env] one.
  • The record lands the way covers land (land_live_settings). A written row's entry goes in the pre-write with pending naming the clauth value it replaces. A removal's entry stays in the pre-write, written there when the migration or Unseen judge found the value, and retires after the live write. A crash between the two writes therefore never turns clauth's value into the user's.
  • A record that does not parse refuses a build only when a key the account leaves unset is held, the same rule as for a cover that needs a decision. The error names the file and never quotes it. A build whose model keys are all set by rows still lands, without updating the record, as an unread record does today.
  • Sibling builds (a shared clauth start copy, the start --auto and delegate demand read, the proxy-delete check) read the record when such a key is held and never write it. So a shared start copies the operator's /model pick from ~/.claude/settings.json and leaves out the rows clauth wrote there for the active account. An --isolated start builds from {} and keeps nothing.
  • The model env keys no longer go through the outgoing account's [env] strip list in build_settings. That list removed an [env]-carried model key whatever the file held, so a value the user changed since would have been lost. The record decides those keys like the rest.

Deliberately not changed.

  • A row an account sets is still written over the file's value, and the user's value it replaced is not restored when a later account has no row. A row has always meant "this account uses this model".
  • Switch-off (release_saved_base_env) still leaves the departing account's model rows in place and still strips an [env]-carried model key, as the wiki already says.
  • CLAUDE_CODE_EFFORT_LEVEL is still written on every build. It is not a model key, and feat(models): set Claude Code's effort per model in each account #173 is working on effort.
  • start::fallback_chain still resolves a default fallback entry from the member's [models] default, not from a kept model.
  • settings_sync already keeps model, advisorModel and the managed env keys per profile, so a /model in a session's copy still stays in that copy.
  • The record stays at v: 1; the new field is optional.

User-visible surface. No new key, flag, TUI row or text. A model value the operator set now survives a switch and reaches a shared clauth start, which the launch-demand walk then counts. In the wiki, Configuration's model-routing paragraph now states the rule and the upgrade case in place of "an unset row removes its key", and the storage-layout line for base-env.json mentions the digests. Auto-Switch's launch-demand parentheticals ("the global model never counts", and the same for the subagent model and advisorModel) now say a global value clauth did not write counts on a shared runtime. SECURITY.md and wiki/Security.md say the record also holds those digests. The README does not state the removal rule, so it is unchanged.

Tests (each in a HomeSandbox, or the runtime suite's with_fake_home, with synthetic files only):

  • tests/inline/actions.rs, through switch_profile_locked:
    • a_presets_model_ids_never_follow_a_switch_onto_an_account_without_rows: all seven keys from one account's rows leave on the switch to a rowless account and come back with it.
    • a_model_the_user_set_survives_a_switch_and_a_row_still_overwrites_it: a hand-set value under all seven keys survives the rowless switch, the next account's rows overwrite it, and those values leave on the switch after.
    • clearing_a_row_on_the_active_account_removes_only_what_the_row_wrote: covers edit_profile_model.
    • an_env_carried_model_key_leaves_only_while_it_holds_the_accounts_value: the same rule for a model key in an account's [env].
    • with_no_record_an_upgrade_removes_only_the_outgoing_accounts_rows and a_record_without_models_removes_only_the_outgoing_accounts_rows: the upgrade case, with the record deleted and with only its models field gone. The outgoing account's rows leave, a /model pick stays, and the record then holds models.
    • an_older_clauths_switch_leaves_its_model_rows_clauths: the marker was moved with no record entry. The older clauth's rows leave and a value no account writes stays.
    • a_failed_first_removal_after_the_upgrade_still_removes_on_the_retry: the upgrade's first removal fails its live write and the retry still removes.
  • tests/inline/claude.rs:
    • an_unset_model_row_removes_only_the_value_clauth_wrote_live: the live apply on its own, all seven keys.
    • a_shared_start_build_keeps_the_users_model_and_drops_the_ones_clauth_wrote: the session build and runtime_model_sources over the live file, plus the isolated case.
    • an_unparseable_record_refuses_a_model_removal_it_would_decide: live and sibling builds refuse without quoting, and the live file is unchanged. A rows-only build still lands.
    • with_no_record_a_hand_set_model_stays: no record and no accounts.
    • a_failed_live_write_leaves_clauths_model_values_clauths: FailLiveSettingsWrite on a removal and on a row replacement, the second pinning pending.
  • tests/inline/runtime.rs a_shared_copy_keeps_the_operators_model_pick_but_not_clauths_rows: write_merged_settings under both isolations.
  • Changed to the new rule: build_settings_clears_stale_model_knobs, build_settings_removes_only_an_advisor_model_clauth_wrote_when_the_profile_has_no_row (was ..._removes_advisor_model_when_the_profile_has_no_advisor_row), runtime_model_sources_read_the_runtime_models_and_drop_a_missing_default and start::a_global_advisor_model_counts_without_a_profile_row_only_when_the_user_set_it (was ..._never_counts_without_a_profile_row). Each used to assert that a value seeded with no record is removed. Each now has clauth write the value through a live apply first, and two of them also assert that a hand-set value stays. Four start --auto tests in tests/inline/cli.rs (cmd_start_auto_never_judges_a_member_by_a_global_model_its_session_drops, ..._by_a_subagent_model_the_session_scrubs, ..._a_no_model_launch_with_a_fallback_keeps_the_blanket_scoped_judgment, ..._an_unresolved_default_keeps_its_known_headline_on_the_scoped_rung) seeded a global model by hand and expected a member without a default to drop it. Each now has clauth's live apply write that model first (a new clauth_wrote_the_global_model helper), and its assertions are unchanged. The two record-stamp assertions in the_first_settings_apply_stamps_the_record_and_keeps_it_while_empty gain "models": {}.

Mutations, each run with nextest on the tests above (all pass without a mutation). The first ten ran on 17 tests. The pre-write mutation passed all 17, so I added a_failed_first_removal_... and ran that mutation and the last one on 18:

  • Every held unset value counts as clauth's (the old rule): 12 red, including every "user's value stays" test.
  • No held value counts as clauth's: 15 red.
  • The migration never matches an outgoing row: the two upgrade tests red.
  • Unseen judged as Recorded: an_older_clauths_switch_leaves_its_model_rows_clauths red.
  • pending never written: a_failed_live_write_leaves_clauths_model_values_clauths red.
  • A removal's pre-write names nothing: a_failed_first_removal_after_the_upgrade_still_removes_on_the_retry red.
  • The [env] strip list keeps removing model keys: an_env_carried_model_key_... red.
  • A torn record never refuses a model decision: an_unparseable_record_... red.
  • Sibling builds never read the record for models: 6 red (start copy, demand, advisor, runtime, start, torn record).
  • A record without models read as Recorded: a_record_without_models_... red.
  • The fable key misspelt in MODEL_KEYS: 10 red. Dropping it from the array instead does not compile.

agent

  • Claude Code (claude-fable-5-2 leading the session; a claude-opus-5-5 subagent wrote this round and ran the checks), operated by @xingfanxia
  • merged mommy (9ca6de2e) in with a merge commit, since the earlier CI failures came from the rustc 1.100 lints fixed there. Then one commit reverts the flag, one adds the rule and its tests, and one updates the docs.
  • ran (macOS, arm64, rustc 1.100.0, cargo-nextest 0.9.151): cargo +1.100.0 fmt --all -- --check clean; cargo +1.100.0 clippy --locked --all-targets --all-features -- -D warnings clean; the same with --release clean; cargo +1.100.0 nextest run --locked --all-targets --all-features --no-fail-fast 12665 passed, 0 failed, 16 skipped; the same with --release 12658 passed, 0 failed, 16 skipped; the mutation runs above. A first full debug run failed the four cli.rs tests listed under Tests, which still seeded the old rule; the numbers are from the rerun after that fix. Not run on Linux or Windows; I'm leaving those to CI. Not tried against a real Claude Code session or a real switch. operator verification: unknown
  • unsure: (1) A torn record now also refuses a switch onto a rowless account whenever the live file holds a model key, which is a much more common state than a cover needing a decision. I followed the cover rule; keeping the value instead would be the other conservative choice. (2) A model-only edit of the active account that is the first live apply after the upgrade judges the old value against the account as disk holds it after the edit, so a row cleared in that edit keeps its old value once. [env] edits pass their pre-edit maps for this; model edits have no such path, and I did not add one. (3) During the upgrade's one-time judgment, an account whose config.toml does not read names nothing, so its value counts as the user's and stays. The cover migration strips in a similar spot. (4) A rows-only build over a torn record writes its rows without recording them, so after the repair a later rowless switch keeps them as the user's. (5) A mommy build from before this change reading a record that has models fails on deny_unknown_fields. The record has not shipped in a release yet (v0.18.2 predates it), so I kept v: 1. (6) The demand walk now reads base-env.json and profiles.toml once per member build when the operator's file holds a model key that member leaves unset. I have not measured it.

🤖 Generated with Claude Code

xingfanxia and others added 2 commits November 13, 2026 02:17
A switch or a start removes every model key the landing account's [models] leaves unset, so a model picked with Claude Code's /model, or a hand-set CLAUDE_CODE_SUBAGENT_MODEL, is dropped on every switch.
keep_unset_models = true in profiles.toml (hand-edited, default off) leaves such a key as the settings file holds it: model, advisorModel, the ANTHROPIC_DEFAULT_*_MODEL remaps and CLAUDE_CODE_SUBAGENT_MODEL. A row the account sets is still written, and CLAUDE_CODE_EFFORT_LEVEL is untouched.
The builder reads the key only when a build would remove a value that is there, and an unreadable profiles.toml reads off, so with the key unset every build is what it was. Two builder tests that seed such a value now hold a HomeSandbox.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Configuration says what the key keeps on a switch and on a start, that a row an account sets then stays after a switch away, and lists it in the profiles.toml table.
Auto-Switch's launch-demand rules note where a kept global model, subagent model or advisor now counts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@xingfanxia

Copy link
Copy Markdown
Contributor Author

A note on the red CI: the three check legs do not fail on this diff. The run picked up rustc 1.100.0 (stable ... updated - rustc 1.100.0 (from rustc 1.99.1) in the ubuntu log), and its clippy reports new lints in src/gateway_edit.rs:2919-2920 and tests/inline/tui_render_overview.rs:4458 and :4475 (needless_range_loop), files this PR does not touch. The windows leg stops at the same place, and the macos leg failed earlier, in a tool install step (curl: (56) Failure writing output to destination). Runs that started a little earlier today were still on 1.99.1, which is also what I ran locally (clippy clean in debug and release there). I left those lints alone so this PR stays on its subject; I can merge mommy in once it is green again, or fix them here if you would rather.

@xingfanxia
xingfanxia marked this pull request as ready for review November 12, 2026 18:31
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 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 narrower design from your note (2) is the one I want: clauth removes a model key only while the file still holds the value clauth wrote there, so a /model pick or a hand edit survives a switch and nobody needs a knob. The base-env record already does this for custom [env] keys, so the fix extends it to the model keys. A global flag keeps clauth's own leftovers too (a preset's model ids following you onto an Anthropic account), and leaves the bug on for everyone who never sets it.

xingfanxia and others added 4 commits November 17, 2026 05:15
Picks up the rustc 1.100 clippy fixes that turned this PR's CI red.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The review asked for the narrower rule instead of a knob: clauth removes a
model key only while the file still holds the value it wrote there. This
takes the flag, its reader, its tests and its wiki rows back out, leaving
these files as on mommy; the next commit builds the rule on the base-env
record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…wrote

A switch or a start removed every model key the landing account leaves
unset: `model`, `advisorModel`, the ANTHROPIC_DEFAULT_*_MODEL remaps and
CLAUDE_CODE_SUBAGENT_MODEL. That moved a running session off a `/model`
pick on every automatic switch and dropped a hand-set subagent model.

The base-env record now keeps, per model key, the digest of the value
clauth last wrote there. An unset row removes the key only while the live
value is one clauth wrote; any other value is the user's and stays. A row
still writes over the key. Where the record has no model entry yet (the
upgrade) or an older clauth moved the marker, the record's own judges
decide: an account's own value counts as clauth's. Entries land around the
live write the way cover entries do, so a crash never turns clauth's value
into the user's. Sibling builds (a shared start copy, the launch-demand
read) read the record and never write it, and an unparseable record
refuses a build only when such a key needs a decision.

The model env keys leave the outgoing account's [env] strip list in the
build, so an [env]-carried model key follows the same rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Configuration's model routing now states the rule (an unset row removes
only the value clauth wrote) and the upgrade case; the launch-demand
parentheticals in Auto-Switch say a global model, subagent model or
advisor clauth did not write counts on a shared runtime; the storage
layout, SECURITY.md and Security say base-env.json also holds the model
digests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@xingfanxia xingfanxia changed the title feat(models): keep_unset_models keeps a model the account leaves unset fix(models): a switch removes only the model values clauth wrote Nov 16, 2026
@xingfanxia

Copy link
Copy Markdown
Contributor Author

Done, and the flag is gone. base-env.json now also keeps a digest of each model value clauth writes (model, advisorModel, the four ANTHROPIC_DEFAULT_*_MODEL, CLAUDE_CODE_SUBAGENT_MODEL). An account without the row removes the key only while the file still holds that value, so a /model pick or a hand edit survives a switch, and a preset's ids leave with the preset. A row still overwrites. A shared clauth start and the demand walk read the same record. For the upgrade, I used the record's migration test: a value with no entry is clauth's when it equals the outgoing account's own value, and the user's otherwise. An [env]-carried model key follows the same rule now rather than the outgoing strip list. I merged mommy in for the 1.100 lints. The PR body has the tests, the mutation runs and the open questions.

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