-
Notifications
You must be signed in to change notification settings - Fork 17
fix: Resilience to Agent SDK cleanup burps #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f31b162
Resilience to Agent SDK cleanup burps
daniloc 7e63c8e
Merge origin/main into fix-agent-interface-race-condition
daniloc 2435848
refactor: extract completeWithSuccess helper to reduce duplication
daniloc be9f2f6
fix: prevent user's ANTHROPIC_API_KEY from overriding wizard OAuth token
daniloc da63be0
fix: use CLAUDE_CODE_OAUTH_TOKEN to override stored /login credentials
daniloc fa1a289
Merge branch 'main' into fix-agent-interface-race-condition
daniloc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import { runAgent } from '../agent-interface'; | ||
| import type { WizardOptions } from '../../utils/types'; | ||
|
|
||
| // Mock dependencies | ||
| jest.mock('../../utils/clack'); | ||
| jest.mock('../../utils/analytics'); | ||
| jest.mock('../../utils/debug'); | ||
|
|
||
| // Mock the SDK module | ||
| const mockQuery = jest.fn(); | ||
| jest.mock('@anthropic-ai/claude-agent-sdk', () => ({ | ||
| query: (...args: unknown[]) => mockQuery(...args), | ||
| })); | ||
|
|
||
| // Get mocked clack for spinner | ||
| import clack from '../../utils/clack'; | ||
| const mockClack = clack as jest.Mocked<typeof clack>; | ||
|
|
||
| describe('runAgent', () => { | ||
| let mockSpinner: { | ||
| start: jest.Mock; | ||
| stop: jest.Mock; | ||
| message: string; | ||
| }; | ||
|
|
||
| const defaultOptions: WizardOptions = { | ||
| debug: false, | ||
| installDir: '/test/dir', | ||
| forceInstall: false, | ||
| default: false, | ||
| signup: false, | ||
| localMcp: false, | ||
| ci: false, | ||
| }; | ||
|
|
||
| const defaultAgentConfig = { | ||
| workingDirectory: '/test/dir', | ||
| mcpServers: {}, | ||
| model: 'claude-opus-4-5-20251101', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| mockSpinner = { | ||
| start: jest.fn(), | ||
| stop: jest.fn(), | ||
| message: '', | ||
| }; | ||
|
|
||
| mockClack.spinner = jest.fn().mockReturnValue(mockSpinner); | ||
| mockClack.log = { | ||
| step: jest.fn(), | ||
| success: jest.fn(), | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| warning: jest.fn(), | ||
| info: jest.fn(), | ||
| message: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe('race condition handling', () => { | ||
| it('should return success when agent completes successfully then SDK cleanup fails', async () => { | ||
| // This simulates the race condition: | ||
| // 1. Agent completes with success result | ||
| // 2. signalDone() is called, completing the prompt generator | ||
| // 3. SDK tries to send cleanup command while streaming is active | ||
| // 4. SDK throws an error | ||
| // The fix should recognize we already got a success and return success anyway | ||
|
|
||
| function* mockGeneratorWithCleanupError() { | ||
| yield { | ||
| type: 'system', | ||
| subtype: 'init', | ||
| model: 'claude-opus-4-5-20251101', | ||
| tools: [], | ||
| mcp_servers: [], | ||
| }; | ||
|
|
||
| yield { | ||
| type: 'result', | ||
| subtype: 'success', | ||
| is_error: false, | ||
| result: 'Agent completed successfully', | ||
| }; | ||
|
|
||
| // Simulate the SDK cleanup error that occurs after success | ||
| throw new Error('only prompt commands are supported in streaming mode'); | ||
| } | ||
|
|
||
| mockQuery.mockReturnValue(mockGeneratorWithCleanupError()); | ||
|
|
||
| const result = await runAgent( | ||
| defaultAgentConfig, | ||
| 'test prompt', | ||
| defaultOptions, | ||
| mockSpinner as unknown as ReturnType<typeof clack.spinner>, | ||
| { | ||
| successMessage: 'Test success', | ||
| errorMessage: 'Test error', | ||
| }, | ||
| ); | ||
|
|
||
| // Should return success (empty object), not throw | ||
| expect(result).toEqual({}); | ||
| expect(mockSpinner.stop).toHaveBeenCalledWith('Test success'); | ||
| }); | ||
|
|
||
| it('should still throw when no success result was received before error', async () => { | ||
| // If we never got a success result, errors should propagate normally | ||
|
|
||
| function* mockGeneratorWithOnlyError() { | ||
| yield { | ||
| type: 'system', | ||
| subtype: 'init', | ||
| model: 'claude-opus-4-5-20251101', | ||
| tools: [], | ||
| mcp_servers: [], | ||
| }; | ||
|
|
||
| // No success result, just an error | ||
| throw new Error('Actual SDK error'); | ||
| } | ||
|
|
||
| mockQuery.mockReturnValue(mockGeneratorWithOnlyError()); | ||
|
|
||
| await expect( | ||
| runAgent( | ||
| defaultAgentConfig, | ||
| 'test prompt', | ||
| defaultOptions, | ||
| mockSpinner as unknown as ReturnType<typeof clack.spinner>, | ||
| { | ||
| successMessage: 'Test success', | ||
| errorMessage: 'Test error', | ||
| }, | ||
| ), | ||
| ).rejects.toThrow('Actual SDK error'); | ||
|
|
||
| expect(mockSpinner.stop).toHaveBeenCalledWith('Test error'); | ||
| }); | ||
|
|
||
| it('should not treat error results as success', async () => { | ||
| // A result with is_error: true should not count as success | ||
| // Even if subtype is 'success', the is_error flag takes precedence | ||
|
|
||
| function* mockGeneratorWithErrorResult() { | ||
| yield { | ||
| type: 'system', | ||
| subtype: 'init', | ||
| model: 'claude-opus-4-5-20251101', | ||
| tools: [], | ||
| mcp_servers: [], | ||
| }; | ||
|
|
||
| yield { | ||
| type: 'result', | ||
| subtype: 'success', // subtype can be success but is_error true | ||
| is_error: true, | ||
| result: 'API Error: 500 Internal Server Error', | ||
| }; | ||
|
|
||
| throw new Error('Process exited with code 1'); | ||
| } | ||
|
|
||
| mockQuery.mockReturnValue(mockGeneratorWithErrorResult()); | ||
|
|
||
| const result = await runAgent( | ||
| defaultAgentConfig, | ||
| 'test prompt', | ||
| defaultOptions, | ||
| mockSpinner as unknown as ReturnType<typeof clack.spinner>, | ||
| { | ||
| successMessage: 'Test success', | ||
| errorMessage: 'Test error', | ||
| }, | ||
| ); | ||
|
|
||
| // Should return API error, not success | ||
| expect(result.error).toBe('WIZARD_API_ERROR'); | ||
| expect(result.message).toContain('API Error'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.