-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathcost-aggregation.integration.test.ts
More file actions
522 lines (466 loc) · 16.5 KB
/
cost-aggregation.integration.test.ts
File metadata and controls
522 lines (466 loc) · 16.5 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import { TEST_USER_ID } from '@codebuff/common/old-constants'
import { TEST_AGENT_RUNTIME_IMPL } from '@codebuff/common/testing/impl/agent-runtime'
import { getInitialSessionState } from '@codebuff/common/types/session-state'
import {
spyOn,
beforeEach,
afterEach,
describe,
expect,
it,
mock,
} from 'bun:test'
import * as messageCostTracker from '../llm-apis/message-cost-tracker'
import { mainPrompt } from '../main-prompt'
import * as agentRegistry from '../templates/agent-registry'
import * as websocketAction from '../websockets/websocket-action'
import type { AgentTemplate } from '../templates/types'
import type { AgentRuntimeDeps } from '@codebuff/common/types/contracts/agent-runtime'
import type { ProjectFileContext } from '@codebuff/common/util/file'
import type { WebSocket } from 'ws'
const mockFileContext: ProjectFileContext = {
projectRoot: '/test',
cwd: '/test',
fileTree: [],
fileTokenScores: {},
knowledgeFiles: {},
gitChanges: {
status: '',
diff: '',
diffCached: '',
lastCommitMessages: '',
},
changesSinceLastChat: {},
shellConfigFiles: {},
agentTemplates: {
base: {
id: 'base',
displayName: 'Base Agent',
outputMode: 'last_message',
inputSchema: {},
spawnerPrompt: '',
model: 'gpt-4o-mini',
includeMessageHistory: false,
inheritParentSystemPrompt: false,
toolNames: ['spawn_agents'],
spawnableAgents: ['editor'],
systemPrompt: 'Base agent system prompt',
instructionsPrompt: 'Base agent instructions',
stepPrompt: 'Base agent step prompt',
},
editor: {
id: 'editor',
displayName: 'Editor Agent',
outputMode: 'last_message',
inputSchema: {},
spawnerPrompt: '',
model: 'gpt-4o-mini',
includeMessageHistory: true,
inheritParentSystemPrompt: false,
toolNames: ['write_file'],
spawnableAgents: [],
systemPrompt: '',
instructionsPrompt: 'Editor agent instructions',
stepPrompt: 'Editor agent step prompt',
},
},
customToolDefinitions: {},
systemInfo: {
platform: 'test',
shell: 'test',
nodeVersion: 'test',
arch: 'test',
homedir: '/home/test',
cpus: 1,
},
}
class MockWebSocket {
sentActions: any[] = []
send(msg: string) {
// Capture sent messages for verification
try {
const parsed = JSON.parse(msg)
if (parsed.type === 'action') {
this.sentActions.push(parsed.data)
}
} catch {}
}
close() {}
on(event: string, listener: (...args: any[]) => void) {}
removeListener(event: string, listener: (...args: any[]) => void) {}
}
describe('Cost Aggregation Integration Tests', () => {
let mockLocalAgentTemplates: Record<string, any>
let mockWebSocket: MockWebSocket
let agentRuntimeImpl: AgentRuntimeDeps
beforeEach(async () => {
agentRuntimeImpl = { ...TEST_AGENT_RUNTIME_IMPL }
mockWebSocket = new MockWebSocket()
// Setup mock agent templates
mockLocalAgentTemplates = {
base: {
id: 'base',
displayName: 'Base Agent',
outputMode: 'last_message',
inputSchema: {},
spawnerPrompt: '',
model: 'gpt-4o-mini',
includeMessageHistory: false,
inheritParentSystemPrompt: false,
mcpServers: {},
toolNames: ['spawn_agents'],
spawnableAgents: ['editor'],
systemPrompt: 'Base agent system prompt',
instructionsPrompt: 'Base agent instructions',
stepPrompt: 'Base agent step prompt',
} satisfies AgentTemplate,
editor: {
id: 'editor',
displayName: 'Editor Agent',
outputMode: 'last_message',
inputSchema: {},
spawnerPrompt: '',
model: 'gpt-4o-mini',
includeMessageHistory: true,
inheritParentSystemPrompt: false,
mcpServers: {},
toolNames: ['write_file'],
spawnableAgents: [],
systemPrompt: '',
instructionsPrompt: 'Editor agent instructions',
stepPrompt: 'Editor agent step prompt',
} satisfies AgentTemplate,
}
// Mock cost tracking to return 0 so only onCostCalculated contributes
spyOn(messageCostTracker, 'saveMessage').mockImplementation(
async (value) => {
// Return 0 so we can control costs only via onCostCalculated
return 0
},
)
// Mock LLM streaming
let callCount = 0
const creditHistory: number[] = []
agentRuntimeImpl.promptAiSdkStream = async function* (options) {
callCount++
const credits = callCount === 1 ? 10 : 7 // Main agent vs subagent costs
creditHistory.push(credits)
if (options.onCostCalculated) {
await options.onCostCalculated(credits)
}
// Simulate different responses based on call
if (callCount === 1) {
// Main agent spawns a subagent
yield {
type: 'text' as const,
text: '<codebuff_tool_call>\n{"cb_tool_name": "spawn_agents", "agents": [{"agent_type": "editor", "prompt": "Write a simple hello world file"}]}\n</codebuff_tool_call>',
}
} else {
// Subagent writes a file
yield {
type: 'text' as const,
text: '<codebuff_tool_call>\n{"cb_tool_name": "write_file", "path": "hello.txt", "instructions": "Create hello world file", "content": "Hello, World!"}\n</codebuff_tool_call>',
}
}
return 'mock-message-id'
}
// Mock tool call execution
spyOn(websocketAction, 'requestToolCall').mockImplementation(
async (ws, userInputId, toolName, input) => {
if (toolName === 'write_file') {
return {
output: [
{
type: 'json',
value: {
message: `File ${input.path} created successfully`,
},
},
],
}
}
return {
output: [
{
type: 'json',
value: {
message: 'Tool executed successfully',
},
},
],
}
},
)
// Mock file reading
spyOn(websocketAction, 'requestFiles').mockImplementation(
async (params: { ws: any; filePaths: string[] }) => {
const results: Record<string, string | null> = {}
params.filePaths.forEach((path) => {
results[path] = path === 'hello.txt' ? 'Hello, World!' : null
})
return results
},
)
// Mock live user input checking
const liveUserInputs = await import('../live-user-inputs')
spyOn(liveUserInputs, 'checkLiveUserInput').mockImplementation(() => true)
// Mock getAgentTemplate to return our mock templates
spyOn(agentRegistry, 'getAgentTemplate').mockImplementation(
async ({ agentId, localAgentTemplates }) => {
return localAgentTemplates[agentId] || null
},
)
})
afterEach(() => {
mock.restore()
})
it('should correctly aggregate costs across the entire main prompt flow', async () => {
const sessionState = getInitialSessionState(mockFileContext)
// Set the main agent to use the 'base' type which is defined in our mock templates
sessionState.mainAgentState.stepsRemaining = 10
sessionState.mainAgentState.agentType = 'base'
const action = {
type: 'prompt' as const,
prompt: 'Create a hello world file using a subagent',
sessionState,
fingerprintId: 'test-fingerprint',
costMode: 'normal' as const,
promptId: 'test-prompt',
toolResults: [],
}
const result = await mainPrompt({
...agentRuntimeImpl,
ws: mockWebSocket as unknown as WebSocket,
action,
userId: TEST_USER_ID,
clientSessionId: 'test-session',
onResponseChunk: () => {},
localAgentTemplates: mockLocalAgentTemplates,
})
// Verify the total cost includes both main agent and subagent costs
const finalCreditsUsed = result.sessionState.mainAgentState.creditsUsed
// The actual cost is higher than expected due to multiple steps in agent execution
expect(finalCreditsUsed).toEqual(73)
// Verify the cost breakdown makes sense
expect(finalCreditsUsed).toBeGreaterThan(0)
expect(Number.isInteger(finalCreditsUsed)).toBe(true)
})
it('should include final cost in prompt response message', async () => {
const sessionState = getInitialSessionState(mockFileContext)
sessionState.mainAgentState.agentType = 'base'
const action = {
type: 'prompt' as const,
prompt: 'Simple task',
sessionState,
fingerprintId: 'test-fingerprint',
costMode: 'normal' as const,
promptId: 'test-prompt',
toolResults: [],
}
// Call through websocket action handler to test full integration
await websocketAction.callMainPrompt({
...agentRuntimeImpl,
ws: mockWebSocket as unknown as WebSocket,
action,
userId: TEST_USER_ID,
promptId: 'test-prompt',
clientSessionId: 'test-session',
})
// Verify final cost is included in prompt response
const promptResponse = mockWebSocket.sentActions.find(
(action) => action.type === 'prompt-response',
)
expect(promptResponse).toBeDefined()
expect(promptResponse.promptId).toBe('test-prompt')
expect(
promptResponse.sessionState.mainAgentState.creditsUsed,
).toBeGreaterThan(0)
})
it('should handle multi-level subagent hierarchies correctly', async () => {
// Mock a more complex scenario with nested subagents
let callCount = 0
agentRuntimeImpl.promptAiSdkStream = async function* (options) {
callCount++
if (options.onCostCalculated) {
await options.onCostCalculated(5) // Each call costs 5 credits
}
if (callCount === 1) {
// Main agent spawns first-level subagent
yield {
type: 'text' as const,
text: '<codebuff_tool_call>\n{"cb_tool_name": "spawn_agents", "agents": [{"agent_type": "editor", "prompt": "Create files"}]}\n</codebuff_tool_call>',
}
} else if (callCount === 2) {
// First-level subagent spawns second-level subagent
yield {
type: 'text' as const,
text: '<codebuff_tool_call>\n{"cb_tool_name": "spawn_agents", "agents": [{"agent_type": "editor", "prompt": "Write specific file"}]}\n</codebuff_tool_call>',
}
} else {
// Second-level subagent does actual work
yield {
type: 'text' as const,
text: '<codebuff_tool_call>\n{"cb_tool_name": "write_file", "path": "nested.txt", "instructions": "Create nested file", "content": "Nested content"}\n</codebuff_tool_call>',
}
}
return 'mock-message-id'
}
const sessionState = getInitialSessionState(mockFileContext)
sessionState.mainAgentState.stepsRemaining = 10
sessionState.mainAgentState.agentType = 'base'
const action = {
type: 'prompt' as const,
prompt: 'Create a complex nested structure',
sessionState,
fingerprintId: 'test-fingerprint',
costMode: 'normal' as const,
promptId: 'test-prompt',
toolResults: [],
}
const result = await mainPrompt({
...agentRuntimeImpl,
ws: mockWebSocket as unknown as WebSocket,
action,
userId: TEST_USER_ID,
clientSessionId: 'test-session',
onResponseChunk: () => {},
localAgentTemplates: mockLocalAgentTemplates,
})
// Should aggregate costs from all levels: main + sub1 + sub2
const finalCreditsUsed = result.sessionState.mainAgentState.creditsUsed
// Multi-level agents should have higher costs than simple ones
expect(finalCreditsUsed).toEqual(50)
})
it('should maintain cost integrity when subagents fail', async () => {
// Mock scenario where subagent fails after incurring partial costs
let callCount = 0
agentRuntimeImpl.promptAiSdkStream = async function* (options) {
callCount++
if (options.onCostCalculated) {
await options.onCostCalculated(6) // Each call costs 6 credits
}
if (callCount === 1) {
// Main agent spawns subagent
yield {
type: 'text' as const,
text: '<codebuff_tool_call>\n{"cb_tool_name": "spawn_agents", "agents": [{"agent_type": "editor", "prompt": "This will fail"}]}\n</codebuff_tool_call>',
}
} else {
// Subagent fails after incurring cost
yield { type: 'text' as const, text: 'Some response' }
throw new Error('Subagent execution failed')
}
return 'mock-message-id'
}
const sessionState = getInitialSessionState(mockFileContext)
sessionState.mainAgentState.agentType = 'base'
const action = {
type: 'prompt' as const,
prompt: 'Task that will partially fail',
sessionState,
fingerprintId: 'test-fingerprint',
costMode: 'normal' as const,
promptId: 'test-prompt',
toolResults: [],
}
let result
try {
result = await mainPrompt({
...agentRuntimeImpl,
ws: mockWebSocket as unknown as WebSocket,
action,
userId: TEST_USER_ID,
clientSessionId: 'test-session',
onResponseChunk: () => {},
localAgentTemplates: mockLocalAgentTemplates,
})
} catch (error) {
// Expected to fail, but costs may still be tracked
}
// Check costs - they should be captured even if execution fails
const finalCreditsUsed = result
? result.sessionState.mainAgentState.creditsUsed
: sessionState.mainAgentState.creditsUsed
// Even if the test fails, some cost should be incurred by the main agent
expect(finalCreditsUsed).toBeGreaterThanOrEqual(0) // At minimum, no negative costs
})
it('should not double-count costs in complex scenarios', async () => {
// Track all saveMessage calls to ensure no duplication
const saveMessageCalls: any[] = []
spyOn(messageCostTracker, 'saveMessage').mockImplementation(
async (value) => {
saveMessageCalls.push({
messageId: value.messageId,
model: value.model,
inputTokens: value.inputTokens,
outputTokens: value.outputTokens,
})
return 8 // Each LLM call costs 8 credits
},
)
const sessionState = getInitialSessionState(mockFileContext)
sessionState.mainAgentState.agentType = 'base'
const action = {
type: 'prompt' as const,
prompt: 'Complex multi-agent task',
sessionState,
fingerprintId: 'test-fingerprint',
costMode: 'normal' as const,
promptId: 'test-prompt',
toolResults: [],
}
await mainPrompt({
...agentRuntimeImpl,
ws: mockWebSocket as unknown as WebSocket,
action,
userId: TEST_USER_ID,
clientSessionId: 'test-session',
onResponseChunk: () => {},
localAgentTemplates: mockLocalAgentTemplates,
})
// Verify no duplicate message IDs (no double-counting)
const messageIds = saveMessageCalls.map((call) => call.messageId)
const uniqueMessageIds = new Set(messageIds)
expect(messageIds.length).toBe(uniqueMessageIds.size)
// Verify that costs are reasonable (not zero, not extremely high)
const finalCreditsUsed = sessionState.mainAgentState.creditsUsed
// Since we're using the websocket callMainPrompt which resets credits to 0, costs will be 0
// This test verifies that the credit reset mechanism works as expected
expect(finalCreditsUsed).toBe(0)
})
it('should respect server-side state authority', async () => {
const sessionState = getInitialSessionState(mockFileContext)
sessionState.mainAgentState.agentType = 'base'
// Simulate malicious client sending manipulated creditsUsed
sessionState.mainAgentState.creditsUsed = 999999
const action = {
type: 'prompt' as const,
prompt: 'Simple task',
sessionState,
fingerprintId: 'test-fingerprint',
costMode: 'normal' as const,
promptId: 'test-prompt',
toolResults: [],
}
// Call through websocket action to test server-side reset
await websocketAction.callMainPrompt({
...agentRuntimeImpl,
ws: mockWebSocket as unknown as WebSocket,
action,
userId: TEST_USER_ID,
promptId: 'test-prompt',
clientSessionId: 'test-session',
})
// Server should have reset the malicious value and calculated correct cost
const promptResponse = mockWebSocket.sentActions.find(
(action) => action.type === 'prompt-response',
)
expect(promptResponse).toBeDefined()
expect(promptResponse.sessionState.mainAgentState.creditsUsed).toBeLessThan(
1000,
) // Reasonable value, not manipulated
expect(
promptResponse.sessionState.mainAgentState.creditsUsed,
).toBeGreaterThan(0) // But still tracked correctly
})
})