Skip to content

Commit d289e40

Browse files
committed
fix: keep model catalog reads read-only
1 parent 097a81f commit d289e40

3 files changed

Lines changed: 52 additions & 11 deletions

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let findModelInCatalog: typeof import("./model-catalog.js").findModelInCatalog;
99
let loadModelCatalog: typeof import("./model-catalog.js").loadModelCatalog;
1010
let resetModelCatalogCacheForTest: typeof import("./model-catalog.js").resetModelCatalogCacheForTest;
1111
let augmentCatalogMock: ReturnType<typeof vi.fn>;
12+
let ensureOpenClawModelsJsonMock: ReturnType<typeof vi.fn>;
1213

1314
vi.mock("./model-suppression.runtime.js", () => ({
1415
shouldSuppressBuiltInModel: (params: { provider?: string; id?: string }) =>
@@ -59,8 +60,9 @@ function mockSingleOpenAiCatalogModel() {
5960

6061
describe("loadModelCatalog", () => {
6162
beforeAll(async () => {
63+
ensureOpenClawModelsJsonMock = vi.fn().mockResolvedValue({ agentDir: "/tmp", wrote: false });
6264
vi.doMock("./models-config.js", () => ({
63-
ensureOpenClawModelsJson: vi.fn().mockResolvedValue({ agentDir: "/tmp", wrote: false }),
65+
ensureOpenClawModelsJson: ensureOpenClawModelsJsonMock,
6466
}));
6567
vi.doMock("./agent-paths.js", () => ({
6668
resolveOpenClawAgentDir: () => "/tmp/openclaw",
@@ -81,6 +83,7 @@ describe("loadModelCatalog", () => {
8183

8284
beforeEach(() => {
8385
resetModelCatalogCacheForTest();
86+
ensureOpenClawModelsJsonMock.mockClear();
8487
});
8588

8689
afterEach(() => {
@@ -146,6 +149,28 @@ describe("loadModelCatalog", () => {
146149
}
147150
});
148151

152+
it("does not prepare models.json when loading catalog in read-only mode", async () => {
153+
const discoverAuthStorage = vi.fn(() => ({}));
154+
__setModelCatalogImportForTest(
155+
async () =>
156+
({
157+
discoverAuthStorage,
158+
AuthStorage: function AuthStorage() {},
159+
ModelRegistry: class {
160+
getAll() {
161+
return [{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" }];
162+
}
163+
},
164+
}) as unknown as PiSdkModule,
165+
);
166+
167+
const result = await loadModelCatalog({ config: {} as OpenClawConfig, readOnly: true });
168+
169+
expect(result).toEqual([{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" }]);
170+
expect(ensureOpenClawModelsJsonMock).not.toHaveBeenCalled();
171+
expect(discoverAuthStorage).toHaveBeenCalledWith("/tmp/openclaw", { readOnly: true });
172+
});
173+
149174
it("does not synthesize stale openai-codex/gpt-5.3-codex-spark entries from gpt-5.4", async () => {
150175
mockPiDiscoveryModels([
151176
{

‎src/agents/model-catalog.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ function instantiatePiModelRegistry(
8181
export async function loadModelCatalog(params?: {
8282
config?: OpenClawConfig;
8383
useCache?: boolean;
84+
readOnly?: boolean;
8485
}): Promise<ModelCatalogEntry[]> {
85-
if (params?.useCache === false) {
86+
const readOnly = params?.readOnly === true;
87+
if (!readOnly && params?.useCache === false) {
8688
modelCatalogPromise = null;
8789
}
88-
if (modelCatalogPromise) {
90+
if (!readOnly && modelCatalogPromise) {
8991
return modelCatalogPromise;
9092
}
9193

92-
modelCatalogPromise = (async () => {
94+
const loadCatalog = async () => {
9395
const models: ModelCatalogEntry[] = [];
9496
const timingEnabled = shouldLogModelCatalogTiming();
9597
const startMs = timingEnabled ? Date.now() : 0;
@@ -110,8 +112,10 @@ export async function loadModelCatalog(params?: {
110112
});
111113
try {
112114
const cfg = params?.config ?? loadConfig();
113-
await ensureOpenClawModelsJson(cfg);
114-
logStage("models-json-ready");
115+
if (!readOnly) {
116+
await ensureOpenClawModelsJson(cfg);
117+
logStage("models-json-ready");
118+
}
115119
// IMPORTANT: keep the dynamic import *inside* the try/catch.
116120
// If this fails once (e.g. during a pnpm install that temporarily swaps node_modules),
117121
// we must not poison the cache with a rejected promise (otherwise all channel handlers
@@ -121,7 +125,10 @@ export async function loadModelCatalog(params?: {
121125
const agentDir = resolveOpenClawAgentDir();
122126
const { shouldSuppressBuiltInModel } = await loadModelSuppression();
123127
logStage("catalog-deps-ready");
124-
const authStorage = piSdk.discoverAuthStorage(agentDir);
128+
const authStorage = piSdk.discoverAuthStorage(
129+
agentDir,
130+
readOnly ? { readOnly: true } : undefined,
131+
);
125132
logStage("auth-storage-ready");
126133
const registry = instantiatePiModelRegistry(
127134
piSdk,
@@ -182,7 +189,9 @@ export async function loadModelCatalog(params?: {
182189

183190
if (models.length === 0) {
184191
// If we found nothing, don't cache this result so we can try again.
185-
modelCatalogPromise = null;
192+
if (!readOnly) {
193+
modelCatalogPromise = null;
194+
}
186195
}
187196

188197
const sorted = sortModels(models);
@@ -194,14 +203,21 @@ export async function loadModelCatalog(params?: {
194203
log.warn(`Failed to load model catalog: ${String(error)}`);
195204
}
196205
// Don't poison the cache on transient dependency/filesystem issues.
197-
modelCatalogPromise = null;
206+
if (!readOnly) {
207+
modelCatalogPromise = null;
208+
}
198209
if (models.length > 0) {
199210
return sortModels(models);
200211
}
201212
return [];
202213
}
203-
})();
214+
};
215+
216+
if (readOnly) {
217+
return loadCatalog();
218+
}
204219

220+
modelCatalogPromise = loadCatalog();
205221
return modelCatalogPromise;
206222
}
207223

‎src/commands/models/list.rows.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export async function appendCatalogSupplementRows(params: {
193193
context: RowBuilderContext;
194194
seenKeys: Set<string>;
195195
}): Promise<void> {
196-
const catalog = await loadModelCatalog({ config: params.context.cfg });
196+
const catalog = await loadModelCatalog({ config: params.context.cfg, readOnly: true });
197197
for (const entry of catalog) {
198198
if (
199199
params.context.filter.provider &&

0 commit comments

Comments
 (0)