Skip to content

Stop runs at a hard budget cap per thread and per automation - #1690

Merged
currentbits merged 4 commits into
mainfrom
coder/hard-budget-caps-per-thread-and-per-auto-b6ec9f
Oct 19, 2026
Merged

currentbits merged 4 commits into
mainfrom
coder/hard-budget-caps-per-thread-and-per-auto-b6ec9f

Conversation

@currentbits

Copy link
Copy Markdown
Owner

A thread or automation can now carry a hard cap on spend, tokens, turns or minutes per run. Hitting any of them stops the run and asks the user instead of continuing. Closes #286.

Changes

  • Cap on the thread, not in the transcript. thread.budget (usd, tokens, turns count lifetime usage; minutes is per run) lives on the thread row, so context compaction, reloads and session resets can't drop it.
  • Never silently continues. Every start is refused while the thread is over its cap: user turns, machine-delivered notices/inbound, quota-wait resumes and workflow runs. A 15 s watchdog sweep stops a live run that crosses a cap, posts "Budget cap reached: …", and flags the thread as needing the user.
  • Defaults flow in. Applying an agent profile copies the profile's cap onto the thread. An automation (from the form or solenta.json) stamps its cap on every thread it fires. Forks and orchestration workers inherit the cap with fresh counters, so fanning out doesn't get around it.
  • UI. A budget card in the thread (opened from the thread menu, or shown by itself while the thread is over its cap) with Save cap / Clear cap. The same cap fields are in the agent profile editor and the Automations form.

Verification

  • npm run test:electron in a CI-like env (inherited CODER_* vars and provider CLIs stripped): 4960 pass, 0 fail.
  • New electron/test/budget-caps.test.js (10 tests): the cap survives compaction and reload; profile, fork and automation stamping; solenta.json parsing; a real fake-claude run refused after crossing a $ cap until the cap is raised; a hung run stopped by the minutes cap.
  • New test/budgetCard.test.tsx, plus budget cases in automationsView and settingsModal. Those files and the ThreadView/plan card/find files pass. I didn't rerun the whole renderer suite after fixing a duplicate React key I had introduced (the fix is in this PR).
  • npm run typecheck (with the preload sync check) and npx vite build are clean.
  • Not tested: clicking through the real app.

Notes for reviewers

  • usd moves only when a provider reports usage. Claude reports once per turn, so a single long turn can overshoot a $ or token cap; minutes is the hard stop partway through a turn. tokens is input + output as the ledger records them, which excludes Claude's cached input. Live per-message metering is a follow-up.
  • solenta.json: a row without a cap keeps its existing hash, so trusted files stay trusted. Adding or changing a cap needs a re-trust, on purpose.
  • Routing only fills a cap when the thread has none, so an automation's own cap beats a routed profile's. An explicit profile apply overwrites the thread's cap.
  • automation_create (the agents' MCP tool) doesn't take a budget yet; that's a follow-up.

claude-opus-5-5 via Claude Code

Willem and others added 2 commits October 19, 2026 10:05
thread.budget {usd,tokens,turns,minutes} is harness state on the thread
row, so compaction cannot drop it. Agent profiles and automations carry a
default stamped onto the thread; forks inherit it. The start gate refuses
an over-cap run; a 15 s watchdog sweep stops a run that crosses a cap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shared BudgetFields in the thread budget card (thread menu, and shown on
its own once over the cap), the agent profile editor and the automation
form. Workflow runs get the same start gate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 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-19T08:47:20.004660Z fabd36d PR opened
ℹ️ 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: fabd36d566

ℹ️ 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".

if (!entry || entry.stopping || budgetStopping.has(threadId)) continue;
const thread = store.getThread(threadId);
if (!thread || !thread.budget || thread.memoryConsolidate === true) continue;
const why = budgetBreach(thread, store.getUsage(threadId), now);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use in-flight workflow usage for cap enforcement

When the active entry is a workflow, completed agents accumulate spend and tokens in entry.runUsage, while store.getUsage(threadId) is not updated until finishSuccess in electron/workflow.js. Consequently, a workflow whose first agent exceeds its USD or token cap continues launching later agents and phases, and stopped or failed workflow usage may never affect the next-start gate. Include the workflow's in-flight usage in this check and persist billed usage on every terminal path.

Useful? React with 👍 / 👎.

setDraft(budgetToDraft(thread.budget));
setError(null);
}, [thread.id, budgetKey]);
if (!onSetBudget || (!open && !reached)) return null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Show minute-cap stops in the budget card

When a minutes-only cap fires, stopRun clears runStartedAt, and budgetReached intentionally checks only lifetime USD/token/turn usage, so reached is false here. Because open is also false for an automatic stop, the watchdog marks the thread awaitingInput and instructs the user to raise or clear the cap, but the card containing those actions remains hidden; preserve a budget-stop reason/state and use it to display the card after minute-cap stops.

Useful? React with 👍 / 👎.

@currentbits
currentbits merged commit cfea8c8 into main Oct 19, 2026
6 checks 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.

Hard budget caps per thread and per automation

1 participant