Skip to content

fix(sdk): preserve mid-session model change across SDK warmup cycles#321

Closed
akuehner wants to merge 1 commit intochadbyte:mainfrom
akuehner:fix/preserve-mid-session-model-change
Closed

fix(sdk): preserve mid-session model change across SDK warmup cycles#321
akuehner wants to merge 1 commit intochadbyte:mainfrom
akuehner:fix/preserve-mid-session-model-change

Conversation

@akuehner
Copy link
Copy Markdown
Contributor

Problem

When a user changes the model mid-session (via the model picker), the change is stored in sm.currentModel. However, on each subsequent SDK warmup — which fires on session reconnect, tab reload, or session resumption — two sites unconditionally overwrite sm.currentModel with sm._savedDefaultModel, resetting the user's choice back to the project or server default without any action on their part.

The symptom: you switch to sonnet, send a message, and Clay silently resets back to opus (or whatever the default is). The UI reflects the reset and the actual API calls use the wrong model.

Root Cause

Two warmup completion paths use the same pattern:

// sdk-message-processor.js:135 (system/init path)
sm.currentModel = sm._savedDefaultModel || parsed.model;

// sdk-bridge.js:1011 (warmup worker result path)
sm.currentModel = sm._savedDefaultModel || result.model;

Both ignore any value already in sm.currentModel, so a mid-session set_model call is always clobbered on the next warmup.

Fix

Prepend a sm.currentModel check in both paths so an already-set in-memory value is preserved:

sm.currentModel = sm.currentModel || sm._savedDefaultModel || parsed.model;
sm.currentModel = sm.currentModel || sm._savedDefaultModel || result.model;

Priority order:

  1. In-memory current value (set by user this session via set_model)
  2. Persisted project/server default (_savedDefaultModel)
  3. SDK-reported model (fallback if nothing is set)

Testing

  1. Set a project or server default model (e.g. opus)
  2. Start a session — confirm model shows the default
  3. Switch model mid-session via the picker (e.g. to sonnet)
  4. Send a follow-up message
  5. Confirm the model does not reset to the default
  6. Start a fresh session — confirm the default is restored correctly (since currentModel starts unset)

When a user changes the model mid-session via set_model, the change is
stored in sm.currentModel. However, on each subsequent SDK warmup (which
fires on session reconnect, tab reload, or session resumption), two sites
unconditionally overwrote sm.currentModel with sm._savedDefaultModel,
resetting any user-initiated change back to the project or server default.

Fix: check sm.currentModel first in both warmup paths so an already-set
in-memory value is preserved. Fall back to _savedDefaultModel (persisted
default) and then to the SDK-reported model only when no current value
exists.

Affected: sdk-message-processor.js (system/init path) and sdk-bridge.js
(warmup worker result path).
@chadbyte chadbyte closed this in 420ad34 Apr 18, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This issue has been resolved in version 2.32.0-beta.5 (main).

To update, run:

npx clay-server@2.32.0-beta.5

-- Clay Deploy Bot

Build anything, with anyone, in one place.

@chadbyte
Copy link
Copy Markdown
Owner

Thanks for the PR! The fix is valid. Since the PR was opened, there have been major changes on main (Codex support, etc.), which caused merge conflicts. I resolved them manually and applied the same fix on main directly. The field name also changed from result.model to result.defaultModel on the current main, so I adjusted accordingly.

Committed in 420ad34. Closing this PR. Thanks again for catching this!

@github-actions
Copy link
Copy Markdown
Contributor

This issue has been resolved in version 2.32.0 (stable).

To update, run:

npx clay-server@2.32.0

-- Clay Deploy Bot

Build anything, with anyone, in one place.

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.

2 participants