fix(usage): stop double counting scheduled runs; attribute workflow node usage

sum_tokens_in_range now skips the scheduler's per-run rollup rows, whose
tokens are already on the run's per-call rows, so the per-agent 24h limit
and the admin total no longer count scheduled spend twice.

Workflow node LLMs carry the workflow agent's id, so their usage rows are
attributed to the agent instead of landing with a user id only.

usage_totals returns a user's tokens and cost since a window start, split
by interactive and agent-key traffic.
This commit is contained in:
Alex committed 2026-09-21 12:11:11 +01:00
1 parent 43fad2a865
commit 6c42139224
4 files changed
+97 -3

No files matched your search

+21
View File
@@ -214,6 +214,27 @@ class TestWorkflowEngineAgenticNode:
assert engine.state["node_agent_agentic_output"] == "agentic answer"
assert engine.state["result"] == "agentic answer"
def test_node_usage_is_attributed_to_the_workflow_agent(self, monkeypatch):
engine = create_engine()
engine.agent.agent_id = "11111111-1111-1111-1111-111111111111"
node = create_agent_node(node_id="agent_attr", agent_type="classic")
captured: Dict[str, Any] = {}
def capture_create(**kwargs):
captured.update(kwargs)
return StubNodeAgent([{"answer": "ok"}])
monkeypatch.setattr(WorkflowNodeAgentFactory, "create", staticmethod(capture_create))
monkeypatch.setattr(
"docsgpt.core.model_utils.get_api_key_for_provider",
lambda _provider: None,
)
list(engine._execute_agent_node(node))
assert captured["agent_id"] == "11111111-1111-1111-1111-111111111111"
def test_agentic_node_passes_retriever_config(self, monkeypatch):
engine = create_engine()
# The node-source authorization gate is exercised separately;