Skip to content

Commit 33c88af

Browse files
authored
fix(onboard): reject unavailable required runtimes (#129855)
1 parent eacceeb commit 33c88af

10 files changed

Lines changed: 586 additions & 239 deletions

‎src/commands/auth-choice.apply.plugin-provider.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type ResolvePluginSetupProvider =
2020
typeof import("../plugins/provider-auth-choice.runtime.js").resolvePluginSetupProvider;
2121
type RunProviderModelSelectedHook =
2222
typeof import("../plugins/provider-auth-choice.runtime.js").runProviderModelSelectedHook;
23+
type ModelSelectionRuntimePluginsResult =
24+
| { ok: true; cfg: ApplyAuthChoiceParams["config"]; codexInstalled: boolean }
25+
| { ok: false; message: string };
2326

2427
const resolvePluginProviders = vi.hoisted(() => vi.fn<() => ProviderPlugin[]>(() => []));
2528
const resolvePluginSetupProvider = vi.hoisted(() =>
@@ -101,6 +104,31 @@ vi.mock("../commands/onboarding-plugin-install.js", () => ({
101104
ensureOnboardingPluginInstalled,
102105
}));
103106

107+
const ensureModelSelectionRuntimePlugins = vi.hoisted(() =>
108+
vi.fn(
109+
async ({
110+
cfg,
111+
}: {
112+
cfg: ApplyAuthChoiceParams["config"];
113+
}): Promise<ModelSelectionRuntimePluginsResult> => ({
114+
ok: true,
115+
cfg,
116+
codexInstalled: false,
117+
}),
118+
),
119+
);
120+
vi.mock("../commands/runtime-plugin-install.js", () => ({
121+
CODEX_RUNTIME_PLUGIN_ID: "codex",
122+
ensureModelSelectionRuntimePlugins,
123+
}));
124+
125+
const offerPostInstallMigrations = vi.hoisted(() =>
126+
vi.fn(async ({ config }: { config: ApplyAuthChoiceParams["config"] }) => ({ config })),
127+
);
128+
vi.mock("../wizard/setup.post-install-migration.js", () => ({
129+
offerPostInstallMigrations,
130+
}));
131+
104132
const LOCAL_PROVIDER_ID = "local-provider";
105133
const LOCAL_PROVIDER_LABEL = "Local Provider";
106134
const LOCAL_AUTH_METHOD_ID = "local";
@@ -240,6 +268,12 @@ describe("applyAuthChoiceLoadedPluginProvider", () => {
240268
pluginId: entry?.pluginId ?? "missing-plugin",
241269
status: "skipped",
242270
}));
271+
ensureModelSelectionRuntimePlugins.mockImplementation(async ({ cfg }) => ({
272+
ok: true,
273+
cfg,
274+
codexInstalled: false,
275+
}));
276+
offerPostInstallMigrations.mockImplementation(async ({ config }) => ({ config }));
243277
});
244278

