Files
devvm/assets/opencode_test.go
w3cjandClaude Opus 5.5 b6b60d5cf3 fix(agents): opencode 2 VMs get the store's changed logins and keys at each launch
opencode 2 keeps its logins in its database, so a VM whose database
existed kept the logins it had: a key saved from another VM, or a ChatGPT
login for another account, never reached it.

Every launch's setup now sends the store's opencode logins as opencode
auth import reads them (agentlogin.OpencodeImport: each API key the proxy
puts in and OpenAI's Codex login, placeholders alone, IDs
cred_devvm_<provider>; methodID chatgpt-browser and a whole-number
expires, as opencode 2.2.5's credential schema needs). The new guest
script assets/guest/opencode.sh reads them on its input, never argv, and,
once opencode has a database, imports them when they differ from its
record (~/.local/state/devvm/opencode-import.json) or when the database
no longer holds one of devvm's IDs (deleted, or replaced by a history
import; checked with opencode auth export, only the IDs kept). It logs
out of devvm's earlier entries (opencode auth import skips an ID it has),
imports each entry on its own and switches its provider to devvm's entry;
the user's own credentials stay in the database. devvm agent login
opencode --api-key runs the same script at once, in place of the old
one-key import. The record lives in the VM because only the VM knows its
database and what it holds.

No partial success: a provider whose import opencode refuses is printed
as devvm-opencode-failed PROVIDER, the script exits 1 and records
nothing, so the next launch tries again, and devvm agent login names the
providers in the new message opencode_import_failed.

Three faults found on real opencode 2.2.5 VMs and fixed: bash -lc runs
~/.bash_logout (Debian's clear_console, failing without a terminal),
which under set -e became the script's status after a good import, so
the work runs in a subshell; opencode 2.2.5 made its database without
importing auth.json while the script had recorded the entries anyway, so
nothing is recorded until the database exists; and a missing expires
becomes 0.

Every VM also sets OPENCODE_DISABLE_AUTOUPDATE=1, beside Claude Code's
DISABLE_AUTOUPDATER in base's /etc/profile.d/devvm-tools.sh: the agents
are read-only shared tools whose versions mise pins.

Tests: TestOpencodeImport; TestOpencodeScript (the script against a fake
opencode: no database, an import, the same input again, a database made
again, a refused entry); TestOpencodeRefused; TestToolsEnvironment;
agent_login.txtar (the script at a key's save, opencode-import.json in
the setup, a refused provider named through the guest stub's new
opencode-refuses switch). e2e's network section: OPENCODE_DISABLE_AUTOUPDATE in an
opencode VM's login shell; a second opencode VM with a database and a
credential of its own gets a key saved from the first at its next
launch, its own credential kept; and gets it again once its database is
deleted and made again. Failures print opencode.sh's stderr, the run
logs' opencode lines and the VM's credential IDs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 01:02:42 -07:00

113 lines
4.2 KiB
Go

package assets
import (
"bytes"
"errors"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// fakeOpencode stands in for opencode 2 in guest/opencode.sh's test: its
// credentials are $HOME/creds.json, auth export prints them, auth import
// adds the entries whose IDs it lacks (refusing one for the provider
// "refused"), auth logout removes one, and each call is logged to
// $HOME/calls.
const fakeOpencode = `#!/bin/bash
set -eu
c=$HOME/creds.json
[ -f "$c" ] || echo '[]' > "$c"
echo "$*" >> "$HOME/calls"
case "$1 $2" in
"auth export") cat "$c" ;;
"auth import")
in=$(cat)
if jq -e 'any(.[]; .integrationID == "refused")' <<<"$in" >/dev/null; then echo "Missing key" >&2; exit 1; fi
jq --argjson in "$in" '. as $have | . + [$in[] | select(.id as $i | all($have[]; .id != $i))]' "$c" > "$c.new" && mv "$c.new" "$c" ;;
"auth logout")
jq --arg id "$4" '[.[] | select(.id != $id)]' "$c" > "$c.new" && mv "$c.new" "$c" ;;
"auth switch") ;;
*) exit 1 ;;
esac
`
// opencode.sh imports devvm's entries once opencode has a database, and
// again whenever the input changed or the database lost one of devvm's
// IDs (made again, or replaced); with no database it records nothing; a
// refused entry names its provider, fails, and records nothing.
func TestOpencodeScript(t *testing.T) {
for _, c := range []string{"bash", "jq"} {
if _, err := exec.LookPath(c); err != nil {
t.Skipf("no %s: %v", c, err)
}
}
script, err := fs.ReadFile(FS, "guest/opencode.sh")
if err != nil {
t.Fatal(err)
}
home := t.TempDir()
bin := filepath.Join(home, "bin")
must(t, os.MkdirAll(bin, 0o700))
must(t, os.WriteFile(filepath.Join(bin, "opencode"), []byte(fakeOpencode), 0o700)) //nolint:gosec // the fake runs: its owner's alone
db := filepath.Join(home, ".local", "share", "opencode", "opencode.db")
const record = ".local/state/devvm/opencode-import.json"
files := os.DirFS(home)
read := func(name string) string { b, _ := fs.ReadFile(files, name); return string(b) }
exists := func(name string) bool { _, err := fs.Stat(files, name); return err == nil }
run := func(input string) (string, int) {
cmd := exec.CommandContext(t.Context(), "bash", "-c", string(script), "opencode.sh") //nolint:gosec // devvm's own embedded script
cmd.Env = []string{"HOME=" + home, "PATH=" + bin + ":" + os.Getenv("PATH")}
cmd.Stdin = strings.NewReader(input)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
code := 0
var e *exec.ExitError
if errors.As(err, &e) {
code = e.ExitCode()
} else if err != nil {
t.Fatal(err)
}
return out.String(), code
}
imports := func() int { return strings.Count(read("calls"), "auth import") }
creds := func() string { return read("creds.json") }
in := `[{"active":true,"id":"cred_devvm_openrouter","integrationID":"openrouter","label":"devvm","value":{"key":"PH","type":"key"}}]`
if _, code := run(in); code != 0 || imports() != 0 || exists(record) {
t.Fatalf("no database: code %d, %d imports, record %v", code, imports(), exists(record))
}
must(t, os.MkdirAll(filepath.Dir(db), 0o700))
must(t, os.WriteFile(db, nil, 0o600))
if _, code := run(in); code != 0 || imports() != 1 || !strings.Contains(creds(), "cred_devvm_openrouter") || !exists(record) {
t.Fatalf("a database: code %d, %d imports, creds %s", code, imports(), creds())
}
if _, code := run(in); code != 0 || imports() != 1 {
t.Fatalf("the same input again: code %d, %d imports", code, imports())
}
// The database made again: devvm's entry is gone, so it's imported again.
must(t, os.Remove(filepath.Join(home, "creds.json")))
if _, code := run(in); code != 0 || imports() != 2 || !strings.Contains(creds(), "cred_devvm_openrouter") {
t.Fatalf("a new database: code %d, %d imports, creds %s", code, imports(), creds())
}
refused := `[{"active":true,"id":"cred_devvm_refused","integrationID":"refused","label":"devvm","value":{"key":"PH","type":"key"}}]`
before := read(record)
if out, code := run(refused); code != 1 || out != "devvm-opencode-failed refused\n" {
t.Fatalf("a refused entry: code %d, output %q", code, out)
}
if read(record) != before {
t.Error("a refused entry changed the record")
}
}
func must(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}