xonecas.io
64875
Mc

mini-coder / commit 878833c

fix(model-info): harden paginated model refresh

Sean2026-03-11 878833c4f450112b2ee7ce7da160416a60db4c10 parent 6b4233c

3 files changed

src/agent/subagent-runner.ts+4−1
@@ -33,7 +33,10 @@ export function createSubagentRunner(
3333 >();
3434
3535 // Depth is injected by the parent subprocess via MC_SUBAGENT_DEPTH env var.
36- const subagentDepth = parseInt(process.env.MC_SUBAGENT_DEPTH ?? "0", 10);
36+ const subagentDepth = Number.parseInt(
37+ process.env.MC_SUBAGENT_DEPTH ?? "0",
38+ 10,
39+ );
3740
3841 // Merge lock: serialises merge operations from concurrent subagents.
3942 let mergeLockTail = Promise.resolve();
src/index.ts+1−1
@@ -109,7 +109,7 @@ function parseArgs(argv: string[]): CliArgs {
109109 args.agentName = argv[++i] ?? null;
110110 break;
111111 case "--output-fd": {
112- const fd = parseInt(argv[++i] ?? "", 10);
112+ const fd = Number.parseInt(argv[++i] ?? "", 10);
113113 if (!Number.isNaN(fd)) args.outputFd = fd;
114114 break;
115115 }
src/llm-api/model-info.ts+70−25
@@ -17,7 +17,8 @@ const MODELS_DEV_URL = "https://models.dev/api.json";
1717
1818 const MODELS_DEV_SYNC_KEY = "last_models_dev_sync_at";
1919 const PROVIDER_SYNC_KEY_PREFIX = "last_provider_sync_at:";
20-
20+const CACHE_VERSION_KEY = "model_info_cache_version";
21+const CACHE_VERSION = 2;
2122 export const MODEL_INFO_TTL_MS = 7 * 24 * 60 * 60 * 1000;
2223
2324 interface ModelInfo {
@@ -382,6 +383,7 @@ function getProviderSyncKey(provider: string): string {
382383
383384 function isModelInfoStale(now = Date.now()): boolean {
384385 ensureLoaded();
386+ if (parseStateInt(CACHE_VERSION_KEY) !== CACHE_VERSION) return true;
385387 if (isStaleTimestamp(parseStateInt(MODELS_DEV_SYNC_KEY), now)) return true;
386388 for (const provider of getProvidersRequiredForFreshness()) {
387389 const providerSync = parseStateInt(getProviderSyncKey(provider));
@@ -423,27 +425,29 @@ async function fetchModelsDevPayload(): Promise<unknown | null> {
423425 async function fetchZenModels(): Promise<ProviderModelCandidate[] | null> {
424426 const key = process.env.OPENCODE_API_KEY;
425427 if (!key) return null;
426- const payload = await fetchJson(
428+ return fetchPaginatedModelsList(
427429 `${ZEN_BASE}/models`,
428430 { headers: { Authorization: `Bearer ${key}` } },
429431 8_000,
432+ "data",
433+ "id",
434+ (item, modelId) => {
435+ const contextWindow =
436+ typeof item.context_window === "number" &&
437+ Number.isFinite(item.context_window)
438+ ? Math.max(0, Math.trunc(item.context_window))
439+ : null;
440+ return {
441+ providerModelId: modelId,
442+ displayName: item.id as string,
443+ contextWindow,
444+ free:
445+ (item.id as string).endsWith("-free") ||
446+ item.id === "gpt-5-nano" ||
447+ item.id === "big-pickle",
448+ };
449+ },
430450 );
431- return processModelsList(payload, "data", "id", (item, modelId) => {
432- const contextWindow =
433- typeof item.context_window === "number" &&
434- Number.isFinite(item.context_window)
435- ? Math.max(0, Math.trunc(item.context_window))
436- : null;
437- return {
438- providerModelId: modelId,
439- displayName: item.id as string,
440- contextWindow,
441- free:
442- (item.id as string).endsWith("-free") ||
443- item.id === "gpt-5-nano" ||
444- item.id === "big-pickle",
445- };
446- });
447451 }
448452
449453 function processModelsList(
@@ -466,20 +470,60 @@ function processModelsList(
466470 }
467471 return out;
468472 }
473+
474+async function fetchPaginatedModelsList(
475+ url: string,
476+ init: RequestInit,
477+ timeoutMs: number,
478+ arrayKey: string,
479+ idKey: string,
480+ mapper: (
481+ item: Record<string, unknown>,
482+ modelId: string,
483+ ) => ProviderModelCandidate | null,
484+): Promise<ProviderModelCandidate[] | null> {
485+ const out: ProviderModelCandidate[] = [];
486+ const seen = new Set<string>();
487+ const baseUrl = new URL(url);
488+ let nextAfter: string | null = null;
489+
490+ for (let page = 0; page < 10; page += 1) {
491+ const currentUrl = new URL(baseUrl);
492+ if (nextAfter !== null) currentUrl.searchParams.set("after", nextAfter);
493+ const payload = await fetchJson(currentUrl.toString(), init, timeoutMs);
494+ const rows = processModelsList(payload, arrayKey, idKey, mapper);
495+ if (rows === null) {
496+ return null;
497+ }
498+ for (const row of rows) {
499+ if (seen.has(row.providerModelId)) continue;
500+ seen.add(row.providerModelId);
501+ out.push(row);
502+ }
503+ if (!isRecord(payload)) break;
504+ if (payload.has_more !== true || typeof payload.last_id !== "string") break;
505+ nextAfter = payload.last_id;
506+ }
507+
508+ return out;
509+}
510+
469511 async function fetchOpenAIModels(): Promise<ProviderModelCandidate[] | null> {
470512 const key = process.env.OPENAI_API_KEY;
471513 if (!key) return null;
472- const payload = await fetchJson(
514+ return fetchPaginatedModelsList(
473515 `${OPENAI_BASE}/v1/models`,
474516 { headers: { Authorization: `Bearer ${key}` } },
475517 6_000,
518+ "data",
519+ "id",
520+ (item, modelId) => ({
521+ providerModelId: modelId,
522+ displayName: item.id as string,
523+ contextWindow: null,
524+ free: false,
525+ }),
476526 );
477- return processModelsList(payload, "data", "id", (item, modelId) => ({
478- providerModelId: modelId,
479- displayName: item.id as string,
480- contextWindow: null,
481- free: false,
482- }));
483527 }
484528
485529 async function fetchAnthropicModels(): Promise<
@@ -624,6 +668,7 @@ async function refreshModelInfoInternal(): Promise<void> {
624668 replaceProviderModels(result.provider, rows);
625669 setModelInfoState(getProviderSyncKey(result.provider), String(now));
626670 }
671+ setModelInfoState(CACHE_VERSION_KEY, String(CACHE_VERSION));
627672
628673 loadCacheFromDb();
629674 }