Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(agent): lease replay visibility through next model request

zeus22973680641172a
diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.py
index 0a2dc1ea5..ed1e53423 100644
--- a/agent/src/agent/loop.py
+++ b/agent/src/agent/loop.py
@@ -533,6 +533,7 @@ def _microcompact(
*,
target_tokens: Optional[int] = None,
measure: Optional[Callable[[list], int]] = None,
+ preserve_tool_call_ids: Optional[set[str]] = None,
) -> list:
"""Layer 1: prune old tool results, keeping the most recent N intact.
@@ -544,6 +545,8 @@ def _microcompact(
still needs is what made it re-fetch its evidence until
``no_progress``, so the loop passes a target.
measure: Prompt-size function for ``target_tokens``.
+ preserve_tool_call_ids: Replayed results no successful model request
+ has carried yet; they are never cleared here.
Returns:
Names of tools whose every result just became unreadable (legacy
@@ -557,6 +560,8 @@ def _microcompact(
for msg in tool_msgs[:-KEEP_RECENT]:
if target_tokens is not None and measure is not None and measure(messages) <= target_tokens:
break
+ if preserve_tool_call_ids and msg.get("tool_call_id") in preserve_tool_call_ids:
+ continue
content = msg.get("content", "")
# Skip a result already cleared: the marker is itself >100 chars, so
# re-clearing it would rewrite the recorded original size with the
@@ -989,6 +994,9 @@ class AgentLoop:
self._readonly_replay_cache: dict[tuple[str, str], str] = {}
self._readonly_replay_ready: set[tuple[str, str]] = set()
self._readonly_replay_protected: set[tuple[str, str]] = set()
+ # Replayed tool_call_ids whose restored payload no successful model
+ # request has carried yet (message identity, not call identity).
+ self._readonly_replay_visibility_pending: set[str] = set()
self._readonly_replay_recoveries = 0
self._tool_progress = ToolProgress()
self._context_meter = ContextMeter()
@@ -1114,6 +1122,7 @@ class AgentLoop:
self._readonly_replay_cache = {}
self._readonly_replay_ready = set()
self._readonly_replay_protected = set()
+ self._readonly_replay_visibility_pending = set()
self._readonly_replay_recoveries = 0
self._tool_progress = ToolProgress()
self._context_meter = ContextMeter()
@@ -1239,7 +1248,12 @@ class AgentLoop:
# can force each layer with a tiny number.
tokens = estimate_tokens(messages)
if tokens > int(_token_threshold() * 0.5):
- self._microcompact_and_unblock(messages, trace, iteration)
+ self._microcompact_and_unblock(
+ messages,
+ trace,
+ iteration,
+ preserve_tool_call_ids=self._readonly_replay_visibility_pending,
+ )
tokens = estimate_tokens(messages)
if tokens > int(_token_threshold() * 0.7):
_context_collapse(messages)
@@ -1475,6 +1489,8 @@ class AgentLoop:
else:
stream_failure_streak = 0
+ self._consume_readonly_replay_visibility(messages, trace, current_iter)
+
# Cancelled mid-stream: discard this turn's partial response and
# end the run now, without executing any of its tool calls.
if self._cancel_event.is_set():
@@ -2391,6 +2407,7 @@ class AgentLoop:
tc.id, tc.name, truncate_tool_result(restored)
)
)
+ self._readonly_replay_visibility_pending.add(tc.id)
self._successful_call_keys[tc.id] = dedup_key
self._called_ok.add(dedup_key)
self._readonly_replay_ready.discard(dedup_key)
@@ -3107,6 +3124,7 @@ class AgentLoop:
iteration,
target_tokens=budget.micro_at,
measure=self._prompt_tokens,
+ preserve_tool_call_ids=self._readonly_replay_visibility_pending,
)
tokens = self._prompt_tokens(messages)
if tokens > budget.collapse_at:
@@ -3172,6 +3190,7 @@ class AgentLoop:
*,
target_tokens: Optional[int] = None,
measure: Optional[Callable[[list], int]] = None,
+ preserve_tool_call_ids: Optional[set[str]] = None,
) -> list[str]:
"""Run layer-1 microcompact and re-open lost readonly call identities.
@@ -3188,12 +3207,18 @@ class AgentLoop:
iteration: Current ReAct iteration, recorded on the trace event.
target_tokens: Clear oldest-first only until the prompt fits.
measure: Prompt-size function for ``target_tokens``.
+ preserve_tool_call_ids: Replayed results still owed one model request.
Returns:
The tool names re-opened, for callers and tests to assert on.
"""
readable_before = self._readable_success_keys(messages)
- _microcompact(messages, target_tokens=target_tokens, measure=measure)
+ _microcompact(
+ messages,
+ target_tokens=target_tokens,
+ measure=measure,
+ preserve_tool_call_ids=preserve_tool_call_ids,
+ )
unreadable_tools = self._unblock_lost_readonly_results(messages, readable_before)
if unreadable_tools:
trace.write({
@@ -3203,6 +3228,21 @@ class AgentLoop:
})
return unreadable_tools
+ def _consume_readonly_replay_visibility(
+ self, messages: list, trace: TraceWriter, iteration: int
+ ) -> None:
+ """Release replay leases once a successful model request carried them."""
+ visible = {
+ msg.get("tool_call_id")
+ for msg in messages
+ if msg.get("role") == "tool"
+ and msg.get("tool_call_id") in self._readonly_replay_visibility_pending
+ and not _result_data_gone(msg.get("content"))
+ }
+ for call_id in sorted(visible):
+ self._readonly_replay_visibility_pending.discard(call_id)
+ trace.write({"type": "replay_visibility_consumed", "iter": iteration, "call_id": call_id})
+
def _readable_success_keys(self, messages: list) -> set[tuple[str, str]]:
"""Identify surviving successful results, not synthetic skip/stub calls."""
return {
diff --git a/agent/tests/test_readonly_replay_after_microcompact.py b/agent/tests/test_readonly_replay_after_microcompact.py
index b8f44a2f4..432611f91 100644
--- a/agent/tests/test_readonly_replay_after_microcompact.py
+++ b/agent/tests/test_readonly_replay_after_microcompact.py
@@ -3,8 +3,19 @@ from __future__ import annotations
import threading
from types import SimpleNamespace
-from src.agent.loop import AgentLoop, MAX_READONLY_REPLAY_RECOVERIES
+import pytest
+
+from src.agent import loop as loop_mod
+from src.agent.context import ContextBuilder
+from src.agent.memory import WorkspaceMemory
+from src.agent.loop import (
+ AgentLoop,
+ KEEP_RECENT,
+ MAX_READONLY_REPLAY_RECOVERIES,
+)
from src.agent.tool_progress import NO_PROGRESS_LIMIT, ToolProgress
+from src.providers.chat import LLMResponse, ToolCallRequest
+from src.agent.trace import TraceWriter
from src.tools.web_reader_tool import WebReaderTool
@@ -80,6 +91,7 @@ def _loop(registry=None):
loop._readonly_replay_cache = {}
loop._readonly_replay_ready = set()
loop._readonly_replay_protected = set()
+ loop._readonly_replay_visibility_pending = set()
loop._readonly_replay_recoveries = 0
loop._grounding = None
loop._cancel_event = threading.Event()
@@ -158,6 +170,240 @@ def test_no_cache_bypasses_repeatable_compaction_replay():
)
+class _VisibilityRegistry:
+ def __init__(self):
+ self.probe_executions = 0
+ self.other_executions = 0
+ names = ["probe"] + [
+ f"{prefix}-{i}"
+ for prefix in ("warmup", "later")
+ for i in range(KEEP_RECENT)
+ ]
+ self._tools = {
+ name: SimpleNamespace(name=name, description="Offline visibility tool")
+ for name in names
+ }
+
+ @property
+ def tool_names(self):
+ return list(self._tools)
+
+ def get_definitions(self):
+ return []
+
+ def get(self, name):
+ return SimpleNamespace(
+ is_readonly=True,
+ repeatable=True,
+ deterministic=False,
+ replay_after_compaction=name == "probe",
+ )
+
+ def result(self, name):
+ if name == "probe":
+ value = "RESULT_PROBE metric_alpha metric_beta " + "x" * 160
+ else:
+ value = f"{name} " + "y" * 160
+ return '{"status":"ok","value":"' + value + '"}'
+
+ def execute(self, name, args):
+ result = self.result(name)
+ if name == "probe":
+ self.probe_executions += 1
+ else:
+ self.other_executions += 1
+ return result
+
+
+def test_agent_loop_sends_replay_before_consuming_its_visibility_lease(
+ monkeypatch, tmp_path
+):
+ assert KEEP_RECENT == 3
+ assert MAX_READONLY_REPLAY_RECOVERIES == 6
+ monkeypatch.setenv("PPI_TRADING_DISABLED", "True")
+ monkeypatch.setattr(loop_mod, "RUNS_DIR", tmp_path / "runs")
+ monkeypatch.setattr(loop_mod, "SESSIONS_DIR", tmp_path / "sessions")
+ run_dir = tmp_path / "run"
+ run_dir.mkdir()
+
+ registry = _VisibilityRegistry()
+ llm = _VisibilityLLM(registry, monkeypatch)
+ loop = AgentLoop(
+ registry=registry,
+ llm=llm,
+ memory=WorkspaceMemory(run_dir=str(run_dir)),
+ max_iterations=4,
+ )
+ result = loop.run("Inspect the offline probe markers.")
+
+ assert result["status"] == "success"
+ assert registry.probe_executions == 1
+ assert len(llm.requests) == 3
+ delivered = next(
+ message
+ for message in llm.requests[2]
+ if message.get("tool_call_id") == "probe-replay"
+ )
+ assert "RESULT_PROBE" in delivered["content"]
+ assert "metric_alpha" in delivered["content"]
+ assert "metric_beta" in delivered["content"]
+ assert loop._readonly_replay_visibility_pending == set()
+
+ events = TraceWriter.read(run_dir)
+ assert sum(event["type"] == "tool_result_replayed" for event in events) == 1
+ assert sum(event["type"] == "replay_visibility_consumed" for event in events) == 1
+
+ # The successful third request consumed the one-decision lease. A later
+ # ordinary Layer 1 pass can now clear the replay behind KEEP_RECENT tools.
+ post_request_trace = TraceWriter(run_dir)
+ loop._microcompact_and_unblock(llm._last_messages, post_request_trace, 4)
+ post_request_trace.close()
+ delivered_after = next(
+ message
+ for message in llm._last_messages
+ if message.get("tool_call_id") == "probe-replay"
+ )
+ assert delivered_after["content"].startswith("[CLEARED FROM CONTEXT:")
+
+
+ "later_tool_count", [0, max(1, KEEP_RECENT - 1), KEEP_RECENT]
+)
+def test_replayed_result_has_one_model_request_visibility_lease(later_tool_count):
+ """Keep a replay through its first post-replay request, then compact it."""
+ assert KEEP_RECENT == 3
+ assert MAX_READONLY_REPLAY_RECOVERIES == 6
+ registry = _VisibilityRegistry()
+ loop = _loop(registry)
+ messages = []
+ trace = _Trace()
+ react_trace = []
+ args = {"query": "offline-probe"}
+
+ def call(call_id, name, arguments, iteration):
+ loop._process_tool_calls(
+ [SimpleNamespace(id=call_id, name=name, arguments=arguments)],
+ _Context(),
+ messages,
+ trace,
+ react_trace,
+ iteration,
+ )
+
+ call("probe-original", "probe", args, 1)
+ for i in range(KEEP_RECENT):
+ call(f"warmup-{i}", f"warmup-{i}", {"i": i}, 1)
+ assert registry.probe_executions == 1
+
+ # The original probe is outside the most recent results and is cleared.
+ loop._microcompact_and_unblock(messages, trace, 2)
+ original = next(m for m in messages if m.get("tool_call_id") == "probe-original")
+ assert original["content"].startswith("[CLEARED FROM CONTEXT:")
+
+ replay_and_later = [
+ SimpleNamespace(id="probe-replay", name="probe", arguments=dict(args))
+ ]
+ replay_and_later.extend(
+ SimpleNamespace(
+ id=f"later-{i}",
+ name=f"later-{i}",
+ arguments={"i": i},
+ )
+ for i in range(later_tool_count)
+ )
+ loop._process_tool_calls(
+ replay_and_later, _Context(), messages, trace, react_trace, 3
+ )
+ assert registry.probe_executions == 1
+ assert sum(e["type"] == "tool_result_replayed" for e in trace.events) == 1
+ assert loop._readonly_replay_visibility_pending == {"probe-replay"}
+
+ # This is the normal pre-request microcompaction. With exactly KEEP_RECENT
+ # newer results, it would clear the replay without its one-decision lease.
+ loop._microcompact_and_unblock(
+ messages,
+ trace,
+ 4,
+ preserve_tool_call_ids=loop._readonly_replay_visibility_pending,
+ )
+ replay = next(m for m in messages if m.get("tool_call_id") == "probe-replay")
+ assert "RESULT_PROBE" in replay["content"]
+ assert "metric_alpha" in replay["content"]
+ assert "metric_beta" in replay["content"]
+ if later_tool_count == KEEP_RECENT:
+ for i in range(KEEP_RECENT):
+ warmup = next(
+ m for m in messages if m.get("tool_call_id") == f"warmup-{i}"
+ )
+ assert warmup["content"].startswith("[CLEARED FROM CONTEXT:")
+
+ # AgentLoop consumes this lease only after stream_chat returns successfully.
+ # At this boundary, the exact messages sent for that request include replay.
+ loop._consume_readonly_replay_visibility(messages, trace, 4)
+ assert loop._readonly_replay_visibility_pending == set()
+ assert any(
+ e["type"] == "replay_visibility_consumed"
+ and e["call_id"] == "probe-replay"
+ and e["iter"] == 4
+ for e in trace.events
+ )
+
+ # More results arrive after that decision; ordinary Layer 1 rules apply.
+ for i in range(KEEP_RECENT):
+ call(f"after-{i}", f"after-{i}", {"i": i}, 5)
+ loop._microcompact_and_unblock(messages, trace, 6)
+ replay = next(m for m in messages if m.get("tool_call_id") == "probe-replay")
+ assert replay["content"].startswith("[CLEARED FROM CONTEXT:")
+ assert registry.probe_executions == 1
+ assert sum(e["type"] == "tool_result_replayed" for e in trace.events) == 1
+
+
+class _VisibilityLLM:
+ model_name = "offline-visibility-stub"
+
+ def __init__(self, registry, monkeypatch):
+ self.registry = registry
+ self.monkeypatch = monkeypatch
+ self.requests = []
+
+ def stream_chat(self, messages, **kwargs):
+ self.requests.append([dict(message) for message in messages])
+ self._last_messages = messages
+ decision = len(self.requests)
+ if decision == 1:
+ calls = [ToolCallRequest("probe-original", "probe", {"query": "offline"})]
+ calls.extend(
+ ToolCallRequest(f"warmup-{i}", f"warmup-{i}", {"i": i})
+ for i in range(KEEP_RECENT)
+ )
+ elif decision == 2:
+ calls = [ToolCallRequest("probe-replay", "probe", {"query": "offline"})]
+ calls.extend(
+ ToolCallRequest(f"later-{i}", f"later-{i}", {"i": i})
+ for i in range(KEEP_RECENT)
+ )
+ else:
+ return LLMResponse(content="Offline probe inspected.")
+
+ future = list(messages)
+ future.append(ContextBuilder.format_assistant_tool_calls(calls))
+ for call in calls:
+ future.append(
+ ContextBuilder.format_tool_result(
+ call.id, call.name, self.registry.result(call.name)
+ )
+ )
+ self.monkeypatch.setattr(
+ loop_mod,
+ "TOKEN_THRESHOLD",
+ max(2, int(loop_mod.estimate_tokens(future) * 1.6)),
+ raising=False,
+ )
+ return LLMResponse(tool_calls=calls)
+
+
+
+
def test_repeatable_opt_in_runs_normally_before_compaction_then_stays_replay_protected():
registry = _Registry(repeatable=True, replay_after_compaction=True)
loop = _loop(registry)
@@ -224,6 +470,7 @@ def test_no_cache_executes_externally_even_after_normal_read_is_protected():
tc = SimpleNamespace(name="read_url", arguments=fresh_args, id="fresh-1")
loop._process_tool_calls([tc], _Context(), [], _Trace(), [], 6)
assert registry.execute_calls == 1
+ assert loop._readonly_replay_visibility_pending == set()
def test_different_repeatable_arguments_still_execute_externally_after_protection():
@@ -372,6 +619,7 @@ def test_a_write_makes_earlier_readonly_results_unreplayable() -> None:
[SimpleNamespace(name="bash", arguments={"command": "python make_factor.py > factor.csv"}, id="w1")],
_Context(), [], _Trace(), [], 2,
)
+ assert loop._readonly_replay_visibility_pending == set()
key = loop._identical_call_key("factor_analysis", read)
loop._unblock_lost_readonly_results([], {key})