Skip to content

Commit 48529f1

Browse files
authored
feat(onboard): offer codex migration after harness install (#81192)
Add a post-install seam so the wizard can prompt the user to import their existing Codex CLI state (skills, archived config/hooks, advisory cached plugins) through the existing `openclaw migrate codex` flow once the harness plugin is in place. Fires on both fresh installs and repair runs; the user can decline at any time. Trigger sites, both routing through one helper: - src/plugins/provider-auth-choice.ts: after `ensureCodexRuntimePluginForModelSelection` reports `installed: true`, dynamically import `offerPostInstallMigrations` and call it before the wizard moves on. - src/commands/onboard-non-interactive/local/auth-choice.plugin-providers.ts: same call shape with `nonInteractive: true`, so the helper emits a hint line only and never mutates state. Helper (src/wizard/setup.post-install-migration.ts) is generic, not Codex-hardcoded — it resolves migration providers via the manifest `migrationProviders` contract, filters to providers owned by plugins the caller flags as installed in this onboarding step, runs `provider.detect`, and on TTY hands accepted runs to `migrateDefaultCommand`. All detect, prompt, and migrate failures are swallowed so onboarding never aborts on this optional offer. Also harden the Codex app-server subprocess lifecycle now that `detect()` runs from a hotter onboarding path: isolate the plugin-install `plugin/read` call (extensions/codex/src/migration/apply.ts) and have the isolated request wait for child exit with a SIGKILL fallback (extensions/codex/src/app-server/request.ts) so parents are not held open by an orphaned codex binary. Tests: - src/wizard/setup.post-install-migration.test.ts (new, 10 cases) - src/commands/onboard-non-interactive/local/auth-choice.plugin-providers.test.ts extended with hint-call assertions and a not-required-no-offer case.
1 parent 37237a5 commit 48529f1

8 files changed

Lines changed: 505 additions & 22 deletions

File tree

‎extensions/codex/src/app-server/request.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export async function requestCodexAppServerJson<T = JsonValue | undefined>(param
5858
return await client.request<T>(params.method, params.requestParams, { timeoutMs });
5959
} finally {
6060
if (params.isolated) {
61-
client.close();
61+
// Wait for the child to actually exit (with a SIGKILL fallback) so
62+
// the parent process doesn't hang on an orphaned codex app-server.
63+
// The stdio bin shim does not always propagate stdin EOF to the
64+
// underlying codex binary, so the unref'd close() path can leave
65+
// the child running and keep the parent's event loop alive.
66+
await client.closeAndWait({ exitTimeoutMs: 2_000, forceKillDelayMs: 250 });
6267
}
6368
}
6469
})(),

‎extensions/codex/src/migration/apply.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ async function applyCodexPluginInstallItem(
126126
startOptions: appServer.start,
127127
agentDir: resolveCodexMigrationTargets(ctx).agentDir,
128128
config: ctx.config,
129+
isolated: true,
129130
}),
130131
appCache: defaultCodexAppInventoryCache,
131132
appCacheKey,

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { RuntimeEnv } from "../runtime.js";
99
import { resolveUserPath } from "../utils.js";
1010
import type { WizardPrompter } from "../wizard/prompts.js";
1111

12-
const CODEX_RUNTIME_PLUGIN_ID = "codex";
12+
export const CODEX_RUNTIME_PLUGIN_ID = "codex";
1313
const CODEX_RUNTIME_PLUGIN_LABEL = "Codex";
1414
const CODEX_RUNTIME_PLUGIN_NPM_SPEC = "@openclaw/codex";
1515

@@ -49,7 +49,11 @@ export async function ensureCodexRuntimePluginForModelSelection(params: {
4949
workspaceDir?: string;
5050
}): Promise<CodexRuntimePluginInstallResult> {
5151
if (!selectedModelShouldEnsureCodexRuntimePlugin({ cfg: params.cfg, model: params.model })) {
52-
return { cfg: params.cfg, required: false, installed: false };
52+
return {
53+
cfg: params.cfg,
54+
required: false,
55+
installed: false,
56+
};
5357
}
5458
const existingRecords = await loadInstalledPluginIndexInstallRecords({ env: process.env });
5559
if (isInstalledRecordPresentOnDisk(existingRecords[CODEX_RUNTIME_PLUGIN_ID], process.env)) {

‎src/commands/onboard-non-interactive/local/auth-choice.plugin-providers.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ const ensureCodexRuntimePluginForModelSelection = vi.hoisted(() =>
1313
),
1414
);
1515
vi.mock("../../codex-runtime-plugin-install.js", () => ({
16+
CODEX_RUNTIME_PLUGIN_ID: "codex",
1617
ensureCodexRuntimePluginForModelSelection,
1718
}));
19+
const offerPostInstallMigrations = vi.hoisted(() => vi.fn(async () => {}));
20+
vi.mock("../../../wizard/setup.post-install-migration.js", () => ({
21+
offerPostInstallMigrations,
22+
}));
1823
const resolvePreferredProviderForAuthChoice = vi.hoisted(() => vi.fn(async () => undefined));
1924
vi.mock("../../../plugins/provider-auth-choice-preference.js", () => ({
2025
resolvePreferredProviderForAuthChoice,
@@ -47,6 +52,7 @@ beforeEach(() => {
4752
required: false,
4853
installed: false,
4954
}));
55+
offerPostInstallMigrations.mockClear();
5056
});
5157

5258
function createRuntime() {
@@ -270,5 +276,40 @@ describe("applyNonInteractivePluginProviderChoice", () => {
270276
expect(ensureInput.runtime).toBe(runtime);
271277
expectWorkspaceDir(ensureInput.workspaceDir);
272278
expect(result).toBe(installedConfig);
279+
expect(offerPostInstallMigrations).toHaveBeenCalledOnce();
280+
const migrationInput = mockArg(offerPostInstallMigrations);
281+
expect(migrationInput.config).toBe(installedConfig);
282+
expect(migrationInput.installedPluginIds).toEqual(["codex"]);
283+
expect(migrationInput.nonInteractive).toBe(true);
284+
});
285+
286+
it("does not offer post-install migration when Codex is not required for the selected model", async () => {
287+
const runtime = createRuntime();
288+
const selectedConfig = {
289+
agents: { defaults: { model: { primary: "openai/gpt-5.5" } } },
290+
} as OpenClawConfig;
291+
const runNonInteractive = vi.fn(async () => selectedConfig);
292+
ensureCodexRuntimePluginForModelSelection.mockResolvedValue({
293+
cfg: selectedConfig,
294+
required: false,
295+
installed: false,
296+
});
297+
resolvePluginProviders.mockReturnValue([{ id: "openai", pluginId: "openai" }] as never);
298+
resolveProviderPluginChoice.mockReturnValue({
299+
provider: { id: "openai", pluginId: "openai", label: "OpenAI" },
300+
method: { runNonInteractive },
301+
});
302+
303+
await applyNonInteractivePluginProviderChoice({
304+
nextConfig: { agents: { defaults: {} } } as OpenClawConfig,
305+
authChoice: "openai-api-key",
306+
opts: {} as never,
307+
runtime: runtime as never,
308+
baseConfig: { agents: { defaults: {} } } as OpenClawConfig,
309+
resolveApiKey: vi.fn(),
310+
toApiKeyCredential: vi.fn(),
311+
});
312+
313+
expect(offerPostInstallMigrations).not.toHaveBeenCalled();
273314
});
274315
});

‎src/commands/onboard-non-interactive/local/auth-choice.plugin-providers.ts‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import type {
1818
import type { RuntimeEnv } from "../../../runtime.js";
1919
import { createLazyRuntimeSurface } from "../../../shared/lazy-runtime.js";
2020
import type { WizardPrompter } from "../../../wizard/prompts.js";
21-
import { ensureCodexRuntimePluginForModelSelection } from "../../codex-runtime-plugin-install.js";
21+
import {
22+
CODEX_RUNTIME_PLUGIN_ID,
23+
ensureCodexRuntimePluginForModelSelection,
24+
} from "../../codex-runtime-plugin-install.js";
2225
import type { OnboardOptions } from "../../onboard-types.js";
2326

2427
const PROVIDER_PLUGIN_CHOICE_PREFIX = "provider-plugin:";
@@ -201,13 +204,27 @@ export async function applyNonInteractivePluginProviderChoice(params: {
201204
if (!selectedModel) {
202205
return result;
203206
}
204-
return (
205-
await ensureCodexRuntimePluginForModelSelection({
206-
cfg: result,
207-
model: selectedModel,
208-
prompter: createNonInteractivePluginInstallPrompter(params.runtime),
207+
const nonInteractivePrompter = createNonInteractivePluginInstallPrompter(params.runtime);
208+
const codexInstall = await ensureCodexRuntimePluginForModelSelection({
209+
cfg: result,
210+
model: selectedModel,
211+
prompter: nonInteractivePrompter,
212+
runtime: params.runtime,
213+
workspaceDir,
214+
});
215+
if (codexInstall.installed) {
216+
// Non-interactive onboarding never auto-applies migration; emit a hint so
217+
// the operator knows Codex CLI state is available to import deliberately.
218+
// Gated on installed (not freshlyInstalled) so repair runs against an
219+
// already-present harness still surface the hint.
220+
const { offerPostInstallMigrations } =
221+
await import("../../../wizard/setup.post-install-migration.js");
222+
await offerPostInstallMigrations({
223+
config: codexInstall.cfg,
209224
runtime: params.runtime,
210-
workspaceDir,
211-
})
212-
).cfg;
225+
installedPluginIds: [CODEX_RUNTIME_PLUGIN_ID],
226+
nonInteractive: true,
227+
});
228+
}
229+
return codexInstall.cfg;
213230
}

‎src/plugins/provider-auth-choice.ts‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,32 @@ async function applyDefaultModelFromAuthChoice(params: {
153153
preserveExistingPrimary: params.preserveExistingDefaultModel === true,
154154
});
155155
if (!preservesDifferentPrimary) {
156-
const { ensureCodexRuntimePluginForModelSelection } =
156+
const { CODEX_RUNTIME_PLUGIN_ID, ensureCodexRuntimePluginForModelSelection } =
157157
await import("../commands/codex-runtime-plugin-install.js");
158-
nextConfig = (
159-
await ensureCodexRuntimePluginForModelSelection({
160-
cfg: nextConfig,
161-
model: params.selectedModel,
162-
prompter: params.prompter,
163-
runtime: params.runtime,
164-
...(params.workspaceDir !== undefined ? { workspaceDir: params.workspaceDir } : {}),
165-
})
166-
).cfg;
158+
const codexInstall = await ensureCodexRuntimePluginForModelSelection({
159+
cfg: nextConfig,
160+
model: params.selectedModel,
161+
prompter: params.prompter,
162+
runtime: params.runtime,
163+
...(params.workspaceDir !== undefined ? { workspaceDir: params.workspaceDir } : {}),
164+
});
165+
nextConfig = codexInstall.cfg;
167166
await params.runSelectedModelHook(nextConfig);
167+
if (codexInstall.installed) {
168+
// Offer Codex CLI state migration whenever the harness is in place for
169+
// the selected model, regardless of whether this run was a fresh install
170+
// or a repair against an already-present harness. The user can always
171+
// decline the prompt; surfacing it again costs nothing if there is no
172+
// migratable state to find.
173+
const { offerPostInstallMigrations } =
174+
await import("../wizard/setup.post-install-migration.js");
175+
await offerPostInstallMigrations({
176+
config: nextConfig,
177+
runtime: params.runtime,
178+
prompter: params.prompter,
179+
installedPluginIds: [CODEX_RUNTIME_PLUGIN_ID],
180+
});
181+
}
168182
}
169183
await noteDefaultModelResult({
170184
previousPrimary,

0 commit comments

Comments
 (0)