Skip to content

The agent that grows with you

fix(agent): isolate background review snapshots

notkisk619ca3011ef1
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.py
index 30b86c61710..87e6bc5d7c9 100644
--- a/agent/codex_runtime.py
+++ b/agent/codex_runtime.py
@@ -933,8 +933,15 @@ def run_codex_app_server_turn(
and (should_review_memory or should_review_skills)
):
try:
+ # Keep the review fork's in-place transcript normalization from
+ # mutating the live foreground messages after persistence. A
+ # shallow list copy still aliases nested tool-call/content data,
+ # which can make the next rebuilt request diverge from the cached
+ # prefix.
+ from agent.turn_finalizer import _clone_background_review_messages
+
agent._spawn_background_review(
- messages_snapshot=list(messages),
+ messages_snapshot=_clone_background_review_messages(messages),
review_memory=should_review_memory,
review_skills=should_review_skills,
)
diff --git a/agent/turn_finalizer.py b/agent/turn_finalizer.py
index 193c56461d8..dbe073495f3 100644
--- a/agent/turn_finalizer.py
+++ b/agent/turn_finalizer.py
@@ -126,6 +126,15 @@ def _drop_verification_continuation_scaffolding(messages) -> None:
]
+def _clone_background_review_messages(messages):
+ """Copy the review input without aliasing the live transcript."""
+ # Import lazily: conversation_loop imports this module during turn
+ # finalization, so a module-level import would create a cycle.
+ from agent.conversation_loop import _clone_message_for_send
+
+ return [_clone_message_for_send(message) for message in messages]
+
+
def finalize_turn(
agent,
*,
@@ -810,8 +819,14 @@ def finalize_turn(
and (_should_review_memory or _should_review_skills)
):
try:
+ # The review fork sanitizes and repairs its private transcript in
+ # place. A shallow list copy would leave the message dicts (and
+ # nested tool-call/content containers) shared with the live
+ # foreground transcript, allowing the review to mutate the
+ # representation that was just persisted and break prefix-cache
+ # parity on the next turn.
agent._spawn_background_review(
- messages_snapshot=list(messages),
+ messages_snapshot=_clone_background_review_messages(messages),
review_memory=_should_review_memory,
review_skills=_should_review_skills,
)
diff --git a/tests/run_agent/test_background_review.py b/tests/run_agent/test_background_review.py
index ca2ebea949b..7a10ef16e3a 100644
--- a/tests/run_agent/test_background_review.py
+++ b/tests/run_agent/test_background_review.py
@@ -461,6 +461,27 @@ def test_background_review_registers_before_start_runs_and_cleans_up(monkeypatch
assert agent._active_children == []
+def test_background_review_snapshot_isolated_from_live_nested_messages():
+ """A review must not mutate the persisted/live transcript through aliases."""
+ original = [{
+ "role": "assistant",
+ "content": [{"type": "text", "text": "answer"}],
+ "tool_calls": [{
+ "id": "call-1",
+ "function": {"name": "read_file", "arguments": '{"path":"x"}'},
+ }],
+ }]
+
+ from agent.turn_finalizer import _clone_background_review_messages
+
+ snapshot = _clone_background_review_messages(original)
+ snapshot[0]["content"][0]["text"] = "review mutation"
+ snapshot[0]["tool_calls"][0]["function"]["arguments"] = "{}"
+
+ assert original[0]["content"][0]["text"] == "answer"
+ assert original[0]["tool_calls"][0]["function"]["arguments"] == '{"path":"x"}'
+
+
def test_live_turn_waits_for_review_exit_before_relay_and_turn_context(monkeypatch):
"""The outer production wrapper waits before same-session instrumentation."""
review_entered = threading.Event()