Files
hermes-agent/tests/test_batch_runner_trajectory_redaction.py
Maciej Lisiewski ba8715c467 fix(trajectory): redact secrets before trajectory and batch dataset persistence (#155351)
* fix(trajectory): redact secrets before persistence

* fix(batch_runner): redact trajectories at the batch_*.jsonl append too

batch_runner saves trajectories itself (save_trajectories=False on the
agent), so the save_trajectory redaction never ran for the main dataset
path. Redact the conversations leaf-wise before _append_jsonl, and make
--resume match the redacted human turn so redacted prompts are not
re-run on every restart.

Fixes #108495

* fix(trajectory): redact embedded tool-call/response JSON decoded

The text pass ran over turns that already hold json.dumps'd <tool_call>/<tool_response>
bodies; whitespace-delimited rules (KEY=..., --password=..., Bearer ...) swallowed the
closing quote/braces, leaving unparseable tool calls in saved and batch trajectories.
Decode each block, redact its values, re-serialize; values the plain pass leaves unchanged
are retried on a one-field document so key-driven rules (api_key, x-api-key, Cookie) still
apply after decoding.

Refs #108495

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>

* fix(run_agent): redact --save_sample trajectory before writing

_save_sample_trajectory wrote the converter output and query raw to sample_<id>.json,
the same persistence boundary save_trajectory and batch_runner now redact.

Refs #108495

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>

---------

Co-authored-by: joaomarcos <joaomarcosdias444@gmail.com>
Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-14 12:43:56 +01:00

53 lines
2.1 KiB
Python

"""batch_runner writes trajectories to the exported dataset; secrets must be redacted (#108495)."""
import json
from batch_runner import BatchRunner, _process_batch_worker
SECRET = "sk-" + "proj-batchrunner-secret-1234567890"
def test_batch_worker_redacts_trajectory_before_append(tmp_path, monkeypatch):
prompt = f"use api_key={SECRET}"
result = {
"success": True,
"trajectory": [
{"from": "human", "value": prompt},
{"from": "gpt", "value": '<tool_call>{"name":"terminal","arguments":{"command":"curl -H \'Authorization: Bearer ' + SECRET + '\'"}}</tool_call>'},
],
"reasoning_stats": {"has_any_reasoning": True},
"tool_stats": {},
"metadata": {},
"completed": True,
"api_calls": 1,
"toolsets_used": [],
}
monkeypatch.setattr("batch_runner._process_single_prompt", lambda *a, **kw: result)
_process_batch_worker((1, [(0, {"prompt": prompt})], tmp_path, set(), {"verbose": False}))
raw = (tmp_path / "batch_1.jsonl").read_text(encoding="utf-8")
assert SECRET not in raw
rows = [json.loads(line) for line in raw.splitlines() if line.strip()]
assert [t["from"] for t in rows[0]["conversations"]] == ["human", "gpt"]
def test_resume_still_skips_prompt_saved_in_redacted_form(tmp_path, monkeypatch):
prompt = f"use api_key={SECRET}"
runner = BatchRunner.__new__(BatchRunner)
runner.output_dir = tmp_path
runner.dataset = [{"prompt": prompt}, {"prompt": "other"}]
result = {
"success": True,
"trajectory": [{"from": "human", "value": prompt}],
"reasoning_stats": {"has_any_reasoning": True},
"tool_stats": {}, "metadata": {}, "completed": True, "api_calls": 1, "toolsets_used": [],
}
monkeypatch.setattr("batch_runner._process_single_prompt", lambda *a, **kw: result)
_process_batch_worker((1, [(0, runner.dataset[0])], tmp_path, set(), {"verbose": False}))
remaining, skipped = runner._filter_dataset_by_completed(runner._scan_completed_prompts_by_content())
assert skipped == [0]
assert [idx for idx, _ in remaining] == [1]