245279
it("stages provider profiles until the caller commits them", async () => {
@@ -416,6 +450,83 @@ describe("applyAuthChoiceLoadedPluginProvider", () => {
416450
expect(hookParams.workspaceDir).toBe("/tmp/workspace");
417451
});
418452

453+
it.each(["failed", "timed_out"] as const)(
454+
"restores the previous model and retries when required Codex is %s",
455+
async (status) => {
456+
const provider = buildProviderWithDefaultModelPatch();
457+
resolvePluginProviders.mockReturnValue([provider]);
458+
resolveProviderPluginChoice.mockReturnValue({
459+
provider,
460+
method: expectDefined(provider.auth[0], "provider.auth[0] test invariant"),
461+
});
462+
const note = vi.fn(async () => {});
463+
const message = `Codex runtime is required but unavailable (status: ${status}). Reason: registry token=***. Retry setup after checking npm and the configured registry.`;
464+
ensureModelSelectionRuntimePlugins.mockResolvedValue({ ok: false, message });
465+
466+
const result = await applyAuthChoiceLoadedPluginProvider(
467+
buildParams({
468+
config: {
469+
agents: { defaults: { model: { primary: EXISTING_DEFAULT_MODEL } } },
470+
},
471+
prompter: { note } as unknown as ApplyAuthChoiceParams["prompter"],
472+
}),
473+
);
474+
475+
expect(result).toEqual({
476+
config: {
477+
agents: { defaults: { model: { primary: EXISTING_DEFAULT_MODEL } } },
478+
},
479+
retrySelection: true,
480+
});
481+
expect(note).toHaveBeenCalledWith(message, "Runtime unavailable");
482+
expect(note).toHaveBeenCalledOnce();
483+
expect(persistAuthProfileBatch).not.toHaveBeenCalled();
484+
expect(runProviderModelSelectedHook).not.toHaveBeenCalled();
485+
expect(offerPostInstallMigrations).not.toHaveBeenCalled();
486+
},
487+
);
488+
489+
it("restores the exact entry config after provider install and auth staging", async () => {
490+
const provider = buildProvider();
491+
const entryConfig = {
492+
agents: { defaults: { model: { primary: EXISTING_DEFAULT_MODEL } } },
493+
wizard: { lastRunVersion: "entry-version" },
494+
};
495+
resolveProviderInstallCatalogEntry.mockReturnValue(buildLocalProviderInstallCatalogEntry());
496+
ensureOnboardingPluginInstalled.mockResolvedValue({
497+
...buildInstalledLocalProviderPluginResult(),
498+
cfg: {
499+
...entryConfig,
500+
plugins: { entries: { "local-provider-plugin": { enabled: true } } },
501+
},
502+
});
503+
resolvePluginProviders.mockReturnValue([provider]);
504+
resolveProviderPluginChoice.mockReturnValueOnce(null).mockReturnValueOnce({
505+
provider,
506+
method: expectDefined(provider.auth[0], "provider.auth[0] test invariant"),
507+
});
508+
const note = vi.fn(async () => {});
509+
ensureModelSelectionRuntimePlugins.mockResolvedValue({
510+
ok: false,
511+
message: "GitHub Copilot agent runtime is required but unavailable.",
512+
});
513+
514+
const result = await applyAuthChoiceLoadedPluginProvider(
515+
buildParams({
516+
config: entryConfig,
517+
prompter: { note } as unknown as ApplyAuthChoiceParams["prompter"],
518+
}),
519+
);
520+
521+
expect(result).toEqual({ config: entryConfig, retrySelection: true });
522+
expect(result?.config).toBe(entryConfig);
523+
expect(ensureOnboardingPluginInstalled).toHaveBeenCalledOnce();
524+
expect(note).toHaveBeenCalledOnce();
525+
expect(runProviderModelSelectedHook).not.toHaveBeenCalled();
526+
expect(offerPostInstallMigrations).not.toHaveBeenCalled();
527+
expect(persistAuthProfileBatch).not.toHaveBeenCalled();
528+
});
529+
419530
it("keeps an existing default when provider auth patches its own primary model", async () => {
420531
const provider = buildProviderWithDefaultModelPatch();
421532
resolvePluginProviders.mockReturnValue([provider]);

‎src/commands/codex-runtime-plugin-install.test.ts‎

Lines changed: 188 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,11 @@ describe("Codex runtime plugin install repair", () => {
9595
});
9696

9797
expect(result).toMatchObject({
98-
cfg,
99-
required: true,
100-
installed: false,
98+
ok: false,
10199
status: "failed",
100+
message: expect.stringContaining("Codex runtime is required but unavailable"),
102101
});
103-
expect(result.reason).toBeTruthy();
102+
expect("cfg" in result).toBe(false);
104103
});
105104

106105
it("enables an allowed existing Codex install", async () => {
@@ -124,9 +123,8 @@ describe("Codex runtime plugin install repair", () => {
124123
});
125124

126125
expect(result).toMatchObject({
126+
ok: true,
127127
required: true,
128-
installed: true,
129-
status: "installed",
130128
cfg: { plugins: { entries: { codex: { enabled: true } } } },
131129
});
132130
});
@@ -150,13 +148,75 @@ describe("Codex runtime plugin install repair", () => {
150148
});
151149

152150
expect(result).toMatchObject({
153-
required: true,
154-
installed: false,
151+
ok: false,
155152
status: "failed",
156-
reason: "npm registry returned EAI_AGAIN while fetching @openclaw/codex",
153+
message: expect.stringContaining(
154+
"npm registry returned EAI_AGAIN while fetching @openclaw/codex",
155+
),
157156
});
158157
});
159158

