Skip to content
37 changes: 36 additions & 1 deletion apps/backend/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@

logger = logging.getLogger(__name__)

# =============================================================================
# Windows System Prompt Limits
# =============================================================================
# Windows CreateProcessW has a 32,768 character limit for the entire command line.
# When CLAUDE.md is very large and passed as --system-prompt, the command can exceed
# this limit, causing ERROR_FILE_NOT_FOUND. We cap CLAUDE.md content to stay safe.
# 20,000 chars leaves ~12KB headroom for CLI overhead (model, tools, MCP config, etc.)
WINDOWS_MAX_SYSTEM_PROMPT_CHARS = 20000
WINDOWS_TRUNCATION_MESSAGE = (
"\n\n[... CLAUDE.md truncated due to Windows command-line length limit ...]"
)

# =============================================================================
# Project Index Cache
# =============================================================================
Expand Down Expand Up @@ -821,8 +833,31 @@ def create_client(
if should_use_claude_md():
claude_md_content = load_claude_md(project_dir)
if claude_md_content:
# On Windows, the SDK passes system_prompt as a --system-prompt CLI argument.
# Windows CreateProcessW has a 32,768 character limit for the entire command line.
# When CLAUDE.md is very large, the command can exceed this limit, causing Windows
# to return ERROR_FILE_NOT_FOUND which the SDK misreports as "Claude Code not found".
# Cap CLAUDE.md content to keep total command line under the limit. (#1661)
was_truncated = False
if is_windows():
max_claude_md_chars = (
WINDOWS_MAX_SYSTEM_PROMPT_CHARS
- len(base_prompt)
- len(WINDOWS_TRUNCATION_MESSAGE)
- len("\n\n# Project Instructions (from CLAUDE.md)\n\n")
)
if len(claude_md_content) > max_claude_md_chars > 0:
claude_md_content = (
claude_md_content[:max_claude_md_chars]
+ WINDOWS_TRUNCATION_MESSAGE
)
print(
" - CLAUDE.md: truncated (exceeded Windows command-line limit)"
)
was_truncated = True
base_prompt = f"{base_prompt}\n\n# Project Instructions (from CLAUDE.md)\n\n{claude_md_content}"
print(" - CLAUDE.md: included in system prompt")
if not was_truncated:
print(" - CLAUDE.md: included in system prompt")
else:
print(" - CLAUDE.md: not found in project root")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('Subprocess Spawn Integration', () => {
'Test task description'
]),
expect.objectContaining({
cwd: AUTO_CLAUDE_SOURCE, // Process runs from auto-claude source directory
cwd: TEST_PROJECT_PATH, // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
env: expect.objectContaining({
PYTHONUNBUFFERED: '1'
})
Expand Down Expand Up @@ -218,7 +218,7 @@ describe('Subprocess Spawn Integration', () => {
'spec-001'
]),
expect.objectContaining({
cwd: AUTO_CLAUDE_SOURCE // Process runs from auto-claude source directory
cwd: TEST_PROJECT_PATH // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
})
);
}, 30000); // Increase timeout for Windows CI (dynamic imports are slow)
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('Subprocess Spawn Integration', () => {
'--qa'
]),
expect.objectContaining({
cwd: AUTO_CLAUDE_SOURCE // Process runs from auto-claude source directory
cwd: TEST_PROJECT_PATH // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
})
);
}, 30000); // Increase timeout for Windows CI (dynamic imports are slow)
Expand Down
12 changes: 9 additions & 3 deletions apps/frontend/src/main/agent/agent-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ export class AgentManager extends EventEmitter {
this.registerTaskWithOperationRegistry(taskId, 'spec-creation', { projectPath, taskDescription, specDir });

// Note: This is spec-creation but it chains to task-execution via run.py
await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'task-execution', projectId);
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive file access
// issues on Windows. The script path is absolute so Python finds its modules via sys.path[0]. (#1661)
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'task-execution', projectId);
}

