Files
devvm/testdata/script/agent_save.txtar
T
w3cjandClaude Opus 5.5 0a28f98f0a feat(agents): devvm agent save, a VM's agent settings into devvm's store
devvm agent save [AGENT...] reads the folder's VM's agents' settings,
shows what differs from devvm's agent store and, after a yes, saves them
there for every VM: import's other direction. It always asks, with no
flag that answers (RefusedWith), so without a terminal or with
--no-input it shows the diff and is refused; devvm starts a stopped VM
for it.

What it reads (a fixed list, the agents asked for only): agentFilesOf
(Claude Code's settings.json and CLAUDE.md, Codex's config.toml and
AGENTS.md), agentDirsOf (skills, agents, commands, output-styles,
plugins with marketplaces; prompts, skills), opencode's config folder's
entries but node_modules and lock files, and ~/.claude.json's subset
(claudeTopKeys, reduced by jq in the VM and again on the host, never its
per-project entries). Never a login (.credentials.json, auth.json) or
history. The guest script sends it as a tar stream; a folder that's the
store's own mount (the link to it) is no change.

Every byte from the VM is untrusted, checked on the host (readSent):
- the stream bounded: 128 MiB, the command cancelled past it; its
  stderr into the run log, at most 1 MiB
- at most 20,000 entries, a file at most 16 MiB (its header first)
- paths relative, clean, valid UTF-8, only characters a terminal shows
  (no control, format, bidi or zero-width), each once, only in the
  list, each inside a folder the stream has (nothing below a link):
  anything else saves nothing
- only files, folders and links; other kinds left out with a warning
- links never written: one inside its settings folder to a file there,
  by relative paths through real folders, at most 8 hops, becomes a
  copy; any other is left out, the store's copy kept
- modes normalized: 600, or 700 when executable; no set-id bits
- every file written with state.WriteFileMode; folders replaced in
  place (pruneStore through an os.Root), files the VM lacks kept
