Skip to content

Commit 732f00f

Browse files
authored
refactor(agents): unify prepared runtime publication (#118414)
1 parent f792cba commit 732f00f

6 files changed

Lines changed: 275 additions & 547 deletions

‎src/agents/prepared-model-runtime.lifecycle.test.ts‎

Lines changed: 11 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,5 @@
1+
import "./prepared-model-runtime.test-harness.js";
12
import { beforeEach, describe, expect, it, vi } from "vitest";
2-
import { createEmptyPluginRegistry } from "../plugins/registry-empty.js";
3-
4-
type LoadStaticCatalog =
5-
typeof import("./embedded-agent-runner/model.static-catalog.js").loadBundledProviderStaticCatalogContextModels;
6-
7-
const mocks = vi.hoisted(() => ({
8-
authStorage: {
9-
getAll: vi.fn(() => ({ custom: { type: "api_key", key: "test-key" } })),
10-
getOAuthProviders: vi.fn(() => []),
11-
},
12-
modelRegistry: {
13-
fork: vi.fn((authStorage: unknown) => ({ authStorage })),
14-
getAll: vi.fn(() => []),
15-
find: vi.fn(() => null),
16-
},
17-
resolveAmbientCredentials: vi.fn((..._args: unknown[]) => ({})),
18-
discoverAuthStorage: vi.fn((..._args: unknown[]) => undefined as unknown),
19-
discoverModels: vi.fn(),
20-
ensureOpenClawModelsJson: vi.fn(async (..._args: unknown[]) => ({
21-
agentDir: "/tmp/agent",
22-
wrote: false,
23-
})),
24-
planOpenClawModelsJsonSource: vi.fn(async (...args: unknown[]) => ({
25-
agentDir: String(args[1]),
26-
modelsJsonContents: null,
27-
pluginCatalogs: [],
28-
})),
29-
buildPreparedModelCatalogSnapshot: vi.fn(async (..._args: unknown[]) => ({
30-
entries: [],
31-
routeVariants: [],
32-
})),
33-
loadAgentRuntimePluginRegistryHandle: vi.fn(),
34-
loadStaticCatalog: vi.fn<LoadStaticCatalog>(async () => []),
35-
prepareStaticCatalog: vi.fn(async (..._args: unknown[]) => ({ entries: [] })),
36-
resolveStaticCatalogModel: vi.fn(() => undefined),
37-
configuredAgentIds: [] as string[],
38-
configuredAgentDirs: new Map<string, string>(),
39-
configuredWorkspaces: new Map<string, string>(),
40-
warn: vi.fn(),
41-
mutationListener: undefined as
42-
| ((event: { agentDir?: string; affectsInheritedStores: boolean }) => void)
43-
| undefined,
44-
}));
45-
46-
vi.mock("./model-catalog.js", () => ({
47-
buildPreparedModelCatalogSnapshot: (...args: unknown[]) =>
48-
mocks.buildPreparedModelCatalogSnapshot(...args),
49-
}));
50-
51-
vi.mock("./agent-auth-discovery.js", () => ({
52-
resolveAmbientAgentCredentialsForDiscovery: (...args: unknown[]) =>
53-
mocks.resolveAmbientCredentials(...args),
54-
}));
55-
56-
vi.mock("./agent-model-discovery.js", () => ({
57-
discoverAuthStorage: (...args: unknown[]) => {
58-
return mocks.discoverAuthStorage(...args) ?? mocks.authStorage;
59-
},
60-
discoverModels: (...args: unknown[]) => {
61-
mocks.discoverModels(...args);
62-
return mocks.modelRegistry;
63-
},
64-
discoverModelsFromCapturedSources: (...args: unknown[]) => {
65-
mocks.discoverModels(...args);
66-
return mocks.modelRegistry;
67-
},
68-
}));
69-
70-
vi.mock("../plugins/synthetic-auth.runtime.js", () => ({
71-
resolveRuntimeSyntheticAuthProviderRefs: () => [],
72-
}));
73-
74-
vi.mock("./agent-scope.js", () => ({
75-
listAgentEntries: (config: { agents?: { list?: unknown[] } }) => config.agents?.list ?? [],
76-
listAgentIds: () => mocks.configuredAgentIds,
77-
resolveAgentDir: (_config: unknown, agentId: string) =>
78-
mocks.configuredAgentDirs.get(agentId) ??
79-
(agentId === "default" ? "/tmp/unused-agent" : `/tmp/configured-${agentId}`),
80-
resolveAgentWorkspaceDir: (_config: unknown, agentId: string) =>
81-
mocks.configuredWorkspaces.get(agentId) ??
82-
(agentId === "default" ? "/tmp/unused-workspace" : `/tmp/workspace-${agentId}`),
83-
resolveDefaultAgentDir: () => "/tmp/unused-agent",
84-
resolveDefaultAgentId: () => "default",
85-
resolveAgentEffectiveModelPrimary: () => undefined,
86-
resolveRunModelFallbacksOverride: () => undefined,
87-
resolveSessionAgentIds: ({ agentId }: { agentId?: string }) => ({
88-
defaultAgentId: "default",
89-
sessionAgentId: agentId ?? "default",
90-
}),
91-
}));
92-
93-
vi.mock("./auth-profiles/runtime-snapshots.js", () => ({
94-
registerRuntimeAuthProfileStoreMutationListener: (
95-
listener: (event: { agentDir?: string; affectsInheritedStores: boolean }) => void,
96-
) => {
97-
mocks.mutationListener = listener;
98-
return () => {};
99-
},
100-
}));
101-
102-
vi.mock("./model-discovery-context.js", () => ({
103-
resolveModelPluginMetadataSnapshot: () => undefined,
104-
}));
105-
106-
vi.mock("./models-config.js", () => ({
107-
ensureOpenClawModelsJson: (...args: unknown[]) => mocks.ensureOpenClawModelsJson(...args),
108-
planOpenClawModelsJsonSource: (...args: unknown[]) => mocks.planOpenClawModelsJsonSource(...args),
109-
}));
110-
111-
vi.mock("./models-config.providers.implicit.js", () => ({
112-
prepareImplicitProviderStaticCatalog: (...args: unknown[]) => mocks.prepareStaticCatalog(...args),
113-
}));
114-
115-
vi.mock("./runtime-plugins.js", () => ({
116-
loadAgentRuntimePluginRegistryHandle: (...args: unknown[]) =>
117-
mocks.loadAgentRuntimePluginRegistryHandle(...args),
118-
}));
119-
120-
vi.mock("./embedded-agent-runner/model.static-catalog.js", () => ({
121-
loadBundledProviderStaticCatalogContextModels: (...args: Parameters<LoadStaticCatalog>) =>
122-
mocks.loadStaticCatalog(...args),
123-
createBundledStaticCatalogModelResolver: () => mocks.resolveStaticCatalogModel,
124-
}));
125-
126-
vi.mock("../logging/subsystem.js", () => ({
127-
createSubsystemLogger: () => ({ warn: mocks.warn }),
128-
}));
129-
1303
import {
1314
acquireAgentRunPreparedModelRuntime,
1325
acquireReadOnlyPreparedModelRuntime,
@@ -136,43 +9,17 @@ import {
1369
publishPreparedModelRuntimeSnapshot,
13710
refreshPreparedModelRuntimeSnapshots,
13811
} from "./prepared-model-runtime.js";
12+
import {
13+
getPreparedModelRuntimeMocks,
14+
getPreparedModelRuntimeTestApi,
15+
resetPreparedModelRuntimeHarness,
16+
} from "./prepared-model-runtime.test-harness.js";
13917

140-
describe("prepared model runtime snapshots", () => {
141-
const getTesting = () =>
142-
(globalThis as Record<PropertyKey, unknown>)[
143-
Symbol.for("openclaw.preparedModelRuntimeTestApi")
144-
] as {
145-
resetPreparedModelRuntimeSnapshotsForTest: () => void;
146-
setModelRuntimeBuildTimeoutMsForTest: (timeoutMs: number) => void;
147-
};
18+
const mocks = getPreparedModelRuntimeMocks();
14819

20+
describe("prepared model runtime snapshots", () => {
14921
beforeEach(() => {
150-
getTesting().resetPreparedModelRuntimeSnapshotsForTest();
151-
mocks.discoverAuthStorage.mockClear();
152-
mocks.resolveAmbientCredentials.mockClear();
153-
mocks.discoverAuthStorage.mockImplementation(() => mocks.authStorage);
154-
mocks.discoverModels.mockClear();
155-
mocks.ensureOpenClawModelsJson.mockReset();
156-
mocks.ensureOpenClawModelsJson.mockResolvedValue({ agentDir: "/tmp/agent", wrote: false });
157-
mocks.planOpenClawModelsJsonSource.mockReset();
158-
mocks.planOpenClawModelsJsonSource.mockImplementation(async (_config, agentDir) => ({
159-
agentDir: String(agentDir),
160-
modelsJsonContents: null,
161-
pluginCatalogs: [],
162-
}));
163-
mocks.buildPreparedModelCatalogSnapshot.mockClear();
164-
mocks.loadAgentRuntimePluginRegistryHandle
165-
.mockReset()
166-
.mockReturnValue(createEmptyPluginRegistry());
167-
mocks.loadStaticCatalog.mockClear();
168-
mocks.prepareStaticCatalog.mockClear();
169-
mocks.resolveStaticCatalogModel.mockClear();
170-
mocks.modelRegistry.fork.mockClear();
171-
mocks.modelRegistry.find.mockClear();
172-
mocks.warn.mockClear();
173-
mocks.configuredAgentIds = [];
174-
mocks.configuredAgentDirs.clear();
175-
mocks.configuredWorkspaces.clear();
22+
resetPreparedModelRuntimeHarness();
17623
});
17724

17825
it("does not discover missing owners from a gateway request", async () => {
@@ -637,7 +484,7 @@ describe("prepared model runtime snapshots", () => {
637484
});
638485

639486
it("fails a timed-out publication without overlapping its late build with a retry", async () => {
640-
getTesting().setModelRuntimeBuildTimeoutMsForTest(1);
487+
getPreparedModelRuntimeTestApi().setModelRuntimeBuildTimeoutMsForTest(1);
641488
let finishTimedOutBuild: (() => void) | undefined;
642489
mocks.ensureOpenClawModelsJson.mockImplementationOnce(
643490
async () =>
@@ -799,7 +646,7 @@ describe("prepared model runtime snapshots", () => {
799646
});
800647

801648
it("does not replay an auth mutation that occurs before the first owner is registered", async () => {
802-
getTesting().setModelRuntimeBuildTimeoutMsForTest(100);
649+
getPreparedModelRuntimeTestApi().setModelRuntimeBuildTimeoutMsForTest(100);
803650
mocks.configuredAgentIds = ["default"];
804651
mocks.prepareStaticCatalog.mockImplementationOnce(async () => {
805652
mocks.mutationListener?.({ affectsInheritedStores: true });

0 commit comments

Comments
 (0)