Repository navigation
feat(spend): paid guards slice 4 for eval triggers generate --rewrite #1670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
napetrov
wants to merge
1
commit into
intel:main
Choose a base branch
from
napetrov:feat/cli-paid-guards-4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+474
−54
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!-- | ||
| Copyright (C) 2026 Intel Corporation | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| --> | ||
|
|
||
| ### Added | ||
|
|
||
| - **`eval triggers generate --rewrite` can be priced before it spends.** `--dry-run` prices one rewrite call per question at list prices and prints the estimate. It builds no model client, calls no model and writes no set. Without `--rewrite` it makes no model call and says so. The estimate assumes 200 reply tokens per rewrite. | ||
|
|
||
| - **`eval triggers generate --rewrite --max-usd X`.** A real run exits 3 before its first call and builds no client if the estimate exceeds the cap or the model has no list price. A stop mid-run exits 1 and writes no set. `--max-usd` without `--rewrite` is an input error (exit 2). | ||
|
|
||
| ### Changed | ||
|
|
||
| - **`--max-usd` holds each call's estimate while the call is in flight.** A call's list-price estimate is reserved from the cap until the call finishes, so calls running together under `--concurrency` cannot pass the cap between them. A call that raises gives its reservation back. This applies to `eval questions analyze` and `eval questions review`; the other metered leaves keep check-then-record. | ||
|
|
||
| - **The cap refusal names only the flags the leaf has.** The refusal said `--tier` and `--max-atoms` on every leaf. Now `--tier` and `--max-atoms` are named only on the novelty leaves that take them; the other leaves get "measure less". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """The ``--rewrite`` calls of ``eval triggers generate``: priced under ``--dry-run``, capped by ``--max-usd``. | ||
|
|
||
| Part of #94 (F1b, spend: paid-guards slice 4). The command's flags and writes stay in | ||
| :mod:`dokimi.commands.catalog`; this module owns what a rewrite costs and when it may run. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import sys | ||
| from collections.abc import Callable | ||
| from functools import cache | ||
|
|
||
| from dokimi import spend_preview | ||
| from dokimi.exit_codes import EXIT_FAILURE, EXIT_INCONCLUSIVE | ||
| from dokimi.files import InputError | ||
| from dokimi.spend import SpendCap, SpendCapReached | ||
|
|
||
| # A rewrite's reply is one short first-person question: 200 tokens is the assumption the estimate prices. | ||
| REWRITE_REPLY_TOKENS = 200 | ||
|
|
||
|
|
||
| # The provider resolver of ``eval triggers generate`` (catalog's ``_trigger_rewrite_model``), passed in: this module | ||
| # is imported by the command module, so it cannot import that module back. | ||
| Resolver = Callable[..., tuple[str, str]] | ||
|
|
||
|
|
||
| def build_queries(args: argparse.Namespace, sources, catalog, specs, resolve: Resolver): | ||
| """The trigger set with its --rewrite calls priced and capped. A dry run exits 0 before any call is made. | ||
|
|
||
| A stop at the --max-usd cap exits 1 here: the set is written after the last rewrite, so no file is left. | ||
| """ | ||
| from dokimi.external import trigger_set as ts | ||
|
|
||
| check_rewrite_flags(args) | ||
| if args.rewrite and (args.dry_run or args.max_usd is not None): | ||
| rewrite_gate(args, sources, catalog, specs, resolve) | ||
| cap = SpendCap(args.max_usd) if args.max_usd is not None else None | ||
| rewrite = trigger_rewriter(args, ts.REWRITE_PROMPT, resolve, cap) | ||
| try: | ||
| return ts.build_trigger_set(sources, catalog, rewrite=rewrite, specs=specs) | ||
| except SpendCapReached as exc: | ||
| print(f"Error: the --max-usd cap was reached: {exc}", file=sys.stderr) | ||
| sys.exit(EXIT_FAILURE) | ||
|
|
||
|
|
||
| def check_rewrite_flags(args: argparse.Namespace) -> None: | ||
| """Refuse a bad combination of --rewrite flags before any call; a dry run without --rewrite exits 0 here.""" | ||
| if args.max_usd is not None and not args.rewrite: | ||
| raise InputError("--max-usd needs --rewrite: a trigger set makes no model call without it") | ||
| if args.dry_run and not args.rewrite: | ||
| print("Dry run: no --rewrite, so the trigger set makes no model call; nothing is written.") | ||
| sys.exit(0) | ||
| if args.rewrite and not args.model: | ||
| raise InputError("--rewrite needs --model") | ||
|
|
||
|
|
||
| def trigger_rewriter(args: argparse.Namespace, prompt: str, resolve: Resolver, | ||
| cap: SpendCap | None = None) -> Callable[[str], str] | None: | ||
| if not args.rewrite: | ||
| return None | ||
| from dokimi.llm import llm_call, llm_call_with_usage | ||
|
|
||
| @cache | ||
| def rewrite_model() -> tuple[str, str]: | ||
| return resolve(args.model, args.provider, named=args.rewrite_provider_named) | ||
|
|
||
| def rewrite(text: str) -> str: | ||
| model, provider = rewrite_model() | ||
| if cap is None: | ||
| return llm_call(prompt + text, model=model, provider=provider) | ||
| # Under --max-usd: the cap is checked before the call and the call's usage is counted after it. | ||
| cap.before_call(model) | ||
| reply, usage = llm_call_with_usage(prompt + text, model=model, provider=provider) | ||
| cap.record(usage, model) | ||
| return reply | ||
|
|
||
| return rewrite | ||
|
|
||
|
|
||
| def price_rewrites(args: argparse.Namespace, sources, catalog, specs, resolve: Resolver) -> str | None: | ||
| """Price the --rewrite calls before any is made: one per question. Returns the gate verdict (None: go ahead).""" | ||
| from dokimi.external import trigger_set as ts | ||
|
|
||
| # Client-free: labels and lint only, the rewrite step is left out. | ||
| queries = ts.build_trigger_set(sources, catalog, rewrite=None, specs=specs) | ||
| model, provider = resolve(args.model, args.provider, named=args.rewrite_provider_named) | ||
| prompts = [ts.REWRITE_PROMPT + q.text for q in queries] | ||
| return spend_preview.gate(lambda: prompts, model=model, provider=provider, reply_tokens=REWRITE_REPLY_TOKENS, | ||
| dry_run=args.dry_run, max_usd=args.max_usd, | ||
| mid_run_stop_note=f"a stop mid-run exits {EXIT_FAILURE} and writes no set") | ||
|
|
||
|
|
||
| def rewrite_gate(args: argparse.Namespace, sources, catalog, specs, resolve: Resolver) -> None: | ||
| """Under --dry-run or --max-usd: exit 0 after a dry run, exit 3 when the estimate is refused; otherwise return.""" | ||
| verdict = price_rewrites(args, sources, catalog, specs, resolve) | ||
| if verdict == spend_preview.REFUSED: | ||
| sys.exit(EXIT_INCONCLUSIVE) | ||
| if verdict == spend_preview.DRY: | ||
| sys.exit(0) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must fix (blocking): If
measure_lessreturnsNone, the tests still pass, so no test reads the hint a novelty leaf gets, and its cap refusal would end inor None.If I change the return at
dokimi/dokimi/novelty/cost.py:226toNone, the spend and novelty test files in the PR's list all still pass (131 tests).Suggestion: Add a test that runs a novelty leaf over its
--max-usdand asserts the refusal names the cost flags that leaf takes, the waytests/test_triggers_dry_run_and_cap.pychecks the trigger leaf.Details
A novelty leaf's cap refusal would then end with
or None. The PR's tests check the hint only for the trigger leaf and for a hint passed in directly, so nothing reads the novelty text. I'd expect a test to read a novelty leaf's hint, since that is the line a user sees when the cap is hit.