Skip to content

Commit 4ceae82

Browse files
committed
fix: scope model provider discovery metadata to workspace
1 parent 4e7de4b commit 4ceae82

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎docs/plugins/architecture.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Gateway startup builds one `PluginMetadataSnapshot` for the current config snaps
151151

152152
Plugin-aware config validation, startup auto-enable, and Gateway plugin bootstrap consume that snapshot instead of rebuilding manifest/index metadata independently. `PluginLookUpTable` is derived from the same snapshot and adds the startup plugin plan for the current runtime config.
153153

154-
After startup, Gateway keeps the current metadata snapshot as a replaceable runtime product. Repeated runtime provider discovery can borrow that snapshot instead of reconstructing the installed index and manifest registry for each provider-catalog pass. The snapshot is cleared or replaced on Gateway shutdown, config/plugin inventory changes, and installed index writes; callers fall back to the cold manifest/index path when no compatible current snapshot exists.
154+
After startup, Gateway keeps the current metadata snapshot as a replaceable runtime product. Repeated runtime provider discovery can borrow that snapshot instead of reconstructing the installed index and manifest registry for each provider-catalog pass. The snapshot is cleared or replaced on Gateway shutdown, config/plugin inventory changes, and installed index writes; callers fall back to the cold manifest/index path when no compatible current snapshot exists. Compatibility checks must include plugin discovery roots such as `plugins.load.paths` and the default agent workspace, because workspace plugins are part of the metadata scope.
155155

156156
The snapshot and lookup table keep repeated startup decisions on the fast path:
157157

‎src/agents/models-config.applies-config-env-vars.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ describe("models-config", () => {
104104
expect(observedSnapshot).toBe(pluginMetadataSnapshot);
105105
});
106106

