Skip to content

fix(pty): graft bionic librust_pty so terminals work (POST /api/pty on bionic) - #40

Open
TanakaLun wants to merge 1 commit into
Hope2333:native-androidfrom
TanakaLun:fix/pty-bionic-asset
Open

TanakaLun wants to merge 1 commit into
Hope2333:native-androidfrom
TanakaLun:fix/pty-bionic-asset

Conversation

@TanakaLun

Copy link
Copy Markdown

症状

headless opencode serve 起来后一切正常(WebUI 可用),但任何终端都创建不出来:
远程客户端 POST /api/pty 失败,服务器只在自己的 stderr 里留一行

ERR_DLOPEN_FAILED: library "libpthread.so.0" not found
    at /$bunfs/root/chunk-4t04mpne.js:8:86

TUI 里开 pane 也一样(pty 是懒加载的,所以启动/渲染都正常,不碰到终端就不报错)。

根因

v2 native 主线(以及 B 线 build-bionic.sh 产物)内嵌的是 bun-pty 的
upstream glibc librust_pty_arm64.so。bun-pty 的解析顺序是

$BUN_PTY_LIB  →  内嵌资产  →  磁盘候选路径

native 主线把运行时直接装到 $PREFIX/bin/opencode(没有 launcher),
所以既没有 BUN_PTY_LIB,也没有磁盘候选,永远走内嵌的 glibc 那份 → bionic 上
libpthread.so.0 不存在 → dlopen 失败。

scripts/build-bionic.sh 的 PTY_VARIANT 只替换 @opencode-ai/pty(那个 helper
daemon),碰不到 Pty 服务懒加载的 bun-pty FFI,所以两条产线都有这个洞。

修法

  • tools/transplant/swap_pty.py:定位 bunfs 里那份 pty 资产的真实 ELF 边界,
    用 tools/bun-pty-embed/vendor/librust_pty_arm64_bionic.so(558 个符号同 ABI,
    8 个 bun_pty_* 导出、0 undefined)等长/更短替换,NUL 补齐到原槽位长度,
    末尾用 ctypes.CDLL 做 dlopen 门禁;兼容 bun ≥ 1.4.2 的 consolidated registry 布局。
  • transplant.py:新增步骤 8b,对两个打包赢家(opencode-native-tui /
    opencode-native-revived)都做 graft;新增 pty-swap 子命令与 steps.pty 报告字段。
  • scripts/build-bionic.sh:新增步骤 4a,B 线产物同样 graft(PTY_ASSET_FIX=0 可关)。
  • make pty-check BIN=<product>:发版门禁,资产是 glibc 就非零退出。
  • tests/transplant/test_swap_pty.py:合成夹具(JS 诱饵、consolidated 布局、
    替换门禁、等长替换),已接进 tests.yml CI。

验证(Android 15 / aarch64,已安装的 2.0.22)

结果
打补丁前 POST /api/pty 500,stderr 见上面的 dlopen 报错
打补丁前 + BUN_PTY_LIB=<vendor> 成功(证明只是资产选择问题,不是 ABI 问题)
打补丁后 POST /api/pty {status: running, pid: ...},ps 能看到 /system/bin/sh -l
make pty-check 对 glibc 产物 非零退出
tests/transplant/test_swap_pty.py PASS

已知边界(请 reviewer 注意)

  • 只实测了 2.0.22。其它版本的资产名/尺寸/哈希不同,脚本按 bunfs 名后缀 +
    ELF 边界动态定位,但请在有产物的版本矩阵上各跑一次
    make pty-check BIN=... 确认没有 absent。
  • vendor .so 是预编译产物(sha256 58aeeb46…,由 tools/bun-pty-embed/build-embed.sh
    重建),换 bun-pty 大版本时需重新走一遍 ABI 比对。
  • 若将来 Pty 服务改用它路(比如彻底切到 @opencode-ai/pty 的 musl daemon),
    资产不存在时工具会报 absent 并跳过,不会误伤。
  • 部署方式不变:这是改产物,不是改安装流程;pkg upgrade 覆盖安装后需重跑补丁
    (已写进 docs/transplant.md §0.5 与 incident 记录)。

