Files
w3cjandClaude Opus 5.5 ea3037bc15 fix(vm): MCP servers' logins in a VM finish and survive launches (#62)
Each launch and each devvm agent login claude copied the store's VM copy
of Claude Code's OAuth login (claudeAiOauth only) over the VM's
~/.claude/.credentials.json, where Claude Code also keeps its logins to
MCP servers (mcpOAuth: their tokens and, mid-login, the PKCE verifier).
So an MCP login failed when the VM was set up again before it finished,
and a finished one was gone at the next launch.

The setup now gets the copy as claude-credentials.json (claudeSyncName),
and sync.sh's merge_claude_credentials merges it in the VM: the copy's
claudeAiOauth replaces the VM's (or takes it out when the copy has none),
every other key of the VM's file stays. It reads the VM's file with one
jq call (--slurpfile, so two JSON values or a non-object are caught) and
writes it whole, mode 600, through a mktemp file in ~/.cache, which no
history export or remake carries, then mv; a failed cat or mv removes
the temp file and fails the setup. A link, or a file that's empty, not
JSON or not one object, is replaced by the copy and said (a link removed
first, so mv never writes through it into a folder); a folder is left
and said.

With a Claude Code token (setup-token), devvm sends no copy, as before,
so the VM's file stays as it is: Claude Code prefers the token in its
environment, and a leftover login's placeholder gets the token at
api.anthropic.com and nothing at the connectors' host.

mcpOAuth stays in the VM: devvm agent save reads only settings and the
.claude.json allowlist, history export excludes .credentials.json
(HistoryExcludes) and import skips it, and a remake leaves login files
out (homeSkips), all unchanged.

Tests: TestStageClaudeOAuthMerged (the copy is staged to merge, never at
home/.claude/.credentials.json); TestClaudeSyncNamesMerged (sync.sh
merges each claudeSyncName name into its place); guest.txtar runs
merge_claude_credentials (mcpOAuth and other keys kept, claudeAiOauth
replaced or removed, a key with a \u escape, empty, not JSON, two values,
an array, a link to a file or folder, a folder, a failed move, the
warnings, mode 600, no temp left); agent_login.txtar reads the new name.

Docs: Coding agents' MCP servers' logins (real tokens the VM's code can
read, never leaving it) and its Copies, How it works' Claude Code's
login and the network page's sentences, what a VM made again keeps, the
sync.sh header, and the safety pages' claim that a VM never holds a
refresh token, narrowed to the logins devvm keeps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-18 13:57:45 -07:00

299 lines
14 KiB
Plaintext

