Skip to content

feat(models): seamless model switch that always keeps the conversation (#20) - #295

Merged
JAORMX merged 3 commits into
mainfrom
feat/model-switch-carryover
Jul 23, 2026
Merged

JAORMX merged 3 commits into
mainfrom
feat/model-switch-carryover

Conversation

@JAORMX

@JAORMX JAORMX commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #20.

A model switch now keeps the conversation instead of restarting cold. Picking a model in the /models picker switches immediately — no confirm overlay, no fresh-vs-carry choice — and the new session is seeded from the current one. /clear remains the single explicit fresh-start verb.

Server (internal/adapter/server): CreateSessionRequest.source_session_id (additive proto field 8) seeds a new session from a source via the existing session.ForkSnapshot + SeedHistory primitives (zero engine-loop change). validateCarryover loads the source through the run-entry funnel loadAndReopen, rejects a running/awaiting source (FailedPrecondition), and seeds before the first Store.Save. Carryover is always allowed across providers:

  • same provider → the snapshot replays verbatim (Reasoning/ProviderPhase/ToolCall.ItemID intact, warm prompt-cache prefix);
  • cross provider → session.StripProviderState clears the provider-private replay blobs, yielding a provider-neutral history both adapters omit safely; the new session's thinking/reasoning config is derived from the new model, not the stripped history.

Cross-provider onto OpenAI additionally synthesizes stable, deterministic, non-fc_-prefixed ItemIDs (carryover_item_N) on the carried tool calls: the OpenAI store:false replay de-duplicates function_call items by the provider id, and an id-less carried item collides with auto-assigned fc_N on the second turn (Azure GPT-5.x "Duplicate item" 400). Anthropic ignores ItemID, so non-OpenAI destinations stay bare-stripped.

Domain (engine/session): new exported StripProviderState — a pure, non-mutating transform clearing Message.Reasoning, Message.ProviderPhase, and every ToolCall.ItemID while preserving roles/text/tool-call IDs/Args/tool results/parts. Classified Added (minor) per engine/COMPATIBILITY.md; engine/api/session.txt regenerated.

mecatui: the modelsConfirm overlay (enter/c/s/esc) is removed. chooseModel now routes directly — a live session switches via restartOnModelWithCarryover (CreateSessionWithCarryover); no live session falls back to a plain create. A one-shot status note names the switch ("switched to <model> — conversation kept"; cross-provider adds "prior reasoning cache dropped"). /clear is untouched and documented as the fresh-start.

Design

This started as a same-provider-only carryover behind a [c] key (commit f5a31fa4). Review feedback reframed it: the [c]/fresh-start choice is redundant with /clear, and the right default is that a switch never drops the thread. The feasibility of cross-provider carryover turned on one fact — both adapters treat an empty replay blob as "no blob" and omit it on the wire (OpenAI request.go:340/360/368, Anthropic reasoning.go:68), so a stripped history replays safely to any provider. The redesign commits to that.

ADR 0016 (which documents the removed overlay UX) is superseded by a follow-up ADR.

Tests (offline: mockllm/memfs/fakeConv/teatest)

  • StripProviderState unit — all three blob fields, parts/tool-result preserved, no input mutation, tool-pairing-valid, backing-array isolation.
  • Service-level: same-provider-verbatim vs cross-provider-strip vs OpenAI-synthesis (determinism pinned).
  • gRPC + HTTP handler wiring, incl. a cross-provider gRPC round-trip (mutation-verified: dropping the field-wiring turns them red).
  • Running/awaiting/missing-source rejection; turn-zero compaction through the real loop.
  • UI seamless-switch unit + teatest e2e: live-session carry, no-session plain create, cross-provider still carries, status-note wording, overlay removal leaves no dangling refs.

Review

Ran the dev-pipeline: architect plan → implement (strip primitive → server strip → overlay removal → ItemID fix) → review panel (security / architecture / duplication / reuse / spec) + QA test-adequacy → iterate → commit. Security surfaced the OpenAI ItemID de-dup finding (fixed, not deferred); architecture a stale comment (fixed); QA's two must-add coverage gaps (fixed). Panel otherwise clean.

Test plan

  • task test (root + engine -race, plus GOWORK=off engine-standalone) — green
  • task lint — 0 issues (both modules)
  • task api:check — engine surface gate green (StripProviderState Added/minor)
  • task docs — llms.txt regenerated, strict link gate green
  • Live Azure GPT-5.x e2e (task e2e) — the ItemID synthesis is verified offline (present/stable/collision-free); a live run would confirm Azure's de-dup accepts the synthesized ids. Optional, not part of the gate.

Co-Authored-By: mecatl [email protected]

JAORMX and others added 3 commits July 23, 2026 13:43
)

