Skip to content

FIX: Resolve scenario-history planned units in Python instead of SQL - #4649

Open
Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:fix/scenario-history-plan-resolution-in-python
Open

Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:fix/scenario-history-plan-resolution-in-python

Conversation

@behnam-o

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

Copy link
Copy Markdown
Collaborator

Description

Scenario History becomes fast on SQL Server without a schema change. The history aggregates expanded each run plan's JSON with OPENJSON inside the join. SQL Server can't estimate rows for OPENJSON, so it picked nested loops that re-parse the full scenario_metadata for every attempt × planned unit, which took tens of minutes for a handful of large runs.

  • One query per page: a single indexed query reads the attempt columns for the page. Each run plan is loaded and parsed once, and attempts are matched to planned units in Python with the existing ScenarioPlanLookup.
  • Same results: ranking, retry pressure, envelope handling, planned vs. unplanned units, and outcome counts follow the previous SQL rules, so API output is unchanged.
  • Less code: removes the per-dialect plan-expansion and role-matching SQL hooks (−752 / +579 lines).
  • Integration test: adds an Azure SQL test with production-shaped runs that checks the history page and the run hierarchy stay within a time budget.
  • One edge case changes: if a plan has two groups with the same name and technique hash, the last one now wins, matching 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.

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>

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 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

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