Skip to content

FIX: Deliver scenario progress results that commit behind the cursor - #4796

Open
Richard Lundeen (richlundeen) wants to merge 1 commit into
microsoft:mainfrom
richlundeen:fix-scenario-progress-late-commit
Open

Richard Lundeen (richlundeen) wants to merge 1 commit into
microsoft:mainfrom
richlundeen:fix-scenario-progress-late-commit

Conversation

@richlundeen

Copy link
Copy Markdown
Contributor

Description

Scenario run progress could permanently miss an attack result. AttackResult.timestamp is set when the result is created, but concurrent atomic attacks commit later. A result created earlier can therefore commit after the progress keyset cursor has already passed its (timestamp, id) position. The server's progress read model and the client's since cursor never fetched it again. As a result, a COMPLETED run could report fewer results than it stored. This was seen in CI as frontend/e2e/scenario-techniques.spec.ts returning 2 of 3 results after completion.

  • Memory: add get_scenario_attack_result_count_async(scenario_result_id, through=...). It counts the run's results at or before a cursor and uses the same ordering condition as the delta query.
  • Read model: after each refresh, compare the stored count through the cache cursor with the cached results. On a mismatch, rebuild the cache entry from the start.
  • Progress protocol: v2 progress cursors carry the number of results the client holds. The server answers a cursor that is behind, or a v1 cursor, with a reset page: results from the first item, the plan, and reset: true.
  • Frontend: useScenarioRunProgress replaces held results when a page has reset: true, including a terminal page, before it stops polling.
  • Document the cursor and reset semantics in pyrit/backend/README.md, and add regression tests: a SQLite late-commit test, cursor reset and decoding tests, a memory count test, and hook reset tests.

AttackResult.timestamp records creation, so a concurrent attack can commit a
row ordered before a progress keyset cursor that already passed it. The read
model and client cursors then skipped that result forever, so a completed run
could report fewer results than it stored.

- Memory: add get_scenario_attack_result_count_async, counting run rows at or
  before a keyset cursor with the same order as the delta query.
- Read model: after hydrating, compare the stored count through the cache
  cursor with the cached deltas and rebuild the entry on mismatch.
- Progress API: version 2 cursors record how many results the client holds
  through the bookmark. When storage holds more (or for version 1 cursors),
  the page restarts from the first result with the plan and reset: true.
- Frontend: a reset page replaces held results, including a terminal page.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 04:35

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

A critical cursor-validation flaw allows a timestamp edit to mask a late insertion, permanently skipping a row even after completion.

1 open issue
What changed in this PR

This PR addresses missing scenario results when concurrent attacks commit behind the progress cursor, coordinating storage checks, API resets, and frontend state replacement.

Changes:

  • Detect missed results and rebuild the progress cache.
  • Add versioned cursors with result counts and reset responses.
  • Handle resets in the frontend and add regression coverage.
File Description
tests/​unit/​memory/​memory_interface/​test_interface_scenario_progress.py Tests cursor-bounded result counts.
tests/​unit/​backend/​test_scenario_run_service.py Tests cursor validation, resets, and late commits.
tests/​unit/​backend/​test_scenario_progress_read_model.py Updates mocks for count checks.
tests/​unit/​backend/​mocks.py Adds a count-aware memory wrapper.
pyrit/​models/​scenario_progress.py Adds the reset response field.
pyrit/​memory/​memory_interface.py Adds counts using shared cursor ordering.
pyrit/​backend/​services/​scenario_run_service.py Implements v2 cursors and reset pages.
pyrit/​backend/​services/​scenario_progress_read_model.py Rebuilds caches when stored counts differ.
pyrit/​backend/​README.md Documents cursor and reset semantics.
frontend/​src/​types/​index.ts Types the reset response field.
frontend/​src/​hooks/​useScenarioRunProgress.tsx Replaces held results on reset.
frontend/​src/​hooks/​useScenarioRunProgress.test.tsx Tests terminal and empty reset pages.

Review effort: Balanced

# A result that committed late can sort before the client's bookmark. The client then
# holds fewer results through that bookmark than storage does, so it must start over.
# An edited result moves past the bookmark instead and is re-sent on this page.
reset = cursor is not None and (cursor.seen is None or start > cursor.seen)

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