Repository navigation
fix(windows): slow launch + keystroke stalls — main process synchronously spawns powershell to re-harden env-store ACLs - #4840
Conversation
…n thread markEnvironmentUsed re-hardened the env-store on every runtime round-trip, spawning powershell.exe ~5x synchronously (~1-1.5s each) and freezing the Windows UI on launch and every keystroke. Throttle the non-security lastUsedAt write (60s window; runtimeId changes bypass) and make path hardening idempotent per process. ACL hardening is preserved; post-rename credential hardening still always runs. ~14.0s -> ~6.8s launch; main thread 95.7% blocked -> 99.0% idle.
WalkthroughThrottle markEnvironmentUsed to skip persisting lastUsedAt when unchanged within 60s, and add an in-process hardened-path cache to avoid redundant Windows ACL hardening. Tests updated to validate throttling, runtimeId-change persistence, caching, invalidation, retry behavior, and test isolation. ChangesWrite-throttling and hardening cache
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — throttle markEnvironmentUsed store writes and deduplicate hardenSecurePath calls to eliminate synchronous PowerShell ACL spawns that block the Electron main thread on Windows.
- Throttle
markEnvironmentUsedinruntime-environment-store.ts— skip rewriting the environment store whenruntimeIdis unchanged andlastUsedAtwas persisted within the last 60s. AruntimeIdchange always persists immediately. - Idempotent path hardening in
secure-file.ts— a process-lifetimeSet(hardenedPathsThisProcess) skips redundanthardenSecurePathcalls for paths already hardened. The post-rename target hardening always runs (new inode) and refreshes the cache entry. - Add
__resetSecureFileHardenedPathsForTests— test-only reset hook, mirroring the existing__resetSecureFileWindowsUserSidForTests.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
|
Update — validated cross-platform. Both changes are platform-neutral. Windows is where the performance win lands (the synchronous Test matrix against this branch's HEAD:
Ready for review. |
Jinwoo-H
left a comment
There was a problem hiding this comment.
Thanks for the detailed Windows perf investigation and fix. I verified the reported path and made one local hardening follow-up before approval:
- markEnvironmentUsed() now avoids rewriting the environment store for fresh same-runtime touches.
- secure-file hardening still runs after each atomic target rename, but repeated read/directory hardening is cached.
- the hardening cache now only records confirmed successes, retries after failed ACL rewrites, and invalidates on file metadata changes.
Validated with targeted shared tests, full typecheck, lint, and an Electron IPC run against a local encrypted fake runtime. The repeated successful runtime calls no longer rewrote orca-environments.json.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/shared/secure-file.test.ts (1)
197-209: 💤 Low valueConsider using a more robust timestamp wait for flaky-test prevention.
The 20ms delay should work on most systems, but filesystem timestamp resolution can vary (e.g., FAT32 has 2-second resolution). If this test ever runs against unusual filesystems or under heavy load, consider either increasing the delay or comparing stat results in a retry loop.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/shared/secure-file.test.ts` around lines 197 - 209, The fixed 20ms sleep in waitForFileTimestampTick is flaky on filesystems with coarse timestamp resolution; replace it with a robust retry loop that checks a file's mtime until it advances or a timeout is reached (or increase the delay to a safe value like 2s), and update tests that rely on it (references: waitForFileTimestampTick, getPowerShellCalls, getPowerShellTarget) to call the new waiter so they assert against a reliably updated timestamp rather than assuming a 20ms tick.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/shared/secure-file.test.ts`:
- Around line 197-209: The fixed 20ms sleep in waitForFileTimestampTick is flaky
on filesystems with coarse timestamp resolution; replace it with a robust retry
loop that checks a file's mtime until it advances or a timeout is reached (or
increase the delay to a safe value like 2s), and update tests that rely on it
(references: waitForFileTimestampTick, getPowerShellCalls, getPowerShellTarget)
to call the new waiter so they assert against a reliably updated timestamp
rather than assuming a 20ms tick.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8a650cc7-ffe9-4291-82d4-ad592ba378f6
📒 Files selected for processing (3)
src/shared/runtime-environment-store.test.tssrc/shared/secure-file.test.tssrc/shared/secure-file.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/shared/runtime-environment-store.test.ts
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — upgraded the process-lifetime hardening cache from a simple Set<string> to a metadata-aware Map<string, HardenedPathCacheEntry> that uses statSync to detect file identity changes, preventing stale-cache skips when a target is replaced.
- Metadata-aware cache in
secure-file.ts—hardenSecurePathOncenow compares currentstatSyncmetadata (dev, ino, size, timestamps) against the cached entry; re-hardens when metadata differs (file replaced, deleted, or type changed). Failed hardening calls are not cached, so they retry on the next invocation. - Return value plumbing —
hardenSecurePathOnce,applySecurePathRestriction, andbestEffortRestrictWindowsPathnow returnbooleanso callers know whether hardening succeeded.writeSecureFileuses this to conditionally refresh the directory cache entry after successful writes. - Test hardening — four new tests cover cache-hit, metadata-change invalidation, retry-after-failure, and
writeSecureFiledirectory caching + per-write target hardening. Env-store tests now pinprocess.platform = 'linux'to avoid accidentally exercising Windows ACL code on Windows CI.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
Two changes fix the v1.4.52+ Windows performance regression (stablyai#4901 regression against stablyai#4840) where 49 powershell.exe processes were spawned in 27 seconds during load, saturating the Electron main thread and causing black terminals and runtimeEnvironments:call timeouts. Root cause: `readEnvironmentStore` calls `hardenExistingSecureFile` on every read. The env-store parent directory's mtime churns constantly (every secure write updates it), so the mtime-keyed idempotency cache never matched → `bestEffortRestrictWindowsPath` (powershell, ~1-1.5s synchronous) fired on every call. After stablyai#4901, the remote-runtime tab-sync loop reads the store ~2×/s, turning sporadic mtime misses into a continuous main-thread storm. Fix 1 – path-cached directory hardening: add `hardenedDirectoryPathsThisProcess (Set<string>)` that caches directory hardening by PATH for the process lifetime. A directory's required ACL does not change when its mtime changes; only file hardening retains the metadata-keyed cache so post-rename inode changes are detected correctly. Fix 2 – async ACL application: replace `execFileSync(powershell.exe, ...)` with `execFile` (fire-and-forget). PowerShell cold-start is ~1-1.5s; the function is already named `bestEffortRestrictWindowsPath` so async/optimistic caching is correct. `applySecurePathRestriction` returns `true` optimistically on win32 so the cache entry is written before the background process completes. Tests: new regression tests verify the directory is hardened exactly once even when its mtime changes between calls, that unchanged files are not re-hardened, and that ACL application goes through async execFile (not execFileSync).
#5011) * fix(windows): stop main-thread PowerShell storm on env-store reads Two changes fix the v1.4.52+ Windows performance regression (#4901 regression against #4840) where 49 powershell.exe processes were spawned in 27 seconds during load, saturating the Electron main thread and causing black terminals and runtimeEnvironments:call timeouts. Root cause: `readEnvironmentStore` calls `hardenExistingSecureFile` on every read. The env-store parent directory's mtime churns constantly (every secure write updates it), so the mtime-keyed idempotency cache never matched → `bestEffortRestrictWindowsPath` (powershell, ~1-1.5s synchronous) fired on every call. After #4901, the remote-runtime tab-sync loop reads the store ~2×/s, turning sporadic mtime misses into a continuous main-thread storm. Fix 1 – path-cached directory hardening: add `hardenedDirectoryPathsThisProcess (Set<string>)` that caches directory hardening by PATH for the process lifetime. A directory's required ACL does not change when its mtime changes; only file hardening retains the metadata-keyed cache so post-rename inode changes are detected correctly. Fix 2 – async ACL application: replace `execFileSync(powershell.exe, ...)` with `execFile` (fire-and-forget). PowerShell cold-start is ~1-1.5s; the function is already named `bestEffortRestrictWindowsPath` so async/optimistic caching is correct. `applySecurePathRestriction` returns `true` optimistically on win32 so the cache entry is written before the background process completes. Tests: new regression tests verify the directory is hardened exactly once even when its mtime changes between calls, that unchanged files are not re-hardened, and that ACL application goes through async execFile (not execFileSync). * fix(windows): apply credential-file ACL synchronously on write path Follow-up rigor on the env-store PowerShell ACL storm fix (#5006). The read-path storm fix (path-cached async directory hardening + async file re-harden) is retained, but switching ALL ACL application to async opened a narrow Windows-only security window: because writeFileSync({mode}) is a no-op on Windows, writeSecureFile returned with the credential file still carrying the parent directory's inherited (broader) ACL for the ~1-1.5s PowerShell cold-start, affecting the e2ee keypair, device registry, and runtime env auth store. Fix: apply the credential FILE's ACL synchronously (execFileSync) on the infrequent write path, before the atomic rename publishes it, and cache the path as hardened only on confirmed success so a failed apply retries. Keep the DIRECTORY hardening async + path-cached for the process lifetime (that is what killed the #4901/#5006 main-thread storm). The read path's existing-file re-harden stays async + metadata-cached (fires at most once per file, no storm). Also: - Document the dir-path cache process-lifetime known limitation (deleted+ recreated dir not re-hardened until restart). - Remove the redundant double dir-cache write in writeSecureFile. - Add docs/windows-secure-file-acl-hardening.md describing the sync-file/ async-dir model and a manual Windows e2e test plan (the cross-platform Playwright harness runs on Linux and cannot reach the PowerShell path). Tests (src/shared/secure-file.test.ts, 13 passing): credential file hardened synchronously while dir stays async (no async file-ACL window); failed sync file-ACL apply is not cached and retries; dir hardened exactly once across many writes despite mtime churn; no PowerShell spawned on non-win32. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * fix: keep POSIX secure directory hardening metadata-aware --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: Neil <[email protected]>
…usly spawns powershell to re-harden env-store ACLs (stablyai#4840) * fix(windows): stop synchronous powershell ACL spawns blocking the main thread markEnvironmentUsed re-hardened the env-store on every runtime round-trip, spawning powershell.exe ~5x synchronously (~1-1.5s each) and freezing the Windows UI on launch and every keystroke. Throttle the non-security lastUsedAt write (60s window; runtimeId changes bypass) and make path hardening idempotent per process. ACL hardening is preserved; post-rename credential hardening still always runs. ~14.0s -> ~6.8s launch; main thread 95.7% blocked -> 99.0% idle. * Fix secure-file hardening cache retries --------- Co-authored-by: Jinwoo Hong <[email protected]>
stablyai#5011) * fix(windows): stop main-thread PowerShell storm on env-store reads Two changes fix the v1.4.52+ Windows performance regression (stablyai#4901 regression against stablyai#4840) where 49 powershell.exe processes were spawned in 27 seconds during load, saturating the Electron main thread and causing black terminals and runtimeEnvironments:call timeouts. Root cause: `readEnvironmentStore` calls `hardenExistingSecureFile` on every read. The env-store parent directory's mtime churns constantly (every secure write updates it), so the mtime-keyed idempotency cache never matched → `bestEffortRestrictWindowsPath` (powershell, ~1-1.5s synchronous) fired on every call. After stablyai#4901, the remote-runtime tab-sync loop reads the store ~2×/s, turning sporadic mtime misses into a continuous main-thread storm. Fix 1 – path-cached directory hardening: add `hardenedDirectoryPathsThisProcess (Set<string>)` that caches directory hardening by PATH for the process lifetime. A directory's required ACL does not change when its mtime changes; only file hardening retains the metadata-keyed cache so post-rename inode changes are detected correctly. Fix 2 – async ACL application: replace `execFileSync(powershell.exe, ...)` with `execFile` (fire-and-forget). PowerShell cold-start is ~1-1.5s; the function is already named `bestEffortRestrictWindowsPath` so async/optimistic caching is correct. `applySecurePathRestriction` returns `true` optimistically on win32 so the cache entry is written before the background process completes. Tests: new regression tests verify the directory is hardened exactly once even when its mtime changes between calls, that unchanged files are not re-hardened, and that ACL application goes through async execFile (not execFileSync). * fix(windows): apply credential-file ACL synchronously on write path Follow-up rigor on the env-store PowerShell ACL storm fix (stablyai#5006). The read-path storm fix (path-cached async directory hardening + async file re-harden) is retained, but switching ALL ACL application to async opened a narrow Windows-only security window: because writeFileSync({mode}) is a no-op on Windows, writeSecureFile returned with the credential file still carrying the parent directory's inherited (broader) ACL for the ~1-1.5s PowerShell cold-start, affecting the e2ee keypair, device registry, and runtime env auth store. Fix: apply the credential FILE's ACL synchronously (execFileSync) on the infrequent write path, before the atomic rename publishes it, and cache the path as hardened only on confirmed success so a failed apply retries. Keep the DIRECTORY hardening async + path-cached for the process lifetime (that is what killed the stablyai#4901/stablyai#5006 main-thread storm). The read path's existing-file re-harden stays async + metadata-cached (fires at most once per file, no storm). Also: - Document the dir-path cache process-lifetime known limitation (deleted+ recreated dir not re-hardened until restart). - Remove the redundant double dir-cache write in writeSecureFile. - Add docs/windows-secure-file-acl-hardening.md describing the sync-file/ async-dir model and a manual Windows e2e test plan (the cross-platform Playwright harness runs on Linux and cannot reach the PowerShell path). Tests (src/shared/secure-file.test.ts, 13 passing): credential file hardened synchronously while dir stays async (no async file-ACL window); failed sync file-ACL apply is not cached and retries; dir hardened exactly once across many writes despite mtime churn; no PowerShell spawned on non-win32. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * fix: keep POSIX secure directory hardening metadata-aware --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: Neil <[email protected]>

Fixes #4839
What this fixes
On Windows, the desktop app is slow to launch (~2×) and stalls on every keystroke in
bursts. Root cause: the Electron main process synchronously spawns
powershell.exeto re-apply NTFS ACLs to the runtime environment-store JSON on every runtime round-trip
(and at startup).
execFileSyncblocks the main thread ~1–1.5s per PowerShell coldstart, and a single
markEnvironmentUsed()triggers ~5 of these spawns(read-path + write-path hardening), so typing — which round-trips to the runtime
continuously — freezes the UI.
This is
process.platform === 'win32'-gated, so a headless Linuxorca serveneverhits it; only the Windows client does.
Measured impact (same machine, 3 runs each)
CDP performance trace during paced typing: the Electron main thread goes from
95.7% blocked in synchronous
spawnto 99.0% idle. ~51% faster launch andkeystroke latency disappears.
The change
Two small, composable fixes — the hardening behavior is preserved, it just stops
blocking the main thread redundantly:
src/shared/runtime-environment-store.ts— throttlemarkEnvironmentUsed.lastUsedAtis a non-security coarse-freshness timestamp, yet stamping it forced afull read+harden+write+harden cycle on every runtime call. Skip the rewrite when the
runtimeIdis unchanged andlastUsedAtwas persisted within the last 60s. AruntimeIdchange inside the window still persists immediately. This removesessentially all per-keystroke spawns.
src/shared/secure-file.ts— make directory/file hardening idempotent per process.A process-lifetime
Setof already-hardened paths makes the read-path re-harden andrepeated directory hardening no-ops after the first. The post-rename target write —
the one case that legitimately needs re-hardening (new inode) — is deliberately kept
always-on and refreshes the cache entry.
hardenSecurePathstays pure;hardenSecurePathOnceis the new caching wrapper.A test-only reset hook (
__resetSecureFileHardenedPathsForTests) mirrors the existing__resetSecureFileWindowsUserSidForTests.No visual change
This is a main-process performance fix only. No UI, layout, copy, or styling changes.
Testing
runtime-environment-store.test.ts: throttle skips a same-runtimewrite inside the window, persists once it elapses, and persists immediately on a
runtimeIdchange inside the window.pnpm lint✅ ·pnpm typecheck✅ ·pnpm build✅.pnpm test: touched suites (secure-file.test.ts,runtime-environment-store.test.ts)pass. (My local full-suite run had unrelated renderer-store import failures that also
reproduce on a clean tree — not introduced here.)
Validated on the two non-Windows platforms too, since both changes are platform-neutral:
secure-file+runtime-environment-storeteststypecheck:node(Windows is where the perf win lands; Linux/macOS exercise the
chmodSyncbranch andconfirm the throttle + idempotency cache are behavior-preserving there.)
AI coding-agent review summary
platform (no new platform branches). The throttle makes
markEnvironmentUsedpersistthe non-security
lastUsedAt/updatedAttimestamps less often everywhere (≤60s coarser;display-only field, no logic depends on it). The idempotency cache skips re-hardening an
already-hardened path within a process on all platforms — on macOS/Linux that elides a
redundant
chmodSync.hardenSecurePathitself stays pure; only the newhardenSecurePathOncewrapper caches, and the write path always re-hardens its ownfreshly-renamed target uncached. Because chmod/ACL application is idempotent, end state
is unchanged; the macOS/Linux delta is strictly fewer redundant syscalls and a coarser
last-used timestamp, not a behavior change in what gets protected. The Windows-only win
is removing the synchronous PowerShell spawns — the performance bug never affected
macOS/Linux, which take the cheap
chmodSyncbranch.src/sharedenv-store/secure-file write paths,exercised identically regardless of transport. Headless
orca serve(Linux) never ranthe win32 spawn, so its behavior is unchanged; the local Windows client is where the
win is realized.
markEnvironmentUsedstill recordsruntimeIdand a coarselastUsedAt; consumers reading those see at most a ≤60s-stalelastUsedAt, never astale
runtimeId(runtime changes bypass the throttle). No integration contract change.runtime round-trip and the redundant ones at startup. Adds an O(1)
Setlookup.hardening always runs. The cache only suppresses repeat hardening of a path already
hardened in the same process (same ACLs would be re-applied). The throttle only defers
a non-security timestamp write. No credential file is left with weaker ACLs than today.
Related
sidebar panel": same symptom class (Windows main-thread freeze), plausibly the same
synchronous-spawn mechanism via a different trigger.
A follow-up could make the ACL call async (
execFile) and/or swappowershell.exefor asingle
icacls.exeinvocation (tens of ms vs ~1–1.5s); kept out of this PR to keep itsingle-topic.
Summary by CodeRabbit
Release Notes
Performance Improvements
Bug Fixes