Skip to content

fix(wsl): read machine output from a fenced login shell - #15290

Merged
OrcaWin merged 6 commits into
mainfrom
OrcaWin/wsl-clean-machine-read-output
Aug 18, 2026
Merged

OrcaWin merged 6 commits into
mainfrom
OrcaWin/wsl-clean-machine-read-output

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​246 $\color{#cf222e}{\Huge{\mathbf{−}}}$​21 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​225
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​209 $\color{#cf222e}{\Huge{\mathbf{−}}}$​36 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​173

Stack

Merge bottom-up; each PR is based on the one above it.

# PR Base
1 #15039 main fix(wsl): pass guest argv verbatim through --exec — merged
2 ▶ this main fix(wsl): read machine output from a fenced login shell ← you are here
3 #15060 ↑ fix(git): fence buffered WSL login-shell reads
4 #15257 ↑ fix(git): run WSL git reads without a shell

Replaces #15040, which GitHub auto-closed when its base branch was deleted on merge of #15039. Same head branch and same commits.

ELI5

To find the user's tools, Orca runs WSL commands through the interactive login shell — the same one you get in a terminal — because that's the only shell that reads ~/.bashrc, where nvm/mise/asdf put things on PATH.

The catch: an interactive shell also prints the welcome banner. On a stock Ubuntu 24.04, with nothing customized, that's:

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

That text goes to stdout — the same pipe Orca reads answers from. So when Orca asked "is this a file or a directory?", it got back the banner and then the answer, and the code compared the whole blob to "directory". It never matched.

This PR puts a marker around the real answer so Orca can find it, while keeping the PATH benefit.

Root cause

Reproduced on a stock distro, no user config:

$ wsl.exe -d Ubuntu-24.04 --exec bash -ilc 'git --version'
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

git version 2.43.0

buildWslLoginShellCommand selects -ilc for bash/zsh (correctly — -lc alone misses ~/.bashrc). Running the actual shipped code paths against a real distro:

caller returned today
local-worktree-filesystem.statPath "To run a command as administrator…\n\ndirectory"
local-worktree-filesystem.readPath banner prepended to the file contents it reads
runPreflightCommandInWsl banner prepended to gh --version / auth output
codex-trust-grant-host binary stamp path = the banner text (see below)

statPath does stdout.trim(), which cannot recover this, so the type never equals file/directory. These two back the worktree-removal orphan-gitdir proof (getLocalWorktreePathAccess), so the proof reads a banner instead of a .git file — they are not the file-explorer listing path.

That three call sites had each grown a different private workaround is the tell that this belongs in one place:

  • preflight-wsl-agent-detection.ts — __ORCA_AGENT_PATH__ line prefix
  • wsl-git-read-environment.ts — ORCA_WSL_GIT_READ_ENV_V1 + lastIndexOf
  • rate-limits/codex-fetcher.ts — an fd dance commented "before shell startup can … print banners"

The ones that lacked a workaround are exactly the broken ones.

The silent one

codex-trust-grant-host.buildWslCodexBinaryStamp reads the identity probe positionally — path before the first newline, version after. With the banner ahead of it the first newline lands inside the banner, so the stamp becomes path = "To run a command as administrator…" with the rest as version. Both halves are non-empty, so nothing throws. The stamp exists to detect "the Codex binary changed", so a garbage stamp reads as a changed binary and reissues the trust grant. This is the only case where the corruption is silent rather than a loud failure.

What changed

buildWslCapturedLoginShellCommand(command) returns the script plus a matching readStdout. The payload is fenced:

printf %s '<BEGIN>'
<command>
_orca_capture_status=$?
printf %s '<END>'
exit $_orca_capture_status
  • rc/motd lands before <BEGIN> and is dropped; ~/.bash_logout noise lands after <END>.
  • Exit status is preserved, so statPath's exit 2 → ENOENT mapping still works.
  • The fence carries a per-call nonce. A fixed marker would truncate cat of any file that happens to quote it — an early build did exactly that, which is why the nonce exists.
  • A payload that execs (the Codex identity probe) never writes a closing fence; the reader returns everything after the opening one.

wsl-git-read-environment drops its bespoke marker and parsing. buildWslCodexIdentityArgs becomes buildWslCodexIdentityProbe and returns the reader alongside the argv so the two cannot drift apart.

Deliberately not fenced: paths that exec into a long-running program (codex app-server, codex login, terminal shells) — stdout belongs to that program there; and availability checks that are exit-code only. The git runner's login-shell mode also feeds streaming consumers, and git already has a shell-free --exec /usr/bin/env … git fast path that is immune by construction.

Testing

New real-WSL E2E (local-worktree-filesystem-wsl-banner.wsl.test.ts, gated behind ORCA_REAL_WSL_BANNER_TEST=1, following the existing .wsl.test.ts convention). Against a live Ubuntu-24.04:

  • 5/5 pass with the fix.
  • 4/5 fail with fence-stripping disabled, with exactly the reported symptom:
    AssertionError: expected 'To run a command as administrator (us…' to be 'directory'
    

It deliberately does not modify the developer's ~/.bashrc; it compares the fenced read against a raw login-shell read, so it is meaningful on both chatty and silent distros.

The trust-grant fix is mutation-checked the same way — reverting it reproduces path: "To run a command as administrator…".

Unit tests cover banner stripping, exit-status preservation, payload-contains-a-fence, missing fence, early exit, and fence uniqueness.

This PR also fixes two tests that are currently red on main. wsl-login-shell-command.test.ts and posix-command-path-lookup.test.ts both assert a raw interactive login-shell read matches an expected value; on any machine with a stock Ubuntu they get the banner instead. The second sits in the shell contracts CI gate but skips on Linux, which is why the breakage stayed invisible.

Known remaining (follow-up, not this PR)

Five call sites hand-write bash -lc / sh -lc directly, bypassing both the shell detection and the fence — chiefly the Claude/Codex managed-auth path probes and the WSL CLI installer, which parse a path out of stdout and feed it onward as trusted.

I measured this tier rather than assuming: on stock Ubuntu-24.04 -lc is clean — the banner is -i-gated. But that cleanliness is coincidental (01-locale-fix.sh happens to sort before Z99-cloud-locale-test.sh, which has no interactive guard and writes to stdout), and login shells read ~/.bash_profile/~/.profile unconditionally — so an nvm/conda/direnv installer line or a corporate golden-image banner contaminates -lc exactly as much as -ilc:

$ env HOME=$tmp wsl.exe -d Ubuntu-24.04 --exec env HOME=$tmp bash -lc 'printf READY'
Welcome to Contoso Corp WSL image v3.2
READY

That is a real latent risk on ordinary developer machines, but it is a different tier from what this PR proves, and those sites share a common helper (buildEncodedWslBashCommand) that makes them a clean separate change. Left as a follow-up rather than widening this diff.

Orca runs WSL reads through the distro's *interactive* login shell so
PATH matches the user's own terminal (nvm, mise and asdf only install
into rc files interactive shells read). An interactive shell also runs
the distro's rc/motd, and stock Ubuntu 24.04 writes its "run a command
as administrator" hint to stdout -- no user customization required.

Every caller parsing that stream was reading the banner as data:

  statPath  -> "To run a command as administrator...\n\ndirectory"
  readPath  -> banner prepended to the contents of every file read
  preflight -> banner prepended to `gh --version` / auth output

`.trim()` cannot recover any of these, so a WSL worktree's file
explorer sees no valid entry types and file reads return junk.

Three call sites had independently grown their own marker to survive
this (`__ORCA_AGENT_PATH__`, `ORCA_WSL_GIT_READ_ENV_V1`, and a
`>/dev/null` fd dance), which is the tell that it belongs in one place.

Fence the payload once, in the shared builder, and hand callers a
reader that returns just their bytes. The fence carries a per-call
nonce so `cat`-ing a file that happens to quote a marker is not
truncated. Exit status is preserved, so the ENOENT mapping still works.

wsl-git-read-environment drops its bespoke marker and parsing.
It asserted a raw interactive login-shell read matched an absolute path,
so the distro rc banner made it fail on any stock Ubuntu. It is part of
the shell-contracts CI gate, where it skips on Linux and hid the break.
Both failure modes are silent - the command runs, exits 0, and returns
the wrong bytes - so the rules need to live somewhere a reader will
find them before writing the next wsl.exe call site.
buildWslCodexBinaryStamp reads the login shell's stdout positionally --
path before the first newline, version after -- through an interactive
login shell. On a stock Ubuntu the rc banner lands ahead of the payload,
so the first newline falls inside the banner and the stamp becomes
path="To run a command as administrator..." with the rest as version.
Both halves are non-empty, so nothing throws: the stamp is silently
wrong, and an unstable stamp reads as "the Codex binary changed" and
reissues the trust grant.

The identity script ends in `exec`, so it never writes a closing fence;
the reader returns everything after the opening one, which is exactly
this case.

buildWslCodexIdentityArgs becomes buildWslCodexIdentityProbe and returns
the reader with the argv so the two cannot drift apart. The other three
WSL Codex commands are deliberately left unfenced: availability is
exit-code only, and app-server/login hand stdout to a long-running
program.
- readStdout now takes the LAST opening fence, matching the lastIndexOf
  the wsl-git-read-environment marker used deliberately: a login shell
  can echo the command text before running it, repeating the fence.
- local-worktree-filesystem throws instead of falling back to raw stdout
  when the fence is missing. The fallback silently reinstated the bug
  being fixed -- statPath would return the banner as a file type and
  readPath would return banner+contents, with no signal. Preflight keeps
  its fallback; its matchers scan the whole blob and tolerate a prefix.
- The exit-status test asserted only that the script CONTAINS `exit $?`,
  which is true for any input and never executed those lines. It now
  runs a real distro and asserts status 2 reaches the caller, which is
  what statPath's ENOENT mapping depends on.
- Corrected the doc: a sed backreference has no `$`, so `--` never
  rewrote it. Replaced with the positional and shell-local cases that
  were measured to differ.
statPath/readPath/rm run coreutils at standard paths and shell builtins.
They need nothing from the user's PATH, so there was never a reason to
start a login shell -- and starting one is what put the distro's rc/motd
on the stdout these callers parse.

Fencing that output treated the symptom. Using a plain `sh -c` removes
the cause: no profile, no rc, no banner, by construction. The fence and
its missing-fence error go away with it.

The fence stays where it is actually needed: the three places that must
run the user's shell to resolve their PATH (the preflight CLI probe, the
WSL git environment probe, and the Codex identity probe).

