Windows PATH truncation in AI harnesses
On Windows 10 and 11, Windows Explorer and several legacy process launchers silently truncate PATH—or drop the entire User PATH—when the expanded value exceeds ~2,047 characters.
No dialog appears, no event log warning is surfaced to the user, and every IDE, terminal, and AI coding harness launched from that shell inherits the damaged environment block.
This is a decades-old Windows shell defect that routinely breaks modern developer machines, where language toolchains (Node/pnpm, Python/uv, Rust/Cargo, Go, D/LDC, Flutter, Android SDK, Git, .NET, Visual Studio) easily push Machine + User PATH past 2,047 characters.
What actually breaks on Windows
Windows stores PATH in two Registry locations and combines them when creating a user environment block:
-
Machine (
HKLM):HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path -
User (
HKCU):HKCU\Environment\Path
Although the Win32 CreateProcess API supports environment variables up to 32,767 characters, older Windows shell plumbing still imposes much smaller hardcoded buffers:
| Component / API | Effective limit | Failure behavior |
|---|---|---|
|
1,024 characters |
Silently truncates the written Registry value itself, permanently destroying trailing entries. |
Windows Explorer ( |
~2,047 characters (2,048 bytes including NUL in legacy buffers) |
Silently truncates the merged |
Win32 |
32,767 characters (per variable) |
Works cleanly when a process rebuilds its own environment directly from Registry and expands |
Because the Registry itself still contains the full Machine and User strings (unless setx corrupted them), opening System Properties shows your paths as "present" while every app launched via Explorer sees a truncated PATH.
Why AI coding harnesses hit this harder than humans
Interactive developers sometimes miss this bug because their favorite terminal profile or shell startup script patches PATH back together.
AI coding harnesses hit it immediately and repeatedly:
-
Non-interactive subshells inherit the broken parent. Harnesses spawn
powershell.exe,pwsh, orcmd.exeas child processes of the IDE or desktop app—inheriting Explorer’s truncatedPATHwithout running interactive login profiles. -
False "tool not installed" loops. An agent checks
Get-Command uv,dub,rg,nu,pnpm, orgit-ai, gets nothing back, assumes the workstation is missing the CLI, and either tries to re-install an already-installed tool or abandons a working workflow. -
Stateless per-command runners do not remember shell edits. Several agent harnesses execute every terminal command in a brand-new child process. Fixing
$env:Pathin one command disappears on the very next command unless the harness process itself repairs its ownPATH(or every CLI command rebuilds$env:Pathfrom Registry first).
Detection check (PowerShell)
Compare the running process’s PATH against the combined Registry Machine + User PATH:
$proc = $env:Path
$machine = [Environment]::GetEnvironmentVariable("Path", "Machine")
$user = [Environment]::GetEnvironmentVariable("Path", "User")
$splitClean = {
param([string]$s)
($s -split ';' | ForEach-Object { $_.Trim().TrimEnd('\') } | Where-Object { $_ })
}
$procSet = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
foreach ($p in (& $splitClean $proc)) { [void]$procSet.Add($p) }
$regEntries = @(& $splitClean $machine) + @(& $splitClean $user)
$dropped = $regEntries | Where-Object { -not $procSet.Contains($_) } | Select-Object -Unique
[pscustomobject]@{
ProcessLength = $proc.Length
MachineLength = $machine.Length
UserLength = $user.Length
RegistryTotal = $machine.Length + 1 + $user.Length
DroppedCount = $dropped.Count
RegistryOver2k = ($user.Length -gt 2047) -or (($machine.Length + 1 + $user.Length) -gt 2047)
}
To repair the current PowerShell process immediately:
$env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [Environment]::GetEnvironmentVariable("Path", "User")
How third-party AI harnesses handle it (session vs. conversation)
Inside third-party harnesses (Cursor, Antigravity, Claude Code, Codex, Windsurf), an agent inside a chat usually cannot determine whether a conversation shares an already-checked process session or started a fresh one, and some harnesses spawn a fresh shell per command.
Dev-Centr’s agent-rules (general/windows-path-safety.md and skill windows-path-safety) therefore makes the check mandatory at the start of every new conversation on Windows (before relying on CLI probes or running tool-dependent commands):
-
Run the process-vs-Registry check once on the first shell command of the conversation (or via a native session-start hook if the harness exposes one that runs in the same inherited environment).
-
If truncation is detected:
-
Report the truncation once to the user with the dropped-entry count and a link to this page.
-
Record
WINDOWS_PATH_TRUNCATION = detected (…)in machine-local$CODE_ROOT/harness.md. -
Prepend the Registry
PATHrefresh to CLI-dependent commands in stateless per-command runners so tools are never falsely reported as missing.
-
How DevCentr Harness handles it automatically
Unlike third-party hosts, DevCentr Harnessunchecked controls its own process startup:
-
On every process launch (
harness init,spawn,serve, etc.), the runtime readsHKLMMachinePATHandHKCUUserPATHdirectly from the Windows Registry, expandsREG_EXPAND_SZreferences, and compares them against the inherited processPATH. -
Automatic process repair: When entries from Registry are missing from the process
PATH, Harness merges and deduplicatesMachine+User+ any process-only additions and updates its own processPATHvia Win32SetEnvironmentVariableW. Every child process and tool spawned by Harness then inherits the completePATH. -
Visible UI & CLI reporting:
-
In the Harness desk UI, a prominent banner reports either that Windows truncated
PATHand Harness automatically repaired it for this process (showing the dropped-entry count and sample dropped paths), or an error banner if the check or repair failed—always with a direct link back to this article. -
Over HTTP,
GET /api/healthandGET /api/env-healthexpose the structuredpathHealthpayload. -
On CLI subcommands, repair or failure diagnostics are written to
stderrsostdoutJSON stays intact.
-
|
Why show an alert even after auto-fixing?
Process-level repair unblocks the current Harness run, but Windows Explorer is still truncating |
Permanent remediation on Windows
Process-level repair keeps your current session working, but to stop Windows Explorer from truncating newly launched apps altogether, bring both User PATH and combined Machine + User PATH back under 2,047 characters:
-
Remove duplicates and missing directories from
HKCU\Environment\PathandHKLM...\Environment\Path. -
Consolidate tool shims into one short directory (for example
C:\binor a unified shim folder) instead of adding one long%LOCALAPPDATA%\Programs...directory per tool. -
Use short roots or junctions (such as
Z:\codeorC:\tools) instead of deep nested profile paths. -
Never use
setxto editPATH(it truncates at 1,024 characters). Use Pathman (DevCentr’sPATHCLI, not yet published), the Registry API ([Environment]::SetEnvironmentVariable), or a modern PATH editor that preservesREG_EXPAND_SZand avoidssetx. -
Restart Explorer or sign out and back in after trimming Registry
PATHso newly launched desktop apps inherit the clean environment block.
Related
-
Shell environment refresh — why running terminals and IDEs keep stale
PATHvalues even when not truncated -
DevCentr agent harnessunchecked — runtime architecture and desk UI
-
DevCentr Pathman — Windows
PATHmanagement CLI (not yet published) -
general/windows-path-safety.mdunchecked — portable mandatory session-start rule for AI harnesses