Repository navigation
FEAT: Store scenario planned units and link attempts to them - #4650
Open
Behnam (behnam-o) wants to merge 1 commit into
Open
Behnam (behnam-o) wants to merge 1 commit into
Behnam (behnam-o) wants to merge 1 commit into
Conversation
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>
This was referenced Nov 25, 2026
Contributor
There was a problem hiding this comment.
🟡 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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_metadataJSON and guessing which unit each attempt belongs to on every read.ScenarioPlannedUnitEntriestable: 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.AttackResultEntries.planned_unit_idforeign key:AtomicAttacksets it throughAttackResultAttribution, and so do the adversarial benchmark's cached result copies.NULL, the counts are logged, and the migration is safe to run again.attribution_dataare no longer counted; existing rows are covered by the migration.test_finding_field_values_migration, which assumed its revision was the latest.Open questions:
NO ACTION). PyRIT has no delete path for scenarios today.run_planand 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.