Skip to content

feat(comparators): add AngleComparator for wraparound angles - #633

Merged
vawsgit merged 4 commits into
awslabs:devfrom
digalaviz:feat/angle-comparator
Nov 27, 2026
Merged

vawsgit merged 4 commits into
awslabs:devfrom
digalaviz:feat/angle-comparator

Conversation

@digalaviz

Copy link
Copy Markdown
Contributor

tl;dr for humans

NumericComparator scores a heading of 359 against 1 as 358 apart, so orientation cannot be scored. This adds AngleComparator, which uses the shortest difference around the circle, in degrees or radians. No existing comparator, default or output changes.


for agents

  • intent: score object headings (yaw) for physical AI object lists; part of [FEATURE]: geometry comparators for physical AI object lists: point distance and wraparound angle #631, with the companion comparator PR.
  • behavior:
    • AngleComparator(threshold=None, max_angular_difference=0.0, angle_unit="degrees"), DEFAULT_THRESHOLD = 1.0.
    • score is 1.0 when the shortest angular difference is within max_angular_difference (inclusive, with the ThresholdHelper float tolerance), else 0.0, the same binary rule as NumericComparator(absolute_tolerance=...).
    • each angle is reduced to one turn before subtracting, so 1e308 vs -1e308 scores 0.0 and 1e17 vs 280.0 scores 1.0 without overflow.
    • construction raises ValueError for an unknown unit, or a tolerance that is negative, non-finite, a bool, a string, too large for a float, or at least half a turn (which would match every pair).
    • strings, bools, Decimal, inf and overflowing values score 0.0; None and float NaN follow the shared policy in base.py.
    • on a List[float] field each element is an angle and elements pair as usual.
  • code: new src/stickler/comparators/angle.py; one line each in stickler/__init__.py, stickler/comparators/__init__.py, comparator_registry.py, tests/test_top_level_exports.py, tests/common/comparators/test_none_handling.py; docs in README.md, docs/docs/API-Reference/comparators.md, docs/docs/Guides/Comparators/README.md, src/stickler/comparators/Comparators.md, src/stickler/comparators/README.md, src/stickler/structured_object_evaluator/README.md; CHANGELOG.md under [Unreleased] / Added.
  • commits: fb027c0 feat(comparators): add AngleComparator for wraparound angles, minor: a new capability.
  • verification: tests/common/comparators/test_angle.py (29 tests: test_degrees_use_the_shortest_difference, test_tolerance_is_inclusive, test_large_angles_reduce_without_overflow, test_radians, test_invalid_config_raises, test_malformed_input_scores_zero, test_missing_values_follow_the_shared_policy, test_config_round_trips, test_structured_model_field_and_list_field, test_json_schema_and_config_import, and more). Full suite on this branch (inspect.getfile(stickler) pointed at the branch's src/): 6626 passed, 13 skipped, 1 xfailed; tests/integrations 821 passed. compare_with output for models using only existing comparators is byte-identical to dev over 1200 runs. uv lock --check passes; no dependency or workflow change.
  • not covered: a graded (non-binary) angle score and 180 degree symmetric objects; NumPy float32 NaN scores 0.0 while a float NaN counts as missing, from the shared base policy. A merge with the PointDistanceComparator PR conflicts only in three docs files, keeping both sides.

- NumericComparator scores 359 against 1 as 358 apart; AngleComparator
  uses the shortest difference around the circle, so they are 2 apart
- binary score like NumericComparator: 1.0 within
  max_angular_difference, 0.0 beyond it; angle_unit is degrees or
  radians
- each angle is reduced to one turn before subtracting, so very large
  inputs neither overflow nor lose the angle
- the tolerance check reuses the ThresholdHelper float tolerance, so
  359.9 against 0.1 meets 0.2
- an unknown unit or a tolerance that is negative, non-finite, too
  large for a float or at least half a turn raises ValueError
- strings, booleans, Decimal, infinite and overflowing values score 0.0
- registered for JSON Schema and stickler config import and exported
  from stickler and stickler.comparators
@digalaviz
digalaviz requested a review from sromoam as a code owner November 26, 2026 02:46
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Security Scan Results

ASH Security Scan Report

  • Report generated: 2026-11-27T21:19:34+00:00
  • Time since scan: 2 minutes

Scan Metadata

  • Project: ASH
  • Scan executed: 2026-11-27T21:16:48+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 7492 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 6 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-11-27T21:19:34+00:00

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

needs changes

One blocker, posted inline on angle.py:102-105: the math.remainder calls trip a semgrep Python-3.7 compatibility rule. The required security (3.12) check fails because of it, so the PR cannot merge. It is a false positive. The fix is two # nosemgrep: comments. The inline comment has the exact text, and I tested it.

The comparator behaved correctly in every case I ran. It is ready apart from the blocker.

What I verified at fb027c0

  • Full suite with PYTHONPATH set to the PR's src gives 6627 passed, 13 skipped, 1 xfailed, 0 failed. The 13 skips are the bert and strands extras plus placeholder tests, and none touches AngleComparator. test_angle.py gives 29 passed. Every other CI check is green.
  • Wraparound in degrees with a tolerance of 5: 359/1, -1/1 and 359.9/0.1 score 1.0. 0/180, 90/-90 and 184/4 score 0.0.
  • Whole turns at the default tolerance: 0/360, 0/720 and -90/630 score 1.0.
  • Large values do not overflow: 1e308 vs -1e308 scores 0.0, and 1e17 vs 280 scores 1.0, as the PR body claims.
  • Radians: 0/2π and π/-π score 1.0. Tolerance edges are inclusive: 2 for 359/1, and 0.2 for 359.9/0.1.
  • Bad configuration raises ValueError: unknown unit, negative, 180, π in radians, NaN, bool, str, 10**400 and None.
  • Bad inputs (str, bool, Decimal, complex, inf) score 0.0. None and NaN follow the shared base.py policy, the same as NumericComparator.
  • Integration: the registry name resolves. from_json_schema and model_from_json both build a working comparator. Exporting with to_json_schema or to_stickler_config and re-importing keeps angle_unit="radians", and scores match. List[float] elements pair across the wrap.
  • Mutation testing: the tests caught 11 of 13 mutations. The one meaningful survivor is the first low item below. The other, fmod for the inner calls, gives the same results.
  • The PR is not stale. Its merge base is the current dev tip, and the change only adds lines.

Low, non-blocking

  • test_config_round_trips compares config with config, so dropping angle_unit from config still passes all 29 tests. Asserting behaviour after the round trip would catch it, for example AngleComparator(**c.config).compare(0, 2*math.pi) == 1.0 for a radians comparator.
  • The bool-input test uses True vs 10, which scores 0.0 even without the bool check. Using True vs 1 would pin the check.
  • My test counts differ from the PR body: 6627 against 6626 for the full suite, and 641 against 821 for tests/integrations. The likely cause is different installed extras. Neither difference points to a defect.

Comment on lines +102 to +105
math.remainder(
math.remainder(float1, full_turn) - math.remainder(float2, full_turn),
full_turn,
)

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.

Blocker (high): these math.remainder calls make the required security (3.12) check fail.

Semgrep rule python.lang.compatibility.python37.python37-compatibility-math1 reports 3 findings here: the outer call (lines 102-105) and both inner calls (line 103). They are false positives, because pyproject.toml sets requires-python = ">=3.10" and math.remainder exists from 3.7. ASH still fails at its MEDIUM threshold:

semgrep      │ 3 │ 3 │ 0 │ 0 │ 0    │ 0 │ 23.1s │ 3      │ FAILED  │ MED (g) │
ERROR (2) Exiting due to 3 actionable findings found in ASH scan

(run 45744687421). I reproduced it locally: uvx semgrep --metrics=off --config r/python.lang.compatibility.python37.python37-compatibility-math1 angle.py gives Ran 1 rule on 1 file: 3 findings. security (3.12) is in the dev ruleset's required status checks, so this PR cannot merge as it stands.

Fix: suppress it with # nosemgrep: and a reason, as the repo already does for the non-literal-import false positives (comparator_registry.py:114). It needs a comment above line 102 and one above line 103. One comment does not cover the inner calls. With both comments, the same semgrep command gives 0 findings:

            # requires-python is >=3.10, so math.remainder (3.7+) is always present.
            # nosemgrep: python.lang.compatibility.python37.python37-compatibility-math1
            math.remainder(
                # nosemgrep: python.lang.compatibility.python37.python37-compatibility-math1
                math.remainder(float1, full_turn) - math.remainder(float2, full_turn),

Changing the inner calls to math.fmod, as the scanner suggests, does not clear the scan, because the outer remainder is still flagged.

@digalaviz
digalaviz requested a review from harrycav November 26, 2026 13:39
…mainder

requires-python is >=3.10 and math.remainder exists
  since 3.7. The outer and inner calls each need a nosemgrep comment.
@digalaviz

Copy link
Copy Markdown
Contributor Author

Both points are addressed.

  • Blocker: 8cbe16b adds a # nosemgrep: comment above the outer math.remainder call and another above the inner one, with the reason, as you suggested. The semgrep rule now reports 0 findings on angle.py.
  • The two low items: df9f245 fixes the tests. test_config_round_trips now checks how the rebuilt comparator scores (0 vs 2π and 0 vs 0.05, in radians), so dropping angle_unit or the tolerance from config fails the test. In the malformed-input test, each value is paired with a number it equals, so True is now compared with 1. I removed each of those three checks from the source in turn, and each removal makes the tests fail.

Ready for another look.

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

✅ LGTM!

The two new commits fix the blocker and both low items from my review at fb027c0. I found no new medium or high problems.

Previous findings, checked at df9f245

  • Blocker (semgrep python37-compatibility-math1): fixed. angle.py:102-106 has two # nosemgrep: comments, each naming only that rule. The rule went from 3 findings to 0: Ran 1 rule on 1 file: 0 findings. With --disable-nosem it reports the same 3 findings, and p/python plus p/default (320 rules) also gives 3 with --disable-nosem and 0 without. So the comments hide nothing else. The parsed code of angle.py is identical before and after, so behaviour is unchanged. security (3.12) now passes.
  • test_config_round_trips: fixed. It now scores the rebuilt comparator. Dropping angle_unit from config fails it (1 failed, 28 passed). Dropping max_angular_difference also fails it.
  • Bool input: fixed. The test now compares True with 1. Removing the bool check in _finite_float fails test_malformed_input_scores_zero[True-1].

Other checks

  • test_angle.py: 29 passed. Full suite with the PR's src (and the llm extra installed): 6636 passed, 11 skipped, 1 xfailed, 0 failed.
  • Three more mutations of the input checks (Real, isfinite, the OverflowError guard) each fail at least one test.
  • Not stale: the merge base is the current dev tip, dev has no commits the PR lacks, and the merge is clean.
  • All 11 CI checks pass at df9f245.

# Conflicts:
#	CHANGELOG.md
#	src/stickler/comparators/README.md
#	src/stickler/structured_object_evaluator/README.md
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