Repository navigation
# Refinement requests reuse the conversation's session id, which makes session-keyed proxies replay the whole conversation #3967
lsvab
started this conversation in
Feature requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Category: Feature request
Version: prime-agent 0.10.2 (Rust), checked against
main@bba5bcfSummary
Refiner calls (
refine.run(),/refine, auto-refine review) send the conversation's ownsession_id. With anopenai-completionsprovider that hascompat.sendSessionAffinityHeaders: true, this becomes thex-session-affinityheader. A proxy that keys conversation state by that header then treats the one-off refiner prompt as a turn of the main conversation. The next real turn no longer matches the stored history, so the proxy replays the full conversation.I would like to discuss how refiner calls could stay separate from the conversation for such proxies while keeping the owning-session identity that #2266 added for OpenCode.
Setup
api: "openai-completions",compat.sendSessionAffinityHeaders: true, pointing to a local proxy that runs Claude through the Claude Agent SDK (Meridian). The proxy keys its SDK sessions byx-session-affinity.lineage=continuation, ~99% prompt-cache hits).Observed behavior
x-session-affinityvalue and a 2-message body. The proxy logsSession not resumable … reason=unrelated-historyand starts a fresh session for that key.unrelated-historyagain and replays the whole conversation.Measured on one test session (~50k-token context):
continuation, ~185 cache-write tokensnew(full replay), ~50,300 cache-write tokensWithout an extra proxy-side flag for concurrent requests per key, the overlap sometimes also fails the main request with
400 session_turn_conflict, and the turn stalls. Prime's provider retry does not recover from it.auxiliaryModeldoes not help here. It moves compaction and branch summaries to another model (compact_session.rs,branch_summarization.rs→resolve_auxiliary_model), but refiner calls always use the session model and session id.Code path
crates/pa-core/src/session_engine/refine.rs—refiner_call()("The refiner model call on the session model") callssession_fallback_with_headers()even when an auxiliary context exists, soauxiliaryModelis only used for headers.refine.rs—default_refiner_call()setssession_id: Some(session_id)("they keep the conversation id (cache key, opencode routing)").crates/pa-ai/src/providers/openai_completions/params.rs—session_id→session_id,x-client-request-id,x-session-affinityheaders whensend_session_affinity_headersis set.refinement_and_auto_review_send_the_session_request_identityasserts the current behavior.Related
I understand this is intentional. #2131 and #2266 (from #2110 / #2072) added the owning session identity to compaction, refinement planning, auto-refine review and branch summaries, because OpenCode Zen/Go reject requests without
x-opencode-session. #2266 states that every maintenance request should carry the owning session header. I do not want to break that contract. The question is how to keep it and still separate refiner traffic from the conversation for session-keyed proxies.In the Rust port,
StreamOptionshas a singlesession_id. Inopenai_completionsit feeds two separate parameters ofbuild_headers():cache_session_id(session_id,x-client-request-id,x-session-affinity; plusprompt_cache_keyin the body for OpenAI endpoints / long retention) andconversation_id(x-opencode-session). So the split between cache/affinity key and conversation identity already exists at the header level. Only the input is shared.The refiner prompt has its own system prompt and a single user message containing the transcript text. It shares no prefix with the conversation, so reusing the conversation id gives it no prompt-cache benefit on the conversation's cache.
Possible options
A. Route refiner calls through
auxiliaryModel, like compaction summaries (resolve_auxiliary_model(…, "refinement", …), with the same context-window fallback). The owning session identity stays as it is. Users with a session-keyed proxy can point refinement at another provider. This seems the most consistent with #2266.B. Separate the cache/affinity key from the conversation identity for refiner calls. For example, an optional cache-key field in
StreamOptionsthat refiner calls set to<session_id>:refine.x-opencode-sessionkeeps the owning session id; onlyx-session-affinity/session_id/prompt_cache_keychange. The refiner prompt shares no prefix with the conversation, so it loses no cache benefit.C. Derived
session_idfor refiner calls (<session_id>:refine). This is the smallest change (one line), but it also changesx-opencode-session, so it conflicts with the #2266 contract (the header is still present, but it no longer names the owning session).Local validation
I implemented option B and measured it, together with option C for comparison.
Option B (11 files, +56/−14): new optional
StreamOptions::cache_session_idwith acache_key()helper that falls back tosession_id. Cache/affinity sites usecache_key():session_id/x-client-request-id/x-session-affinityheaders andprompt_cache_key(OpenAI Completions/Responses, Azure, Codex), and Mistralx-affinity. Identity sites keepsession_id:x-opencode-sessionand the Codex WebSocket connection. Refiner calls keepsession_id= owning session and setcache_session_id = "<session_id>:refine". All other callers leave the new fieldNone, so their behavior does not change.cargo fmt --all --check: ok.cargo test -p pa-aiandcargo test -p pa-core --lib: 1641 passed, 0 failed. This includestests/opencode_session.rsandrefinement_and_auto_review_send_the_session_request_identity. The latter still asserts the owning session id, and now also asserts the separate cache key.cargo build --release -p pa-cli: ok.cargo clippy -p pa-ai -p pa-core --all-targets -- -D warnings: the only remaining errors are two lints from my newer local toolchain (Rust 1.99):refine.rs:673#[must_use]andcompaction_arms.rs:67. The same errors appear on unmodifiedmain, in code this change does not touch. (deprecatedandclippy::assert_is_emptywere allowed for the same reason; they also fail in untouched crates onmain.)Live test through the proxy (turn 1: REPL call +
refine.run(); turn 2: two REPL calls):mainunrelated-historynew(full replay), ~50,300 cache-write tokensnot-found)continuation, 181 cache-write tokensnot-found)continuation, 185 cache-write tokensThere were no errors in either variant, and the conversation state was correct after the refine.
Patch (option B)
If option A fits the project better, I can test it locally the same way and report the results here.
All reactions