Root cause: the v2 native mainline (and the B-line build) ships the upstream
glibc librust_pty inside the bunfs payload. bun-pty resolves its lib as
$BUN_PTY_LIB -> embedded asset -> on-disk paths, so a launcherless install
always dlopens the glibc copy and fails on bionic:

  ERR_DLOPEN_FAILED: library "libpthread.so.0" not found

TUI rendering and serve startup are unaffected (the Pty service lazily imports
the FFI chunk), so the defect only shows up when a terminal is created: TUI
panes and, for a headless server, a remote client's POST /api/pty.

Fix:
- tools/transplant/swap_pty.py: equal-or-smaller byte swap of the embedded
  asset with tools/bun-pty-embed/vendor/librust_pty_arm64_bionic.so
  (8-symbol ABI, zero undefined syms), NUL-padded to the exact slot, with a
  ctypes.CDLL dlopen gate; consolidated-registry (bun >=1.4.2) fallback.
- transplant.py: step 8b grafts both packaging winners (-tui, -revived),
  new pty-swap subcommand, steps.pty/pty report fields.
- build-bionic.sh: step 4a applies the same graft to the B-line product
  (PTY_ASSET_FIX=0 to disable).
- make pty-check BIN=<product>: release gate, non-zero on a glibc asset.
- tests/transplant/test_swap_pty.py: synthetic fixtures (marker decoy,
  consolidated layout, replacement gates, in-place swap).

Verified on Android 15/aarch64 with the installed 2.0.22 build:
before -> /api/pty 500 + libpthread.so.0 dlopen error;
after  -> {status: running, pid: ...} and the shell shows up in ps;
BUN_PTY_LIB=<vendor> on the pristine binary works too.
@Hope2333

Hope2333 commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Thanks — the diagnosis is exactly right, and it matches what we measured independently: POST /api/pty dies with ERR_DLOPEN_FAILED because bun-pty's embedded librust_pty is the glibc build, and native has no launcher to set BUN_PTY_LIB.

Two things before this can be merged as-is:

  1. The PR is missing the files it depends on. scripts/build-bionic.sh calls transplant.py pty-swap, which needs tools/bun-pty-embed/vendor/librust_pty_arm64_bionic.so and tools/bun-pty-embed/build-embed.sh — neither is in the diff (12 files, no tools/bun-pty-embed/). As it stands the build would abort at the new step with "rebuild the vendor lib".

  2. We took a different route that avoids the ELF surgery, and it works on device now:

  • Build the crate for bionic instead of shipping a musl/splice build. This host's rustc is built for aarch64-linux-android, so cargo build --release in bun-pty's rust-pty/ produces a native bionic librust_pty.so directly (540 KB, NEEDED libc.so/libdl.so, 8 bun_pty_* symbols). Two upstream gaps had to be filled: termios 0.2.2 has no android cfg (vendored copy adds two #[cfg(target_os = "android")] lines), and _Unwind_*@LIBC_R stays undefined because rustc's -lc stub resolves it first (a linker wrapper inserts libunwind.a before the first -lc).
  • Replace the asset at build time, not after. bun snapshots the file from node_modules/bun-pty/rust-pty/target/release/ at compile time, so swapping it right before build.ts is enough — no bunfs slot maths, no size ceiling. A post-compile assertion fails the build if libpthread.so.0 survives in the payload or if bun_pty_spawn is gone.

Result on the reference device (Android 9 / kernel 3.18.140): POST /api/pty → 200, real zsh process spawned, TUI/serve/keystroke unchanged.

So: we're going to keep our implementation for now, but your gates (glibc-marker check, 8-symbol check, dlopen verification) are good and we adopted equivalents. If you'd like, the most useful follow-up is your build-embed.sh recipe — if it builds the bionic lib reproducibly we'd happily compare notes, and a PR that only adds tools/bun-pty-embed/ (no swap_pty.py) would be easy to take.

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