|
| 1 | +# Bun shell migration plan |
| 2 | + |
| 3 | +Practical phased replacement of Bun `$` calls. |
| 4 | + |
| 5 | +## Goal |
| 6 | + |
| 7 | +Replace runtime Bun shell template-tag usage in `packages/opencode/src` with a unified `Process` API in `util/process.ts`. |
| 8 | + |
| 9 | +Keep behavior stable while improving safety, testability, and observability. |
| 10 | + |
| 11 | +Current baseline from audit: |
| 12 | + |
| 13 | +- 143 runtime command invocations across 17 files |
| 14 | +- 84 are git commands |
| 15 | +- Largest hotspots: |
| 16 | + - `src/cli/cmd/github.ts` (33) |
| 17 | + - `src/worktree/index.ts` (22) |
| 18 | + - `src/lsp/server.ts` (21) |
| 19 | + - `src/installation/index.ts` (20) |
| 20 | + - `src/snapshot/index.ts` (18) |
| 21 | + |
| 22 | +## Decisions |
| 23 | + |
| 24 | +- Extend `src/util/process.ts` (do not create a separate exec module). |
| 25 | +- Proceed with phased migration for both git and non-git paths. |
| 26 | +- Keep plugin `$` compatibility in 1.x and remove in 2.0. |
| 27 | + |
| 28 | +## Non-goals |
| 29 | + |
| 30 | +- Do not remove plugin `$` compatibility in this effort. |
| 31 | +- Do not redesign command semantics beyond what is needed to preserve behavior. |
| 32 | + |
| 33 | +## Constraints |
| 34 | + |
| 35 | +- Keep migration phased, not big-bang. |
| 36 | +- Minimize behavioral drift. |
| 37 | +- Keep these explicit shell-only exceptions: |
| 38 | + - `src/session/prompt.ts` raw command execution |
| 39 | + - worktree start scripts in `src/worktree/index.ts` |
| 40 | + |
| 41 | +## Process API proposal (`src/util/process.ts`) |
| 42 | + |
| 43 | +Add higher-level wrappers on top of current spawn support. |
| 44 | + |
| 45 | +Core methods: |
| 46 | + |
| 47 | +- `Process.run(cmd, opts)` |
| 48 | +- `Process.text(cmd, opts)` |
| 49 | +- `Process.lines(cmd, opts)` |
| 50 | +- `Process.status(cmd, opts)` |
| 51 | +- `Process.shell(command, opts)` for intentional shell execution |
| 52 | + |
| 53 | +Git helpers: |
| 54 | + |
| 55 | +- `Process.git(args, opts)` |
| 56 | +- `Process.gitText(args, opts)` |
| 57 | + |
| 58 | +Shared options: |
| 59 | + |
| 60 | +- `cwd`, `env`, `stdin`, `stdout`, `stderr`, `abort`, `timeout`, `kill` |
| 61 | +- `allowFailure` / non-throw mode |
| 62 | +- optional redaction + trace metadata |
| 63 | + |
| 64 | +Standard result shape: |
| 65 | + |
| 66 | +- `code`, `stdout`, `stderr`, `duration_ms`, `cmd` |
| 67 | +- helpers like `text()` and `arrayBuffer()` where useful |
| 68 | + |
| 69 | +## Phased rollout |
| 70 | + |
| 71 | +### Phase 0: Foundation |
| 72 | + |
| 73 | +- Implement Process wrappers in `src/util/process.ts`. |
| 74 | +- Refactor `src/util/git.ts` to use Process only. |
| 75 | +- Add tests for exit handling, timeout, abort, and output capture. |
| 76 | + |
| 77 | +### Phase 1: High-impact hotspots |
| 78 | + |
| 79 | +Migrate these first: |
| 80 | + |
| 81 | +- `src/cli/cmd/github.ts` |
| 82 | +- `src/worktree/index.ts` |
| 83 | +- `src/lsp/server.ts` |
| 84 | +- `src/installation/index.ts` |
| 85 | +- `src/snapshot/index.ts` |
| 86 | + |
| 87 | +Within each file, migrate git paths first where applicable. |
| 88 | + |
| 89 | +### Phase 2: Remaining git-heavy files |
| 90 | + |
| 91 | +Migrate git-centric call sites to `Process.git*` helpers: |
| 92 | + |
| 93 | +- `src/file/index.ts` |
| 94 | +- `src/project/vcs.ts` |
| 95 | +- `src/file/watcher.ts` |
| 96 | +- `src/storage/storage.ts` |
| 97 | +- `src/cli/cmd/pr.ts` |
| 98 | + |
| 99 | +### Phase 3: Remaining non-git files |
| 100 | + |
| 101 | +Migrate residual non-git usages: |
| 102 | + |
| 103 | +- `src/cli/cmd/tui/util/clipboard.ts` |
| 104 | +- `src/util/archive.ts` |
| 105 | +- `src/file/ripgrep.ts` |
| 106 | +- `src/tool/bash.ts` |
| 107 | +- `src/cli/cmd/uninstall.ts` |
| 108 | + |
| 109 | +### Phase 4: Stabilize |
| 110 | + |
| 111 | +- Remove dead wrappers and one-off patterns. |
| 112 | +- Keep plugin `$` compatibility isolated and documented as temporary. |
| 113 | +- Create linked 2.0 task for plugin `$` removal. |
| 114 | + |
| 115 | +## Validation strategy |
| 116 | + |
| 117 | +- Unit tests for new `Process` methods and options. |
| 118 | +- Integration tests on hotspot modules. |
| 119 | +- Smoke tests for install, snapshot, worktree, and GitHub flows. |
| 120 | +- Regression checks for output parsing behavior. |
| 121 | + |
| 122 | +## Risk mitigation |
| 123 | + |
| 124 | +- File-by-file PRs with small diffs. |
| 125 | +- Preserve behavior first, simplify second. |
| 126 | +- Keep shell-only exceptions explicit and documented. |
| 127 | +- Add consistent error shaping and logging at Process layer. |
| 128 | + |
| 129 | +## Definition of done |
| 130 | + |
| 131 | +- Runtime Bun `$` usage in `packages/opencode/src` is removed except: |
| 132 | + - approved shell-only exceptions |
| 133 | + - temporary plugin compatibility path (1.x) |
| 134 | +- Git paths use `Process.git*` consistently. |
| 135 | +- CI and targeted smoke tests pass. |
| 136 | +- 2.0 issue exists for plugin `$` removal. |
0 commit comments