/**
Expand Down Expand Up @@ -410,7 +412,10 @@ export class AgentManager extends EventEmitter {
// Register with unified OperationRegistry for proactive swap support
this.registerTaskWithOperationRegistry(taskId, 'task-execution', { projectPath, specId, options });

await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'task-execution', projectId);
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive file access
// issues on Windows. The script path (runPath) is absolute so Python finds its modules
// via sys.path[0] which is set to the script's directory. (#1661)
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'task-execution', projectId);
}

/**
Expand Down Expand Up @@ -448,7 +453,8 @@ export class AgentManager extends EventEmitter {

const args = [runPath, '--spec', specId, '--project-dir', projectPath, '--qa'];

await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'qa-process', projectId);
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive issues on Windows (#1661)
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'qa-process', projectId);
}

/**
Expand Down
18 changes: 14 additions & 4 deletions apps/frontend/src/main/agent/agent-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import { pythonEnvManager, getConfiguredPythonPath } from '../python-env-manager
import { buildMemoryEnvVars } from '../memory-env-builder';
import { readSettingsFile } from '../settings-utils';
import type { AppSettings } from '../../shared/types/settings';
import { getOAuthModeClearVars } from './env-utils';
import { getOAuthModeClearVars, normalizeEnvPathKey, mergePythonEnvPath } from './env-utils';
import { getAugmentedEnv } from '../env-utils';
import { getToolInfo, getClaudeCliPathForSdk } from '../cli-tool-manager';
import { killProcessGracefully, isWindows } from '../platform';
import { killProcessGracefully, isWindows, getPathDelimiter } from '../platform';
import { debugLog } from '../../shared/utils/debug-logger';

/**
Expand Down Expand Up @@ -679,15 +679,25 @@ export class AgentProcessManager {
},
});

// Parse Python commandto handle space-separated commands like "py -3"
// Merge PATH from pythonEnv with augmented PATH from env.
// pythonEnv may contain its own PATH (e.g., on Windows with pywin32_system32 prepended).
// Simply spreading pythonEnv after env would overwrite the augmented PATH (which includes
// npm globals, homebrew, etc.), causing "Claude code not found" on Windows (#1661).
// mergePythonEnvPath() normalizes PATH key casing and prepends pythonEnv-specific paths.
const mergedPythonEnv = { ...pythonEnv };
const pathSep = getPathDelimiter();

mergePythonEnvPath(env as Record<string, string | undefined>, mergedPythonEnv as Record<string, string | undefined>, pathSep);

// Parse Python command to handle space-separated commands like "py -3"
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(this.getPythonPath());
let childProcess;
try {
childProcess = spawn(pythonCommand, [...pythonBaseArgs, ...args], {
cwd,
env: {
...env, // Already includes process.env, extraEnv, profileEnv, PYTHONUNBUFFERED, PYTHONUTF8
...pythonEnv, // Include Python environment (PYTHONPATH for bundled packages)
...mergedPythonEnv, // Python env with merged PATH (preserves augmented PATH entries)
...oauthModeClearVars, // Clear stale ANTHROPIC_* vars when in OAuth mode
...apiProfileEnv // Include active API profile config (highest priority for ANTHROPIC_* vars)
}
Expand Down
14 changes: 13 additions & 1 deletion apps/frontend/src/main/agent/agent-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { IdeationConfig, Idea } from '../../shared/types';
import { AUTO_BUILD_PATHS } from '../../shared/constants';
import { detectRateLimit, createSDKRateLimitInfo, getBestAvailableProfileEnv } from '../rate-limit-detector';
import { getAPIProfileEnv } from '../services/profile';
import { getOAuthModeClearVars } from './env-utils';
import { getOAuthModeClearVars, normalizeEnvPathKey } from './env-utils';
import { debugLog, debugError } from '../../shared/utils/debug-logger';
import { stripAnsiCodes } from '../../shared/utils/ansi-sanitizer';
import { parsePythonCommand } from '../python-detector';
Expand Down Expand Up @@ -397,6 +397,12 @@ export class AgentQueueManager {
PYTHONUTF8: '1'
};

// Normalize PATH key to a single uppercase 'PATH' entry.
// On Windows, process.env spread produces 'Path' while pythonEnv may write 'PATH',
// resulting in duplicate keys in the final object. Without normalization the child
// process inherits both keys, which can cause tool-not-found errors (#1661).
normalizeEnvPathKey(finalEnv as Record<string, string | undefined>);

// Debug: Show OAuth token source (token values intentionally omitted for security - AC4)
const tokenSource = profileEnv['CLAUDE_CODE_OAUTH_TOKEN']
? 'Electron app profile'
Expand Down Expand Up @@ -730,6 +736,12 @@ export class AgentQueueManager {
PYTHONUTF8: '1'
};

// Normalize PATH key to a single uppercase 'PATH' entry.
// On Windows, process.env spread produces 'Path' while pythonEnv may write 'PATH',
// resulting in duplicate keys in the final object. Without normalization the child
// process inherits both keys, which can cause tool-not-found errors (#1661).
normalizeEnvPathKey(finalEnv as Record<string, string | undefined>);

// Debug: Show OAuth token source (token values intentionally omitted for security - AC4)
const tokenSource = profileEnv['CLAUDE_CODE_OAUTH_TOKEN']
? 'Electron app profile'
Expand Down
165 changes: 164 additions & 1 deletion apps/frontend/src/main/agent/env-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { describe, it, expect } from 'vitest';
import { getOAuthModeClearVars } from './env-utils';
import { getOAuthModeClearVars, normalizeEnvPathKey, mergePythonEnvPath } from './env-utils';

describe('getOAuthModeClearVars', () => {
describe('OAuth mode (no active API profile)', () => {
Expand Down Expand Up @@ -132,3 +132,166 @@ describe('getOAuthModeClearVars', () => {
});
});
});

describe('normalizeEnvPathKey', () => {
it('should leave an already-uppercase PATH key untouched', () => {
const env: Record<string, string | undefined> = { PATH: '/usr/bin:/bin', HOME: '/home/user' };
normalizeEnvPathKey(env);
expect(env).toEqual({ PATH: '/usr/bin:/bin', HOME: '/home/user' });
});

it('should rename a lowercase-variant "Path" key to "PATH"', () => {
const env: Record<string, string | undefined> = { Path: 'C:\\Windows\\system32', HOME: '/home/user' };
normalizeEnvPathKey(env);
expect(env['PATH']).toBe('C:\\Windows\\system32');
expect('Path' in env).toBe(false);
});

it('should prefer existing "PATH" and remove "Path" when both keys coexist', () => {
// Simulates process.env spread ('Path') after getAugmentedEnv writes ('PATH')
const env: Record<string, string | undefined> = {
Path: 'C:\\old',
PATH: 'C:\\Windows\\system32;C:\\augmented',
HOME: '/home/user'
};
normalizeEnvPathKey(env);
expect(env.PATH).toBe('C:\\Windows\\system32;C:\\augmented');
expect('Path' in env).toBe(false);
});

it('should remove all case-variant PATH duplicates when PATH is already present', () => {
const env: Record<string, string | undefined> = {
PATH: '/correct',
Path: '/old1',
path: '/old2'
};
normalizeEnvPathKey(env);
expect(env.PATH).toBe('/correct');
expect('Path' in env).toBe(false);
expect('path' in env).toBe(false);
});

it('should handle env with no PATH-like key gracefully', () => {
const env: Record<string, string | undefined> = { HOME: '/home/user', SHELL: '/bin/zsh' };
normalizeEnvPathKey(env);
expect(env).toEqual({ HOME: '/home/user', SHELL: '/bin/zsh' });
});

it('should return the same env object reference (mutates in place)', () => {
const env: Record<string, string | undefined> = { PATH: '/usr/bin' };
const result = normalizeEnvPathKey(env);
expect(result).toBe(env);
});
});

describe('mergePythonEnvPath - Windows PATH merge logic (#1661)', () => {
const SEP = ';'; // Use Windows separator for these tests

it('should prepend pythonEnv-only entries to the augmented PATH', () => {
const env: Record<string, string | undefined> = {
PATH: 'C:\\npm;C:\\homebrew'
};
const mergedPythonEnv: Record<string, string | undefined> = {
PATH: 'C:\\pywin32_system32;C:\\npm;C:\\homebrew'
};

mergePythonEnvPath(env, mergedPythonEnv, SEP);

// pywin32_system32 is unique to pythonEnv, so it should be prepended
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
});

it('should deduplicate entries that already exist in augmented PATH', () => {
const env: Record<string, string | undefined> = {
PATH: 'C:\\npm;C:\\homebrew;C:\\pywin32_system32'
};
const mergedPythonEnv: Record<string, string | undefined> = {
PATH: 'C:\\pywin32_system32;C:\\npm'
};

mergePythonEnvPath(env, mergedPythonEnv, SEP);

// All pythonEnv entries are already in env.PATH, so mergedPythonEnv.PATH should equal env.PATH
expect(mergedPythonEnv.PATH).toBe('C:\\npm;C:\\homebrew;C:\\pywin32_system32');
});

it('should normalize Windows-style "Path" key in pythonEnv to "PATH"', () => {
const env: Record<string, string | undefined> = {
PATH: 'C:\\npm;C:\\homebrew'
};
// pythonEnv uses 'Path' (Windows native casing)
const mergedPythonEnv: Record<string, string | undefined> = {
Path: 'C:\\pywin32_system32;C:\\npm'
};

mergePythonEnvPath(env, mergedPythonEnv, SEP);

// 'Path' should be normalized to 'PATH' and pythonEnv-specific entry prepended
expect('Path' in mergedPythonEnv).toBe(false);
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
});

it('should normalize Windows-style "Path" in env and deduplicate duplicates', () => {
// Simulates process.env spread ('Path') + getAugmentedEnv write ('PATH') leaving both
const env: Record<string, string | undefined> = {
Path: 'C:\\old',
PATH: 'C:\\npm;C:\\homebrew'
};
const mergedPythonEnv: Record<string, string | undefined> = {
PATH: 'C:\\pywin32_system32;C:\\npm'
};

mergePythonEnvPath(env, mergedPythonEnv, SEP);

// env 'Path' should be removed; augmented 'PATH' value preserved
expect('Path' in env).toBe(false);
expect(env.PATH).toBe('C:\\npm;C:\\homebrew');
// Only the unique pywin32_system32 entry prepended
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
});

it('should use env.PATH unchanged when pythonEnv has no unique entries', () => {
const env: Record<string, string | undefined> = {
PATH: 'C:\\npm;C:\\homebrew'
};
const mergedPythonEnv: Record<string, string | undefined> = {
PATH: 'C:\\npm;C:\\homebrew'
};

mergePythonEnvPath(env, mergedPythonEnv, SEP);

expect(mergedPythonEnv.PATH).toBe('C:\\npm;C:\\homebrew');
});

it('should work correctly with Unix colon separator', () => {
const unixSep = ':';
const env: Record<string, string | undefined> = {
PATH: '/usr/bin:/bin'
};
const mergedPythonEnv: Record<string, string | undefined> = {
PATH: '/opt/pyenv/shims:/usr/bin:/bin'
};

mergePythonEnvPath(env, mergedPythonEnv, unixSep);

// /opt/pyenv/shims is unique and should be prepended
expect(mergedPythonEnv.PATH).toBe('/opt/pyenv/shims:/usr/bin:/bin');
});

it('should handle missing PATH in pythonEnv gracefully (no-op)', () => {
const env: Record<string, string | undefined> = {
PATH: 'C:\\npm;C:\\homebrew'
};
// pythonEnv has no PATH at all
const mergedPythonEnv: Record<string, string | undefined> = {
PYTHONPATH: '/site-packages'
};

mergePythonEnvPath(env, mergedPythonEnv, SEP);

// Nothing should change
expect(mergedPythonEnv.PATH).toBeUndefined();
expect(mergedPythonEnv.PYTHONPATH).toBe('/site-packages');
expect(env.PATH).toBe('C:\\npm;C:\\homebrew');
});
});
Loading