Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/qoder/runtime/qoder-daemon-launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export const INTEGRATION_CLI_ENV = {
QODER_INTEGRATION_ID: '70457212-a449-4ae3-af1e-5fb9e00fafcf',
} as const;

let baseEnvSnapshot: Record<string, string | undefined> | 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<string, string | undefined> {
baseEnvSnapshot ??= { ...process.env };
return baseEnvSnapshot;
}

export interface DaemonLaunchOptions {
pathToQoderCLIExecutable: string;
transport: Options['transport'];
Expand All @@ -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,
Expand All @@ -41,7 +55,7 @@ export function buildDaemonLaunchOptions(
pathToQoderCLIExecutable: cliPath,
transport: ProcessTransport.default,
env: {
...process.env,
...getBaseEnv(),
...extraEnv,
...CUSTOM_PROVIDER_CLI_ENV,
...INTEGRATION_CLI_ENV,
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/qoder/runtime/qoder-daemon-launch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
});
Loading