Skip to content

World's first open-source, agentic video production system. 12 production pipelines, 100+ tools, 700+ agent skill and production-knowledge files. Turn your AI coding assistant into a full video production studio.

fix(cost-tracker): reject invalid state transitions (#647)

Terminator6666667ad9fac9ba59
diff --git a/tests/tools/test_cost_tracker_governance.py b/tests/tools/test_cost_tracker_governance.py
index e97e4783..8a25bf61 100644
--- a/tests/tools/test_cost_tracker_governance.py
+++ b/tests/tools/test_cost_tracker_governance.py
@@ -55,6 +55,35 @@ class CostTrackerGovernanceTests(unittest.TestCase):
restarted.reserve(entry_id)
self.assertEqual(restarted.entries[-1]["status"], "reserved")
+ def test_completed_entry_cannot_be_refunded(self) -> None:
+ tracker = CostTracker(
+ mode=BudgetMode.OBSERVE,
+ require_approval_for_new_paid_tool=False,
+ )
+ entry_id = tracker.estimate("paid_video", "generate", 1.0)
+ tracker.reserve(entry_id)
+ tracker.reconcile(entry_id, actual_usd=0.8)
+
+ with self.assertRaisesRegex(ValueError, "Cannot refund.*completed"):
+ tracker.refund(entry_id)
+
+ self.assertEqual(tracker.budget_spent_usd, 0.8)
+ self.assertEqual(tracker.entries[0]["status"], "completed")
+
+ def test_entry_lifecycle_rejects_out_of_order_operations(self) -> None:
+ tracker = CostTracker(
+ mode=BudgetMode.OBSERVE,
+ require_approval_for_new_paid_tool=False,
+ )
+ entry_id = tracker.estimate("paid_video", "generate", 1.0)
+
+ with self.assertRaisesRegex(ValueError, "Cannot reconcile.*estimated"):
+ tracker.reconcile(entry_id, actual_usd=0.8)
+
+ tracker.reserve(entry_id)
+ with self.assertRaisesRegex(ValueError, "Cannot reserve.*reserved"):
+ tracker.reserve(entry_id)
+
if __name__ == "__main__":
unittest.main()
diff --git a/tools/cost_tracker.py b/tools/cost_tracker.py
index 8c363d7d..be0355e3 100644
--- a/tools/cost_tracker.py
+++ b/tools/cost_tracker.py
@@ -121,6 +121,7 @@ class CostTracker:
when the action exceeds the single-action approval threshold.
"""
entry = self._find(entry_id)
+ self._require_status(entry, EntryStatus.ESTIMATED, "reserve")
estimated = entry["estimated_usd"]
# Check single-action approval threshold
@@ -164,6 +165,7 @@ class CostTracker:
def reconcile(self, entry_id: str, actual_usd: float, success: bool = True) -> None:
"""Reconcile actual spend after tool execution."""
entry = self._find(entry_id)
+ self._require_status(entry, EntryStatus.RESERVED, "reconcile")
entry["status"] = EntryStatus.COMPLETED.value if success else EntryStatus.FAILED.value
entry["actual_usd"] = round(actual_usd, 4)
entry["reserved_usd"] = 0.0
@@ -173,6 +175,7 @@ class CostTracker:
def refund(self, entry_id: str) -> None:
"""Cancel a reservation without executing."""
entry = self._find(entry_id)
+ self._require_status(entry, EntryStatus.RESERVED, "refund")
entry["status"] = EntryStatus.REFUNDED.value
entry["reserved_usd"] = 0.0
entry["timestamp"] = self._now()
@@ -514,6 +517,16 @@ class CostTracker:
return entry
raise KeyError(f"Cost entry {entry_id!r} not found")
+ @staticmethod
+ def _require_status(
+ entry: dict[str, Any], expected: EntryStatus, operation: str
+ ) -> None:
+ if entry["status"] != expected.value:
+ raise ValueError(
+ f"Cannot {operation} cost entry {entry['id']!r} with status "
+ f"{entry['status']!r}; expected {expected.value!r}"
+ )
+
@staticmethod
def _new_id() -> str:
return uuid.uuid4().hex[:12]