Skip to content

Keep a conversation on the upstream that answered it, and hold a turn's route - #237

Merged
fylorn merged 1 commit into
mainfrom
feat/cache-affinity
Sep 29, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/cache-affinity

Conversation

@fylorn

@fylorn fylorn commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Routing rules were re-evaluated on every request, and only load-balance groups pinned a session (by fingerprint modulo). A rule keyed on input_tokens or image could flip halfway through an agent turn, and any failover or reordering moved a conversation away from the upstream holding its prompt cache.

  • Conversation identity (tw_gateway::affinity::identity): a client session header (x-claude-code-session-id, session_id, session-id, conversation_id, x-session-affinity, x-opencode-session) hashed together with the existing content fingerprint (session::fingerprint, which already honours prompt_cache_key), or the fingerprint alone. The fingerprint is kept alongside the header so side requests and sub-agents under one Claude Code session stay separate conversations.
  • Turn detection from the decoded IR: the number of user messages that say something (text/image/file, no tool result); within when the last user message carries a tool result.
  • Route held per turn: the engine's decision at a turn's start is reused for the rest of that turn (same config only). It is re-decided when the estimated input reaches 95% of the smallest known max_input_tokens among the candidates' asked models (price book); unknown windows skip the check.
  • Affinity to the one that answered: recorded when a response finishes with 2xx (via Ending), including a failover member. Scoped by route + group. Stay rules: within a turn always; across turns only if the previous answer's cache_read + cache_write >= 1024 and it was < 5 min ago. Released when the upstream is not Health::is_available, not among the candidates, or in another group. In memory, 4096 conversations, 24 h.
  • Load-balance now only rotates new conversations; the fingerprint-modulo code, Facts::session, groups[].session_affinity and hurts_cache are removed (no strategy drops a warm cache mid-conversation any more).
  • Visible: RequestRouted / RoutingView gain affinity: { held_route, stayed?: "turn" | "cache" } (optional; no DB schema change).

Control API (28 → 29)

  • Removed: GroupView.session_affinity, GroupView.hurts_cache, GroupInput.session_affinity, DryRunResult.hurts_cache.
  • Added: AffinityView, Stay, RequestRouted.affinity, RoutingView.affinity.
  • Config: groups[].session_affinity is gone; Group is deny_unknown_fields, so a config that still has it fails to load.

Not covered

  • Phase-two rules (provider_would_be) are still evaluated per hop.
  • The WebSocket upgrade path is unchanged (one connection already stays on one upstream).

Tests

  • Unit (affinity.rs): header vs hash identity, turn detection, route held within a turn / not across turns / not across config reloads, within-turn stay regardless of cache, cross-turn stay vs release by cache size and age, release when cooling down, failover member becomes the pin, group scoping, bounds/expiry.
  • Integration (tests/affinity.rs): a turn keeps its rule and upstream while its input grows past an input_tokens rule, the next turn stays on the warm upstream, and a new large turn re-decides.

🤖 Generated with Claude Code

…'s route

A conversation (client session header plus the content fingerprint, or the
fingerprint alone) now stays on the upstream that last answered it: always
within a turn (the last user message is a tool result), and across turns
only while the previous answer read or wrote at least 1024 tokens of prompt
cache less than five minutes ago. A pinned upstream that is cooling down is
released, and a failover member that answered becomes the pin. Pins are
scoped by route and group, kept in memory (4096 conversations, 24 hours).

The routing decision made at the start of a turn is reused for the rest of
the turn, so rules keyed on input size or images no longer move a turn
halfway; it is re-evaluated only when the input no longer fits the smallest
known context window among its candidates (from the price book).

This replaces the load-balance fingerprint-modulo pinning: load-balance now
rotates new conversations. `groups[].session_affinity` and `hurts_cache` are
removed. RequestRouted and RoutingView gain `affinity` (held_route, stayed:
turn | cache). Control API version 29.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit fc0d472 into main Sep 29, 2026
4 checks passed
@fylorn
fylorn deleted the feat/cache-affinity branch September 29, 2026 15:25
@fylorn fylorn mentioned this pull request Sep 29, 2026
fylorn added a commit that referenced this pull request Sep 29, 2026
- Bump the workspace version to 0.56.0
- release-notes/0.56.0.md

Includes #237 (conversation affinity, route held per turn), #238 (local
token counts, resend without sealed reasoning) and #239 (fail over at the
start of a stream, pause upstreams by the reason they give).

The control-plane protocol goes from 28 to 30; the request store schema
(22) is unchanged. groups[].session_affinity is removed.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant