Fallback for existing sandbox and name collision

This commit is contained in:
Jarek Rozanski 2026-10-11 10:50:14 +02:00
commit 1cc8dddc30
No known key found for this signature in database
2 changed files with 46 additions and 4 deletions

View file

@ -67,12 +67,34 @@ two things, both idempotent — safe to re-run:
```sh ```sh
#!/bin/sh #!/bin/sh
exec sbx run -t localhost/opencode-v2:local "$@" opencode # sbx run accepts -t/--template only at sandbox creation time; re-running
# against an existing sandbox with a template fails. So pass the template
# only when the named sandbox doesn't exist yet.
TEMPLATE=localhost/opencode-v2:local
# Extract --name VALUE / --name=VALUE from the arguments, if given.
name=
prev=
for arg in "$@"; do
[ "$prev" = --name ] && name=$arg
case "$arg" in
--name=*) name=${arg#--name=} ;;
esac
prev=$arg
done
if [ -n "$name" ] && sbx ls -q 2>/dev/null | grep -Fxq -- "$name"; then
exec sbx run "$@" opencode
fi
exec sbx run -t "$TEMPLATE" "$@" opencode
``` ```
`-t localhost/opencode-v2:local` points at the template `make build` `-t localhost/opencode-v2:local` points at the template `make build`
registers (see below); `opencode` is the agent name; `"$@"` forwards any registers (see below), passed only when `--name` names a sandbox that
extra args (e.g. `opencode-sandbox --model claude-sonnet-5`). Being a real doesn't exist yet (sbx rejects a template when re-attaching); `opencode`
is the agent name; `"$@"` forwards any extra args
(e.g. `opencode-sandbox --model claude-sonnet-5`). Being a real
executable on `PATH` rather than a shell function means it works executable on `PATH` rather than a shell function means it works
identically under bash, zsh, fish, or anything else. identically under bash, zsh, fish, or anything else.

View file

@ -22,7 +22,27 @@ mkdir -p "$BIN_DIR"
cat > "$SHIM" <<'EOF' cat > "$SHIM" <<'EOF'
#!/bin/sh #!/bin/sh
exec sbx run -t localhost/opencode-v2:local "$@" opencode # sbx run accepts -t/--template only at sandbox creation time; re-running
# against an existing sandbox with a template fails. So pass the template
# only when the named sandbox doesn't exist yet.
TEMPLATE=localhost/opencode-v2:local
# Extract --name VALUE / --name=VALUE from the arguments, if given.
name=
prev=
for arg in "$@"; do
[ "$prev" = --name ] && name=$arg
case "$arg" in
--name=*) name=${arg#--name=} ;;
esac
prev=$arg
done
if [ -n "$name" ] && sbx ls -q 2>/dev/null | grep -Fxq -- "$name"; then
exec sbx run "$@" opencode
fi
exec sbx run -t "$TEMPLATE" "$@" opencode
EOF EOF
chmod +x "$SHIM" chmod +x "$SHIM"
echo "==> Installed shim: $SHIM" echo "==> Installed shim: $SHIM"