Net -12 lines.
@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Added nonce-fenced stdout capture for WSL login-shell commands with exit-status preservation. Updated Codex, Git, preflight, and path probes to parse captured output. Changed WSL filesystem operations to use non-login sh -c execution. Added tests for banners, fences, probe parsing, filesystem behavior, and missing paths. Added WSL execution documentation and repository guidance.

Merge Risk: 🟡 Moderate · up to 6f46f

The change improves WSL machine-output parsing, but the current implementation can still produce incorrect command results when the opening fence is missing or when payload output contains an earlier closing marker; the guidance update may also become inconsistent because its source was not regenerated. These concrete merge-readiness issues should be fixed or explicitly accepted before merging.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, implementation, testing, and scope, but it omits required Linked Issue and Visual Proof sections and most checklist sections. Add the required template sections, including a linked issue, Visual Proof with N/A and a reason if applicable, checklist status, and cross-platform review details.
Docstring Coverage ⚠️ Warning Docstring coverage is 29.41% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main WSL login-shell output-fencing change.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (1)
docs/reference/wsl-command-execution.md (1)

12-17: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add language identifiers to the fenced examples.

markdownlint reports MD040 at Lines 12, 41, and 66. Use console for transcript blocks and sh for the executable command block.

Also applies to: 41-47, 66-68

