feat(opencode): add per-session budget limit - #42202
Open
HHrddtu wants to merge 4 commits into
Open
Conversation
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
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue for this PR
No linked issue — new feature.
Type of change
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.
budgetfield on the session schema (v1 + current), a nullablebudgetcolumn on the session table, and a generated drizzle migrationPATCH /session/{id}acceptsbudget(a number sets it,nullclears it); sessions can be created with a budgetstep-finishthe 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)$0.42 / $2.00 budget(red when exceeded,unlimited budgetwhen 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 cancelsSchema.NullOrwrapped inSchema.optional, so the sdk build script now widens the update-side budget types tonumber | nullKnown limitation: enforcement lives in the V1 prompt loop. The durable V2 runner does not compute cost yet (
cost: 0is hardcoded inStep.Ended), so budget stopping is a no-op there until V2 cost accounting lands.Small extra:
packages/core/script/migration.tsgot a Windows fix —generatedMigrationssplit 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 typecheckin packages/schema, core, opencode, tui, sdk/js — all cleanbun test test/sessionin 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)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--checkpassesbun devand exercised the widget with mouse and keyboardScreenshots / recordings
Char frame from the headless test renderer (the Context panel of the session sidebar):
Checklist