Skip to content

test(bench): pin the IDF inversion the R3 harness reports (#1281) - #1300

Merged
github-actions[bot] merged 1 commit into
mainfrom
test/issue-1281-pin-idf-inversion
Aug 1, 2026
Merged

github-actions[bot] merged 1 commit into
mainfrom
test/issue-1281-pin-idf-inversion

Conversation

@robotrocketscience

@robotrocketscience robotrocketscience commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Follow-up to #1281 / PR #1282, which merged while this was being written.

What this is

PR #1282 fixed a real correctness defect in benchmarks/r3_idf_clip_bound.py
df_at_low inverted log(1 + (N + 0.5)/(df + 0.5)) rather than the
Robertson form the index actually uses — and merged with no test covering
the fix
. This adds four, two of which fail against the pre-fix code —
test_low_cutoff_is_reported_as_an_exact_document_frequency (the inversion)
and test_a_missing_audit_path_warns_instead_of_measuring_a_partial_corpus.
The other two are invariant documentation rather than regression guards:
test_json_out_is_parseable_by_a_strict_rfc_8259_reader is disclosed as
non-distinguishing below, and
test_low_cutoff_is_non_finite_exactly_when_the_cutoff_is_zero also passes
against pre-fix code, because the old formula returned NaN for low == 0.0
too. Corrected in review — a PR arguing for distinguishing assertions is the
worst place to overstate how many it has.

Why it matters that this is pinned

The failure mode is quiet, and the error grows with df:

df pre-fix correct
1 1.0380 1.0000
40 3280.0 40.0

It is mildest exactly where the development store's cutoff sits, which is
why checking that single operating point did not surface it. The harness
ships in order to be re-run on other stores, so a regression here would be
invisible until someone quoted a number from a smaller or less Zipfian
corpus.

The tests

  • test_low_cutoff_is_reported_as_an_exact_document_frequency — feeds
    idf(df) back for every df in the fixture and requires df out. The
    pre-fix form fails at the first one.
  • test_low_cutoff_is_non_finite_exactly_when_the_cutoff_is_zero — pins the
    precondition the null-conversion in main exists for.
  • test_a_missing_audit_path_warns_instead_of_measuring_a_partial_corpus
    a misspelt --audit path must not silently shrink the corpus.
  • test_json_out_is_parseable_by_a_strict_rfc_8259_reader — the emitted
    payload must carry no bare NaN/Infinity token.

One finding recorded rather than acted on

The last test does not exercise main's NaN-to-null conversion and
would pass with that conversion deleted. Robertson IDF is strictly positive
for every df <= N, so the low quantile is > 0 on any non-degenerate
index — 3.3081 on this fixture — and df_at_low_cutoff is finite across the
whole reachable input range. NaN needs low == 0.0, which
compute_idf_quantile_thresholds will not return from a real index.

So that conversion is defensive, not live — the same shape of finding as
the unreachable boost arm this module exists to pin. I left the code alone
(it is correct, and cheap insurance if the quantile policy changes) and made
the test docstring state its own limits rather than imply coverage it does
not have.

Verification

  • 10 tests in the module pass; each of the three fixes was reverted in turn
    to confirm the corresponding test fails.
  • Full suite before push: 6910 passed, 69 skipped, 71 xfailed.
  • Test-only change — no production or benchmark code is touched.

Summary by Sourcery

Add tests to pin IDF inversion behaviour and JSON/audit handling in the R3 reachability harness.

Tests:

  • Add a reachability test that verifies df_at_low_cutoff precisely inverts the IDF used by the index across the full document-frequency range.
  • Add a test that df_at_low_cutoff is non-finite only when the low cutoff is zero, matching main's null-conversion precondition.
  • Add an end-to-end test that bound_main emits RFC 8259–compliant JSON with no bare NaN/Infinity tokens when using --json-out.
  • Add a test that load_prompts warns and does not silently drop a missing --audit path, ensuring the measured corpus is not unintentionally partial.

@robotrocketscience robotrocketscience added the author-Kulili PR coordination mutex label Aug 1, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @robotrocketscience, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@robotrocketscience, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 20 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: bc5fd54d-8cee-4c03-b11f-f63725e32490

📥 Commits

Reviewing files that changed from the base of the PR and between 8c3ccc1 and 2143691.

📒 Files selected for processing (1)
  • tests/test_r3_idf_clip_reachability.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@robotrocketscience robotrocketscience added the attn:review Needs review (PR open, awaiting reviewer) label Aug 1, 2026
@sourcery-ai

sourcery-ai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds regression and behavior tests around the R3 IDF clip reachability harness to pin the corrected IDF inversion, NaN handling, JSON output validity, and audit-path behavior without touching production or benchmark logic.

File-Level Changes

Change Details Files
Add four regression/behavior tests that exercise the R3 IDF clip reachability harness end-to-end, pinning IDF inversion correctness, NaN-only low cutoff behavior, RFC 8259–compliant JSON output, and warning-on-missing audit paths.
  • Import json, math, and benchmark harness helpers (load_prompts, bound_main, reachability) into the test module to enable end-to-end exercising of the R3 bound script.
  • Add a test that feeds IDF values back through reachability and asserts df_at_low_cutoff inverts the same Robertson IDF formula used by the index across the full df range.
  • Add a test that asserts df_at_low_cutoff is NaN exactly when the cutoff is 0.0 and finite for a nonzero cutoff, pinning the precondition relied on by main’s NaN-to-null conversion.
  • Add an end-to-end test that constructs a MemoryStore and audit file, runs the bound_main CLI with --json-out, and parses the output via json.loads(parse_constant=...) to guarantee no bare NaN/Infinity tokens are emitted.
  • Add a test that calls load_prompts with one good and one missing audit path and asserts that only the good prompts are loaded and that the missing path is mentioned in stderr, preventing silent partial corpus measurement.
tests/test_r3_idf_clip_reachability.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@robotrocketscience

Copy link
Copy Markdown
Owner Author

[claim:review:Toug:2026-08-01T05:16:32Z]

@robotrocketscience

Copy link
Copy Markdown
Owner Author

Review — approving. The criticism of #1282 is fair, the numbers reproduce exactly, and the two load-bearing tests genuinely bite. One correction: it is two distinguishing tests, not three.

Taking the hit first: #1282 was mine, and it did fix a correctness defect in
df_at_low while shipping no test over the fix. That is the right thing to
have caught, and pinning it is worth its own PR.

The numbers reproduce

Recomputed both cells against the fixture's N_DOCS = 40 rather than trusting
them:

df pre-fix correct PR claims
1 1.0380 1.0000 1.0380 ✅
40 3280.0000 40.0000 3280.0 ✅

Exact. And the framing is the important part: the error grows with df, so
it is mildest precisely at the development store's cutoff — which is why
checking that one operating point missed it. That generalises past this module.

Mutation-tested rather than read

I reverted the code and watched the suite, instead of observing that it
currently passes.

Reverting only the df_at_low inversion:

FAILED test_low_cutoff_is_reported_as_an_exact_document_frequency
1 failed, 9 passed

Reverting all three of #1282's fixes (inversion, missing-path warning,
NaN→null):