Source: Linters/SAST tools


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b02f29e4-22ca-4fa2-a01d-8b575fddbebb

📥 Commits

Reviewing files that changed from the base of the PR and between 6e8da1d and 6f46fe5.

📒 Files selected for processing (16)
  • .gitignore
  • AGENTS.md
  • docs/reference/wsl-command-execution.md
  • src/main/codex-accounts/wsl-codex-command.test.ts
  • src/main/codex-accounts/wsl-codex-command.ts
  • src/main/codex/codex-trust-grant-host.test.ts
  • src/main/codex/codex-trust-grant-host.ts
  • src/main/git/runner-wsl-direct-read.test.ts
  • src/main/git/wsl-git-read-environment.ts
  • src/main/ipc/preflight-wsl-command.ts
  • src/main/local-worktree-filesystem-wsl-banner.wsl.test.ts
  • src/main/local-worktree-filesystem.test.ts
  • src/main/local-worktree-filesystem.ts
  • src/shared/posix-command-path-lookup.test.ts
  • src/shared/wsl-login-shell-command.test.ts
  • src/shared/wsl-login-shell-command.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.

Comment thread AGENTS.md
- **Shortcut labels in UI**: Display `⌘` / `⇧` on Mac and `Ctrl+` / `Shift+` on other platforms.
- **File paths**: Use `path.join` or Electron/Node path utilities — never assume `/` or `\`.
- **Windows setup scripts**: the setup/issue-command runner is a `.cmd` batch file unless the script starts with a `#!` line — never derive that from the user's terminal-shell preference, and never launch a `.cmd` runner with a bare `cmd.exe /c` from a Git Bash pane (MSYS rewrites the `/c`). See [`docs/reference/windows-setup-shell.md`](./docs/reference/windows-setup-shell.md).
- **WSL commands**: build argv with `buildWslExecArgs` (always `--exec` — under `--`, `wsl.exe` expands `$name` in every argument and silently rewrites the script), and fence anything whose stdout you parse with `buildWslCapturedLoginShellCommand`, because the interactive login shell prints the distro banner to stdout. See [`docs/reference/wsl-command-execution.md`](./docs/reference/wsl-command-execution.md).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Regenerate the synchronized guidance from its source.

Add this WSL rule to vibe-kit/ai-doc/references/agents-core.md, then run node vibe-kit/ai-doc/scripts/sync-agents-core.cjs. A direct edit to AGENTS.md can be lost or leave CLAUDE.md inconsistent.

Based on learnings: synchronized guidance blocks must be changed in the source template and then regenerated.

Source: Learnings

Comment on lines +33 to +35
Orca runs guest commands through the distro user's **interactive** login shell (`-ilc` for
bash/zsh) because that is the only shell that reads `~/.bashrc`, where `nvm`, `mise` and `asdf`
install their PATH entries. Dropping `-i` would break tool detection for those users.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Document the non-login sh -c path.

The filesystem implementation uses plain sh -c for statPath, readPath, and rm; PATH-dependent probes use captured login shells. These lines make fencing sound unconditional and document only direct binary execution. State the three execution cases separately.

The PR objective explicitly defines this filesystem split.

Also applies to: 49-51, 61-72

Comment on lines +29 to +30
const payload = captured.readStdout(result.stdout)
return payload === null ? result : { ...result, stdout: payload }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Do not return raw stdout when the opening fence is absent.

When readStdout returns null, return empty stdout or reject the result. Raw rc output can match preflight patterns as CLI output. Git and Codex fail closed for the same condition.

Proposed fix
-  return payload === null ? result : { ...result, stdout: payload }
+  return { ...result, stdout: payload ?? '' }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const payload = captured.readStdout(result.stdout)
return payload === null ? result : { ...result, stdout: payload }
const payload = captured.readStdout(result.stdout)
return { ...result, stdout: payload ?? '' }
🧰 Tools
🪛 ast-grep (0.45.1)

[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import { execFile } from 'node:child_process'
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').

(detect-child-process-typescript)

Comment on lines +92 to +95
const endIndex = stdout.indexOf(end, payloadStart)
// A payload that exited early never prints the closing fence; the rest of
// the stream is still its output, and login shells run exit hooks after it.
return endIndex === -1 ? stdout.slice(payloadStart) : stdout.slice(payloadStart, endIndex)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Use the final closing fence.

indexOf can select a closing marker emitted by stdout tracing before the real fence. This returns trace text as payload and corrupts machine output. Use the last closing marker after payloadStart, and add a tracing fixture.

Proposed fix
-      const endIndex = stdout.indexOf(end, payloadStart)
+      const endIndex = stdout.lastIndexOf(end)
       // A payload that exited early never prints the closing fence; the rest of
       // the stream is still its output, and login shells run exit hooks after it.
-      return endIndex === -1 ? stdout.slice(payloadStart) : stdout.slice(payloadStart, endIndex)
+      return endIndex < payloadStart
+        ? stdout.slice(payloadStart)
+        : stdout.slice(payloadStart, endIndex)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const endIndex = stdout.indexOf(end, payloadStart)
// A payload that exited early never prints the closing fence; the rest of
// the stream is still its output, and login shells run exit hooks after it.
return endIndex === -1 ? stdout.slice(payloadStart) : stdout.slice(payloadStart, endIndex)
const endIndex = stdout.lastIndexOf(end)
// A payload that exited early never prints the closing fence; the rest of
// the stream is still its output, and login shells run exit hooks after it.
return endIndex < payloadStart
? stdout.slice(payloadStart)
: stdout.slice(payloadStart, endIndex)

@OrcaWin
OrcaWin merged commit 3a9f40e into main Aug 18, 2026
47 checks passed
@OrcaWin
OrcaWin deleted the OrcaWin/wsl-clean-machine-read-output branch August 18, 2026 09:54
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Aug 21, 2026
* fix(wsl): read machine output from a fenced login shell

Orca runs WSL reads through the distro's *interactive* login shell so
PATH matches the user's own terminal (nvm, mise and asdf only install
into rc files interactive shells read). An interactive shell also runs
the distro's rc/motd, and stock Ubuntu 24.04 writes its "run a command
as administrator" hint to stdout -- no user customization required.

Every caller parsing that stream was reading the banner as data:

  statPath  -> "To run a command as administrator...\n\ndirectory"
  readPath  -> banner prepended to the contents of every file read
  preflight -> banner prepended to `gh --version` / auth output

`.trim()` cannot recover any of these, so a WSL worktree's file
explorer sees no valid entry types and file reads return junk.

Three call sites had independently grown their own marker to survive
this (`__ORCA_AGENT_PATH__`, `ORCA_WSL_GIT_READ_ENV_V1`, and a
`>/dev/null` fd dance), which is the tell that it belongs in one place.

Fence the payload once, in the shared builder, and hand callers a
reader that returns just their bytes. The fence carries a per-call
nonce so `cat`-ing a file that happens to quote a marker is not
truncated. Exit status is preserved, so the ENOENT mapping still works.

wsl-git-read-environment drops its bespoke marker and parsing.

* test(wsl): fence the login-shell path-lookup boundary test

It asserted a raw interactive login-shell read matched an absolute path,
so the distro rc banner made it fail on any stock Ubuntu. It is part of
the shell-contracts CI gate, where it skips on Linux and hid the break.

* docs(wsl): record the guest command-execution contract

Both failure modes are silent - the command runs, exits 0, and returns
the wrong bytes - so the rules need to live somewhere a reader will
find them before writing the next wsl.exe call site.

* fix(codex): fence the WSL Codex identity probe

buildWslCodexBinaryStamp reads the login shell's stdout positionally --
path before the first newline, version after -- through an interactive
login shell. On a stock Ubuntu the rc banner lands ahead of the payload,
so the first newline falls inside the banner and the stamp becomes
path="To run a command as administrator..." with the rest as version.
Both halves are non-empty, so nothing throws: the stamp is silently
wrong, and an unstable stamp reads as "the Codex binary changed" and
reissues the trust grant.

The identity script ends in `exec`, so it never writes a closing fence;
the reader returns everything after the opening one, which is exactly
this case.

buildWslCodexIdentityArgs becomes buildWslCodexIdentityProbe and returns
the reader with the argv so the two cannot drift apart. The other three
WSL Codex commands are deliberately left unfenced: availability is
exit-code only, and app-server/login hand stdout to a long-running
program.

* fix(wsl): harden the capture fence after review

- readStdout now takes the LAST opening fence, matching the lastIndexOf
  the wsl-git-read-environment marker used deliberately: a login shell
  can echo the command text before running it, repeating the fence.
- local-worktree-filesystem throws instead of falling back to raw stdout
  when the fence is missing. The fallback silently reinstated the bug
  being fixed -- statPath would return the banner as a file type and
  readPath would return banner+contents, with no signal. Preflight keeps
  its fallback; its matchers scan the whole blob and tolerate a prefix.
- The exit-status test asserted only that the script CONTAINS `exit $?`,
  which is true for any input and never executed those lines. It now
  runs a real distro and asserts status 2 reaches the caller, which is
  what statPath's ENOENT mapping depends on.
- Corrected the doc: a sed backreference has no `$`, so `--` never
  rewrote it. Replaced with the positional and shell-local cases that
  were measured to differ.

* fix(wsl): stop running a login shell for filesystem reads

statPath/readPath/rm run coreutils at standard paths and shell builtins.
They need nothing from the user's PATH, so there was never a reason to
start a login shell -- and starting one is what put the distro's rc/motd
on the stdout these callers parse.

Fencing that output treated the symptom. Using a plain `sh -c` removes
the cause: no profile, no rc, no banner, by construction. The fence and
its missing-fence error go away with it.

The fence stays where it is actually needed: the three places that must
run the user's shell to resolve their PATH (the preflight CLI probe, the
WSL git environment probe, and the Codex identity probe).

Net -12 lines.

---------

Co-authored-by: OrcaWin <[email protected]>
dallascrilley pushed a commit to dallascrilley/orca that referenced this pull request Aug 27, 2026
* fix(wsl): read machine output from a fenced login shell

Orca runs WSL reads through the distro's *interactive* login shell so
PATH matches the user's own terminal (nvm, mise and asdf only install
into rc files interactive shells read). An interactive shell also runs
the distro's rc/motd, and stock Ubuntu 24.04 writes its "run a command
as administrator" hint to stdout -- no user customization required.

Every caller parsing that stream was reading the banner as data:

  statPath  -> "To run a command as administrator...\n\ndirectory"
  readPath  -> banner prepended to the contents of every file read
  preflight -> banner prepended to `gh --version` / auth output

`.trim()` cannot recover any of these, so a WSL worktree's file
explorer sees no valid entry types and file reads return junk.

Three call sites had independently grown their own marker to survive
this (`__ORCA_AGENT_PATH__`, `ORCA_WSL_GIT_READ_ENV_V1`, and a
`>/dev/null` fd dance), which is the tell that it belongs in one place.

Fence the payload once, in the shared builder, and hand callers a
reader that returns just their bytes. The fence carries a per-call
nonce so `cat`-ing a file that happens to quote a marker is not
truncated. Exit status is preserved, so the ENOENT mapping still works.

wsl-git-read-environment drops its bespoke marker and parsing.

* test(wsl): fence the login-shell path-lookup boundary test

It asserted a raw interactive login-shell read matched an absolute path,
so the distro rc banner made it fail on any stock Ubuntu. It is part of
the shell-contracts CI gate, where it skips on Linux and hid the break.

* docs(wsl): record the guest command-execution contract

Both failure modes are silent - the command runs, exits 0, and returns
the wrong bytes - so the rules need to live somewhere a reader will
find them before writing the next wsl.exe call site.

* fix(codex): fence the WSL Codex identity probe

buildWslCodexBinaryStamp reads the login shell's stdout positionally --
path before the first newline, version after -- through an interactive
login shell. On a stock Ubuntu the rc banner lands ahead of the payload,
so the first newline falls inside the banner and the stamp becomes
path="To run a command as administrator..." with the rest as version.
Both halves are non-empty, so nothing throws: the stamp is silently
wrong, and an unstable stamp reads as "the Codex binary changed" and
reissues the trust grant.

The identity script ends in `exec`, so it never writes a closing fence;
the reader returns everything after the opening one, which is exactly
this case.

buildWslCodexIdentityArgs becomes buildWslCodexIdentityProbe and returns
the reader with the argv so the two cannot drift apart. The other three
WSL Codex commands are deliberately left unfenced: availability is
exit-code only, and app-server/login hand stdout to a long-running
program.

* fix(wsl): harden the capture fence after review

- readStdout now takes the LAST opening fence, matching the lastIndexOf
  the wsl-git-read-environment marker used deliberately: a login shell
  can echo the command text before running it, repeating the fence.
- local-worktree-filesystem throws instead of falling back to raw stdout
  when the fence is missing. The fallback silently reinstated the bug
  being fixed -- statPath would return the banner as a file type and
  readPath would return banner+contents, with no signal. Preflight keeps
  its fallback; its matchers scan the whole blob and tolerate a prefix.
- The exit-status test asserted only that the script CONTAINS `exit $?`,
  which is true for any input and never executed those lines. It now
  runs a real distro and asserts status 2 reaches the caller, which is
  what statPath's ENOENT mapping depends on.
- Corrected the doc: a sed backreference has no `$`, so `--` never
  rewrote it. Replaced with the positional and shell-local cases that
  were measured to differ.

* fix(wsl): stop running a login shell for filesystem reads

statPath/readPath/rm run coreutils at standard paths and shell builtins.
They need nothing from the user's PATH, so there was never a reason to
start a login shell -- and starting one is what put the distro's rc/motd
on the stdout these callers parse.

Fencing that output treated the symptom. Using a plain `sh -c` removes
the cause: no profile, no rc, no banner, by construction. The fence and
its missing-fence error go away with it.

The fence stays where it is actually needed: the three places that must
run the user's shell to resolve their PATH (the preflight CLI probe, the
WSL git environment probe, and the Codex identity probe).

Net -12 lines.

---------

Co-authored-by: OrcaWin <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* fix(wsl): read machine output from a fenced login shell

Orca runs WSL reads through the distro's *interactive* login shell so
PATH matches the user's own terminal (nvm, mise and asdf only install
into rc files interactive shells read). An interactive shell also runs
the distro's rc/motd, and stock Ubuntu 24.04 writes its "run a command
as administrator" hint to stdout -- no user customization required.

Every caller parsing that stream was reading the banner as data:

  statPath  -> "To run a command as administrator...\n\ndirectory"
  readPath  -> banner prepended to the contents of every file read
  preflight -> banner prepended to `gh --version` / auth output

`.trim()` cannot recover any of these, so a WSL worktree's file
explorer sees no valid entry types and file reads return junk.

Three call sites had independently grown their own marker to survive
this (`__ORCA_AGENT_PATH__`, `ORCA_WSL_GIT_READ_ENV_V1`, and a
`>/dev/null` fd dance), which is the tell that it belongs in one place.

Fence the payload once, in the shared builder, and hand callers a
reader that returns just their bytes. The fence carries a per-call
nonce so `cat`-ing a file that happens to quote a marker is not
truncated. Exit status is preserved, so the ENOENT mapping still works.

wsl-git-read-environment drops its bespoke marker and parsing.

* test(wsl): fence the login-shell path-lookup boundary test

It asserted a raw interactive login-shell read matched an absolute path,
so the distro rc banner made it fail on any stock Ubuntu. It is part of
the shell-contracts CI gate, where it skips on Linux and hid the break.

* docs(wsl): record the guest command-execution contract

Both failure modes are silent - the command runs, exits 0, and returns
the wrong bytes - so the rules need to live somewhere a reader will
find them before writing the next wsl.exe call site.

* fix(codex): fence the WSL Codex identity probe

buildWslCodexBinaryStamp reads the login shell's stdout positionally --
path before the first newline, version after -- through an interactive
login shell. On a stock Ubuntu the rc banner lands ahead of the payload,
so the first newline falls inside the banner and the stamp becomes
path="To run a command as administrator..." with the rest as version.
Both halves are non-empty, so nothing throws: the stamp is silently
wrong, and an unstable stamp reads as "the Codex binary changed" and
reissues the trust grant.

The identity script ends in `exec`, so it never writes a closing fence;
the reader returns everything after the opening one, which is exactly
this case.

buildWslCodexIdentityArgs becomes buildWslCodexIdentityProbe and returns
the reader with the argv so the two cannot drift apart. The other three
WSL Codex commands are deliberately left unfenced: availability is
exit-code only, and app-server/login hand stdout to a long-running
program.

* fix(wsl): harden the capture fence after review

- readStdout now takes the LAST opening fence, matching the lastIndexOf
  the wsl-git-read-environment marker used deliberately: a login shell
  can echo the command text before running it, repeating the fence.
- local-worktree-filesystem throws instead of falling back to raw stdout
  when the fence is missing. The fallback silently reinstated the bug
  being fixed -- statPath would return the banner as a file type and
  readPath would return banner+contents, with no signal. Preflight keeps
  its fallback; its matchers scan the whole blob and tolerate a prefix.
- The exit-status test asserted only that the script CONTAINS `exit $?`,
  which is true for any input and never executed those lines. It now
  runs a real distro and asserts status 2 reaches the caller, which is
  what statPath's ENOENT mapping depends on.
- Corrected the doc: a sed backreference has no `$`, so `--` never
  rewrote it. Replaced with the positional and shell-local cases that
  were measured to differ.

* fix(wsl): stop running a login shell for filesystem reads

statPath/readPath/rm run coreutils at standard paths and shell builtins.
They need nothing from the user's PATH, so there was never a reason to
start a login shell -- and starting one is what put the distro's rc/motd
on the stdout these callers parse.

Fencing that output treated the symptom. Using a plain `sh -c` removes
the cause: no profile, no rc, no banner, by construction. The fence and
its missing-fence error go away with it.

The fence stays where it is actually needed: the three places that must
run the user's shell to resolve their PATH (the preflight CLI probe, the
WSL git environment probe, and the Codex identity probe).

Net -12 lines.

---------

Co-authored-by: OrcaWin <[email protected]>
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.

1 participant