Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Local proxy: keep a window the proxy reported when a later probe falls back

Claude7dc87267fee1
diff --git a/src/chrome/src/providers/manager.js b/src/chrome/src/providers/manager.js
index b0b195fb6..6232172f5 100644
--- a/src/chrome/src/providers/manager.js
+++ b/src/chrome/src/providers/manager.js
@@ -2056,6 +2056,10 @@ export class ProviderManager {
if (updates.manualContextWindow === true) {
delete merged.autoContextWindow;
}
+ if (!Number.isFinite(Number(merged.autoContextWindow))) {
+ delete merged.autoContextWindowSource;
+ delete merged.autoContextWindowModel;
+ }
if (id === 'webgpu' && Object.hasOwn(updates, 'model')) {
merged.model = normalizeWebgpuModelId(merged.model);
const preset = webgpuModelPreset(merged.model);
@@ -2589,7 +2593,8 @@ export class ProviderManager {
const detected = await this._detectLocalContextWindow(id, provider, { model: selectedModel });
if (detected?.contextWindow == null) return { ok: false, error: 'No context window detected' };
const current = this.providers.get(id) || provider;
- if (!shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
+ if (this._keepsReportedContextWindow(id, current?.config, detected)
+ || !shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
shrinkOverride: detected.shrinkOverride === true,
replaceable: this._replaceableContextWindows(id, current?.config),
explicitOverride: this._hasExplicitContextWindow(id, current?.config),
@@ -2597,7 +2602,7 @@ export class ProviderManager {
return { ok: true };
}
if (!await this._updateProviderContextWindow(id, detected.contextWindow, observed,
- this._detectedContextWindowFields(id, current?.config, detected.contextWindow))) {
+ this._detectedContextWindowFields(id, current?.config, detected))) {
return { ok: false, error: 'Provider changed before context detection completed' };
}
return { ok: true, contextWindow: detected.contextWindow };
@@ -2724,10 +2729,27 @@ export class ProviderManager {
return this._providerDefinitionId(id, config) === 'local_openai_proxy' && config?.manualContextWindow === true;
}
- _detectedContextWindowFields(id, config, contextWindow) {
- return this._providerDefinitionId(id, config) === 'local_openai_proxy'
- ? { autoContextWindow: contextWindow, manualContextWindow: false }
- : {};
+ // Where the stored auto window came from: a window the proxy itself reported (its model card or a
+ // live /props) outranks one inferred from the model name or the 64k fallback.
+ _detectedContextWindowFields(id, config, detected) {
+ if (this._providerDefinitionId(id, config) !== 'local_openai_proxy') return {};
+ return {
+ autoContextWindow: detected.contextWindow,
+ manualContextWindow: false,
+ autoContextWindowSource: detected.reported === true ? 'reported' : 'inferred',
+ autoContextWindowModel: String(detected.model || ''),
+ };
+ }
+
+ // A later probe that cannot reach the proxy's metadata (a timeout, a restart) falls back to the
+ // name or the 64k default. That must not replace a window the proxy reported for the same model.
+ _keepsReportedContextWindow(id, config, detected) {
+ if (this._providerDefinitionId(id, config) !== 'local_openai_proxy') return false;
+ if (detected?.reported !== false) return false;
+ const auto = Number(config?.autoContextWindow);
+ return config?.autoContextWindowSource === 'reported'
+ && Number.isFinite(auto) && auto > 0 && Number(config?.contextWindow) === auto
+ && String(config?.autoContextWindowModel || '') === String(detected.model || '');
}
async _attachDetectedContextWindow(id, provider, result, options = {}) {
@@ -2740,7 +2762,8 @@ export class ProviderManager {
const detected = await this._detectLocalContextWindow(id, provider, options);
if (detected?.contextWindow == null) return result;
const current = this.providers.get(id) || provider;
- if (!shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
+ if (this._keepsReportedContextWindow(id, current?.config, detected)
+ || !shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
shrinkOverride: detected.shrinkOverride === true,
replaceable: this._replaceableContextWindows(id, current?.config),
explicitOverride: this._hasExplicitContextWindow(id, current?.config),
@@ -2748,7 +2771,7 @@ export class ProviderManager {
return result;
}
if (!await this._updateProviderContextWindow(id, detected.contextWindow, observed,
- this._detectedContextWindowFields(id, current?.config, detected.contextWindow))) {
+ this._detectedContextWindowFields(id, current?.config, detected))) {
return result;
}
return { ...result, contextWindow: detected.contextWindow };
@@ -2851,21 +2874,21 @@ export class ProviderManager {
} catch { /* try the next source */ }
}
const listed = data ? parseOpenAiProxyModelListContextWindow(data, model) : null;
- if (listed != null) return { contextWindow: listed, shrinkOverride: true };
+ if (listed != null) return { contextWindow: listed, shrinkOverride: true, reported: true, model };
// 2. A llama.cpp server behind the proxy reports the loaded model's
// live n_ctx (`?model=` selects it on a llama-server router).
try {
const res = await fetchWithFallback(`${root}/props?model=${encodeURIComponent(model)}`, { method: 'GET', headers });
if (res.ok) {
const live = parseLlamaCppPropsContextWindow(await res.json());
- if (live != null) return { contextWindow: live, shrinkOverride: true };
+ if (live != null) return { contextWindow: live, shrinkOverride: true, reported: true, model };
}
} catch { /* try the next source */ }
// 3. A hosted model WebBrain knows by name, capped at 128k.
const named = inferLocalProxyModelContextWindow(model);
- if (named != null) return { contextWindow: named, shrinkOverride: false };
+ if (named != null) return { contextWindow: named, shrinkOverride: false, reported: false, model };
// 4. The proxy fallback. It only replaces a default, never an override.
- return { contextWindow: LOCAL_OPENAI_PROXY_CONTEXT_WINDOW, shrinkOverride: false };
+ return { contextWindow: LOCAL_OPENAI_PROXY_CONTEXT_WINDOW, shrinkOverride: false, reported: false, model };
}
if (definitionId === 'localai') {
diff --git a/src/firefox/src/providers/manager.js b/src/firefox/src/providers/manager.js
index 652f270b8..69f24e6c2 100644
--- a/src/firefox/src/providers/manager.js
+++ b/src/firefox/src/providers/manager.js
@@ -1637,6 +1637,10 @@ export class ProviderManager {
if (updates.manualContextWindow === true) {
delete merged.autoContextWindow;
}
+ if (!Number.isFinite(Number(merged.autoContextWindow))) {
+ delete merged.autoContextWindowSource;
+ delete merged.autoContextWindowModel;
+ }
if (this._providerDefinitionId(id, current) === 'ollama') {
merged.visionMode = OLLAMA_VISION_MODES.has(merged.visionMode) ? merged.visionMode : 'auto';
delete merged.supportsVision;
@@ -2128,7 +2132,8 @@ export class ProviderManager {
const detected = await this._detectLocalContextWindow(id, provider, { model: selectedModel });
if (detected?.contextWindow == null) return { ok: false, error: 'No context window detected' };
const current = this.providers.get(id) || provider;
- if (!shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
+ if (this._keepsReportedContextWindow(id, current?.config, detected)
+ || !shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
shrinkOverride: detected.shrinkOverride === true,
replaceable: this._replaceableContextWindows(id, current?.config),
explicitOverride: this._hasExplicitContextWindow(id, current?.config),
@@ -2136,7 +2141,7 @@ export class ProviderManager {
return { ok: true };
}
if (!await this._updateProviderContextWindow(id, detected.contextWindow, observed,
- this._detectedContextWindowFields(id, current?.config, detected.contextWindow))) {
+ this._detectedContextWindowFields(id, current?.config, detected))) {
return { ok: false, error: 'Provider changed before context detection completed' };
}
return { ok: true, contextWindow: detected.contextWindow };
@@ -2263,10 +2268,27 @@ export class ProviderManager {
return this._providerDefinitionId(id, config) === 'local_openai_proxy' && config?.manualContextWindow === true;
}
- _detectedContextWindowFields(id, config, contextWindow) {
- return this._providerDefinitionId(id, config) === 'local_openai_proxy'
- ? { autoContextWindow: contextWindow, manualContextWindow: false }
- : {};
+ // Where the stored auto window came from: a window the proxy itself reported (its model card or a
+ // live /props) outranks one inferred from the model name or the 64k fallback.
+ _detectedContextWindowFields(id, config, detected) {
+ if (this._providerDefinitionId(id, config) !== 'local_openai_proxy') return {};
+ return {
+ autoContextWindow: detected.contextWindow,
+ manualContextWindow: false,
+ autoContextWindowSource: detected.reported === true ? 'reported' : 'inferred',
+ autoContextWindowModel: String(detected.model || ''),
+ };
+ }
+
+ // A later probe that cannot reach the proxy's metadata (a timeout, a restart) falls back to the
+ // name or the 64k default. That must not replace a window the proxy reported for the same model.
+ _keepsReportedContextWindow(id, config, detected) {
+ if (this._providerDefinitionId(id, config) !== 'local_openai_proxy') return false;
+ if (detected?.reported !== false) return false;
+ const auto = Number(config?.autoContextWindow);
+ return config?.autoContextWindowSource === 'reported'
+ && Number.isFinite(auto) && auto > 0 && Number(config?.contextWindow) === auto
+ && String(config?.autoContextWindowModel || '') === String(detected.model || '');
}
async _attachDetectedContextWindow(id, provider, result, options = {}) {
@@ -2279,7 +2301,8 @@ export class ProviderManager {
const detected = await this._detectLocalContextWindow(id, provider, options);
if (detected?.contextWindow == null) return result;
const current = this.providers.get(id) || provider;
- if (!shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
+ if (this._keepsReportedContextWindow(id, current?.config, detected)
+ || !shouldApplyDetectedContextWindow(current?.config?.contextWindow, detected.contextWindow, {
shrinkOverride: detected.shrinkOverride === true,
replaceable: this._replaceableContextWindows(id, current?.config),
explicitOverride: this._hasExplicitContextWindow(id, current?.config),
@@ -2287,7 +2310,7 @@ export class ProviderManager {
return result;
}
if (!await this._updateProviderContextWindow(id, detected.contextWindow, observed,
- this._detectedContextWindowFields(id, current?.config, detected.contextWindow))) {
+ this._detectedContextWindowFields(id, current?.config, detected))) {
return result;
}
return { ...result, contextWindow: detected.contextWindow };
@@ -2390,21 +2413,21 @@ export class ProviderManager {
} catch { /* try the next source */ }
}
const listed = data ? parseOpenAiProxyModelListContextWindow(data, model) : null;
- if (listed != null) return { contextWindow: listed, shrinkOverride: true };
+ if (listed != null) return { contextWindow: listed, shrinkOverride: true, reported: true, model };
// 2. A llama.cpp server behind the proxy reports the loaded model's
// live n_ctx (`?model=` selects it on a llama-server router).
try {
const res = await fetch(`${root}/props?model=${encodeURIComponent(model)}`, { method: 'GET', headers });
if (res.ok) {
const live = parseLlamaCppPropsContextWindow(await res.json());
- if (live != null) return { contextWindow: live, shrinkOverride: true };
+ if (live != null) return { contextWindow: live, shrinkOverride: true, reported: true, model };
}
} catch { /* try the next source */ }
// 3. A hosted model WebBrain knows by name, capped at 128k.
const named = inferLocalProxyModelContextWindow(model);
- if (named != null) return { contextWindow: named, shrinkOverride: false };
+ if (named != null) return { contextWindow: named, shrinkOverride: false, reported: false, model };
// 4. The proxy fallback. It only replaces a default, never an override.
- return { contextWindow: LOCAL_OPENAI_PROXY_CONTEXT_WINDOW, shrinkOverride: false };
+ return { contextWindow: LOCAL_OPENAI_PROXY_CONTEXT_WINDOW, shrinkOverride: false, reported: false, model };
}
if (definitionId === 'localai') {
diff --git a/test/run.js b/test/run.js
index 1882b9f31..3bc97daa2 100644
--- a/test/run.js
+++ b/test/run.js
@@ -73101,6 +73101,39 @@ test('Local OpenAI-compatible Proxy context window: model card, live props, mode
assert.equal((await legacy.mgr.detectProviderContextWindow('local_openai_proxy', 'claude-sonnet-4-5')).contextWindow, 128000);
}
+ // 5e-3. A window the proxy reported is not replaced by a name guess or the 64k fallback when a
+ // later probe cannot reach the metadata; a different model starts over.
+ {
+ let reachable = true;
+ const { mgr, config } = setup(async (url) => (reachable && String(url).endsWith('/v1/models')
+ ? jsonOk({ data: [{ id: 'claude-sonnet-4-5', max_model_len: 8192 }, { id: 'other-model', max_model_len: 32768 }] })
+ : new Response('missing', { status: 404 })), { model: 'claude-sonnet-4-5', contextWindow: 65536 });
+ assert.equal((await mgr.detectProviderContextWindow('local_openai_proxy', 'claude-sonnet-4-5')).contextWindow, 8192);
+ assert.equal(config().autoContextWindowSource, 'reported');
+ reachable = false;
+ const failed = await mgr.detectProviderContextWindow('local_openai_proxy', 'claude-sonnet-4-5');
+ assert.equal(failed.contextWindow, undefined, `${label}: the name-based 128k does not replace the reported 8k`);
+ assert.equal(config().contextWindow, 8192);
+ const listFail = await mgr._attachDetectedContextWindow('local_openai_proxy', mgr.providers.get('local_openai_proxy'), { ok: true, models: [] }, { model: 'claude-sonnet-4-5' });
+ assert.equal(listFail.contextWindow, undefined, `${label}: Load models / Test connection keep it too`);
+ assert.equal(config().contextWindow, 8192);
+ // A user edit drops the provenance with the auto window.
+ await mgr.updateProvider('local_openai_proxy', { contextWindow: 4096, manualContextWindow: true });
+ assert.equal(config().autoContextWindowSource, undefined);
+ assert.equal(config().autoContextWindowModel, undefined);
+ }
+ // ...and an inferred window stays replaceable by a later reported one.
+ {
+ let reachable = false;
+ const { mgr, config } = setup(async (url) => (reachable && String(url).endsWith('/v1/models')
+ ? jsonOk({ data: [{ id: 'claude-sonnet-4-5', max_model_len: 8192 }] })
+ : new Response('missing', { status: 404 })), { model: 'claude-sonnet-4-5', contextWindow: 65536 });
+ assert.equal((await mgr.detectProviderContextWindow('local_openai_proxy', 'claude-sonnet-4-5')).contextWindow, 128000);
+ assert.equal(config().autoContextWindowSource, 'inferred');
+ reachable = true;
+ assert.equal((await mgr.detectProviderContextWindow('local_openai_proxy', 'claude-sonnet-4-5')).contextWindow, 8192, `${label}: a reported window replaces an inferred one`);
+ }
+
// 5f. Settings UI: edits made while detection is pending are preserved
{
const settingsSource = fs.readFileSync(path.join(ROOT, label === 'chrome' ? 'src/chrome/src/ui/settings.js' : 'src/firefox/src/ui/settings.js'), 'utf8');