Files
devvm/testdata/script/sync.txtar
T
w3cjandClaude Opus 5.5 9f6f62d28a refactor(state): vm.json, typed state in package sections, changed under the VM's lock
A VM's state is vms/<hash>/vm.json: core fields (version, id, name,
alias, image, chain, from, dir, folder, backend, backend_at, created)
and one section per package, each a json.RawMessage its owner decodes
into its own type (state.Section, state.SetSection), what was asked for
at its top level and what devvm made under "made":
net (netpolicy), git (gitprotect), ports (the new internal/ports),
agent (sshagent), tools (tools), secrets (secrets), and size, clone,
devcontainer, desktop and links (vm, internal/vm/state.go).

- state.VM.Update takes vms/<hash>/lock (platform.LockFile), reads the
  file again, applies a change and writes it once; a failed change, or
  write, writes nothing. VM.Set and its calls are gone; read-modify-write
  of lists (links, publish, secrets, tools, size, ports) happens inside
  Update. VM.Claim also holds run/claims.lock, after the VM's own;
  VM.Create writes a new VM's whole record once, its alias chosen
  (sshconfig.ChooseAlias) under the claims lock. Reads take no lock.
- created replaces "net unset" as unfinished (VM.Finished).
- internal/ports: the ports section (forward, host.ssh/proxy/desktop),
  ports.Used (was netpolicy.UsedPorts), and one Allocator for ssh, proxy
  and desktop ports under the claims lock (desktop allocated without
  one before; run/ports.lock and netpolicy's own ports key are gone).
- gitprotect: mode recorded before the backend makes the VM (NewMode;
  clone for --clone, which ls and git-protect share as gitprotect.Mode);
  made.git_id (platform.FolderID), one git_dirs list for modules and
  embedded git dirs, delegations, protected; Unlock takes the union of
  recorded and current git dirs.
- links' entries, git dirs, tools and devcontainer commands are JSON
  lists, not strings in values; the key=value parser is gone.
- vm.checkState reads every section of the folder's VM as it's found.
- registry.For drops its unused Dirs.
- Tests: TestVMRoundTrip, TestVMOtherName, TestVMID, TestVMUpdate,
  TestVMUpdateTogether (two writers on one VM and on crossed VMs),
  TestVMClaim, TestVMFailures, TestVMCorrupt, TestChooseAliasTogether,
  TestUsed, TestSSH, TestChooseTogether, TestUnlockRecorded,
  TestCheckState, TestChangesTogether, TestChangesAtOnce; testscript's
  vm_state, json_set with dotted and := keys; e2e's vm_state/vm_set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 14:47:06 -07:00

144 lines
6.4 KiB
Plaintext

# A VM's setup on every launch: the host's settings, its agents' config
# (never their history), their folders mounted read-only, and tokens: as
# they are without network policy, placeholders with it.
env TZ=Europe/Paris
env LANG=en_US.UTF-8
exec git config --global user.name 'Test User'
exec git config --global user.email [email protected]
# The host's agent config, and their history.
mkdir $HOME/.claude/skills $HOME/.claude/projects/p $HOME/.codex/prompts
cp $WORK/settings.json $HOME/.claude/settings.json
cp $WORK/claude.json $HOME/.claude.json
cp $WORK/codex.json $HOME/.codex/auth.json
cp $WORK/config.toml $HOME/.codex/config.toml
cp $WORK/history.jsonl $HOME/.claude/projects/p/history.jsonl
cp $WORK/history.jsonl $HOME/.codex/history.jsonl
# Without network policy: the agents' folders mounted read-only, their
# files and tokens copied as they are, with a warning naming the MCP
# servers whose secrets go too.
project plain
env CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-testfake
exec devvm --no-network-policy --no-git-protect --agents claude,codex
stderr '^devvm: claude_mcp_secrets_copied servers=srv vm=devvm-plain-[0-9a-f]{14}$'
stub_calls
stdout '^limactl clone --tty=false \S+ devvm-plain-[0-9a-f]{14} --mount-none --set \.mounts = \[\{"location":"\S*/plain","writable":true\},\{"location":"\S*/home/\.claude/skills","writable":false\},\{"location":"\S*/home/\.codex/prompts","writable":false\},\{"location":"\S*/data/devvm/tools","mountPoint":"/mnt/devvm-tools","writable":false\}\]'
exec tar -tf $STUB_DIR/sync.tar
stdout '^home/\.claude/settings\.json$'
stdout '^home/\.codex/auth\.json$'
stdout '^home/\.codex/config\.toml$'
stdout '^claude\.json$'
! stdout 'history|projects|^ca/'
exec tar -xOf $STUB_DIR/sync.tar home/.claude/settings.json
stdout testfake-settings
exec tar -xOf $STUB_DIR/sync.tar claude.json
stdout testfake-mcp
exec tar -xOf $STUB_DIR/sync.tar links
stdout '^\.claude/skills\n\.claude/agents\n\.claude/commands\n\.claude/output-styles\n\.claude/plugins\n\.codex/prompts\n\.codex/skills\n\z'
exec tar -xOf $STUB_DIR/sync.tar host_home
stdout '/home$'
exec tar -xOf $STUB_DIR/sync.tar vm
cmp stdout $WORK/vm-plain
# Without links, the hosts file for /etc/hosts is empty.
exec tar -xOf $STUB_DIR/sync.tar hosts
! stdout .
exec tar -xOf $STUB_DIR/sync.tar env
stdout '^export LANG=''en_US\.UTF-8''\nexport CLAUDE_CODE_OAUTH_TOKEN=''sk-ant-oat01-testfake''\n\z'
# A Claude token that isn't a setup-token token isn't sent; no token at
# all is named too.
project badtoken
env CLAUDE_CODE_OAUTH_TOKEN=not-a-setup-token
exec devvm --no-network-policy --no-git-protect --agents claude
stderr '^devvm: claude_token_invalid$'
exec tar -xOf $STUB_DIR/sync.tar env
! stdout CLAUDE_CODE_OAUTH_TOKEN
project notoken
env CLAUDE_CODE_OAUTH_TOKEN=
exec devvm --no-network-policy --no-git-protect --agents claude
stderr '^devvm: no_claude_token vm=devvm-notoken-[0-9a-f]{14}$'
# With network policy: devvm's CA, the VM's own placeholders, the logins'
# copies with placeholders (the host's files unchanged), and the
# settings files with their keys taken out; one devvm can't read that way
# isn't sent.
project net
env CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-testfake
exec devvm --network-policy --no-git-protect --agents claude,codex
stderr '^config_redacted file=\S*/home/\.claude/settings\.json keys=env\.ANTHROPIC_API_KEY vm=devvm-net-[0-9a-f]{14}$'
stderr '^devvm: config_not_copied file=\S*/home/\.codex/config\.toml vm=devvm-net-[0-9a-f]{14}$'
! stderr claude_mcp_secrets_copied
exec tar -tf $STUB_DIR/sync.tar
! stdout '^home/\.codex/config\.toml$'
exec tar -xOf $STUB_DIR/sync.tar ca/devvm-proxy.crt
cmp stdout $DATA/ca/ca.pem
exec sh -c 'tar -xOf "$STUB_DIR/sync.tar" home/.claude/settings.json home/.codex/auth.json claude.json env'
! stdout testfake
stdout devvm-redacted
stdout '"OPENAI_API_KEY": *"sk-devvm-proxy-managed'
! stdout projects
vm_name NET net
exec sh -c 'salt=$(sed -n "s/.*\"proxy_salt\": \"\([^\"]*\)\".*/\1/p" "$NET_dir/vm.json"); tar -xOf "$STUB_DIR/sync.tar" env | grep -Fx "export CLAUDE_CODE_OAUTH_TOKEN=''sk-ant-oat01-devvm-proxy-managed-$salt''"'
exec sh -c 'key=$(sed -n "s/.*\"proxy_key\": \"\([^\"]*\)\".*/\1/p" "$NET_dir/vm.json"); tar -xOf "$STUB_DIR/sync.tar" vm | grep -E "^DEVVM_PROXY=''http://devvm:$key@[0-9.]+:[0-9]+''$"'
cmp $HOME/.codex/auth.json $WORK/codex.json
cmp $HOME/.claude.json $WORK/claude.json
# A setup that fails says so, and leaves nothing of what it was sent.
mkdir $WORK/tmp $WORK/fails
cd $WORK/fails
env TMPDIR=$WORK/tmp
exec sh -c ': > "$STUB_DIR/shell-fails"'
! exec devvm --no-network-policy --no-git-protect --agents claude
stderr '^devvm: sync_failed vm=devvm-fails-[0-9a-f]{14}$'
exec sh -c 'ls -A "$TMPDIR"'
! stdout .
rm $STUB_DIR/shell-fails
# A shared tool the VM couldn't mount stops the launch, named by the VM's
# own tool list: a line naming any other file doesn't count, and with
# none left it's the setup that failed.
vm_name FAILS fails
exec bash $WORK/unmounted.sh '' ../../etc/passwd
! exec devvm --no-network-policy --no-git-protect
stderr '^devvm: tools_unmounted tools=claude [0-9][0-9.]* vm=devvm-fails-[0-9a-f]{14}$'
exec bash $WORK/unmounted.sh claude-9.9.9-000000000000.erofs
! exec devvm --no-network-policy --no-git-protect
stderr '^devvm: sync_failed vm=devvm-fails-[0-9a-f]{14}$'
rm $STUB_DIR/sync-fails-with
exec devvm --no-network-policy --no-git-protect
! stderr 'tools_unmounted|sync_failed'
-- unmounted.sh --
# The report tools.sh writes when it fails, listing each file named ('' for
# the VM's first tool image) after lines of its own, and with a last line
# cut short.
first=$(sed -n '/"files": \[/{n;s/.*"\([^"]*\)".*/\1/p;}' "$FAILS_dir/vm.json")
{
echo "devvm-tools: can't mount something"
printf '%s\n' "${first%% *} and more"
for f in "$@"; do echo "${f:-${first%% *}}"; done
printf '%s' "${first%% *}"
} > "$STUB_DIR/sync-fails-with"
-- settings.json --
{"env":{"ANTHROPIC_API_KEY":"testfake-settings"},"theme":"dark"}
-- claude.json --
{"theme":"dark","mcpServers":{"srv":{"command":"x","env":{"API_KEY":"testfake-mcp"}}},"projects":{"/p":{"history":["testfake-history"]}}}
-- codex.json --
{"OPENAI_API_KEY":"sk-testfake-codex","last_refresh":null}
-- config.toml --
note = """
testfake-multiline
"""
-- history.jsonl --
{"prompt":"testfake-history"}
-- vm-plain --
DEVVM_HOSTNAME='plain'
DEVVM_TZ='Europe/Paris'
DEVVM_LANG='en_US.UTF-8'
DEVVM_GIT_NAME='Test User'
DEVVM_GIT_EMAIL='[email protected]'
DEVVM_PROXY=''
DEVVM_NO_PROXY='localhost,127.0.0.1,::1'