"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(live): audit the no-side-effect sweep re-check once per episode, not per tick
Haozhe Wu637791b19243
diff --git a/agent/src/live/runtime/runner.py b/agent/src/live/runtime/runner.pyindex 65607b0e9..e09f6f2f8 100644--- a/agent/src/live/runtime/runner.py+++ b/agent/src/live/runtime/runner.py@@ -396,6 +396,9 @@ class LiveRunner:#: Set once the preemptive sweep has fired so a tripped channel never#: flattens twice across consecutive ticks (no-retry, SPEC §8.5).self._flatten_fired = False+ # (episode, intent) pairs whose no-side-effect sweep outcome this+ # runner has already audited — see _note_sweep_recheck.+ self._sweep_recheck_audited: set[tuple[str, str]] = set()@propertydef runner_id(self) -> str:@@ -549,16 +552,20 @@ class LiveRunner:logger.warning("failed to write heartbeat for %s", self.runner_id, exc_info=True)def _halted_result(self) -> dict[str, Any]:- """Fire the preemptive sweep ONCE, audit, and return for a halted tick.+ """Fire the preemptive sweep, audit, and return for a halted tick.This closes Hole #1 (SPEC §7.5 #6): a tripped halt is no longer merelycooperative (refuse the next order). The runner cancels every restingorder and — per the mandate's flatten flag — flattens open positions, via- the injected broker submit callable. The sweep runs at most once per- runner lifetime (``_flatten_fired`` latch): a halted channel that keeps- ticking must not re-submit closes (no-retry, SPEC §8.5). With no broker- write surface wired (``submit_fn is None``) the sweep is skipped and the- cooperative gate alone blocks future orders.+ the injected broker submit callable. The sweep submits closes at most+ once per halt episode (``_flatten_fired`` plus the on-disk latch): a+ halted channel that keeps ticking must not re-submit closes (no-retry,+ SPEC §8.5). A sweep that attempted NO broker write — reads failed, or+ the book was already flat — is not latched and re-checks on the next+ tick, so a resting order that appears after the first sweep is still+ cancelled. With no broker write surface wired (``submit_fn is None``)+ the sweep is skipped and the cooperative gate alone blocks future+ orders."""self._run_preemptive_sweep()audit_id = self._audit(@@ -573,6 +580,31 @@ class LiveRunner:audit_id=audit_id,).to_dict()+ def _note_sweep_recheck(self, episode: str, intent: str) -> bool:+ """Return whether this sweep re-check outcome is new for the episode.++ The no-side-effect branch of :meth:`_run_preemptive_sweep` re-runs on+ every halted tick by design (a resting order that appears after the+ first sweep must still be cancelled). Its audit record must not repeat+ with it: one tick per minute on a channel left halted overnight would+ append ~1440 records describing the same unchanged condition. The+ record is therefore written the first time a given ``(episode,+ intent)`` pair is seen by this runner and suppressed afterwards, while+ the logger still sees every occurrence.++ Args:+ episode: Halt-episode identity the sweep is running under.+ intent: Normalized intent string identifying the condition.++ Returns:+ ``True`` when the pair has not been audited yet by this runner.+ """+ key = (episode, intent)+ if key in self._sweep_recheck_audited:+ return False+ self._sweep_recheck_audited.add(key)+ return True+def _run_preemptive_sweep(self) -> None:"""Cancel resting orders + (per mandate) flatten positions, exactly once.@@ -692,18 +724,29 @@ class LiveRunner:# No cancel or close was attempted → nothing happened that a# later tick (or a restart) must not repeat. Do NOT latch: the# sweep retries for this episode.+ #+ # This branch runs on EVERY halted tick, so the audit record is+ # written once per (episode, condition) per runner instead of+ # once per tick: a channel left halted overnight would otherwise+ # append one record per tick to the hash-chained ledger forever.+ # Every occurrence is still logged. A flat book is also not a+ # breach — auditing "nothing to act on" as kind=breach/error+ # made a clean outcome indistinguishable from a real one.if read_errors:detail = "; ".join(str(e.get("error")) for e in read_errors)+ kind, outcome = "breach", "error"intent = "preemptive halt sweep read failed — retrying on next tick"else:detail = "no open orders or positions to act on"+ kind, outcome = "halt_tripped", "blocked"intent = "preemptive halt sweep found nothing to act on — re-checking on next tick"- self._audit(- kind="breach",- outcome="error",- intent=intent,- error=detail,- )+ if self._note_sweep_recheck(episode, intent):+ self._audit(+ kind=kind,+ outcome=outcome,+ intent=intent,+ error=detail,+ )returnself._flatten_fired = Truetry:diff --git a/agent/tests/test_sweep_latch.py b/agent/tests/test_sweep_latch.pyindex fcef44c56..19cc0325b 100644--- a/agent/tests/test_sweep_latch.py+++ b/agent/tests/test_sweep_latch.py@@ -386,3 +386,62 @@ def test_mid_sweep_retrip_does_not_release_new_episode_claim(# The ep1 owner's own claim must be gone (released normally).assert not sweep_latch.claim_path(BROKER, "2026-08-27T01:00:00+00:00").exists()sweep_latch.release_claim(BROKER, captured["ep2"])+++def _count_audits(live_root: Path, flatten_fn: Any, ticks: int) -> tuple[int, list[str]]:+ """Run one runner over ``ticks`` halted ticks; return audit count + kinds."""+ kinds: list[str] = []++ async def _agent_caller(session_id: str, prompt: str) -> Mapping[str, Any]:+ return {"status": "success"}++ def _audit(event) -> Mapping[str, Any]:+ kinds.append(getattr(event, "kind", "?"))+ return {"audit_id": "a1"}++ runner = LiveRunner(+ BROKER,+ agent_caller=_agent_caller,+ reconcile_fn=lambda *a, **k: None,+ read_positions=list,+ read_balance=list,+ read_open_orders=list,+ write_audit_fn=_audit,+ halt_flag_fn=lambda broker: True,+ submit_fn=lambda request: {"status": "ok"},+ flatten_fn=flatten_fn,+ session_id="latch-test",+ )+ for _ in range(ticks):+ asyncio.run(runner.run_once())+ return len(kinds), kinds+++def test_flat_book_recheck_audits_once_not_once_per_tick(live_root: Path) -> None:+ # The no-side-effect branch re-runs every halted tick by design. Its audit+ # record must not: a channel left halted overnight at a 1-minute tick would+ # append ~1440 identical records to the hash-chained ledger. One record per+ # (episode, condition) per runner; the rest is logging.+ _trip_with_timestamp(BROKER, "2026-08-27T01:00:00+00:00")++ def _flatten(broker, submit, read_positions, read_open_orders):+ return {"errors": []} # reads ok, nothing to act on++ total, kinds = _count_audits(live_root, _flatten, ticks=5)+ # 5 halt_tripped tick records + exactly ONE re-check record.+ assert total == 6, kinds+ # A flat book is not a breach.+ assert "breach" not in kinds, kinds+++def test_persistent_read_failure_audits_the_breach_once_per_episode(+ live_root: Path,+) -> None:+ _trip_with_timestamp(BROKER, "2026-08-27T01:00:00+00:00")++ def _flatten(broker, submit, read_positions, read_open_orders):+ return {"errors": [{"phase": "read_positions", "error": "broker read failed"}]}++ total, kinds = _count_audits(live_root, _flatten, ticks=5)+ assert total == 6, kinds+ assert kinds.count("breach") == 1, kinds