# What assets/guest/sync.sh sets up in a VM, its pieces run here: the prompt,
# what the proxy blocked, the merge of Claude Code's settings, and VS
# Code's settings; and assets/guest/tools.sh, which mounts the shared tools.
exec bash snippets.sh $DEVVM_REPO/assets/guest/sync.sh
# devvm reads devvm-tools' report after a failed setup, so the setup's
# first command removes an older report (from boot): a failure before
# devvm-tools runs isn't taken for an unmounted tool.
exec bash first-cmd.sh $DEVVM_REPO/assets/guest/sync.sh
stdout '^sudo rm -f /run/devvm/tools-unmounted$'
# The prompt starts with (devvm), after a theme that rebuilds PS1 before
# each prompt; with no theme, once.
exec bash prompt-test.sh
cmp stdout prompt.want
# Before each prompt: what the proxy blocked since the last one, and how
# to allow it. A new shell asks with no count and prints nothing; then the
# new block; then nothing new; then the proxy can't be reached (curl
# fails): nothing, and the count stays.
exec bash blocked-test.sh
cmp stdout blocked.want
# The VM's MCP servers follow the host's: removed ones go, copies are
# replaced whole (no real header of an older copy survives), the VM's own
# stay; the host's settings win but the VM keeps its per-folder state.
exec bash merge-test.sh
cmp stdout merge.want
# The VM's Claude Code login follows devvm's: its claudeAiOauth is
# replaced by the store's copy (or taken out, when the copy has none), and
# the rest of the VM's file stays, such as its logins to MCP servers
# (mcpOAuth), mode 600. A file that's missing is the copy; one that's a
# link, not JSON or not one object is replaced by the copy, and said; a
# link to a folder is replaced, not written into; a folder is left as it
# is, and said.
env HOME=$WORK/credhome
exec bash creds-test.sh
cmp stdout creds.want
stderr -count=6 '^devvm: .*/credhome/\.claude/\.credentials\.json couldn''t be read as a file of one JSON object, so devvm''s login replaced it, with any MCP servers'' logins in it$'
stderr -count=1 '^devvm: .*/credhome/\.claude/\.credentials\.json is a folder, so Claude Code in this VM doesn''t get devvm''s login$'
! stderr 'jq|error'
# VS Code's and Cursor's settings in the VM: no port forwarding to the
# host, no git sign-in relay; their other settings kept. A file that isn't
# plain JSON is left as it is, and said; the other editor's still set.
env HOME=$WORK/vmhome
exec bash vsc.sh
exec jq -c . $HOME/.vscode-server/data/Machine/settings.json
stdout '^\{"remote\.autoForwardPorts":false,"git\.terminalAuthentication":false,"github\.gitAuthentication":false\}$'
exec jq -c . $HOME/.cursor-server/data/Machine/settings.json
stdout '^\{"remote\.autoForwardPorts":false,"git\.terminalAuthentication":false,"github\.gitAuthentication":false\}$'
cp vsc-mine.json $HOME/.vscode-server/data/Machine/settings.json
cp vsc-mine.json $HOME/.cursor-server/data/Machine/settings.json
exec bash vsc.sh
exec jq -c . $HOME/.vscode-server/data/Machine/settings.json
stdout '^\{"editor\.fontSize":14,"remote\.autoForwardPorts":false,"git\.terminalAuthentication":false,"github\.gitAuthentication":false\}$'
exec jq -c . $HOME/.cursor-server/data/Machine/settings.json
stdout '^\{"editor\.fontSize":14,"remote\.autoForwardPorts":false,"git\.terminalAuthentication":false,"github\.gitAuthentication":false\}$'
cp vsc-comments.json $HOME/.vscode-server/data/Machine/settings.json
cp vsc-mine.json $HOME/.cursor-server/data/Machine/settings.json
exec bash vsc.sh
stderr '\.vscode-server/data/Machine/settings\.json isn''t plain JSON'
! stderr 'cursor-server'
cmp $HOME/.vscode-server/data/Machine/settings.json vsc-comments.json
exec jq -c . $HOME/.cursor-server/data/Machine/settings.json
stdout '^\{"editor\.fontSize":14,"remote\.autoForwardPorts":false,"git\.terminalAuthentication":false,"github\.gitAuthentication":false\}$'
cp vsc-comments.json $HOME/.cursor-server/data/Machine/settings.json
exec bash vsc.sh
stderr '\.cursor-server/data/Machine/settings\.json isn''t plain JSON'
cmp $HOME/.cursor-server/data/Machine/settings.json vsc-comments.json
# A folder of theirs that isn't one, or a file devvm can't write: said,
# and the rest of the setup runs on (set -e, as sync.sh has).
rm $HOME/.cursor-server
cp vsc-comments.json $HOME/.cursor-server
rm $HOME/.vscode-server/data/Machine/settings.json
exec bash -c 'set -euo pipefail; . ./vsc.sh; echo after'
stderr '\.cursor-server/data/Machine/settings\.json can''t be written'
stdout '^after$'
exec jq -c . $HOME/.vscode-server/data/Machine/settings.json
stdout '^\{"remote\.autoForwardPorts":false,"git\.terminalAuthentication":false,"github\.gitAuthentication":false\}$'
# devvm-tools (assets/guest/tools.sh) tries every image in the VM's list:
# it mounts the ones it can, and says why for each it can't (missing from
# the share, a loop device or mount that fails), leaving no empty mount
# point of theirs; then it lists them in
# its report, a line each, and fails. With all of them there, it succeeds
# and leaves no report.
exec bash tools-test.sh $DEVVM_REPO/assets/guest/tools.sh
cmp stdout tools.want
# The VM's links in /etc/hosts: a marked block after the file's own lines,
# replaced whole when they change, and gone without links. The file is
# written in place, never renamed over, and not at all when nothing
# changed.
exec bash hosts-test.sh
cmp stdout hosts.want
-- snippets.sh --
# Each piece of sync.sh ($1) into a file of its own.
awk '/^cat > ~\/.bashrc.d\/devvm-prompt.sh/ { on = 1; next } on && /^EOF$/ { exit } on' "$1" > prompt.sh
awk '/^_devvm_blocked\(\) \{/ { on = 1 } on && /^EOF$/ { exit } on' "$1" > blocked.sh
awk '/^merge_claude_json\(\) \{/ { on = 1 } on { print } on && /^\}$/ { exit }' "$1" > merge.sh
awk '/^merge_claude_credentials\(\) \{/ { on = 1 } on { print } on && /^\}$/ { exit }' "$1" > creds.sh
awk '/^editor_off=/ { on = 1 } on && /^done$/ { print; exit } on' "$1" > vsc.sh
awk '/^link_hosts\(\) \{/ { on = 1 } on { print } on && /^\}$/ { exit }' "$1" > hosts.sh
for f in prompt.sh blocked.sh merge.sh creds.sh vsc.sh hosts.sh; do [[ -s $f ]] || { echo "no $f in $1" >&2; exit 1; }; done
-- hosts-test.sh --
# sudo runs the command and says when it writes; hosts.link is the same
# file as hosts, which a write in place keeps.
exec 3>&1
sudo() { echo "(write)" >&3; "$@"; }
. hosts.sh
printf '127.0.0.1 localhost\n127.0.1.1 web\n' > hosts
ln hosts hosts.link
printf '127.77.0.2 db db.devvm.internal\n' > links
link_hosts hosts links; cat hosts.link
link_hosts hosts links; echo '-- again'
printf '127.77.0.2 db db.devvm.internal\n127.77.0.3 api api.devvm.internal\n' > links
link_hosts hosts links; cat hosts.link
: > links
link_hosts hosts links; cat hosts.link
-- hosts.want --
(write)
127.0.0.1 localhost
127.0.1.1 web
# devvm links begin
127.77.0.2 db db.devvm.internal
# devvm links end
-- again
(write)
127.0.0.1 localhost
127.0.1.1 web
# devvm links begin
127.77.0.2 db db.devvm.internal
127.77.0.3 api api.devvm.internal
# devvm links end
(write)
127.0.0.1 localhost
127.0.1.1 web
-- first-cmd.sh --
# The first command of a script ($1) after its set line, past comments.
awk 'on && !/^#/ { print; exit } /^set -euo pipefail$/ { on = 1 }' "$1"
-- prompt-test.sh --
# Two prompts, the way bash runs PROMPT_COMMAND (an array of commands on
# bash 5.1+, else a string).
prompts='for i in 1 2; do for c in "${PROMPT_COMMAND[@]}"; do eval "$c"; done; done; printf "[%s]\n" "$PS1"'
bash -c 'theme() { PS1="03:26:21 cj@vm \$ "; }; PROMPT_COMMAND=theme; . "$1"; '"$prompts" _ prompt.sh
bash -c 'PS1="\u@\h:\w\$ "; unset PROMPT_COMMAND; . "$1"; '"$prompts" _ prompt.sh
-- prompt.want --
[(devvm) 03:26:21 cj@vm $ ]
[(devvm) \u@\h:\w$ ]
-- blocked-test.sh --
# The proxy: each call answers with the next reply, and says which
# "after" it was asked for.
replies=("3" $'5\ndevvm: blocked registry.example.com:443 (not in the allowlist)\nTo allow, run this on the host:\ndevvm network allow registry.example.com' "5")
i=0
curl() { local url=${!#}; echo "after=${url##*after=}" >&2; [[ $i -lt ${#replies[@]} ]] || return 7; printf "%s\n" "${replies[i]}"; }
. blocked.sh
for c in 1 2 3 4; do _devvm_blocked 2>&1; i=$((i + 1)); done
-- blocked.want --
after=
after=3
devvm: blocked registry.example.com:443 (not in the allowlist)
To allow, run this on the host:
devvm network allow registry.example.com
after=5
after=5
-- merge-test.sh --
. merge.sh
v=vm.json h=host.json c=state/copied.json
# An earlier copy (network policy off then): a real header; plus a server
# added in the VM.
echo '{"theme":"light","projects":{"/p":{"trust":true}},"mcpServers":{
"gh":{"url":"https://x","headers":{"Authorization":"REAL","X-Old":"REAL2"}},
"gone":{"command":"old"},"mine":{"command":"vm-only"}}}' > "$v"
mkdir -p state; echo '["gh","gone"]' > "$c"
echo '{"theme":"dark","projects":{"/h":{}},"mcpServers":{"gh":{"url":"https://x","headers":{"Authorization":"PLACEHOLDER"}},"new":{"command":"n"}}}' > "$h"
merge_claude_json "$v" "$h" "$c"
jq -c '{theme, projects, mcpServers}' "$v"
jq -c . "$c"
# The first time: no list yet, no VM file.
rm -f "$v" "$c"
merge_claude_json "$v" "$h" "$c"
jq -c '.mcpServers | keys' "$v"
-- merge.want --
{"theme":"dark","projects":{"/p":{"trust":true}},"mcpServers":{"mine":{"command":"vm-only"},"gh":{"url":"https://x","headers":{"Authorization":"PLACEHOLDER"}},"new":{"command":"n"}}}
["gh","new"]
["gh","new"]
-- creds-test.sh --
set -euo pipefail
. creds.sh
v=$HOME/.claude/.credentials.json
show() { printf '%s %s\n' "$(stat -c %a "$v" 2>/dev/null || stat -f %Lp "$v")" "$(jq -cS . "$v")"; }
echo '{"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}' > store.json
# No VM file yet: the copy.
merge_claude_credentials "$v" store.json; show
# The VM's own logins stay, its old login is replaced whole.
echo '{"claudeAiOauth":{"accessToken":"OLD","extra":"x"},"mcpOAuth":{"srv|1":{"accessToken":"MCP"}},"other":1}' > "$v"
chmod 644 "$v"
merge_claude_credentials "$v" store.json; show
# A copy with no login takes the VM's away and keeps the rest.
echo '{}' > empty.json
merge_claude_credentials "$v" empty.json; show
# A key written with a JSON escape for its O is the same key.
bs=$(printf '\134')
printf '%s' "{\"claudeAi${bs}u004fauth\":{\"accessToken\":\"OLD\"},\"mcpOAuth\":{}}" > "$v"
grep -qF "${bs}u004f" "$v"
merge_claude_credentials "$v" store.json; show
# Empty, not JSON, two values, not an object: the copy.
for bad in '' 'not json' '{"mcpOAuth":{}} {"claudeAiOauth":{}}' '[1]'; do
printf '%s' "$bad" > "$v"
merge_claude_credentials "$v" store.json; show
done
# A link, to a file or to a folder: replaced by the copy, its target left.
echo '{"mcpOAuth":{"x":1}}' > target.json
ln -sfn "$PWD/target.json" "$v"
merge_claude_credentials "$v" store.json; show; [[ ! -L $v ]] && jq -c . target.json
mkdir -p elsewhere; rm "$v"; ln -s "$PWD/elsewhere" "$v"
merge_claude_credentials "$v" store.json; show; ls -A elsewhere | wc -l | tr -d ' '
# A folder: left, and said.
rm "$v"; mkdir "$v"
merge_claude_credentials "$v" store.json; [[ -d $v ]] && echo folder kept
# No temp file left behind, and none beside the VM's file.
ls -A ~/.cache | wc -l | tr -d ' '
ls -A ~/.claude
# A move that fails (a folder it can't write in): it fails, and its temp
# file goes.
rmdir "$v"; chmod 500 ~/.claude
if merge_claude_credentials "$v" store.json 2>/dev/null; then echo moved; else echo move failed; fi
chmod 700 ~/.claude
ls -A ~/.cache | wc -l | tr -d ' '
-- creds.want --
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"},"mcpOAuth":{"srv|1":{"accessToken":"MCP"}},"other":1}
600 {"mcpOAuth":{"srv|1":{"accessToken":"MCP"}},"other":1}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"},"mcpOAuth":{}}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}
{"mcpOAuth":{"x":1}}
600 {"claudeAiOauth":{"accessToken":"PLACEHOLDER","refreshToken":"devvm-proxy-managed"}}
0
folder kept
0
.credentials.json
move failed
0
-- tools-test.sh --
# tools.sh ($1) with its folders under t/, and the commands it mounts with
# stubbed: losetup fails for bad-loop.erofs, and mount for bad-mount.erofs (at mnt/1).
export d=$PWD/t
mkdir -p "$d/share" "$d/etc"
touch "$d/share/node-1-k.erofs" "$d/share/py-1-k.erofs" "$d/share/bad-loop.erofs" "$d/share/bad-mount.erofs"
from='share=/mnt/devvm-tools root=/opt/devvm/tools list=/etc/devvm/tools report=/run/devvm/tools-unmounted'
grep -qx "$from" "$1" || { echo "no folders line in $1" >&2; exit 1; }
sed "s|^$from\$|share=$d/share root=$d/opt list=$d/etc/tools report=$d/run/tools-unmounted|" "$1" > tools.sh
mountpoint() { return 1; }
losetup() { case $* in *bad-loop*) return 1 ;; -d*) echo "losetup $*" ;; *) echo /dev/loop0 ;; esac; }
mount() { t=${*: -1}; [[ $t != */mnt/1 ]] || return 32; echo "mount ${t#"$d"/}"; }
findmnt() { :; }
export -f mountpoint losetup mount findmnt
printf '%s\n' "node-1-k.erofs node 1" "gone-1-k.erofs gone 1" "bad-loop.erofs loop 1" \
"bad-mount.erofs mnt 1" "py-1-k.erofs python 1" > "$d/etc/tools"
bash tools.sh 2>&1; echo "exit $?"
sed 's/^/report: /' "$d/run/tools-unmounted"
cd "$d/opt" && for m in */1; do [[ ! -e $m ]] || echo "there $m"; done
printf '%s\n' "node-1-k.erofs node 1" "py-1-k.erofs python 1" > "$d/etc/tools"
bash ../../tools.sh 2>&1; echo "exit $?"
[[ -e $d/run/tools-unmounted ]] || echo "no report"
-- tools.want --
mount opt/node/1
devvm-tools: gone-1-k.erofs is missing from the share
devvm-tools: can't mount bad-loop.erofs
devvm-tools: can't mount bad-mount.erofs
losetup -d /dev/loop0
mount opt/python/1
devvm-tools: unmounted gone-1-k.erofs
devvm-tools: unmounted bad-loop.erofs
devvm-tools: unmounted bad-mount.erofs
exit 1
report: gone-1-k.erofs
report: bad-loop.erofs
report: bad-mount.erofs
there node/1
there python/1
mount opt/node/1
mount opt/python/1
exit 0
no report
-- vsc-mine.json --
{"editor.fontSize": 14, "remote.autoForwardPorts": true}
-- vsc-comments.json --
{
// a comment
}