diff --git a/src/qoder/runtime/qoder-daemon-launch.ts b/src/qoder/runtime/qoder-daemon-launch.ts index e8c5cb1..f47820f 100644 --- a/src/qoder/runtime/qoder-daemon-launch.ts +++ b/src/qoder/runtime/qoder-daemon-launch.ts @@ -17,6 +17,19 @@ export const INTEGRATION_CLI_ENV = { QODER_INTEGRATION_ID: '70457212-a449-4ae3-af1e-5fb9e00fafcf', } as const; +let baseEnvSnapshot: Record | undefined; + +/** + * Returns the host environment as first seen by this module. Other plugins + * share the renderer's `process.env` and may add keys at any time (e.g. a + * bundled Claude Agent SDK sets `CLAUDE_AGENT_SDK_VERSION`); reading it live + * would make later Queries conflict with the already-running daemon. + */ +function getBaseEnv(): Record { + baseEnvSnapshot ??= { ...process.env }; + return baseEnvSnapshot; +} + export interface DaemonLaunchOptions { pathToQoderCLIExecutable: string; transport: Options['transport']; @@ -30,7 +43,8 @@ export interface DaemonLaunchOptions { * launches that process, so launch-critical settings (the CLI entry and its * environment) must live in these options instead. The daemon is process-wide * and rejects a Query whose launch options differ, so every query site must - * build them through this helper. + * build them through this helper, and the inherited environment comes from a + * one-time snapshot rather than the live `process.env`. */ export function buildDaemonLaunchOptions( cliPath: string, @@ -41,7 +55,7 @@ export function buildDaemonLaunchOptions( pathToQoderCLIExecutable: cliPath, transport: ProcessTransport.default, env: { - ...process.env, + ...getBaseEnv(), ...extraEnv, ...CUSTOM_PROVIDER_CLI_ENV, ...INTEGRATION_CLI_ENV, diff --git a/tests/unit/qoder/runtime/qoder-daemon-launch.test.ts b/tests/unit/qoder/runtime/qoder-daemon-launch.test.ts index 28e048f..7cb178e 100644 --- a/tests/unit/qoder/runtime/qoder-daemon-launch.test.ts +++ b/tests/unit/qoder/runtime/qoder-daemon-launch.test.ts @@ -33,4 +33,21 @@ describe('buildDaemonLaunchOptions', () => { expect(options.env.CUSTOM_FLAG).toBe('1'); expect(options.env.PATH).toBe('/opt/bin'); }); + + it('ignores process.env keys added after the first build', () => { + // Other plugins share the renderer's process.env (e.g. Copilot sets + // CLAUDE_AGENT_SDK_VERSION), and the SDK rejects a Query whose daemon env + // gained a key since the daemon started. + const first = buildDaemonLaunchOptions('/opt/qodercli', '/opt/bin'); + process.env.QODERIAN_TEST_LATE_ENV = '0.3.306'; + try { + const second = buildDaemonLaunchOptions('/opt/qodercli', '/opt/bin'); + + // Compare keys only so a failure never prints the host's env values. + expect(Object.keys(second.env).sort()).toEqual(Object.keys(first.env).sort()); + expect(second.env.QODERIAN_TEST_LATE_ENV).toBeUndefined(); + } finally { + delete process.env.QODERIAN_TEST_LATE_ENV; + } + }); });