Files
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

58 lines
2.9 KiB
Bash

#!/bin/bash
# Brings opencode 2's logins in this VM in line with devvm's store, run in a login shell (opencode on
# PATH) by sync.sh on every launch and by devvm agent login --api-key. Its input is the store's
# opencode logins as opencode auth export prints them, with placeholders alone, under devvm's IDs
# (cred_devvm_PROVIDER). opencode 2 keeps its logins in its database, so once it has made one, an
# input that differs from the last this VM's database got, or one whose IDs the database no longer
# holds, is imported: devvm's entries of the last
# are logged out first, then each new one is imported on its own and made its provider's active
# credential, and the user's own credentials stay in the database. Until opencode has made its
# database there is nothing to import into, and nothing is recorded, so the first launch after it
# imports them all. Each provider whose import fails is printed as a line "devvm-opencode-failed
# PROVIDER", and then the script exits non-zero, recording nothing, so the next launch tries again;
# any other failure prints the command that failed to stderr.
#
# It runs in a login shell, whose ~/.bash_logout (Debian's clear_console, which fails without a
# terminal) would end it with its own status under set -e, so the work runs in a subshell and the
# shell itself exits with the subshell's status.
(
set -euo pipefail
trap 'echo "opencode.sh: line $LINENO: $BASH_COMMAND failed" >&2' ERR
new=$(cat)
last=~/.local/state/devvm/opencode-import.json
command -v opencode >/dev/null || exit 0
set -- ~/.local/share/opencode/opencode*.db
[[ -e $1 ]] || exit 0
# The record alone isn't enough: a database made again (deleted, or replaced by a history import)
# no longer holds devvm's entries, so every ID of the input must still be there. Only the IDs leave
# opencode auth export's output.
have=$(opencode auth export </dev/null 2>/dev/null | jq -r '.[].id' 2>/dev/null) || have=
missing=0
while read -r id; do
grep -qxF -- "$id" <<<"$have" || missing=1
done < <(jq -r '.[].id' <<<"$new")
[[ "$new" != "$(cat "$last" 2>/dev/null)" || $missing == 1 ]] || exit 0
if [[ -f $last ]]; then
{ jq -r '.[] | "\(.integrationID) \(.id)"' "$last" 2>/dev/null || true; } | while read -r provider id; do
opencode auth logout "$provider" "$id" </dev/null >/dev/null 2>&1 || true
done
fi
# One entry at a time, so a refused one names its provider and the others still go in. The store's
# login is the one made last, so each provider then uses devvm's entry, as every VM does.
failed=0
while read -r entry; do
provider=$(jq -r .integrationID <<<"$entry")
if ! opencode auth import <<<"[$entry]" >&2; then
echo "devvm-opencode-failed $provider"
failed=1
continue
fi
opencode auth switch "$provider" "$(jq -r .id <<<"$entry")" </dev/null >/dev/null 2>&1 || true
done < <(jq -c '.[]' <<<"$new")
[[ $failed == 0 ]] || exit 1
mkdir -p "${last%/*}"
printf '%s\n' "$new" > "$last.devvm"
mv -f "$last.devvm" "$last"
)
exit $?