Skip to content

Studio: link a saved preset to a model and apply it on every switch - #15279

Open
debojyoti-dev wants to merge 25 commits into
unslothai:mainfrom
debojyoti-dev:feat/model-preset-link-14900
Open

debojyoti-dev wants to merge 25 commits into
unslothai:mainfrom
debojyoti-dev:feat/model-preset-link-14900

Conversation

@debojyoti-dev

@debojyoti-dev debojyoti-dev commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #14900.

Before: a saved preset applied to whatever model was loaded. Switching between models with different sampling needs meant reopening the preset list every time, and it was easy to run one model on another's temperature or system prompt.

After: Run settings has a Use for this model switch under the preset picker. Turning it on links the active custom preset to the loaded model, and from then on selecting that model applies the preset automatically. Several models can share one preset.

  • The preset list marks the preset linked to the current model ("This model").
  • If the model is linked to a different preset than the one showing, a line under the switch names it, with an Unlink action.
  • Picking another preset or moving a slider is a temporary override. It doesn't touch the link or the saved preset, and the next time you switch back to the model the linked preset comes back.
  • Models with no link work as before. Only custom presets can be linked, since Default already follows each model's own recommendation.
  • Deleting a preset removes its links, so a later preset saved under the same name doesn't pick them up.
  • Links survive unloads, restarts and other browsers, because they live in the Studio settings DB alongside the presets.

The preset's sampling settings and system prompt are applied when the model is selected. Load settings stored in a preset keep the existing behaviour (applied on the next reload), so switching models never triggers a second load.

How

  • modelPresets (model id → preset name) is a new chat setting. The backend schema accepts it, and the existing per-key deep merge keeps every other model's link when one changes. A null entry removes a link and is dropped from storage instead of being kept.
  • The chat store keeps the map, hydrates it with the same mutation-version fence the presets use, and adds setModelPreset. A store subscription watches the active checkpoint. When it changes to a linked model, the subscription applies the preset the same way picking it in Run settings does (applyPresetForProvider, fromPreset, so the per-model memory is handled as for a manual pick). It runs one microtask after the load path's own setParams, so the model's defaults are filed first. When settings finish hydrating under a model that is already loaded, the linked preset is applied unless it is already the active one. That way a restored temporary override isn't thrown away on reload.
  • getLinkedPreset in preset-policy.ts resolves a link and ignores built-in names and presets that no longer exist.

Testing

  • New tests/preset-model-link.test.ts runs the real store against the settings HTTP stub. It covers hydrating links (including a removed null one), auto-apply on switch, separate presets per model, unlinked models keeping Default's recommendation, a temporary override not moving the link or the saved preset, a link to a missing preset being ignored, and links and unlinks saved per model.
  • New backend tests check the payload schema (a null value is accepted, junk is rejected) and the per-model merge and removal in studio_db, through both the plain merge and compare-and-set.
  • npm run typecheck passes. The full frontend suite passes (13,950 passed, 11 skipped, 0 failed). ESLint on the touched files reports only issues that were already there.
  • Backend: test_chat_history_storage.py, test_chat_history_routes.py, test_min_p_mode_settings.py, test_account_chat_settings.py, test_chat_settings_payload.py, test_media_generation_preset_settings.py and multi_account/test_downgrade_sim.py pass. test_search_images.py fails here only because ddgs isn't installed on this machine. That is unrelated to this change.
  • Tested on Windows. I didn't click through the switch in a running Studio with a model loaded. The UI change is covered by the typecheck and the store tests.

Maintainer changes

Reviewing this, linking one model made its preset leak into every other model: after visiting a linked model, an unlinked model you had never opened ran the linked preset's temperature and system prompt, and a model you had opened kept its own values while the picker named the linked preset, so a Save overwrote that preset. The link now applies to the open tab only. It never writes the settings DB, a chat, or a model's remembered settings, so the stored pick stays your own: leaving a linked model, or reloading on one, puts back exactly what you had before.

  • Leaving a link restores the earlier pick and its values, then the destination's own memory and the open chat's settings over them. Under Default, the destination's recommendation applies at once, including when another tab or the CLI loads the model.
  • While a link shows, status refreshes, chat restores and replays keep it on top per key, so an edit overrides only the key you moved, and the model's memory and chat pins are left untouched.
  • Saving the shown preset (any name) commits it like a pick. Unlinking the model on screen restores the earlier pick. The linked token budget is held to the loaded window or the provider's output cap.
  • Load settings stored in a preset are not applied by a link; pick the preset by hand to stage them for a reload.