107+
it("threads workspace scope into implicit provider discovery", async () => {
108+
let observedWorkspaceDir: string | undefined;
109+
110+
await resolveProvidersForModelsJsonWithDeps(
111+
{
112+
cfg: { models: { providers: {} } },
113+
agentDir: "/tmp/openclaw-models-config-env-vars-test",
114+
env: {},
115+
workspaceDir: "/tmp/openclaw-workspace",
116+
},
117+
{
118+
resolveImplicitProviders: async ({ workspaceDir }) => {
119+
observedWorkspaceDir = workspaceDir;
120+
return {};
121+
},
122+
},
123+
);
124+
125+
expect(observedWorkspaceDir).toBe("/tmp/openclaw-workspace");
126+
});
127+
107128
it("threads plugin metadata snapshots through models.json planning", async () => {
108129
const pluginMetadataSnapshot = {
109130
index: { plugins: [] },

‎src/agents/models-config.plan.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type ResolveImplicitProvidersForModelsJson = (params: {
1919
agentDir: string;
2020
config: OpenClawConfig;
2121
env: NodeJS.ProcessEnv;
22+
workspaceDir?: string;
2223
explicitProviders: Record<string, ProviderConfig>;
2324
pluginMetadataSnapshot?: Pick<PluginMetadataSnapshot, "index" | "manifestRegistry" | "owners">;
2425
}) => Promise<Record<string, ProviderConfig>>;
@@ -40,6 +41,7 @@ export async function resolveProvidersForModelsJsonWithDeps(
4041
cfg: OpenClawConfig;
4142
agentDir: string;
4243
env: NodeJS.ProcessEnv;
44+
workspaceDir?: string;
4345
pluginMetadataSnapshot?: Pick<PluginMetadataSnapshot, "index" | "manifestRegistry" | "owners">;
4446
},
4547
deps?: {
@@ -53,6 +55,7 @@ export async function resolveProvidersForModelsJsonWithDeps(
5355
agentDir,
5456
config: cfg,
5557
env,
58+
...(params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}),
5659
explicitProviders,
5760
...(params.pluginMetadataSnapshot
5861
? { pluginMetadataSnapshot: params.pluginMetadataSnapshot }
@@ -94,6 +97,7 @@ export async function planOpenClawModelsJsonWithDeps(
9497
sourceConfigForSecrets?: OpenClawConfig;
9598
agentDir: string;
9699
env: NodeJS.ProcessEnv;
100+
workspaceDir?: string;
97101
existingRaw: string;
98102
existingParsed: unknown;
99103
pluginMetadataSnapshot?: Pick<PluginMetadataSnapshot, "index" | "manifestRegistry" | "owners">;
@@ -108,6 +112,7 @@ export async function planOpenClawModelsJsonWithDeps(
108112
cfg,
109113
agentDir,
110114
env,
115+
...(params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}),
111116
...(params.pluginMetadataSnapshot
112117
? { pluginMetadataSnapshot: params.pluginMetadataSnapshot }
113118
: {}),

‎src/agents/models-config.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-meta
1111
import { resolveInstalledManifestRegistryIndexFingerprint } from "../plugins/manifest-registry-installed.js";
1212
import type { PluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
1313
import { resolveOpenClawAgentDir } from "./agent-paths.js";
14+
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "./agent-scope.js";
1415
import { MODELS_JSON_STATE } from "./models-config-state.js";
1516
import { planOpenClawModelsJson } from "./models-config.plan.js";
1617

@@ -44,6 +45,7 @@ async function buildModelsJsonFingerprint(params: {
4445
config: OpenClawConfig;
4546
sourceConfigForSecrets: OpenClawConfig;
4647
agentDir: string;
48+
workspaceDir?: string;
4749
pluginMetadataSnapshot?: Pick<PluginMetadataSnapshot, "index">;
4850
}): Promise<string> {
4951
const authProfilesMtimeMs = await readFileMtimeMs(
@@ -60,6 +62,7 @@ async function buildModelsJsonFingerprint(params: {
6062
envShape,
6163
authProfilesMtimeMs,
6264
modelsFileMtimeMs,
65+
workspaceDir: params.workspaceDir,
6366
pluginMetadataSnapshotIndexFingerprint,
6467
});
6568
}
@@ -152,14 +155,17 @@ export async function ensureOpenClawModelsJson(
152155
): Promise<{ agentDir: string; wrote: boolean }> {
153156
const resolved = resolveModelsConfigInput(config);
154157
const cfg = resolved.config;
158+
const workspaceDir = resolveAgentWorkspaceDir(cfg, resolveDefaultAgentId(cfg));
155159
const pluginMetadataSnapshot =
156-
options.pluginMetadataSnapshot ?? getCurrentPluginMetadataSnapshot({ config: cfg });
160+
options.pluginMetadataSnapshot ??
161+
getCurrentPluginMetadataSnapshot({ config: cfg, workspaceDir });
157162
const agentDir = agentDirOverride?.trim() ? agentDirOverride.trim() : resolveOpenClawAgentDir();
158163
const targetPath = path.join(agentDir, "models.json");
159164
const fingerprint = await buildModelsJsonFingerprint({
160165
config: cfg,
161166
sourceConfigForSecrets: resolved.sourceConfigForSecrets,
162167
agentDir,
168+
workspaceDir,
163169
...(pluginMetadataSnapshot ? { pluginMetadataSnapshot } : {}),
164170
});
165171
const cached = MODELS_JSON_STATE.readyCache.get(targetPath);
@@ -181,6 +187,7 @@ export async function ensureOpenClawModelsJson(
181187
sourceConfigForSecrets: resolved.sourceConfigForSecrets,
182188
agentDir,
183189
env,
190+
workspaceDir,
184191
existingRaw: existingModelsFile.raw,
185192
existingParsed: existingModelsFile.parsed,
186193
...(pluginMetadataSnapshot ? { pluginMetadataSnapshot } : {}),

0 commit comments

Comments
 (0)