Files
w3cjandClaude Opus 5.5 e0c4ed97e4 fix(vm): bound what devvm reads from a VM
The history export keeps at most 2 GiB: past it, devvm stops the VM's
command, removes the partial file and keeps no export. devvm history
export fails with history_too_large; devvm rm, devvm rm VM and a remake
warn with it and go on. opencode's import of devvm's logins (devvm agent
login) is read up to 1 MiB, then stopped and refused
(opencode_import_too_much). Each VM command adds at most 4 MiB to the
run log (Env.vmLog), then a "devvm: truncated" line.

agentsave's bounded writer is the one owner, in bounded.go: it takes in
vm's capped, and with a cancel it fails every write past its max, so a
command that keeps writing ends. Its buffer is a field: embedded, it gave
the writer bytes.Buffer's ReadFrom, which io.Copy (os/exec's copy of a
command's output) used in place of Write, so no cap held, agent save's
128 MiB included. vmssh's capped had the same flaw, fixed the same way.

Env.HistoryMax, and DEVVM_HISTORY_MAX in a test build, stand in for the
export's cap in tests. The stub guest's opencode-floods and
history-floods switches write without end.

Tests: TestBounded (io.Copy too), TestVMLog, TestExportTooLarge,
TestRemakeHistoryTooLarge, TestImportLoginsTooMuch, TestShellFloods,
vmssh's capped through io.Copy; history_import.txtar (an export that never
ends: export fails, rm deletes and warns, the run log truncated) and
agent_login.txtar (opencode's import floods).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 07:13:30 -07:00

96 lines
2.9 KiB
Go

package vm
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"github.com/devvmhq/devvm/internal/msg"
)
// bounded keeps, or passes on, at most max bytes. With a cancel, it then
// cancels and fails every write, so a writer that keeps going stops;
// without, it passes on its mark once and takes the rest.
func TestBounded(t *testing.T) {
writes := []string{"ab", "cd", "ef", "g"}
t.Run("cancels", func(t *testing.T) {
var log bytes.Buffer
cancelled := 0
b := &bounded{to: &log, max: 5, cancel: func() { cancelled++ }}
var errs []error
for _, s := range writes {
_, err := b.Write([]byte(s))
errs = append(errs, err)
}
if errs[0] != nil || errs[1] != nil || !errors.Is(errs[2], errBounded) || !errors.Is(errs[3], errBounded) {
t.Errorf("errors %v", errs)
}
if log.String() != "abcde" || b.Len() != 0 || !b.over || cancelled != 1 {
t.Errorf("log %q, kept %q, over %v, cancelled %d", log.String(), b.String(), b.over, cancelled)
}
})
t.Run("marks", func(t *testing.T) {
var log bytes.Buffer
b := &bounded{to: &log, max: 5, mark: "|cut"}
for _, s := range writes {
if n, err := b.Write([]byte(s)); n != len(s) || err != nil {
t.Fatal(n, err)
}
}
if log.String() != "abcde|cut" || !b.over {
t.Errorf("log %q, over %v", log.String(), b.over)
}
})
t.Run("keeps", func(t *testing.T) {
b := &bounded{max: 4}
for _, s := range writes {
if n, err := b.Write([]byte(s)); n != len(s) || err != nil {
t.Fatal(n, err)
}
}
if b.String() != "abcd" || !b.over {
t.Errorf("kept %q, over %v", b.String(), b.over)
}
})
// io.Copy, as os/exec copies a command's output, goes through Write:
// a bounded has no ReadFrom to read past its max.
t.Run("copied", func(t *testing.T) {
if _, ok := any(&bounded{}).(io.ReaderFrom); ok {
t.Fatal("bounded is an io.ReaderFrom")
}
b := &bounded{max: 4, cancel: func() {}}
if _, err := io.Copy(b, strings.NewReader(strings.Repeat("x", 1<<20))); !errors.Is(err, errBounded) || b.Len() != 4 {
t.Errorf("copied: kept %d, %v", b.Len(), err)
}
})
// Exactly max isn't over.
b := &bounded{max: 4}
_, _ = b.Write([]byte("abcd"))
if b.over {
t.Error("over at max")
}
}
// A VM command adds at most vmLogMax bytes to the run log, then the
// truncated mark; each command gets its own.
func TestVMLog(t *testing.T) {
var log bytes.Buffer
e := &Env{Reporter: &msg.Fake{Log: &log}}
w := e.vmLog()
chunk := []byte(strings.Repeat("x", 1<<16))
for range (vmLogMax / len(chunk)) + 3 {
if _, err := w.Write(chunk); err != nil {
t.Fatal(err)
}
}
if log.Len() != vmLogMax+len(vmLogCut) || !strings.HasSuffix(log.String(), vmLogCut) {
t.Errorf("log of %d bytes, ending %q", log.Len(), log.String()[log.Len()-40:])
}
log.Reset()
if _, err := e.vmLog().Write([]byte("next\n")); err != nil || log.String() != "next\n" {
t.Errorf("the next command's log %q, %v", log.String(), err)
}
}