Repository navigation
FIX: Resolve scenario-history planned units in Python instead of SQL - #4649
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
The scenario-history aggregates (GET /api/scenarios/runs and the run hierarchy aggregate) expanded every run plan from scenario_metadata with OPENJSON/json_each and matched attempts to planned units in SQL. On SQL Server this became nested loops that re-parsed the full metadata for every attempt x planned unit, taking tens of minutes for a handful of large runs and exhausting the connection pool. Memory now reads each page's attempt columns with one indexed query, loads each run plan once, and resolves attempts with ScenarioPlanLookup in Python. Unit ranking, retry pressure, envelope handling, and outcome and producer counts keep the previous SQL semantics, so API output is unchanged. The per-dialect plan expansions and SQL role-match hook are removed. No schema change. Adds an Azure SQL integration test with production-shaped runs that asserts the history page and run hierarchy stay within a time budget. 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 accepted compact plan can yield an empty lookup when a persisted seed lacks an objective, incorrectly reporting zero completed or successful units while suppressing the legacy recount.
1 open issue
What changed in this PR
This PR targets slow Scenario History reads on SQL Server by resolving planned units in Python instead of expanding run-plan JSON in SQL, without a schema change. It addresses query cost directly, complementing #4648’s timeout protection.
Changes:
- Read attempt columns once per page and calculate history counters using
ScenarioPlanLookup. - Remove obsolete dialect-specific plan-expansion and role-matching SQL.
- Update query assertions and add Azure SQL history and hierarchy performance coverage.
| File | Description |
|---|---|
tests/unit/memory/test_azure_sql_memory.py |
Replaces removed SQL-helper tests with an attempt-query isolation assertion. |
tests/unit/memory/memory_interface/test_interface_scenario_history.py |
Removes the obsolete plan-expansion hook test case. |
tests/unit/analytics/test_scenario_statistics_parity.py |
Updates comments to describe history aggregation rather than SQL aggregation. |
tests/integration/memory/test_scenario_history_scale_integration.py |
Adds production-shaped Azure SQL runs and read-time budgets. |
pyrit/memory/sqlite_memory.py |
Removes SQLite run-plan expansion SQL. |
pyrit/memory/memory_interface.py |
Replaces SQL aggregation with attempt retrieval, plan loading, and Python reduction. |
pyrit/memory/azure_sql_memory.py |
Removes SQL Server plan-expansion and role-matching helpers. |
pyrit/memory/_scenario_history_aggregation.py |
Adds Python unit resolution and history counter calculation. |
pyrit/analytics/scenario_statistics.py |
Extracts reusable parsing of persisted run-plan metadata. |
Review effort: Balanced
Comment on lines
+8784
to
+8785
| str(entry_id): ScenarioPlanLookup.from_plan( | ||
| plan=parse_scenario_run_plan(metadata=plan_metadata.get(entry_id), scenario_result_id=str(entry_id)) |
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
Scenario History becomes fast on SQL Server without a schema change. The history aggregates expanded each run plan's JSON with
OPENJSONinside the join. SQL Server can't estimate rows forOPENJSON, so it picked nested loops that re-parse the fullscenario_metadatafor every attempt × planned unit, which took tens of minutes for a handful of large runs.ScenarioPlanLookup.ScenarioPlanLookup. Previously the SQL picked the first. Scenarios don't produce such plans.Related: #4648 (limits slow reads) and #4650 (stores planned units as rows). Each stands on its own. This one and #4650 both change the history aggregation code, so whichever merges second will be rebased.