Skip to content

perf(pi): skip rewriting current managed extensions on every terminal spawn - #38481

Merged
nwparker merged 1 commit into
mainfrom
OrcaWin/perf-startup-pi-extension-writes
Nov 5, 2026
Merged

nwparker merged 1 commit into
mainfrom
OrcaWin/perf-startup-pi-extension-writes

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

PiTitlebarExtensionService.buildPtyEnv runs on the main thread for every local terminal spawn when agent status hooks are on and a Pi agent home exists (~/.pi/agent, or a configured one). This includes bare shells. Each call ran writeFileSync on three Orca-owned extension files (orca-titlebar-spinner.ts, orca-prefill.ts, orca-agent-status.ts, about 54 KB together) in the user's extensions/ dir, even when they already held the current source.

The ownership check already read each file (canOverwriteManagedExtension) and then threw away what it read. So restoring 50 tabs did 150 synchronous file writes in the user's home, and every new tab rewrote the files and bumped their mtimes. In a main-process CPU profile of one launch, installManagedExtensions took about 36 ms, about 22 ms of it in writeFileUtf8.

The OpenCode and OMP equivalents (writeOpenCodePluginConfig, materializeOmpFreshConfig) already compare before writing. Pi was the outlier.

Fix

writeManagedExtension reads the file once:

  • identical to the current source → done, no write
  • exists without the Orca marker → skipped-user-owned (as before)
  • missing, stale Orca-owned, or unreadable → write (as before)

The redundant existsSync and canOverwriteManagedExtension are removed. Net: one method, same outcomes.

Measurements

Environment: M4 Air, macOS 26, Node 26 through vitest. A temporary harness (not committed) calls buildPtyEnv('pty-i', home, 'pi') N times on a tmp Pi home after one warm-up spawn and counts writeFileSync calls through a node:fs mock. There were 3 runs each at base and fix.

base fix
writeFileSync calls, 50 spawns 150 0
bytes written, 50 spawns 2,709,300 0
writeFileSync calls, 500 spawns 1,500 0
bytes written, 500 spawns 27,093,000 0
main-thread time per spawn (tmpdir, 50 spawns) 0.45 / 0.53 / 0.42 ms 0.33 / 0.28 / 0.31 ms
main-thread time per spawn (tmpdir, 500 spawns) 0.31 / 0.31 / 0.25 ms 0.18 / 0.23 / 0.18 ms

The timings are on a warm tmpdir. Writes into a real home dir (Spotlight/FSEvents) cost more, as the in-app profile above shows. The write and byte counts are deterministic.

The new unit test ("leaves current Orca-owned extensions untouched on later spawns") sets the installed files' mtime to 0, spawns again and asserts the mtime is still 0. It fails without the fix.

Risk / compat

  • Behavior is unchanged for missing, stale, user-owned and unreadable files. Only the "already current" case skips the write.
  • Windows/Linux/macOS: only plain fs reads and writes, no platform branches.
  • SSH/remote: not affected. Host-local env assembly does not run for SSH PTYs.
  • WSL: unchanged. It still writes through the same function when the content differs.
  • Folder workspaces: not involved.

Test plan

  • pnpm test src/main/pi/titlebar-extension-service.test.ts src/main/pi/titlebar-extension-overlay-path.test.ts
  • The new test fails with the fix reverted
  • pnpm tc:node, oxlint, oxfmt
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​15 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​15
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​8 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1

… spawn

Each local terminal spawn with a Pi agent home rewrote three Orca-owned
extension files (~54 KB) synchronously on the main thread even when their
contents were already current. Compare the read we already do for the
ownership check and write only when the content differs.
@OrcaWin
OrcaWin marked this pull request as ready for review November 5, 2026 09:09
@coderabbitai

coderabbitai Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 2 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cdc41379-9e77-4e21-b1a7-9b8567f959c6
Commits

Reviewing files that changed from the base of the PR and between afac091 and 1d55624.

Files selected for processing: 2

  • src/main/pi/titlebar-extension-service.test.ts
  • src/main/pi/titlebar-extension-service.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@nwparker
nwparker merged commit 646e2ef into main Nov 5, 2026
111 of 113 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants