diff --git a/backend/src/services/change-walkthroughs.ts b/backend/src/services/change-walkthroughs.ts index 55a651fb..4dc7d646 100644 --- a/backend/src/services/change-walkthroughs.ts +++ b/backend/src/services/change-walkthroughs.ts @@ -4,6 +4,7 @@ import { z } from 'zod' import { isSessionNotFoundError, type FileDiffInfo, + type ModelRef, type SessionInfo, } from '@opencode-manager/shared/opencode' import { @@ -31,6 +32,7 @@ import { readSessionChanges } from './session-changes' import type { SSEEvent } from './sse-aggregator' const DEFAULT_TIMEOUT_MS = 120_000 +const WALKTHROUGH_CALL_ATTEMPTS = 2 const HUNK_TRUNCATION_MARKER = '\n[hunk truncated]' const TEXT_TRUNCATION_MARKER = '' const REMAINING_STOP_TITLE = 'Remaining changes' @@ -497,28 +499,9 @@ export class ChangeWalkthroughService { onModelStart() - let responseText: string - try { - responseText = await generateTextWithTimeout( - this.openCodeClient, - { prompt, model: session.model }, - this.timeoutMs, - ) - } catch (error) { - if (error instanceof GenerateTextTimeoutError) { - throw new ChangeWalkthroughError('Generating the change walkthrough timed out', 504, { - code: 'WALKTHROUGH_TIMEOUT', - }) - } - throw new ChangeWalkthroughError(getErrorMessage(error) || 'Failed to generate the change walkthrough', 502) - } - - const parsed = parseWalkthroughResponse(responseText, hunks) - if (!parsed) { - throw new ChangeWalkthroughError('The model did not return a usable change walkthrough', 502, { - code: 'WALKTHROUGH_UNPARSEABLE', - }) - } + const parsed = await this.callModelParsed(prompt, session.model, (text) => + parseWalkthroughResponse(text, hunks), + ) const walkthrough: ChangeWalkthrough = { sessionId, @@ -538,6 +521,43 @@ export class ChangeWalkthroughService { return { walkthrough, created: true } } + private async callModelParsed( + prompt: string, + model: ModelRef | undefined, + parse: (text: string) => T | null, + ): Promise { + let lastError = new ChangeWalkthroughError('Failed to generate the change walkthrough', 502) + + for (let attempt = 0; attempt < WALKTHROUGH_CALL_ATTEMPTS; attempt += 1) { + let text: string + try { + text = await generateTextWithTimeout(this.openCodeClient, { prompt, model }, this.timeoutMs) + } catch (error) { + lastError = + error instanceof GenerateTextTimeoutError + ? new ChangeWalkthroughError('Generating the change walkthrough timed out', 504, { + code: 'WALKTHROUGH_TIMEOUT', + }) + : new ChangeWalkthroughError( + getErrorMessage(error) || 'Failed to generate the change walkthrough', + 502, + ) + continue + } + + const parsed = parse(text) + if (parsed !== null) { + return parsed + } + + lastError = new ChangeWalkthroughError('The model did not return a usable change walkthrough', 502, { + code: 'WALKTHROUGH_UNPARSEABLE', + }) + } + + throw lastError + } + private async readSession(sessionId: string): Promise { try { return await this.openCodeClient.api.session.get({ sessionID: sessionId }) diff --git a/backend/test/services/change-walkthroughs.test.ts b/backend/test/services/change-walkthroughs.test.ts index 15042695..6405348a 100644 --- a/backend/test/services/change-walkthroughs.test.ts +++ b/backend/test/services/change-walkthroughs.test.ts @@ -728,6 +728,7 @@ describe('ChangeWalkthroughService', () => { status: 502, code: 'WALKTHROUGH_UNPARSEABLE', }) + expect(fake.generateCalls).toHaveLength(2) }) it('rejects with 504 when generation times out', async () => { @@ -738,6 +739,7 @@ describe('ChangeWalkthroughService', () => { status: 504, code: 'WALKTHROUGH_TIMEOUT', }) + expect(fake.generateCalls).toHaveLength(2) }) it('rejects with 502 when the model call fails', async () => { @@ -746,6 +748,38 @@ describe('ChangeWalkthroughService', () => { }) await expect(service.generate(SESSION_ID, {})).rejects.toMatchObject({ status: 502 }) + expect(fake.generateCalls).toHaveLength(2) + }) + + it('retries an unparseable reply once', async () => { + let calls = 0 + fake.setGenerateImpl(async () => { + calls += 1 + return calls === 1 ? 'not json' : modelReply([{ title: 'A', explanation: 'x', hunkIds: THREE_HUNK_IDS }]) + }) + + const { walkthrough, created } = await service.generate(SESSION_ID, {}) + + expect(created).toBe(true) + expect(fake.generateCalls).toHaveLength(2) + expect(walkthrough.stops).toHaveLength(1) + }) + + it('retries a failed call once', async () => { + let calls = 0 + fake.setGenerateImpl(async () => { + calls += 1 + if (calls === 1) { + throw new Error('model unavailable') + } + return modelReply([{ title: 'A', explanation: 'x', hunkIds: THREE_HUNK_IDS }]) + }) + + const { walkthrough, created } = await service.generate(SESSION_ID, {}) + + expect(created).toBe(true) + expect(fake.generateCalls).toHaveLength(2) + expect(walkthrough.stops).toHaveLength(1) }) it('rejects with 502 when reading changes fails', async () => {