Skip to content

fix(state): use COALESCE(?, model) so mid-session model switches are persisted - #49682

Closed
uzunkuyruk wants to merge 1 commit into
NousResearch:mainfrom
uzunkuyruk:fix/model-coalesce-order-34850
Closed

fix(state): use COALESCE(?, model) so mid-session model switches are persisted#49682
uzunkuyruk wants to merge 1 commit into
NousResearch:mainfrom
uzunkuyruk:fix/model-coalesce-order-34850

Conversation

@uzunkuyruk

Copy link
Copy Markdown
Contributor

Fixes #34850

Problem

update_token_counts() uses model = COALESCE(model, ?) in both the
absolute=True and absolute=False SQL paths. Once a session's model
column is set at creation, any subsequent call with a new model value is
silently discarded — /model switches never reach the database.

Fix

Swap argument order to COALESCE(?, model): the provided value takes
priority when non-NULL; falls back to the existing column value otherwise.

Test

db.update_token_counts(session_id, model="gpt-4o")
db.update_token_counts(session_id, model="claude-opus-4-8")
session = db.get_session(session_id)
assert session["model"] == "claude-opus-4-8"  # previously returned "gpt-4o"

@alt-glitch alt-glitch added type/bug Something isn't working comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists labels Jun 20, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the report and the digging, @uzunkuyruk — but this is already fixed on main, and the proposed swap would introduce a different bug.

Already fixed: Issue #34850 was closed as completed on May 30 via merged PR #35256, which added a dedicated update_session_model(session_id, model) method (hermes_state.py:1563) that unconditionally sets the model column. The gateway /model handler calls it to persist mid-session switches — exactly the issue's own primary suggested fix.

Why the swap is the wrong fix: The COALESCE(model, ?) in update_token_counts() is intentional backfill-only behavior, documented at hermes_state.py:1566-1568 as the deliberate contrast to update_session_model(). update_token_counts() runs on every per-API-call token update (the hot path) with whatever model the caller happens to pass; changing it to COALESCE(?, model) would make every token update clobber the model column and fight the canonical writer (update_session_model), producing inconsistent values rather than fixing anything.

Closing as already-resolved. The model-persistence path you want lives in update_session_model(). Appreciate the contribution.

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

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Model switch mid-session not persisted to database (dashboard shows stale model)

3 participants