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
Decision models: a cached decision-model answer is reused only for the same model
Claudec9d4269726d3
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 6229895fb..7c1b131c0 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -9028,6 +9028,20 @@ export class Agent extends LoopDetector {return decisionFeatureEnabled(decisionConfig, feature) && !this.strictSecretMode && (decisionConfig.local || globalThis.navigator?.onLine !== false) ? { ...stored, decisionConfig } : null;}+ // Which decision model an answer came from. A cached answer of a decision model is reused only+ // while that same model is still enabled: after a switch to Off (or another provider) the+ // question is asked again instead of continuing on the earlier model's answer.+ _semanticDecisionKey(settings) {+ const config = settings?.decisionConfig;+ return config ? JSON.stringify([config.provider, config.model, config.baseUrl || '', config.mode || '']) : '';+ }++ async _semanticCacheHit(tabId, provider, cached) {+ if (!cached || cached.answeredBy !== 'decision_model') return cached || null;+ const current = this._semanticDecisionKey(await this._jevSettings(tabId, provider));+ return current && current === cached.decisionKey ? cached : null;+ }+async _jevClassify(tabId, instructions, criteria, state) {const context = this.systemOneContext(tabId);try {@@ -9078,10 +9092,11 @@ export class Agent extends LoopDetector {const data = boundedSemanticData(page);const cacheKey = this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, kind, trusted, data]));this._semanticDecisionCache ??= new Map();- const cached = this._semanticDecisionCache.get(cacheKey);+ const cached = await this._semanticCacheHit(tabId, provider, this._semanticDecisionCache.get(cacheKey));if (cached && !retryChoices.includes(cached.choice)) return { ...cached, source: 'cache' };+ let decisionKey = '';const remember = result => {- this._semanticDecisionCache.set(cacheKey, { choice: result.choice, evidence: result.evidence, answeredBy: result.source });+ this._semanticDecisionCache.set(cacheKey, { choice: result.choice, evidence: result.evidence, answeredBy: result.source, decisionKey: result.source === 'decision_model' ? decisionKey : '' });while (this._semanticDecisionCache.size > 256) this._semanticDecisionCache.delete(this._semanticDecisionCache.keys().next().value);return result;};@@ -9089,6 +9104,7 @@ export class Agent extends LoopDetector {const context = this.systemOneContext(tabId);try {const settings = await this._jevSettings(tabId, provider);+ decisionKey = this._semanticDecisionKey(settings);const decisionText = redactSystemOneText(JSON.stringify({ request: trusted, page: data }));// Keep the existing bounded fast route, but use the full-text main// model directly when a relationship question cannot fit in it.@@ -9146,13 +9162,16 @@ export class Agent extends LoopDetector {const data = (Array.isArray(items) ? items : []).map(item => boundedSemanticData(item));const keys = data.map(item => this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, kind, trusted, item])));this._semanticDecisionCache ??= new Map();- const results = keys.map(key => {- const cached = this._semanticDecisionCache.get(key);- return cached ? { ...cached, source: 'cache' } : null;- });+ const cachedEntries = keys.map(key => this._semanticDecisionCache.get(key));+ const currentDecisionKey = cachedEntries.some(entry => entry?.answeredBy === 'decision_model')+ ? this._semanticDecisionKey(await this._jevSettings(tabId, provider))+ : '';+ const results = cachedEntries.map(cached => (cached && (cached.answeredBy !== 'decision_model'+ || (currentDecisionKey && currentDecisionKey === cached.decisionKey)) ? { ...cached, source: 'cache' } : null));+ let decisionKey = '';const remember = (index, result) => {results[index] = result;- this._semanticDecisionCache.set(keys[index], { choice: result.choice, evidence: result.evidence, answeredBy: result.source });+ this._semanticDecisionCache.set(keys[index], { choice: result.choice, evidence: result.evidence, answeredBy: result.source, decisionKey: result.source === 'decision_model' ? decisionKey : '' });while (this._semanticDecisionCache.size > 512) this._semanticDecisionCache.delete(this._semanticDecisionCache.keys().next().value);};const pending = () => results.map((result, index) => (result ? -1 : index)).filter(index => index >= 0);@@ -9165,6 +9184,7 @@ export class Agent extends LoopDetector {if (open.length) {try {const settings = await this._jevSettings(tabId, provider);+ decisionKey = this._semanticDecisionKey(settings);for (const chunk of settings ? semanticBatchChunks(open.map(index => data[index]), SEMANTIC_BATCH_LIMITS.decision) : []) {if (stopped()) break;const indexes = chunk.map(position => open[position]);diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 7655bdb72..3a2e223e9 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -8738,6 +8738,20 @@ export class Agent extends LoopDetector {return decisionFeatureEnabled(decisionConfig, feature) && !this.strictSecretMode && (decisionConfig.local || globalThis.navigator?.onLine !== false) ? { ...stored, decisionConfig } : null;}+ // Which decision model an answer came from. A cached answer of a decision model is reused only+ // while that same model is still enabled: after a switch to Off (or another provider) the+ // question is asked again instead of continuing on the earlier model's answer.+ _semanticDecisionKey(settings) {+ const config = settings?.decisionConfig;+ return config ? JSON.stringify([config.provider, config.model, config.baseUrl || '', config.mode || '']) : '';+ }++ async _semanticCacheHit(tabId, provider, cached) {+ if (!cached || cached.answeredBy !== 'decision_model') return cached || null;+ const current = this._semanticDecisionKey(await this._jevSettings(tabId, provider));+ return current && current === cached.decisionKey ? cached : null;+ }+async _jevClassify(tabId, instructions, criteria, state) {const context = this.systemOneContext(tabId);try {@@ -8788,10 +8802,11 @@ export class Agent extends LoopDetector {const data = boundedSemanticData(page);const cacheKey = this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, kind, trusted, data]));this._semanticDecisionCache ??= new Map();- const cached = this._semanticDecisionCache.get(cacheKey);+ const cached = await this._semanticCacheHit(tabId, provider, this._semanticDecisionCache.get(cacheKey));if (cached && !retryChoices.includes(cached.choice)) return { ...cached, source: 'cache' };+ let decisionKey = '';const remember = result => {- this._semanticDecisionCache.set(cacheKey, { choice: result.choice, evidence: result.evidence, answeredBy: result.source });+ this._semanticDecisionCache.set(cacheKey, { choice: result.choice, evidence: result.evidence, answeredBy: result.source, decisionKey: result.source === 'decision_model' ? decisionKey : '' });while (this._semanticDecisionCache.size > 256) this._semanticDecisionCache.delete(this._semanticDecisionCache.keys().next().value);return result;};@@ -8799,6 +8814,7 @@ export class Agent extends LoopDetector {const context = this.systemOneContext(tabId);try {const settings = await this._jevSettings(tabId, provider);+ decisionKey = this._semanticDecisionKey(settings);const decisionText = redactSystemOneText(JSON.stringify({ request: trusted, page: data }));// Keep the existing bounded fast route, but use the full-text main// model directly when a relationship question cannot fit in it.@@ -8856,13 +8872,16 @@ export class Agent extends LoopDetector {const data = (Array.isArray(items) ? items : []).map(item => boundedSemanticData(item));const keys = data.map(item => this._sha256TextSync(JSON.stringify([SEMANTIC_DECISION_VERSION, kind, trusted, item])));this._semanticDecisionCache ??= new Map();- const results = keys.map(key => {- const cached = this._semanticDecisionCache.get(key);- return cached ? { ...cached, source: 'cache' } : null;- });+ const cachedEntries = keys.map(key => this._semanticDecisionCache.get(key));+ const currentDecisionKey = cachedEntries.some(entry => entry?.answeredBy === 'decision_model')+ ? this._semanticDecisionKey(await this._jevSettings(tabId, provider))+ : '';+ const results = cachedEntries.map(cached => (cached && (cached.answeredBy !== 'decision_model'+ || (currentDecisionKey && currentDecisionKey === cached.decisionKey)) ? { ...cached, source: 'cache' } : null));+ let decisionKey = '';const remember = (index, result) => {results[index] = result;- this._semanticDecisionCache.set(keys[index], { choice: result.choice, evidence: result.evidence, answeredBy: result.source });+ this._semanticDecisionCache.set(keys[index], { choice: result.choice, evidence: result.evidence, answeredBy: result.source, decisionKey: result.source === 'decision_model' ? decisionKey : '' });while (this._semanticDecisionCache.size > 512) this._semanticDecisionCache.delete(this._semanticDecisionCache.keys().next().value);};const pending = () => results.map((result, index) => (result ? -1 : index)).filter(index => index >= 0);@@ -8875,6 +8894,7 @@ export class Agent extends LoopDetector {if (open.length) {try {const settings = await this._jevSettings(tabId, provider);+ decisionKey = this._semanticDecisionKey(settings);for (const chunk of settings ? semanticBatchChunks(open.map(index => data[index]), SEMANTIC_BATCH_LIMITS.decision) : []) {if (stopped()) break;const indexes = chunk.map(position => open[position]);diff --git a/test/decision-mode.mjs b/test/decision-mode.mjsindex fc29f9d9e..6fdb9d048 100644--- a/test/decision-mode.mjs+++ b/test/decision-mode.mjs@@ -323,3 +323,50 @@ for (const build of ['chrome', 'firefox']) {}});}++for (const build of ['chrome', 'firefox']) {+ test(`${build}: a cached decision-model answer is not reused after the mode or model changes`, async () => {+ const saved = globalThis.chrome;+ const store = { decisionMode: 'on', decisionProvider: 'local', decisionModel: 'kev-latest', decisionBaseUrl: 'http://127.0.0.1:8009' };+ const local = { get: async () => ({ ...store }), set: async values => Object.assign(store, values) };+ globalThis.chrome = globalThis.browser = { ...saved, storage: { local, session: local } };+ try {+ const { Agent } = await import(`../src/${build}/src/agent/agent.js`);+ const { SEMANTIC_QUESTIONS } = await import(`../src/${build}/src/agent/semantic-decisions.js`);+ const kind = Object.keys(SEMANTIC_QUESTIONS)[0];+ const choice = SEMANTIC_QUESTIONS[kind].authorizing?.[0] || Object.keys(SEMANTIC_QUESTIONS[kind].choices || {})[0] || 'yes';+ const agent = new Agent({ getActive: () => ({ config: {} }) });+ const provider = { config: {} }; // no chat model: whatever is not cached is "unavailable"+ agent._sha256TextSync = () => 'fixed';+ agent._semanticDecisionCache = new Map();+ const ask = () => agent._semanticDecision(1, kind, { request: 'q', page: {} }, { provider });+ const seed = async (answeredBy) => agent._semanticDecisionCache.set('fixed', {+ choice, evidence: '', answeredBy,+ decisionKey: answeredBy === 'decision_model' ? agent._semanticDecisionKey(await agent._jevSettings(1, provider)) : '',+ });+ await seed('decision_model');+ assert.equal((await ask()).source, 'cache', 'still On with the same model');+ store.decisionMode = 'off';+ assert.equal((await ask()).source, 'unavailable', 'Off does not continue on the earlier model\'s answer');+ store.decisionMode = 'on';+ assert.equal((await ask()).source, 'cache', 'back On with the same model');+ store.decisionModel = 'another-model';+ assert.equal((await ask()).source, 'unavailable', 'another decision model asks again');+ store.decisionModel = 'kev-latest';+ // An answer the main model gave does not depend on the decision model.+ await seed('main_model');+ store.decisionMode = 'off';+ assert.equal((await ask()).source, 'cache', 'a main-model answer survives Off');+ // The batch form follows the same rule.+ store.decisionMode = 'on';+ await seed('decision_model');+ agent._sha256TextSync = () => 'fixed';+ const batch = () => agent._semanticDecisions(1, kind, { request: 'q', items: [{ a: 1 }] }, { provider });+ assert.equal((await batch())[0].source, 'cache');+ store.decisionMode = 'off';+ assert.notEqual((await batch())[0].source, 'cache');+ } finally {+ globalThis.chrome = globalThis.browser = saved;+ }+ });+}