Skip to content

Commit 6089bdb

Browse files
LouisCltclaude
andcommitted
fix(prompts): stop spawning PowerShell for the OS name on Windows
The system prompt's SYSTEM INFORMATION section called os-name on every request. On Windows its windows-release dependency runs wmic, then `powershell (Get-CimInstance Win32_OperatingSystem).caption`, through execaSync with no timeout, on the extension host's only JavaScript thread. Where wmic has been removed (recent Windows 11 builds), PowerShell starts on every request. I measured 3 to 4.5 s per call, during which the extension host is blocked. In one observed freeze, the PowerShell process did not exit, and the extension host stayed blocked: the request was never sent, Stop did nothing, and only reloading the window recovered. On Windows, read the product name from os.version(), which spawns nothing and costs well under a microsecond. Other platforms keep os-name, which does not spawn a process there, with the existing fallback. Fixes #2448 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 53cb5b5 commit 6089bdb

3 files changed

Lines changed: 48 additions & 24 deletions

File tree

‎src/core/prompts/sections/__tests__/system-info.spec.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ vi.mock("../../../../utils/shell", () => ({
1212
import { getSystemInfoSection } from "../system-info"
1313
import osName from "os-name"
1414

15-
const mockOsName = osName as unknown as ReturnType<typeof vi.fn>
15+
const mockOsName = vi.mocked(osName)
1616

1717
describe("getSystemInfoSection", () => {
1818
const mockCwd = "/test/workspace"
1919
const mockHomeDir = "/home/user"
2020

2121
beforeEach(() => {
2222
vi.spyOn(os, "homedir").mockReturnValue(mockHomeDir)
23-
vi.spyOn(os, "platform").mockReturnValue("linux" as any)
23+
vi.spyOn(os, "platform").mockReturnValue("linux")
2424
vi.spyOn(os, "release").mockReturnValue("5.15.0")
2525
})
2626

@@ -34,6 +34,7 @@ describe("getSystemInfoSection", () => {
3434

3535
afterEach(() => {
3636
vi.clearAllMocks()
37+
vi.restoreAllMocks()
3738
})
3839

3940
it("should return system info with os-name when available", () => {
@@ -60,16 +61,31 @@ describe("getSystemInfoSection", () => {
6061
expect(result).toContain(`Current Workspace Directory: ${mockCwd}`)
6162
})
6263

63-
it("should handle Windows platform in fallback", () => {
64-
mockOsName.mockImplementation(() => {
65-
throw new Error("Command failed with ENOENT: powershell")
64+
describe("on Windows", () => {
65+
beforeEach(() => {
66+
vi.spyOn(os, "platform").mockReturnValue("win32")
67+
vi.spyOn(os, "release").mockReturnValue("10.0.26300")
6668
})
67-
vi.spyOn(os, "platform").mockReturnValue("win32" as any)
68-
vi.spyOn(os, "release").mockReturnValue("10.0.19043")
6969

70-
const result = getSystemInfoSection(mockCwd, policyFor())
70+
it("reads the product name from os.version() without calling os-name", () => {
71+
// os-name runs wmic and PowerShell synchronously on Windows; a PowerShell that never
72+
// exits freezes the extension host, so it must not be reached at all.
73+
vi.spyOn(os, "version").mockReturnValue("Windows 11 Pro")
7174

72-
expect(result).toContain("Operating System: win32 10.0.19043")
75+
const result = getSystemInfoSection(mockCwd, policyFor())
76+
77+
expect(result).toContain("Operating System: Windows 11 Pro")
78+
expect(mockOsName).not.toHaveBeenCalled()
79+
})
80+
81+
it("falls back to platform and release when os.version() is empty", () => {
82+
vi.spyOn(os, "version").mockReturnValue("")
83+
84+
const result = getSystemInfoSection(mockCwd, policyFor())
85+
86+
expect(result).toContain("Operating System: win32 10.0.26300")
87+
expect(mockOsName).not.toHaveBeenCalled()
88+
})
7389
})
7490

7591
it("omits the terminal sentence when execute_command is absent", () => {

‎src/core/prompts/sections/system-info.ts‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ import { getShell } from "../../../utils/shell"
55

66
import type { EffectiveToolPolicy } from "../tools/effective-tool-policy"
77

8+
/**
9+
* Describes the host operating system for the system prompt.
10+
*
11+
* Windows bypasses `os-name`: its `windows-release` dependency runs `wmic`, then PowerShell,
12+
* through `execaSync` with no timeout. This section is built for every request, so that call
13+
* blocks the extension host for as long as the process runs. `os.version()` reads the product
14+
* name without starting a process.
15+
*/
16+
function getOsInfo(): string {
17+
const fallback = `${os.platform()} ${os.release()}`
18+
19+
if (os.platform() === "win32") {
20+
return os.version() || fallback
21+
}
22+
23+
try {
24+
return osName()
25+
} catch {
26+
return fallback
27+
}
28+
}
29+
830
/**
931
* Builds the SYSTEM INFORMATION section of the system prompt.
1032
*
@@ -16,16 +38,7 @@ import type { EffectiveToolPolicy } from "../tools/effective-tool-policy"
1638
* @param policy The request's effective tool policy.
1739
*/
1840
export function getSystemInfoSection(cwd: string, policy: EffectiveToolPolicy): string {
19-
// Try to get detailed OS name, fall back to basic info if it fails
20-
let osInfo: string
21-
try {
22-
osInfo = osName()
23-
} catch {
24-
// Fallback when os-name fails (e.g., PowerShell not available on Windows)
25-
const platform = os.platform()
26-
const release = os.release()
27-
osInfo = `${platform} ${release}`
28-
}
41+
const osInfo = getOsInfo()
2942

3043
const executeCommandAvailable = policy.tools.has("execute_command")
3144

‎src/eslint-suppressions.json‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,6 @@
644644
"count": 55
645645
}
646646
},
647-
"core/prompts/sections/__tests__/system-info.spec.ts": {
648-
"@typescript-eslint/no-explicit-any": {
649-
"count": 2
650-
}
651-
},
652647
"core/prompts/tools/__tests__/filter-tools-for-mode.spec.ts": {
653648
"@typescript-eslint/no-explicit-any": {
654649
"count": 14

0 commit comments

Comments
 (0)