The /models picker restart-now flow starts a new session on the picked model
(provider/model fixed per session => switching = new CreateSession), and until
now the transcript was always reset. Add an optional same-provider carryover:
the confirm overlay gains a [c] carry-over key that seeds the new session with
the current session's conversation, so a model switch can feel like a
continuation rather than a cold start.

Wire: one additive proto field, CreateSessionRequest.source_session_id (8),
server-side so the service owns load/validate/seed and the same-provider gate.

Server (internal/adapter/server):
- validateCarryover loads the source via the run-entry funnel loadAndReopen,
  rejects a running/awaiting source (FailedPrecondition), rejects a
  cross-provider switch (InvalidArgument; provider canonicalised to the
  default when empty; the MODEL is intentionally not compared — v1 carries the
  conversation onto a different model within the same provider), and snapshots
  via session.ForkSnapshot. seedCarryover seeds via SeedHistory before the
  first Store.Save. Reuses the ADR-0065 fork primitives; zero engine/domain
  change, port.LLMRequest untouched.
- Threaded through the gRPC CreateSession handler and the HTTP POST
  /v1/sessions handler.

mecatui: [c] key gated on liveProviderID() == candidate provider (offered only
for a same-provider switch); restartOnModelWithCarryover + carryoverCmd mirror
restartOnModel, closing the old session best-effort after the new one is
ready; CreateSessionWithCarryover added to the client and the SessionCreator
interface (additive sibling).

Turn-zero compaction needs no new code: maybeCompact already runs at every
turn top against the new model's live-resolved window, with the recent user
instructions surviving via snapCutToRecentUserTurn.

Cross-provider carryover (strip Message.Reasoning/ProviderPhase) is deferred
to v2 — v1 rejects it loudly rather than silently stripping.

Tests (offline, mockllm/memfs/teatest): service-level seed/same-provider/
both-default/running+awaiting/missing; gRPC + HTTP handler-wiring (mutation-
verified: dropping the field-wiring line turns them red); turn-zero compaction
through the real loop (recent user turn survives verbatim, history stays
tool-pairing-valid); UI confirm-key unit + teatest e2e (handoff, cross-provider
key swallowed, failure recoverable); new models_confirm_carryover golden.

Docs: IMPLEMENTATION-NOTES carryover section, PRODUCTION-READINESS flip,
ADR 0016 mid-session-switch note, ADR 0065 see-also, docs/tui.md [c] key,
user-docs gRPC/HTTP surface.

Co-Authored-By: mecatl <[email protected]>
#20)

A model switch now keeps the conversation instead of restarting cold. Picking a
model in the /models picker switches immediately — no confirm overlay, no
fresh-vs-carry choice — and the new session is seeded from the current one.
/clear remains the single explicit fresh-start verb.

