Skip to content

feat(verify): add 3-layer surgical selector healer and read-back-gated fleet patch propagation - #2579

Open
abrichr wants to merge 2 commits into
mainfrom
gemini/surgical-selector-healer-fleet-patch
Open

abrichr wants to merge 2 commits into
mainfrom
gemini/surgical-selector-healer-fleet-patch

Conversation

@abrichr

@abrichr abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member

Implements 3-layer Compile -> Surgical 1-Step Heal -> Fleet Propagate in packages/verify/src/openadapt_verify/surgical_heal.py (zero changes to packages/types/, pure stdlib, preserves openadapt-verify's slim import boundary).

Summary

  • ElementTarget, SurgicalHealCandidate, SurgicalSelectorHealer:
    • Ranks visible, enabled candidate DOM/ARIA elements using deterministic semantic signals (ARIA role, accessible name/label similarity, form section proximity, input type match) plus an optional local visual-locator callback (fails closed on out-of-range/NaN scores or callback errors).
    • Enforces a fail-closed confidence margin gate (top_score >= min_score and top_score - second_score >= min_margin; otherwise returns ABSTAIN_HALT without invoking the read-back oracle).
  • Post-heal read-back gate & FleetSelectorPatch:
    • heal_and_verify() only promotes a healed locator into a content-addressed FleetSelectorPatch (schema_version = "openadapt.fleet-selector-patch/v1", readback_verified = True, canonical patch_sha256) after an independent read-back oracle confirms the healed step succeeded. Failed or raising read-back oracles return ABSTAIN_HALT with patch=None.
  • Atomic fleet propagation (apply_fleet_patch, apply_fleet_patch_to_packs):
    • Verifies readback_verified is True and constant-time patch_sha256 integrity, checks exact step_id, workflow_id, and old_selector match, and returns deep-copied updated packs without mutating inputs.

Verification

  • pytest packages/verify/tests/test_surgical_heal.py -v: 6 passed (deterministic ARIA/label heal + 32-agency fleet propagation, ambiguous tie ABSTAIN_HALT, below-threshold ABSTAIN_HALT, failed read-back withholding patch, visual-locator tie-breaker + NaN fail-closed, tampered patch_sha256 / unverified / selector-mismatch rejection).
  • ruff check & ruff format --check: clean.
  • mypy packages/verify/src/openadapt_verify/surgical_heal.py: Success: no issues found in 1 source file.
  • scripts/check_source_boundary.py: OK: no boundary violations.
  • gitleaks detect: no leaks found.
  • Added openadapt_verify.surgical_heal to packages/verify/tests/test_import_boundary.py.

Generality & Durability

  1. No per-app code in the engine: surgical_heal.py contains zero app/carrier/portal names or branches; all element semantics come from the compiled pack's ElementTarget and observed DOM/ARIA candidates.
  2. Fix the class, not the instance: Handles the general class of post-release selector drift with one deterministic, margin-gated, read-back-verified fleet propagation model.
  3. Held-out evidence: Not yet run on a live carrier portal; covered by synthetic unit tests over drifted selectors, ambiguous ties, read-back failures, and 32-pack fleet propagation.
  • Durability: Advances verifiable, reversible, accountable recovery (read-back-gated, content-addressed fleet patches that fail closed to ABSTAIN_HALT whenever confidence or read-back is missing).

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Claude
Reviewed-Head: 8fd5a45

I read the diff and ran the new module on its own with a stdlib-only probe script. I did not run the test suite. CI was still queued when I looked.

P0: the healer picks the wrong field when a label is renamed

The margin gate only compares candidates with each other. It never checks that the winner is the same field. I ran this case:

  • Target: textbox, name "Patient last name", section "Demographics", type text (old selector #last_name).
  • Drifted page: #fname "Patient first name" and #surname "Surname". Both are textboxes in the same section.

Result: HEALED on #fname, score 0.871 against 0.683, margin 0.188. That passes both default gates (0.75 and 0.15). The run would then write the last name into the first-name field. Role, section and input type match on almost every field in a form, so they make up 0.60 of the 0.85 active weight. Token overlap plus SequenceMatcher then gives near-miss labels ("first" against "last") a high name score. With one eligible candidate, second_score is 0.0, so the margin equals the score and a single wrong field heals without any ambiguity check.

The only later check is readback_oracle(candidate). It is a caller-supplied boolean, and it runs after the step has acted on the chosen element. Nothing re-runs identity or postcondition checks before the action. A healed locator has to pass the same identity check a compiled step passes before it acts. A changed accessible name has to be treated as a lost identity anchor, not as 0.68 similarity. A single candidate should not skip the ambiguity rule.

P0: patches are unsigned, so anyone can forge one

patch_sha256 is a plain SHA-256 over the patch's own fields, with no key. Anyone who can write a patch can recompute the hash. hmac.compare_digest on an unkeyed digest only checks that the patch is self-consistent. It does not show who made the patch. Probe: FleetSelectorPatch.create(..., new_selector="#evil", readback_verified=True) produced a patch that apply_fleet_patch accepted. The tamper test only covers a patch that was edited without recomputing the hash. readback_verified is a flag the creator sets for itself. from_dict also uses bool(data["readback_verified"]), so the JSON string "false" loads as True and still passes verify_integrity(). There is no signer, no key or trust root, and no signature-error path to fail closed on. Fleet distribution needs signed patches, for example a DSSE envelope like the one on Receipts. They should be verified against a pinned key, and a patch should be refused on any signature or parse error.

P0: one patch applies to every tenant, and the workflow check can be skipped

apply_fleet_patch_to_packs applies one patch to every pack in the mapping. It has no customer, tenant or app scope. In apply_fleet_patch, the workflow_id guard is skipped when a step has no workflow_id or has None. Probe: packs custA and custB, neither with a workflow id, each had a step s3 with selector #last_name. Both were rewritten to #evil. Step ids like s3 and generic selectors like #submit repeat across unrelated programs, so a heal seen at one customer rewrites another customer's program. The patch needs a scope bound into the signed payload: tenant, program id and hash, app or origin. Steps without a matching scope must be refused.

P1: patched programs skip qualification and admission

The output is a rewritten step list plus a healed_by_patch_sha256 marker, ready to run. Nothing re-qualifies the program, re-admits it, or holds it for approval, and its identity or hash does not change. The standing rule is that a changed program has to be qualified or approved again. A fleet patch should produce a candidate program that goes through qualification or approval before any tenant runs it.

Other findings

  • Generality: passes. The engine has no app names. The fixtures (nw-eff-date, policy quote) are synthetic.
  • Protected paths: none touched. The module has no callers (git grep finds only its own files), so nothing above is live yet. It is still a public __all__ API whose stated job is to propagate patches across a fleet.
  • Tests: all 6 are synthetic happy-path or same-label tie tests. None covers a near-miss label (first against last), a single candidate, a forged patch with a recomputed hash, the "false" string, a step without workflow_id, or cross-tenant packs. The test name says 30 agencies but the body uses 32. The PR does say no live run has been done. Please keep the "32-agency fleet propagation" wording out of anything that reads as a measured result.

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.

1 participant