Repository navigation
fix: exit with error when agent job status is failed #296
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
Open
flcrom
wants to merge
6
commits into
firecrawl:main
Choose a base branch
from
flcrom:fix/agent-failed-status-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2188ef7
fix: exit with error when agent job status is failed
flcrom a68f276
test: cover agent status check exit codes
flcrom 2a3fdd6
fix: show fallback for empty agent errors
flcrom 4746693
test: cover empty errors and HTTP status rejection
flcrom d807d35
test: build agent status CLI fixture before running tests
flcrom 727ec20
test: isolate agent CLI fixture builds
flcrom 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,126 @@ | ||
| import { execFile } from 'node:child_process'; | ||
| import { createServer, type Server } from 'node:http'; | ||
| import { copyFileSync, mkdtempSync, rmSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { promisify } from 'node:util'; | ||
| import { afterAll, beforeAll, expect, it } from 'vitest'; | ||
|
|
||
| const exec = promisify(execFile); | ||
| let server: Server; | ||
| let baseUrl: string; | ||
| let payload: Record<string, unknown> = {}; | ||
| let responseStatus = 200; | ||
| const home = mkdtempSync(join(tmpdir(), 'agent-status-cli-')); | ||
|
|
||
| beforeAll(async () => { | ||
| copyFileSync(join(process.cwd(), 'package.json'), join(home, 'package.json')); | ||
| await exec(process.execPath, [ | ||
| join(process.cwd(), 'node_modules/typescript/bin/tsc'), | ||
| '--outDir', | ||
| join(home, 'dist'), | ||
| ]); | ||
| server = createServer((req, res) => { | ||
| res.writeHead(responseStatus, { 'content-type': 'application/json' }); | ||
| res.end(JSON.stringify(payload)); | ||
| }); | ||
| await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)); | ||
| baseUrl = `http://127.0.0.1:${(server.address() as { port: number }).port}`; | ||
| }, 30000); | ||
|
|
||
| afterAll(async () => { | ||
| if (server) { | ||
| await new Promise<void>((resolve, reject) => | ||
| server.close((error) => (error ? reject(error) : resolve())) | ||
| ); | ||
| } | ||
| rmSync(home, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| async function cli(args: string[]) { | ||
| try { | ||
| return { | ||
| code: 0, | ||
| ...(await exec(process.execPath, [join(home, 'dist/index.js'), ...args], { | ||
| timeout: 10000, | ||
| env: { | ||
| ...process.env, | ||
| NODE_PATH: join(process.cwd(), 'node_modules'), | ||
| HOME: home, | ||
| USERPROFILE: home, | ||
| FIRECRAWL_API_KEY: 'fc-test', | ||
| FIRECRAWL_API_URL: baseUrl, | ||
| FIRECRAWL_NO_UPDATE_CHECK: '1', | ||
| }, | ||
| })), | ||
| }; | ||
| } catch (error) { | ||
| return error as { code: number; stdout: string; stderr: string }; | ||
| } | ||
| } | ||
|
|
||
| const JOB_ID = '019e5299-8235-7538-9f62-bc39d4b058f1'; | ||
|
|
||
| it('exits nonzero and surfaces the server error when checking a failed agent without --wait', async () => { | ||
| payload = { | ||
| success: true, | ||
| status: 'failed', | ||
| error: 'Refusal: Error: Agent reached max credits', | ||
| data: null, | ||
| }; | ||
| const result = await cli(['agent', JOB_ID, '--json']); | ||
| expect(result.code).toBe(1); | ||
| expect(result.stderr).toContain('Agent reached max credits'); | ||
| }); | ||
|
|
||
| it('keeps success semantics for a completed agent status', async () => { | ||
| payload = { | ||
| success: true, | ||
| status: 'completed', | ||
| data: { answer: 'done' }, | ||
| creditsUsed: 12, | ||
| }; | ||
| const result = await cli(['agent', JOB_ID, '--json']); | ||
| expect(result.code).toBe(0); | ||
| const parsed = JSON.parse(result.stdout); | ||
| expect(parsed.success).toBe(true); | ||
| expect(parsed.status).toBe('completed'); | ||
| expect(parsed.data).toEqual({ answer: 'done' }); | ||
| }); | ||
|
|
||
| it('keeps success semantics for a processing agent status', async () => { | ||
| payload = { success: true, status: 'processing' }; | ||
| const result = await cli(['agent', JOB_ID, '--json']); | ||
| expect(result.code).toBe(0); | ||
| const parsed = JSON.parse(result.stdout); | ||
| expect(parsed.success).toBe(true); | ||
| expect(parsed.status).toBe('processing'); | ||
| }); | ||
|
|
||
| it('keeps success semantics for a cancelled agent status', async () => { | ||
| payload = { success: true, status: 'cancelled' }; | ||
| const result = await cli(['agent', JOB_ID, '--json']); | ||
| expect(result.code).toBe(0); | ||
| const parsed = JSON.parse(result.stdout); | ||
| expect(parsed.success).toBe(true); | ||
| expect(parsed.status).toBe('cancelled'); | ||
| }); | ||
|
|
||
| it('keeps failure semantics when the server rejects the status request', async () => { | ||
| payload = { success: false, error: 'Unauthorized: Invalid API key' }; | ||
| responseStatus = 401; | ||
| const result = await cli(['agent', JOB_ID, '--json']); | ||
| responseStatus = 200; | ||
| expect(result.code).toBe(1); | ||
| expect(result.stderr).toContain('Unauthorized: Invalid API key'); | ||
| }); | ||
|
|
||
| it.each([undefined, null, ''])( | ||
| 'uses the failure fallback for a missing or empty error (%j)', | ||
| async (error) => { | ||
| payload = { success: true, status: 'failed', data: null, error }; | ||
| const result = await cli(['agent', JOB_ID, '--json']); | ||
| expect(result.code).toBe(1); | ||
| expect(result.stderr).toContain('Agent failed'); | ||
| } | ||
| ); | ||
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
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.