Skip to content

A turn may carry its own token cap (C6) - #118

Merged
lydakis merged 2 commits into
mainfrom
claude/project-thread-7oycm8
Oct 10, 2026
Merged

lydakis merged 2 commits into
mainfrom
claude/project-thread-7oycm8

Conversation

@lydakis

@lydakis lydakis commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Before: the only token cap was a bot's lifetime --budget-tokens. A scheduled agent's one runaway fire could spend all of it.

After: submit takes an optional budget_tokens for the turn it starts. run --turn-budget-tokens N sends it, and trigger add --turn-budget-tokens N sends it on every fire. A turn that reaches its cap fails loudly with turn_budget_exhausted, just as a bot past its own budget fails with budget_exhausted.

This is C6 in the shipping plan, using the recommended pick for decision #15 (yes).

How

  • Engine:
    • The cap is checked where the bot's budget already is: before each model call, before a retry, and when a summary is held beside a call.
    • Bot::exhausted() names the bot's own budget_exhausted first, since that cap outlasts the turn; otherwise it reports turn_budget_exhausted with the facts turn_budget_tokens and turn_tokens_used.
    • A summary held beside a call counts toward both caps.
  • Store:
    • The cap is stored on the turn: a new turns.budget_tokens column, added by a one-way migration from schema 47 to 48.
    • The engine computes the limit from the bot's count minus what the turn has already spent, so a turn resumed after parking counts what it spent before.
    • A resend that names a different cap is an idempotency_conflict.
    • turns lists each turn's cap.
  • Refusals:
    • A steer with a cap is refused with invalid_delivery, because it joins a turn that already has its own cap.
    • A cap of 0 is invalid_budget, as it is for create.
  • Protocol: goes to 11. A daemon from before this change would ignore the field and silently enforce no cap, so a mismatch has to be refused.

Defaults: no cap unless one is passed. The flag name --turn-budget-tokens keeps it apart from the creation setting --budget-tokens.

Trigger runner: the app crate doesn't build on Linux. The trigger change was checked with rustfmt --edition 2024 and by reading it, and its unit tests (argument round trip, parse_add) were extended. The new tests/test_trigger.py case runs only where the app builds, which is George's Mac.

Performance: daemon CPU to finish 64 two-round tool turns, median of 15 runs on the synthetic model:

Build Daemon CPU IQR
main 107.9 ms 95.4–111.1
this branch, uncapped 99.8 ms 94.7–105.9
this branch, capped 97.1 ms 93.1–110.2

All three are within noise. The check is in memory, and the turn's spent count is read in the existing context() row read. The migration is one ALTER TABLE ADD COLUMN, which changes only the schema and rewrites no rows.

Tests:

  • All passing: cargo test (agent-runtime and agent-client), clippy -D warnings, fmt, the Python suite (433 tests, 55 skipped), and the node tests (207).
  • New tests:
    • schema_47_turns_take_their_own_token_cap (migration, the limit after a reload, facts, resend conflict, listing).
    • test_a_turns_own_cap_stops_it_before_the_next_call (refusals, a tool still runs before the stop, the next turn starts from zero, a parked turn resumes, both caps at once).

🤖 Generated with Claude Code

submit takes an optional budget_tokens for the turn it starts, checked
before each model call where the bot's lifetime budget already is. A turn
past it fails with turn_budget_exhausted and the facts turn_budget_tokens
and turn_tokens_used. The cap is stored on the turn (migration 47 to 48),
so a resumed turn counts what it spent, a resend naming another cap is an
idempotency_conflict, and turns lists it. A steer with a cap is refused.

run --turn-budget-tokens N passes it, and trigger add
--turn-budget-tokens N makes every fire pass it. Protocol 11.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01L9TT37y1798majTjQbUtZW
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T07:32:10.562676Z 8602497 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d315550d79

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/server/turn.rs Resolved
Comment thread src/server/mod.rs Resolved
A keep-warm refresh during a long tool or reply bills the turn, so the
refreshes now stop once they have billed what the bot's and turn's caps
leave, and none start when the call itself reached a cap. A turn cap
above i64::MAX is invalid_budget instead of wrapping in the store.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01L9TT37y1798majTjQbUtZW

lydakis commented Oct 10, 2026

Copy link
Copy Markdown
Owner Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

1 similar comment
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8602497e5d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/server/turn.rs Resolved
@lydakis
lydakis merged commit 986128a into main Oct 10, 2026
1 check passed
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