Implementation:AISecurityLab Hackagent arun campaign
| Knowledge Sources | |
|---|---|
| Domains | Security_Testing, Orchestration, Concurrency |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete tool for loading, resolving, auditing, and executing an AI red-team campaign end-to-end, provided by the HackAgent orchestrator.
Description
arun_campaign is the composition root of HackAgent. It loads and resolves the campaign, optionally audits the judge panel, executes every attack against every goal, records results to the store, and writes output files. The companion _AttackRunner class runs individual attacks, managing shared semaphores for attack/target/judge concurrency, trace recording, event emission, and error handling.
The synchronous wrapper run_campaign calls arun_campaign via run_coroutine_blocking, making it safe to call from within an already-running event loop (e.g., a Jupyter notebook).
Key sub-operations:
- _execute: Orchestrates attack execution (sequential, parallel, or escalate mode), optional goal labelling and selection.
- _AttackRunner.run: Runs one attack against its goals, creating a RunTracker, emitting events, and recording attempts.
- _AttackRunner._run_goal: Runs one goal through a static or iterative attack pipeline.
- _AttackRunner._generate_and_send: Static attack path: generate → send → decode → judge.
- _AttackRunner._search: Iterative attack path: search with panel in loop, collect findings, split trace.
- _AttackRunner._send / _complete / _judge / _ask_panel: Traced, concurrency-limited callables passed to attacks.
Usage
- Run a full campaign: Call run_campaign(yaml_path) or await arun_campaign(yaml_path) to execute a campaign from YAML.
- Dry run: Set execution.dry_run: true in the campaign YAML to validate and build everything without sending requests.
- Event monitoring: Pass an on_event callback to receive audit_finished, attack_started, goal_finished, attack_finished events.
- Pre-resolved campaign: Pass a resolved ResolvedCampaign to skip re-resolution (e.g., to report what the run will do before starting).
Code Reference
Source Location
- Repository: AISecurityLab_Hackagent
- File: hackagent/orchestrator/campaign/runner.py
Signature
async def arun_campaign(
source: str | Path | Mapping[str, Any] | CampaignSpec,
*,
on_event: Optional[EventCallback] = None,
build: ModelBuilder = build_model,
load: GoalLoader = load_goals,
store: Optional[Store] = None,
calibration: CalibrationLoader = load_calibration,
build_embed: EmbedderBuilder = build_embedder,
settings: Optional[Settings] = None,
resolved: Optional[ResolvedCampaign] = None,
) -> CampaignResult:
"""Load, resolve, audit, and execute a campaign, then write its outputs."""
def run_campaign(
source: str | Path | Mapping[str, Any] | CampaignSpec,
*,
on_event: Optional[EventCallback] = None,
build: ModelBuilder = build_model,
load: GoalLoader = load_goals,
store: Optional[Store] = None,
calibration: CalibrationLoader = load_calibration,
build_embed: EmbedderBuilder = build_embedder,
settings: Optional[Settings] = None,
resolved: Optional[ResolvedCampaign] = None,
) -> CampaignResult:
"""Synchronous form of arun_campaign."""
Import
from hackagent.orchestrator.campaign.runner import run_campaign, arun_campaign, CampaignCancelled
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| source | Path | Mapping | CampaignSpec | Yes | Campaign YAML path, decoded mapping, or CampaignSpec |
| on_event | EventCallback | No | Listener for audit_finished, attack_started, goal_finished, attack_finished |
| build | ModelBuilder | No | Model factory (default: build_model) |
| load | GoalLoader | No | Goal loader (default: load_goals) |
| store | Store | No | Persistence backend (opened if None) |
| calibration | CalibrationLoader | No | Labelled sample loader (default: load_calibration) |
| build_embed | EmbedderBuilder | No | Embedder factory (default: build_embedder) |
| settings | Settings | No | Runtime settings (resolved if None) |
| resolved | ResolvedCampaign | No | Pre-resolved campaign (skip re-resolution) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | CampaignResult | campaign_name, run_id, dry_run, campaign_run_id, attacks, audit, outputs |
| raises | When panel fails audit and on_failure is "stop" | |
| raises | When on_event listener raises CampaignCancelled |
Usage Examples
Run a Campaign Synchronously
from hackagent.orchestrator.campaign.runner import run_campaign
result = run_campaign("campaigns/quick_check.yaml")
print(f"Run {result.run_id}: {len(result.attacks)} attacks, {len(result.outputs)} output files")
for outcome in result.attacks:
print(f" {outcome.name}: {len(outcome.attempts)} attempts, error={outcome.error}")
Run with Event Callback
from hackagent.orchestrator.campaign.runner import run_campaign
def on_event(event, **payload):
if event == "goal_finished":
print(f" Goal {payload['goal_index']}: success={payload['success']}, "
f"{payload['attempts']} attempts, {payload['elapsed_s']:.1f}s")
elif event == "attack_finished":
print(f"Attack {payload['attack']} finished: error={payload.get('error')}")
result = run_campaign("campaigns/quick_check.yaml", on_event=on_event)
Dry Run (Validate Only)
from hackagent.orchestrator.campaign.runner import run_campaign
# Set dry_run: true in the YAML or pass a spec with execution.dry_run=True
result = run_campaign("campaigns/quick_check.yaml")
if result.dry_run:
print("Dry run — validated and built, no requests sent")
Related Pages
Implements Principle
- Principle:AISecurityLab_Hackagent_Campaign_Execution_Orchestration
- Principle:AISecurityLab_Hackagent_Concurrency_Control
- Principle:AISecurityLab_Hackagent_Trace_Tree_Recording