Tests: tests/preset-model-link.test.ts grew to 44 store-level cases, each new one failing on the commit before its fix. Full frontend suite 16053/16053 on the merged branch, backend chat-settings tests 167/167. Studio regression switchboard base vs head: no functional regression; the only visual difference is the new switch row in Run settings.

Run settings with a GGUF loaded (gemma-3-270m, CPU): the new switch, on after linking Research.

Run settings: base has no link switch; this PR shows "Use for this model" on

Preset list: the linked preset is marked "This model".

Preset list: base plain; this PR marks Research as This model

…nslothai#14900)

Run settings gets a "Use for this model" switch under the preset picker.
Turning it on links the active custom preset to the loaded model; from
then on, selecting that model puts the preset on exactly as picking it by
hand would. Edits made afterwards stay temporary until saved, and the
next switch back puts the linked preset on again. The picker marks the
linked preset, and a model linked to a preset other than the active one
says so with an Unlink action. Deleting a preset removes its links.

The links persist in chat settings as modelPresets, a map from model id
to preset name that the server merges per model; a null entry removes
one link and is dropped rather than stored.
@danielhanchen

Copy link
Copy Markdown
Member

Frontend build + bundle sanity is red on this PR:

eager startup JS: 6786.5 KB raw, 2047.5 KB transfer, 105 chunks
over the startup budget: transfer 2047.5 KB > 2047.2 KB

Main measures 2046.8 KB transfer / 6783.7 KB raw, on #15276's build against 8b050a7a90. So linking presets to a model adds about 0.7 KB transfer / 2.8 KB raw to the eager startup chunks. studio/frontend/scripts/check-bundle-budget.ts asks for that in the same PR, either:

  • load the new pieces lazily, or
  • raise BUDGET by the measured growth, with a comment in the same style as the entries above it.

The Repo tests (CPU, studio) red is not from this PR: its 6 failures are the ORPO tokenizer tests that are red on main until #15262 lands.

danielhanchen added a commit that referenced this pull request Oct 25, 2026
`studio/backend/tests/test_mcp_servers.py::test_get_enabled_mcp_tools_caches_discovery`
fails now and then in `(Python 3.13, g-r)`, for example on #15279, and
passes when run alone:

```
tests/test_mcp_servers.py:1336: in test_get_enabled_mcp_tools_caches_discovery
E   assert 0 == 1
E    +  where 0 = len([])
```

## Cause

The tests, not the product. `core.inference.mcp_client` keeps discovered
tools (`_tool_cache`) and failed-probe cool-offs
(`_probe_cooloff_until`) as module state keyed by server id, and most
MCP tests name their server `s1`.

1.
`test_mcp_apps.py::test_a_rediscovery_that_fails_or_races_an_edit_caches_nothing`
(from #9301) runs a probe that fails. That correctly records a cool-off
for `s1`, and nothing removes it.
2. When `test_get_enabled_mcp_tools_caches_discovery` later runs on the
same worker, it resets the tool cache but not the cool-off map.
`get_enabled_mcp_tools()` then skips discovery for its own `s1`, so the
fake probe is never called.

How it was found: I ran the 151 MCP-related backend test files with a
detector on that state. It named this leak, plus
`test_mcp_tool_read_off_event_loop.py` leaving `s1` in the tool cache.

Running the two tests in that order reproduces the CI failure every
time.

## Fix

- An autouse fixture in `studio/backend/tests/conftest.py`,
`_forget_mcp_discovery_state`, clears both maps before and after every
test. It follows the other module-state resets already there and does
nothing when `mcp_client` has not been imported.
- `test_mcp_discovery_state_reset_between_tests.py` pins it: two tests
in one xdist group. The first records a cool-off and a cache entry the
way the client does; the second asserts neither survived.

## Verification

- The leak order (`test_mcp_apps.py::test_a_rediscovery...`, then
`test_get_enabled_mcp_tools_caches_discovery`), plus the new pair: 5
passed here. Without the fixture, 2 failed: the caching test and the
pair's second test.
- All 40 backend test files that touch `mcp_client`: 5703 passed here
against 5701 on main, which is the 2 new tests. The same 5 failures on
both sides come from this host's environment (`test_blender_managed.py`
x4 and `test_validate_url_rejects_bad[noscheme.com]`).
@danielhanchen danielhanchen self-assigned this Nov 13, 2026
A linked preset stayed active on the next model with no link, so a model
never visited before ran the linked model's sampling and system prompt,
and a visited one showed the linked preset's name over its own values
(a Save then overwrote that preset). Leaving a linked model for an
unlinked one now puts back the pick from before the link took over,
with the destination's own memory replayed over it.

