Skip to content

Commit ed9c67f

Browse files
committed
fix(models): require prefetched promotions refresh
1 parent 2da8e56 commit ed9c67f

4 files changed

Lines changed: 111 additions & 24 deletions

File tree

‎src/commands/models/list.list-command.forward-compat.test.ts‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,68 @@ describe("modelsListCommand forward-compat", () => {
400400

401401
it("preserves the human-readable message for an empty model list", async () => {
402402
mocks.resolveConfiguredEntries.mockReturnValueOnce({ entries: [] });
403+
const refreshToken = { nowMs: 1, statePromise: Promise.resolve() };
404+
mocks.startPromotionsFeedRefresh.mockReturnValueOnce(refreshToken);
403405
const runtime = createRuntime();
404406

405407
await modelsListCommand({ provider: "autoqa-no-such-provider" }, runtime as never);
406408

407409
expect(runtime.log).toHaveBeenCalledWith("No models found.");
408410
expect(mocks.printModelTable).not.toHaveBeenCalled();
411+
expect(mocks.startPromotionsFeedRefresh).toHaveBeenCalledOnce();
412+
expect(mocks.printAvailablePromotionsSection).toHaveBeenCalledWith(
413+
expect.objectContaining({ refresh: refreshToken }),
414+
);
409415
});
410416
});
411417

