|
| 1 | +import * as analytics from '@codebuff/common/analytics' |
| 2 | +import { TEST_USER_ID } from '@codebuff/common/old-constants' |
| 3 | +import { createTestAgentRuntimeParams } from '@codebuff/common/testing/fixtures/agent-runtime' |
| 4 | +import { clearMockedModules } from '@codebuff/common/testing/mock-modules' |
| 5 | +import { |
| 6 | + createMockDbOperations, |
| 7 | + setupDbSpies, |
| 8 | +} from '@codebuff/common/testing/mocks/database' |
| 9 | +import { getInitialSessionState } from '@codebuff/common/types/session-state' |
| 10 | +import { promptSuccess } from '@codebuff/common/util/error' |
| 11 | +import { |
| 12 | + afterAll, |
| 13 | + afterEach, |
| 14 | + beforeEach, |
| 15 | + describe, |
| 16 | + expect, |
| 17 | + it, |
| 18 | + mock, |
| 19 | + spyOn, |
| 20 | +} from 'bun:test' |
| 21 | + |
| 22 | +import { loopAgentSteps } from '../run-agent-step' |
| 23 | +import { clearAgentGeneratorCache } from '../run-programmatic-step' |
| 24 | +import { createToolCallChunk, mockFileContext } from './test-utils' |
| 25 | + |
| 26 | +import type { AgentTemplate } from '../templates/types' |
| 27 | +import type { ProjectProfileReport } from '@codebuff/common/constants/project-profile' |
| 28 | +import type { DbSpies } from '@codebuff/common/testing/mocks/database' |
| 29 | + |
| 30 | +describe('project profile report in loopAgentSteps', () => { |
| 31 | + let dbSpies: DbSpies |
| 32 | + let runtimeImpl: any |
| 33 | + let requests: any[] |
| 34 | + let reports: ProjectProfileReport[] |
| 35 | + let events: string[] |
| 36 | + |
| 37 | + const template: AgentTemplate = { |
| 38 | + id: 'free-root', |
| 39 | + displayName: 'Free Root', |
| 40 | + spawnerPrompt: 'Testing', |
| 41 | + model: 'deepseek/deepseek-v4-flash', |
| 42 | + inputSchema: {}, |
| 43 | + outputMode: 'last_message', |
| 44 | + includeMessageHistory: true, |
| 45 | + inheritParentSystemPrompt: false, |
| 46 | + mcpServers: {}, |
| 47 | + toolNames: ['read_files', 'end_turn'], |
| 48 | + spawnableAgents: [], |
| 49 | + systemPrompt: 'Test system prompt', |
| 50 | + instructionsPrompt: '', |
| 51 | + stepPrompt: '', |
| 52 | + handleSteps: undefined, |
| 53 | + } satisfies AgentTemplate as AgentTemplate |
| 54 | + |
| 55 | + const runLoop = async (params: { costMode: string; due: boolean }) => { |
| 56 | + const { |
| 57 | + agentTemplate: _, |
| 58 | + localAgentTemplates: __, |
| 59 | + ...baseRuntimeParams |
| 60 | + } = createTestAgentRuntimeParams() |
| 61 | + let finished!: () => void |
| 62 | + const finishedPromise = new Promise<void>((resolve) => { |
| 63 | + finished = resolve |
| 64 | + }) |
| 65 | + runtimeImpl = { |
| 66 | + ...baseRuntimeParams, |
| 67 | + finishAgentRun: mock(async () => { |
| 68 | + events.push('finish') |
| 69 | + finished() |
| 70 | + }), |
| 71 | + getProjectProfile: mock(async () => ({ |
| 72 | + due: params.due, |
| 73 | + profile: { appKinds: ['Web app'], technologies: ['TypeScript'] }, |
| 74 | + })), |
| 75 | + reportProjectProfile: mock(async ({ report }: any) => { |
| 76 | + events.push('report') |
| 77 | + reports.push(report) |
| 78 | + return true |
| 79 | + }), |
| 80 | + } |
| 81 | + runtimeImpl.promptAiSdkStream = mock(async function* (request: any) { |
| 82 | + requests.push(request) |
| 83 | + if ( |
| 84 | + JSON.stringify(request.messages.at(-1)).includes( |
| 85 | + 'report_project_profile once', |
| 86 | + ) |
| 87 | + ) { |
| 88 | + yield { |
| 89 | + ...createToolCallChunk('report_project_profile', {}), |
| 90 | + input: { status: 'unchanged' }, |
| 91 | + } as ReturnType<typeof createToolCallChunk> |
| 92 | + return promptSuccess('report-message-id') |
| 93 | + } |
| 94 | + yield { type: 'text' as const, text: 'done' } |
| 95 | + yield createToolCallChunk('end_turn', {}) |
| 96 | + return promptSuccess('mock-message-id') |
| 97 | + }) |
| 98 | + |
| 99 | + const sessionState = getInitialSessionState(mockFileContext) |
| 100 | + const result = await loopAgentSteps({ |
| 101 | + ...runtimeImpl, |
| 102 | + agentType: template.id, |
| 103 | + localAgentTemplates: { [template.id]: template }, |
| 104 | + repoId: undefined, |
| 105 | + repoUrl: undefined, |
| 106 | + userInputId: 'test-user-input', |
| 107 | + agentState: { |
| 108 | + ...sessionState.mainAgentState, |
| 109 | + agentId: 'root-agent-id', |
| 110 | + messageHistory: [], |
| 111 | + output: undefined, |
| 112 | + stepsRemaining: 5, |
| 113 | + }, |
| 114 | + prompt: 'build the thing', |
| 115 | + spawnParams: undefined, |
| 116 | + fingerprintId: 'test-fingerprint', |
| 117 | + fileContext: mockFileContext, |
| 118 | + userId: TEST_USER_ID, |
| 119 | + clientSessionId: 'test-session', |
| 120 | + ancestorRunIds: [], |
| 121 | + costMode: params.costMode, |
| 122 | + onResponseChunk: () => {}, |
| 123 | + signal: new AbortController().signal, |
| 124 | + } as any) |
| 125 | + await finishedPromise |
| 126 | + return result |
| 127 | + } |
| 128 | + |
| 129 | + beforeEach(() => { |
| 130 | + requests = [] |
| 131 | + reports = [] |
| 132 | + events = [] |
| 133 | + dbSpies = setupDbSpies(createMockDbOperations()) |
| 134 | + spyOn(analytics, 'trackEvent').mockImplementation(() => {}) |
| 135 | + }) |
| 136 | + |
| 137 | + afterEach(() => { |
| 138 | + if (runtimeImpl) clearAgentGeneratorCache(runtimeImpl) |
| 139 | + dbSpies.restore() |
| 140 | + mock.restore() |
| 141 | + }) |
| 142 | + |
| 143 | + afterAll(() => { |
| 144 | + clearMockedModules() |
| 145 | + }) |
| 146 | + |
| 147 | + it('offers the tool on every step and reports with the turn prefix before finishing the run', async () => { |
| 148 | + const result = await runLoop({ costMode: 'free', due: true }) |
| 149 | + |
| 150 | + expect(requests).toHaveLength(2) |
| 151 | + const [turn, report] = requests |
| 152 | + expect(Object.keys(turn.tools)).toContain('report_project_profile') |
| 153 | + expect(Object.keys(report.tools)).toEqual(Object.keys(turn.tools)) |
| 154 | + expect(report.messages.slice(0, turn.messages.length)).toEqual( |
| 155 | + turn.messages, |
| 156 | + ) |
| 157 | + expect(report.toolChoice).toBe('required') |
| 158 | + expect(report.extraCodebuffMetadata.llm_step_number).toBe('2') |
| 159 | + expect(reports).toEqual([{ changed: false }]) |
| 160 | + expect(events).toEqual(['report', 'finish']) |
| 161 | + expect(JSON.stringify(result.agentState.messageHistory)).not.toContain( |
| 162 | + 'report_project_profile once', |
| 163 | + ) |
| 164 | + }) |
| 165 | + |
| 166 | + it('makes no report request when the profile is fresh', async () => { |
| 167 | + await runLoop({ costMode: 'free', due: false }) |
| 168 | + expect(requests).toHaveLength(1) |
| 169 | + expect(reports).toEqual([]) |
| 170 | + expect(events).toEqual(['finish']) |
| 171 | + }) |
| 172 | + |
| 173 | + it('leaves paid runs untouched', async () => { |
| 174 | + await runLoop({ costMode: 'normal', due: true }) |
| 175 | + expect(requests).toHaveLength(1) |
| 176 | + expect(Object.keys(requests[0].tools)).not.toContain( |
| 177 | + 'report_project_profile', |
| 178 | + ) |
| 179 | + expect(runtimeImpl.getProjectProfile).not.toHaveBeenCalled() |
| 180 | + }) |
| 181 | +}) |
0 commit comments