- every settings file parsed whole (Redact's decode), left out when it
  doesn't parse
- placeholders never saved: agentlogin.Restore puts the store's value
  back only where the VM's copy (the store's file redacted again, or
  for .claude.json the VM's salted claudeVM copy, as every VM gets)
  had the same placeholder at the same path and its table and the one
  above are unchanged; else the file is left out. A placeholder in any
  other file leaves it out; opencode's withheld files, which no VM
  gets, are left out.

The diff (data output): each file's lines against the store's, settings
files and .claude.json redacted on both sides so it never prints a
secret, a folder's A/M/D list then each added or changed file's diff, a
mode change line, every character a terminal wouldn't show as \u{HEX},
binary files named only, a file over 1 MiB or 10,000 lines and lines
past 20,000 as counts. Then agent_save_runs: every VM runs the hooks,
MCP servers' commands and plugins saved.

Security review (security-audit skill, guidance mode, an independent
read): nothing reaches outside the store, follows a link, or sends a
store secret to another host. Findings, all fixed here:
- folder files showed only as A/M/D names, so a changed hook or
  script went in unseen: their diffs are shown
- invisible Unicode (tag characters, zero-width) passed
  msg.Printable into the diff: shownLine escapes every non-graphic
  character
- names with bidi or format characters spoofed the diff's headers:
  refused in entryName, headers escaped too
- the diff could use GBs of memory on 16 MiB files of newlines: per
  file and total caps
- Restore's rewritten JSON/TOML hid a change among reformatted lines:
  both sides are diffed as rewritten
- the VM's stderr went into the run log without limit: 1 MiB
- quadratic work over many opencode folders: entries indexed by folder
- an execute-bit change showed no line: a mode line
Found in my own review: the diff printed the store's secrets in
context lines (now redacted), and a VM could replace opencode files
it never got, such as a .env (now left out). With captured logins,
HasPlaceholder also knows the JWT-shaped placeholders (fakeJWT's
devvm-placeholder signature, now jwtMark), so a captured token's
placeholder copied into a settings file is never saved; the save never
writes Codex's or opencode's auth.json or Claude Code's
.credentials.json, which the capture keeps in the logins store.

- agentlogin: Restore, PlaceholderError, HasPlaceholder, ClaudeSubset,
  ClaudeTopKeys; Redact and ClaudeCopy share decode/encode
- vm: AgentSave (agentsave.go), readSent, listedPath, resolve,
  agentSaveScript (agentsave_read.go), lineDiff, shownLine
  (linediff.go); opencodeFolder/opencodeFile from agentDirs/agentFiles
- cli: devvm agent save, with --here/--parent
- messages: agent_save_no_agent, agent_save_none, agent_save_nothing,
  agent_save_ask, agent_save_runs, agent_save_refused, nothing_saved,
  agent_settings_saved, agent_save_unsafe, agent_save_too_much,
  agent_save_link_left_out, agent_save_kind_left_out,
  agent_save_unparsed, agent_save_placeholder, agent_save_withheld,
  agent_save_not_read, agent_save_failed
- stub: agent-save.tar, the VM's stream
- tests: agentsave_test.go, linediff_test.go, restore_test.go,
  testdata/script/agent_save.txtar; e2e project (refused, then saved,
  never the login or a link out) and network (MCP header put back,
  refused once its URL changed)
- docs: coding agents (saving a VM's settings), commands, attack
  vectors, internals network (saving the copies back), contributing,
  the cli skill's tree, CLI reference

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 15:17:37 -07:00

107 lines
3.9 KiB
Plaintext

# devvm agent save: the folder's VM's agents' settings read from the VM
# (the stub's agent-save.tar stands for its stream), shown as a diff and a
# list of a folder's files, then saved into devvm's store after a yes. It
# always asks: without a terminal or with --no-input it's refused, and the
# store stays as it is. A stream with anything outside the agents'
# settings saves nothing.
env COPYFILE_DISABLE=1
# Its arguments are agents; without a VM there's nothing to save from.
! exec devvm agent save nope
stderr '^devvm: arg_not_choice choices=claude, codex, opencode command=devvm agent save given=nope$'
project agent
! exec devvm agent save
stderr '^devvm: no_vm dir=\S*/agent$'
# A VM with Claude Code: an agent it doesn't have is warned about, and
# an empty stream is nothing to save.
exec devvm --network open --no-git-protect --agents claude
vm_name AGENT agent
stub_reset
exec devvm agent save codex
stderr '^devvm: agent_save_none agent=codex vm='$AGENT'$'
exec devvm agent save
stdout '^==> agent_save_nothing vm='$AGENT'$'
stub_calls
stdout '\.devvm-save'
stdout 'jq --argjson k'
# The VM's settings: without a terminal, the diff and the folder's files,
# then the refusal; nothing saved.
mkdir $CFG/agents/claude/.claude
cp $WORK/old-settings.json $CFG/agents/claude/.claude/settings.json
exec tar -cf $STUB_DIR/agent-save.tar -C $WORK/vm .devvm-save .claude/settings.json .claude/skills
! exec devvm agent save
stdout '^--- \S*/config/devvm/agents/claude/\.claude/settings\.json$'
stdout '^\+\+\+ '$AGENT':~/\.claude/settings\.json$'
stdout '^- "theme": "dark"$'
stdout '^\+ "theme": "light"$'
stdout '^~/\.claude/skills in '$AGENT', into \S*/config/devvm/agents/claude/\.claude/skills:$'
stdout '^ A s/SKILL\.md$'
stdout '^\+\+\+ '$AGENT':~/\.claude\.json$'
stderr '^devvm: agent_save_refused$'
! stderr agent_save_ask
cmp $CFG/agents/claude/.claude/settings.json $WORK/old-settings.json
! exists $CFG/agents/claude/.claude/skills
! exec devvm agent save --no-input
stderr '^devvm: agent_save_refused$'
# In a terminal it says what saved settings run, and asks: no saves
# nothing, yes saves them, with ~/.claude.json's subset, never its
# projects.
ttyin -stdin $WORK/no.txt
exec devvm agent save
stderr '^agent_save_runs$'
stderr 'agent_save_ask vm='$AGENT' \[y/N\] '
stdout '^==> nothing_saved$'
cmp $CFG/agents/claude/.claude/settings.json $WORK/old-settings.json
ttyin -stdin $WORK/yes.txt
exec devvm agent save claude
stdout '^==> agent_settings_saved agent=claude dir=\S*/config/devvm/agents/claude vm='$AGENT'$'
cmp $CFG/agents/claude/.claude/settings.json $WORK/vm/.claude/settings.json
cmp $CFG/agents/claude/.claude/skills/s/SKILL.md $WORK/vm/.claude/skills/s/SKILL.md
grep '"theme": "light"' $DATA/logins/claude/.claude.json
! grep projects $DATA/logins/claude/.claude.json
# Saved, it's the same as the store's.
exec devvm agent save
stdout '^==> agent_save_nothing vm='$AGENT'$'
# A stream with a path outside the agents' settings saves nothing.
cp $WORK/new-settings.json $WORK/vm/.claude/settings.json
exec tar -cf $STUB_DIR/agent-save.tar -C $WORK/vm .devvm-save .claude/settings.json .bashrc
env DEVVM_PIPED_ANSWERS=1
stdin $WORK/yes.txt
! exec devvm agent save
env DEVVM_PIPED_ANSWERS=
stderr '^devvm: agent_save_unsafe path=\.bashrc vm='$AGENT'$'
! stdout '^\+\+\+'
cmp $CFG/agents/claude/.claude/settings.json $WORK/old-light.json
# The VM's tar failing is a failed read.
exec sh -c 'echo .devvm-save > "$STUB_DIR/shell-fails-on"'
! exec devvm agent save
stderr '^devvm: agent_save_not_read vm='$AGENT'$'
rm $STUB_DIR/shell-fails-on
-- old-settings.json --
{"theme": "dark"}
-- old-light.json --
{"theme": "light"}
-- new-settings.json --
{"theme": "blue"}
-- vm/.claude/settings.json --
{"theme": "light"}
-- vm/.claude/skills/s/SKILL.md --
# a skill made in the VM
-- vm/.bashrc --
echo hi
-- vm/.devvm-save/home --
/home/u.linux
-- vm/.devvm-save/claude.json --
{"theme": "light", "projects": {"/p": {"history": ["testfake-history"]}}}
-- no.txt --
n
-- yes.txt --
y