Skip to content

fix(gate): compare NumPy scalar measurements instead of abstaining - #795

Open
Abhishek-B-R wants to merge 3 commits into
Hebbian-Robotics:mainfrom
Abhishek-B-R:fix/gate-numpy-measurements
Open

Abhishek-B-R wants to merge 3 commits into
Hebbian-Robotics:mainfrom
Abhishek-B-R:fix/gate-numpy-measurements

Conversation

@Abhishek-B-R

@Abhishek-B-R Abhishek-B-R commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A gate now compares NumPy scalar measurements. Before, a check that returned np.int64 or np.float32 (what np.count_nonzero, .sum() on an int array, or any float32 array reduction gives you) made its gate abstain, so a critical check never quarantined the episode however bad the number was.

Why

evaluate_gate keeps a matched value only if it is an int | float instance. np.int64 and np.float32 are not subclasses of either (only np.float64 is), so they fell into the "not a real number" branch. The catalog boundary already converts NumPy scalars with .item() (#126), and WeightedValue does the same (#647), but the gate runs on the raw CheckResult before the catalog sees it.

Repro:

gate = hflow.Gate(accept_when=(hflow.Threshold("value", hflow.Comparison.AT_MOST, 50.0),))

@app.check(version="1", critical=True, gate=gate)
async def counts_frames(ep):
    return hflow.CheckResult(measurements={"value": np.int64(99)})

On main the run reports measured, quarantine_tags == [] and tags gate-unevaluated:value. With this change it reports failed and quarantined:counts_frames, the same as for 99 or 99.0.

The fix converts np.generic values with .item() before the existing checks, looking NumPy up in sys.modules the way statistics.py does so steps.py stays NumPy free. np.bool_ becomes bool and still abstains, as True does today.

Validation

  • New test_numpy_scalar_measurements_compare_like_python_numbers in tests/test_gates.py: fails on main (GateAbstained(unevaluated_patterns=('value',)) == GateDecided(verdict=False)), passes with the fix.
  • uv run pytest -q tests/test_gates.py tests/test_checks.py tests/test_statistics.py: 167 passed.
  • uv run pytest -q -n 4: 2595 passed, 49 skipped, 3 failed. The 3 are tests/test_cli.py::test_the_module_form_*, which fail the same way on main in my macOS environment and do not touch this code.
  • uv run ruff check, uv run ruff format --check, uv run ty check: clean.

Checklist

  • I added or updated outcome-focused tests for changed business logic.
  • I updated documentation for changed behavior, flags, formats, or requirements. (No documented behavior changes.)
  • I ran uv run ruff check --fix, uv run ruff format, and uv run ty check.
  • I ran the relevant pytest suite.
  • I did not add recordings, generated media, credentials, private URLs, or runtime artifacts.
  • I preserved stored-data compatibility or documented an explicit version change.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.


Note

Medium Risk
Changes quality-gating behavior for checks that emit NumPy scalars—correct for critical quarantine but could newly fail episodes that previously abstained; scope is limited to evaluate_gate with targeted tests.

Overview
Gate evaluation now treats NumPy integer and floating scalars (e.g. np.int64, np.float32 from reductions like np.count_nonzero) like plain Python numbers by calling .item() before threshold checks. Previously those values failed isinstance(..., int | float), so gates abstained and critical checks did not quarantine even when the measurement clearly failed.

NumPy is resolved via sys.modules so steps.py stays NumPy-free; np.timedelta64 and other non-numeric scalars are still left alone so gates abstain as before.

A new gate test covers direct evaluate_gate calls and an end-to-end critical check that returns np.int64(99) against an AT_MOST 50 threshold, expecting FAILED and quarantine.

Reviewed by Cursor Bugbot for commit 07d15ee. Bugbot is set up for automated code reviews on this repo. Configure here.

evaluate_gate only compared values that were int or float instances.
np.int64 and np.float32 are neither, so a check that returned a count
straight from NumPy made its gate abstain: the episode got a
gate-unevaluated tag and no verdict, and a critical check never
quarantined it. The catalog already stores those scalars as their Python
values, so the gate now reads them the same way before comparing.
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

Gates compare NumPy numeric measurements instead of abstaining.

  • Gate thresholds now compare NumPy integer and float measurements.

Reviews (3) · Last reviewed commit: "fix(gate): read np.longdouble measuremen..." · Reviewed by Greptile

greptile-apps[bot]
greptile-apps Bot previously approved these changes Nov 17, 2026

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

Could you limit the conversion to numeric scalars and cover a date or duration in the regression test? I verified that both np.datetime64(99, "ns") and np.timedelta64(99, "ns") now produce a failing threshold verdict where the gate used to abstain.

np.datetime64 and np.timedelta64 are NumPy scalars too, so the first
version turned them into ints and compared them against the threshold
instead of abstaining. timedelta64 even subclasses np.integer, so it is
excluded by name. The regression test now covers both.
@greptile-apps
greptile-apps Bot dismissed their stale review November 20, 2026 13:07

Dismissed because a newer commit was pushed; Greptile will re-review the current head.

greptile-apps[bot]
greptile-apps Bot previously approved these changes Nov 20, 2026
@Abhishek-B-R

Copy link
Copy Markdown
Contributor Author

@kstonekuan Done in 07d15ee. The conversion now only applies to np.integer and np.floating and explicitly excludes np.timedelta64 (it subclasses np.signedinteger); np.datetime64 isn't in either branch, so it falls through unchanged. The regression test now asserts that np.bool_(True), np.datetime64(99, "ns") and np.timedelta64(99, "ns") all make the gate abstain, alongside the existing np.int64/np.float32 comparison cases. Could you take another look?

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

Could you cover np.longdouble too? On Linux x86-64, np.longdouble(99).item() stays a NumPy scalar, so a gate with AT_MOST 50 still abstains.

np.longdouble(...).item() returns a NumPy scalar rather than a Python float, so a longdouble measurement still made the gate abstain. Convert with int()/float() instead, and cover np.longdouble in the regression test.
@greptile-apps
greptile-apps Bot dismissed their stale review November 23, 2026 18:15

Dismissed because a newer commit was pushed; Greptile will re-review the current head.

@Abhishek-B-R

Copy link
Copy Markdown
Contributor Author

@kstonekuan Covered in b7e1e00. as_python_number now converts with int() / float() instead of .item(), since np.longdouble(99).item() stays a NumPy scalar where long double is wider than a C double (Linux x86-64). The regression test adds np.longdouble(99) to the values that must compare, so AT_MOST 50 now gives a failing verdict for it instead of abstaining; the bool/datetime64/timedelta64 abstain cases are unchanged.

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.

2 participants