Skip to content

feat(opencode): add per-session budget limit - #42202

Open
HHrddtu wants to merge 4 commits into
anomalyco:devfrom
HHrddtu:session-budget
Open

feat(opencode): add per-session budget limit#42202
HHrddtu wants to merge 4 commits into
anomalyco:devfrom
HHrddtu:session-budget

Conversation

@HHrddtu

@HHrddtu HHrddtu commented Aug 13, 2026

Copy link
Copy Markdown

Issue for this PR

No linked issue — new feature.

Type of change

  • New feature

What does this PR do?

Adds an optional per-session budget that stops the assistant when the session cost reaches it, plus a small widget in the TUI sidebar Context panel to view and set it.

  • budget field on the session schema (v1 + current), a nullable budget column on the session table, and a generated drizzle migration
  • PATCH /session/{id} accepts budget (a number sets it, null clears it); sessions can be created with a budget
  • Enforcement in the V1 prompt loop: after each step-finish the accumulated cost is compared against the budget; when crossed, a text part "Session budget reached. Increase the budget to continue." is appended and the loop stops. Re-prompting does not start a new provider turn until the budget is raised (guard at loop entry)
  • TUI: the Context panel shows $0.42 / $2.00 budget (red when exceeded, unlimited budget when unset). Mouse scroll over the row adjusts it in $0.25 steps (scrolling below $0.25 clears it), clicking switches to an input — Enter saves, Esc cancels
  • SDK types regenerated; the OpenAPI encoder drops the null arm of Schema.NullOr wrapped in Schema.optional, so the sdk build script now widens the update-side budget types to number | null

Known limitation: enforcement lives in the V1 prompt loop. The durable V2 runner does not compute cost yet (cost: 0 is hardcoded in Step.Ended), so budget stopping is a no-op there until V2 cost accounting lands.

Small extra: packages/core/script/migration.ts got a Windows fix — generatedMigrations split on / only, which broke migration generation on Windows. It was required to generate the migration for this PR.

How did you verify your code works?

  • bun typecheck in packages/schema, core, opencode, tui, sdk/js — all clean
  • bun test test/session in packages/opencode: 383 pass (new tests: budget stops the turn, exhausted budget exits the loop without an LLM request, budget round-trips through Session.Info)
  • new packages/tui/test/feature-plugins/sidebar-context.test.tsx: headless widget tests (render, wheel up/down, clear, click-to-edit, Enter save, Esc cancel) using the OpenTUI test renderer with mock mouse/keys
  • migration generator --check passes
  • manually ran bun dev and exercised the widget with mouse and keyboard

Screenshots / recordings

Char frame from the headless test renderer (the Context panel of the session sidebar):

Context
0 tokens
0% used
$0.42 spent
$0.42 / $2.00 budget

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Add an optional budget to sessions that stops the assistant when the
session cost reaches it:

- budget field in session schemas, storage column, and database migration
- PATCH /session/:id accepts budget (null clears it), sessions can be
  created with a budget
- the prompt loop stops after a step-finish that crosses the budget and
  appends an explanatory text part; re-prompting does not start a new
  turn until the budget is raised
- TUI sidebar context panel shows spent/budget, adjusts the budget with
  mouse scroll (0.25 steps, scroll below zero clears it) and edits it by
  clicking and typing (Enter saves, Esc cancels)
- regenerated SDK types with a nullable budget on the session update
  request
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Aug 13, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Aug 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

1 participant