Files
devvm/testdata/script/sync.txtar
T
w3cjandClaude Opus 5.5 d8a7d4de7f refactor(state): a folder per VM, keyed by its project folder's hash
Every file devvm keeps for a project VM is in one folder, vms/<hash>/,
where <hash> is the 14-hex hash of the project folder's path that the VM's
name ends with: its state, its own rules (policy), its backend's files
(backend/: record.json, key, known_hosts, proxy, ports and the port
forwarder's ports.*), what its runs leave (run/: proxy.*, sandbox.sb,
started, lifeline.pid, secrets.*, links.*) and its logs (logs/:
network.jsonl, proxy.log, links.log). Build VMs keep the same layout in
run/builds/<name>/. The top-level run/ holds only what isn't one VM's or
must be short: the hashed control sockets, links' link-<hash>.sock/.ctl,
the QEMU wrapper and the ports lock.

A folder's VM is found by its path's hash (state.ReadHash), with the name
and backend its state records, so devvm --backend docker then devvm in
the same folder opens the one VM, and a backend's name limit applies to
new VMs only. The state records the VM's name and its ssh alias, chosen
once at creation (sshconfig.ChooseAlias) and never recomputed. Links'
targets match recorded names, aliases and folders (project.Targets).

One teardown (vmFolders.teardown) replaces Lifecycle.forget,
undoCreate's own list, Leftovers.Forget's, Proxy.Forget and space's
leftoverFiles: it stops the proxy and link session, then the pieces
outside the folder (clone remote or .git locks, other VMs' links and the
group, host keys, the backend's leftovers), removes the folder, and
writes the ssh config. Each backend's Delete and Forget remove the VM's
backend/ folder (vmssh.Forwards.Forget). A new VM made where an earlier
one's folder was left clears it first. devvm space counts an orphan's
whole folder as leftover state and the other VMs' logs as network logs.

Path builders move into paths: Dirs.VMDir, Dirs.VM and paths.VM's files
(secrets' BindingsFile/PolicyFile, netpolicy's run, EventLog, ProxyLog,
LinkList, VMPolicyFile, Lima's profileFile, startedFile, forwards.file,
vmssh.Forwards.file, links' LinksLog/linksPID, Docker's vmFile and
lifeline), ToolLockConfig, ToolLockChecked and ToolsCheckLog; the
user's cache folder for Lima's and smolvm's caches is Dirs.UserCache,
from HOME, not os.UserCacheDir. Dirs.VMState, LimaVMs, SmolvmVM and
DockerVM go. vmname.HashOf and IsHash, project.Hash.

The proxy takes the links' sockets' folder (-link-sockets, devvm's run
folder) apart from the links' list, which is in the VM's run folder.

Tests: a VM made and removed (rm, gc --orphans) leaves nothing under
vms/, and with network policy has no file named for it outside its
folder (TestTeardownLeavesNothing); a new VM over an old folder starts
clean; lookup finds a VM made on another backend with a long folder name
and makes no second one (TestLookupOtherBackend, and in docker.txtar);
the alias stays put when a second VM shares the folder name
(TestChooseAlias, ssh.txtar); every socket fits under a 35-character
home with the longest VM name (TestSocketsFit); space counts an
orphan's whole folder (TestLeftoverFolders); paths.VM's files; state by
hash and name; the proxy refuses a socket in the list's own folder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 08:16:39 -07:00

144 lines
6.3 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=//p" "$NET_dir/state"); 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=//p" "$NET_dir/state"); 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 's/^tools=//p' "$FAILS_dir/state")
{
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'