Skip to content

The agent that grows with you

chore: stack on #115830 to resolve agent/opencode_affinity.py conflict

teknium111f44afbbdf0
diff --cc agent/opencode_affinity.py
index afe2a32cb7b,824e5df57fb..6f9355c2593
--- a/agent/opencode_affinity.py
+++ b/agent/opencode_affinity.py
@@@ -66,31 -49,40 +67,41 @@@ def opencode_session_headers
base_url: Optional[str],
session_id: Optional[str] = None,
) -> dict[str, str]:
-- """Return ``{"x-opencode-session": <key>}`` for OpenCode targets, else ``{}``."""
++ """Return ``{"x-opencode-session": <key>}`` for OpenCode targets, else ``{}``.
++
++ OpenCode targets always get a key: when no conversation/session key resolves, an
++ ephemeral ``oneshot-<hex>`` value is generated (OpenCode Go rejects requests without
++ the header, #105841)."""
if not is_opencode_target(provider, base_url):
return {}
- try:
- from agent.portal_tags import get_affinity_scope, get_conversation_context
- from agent.transports.codex import _cache_scope_from_session_id
-
- key = _cache_scope_from_session_id(
- # Top-level session_id → OpenRouter's sticky routing key. Per their prompt-caching docs it is
- # used directly as the routing key instead of hashing the opening messages, and it activates
- # stickiness on the first successful request rather than only after a cache hit. Resolve it from
- # the declared routing scope first (set only by a host that names its own conversation, #96811),
- # then the ambient conversation contextvar, with the explicit argument as fallback. The gap this
- # closes is the auxiliary call sites — compression, title generation, vision, web_extract,
- # session_search, MoA slots — which funnel through ``agent.auxiliary_client``. That module has
- # no session handle and passes no ``session_id``, so those calls sent NO sticky key at all and
- # each routed independently of the conversation it belonged to (#70820). Mirrors the Nous Portal
- # profile, which resolves the same way (f2f4df064d). The ambient value is the session-lineage
- # ROOT, so it also stays stable for installs that opt out of the default ``compression.in_place:
- # true`` and across delegate-subagent trees.
- get_affinity_scope() or get_conversation_context() or session_id
- )
- except Exception:
- key = str(session_id or "")
+ key = resolve_affinity_key(session_id)
- return {OPENCODE_SESSION_HEADER: key} if key else {}
+ if not key:
+ # Stateless one-shot requests (commit messages, summaries, standalone prompts outside
+ # a session) lack an ambient conversation or session id. OpenCode Go strictly requires
+ # x-opencode-session on every request (HTTP 400 MissingSessionID if absent, #105841)
+ # so generate an ephemeral session id fallback.
+ key = f"oneshot-{uuid.uuid4().hex[:16]}"
+ return {OPENCODE_SESSION_HEADER: key}
-def merge_opencode_session_headers(
+def custom_provider_session_affinity_headers(
+ base_url: Optional[str],
+ session_id: Optional[str] = None,
+) -> dict[str, str]:
+ """Return ``{<session_affinity_header>: <key>}`` when the route's provider entry declares one, else ``{}``."""
+ try:
+ from hermes_cli.config import get_custom_provider_session_affinity_header
+
+ header = get_custom_provider_session_affinity_header(str(base_url or ""))
+ except Exception:
+ return {}
+ if not header:
+ return {}
+ key = resolve_affinity_key(session_id)
+ return {header: key} if key else {}
+
+
+def merge_session_affinity_headers(
kwargs: dict[str, Any],
provider: Optional[str],
base_url: Optional[str],