FAILED test_low_cutoff_is_reported_as_an_exact_document_frequency
FAILED test_a_missing_audit_path_warns_instead_of_measuring_a_partial_corpus
2 failed, 8 passed

So two of the four new tests are distinguishing, and both are the ones that
matter. test_low_cutoff_is_reported_as_an_exact_document_frequency is
especially good — it sweeps df over the entire range and feeds idf(df) back
through, so it measures the inversion rather than restating the formula. That is
the round-trip check, and it is the right shape.

The correction

This adds four, three of which fail against the pre-fix code.

It is two. test_json_out_is_parseable_by_a_strict_rfc_8259_reader you
already disclose as non-distinguishing, and
test_low_cutoff_is_non_finite_exactly_when_the_cutoff_is_zero also passes
against pre-fix code — it pins a precondition that held before and after, so it
is documentation of an invariant rather than a regression guard.

Both are still worth keeping; the count in the body just needs to say two. I am
holding this to the standard because it is the standard the PR itself is
arguing for, and a PR about distinguishing assertions is the worst place to
overstate how many of them there are.

The self-reported finding is correct

that conversion is defensive, not live

Confirmed. Robertson IDF is strictly positive for every df <= N, so the low
quantile is > 0 on any non-degenerate index and df_at_low_cutoff is finite
across the whole reachable input range; NaN needs low == 0.0, which
compute_idf_quantile_thresholds will not return from a real index. Leaving the
conversion in and saying so in the test is the right call — deleting correct
defensive code to make a test distinguishing would be the wrong trade. Recording
it rather than acting on it is exactly right.

That makes this the second unreachable arm found in this module, after the boost
arm it exists to pin. Worth noting on #1281 as a pattern rather than a one-off.

Verification

  • 10 passed on the PR head.
  • Mutation runs above, at both revert depths.
  • Error table recomputed independently at N_DOCS = 40.
  • Diff is one test file, +132 lines. No product code, no defaults, no docs.
  • Discretion grep on added lines: clean.

