Repository navigation
Hierarchical budgets: min(child, parent) with forced wrap-up (off by default) - #206
Merged
Merged
Conversation
… by default) Per-level token/turn budgets: task -> intent (worker run) -> planner round (counted inside the task). A child's effective budget is min(own budget, parent's remaining above the wrap-up reserve). When the task can no longer afford one worker, workers stop claiming intents and the task settles through one forced wrap-up planner round (the existing settle sequence, budget words instead of the timeout words). Config: ARTEX_TASK_BUDGET_TOKENS, ARTEX_TASK_BUDGET_TURNS, ARTEX_INTENT_BUDGET_TOKENS, ARTEX_INTENT_BUDGET_TURNS. 0 or unset = unlimited; with all four unset nothing changes.
…e_finding_report in wrap-up - resetSettledRun clears settling and the budget ledger; a done/failed task revived to running now plans and claims again (was gated by the old wrap-up). resetTimeoutRevival calls it. Test: TestResetSettledRunRestartsBudget. - docker-compose.yml passes the four ARTEX_*_BUDGET_* variables through. - .env.example and README document the variables, the wrap-up, the in-memory ledger and the practical floor. - llmrec: the usage sink runs even without a PostgreSQL recorder; comment fixed. - BudgetWrapupBlocks allows update_finding_report.
…er planner round only
wxcvm
referenced
this pull request
in wxcvm/ARTEX
Nov 14, 2026
…ration (#86) * feat(runtime): optional hierarchical budgets and knowledge binding migration Adapt upstream #206 to the fork's transition lock, deadline settlement and revival semantics. Default unlimited; retain existing tool bindings except legacy enabled defaults. * test(runtime): cover budget lifecycle and legacy binding migration
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.
Design
agent.EffectiveBudget(own, parentRemaining Budget) Budget(agent/budget.go), the per-dimension min.Budget{Tokens, Turns}, whereUnlimited(=math.MaxInt64) means no cap. A child getsmin(own budget, parent remaining above the wrap-up reserve).BudgetLedgerper task, inEngine.budgets.IntentMeter. Its effective budget ismin(per-intent budget, task remaining above the reserve), re-evaluated on every call.llmrec.WithUsageSink) with the samellm.Usagevalue it writes tollm_usage, at the same point, so the counters see the calls that get a row, and also calls with no recorder (the sink runs before the PostgreSQL check). Each metered call is one turn. Tokens =input + output. norma's normalizedInputTokensalready contains the cache read/write buckets, so those buckets are not added again and each token is counted once.server/engine.go: plannerLoop/runRound→Engine.budgetStop. It logs the remaining budget at every round. Once no worker is affordable, it starts the forced wrap-up exactly once (BudgetLedger.BeginWrapup) through the existing settle sequence:settleTask(ctx, t, budget=true).server/engine.go: workerLoop→Engine.budgetBlocksClaim. No new worker is dispatched (no intent claimed) while the budget does not allow one.server/engine.go: runIntent→Engine.budgetIntentmeters the run and caps it two ways:MaxTurnsis clamped to the run's effective remaining turns (Worker.turnCap), so the existing settlement wrap-up runs when the cap is hit.steerHooks.PreToolUserefuses every tool exceptrecord_fact/insert_assets/report_findingand returns a wrap-up instruction. The worker records its facts and ends with its summary; there is no hard cut.max(5% of the task budget, floor)withBudgetMarginPercent = 5,BudgetMarginMinTokens = 20_000,BudgetMarginMinTurns = 2. At or under the reserve, dispatch stops. The final planner round gets a code-owned wrap-up instruction appended to its situational block, the same way the endgametc.Finalwords are (plannerBudgetBlock): final goal judgment, no new intents, one-line summary.DefaultWorkerEstimateTokens = 50_000/DefaultWorkerEstimateTurns = 10. It is never above the per-intent cap.[Task budget] remaining: tokens 120000 of 500000, turns unlimited (wrap-up reserve: 25000 tokens, 0 turns). The same line is logged as[budget] task N planner round: ....Task run budget exhausted: forced wrap-up done, task stopped. The persisted task status reuses the existing terminal states:doneif the final judgment met the goals, otherwisetimeout. No new status value is added.ARTEX_TASK_BUDGET_TOKENS,ARTEX_TASK_BUDGET_TURNS,ARTEX_INTENT_BUDGET_TOKENS,ARTEX_INTENT_BUDGET_TURNS. The default is 0 or unset, which means unlimited. With all four unset, no ledger is created and every hook is a no-op: no behaviour change.What is NOT done
budget_exhaustedtask status. It would touch about 8 SQL status lists, revival and notifications, and the UI type. As a result, a budget-exhausted run that did not meet its goals shows astimeoutand fires the existing timeout notification.Tests
TestEffectiveBudgetNeverExceedsParent(agent): the calculator never exceeds the parent's or its own budget, including the unlimited and exhausted cases. Through the meter, a 100k intent budget is cut to the task's remaining above the reserve, and the workerMaxTurnsis clamped to the intent's turns.TestWrapupMarginTriggersOnceAndStopsDispatch(agent): the reserve is 5% or the floor. One token above the reserve, dispatch is on; at the reserve, dispatch stops. Repeated wake-ups start the wrap-up exactly once. A running worker is then held to its write-back tools. The planner wrap-up block contains the instruction exactly once, plus the remaining line.TestDispatchStopsUnderOneWorkerEstimate(agent): dispatch stops one token under the default estimate, and one token under the average of finished worker runs, while still above the reserve.TestBudgetUnsetIsPassthrough(agent): unset,0or invalid env gives no ledger. The planner round input is byte-identical with and without the budget path, and workerMaxTurnsis unchanged.TestEngineBudgetPassthroughAndGate(server): with the env unset, no round is stopped, no claim is blocked, contexts are returned untouched and no tool call is refused. With a task budget set, the claim is blocked below one worker's estimate and an exhausted run gets the wrap-up instruction whilerecord_factstays allowed.TestBudgetCountsMatchUsageFixture(agent): the token and turn counters match a fixture of usage rows (cache buckets counted once, an errored zero-usage row counted as a turn), along with the per-worker estimate and the remaining budget.TestUsageSinkSeesMeteredCalls(llmrec): the sink receives each streamed and non-streamed call once, with the same usage thellm_usagerow gets. A zero-token call with no model is not seen (no row, no sink call).Review fixes (commit 4cde1a8)
settlingflag and wrap-up ledger still gated the planner and the workers.resetSettledRunnow clearssettlingand the ledger;resetTimeoutRevivalcalls it, andserver/goals.gocalls it for anywasTerminalrevive. Test:TestResetSettledRunRestartsBudget.docker-compose.ymlpasses the four variables through (they were dropped by the explicitenvironmentlist)..env.exampleand README document them.llmrec: the usage sink runs before the PostgreSQL check, so it also sees calls with no recorder. The comment said otherwise; corrected.BudgetWrapupBlocksallowsupdate_finding_report, so a worker can amend a finding during the wrap-up.Config
ARTEX_TASK_BUDGET_TOKENS,ARTEX_TASK_BUDGET_TURNSARTEX_INTENT_BUDGET_TOKENS,ARTEX_INTENT_BUDGET_TURNSUnset or
0is unlimited. Practical floor: a worker is estimated at 50000 tokens / 10 turns, and the wrap-up reserve is max(5%, 20000 tokens) / 2 turns. Below 50000 tokens a task starts no worker; from 50000 to about 70000 it typically gets one worker, then the wrap-up. Budgets live in memory: a restart or a revive starts a fresh budget.Note on the test list above:
TestUsageSinkSeesMeteredCallscovers the zero-token, no-model filter (such calls skip both the sink and the row). It does not coverr.pg == nil.