Also drop the dead provider lookup for custom presets, correct the hint
about load settings, and trim comments.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T11:55:03.360673Z 047735e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c06fa017c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1041 to +1042
for (const [modelId, linkedName] of Object.entries(modelPresets)) {
if (linkedName === name) setModelPreset(modelId, null);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Remove deleted-preset links from the server's current map

If another browser links an additional model to this preset after this tab hydrates, that model is absent from this local modelPresets snapshot, so deletion never emits its null entry. The backend's per-key merge retains that link; it is ignored while the preset is missing, but saving a new preset with the same name reactivates it on that model. Perform deletion cleanup atomically against the server's current link map rather than relying only on links known to the deleting tab.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not fixing: needs a second browser to link another model to this preset after this tab loaded, then a delete here and a new preset under the same name. The stale link is inert until that recreate, and the server-side merge stays per key by design.

Keep a restored edit of the linked preset when the first model of the page
arrives after settings, restore the exact pre-link values and provenance
rather than the preset definition, and ignore the checkpoint round trip a
background auto-load makes when it puts the visible model back.

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…ross reloads

Leaving a linked model now gives an open conversation its own values back
the way the link took them, as chat edits, and the pick from before the
link survives a reload on the linked model.

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The stored pick is dropped on sign-out and on a new sign-in, a chat other
than the one open when the link applied keeps its own sampling, and a pick
deleted while on the linked model falls back to Default.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

A link now puts its preset on the live settings without saving it to the
settings DB, a chat or the model's memory, so the stored pick and the
installation defaults stay the user's own. Reloading on a linked model
finds that pick and applies the link over it again; leaving the model
writes the pick back, undoing edits made over the link. Drops the
localStorage copy and auth listeners the persisted version needed.

[skip ci]
Status and load merges for a model with no link honour the pick the link
displaced, so leaving a link under Default applies the new model's
recommendation at once (adoption included) instead of the linked
model's; the restore no longer lays the old recommendation back over it.

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The link drops the linked model's memory in this tab only, so a later
status replay cannot lay it back over the preset; leaving a link for an
external model puts every earlier value back, since no status merge
brings a recommendation there.

[skip ci]
Replays lay the model's memory and the open chat's pins back on; a link
still showing unedited now stays on top of them instead of the link
dropping the model's memory, so neither snapshot is touched.
The overlay and chat snapshots compare against the values the link last
applied rather than a fresh copy, so a reload that moves the context cap
keeps the capped linked budget instead of reading it as an edit.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b81d549bfc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +6767 to +6769
freezePendingChatWrite(shown);
recommendationMergedFor = null;
state.setParams(linkedParams(state.params, preset), { persist: false });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve pairing-held edits before applying a linked preset

If a chat's settings read is pending, a sampling edit is held in heldThreadScopedEdits rather than in the debounce handled by freezePendingChatWrite(). Selecting a linked model here replaces that live edit, and when the read resolves, applyThreadScopedSettings() processes held sampling fields by rereading the now-linked values instead of their captured values. A single temperature edit to 0.77, followed more than 400 ms later by selecting a 0.2 link, is lost and the chat ultimately restores its old 0.4. Preserve the held per-field values across this non-persisting update so delayed chat hydration cannot discard them.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not fixing: needs a chat settings read still pending while the user both edits and switches onto a linked model; that read is a local call and the race is not practically reachable. Edits around a link without a pending read are covered.

A chat's own settings arriving after a link applied (a reload in a chat,
or opening one on a linked model) now go under the link in this tab, as
a replay's do, instead of replacing it while the picker names the link.

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Putting a shown link back after a chat's settings arrive now touches only
the chat-scoped keys that restore displaced, so a Max Tokens edit or
other non-chat override stays.

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The link is put back after chat settings land only on keys the user had
not moved, so a no-op or first-chat pairing call cannot reset an edit
made before sending.

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 42c8ba2fd5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 13, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Repo tests (CPU, studio) fails on this branch merged with current main, in tests/studio/test_chat_autoload_failure_gate.py (every scenario):

AssertionError: the sliced region uses ['suspendLinkedPresetSync'], imported by chat-adapter.ts but absent from this harness. Add a stub to PREAMBLE, or these scenarios will fail as wrong-model assertions rather than ...

suspendLinkedPresetSync is new in this PR's chat-adapter.ts, and the harness runs the sliced autoLoadSmallestModel region with stubs for its imports. Adding a no-op stub next to the others in PREAMBLE (around line 106 of that test) should be enough, for example:

