"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(agent): bound readonly replay after compaction
zeus229103639b06b60
diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.pyindex 59793bf5a..607bae6a9 100644--- a/agent/src/agent/loop.py+++ b/agent/src/agent/loop.py@@ -80,6 +80,13 @@ SUMMARY_CHUNK_CHARS = 80_000# retry once with a nudge before failing the run on a second consecutive one.MAX_CONSECUTIVE_EMPTY_RESPONSE_SKIPS = 1+# Compaction recovery is a bounded reliability aid, not an alternate research+# loop. This mirrors the grounding recovery design: once the run has restored+# enough lost readonly payloads, later context loss keeps those exact calls+# locked so the planner must continue from summaries/other evidence instead of+# churning through replay forever.+MAX_READONLY_REPLAY_RECOVERIES = 6+def _override(name: str):"""Return a monkeypatched module-level override if present."""@@ -469,7 +476,8 @@ def _cleared_text(original_len: int) -> str:f"{_CLEARED_PREFIX} this tool call SUCCEEDED and returned "f"{original_len} characters, which were removed to free context ""space. This is NOT a tool failure and NOT an empty result. If you "- "need these values, call the tool again with the same arguments.]"+ "need these values, request the same tool call again; the loop may "+ "restore the prior successful result without refetching it.]")@@ -478,6 +486,35 @@ def _is_cleared(content: Any) -> bool:return isinstance(content, str) and content.startswith(_CLEARED_PREFIX)+def _replay_context_result(result: str) -> str:+ """Annotate a restored readonly result with planner guidance.++ Replay exists to recover evidence that context compaction removed, not to+ trigger another fetch under slightly different freshness arguments. Keep+ the original payload intact and add a reserved metadata field when the+ result is a JSON object; non-JSON results get a short textual suffix.+ """+ notice = (+ "Exact prior successful result restored after context compaction. "+ "Treat this payload as available evidence and continue the analysis. "+ "Do not change cache/freshness arguments merely to bypass replay; "+ "request a fresh fetch only when the evidence itself is stale/cached "+ "or the task genuinely requires newer data."+ )+ try:+ payload = json.loads(result)+ except (TypeError, ValueError):+ return f"{result}\n\n[Replay notice: {notice}]"+ if not isinstance(payload, dict):+ return f"{result}\n\n[Replay notice: {notice}]"+ replay_payload = dict(payload)+ replay_payload["_vibe_replay"] = {+ "restored": True,+ "notice": notice,+ }+ return json.dumps(replay_payload, ensure_ascii=False)++def _microcompact(messages: list) -> list:"""Layer 1: silently prune old tool results, keeping the most recent N intact.@@ -1113,6 +1150,14 @@ class AgentLoop:# 5-9x each (2026-08-20 INTC run) because it could no longer see its# own verification records.self._called_identical: dict[tuple[str, str], str] = {}+ # Successful readonly results are retained only for this run. If+ # compaction removes the visible result, an exact repeat can restore it+ # without hitting the external source again. Repeatable tools require an+ # explicit replay policy for persistent post-compaction replay.+ 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()+ self._readonly_replay_recoveries = 0self._tool_progress = ToolProgress()def cancel(self) -> None:@@ -1230,6 +1275,10 @@ class AgentLoop:self._last_activity_wall = _time.time()self._run_done = threading.Event()self._called_identical = {}+ self._readonly_replay_cache = {}+ self._readonly_replay_ready = set()+ self._readonly_replay_protected = set()+ self._readonly_replay_recoveries = 0self._tool_progress = ToolProgress()run_started_wall = _time.time()@@ -2342,18 +2391,6 @@ class AgentLoop:iteration,)continue- if (- dedup_key is not None- and dedup_key in self._called_ok- and not is_repeatable- ):- logger.warning(f"Blocked duplicate call: {tc.name} (already succeeded)")- skip_msg = json.dumps({"skipped": True, "reason": f"{tc.name} already completed successfully. Use the previous result."})- messages.append(context.format_tool_result(tc.id, tc.name, skip_msg))- trace.write({"type": "tool_skipped", "iter": iteration, "tool": tc.name})- react_trace.append({"type": "tool_skipped", "tool": tc.name})- continue-if self._grounding is not None:authorization = self._grounding.authorize_tool_call(tc.name,@@ -2374,6 +2411,78 @@ class AgentLoop:)continue+ # A successful readonly call whose visible result was removed by+ # micro/auto-compaction can be replayed from this run's memory. This+ # extends the existing deterministic-cache pattern without declaring+ # mutable web resources deterministic. Fresh/no-cache calls bypass+ # this path, while repeatable calls must explicitly opt in.+ if (+ dedup_key is not None+ and dedup_key in self._readonly_replay_ready+ and self._readonly_replay_allowed(tool_def, tc.arguments)+ and dedup_key in self._readonly_replay_cache+ ):+ cached = self._readonly_replay_cache[dedup_key]+ restored = _replay_context_result(cached)+ messages.append(+ context.format_tool_result(+ tc.id, tc.name, truncate_tool_result(restored)+ )+ )+ self._successful_call_keys[tc.id] = dedup_key+ self._called_ok.add(dedup_key)+ self._readonly_replay_ready.discard(dedup_key)+ self._readonly_replay_recoveries += 1+ # Restoring data that compaction removed is forward progress for+ # the working context, but not a new external observation.+ self._tool_progress.mark_context_restored()+ trace.write({+ "type": "tool_result_replayed",+ "iter": iteration,+ "tool": tc.name,+ "call_id": tc.id,+ "recovery_count": self._readonly_replay_recoveries,+ "recovery_limit": MAX_READONLY_REPLAY_RECOVERIES,+ })+ react_trace.append({"type": "tool_result_replayed", "tool": tc.name})+ self._emit(+ "tool_result",+ {+ "tool": tc.name,+ "status": "ok",+ "elapsed_ms": 0,+ "preview": redact_tool_result(cached)[:200],+ "call_id": tc.id,+ "cached": True,+ "replayed": True,+ },+ )+ continue++ if (+ dedup_key is not None+ and dedup_key in self._called_ok+ and (+ not is_repeatable+ or dedup_key in self._readonly_replay_protected+ )+ ):+ logger.warning(f"Blocked duplicate call: {tc.name} (already succeeded)")+ replay_restored = dedup_key in self._readonly_replay_protected+ reason = (+ f"{tc.name} was just restored from the run-scoped replay cache after "+ "compaction. The payload is already visible in context; use that "+ "result and continue the analysis instead of requesting the same "+ "call again."+ if replay_restored+ else f"{tc.name} already completed successfully. Use the previous result."+ )+ skip_msg = json.dumps({"skipped": True, "reason": reason})+ messages.append(context.format_tool_result(tc.id, tc.name, skip_msg))+ trace.write({"type": "tool_skipped", "iter": iteration, "tool": tc.name})+ react_trace.append({"type": "tool_skipped", "tool": tc.name})+ continue+# Deterministic tools (e.g. financial_rigor calc) return the same# result for the same args. Checked AFTER authorization above so a# cached repeat can never bypass the identity gate. After auto-compact cleared earlier@@ -2836,6 +2945,27 @@ class AgentLoop:return Falsereturn bool(tool_def and getattr(tool_def, "is_readonly", False))+ def _readonly_replay_allowed(self, tool_def: Any, arguments: Mapping[str, Any]) -> bool:+ """Whether an exact readonly result may be restored after compaction.++ This is intentionally narrower than ``is_readonly``: deterministic+ calls already use the existing cache, and ``no_cache=True`` explicitly+ requests a fresh read. Repeatable calls require explicit opt-in because+ they may otherwise be intentionally refreshed.+ """+ if tool_def is None or not getattr(tool_def, "is_readonly", False):+ return False+ if getattr(tool_def, "deterministic", False):+ return False+ try:+ if bool((arguments or {}).get("no_cache")):+ return False+ except AttributeError:+ return False+ if getattr(tool_def, "repeatable", False):+ return bool(getattr(tool_def, "replay_after_compaction", False))+ return True+def _record_written_target(self, arguments: Mapping[str, Any]) -> None:"""Remember a file written by write_file/edit_file for completion checks."""raw = arguments.get("path") or arguments.get("file_path")@@ -2952,10 +3082,25 @@ class AgentLoop:) -> list[str]:"""Recover only lost exact queries; context loss cannot replay writes."""lost = readable_before - self._readable_success_keys(messages)- reopened = {+ candidates = {key for key in lost & self._called_ok if self._is_tool_readonly(key[0])}+ replay_budget_available = (+ self._readonly_replay_recoveries < MAX_READONLY_REPLAY_RECOVERIES+ )+ reopened = candidates if replay_budget_available else set()self._called_ok.difference_update(reopened)+ replayable = {key for key in reopened if key in self._readonly_replay_cache}+ self._readonly_replay_ready.update(replayable)+ for key in replayable:+ tool_def = self.registry.get(key[0])+ if (+ getattr(tool_def, "is_readonly", False)+ and getattr(tool_def, "repeatable", False)+ and getattr(tool_def, "replay_after_compaction", False)+ and not getattr(tool_def, "deterministic", False)+ ):+ self._readonly_replay_protected.add(key)return sorted({key[0] for key in reopened})def _identical_call_key(self, tool_name: str, arguments: Mapping[str, Any]) -> tuple[str, str] | None:@@ -3050,16 +3195,24 @@ class AgentLoop:# Cache successful deterministic results so an identical later call is# served without re-execution (regression: repeated financial_rigor- # calcs after compaction, 2026-08-20 INTC run).+ # calcs after compaction, 2026-08-20 INTC run). Readonly results use a+ # separate run-scoped replay cache: it is only consulted after+ # compaction has made that exact result unreadable, and only for tools+ # whose replay policy allows it.if success:try:tool_def = self.registry.get(tc.name)except Exception: # noqa: BLE001tool_def = None+ cache_key = self._identical_call_key(tc.name, tc.arguments)if tool_def is not None and getattr(tool_def, "deterministic", False):- cache_key = self._identical_call_key(tc.name, tc.arguments)if cache_key is not None:self._called_identical[cache_key] = result+ elif (+ cache_key is not None+ and self._readonly_replay_allowed(tool_def, tc.arguments)+ ):+ self._readonly_replay_cache[cache_key] = resultstatus = "ok" if success else "error"truncated = truncate_tool_result(result)diff --git a/agent/src/agent/tool_progress.py b/agent/src/agent/tool_progress.pyindex 258848ba3..771fad704 100644--- a/agent/src/agent/tool_progress.py+++ b/agent/src/agent/tool_progress.py@@ -17,8 +17,8 @@ NO_PROGRESS_LIMIT = 8FAILURE_BLOCK_THRESHOLD = 2RECOVERY_MESSAGE = ("I stopped because repeated tool attempts did not produce new information. "- "I cannot reliably answer from the previous summary alone. Please provide "- "the artifact path or confirm that you want to rerun the missing research step."+ "I cannot reliably answer from the available evidence. Please ask me to "+ "continue with a different source or rerun the missing research step.")@@ -29,6 +29,8 @@ class ToolProgress:self.failed: dict[tuple[str, str], int] = {}self._observations: set[tuple[str, str | None, str]] = set()self._new_observation = False+ self._context_restored = False+ self._context_restore_grace_used = Falseself.stalled_iterations = 0def record(@@ -63,6 +65,21 @@ class ToolProgress:self._observations.add(observation)self._new_observation = True+ def mark_context_restored(self) -> None:+ """Grant one run-scoped grace iteration after compaction restores evidence.++ Replaying a cached readonly result is not a new external observation, so+ it must never reset the no-progress history indefinitely. The first+ restoration in a run may hold the current stall count for one iteration+ so the model gets a chance to consume the restored payload; later+ restorations do not extend the budget. A genuinely new observation still+ resets the stall chain through :meth:`record`.+ """+ if self._context_restore_grace_used:+ return+ self._context_restore_grace_used = True+ self._context_restored = True+def is_blocked(self, key: tuple[str, str]) -> bool:"""Whether this exact call failed often enough to refuse a repeat.@@ -77,8 +94,15 @@ class ToolProgress:def finish_iteration(self) -> bool:"""Return whether the run exhausted its consecutive no-progress budget."""- self.stalled_iterations = (- 0 if self._new_observation else self.stalled_iterations + 1- )+ if self._new_observation:+ self.stalled_iterations = 0+ elif self._context_restored:+ # One grace iteration: preserve, but do not erase, prior stall+ # history. This lets the next model turn see the restored evidence+ # without allowing replay-only loops to run forever.+ pass+ else:+ self.stalled_iterations += 1self._new_observation = False+ self._context_restored = Falsereturn self.stalled_iterations >= NO_PROGRESS_LIMITdiff --git a/agent/src/agent/tools.py b/agent/src/agent/tools.pyindex 1dca12ab7..1bb8ae215 100644--- a/agent/src/agent/tools.py+++ b/agent/src/agent/tools.py@@ -18,6 +18,11 @@ class BaseTool(ABC):description: Tool description shown to the LLM.parameters: Parameter definition in JSON Schema format.repeatable: Whether the tool may be called more than once.+ replay_after_compaction: Whether an exact successful readonly call may+ restore its run-scoped result after compaction removed the visible+ payload, even when the tool is otherwise repeatable. Tools opting+ in must provide an explicit freshness argument when a caller needs+ to bypass replay (for example ``no_cache=True``)."""name: str = ""@@ -29,6 +34,11 @@ class BaseTool(ABC):# same result, so the loop may serve repeated identical calls from cache# instead of re-executing them (financial_rigor calc/verify, etc.).deterministic: bool = False+ # Mutable/read-through tools can stay repeatable during normal operation+ # while opting into replay only when compaction has removed an exact prior+ # successful result. The loop still bypasses replay for explicit freshness+ # requests such as ``no_cache=True``.+ replay_after_compaction: bool = False@classmethoddef check_available(cls) -> bool:diff --git a/agent/src/skills/web-reader/SKILL.md b/agent/src/skills/web-reader/SKILL.mdindex 146bc09d9..6f00094da 100644--- a/agent/src/skills/web-reader/SKILL.md+++ b/agent/src/skills/web-reader/SKILL.md@@ -46,7 +46,11 @@ Returns JSON:private/internal addresses — they would leave this host.- **Caching/staleness:** results may be a cached snapshot, not live data.When stale, the JSON includes `"cached": true`; pass `no_cache=true` to- force a fresh fetch (slower — use only when freshness matters).+ force a fresh fetch (slower). Use `no_cache=true` only when a prior result+ explicitly reported `"cached": true` or the task genuinely requires newer+ data. Do **not** use it merely because context compaction removed a prior+ payload or because that payload was restored by replay; in that case consume+ the restored evidence and continue the analysis.- **Bash fallback caveat:** if a site blocks the reader (e.g. HTTP 451) amanual `bash + requests` fetch is possible, but it **bypasses thistool's URL safety guard and the Jina layer** — use sparingly and neverdiff --git a/agent/src/tools/web_reader_tool.py b/agent/src/tools/web_reader_tool.pyindex 481fca220..304049be7 100644--- a/agent/src/tools/web_reader_tool.py+++ b/agent/src/tools/web_reader_tool.py@@ -140,11 +140,25 @@ class WebReaderTool(BaseTool):"type": "object","properties": {"url": {"type": "string", "description": "URL of the web page to read"},- "no_cache": {"type": "boolean", "description": "Request a fresh (uncached) fetch", "default": False},+ "no_cache": {+ "type": "boolean",+ "description": (+ "Request a fresh (uncached) fetch. Use only when the prior "+ "read explicitly reported cached=true or the task genuinely "+ "requires newer data. Never set no_cache merely because a "+ "previous result was compacted or replayed."+ ),+ "default": False,+ },},"required": ["url"],}repeatable = True+ # Normal repeated reads remain allowed while the prior payload is visible.+ # If compaction removes an exact successful read, AgentLoop may restore the+ # run-scoped result instead of hitting Jina/the origin again. ``no_cache``+ # remains the explicit freshness escape hatch and bypasses that replay.+ replay_after_compaction = Truedef execute(self, **kwargs) -> str:"""Fetch web page."""diff --git a/agent/tests/test_agent_loop_repeatable_tools.py b/agent/tests/test_agent_loop_repeatable_tools.pyindex d255a737d..3b683fbac 100644--- a/agent/tests/test_agent_loop_repeatable_tools.py+++ b/agent/tests/test_agent_loop_repeatable_tools.py@@ -118,28 +118,25 @@ def test_repeatable_query_executes_again_with_different_arguments()-def test_cleared_result_reopens_the_dedup_gate(monkeypatch, tmp_path: Path) -> None:- """#1343: a non-repeatable tool blocked by the dedup ledger must become- callable again once microcompact has cleared its only result.-- This drives the real gate in ``_process_tool_calls`` rather than only the- return value of ``_microcompact``: the first call fills ``_called_ok``, the- second is correctly skipped while the result is still readable, and the- third must actually execute once the result has been cleared. Without the- ledger update at the call site the third call is skipped too, which is the- 44-blocked-retries deadlock this fixes.+def test_cleared_result_replays_run_scoped_readonly_cache(monkeypatch, tmp_path: Path) -> None:+ """A non-repeatable readonly result cleared by microcompact is restored+ from this run's cache instead of repeating the external query.++ The result remains gated while readable. Once microcompact removes it, the+ exact call is reopened and a subsequent request restores the successful+ payload via ``tool_result_replayed``. This gives the model its evidence+ back without another external fetch and without weakening write-tool+ deduplication."""from src.agent.loop import KEEP_RECENTcalls: list[dict[str, object]] = []- # One of the five tools #1343 actually saw blocked; GetFundamentalsTool- # above is repeatable and therefore has no gate to exercise.tool = FundFlowTool()assert not tool.repeatable, "test needs a non-repeatable tool to have a gate at all"+ assert tool.is_readonly, "run-scoped replay is restricted to readonly tools"def _execute(**kwargs: object) -> str:calls.append(kwargs)- # Long enough that microcompact clears it (>100 chars).return json.dumps({"status": "ok", "rows": ["x" * 200]})monkeypatch.setattr(tool, "execute", _execute)@@ -171,8 +168,6 @@ def test_cleared_result_reopens_the_dedup_gate(monkeypatch, tmp_path: Path) -> N_call("call_2", 2)assert len(calls) == 1, "second call must be skipped while the result is readable"- # Pad past KEEP_RECENT so the real result is old enough to be cleared, then- # run the actual production path rather than reaching into _called_ok.for i in range(KEEP_RECENT + 1):messages.append({"role": "tool",@@ -186,11 +181,11 @@ def test_cleared_result_reopens_the_dedup_gate(monkeypatch, tmp_path: Path) -> N_call("call_3", 4)trace.close()- assert len(calls) == 2, (- "third call must execute: its only result was cleared from context, so "- "'use the previous result' points at [cleared]"- )+ assert len(calls) == 1, "cleared readonly evidence must be restored without a second fetch"events = TraceWriter.read(run_dir)assert any(e["type"] == "microcompact_cleared" for e in events), (- "the clear must leave a trace event; this layer used to act silently"+ "the clear must leave a trace event"+ )+ assert any(e["type"] == "tool_result_replayed" for e in events), (+ "restoring compacted evidence must be explicit in the trace")diff --git a/agent/tests/test_compaction_call_identity.py b/agent/tests/test_compaction_call_identity.pyindex 3c75cacd8..d3acda583 100644--- a/agent/tests/test_compaction_call_identity.py+++ b/agent/tests/test_compaction_call_identity.py@@ -102,11 +102,12 @@ def test_microcompact_reopens_only_lost_argument_variant(harness):assert _is_cleared(income["content"])assert not _is_cleared(balance["content"])- h.call({"statement": "income"})- assert len(h.tool.calls) == 3, "lost income must execute despite readable balance"+ result = h.call({"statement": "income"})+ assert json.loads(result["content"])["status"] == "ok"+ assert len(h.tool.calls) == 2, "lost income must replay without another external query"result = h.call({"statement": "balance"})assert json.loads(result["content"])["skipped"] is True- assert len(h.tool.calls) == 3, "readable balance must remain gated"+ assert len(h.tool.calls) == 2, "readable balance must remain gated"def test_variants_clear_separately_and_second_pass_is_idempotent(harness):@@ -126,11 +127,11 @@ def test_variants_clear_separately_and_second_pass_is_idempotent(harness):assert income_call["function"]["arguments"] == "{}"# Layer 2's lossy arguments must not change the captured identity.h.agent._auto_compact(h.messages, h.run_dir, h.trace)- h.call(income_args)- h.call({"statement": "balance"})- assert len(h.tool.calls) == 4+ assert json.loads(h.call(income_args)["content"])["status"] == "ok"+ assert json.loads(h.call({"statement": "balance"})["content"])["status"] == "ok"+ assert len(h.tool.calls) == 2, "both compacted readonly variants must replay from run cache"assert json.loads(h.call(income_args)["content"])["skipped"] is True- assert len(h.tool.calls) == 4+ assert len(h.tool.calls) == 2@pytest.mark.parametrize("compact", ["micro", "auto"])@@ -166,10 +167,11 @@ def test_only_real_readable_duplicate_keeps_lock(result = h.call(args)if duplicate == "success":assert json.loads(result["content"])["skipped"] is True- assert len(h.tool.calls) == calls_beforeelse:assert json.loads(result["content"])["status"] == "ok"- assert len(h.tool.calls) == calls_before + 1+ assert len(h.tool.calls) == calls_before, (+ "a lost successful readonly observation must replay even when the visible duplicate is skipped, failed, or stubbed"+ )@pytest.mark.parametrize("compact", ["micro", "auto"])@@ -197,11 +199,12 @@ def test_compaction_uses_same_run_dir_identity_as_gate(harness):h.call({"statement": "income"})h.pad()h.agent._microcompact_and_unblock(h.messages, h.trace, 2)- h.call({"run_dir": ".", "statement": "income"})- assert len(h.tool.calls) == 2+ result = h.call({"run_dir": ".", "statement": "income"})+ assert json.loads(result["content"])["status"] == "ok"+ assert len(h.tool.calls) == 1, "normalized run_dir identity must replay rather than refetch"result = h.call({"statement": "income", "run_dir": str(h.run_dir)})assert json.loads(result["content"])["skipped"] is True- assert len(h.tool.calls) == 2+ assert len(h.tool.calls) == 1def test_none_canonical_key_never_creates_lock(harness, monkeypatch):@@ -231,6 +234,45 @@ def test_recovered_cached_result_restores_identical_call_gate(harness):assert len(h.tool.calls) == 1+def test_replayable_repeatable_result_restores_gate_after_one_replay(harness):+ """Match deterministic recovery semantics for mutable readonly replay.++ One genuine context loss permits one run-scoped replay. Once that payload+ is visible again, an immediate identical planner request is skipped rather+ than replayed again or re-fetched. A later genuine context loss may reopen+ the same exact identity once more.+ """+ h = harness+ h.tool.repeatable = True+ h.tool.replay_after_compaction = True+ args = {"statement": "income"}++ first = h.call(args)+ assert len(h.tool.calls) == 1++ h.pad()+ h.agent._microcompact_and_unblock(h.messages, h.trace, 2)+ assert _is_cleared(first["content"])++ replay = h.call(args)+ assert json.loads(replay["content"])["status"] == "ok"+ assert len(h.tool.calls) == 1, "lost readonly data must replay without refetch"++ repeated = h.call(args)+ repeated_payload = json.loads(repeated["content"])+ assert repeated_payload.get("skipped") is True+ assert "restored from the run-scoped replay cache" in repeated_payload["reason"]+ assert len(h.tool.calls) == 1, "visible replay must restore the exact-call gate"++ # Lose the restored copy in a later compaction: that is a new recovery+ # event, so one more replay is allowed, still without an external fetch.+ h.pad()+ h.agent._microcompact_and_unblock(h.messages, h.trace, 3)+ second_replay = h.call(args)+ assert json.loads(second_replay["content"])["status"] == "ok"+ assert len(h.tool.calls) == 1++def test_readable_cached_duplicate_keeps_exact_key_locked(harness):h = harness# A repeatable deterministic tool can have two successful readable copies,@@ -274,11 +316,11 @@ def test_auto_compact_reopens_head_but_preserves_tail_gate(assert balance in h.messagesif degraded:assert "compaction degraded" in h.messages[1]["content"]- h.call({"statement": "income"})- assert len(h.tool.calls) == 3, "summary is not readable original income data"+ assert json.loads(h.call({"statement": "income"})["content"])["status"] == "ok"+ assert len(h.tool.calls) == 2, "summary is not original income data; replay must restore it"result = h.call({"statement": "balance"})assert json.loads(result["content"])["skipped"] is True- assert len(h.tool.calls) == 3+ assert len(h.tool.calls) == 2def test_auto_compact_summary_call_sees_the_bound_llm_session(harness, monkeypatch):diff --git a/agent/tests/test_readonly_replay_after_microcompact.py b/agent/tests/test_readonly_replay_after_microcompact.pynew file mode 100644index 000000000..101d94b72--- /dev/null+++ b/agent/tests/test_readonly_replay_after_microcompact.py@@ -0,0 +1,291 @@+from __future__ import annotations++import threading+from types import SimpleNamespace++from src.agent.loop import AgentLoop, MAX_READONLY_REPLAY_RECOVERIES+from src.agent.tool_progress import NO_PROGRESS_LIMIT, ToolProgress+from src.tools.web_reader_tool import WebReaderTool+++class _Registry:+ def __init__(+ self,+ *,+ readonly=True,+ repeatable=False,+ deterministic=False,+ replay_after_compaction=False,+ ):+ self.tool = SimpleNamespace(+ is_readonly=readonly,+ repeatable=repeatable,+ deterministic=deterministic,+ replay_after_compaction=replay_after_compaction,+ )+ self.execute_calls = 0++ def get(self, name):+ return self.tool++ def execute(self, name, args):+ self.execute_calls += 1+ return '{"status":"ok","value":"fresh"}'+++class _Context:+ @staticmethod+ def format_tool_result(call_id, name, result):+ return {"role": "tool", "tool_call_id": call_id, "name": name, "content": result}+++class _Trace:+ def __init__(self):+ self.events = []++ def write(self, event):+ self.events.append(event)++ def write_tool_result(self, *, call_id, result, tool_name, status, elapsed_ms, iteration):+ self.events.append(+ {+ "type": "tool_result",+ "call_id": call_id,+ "result": result,+ "tool": tool_name,+ "status": status,+ "elapsed_ms": elapsed_ms,+ "iter": iteration,+ }+ )+++def _loop(registry=None):+ loop = object.__new__(AgentLoop)+ loop.registry = registry or _Registry()+ loop.memory = SimpleNamespace(+ run_dir="/tmp/test-run",+ increment=lambda *_args, **_kwargs: None,+ )+ loop._called_ok = set()+ loop._successful_call_keys = {}+ loop._called_identical = {}+ loop._readonly_replay_cache = {}+ loop._readonly_replay_ready = set()+ loop._readonly_replay_protected = set()+ loop._readonly_replay_recoveries = 0+ loop._grounding = None+ loop._cancel_event = threading.Event()+ loop._event_callback = None+ loop._tool_progress = ToolProgress()+ return loop+++def test_compaction_marks_lost_readonly_result_for_run_scoped_replay():+ loop = _loop()+ key = loop._identical_call_key("read_url", {"url": "https://example.test/report"})+ assert key is not None+ loop._called_ok.add(key)+ loop._readonly_replay_cache[key] = '{"status":"ok","body":"report"}'+ reopened = loop._unblock_lost_readonly_results([], {key})+ assert reopened == ["read_url"]+ assert key not in loop._called_ok+ assert key in loop._readonly_replay_ready+ assert key not in loop._readonly_replay_protected+++def test_replay_restores_result_without_external_execution():+ registry = _Registry()+ loop = _loop(registry)+ args = {"url": "https://example.test/report"}+ key = loop._identical_call_key("read_url", args)+ assert key is not None+ cached = '{"status":"ok","body":"report"}'+ loop._readonly_replay_cache[key] = cached+ loop._readonly_replay_ready.add(key)+ loop._readonly_replay_protected.add(key)+ tc = SimpleNamespace(name="read_url", arguments=args, id="replay-1")+ messages = []+ trace = _Trace()+ react_trace = []+ loop._process_tool_calls([tc], _Context(), messages, trace, react_trace, 22)+ assert registry.execute_calls == 0+ replayed_payload = __import__("json").loads(messages[-1]["content"])+ assert replayed_payload["status"] == "ok"+ assert replayed_payload["body"] == "report"+ assert replayed_payload["_vibe_replay"]["restored"] is True+ assert "continue the analysis" in replayed_payload["_vibe_replay"]["notice"]+ assert "freshness arguments" in replayed_payload["_vibe_replay"]["notice"]+ assert key in loop._called_ok+ assert key not in loop._readonly_replay_ready+ assert key in loop._readonly_replay_protected+ assert trace.events[-1]["type"] == "tool_result_replayed"+ assert loop._tool_progress.finish_iteration() is False+ assert loop._tool_progress.stalled_iterations == 0+++def test_repeatable_requires_explicit_compaction_replay_opt_in():+ loop = _loop(_Registry())+ ordinary_repeatable = _Registry(repeatable=True).get("poll")+ assert loop._readonly_replay_allowed(ordinary_repeatable, {}) is False++ replayable_repeatable = _Registry(+ repeatable=True,+ replay_after_compaction=True,+ ).get("read_url")+ assert loop._readonly_replay_allowed(replayable_repeatable, {"url": "x"}) is True+++def test_no_cache_bypasses_repeatable_compaction_replay():+ loop = _loop(_Registry())+ replayable_repeatable = _Registry(+ repeatable=True,+ replay_after_compaction=True,+ ).get("read_url")+ assert (+ loop._readonly_replay_allowed(+ replayable_repeatable,+ {"url": "x", "no_cache": True},+ )+ is False+ )+++def test_repeatable_opt_in_runs_normally_before_compaction_then_stays_replay_protected():+ registry = _Registry(repeatable=True, replay_after_compaction=True)+ loop = _loop(registry)+ args = {"url": "https://example.test/report"}+ messages = []+ trace = _Trace()+ react_trace = []++ tc1 = SimpleNamespace(name="read_url", arguments=args, id="call-1")+ loop._process_tool_calls([tc1], _Context(), messages, trace, react_trace, 1)+ assert registry.execute_calls == 1++ # Before compaction loss, repeatable keeps its ordinary refresh semantics.+ tc2 = SimpleNamespace(name="read_url", arguments=args, id="call-2")+ loop._process_tool_calls([tc2], _Context(), messages, trace, react_trace, 2)+ assert registry.execute_calls == 2++ key = loop._identical_call_key("read_url", args)+ assert key is not None+ reopened = loop._unblock_lost_readonly_results([], {key})+ assert reopened == ["read_url"]+ assert key in loop._readonly_replay_ready+ assert key in loop._readonly_replay_protected++ # First exact call after loss is restored from the run-scoped cache.+ tc3 = SimpleNamespace(name="read_url", arguments=args, id="call-3")+ loop._process_tool_calls([tc3], _Context(), messages, trace, react_trace, 3)+ assert registry.execute_calls == 2+ assert key not in loop._readonly_replay_ready++ # An immediate identical repeat must not escape to the external source or+ # replay the same payload again just because the tool is repeatable. The+ # restored result is already visible, so the exact-call protection gate+ # returns a synthetic skip and tells the planner to continue with it.+ tc4 = SimpleNamespace(name="read_url", arguments=args, id="call-4")+ loop._process_tool_calls([tc4], _Context(), messages, trace, react_trace, 4)+ assert registry.execute_calls == 2+ assert sum(e["type"] == "tool_result_replayed" for e in trace.events) == 1+ assert messages[-1]["content"]+ assert '"skipped": true' in messages[-1]["content"]+ assert "restored from the run-scoped replay cache" in messages[-1]["content"]++ # If compaction removes the replay again, the same key can be restored once+ # more without a network fetch.+ loop._unblock_lost_readonly_results([], {key})+ tc5 = SimpleNamespace(name="read_url", arguments=args, id="call-5")+ loop._process_tool_calls([tc5], _Context(), messages, trace, react_trace, 5)+ assert registry.execute_calls == 2+ assert sum(e["type"] == "tool_result_replayed" for e in trace.events) == 2+++def test_no_cache_executes_externally_even_after_normal_read_is_protected():+ registry = _Registry(repeatable=True, replay_after_compaction=True)+ loop = _loop(registry)+ normal_args = {"url": "https://example.test/report"}+ normal_key = loop._identical_call_key("read_url", normal_args)+ assert normal_key is not None+ loop._readonly_replay_cache[normal_key] = '{"status":"ok","body":"cached"}'+ loop._readonly_replay_protected.add(normal_key)++ fresh_args = {"url": "https://example.test/report", "no_cache": True}+ tc = SimpleNamespace(name="read_url", arguments=fresh_args, id="fresh-1")+ loop._process_tool_calls([tc], _Context(), [], _Trace(), [], 6)+ assert registry.execute_calls == 1+++def test_different_repeatable_arguments_still_execute_externally_after_protection():+ registry = _Registry(repeatable=True, replay_after_compaction=True)+ loop = _loop(registry)+ protected_args = {"url": "https://example.test/a"}+ protected_key = loop._identical_call_key("read_url", protected_args)+ assert protected_key is not None+ loop._readonly_replay_cache[protected_key] = '{"status":"ok","body":"a"}'+ loop._readonly_replay_protected.add(protected_key)++ tc = SimpleNamespace(+ name="read_url",+ arguments={"url": "https://example.test/b"},+ id="different-1",+ )+ loop._process_tool_calls([tc], _Context(), [], _Trace(), [], 7)+ assert registry.execute_calls == 1+++def test_web_reader_declares_repeatable_compaction_replay_contract():+ tool = WebReaderTool()+ assert tool.is_readonly is True+ assert tool.repeatable is True+ assert tool.replay_after_compaction is True+ assert tool.deterministic is False+++def test_mutating_tools_and_existing_deterministic_cache_stay_separate():+ loop = _loop(_Registry(readonly=False))+ assert loop._readonly_replay_allowed(loop.registry.get("write_file"), {}) is False+ deterministic = _Registry(deterministic=True).get("financial_rigor")+ assert loop._readonly_replay_allowed(deterministic, {}) is False+++def test_replay_only_iterations_remain_bounded_by_no_progress_limit():+ progress = ToolProgress()+ for iteration in range(NO_PROGRESS_LIMIT + 1):+ progress.mark_context_restored()+ stopped = progress.finish_iteration()+ if stopped:+ break+ assert stopped is True+ assert progress.stalled_iterations == NO_PROGRESS_LIMIT+ assert iteration <= NO_PROGRESS_LIMIT+++def test_run_scoped_replay_recovery_budget_keeps_lost_calls_locked() -> None:+ registry = _Registry(repeatable=True, replay_after_compaction=True)+ loop = _loop(registry)+ args = {"url": "https://example.test/report"}+ key = loop._identical_call_key("read_url", args)+ assert key is not None+ loop._called_ok.add(key)+ loop._readonly_replay_cache[key] = '{"status":"ok","body":"report"}'+ loop._readonly_replay_protected.add(key)+ loop._readonly_replay_recoveries = MAX_READONLY_REPLAY_RECOVERIES++ reopened = loop._unblock_lost_readonly_results([], {key})++ assert reopened == []+ assert key in loop._called_ok+ assert key not in loop._readonly_replay_ready++ messages = []+ trace = _Trace()+ tc = SimpleNamespace(name="read_url", arguments=args, id="budget-exhausted")+ loop._process_tool_calls([tc], _Context(), messages, trace, [], 9)++ assert registry.execute_calls == 0+ payload = __import__("json").loads(messages[-1]["content"])+ assert payload["skipped"] is True+ assert "restored from the run-scoped replay cache" in payload["reason"]+ assert not any(e["type"] == "tool_result_replayed" for e in trace.events)diff --git a/agent/tests/test_tool_progress.py b/agent/tests/test_tool_progress.pynew file mode 100644index 000000000..688c912ec--- /dev/null+++ b/agent/tests/test_tool_progress.py@@ -0,0 +1,122 @@+"""Focused tests for per-run tool progress and no-progress recovery."""++from __future__ import annotations++from src.agent.tool_progress import (+ FAILURE_BLOCK_THRESHOLD,+ NO_PROGRESS_LIMIT,+ ToolProgress,+)+++def test_new_readonly_observation_resets_stall_chain() -> None:+ progress = ToolProgress()+ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 1++ progress.record(+ "read_url",+ ("read_url", '{"url":"https://example.test"}'),+ '{"status":"ok","body":"new"}',+ success=True,+ is_readonly=True,+ )+ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 0+++def test_identical_readonly_observation_is_not_new_progress() -> None:+ progress = ToolProgress()+ key = ("read_url", '{"url":"https://example.test"}')+ result = '{"status":"ok","body":"same"}'++ progress.record("read_url", key, result, success=True, is_readonly=True)+ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 0++ progress.record("read_url", key, result, success=True, is_readonly=True)+ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 1+++def test_context_restore_grants_one_grace_without_resetting_stall_history() -> None:+ progress = ToolProgress()+ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 1++ progress.mark_context_restored()+ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 1++ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 2+++def test_context_restore_does_not_create_external_observation() -> None:+ progress = ToolProgress()+ progress.mark_context_restored()+ assert progress._observations == set()+ assert progress.finish_iteration() is False+ assert progress.stalled_iterations == 0+++def test_repeated_context_restore_cannot_make_no_progress_unbounded() -> None:+ progress = ToolProgress()+ # The first restore may hold the counter for one iteration. Every later+ # replay-only iteration must consume the ordinary no-progress budget.+ for iteration in range(NO_PROGRESS_LIMIT + 1):+ progress.mark_context_restored()+ stopped = progress.finish_iteration()+ if stopped:+ break+ assert stopped is True+ assert progress.stalled_iterations == NO_PROGRESS_LIMIT+ assert iteration <= NO_PROGRESS_LIMIT+++def test_exact_failure_is_blocked_only_after_threshold() -> None:+ progress = ToolProgress()+ key = ("read_url", '{"url":"https://example.test/missing"}')++ for attempt in range(FAILURE_BLOCK_THRESHOLD):+ assert progress.is_blocked(key) is (attempt >= FAILURE_BLOCK_THRESHOLD)+ progress.record(+ "read_url",+ key,+ '{"status":"error","message":"missing"}',+ success=False,+ is_readonly=True,+ )++ assert progress.is_blocked(key) is True+++def test_successful_mutation_clears_failed_call_ledger() -> None:+ progress = ToolProgress()+ key = ("read_url", '{"url":"https://example.test/missing"}')+ for _ in range(FAILURE_BLOCK_THRESHOLD):+ progress.record(+ "read_url",+ key,+ '{"status":"error"}',+ success=False,+ is_readonly=True,+ )+ assert progress.is_blocked(key) is True++ progress.record(+ "write_file",+ ("write_file", '{"path":"note.md"}'),+ '{"status":"ok"}',+ success=True,+ is_readonly=False,+ )+ assert progress.is_blocked(key) is False+++def test_no_progress_limit_remains_bounded() -> None:+ progress = ToolProgress()+ for _ in range(NO_PROGRESS_LIMIT - 1):+ assert progress.finish_iteration() is False+ assert progress.finish_iteration() is True+ assert progress.stalled_iterations == NO_PROGRESS_LIMIT