← Back to Pull requests

Switch model via session.mutate on session-authority gateways #503

Closed
unknown closed this pull request without merging on Nov 25, 2026 01:44 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 22:49 UTC
Author

Found by a Hermes watch contract audit of Kyllene master against hermes-agent v0.23.4. There's no issue to close.

What was wrong


On a session-authority gateway (gateway.ready.session_authority: true, the gateway-hosted Webapp listener), switching a live chat's model always failed. The pill flipped to the new model, flipped back, and a red banner showed the raw use_session_mutation_model.

  • Kyllene: ChatSession.setModel (ChatSession.kt:1867-1891 on master) always sent config.set {key: "model", value: "<m> --provider <p> --session"}. AuthorityRequests.prepare (AuthorityRequests.kt:37-47) leaves config.set as it is.

  • hermes-agent v0.23.4:

  • - session_tui_config.py:17, :187-194 route the model key to busy_config.
    - session_busy_controls.py:31-35 raises AuthorityRefusal('use_session_mutation_model'), which goes out as wire 4001 (session_controls.py:414-419).
    - This is on purpose (upstream ae2fd61c50). A pick on the authority is session.mutate operation: "model": expected_generation is required, and the payload values must be strings. A guarded (expensive) target answers status: "confirmation_required" with a confirm token, and only that token applies it.
    - Upstream's own desktop client makes the same rewrite (apps/desktop/src/api/canonical-protocol.ts:474-477).
  • Main: the same as v0.23.4 in every file above.

  • Affected: hermes-agent v0.22.1, v0.22.3-v0.23.4 and main, and Kyllene since #402 (v0.17.0+). Older gateways and the legacy dashboard /api/ws don't have the refusal.
  • Audit claims, checked


    | Claim | Result |
    |---|---|
    | setModel sends config.set model "<m> --provider <p> --session" (+ confirm_expensive_model) | Confirmed |
    | The authority refuses it via busy_config, use_session_mutation_model, 4001 | Confirmed. On v0.22.1 the check raises RuntimeStoreError, but the wire reply is the same. v0.22.2 has no authority. |
    | The replacement is session.mutate model {model, provider[, confirm]} | Confirmed, with corrections: the desktop rewrite is at canonical-protocol.ts:474-477, not 456-475, expected_generation is required, and confirm is the gateway's token string (a boolean is invalid_params). |
    | config.set reasoning/fast/yolo still work; only model breaks | Confirmed. Those paths are unchanged. |
    | Older gateways still need config.set | Confirmed. The legacy tui_gateway has no session.mutate. |

    The fix


  • On client.sessionAuthority, setModel sends session.mutate with:

  • - operation: "model" and payload: {model, provider}
    - expected_revision and expected_generation (the session's fences)
    - a fresh request_id
  • Shared send: mutateBranch became mutateFenced(operation, …), which branch and model both use. A stale-fence refusal is sent once more at the fences it carries, and a second one rolls back with "The chat changed on the gateway while switching the model."

  • Confirmation: confirmation_required still returns NeedsConfirmation. Its confirm token is kept for that model and provider and goes back as payload.confirm when the user confirms. A stale token just asks again. The UI is unchanged.

  • Running turn: a pick during a turn is answered deferred, which counts as switched. v0.22.1 has no deferred pick and refuses it as session_busy; the banner then says "The model can't change while a reply is running. Switch it when this one is done." instead of the raw reason.

  • Guarded pick: detected by confirm_required or status: "confirmation_required". Every version sends both, but either is enough.

  • Fallback: a session authority that answers session.mutate with -32601 gets today's config.set. Every authority version in the window has the method, so this is only a guard.

  • Other gateways (session_authority absent or false) keep config.set exactly as before: same method and params, no new keys.

  • Not covered: a deferred pick that needs confirmation at the turn boundary (a session.updated with confirmation_required). It is left for a follow-up.

  • ROADMAP §7 says how the model switch works on the authority now.
  • Back-compat: hermes-agent v0.22.0 to v0.23.4


    The mode switch is gateway.ready.session_authority (JsonRpcClient.sessionAuthority), not a version number.

    | Version | Legacy sidecar (config.set model) | Session authority (session.mutate model) |
    |---|---|---|
    | v0.22.0 | Unchanged request; config.set with confirm_expensive_model is in tui_gateway/methods_config_set.py | No session authority |
    | v0.22.1 | Unchanged | validate_action('model', …) in hermes_state_mutations.py accepts {model, provider} and {…, confirm: "<token>"}, rejects a boolean confirm (run in-process); expected_generation required (session_mutations.py:105); no deferred pick, so a running turn is session_busy (handled) |
    | v0.22.2 | Unchanged | No session authority |
    | v0.22.3 | Unchanged | Same validate_action results (in-process); deferred pick present |
    | v0.22.7 | Unchanged | Same validate_action results (in-process). Live: config.set model → 4001 use_session_mutation_model; session.mutate model → {"revision":1,"execution_generation":1,"operation":"model","model":"test-model-b","provider":"custom"}; session.info → test-model-b |
    | v0.23.4 | Unchanged | parse_mutation('model', …) in persistence/authority/runtime.py gives the same results (in-process). Live: same refusal and acceptance as v0.22.7, plus the app run below |

    Legacy rows were checked from source, not live: the request on that path is byte-for-byte what master sends. The scripted live probe of each version's dashboard /api/ws (the legacy path, and where v0.22.0 and v0.22.3 serve their sockets, since they have no Webapp listener) failed before any chat call, because the dashboards refused the script's ws-ticket after a password login (401). So v0.22.0 and v0.22.3 were not run live.

    Tests


    New shared/core/src/commonTest/.../chat/ModelSwitchTest.kt (FakeGateway; authority and legacy):
  • theSessionAuthoritySwitchesWithAFencedModelMutation: checks the method, operation, fences, request id and string payload, that no config.set model is sent, and that reasoning still uses config.set.

  • anExpensivePickOnTheSessionAuthorityIsConfirmedWithTheGatewaysToken: the token goes back as a string.

  • aStaleConfirmationOnTheSessionAuthorityAsksAgain: also checks that a token is only sent for its own pick.

  • aPickDuringATurnOnTheSessionAuthorityIsDeferredNotFailed

  • aStaleFenceOnTheSessionAuthorityIsRetriedOnceWithTheCurrentOnes

  • aSecondStaleFenceOnTheSessionAuthorityRollsBackWithAReadableError

  • aGuardedPickMarkedOnlyByItsStatusStillAsks

  • aPickDuringATurnOnAnEarlySessionAuthorityIsExplained (v0.22.1's session_busy)

  • aSessionAuthorityWithoutModelMutationsFallsBackToConfigSet (-32601)

  • anOlderGatewayStillSwitchesOverConfigSet: config.set … --session, then confirm_expensive_model: true, and no session.mutate.
  • Runs:

  • ./gradlew :shared:core:testAndroidHostTest --tests "dev.hermeskotlin.core.chat.ModelSwitchTest"

  • - With the fix: 10/10 pass.
    - With master's ChatSession.kt (first commit's tests): 6 of 7 fail. Only the legacy test passes, as it should.
  • ./gradlew :shared:core:testAndroidHostTest :shared:ui:testAndroidHostTest: BUILD SUCCESSFUL, 2328 tests, 0 failures (before the status-only test; ModelSwitchTest and ChatSessionTest re-run after it: pass). This includes ChatSessionTest's existing legacy model test and the #467 branch tests.

  • ./gradlew :androidApp:assembleDev :desktopApp:compileKotlinJvm :androidApp:lintDebug: BUILD SUCCESSFUL.
  • Live gateway run


    Kyllene Dev from this branch on the emulator, against hermes-agent v0.23.4's Webapp listener (session_authority: true). The fake model logs the model each turn asks for:

    1. In an open chat, sent "turn before switch". Fake model: request model=test-model.
    2. Picked Test Model B. The pill stays on Test Model B and no banner shows (before this fix the gateway refused with use_session_mutation_model; the pill flipped back and a red banner showed it).
    3. Sent "turn after switch". Fake model: request model=test-model-b.

    The request probe (raw /api/ws) on v0.23.4:

    gateway.ready session_authority = True
    >> config.set {"session_id": "local-…", "key": "model", "value": "test-model-b --provider custom --session"}
    << {"code": 4001, "message": "use_session_mutation_model", "data": {"reason": "use_session_mutation_model"}}
    >> session.mutate {"session_id": "local-…", "request_id": "…", "expected_revision": 0, "expected_generation": 0, "operation": "model", "payload": {"model": "test-model-b", "provider": "custom"}}
    << {"session_id": "local-…", "revision": 1, "execution_generation": 1, "operation": "model", "model": "test-model-b", "provider": "custom"}
    >> session.info → … "model": "test-model-b", "provider": "custom" …
    

    v0.22.7 gave the same three answers.

    Not covered live: the confirmation round trip on a real guarded (expensive) model, since the fake model never triggers a guard. Its shape comes from source, and the unit tests cover it.

    Review follow-up


  • Rebased on master.

  • v0.22.1 stale fence: v0.22.1's authority refuses a stale fence (revision_conflict) without the current revision and generation. v0.22.3 and later include them. The banner then showed the raw reason. It now says "The chat changed on the gateway while switching the model." This also covers a branch on v0.22.1. Test: aStaleFenceWithoutTheCurrentOnesRollsBackWithAReadableError, which fails without the fix.
  • Live matrix (raw JSON-RPC, each tag's own gateway and test home)


    | Version | Session authority: config.set model / session.mutate model / boolean confirm / stale fence | Legacy dashboard /api/ws: session.mutate / config.set model … --session (± confirm_expensive_model) |
    |---|---|---|
    | v0.22.0 | No authority (no session_mutations.py) | -32601 / applied |
    | v0.22.1 | 4001 use_session_mutation_model / applied, session.updated revision 1 / invalid_params / revision_conflict without fences | -32601 / applied |
    | v0.22.2 | No authority (no session_mutations.py) | -32601 / applied |
    | v0.22.3 | same / applied / invalid_params / revision_conflict with fences | -32601 / applied |
    | v0.22.4, v0.22.5, v0.22.6, v0.22.7 | same as v0.22.3 | -32601 / applied |
    | v0.23.0, v0.23.1, v0.23.2, v0.23.3, v0.23.4 | same as v0.22.3 | -32601 / applied |

    Every stable tag from v0.22.0 to v0.23.4 is in the table; canary tags are left out.

    v0.22.1, v0.22.3, v0.22.4 and v0.22.5 have no Webapp listener and serve the authority only on a loopback listener that takes a control-socket ticket, so the phone can't reach it. They were probed with hermes' own local client. The legacy dashboards were probed the way the app signs in: password login, ws-ticket, then the ticket subprotocol. Their custom provider needs a named custom_providers entry before a legacy switch resolves; that is a test-setup detail, and master sends the same request.

    App run on the emulator (v0.23.4, head 53c43b51)


  • Session authority: the turn before the pick asks the model for test-model. Picking Test Model B keeps the pill with no banner, and the next turn asks for test-model-b. Switching back gives test-model, so a second fenced switch works. A pick during a running turn: the running turn keeps test-model, and the next one asks for test-model-b.

  • The same pick on master: a red use_session_mutation_model banner, and the pill flips back.

  • Legacy (v0.23.4 hermes dashboard): the turn before the pick asks for test-model, and the turn after asks for test-model-b, with no banner.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: Giton22 Review duration: 174m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.75) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #503"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.7% · Safe to merge
    29.7%