Repository navigation
refactor(shell): build every zsh startup wrapper from one shared builder - #15245
Conversation
Captures .zshenv/.zprofile/.zshrc/.zlogin, the bash rcfile, and the fish init command for all three transports (local PTY, daemon/SSH, relay) so the upcoming wrapper unification can be proven byte-for-byte identical.
Local PTY, daemon/SSH, and relay each had their own copy of the zsh ZDOTDIR wrapper templates, and the copies had drifted. buildZshStartupWrapperFiles now produces .zshenv/.zprofile/.zshrc/.zlogin for all three, with every real difference expressed as a field on ZshStartupWrapperSpec. No behavior change: the generated text is byte-for-byte identical for every configuration, pinned by the snapshots committed in the previous commit (captured from the pre-refactor generators).
📝 WalkthroughWalkthroughThe change centralizes Zsh startup-wrapper generation in a shared builder with transport-specific specifications. Local PTY, daemon, and relay integrations now write builder-generated startup files. Zsh templates preserve directory discovery, startup-file sourcing, environment restoration, history handling, command wrappers, and OSC 133 markers. Bash and Fish wrapper snapshots add shell lifecycle and readiness behavior. Snapshot tests cover generated wrappers across transports. Merge Risk: 🟡 Moderate · up to The current head can prevent relay Bash sessions from starting for users with 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
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.
Actionable comments posted: 2
🧹 Nitpick comments (1)
src/main/providers/local-pty-shell-ready-wrapper-generation.ts (1)
48-50: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse
joinfor wrapper paths.Build
zshDir,bashDir, and each wrapper file path withjoin. The template literals assume/and can create mixed separators when this exported helper runs on a non-POSIX host.As per coding guidelines, “Use
path.joinor Electron/Node path utilities for file paths; never assume/or\.”Also applies to: 65-69
Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: d9c231e9-c136-4a8f-9f18-c1bf33c01f45
📒 Files selected for processing (25)
src/main/daemon/daemon-zsh-shell-ready-rcfile.tssrc/main/daemon/daemon-zsh-shell-ready-wrapper-spec.tssrc/main/daemon/shell-ready.tssrc/main/providers/local-pty-shell-ready-wrapper-generation.tssrc/main/shell-templates.tssrc/main/shell-wrapper-generated-file-snapshot.test.tssrc/main/shell-wrapper-snapshots/daemon-bash-rcfile.txtsrc/main/shell-wrapper-snapshots/daemon-fish-init.txtsrc/main/shell-wrapper-snapshots/daemon-zsh-zlogin.txtsrc/main/shell-wrapper-snapshots/daemon-zsh-zprofile.txtsrc/main/shell-wrapper-snapshots/daemon-zsh-zshenv.txtsrc/main/shell-wrapper-snapshots/daemon-zsh-zshrc.txtsrc/main/shell-wrapper-snapshots/local-bash-rcfile.txtsrc/main/shell-wrapper-snapshots/local-fish-init.txtsrc/main/shell-wrapper-snapshots/local-zsh-zlogin.txtsrc/main/shell-wrapper-snapshots/local-zsh-zprofile.txtsrc/main/shell-wrapper-snapshots/local-zsh-zshenv.txtsrc/main/shell-wrapper-snapshots/local-zsh-zshrc.txtsrc/main/shell-wrapper-snapshots/relay-bash-rcfile.txtsrc/main/shell-wrapper-snapshots/relay-zsh-zlogin.txtsrc/main/shell-wrapper-snapshots/relay-zsh-zprofile.txtsrc/main/shell-wrapper-snapshots/relay-zsh-zshenv.txtsrc/main/shell-wrapper-snapshots/relay-zsh-zshrc.txtsrc/main/zsh-startup-wrapper-builder.tssrc/relay/pty-shell-overlay-wrappers.ts
💤 Files with no reviewable changes (1)
- src/main/daemon/daemon-zsh-shell-ready-rcfile.ts
Included review availability: Your plan includes up to 10 reviews per rolling hour; 2 remain after this review.
| printf "\033]133;D;%s\007" "$exit_code" | ||
| unset __orca_in_command | ||
| fi | ||
| printf "\033]133;A\007" | ||
| # Why: emit the shell-ready marker here (not a trailing PROMPT_COMMAND entry) | ||
| # so a framework that must be last in PROMPT_COMMAND — bash-preexec — is not | ||
| # displaced by one of Orca's own hooks. | ||
| [[ "${ORCA_SHELL_READY_MARKER:-0}" == "1" ]] && printf "\033]777;orca-shell-ready\007" | ||
| return "$exit_code" |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -eu
for f in \
src/main/shell-wrapper-snapshots/daemon-bash-rcfile.txt \
src/main/shell-wrapper-snapshots/local-bash-rcfile.txt \
src/main/shell-wrapper-snapshots/relay-bash-rcfile.txt
do
echo "===== $f ====="
wc -l "$f"
rg -n -C 3 'printf|PROMPT_COMMAND|ORCA_SHELL_READY_MARKER|osc133|orca-shell-ready' "$f"
doneRepository: stablyai/orca
Length of output: 26888
🏁 Script executed:
#!/bin/bash
set -eu
echo '===== Bash command-resolution probe ====='
bash -c '
printf() { :; }
marker() { printf "\033]133;A\007"; }
value=
normalize() { printf -v value "%s" "$1"; }
printf "direct-output\n"
marker
normalize "normalized"
printf "value=<%s>\n" "$value"
unset -f printf
printf "builtin-output\n"
marker
value=
normalize "normalized"
printf "value=<%s>\n" "$value"
'
echo '===== Unqualified printf inventory ====='
for f in \
src/main/shell-wrapper-snapshots/daemon-bash-rcfile.txt \
src/main/shell-wrapper-snapshots/local-bash-rcfile.txt \
src/main/shell-wrapper-snapshots/relay-bash-rcfile.txt
do
echo "===== $f ====="
rg -n '^[[:space:]]*printf([[:space:]]|$)' "$f"
doneRepository: stablyai/orca
Length of output: 1220
Use builtin printf for every wrapper-owned printf call in all three Bash snapshots.
A startup-file printf function overrides the Bash builtin. It can suppress OSC 133/777 markers and prevent printf -v from assigning normalized prompt values. Update all unqualified calls in daemon-bash-rcfile.txt, local-bash-rcfile.txt, and relay-bash-rcfile.txt.
📍 Affects 3 files
src/main/shell-wrapper-snapshots/daemon-bash-rcfile.txt#L80-L88(this comment)src/main/shell-wrapper-snapshots/local-bash-rcfile.txt#L80-L88src/main/shell-wrapper-snapshots/relay-bash-rcfile.txt#L64-L72
| if [[ -n "${ORCA_HISTFILE:-}" ]]; then | ||
| HISTFILE="$ORCA_HISTFILE" | ||
| elif [[ "${HISTFILE:-}" == "$ZDOTDIR/.zsh_history" ]]; then | ||
| # Why also when Orca injected nothing: /etc/zshrc derived this from Orca's | ||
| # wrapper ZDOTDIR, so history would accumulate INSIDE the wrapper dir and the | ||
| # user's real history would be invisible — the plain #11044 bug, with no | ||
| # per-worktree scoping involved. Matching the exact clobbered value means a | ||
| # HISTFILE the user set deliberately is never touched. | ||
| HISTFILE="${ORCA_ORIG_ZDOTDIR:-$HOME}/.zsh_history" | ||
| fi |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -eu
printf '%s\n' '--- candidate files ---'
git ls-files | rg '(^|/)(relay-bash-rcfile\.txt|pty-shell-launch\.ts|.*(bash|zsh).*(rc|overlay|snapshot).*)$' || true
printf '%s\n' '--- wrapper snapshot ---'
cat -n src/main/shell-wrapper-snapshots/relay-bash-rcfile.txt | sed -n '1,130p'
printf '%s\n' '--- launch selection and environment ---'
ast-grep outline src/relay/pty-shell-launch.ts --view expanded || true
cat -n src/relay/pty-shell-launch.ts | sed -n '50,145p'
printf '%s\n' '--- related history and rcfile generation references ---'
rg -n -C 4 'relay-bash-rcfile|ORCA_HISTFILE|ORCA_ORIG_ZDOTDIR|ZDOTDIR|zsh_history|HISTFILE|set -u|nounset' src test tests 2>/dev/null || trueRepository: stablyai/orca
Length of output: 50372
🏁 Script executed:
#!/bin/bash
set -eu
printf '%s\n' '--- Bash snapshot ---'
cat -n src/main/shell-wrapper-snapshots/relay-bash-rcfile.txt | sed -n '1,125p'
printf '%s\n' '--- PTY launcher references ---'
rg -n -C 8 --glob '*.ts' --glob '*.tsx' \
'relay-bash-rcfile|bash.*--rcfile|--rcfile|ORCA_HISTFILE|ORCA_ORIG_ZDOTDIR' \
src/relay src/main 2>/dev/null | head -n 500
printf '%s\n' '--- snapshot/generator references ---'
rg -n -C 5 --glob '*.ts' --glob '*.tsx' --glob '*.txt' \
'relay-bash-rcfile|shell-wrapper-snapshots|HISTFILE.*ZDOTDIR|ZDOTDIR.*HISTFILE' \
. 2>/dev/null | head -n 500Repository: stablyai/orca
Length of output: 50369
🏁 Script executed:
#!/bin/bash
set -eu
printf '%s\n' '--- launcher source ---'
cat -n src/relay/pty-shell-launch.ts | sed -n '1,135p'
printf '%s\n' '--- wrapper generation source ---'
rg -n -C 12 --glob '*.ts' \
'getBashShellReadyRcfileContent|getBash.*Rcfile|relay bash overlay|Orca relay bash overlay wrapper|bash.*rcfile' \
src/relay src/main/providers src/main | head -n 350
printf '%s\n' '--- exact shell behavior: nounset and path comparison ---'
bash --noprofile --norc -c '
set -u
unset ZDOTDIR HISTFILE ORCA_HISTFILE
if [[ -n "${ORCA_HISTFILE:-}" ]]; then
HISTFILE="$ORCA_HISTFILE"
elif [[ "${HISTFILE:-}" == "$ZDOTDIR/.zsh_history" ]]; then
HISTFILE="${ORCA_ORIG_ZDOTDIR:-$HOME}/.zsh_history"
fi
printf "completed-without-error\n"
' 2>&1 || printf 'status=%s\n' "$?"
bash --noprofile --norc -c '
set +u
unset ORCA_HISTFILE
ZDOTDIR=/tmp/orca-wrapper
HISTFILE=/tmp/orca-wrapper/.zsh_history
if [[ -n "${ORCA_HISTFILE:-}" ]]; then
HISTFILE="$ORCA_HISTFILE"
elif [[ "${HISTFILE:-}" == "$ZDOTDIR/.zsh_history" ]]; then
HISTFILE="${ORCA_ORIG_ZDOTDIR:-$HOME}/.zsh_history"
fi
printf "matched-path=%s\n" "$HISTFILE"
'Repository: stablyai/orca
Length of output: 40494
🏁 Script executed:
#!/bin/bash
set -eu
printf '%s\n' '--- Bash rcfile generator ---'
cat -n src/main/providers/local-pty-shell-ready-bash-rcfile.ts | sed -n '1,115p'
printf '%s\n' '--- relay overlay wrapper imports and Bash content path ---'
cat -n src/relay/pty-shell-overlay-wrappers.ts | sed -n '1,90p'
rg -n -C 8 --glob '*.ts' \
'ensureOverlayRestoreWrappers|getBashShellReadyRcfileContent|bash/rcfile|relay-bash-rcfile' \
src/relay src/main/providers | head -n 250Repository: stablyai/orca
Length of output: 36085
🏁 Script executed:
#!/bin/bash
set -eu
printf '%s\n' '--- shared history block definition and uses ---'
rg -n -C 12 --glob '*.ts' \
'ZSH_HISTFILE_RESTORE_BLOCK|zsh_history|HISTFILE.*ZDOTDIR|ZDOTDIR.*zsh_history' \
src/main src/relay | head -n 300
printf '%s\n' '--- relay Bash history block context ---'
cat -n src/relay/pty-shell-overlay-wrappers.ts | sed -n '45,82p'Repository: stablyai/orca
Length of output: 24704
Remove the zsh-only history block from the relay Bash wrapper.
When a user profile enables set -u and leaves ZDOTDIR unset, ${ZSH_HISTFILE_RESTORE_BLOCK} aborts Bash startup while evaluating ZDOTDIR. Remove this block from src/relay/pty-shell-overlay-wrappers.ts and regenerate the snapshot. Preserve it in the relay zsh wrapper.
…der (stablyai#15245) * test(shell): pin every generated shell wrapper file with byte snapshots Captures .zshenv/.zprofile/.zshrc/.zlogin, the bash rcfile, and the fish init command for all three transports (local PTY, daemon/SSH, relay) so the upcoming wrapper unification can be proven byte-for-byte identical. * refactor(shell): build every zsh startup wrapper from one shared builder Local PTY, daemon/SSH, and relay each had their own copy of the zsh ZDOTDIR wrapper templates, and the copies had drifted. buildZshStartupWrapperFiles now produces .zshenv/.zprofile/.zshrc/.zlogin for all three, with every real difference expressed as a field on ZshStartupWrapperSpec. No behavior change: the generated text is byte-for-byte identical for every configuration, pinned by the snapshots committed in the previous commit (captured from the pre-refactor generators).
…der (stablyai#15245) * test(shell): pin every generated shell wrapper file with byte snapshots Captures .zshenv/.zprofile/.zshrc/.zlogin, the bash rcfile, and the fish init command for all three transports (local PTY, daemon/SSH, relay) so the upcoming wrapper unification can be proven byte-for-byte identical. * refactor(shell): build every zsh startup wrapper from one shared builder Local PTY, daemon/SSH, and relay each had their own copy of the zsh ZDOTDIR wrapper templates, and the copies had drifted. buildZshStartupWrapperFiles now produces .zshenv/.zprofile/.zshrc/.zlogin for all three, with every real difference expressed as a field on ZshStartupWrapperSpec. No behavior change: the generated text is byte-for-byte identical for every configuration, pinned by the snapshots committed in the previous commit (captured from the pre-refactor generators).
…der (stablyai#15245) * test(shell): pin every generated shell wrapper file with byte snapshots Captures .zshenv/.zprofile/.zshrc/.zlogin, the bash rcfile, and the fish init command for all three transports (local PTY, daemon/SSH, relay) so the upcoming wrapper unification can be proven byte-for-byte identical. * refactor(shell): build every zsh startup wrapper from one shared builder Local PTY, daemon/SSH, and relay each had their own copy of the zsh ZDOTDIR wrapper templates, and the copies had drifted. buildZshStartupWrapperFiles now produces .zshenv/.zprofile/.zshrc/.zlogin for all three, with every real difference expressed as a field on ZshStartupWrapperSpec. No behavior change: the generated text is byte-for-byte identical for every configuration, pinned by the snapshots committed in the previous commit (captured from the pre-refactor generators).
ELI5
Orca writes little zsh startup files so a shell it launches behaves normally and tells Orca when it's ready. There were three near-identical copies of that code (local terminals, the daemon/SSH path, and the relay), and they had quietly drifted apart. This PR folds them into one builder. The files it writes out are exactly the same bytes as before — nothing about how your shell starts changes.
What Changed
This is a pure refactor. No behavior change.
src/main/zsh-startup-wrapper-builder.ts:buildZshStartupWrapperFiles(spec)returns the.zshenv/.zprofile/.zshrc/.zlogincontents, parameterised byZshStartupWrapperSpec.src/main/providers/local-pty-shell-ready-wrapper-generation.ts(local PTY)src/main/daemon/daemon-zsh-shell-ready-wrapper-spec.ts+src/main/daemon/shell-ready.ts(daemon / SSH host) — the olddaemon-zsh-shell-ready-rcfile.tsis renamed to the spec modulesrc/relay/pty-shell-overlay-wrappers.ts(relay overlay)src/main/shell-templates.ts:getZshEnvTemplatenow takes a full header label instead of a prefix, and the relay's.zshenvmoved in beside it asgetZshOverlayEnvTemplate.Not in this PR (deliberately): no feature-selection channel, no change to which panes get wrapped, no change to
ORCA_SHELL_COMMAND_MARKERSor any other gate, no fix for the stacked-ZDOTDIR issue.Why
A follow-up needs to gate individual wrapper features (history restore, OSC 133 command markers, the shell-ready marker, startup identity, overlay restores) independently and per pane. Doing that against three drifted generators is how the previous attempt (#15197) shipped a new regression in each of three review rounds — a fix landed in one transport and silently missed the other two. Unify first with zero behavior change, gate second. That also makes this half separately revertable from the risky half.
Byte-identity proof
The generated text is unchanged byte for byte for every configuration each of the three call sites can produce. This was proven, not asserted:
origin/main,src/main/shell-wrapper-generated-file-snapshot.test.tswas written and run. It drives the real entry points (ensureShellReadyWrappersAt, the daemon'sgetShellReadyLaunchConfigwithORCA_USER_DATA_PATHpointed at a temp dir, andensureOverlayRestoreWrappers), reads the files back off disk, and snapshots them. That produced 17 fixtures:.zshenv/.zprofile/.zshrc/.zlogin/bash/rcfilefor each of local, daemon and relay, plus the fish--init-commandtext for local and daemon. Only the temp wrapper root is normalized (<WRAPPER_ROOT>), because that is the sole path-dependent byte in the output.da44b375a89, whose tree isorigin/mainsource + the test. sha256 of every fixture was recorded at that point.c3f41e9edd4. The same test passes with the fixtures untouched — identical sha256 for all 17 files, no snapshot rewrite.origin/mainsource, and it passed against the already-committed fixtures. Sogit checkout da44b375a89 && npx vitest run --config config/vitest.config.ts src/main/shell-wrapper-generated-file-snapshot.test.tsreproduces the baseline independently.One mismatch surfaced during the refactor and was fixed by preserving the original (the CODEX_HOME comment exists only in local's
.zshrc, not its.zlogin) — no snapshot was ever accepted over a diff.This matters for remote wire compatibility too: the wrapper text runs on a remote host that may be a different version than the client generating it. Byte-identity makes mixed versions a non-issue here.
Divergences preserved as parameters (follow-ups, not fixed here)
Real, pre-existing differences between the three generators. Each is now an explicit field instead of a silent copy-paste difference, and each is left exactly as it was:
.zshenvderives the user's ZDOTDIR differently (zshenvStrategy). Local and daemon source the user's.zshenvand read back what it exported, with%xself-discovery for WSL and the non-ASCII-username workaround ([Bug]: WSL terminal — zsh shell-ready wrapper exports ZDOTDIR as a raw Windows path, so user's .zshrc never loads #8003). The relay trusts the inherited ZDOTDIR and republishes it asORCA_USER_ZDOTDIR, which is why the relay wrapper files also resolve_orca_homefrom a different expression (homeExpression). Reconciling these is a follow-up.restores,osc133CommandMarkers). Whether the relay should have the missing ones is a real question, and not this PR's..zshrcdoes not skip a user.zshrcwhose dir is the wrapper ZDOTDIR (skipUserZshrcWhenHomeIsWrapperDir), and the relay.zloginregisters the ready marker after the final ZDOTDIR restore where local/daemon register it before (readyMarkerOrder)..zshrc, grouped underlegacyFormattingand documented as "normalize in a follow-up": theMIMOCODE_HOMErestore lost its two-space indent, and a comment above theCODEX_HOMErestore exists only there. Both are shell-semantically inert, but byte-identity is the contract of this PR, so they are preserved rather than tidied.What the next PR will do
Add the per-pane feature-selection channel on top of this single builder: gate history restore, OSC 133 command markers, the shell-ready marker, startup identity and the overlay restores independently, per pane, across all three transports at once.
Linked Issue
N/A — refactor prerequisite for the wrapper feature-gating work, follow-up to the reverted #15197.
Visual Proof
N/A— no UI surface and no behavior change. The only output of this code is shell wrapper text, and it is proven byte-for-byte identical above; a screenshot would show nothing.Testing
Platform: macOS 26.3.1 (arm64), zsh 5.9, real Git and real shells.
npx vitest run --config config/vitest.config.ts src/main/shell-wrapper-generated-file-snapshot.test.ts— 4 passed, 17 snapshots matched with no rewrite.npx vitest run --config config/vitest.config.ts src/main/zsh-scoped-histfile.live-shell.test.ts— 3 passed (real-shell test, executed against the system zsh 5.9 on macOS).npx vitest run --config config/vitest.config.tsoversrc/main/providers/,src/main/daemon/,src/relay/— 3637 passed. The 10 failures inheadless-emulator-fidelity.fuzz.test.ts,terminal-snapshot-color-parity.test.tsandterminal-snapshot-osc8-roundtrip.test.tsare pre-existing onorigin/main: verified by stashing this branch's source changes and re-running those three files, which fail identically without any of this change.local-pty-shell-ready-*(wrapper generation, zsh startup-file behavior, ZDOTDIR discovery, ZDOTDIR normalization, launch environment),daemon/shell-ready.test.ts,repro-13767-shell-ready-marker-lost-to-exec.test.ts,relay/pty-shell-launch.test.ts,relay/pty-handler-spawn-environment.test.ts— 143 passed.npx oxlinton all changed files — clean, nomax-linesdisable and no ratchet bump (the builder is a new file with its own responsibility; the daemon's zsh module was renamed to hold its spec).npx tsc --noEmit -p config/tsconfig.node.json— clean.npx oxfmt --check— clean.Cross-platform: generation stays POSIX-gated exactly as before (both
ensureShellReadyWrapperswin32 early-returns and the relay's win32 branch are untouched), so Windows behavior is unchanged; the snapshot suite skips on win32 for that reason and because native path separators would make fixtures unstable. Local, daemon/SSH and relay transports are each covered by their own fixtures.AI Disclosure
Review
Suggested reading order: the snapshot test and fixtures first (
da44b375a89), thenzsh-startup-wrapper-builder.ts, then the three specs. The interesting part of the review is whether the spec fields describe the real differences honestly — the fixtures already guarantee the bytes.Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
Security: no new inputs; the only interpolated value is the wrapper dir, still single-quote escaped by the same
quotePosixSingle. Cross-platform: unchanged win32 gating. Remote SSH: relay and daemon wrapper text byte-identical, so old hosts and new clients (and the reverse) behave exactly as today. Mobile: untouched. Performance: same string building, done once per wrapper write. Backwards compatibility: wrapper files on disk are rewritten only when their content differs (relay) or when missing (local/daemon), and the content does not differ.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)