Skip to content

Commit 9065e0c

Browse files
mogulOpenCode Agent
andcommitted
fix(kits): resolve opencode scratch dir ownership by agent name
Look up the sandbox agent's uid/gid with id instead of assigning the literal 1000:1000, which breaks on MSB images where acq provisions agent at another uid. Fail closed when the agent user is missing. Add tests with id/install shims for a non-1000 agent and a missing agent. Co-authored-by: OpenCode Agent <[email protected]>
1 parent 74412c6 commit 9065e0c

3 files changed

Lines changed: 71 additions & 9 deletions

File tree

‎integrations/isolation/acq-kits/opencode/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ deterministic, version-pinned install.
1010

1111
The install-time `opencode --version` checks run as root and can create
1212
`/tmp/opencode` with root ownership. On every sandbox startup, a root command
13-
creates or repairs that directory with agent ownership (`1000:1000`) and private
14-
permissions (`0700`). This also handles a cleared `/tmp` after a restart.
13+
creates or repairs that directory with ownership of the sandbox's `agent` user
14+
and group, resolved by name at startup (the numeric uid can differ from `1000`
15+
on some MSB images), and private permissions (`0700`). It fails closed if the
16+
`agent` user does not exist. This also handles a cleared `/tmp` after a restart.
1517

1618
The command changes only the directory itself, not existing contents, and
1719
refuses a pre-existing symlink or non-directory. It uses the same neutral

‎integrations/isolation/acq-kits/opencode/spec.yaml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ commands:
122122
- -c
123123
- |
124124
set -eu
125+
# Resolve ownership by name: acq may provision `agent` at a uid other
126+
# than the default (for example when that uid is already taken on an MSB image).
127+
if ! agent_uid=$(id -u agent 2>/dev/null) || ! agent_gid=$(id -g agent 2>/dev/null); then
128+
echo "opencode-kit(startup): user 'agent' not found; cannot prepare /tmp/opencode" >&2
129+
exit 1
130+
fi
125131
if [ -L /tmp/opencode ]; then
126132
echo "opencode-kit(startup): refusing symlink at /tmp/opencode" >&2
127133
exit 1
@@ -130,7 +136,7 @@ commands:
130136
echo "opencode-kit(startup): refusing non-directory at /tmp/opencode" >&2
131137
exit 1
132138
fi
133-
install -d -o 1000 -g 1000 -m 0700 /tmp/opencode
139+
install -d -o "$agent_uid" -g "$agent_gid" -m 0700 /tmp/opencode
134140
# acq supplies ACQ_PROVIDER_INPUT_ROOT. It resolves to the same read-only
135141
# models/providers layout for every backend. This is the sole acq-owned writer
136142
# of OpenCode's global config after the cutover.

‎integrations/isolation/acq-kits/opencode/tests/tmp-directory.test.mjs‎

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,79 @@ const startup = spec.match(/ - phase: startup\n user: "0"\n description:
1010
assert.ok(startup, "scratch directory preparation must be wired as a root startup command")
1111
const body = startup[1].replace(/^ /gm, "")
1212

13-
async function fixture(t) {
13+
const idShim = `#!/bin/sh
14+
[ -n "$FAKE_UID" ] || exit 1
15+
[ "$2" = agent ] || exit 1
16+
case "$1" in -u) echo "$FAKE_UID" ;; -g) echo "$FAKE_GID" ;; *) exit 1 ;; esac
17+
`
18+
19+
// Records the requested ownership, then installs with the caller's real ids
20+
// (the test cannot chown to arbitrary ids without root).
21+
const installShim = `#!/bin/sh
22+
echo "$*" >> "$INSTALL_LOG"
23+
n=$#
24+
while [ "$n" -gt 0 ]; do
25+
a=$1; shift; n=$((n - 1))
26+
case "$a" in
27+
-o) shift; n=$((n - 1)); set -- "$@" -o "$REAL_UID" ;;
28+
-g) shift; n=$((n - 1)); set -- "$@" -g "$REAL_GID" ;;
29+
*) set -- "$@" "$a" ;;
30+
esac
31+
done
32+
exec /usr/bin/install "$@"
33+
`
34+
35+
async function fixture(t, { agent = { uid: process.getuid(), gid: process.getgid() } } = {}) {
1436
const root = await mkdtemp(path.join(os.tmpdir(), "opencode-tmp-test-"))
1537
t.after(() => rm(root, { recursive: true, force: true }))
1638
const directory = path.join(root, "opencode")
39+
const bin = path.join(root, "bin")
40+
const installLog = path.join(root, "install.log")
41+
await mkdir(bin)
42+
await writeFile(path.join(bin, "id"), idShim, { mode: 0o755 })
43+
await writeFile(path.join(bin, "install"), installShim, { mode: 0o755 })
1744
// Exercise real install/chmod without requiring root or touching /tmp/opencode.
1845
const command = body.replaceAll("/tmp/opencode", directory)
19-
.replace("-o 1000 -g 1000", `-o ${process.getuid()} -g ${process.getgid()}`)
2046
const run = () => spawnSync("/bin/sh", ["-c", command], {
21-
env: { PATH: "/usr/bin:/bin" }, encoding: "utf8",
47+
env: {
48+
PATH: `${bin}:/usr/bin:/bin`,
49+
INSTALL_LOG: installLog,
50+
REAL_UID: String(process.getuid()),
51+
REAL_GID: String(process.getgid()),
52+
...(agent ? { FAKE_UID: String(agent.uid), FAKE_GID: String(agent.gid) } : {}),
53+
},
54+
encoding: "utf8",
2255
})
23-
return { root, directory, run }
56+
const installCalls = async () => readFile(installLog, "utf8").catch(() => "")
57+
return { root, directory, run, installCalls }
2458
}
2559

26-
test("root startup assigns the sandbox agent's numeric UID/GID without recursive changes", () => {
27-
assert.match(body, /install -d -o 1000 -g 1000 -m 0700 \/tmp\/opencode/)
60+
test("root startup resolves the agent's UID/GID by name without recursive changes", () => {
61+
assert.match(body, /id -u agent/)
62+
assert.match(body, /id -g agent/)
63+
assert.match(body, /install -d -o "\$agent_uid" -g "\$agent_gid" -m 0700 \/tmp\/opencode/)
64+
assert.doesNotMatch(body, /\b1000\b/)
2865
assert.doesNotMatch(body, /(?:chown|chmod)\s+-R/)
2966
assert.ok(spec.indexOf(startup[0]) < spec.indexOf("description: Render current provider inputs"))
3067
})
3168

69+
test("startup uses a non-1000 agent uid/gid reported by the sandbox", async (t) => {
70+
const { directory, run, installCalls } = await fixture(t, { agent: { uid: 2345, gid: 2346 } })
71+
const result = run()
72+
assert.equal(result.status, 0, result.stderr)
73+
assert.match(await installCalls(), /-d -o 2345 -g 2346 -m 0700 \S+\/opencode/)
74+
assert.ok((await stat(directory)).isDirectory())
75+
})
76+
77+
test("startup fails closed when the agent user does not exist", async (t) => {
78+
const { directory, run, installCalls } = await fixture(t, { agent: null })
79+
const result = run()
80+
assert.notEqual(result.status, 0)
81+
assert.match(result.stderr, /user 'agent' not found/)
82+
assert.equal(await installCalls(), "")
83+
await assert.rejects(stat(directory))
84+
})
85+
3286
test("startup creates a private writable directory and is idempotent", async (t) => {
3387
const { directory, run } = await fixture(t)
3488
for (let attempt = 0; attempt < 2; attempt++) {

0 commit comments

Comments
 (0)