Skip to content

fix(462): give every threshold verdict one reason text - #696

Merged
sromoam merged 3 commits into
awslabs:devfrom
WilliamK112:fix/462-one-threshold-reason
Dec 4, 2026
Merged

sromoam merged 3 commits into
awslabs:devfrom
WilliamK112:fix/462-one-threshold-reason

Conversation

@WilliamK112

@WilliamK112 WilliamK112 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Issue #, if available: Fixes #462

Description of changes:

Every field_comparisons row now gets its reason from one helper, so the same verdict reads the same everywhere and a score inside the 1e-10 allowance shows its real value.

  • threshold_helper.py: new ThresholdHelper.match_reason(is_match, score, threshold). A score that meets the threshold only through the allowance reads within threshold tolerance (<score> vs <threshold>), with the score in repr (0.7999999999999999, not 0.8; a NumPy float reads as a plain float). Otherwise it gives exact match, above threshold (...) or below threshold (...), with the score to three decimals unless that would round it across the threshold, in which case it is shown with repr too (0.7996 < 0.8, not 0.800 < 0.8).
  • comparison_helper_base.py (list items) and field_comparison_collector.py (top-level and nested-model fields) both call it. Before, the collector said above threshold (0.800 >= 0.8) for the same score that list items called within threshold tolerance (0.8 vs 0.8). Their unused math imports are dropped. non_match_collector.py uses it for top-level non_matches reasons as well.
  • Item 2's test: tests/test_list_reporting.py built its expected string with the code's own .12g format. It now asserts the literal text.
  • Item 3, stale descriptions of the old exact >= test: the recursion-gate snippet in architecture.md now shows the current zip(matched_pairs, verdicts) filter and names the methods instead of a line range; the binary_compare comment in Comparators.md; and the comments in structured_list_comparator.py and metrics_helper.py.
  • New tests/structured_object_evaluator/test_threshold_reason.py: a 0.7 + 0.1 score against 0.8 on a top-level field, a nested-model field and a list item all give the same reason, plus a table test for match_reason.
  • CHANGELOG.md: a ### Fixed bullet under [Unreleased].

Not done here: item 4 (one named constant for 1e-10, and moving the check to stickler/utils) is optional in the issue, so I left it for a separate change. #693 (for #509) edits the same lines in field_comparison_collector.py and says it leaves #462 to a separate PR, so whichever lands second needs a small rebase. I'm happy to do it.

Checks, on dev at 33ed6c3:

  • All 4 consistency cases (top-level, nested-model, list item, list-of-models) in the new test fail on dev's code (two read above threshold (0.800 >= 0.8), the list item reads (0.8 vs 0.8)) and pass here.
  • Full uv run pytest -q: 7,118 passed, 14 skipped, 1 xfailed. The 9 failures are the same as on dev on this Windows machine (deep-nesting timeouts and one gallery path test).
  • ruff check src tests is clean. The new test file passes ruff format --check; the two edited modules already had format differences on dev, and I didn't reformat them. docs/check_links.py passes.

Written with Claude Code.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

🤖 Generated with Claude Code

field_comparisons rows now take their reason from ThresholdHelper.match_reason,
so a top-level or nested-model field inside the 1e-10 allowance reads
"within threshold tolerance" like a list item does, and the score is shown
with repr (0.7999999999999999, not 0.8). Stale descriptions of the old
exact >= test are updated in architecture.md, Comparators.md and three
comments.

Closes awslabs#462

Signed-off-by: Ching Wei Kang <164879897+WilliamK112@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@mangeshraut712 mangeshraut712 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.

Verified the unification and the tolerance text by running the helper across the boundary, then the full suite.

Results on this head (b7d965e)

Check Result
test_threshold_reason.py + test_list_reporting.py 54 / 54 pass
Same tests against main's threshold_helper.py 10 fail (the new assertions) — clean fail → pass
Full suite (tests/, integration excluded) 7128 passed, 13 skipped, 1 xfailed, 0 failed
Call sites routed through match_reason 2 (comparison_helper_base.py:100, field_comparison_collector.py:164); no inline above/below threshold format strings remain in the touched files; unused math imports dropped

Boundary sweep (threshold 0.8)

