Skip to content

refactor(deep-scan)!: reuse finding matching and save merge progress - #1158

Merged
mldangelo-oai merged 7 commits into
mdangelo/codex/pr939-split-31-saved-draftsfrom
mdangelo/codex/pr939-split-35-saved-merge-results
Oct 4, 2026
Merged

mldangelo-oai merged 7 commits into
mdangelo/codex/pr939-split-31-saved-draftsfrom
mdangelo/codex/pr939-split-35-saved-merge-results

Conversation

@mldangelo-oai

@mldangelo-oai mldangelo-oai commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Part 35 of 46. Previous: #1157 · Next: #1159 · Stack index

Summary

Let Deep Scans reuse the finding matcher already used by scan history and component scans, and save enough progress to resume completed work. Original findings and accepted combined results remain available for recovery and reports.

Changes

  • Compare completed child scans in order with the shared matcher. Only confirmed matches combine groups; uncertain or related comparisons remain separate. First reports and compatible empty batches need no model call.
  • Retain accepted groups and identities, distinct repairs and locations, complete source evidence, and the highest source severity. Build the report from those groups without an extra object repeating their IDs.
  • Store original findings and accepted revisions separately and reference them from version-3 checkpoints. Save source artifacts before the checkpoint pointer through the existing completion lock and current ownership/running-state checks.
  • Retry invalid matching responses in the same conversation and persist the retry count. Preserve accepted results and retry publication without repeating completed child work or accepted matching.
  • Record parent model work only when a call starts, and retain the final cost receipt. A merge needing no parent model call does not require a receipt for a session that never ran.
  • Update Python checkpoint readers, pending composition recovery, artifact restoration and merge evaluation fixtures.
  • Report all known recovered child costs before a missing-receipt error stops continuation, so known spending is retained even when the total cost is unavailable.

Testing

Local checks for the implementation:

  • Local checkpoint regressions passed for interrupted checkpoint publication and writes after external cancellation.

Both recovered-cost regressions failed before the correction; 78 cost, composition and lifecycle cases passed afterward.

Checks against the complete stack passed: SDK and MCP typechecks, formatting, Ruff, the SDK build, portable plugin compatibility and its tests, focused recovery/accounting tests, and local-artifact package smoke using exact cached dependencies. The package check was not a clean registry install. See this PR’s Checks tab for CI results for its current commit.

Risk and rollout

Live recovery requires version-3 checkpoints. Python can still read version-2 metadata without loading its combined findings, but unfinished older Deep Scans require their original version or a new scan.

Matching compares findings between child scans. Duplicates found only within one child remain that Standard scan's responsibility. Comparisons run in order within a batch and may request additional evidence, so the number of model calls can differ from the previous single grouping request.

The merge evaluation runner keeps successful result records compatible. Failed merges retain raw turns and errors without returning an unaccepted combined decision.

The SDK entrypoint adopts this runner in part 39; the plugin switches in part 40.

Public disclosure review

  • No customer, partner, prospect, or user identities, data, or identifying details are included.
  • No credentials, personal data, private source, scan findings, or nonpublic links or tickets are included.
  • I reviewed the branch name, title, description, commits, changes, comments, logs, screenshots, attachments, and links for public disclosure.

@mldangelo-oai

Copy link
Copy Markdown
Collaborator Author

@codex review

@mldangelo-oai
mldangelo-oai requested a balanced review from Copilot October 1, 2026 06:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-04T18:07:42.719938Z 9bb1b06 Draft marked ready
🔒 Security Review ⚠️ Failed 2026-10-04T18:07:46.631296Z 9bb1b06 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Checkpoint writes can race terminal operations, external cancellation is no longer observed, and recovered child coverage may be omitted.

Review effort: Balanced
Findings: 2 High severity · 1 Medium severity

Open (3)
What changed in this PR

Introduces version-3 Deep Scan checkpoints and resumable, content-addressed merge state.

