|
1 | 1 | import * as analytics from '@codebuff/common/analytics' |
| 2 | +import { FREEBUFF_TURN_SPEND_LIMIT_MESSAGE } from '@codebuff/common/constants/freebuff-errors' |
2 | 3 | import { TEST_USER_ID } from '@codebuff/common/old-constants' |
3 | 4 | import { createTestAgentRuntimeParams } from '@codebuff/common/testing/fixtures/agent-runtime' |
4 | 5 | import { clearMockedModules } from '@codebuff/common/testing/mock-modules' |
@@ -157,6 +158,57 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () => |
157 | 158 | clearMockedModules() |
158 | 159 | }) |
159 | 160 |
|
| 161 | + it('retains completed steps after a spending cap and resumes under a new run id', async () => { |
| 162 | + mockTemplate.handleSteps = function* () { |
| 163 | + yield 'STEP' |
| 164 | + yield 'STEP' |
| 165 | + } |
| 166 | + let calls = 0 |
| 167 | + const capped = await loopAgentSteps({ |
| 168 | + ...loopAgentStepsBaseParams, |
| 169 | + startAgentRun: async () => 'capped-run', |
| 170 | + promptAiSdkStream: async function* () { |
| 171 | + if (++calls === 2) { |
| 172 | + throw new APICallError({ |
| 173 | + statusCode: 429, |
| 174 | + message: FREEBUFF_TURN_SPEND_LIMIT_MESSAGE, |
| 175 | + url: 'http://localhost/api/v1/chat/completions', |
| 176 | + requestBodyValues: {}, |
| 177 | + responseBody: JSON.stringify({ |
| 178 | + error: 'turn_spend_limit', |
| 179 | + message: FREEBUFF_TURN_SPEND_LIMIT_MESSAGE, |
| 180 | + }), |
| 181 | + isRetryable: false, |
| 182 | + }) |
| 183 | + } |
| 184 | + yield { type: 'text' as const, text: 'Completed research: result 42.' } |
| 185 | + yield createToolCallChunk('end_turn', {}) |
| 186 | + return promptSuccess('completed-step') |
| 187 | + }, |
| 188 | + }) |
| 189 | + expect(calls).toBe(2) |
| 190 | + expect(capped.output).toMatchObject({ |
| 191 | + type: 'error', |
| 192 | + error: 'turn_spend_limit', |
| 193 | + }) |
| 194 | + expect(JSON.stringify(capped.agentState.messageHistory)).toContain( |
| 195 | + 'Completed research: result 42.', |
| 196 | + ) |
| 197 | + expect(capped.agentState.runId).toBe('capped-run') |
| 198 | + |
| 199 | + const resumed = await loopAgentSteps({ |
| 200 | + ...loopAgentStepsBaseParams, |
| 201 | + agentState: capped.agentState, |
| 202 | + prompt: 'Continue', |
| 203 | + startAgentRun: async () => 'fresh-run', |
| 204 | + }) |
| 205 | + expect(resumed.output.type).not.toBe('error') |
| 206 | + expect(resumed.agentState.runId).toBe('fresh-run') |
| 207 | + const history = JSON.stringify(resumed.agentState.messageHistory) |
| 208 | + expect(history).toContain('Completed research: result 42.') |
| 209 | + expect(history).toContain('Continue') |
| 210 | + }) |
| 211 | + |
160 | 212 | it('an abort during agent-run registration is a cancel, not a failed run', async () => { |
161 | 213 | // registration ends on the run's signal now, so the null it returns under an abort is the |
162 | 214 | // abort itself and must read like every other cancel |
|
0 commit comments