Skip to content

Commit 3f6f679

Browse files
jalehmansteipete
andauthored
fix: New Session adopts discovered models (#129307)
* oc-92a.8: converge dynamic chat metadata catalog * fix(gateway): converge discovered model catalogs across sessions Co-authored-by: Josh Lehman <[email protected]> * test(gateway): satisfy deferred helper lint * test(ui): bind model discovery proof to the active route * chore: keep release notes in pull request metadata * test(agents): preserve plugin metadata scope in model fixtures --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 35d9925 commit 3f6f679

22 files changed

Lines changed: 358 additions & 64 deletions

‎src/agents/embedded-agent-runner/model-resolution-consistency.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ vi.mock("./model.js", () => ({
6868
resolveModelAsync: resolveModelAsyncMock,
6969
}));
7070

71+
vi.mock("../provider-model-normalization.runtime.js", () => ({
72+
normalizeProviderModelIdWithRuntime: () => undefined,
73+
}));
74+
7175
vi.mock("../harness/runtime-plugin.js", () => ({
7276
ensureSelectedAgentHarnessPlugin: vi.fn(async () => undefined),
7377
}));

‎src/agents/prepared-model-catalog-worker.integration.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import {
2626
import {
2727
createPreparedModelCatalogWorker,
2828
createPreparedModelCatalogWorkerInput,
29-
getPreparedModelFullCatalogAuth,
3029
} from "./prepared-model-catalog-worker.js";
3130
import {
31+
getPreparedModelFullCatalogAuth,
3232
getPreparedModelRuntimeAuthStore,
3333
loadPreparedModelRuntimeAuth,
3434
setPreparedModelRuntimeAuthLoader,

‎src/agents/prepared-model-catalog-worker.ts‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import type { PreparedAgentCredentialModes } from "./agent-auth-credential-modes
88
import { cloneAuthProfileStore } from "./auth-profiles/clone.js";
99
import type { AuthProfileStore } from "./auth-profiles/types.js";
1010
import type { ModelCatalogSnapshot } from "./model-catalog.types.js";
11-
import type {
12-
PreparedModelRuntimeAuth,
13-
PreparedModelRuntimeAuthScope,
11+
import {
12+
setPreparedModelFullCatalogAuth,
13+
type PreparedModelRuntimeAuth,
14+
type PreparedModelRuntimeAuthScope,
1415
} from "./prepared-model-runtime-auth.js";
1516
import type { PreparedModelRuntimeAgentFacts } from "./prepared-model-runtime.catalog-contract.js";
1617
import { PreparedModelRuntimePublicationSupersededError } from "./prepared-model-runtime.errors.js";
@@ -62,22 +63,6 @@ export type PreparedModelWorkerResult =
6263
}>
6364
| Readonly<{ status: "failed"; requestId: number; error: string }>;
6465

65-
const authByFullCatalog = new WeakMap<
66-
object,
67-
Readonly<{ authStore: AuthProfileStore; authModes: PreparedAgentCredentialModes }>
68-
>();
69-
70-
function setPreparedModelFullCatalogAuth(
71-
modelCatalog: object,
72-
auth: Readonly<{ authStore: AuthProfileStore; authModes: PreparedAgentCredentialModes }>,
73-
): void {
74-
authByFullCatalog.set(modelCatalog, auth);
75-
}
76-
77-
export function getPreparedModelFullCatalogAuth(modelCatalog: object) {
78-
return authByFullCatalog.get(modelCatalog);
79-
}
80-
8166
// Cold source/plugin loading can take well over a minute. Three minutes preserves exact full-view
8267
// discovery while bounding a wedged provider; expiry rejects and never returns partial results.
8368
const PREPARED_MODEL_CATALOG_WORKER_TIMEOUT_MS = 180_000;

‎src/agents/prepared-model-catalog.test.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ const mocks = vi.hoisted(() => ({
1010
loadSnapshot: vi.fn(),
1111
prepareSnapshot: vi.fn(),
1212
prepareScopedCatalog: vi.fn(),
13-
fullCatalogAuth: undefined as
14-
| undefined
15-
| { authStore: { version: number; profiles: object }; authModes: object },
1613
isFullCatalog: vi.fn(),
1714
releaseSnapshot: vi.fn(),
1815
}));
@@ -57,10 +54,6 @@ vi.mock("./prepared-model-runtime.full-catalog.js", () => ({
5754
isPreparedModelCatalogFull: (...args: unknown[]) => mocks.isFullCatalog(...args),
5855
}));
5956

60-
vi.mock("./prepared-model-catalog-worker.js", () => ({
61-
getPreparedModelFullCatalogAuth: () => mocks.fullCatalogAuth,
62-
}));
63-
6457
vi.mock("./prepared-model-runtime.scoped-catalog.js", () => ({
6558
prepareScopedReadOnlyModelCatalog: (...args: unknown[]) => mocks.prepareScopedCatalog(...args),
6659
}));
@@ -77,6 +70,7 @@ import {
7770
} from "./prepared-model-catalog.js";
7871
import {
7972
getPreparedModelRuntimeAuthStore,
73+
setPreparedModelFullCatalogAuth,
8074
setPreparedModelRuntimeAuthStore,
8175
} from "./prepared-model-runtime-auth.js";
8276
import { PreparedModelRuntimeOwnerNotPublishedError } from "./prepared-model-runtime.js";
@@ -106,7 +100,6 @@ describe("prepared model catalog access", () => {
106100
mocks.loadSnapshot.mockReset();
107101
mocks.prepareSnapshot.mockReset();
108102
mocks.prepareScopedCatalog.mockReset();
109-
mocks.fullCatalogAuth = undefined;
110103
mocks.isFullCatalog.mockReset();
111104
mocks.releaseSnapshot.mockReset();
112105
});
@@ -208,7 +201,7 @@ describe("prepared model catalog access", () => {
208201
routeVariants: [],
209202
};
210203
const { authStore, ...snapshotFacts } = fullSnapshot;
211-
mocks.fullCatalogAuth = { authStore, authModes: {} };
204+
setPreparedModelFullCatalogAuth(discoveredCatalog, { authStore, authModes: {} });
212205
const loadFullModelCatalog = vi.fn(async () => discoveredCatalog);
213206
const snapshot = {
214207
...snapshotFacts,

‎src/agents/prepared-model-catalog.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {
1010
import { resolveLegacyInheritedAuthDir } from "./legacy-inherited-auth-dir.js";
1111
import type { ModelCatalogEntry, ModelCatalogSnapshot } from "./model-catalog.types.js";
1212
import { resolvePublishedModelCatalogOwner } from "./prepared-model-catalog-owner.js";
13-
import { getPreparedModelFullCatalogAuth } from "./prepared-model-catalog-worker.js";
1413
import { PreparedModelCatalogConfigReplacedError } from "./prepared-model-catalog.errors.js";
1514
import type { ResolvedPublishedModelCatalogOwner } from "./prepared-model-catalog.types.js";
1615
import {
16+
getPreparedModelFullCatalogAuth,
1717
getPreparedModelRuntimeAuthMaterializations,
1818
loadPreparedModelRuntimeAuth,
1919
setPreparedModelRuntimeAuthMaterializations,

‎src/agents/prepared-model-runtime-auth.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const authLoaderBySnapshot = new WeakMap<
1919
object,
2020
(scope: PreparedModelRuntimeAuthScope) => Promise<PreparedModelRuntimeAuth>
2121
>();
22+
const authByFullCatalog = new WeakMap<object, PreparedModelRuntimeAuth>();
2223

2324
// Secret-bearing state stays lifecycle-owned without becoming part of the public snapshot shape.
2425
export function setPreparedModelRuntimeAuthStore(
@@ -32,6 +33,17 @@ export function getPreparedModelRuntimeAuthStore(snapshot: object): AuthProfileS
3233
return authStoreBySnapshot.get(snapshot);
3334
}
3435

36+
export function setPreparedModelFullCatalogAuth(
37+
snapshot: object,
38+
auth: PreparedModelRuntimeAuth,
39+
): void {
40+
authByFullCatalog.set(snapshot, auth);
41+
}
42+
43+
export function getPreparedModelFullCatalogAuth(snapshot: object) {
44+
return authByFullCatalog.get(snapshot);
45+
}
46+
3547
export function setPreparedModelRuntimeAuthLoader(
3648
snapshot: object,
3749
loader: (scope: PreparedModelRuntimeAuthScope) => Promise<PreparedModelRuntimeAuth>,

‎src/agents/prepared-model-runtime.build.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
createPreparedInboundRegistryLoader,
3737
preparedModelRuntimeWorkspaceFactsKey,
3838
} from "./prepared-model-runtime.inbound-registry.js";
39+
import { notifyPreparedModelRuntimePublication } from "./prepared-model-runtime.publication-events.js";
3940
import type {
4041
PreparedModelRuntimeBuildStats,
4142
PreparedModelRuntimeCatalogMode,
@@ -214,6 +215,7 @@ function createFullModelCatalogAccess(params: {
214215
pending = build
215216
.then((catalog) => {
216217
fullCatalog = catalog;
218+
notifyPreparedModelRuntimePublication({ phase: "catalog-published" });
217219
return catalog;
218220
})
219221
.finally(() => {

‎src/agents/prepared-model-runtime.publication-events.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createSubsystemLogger } from "../logging/subsystem.js";
33
const log = createSubsystemLogger("agents/prepared-model-runtime");
44

55
type PreparedModelRuntimePublicationEvent =
6-
| { phase: "invalidated" | "published" }
6+
| { phase: "catalog-published" | "invalidated" | "published" }
77
| { phase: "failed"; error: Error };
88

99
const publicationListeners = new Set<(event: PreparedModelRuntimePublicationEvent) => void>();

‎src/agents/prepared-model-runtime.startup-static.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,11 @@ vi.mock("../logging/subsystem.js", () => ({
209209
createSubsystemLogger: () => ({ warn: vi.fn() }),
210210
}));
211211

212-
const { getPreparedModelRuntimeSnapshot, refreshPreparedModelRuntimeSnapshots } =
213-
await import("./prepared-model-runtime.js");
212+
const {
213+
getPreparedModelRuntimeSnapshot,
214+
refreshPreparedModelRuntimeSnapshots,
215+
registerPreparedModelRuntimePublicationListener,
216+
} = await import("./prepared-model-runtime.js");
214217
const { getAvailablePreparedModelCatalogSnapshot } = await import("./prepared-model-catalog.js");
215218
const { prepareScopedReadOnlyLiveModelCatalog, prepareScopedReadOnlyModelCatalog } =
216219
await import("./prepared-model-runtime.scoped-catalog.js");
@@ -431,7 +434,12 @@ describe("prepared model runtime Gateway catalog mode", () => {
431434
expect(snapshot?.pluginRegistry).toBeDefined();
432435
expect(snapshot?.messageToolCatalog).toBeUndefined();
433436
expect(snapshot?.mediaCapabilityProviders).toBeDefined();
437+
const catalogPublicationEvents: string[] = [];
438+
const unregisterCatalogPublication = registerPreparedModelRuntimePublicationListener((event) =>
439+
catalogPublicationEvents.push(event.phase),
440+
);
434441
await snapshot?.loadFullModelCatalog?.();
442+
expect(catalogPublicationEvents).toEqual(["catalog-published"]);
435443
expect(mocks.ensureOpenClawModelsJson).not.toHaveBeenCalled();
436444
expect(mocks.runPreparedModelCatalogWorker).toHaveBeenCalledOnce();
437445
expect(mocks.loadAgentRuntimePluginRegistryHandle).toHaveBeenCalledTimes(2);
@@ -452,13 +460,17 @@ describe("prepared model runtime Gateway catalog mode", () => {
452460
}),
453461
).toEqual({ entries: [], routeVariants: [] });
454462
expect(mocks.runPreparedModelCatalogWorker).toHaveBeenCalledOnce();
463+
expect(catalogPublicationEvents).toEqual(["catalog-published"]);
455464

456465
await snapshot?.loadFullModelCatalog?.({ refresh: true });
466+
expect(catalogPublicationEvents).toEqual(["catalog-published", "catalog-published"]);
457467
expect(mocks.runPreparedModelCatalogWorker).toHaveBeenCalledTimes(2);
458468
mocks.runPreparedModelCatalogWorker.mockRejectedValueOnce(new Error("refresh failed"));
459469
await expect(snapshot?.loadFullModelCatalog?.({ refresh: true })).rejects.toThrow(
460470
"refresh failed",
461471
);
472+
expect(catalogPublicationEvents).toEqual(["catalog-published", "catalog-published"]);
473+
unregisterCatalogPublication();
462474
expect(snapshot?.readFullModelCatalog?.()).toEqual({ entries: [], routeVariants: [] });
463475
expect(mocks.runPreparedModelCatalogWorker).toHaveBeenCalledTimes(3);
464476
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();

‎src/gateway/server-chat-metadata-lifecycle.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { createDeferred } from "../../test/helpers/promise.js";
23
import type { OpenClawConfig } from "../config/types.openclaw.js";
34
import type { GatewayRequestContext } from "./server-methods/types.js";
45

@@ -151,10 +152,11 @@ describe("gateway chat metadata lifecycle", () => {
151152
expect(skillsListener).toEqual(expect.any(Function));
152153

153154
modelListener({ phase: "invalidated" });
155+
modelListener({ phase: "catalog-published" });
154156
authListener();
155157
skillsListener();
156158

157-
expect(mocks.invalidate).toHaveBeenCalledTimes(3);
159+
expect(mocks.invalidate).toHaveBeenCalledTimes(4);
158160
expect(mocks.refresh).toHaveBeenCalledOnce();
159161
expect(warn).not.toHaveBeenCalled();
160162

@@ -178,6 +180,41 @@ describe("gateway chat metadata lifecycle", () => {
178180
await vi.waitFor(() => expect(mocks.refresh).toHaveBeenCalledTimes(3));
179181
});
180182

183+
it("refreshes after the prepared owner publishes a completed full catalog", async () => {
184+
const { lifecycle: pendingLifecycle } = createLifecycle(false);
185+
const lifecycle = await pendingLifecycle;
186+
187+
await lifecycle.attachContext(context, []);
188+
const modelListener = mocks.registerModelListener.mock.calls[0]?.[0];
189+
modelListener({ phase: "published" });
190+
await vi.waitFor(() => expect(mocks.refresh).toHaveBeenCalledTimes(2));
191+
mocks.invalidate.mockClear();
192+
193+
modelListener({ phase: "catalog-published" });
194+
195+
expect(mocks.invalidate).toHaveBeenCalledOnce();
196+
await vi.waitFor(() => expect(mocks.refresh).toHaveBeenCalledTimes(3));
197+
});
198+
199+
it("keeps an owner available when a subordinate catalog publishes during attachment", async () => {
200+
const pendingRefresh = createDeferred();
201+
mocks.refresh.mockReturnValueOnce(pendingRefresh.promise);
202+
const { lifecycle: pendingLifecycle } = createLifecycle(false);
203+
const lifecycle = await pendingLifecycle;
204+
205+
const attachment = lifecycle.attachContext(context, []);
206+
await vi.waitFor(() => expect(mocks.refresh).toHaveBeenCalledOnce());
207+
const modelListener = mocks.registerModelListener.mock.calls[0]?.[0];
208+
modelListener({ phase: "catalog-published" });
209+
pendingRefresh.resolve();
210+
await attachment;
211+
212+
const authListener = mocks.registerAuthListener.mock.calls[0]?.[0];
213+
authListener();
214+
215+
await vi.waitFor(() => expect(mocks.refresh).toHaveBeenCalledTimes(2));
216+
});
217+
181218
it("propagates a failed model publication without starting a refresh", async () => {
182219
const { lifecycle: pendingLifecycle } = createLifecycle(false);
183220
const lifecycle = await pendingLifecycle;

0 commit comments

Comments
 (0)