@@ -24,9 +24,7 @@ import * as promptAgentStream from '../prompt-agent-stream'
2424import * as requestContext from '../websockets/request-context'
2525
2626import type { AgentTemplate , StepGenerator } from '../templates/types'
27- import type {
28- AgentState ,
29- } from '@codebuff/common/types/session-state'
27+ import type { AgentState } from '@codebuff/common/types/session-state'
3028import type { WebSocket } from 'ws'
3129
3230describe ( 'loopAgentSteps STEP behavior' , ( ) => {
@@ -151,7 +149,7 @@ describe('loopAgentSteps STEP behavior', () => {
151149 parentPrompt : 'Testing' ,
152150 model : 'claude-3-5-sonnet-20241022' ,
153151 inputSchema : { } ,
154- outputMode : 'json ' ,
152+ outputMode : 'structured_output ' ,
155153 includeMessageHistory : true ,
156154 toolNames : [ 'read_files' , 'write_file' , 'end_turn' ] ,
157155 subagents : [ ] ,
@@ -180,26 +178,29 @@ describe('loopAgentSteps STEP behavior', () => {
180178 clearAgentGeneratorCache ( )
181179 } )
182180
183- llmCallCount = 0 // Reset LLM call count
181+ llmCallCount = 0 // Reset LLM call count
184182 afterAll ( ( ) => {
185183 clearMockedModules ( )
186184 } )
187185
188186 it ( 'should verify correct STEP behavior - LLM called once after STEP' , async ( ) => {
189187 // This test verifies that programmatic agents don't call the LLM,
190188 // and that STEP yielding works correctly without LLM involvement
191-
189+
192190 let stepCount = 0
193191 const mockGenerator = ( function * ( ) {
194192 stepCount ++
195-
193+
196194 if ( stepCount === 1 ) {
197195 // First call: Execute a tool, then STEP
198196 yield { toolName : 'read_files' , args : { paths : [ 'file1.txt' ] } }
199197 yield 'STEP' // Should pause here
200198 } else if ( stepCount === 2 ) {
201199 // Second call: Should continue from here, not call LLM
202- yield { toolName : 'write_file' , args : { path : 'output.txt' , content : 'test' } }
200+ yield {
201+ toolName : 'write_file' ,
202+ args : { path : 'output.txt' , content : 'test' } ,
203+ }
203204 yield { toolName : 'end_turn' , args : { } }
204205 }
205206 } ) ( ) as StepGenerator
@@ -213,47 +214,55 @@ describe('loopAgentSteps STEP behavior', () => {
213214 // Mock checkLiveUserInput to return true for multiple iterations
214215 let checkCallCount = 0
215216 const mockCheckLiveUserInput = require ( '@codebuff/backend/live-user-inputs' )
216- spyOn ( mockCheckLiveUserInput , 'checkLiveUserInput' ) . mockImplementation ( ( ) => {
217- checkCallCount ++
218- // Allow enough iterations to see the bug
219- return checkCallCount <= 3
220- } )
217+ spyOn ( mockCheckLiveUserInput , 'checkLiveUserInput' ) . mockImplementation (
218+ ( ) => {
219+ checkCallCount ++
220+ // Allow enough iterations to see the bug
221+ return checkCallCount <= 3
222+ } ,
223+ )
221224
222- const result = await loopAgentSteps ( new MockWebSocket ( ) as unknown as WebSocket , {
223- userInputId : 'test-user-input' ,
224- agentType : 'test-agent' ,
225- agentState : mockAgentState ,
226- prompt : 'Test prompt' ,
227- params : undefined ,
228- fingerprintId : 'test-fingerprint' ,
229- fileContext : mockFileContext ,
230- toolResults : [ ] ,
231- localAgentTemplates,
232- userId : TEST_USER_ID ,
233- clientSessionId : 'test-session' ,
234- onResponseChunk : ( ) => { } ,
235- } )
225+ const result = await loopAgentSteps (
226+ new MockWebSocket ( ) as unknown as WebSocket ,
227+ {
228+ userInputId : 'test-user-input' ,
229+ agentType : 'test-agent' ,
230+ agentState : mockAgentState ,
231+ prompt : 'Test prompt' ,
232+ params : undefined ,
233+ fingerprintId : 'test-fingerprint' ,
234+ fileContext : mockFileContext ,
235+ toolResults : [ ] ,
236+ localAgentTemplates,
237+ userId : TEST_USER_ID ,
238+ clientSessionId : 'test-session' ,
239+ onResponseChunk : ( ) => { } ,
240+ } ,
241+ )
236242
237243 console . log ( `LLM calls made: ${ llmCallCount } ` )
238244 console . log ( `Step count: ${ stepCount } ` )
239-
245+
240246 // CORRECT BEHAVIOR: After STEP, LLM should be called once, then no more
241247 // The programmatic agent yields STEP, then LLM runs once
242248 expect ( llmCallCount ) . toBe ( 1 ) // LLM called once after STEP
243-
249+
244250 // The programmatic agent should have been called once (yielded STEP)
245251 expect ( stepCount ) . toBe ( 1 )
246-
252+
247253 // After STEP, the LLM should run once, then the loop should continue correctly
248254 } )
249255
250256 it ( 'should demonstrate correct behavior when programmatic agent completes without STEP' , async ( ) => {
251257 // This test shows that when a programmatic agent doesn't yield STEP,
252258 // it should complete without calling the LLM at all (since it ends with end_turn)
253-
259+
254260 const mockGenerator = ( function * ( ) {
255261 yield { toolName : 'read_files' , args : { paths : [ 'file1.txt' ] } }
256- yield { toolName : 'write_file' , args : { path : 'output.txt' , content : 'test' } }
262+ yield {
263+ toolName : 'write_file' ,
264+ args : { path : 'output.txt' , content : 'test' } ,
265+ }
257266 yield { toolName : 'end_turn' , args : { } }
258267 } ) ( ) as StepGenerator
259268
@@ -263,20 +272,23 @@ describe('loopAgentSteps STEP behavior', () => {
263272 'test-agent' : mockTemplate ,
264273 }
265274
266- const result = await loopAgentSteps ( new MockWebSocket ( ) as unknown as WebSocket , {
267- userInputId : 'test-user-input' ,
268- agentType : 'test-agent' ,
269- agentState : mockAgentState ,
270- prompt : 'Test prompt' ,
271- params : undefined ,
272- fingerprintId : 'test-fingerprint' ,
273- fileContext : mockFileContext ,
274- toolResults : [ ] ,
275- localAgentTemplates,
276- userId : TEST_USER_ID ,
277- clientSessionId : 'test-session' ,
278- onResponseChunk : ( ) => { } ,
279- } )
275+ const result = await loopAgentSteps (
276+ new MockWebSocket ( ) as unknown as WebSocket ,
277+ {
278+ userInputId : 'test-user-input' ,
279+ agentType : 'test-agent' ,
280+ agentState : mockAgentState ,
281+ prompt : 'Test prompt' ,
282+ params : undefined ,
283+ fingerprintId : 'test-fingerprint' ,
284+ fileContext : mockFileContext ,
285+ toolResults : [ ] ,
286+ localAgentTemplates,
287+ userId : TEST_USER_ID ,
288+ clientSessionId : 'test-session' ,
289+ onResponseChunk : ( ) => { } ,
290+ } ,
291+ )
280292
281293 // Should NOT call LLM since the programmatic agent ended with end_turn
282294 expect ( llmCallCount ) . toBe ( 0 )
0 commit comments