Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(workflow): persist budget in run snapshots

guozirui1176848a70451
diff --git a/jiuwenswarm/agents/harness/team/handlers/workflow_state.py b/jiuwenswarm/agents/harness/team/handlers/workflow_state.py
index 8d750ab8b9..e3f5e5f072 100644
--- a/jiuwenswarm/agents/harness/team/handlers/workflow_state.py
+++ b/jiuwenswarm/agents/harness/team/handlers/workflow_state.py
@@ -78,6 +78,7 @@ class WorkflowProgress(BaseModel):
node_type: Optional[str] = None
agent_id: Optional[str] = None
answer: Optional[str] = None
+ budget: Optional[dict[str, Any]] = None
# ---------------------------------------------------------------------------
@@ -191,6 +192,7 @@ class WorkflowRunState(BaseModel):
token_count: Optional[int] = None
duration_ms: Optional[int] = None
estimated_token_count: Optional[int] = None
+ budget: Optional[dict[str, Any]] = None
# Private mutable state for ID generation sequencing (not serialized)
_phase_counter: int = 0 # Global phase counter (1-based)
@@ -288,6 +290,8 @@ class WorkflowRunState(BaseModel):
handler = self._KIND_HANDLERS.get(kind)
if handler is None:
return None
+ if progress.budget is not None:
+ self.budget = progress.budget
method = getattr(self, handler)
return method(progress)
@@ -783,6 +787,7 @@ class WorkflowRunState(BaseModel):
"agent_count": self.agent_count,
"completed_agent_count": self.completed_agent_count,
"started_at": self.started_at,
+ "budget": self.budget,
"logs": [log_text],
}
@@ -796,6 +801,7 @@ class WorkflowRunState(BaseModel):
"agent_count": self.agent_count,
"completed_agent_count": self.completed_agent_count,
"started_at": self.started_at,
+ "budget": self.budget,
"phases": [p.to_dict() for p in self.phases],
"logs": list(self.logs),
}
@@ -813,6 +819,7 @@ class WorkflowRunState(BaseModel):
"agent_count": self.agent_count,
"completed_agent_count": self.completed_agent_count,
"started_at": self.started_at,
+ "budget": self.budget,
"phases": [p.to_dict() for p in phases],
}
@@ -828,6 +835,7 @@ class WorkflowRunState(BaseModel):
"started_at": self.started_at,
"completed_at": self.completed_at,
"duration_ms": self.duration_ms,
+ "budget": self.budget,
}
if self.error:
result["error"] = self.error
@@ -866,4 +874,5 @@ class WorkflowRunState(BaseModel):
# reserved fields — pending upstream token accounting
result["token_count"] = self.token_count
result["estimated_token_count"] = self.estimated_token_count
+ result["budget"] = self.budget
return result
diff --git a/tests/unit_tests/agentserver/test_workflow_state.py b/tests/unit_tests/agentserver/test_workflow_state.py
index b92e5adac4..43622bf04c 100644
--- a/tests/unit_tests/agentserver/test_workflow_state.py
+++ b/tests/unit_tests/agentserver/test_workflow_state.py
@@ -342,6 +342,24 @@ class TestWorkflowRunStateSerialization:
assert len(snapshot["phases"]) == 1
assert len(snapshot["phases"][0]["agents"]) == 1
+ @staticmethod
+ def test_to_workflow_run_dict_preserves_budget_from_progress():
+ """Workflow snapshots expose the budget received from agent-core events."""
+ budget = {"spent": 2, "total": 5, "remaining": 3}
+ state = WorkflowRunState()
+ state.apply(
+ _make_progress(
+ "workflow_started",
+ workflow_name="budgeted-workflow",
+ budget=budget,
+ )
+ )
+
+ snapshot = state.to_workflow_run_dict()
+
+ assert state.budget == budget
+ assert snapshot["budget"] == budget
+
class TestWorkflowRunStateTimestamps:
"""Verify timestamp and duration fields."""
@@ -802,4 +820,4 @@ class TestAgentNodeType:
assert state.phases[0].name == _UNNAMED_PHASE == "unnamed phase"
assert state.phases[0].status == "running"
assert [a.name for a in state.phases[0].agents] == ["coder", "reviewer"]
- assert state.phases[0].agent_count == 2
\ No newline at end of file
+ assert state.phases[0].agent_count == 2