The helper's score < threshold test is exactly consistent with is_above_threshold's allowance (score >= threshold or abs(score - threshold) < 1e-10), so "within tolerance" fires precisely when the match came from the allowance:

Score is_above_threshold Reason
0.7999999999999999 (0.7 + 0.1) match within threshold tolerance (0.7999999999999999 vs 0.8)
0.79999999995 (inside 1e-10) match within threshold tolerance (0.79999999995 vs 0.8)
0.8 match above threshold (0.800 >= 0.8)
0.7999999998 (outside 1e-10) no match below threshold (0.800 < 0.8) ← see below
1.0 / 0.999999999999 match exact match

NumPy float64(0.7) + float64(0.1) renders as a plain float in the text (no np.float64(...)). 8/8 consistency cases pass.

One gap the same fix could close on the "below" branch

The PR fixes the hidden-precision problem for the tolerance branch with repr, but the below threshold branch still formats with .3f. That produces a self-contradicting message for any non-match within < 5e-4 of the threshold:

below threshold (0.800 < 0.8)      # score 0.7999999998, 0.7999999, 0.79999, 0.7996 ...
below threshold (0.500 < 0.5)
below threshold (0.123 < 0.123)

I counted 30 such renderings sweeping 5 thresholds × 6 sub-threshold offsets. A user reading 0.800 < 0.8 can't tell whether they missed the threshold by 2e-10 or 4e-4, which is the same confusion the tolerance branch now avoids. The above direction doesn't have this problem (rounding a value at-or-above the threshold can't print below it — 0 cases), so the asymmetry is only on the "below" side. Suggested tweak, mirroring the tolerance branch:

shown = repr(float(score)) if abs(score - threshold) < 5e-4 else f"{score:.3f}"
return f"below threshold ({shown} < {threshold})"

(or simply use repr(float(score)) whenever .3f would round across the threshold). Not blocking — the PR does what it says — but since the goal is "the same verdict reads the same everywhere", it'd be nice for the "below" text to never state something numerically false.

Nits

  • above threshold (0.800 >= 0.8) at an exactly-at score is fine, but note it's the only branch where an exact hit reads "above". If exact match is meant for 1.0 only (as the docstring implies), that's consistent; just flagging the wording.
  • CHANGELOG entry references #462; good.

@sromoam sromoam 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.

Thanks @WilliamK112, this is a tidy fix and the description made it easy to check. On b7d965e I reproduced the mismatch on dev and confirmed it's gone here: top-level, Optional and nested-model fields, List[model], Optional[List[...]] and a list inside a list item all read within threshold tolerance (0.7999999999999999 vs 0.8) now. The new consistency tests fail on dev and pass here, and the full suite passes locally.

Must fix

  1. The above and below texts can still print an inequality that is false (threshold_helper.py:229-230). Agree with mangeshraut712 on the below branch. The above branch has the same problem when the threshold has more than three decimals: match_reason(True, 0.8004, 0.8004) gives above threshold (0.800 >= 0.8004). The new table test also pins one of these: below threshold (0.800 < 0.8) for 0.8 - 1e-9 (test_threshold_reason.py:67). Since #462 asks for a reason that shows the real score, please apply the rule field_comparison_helper.py:157-160 already uses: keep .3f unless it rounds across the threshold, then use repr. Suggestions inline. non_match_collector.py:179 builds the same below text on its own, so please have it call ThresholdHelper.match_reason(False, similarity, info.threshold) too. Otherwise a top-level non_matches entry keeps 0.800 < 0.8 while a list item's reads 0.7996 < 0.8. I tried both changes together: the only test that changes is line 67.

Smaller

  1. test_list_reporting.py:507 still passes on dev. repr(0.7 - 1e-12) and its .12g form are both 0.699999999999, so the literal can't catch the old format (#462 item 2). Adding the list-of-models shape to the new consistency test covers the same structured-child path: it fails on dev with (0.8 vs 0.8) and passes here. Suggestions inline.
  2. Twins of the comments you rewrote still describe the gate as a bare >=. comparison_helper.py:561-562 (tp = 0 # True positives (score >= threshold)) matches the structured_list_comparator.py:158 comment you rewrote, and is_above_threshold decides it a few lines below. The Returns blocks at comparison_helper.py:425-426 and :551-552 and structured_model.py:1784-1785 say the same. #462's done-when covers comments, so they're worth the same pass.
  3. One more row changes text that the CHANGELOG doesn't mention. With a threshold of 1.0, a top-level or nested field scoring 0.9999999999999999 read exact match on dev and now reads within threshold tolerance (0.9999999999999999 vs 1.0), as list items already did. Suggestion inline, which also covers must-fix 1.

One heads-up: we're not merging anything outside the 1.1.0 milestone until 1.1.0 lands, so this will wait until after that even once it's ready. It also conflicts with dev in CHANGELOG.md now. No need to resolve that yet: bring dev in after 1.1.0 lands.

Comment on lines +224 to +230
if is_match:
if score < threshold:
return f"within threshold tolerance ({float(score)!r} vs {threshold})"
if math.isclose(score, 1.0):
return "exact match"
return f"above threshold ({score:.3f} >= {threshold})"
return f"below threshold ({score:.3f} < {threshold})"

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.

Measured with this in place: 0.7996 vs 0.8 reads below threshold (0.7996 < 0.8), 0.8004 vs 0.8004 reads above threshold (0.8004 >= 0.8004), and the usual rows (0.900 >= 0.8, 0.500 < 0.8, exact match, the tolerance text) don't change. The docstring above could say the same in one sentence.

Suggested change
if is_match:
if score < threshold:
return f"within threshold tolerance ({float(score)!r} vs {threshold})"
if math.isclose(score, 1.0):
return "exact match"
return f"above threshold ({score:.3f} >= {threshold})"
return f"below threshold ({score:.3f} < {threshold})"
if is_match:
if score < threshold:
return f"within threshold tolerance ({float(score)!r} vs {threshold})"
if math.isclose(score, 1.0):
return "exact match"
shown = f"{score:.3f}"
# Never round a score across the threshold it was tested against.
if (float(shown) >= threshold) != is_match:
shown = repr(float(score))
if is_match:
return f"above threshold ({shown} >= {threshold})"
return f"below threshold ({shown} < {threshold})"

(True, 0.9, "above threshold (0.900 >= 0.8)"),
(True, 0.8, "above threshold (0.800 >= 0.8)"),
(True, NEAR, TOLERANCE_REASON),
(False, 0.8 - 1e-9, "below threshold (0.800 < 0.8)"),

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.

With the change above, this row shows the real score. The added row is the common case: a score that three decimals round up to the threshold.

Suggested change
(False, 0.8 - 1e-9, "below threshold (0.800 < 0.8)"),
(False, 0.8 - 1e-9, "below threshold (0.7999999990000001 < 0.8)"),
(False, 0.7996, "below threshold (0.7996 < 0.8)"),

Comment on lines +31 to +32
class Listed(StructuredModel):
names: list[str] = ComparableField(comparator=NearComparator(), threshold=0.8)

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.

A model for the list-of-models case below.

Suggested change
class Listed(StructuredModel):
names: list[str] = ComparableField(comparator=NearComparator(), threshold=0.8)
class Listed(StructuredModel):
names: list[str] = ComparableField(comparator=NearComparator(), threshold=0.8)
class Items(StructuredModel):
items: list[Flat]

Outer(inner=Flat(name="b")),
id="nested-model field",
),
pytest.param(Listed(names=["a"]), Listed(names=["b"]), id="list item"),

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.

This covers the items[0].name child row, the path test_list_reporting.py:507 was meant to guard. It fails on dev with within threshold tolerance (0.8 vs 0.8) and passes here.

Suggested change
pytest.param(Listed(names=["a"]), Listed(names=["b"]), id="list item"),
pytest.param(Listed(names=["a"]), Listed(names=["b"]), id="list item"),
pytest.param(
Items(items=[Flat(name="a")]),
Items(items=[Flat(name="b")]),
id="list-of-models field",
),

Comment thread CHANGELOG.md Outdated
Comment on lines +382 to +387
- A `field_comparisons` row that meets its threshold only through the `1e-10`
allowance now reads `within threshold tolerance (<score> vs <threshold>)` for a
top-level or nested-model field too, where it read `above threshold`, and the
score is shown in full (`0.7999999999999999`, not `0.8`). Every row with the
same verdict gets its reason from `ThresholdHelper.match_reason`
([#462](https://github.com/awslabs/stickler/issues/462)).

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.

Names the threshold-1.0 case, and the rounding rule from must-fix 1:

Suggested change
- A `field_comparisons` row that meets its threshold only through the `1e-10`
allowance now reads `within threshold tolerance (<score> vs <threshold>)` for a
top-level or nested-model field too, where it read `above threshold`, and the
score is shown in full (`0.7999999999999999`, not `0.8`). Every row with the
same verdict gets its reason from `ThresholdHelper.match_reason`
([#462](https://github.com/awslabs/stickler/issues/462)).
- A `field_comparisons` row that meets its threshold only through the `1e-10`
allowance now reads `within threshold tolerance (<score> vs <threshold>)` for a
top-level or nested-model field too, where it read `above threshold` (or
`exact match` at a threshold of `1.0`), and the score is shown in full
(`0.7999999999999999`, not `0.8`). An `above threshold` or `below threshold`
reason also shows the full score where three decimals would round it across
the threshold (`0.7996 < 0.8`, not `0.800 < 0.8`). Every row with the same
verdict gets its reason from `ThresholdHelper.match_reason`
([#462](https://github.com/awslabs/stickler/issues/462)).

Keep three decimals unless they would round the score across the
threshold it was tested against, then show it with repr (the rule
field_comparison_helper already uses). Top-level non_matches reasons now
come from ThresholdHelper.match_reason too. Adds the list-of-models case
to the consistency test, rewrites the remaining bare ">=" gate comments,
and names the threshold-1.0 case in the CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WilliamK112

Copy link
Copy Markdown
Contributor Author

Thanks @sromoam, and thanks @mangeshraut712 for the boundary sweep. Pushed 47c8ead:

  • Must fix 1: match_reason now keeps .3f unless it would round the score across the threshold, and then shows repr. That's your suggestion, with one sentence added to the docstring. 0.7996 vs 0.8 reads below threshold (0.7996 < 0.8), and 0.8004 vs 0.8004 reads above threshold (0.8004 >= 0.8004). 0.900 >= 0.8, 0.500 < 0.8, exact match and the tolerance text are unchanged. non_match_collector.py now calls ThresholdHelper.match_reason(False, similarity, info.threshold), so top-level non_matches entries and list items read the same. The table row at line 67 now expects 0.7999999990000001, and I added the 0.7996 row plus a test for the 0.8004 >= 0.8004 case. All three fail against the previous head.
  • Smaller 1: added Items and the list-of-models field case to the consistency test. I left the test_list_reporting.py literal as it is: it's readable, but you're right that it can't catch the old format. The new case is the real guard now.
  • Smaller 2: rewrote the tp/fd comment at comparison_helper.py:561-562 and the three Returns blocks (comparison_helper.py ×2, structured_model.py) the same way as the ones already changed.
  • Smaller 3: used your CHANGELOG wording, which names the threshold-1.0 case and the rounding rule.

The full suite has the same 9 Windows-only failures as dev (deep-nesting timeouts and NaN recursion, one gallery path test), and ruff passes. I'll bring dev in after 1.1.0 lands, as you asked.

Signed-off-by: Ching-Wei Kang <ckang53@wisc.edu>
@WilliamK112

Copy link
Copy Markdown
Contributor Author

I merged current dev after the 1.1.0 release in 3db297f and kept the threshold-reason note under the new [Unreleased] → Fixed. The 204 focused threshold and list-reporting tests, Ruff, and the docs link check pass.

@sromoam sromoam 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.

Thanks @WilliamK112, this is ready. I checked 3db297f merged with current dev (9680269d). The bullet sits under ## [Unreleased], ### Fixed, and 1.1.0 is untouched. Everything from the last round is in:

  • match_reason keeps .3f unless that would round the score across the threshold (0.7996 < 0.8, 0.8004 >= 0.8004).
  • Top-level non_matches reasons come from it too.
  • The list-of-models case is in the consistency test.
  • The remaining >= comments and Returns blocks are rewritten.

The new tests fail on dev (13 failed) and pass here. The full suite passes merged (7492 passed, 3 skipped, 1 xfailed), and ruff and the docs build are clean.

Smaller

  1. Nothing pins the non_matches change. With non_match_collector.py put back to dev's version, the new tests and the 1648 non-match, threshold and report tests all still pass. One assertion would cover it: a top-level field scoring 0.7996 against 0.8 should give details["reason"] == "below threshold (0.7996 < 0.8)", where dev gives 0.800 < 0.8.
  2. #693 edits the same lines in field_comparison_collector.py, so whichever lands second will hit one conflict there. Keep #693's not_comparable branch and call match_reason in its else. I tried that resolution, and both PRs' tests pass.

@sromoam sromoam added this to the 1.1.1 milestone Dec 4, 2026
@sromoam
sromoam merged commit 8d7cc65 into awslabs:dev Dec 4, 2026
11 checks passed
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

Security Scan Results

ASH Security Scan Report

  • Report generated: 2026-12-04T22:25:22+00:00
  • Time since scan: 3 minutes

Scan Metadata

  • Project: ASH
  • Scan executed: 2026-12-04T22:21:36+00:00
  • ASH version: 3.1.2

Summary

Scanner Results

The table below shows findings by scanner, with status based on severity thresholds and dependencies:

  • Severity levels:
    • Suppressed (S): Findings that have been explicitly suppressed and don't affect scanner status
    • Critical (C): Highest severity findings that require immediate attention
    • High (H): Serious findings that should be addressed soon
    • Medium (M): Moderate risk findings
    • Low (L): Lower risk findings
    • Info (I): Informational findings with minimal risk
  • Duration (Time): Time taken by the scanner to complete its execution
  • Actionable: Number of findings at or above the threshold severity level that require attention
  • Result:
    • PASSED = No findings at or above threshold
    • FAILED = Findings at or above threshold
    • MISSING = Required dependencies not available
    • SKIPPED = Scanner explicitly disabled
    • ERROR = Scanner execution error
  • Threshold: The minimum severity level that will cause a scanner to fail
    • Thresholds: ALL, LOW, MEDIUM, HIGH, CRITICAL
    • Source: Values in parentheses indicate where the threshold is set:
      • global (global_settings section in the ASH_CONFIG used)
      • config (scanner config section in the ASH_CONFIG used)
      • scanner (default configuration in the plugin, if explicitly set)
  • Statistics calculation:
    • All statistics are calculated from the final aggregated SARIF report
    • Suppressed findings are counted separately and do not contribute to actionable findings
    • Scanner status is determined by comparing actionable findings to the threshold
Scanner Suppressed Critical High Medium Low Info Actionable Result Threshold
bandit 0 0 0 0 8213 0 0 PASSED MEDIUM (global)
cdk-nag 0 0 0 0 0 0 0 PASSED MEDIUM (global)
cfn-nag 0 0 0 0 0 0 0 MISSING MEDIUM (global)
checkov 0 0 0 0 0 0 0 PASSED MEDIUM (global)
detect-secrets 0 0 0 0 0 0 0 PASSED MEDIUM (global)
grype 0 0 0 0 0 0 0 MISSING MEDIUM (global)
npm-audit 0 0 0 0 0 0 0 PASSED MEDIUM (global)
opengrep 0 0 0 0 0 0 0 MISSING MEDIUM (global)
semgrep 7 0 0 0 0 0 0 PASSED MEDIUM (global)
syft 0 0 0 0 0 0 0 MISSING MEDIUM (global)

Report generated by Automated Security Helper (ASH) at 2026-12-04T22:25:23+00:00

WilliamK112 added a commit to WilliamK112/stickler that referenced this pull request Dec 5, 2026
Signed-off-by: Ching Wei Kang <164879897+WilliamK112@users.noreply.github.com>
WilliamK112 added a commit to WilliamK112/stickler that referenced this pull request Dec 5, 2026
…ries

Signed-off-by: Ching Wei Kang <164879897+WilliamK112@users.noreply.github.com>
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.

Threshold tolerance follow-ups from #421: one reason text, real scores, stale >= references

3 participants