feat(agentloop): per-turn thinking/token budget enforcement (#375) - #406
Closed
Delqhi wants to merge 1 commit into
Closed
feat(agentloop): per-turn thinking/token budget enforcement (#375)#406Delqhi wants to merge 1 commit into
Delqhi wants to merge 1 commit into
Conversation
) Loop gains a per-turn budget tracker (perTurnBudget *PerTurnBudget) that records thinking + total tokens for every single LLM turn and returns ErrPerTurnBudgetExceeded when a non-zero cap is breached. The charge happens AFTER the provider response, then the loop: 1. fires hooks.BudgetExceeded with turn + caps + used so dashboards can render the precise per-trip breach 2. sets verifiedOnly = true so subsequent per-run caps (MaxTokens / ThinkingBudgetPerRequest) are skipped for the rest of this turn (issue #375) 3. returns the per-turn error to the caller so the post-mortem surfaces the cap as the failure cause The verify-gate (Gate.Run) still runs on the over-cap response because the response is appended to msgs before the error return (mandate M3: budget must NEVER bypass verify). New constants / config keys: hooks.BudgetExceeded = "budget.exceeded" SinCodeConfig.AgentLoopPerTurnBudget (agentloop.per_turn_budget) SinCodeConfig.AgentLoopPerTurnThinkingBudget (agentloop.per_turn_thinking_budget) loopbuilder.Config.PerTurnBudget loopbuilder.Config.PerTurnThinkingBudget Wired end-to-end through loopbuilder/builder.go: cfg zeros are filled from the merged SinCodeConfig exactly like the legacy ThinkingBudgetPerRequest. Loop.Run() lazy-constructs the tracker only when at least one per-turn cap is non-zero, so the no-cap path stays zero-cost (mandate M7 race-clean check). PerTurnBudget (in budget.go alongside the existing Budget struct): - PerTurnBudget.Reset() - zero per-turn, keep lifetime - PerTurnBudget.Charge(thinking, total) - always increments first - PerTurnBudget.PreFlight() - cut off BEFORE sending - PerTurnBudget.IsEnforced() - cheap guard for lazy paths - nil-safe (every public method survives a nil receiver) Tests (added to budget_test.go alongside existing Budget tests): - 8 unit tests on PerTurnBudget - 3 Loop.Run integration tests - All pass under -race -count=1 (mandate M7) - Full agentloop suite green (7.4s) Verified clean: go build ./... ok; go test ./... ok; race detector clean.
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🏆 CEO Audit — A+ (100.0/100)
📥 Download full report (Markdown)
|
🏆 CEO Audit — A+ (100.0/100)
📥 Download full report (Markdown) Run ID:
|
Collaborator
Author
|
Closing as duplicate. Issue #375 (Per-turn budget enforcement) was already shipped by a parallel agent on
|
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.
Per-turn budget enforcement with hooks.BudgetExceeded event. M3: budget-exceeded never bypasses verify gate.