Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix(agent): propagate completion cost stops to scheduled jobs
Emre Sokulluee81e6a681ae
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex e00ff536c..c9d6509eb 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -44425,6 +44425,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaionUpdate('warning', { message: finalResponse });onUpdate('text', { content: finalResponse, replace: true });await this._persistNow(tabId);+ if (runOptions.scheduledRun === true) throw error;return finalResponse;}const message = formatErrorMessage(error);@@ -45494,6 +45495,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaiawait closeTraceStep({ ok: false, code: stepErrorCode });this._logDebug({ type: 'llm_stream_error', step: steps, error: caughtMessage });if (this._isCostAllowanceError(e)) {+ if (runOptions.scheduledRun === true) throw e;if (typeof onUpdate === 'function' && e.quota) onUpdate('quota', { quota: e.quota });messages.push({ role: 'assistant', content: caughtMessage });onUpdate('warning', { message: caughtMessage });@@ -45586,6 +45588,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaionUpdate('warning', { message: finalResponse });onUpdate('text', { content: finalResponse, replace: true });await this._persistNow(tabId);+ if (runOptions.scheduledRun === true) throw error;return finalResponse;}const message = formatErrorMessage(error);diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex b9a20ef16..41a12b686 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -37264,6 +37264,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout donUpdate('warning', { message: finalResponse });onUpdate('text', { content: finalResponse, replace: true });await this._persistNow(tabId);+ if (runOptions.scheduledRun === true) throw error;return finalResponse;}const message = formatErrorMessage(error);@@ -38183,6 +38184,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dawait closeTraceStep({ ok: false, code: stepErrorCode });this._logDebug({ type: 'llm_stream_error', step: steps, error: caughtMessage });if (this._isCostAllowanceError(e)) {+ if (runOptions.scheduledRun === true) throw e;if (typeof onUpdate === 'function' && e.quota) onUpdate('quota', { quota: e.quota });messages.push({ role: 'assistant', content: caughtMessage });onUpdate('warning', { message: caughtMessage });@@ -38265,6 +38267,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout donUpdate('warning', { message: finalResponse });onUpdate('text', { content: finalResponse, replace: true });await this._persistNow(tabId);+ if (runOptions.scheduledRun === true) throw error;return finalResponse;}const message = formatErrorMessage(error);diff --git a/test/run.js b/test/run.jsindex 7ea095350..7b0df2eb7 100644--- a/test/run.js+++ b/test/run.js@@ -90661,8 +90661,8 @@ test('decision completion releases forced done into read-only recovery and then}});-test('completion decision quota errors stop streaming and non-streaming runs without another model call', async () => {- for (const AgentClass of [AgentCh, AgentFx]) for (const streaming of [false, true]) {+test('completion decision quota errors stop interactive runs and reject scheduled runs without another model call', async () => {+ for (const AgentClass of [AgentCh, AgentFx]) for (const streaming of [false, true]) for (const scheduledRun of [false, true]) {let calls = 0, status;const tool = { id: 'quota_done', function: { name: 'done', arguments: '{"outcome":"success","summary":"Read the page."}' } };const provider = { supportsTools: true, supportsVision: false, promptTier: 'full', contextWindow: 128000, model: 'test-model', name: 'test-provider' };@@ -90679,11 +90679,28 @@ test('completion decision quota errors stop streaming and non-streaming runs witagent.executeTool = async () => { throw Object.assign(new Error('Decision quota exhausted.'), { code: 'WB_COST_ALLOWANCE', status: 402, quota: { code: 'webbrain_cloud_free_tier_exceeded' } }); };const updates = [];const run = streaming ? agent.processMessageStream.bind(agent) : agent.processMessage.bind(agent);- const options = { onRunFinished: value => { status = value; } };- const result = await run(24970, 'Read the page', (type, data) => updates.push({ type, data }), 'act', ...(streaming ? [options] : [[], options]));+ const options = { scheduledRun, onRunFinished: value => { status = value; } };+ const operation = run(24970, 'Read the page', (type, data) => updates.push({ type, data }), 'act', ...(streaming ? [options] : [[], options]));+ if (scheduledRun) await assert.rejects(operation, error => error.code === 'WB_COST_ALLOWANCE' && error.status === 402, `${AgentClass.name}/${streaming}: a scheduled cost stop must reject`);+ else assert.match(await operation, /quota exhausted/);assert.equal(calls, 1, 'quota stops must not call another model');- assert.match(result, /quota exhausted/); assert.equal(status, 'cost_limit');+ assert.equal(status, 'cost_limit');assert.equal(updates.filter(update => update.type === 'quota').length, 1);+ if (scheduledRun) for (const schedule of [{ type: 'once', after_seconds: 0 }, { type: 'recurring', after_seconds: 0, interval_minutes: 5 }]) {+ const h = makeSchedulerHarness(AgentClass === AgentCh ? SchedulerCh : SchedulerFx, {+ processMessage: (...args) => run(args[0], args[1], args[2], args[3], ...(streaming ? [args[5]] : [args[4], args[5]])),+ });+ const created = await h.manager.createTaskJob({+ tabId: 77, conversationId: 'quota-job', currentUrl: 'https://example.com/', currentTitle: 'Example',+ args: { title: 'Quota stop', prompt: 'Read the page', schedule, target: { type: 'current_tab' } },+ });+ const before = calls;+ await h.manager.handleAlarm(h.alarmName(created.jobId));+ const stopped = h.jobs().find(job => job.id === created.jobId);+ assert.equal(stopped.status, 'failed', 'cost-limited jobs must not complete or silently reschedule');+ assert.match(stopped.lastError, /quota exhausted/);+ assert.equal(stopped.lastOutcome, null); assert.equal(calls, before + 1);+ }}});