ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
Restore a task spend cap from the lanes the live ledger counts.
protonmatterbe25f1497083
diff --git a/agent/budget.go b/agent/budget.goindex f22028e..8fe727e 100644--- a/agent/budget.go+++ b/agent/budget.go@@ -110,6 +110,19 @@ func NewBudgetLedger(task, intent Budget) *BudgetLedger {return &BudgetLedger{task: task, intent: intent}}+// RestoreSpend adds usage that was already recorded before this process started.+// It is applied once, to a ledger that has not recorded anything yet. A nil+// ledger (no budget configured) is a no-op.+func (l *BudgetLedger) RestoreSpend(spent Spend) {+ if l == nil || (spent.Tokens == 0 && spent.Turns == 0) {+ return+ }+ l.mu.Lock()+ l.spent.Tokens += spent.Tokens+ l.spent.Turns += spent.Turns+ l.mu.Unlock()+}+// Record meters one task-level call (the planner's own rounds).func (l *BudgetLedger) Record(u llm.Usage) {l.mu.Lock()diff --git a/agent/budget_test.go b/agent/budget_test.goindex b35f358..2c4c10a 100644--- a/agent/budget_test.go+++ b/agent/budget_test.go@@ -144,3 +144,14 @@ func TestBudgetCountsMatchUsageFixture(t *testing.T) {t.Fatalf("spent %+v remaining %+v estimate %+v, want {%d 5}, remaining, {10400 3}", st.Spent, st.Remaining, st.Estimate, want)}}++func TestRestoreSpendCountsTowardCap(t *testing.T) {+ l := NewBudgetLedger(NewBudget(100, 10), NewBudget(0, 0))+ l.RestoreSpend(Spend{Tokens: 40, Turns: 4})+ st := l.State()+ if st.Spent != (Spend{40, 4}) || st.Remaining != (Budget{60, 6}) {+ t.Fatalf("spent=%+v remaining=%+v", st.Spent, st.Remaining)+ }+ var none *BudgetLedger+ none.RestoreSpend(Spend{Tokens: 1, Turns: 1})+}diff --git a/db/llm_usage.go b/db/llm_usage.goindex a721aa4..5945974 100644--- a/db/llm_usage.go+++ b/db/llm_usage.go@@ -33,6 +33,24 @@ func (d *DB) EnsureLLMUsageTable() error {return d.execEmbeddedMigration(llmUsageMigration)}+// TaskLLMSpend is the token and turn total the in-memory task ledger would+// already have counted. One row is one turn. The ledger sees planner and worker+// completions, plus judge and compactor calls made on those contexts. Main-agent+// chat, goal decomposition, and side questions write llm_usage rows for the same+// task and never enter the ledger, so they are left out: counting them would+// tighten the turn cap after a restart. Cache read and write are not added:+// input_tokens already includes them. Every status counts, because a failed call+// is metered too.+func (d *DB) TaskLLMSpend(taskID string) (tokens, turns int64, err error) {+ if d == nil || taskID == "" {+ return 0, 0, nil+ }+ err = d.QueryRow(`SELECT COALESCE(SUM(input_tokens + output_tokens), 0), COUNT(*)+FROM llm_usage+WHERE task_id=$1 AND worker IN ('planner', 'worker', 'judge', 'compactor')`, taskID).Scan(&tokens, &turns)+ return tokens, turns, err+}+// InsertLLMUsage appends one metering row. Best-effort: callers log and continue on// error (a lost metering row must never break the LLM call).func (d *DB) InsertLLMUsage(u *LLMUsage) error {diff --git a/server/engine_budget.go b/server/engine_budget.goindex b6021b2..a11ab7a 100644--- a/server/engine_budget.go+++ b/server/engine_budget.go@@ -17,8 +17,33 @@ func (e *Engine) budgetFor(taskID string) *agent.BudgetLedger {if v, ok := e.budgets.Load(taskID); ok {return v.(*agent.BudgetLedger)}- v, _ := e.budgets.LoadOrStore(taskID, agent.NewBudgetLedger(agent.BudgetsFromEnv()))- return v.(*agent.BudgetLedger)+ created := agent.NewBudgetLedger(agent.BudgetsFromEnv())+ e.seedBudget(taskID, created)+ v, loaded := e.budgets.LoadOrStore(taskID, created)+ if loaded {+ return v.(*agent.BudgetLedger)+ }+ return created+}++// seedBudget rebuilds spent tokens and turns from llm_usage the first time a+// ledger is created in this process. The in-memory sink runs before the row is+// inserted, so a call that happens after this seed is counted once. A nil+// ledger means no budget is configured and nothing is read.+func (e *Engine) seedBudget(taskID string, ledger *agent.BudgetLedger) {+ if ledger == nil || e == nil || e.m == nil || e.m.pg == nil || taskID == "" {+ return+ }+ tokens, turns, err := e.m.pg.TaskLLMSpend(taskID)+ if err != nil {+ log.Printf("[budget] task %s: cannot restore spend from llm_usage: %v", taskID, err)+ return+ }+ if tokens == 0 && turns == 0 {+ return+ }+ ledger.RestoreSpend(agent.Spend{Tokens: tokens, Turns: turns})+ log.Printf("[budget] task %s restored %d tokens / %d turns from llm_usage", taskID, tokens, turns)}// budgetStop is the enforcement point of a planner round: it logs the remaining