Skip to content

fix: backfill model on gateway sessions after agent runs#996

Closed
heyalchang wants to merge 1 commit intoNousResearch:mainfrom
heyalchang:fix/backfill-session-model
Closed

fix: backfill model on gateway sessions after agent runs#996
heyalchang wants to merge 1 commit intoNousResearch:mainfrom
heyalchang:fix/backfill-session-model

Conversation

@heyalchang
Copy link

Fixes #987

Gateway sessions have model = NULL because the session is created before AIAgent is constructed. After the agent responds, update_session() writes token counts but never backfills the model.

This adds the model to _run_agent()'s return dict and threads it through update_session()update_token_counts(). The SQL uses COALESCE(model, ?) so it only fills NULL — never overwrites a model that was set at creation time (e.g. CLI sessions).

If the agent falls back to a different provider/model, agent.model is updated in-place by _try_activate_fallback(), so the recorded model reflects whichever model actually produced the response.

Tested on a live gateway — new sessions now show the correct model in the DB. Older sessions with NULL are unaffected (COALESCE only fires on the next update_session() call, not retroactively).

Gateway sessions had NULL model because the session is created before
the AIAgent is constructed. Now update_session() accepts a model param
and backfills it via COALESCE (only fills NULL, never overwrites).

Upstream: NousResearch#987

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@heyalchang heyalchang closed this Mar 12, 2026
@heyalchang
Copy link
Author

Redone as #997 — squashed to a single commit and cleaned up whitespace noise in the diff.

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.

Gateway sessions have NULL model in DB

1 participant