-
Notifications
You must be signed in to change notification settings - Fork 112
fix: stop agent --wait polling when the job is cancelled #297
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
2
commits into
firecrawl:main
Choose a base branch
from
flcrom:fix/agent-cancelled-wait
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 { 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); | ||
| const JOB_ID = '019e5299-8235-7538-9f62-bc39d4b058f1'; | ||
| const home = mkdtempSync(join(tmpdir(), 'agent-cancellation-cli-')); | ||
| let server: Server; | ||
| let baseUrl: string; | ||
| let statuses: string[]; | ||
| let polls: number; | ||
|
|
||
| beforeAll(async () => { | ||
| server = createServer((req, res) => { | ||
| res.writeHead(200, { 'content-type': 'application/json' }); | ||
| if (req.method === 'POST') { | ||
| res.end(JSON.stringify({ success: true, id: JOB_ID })); | ||
| return; | ||
| } | ||
| const status = statuses[Math.min(polls++, statuses.length - 1)]; | ||
| res.end( | ||
| JSON.stringify({ | ||
| success: true, | ||
| status, | ||
| data: { answer: 'result' }, | ||
| creditsUsed: 12, | ||
| threadId: 'thread-fixture', | ||
| error: status === 'failed' ? 'Agent reached max credits' : undefined, | ||
| }) | ||
| ); | ||
| }); | ||
| 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}`; | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await new Promise<void>((resolve, reject) => | ||
| server.close((error) => (error ? reject(error) : resolve())) | ||
| ); | ||
| rmSync(home, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| async function cli(sequence: string[], prompt = 'research example') { | ||
| statuses = sequence; | ||
| polls = 0; | ||
| try { | ||
| return { | ||
| code: 0, | ||
| ...(await exec( | ||
| process.execPath, | ||
| [ | ||
| 'dist/index.js', | ||
| 'agent', | ||
| prompt, | ||
| '--wait', | ||
| '--poll-interval', | ||
| '0.01', | ||
| '--timeout', | ||
| '1', | ||
| '--json', | ||
| ], | ||
| { | ||
| timeout: 10000, | ||
| env: { | ||
| ...process.env, | ||
| 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 }; | ||
| } | ||
| } | ||
|
|
||
| it.each([['cancelled'], ['processing', 'cancelled']])( | ||
| 'stops prompt-start polling at cancellation (%j)', | ||
| async (...sequence) => { | ||
| const result = await cli(sequence); | ||
| expect(result.code).toBe(0); | ||
| expect(polls).toBe(sequence.length); | ||
| expect(JSON.parse(result.stdout)).toMatchObject({ | ||
| success: true, | ||
| id: JOB_ID, | ||
| status: 'cancelled', | ||
| creditsUsed: 12, | ||
| threadId: 'thread-fixture', | ||
| }); | ||
| expect(result.stderr).not.toContain('still processing'); | ||
| } | ||
| ); | ||
|
|
||
| it('keeps existing-job cancellation successful', async () => { | ||
| const result = await cli(['cancelled'], JOB_ID); | ||
| expect(result.code).toBe(0); | ||
| expect(polls).toBe(1); | ||
| expect(JSON.parse(result.stdout).status).toBe('cancelled'); | ||
| }); | ||
|
|
||
| it('keeps processing-to-completed successful', async () => { | ||
| const result = await cli(['processing', 'completed']); | ||
| expect(result.code).toBe(0); | ||
| expect(polls).toBe(2); | ||
| expect(JSON.parse(result.stdout).data).toEqual({ answer: 'result' }); | ||
| }); | ||
|
|
||
| it('keeps failed jobs unsuccessful', async () => { | ||
| const result = await cli(['failed']); | ||
| expect(result.code).toBe(1); | ||
| expect(polls).toBe(1); | ||
| expect(result.stderr).toContain('Agent reached max credits'); | ||
| }); | ||
|
|
||
| it('still times out when the agent remains processing', async () => { | ||
| const result = await cli(['processing']); | ||
| expect(result.code).toBe(1); | ||
| expect(result.stderr).toContain('Agent still processing'); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: This test spawns the compiled
dist/index.js, butpnpm test(vitest run) builds nothing, so on a clean checkout the CLI spawn fails with ENOENT and every case errors out — or, after unrelated source edits, the suite quietly tests a stale build. Build the project before running the suite (e.g., apretestscript or CIbuildstep) or havecli()fail with a clear message whendist/is missing.Prompt for AI agents