export function suspendLinkedPresetSync(..._args: any[]): any {
  return () => {};
}

Match the real return shape if the region uses the result. Main is green for this test, so this one is specific to the PR.

The sliced chat-adapter region now calls it, so the harness preamble
needs a no-op like its other imports.
@Etherll Etherll self-assigned this Nov 14, 2026
Moving a slider on a linked model saved "modified" as the pick's source and the
edited value as the installation default, so a reload before leaving the model
handed the edit to every unlinked model. Over a link the edit now reaches only
the model's own memory, and the stored source stays the earlier pick's.

Deleting the linked preset while it showed through the link replaced the stored
pick with Default. It now restores the earlier pick, as unlinking does.
@Etherll

Etherll commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

@codex review

1 similar comment
@Etherll

Etherll commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 047735e38a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

queueMicrotask(() => syncLinkedPreset(modelId, false));
}
} else {
modelPresets[modelId] = presetName;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Sync links added to the currently loaded model

When the user enables the switch for the already-loaded model, this branch updates only modelPresets and never calls syncLinkedPreset, leaving linkShown and pickBeforeLink unset even though the UI marks the preset as linked. A subsequent slider edit therefore follows the normal global-persistence path, and switching to an unlinked model leaves the edit in place instead of restoring the pre-link values. Sync the active model when adding the link so edits are treated as temporary as soon as the switch is enabled.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not fixing: when the switch is turned on for the loaded model, the preset on screen is the one picked by hand, so it is already the stored pick and there is no earlier pick to put back. A slider edit after that behaves exactly as it does with no link: the stored pick, its source and the values are the same with the switch on or off, and returning to the model shows the saved preset again. Edits made while the link itself put the preset on screen stay out of the stored pick, which is the case 047735e covers.

Lyxot added a commit that referenced this pull request Nov 17, 2026
…edit (#18011)

Editing the system prompt on Default stops Studio from applying each new
model's recommended sampling. Load Qwen3-0.6B, type a system prompt in
Run settings, then load Gemma 4 E2B for the first time: Gemma keeps
Qwen's temperature 0.6 and top-k 20 instead of its own 1.0 and 64.
Switching away then saves those values as Gemma's settings.

Any preset edit sets `activePresetSource` to `modified`. Model loads and
Qwen thinking-mode changes only apply recommended values under
`builtin-default`, so a prompt edit blocks every sampling value. Moving
one slider has the same problem: changing Temperature also keeps the
previous model's top-k, max tokens and the rest.

With this PR, `presetEditedKeys` records which sampling controls the
user changed. A prompt edit no longer blocks recommended sampling.
Changing Temperature keeps that temperature, while the other values
follow the next model. The same rule is used for model loads, status
refreshes, comparison panes and Qwen thinking changes.

Custom presets still keep all their values, and a model with remembered
settings still uses them. Picking a preset clears the edited keys.

The edited keys are saved one key at a time, so edits from two open tabs
add up instead of overwriting each other. Settings saved as `modified`
before this change have no record of which controls changed. When the
backend starts, it marks every control as edited for them, so they keep
the old behavior until a preset is picked again.

## Verification

Studio before and after, starting with empty settings. Edits were made
in Run settings; models were loaded through the API and picked up by a
fresh `/chat` page load. These are the temperature / top-p / top-k
values in the outgoing chat request:

| Scenario | Before | After |
|---|---|---|
| Qwen3-0.6B, edit the system prompt, then Gemma 4 E2B | 0.6 / 0.95 / 20
| 1.0 / 0.95 / 64 |
| Gemma 4 E2B, set Temperature to 0.3, then Qwen3-0.6B | 0.3 / 0.95 / 64
| 0.3 / 0.95 / 20 |

The edited system prompt was still sent after the switch in both builds.
These runs used the first revision of this PR. Later commits changed how
the edited keys are saved, not how they are applied.

New tests cover prompt and slider edits, preset changes, per-model
memory, Qwen thinking changes, saving and restoring the edited keys,
edits made before settings finish loading, two open tabs, and the
backend upgrade of older settings. Typecheck and the backend chat
settings and chat history route tests (116) pass. The full frontend
suite has 18 failures, and none of them are in chat settings or presets:
some test files need a newer Node than the one I ran, and the others
depend on locale or time zone.

This touches the same Qwen checks as #15279, so whichever merges second
will need a rebase.

---------

Co-authored-by: Lyxot <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Assign saved inference presets to models and auto-apply on model switch

4 participants