Files
magpie/internal/agent/preview.go
T
yetoneandFake Trader 0cabaaf243 agent: a portable VS Code on a Mac is wired in its code-portable-data folder (#4151)
faketrader runs VS Code portable on a Mac: /usr/local/bin/code links into
/Applications/Code/Visual Studio Code.app, and the build reads
/Applications/Code/code-portable-data/user-data/User. magpie detected it
but wrote ~/Library/Application Support/Code/User, which that build never
reads. Their patch on #4151 is the base; this takes its rule and makes it
VS Code's own (configurePortable in src/bootstrap-node.ts), on every OS:

- A build is portable when its product.json has no `target` (an
  installer's build has one) and its data folder exists: on a Mac
  product.json's `portable`, else `<applicationName>-portable-data`,
  beside the .app; on Windows and Linux `data` beside the install, also
  for a build that updates into `<install>/<version>/resources/app`.
- The build is the one the launcher on PATH is, through a Scoop shim and
  every symlink. A launcher whose product.json names another editor (a
  fork's `code`) isn't the build's. With no launcher of its own, a Mac
  reads the app folders and Spotlight and takes a portable bundle only
  when every bundle found is that one; beside an installed bundle, which
  one runs is unknown and nothing moves.
- VS Code, Insiders and VSCodium each go by their own bundle and launcher.
- User and extensions come from one place (vscodeDirs): connect, Sync,
  profiles and Disconnect use the portable User folder, and Zoo Code is
  found by its extension in <data>/extensions and writes its import file
  there. Windows' Zoo Code under Scoop looked in ~/.vscode/extensions.
- Disconnect's preview is refused for a file in a portable data folder:
  the preview's child keeps PATH, and its launcher would reach the build's
  real files, not the copy. That also covers Scoop under home on Windows,
  which main previewed.
- An installed build with no portable folder keeps the OS's folder and
  ~/.vscode/extensions, unchanged.

#3260 and #4009 (Windows): 509d8e7d1 (ggbdpq) already moved Scoop's
`current\data`; this adds the target check, the versioned layout, the
Windows Zoo Code folder and the preview refusal above.

Tests (fixtures under t.TempDir, never /Applications or ~/Library; app
folders and Spotlight stubbed): TestVSCodeMacPortableDirs (stable,
Insiders, product's `portable`, no data folder, Windows' `data` name,
another editor, another channel's folder, unreadable product, installer
target), TestVSCodeMacPortableLinkChain (three links, folders with
spaces), TestVSCodeMacPortableWiring, TestVSCodeMacPortableWithoutLauncher,
TestVSCodeMacPortableLauncherPicks, TestVSCodeMacPortableForkLauncher,
TestVSCodePortableProduct (Windows/Linux: target, fork product, versioned
install, Zoo Code, Scoop preview).

Broken by hand, each went red and was restored: main's rule on darwin
(6 Mac tests FAIL, standard folder used), target ignored (Dirs, Product),
no preview refusal (Wiring, Product), first bundle wins
(WithoutLauncher), fork launcher decides (ForkLauncher), Zoo Code on
home's extensions (3 FAIL). On origin/main the new tests don't compile
(vscodeDirs is new); break #1 is main's Mac behaviour.

Run:
- mote (macOS 27), full suite at this change on 479476784:
  go test -tags nogui -timeout 45m -count=1 ./... : 85 packages ok
  (internal/agent 405s, gateway 832s, provider 251s), internal/gui red on
  two filed tests this change doesn't reach: TestSettingsSaveKeepsWhat-
  ItDoesNotSend (SearchAll, #4514) and TestColdTurnsTaken (#4510).
  Rebased onto 90c02326b (gui takeover, sessions save, kiro min; nothing
  in internal/agent): go build, go vet of agent/gui/sessions/provider and
  go test of internal/agent, internal/sessions, internal/provider again
  at the pushed head: ok (agent 352s, sessions 110s, provider 228s).
- mote (macOS 27): go test -tags nogui ./internal/agent -run
  'TestVSCode|TestZooCode' ok; gofmt -l clean; go vet ./...,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., GOOS=linux
  go build -tags nogui, GOOS=windows go build ok.
- win-run (Windows 11) and wsl-run (Ubuntu 24.04): the agent tests above
  plus Preview, ok.
- Real use on mote, sandbox HOME, PATH=$SB/bin:/usr/bin:/bin: VS Code
  1.144.0 darwin-arm64 from update.code.visualstudio.com unpacked to
  "$SB/Apps With Space/Code/Visual Studio Code.app", `code` linked
  bin/code -> "link one/code" -> the bundle's bin/code. Without
  code-portable-data, `magpie agents` lists ~/Library/Application
  Support/Code/User/settings.json; with it, ".../code-portable-data/
  user-data/User/settings.json". The real `code --install-extension
  zoocodeorganization.zoo-code` put Zoo Code 3.94.0 in
  code-portable-data/extensions (no ~/.vscode made), and magpie then
  listed Zoo Code at the portable User folder. `magpie vscode
  deepseek/deepseek-chat` wrote settings.json and chatLanguageModels.json
  there, `magpie vscode default` took magpie out of both, and Zoo Code's
  connect wrote its import file under the portable globalStorage and its
  disconnect removed the setting; ~/Library/Application Support/Code was
  never made.
- Real use on xiaomi-gpu (Windows 11), sandbox USERPROFILE/APPDATA,
  PATH=<install>\bin;System32: VS Code's win32-x64 ZIP (its versioned
  layout, <install>\2c33b50b16\resources\app) unpacked to "Apps With
  Space\VSCode". Without data, `magpie agents` lists ~\AppData\Roaming\
  Code\User\settings.json; with data, <install>\data\user-data\User\
  settings.json. The real code.cmd --install-extension put Zoo Code in
  data\extensions (no ~\.vscode), magpie then listed Zoo Code there,
  and connect/disconnect wrote and cleaned that User folder; %APPDATA%\Code
  was never made.

Not tried / not handled: VS Code's window wasn't opened to see the model
picker (headless ssh). VSCODE_PORTABLE isn't read: it is the editor's
environment, not magpie's. A Windows or Linux portable build with no
launcher on PATH isn't found (there is no standard place to look). A group
an older magpie wrote in the OS's folder for a portable build stays there;
connecting again writes the portable one. A Claude Code or Codex extension
in a portable build doesn't keep that CLI from showing as gone: #4672.

Co-authored-by: Fake Trader <9635792+faketrader@users.noreply.github.com>
2026-11-01 11:55:33 +08:00

538 lines
16 KiB
Go

package agent
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/yetone/magpie/internal/agentenv"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/provider"
)
// Before an agent is disconnected the user is shown what changes in its
// files, line by line (the owner's 断开 design: 删除、改回、保留), not a
// sentence about it. What Disconnect does depends on the stash, the record
// and each agent's own unwiring, so rather than a second account of it that
// could say otherwise, magpie runs Disconnect itself on a copy: the agent's
// config folder and magpie's own files under a temporary home, in a process
// of its own (DryRunArg), and compares the copy with the files as they are.
// DryRunArg is the command line's first word for the process that
// disconnects an agent on a copy of its files (main.go).
const DryRunArg = "agent-disconnect-dry-run"
const (
dryProvidersVar = "MAGPIE_DRY_PROVIDERS"
dryProvidersFile = ".magpie-dry-providers"
)
// dryProviders is, in the process DryRun runs, the ids of the providers the
// magpie that asked for the preview has on.
var dryProviders map[string]bool
// disconnectDryRun is confined to the preview subprocess. Its temporary
// HOME isolates files, but not a running agent reached through its CLI.
var disconnectDryRun bool
// DryRun disconnects an agent in the process DisconnectPreview starts, on
// the copy under its temporary home.
func DryRun(id string) error {
disconnectDryRun = true
if p := os.Getenv(dryProvidersVar); p != "" {
b, err := os.ReadFile(p)
if err != nil {
return err
}
dryProviders = map[string]bool{}
for _, id := range strings.Split(string(b), "\n") {
if id != "" {
dryProviders[id] = true
}
}
}
a, err := Find(id)
if err != nil {
return err
}
return a.Disconnect()
}
// FileChange is how disconnecting changes one of the agent's files.
type FileChange struct {
Path string `json:"path"` // as the user knows it, ~ for home
Removed bool `json:"removed,omitempty"` // the file goes
Created bool `json:"created,omitempty"` // the file is made
Lines []LineDiff `json:"lines,omitempty"`
}
// LineDiff is a line of a file disconnecting changes: Op is "-" for a line
// taken out, "+" for one put in, "~" for a line whose value goes back to
// another (Was is the line now).
type LineDiff struct {
Op string `json:"op"`
Text string `json:"text"`
Was string `json:"was,omitempty"`
}
// DisconnectPreview is what Disconnect would change in the agent's files,
// found by running it on a copy. An error is a preview not to be had (the
// agent's files outside home, a WSL agent): the dialog then says it in
// words.
func DisconnectPreview(a *Agent, exe string) ([]FileChange, error) {
if a.Native != nil {
plan, err := a.Native.Disconnect()
if err != nil {
return nil, err
}
return plan.Preview(), nil
}
if a.WSL != "" || a.Path == "" {
return nil, errors.New("no preview for this agent")
}
if appdir.Portable() != "" {
return nil, errors.New("no preview for a portable magpie")
}
// A portable VS Code's folder is found through its launcher, on the
// PATH the preview's process keeps, not through home: that process
// would disconnect the build's own files, not the copy (#4151).
if vscodePortableFile(a.Path) {
return nil, errors.New("no preview for a portable VS Code")
}
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
rel := func(p string) (string, bool) {
r, err := filepath.Rel(home, p)
return r, err == nil && r != "." && !strings.HasPrefix(r, "..")
}
dir := filepath.Dir(a.Path)
dirRel, ok := rel(dir)
cfg := appdir.Config()
cfgRel, ok2 := rel(cfg)
if !ok || !ok2 {
return nil, errors.New("the agent's files are outside home")
}
sweepPreviews()
tmp, err := os.MkdirTemp("", "magpie-preview-")
if err != nil {
return nil, err
}
defer removePreview(tmp)
tmp, _ = filepath.EvalSymlinks(tmp)
// the agent's folder: its files, copied; its folders aren't its config
before := map[string][]byte{}
if err := copyTop(dir, filepath.Join(tmp, dirRel), before, false); err != nil {
return nil, err
}
// magpie's: its files copied (the stash, the record, the providers),
// its folders (plugins) only read, so linked
if err := copyTop(cfg, filepath.Join(tmp, cfgRel), nil, true); err != nil {
return nil, err
}
// magpie's providers as this magpie has them: some come from another
// agent's sign-in (Codex's auth.json) the copy doesn't hold, and a
// model of theirs is magpie's all the same (dryProviders)
var ids []string
for _, p := range provider.All() {
if p.On() {
ids = append(ids, p.ID)
ids = append(ids, p.Was...)
}
}
held := filepath.Join(tmp, dryProvidersFile)
if err := os.WriteFile(held, []byte(strings.Join(ids, "\n")), 0o600); err != nil {
return nil, err
}
env := append(previewEnv(home, tmp), dryProvidersVar+"="+held)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
cmd := proc.CommandContext(ctx, exe, DryRunArg, a.ID)
cmd.Env = env
var out bytes.Buffer
cmd.Stdout, cmd.Stderr = &out, &out
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("dry run: %v: %s", err, strings.TrimSpace(out.String()))
}
// what the agent's folder holds now against what it held
var changes []FileChange
copied := filepath.Join(tmp, dirRel)
after := map[string][]byte{}
entries, _ := os.ReadDir(copied)
for _, e := range entries {
if e.Type().IsRegular() {
if b, err := os.ReadFile(filepath.Join(copied, e.Name())); err == nil {
after[e.Name()] = b
}
}
}
shown := func(name string) string { return "~/" + filepath.ToSlash(filepath.Join(dirRel, name)) }
// the agent's own file first, then the others by name
names := []string{filepath.Base(a.Path)}
for n := range before {
if n != names[0] {
names = append(names, n)
}
}
for n := range after {
if _, ok := before[n]; !ok {
names = append(names, n)
}
}
sortTail(names)
for _, n := range names {
was, had := before[n]
now, has := after[n]
switch {
case had && !has:
changes = append(changes, FileChange{Path: shown(n), Removed: true})
case !had && has:
changes = append(changes, FileChange{Path: shown(n), Created: true, Lines: diffLines("", string(now))})
case had && has && !bytes.Equal(was, now):
if l := diffLines(string(was), string(now)); len(l) > 0 {
changes = append(changes, FileChange{Path: shown(n), Lines: l})
}
}
}
return changes, nil
}
// previewStale is how old a sandbox in the temporary dir must be for the
// sweep to take it: a live preview lasts seconds (the dry run is bounded
// at 20 seconds), so an hour-old one is a sandbox an earlier magpie was
// killed holding (#3136).
const previewStale = time.Hour
// sweepPreviews removes sandboxes an earlier magpie was killed holding,
// as sweepCursorSnaps does for its magpie-cursor-* copies.
func sweepPreviews() {
old, _ := filepath.Glob(filepath.Join(os.TempDir(), "magpie-preview-*"))
for _, d := range old {
if fi, err := os.Stat(d); err == nil && fi.IsDir() && time.Since(fi.ModTime()) > previewStale {
removePreview(d)
}
}
}
// removePreview removes a sandbox, retrying while processes of the dry run
// are still letting go of files in it (Windows keeps a file locked until
// the last holder is out), and says so when it still could not.
var previewRemoveAll = os.RemoveAll
func removePreview(dir string) error {
var err error
for try := 0; try < 5; try++ {
if err = previewRemoveAll(dir); err == nil {
return nil
}
time.Sleep(time.Duration(try+1) * 100 * time.Millisecond)
}
log.Printf("agent: removing disconnect-preview sandbox %s: %v", dir, err)
return err
}
// sortTail sorts all but the first name.
func sortTail(names []string) {
for i := 2; i < len(names); i++ {
for j := i; j > 1 && names[j] < names[j-1]; j-- {
names[j], names[j-1] = names[j-1], names[j]
}
}
}
// copyTop copies a folder's files into to, keeping what they held in
// before when it is given; with link, its folders are linked there.
func copyTop(from, to string, before map[string][]byte, link bool) error {
if err := os.MkdirAll(to, 0o700); err != nil {
return err
}
entries, err := os.ReadDir(from)
if errors.Is(err, fs.ErrNotExist) {
return nil
}
if err != nil {
return err
}
const most = 16 << 20
for _, e := range entries {
p := filepath.Join(from, e.Name())
st, err := os.Stat(p) // a linked file is its target
if err != nil {
continue
}
if st.IsDir() {
if link {
os.Symlink(p, filepath.Join(to, e.Name()))
}
continue
}
if !st.Mode().IsRegular() || st.Size() > most || notConfig(e.Name()) {
continue
}
b, err := os.ReadFile(p)
if err != nil {
continue
}
if err := os.WriteFile(filepath.Join(to, e.Name()), b, 0o600); err != nil {
return err
}
if before != nil {
before[e.Name()] = b
}
}
return nil
}
// notConfig is a file no agent keeps its settings in: a database, a log, a
// history, which can be large and which Disconnect never writes.
func notConfig(name string) bool {
for _, ext := range []string{".sqlite", ".sqlite-wal", ".sqlite-shm", ".db", ".db-wal", ".db-shm", ".jsonl", ".log"} {
if strings.HasSuffix(name, ext) {
return true
}
}
return false
}
// previewEnv is this process's environment with home, and the folders under
// it, moved to the copy.
func previewEnv(home, tmp string) []string {
moved := map[string]bool{"HOME": true, "USERPROFILE": true, "XDG_CONFIG_HOME": true, "XDG_CACHE_HOME": true, "XDG_DATA_HOME": true, "XDG_STATE_HOME": true, "APPDATA": true, "LOCALAPPDATA": true}
for _, k := range agentenv.Vars {
moved[k] = true
}
var out []string
for _, kv := range os.Environ() {
k, v, _ := strings.Cut(kv, "=")
if !moved[k] {
out = append(out, kv)
continue
}
if k == "HOME" || k == "USERPROFILE" {
continue
}
// a folder under home is the same one under the copy's; one
// elsewhere isn't copied, so it is left out
if r, err := filepath.Rel(home, v); err == nil && !strings.HasPrefix(r, "..") {
out = append(out, k+"="+filepath.Join(tmp, r))
}
}
// The test binary recognizes this marker and keeps the fixture HOME
// instead of creating a second isolated home. Production binaries ignore
// the variable.
return append(out, "HOME="+tmp, "USERPROFILE="+tmp, "TMPDIR="+tmp, "MAGPIE_TEST_SANDBOX="+tmp)
}
// diffLines is the lines that differ between two texts, in the order of the
// new one: a line taken out, one put in, and a line whose key stays with its
// value changed (model = "x" → model = "y") as one going back. Values that
// read as secrets are masked. Between the lines both start and end with,
// texts too far apart to compare line by line (OpenCode's table of
// thousands of magpie models taken out) keep the lines of the new one
// found in order in the old.
func diffLines(a, b string) []LineDiff {
x, y := splitLines(a), splitLines(b)
pre := 0
for pre < len(x) && pre < len(y) && x[pre] == y[pre] {
pre++
}
suf := 0
for suf < len(x)-pre && suf < len(y)-pre && x[len(x)-1-suf] == y[len(y)-1-suf] {
suf++
}
keepX, keepY := make([]bool, len(x)), make([]bool, len(y))
for i := 0; i < pre; i++ {
keepX[i], keepY[i] = true, true
}
for i := 1; i <= suf; i++ {
keepX[len(x)-i], keepY[len(y)-i] = true, true
}
mx, my := x[pre:len(x)-suf], y[pre:len(y)-suf]
if kx, ky, ok := sameLines(mx, my, 4000); ok {
copy(keepX[pre:], kx)
copy(keepY[pre:], ky)
} else if len(mx)*len(my) <= 50_000_000 {
// each line of the one, in order, kept where the other has it
// next: what is left of a file once a block is taken out of it
// (past that, all of it taken out and put in)
i := 0
for j := range my {
for k := i; k < len(mx); k++ {
if mx[k] == my[j] {
keepX[pre+k], keepY[pre+j] = true, true
i = k + 1
break
}
}
}
}
var out []LineDiff
var gone, came []string
flush := func() {
// a line taken out and one put in under the same key, as deep in
// the file, are one going back (Claude's own "model" with
// modelPicker's taken out, not one of the picker's)
for _, c := range came {
k := lineKey(c)
paired := false
for i, g := range gone {
if k != "" && lineKey(g) == k && indent(g) == indent(c) {
out = append(out, LineDiff{Op: "~", Text: mask(c), Was: mask(g)})
gone = append(gone[:i], gone[i+1:]...)
paired = true
break
}
}
if !paired {
out = append(out, LineDiff{Op: "+", Text: mask(c)})
}
}
for _, g := range gone {
out = append(out, LineDiff{Op: "-", Text: mask(g)})
}
gone, came = nil, nil
}
i, j := 0, 0
for i < len(x) || j < len(y) {
switch {
case i < len(x) && j < len(y) && keepX[i] && keepY[j]:
flush()
i++
j++
case i < len(x) && !keepX[i]:
if strings.TrimSpace(x[i]) != "" {
gone = append(gone, x[i])
}
i++
default:
if strings.TrimSpace(y[j]) != "" {
came = append(came, y[j])
}
j++
}
}
flush()
// a bracket or brace left alone on its line says nothing
var kept []LineDiff
for _, l := range out {
if t := strings.Trim(strings.TrimSpace(l.Text), "{}[](),"); t == "" {
continue
}
kept = append(kept, l)
}
return kept
}
// sameLines is which lines of x and of y a shortest edit keeps (Myers'
// diff: time and memory grow with the edits, not the files — an agent's
// config can be 30,000 lines, and magpie changes a few). Not ok past most
// edits.
func sameLines(x, y []string, most int) (keepX, keepY []bool, ok bool) {
n, m := len(x), len(y)
if most > n+m {
most = n + m
}
off := most + 1
v := make([]int, 2*off+1)
// trace[d] is v as step d found it, for k in -d-1..d+1
var trace [][]int
get := func(d, k int) int { return trace[d][k+d+1] }
for d := 0; d <= most; d++ {
trace = append(trace, append([]int(nil), v[off-d-1:off+d+2]...))
for k := -d; k <= d; k += 2 {
var xi int
if k == -d || (k != d && v[off+k-1] < v[off+k+1]) {
xi = v[off+k+1]
} else {
xi = v[off+k-1] + 1
}
yi := xi - k
for xi < n && yi < m && x[xi] == y[yi] {
xi++
yi++
}
v[off+k] = xi
if xi < n || yi < m {
continue
}
// back from the end, marking the diagonals walked
keepX, keepY = make([]bool, n), make([]bool, m)
xi, yi = n, m
for e := d; e >= 0; e-- {
k := xi - yi
var pk int
if k == -e || (k != e && get(e, k-1) < get(e, k+1)) {
pk = k + 1
} else {
pk = k - 1
}
px := get(e, pk)
py := px - pk
for xi > px && yi > py && xi > 0 && yi > 0 {
keepX[xi-1], keepY[yi-1] = true, true
xi--
yi--
}
xi, yi = px, py
}
return keepX, keepY, true
}
}
return nil, nil, false
}
func splitLines(s string) []string {
if s == "" {
return nil
}
return strings.Split(strings.TrimRight(strings.ReplaceAll(s, "\r\n", "\n"), "\n"), "\n")
}
// lineKey is what a line sets: TOML's and .env's key before =, JSON's and
// YAML's before :, "" for a line that sets nothing.
var keyRe = regexp.MustCompile(`^\s*(?:export\s+)?"?([A-Za-z0-9_.\-]+)"?\s*[:=]`)
func lineKey(l string) string {
if m := keyRe.FindStringSubmatch(l); m != nil {
return m[1]
}
if t := strings.TrimSpace(l); strings.HasPrefix(t, "[") && strings.HasSuffix(t, "]") {
return t
}
return ""
}
func indent(l string) string { return l[:len(l)-len(strings.TrimLeft(l, " \t"))] }
var secretKey = regexp.MustCompile(`(?i)(key|token|secret|password|auth)`)
// mask hides the value of a line whose key reads as a secret's; magpie's
// own token, which is no secret, stays.
func mask(l string) string {
k := lineKey(l)
// …_TOKENS is a count of them (CLAUDE_CODE_MAX_CONTEXT_TOKENS)
if k == "" || !secretKey.MatchString(k) || strings.HasSuffix(strings.ToUpper(k), "TOKENS") {
return l
}
m := keyRe.FindStringIndex(l)
if m == nil { // a table header or compact array, not a key/value line
return l
}
val := strings.TrimSpace(l[m[1]:])
bare := strings.Trim(val, `"', `)
if bare == "" || bare == "magpie" || strings.HasPrefix(bare, "{") || strings.HasPrefix(bare, "$") {
return l
}
return l[:m[1]] + " ••••"
}