159+
const sensitiveFixture = ["fixture", "credential"].join("-");
160+
it.each([
161+
{
162+
status: "failed" as const,
163+
error: `Install failed: https://user:${sensitiveFixture}@registry.example.test/pkg?token=${sensitiveFixture}\u001b[2K`,
164+
},
165+
{
166+
status: "timed_out" as const,
167+
error: undefined,
168+
},
169+
])("formats a sanitized actionable $status failure for required Codex", async (failure) => {
170+
mocks.ensureOnboardingPluginInstalled.mockResolvedValueOnce({
171+
cfg: {},
172+
installed: false,
173+
pluginId: "codex",
174+
status: failure.status,
175+
...(failure.error ? { error: failure.error } : {}),
176+
});
177+
const { ensureCodexRuntimePluginForModelSelection } =
178+
await import("./codex-runtime-plugin-install.js");
179+
180+
const result = await ensureCodexRuntimePluginForModelSelection({
181+
cfg: {},
182+
model: "openai/gpt-5.5",
183+
prompter: {} as never,
184+
runtime: {} as never,
185+
});
186+
187+
expect(result.ok).toBe(false);
188+
if (result.ok) {
189+
throw new Error("expected required runtime failure");
190+
}
191+
expect(result.message).toContain(`Codex runtime is required but unavailable`);
192+
expect(result.message).toContain(`status: ${failure.status}`);
193+
expect(result.message).toContain("Retry setup");
194+
expect(result.message).toContain("npm");
195+
expect(result.message).toContain("registry");
196+
expect(result.message).not.toContain(sensitiveFixture);
197+
expect(result.message).not.toContain("\u001b");
198+
expect(result).not.toHaveProperty("cfg");
199+
});
200+
201+
it("keeps an optional Codex runtime selection as a successful no-op", async () => {
202+
const { ensureCodexRuntimePluginForModelSelection } =
203+
await import("./codex-runtime-plugin-install.js");
204+
205+
const result = await ensureCodexRuntimePluginForModelSelection({
206+
cfg: {},
207+
model: "anthropic/claude-sonnet-4-6",
208+
prompter: {} as never,
209+
runtime: {} as never,
210+
});
211+
212+
expect(result).toEqual({
213+
ok: true,
214+
cfg: {},
215+
required: false,
216+
});
217+
expect(mocks.ensureOnboardingPluginInstalled).not.toHaveBeenCalled();
218+
});
219+
160220
it("allows source checkouts to use the matching bundled Codex plugin", async () => {
161221
const { ensureCodexRuntimePluginForModelSelection } =
162222
await import("./codex-runtime-plugin-install.js");
@@ -214,9 +274,128 @@ describe("Codex runtime plugin install repair", () => {
214274
});
215275

216276
expect(result).toMatchObject({
277+
ok: true,
217278
required: true,
279+
});
280+
});
281+
282+
it("stops the aggregate after a required Codex failure with no config result", async () => {
283+
mocks.ensureOnboardingPluginInstalled.mockResolvedValueOnce({
284+
cfg: { plugins: { entries: { codex: { enabled: false } } } },
285+
installed: false,
286+
pluginId: "codex",
287+
status: "failed",
288+
error: "registry unavailable",
289+
});
290+
const { ensureModelSelectionRuntimePlugins } = await import("./runtime-plugin-install.js");
291+
292+
const result = await ensureModelSelectionRuntimePlugins({
293+
cfg: {},
294+
model: "openai/gpt-5.5",
295+
prompter: {} as never,
296+
runtime: {} as never,
297+
});
298+
299+
expect(result).toEqual({
300+
ok: false,
301+
message: expect.stringContaining("registry unavailable"),
302+
});
303+
expect(result).not.toHaveProperty("cfg");
304+
expect(mocks.ensureOnboardingPluginInstalled).toHaveBeenCalledOnce();
305+
});
306+
307+
it("evaluates Copilot after optional Codex and returns the closed success shape", async () => {
308+
const installedConfig = { plugins: { entries: { copilot: { enabled: true } } } };
309+
mocks.ensureOnboardingPluginInstalled.mockResolvedValueOnce({
310+
cfg: installedConfig,
218311
installed: true,
312+
pluginId: "copilot",
219313
status: "installed",
220314
});
315+
const { ensureModelSelectionRuntimePlugins } = await import("./runtime-plugin-install.js");
316+
317+
const result = await ensureModelSelectionRuntimePlugins({
318+
cfg: {
319+
models: {
320+
providers: {
321+
"github-copilot": {
322+
baseUrl: "https://api.githubcopilot.com",
323+
models: [],
324+
agentRuntime: { id: "copilot" },
325+
},
326+
},
327+
},
328+
},
329+
model: "github-copilot/gpt-5.5",
330+
prompter: {} as never,
331+
runtime: {} as never,
332+
});
333+
334+
expect(result).toEqual({ ok: true, cfg: installedConfig, codexInstalled: false });
335+
expect(mocks.ensureOnboardingPluginInstalled).toHaveBeenCalledOnce();
336+
expect(mocks.ensureOnboardingPluginInstalled).toHaveBeenCalledWith(
337+
expect.objectContaining({ entry: expect.objectContaining({ pluginId: "copilot" }) }),
338+
);
339+
});
340+
341+
it("silences installer output and rejects prompts in non-interactive mode", async () => {
342+
const note = vi.fn(async () => {});
343+
const log = vi.fn();
344+
const error = vi.fn();
345+
mocks.ensureOnboardingPluginInstalled.mockImplementationOnce(async (params) => {
346+
await params.prompter.note("installer note");
347+
params.prompter.progress("installing").update("downloading");
348+
params.runtime.log("installer log");
349+
params.runtime.error("installer error");
350+
await expect(params.prompter.confirm({ message: "fallback?" })).rejects.toThrow(
351+
"Runtime plugin install unexpectedly prompted",
352+
);
353+
return {
354+
cfg: {},
355+
installed: false,
356+
pluginId: "codex",
357+
status: "timed_out",
358+
};
359+
});
360+
const { ensureModelSelectionRuntimePlugins } = await import("./runtime-plugin-install.js");
361+
362+
await ensureModelSelectionRuntimePlugins({
363+
cfg: {},
364+
model: "openai/gpt-5.5",
365+
prompter: { note } as never,
366+
runtime: { log, error, exit: vi.fn() },
367+
output: "silent",
368+
});
369+
370+
expect(note).not.toHaveBeenCalled();
371+
expect(log).not.toHaveBeenCalled();
372+
expect(error).not.toHaveBeenCalled();
373+
});
374+
375+
it("suppresses installer terminal failure presentation in interactive mode", async () => {
376+
const note = vi.fn(async () => {});
377+
const error = vi.fn();
378+
mocks.ensureOnboardingPluginInstalled.mockImplementationOnce(async (params) => {
379+
await params.prompter.note("installer failure");
380+
params.runtime.error("installer failure");
381+
return {
382+
cfg: {},
383+
installed: false,
384+
pluginId: "codex",
385+
status: "failed",
386+
};
387+
});
388+
const { ensureCodexRuntimePluginForModelSelection } =
389+
await import("./codex-runtime-plugin-install.js");
390+
391+
await ensureCodexRuntimePluginForModelSelection({
392+
cfg: {},
393+
model: "openai/gpt-5.5",
394+
prompter: { note } as never,
395+
runtime: { log: vi.fn(), error, exit: vi.fn() },
396+
});
397+
398+
expect(note).not.toHaveBeenCalled();
399+
expect(error).not.toHaveBeenCalled();
221400
});
222401
});

0 commit comments

Comments
 (0)