412418
describe("promotion refresh scheduling", () => {
413-
it("constructs rows before refresh resolves and appends promotion output after the table", async () => {
419+
it("does not start refresh when config resolution fails", async () => {
420+
mocks.loadModelsConfigWithSource.mockRejectedValueOnce(new Error("config failed"));
421+
422+
await expect(modelsListCommand({}, createRuntime() as never)).rejects.toThrow(
423+
"config failed",
424+
);
425+
426+
expect(mocks.startPromotionsFeedRefresh).not.toHaveBeenCalled();
427+
});
428+
429+
it("does not start refresh when registry loading fails", async () => {
430+
mocks.loadModelRegistry.mockRejectedValueOnce(new Error("registry failed"));
431+
const runtime = createRuntime();
432+
433+
await modelsListCommand({ all: true }, runtime as never);
434+
435+
expect(runtime.error).toHaveBeenCalledWith(expect.stringContaining("registry failed"));
436+
expect(mocks.startPromotionsFeedRefresh).not.toHaveBeenCalled();
437+
expect(mocks.printAvailablePromotionsSection).not.toHaveBeenCalled();
438+
});
439+
440+
it.each([{ json: true }, { plain: true }])(
441+
"does not start refresh for machine output",
442+
async (options) => {
443+
await modelsListCommand(options, createRuntime() as never);
444+
445+
expect(mocks.startPromotionsFeedRefresh).not.toHaveBeenCalled();
446+
expect(mocks.printAvailablePromotionsSection).not.toHaveBeenCalled();
447+
},
448+
);
449+
450+
it("starts refresh before row construction finishes and appends output after the table", async () => {
414451
const refresh = createDeferred();
452+
const rowConstructionStarted = createDeferred();
453+
const releaseRowConstruction = createDeferred();
415454
const tablePrinted = createDeferred();
416-
mocks.startPromotionsFeedRefresh.mockReturnValueOnce({
455+
const refreshToken = {
417456
nowMs: 1,
418457
statePromise: refresh.promise,
458+
};
459+
mocks.loadModelCatalog.mockImplementationOnce(async () => {
460+
rowConstructionStarted.resolve();
461+
await releaseRowConstruction.promise;
462+
return [];
419463
});
464+
mocks.startPromotionsFeedRefresh.mockReturnValueOnce(refreshToken);
420465
mocks.printModelTable.mockImplementationOnce((_rows, runtime) => {
421466
runtime.log("model table");
422467
tablePrinted.resolve();
@@ -430,16 +475,45 @@ describe("modelsListCommand forward-compat", () => {
430475
const runtime = createRuntime();
431476

432477
const commandPromise = modelsListCommand({}, runtime as never);
478+
await rowConstructionStarted.promise;
479+
480+
expect(mocks.startPromotionsFeedRefresh).toHaveBeenCalledOnce();
481+
expect(mocks.printModelTable).not.toHaveBeenCalled();
482+
483+
releaseRowConstruction.resolve();
433484
await tablePrinted.promise;
434485

435486
expectRowKeys(lastPrintedRows<{ key: string }>(), ["openai/gpt-5.4"]);
436-
expect(mocks.startPromotionsFeedRefresh).toHaveBeenCalledOnce();
437487
expect(runtime.log.mock.calls).toEqual([["model table"]]);
438488

439489
refresh.resolve();
440490
await commandPromise;
441491

442492
expect(runtime.log.mock.calls).toEqual([["model table"], ["promotion section"]]);
493+
expect(mocks.printAvailablePromotionsSection).toHaveBeenCalledWith(
494+
expect.objectContaining({ refresh: refreshToken }),
495+
);
496+
});
497+
498+
it("keeps a rejected refresh fail-silent", async () => {
499+
const refresh = createDeferred();
500+
const sectionStarted = createDeferred();
501+
mocks.startPromotionsFeedRefresh.mockReturnValueOnce({
502+
nowMs: 1,
503+
statePromise: refresh.promise,
504+
});
505+
mocks.printAvailablePromotionsSection.mockImplementationOnce(
506+
async ({ refresh: pendingRefresh }) => {
507+
sectionStarted.resolve();
508+
await pendingRefresh.statePromise;
509+
},
510+
);
511+
512+
const commandPromise = modelsListCommand({}, createRuntime() as never);
513+
await sectionStarted.promise;
514+
refresh.reject(new Error("refresh failed"));
515+
516+
await expect(commandPromise).resolves.toBeUndefined();
443517
});
444518
});
445519

‎src/commands/models/list.list-command.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export async function modelsListCommand(
7272
return;
7373
}
7474
const humanReadable = !opts.json && !opts.plain;
75-
const promotionsModulePromise = humanReadable ? promotionsModuleLoader.load() : undefined;
7675
const [
7776
{ loadAuthProfileStoreWithoutExternalProfiles },
7877
{ resolveAgentWorkspaceDir, resolveDefaultAgentDir, resolveDefaultAgentId },
@@ -102,9 +101,6 @@ export async function modelsListCommand(
102101
})
103102
: undefined;
104103
const { entries } = resolveConfiguredEntries(cfg, metadataSnapshot);
105-
const promotionsRefreshPromise = promotionsModulePromise?.then((promotionsModule) =>
106-
promotionsModule.startPromotionsFeedRefresh(),
107-
);
108104
const authIndex = createModelListAuthIndex({
109105
cfg,
110106
authStore,
@@ -188,6 +184,10 @@ export async function modelsListCommand(
188184
process.exitCode = 1;
189185
return;
190186
}
187+
const promotionsModulePromise = humanReadable ? promotionsModuleLoader.load() : undefined;
188+
const promotionsRefreshPromise = promotionsModulePromise
189+
?.then((promotionsModule) => promotionsModule.startPromotionsFeedRefresh())
190+
.catch(() => undefined);
191191
const buildRowContext = (skipRuntimeModelSuppression: boolean) => ({
192192
cfg,
193193
agentId,
@@ -255,11 +255,14 @@ export async function modelsListCommand(
255255
// the configured entries, not the rendered rows — filtered and --all
256256
// listings show a different set.
257257
try {
258-
await promotionsModule.printAvailablePromotionsSection({
259-
configuredKeys: new Set(entries.map((entry) => entry.key)),
260-
refresh: await promotionsRefreshPromise,
261-
runtime,
262-
});
258+
const refresh = await promotionsRefreshPromise;
259+
if (refresh) {
260+
await promotionsModule.printAvailablePromotionsSection({
261+
configuredKeys: new Set(entries.map((entry) => entry.key)),
262+
refresh,
263+
runtime,
264+
});
265+
}
263266
} catch {
264267
// Passive discovery must never fail the listing.
265268
}

‎src/commands/models/list.promotions.test.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
createOpenClawTestState,
99
type OpenClawTestState,
1010
} from "../../test-utils/openclaw-test-state.js";
11-
import { applyPromotionClaimTags, printAvailablePromotionsSection } from "./list.promotions.js";
11+
import {
12+
applyPromotionClaimTags,
13+
printAvailablePromotionsSection,
14+
startPromotionsFeedRefresh,
15+
} from "./list.promotions.js";
1216
import type { ModelRow } from "./list.types.js";
1317

1418
const NOW = Date.parse("2026-07-05T12:00:00.000Z");
@@ -116,8 +120,8 @@ describe("models list promotion decorations", () => {
116120
const { runtime, lines } = makeRuntime();
117121
await printAvailablePromotionsSection({
118122
configuredKeys: new Set(["other/model"]),
123+
refresh: startPromotionsFeedRefresh(NOW),
119124
runtime,
120-
nowMs: NOW,
121125
});
122126
const text = lines.join("\n");
123127
expect(text).toContain("Available via promotion:");
@@ -133,8 +137,8 @@ describe("models list promotion decorations", () => {
133137
const first = makeRuntime();
134138
await printAvailablePromotionsSection({
135139
configuredKeys: configured,
140+
refresh: startPromotionsFeedRefresh(NOW),
136141
runtime: first.runtime,
137-
nowMs: NOW,
138142
});
139143
const firstText = first.lines.join("\n");
140144
expect(firstText).not.toContain("Available via promotion:");
@@ -143,16 +147,20 @@ describe("models list promotion decorations", () => {
143147
const second = makeRuntime();
144148
await printAvailablePromotionsSection({
145149
configuredKeys: configured,
150+
refresh: startPromotionsFeedRefresh(NOW),
146151
runtime: second.runtime,
147-
nowMs: NOW,
148152
});
149153
expect(second.lines.join("\n")).toBe("");
150154
});
151155

152156
it("renders the section for an empty model list (fresh install)", async () => {
153157
await seedFeedCache([liveEntry]);
154158
const { runtime, lines } = makeRuntime();
155-
await printAvailablePromotionsSection({ configuredKeys: new Set(), runtime, nowMs: NOW });
159+
await printAvailablePromotionsSection({
160+
configuredKeys: new Set(),
161+
refresh: startPromotionsFeedRefresh(NOW),
162+
runtime,
163+
});
156164
const text = lines.join("\n");
157165
expect(text).toContain("Available via promotion:");
158166
expect(text).toContain("openclaw promos claim example-models-launch");
@@ -163,8 +171,8 @@ describe("models list promotion decorations", () => {
163171
const { runtime, lines } = makeRuntime();
164172
await printAvailablePromotionsSection({
165173
configuredKeys: new Set(["other/model"]),
174+
refresh: startPromotionsFeedRefresh(NOW),
166175
runtime,
167-
nowMs: NOW,
168176
});
169177
expect(lines.join("\n")).toBe("");
170178
});

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ const PROMOTIONS_SECTION_MAX_ENTRIES = 3;
1616

1717
type PromotionsFeedRefresh = {
1818
nowMs: number;
19-
statePromise: ReturnType<typeof maybeRefreshPromotionsFeed>;
19+
statePromise: Promise<Awaited<ReturnType<typeof maybeRefreshPromotionsFeed>> | undefined>;
2020
};
2121

2222
/** Starts the passive feed refresh so callers can overlap it with model row construction. */
2323
export function startPromotionsFeedRefresh(nowMs = Date.now()): PromotionsFeedRefresh {
2424
return {
2525
nowMs,
26-
statePromise: maybeRefreshPromotionsFeed({ nowMs }),
26+
statePromise: maybeRefreshPromotionsFeed({ nowMs }).catch(() => undefined),
2727
};
2828
}
2929

@@ -77,13 +77,15 @@ function canonicalPromotionModelKey(
7777
*/
7878
export async function printAvailablePromotionsSection(params: {
7979
configuredKeys: ReadonlySet<string>;
80-
refresh?: PromotionsFeedRefresh;
80+
refresh: PromotionsFeedRefresh;
8181
runtime: RuntimeEnv;
82-
nowMs?: number;
8382
}): Promise<void> {
84-
const refresh = params.refresh ?? startPromotionsFeedRefresh(params.nowMs);
85-
const nowMs = params.nowMs ?? refresh.nowMs;
83+
const { refresh } = params;
84+
const nowMs = refresh.nowMs;
8685
const state = await refresh.statePromise;
86+
if (!state) {
87+
return;
88+
}
8789
const live = listLivePromotionEntries(state, nowMs);
8890
if (live.length === 0) {
8991
return;

0 commit comments

Comments
 (0)