Files
hermes-agent/tools/connectors/data_use.py
Siddharth Balyan 086984caf5 refactor(data-use): carry the requirement on the dispatch result, not a global store
The process-global _PENDING dict keyed by (turn_id, tool_call_id), its lock
and its 512 cap existed only to move a value from connector dispatch to
_commit_tool_result. _dispatch_authorized_once already owns the one real
dispatch and returns _ManagedToolResult to both executors, so it now binds a
per-call ContextVar sink around the dispatch and stores what connector
dispatch recorded on that result (and on _ToolOutcome for the concurrent
path). A batch's entries add to the wrapper's sink, so the union needs no
take/re-record. Callers that bind no sink (execute_code RPC, MCP serve)
record nothing, which removes the unbounded-growth case the cap guarded.
2026-10-21 15:46:19 +05:30

31 lines
958 B
Python

"""Data-use requirements of connector results, collected while one tool call dispatches.
The executor binds a sink around each dispatch and connector dispatch adds to it, so a batch's entries
union into the wrapper's sink and the requirement never rides the model-visible result string.
"""
from __future__ import annotations
import contextlib
import contextvars
from typing import Iterable, Iterator, Optional
_sink: contextvars.ContextVar[Optional[set[str]]] = contextvars.ContextVar("connector_data_use", default=None)
@contextlib.contextmanager
def collect_data_use() -> Iterator[set[str]]:
sink: set[str] = set()
token = _sink.set(sink)
try:
yield sink
finally:
_sink.reset(token)
def record_data_use(requires: Iterable[str]) -> None:
"""No-op outside an agent dispatch (execute_code RPC, MCP serve): no message is built there."""
sink = _sink.get()
if sink is not None:
sink.update(requires)