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
Skip the planner only when the earlier task was read whole too
Emre Sokullua71734bda939
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 29d85a797..9ec6cb25d 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -16,7 +16,7 @@ import { JEV_FAST_KEYS, JEV_CLASSIFIER_THRESHOLD, JEV_BROWSER_THRESHOLD, confideimport { redactSystemOneText, wrapSystemOneData, boundedSystemOneText, systemOneStateBytes } from './systemone-evidence.js';import { createSystemOneJudge, isSystemOneResponseContractError, systemOneFailureReason, SYSTEM_ONE_COST_PROVIDER } from './systemone-judge.js';import { AUTO_MODE, AUTO_MODE_ASK_NOTE, REQUEST_ACTION_MODE_TOOL_NAME, acceptModeEscalation, autoModeForChoice, autoModeHardRoute, autoModePlanForChoice, carriesUntrustedText, confirmedEscalationMode, isRunMode, settleAutoModeRoute, withRoutedRuntimeMode } from './auto-mode.js';-import { SEMANTIC_BATCH_LIMITS, SEMANTIC_DECISION_VERSION, acceptSemanticAnswer, acceptSemanticBatchAnswers, boundedSemanticData, controlEffectCommits, semanticBatchChunks, semanticBatchDecisionMessages, semanticDecisionMessages, semanticQuestion, semanticSystemOneBatchQuestions, semanticSystemOneQuestion, WORKFLOW_STATES } from './semantic-decisions.js';+import { SEMANTIC_BATCH_LIMITS, SEMANTIC_DECISION_VERSION, acceptSemanticAnswer, acceptSemanticBatchAnswers, boundedSemanticData, controlEffectCommits, semanticDataComplete, semanticBatchChunks, semanticBatchDecisionMessages, semanticDecisionMessages, semanticQuestion, semanticSystemOneBatchQuestions, semanticSystemOneQuestion, WORKFLOW_STATES } from './semantic-decisions.js';import { SOCIAL_PLATFORMS, socialPublicationApiPlatform, normalizePublicationContract, resolvePublicationText, publicationProgress, exactPublicationText, publicationMediaMatches, publicationContractMessages, publicationAuditMessages, parsePublicationJson, publicationAuditValidation } from './social-publish-contract.js';import { mastodonPublicationIntentMessages, normalizeMastodonPublicationIntent, captureNativeMastodonPublication, mastodonPublicationBinding, mastodonPublicationReceipt, mastodonPublicationNavigation } from './mastodon-publication.js';import { AGENT_TOOLS, AGENT_TOOL_NAMES, RESERVED_AGENT_TOOL_NAMES, RETIRED_AGENT_TOOL_NAMES, getToolsForMode, SYSTEM_PROMPT_BATCHING, SYSTEM_PROMPT_ASK, SYSTEM_PROMPT_ACT, SYSTEM_PROMPT_ACT_COMPACT, SYSTEM_PROMPT_ACT_MID, SYSTEM_PROMPT_ACT_LEAN, SYSTEM_PROMPT_GENERATIVE_MEDIA, SYSTEM_PROMPT_DEV_APPENDIX, SYSTEM_PROMPT_WEBMCP_ASK, SYSTEM_PROMPT_WEBMCP_ACT } from './tools.js';@@ -9222,8 +9222,8 @@ export class Agent extends LoopDetector {* in the same decision-model request (run_mode with plan_need). Their sure* answers come back as `alongside: { kind: choice | null }` and are* remembered on their own; the active model never answers them. An- * authorizing answer among them needs the whole request: when the request- * was cut to fit the decision model, it is not used.+ * authorizing answer among them needs the whole request and data: when+ * either was cut to fit the decision model, it is not used.*/async _semanticDecision(tabId, kind, { request = '', page = {} } = {}, { provider = this._activeProvider(tabId), signal = null, mainModel = true, retryChoices = [], alongside = [] } = {}) {const spec = semanticQuestion(kind);@@ -9232,9 +9232,12 @@ export class Agent extends LoopDetector {const requestText = String(request ?? '');const trusted = kind === 'user_turn_kind' ? requestText : requestText.slice(0, 6000);const data = boundedSemanticData(page);- // Keyed by the whole request, not the part a model reads: two requests- // that share a cut-off start are different requests.- const keyFor = question => this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, question, requestText, data]));+ // Whether a model reads all of it: the request is cut to 6,000 characters+ // above, and boundedSemanticData shortens long data (an earlier task).+ const inputComplete = trusted.length === requestText.length && semanticDataComplete(page);+ // Keyed by the whole request, not the part a model reads, and by whether+ // the data was cut: inputs that share a cut-off start are different.+ const keyFor = question => this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, question, requestText, data, inputComplete]));const cacheKey = keyFor(kind);const extraKinds = [...new Set(Array.isArray(alongside) ? alongside : [])].filter(extra => extra !== kind && semanticQuestion(extra));this._semanticDecisionCache ??= new Map();@@ -9263,9 +9266,9 @@ export class Agent extends LoopDetector {const settings = await this._jevSettings(tabId, provider);decisionKey = this._semanticDecisionKey(settings);const decisionText = redactSystemOneText(JSON.stringify({ request: trusted, page: data }));- // What the decision model reads of the request: cut to 6,000 characters- // above, then to the decision state's budget below.- const partialRequest = trusted.length < requestText.length || systemOneStateBytes(decisionText) > 12000;+ // What the decision model reads: the input above, then cut to the+ // decision state's budget below.+ const partialInput = !inputComplete || systemOneStateBytes(decisionText) > 12000;// Keep the existing bounded fast route, but use the full-text main// model directly when a relationship question cannot fit in it.const decisionFits = kind !== 'user_turn_kind' || systemOneStateBytes(decisionText) <= 12000;@@ -9278,9 +9281,9 @@ export class Agent extends LoopDetector {for (const extra of extraKinds) {const extraAnswer = verdict.answers?.[extra];const authorizing = semanticQuestion(extra).authorizing.includes(extraAnswer?.choice);- // A part of the request cannot authorize skipping anything the rest+ // A part of the input cannot authorize skipping anything the rest// of it might need (plan_need: a payment after the cut).- if (authorizing && partialRequest) continue;+ if (authorizing && partialInput) continue;const extraChoice = confidentChoice(extraAnswer, authorizing ? 0.85 : 0.7);if (extraChoice) {extraAnswers[extra] = extraChoice;diff --git a/src/chrome/src/agent/semantic-decisions.js b/src/chrome/src/agent/semantic-decisions.jsindex adb37a950..5d58d5a59 100644--- a/src/chrome/src/agent/semantic-decisions.js+++ b/src/chrome/src/agent/semantic-decisions.js@@ -572,6 +572,20 @@ export function boundedSemanticData(value, depth = 0) {return null;}+// Whether boundedSemanticData(value) keeps all of value: no string past 600+// characters, no list or object past 16 entries, nothing nested too deep.+export function semanticDataComplete(value, depth = 0) {+ if (value == null || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))) return true;+ if (typeof value === 'string') return value.normalize('NFC').replace(/\s+/g, ' ').trim().length <= 600;+ if (depth >= 3) return false;+ if (Array.isArray(value)) return value.length <= 16 && value.every(item => semanticDataComplete(item, depth + 1));+ if (typeof value === 'object') {+ const entries = Object.entries(value);+ return entries.length <= 16 && entries.every(([key, item]) => String(key).length <= 40 && semanticDataComplete(item, depth + 1));+ }+ return false;+}+// The decision model's form of the question.export function semanticSystemOneQuestion(kind) {const spec = semanticQuestion(kind);diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 01ec17594..61402a2a4 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -14,7 +14,7 @@ import { JEV_FAST_KEYS, JEV_CLASSIFIER_THRESHOLD, JEV_BROWSER_THRESHOLD, confideimport { redactSystemOneText, wrapSystemOneData, boundedSystemOneText, systemOneStateBytes } from './systemone-evidence.js';import { createSystemOneJudge, isSystemOneResponseContractError, systemOneFailureReason, SYSTEM_ONE_COST_PROVIDER } from './systemone-judge.js';import { AUTO_MODE, AUTO_MODE_ASK_NOTE, REQUEST_ACTION_MODE_TOOL_NAME, acceptModeEscalation, autoModeForChoice, autoModeHardRoute, autoModePlanForChoice, carriesUntrustedText, confirmedEscalationMode, isRunMode, settleAutoModeRoute, withRoutedRuntimeMode } from './auto-mode.js';-import { SEMANTIC_BATCH_LIMITS, SEMANTIC_DECISION_VERSION, acceptSemanticAnswer, acceptSemanticBatchAnswers, boundedSemanticData, controlEffectCommits, semanticBatchChunks, semanticBatchDecisionMessages, semanticDecisionMessages, semanticQuestion, semanticSystemOneBatchQuestions, semanticSystemOneQuestion, WORKFLOW_STATES } from './semantic-decisions.js';+import { SEMANTIC_BATCH_LIMITS, SEMANTIC_DECISION_VERSION, acceptSemanticAnswer, acceptSemanticBatchAnswers, boundedSemanticData, controlEffectCommits, semanticDataComplete, semanticBatchChunks, semanticBatchDecisionMessages, semanticDecisionMessages, semanticQuestion, semanticSystemOneBatchQuestions, semanticSystemOneQuestion, WORKFLOW_STATES } from './semantic-decisions.js';import { firefoxBidi } from '../bidi/client.js';import { SOCIAL_PLATFORMS, socialPublicationApiPlatform, normalizePublicationContract, resolvePublicationText, publicationProgress, exactPublicationText, publicationMediaMatches, publicationContractMessages, publicationAuditMessages, parsePublicationJson, publicationAuditValidation } from './social-publish-contract.js';import { mastodonPublicationIntentMessages, normalizeMastodonPublicationIntent, captureNativeMastodonPublication, mastodonPublicationBinding, mastodonPublicationReceipt, mastodonPublicationNavigation } from './mastodon-publication.js';@@ -8931,8 +8931,8 @@ export class Agent extends LoopDetector {* in the same decision-model request (run_mode with plan_need). Their sure* answers come back as `alongside: { kind: choice | null }` and are* remembered on their own; the active model never answers them. An- * authorizing answer among them needs the whole request: when the request- * was cut to fit the decision model, it is not used.+ * authorizing answer among them needs the whole request and data: when+ * either was cut to fit the decision model, it is not used.*/async _semanticDecision(tabId, kind, { request = '', page = {} } = {}, { provider = this._activeProvider(tabId), signal = null, mainModel = true, retryChoices = [], alongside = [] } = {}) {const spec = semanticQuestion(kind);@@ -8941,9 +8941,12 @@ export class Agent extends LoopDetector {const requestText = String(request ?? '');const trusted = kind === 'user_turn_kind' ? requestText : requestText.slice(0, 6000);const data = boundedSemanticData(page);- // Keyed by the whole request, not the part a model reads: two requests- // that share a cut-off start are different requests.- const keyFor = question => this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, question, requestText, data]));+ // Whether a model reads all of it: the request is cut to 6,000 characters+ // above, and boundedSemanticData shortens long data (an earlier task).+ const inputComplete = trusted.length === requestText.length && semanticDataComplete(page);+ // Keyed by the whole request, not the part a model reads, and by whether+ // the data was cut: inputs that share a cut-off start are different.+ const keyFor = question => this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, question, requestText, data, inputComplete]));const cacheKey = keyFor(kind);const extraKinds = [...new Set(Array.isArray(alongside) ? alongside : [])].filter(extra => extra !== kind && semanticQuestion(extra));this._semanticDecisionCache ??= new Map();@@ -8972,9 +8975,9 @@ export class Agent extends LoopDetector {const settings = await this._jevSettings(tabId, provider);decisionKey = this._semanticDecisionKey(settings);const decisionText = redactSystemOneText(JSON.stringify({ request: trusted, page: data }));- // What the decision model reads of the request: cut to 6,000 characters- // above, then to the decision state's budget below.- const partialRequest = trusted.length < requestText.length || systemOneStateBytes(decisionText) > 12000;+ // What the decision model reads: the input above, then cut to the+ // decision state's budget below.+ const partialInput = !inputComplete || systemOneStateBytes(decisionText) > 12000;// Keep the existing bounded fast route, but use the full-text main// model directly when a relationship question cannot fit in it.const decisionFits = kind !== 'user_turn_kind' || systemOneStateBytes(decisionText) <= 12000;@@ -8987,9 +8990,9 @@ export class Agent extends LoopDetector {for (const extra of extraKinds) {const extraAnswer = verdict.answers?.[extra];const authorizing = semanticQuestion(extra).authorizing.includes(extraAnswer?.choice);- // A part of the request cannot authorize skipping anything the rest+ // A part of the input cannot authorize skipping anything the rest// of it might need (plan_need: a payment after the cut).- if (authorizing && partialRequest) continue;+ if (authorizing && partialInput) continue;const extraChoice = confidentChoice(extraAnswer, authorizing ? 0.85 : 0.7);if (extraChoice) {extraAnswers[extra] = extraChoice;diff --git a/src/firefox/src/agent/semantic-decisions.js b/src/firefox/src/agent/semantic-decisions.jsindex adb37a950..5d58d5a59 100644--- a/src/firefox/src/agent/semantic-decisions.js+++ b/src/firefox/src/agent/semantic-decisions.js@@ -572,6 +572,20 @@ export function boundedSemanticData(value, depth = 0) {return null;}+// Whether boundedSemanticData(value) keeps all of value: no string past 600+// characters, no list or object past 16 entries, nothing nested too deep.+export function semanticDataComplete(value, depth = 0) {+ if (value == null || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))) return true;+ if (typeof value === 'string') return value.normalize('NFC').replace(/\s+/g, ' ').trim().length <= 600;+ if (depth >= 3) return false;+ if (Array.isArray(value)) return value.length <= 16 && value.every(item => semanticDataComplete(item, depth + 1));+ if (typeof value === 'object') {+ const entries = Object.entries(value);+ return entries.length <= 16 && entries.every(([key, item]) => String(key).length <= 40 && semanticDataComplete(item, depth + 1));+ }+ return false;+}+// The decision model's form of the question.export function semanticSystemOneQuestion(kind) {const spec = semanticQuestion(kind);diff --git a/test/plan-when-needed.mjs b/test/plan-when-needed.mjsindex d10feedcb..242a2ca92 100644--- a/test/plan-when-needed.mjs+++ b/test/plan-when-needed.mjs@@ -126,6 +126,17 @@ for (const build of BUILDS) {assert.equal('plan' in await wide.agent._routeAutoRunMode(6, { ...question, request: 'あ'.repeat(4500) }), false);});+ test(`${build}: an earlier task cut to fit the decision data never skips the planner`, async () => {+ // A short follow-up leans on the earlier task, which the data keeps to 600 characters.+ const start = 'Read the next row and note its owner. '.repeat(20).slice(0, 600);+ const followUp = task => ({ request: 'Continue.', page: { previous_mode: 'act', current_task: task } });+ const { agent, requests } = decisionAgent({ run_mode: sure('act'), plan_need: sure('direct') });+ assert.equal((await agent._routeAutoRunMode(8, followUp(start))).plan, 'direct', 'an earlier task read whole');+ const longer = await agent._routeAutoRunMode(8, followUp(`${start} Then delete every row you read.`));+ assert.equal(requests.length, 2, 'the longer earlier task is a new question');+ assert.equal('plan' in longer, false);+ });+test(`${build}: a remembered answer belongs to the whole request, not to its first 6,000 characters`, async () => {const start = 'Read this paragraph. '.repeat(300).slice(0, 6000);const { agent, requests } = decisionAgent({ run_mode: sure('act'), plan_need: sure('direct') });