Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (agy): clear a stale window map when every known model is delisted

[._.]/ Adam Eivy9ab720e326ba
diff --git a/server/lib/aiToolkit/internal/modelCatalog.js b/server/lib/aiToolkit/internal/modelCatalog.js
index a7abcd6491..d693139335 100644
--- a/server/lib/aiToolkit/internal/modelCatalog.js
+++ b/server/lib/aiToolkit/internal/modelCatalog.js
@@ -126,13 +126,23 @@ export function toModelCatalog(result) {
}
/**
- * The one place that decides whether `modelContextWindows` is written at all:
- * a non-empty map becomes a patch, an empty one writes nothing. Both the
- * refresh path and `createProvider` go through this so the rule can't be
- * relaxed on one side only.
+ * The one place that decides whether `modelContextWindows` is written at all.
+ * Both the refresh path and `createProvider` go through this so the rule can't
+ * be relaxed on one side only.
+ *
+ * `clearWhenEmpty` is what separates the two callers. `createProvider` builds a
+ * NEW record, so an empty map simply means "omit the field". A refresh PATCHES
+ * an existing record through `updateProvider`'s shallow merge, where an omitted
+ * key leaves the stored value untouched — so clearing a stale map there needs an
+ * explicit `undefined`. Without it the prune below would silently no-op in
+ * exactly the case it has the most to remove: every previously-known model
+ * delisted at once, leaving windows keyed to models the provider no longer
+ * serves (and still consulted for a `defaultModel` that dropped out of the list).
*/
-export const modelContextWindowPatch = (windows) =>
- windows && Object.keys(windows).length > 0 ? { modelContextWindows: { ...windows } } : {};
+export const modelContextWindowPatch = (windows, { clearWhenEmpty = false } = {}) => {
+ if (windows && Object.keys(windows).length > 0) return { modelContextWindows: { ...windows } };
+ return clearWhenEmpty ? { modelContextWindows: undefined } : {};
+};
/**
* The `updateProvider` patch a probed catalog produces.
@@ -160,5 +170,8 @@ export function modelCatalogUpdate(catalog, previousWindows) {
if (kept && listed.has(id)) merged[id] = kept;
}
Object.assign(merged, catalog.contextWindows || {});
- return { models: [...catalog.models], ...modelContextWindowPatch(merged) };
+ // Clear only when the record actually had windows — no need to write an
+ // explicit `undefined` onto a provider that never learned any.
+ const hadWindows = Boolean(previousWindows && Object.keys(previousWindows).length > 0);
+ return { models: [...catalog.models], ...modelContextWindowPatch(merged, { clearWhenEmpty: hadWindows }) };
}
diff --git a/server/lib/aiToolkit/internal/modelCatalog.test.js b/server/lib/aiToolkit/internal/modelCatalog.test.js
index 3e965b5798..77a87240ed 100644
--- a/server/lib/aiToolkit/internal/modelCatalog.test.js
+++ b/server/lib/aiToolkit/internal/modelCatalog.test.js
@@ -100,9 +100,23 @@ describe('modelCatalogUpdate', () => {
.toEqual({ a: 512_000 });
});
+ it('clears a stale map when every known model is delisted at once', () => {
+ // `updateProvider` shallow-merges, so an OMITTED key leaves the stored map
+ // in place — the prune would no-op in exactly the case it has the most to
+ // remove, leaving windows keyed to models the provider no longer serves.
+ const patch = modelCatalogUpdate({ models: ['fresh'], contextWindows: {} }, { gone: 64_000 });
+ expect('modelContextWindows' in patch).toBe(true);
+ expect(patch.modelContextWindows).toBeUndefined();
+ // And the shallow merge a real update performs actually drops it.
+ expect({ ...{ modelContextWindows: { gone: 64_000 } }, ...patch }.modelContextWindows).toBeUndefined();
+ });
+
it('omits the key entirely when nothing is known', () => {
- // Absent, not `{}` — a record that never refreshed stays clean.
- expect(modelCatalogUpdate({ models: ['a'], contextWindows: {} })).toEqual({ models: ['a'] });
+ // Absent, not `{}` and not an explicit `undefined` — a record that never
+ // learned a window has nothing to clear, so it stays clean.
+ const patch = modelCatalogUpdate({ models: ['a'], contextWindows: {} });
+ expect(patch).toEqual({ models: ['a'] });
+ expect('modelContextWindows' in patch).toBe(false);
});
it('always writes the model list, including a legitimately empty one', () => {