From 1cc8dddc302067af97307c246214db858a940593 Mon Sep 17 00:00:00 2001 From: Jarek Rozanski Date: Sun, 11 Oct 2026 10:50:14 +0200 Subject: [PATCH] Fallback for existing sandbox and name collision --- README.md | 28 +++++++++++++++++++++++++--- install.sh | 22 +++++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2ca5cfb..6f8f09b 100644 --- a/README.md +++ b/README.md @@ -67,12 +67,34 @@ two things, both idempotent — safe to re-run: ```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` - registers (see below); `opencode` is the agent name; `"$@"` forwards any - extra args (e.g. `opencode-sandbox --model claude-sonnet-5`). Being a real + registers (see below), passed only when `--name` names a sandbox that + 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 identically under bash, zsh, fish, or anything else. diff --git a/install.sh b/install.sh index af1ec47..284babd 100755 --- a/install.sh +++ b/install.sh @@ -22,7 +22,27 @@ mkdir -p "$BIN_DIR" cat > "$SHIM" <<'EOF' #!/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 chmod +x "$SHIM" echo "==> Installed shim: $SHIM"