mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-15 08:16:38 +00:00
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:
1 parent
43fad2a865
commit
6c42139224
4 files changed
+97
-3
No files matched your search
@@ -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;
|
||||
|
||||
@@ -45,6 +45,46 @@ class TestInsert:
|
||||
assert [float(c) for c in costs] == [0.0, 0.00012345]
|
||||
|
||||
|
||||
class TestRollupExclusion:
|
||||
def test_sum_tokens_ignores_schedule_rollups(self, pg_conn):
|
||||
repo = _repo(pg_conn)
|
||||
repo.insert(user_id="u-roll", prompt_tokens=10, generated_tokens=5, source="agent_stream")
|
||||
repo.insert(user_id="u-roll", prompt_tokens=10, generated_tokens=5, source="schedule")
|
||||
total = repo.sum_tokens_in_range(
|
||||
start=_now() - timedelta(minutes=1), end=_now() + timedelta(minutes=1), user_id="u-roll"
|
||||
)
|
||||
assert total == 15
|
||||
|
||||
|
||||
class TestUsageTotals:
|
||||
def _seed(self, repo):
|
||||
repo.insert(user_id="u-tot", prompt_tokens=100, generated_tokens=10, cost=0.5)
|
||||
repo.insert(user_id="u-tot", api_key="k", prompt_tokens=20, generated_tokens=2, cost=0.25)
|
||||
repo.insert(user_id="u-tot", prompt_tokens=7, generated_tokens=0, cost=0.125, source="title")
|
||||
repo.insert(user_id="u-tot", prompt_tokens=999, generated_tokens=0, source="schedule")
|
||||
repo.insert(user_id="u-other", prompt_tokens=999, generated_tokens=0, cost=9)
|
||||
repo.insert(
|
||||
user_id="u-tot", prompt_tokens=999, generated_tokens=0, cost=9,
|
||||
timestamp=_now() - timedelta(days=40),
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"bucket, expected",
|
||||
[("all", (139, 0.875)), ("direct", (117, 0.625)), ("agent", (22, 0.25))],
|
||||
)
|
||||
def test_totals_per_bucket(self, pg_conn, bucket, expected):
|
||||
repo = _repo(pg_conn)
|
||||
self._seed(repo)
|
||||
assert repo.usage_totals(user_id="u-tot", start=_now() - timedelta(days=1), bucket=bucket) == expected
|
||||
|
||||
def test_no_usage_is_zero(self, pg_conn):
|
||||
assert _repo(pg_conn).usage_totals(user_id="nobody", start=_now() - timedelta(days=1)) == (0, 0.0)
|
||||
|
||||
def test_unknown_bucket_rejected(self, pg_conn):
|
||||
with pytest.raises(ValueError):
|
||||
_repo(pg_conn).usage_totals(user_id="u", start=_now(), bucket="nope")
|
||||
|
||||
|
||||
class TestReassignApiKey:
|
||||
def test_rewrites_and_preserves_rate_limit_window(self, pg_conn):
|
||||
# Rotating an agent key must carry the running 24h usage window over
|
||||
|
||||
Reference in new issue
Block a user