Skip to content

feat(spend): paid guards slice 4 for eval triggers generate --rewrite - #1670

Open
napetrov wants to merge 1 commit into
intel:mainfrom
napetrov:feat/cli-paid-guards-4
Open

napetrov wants to merge 1 commit into
intel:mainfrom
napetrov:feat/cli-paid-guards-4

Conversation

@napetrov

@napetrov napetrov commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Part of #94 (F1b, spend: paid-guards slice 4, the leftovers of #1576). #94 stays open.

What

  • eval triggers generate --rewrite gets --dry-run and --max-usd (the feat(questions): --dry-run and --max-usd on eval questions analyze and review (Part of #94) #1576 pattern).
    • Dry run: prints the estimate for one rewrite per question, builds no model client, writes nothing.
    • --max-usd: refused before the first call (exit 3, no call made), or stopped mid-run (exit 1, no file written).
    • The estimate assumes 200 reply tokens per rewrite. Input is priced from the actual prompt.
    • Trigger generate is serial: a call starts only while the spend is under the cap, so the last call can overshoot --max-usd by its own cost.
  • Leftover (a), concurrency: SpendCap.before_call(reserve_usd=...) reserves a call's list-price estimate before dispatch, released when the call returns or raises. MeteredCalls (questions analyze/review) uses it, so calls in flight under --concurrency cannot pass the cap together. Trigger generate is serial and keeps the plain check-then-record.
  • Behaviour change, questions analyze / review at any --concurrency: a call is refused when its list-price estimate (400 or 800 assumed reply tokens) plus the calls in flight would pass --max-usd. A run can therefore stop a little earlier than before. The reservation is a guard at list price, not the accounting figure.
  • Leftover (b), hint: SpendCap.measure_less is set per leaf. Novelty names --tier / --max-atoms only where those flags exist; other leaves say "measure less".
  • Refactor: the rewrite pricing and gate moved to dokimi/commands/catalog_rewrite.py (catalog.py was over the 800-line cap). The reply-token constants moved to dokimi/spend_preview.py, so the command no longer imports the questions package (which needs optional deps such as httpx).

Leaves

Leaf Before After
eval triggers generate --rewrite no guard --dry-run, --max-usd

Kept rows: none. cli-interface.json gains the two flags (+2 lines).

Not guarded, with reasons

Remaining paid leaves with no --max-usd (from scripts/cli_spend_controls.py, after this PR): agent run; answers generate; arms matrix-run; arms run; benchmark batch; benchmark run; catalog trigger-probe; delivery scan; eval judge agreement/validate/wording; eval questions generate/refine; eval redteam; eval score checklist/pairwise/panel/pointwise/ragas/reference; evaluate; explore run; repo reach; repo skill-reach; subjects run; tasks run.

Evidence

  • New tests: tests/test_triggers_dry_run_and_cap.py (8), tests/test_spend_inflight_reserve.py (9). The reservation, hint and dry-run/cap tests failed on the old code; test_plain_rewrite_still_writes_the_set and test_metered_calls_without_a_cap_price_nothing guard existing paths.
  • Local make ci-local CI_LOCAL_ARGS="--offline": exit 0.
  • Every client call in the tests is mocked. No paid leaf was run.

🤖 Generated with Claude Code

@napetrov
napetrov requested a review from a team as a code owner December 6, 2026 08:02
Copilot AI balanced review requested due to automatic review settings December 6, 2026 08:02
@napetrov napetrov added the cli-interface-change Changes dokimi's user-facing CLI surface label Dec 6, 2026
@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown

CLI interface change detected

This PR changes dokimi's user-facing CLI surface (commands and/or options):

~ `dokimi eval triggers generate` parameters changed:
    + added option `--dry-run`
    + added option `--max-usd`

Updated on every push; the cli-interface-change label is on while the diff is non-empty.

Copilot AI 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.

🟡 Changes recommended

Analyze/review can stop even when estimates exactly fit the budget because floating-point reservation sums exceed the cap by roundoff; separately, releasing a child reservation leaves the parent’s reservation held, so a retry can be incorrectly refused even though nothing was spent.

2 open issues
What changed in this PR

2 new findings. This PR extends dokimi’s paid-command spend controls for #94, adding guarded trigger rewrites and concurrent-call reservations.

Changes:

  • Adds --dry-run and --max-usd to trigger generation.
  • Reserves estimated spend for question analysis/review calls in flight.
  • Makes cap-refusal hints specific to each command.
File Description
dokimi/​tests/​test_triggers_dry_run_and_cap.py Tests rewrite previews and spending limits.
dokimi/​tests/​test_spend_inflight_reserve.py Tests reservations, releases, and hints.
dokimi/​tests/​test_cli_spend_controls.py Updates guarded-command coverage.
dokimi/​dokimi/​spend.py Adds spend reservations and configurable hints.
dokimi/​dokimi/​questions/​quality_analyzer.py Supplies classification token estimates.
dokimi/​dokimi/​questions/​panel_reviewer.py Supplies review token estimates.
dokimi/​dokimi/​questions/​metered.py Reserves and releases estimated call costs.
dokimi/​dokimi/​novelty/​cost.py Builds command-specific refusal hints.
dokimi/​dokimi/​novelty/​client.py Propagates reservations through nested caps.
dokimi/​dokimi/​commands/​questions.py Reuses shared token assumptions.
dokimi/​dokimi/​commands/​catalog.py Registers flags and delegates rewrite handling.
dokimi/​dokimi/​commands/​catalog_rewrite.py Implements rewrite pricing and spending guards.
dokimi/​dokimi/​commands/​catalog_novelty.py Passes hints to shared and nested caps.
dokimi/​docs/​reference/​cli-interface.json Records the two new flags.
dokimi/​changelog.d/​20261205_120000_cli-paid-guards-4.md Documents spend-control changes.

Review effort: Balanced

self.parent.record(usage, model, reserved=reserved)
super().record(usage, model, reserved=reserved)
self.parent.record(usage, model, reserved=reserved, reserve_usd=reserve_usd)
super().record(usage, model, reserved=reserved, reserve_usd=reserve_usd)
Comment thread dokimi/dokimi/spend.py
Comment on lines +209 to +210
if self.limit_usd is not None and reserve_usd > 0 and (
self.spent_usd + self.reserved_usd + reserve_usd > self.limit_usd):
@exai-bot

exai-bot commented Dec 6, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Review complete: 0 finding(s) across 16 selected item(s).

CI at 2ded3ab: 22 passing, 4 skipped.

Test notes (2)
  • dokimi/dokimi/spend.py:209: With if self.limit_usd is not None and reserve_usd > 0 and ( changed to if self.limit_usd is not None and reserve_usd >= 0 and (, the tests still pass: dokimi/tests/test_cli_spend_controls.py, dokimi/tests/test_spend.py, dokimi/tests/test_spend_inflight_reserve.py, dokimi/tests/test_triggers_dry_run_and_cap.py. A value exactly at the boundary gives the same result either way here, so no test says which one is meant.
  • dokimi/dokimi/spend.py:210: With self.spent_usd + self.reserved_usd + reserve_usd > self.limit_usd): changed to self.spent_usd + self.reserved_usd + reserve_usd >= self.limit_usd):, the tests still pass: dokimi/tests/test_cli_spend_controls.py, dokimi/tests/test_spend.py, dokimi/tests/test_spend_inflight_reserve.py, dokimi/tests/test_triggers_dry_run_and_cap.py. A value exactly at the boundary gives the same result either way here, so no test says which one is meant.
Review details
  • Commit reviewed: 2ded3ab against merge base 19e5222
  • Coverage: 16 files in 3 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 619k tokens (397k from cache), 57s
  • Tool calls: 58 (37 file_read, 9 code_search, 5 file_read_diff, 5 pr_threads, 2 local_check_results)
  • Catalog CLI paid guards, novelty costs, and rewrite command: 7 files
  • Question metering and trigger dry-run/cap enforcement: 5 files
  • Shared spend controls, previews, and in-flight reservations: 4 files

Comment thread dokimi/dokimi/spend.py
Comment on lines +209 to +210
if self.limit_usd is not None and reserve_usd > 0 and (
self.spent_usd + self.reserved_usd + reserve_usd > self.limit_usd):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bug · should fix
Impact: a call whose estimate fits the remaining budget exactly can be refused, prematurely stopping analyze/review.

Trigger: cap = SpendCap(0.3); cap.before_call("m", reserve_usd=0.1); cap.before_call("m", reserve_usd=0.2).

Evidence: the new sum evaluates to 0.30000000000000004 > 0.3, so the second reservation raises SpendCapReached; production MeteredCalls passes float list-price estimates into this comparison. The preview sums prices using Decimal and accepts the corresponding $0.30 budget, making admission inconsistent with that gate.

Fix: Use decimal accounting or a tightly bounded rounding tolerance for reservation admission, and add an exact-budget regression test (including release/re-reserve).

Workaround: increase --max-usd slightly.

Should fix: A run shows it; it was not judged to be met in ordinary use.

Shown by running: exit 1 (at the head) · exit 1 (at the merge base, which cannot run it) · exit 0 (at the head with the fix)

python -m pytest -q -p no:randomly -p no:cacheprovider tests/test_proof_reserve_exact.py

What the proof does: Reserves 0.1 then 0.2 against a 0.3 cap and asserts the second reservation is admitted.

The problem shows as the output EXACT_FIT_REFUSED.

dokimi/tests/test_proof_reserve_exact.py

from dokimi.spend import SpendCap, SpendCapReached


def test_exact_fit_reservation_admitted():
    cap = SpendCap(0.3)
    cap.before_call('m', reserve_usd=0.1)
    try:
        cap.before_call('m', reserve_usd=0.2)
    except SpendCapReached:
        raise AssertionError('EXACT_FIT_REFUSED: reservation that equals the limit was refused')
        try:
            cap.before_call('m', reserve_usd=0.2)
        except SpendCapReached:
>           raise AssertionError('EXACT_FIT_REFUSED: reservation that equals the limit was refused')
E           AssertionError: EXACT_FIT_REFUSED: reservation that equals the limit was refused

tests/test_proof_reserve_exact.py:10: AssertionError
=========================== short test summary info ============================
FAILED tests/test_proof_reserve_exact.py::test_exact_fit_reservation_admitted
1 failed in 0.50s

…ed and capped

- eval triggers generate --rewrite: --dry-run prints the estimate (200 reply
  tokens per rewrite) and makes no client; --max-usd refuses before the first
  call or stops mid-run with exit 1 and writes no file.
- --concurrency with --max-usd: a call in flight reserves its list-price
  estimate before dispatch and releases it on return, so calls in flight
  cannot pass the cap together (questions analyze/review).
- The cap refusal names only the flags the leaf has: SpendCap.measure_less is
  set per leaf (novelty names --tier/--max-atoms only where they exist).

Part of intel#94 (F1b, spend: paid-guards slice 4).

Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>
@napetrov
napetrov force-pushed the feat/cli-paid-guards-4 branch from e82eadc to 2ded3ab Compare December 6, 2026 08:42
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.44%. Comparing base (19e5222) to head (2ded3ab).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1670      +/-   ##
==========================================
- Coverage   96.44%   96.44%   -0.01%     
==========================================
  Files         585      586       +1     
  Lines       76600    76680      +80     
  Branches    13175    13185      +10     
==========================================
+ Hits        73876    73951      +75     
- Misses       1671     1672       +1     
- Partials     1053     1057       +4     
Files with missing lines Coverage Δ
dokimi/dokimi/commands/catalog.py 98.66% <100.00%> (-0.04%) ⬇️
dokimi/dokimi/commands/catalog_novelty.py 94.22% <100.00%> (+0.01%) ⬆️
dokimi/dokimi/commands/catalog_rewrite.py 100.00% <100.00%> (ø)
dokimi/dokimi/commands/questions.py 92.37% <100.00%> (-0.03%) ⬇️
dokimi/dokimi/novelty/client.py 94.24% <100.00%> (+0.04%) ⬆️
dokimi/dokimi/novelty/cost.py 96.87% <100.00%> (+0.07%) ⬆️
dokimi/dokimi/questions/metered.py 100.00% <100.00%> (ø)
dokimi/dokimi/questions/panel_reviewer.py 95.83% <100.00%> (+0.02%) ⬆️
dokimi/dokimi/questions/quality_analyzer.py 100.00% <100.00%> (ø)
dokimi/dokimi/spend.py 100.00% <100.00%> (ø)
... and 1 more

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown

Mutation of changed lines

⚠️ Tests miss 14 of 56 small breaks of the lines this PR changes, and the check was not complete

At 2ded3ab: 42 caught · 14 missed · 24 not tried in time

Each row is a small deliberate break (a mutant) of a line this PR changes that every test reaching that line still passes. Add a test that fails on it or, if it cannot change behaviour, put # mutation-ok[<operator>]: <reason> at the end of the line or alone on the line above.

Where Operator Change no test notices Tests reaching the line
dokimi/dokimi/commands/catalog_rewrite.py:23 bump-int 200 → 201 55
dokimi/dokimi/commands/catalog_rewrite.py:38 delete-statement check_rewrite_flags(args) → pass 18
dokimi/dokimi/commands/catalog_rewrite.py:39 drop-clause args.rewrite and (args.dry_run or args.max_usd is not None) → (args.dry_run or args.max_usd is not None) 18
dokimi/dokimi/commands/catalog_rewrite.py:41 take-branch SpendCap(args.max_usd) if args.max_usd is not None else None → (None) 18
dokimi/dokimi/commands/catalog_rewrite.py:52 drop-clause args.max_usd is not None and not args.rewrite → (args.max_usd is not None) 18
dokimi/dokimi/commands/catalog_rewrite.py:54 drop-clause args.dry_run and not args.rewrite → (args.dry_run) 18
dokimi/dokimi/novelty/client.py:91 delete-statement self._release_usd_locked(reserve_usd) → pass 6
dokimi/dokimi/novelty/client.py:94 flip-bool True → False 72
dokimi/dokimi/novelty/cost.py:225 drop-filter hasattr(args, name) → True 110
dokimi/dokimi/novelty/cost.py:226 return-none f"measure less ({', '.join(flags)})" if flags else "measure less" → None 110
dokimi/dokimi/questions/metered.py:24 bump-int 0 → 1 9
dokimi/dokimi/spend.py:182 delete-statement self.reserved_usd = 0.0 → pass 224
dokimi/dokimi/spend_preview.py:23 bump-int 400 → 401 59
dokimi/dokimi/spend_preview.py:24 bump-int 800 → 801 59

Not the whole picture:

  • 24 breaks were not tried before the time budget ran out
  • 39 more breaks of covered changed lines were over the limit and not tried
  • 34 changed lines were not mutated

Warning only: a missed break does not fail the job. Updated on every push; the full report is in the ci run summary.

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

cli-interface-change Changes dokimi's user-facing CLI surface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants