diff --git a/src/application/Setup.fixture.ts b/src/application/Setup.fixture.ts index 61b8f1dc5..95f8c54a0 100644 --- a/src/application/Setup.fixture.ts +++ b/src/application/Setup.fixture.ts @@ -52,6 +52,11 @@ export class FakeSetupHost implements SetupHost { linuxDistribution = (): Promise => Promise.resolve(this.distribution); hopperPath = (): Promise => Promise.resolve(this.hopper); + initialSetupState = async (scope?: DoctorScope) => ({ + ...(this.hopper === undefined ? {} : { hopperPath: this.hopper }), + providerEnvironment: await this.providerEnvironment(), + doctor: await this.doctor(scope), + }); providerEnvironment = (): Promise => Promise.resolve({ ...(this.hopper === undefined diff --git a/src/application/Setup.initialSnapshot.test.ts b/src/application/Setup.initialSnapshot.test.ts new file mode 100644 index 000000000..20fba2b71 --- /dev/null +++ b/src/application/Setup.initialSnapshot.test.ts @@ -0,0 +1,51 @@ +import { expect, it } from "vitest"; + +import { runSetup } from "./Setup.js"; +import { FakeSetupHost, options } from "./Setup.fixture.js"; + +it("uses the shared initial host snapshot for setup planning", async () => { + const host = new FakeSetupHost(); + host.hopperPath = () => { + throw new Error("setup snapshot should provide the Hopper path"); + }; + host.providerEnvironment = () => { + throw new Error("setup snapshot should provide provider settings"); + }; + host.initialSetupState = async (scope) => ({ + hopperPath: "/snapshot/Hopper", + providerEnvironment: { HOPPER_LAUNCHER_PATH: "/snapshot/Hopper" }, + doctor: await host.doctor(scope), + }); + host.clients = [{ name: "codex", configPath: "/codex.json" }]; + + const result = await runSetup( + { ...options(false), dryRun: true, clientIds: ["codex"] }, + host, + ); + + expect(result.status).toBe("planned"); + expect(result.plannedActions.map(({ id }) => id)).toContain( + "configure_client:codex", + ); + expect(host.doctorCalls).toBe(1); +}); + +it("refreshes doctor after approved setup changes", async () => { + const host = new FakeSetupHost(); + host.doctorHealthy = false; + host.skill = "unchanged"; + host.clients = [{ name: "codex", configPath: "/codex.json" }]; + const configureClient = host.configureClient; + host.configureClient = (client, providerEnvironment, command) => { + host.doctorHealthy = true; + return configureClient(client, providerEnvironment, command); + }; + + const result = await runSetup( + { ...options(true), clientIds: ["codex"], installSkill: false }, + host, + ); + + expect(result.doctor.healthy).toBe(true); + expect(host.doctorCalls).toBe(2); +}); diff --git a/src/application/Setup.ts b/src/application/Setup.ts index c99a25ea7..9032d79e8 100644 --- a/src/application/Setup.ts +++ b/src/application/Setup.ts @@ -42,8 +42,15 @@ export const runSetup = async ( ); const unsupported = await hostRemediation(host, false); if (unsupported !== undefined) return fail(unsupported); - let hopperPath = await host.hopperPath(); - let providerEnvironment = await initialProviderEnvironment(host, hopperPath); + const initialState = await host.initialSetupState?.(options.readinessScope); + let hopperPath = + initialState === undefined + ? await host.hopperPath() + : initialState.hopperPath; + let providerEnvironment = + initialState === undefined + ? await initialProviderEnvironment(host, hopperPath) + : initialState.providerEnvironment; const discovery = await discoverSetupState({ host, providerEnvironment, @@ -51,6 +58,9 @@ export const runSetup = async ( proposeHopper: options.proposeHopper ?? (confirm !== undefined && !options.structured), doctorScope: options.readinessScope, + ...(initialState === undefined + ? {} + : { initialDoctor: initialState.doctor }), }); clientStates = discovery.clientStates; const clientSelectionAllowed = diff --git a/src/application/SetupHost.ts b/src/application/SetupHost.ts index d574c5bd0..6bda5b494 100644 --- a/src/application/SetupHost.ts +++ b/src/application/SetupHost.ts @@ -22,7 +22,12 @@ import { } from "./SetupClientConfiguration.js"; import { setupInstallFailure } from "./SetupInstallFailure.js"; import { providerRegistrationEnvironment } from "./SetupRegistrationEnvironment.js"; -import type { SetupHost, SetupProviderEnvironment } from "./SetupTypes.js"; +import type { + SetupHost, + SetupInitialState, + SetupProviderEnvironment, +} from "./SetupTypes.js"; +import type { DoctorScope } from "./Doctor.js"; /** Resolve the executable and arguments used in a managed MCP registration. */ export const setupRegistrationCommand = ( @@ -91,6 +96,25 @@ export const systemSetupHost = ( macosVersion: () => doctorHost.macosVersion(), linuxDistribution: readLinuxDistribution, hopperPath: async () => (await runDoctor(undefined, doctorHost)).hopperPath, + initialSetupState: async ( + scope?: DoctorScope, + ): Promise => { + const diagnosis = await runDoctor(undefined, doctorHost, scope); + return { + ...(diagnosis.hopperPath === undefined + ? {} + : { hopperPath: diagnosis.hopperPath }), + providerEnvironment: { + ...providerRegistrationEnvironment( + diagnosis.providerInspections ?? [], + ), + ...(diagnosis.hopperPath === undefined + ? {} + : { HOPPER_LAUNCHER_PATH: diagnosis.hopperPath }), + }, + doctor: diagnosis, + }; + }, providerEnvironment: async () => { const diagnosis = await runDoctor(undefined, doctorHost); return { diff --git a/src/application/SetupPlan.ts b/src/application/SetupPlan.ts index e89e07229..fcea6271d 100644 --- a/src/application/SetupPlan.ts +++ b/src/application/SetupPlan.ts @@ -16,6 +16,7 @@ import type { } from "./SetupTypes.js"; import type { SetupClient } from "./SupportedClients.js"; import type { DoctorScope } from "./Doctor.js"; +import type { DoctorReport } from "./Doctor.js"; /** Read-only setup facts shared by client selection and preflight. */ export interface SetupDiscovery { @@ -36,8 +37,10 @@ export const discoverSetupState = async (input: { readonly forceHopperInstall: boolean; readonly proposeHopper: boolean; readonly doctorScope: DoctorScope | undefined; + readonly initialDoctor?: DoctorReport; }): Promise => { - const initialDoctor = await input.host.doctor(input.doctorScope); + const initialDoctor = + input.initialDoctor ?? (await input.host.doctor(input.doctorScope)); const linuxHopperRepairNeeded = initialDoctor.checks.some( ({ name, ok, detail }) => !ok && diff --git a/src/application/SetupTypes.ts b/src/application/SetupTypes.ts index ac4da9d7b..f3051bdbd 100644 --- a/src/application/SetupTypes.ts +++ b/src/application/SetupTypes.ts @@ -9,6 +9,13 @@ import type { /** Exact non-secret provider variables propagated into managed registrations. */ export type SetupProviderEnvironment = Readonly>; +/** Read-only host facts shared by the initial setup plan. */ +export interface SetupInitialState { + readonly hopperPath?: string; + readonly providerEnvironment: SetupProviderEnvironment; + readonly doctor: DoctorReport; +} + /** Result of one backup/write/readback transaction. */ export type ClientConfigurationResult = | { @@ -38,6 +45,7 @@ export interface SetupHost { macosVersion(): Promise; linuxDistribution(): Promise; hopperPath(): Promise; + initialSetupState?(scope?: DoctorScope): Promise; installHopper(replaceExisting: boolean): Promise; providerEnvironment?(): Promise; detectedClients(): Promise;