Implementation:AISecurityLab Hackagent TraceNode
| Knowledge Sources | |
|---|---|
| Domains | Security_Testing, Observability, Tracing |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete tool for recording what an attack did as a tree of trace nodes, provided by the HackAgent attacks framework.
Description
The trace module records an attack's search as a tree of TraceNode objects, not a flat log. A search is a tree: BoN fans a step out into candidates, PAIR runs independent streams, TAP expands each surviving node into children and prunes the rest. Recording that as a flat list loses the structure — especially under concurrency, where asyncio finishes tasks in whatever order it likes.
Every node carries its own path: a tuple like (2, 0) meaning the first child of the third node. Position is recorded, never inferred. There are four kinds of node:
- phase: A step of the algorithm (an iteration, a stream, a branch). Opened with the phase() context manager.
- call: One model call — target, role, or panel — in a uniform shape so cost can be read without knowing which attack produced it.
- decision: Why the search went the way it did — stopped, pruned, backtracked, skipped.
- artifact: Something the attack wrote down that is not a call (e.g., RAG's poisoned documents).
The module uses ContextVar-based recording so concurrent branches of one search each get their own position. The campaign runner opens a recording() per goal and records every call itself; attacks mark only their own structure.
Key functions: recording(), phase(), decision(), artifact(), record_call(), record_target_call(), record_judge_call(), traced_completion().
Usage
- Recording a search: Open a recording() context, use phase() to mark iterations, decision() for turns, and the runner records calls automatically.
- Wrapping a role callable: Use traced_completion(name, completion) to wrap an auxiliary model so each call is recorded as a call node.
- Reading the tree: After recording, iterate trace.nodes — each TraceNode has path, node, label, scope, data, latency_s, error.
Code Reference
Source Location
- Repository: AISecurityLab_Hackagent
- File: hackagent/attacks/techniques/trace.py
Signature
@dataclass(frozen=True)
class TraceNode:
"""One node of a search, at a known position in it."""
path: Path
node: str # "phase", "call", "decision", "artifact"
label: str = ""
scope: Scope = "goal"
data: Mapping[str, Any] = field(default_factory=dict)
latency_s: Optional[float] = None
error: Optional[str] = None
@property
def parent(self) -> Path: ...
def under(self, path: Path) -> bool: ...
@contextmanager
def recording(scope: Scope = "goal") -> Iterator[Trace]: ...
@contextmanager
def phase(label: str, **data: Any) -> Iterator[Path]: ...
def decision(outcome: str, reason: str = "", **data: Any) -> None: ...
def artifact(kind: str, uri: str = "", **data: Any) -> None: ...
def record_call(role, request, response, latency_s, *, error=None, usage=None) -> None: ...
def record_target_call(messages, response, latency_s) -> None: ...
def record_judge_call(sample, verdict, latency_s) -> None: ...
def traced_completion(name: str, completion: Completion) -> Completion: ...
Import
from hackagent.attacks.techniques.trace import (
TraceNode, recording, phase, decision, artifact,
record_call, record_target_call, record_judge_call, traced_completion,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| scope | str | No | "goal" (default) or "run" for run-scoped work |
| label | str | Yes (phase/decision) | Name of the step or outcome |
| data | dict | No | Node-specific payload |
Outputs
| Name | Type | Description |
|---|---|---|
| Trace.nodes | Flat list of tree nodes, each with its path | |
| traced_completion | Wrapped callable that records each call |
Usage Examples
Recording a Search with Phases
from hackagent.attacks.techniques.trace import recording, phase, decision, current_path
with recording() as trace:
async with phase("round_0"):
# Runner records the target call automatically
response = await target(messages)
verdict = await judge(sample)
if verdict and verdict.success:
decision("stopped", "goal achieved")
else:
decision("continue", "trying next round")
# Read the tree
for node in trace.nodes:
print(f" {' ' * (len(node.path) - 1)}{node.node}: {node.label}")
Wrapping a Role Model for Tracing
from hackagent.attacks.techniques.trace import traced_completion
from hackagent.models import as_completion
# During resolution, each role's completion is wrapped:
attacker_callable = as_completion(build(model_config))
traced_attacker = traced_completion("attacker", attacker_callable)
# Now every call through traced_attacker is recorded as a "call" node