Files
Agent-System-Backend/internal/runtime/material_final_reserve.go
T
weichen 373f8a1141 Reserve material subagent budget for final answers
Why: Material runs could spend the remaining budget on checkpoints and leave no final response.

Evidence: go test ./... -count=1; go test -race ./internal/runtime -run TestMaterialFinalReserve -count=1; offline replay of the prior A01 usage boundary.

Impact: Material subagents enter final-only mode or stop before another provider request; ordinary subagents retain their existing behavior.

Refs: U3 quality revision r0.2, Task 4
2026-10-13 10:13:48 +00:00

32 lines
995 B
Go

package runtime
// materialBudgetState describes only usage returned by the provider for a
// material subagent. A response with no reported usage cannot authorize a
// further request.
type materialBudgetState struct {
Limit, Used, LastPrompt, OutputCap, SavedCheckpoints, Responses int
UsageKnown bool
}
// materialFinalDecision reserves room for an answer before allowing another
// checkpoint request. It is deliberately an estimate, not a billing limit for
// an already in-flight request.
func materialFinalDecision(s materialBudgetState) (finalOnly, stop bool) {
if s.Responses == 0 {
return false, false
}
if !s.UsageKnown {
return false, true
}
margin := s.LastPrompt / 4
if margin < 1024 {
margin = 1024
}
remaining := s.Limit - s.Used
if remaining <= s.LastPrompt+margin {
return false, true
}
reserve := s.LastPrompt + s.OutputCap + margin
return s.SavedCheckpoints >= 2 || remaining <= 2*reserve, false
}