Skip to content

Commit c2eed57

Browse files
fix(opencode): confine Bun temp library copies for managed OpenCode
OpenCode 1.18.32, the latest stable release, is built with Bun 1.3.14. That runtime copies each bundled native library to a new randomly named file in the temp directory on every load and never deletes the copy (anomalyco/opencode#42700, #49283). The Bun fix (oven-sh/bun#29587) needs Bun 1.4 or later. The 1.18.x upgrade (anomalyco/opencode#44946) is still open. ODS now starts its managed OpenCode with BUN_TMPDIR set to an ODS-owned directory that is emptied on every start: - Linux systemd user unit: ExecStartPre empties it, ExecStopPost removes it - macOS LaunchAgent: a /bin/sh wrapper empties it, then execs OpenCode - Windows scheduled-task launcher: empties it before starting OpenCode TMPDIR for OpenCode's tools is unchanged. New test tests/test-opencode-bun-tmpdir.sh runs in make test and in Test Linux CI. The Hermes Agent (v2026.9.24, 0.21.5) and OpenCode (1.18.32) pins are already the latest upstream releases, so no version changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2d3162b commit c2eed57

8 files changed

Lines changed: 183 additions & 0 deletions

File tree

‎.github/workflows/test-linux.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ jobs:
294294
- name: brave-search searxng Compat Tests
295295
run: bash tests/test-brave-search-searxng-compat.sh
296296

297+
- name: OpenCode Bun Temp Confinement Tests
298+
run: bash tests/test-opencode-bun-tmpdir.sh
299+
297300
- name: ODS Doctor Tests
298301
run: |
299302
bash tests/test-ods-doctor.sh

‎ods/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ test: ## Run unit and contract tests
3232
@bash tests/test-hermes-slash-worker-prune.sh
3333
@python3 tests/contracts/test-hermes-worker-guardrails.py
3434
@echo ""
35+
@echo "=== OpenCode managed launchers confine Bun temp copies ==="
36+
@bash tests/test-opencode-bun-tmpdir.sh
37+
@echo ""
3538
@echo "=== Installer contracts ==="
3639
@bash tests/contracts/test-installer-contracts.sh
3740
@bash tests/test-external-services.sh

‎ods/installers/macos/install-macos.sh‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,6 +2978,15 @@ for service in (data.get("services") or {}).values():
29782978
<string>${OPENCODE_PLIST_LABEL}</string>
29792979
<key>ProgramArguments</key>
29802980
<array>
2981+
<!-- OpenCode 1.18.x (Bun 1.3.14) copies bundled native libraries to a
2982+
new temp file on every load and never deletes them
2983+
(anomalyco/opencode#42700, #49283). Empty the ODS-owned
2984+
BUN_TMPDIR on every start, then exec OpenCode itself. -->
2985+
<string>/bin/sh</string>
2986+
<string>-c</string>
2987+
<string>dir="\$1"; shift; rm -rf "\$dir" &amp;&amp; mkdir -p -m 0700 "\$dir" &amp;&amp; export BUN_TMPDIR="\$dir" &amp;&amp; exec "\$@"</string>
2988+
<string>ods-opencode-web</string>
2989+
<string>${OPENCODE_BUN_TMPDIR}</string>
29812990
<string>${OPENCODE_BIN}</string>
29822991
<string>web</string>
29832992
<string>--port</string>

‎ods/installers/macos/lib/constants.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ OPENCODE_CONFIG_DIR="$HOME/.config/opencode"
6565
OPENCODE_PORT=3003
6666
OPENCODE_PLIST_LABEL="com.ods.opencode-web"
6767
OPENCODE_PLIST="$HOME/Library/LaunchAgents/${OPENCODE_PLIST_LABEL}.plist"
68+
# ODS-owned BUN_TMPDIR for the managed service; emptied on every service start.
69+
OPENCODE_BUN_TMPDIR="$HOME/Library/Caches/ODS/opencode-bun-tmp"
6870

6971
# Docker
7072
DOCKER_COMPOSE_CMD="docker compose"

‎ods/installers/windows/phases/07-devtools.ps1‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ if ($_ocReady -and (Test-Path $script:OPENCODE_EXE)) {
7575
Remove-Item Env:OPENCODE_SERVER_PASSWORD -ErrorAction SilentlyContinue
7676
`$env:OPENCODE_ENABLE_EXA = '1'
7777
# Preserve inherited OPENCODE_WEBSEARCH_PROVIDER; enabled Exa is the default.
78+
# OpenCode 1.18.x (Bun 1.3.14) copies bundled native libraries to a new temp
79+
# file on every load and never deletes them (anomalyco/opencode#42700).
80+
# Start from an emptied ODS-owned BUN_TMPDIR.
81+
`$bunTmp = Join-Path `$env:LOCALAPPDATA 'ODS\opencode-bun-tmp'
82+
if (Test-Path -LiteralPath `$bunTmp) { Remove-Item -LiteralPath `$bunTmp -Recurse -Force }
83+
New-Item -ItemType Directory -Path `$bunTmp -Force | Out-Null
84+
`$env:BUN_TMPDIR = `$bunTmp
7885
Set-Location -LiteralPath '$_ocDirLiteral'
7986
& '$_ocExeLiteral' web --port $($script:OPENCODE_PORT) --hostname 127.0.0.1
8087
exit `$LASTEXITCODE

‎ods/opencode/RELEASE.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ A locked Windows binary also fails without terminating its process: finish activ
2020
work and rerun the installer. Configuration/session directories are not removed.
2121
The existing ODS model-route configuration migration still runs after success.
2222

23+
OpenCode 1.18.32 is built with Bun 1.3.14. That runtime copies each bundled
24+
native library (`.so`, `.dylib`, `.dll`, `.node`) to a new randomly named file
25+
in the temp directory whenever it loads it, and never deletes the copy
26+
([#42700](https://github.com/anomalyco/opencode/issues/42700),
27+
[#49283](https://github.com/anomalyco/opencode/issues/49283)). The upstream fix
28+
needs Bun 1.4 or later; the 1.18.x upgrade
29+
([#44946](https://github.com/anomalyco/opencode/pull/44946)) is still open.
30+
ODS therefore starts
31+
its managed OpenCode with `BUN_TMPDIR` set to an ODS-owned directory, emptied on
32+
every start: `~/.cache/ods/opencode-bun-tmp` (systemd `ExecStartPre`, also removed
33+
by `ExecStopPost`), `~/Library/Caches/ODS/opencode-bun-tmp` (LaunchAgent wrapper),
34+
and `%LOCALAPPDATA%\ODS\opencode-bun-tmp` (Windows launcher). `TMPDIR` for
35+
OpenCode's tools is unchanged. OpenCode run directly by a user (TUI, `opencode
36+
run`) is not covered. The Windows host-agent restart after a model switch starts
37+
`opencode.exe` without the launcher, so it is not covered either.
38+
2339
ODS launchers enable the upstream `websearch` tool for local model providers via
2440
`OPENCODE_ENABLE_EXA=1`. Upstream uses Exa by default and ODS preserves an explicit
2541
`OPENCODE_WEBSEARCH_PROVIDER` override in the service/launcher environment.

‎ods/opencode/opencode-web.service‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ Environment=OPENCODE_ENABLE_EXA=1
2222
# OPENCODE_SERVER_PASSWORD as a Basic Auth password when present, but this
2323
# localhost-only web UI should be reachable without a login prompt.
2424
UnsetEnvironment=OPENCODE_SERVER_PASSWORD
25+
# OpenCode 1.18.x embeds Bun 1.3.14, which copies bundled native libraries to
26+
# a new randomly named temp file on every load and never deletes them
27+
# (anomalyco/opencode#42700, #49283). Keep those copies in an ODS-owned
28+
# directory that is emptied before each start and removed after stop.
29+
# BUN_TMPDIR leaves TMPDIR for OpenCode's tools unchanged.
30+
Environment="BUN_TMPDIR=__HOME__/.cache/ods/opencode-bun-tmp"
31+
ExecStartPre=rm -rf "__HOME__/.cache/ods/opencode-bun-tmp"
32+
ExecStartPre=mkdir -p -m 0700 "__HOME__/.cache/ods/opencode-bun-tmp"
33+
ExecStopPost=rm -rf "__HOME__/.cache/ods/opencode-bun-tmp"
2534

2635
# Hardening
2736
NoNewPrivileges=true
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env bash
2+
# OpenCode 1.18.x embeds Bun 1.3.14, which copies bundled native libraries to a
3+
# new randomly named temp file on every load and never deletes them
4+
# (anomalyco/opencode#42700, #49283). Every ODS-managed OpenCode launcher must
5+
# confine those copies to an ODS-owned BUN_TMPDIR that is emptied on start.
6+
set -euo pipefail
7+
8+
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
9+
SERVICE="$ROOT_DIR/opencode/opencode-web.service"
10+
MACOS_INSTALLER="$ROOT_DIR/installers/macos/install-macos.sh"
11+
MACOS_CONSTANTS="$ROOT_DIR/installers/macos/lib/constants.sh"
12+
WINDOWS_PHASE="$ROOT_DIR/installers/windows/phases/07-devtools.ps1"
13+
14+
pass() { echo "[PASS] $1"; }
15+
fail() { echo "[FAIL] $1" >&2; exit 1; }
16+
mode_of() { python3 -c 'import os, stat, sys; print(oct(stat.S_IMODE(os.stat(sys.argv[1]).st_mode)))' "$1"; }
17+
entries_in() { find "$1" -mindepth 1 -maxdepth 1 | wc -l | tr -d ' '; }
18+
leak_into() { printf 'stale native library copy' > "$1/.0123456789abcdef-00000000.so"; }
19+
20+
scratch="$(mktemp -d)"
21+
trap 'rm -rf "$scratch"' EXIT
22+
23+
echo "=== OpenCode BUN_TMPDIR confinement ==="
24+
25+
# --- Linux systemd user unit, rendered like installers/phases/07-devtools.sh ---
26+
home="$scratch/home with space"
27+
mkdir -p "$home"
28+
unit="$scratch/opencode-web.service"
29+
sed -e "s|__HOME__|$home|g" -e 's|__OPENCODE_BIN__|/opt/oc/opencode|g' \
30+
-e 's|__OPENCODE_BIN_DIR__|/opt/oc|g' "$SERVICE" > "$unit"
31+
linux_dir="$home/.cache/ods/opencode-bun-tmp"
32+
33+
grep -Fxq "Environment=\"BUN_TMPDIR=$linux_dir\"" "$unit" \
34+
|| fail "Linux unit must set an ODS-owned BUN_TMPDIR"
35+
if grep -Eq '^Environment="?TMPDIR=' "$unit"; then
36+
fail "Linux unit must leave TMPDIR for OpenCode tools unchanged"
37+
fi
38+
grep -q '^PrivateTmp=true$' "$unit" || fail "Linux unit must keep PrivateTmp=true"
39+
40+
# Run the unit's own ExecStartPre/ExecStopPost commands (plain double-quoted
41+
# arguments, which systemd and sh split identically).
42+
run_directives() {
43+
local key="$1" line found=0
44+
while IFS= read -r line; do
45+
eval "${line#"$key="}"
46+
found=1
47+
done < <(grep "^$key=" "$unit")
48+
[[ "$found" == 1 ]] || fail "Linux unit is missing $key"
49+
}
50+
51+
mkdir -p "$linux_dir"
52+
leak_into "$linux_dir"
53+
run_directives ExecStartPre
54+
[[ -d "$linux_dir" && "$(entries_in "$linux_dir")" == 0 ]] \
55+
|| fail "Linux ExecStartPre must leave an empty BUN_TMPDIR"
56+
[[ "$(mode_of "$linux_dir")" == "0o700" ]] || fail "Linux BUN_TMPDIR must be private (0700)"
57+
leak_into "$linux_dir"
58+
run_directives ExecStopPost
59+
[[ ! -e "$linux_dir" ]] || fail "Linux ExecStopPost must remove BUN_TMPDIR"
60+
pass "Linux unit empties BUN_TMPDIR before start and removes it after stop"
61+
62+
# --- macOS LaunchAgent, rendered from the installer heredoc ---
63+
# shellcheck disable=SC2016 # literal $HOME in the constants file
64+
grep -Fxq 'OPENCODE_BUN_TMPDIR="$HOME/Library/Caches/ODS/opencode-bun-tmp"' "$MACOS_CONSTANTS" \
65+
|| fail "macOS BUN_TMPDIR must live in the user's ODS cache"
66+
mac_dir="$scratch/mac bun tmp"
67+
fake_bin="$scratch/fake-opencode"
68+
record="$scratch/fake-opencode.record"
69+
cat > "$fake_bin" <<'FAKE'
70+
#!/bin/sh
71+
{
72+
printf 'BUN_TMPDIR=%s\n' "$BUN_TMPDIR"
73+
printf 'ENTRIES=%s\n' "$(find "$BUN_TMPDIR" -mindepth 1 -maxdepth 1 | wc -l | tr -d ' ')"
74+
printf 'ARGS=%s\n' "$*"
75+
} > "$ODS_TEST_RECORD"
76+
printf 'fresh copy' > "$BUN_TMPDIR/.fedcba9876543210-00000000.so"
77+
FAKE
78+
chmod +x "$fake_bin"
79+
80+
{
81+
echo 'cat <<PLIST_EOF'
82+
awk '/<<PLIST_EOF$/{inside=1; next} /^PLIST_EOF$/{inside=0} inside' "$MACOS_INSTALLER"
83+
echo 'PLIST_EOF'
84+
} > "$scratch/render-plist.sh"
85+
OPENCODE_PLIST_LABEL="com.ods.opencode-web" OPENCODE_BIN="$fake_bin" \
86+
OPENCODE_BUN_TMPDIR="$mac_dir" INSTALL_DIR="$scratch" HOME="$scratch" \
87+
OPENCODE_LAUNCHD_PATH="/usr/bin:/bin" bash "$scratch/render-plist.sh" > "$scratch/opencode.plist"
88+
89+
# plistlib also proves the rendered XML (escaping, comments) is a valid plist.
90+
python3 - "$scratch/opencode.plist" > "$scratch/program-args.txt" <<'PY'
91+
import plistlib, sys
92+
with open(sys.argv[1], 'rb') as handle:
93+
plist = plistlib.load(handle)
94+
for value in plist['ProgramArguments']:
95+
print(value)
96+
PY
97+
program_args=()
98+
while IFS= read -r value; do
99+
program_args+=("$value")
100+
done < "$scratch/program-args.txt"
101+
[[ "${program_args[0]}" == "/bin/sh" && "${program_args[1]}" == "-c" \
102+
&& "${program_args[3]}" == "ods-opencode-web" && "${program_args[4]}" == "$mac_dir" \
103+
&& "${program_args[5]}" == "$fake_bin" ]] \
104+
|| fail "macOS LaunchAgent must start OpenCode through the BUN_TMPDIR wrapper"
105+
[[ "${program_args[*]:6}" == "web --port 3003 --hostname 127.0.0.1" ]] \
106+
|| fail "macOS LaunchAgent must keep the loopback web command"
107+
108+
mkdir -p "$mac_dir"
109+
leak_into "$mac_dir"
110+
for start in 1 2; do
111+
ODS_TEST_RECORD="$record" "${program_args[@]}"
112+
grep -Fxq "BUN_TMPDIR=$mac_dir" "$record" || fail "macOS start $start must export BUN_TMPDIR"
113+
grep -Fxq "ENTRIES=0" "$record" || fail "macOS start $start must begin with an empty BUN_TMPDIR"
114+
grep -Fxq "ARGS=web --port 3003 --hostname 127.0.0.1" "$record" \
115+
|| fail "macOS start $start must pass the web command unchanged"
116+
done
117+
[[ "$(entries_in "$mac_dir")" == 1 ]] || fail "macOS restarts must not accumulate copies"
118+
[[ "$(mode_of "$mac_dir")" == "0o700" ]] || fail "macOS BUN_TMPDIR must be private (0700)"
119+
pass "macOS LaunchAgent empties BUN_TMPDIR on every start and execs OpenCode"
120+
121+
# --- Windows scheduled-task launcher (literal PowerShell text) ---
122+
# shellcheck disable=SC2016
123+
set_line="$(grep -n 'env:BUN_TMPDIR = `\$bunTmp' "$WINDOWS_PHASE" | cut -d: -f1)"
124+
# shellcheck disable=SC2016
125+
remove_line="$(grep -n 'Remove-Item -LiteralPath `\$bunTmp -Recurse -Force' "$WINDOWS_PHASE" | cut -d: -f1)"
126+
launch_line="$(grep -n "& '\$_ocExeLiteral' web --port" "$WINDOWS_PHASE" | cut -d: -f1)"
127+
grep -q "Join-Path \`\$env:LOCALAPPDATA 'ODS\\\\opencode-bun-tmp'" "$WINDOWS_PHASE" \
128+
|| fail "Windows launcher must use an ODS-owned BUN_TMPDIR"
129+
[[ -n "$set_line" && -n "$remove_line" && -n "$launch_line" \
130+
&& "$remove_line" -lt "$set_line" && "$set_line" -lt "$launch_line" ]] \
131+
|| fail "Windows launcher must empty and export BUN_TMPDIR before starting OpenCode"
132+
pass "Windows launcher empties BUN_TMPDIR before starting OpenCode"
133+
134+
echo "[PASS] OpenCode BUN_TMPDIR confinement"

0 commit comments

Comments
 (0)