forked from omnigent-ai/omnigent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_token_usage.py
More file actions
31 lines (23 loc) · 1.05 KB
/
Copy path_token_usage.py
File metadata and controls
31 lines (23 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Per-test nodeid attribution for the token-usage auto-recorder.
The recording + JSON-writing logic lives in
:mod:`omnigent.llms._usage_observer`, which auto-activates whenever
``OMNIGENT_TOKEN_USAGE_JSON`` is set in the environment (including in
subprocesses spawned by tests). This plugin's only job is to mark
which test is currently running so LLM calls get attributed to the
right ``nodeid``: both in-process calls (via a module global) and
calls fired in spawned server / runner / harness subprocesses (via
the per-worker sidecar file that ``set_current_test`` publishes).
See the observer module's docstring for the file format and how
multi-process writes are merged at the workflow level.
"""
from __future__ import annotations
from typing import Any
import pytest
from omnigent.llms import _usage_observer
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_protocol(item: pytest.Item, nextitem: pytest.Item | None) -> Any:
_usage_observer.set_current_test(item.nodeid)
try:
yield
finally:
_usage_observer.set_current_test(None)