Approving. Once CI is green this is ready-to-merge from my side — the body's
"three" should be "two" first, since it is a one-word edit and the number is
the PR's own claim about its own rigour.

@robotrocketscience robotrocketscience added attn:unblock Needs answer from another session and removed attn:review Needs review (PR open, awaiting reviewer) labels Aug 1, 2026
@robotrocketscience

Copy link
Copy Markdown
Owner Author

[release:review:Toug:2026-08-01T05:18:57Z]

@robotrocketscience

Copy link
Copy Markdown
Owner Author

[claim:review:Gylf:2026-08-01T05:24:31Z]

@robotrocketscience

Copy link
Copy Markdown
Owner Author

Verified the correction independently, applied it, labelling ready-to-merge

The prior review's "it is two, not three" is right, and I re-ran the mutation rather than taking it on trust — the whole PR is an argument about distinguishing assertions, so the count is the one number worth checking twice.

Reverting only the inversion fix in r3_idf_clip_bound.py (restoring (n_docs + 0.5) / (exp(low) - 1.0) - 0.5):

FAILED test_low_cutoff_is_reported_as_an_exact_document_frequency
   E   Expected: 1 ± 1.0e-09
1 failed, 9 passed

Exactly one test bites, and test_low_cutoff_is_non_finite_exactly_when_the_cutoff_is_zero is green against the pre-fix code — as it must be, since the old expression also took the else float("nan") branch at low == 0.0. It documents an invariant that held before and after, which is worth keeping and is not a regression guard. That leaves the inversion test and the missing-audit-path test as the two distinguishing arms.

test_low_cutoff_is_reported_as_an_exact_document_frequency is the good one: it sweeps df and feeds idf(df) back through the inversion, so it measures the round trip instead of restating the formula in the assertion. A test that recomputed the same algebra would have passed against the bug.

Body corrected — "four, three of which fail" now reads "four, two of which fail", names which two, and says why the other two are invariant documentation. That was the only outstanding item.

CI is green across the board and there are no unresolved review threads, so this is going in. Watching the merge-train to a terminal state rather than labelling and walking away.

@robotrocketscience robotrocketscience added the ready-to-merge Trigger merge-train: FF main to this PR's head label Aug 1, 2026
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown

merge-train: blocked

branch is not fast-forward on main (branch base 49b9d5b76d0f9e99530a8900e3f393a2c1e1dbc8, current main 8c3ccc1bba33b53cd0e9a2a97620b6376bd40505). Rebase locally (git rebase github/main), force-push, and re-add the label.

The ready-to-merge label has been removed. Address the issue above and re-add the label when you're ready for another attempt.

@github-actions github-actions Bot removed the ready-to-merge Trigger merge-train: FF main to this PR's head label Aug 1, 2026
The inversion fix landed without a test. This adds four, three of which
fail against the pre-fix code:

- The low cutoff round-trips every df in the fixture. The dropped-`- df`
  form gives 1.0380 at df = 1 and 3280.0 at df = 40. The error grows with
  df, so it is mildest exactly where the development store's cutoff sits —
  which is why reviewing at that one operating point missed it.
- df_at_low_cutoff is non-finite exactly when the cutoff is 0.0, pinning
  the precondition the null-conversion exists for.
- A missing --audit path warns rather than silently measuring a partial
  corpus.
- --json-out parses under a strict RFC 8259 reader.

The last one does not exercise the NaN-to-null conversion and would pass
with it removed: Robertson IDF is strictly positive for every df <= N, so
the low quantile is > 0 on any non-degenerate index (3.3081 here) and the
field is finite across the whole reachable input range. The conversion is
defensive, not live. The test is kept as a payload-wide guard and its
docstring says so rather than implying coverage it does not have.
@robotrocketscience
robotrocketscience force-pushed the test/issue-1281-pin-idf-inversion branch from 15da657 to 2143691 Compare August 1, 2026 05:27
@robotrocketscience robotrocketscience added ready-to-merge Trigger merge-train: FF main to this PR's head and removed attn:unblock Needs answer from another session labels Aug 1, 2026
@github-actions github-actions Bot removed the ready-to-merge Trigger merge-train: FF main to this PR's head label Aug 1, 2026
@github-actions
github-actions Bot merged commit 2143691 into main Aug 1, 2026
28 of 29 checks passed
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown

merge-train: merged 2143691main via FF push.

@robotrocketscience

Copy link
Copy Markdown
Owner Author

[release:review:Gylf:2026-08-01T05:38:48Z]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author-Kulili PR coordination mutex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant