Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(shared): keep catalog context snapshots when the lookup misses

renwanga10a129c6f6f0
diff --git a/docs/spec/03-runtime/12-provider-config-schema.md b/docs/spec/03-runtime/12-provider-config-schema.md
index ad9c138af..13d8be337 100644
--- a/docs/spec/03-runtime/12-provider-config-schema.md
+++ b/docs/spec/03-runtime/12-provider-config-schema.md
@@ -122,7 +122,8 @@ over-long alias with `MODEL_ALIAS_TOO_LONG`.
`models[].contextWindowSource` records where the stored `contextWindow` came
from. `catalog` marks a models.dev snapshot that a later catalog correction may
-replace; `user` marks a number entered in Settings and is never replaced. The
+replace (a lookup that falls back to the generic shape is not a correction);
+`user` marks a number entered in Settings and is never replaced. The
property is optional, so a config written before the marker stays readable and
older clients ignore it. Host-core keeps only those two values and drops anything
else, so an unreadable marker cannot turn into a third state. The resolution rule
diff --git a/docs/spec/03-runtime/13-model-catalog-and-selection.md b/docs/spec/03-runtime/13-model-catalog-and-selection.md
index 21347b354..8cf9a67b4 100644
--- a/docs/spec/03-runtime/13-model-catalog-and-selection.md
+++ b/docs/spec/03-runtime/13-model-catalog-and-selection.md
@@ -327,7 +327,10 @@ model window. Every binding records where its `contextWindow` came from
published `limit.context` replaces it. A refreshed record such as
`gpt-5.6-luna` (`1,050,000` tokens) stops appearing as a 128k model, and a limit
that models.dev corrects reaches the binding without deleting and re-adding the
- model.
+ model. Only a resolved models.dev record counts as published: when the lookup
+ misses and falls back to the generic shape (for example a custom gateway URL
+ serving an id several publishers list), its `128,000` is not a correction, and
+ the stored catalog snapshot stays in force.
- `user` — the number was entered through the per-model Advanced control (or the
preset ladder in it) and is never replaced by the catalog, including the
`128,000` value that is otherwise the generic seed.
diff --git a/docs/zh-CN/spec/03-runtime/12-provider-config-schema.md b/docs/zh-CN/spec/03-runtime/12-provider-config-schema.md
index 2a0813c84..6b4917ee9 100644
--- a/docs/zh-CN/spec/03-runtime/12-provider-config-schema.md
+++ b/docs/zh-CN/spec/03-runtime/12-provider-config-schema.md
@@ -137,7 +137,7 @@ JSON、根节点非对象、`models` 非数组,或任一条目不可读,都
`MODEL_BINDINGS_DEGRADED` 拒绝显式替换模型数组;不涉及模型数组的提供商字段仍可更新。
`models[].contextWindowSource` 记录存储的 `contextWindow` 来自哪里:`catalog` 表示
-models.dev 快照,之后的目录修正可以替换它;`user` 表示用户在设置中手改的值,永不被
+models.dev 快照,之后的目录修正可以替换它(查询未命中而回退到通用形状不算修正);`user` 表示用户在设置中手改的值,永不被
替换。该字段可选,因此早于该标记写出的配置仍可读,旧客户端会忽略它。host-core 只
保留这两个取值、丢弃其它值,避免出现第三种无人识别的状态。解析规则见
[13-model-catalog-and-selection](13-model-catalog-and-selection.md) §9.1。
diff --git a/packages/shared/src/model-catalog.test.ts b/packages/shared/src/model-catalog.test.ts
index 6186b6740..d384720c5 100644
--- a/packages/shared/src/model-catalog.test.ts
+++ b/packages/shared/src/model-catalog.test.ts
@@ -6,6 +6,7 @@ import {
bindingSupportsDocuments,
effectiveContextWindow,
bindingSupportsImages,
+ resolveBindingContextWindow,
formatCompactTokenCount,
formatTokenCount,
modelMatchesFilter,
@@ -93,6 +94,27 @@ describe("binding context-window provenance", () => {
256_000,
);
});
+
+ it("keeps a catalog snapshot when the lookup falls back to the generic shape", () => {
+ // The bug this guards: a gateway row whose catalog lookup started missing
+ // (for example an id several publishers list) resolved to the generic
+ // 128k shape, which then replaced the saved 1M catalog snapshot and made
+ // automatic compaction start at ~86k tokens.
+ const binding = { contextWindow: 1_000_000, contextWindowSource: "catalog" as const };
+ const generic = { source: "generic", contextWindow: 128_000 };
+ const resolved = resolveBindingContextWindow(generic, binding);
+ expect(resolved.catalogConfig.contextWindow).toBe(1_000_000);
+ expect(resolved.binding.contextWindow).toBe(1_000_000);
+ expect(resolved.binding.contextWindowSource).toBe("catalog");
+ });
+
+ it("still follows a published record for a catalog-sourced window", () => {
+ const binding = { contextWindow: 1_000_000, contextWindowSource: "catalog" as const };
+ const published = { source: "models.dev", contextWindow: 1_048_576 };
+ const resolved = resolveBindingContextWindow(published, binding);
+ expect(resolved.catalogConfig.contextWindow).toBe(1_048_576);
+ expect(resolved.binding.contextWindow).toBe(1_048_576);
+ });
});
describe("provider API style compatibility", () => {
diff --git a/packages/shared/src/model-catalog.ts b/packages/shared/src/model-catalog.ts
index 913ec2bef..206df9563 100644
--- a/packages/shared/src/model-catalog.ts
+++ b/packages/shared/src/model-catalog.ts
@@ -191,27 +191,33 @@ type BindingContextWindow = Pick<ModelBinding, "contextWindow"> &
* same answer. An exported binding keeps the catalog as its provenance whenever
* the catalog supplied the value, so a later settings save cannot freeze an
* inherited value into a snapshot of its own.
+ *
+ * A `generic` baseline is the fallback for a lookup that found no record, not
+ * a published limit, so it never replaces a saved window: a catalog snapshot
+ * taken while the record still resolved stays in force.
*/
export function resolveBindingContextWindow<
- C extends { contextWindow?: number | null },
+ C extends { contextWindow?: number | null; source?: string },
B extends BindingContextWindow,
>(catalogConfig: C, binding: B): { catalogConfig: C; binding: B };
export function resolveBindingContextWindow<
- C extends { contextWindow?: number | null },
+ C extends { contextWindow?: number | null; source?: string },
B extends BindingContextWindow,
>(
catalogConfig: C,
binding: B | null | undefined,
): { catalogConfig: C; binding: B | null | undefined };
export function resolveBindingContextWindow(
- catalogConfig: { contextWindow?: number | null },
+ catalogConfig: { contextWindow?: number | null; source?: string },
binding: BindingContextWindow | null | undefined,
): {
- catalogConfig: { contextWindow?: number | null };
+ catalogConfig: { contextWindow?: number | null; source?: string };
binding: BindingContextWindow | null | undefined;
} {
if (!binding) return { catalogConfig, binding };
- const published = positiveTokenCount(catalogConfig.contextWindow);
+ const published = catalogConfig.source === "generic"
+ ? undefined
+ : positiveTokenCount(catalogConfig.contextWindow);
const source = binding.contextWindowSource ?? undefined;
const resolved = effectiveContextWindow(published, binding.contextWindow, source);
if (resolved === undefined) return { catalogConfig, binding };