Skip to content

The agent that grows with you

fix(gateway): scope out-of-turn compression dedup reset to the live session task

liuhao10248e22d4f1d51b
diff --git a/gateway/run.py b/gateway/run.py
index 8db59e9ec29..ea61ac90406 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -247,7 +247,8 @@ async def run_codex_hygiene_compaction(
count_before = getattr(compressor, "compression_count", 0)
# copy_context carries profile secret scope / HERMES_HOME override (executors don't propagate ContextVars).
worker_future = asyncio.get_running_loop().run_in_executor(
- None, copy_context().run, lambda: agent._compress_context(history, "", approx_tokens=approx_tokens))
+ None, copy_context().run,
+ lambda: agent._compress_context(history, "", approx_tokens=approx_tokens, task_id=session_id))
track_worker = getattr(gateway, "_track_deferred_agent_worker", None)
if callable(track_worker):
# ``wait_for`` only cancels the asyncio wrapper; keep the running executor thread visible to shutdown.
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index b9dd88fe169..77d3afb0289 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -1239,8 +1239,11 @@ class GatewayTurnMixin:
# fails closed (UnscopedSecretError) and every hygiene compaction silently degrades to a
# lossy truncation (#100849 bundle).
copy_context().run,
+ # task_id is the session row id: the live turn scopes skill_view/read_file dedup to it, and
+ # the boundary reset must hit the same bucket or a post-compaction re-read stubs (#98206).
lambda: _hyg_agent._compress_context(
_hyg_msgs, "", approx_tokens=plan.approx_tokens, commit_fence=_hyg_commit_fence,
+ task_id=session_entry.session_id,
),
)
attempt.wait_started = time.monotonic()
diff --git a/tests/gateway/test_session_hygiene.py b/tests/gateway/test_session_hygiene.py
index 440bc54a98f..b7f81d6e1bc 100644
--- a/tests/gateway/test_session_hygiene.py
+++ b/tests/gateway/test_session_hygiene.py
@@ -251,9 +251,15 @@ async def test_session_hygiene_preserves_transcript_when_no_rotation(monkeypatch
self._print_fn = None
self.shutdown_memory_provider = MagicMock()
self.close = MagicMock()
+ self.compress_task_id = None
type(self).last_instance = self
def _compress_context(self, messages, *_args, **_kwargs):
+ # Capture the task scope the gateway forwards (#98206): the dedup
+ # reset inside compress_context is only effective under the live
+ # session row id, the same task_id the main turn hands to
+ # run_conversation.
+ self.compress_task_id = _kwargs.get("task_id")
# No session_db → cannot rotate: session_id is UNCHANGED, and this
# is a failure-to-rotate, not an in-place success.
return ([{"role": "assistant", "content": "summary only"}], None)
@@ -341,6 +347,12 @@ async def test_session_hygiene_preserves_transcript_when_no_rotation(monkeypatch
assert result == "ok"
# The transcript must NOT be rewritten — the original is preserved.
runner.session_store.rewrite_transcript.assert_not_called()
+ # #98206: hygiene compaction must scope the dedup reset to the LIVE
+ # session row id ("sess-1" from this test's SessionEntry) — the same
+ # task_id the main turn passes to run_conversation. Under the "default"
+ # fallback the skill_view/read_file dedup records survive compression
+ # and a re-read returns a stub pointing at pruned context.
+ assert NonRotatingCompressAgent.last_instance.compress_task_id == "sess-1"
# This run neither rotated nor compacted in place, so it did NOT recover
# the session: the reset must NOT have been reached. Spying on the module