From 1bcac11284b40d6878582fac7ea5843a4d13e9cb Mon Sep 17 00:00:00 2001 From: abrichr Date: Tue, 24 Nov 2026 05:09:19 -0500 Subject: [PATCH 1/2] fix(verify): bind reload read-back to visible field values and cap declared producers The fresh-reload oracle took the compared value from the caller and only searched for it near a label, so -$500, 1,500 and 12345-B matched 500 and 12345, and text in display:none, hidden, aria-hidden and textarea elements counted as shown. A caller-made DOM with a declared separate_session producer also reached VERIFIED. - Read each field's value from the reloaded page: the visible value bound to its label (dt/dd, Label: value, label[for] to a visible control) or an explicit id/name locator. Caller values are cross-checked, never used. - Compare the whole normalized value; a sign before the currency symbol now normalizes. Repeated labels with different values are UNCERTAIN. - Track visibility with a tag stack: hidden, aria-hidden, inline display:none and visibility:hidden subtrees, hidden inputs, comments, script, style, template and unbound form-control text are not page text. - Decode UTF-8 strictly; a decode or parse error is UNCERTAIN. - Inline DOM bytes carry only a declared producer: cap at HALT/UNCERTAIN and record producer_source=declared, check_level=same_screen. A new summary_dom_path route grades the store with grade_artifact, as the PDF channel does, and is the only route to VERIFIED. - Verify facade count guard: 131 -> 147. --- .github/workflows/packages.yml | 2 +- .../src/openadapt_verify/portal_readback.py | 535 ++++++++++++------ packages/verify/tests/test_portal_readback.py | 241 +++++++- 3 files changed, 602 insertions(+), 176 deletions(-) diff --git a/.github/workflows/packages.yml b/.github/workflows/packages.yml index 524e72ab6c..cb9f36d138 100644 --- a/.github/workflows/packages.yml +++ b/.github/workflows/packages.yml @@ -475,7 +475,7 @@ jobs: python - <<'PY' import xml.etree.ElementTree as ET cases = ET.parse("tests.xml").getroot().findall(".//testcase") - assert len(cases) == 131, f"expected 131 independent-store, surgical-heal, and portal-readback controls, got {len(cases)}" + assert len(cases) == 147, f"expected 147 independent-store, surgical-heal, and portal-readback controls, got {len(cases)}" # The PostgreSQL and MySQL quickstart controls need the database # services of verify-clean-install, which runs them; nothing else skips. skipped = {c.get("name") for c in cases if c.find("skipped") is not None} diff --git a/packages/verify/src/openadapt_verify/portal_readback.py b/packages/verify/src/openadapt_verify/portal_readback.py index ec4a33bb0c..5991f0e689 100644 --- a/packages/verify/src/openadapt_verify/portal_readback.py +++ b/packages/verify/src/openadapt_verify/portal_readback.py @@ -8,8 +8,11 @@ Requires an independent producer (``separate_session`` or ``system_of_record``), valid same-origin ``submit_url`` / ``summary_url``, ISO-8601 ``reloaded_at_utc``, a 64-hex ``summary_dom_sha256`` digest, and distinct non-empty DOM tokens - proving a fresh navigation/reload distinct from the submit DOM, then diffs - extracted summary fields against the expected intake record. + proving a fresh navigation/reload distinct from the submit DOM. It then + reads each field from the reloaded page itself: the visible value bound to + the field's label (or locator), compared as a whole normalized value. A + repeated label with different values is UNCERTAIN. Inline DOM bytes with a + declared producer cannot reach VERIFIED; a DOM file in a graded store can. 2. ``ConfirmationPdfOracle``: Enforces artifact provenance via @@ -56,8 +59,10 @@ "€": "€", "£": "£", } +# Groups: a sign before the symbol, the symbol, then the amount (which may carry +# its own sign). ``-$500`` and ``$-500`` both normalize to ``$:-500.00``. _CURRENCY_RE = re.compile( - r"^\s*([$€£])?\s*([+-]?\d{1,3}(?:,\d{3})*(?:\.\d+)?|[+-]?\d+(?:\.\d+)?)\s*$" + r"^\s*([+-])?\s*([$€£])?\s*([+-]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?)\s*$" ) _DATE_FORMATS = ( "%Y-%m-%d", @@ -232,13 +237,20 @@ def normalize_field_value(value: Any, *, field_type: str | None = None) -> str: continue m_cur = _CURRENCY_RE.match(text) - if m_cur and ( - field_type == "currency" or m_cur.group(1) is not None or "," in m_cur.group(2) + lead_sign = m_cur.group(1) if m_cur else None + if ( + m_cur + and not (lead_sign and m_cur.group(3)[0] in "+-") + and ( + field_type == "currency" + or m_cur.group(2) is not None + or "," in m_cur.group(3) + ) ): - raw_num = m_cur.group(2).replace(",", "") + raw_num = (lead_sign or "") + m_cur.group(3).replace(",", "") try: dec = Decimal(raw_num).quantize(Decimal("0.01")) - sym = m_cur.group(1) + sym = m_cur.group(2) if sym is not None: code = _CURRENCY_SYMBOL_TO_CODE.get(sym, sym) return f"{code}:{dec}" @@ -367,31 +379,145 @@ def _compare_fields( return tuple(checks), confirmed, refuted, abstained -class _VisibleTextExtractor(HTMLParser): - """Extract ordered visible text nodes from HTML, ignoring attributes and non-rendered elements.""" +_VOID_TAGS = frozenset( + { + "area", + "base", + "br", + "col", + "embed", + "hr", + "img", + "input", + "link", + "meta", + "param", + "source", + "track", + "wbr", + } +) +# Elements whose text is never rendered page text. +_NEVER_RENDERED_TAGS = frozenset( + {"script", "style", "noscript", "template", "head", "title"} +) +# Form controls: their text is editable state, not a persisted summary. It counts +# only when the control is the field being checked (see ``_bound_values``). +_FORM_CONTROL_TEXT_TAGS = frozenset({"textarea", "select", "option", "datalist"}) +_HIDDEN_STYLE_DECLS = frozenset( + {"display:none", "visibility:hidden", "visibility:collapse"} +) +# Input types whose ``value`` is a field value a person reads on the page. +_VALUE_INPUT_TYPES = frozenset( + {"", "text", "number", "email", "tel", "url", "search", "date", "month", "week"} +) + - _SKIP_TAGS = frozenset({"script", "style", "noscript", "template", "head", "title"}) +def _attrs_hide_element(tag: str, attrs: Mapping[str, str | None]) -> bool: + """Whether inline markup alone hides this element and its subtree.""" + if "hidden" in attrs: + return True + if (attrs.get("aria-hidden") or "").strip().lower() == "true": + return True + if tag == "input" and (attrs.get("type") or "").strip().lower() == "hidden": + return True + style = re.sub(r"\s+", "", (attrs.get("style") or "").lower()) + for decl in style.split(";"): + if decl.replace("!important", "") in _HIDDEN_STYLE_DECLS: + return True + return False + + +class _VisibleDomExtractor(HTMLParser): + """Collect what a reloaded page shows, using inline markup only. + + - ``nodes``: visible text nodes in document order. Comments, attributes, + ``script``/``style``/``template``/``head``, form-control text, and subtrees + hidden by ``hidden``, ``aria-hidden="true"`` or an inline ``display:none`` / + ``visibility:hidden`` style are left out. + - ``controls_by_id`` / ``controls_by_name``: values of visible, text-like + ``input`` and ``textarea`` controls. They are read only when a check binds + a field to that control by ``