forked from chriswritescode-dev/opencode-forge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-budget.ts
More file actions
221 lines (190 loc) · 6.31 KB
/
agent-budget.ts
File metadata and controls
221 lines (190 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Per-agent runtime budget enforcement.
*
* Tracks turns, tool failures, requests, and tokens per session for each agent
* and enforces configurable limits with warning thresholds and stop policies.
*/
import type { AgentBudget, Logger } from '../types'
export type BudgetViolation = 'max_turns' | 'max_tool_failures' | 'max_requests' | 'max_tokens'
export type StopPolicy = 'warn' | 'stop' | 'warn_then_stop'
export interface BudgetState {
turns: number
toolFailures: number
requests: number
tokensUsed: number
}
export interface BudgetCheckResult {
allowed: boolean
violations: BudgetViolation[]
warnings: BudgetViolation[]
detail: string
}
/** Default warning threshold as a fraction of the limit. */
const WARNING_THRESHOLD = 0.8
export class AgentBudgetEnforcer {
private budgets = new Map<string, AgentBudget>()
private states = new Map<string, BudgetState>()
private logger: Logger
private stopPolicy: StopPolicy
constructor(logger: Logger, stopPolicy: StopPolicy = 'warn_then_stop') {
this.logger = logger
this.stopPolicy = stopPolicy
}
/**
* Register an agent's budget configuration.
*/
configure(agentName: string, budget: AgentBudget): void {
this.budgets.set(agentName, budget)
}
/**
* Get the current state for an agent in a session.
*/
getState(key: string): BudgetState {
return this.states.get(key) ?? { turns: 0, toolFailures: 0, requests: 0, tokensUsed: 0 }
}
/**
* Get the state for a specific agent + session pair.
*/
getStateFor(agentName: string, sessionId: string): BudgetState {
return this.getState(budgetKey(agentName, sessionId))
}
/**
* Get the configured budget for an agent (or null if unconfigured).
*/
getBudget(agentName: string): AgentBudget | null {
return this.budgets.get(agentName) ?? null
}
/**
* Record a turn for the agent.
*/
recordTurn(agentName: string, sessionId: string): BudgetCheckResult {
const key = budgetKey(agentName, sessionId)
const state = this.ensureState(key)
state.turns++
return this.check(agentName, key, state)
}
/**
* Record a tool failure.
*/
recordToolFailure(agentName: string, sessionId: string): BudgetCheckResult {
const key = budgetKey(agentName, sessionId)
const state = this.ensureState(key)
state.toolFailures++
return this.check(agentName, key, state)
}
/**
* Record a request.
*/
recordRequest(agentName: string, sessionId: string): BudgetCheckResult {
const key = budgetKey(agentName, sessionId)
const state = this.ensureState(key)
state.requests++
return this.check(agentName, key, state)
}
/**
* Record token usage.
*/
recordTokens(agentName: string, sessionId: string, tokens: number): BudgetCheckResult {
const key = budgetKey(agentName, sessionId)
const state = this.ensureState(key)
state.tokensUsed += tokens
return this.check(agentName, key, state)
}
/**
* Reset budget state for a session (e.g. on session reset / new turn).
*/
resetSession(agentName: string, sessionId: string): void {
this.states.delete(budgetKey(agentName, sessionId))
}
/**
* Reset per-turn counters (tool failures, requests) while keeping session-level ones.
*/
resetTurn(agentName: string, sessionId: string): void {
const key = budgetKey(agentName, sessionId)
const state = this.states.get(key)
if (state) {
state.toolFailures = 0
state.requests = 0
}
}
/**
* Check budget without recording anything.
*/
checkBudget(agentName: string, sessionId: string): BudgetCheckResult {
const key = budgetKey(agentName, sessionId)
const state = this.getState(key)
return this.check(agentName, key, state)
}
private ensureState(key: string): BudgetState {
let state = this.states.get(key)
if (!state) {
state = { turns: 0, toolFailures: 0, requests: 0, tokensUsed: 0 }
this.states.set(key, state)
}
return state
}
private check(agentName: string, _key: string, state: BudgetState): BudgetCheckResult {
const budget = this.budgets.get(agentName)
if (!budget) {
return { allowed: true, violations: [], warnings: [], detail: 'no budget configured' }
}
const violations: BudgetViolation[] = []
const warnings: BudgetViolation[] = []
if (budget.maxTurns !== undefined && budget.maxTurns > 0) {
if (state.turns >= budget.maxTurns) {
violations.push('max_turns')
} else if (state.turns >= budget.maxTurns * WARNING_THRESHOLD) {
warnings.push('max_turns')
}
}
if (budget.maxToolFailuresPerTurn !== undefined && budget.maxToolFailuresPerTurn > 0) {
if (state.toolFailures >= budget.maxToolFailuresPerTurn) {
violations.push('max_tool_failures')
} else if (state.toolFailures >= budget.maxToolFailuresPerTurn * WARNING_THRESHOLD) {
warnings.push('max_tool_failures')
}
}
if (budget.maxRequestsPerTurn !== undefined && budget.maxRequestsPerTurn > 0) {
if (state.requests >= budget.maxRequestsPerTurn) {
violations.push('max_requests')
} else if (state.requests >= budget.maxRequestsPerTurn * WARNING_THRESHOLD) {
warnings.push('max_requests')
}
}
if (budget.maxTokensPerSession !== undefined && budget.maxTokensPerSession > 0) {
if (state.tokensUsed >= budget.maxTokensPerSession) {
violations.push('max_tokens')
} else if (state.tokensUsed >= budget.maxTokensPerSession * WARNING_THRESHOLD) {
warnings.push('max_tokens')
}
}
for (const w of warnings) {
this.logger.log(`[budget] warning: agent ${agentName} approaching ${w} limit`)
}
const allowed = this.resolveAllowed(violations, agentName)
const detail =
violations.length > 0
? `budget exceeded: ${violations.join(', ')}`
: warnings.length > 0
? `approaching limits: ${warnings.join(', ')}`
: 'within budget'
return { allowed, violations, warnings, detail }
}
private resolveAllowed(violations: BudgetViolation[], agentName: string): boolean {
if (violations.length === 0) return true
switch (this.stopPolicy) {
case 'warn':
this.logger.log(`[budget] agent ${agentName} exceeded budget but policy=warn, continuing`)
return true
case 'stop':
this.logger.log(`[budget] agent ${agentName} exceeded budget, policy=stop, blocking`)
return false
case 'warn_then_stop':
this.logger.log(`[budget] agent ${agentName} exceeded budget, policy=warn_then_stop, blocking`)
return false
}
}
}
function budgetKey(agentName: string, sessionId: string): string {
return `${agentName}:${sessionId}`
}