From b4cacba6ae34ba20a16508d5d44807e5331fa728 Mon Sep 17 00:00:00 2001 From: Evo Date: Mon, 15 Jun 2026 20:46:57 +0800 Subject: [PATCH] fix(gateway): re-baseline agent-cache message_count before in-band queued follow-up turn The cross-process cache-coherence guard (#45966) re-baselines the cached agent's message_count only on the external-turn boundary (#46237, at _handle_message_with_agent). The in-band queued (/queue) follow-up recurses into _run_agent mid-chain with the stale build-time snapshot, so the follow-up's guard sees the first turn's own writes as a mismatch and rebuilds the agent -- re-introducing the every-turn rebuild / prompt-cache destruction #46237 set out to prevent, on the in-band path. Re-baseline before the recursion, symmetric with the accepted external-path fix. --- gateway/run.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gateway/run.py b/gateway/run.py index 9a34a066659..ca746bb7837 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -18206,6 +18206,22 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew except Exception: pass + # Re-baseline the cached agent's message_count snapshot before + # recursing into the in-band queued (/queue) follow-up turn. + # The first turn has completed and flushed its own user + + # assistant rows to the SessionDB, so the cross-process + # coherence guard (#45966) — which this recursive _run_agent + # call re-enters — would otherwise see the grown on-disk count + # against the stale build-time snapshot and rebuild the agent + # on THIS process's OWN writes, destroying the prompt-cache + # prefix #46237 was merged to preserve. The existing + # re-baseline in _handle_message_with_agent only runs after the + # whole _run_agent chain unwinds — too late for the in-band + # follow-up. Use the same (session_key, session_id) the + # recursive call runs under so the snapshot matches exactly + # what the follow-up's guard will consult. Fail-safe in helper. + self._refresh_agent_cache_message_count(session_key, session_id) + followup_result = await self._run_agent( message=next_message, context_prompt=context_prompt,