Skip to content

FEAT: Store scenario planned units and link attempts to them - #4650

Open
Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:feat/scenario-planned-units-table
Open

Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:feat/scenario-planned-units-table

Conversation

@behnam-o

@behnam-o Behnam (behnam-o) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A run's planned units are now stored as rows, and each attempt is linked to its unit when it's written. Scenario history becomes an indexed join, instead of rebuilding the plan from scenario_metadata JSON and guessing which unit each attempt belongs to on every read.

  • New ScenarioPlannedUnitEntries table: one row per planned atomic group × seed group, written in the same transaction as the scenario result. Row IDs are deterministic (uuid5), so resumed runs can link attempts without reading the table back.
  • New AttackResultEntries.planned_unit_id foreign key: AtomicAttack sets it through AttackResultAttribution, and so do the adversarial benchmark's cached result copies.
  • History aggregates: join on the new foreign key. Runs without a plan keep the existing path. The per-dialect plan-expansion SQL is removed.
  • Migration (SQLite and Azure SQL): creates the table, column and indexes, fills in units from stored plans, and links existing attempts using the same rules history used. Ambiguous attempts stay NULL, the counts are logged, and the migration is safe to run again.
  • Behavior change: for runs with a plan, history counts only linked attempts. Results written directly with only attribution_data are no longer counted; existing rows are covered by the migration.
  • Also updates test_finding_field_values_migration, which assumed its revision was the latest.

Open questions:

  • On SQL Server, deleting a scenario that has linked attempts is now blocked (NO ACTION). PyRIT has no delete path for scenarios today.
  • During rollout, attempts written by an older PyRIT process won't be linked.
  • The units are now stored in both run_plan and the new table.

Related: #4648 (limits slow reads) and #4649 (Python-side plan matching, no migration). Each stands on its own. This one and #4649 both change the history aggregation code, so whichever merges second will be rebased.

Scenario history rebuilt each run's planned units from the run_plan JSON in
ScenarioResultEntries.scenario_metadata and matched every attempt to them with
nested OPENJSON/json_each on every read. On SQL Server this re-expanded the
plan per attempt and exhausted the database.

- Add ScenarioPlannedUnitEntries (one row per planned atomic group x seed
  group), written in the same transaction as the scenario result.
- Add AttackResultEntries.planned_unit_id (FK, NO ACTION). AtomicAttack and
  the adversarial benchmark's cached copies set it at write time through
  AttackResultAttribution, using the deterministic
  ScenarioRunPlan.compute_planned_unit_id so resumed runs need no lookup.
- Scenario history aggregates join the planned unit on its primary key
  instead of expanding the plan; runs without a plan keep the legacy path.
  The backend _get_scenario_plan_unit_subqueries hooks are removed.
- Migration 54eb9489b8f0 creates the table, column and indexes on SQLite and
  SQL Server, backfills units from every stored run plan, and links existing
  attempts with the resolution history used (name + technique hash, then
  attributed seed group or a unique objective match). Unresolved attempts
  stay NULL; counts are logged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An SDK recount can incorrectly mark unlinked planned-run attempts as completed, while quadratic backfill work can delay schema upgrades and prolong database locks.

2 open issues
What changed in this PR

This PR moves planned scenario units into persistent rows and links attempts when they are written, replacing repeated plan-JSON matching in history queries with indexed joins.

Changes:

  • Adds deterministic planned-unit IDs, storage, and attempt attribution.
  • Migrates existing plans and resolvable attempts while leaving ambiguous attempts unlinked.
  • Updates history aggregation and regression coverage for link-based counting.
File Description
tests/​unit/​scenario/​core/​test_atomic_attack.py Checks per-seed planned-unit attribution.
tests/​unit/​scenario/​benchmark/​test_adversarial.py Checks cached copies link to the current run.
tests/​unit/​memory/​test_scenario_planned_units_migration.py Covers backfill, repeatability, downgrade, and SQL Server schema compilation.
tests/​unit/​memory/​test_finding_field_values_migration.py Upgrades to head before schema validation.
tests/​unit/​memory/​test_azure_sql_memory.py Checks history queries use planned-unit joins.
tests/​unit/​memory/​memory_interface/​test_interface_scenario_history.py Covers planned-unit persistence and linked counting.
tests/​unit/​executor/​attack/​core/​test_attack_strategy.py Checks attribution on success and error paths.
tests/​unit/​backend/​test_scenario_hierarchy_parity.py Adds planned-unit links to parity fixtures.
tests/​unit/​analytics/​test_scenario_statistics_parity.py Backfills legacy-style parity fixtures.
pyrit/​scenario/​scenarios/​benchmark/​adversarial.py Links cached result copies to current planned units.
pyrit/​scenario/​core/​atomic_attack.py Computes planned-unit IDs for attempt attribution.
pyrit/​models/​scenario_progress.py Defines deterministic planned-unit ID generation.
pyrit/​models/​results/​attack_result.py Adds the optional planned-unit reference.
pyrit/​memory/​sqlite_memory.py Removes SQLite plan-expansion SQL.
pyrit/​memory/​memory_models.py Adds planned-unit rows and attempt foreign-key mapping.
pyrit/​memory/​memory_interface.py Persists planned units and joins them for history counts.
pyrit/​memory/​azure_sql_memory.py Removes SQL Server plan-expansion SQL.
pyrit/​memory/​alembic/​versions/​54eb9489b8f0_add_scenario_planned_units.py Creates and backfills planned units and attempt links.
pyrit/​executor/​attack/​core/​attack_strategy.py Copies planned-unit attribution onto results.
pyrit/​executor/​attack/​core/​attack_result_attribution.py Adds planned-unit ID to execution attribution.
pyrit/​analytics/​scenario_statistics.py Updates documentation to describe link-based SQL history.

Review effort: Balanced

(matched.c.scenario_result_id.notin_(plan_entry_ids), 1),
(matched.c.planned_seed_group_id.is_(None), 0),
(attempts.c.scenario_result_id.notin_(plan_entry_ids), 1),
(planned_units.c.id.is_(None), 0),
eval_hash = "" if eval_hash is None else eval_hash
seed_group_id = data.get("seed_group_id") or None
best: tuple[tuple[int, str], uuid.UUID] | None = None
for unit in units:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants