# psh in Process

nanocode embeds a modified psh shell to run agent commands and common utilities in-process. Standalone shell experiments and the shell shipped in the TypeScript SDK are different artifacts.

## Upstream Credit

[psh](https://github.com/earendil-works/psh) originates at Earendil. Its [initial research commit](https://github.com/earendil-works/psh/commit/c22b0732bb5b2f2e2dbb17cffb921400d71b976c) names Armin Ronacher as author. Credit belongs to Earendil and its contributors, not nanocode.

The [vendor commit](https://github.com/nanocodexyz/nanocode/commit/bd841c6d2bd535830cb3872017a525082012f982) imports [upstream `e5f136e`](https://github.com/earendil-works/psh/commit/e5f136e3122d05e811f4665120721900a3bf0392). The modified copy lives [inside nanocode](https://github.com/nanocodexyz/nanocode/tree/6ed92be87b3a2f704a7af857a2237b3fccafbb27/crates/patches/psh).

Earendil Inc.'s [MIT](https://github.com/nanocodexyz/nanocode/blob/6ed92be87b3a2f704a7af857a2237b3fccafbb27/crates/patches/psh/LICENSE-MIT) or [Apache](https://github.com/nanocodexyz/nanocode/blob/6ed92be87b3a2f704a7af857a2237b3fccafbb27/crates/patches/psh/LICENSE-APACHE) license choice remains. The [NOTICE](https://github.com/nanocodexyz/nanocode/blob/6ed92be87b3a2f704a7af857a2237b3fccafbb27/crates/patches/psh/NOTICE) preserves credits for adapted code, including brush, oh-my-pi, Rust, uutils, ripgrep, fd, and GoAWK.

## Measured Change

[Flat-loop compilation](https://github.com/nanocodexyz/nanocode/commit/a951e636dbad00e718d43b01127540422409e171) resolves integer variables into slots. The [next measured change](https://github.com/nanocodexyz/nanocode/commit/3aaede0fa7ed5f944cfc6a525a4c121368f036a0) runs native loop stretches synchronously, iterates brace ranges lazily, and statically links the standalone shell.

The [historical run](/experiments/evidence/fork-psh.json) compares [baseline `02b304a`](https://github.com/nanocodexyz/nanocode/commit/02b304a435916a35c49b09b501c42d11bd99cab1) with that next change. The [measurement record](https://github.com/nanocodexyz/nanocode/blob/3aaede0fa7ed5f944cfc6a525a4c121368f036a0/DECISIONS.md#L3) reports median user-plus-system CPU time over 21 runs on a loaded five-core host. These are historical results, not new reruns.

| Workload                  | Before CPU ms | After CPU ms | Evidence                                               |
| ------------------------- | ------------: | -----------: | ------------------------------------------------------ |
| Standalone startup        |          2.42 |         0.62 | [Run and commits](/experiments/evidence/fork-psh.json) |
| `while [ ]` counting      |          5.47 |         0.57 | [Run and commits](/experiments/evidence/fork-psh.json) |
| `while (( ))` counting    |          5.07 |         2.13 | [Run and commits](/experiments/evidence/fork-psh.json) |
| `sum += i` loop           |          6.43 |         3.00 | [Run and commits](/experiments/evidence/fork-psh.json) |
| Brace-range echo pipeline |          9.73 |         1.82 | [Run and commits](/experiments/evidence/fork-psh.json) |

Loop counts, signal defaults, checks, and the exact revisions are in the [run data](/experiments/evidence/fork-psh.json). No confidence interval or raw per-run timing distribution is available here.

## Checks and Limits

The [record](https://github.com/nanocodexyz/nanocode/blob/3aaede0fa7ed5f944cfc6a525a4c121368f036a0/DECISIONS.md#L3) includes spec-suite counts, differential output checks, and a known load-sensitive kill-case failure. The statically linked spec harness crashed. Spec checks therefore used dynamic linkage, not the standalone static build.

The measured startup missed its stated goal, and arithmetic loops and pipelines did not all meet their speedup goals. These results do not establish complete Bash compatibility or equivalent performance for arbitrary agent commands.

## What Remains

The [current TypeScript release](/experiments/evidence/fork-python.json) identifies the earlier shell revision, not the later standalone experiment. Static standalone linkage must not be attributed to that release's WebAssembly shell.

The remaining work is to qualify each shipped artifact separately, retain raw timing distributions, and reduce interpreter overhead without changing shell semantics. The [local history](https://github.com/nanocodexyz/nanocode/commits/6ed92be87b3a2f704a7af857a2237b3fccafbb27/crates/patches/psh) records those changes separately.
