Skip to content

Restore task spend from the lanes the live ledger counts - #302

Merged
BaaRai merged 3 commits into
BaaRai:mainfrom
Protonmatter:protonmatter/budget-restore
Nov 17, 2026
Merged

BaaRai merged 3 commits into
BaaRai:mainfrom
Protonmatter:protonmatter/budget-restore

Conversation

@Protonmatter

Copy link
Copy Markdown
Contributor

Summary

  • Split from Keep control-plane state correct when a write or a restart fails #293.
  • The live task ledger counts each planner completion and each worker completion. It does not count planner rounds alone: IntentMeter.Record adds the worker call to the same task spend.
  • Restore counts llm_usage rows in the planner, worker, judge, and compactor lanes. Those calls reach the budget sink. Main-agent chat, goal decomposition, and side questions do not, so they no longer tighten the turn cap after a restart.

Test plan

  • TestRestoreSpendCountsTowardCap and TestBudgetCountsMatchUsageFixture.
  • Restart a task that has main-agent usage and confirm those rows are not added to the restored turn count.

Planner, worker, judge, and compactor rows rebuild the cap. Main-agent chat, goal decomposition, and side questions stay out, so a restart does not count turns the running process never metered.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for restoring spend from the database: a restart should not reset the cap.

1. Please add a test for TaskLLMSpend (db/llm_usage.go:44-51). An llm_usage row with worker='judge' and this task_id must be counted; a chat row for the same task must not. The query has no test today, and the Postgres-backed tests skip without a database.

2. Please pin down what the cap means after a restart, with a test. The restore counts the runs recorded before the restart against the cap. That fits a per-task budget (ARTEX_TASK_BUDGET_TOKENS, README.md:255 and 450), so the restore itself is fine. Two things need to be explicit:

  • A restored task that is already near the cap only gets the wrap-up margin. Please confirm this is intended, document it next to the budget env vars, and cover it with a test.
  • RestoreSpend only adds to spent (agent/budget.go:116-124). workerRuns and workerSpent stay at 0, so State() falls back to the default worker estimate (agent/budget.go:149-152) even when the task has real worker history. Should the restore also rebuild the per-worker averages? If not, please document that the estimate restarts from the defaults.

3. The comment on seedBudget is wrong. server/engine_budget.go:29-30 says the seed runs "the first time a ledger is created in this process". budgetFor seeds every new ledger (server/engine_budget.go:20-21), and ledgers are deleted and rebuilt inside the same process by resetSettledRun (server/engine_timeout.go:100-102, called on revive from server/goals.go:202) and by StopTask (server/engine.go:295). Please fix the comment, and add a Postgres-backed version of TestResetSettledRunRestartsBudget (server/engine_budget_test.go:66-68 expects a fresh ledger after a revive; without Postgres seedBudget returns early, so the test does not see the reseed).

… count.

A task already inside the wrap-up reserve only has that reserve left. The worker estimate starts from the defaults because usage rows have no run boundary.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, the README and the seedBudget comment now match the behaviour, and the Postgres test pins the revive case near the cap.

One line is still wrong: resetSettledRun (server/engine_timeout.go) says it clears the ledger "so a revived task can plan and claim again with a fresh budget", which the reseed contradicts. Say that the next ledger is seeded from llm_usage, as the README now does.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, the comment now says the next ledger is seeded from llm_usage. Approved.

@BaaRai
BaaRai merged commit 9bc5856 into BaaRai:main Nov 17, 2026
5 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.

2 participants