Changes:

  • Separates aggregates, source evidence, and revisions into restorable artifacts.
  • Reworks merge reconciliation, recovery, publication, and cost accounting.
  • Expands TypeScript and Python recovery tests and merge evaluation.
File Description
sdk/​typescript/​tests-ts/​support/​api-client.ts Adds batched restore mock.
sdk/​typescript/​tests-ts/​scan-projection-fixtures.test.ts Updates merge fixture validation.
sdk/​typescript/​tests-ts/​scan-merge.test.ts Tests reconciliation and persistence.
sdk/​typescript/​tests-ts/​scan-merge-reconciliation.test.ts Tests schema and evidence isolation.
sdk/​typescript/​tests-ts/​runtime.test.ts Tests safe batched restoration.
sdk/​typescript/​tests-ts/​merge-eval.test.ts Updates merge quality tests.
sdk/​typescript/​tests-ts/​deep-scan-composition.test.ts Reworks scheduler recovery coverage.
sdk/​typescript/​tests-ts/​deep-scan-checkpoint.test.ts Tests version-3 metadata.
sdk/​typescript/​src/​scan-merge.ts Implements aggregate reconciliation and storage.
sdk/​typescript/​src/​runtime.ts Adds batched artifact restoration.
sdk/​typescript/​src/​deep-scan.ts Refactors scheduling and checkpoint recovery.
sdk/​typescript/​src/​deep-scan-lifecycle.ts Simplifies pass lifecycle state.
sdk/​typescript/​src/​deep-scan-checkpoint.ts Defines version-3 checkpoints.
sdk/​typescript/​scripts/​merge-eval/​run.ts Updates merge evaluation runner.
sdk/​typescript/​scripts/​merge-eval/​README.md Documents revised evaluation.
sdk/​typescript/​scripts/​merge-eval/​grade.ts Grades materialized findings.
sdk/​typescript/​scripts/​merge-eval/​fixtures.ts Revises merge fixtures.
plugins/​codex-security/​tests/​workbench_test_support.py Adds version-3 helpers.
plugins/​codex-security/​tests/​test_workbench_scan_composition.py Tests metadata-only loading.
plugins/​codex-security/​tests/​test_workbench_current_draft.py Tests source hydration.
plugins/​codex-security/​tests/​test_scan_projection.py Updates coverage helper usage.
plugins/​codex-security/​scripts/​workbench_scan_history.py Adds composition completion validation.
plugins/​codex-security/​scripts/​workbench_saved_results.py Materializes and recovers aggregates.
plugins/​codex-security/​scripts/​workbench_composition.py Loads version-3 composition state.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1193 to +1195
write_scan_local_bytes(
scan_dir, f"checkpoints/{hashlib.sha256(payload).hexdigest()}.json", payload
)
Comment thread sdk/typescript/src/deep-scan.ts Outdated
Comment on lines +182 to +186
await input.writer.restoreMany([
...revisions,
...(contents === undefined || aggregatePath === savedAggregatePath
? []
: [{ path: aggregatePath!, contents: Buffer.from(contents) }]),

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.

Reproduced against the actual SDK/workbench fixture at stack tip 835f0d1. With one worker, I canceled the parent through a separate cancel-scan command just after the first child's complete-scan, leaving the caller signal un-aborted. After cancellation returned, the coordinator changed checkpoint.json from mergedScanIds: [], aggregatePath: null to a checkpoint containing the child ID and a new aggregate path. Publication then failed because the parent was canceled. The raw restoreMany write therefore really does modify the stopped parent after its stop operation completes. This probe does not claim that another child launched; it isolates the checkpoint-write violation.

Comment on lines 329 to 333
const executionSignal = AbortSignal.any([signal, externalStop.signal]);
const discoverySignal = AbortSignal.any([
executionSignal,
deadlineController.signal,
]);

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3eaca35c4b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +200 to +204
await workbench([
"update-progress",
"--scan-id",
scanId,
"--phase",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restore continuous parent-status polling

When another process cancels or fails the parent scan, input.signal is not aborted, and this one-shot update-progress call is the only remaining parent-status interaction—its failure is ignored. Since checkpoint saves now use the filesystem writer directly and register_cli_scan does not require a Deep Scan parent to still be running, a resumed or active coordinator can continue launching children and modifying artifacts after the parent has stopped or been sealed. Preserve the previous ongoing status check, or otherwise abort when the persisted parent stops.

AGENTS.md reference: sdk/typescript/AGENTS.md:L23-L25

Useful? React with 👍 / 👎.

Comment thread sdk/typescript/src/scan-merge.ts Outdated
Comment on lines +191 to +192
const representative =
accepted[0] ?? presentations.get(group.representativeId)!;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Use the selected representative for revised findings

When a later pass supplies a corrected duplicate of an already accepted finding and the merge decision selects that new source as representativeId, this expression always chooses the old accepted presentation instead. presentGroup only unions summaries, repairs, locations, tests, controls, and severity, so corrected fields such as confidence, taxonomy, and attack-path details remain stale even though the model explicitly selected the newer representative. Base the revised presentation on the selected representative while copying only the established identity fields that must remain stable.

Useful? React with 👍 / 👎.

Comment on lines +462 to +464
onCost: (cost) => {
input.onCost(pass.directory, cost);
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Persist the final cost when a resumed child fails

When an already registered child is resumed and then fails, ScanCostTracker.stop() reports its final snapshot through this callback, but the value is now forwarded without being retained. The API intentionally skips its own fail-scan persistence when resumeScanId is set, while runPass subsequently invokes fail-scan without --cost-json and rereads a null receipt. Consequently a required-cost Deep Scan stops with ScanCostTrackingError despite having measured the cost, and an interrupted optional-cost run cannot recover that usage. Retain the final callback value and persist it at the resumed-child failure boundary.

AGENTS.md reference: sdk/typescript/AGENTS.md:L25-L25

Useful? React with 👍 / 👎.

@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch from 3eaca35 to c44918f Compare October 1, 2026 07:25
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-34-draft-recovery branch from 5b80d58 to 7a8a356 Compare October 1, 2026 07:25
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch from c44918f to d7ded9c Compare October 1, 2026 07:39
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-34-draft-recovery branch from 7a8a356 to 272b793 Compare October 1, 2026 07:39
@mldangelo-oai mldangelo-oai changed the title refactor(deep-scan)!: prepare checkpoint recovery for scan batches refactor(deep-scan)!: save child results and merge progress Oct 1, 2026
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch from d7ded9c to 1ef7e65 Compare October 1, 2026 17:10
@mldangelo-oai mldangelo-oai changed the title refactor(deep-scan)!: save child results and merge progress refactor(deep-scan)!: reuse finding matching and save merge progress Oct 1, 2026

@faizan-oai faizan-oai 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.

Holding approval for writes after external cancellation. In an actual SDK/workbench fixture, I canceled the parent through a separate workbench command after its first child completed, without aborting the caller signal. The coordinator then rewrote the stopped parent's checkpoint, adding the child to mergedScanIds and setting a new aggregatePath, before publication rejected the canceled parent. The persisted stop is therefore not respected at the checkpoint write boundary. Details added to the existing thread. Separately, the new matching/comparison/composition tests pass in the integrated stack.

@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch 2 times, most recently from 5477f91 to 253cbca Compare October 1, 2026 20:29

@alandelong-oai alandelong-oai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the updated head 253cbca02d7b1ac8347983c6c2ed497451d7d171 with all three independent reviewers and root verification. Approval is withheld for these remaining issues:

  • Checkpoint writes bypass continuation and stop checks, and saved-parent cancellation no longer reaches workers. Using the real workbench and SDK artifact writer, I canceled a parent after its child completed. After cancellation returned, the coordinator changed the checkpoint from no merged children/no aggregate to an accepted child and new aggregate path, then attempted publication. The updated head reproduces this.
  • Composed recovery must retain its pending marker. A canceled parent with an existing draft retains an unmerged child finding but loses its surface, deferred review, and unfinished-pass explanation. Restoring the prior pending-checkpoint write restores that coverage.
  • Resumed-child final cost is no longer persisted. A resumed scheduler probe receives the cost callback but sends fail-scan without cost-json, then replaces the receipt with null and fails required accounting. The API skips its own failure persistence for resumed scans; this also needs verification through the later activation PR.

The earlier selected-representative comment is superseded by this update removing that argument. Several earlier retry/publication bugs are addressed and will be reconciled against the final stack head. Initial validation passed 366 SDK tests (25 platform skips), 81 Python tests, the plugin build, and SDK build:ci. I reran the six affected SDK suites on the updated head and repeated the cancellation and cost probes. No repository files were changed.

@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch from 253cbca to c7800a5 Compare October 1, 2026 23:44
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-34-draft-recovery branch from 272b793 to a43db59 Compare October 1, 2026 23:44

@alandelong-oai alandelong-oai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Three independent restack passes and root reconciliation are complete.

Durable cancellation now prevents checkpoint persistence, and pending child coverage is retained at this own head. The resumed-child cost issue remains: the scheduler forwards the callback, sends fail-scan without that receipt, then rereads saved cost, while this head's child API omits failure-cost persistence for resumed runs. The later #1162 child API fixes it. Approval remains withheld for this existing P2.

Approval remains withheld. This review distinguishes the PR's own head from fixes verified later in the stack; it does not duplicate existing inline findings.

@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-34-draft-recovery branch from a43db59 to ea53452 Compare October 2, 2026 00:36
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch from c7800a5 to 837c840 Compare October 2, 2026 00:36

@alandelong-oai alandelong-oai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed the real PR diff at 837c84071b6b254786160eb7ab92bd43c113e79d. Three independent high-reasoning passes covered the complete changed patch and tests; root source checks and focused reproductions informed this decision.

The existing resumed-child cost persistence finding remains at this own head and is repaired later in #1162. A separate recovery regression is now verified: with requireCost, an unavailable receipt prevents subsequent valid recovered receipts from reaching onCost. Exact base/head/tip probes report the known receipt at the base and omit it at this head and the integrated tip; see the new inline finding.

Validation across touched paths at branch tip 06e75cfe: 1,356 SDK tests, 867 Python tests plus 100 subtests, and 108 MCP tests passed; builds, types, portable checks and installed-package checks passed. These are integration checks at the branch tip, not a claim that every intermediate PR or its CI is clean.

Comment thread sdk/typescript/src/deep-scan.ts Outdated
await save();
reportPassCost("previous-work", null);
}
for (const [directory, cost] of costs) reportPassCost(directory, cost);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Report available receipts before rejecting missing costs

With requireCost, reportPassCost throws on the first null receipt. hydratePasses first registers all children as unknown, so if a missing receipt precedes a valid one (or state.costUnavailable is already true), the valid recovered receipt never reaches input.onCost. An exact base/head probe with two saved children reports the known receipt at the base but reports none at this head; the same omission remains at the integrated tip. Report the non-null receipts before raising the unavailable-cost error so the parent can retain and report the known spending even though full cost coverage is unavailable.

@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch 2 times, most recently from ea3b8ae to eef4c96 Compare October 2, 2026 01:56
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-34-draft-recovery branch from 3eae769 to ee01c31 Compare October 2, 2026 01:56

@alandelong-oai alandelong-oai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Retained the three independent passes for the identical patch and reconciled inherited touched-file changes at eef4c96f0585.

The new recovered-receipt ordering issue is fixed: both own-head and tip probes report the known second receipt before raising the required-cost error, including when costUnavailable was already set. The earlier resumed-child persistence issue remains at this own head: a resumed child reports its final cost and then fails, but fail-scan omits --cost-json and the scheduler replaces the receipt with null. That repair still lands later in #1162.

Current tip 355ec321 passed 75 report-projection tests, portable source checks, SDK build:ci and plugin build. The immediately preceding tip fec36f1d passed 1,360 SDK tests, 873 Python tests plus 100 subtests, 109 MCP tests, builds/types and the clean 600-entry installed-package check. Only report_projection.py and its test changed between those tips; SDK/MCP runtime files are byte-identical. Both descend from main 008a8b4d. These are integration checks, separate from own-head probes. No native Windows local run.

Approval remains withheld for the findings above.

@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch from eef4c96 to a7392c9 Compare October 2, 2026 02:47
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-34-draft-recovery branch from ee01c31 to 71b58db Compare October 2, 2026 02:47

@alandelong-oai alandelong-oai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Retained the three independent passes for the verified identical patch and reconciled inherited touched-file changes at a7392c95efcb.

Recovered receipt ordering remains fixed. The own-head resumed-child failure path still reports final cost and then omits --cost-json from fail-scan, allowing the scheduler to replace the receipt with null. Production touched files remain byte-identical to the previously verified head; the only inherited touched-file change extends the report grouping test. This repair still lands later in #1162.

Integrated tip 6ef8cbb8, based on 008a8b4d, passed 1,388 SDK tests, 879 Python tests plus 100 subtests, 109 MCP tests, portable checks, builds and the clean 600-entry installed-package check. These are branch-tip integration checks, separate from own-head probes; no local Windows execution was performed.

Approval remains withheld for the findings above.

@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-34-draft-recovery branch from 71b58db to 270c67a Compare October 2, 2026 03:52
@mldangelo-oai
mldangelo-oai force-pushed the mdangelo/codex/pr939-split-35-saved-merge-results branch from a7392c9 to 2be15f7 Compare October 2, 2026 03:52

@alandelong-oai alandelong-oai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Retained three prior independent passes for the verified identical patch and reconciled inherited touched-file changes at 2be15f70a7a3.

Recovered receipt ordering remains fixed. The own-head resumed-child failure path still reports final cost and omits --cost-json from fail-scan, allowing the scheduler to replace the receipt with null. The new inherited changes to touched files concern sealed-path normalization and the current authentication environment; they do not repair this failure path. The receipt persistence fix remains later in #1162.

Branch tip 3e0083d9, based on 008a8b4d, passed 1,392 SDK tests, 881 Python tests plus 100 subtests, 109 MCP tests, builds, portable source checks and the 600-entry installed-package check. These are tip checks, separate from own-head probes. No local Windows run was performed. The tip excludes the dependency update merged separately in #1184; combined validation remains outstanding.

Approval remains withheld for the outstanding findings.

Consolidate approved Deep Scan changes into the parent topic branch.
@mldangelo-oai
mldangelo-oai changed the base branch from mdangelo/codex/pr939-split-34-draft-recovery to mdangelo/codex/pr939-split-31-saved-drafts October 4, 2026 17:33
mldangelo-oai and others added 2 commits October 4, 2026 18:04
Consolidate Deep Scan changes and their reviewed follow-up fixes into the parent topic branch.
Preserve the child source tree while recording its consolidated parent.
@mldangelo-oai
mldangelo-oai marked this pull request as ready for review October 4, 2026 18:07
@mldangelo-oai
mldangelo-oai merged commit 47f1628 into mdangelo/codex/pr939-split-31-saved-drafts Oct 4, 2026
@mldangelo-oai
mldangelo-oai deleted the mdangelo/codex/pr939-split-35-saved-merge-results branch October 4, 2026 18:07
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.

5 participants