Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(live): publish consent before mandate authority

xkam7arec86d9b90fc1
diff --git a/agent/src/live/mandate/commit.py b/agent/src/live/mandate/commit.py
index 49c397837..7324fd195 100644
--- a/agent/src/live/mandate/commit.py
+++ b/agent/src/live/mandate/commit.py
@@ -413,8 +413,6 @@ def commit_mandate(
"expires_at": expires_iso,
},
}
- _atomic_write_json(broker_dir(broker) / _MANDATE_FILENAME, mandate_doc)
-
consent_record = {
"consent_record_id": consent_record_id,
"mandate_id": mandate_id,
@@ -431,7 +429,11 @@ def commit_mandate(
"created_at": created_iso,
"expires_at": expires_iso,
}
+ # Persist evidence before authority. If the consent write fails, no mandate
+ # becomes usable. If mandate publication fails afterward, the orphaned
+ # consent record is harmless and useful for audit/retry diagnosis.
_atomic_write_json(_consent_dir(broker) / f"{consent_record_id}.json", consent_record)
+ _atomic_write_json(broker_dir(broker) / _MANDATE_FILENAME, mandate_doc)
# One-shot: the proposal can never be committed again.
_invalidate_proposal(broker, proposal_id)
diff --git a/agent/tests/test_consent_commit.py b/agent/tests/test_consent_commit.py
index 4fde5c27f..959c20e92 100644
--- a/agent/tests/test_consent_commit.py
+++ b/agent/tests/test_consent_commit.py
@@ -19,6 +19,7 @@ from pathlib import Path
import pytest
import src.live.paths as paths
+import src.live.mandate.commit as mandate_commit
from src.live.mandate.commit import (
CommitError,
DEFAULT_MANDATE_LIFETIME_DAYS,
@@ -158,6 +159,62 @@ def test_commit_consumes_proposal_no_replay(live_runtime: Path) -> None:
)
+def test_failed_consent_write_does_not_publish_mandate(
+ live_runtime: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A failed approval record must not leave usable live authority."""
+ proposal = _propose()
+ real_write = mandate_commit._atomic_write_json
+
+ def fail_consent(path: Path, payload: dict) -> None:
+ if path.parent.name == "consent":
+ raise OSError("synthetic consent write failure")
+ real_write(path, payload)
+
+ monkeypatch.setattr(mandate_commit, "_atomic_write_json", fail_consent)
+
+ with pytest.raises(OSError, match="synthetic consent write failure"):
+ commit_mandate(
+ proposal_id=proposal["proposal_id"],
+ ordinal=1,
+ adjustments=None,
+ consent_ack=True,
+ broker="robinhood",
+ )
+
+ assert load_mandate("robinhood") is None
+ assert not (paths.broker_dir("robinhood") / "mandate.json").exists()
+
+
+def test_failed_mandate_write_leaves_only_harmless_consent(
+ live_runtime: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Publishing authority last makes its failure fail closed."""
+ proposal = _propose()
+ real_write = mandate_commit._atomic_write_json
+
+ def fail_mandate(path: Path, payload: dict) -> None:
+ if path.name == "mandate.json":
+ raise OSError("synthetic mandate write failure")
+ real_write(path, payload)
+
+ monkeypatch.setattr(mandate_commit, "_atomic_write_json", fail_mandate)
+
+ with pytest.raises(OSError, match="synthetic mandate write failure"):
+ commit_mandate(
+ proposal_id=proposal["proposal_id"],
+ ordinal=1,
+ adjustments=None,
+ consent_ack=True,
+ broker="robinhood",
+ )
+
+ assert load_mandate("robinhood") is None
+ assert len(list((paths.broker_dir("robinhood") / "consent").glob("*.json"))) == 1
+
+
# ---------------------------------------------------------------------------
# consent_ack gate
# ---------------------------------------------------------------------------