Server (internal/adapter/server): CreateSessionRequest.source_session_id
(additive proto field 8) seeds a new session from a source via the existing
session.ForkSnapshot + SeedHistory primitives (zero engine-loop change).
validateCarryover loads the source through the run-entry funnel loadAndReopen,
rejects a running/awaiting source (FailedPrecondition), and seeds before the
first Store.Save. Carryover is always allowed across providers:

  - same provider  -> the snapshot replays VERBATIM (Reasoning/ProviderPhase/
    ToolCall.ItemID intact, warm prompt-cache prefix);
  - cross provider -> session.StripProviderState clears the provider-private
    replay blobs, yielding a provider-neutral history both adapters omit
    safely; the new session's thinking/reasoning config is derived from the
    NEW model, not the stripped history.

Cross-provider onto OpenAI additionally synthesizes stable, deterministic,
non-fc_-prefixed ItemIDs (carryover_item_N) on the carried tool calls: the
OpenAI store:false replay de-duplicates function_call items by the provider
id, and an id-less carried item collides with auto-assigned fc_N on the second
turn (Azure GPT-5.x "Duplicate item" 400). Anthropic ignores ItemID, so
non-OpenAI destinations stay bare-stripped.

Domain (engine/session): new exported StripProviderState — a pure, non-mutating
transform clearing Message.Reasoning, Message.ProviderPhase, and every
ToolCall.ItemID while preserving roles/text/tool-call IDs/Args/tool results/
parts. Classified Added (minor) per COMPATIBILITY.md; engine/api/session.txt
regenerated.

mecatui: the modelsConfirm overlay (enter/c/s/esc) is removed. chooseModel now
routes directly — a live session switches via restartOnModelWithCarryover
(CreateSessionWithCarryover), no live session falls back to a plain create. A
one-shot status note names the switch ("switched to <model> — conversation
kept"; cross-provider adds "prior reasoning cache dropped"). /clear is
untouched and documented as the fresh-start.

Tests (offline: mockllm/memfs/fakeConv/teatest): StripProviderState unit
(all three blob fields, parts/tool-result preserved, no input mutation,
tool-pairing-valid, backing-array isolation); service-level same-provider-
verbatim vs cross-provider-strip vs OpenAI-synthesis (determinism pinned);
gRPC + HTTP handler wiring incl. a cross-provider gRPC round-trip
(mutation-verified); running/awaiting/missing-source rejection; turn-zero
compaction through the real loop; UI seamless-switch unit + teatest e2e
(live-session carry, no-session plain create, cross-provider still carries,
status-note wording, overlay removal leaves no dangling refs).

Docs: IMPLEMENTATION-NOTES + PRODUCTION-READINESS updated, docs/tui.md /models
rewritten, user-docs gRPC/HTTP surface, proto comment. ADR 0016 (which
documents the removed overlay UX) is superseded by a follow-up ADR.

Co-Authored-By: mecatl <[email protected]>
…tion

Records the redesign that supersedes ADR 0016's mid-session-switch confirm
overlay and same-provider carryover gate, and ADR 0065's same-provider
carryover constraint: a model switch always keeps the conversation (verbatim
same-provider, StripProviderState cross-provider, OpenAI ItemID synthesis).
Adds the 'Superseded by' pointers on the two frozen ADRs (the only edit a
frozen ADR takes).

Co-Authored-By: mecatl <[email protected]>
@JAORMX
JAORMX merged commit 957335c into main Jul 23, 2026
14 checks passed
JAORMX pushed a commit that referenced this pull request Jul 25, 2026
 (#308)

OpenRouter rides the same openai-adapter ItemID-dedup path as OpenAI, so
a cross-provider switch onto it hit the same "Duplicate item" collision
the OpenAI-specific synthesis was meant to prevent — gate on openrouter
too, not just the literal "openai" provider id.

A failed carryover create's enter-to-retry re-fires a plain, non-carryover
create, but the switch-note armed by the original pick survived the
failure and falsely reported "conversation kept" on the retry's success.
Clear it in the restartFailedMsg reducer.

Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

Model switch: optional context carryover into the new session

1 participant