Files
devvm/internal/vm/linediff_test.go
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

74 lines
2.7 KiB
Go

package vm
import (
"slices"
"strings"
"testing"
)
// lineDiff is a unified diff: hunks with three lines around each change,
// changes close together in one hunk, a new file and an emptied one, and
// a VM's control characters never reaching the terminal.
func TestLineDiff(t *testing.T) {
lines := func(n int, change map[int]string) string {
var b strings.Builder
for i := 1; i <= n; i++ {
if s, ok := change[i]; ok {
b.WriteString(s + "\n")
continue
}
b.WriteString("l" + string(rune('a'+i%26)) + "\n")
}
return b.String()
}
for _, tc := range []struct {
name string
before, after string
want []string
}{
{"same", "a\nb\n", "a\nb\n", nil},
{"new", "", "a\nb\n", []string{"@@ -0,0 +1,2 @@", "+a", "+b"}},
{"emptied", "a\n", "", []string{"@@ -1,1 +0,0 @@", "-a"}},
{"middle", "a\nb\nc\n", "a\nx\nc\n", []string{"@@ -1,3 +1,3 @@", " a", "-b", "+x", " c"}},
{"added", "a\nc\n", "a\nb\nc\n", []string{"@@ -1,2 +1,3 @@", " a", "+b", " c"}},
{
"apart", lines(20, nil), lines(20, map[int]string{2: "X", 18: "Y"}),
[]string{
"@@ -1,5 +1,5 @@", " lb", "-lc", "+X", " ld", " le", " lf",
"@@ -15,6 +15,6 @@", " lp", " lq", " lr", "-ls", "+Y", " lt", " lu",
},
},
{
"close", lines(12, nil), lines(12, map[int]string{2: "X", 8: "Y"}),
[]string{"@@ -1,11 +1,11 @@", " lb", "-lc", "+X", " ld", " le", " lf", " lg", " lh", "-li", "+Y", " lj", " lk", " ll"},
},
{"control", "a\n", "a\x1b[31mred\x07\n", []string{"@@ -1,1 +1,1 @@", "-a", `+a\u{1b}[31mred\u{7}`}},
{
"hidden", "a\n", string([]rune{0xe0041, 0x200b, 'b', 0x202e, 'c', 0xad}) + "\\u{1}\xff\td\n",
[]string{"@@ -1,1 +1,1 @@", "-a", `+\u{e0041}\u{200b}b\u{202e}c\u{ad}\\u{1}\x{ff}` + "\td"},
},
{"too long", strings.Repeat("a\n", diffMaxLines), "b\n", []string{" (10001 lines before and after, too long to show)"}},
{"too big", strings.Repeat("a", diffMaxBytes), "b", []string{" (0 lines before and after, too long to show)"}},
} {
t.Run(tc.name, func(t *testing.T) {
if got := lineDiff(tc.before, tc.after); !slices.Equal(got, tc.want) {
t.Errorf("lineDiff =\n%s\nwant\n%s", strings.Join(got, "\n"), strings.Join(tc.want, "\n"))
}
})
}
}
// Files too big to compare line by line show one whole as taken out and
// the other as added.
func TestLineDiffTooBig(t *testing.T) {
var a, b []string
for i := range 1100 {
a = append(a, "a"+string(rune('0'+i%10)))
b = append(b, "b"+string(rune('0'+i%10)))
}
got := lineDiff(strings.Join(a, "\n")+"\n", strings.Join(b, "\n")+"\n")
if len(got) != 1+2200 || got[0] != "@@ -1,1100 +1,1100 @@" || got[1] != "-a0" || got[1101] != "+b0" {
t.Errorf("lineDiff = %d lines, %v", len(got), got[:3])
}
}