Files
magpie/internal/agent/mothx.go
T
yetone 73a12e8bf6 wsl: a distro's agent folder that is a link is opened where it leads (#4502)
On Windows, adding a skill or switching RTK on for an agent in WSL failed
with `mkdir \\wsl.localhost\<distro>\root\.codex: Cannot create a file
when that file already exists` (linusxiong). In the distro ~/.codex is a
link to /mnt/c/Users/<user>/.codex. Through \\wsl.localhost Windows can't
follow a link the distro has: a mkdir of it says it exists, and nothing
below it can be made, written, read or listed. ggbdpq found and explained
the link (thanks); their patch, which takes that EEXIST as "there", wasn't
used: the writes below the link fail next (its test passed because a
symlink here is followed).

The fix, for every agent folder and every writer through \\wsl.localhost:
- The WSL probe asks `readlink -f` in the distro where each agent folder,
  its skills folder and the folders beside them lead (wslLinkPaths ->
  distro.Links; a $HOME that is itself through a link is left alone), and
  which drive each Windows mount is (win:/mnt/c C: -> distro.Drives).
- distro.open opens a path at or below a link where it leads: on a Windows
  drive, the drive's own path (C:\Users\<user>\.codex); elsewhere in the
  distro, through \\wsl.localhost again. native spells it back through
  the link, so the agent is told its own path.
- A link readlink couldn't resolve is never guessed at: distro.open
  errors, and the agent is unreached (Agent.Unreached: its notice says
  why; every field Set and Sync returns it).
- Writers: the agents' own files (place.join/place.open, every
  filepath.Join(at.home, ...) in internal/agent), the library's WSL
  targets (wslOpen/wslOpened; one "wsl" problem per agent and nothing
  written), RTK (rtkAt, rtkInstall checks agent.WSLOpen before its mkdir),
  the Sessions page's WSL reads (sessions.WSLHome.Open) and the Copilot
  CLI sign-in read (provider.CopilotWSLHomes -> CopilotWSLDirs).
- Not changed: magpie's image MCP server opens arbitrary paths an agent
  in WSL gives it the same way; filed as #4578. Links deeper than the
  probed paths (other than <agent dir>/skills) aren't resolved. A distro
  probed by an older magpie has no Links until probed again (each run).

Run:
- Real Windows box (xiaomi-gpu, distro magpie-desktop, sandbox
  /tmp/x4502/home with .codex -> /mnt/c/Users/yeton/runs/x4502-target/.codex,
  .claude -> /tmp/x4502/data/claude, .pi -> a missing folder), a throwaway
  Windows test running the real probe with that HOME: probe said
  link:.codex /mnt/c/..., link:.claude /tmp/x4502/data/claude, link:.pi,
  win:/mnt/c C:, win:/mnt/d D:. The old way, MkdirAll(\\wsl.localhost\
  magpie-desktop\tmp\x4502\home\.codex\skills\pdf) = "Cannot create a file
  when that file already exists" (the report's error). distro.open wrote
  SKILL.md to C:\Users\yeton\runs\x4502-target\.codex\skills\pdf and to
  \\wsl.localhost\magpie-desktop\tmp\x4502\data\claude\skills\pdf, both
  read back with cat in the distro through the links; .pi said "~/.pi in
  WSL magpie-desktop is a link magpie couldn't follow". Sandbox and target
  removed after.
- New tests with a fake distro whose ~/.codex and ~/.claude are links to
  /mnt/c/... that don't exist here, so, as through \\wsl.localhost,
  nothing below them can be made: TestWSLFolderIsALinkToC,
  TestWSLFolderLinkUnresolved, TestRTKWSLFolderIsALink,
  TestRTKWSLFolderLinkUnresolved (library); TestWSLProbeLinks (runs the
  probe script), TestWSLOpenThroughLinks, TestWSLAgentFolderIsALink
  (config and a Claude session read on C:), TestWSLAgentFolderLinkUnresolved
  (agent).
- Broken by hand: the library tests on the base (with a no-op
  FakeWSLDrives) fail with "mkdir .../home/me/.codex: file exists", the
  report's error; with ggbdpq's patch applied they still fail (the skill
  mkdir, and RTK's hook not written).
- mote: go build ./...; go vet of agent, library, sessions, provider for
  darwin, GOOS=windows (and linux for agent, library); gofmt -l clean;
  go test -timeout 45m ./internal/agent ./internal/library
  ./internal/sessions ./internal/provider: all ok.
- Windows (win-run): the same four packages. The new tests pass (the
  RTK ones skip: the fake distro runs sh). 32 tests are red, all already
  red at the base c63c2136b run on the same box, and filed (#4115, #2808:
  Hermes profiles, Codex keychain, Agy, desktop entries, ...); the Hermes
  ones fail alike on base and branch with -count=3.

- Rebased on origin/main 3ecc02b78 (it changed internal/agent): at this
  head on mote, go build ./..., go vet of the four packages for darwin,
  windows and linux, gofmt -l, go test ./internal/agent ./internal/library:
  ok.

Not tried: the full ./... suite; the GUI (no GUI change; the unreached
notice uses the existing Notice); a real Codex/Claude Code run against
the moved files in the distro.
2026-10-31 21:29:22 +08:00

290 lines
10 KiB
Go

package agent
// MothX (oschina/mothx, the mothx command; formerly vibecoding) is a Go
// terminal coding agent that reads its providers and the model it starts
// on from one settings.json in its config folder: $MOTHX_DIR, else
// %APPDATA%\mothx on Windows and ~/.mothx elsewhere (platform.ConfigDir,
// configDirForOS). Its fields (config.Settings, ProviderConfig,
// ModelConfig, mothx 1.3.104):
//
// {"defaultProvider":"magpie","defaultModel":"<model>",
// "defaultThinkingLevel":"high",
// "providers":{"magpie":{"baseUrl":"http://127.0.0.1:3425/v1",
// "api":"openai-chat","apiKey":"magpie-mothx","thinkingFormat":"openai",
// "models":[{"id":…,"name":…,"reasoning":true,"contextWindow":…,
// "maxTokens":…,"cost":{…},"input":["text","image"]}]}}}
//
// A session asks provider defaultProvider for model defaultModel on the
// provider's api (openai-chat: baseUrl + /chat/completions) under Bearer
// apiKey. Its requests carry "Vibecoding Client/<version>" as their
// User-Agent, its old name an alias here; the key magpie gives it names it
// too (gateway.TokenFor). It
// reads settings.json at start-up, and its own /model and /auth dialogs
// write the whole file back (SaveGlobalSettings), every default spelled out.
//
// Thanks to zhenruyan, whose fork's internal/agent/mothx.go (#2353) this
// follows.
import (
"encoding/json"
"path/filepath"
"runtime"
"slices"
"sort"
"strings"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
)
// mothxLevels are the thinking levels MothX takes (provider.ThinkingLevel,
// its --thinking flag): Pi's set.
var mothxLevels = []string{"off", "minimal", "low", "medium", "high", "xhigh", "max"}
var noticeMothx = newNotice("MothX reads its settings at start-up — restart open mothx sessions to use this.")
func mothx(home string) *Agent { return mothxIn(here(home)) }
// mothxIn is MothX at a place: this machine's home, or a WSL distro's (see
// wsl.go), its settings.json naming the gateway as it reaches it from there.
func mothxIn(at place) *Agent {
dir := mothxDir(at)
path := filepath.Join(dir, "settings.json")
get := func(k string) (string, bool) { return edit.GetJSON(path, k) }
set := func(kvs ...edit.KV) error { return edit.SetJSON(path, kvs...) }
pair := pairSet(set, "defaultProvider", "defaultModel")
key := func() string { return agentKeyAt("mothx", at.gw()) }
block := mothxBlockKept(path, func() any { return mothxProvider(at, key()) })
writeMagpie := func() error {
return set(edit.KV{Path: "providers." + magpieID, Value: block()})
}
// the file as it was before magpie's first write, put back byte for
// byte by Disconnect where it leaves it saying the same
before := beforeMagpie{at.key("mothx.before.absent"), []string{path}}
return atomic(&Agent{
ID: "mothx", Name: "MothX", Icon: "mothx", Bin: "mothx", Dir: dir, Path: path, Spelled: prefixed,
// the command and the package were vibecoding before (its npm
// vibecoding-installer still installs it under that name), and
// its requests still say "Vibecoding Client/<version>": usage
// names them by the word before the space (usage.AgentOf)
Aliases: []string{"vibecoding"},
Restore: before.restore,
Check: func() string {
if p, _ := get("defaultProvider"); p != magpieID {
return ""
}
return wiringOff("MothX", path, func(k string) (string, bool) { return edit.GetJSON(path, "providers."+magpieID+"."+k) },
"baseUrl", at.v1(), "apiKey", key())
},
ListOnly: true,
Sync: func() error { return syncJSON(path, "providers."+magpieID, block) },
Notice: func() string {
if p, _ := get("defaultProvider"); p == magpieID && Running(`(^|/)mothx( |$)`, `(^|/)vibecoding( |$)`) {
return noticeMothx.String()
}
return ""
},
Fields: []Field{
{
// defaultProvider and defaultModel, as one provider/model
Key: "model", Label: "model",
Get: pairGet(get, "defaultProvider", "defaultModel"),
Set: func(v string) error {
if v == "" {
if err := edit.DelJSON(path, "defaultProvider", "defaultModel"); err != nil {
return err
}
if err := edit.DelJSON(path, "providers."+magpieID); err != nil {
return err
}
// providers left empty is magpie's: MothX writes none
// (omitempty), and reads none the same as {}
if raw, ok := get("providers"); ok && strings.TrimSpace(raw) == "{}" {
return edit.DelJSON(path, "providers")
}
return nil
}
// a model of magpie's: its provider block goes in beside the pick
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if _, has := get("providers." + magpieID); !has {
if p, _ := get("defaultProvider"); p != magpieID {
before.take()
}
}
if err := writeMagpie(); err != nil {
return err
}
}
return pair(v)
},
Options: func(cur map[string]string) []Option {
return append(mothxOwn(path, cur["model"]), viaMagpie("mothx", magpieID+"/")...)
},
},
{
// defaultThinkingLevel, what a session starts at; MothX sends
// none for a model whose entry doesn't say it reasons
Key: "effort", Label: "thinking",
Get: func() string { v, _ := get("defaultThinkingLevel"); return v },
Set: func(v string) error {
if v == "" {
return edit.DelJSON(path, "defaultThinkingLevel")
}
return set(edit.KV{Path: "defaultThinkingLevel", Value: v})
},
Options: func(cur map[string]string) []Option {
if offered := mothxOffered(cur["model"]); offered != nil {
return static(offered...)
}
return static(mothxLevels...)
},
},
},
}, path)
}
// mothxDir is MothX's config folder at a place: $MOTHX_DIR (a relative one
// is its working directory's, which magpie can't know), else
// %APPDATA%\mothx on Windows, else ~/.mothx. A distro's variables aren't
// read, so it is ~/.mothx there.
func mothxDir(at place) string {
if d := at.getenv("MOTHX_DIR"); d != "" {
return d
}
if runtime.GOOS == "windows" && at.spell == nil {
if app := appdir.Getenv("APPDATA"); app != "" {
return filepath.Join(app, "mothx")
}
return at.join("AppData", "Roaming", "mothx")
}
return at.join(".mothx")
}
// mothxBlockKept is theirsKept for magpie's provider in MothX's
// settings.json: what else the user put in it (headers, httpProxy) stays,
// and so does a thinkingFormat already there, which may be theirs.
func mothxBlockKept(path string, value func() any) func() any {
return theirsKept(path, "providers."+magpieID, func() any {
v := value()
if _, set := edit.GetJSON(path, "providers."+magpieID+".thinkingFormat"); set {
if m, ok := v.(map[string]any); ok {
delete(m, "thinkingFormat")
}
}
return v
}, "models")
}
// mothxProvider is magpie as MothX's provider: the gateway's Chat
// Completions, the key that names MothX, and one entry per model of
// magpie's.
//
// thinkingFormat "openai" has MothX ask every model for a level as
// reasoning_effort, which the gateway carries to each vendor's own form.
// Without it MothX guesses the vendor from the model id and the base URL
// (thinkingFormatForModel): a magpie model named qwen3.7… got Qwen's
// enable_thinking and thinking_budget, a deepseek-v4.1… only
// thinking:{type:enabled}, whatever served it.
func mothxProvider(at place, key string) any {
ms := []map[string]any{}
for _, m := range magpieModels("mothx") {
ms = append(ms, mothxModelJSON(m))
}
return map[string]any{"baseUrl": at.v1(), "api": "openai-chat", "apiKey": key,
"thinkingFormat": "openai", "models": ms}
}
// mothxModelJSON is one of magpie's models as an entry of a MothX
// provider's models, as its own presets write one.
func mothxModelJSON(m catalog.Model) map[string]any {
// reasoning is what has MothX send a thinking level at all
e := map[string]any{"id": m.ID, "name": m.Name, "reasoning": len(m.Efforts) > 0}
in := []string{"text"}
if m.Images {
in = append(in, "image")
}
e["input"] = in
// without them MothX takes the model for its own default window and
// caps a reply at its own default
if m.Context > 0 {
e["contextWindow"] = m.Context
}
if m.Output > 0 {
e["maxTokens"] = maxTokens(m)
}
// MothX counts a turn's cost by it, per million tokens as magpie's
if m.Price != nil {
e["cost"] = map[string]any{"input": m.Price.Input, "output": m.Price.Output,
"cacheRead": m.Price.CacheRead, "cacheWrite": m.Price.CacheWrite}
}
return e
}
// mothxOwn are the models of the user's own providers in settings.json,
// as provider/model, and the current pick when it is none of them (one of
// MothX's built-in presets, such as deepseek-openai).
func mothxOwn(path, cur string) []Option {
var out []Option
seen := map[string]bool{}
if raw, ok := edit.GetJSON(path, "providers"); ok {
var ps map[string]struct {
Models []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"models"`
}
if json.Unmarshal([]byte(raw), &ps) == nil {
names := make([]string, 0, len(ps))
for p := range ps {
if p != magpieID {
names = append(names, p)
}
}
sort.Strings(names)
for _, p := range names {
var ms []catalog.Model
for _, m := range ps[p].Models {
if m.ID != "" {
ms = append(ms, catalog.Model{ID: m.ID, Name: m.Name})
seen[p+"/"+m.ID] = true
}
}
out = append(out, group(p, options(ms, p+"/"))...)
}
}
}
if cur != "" && !usesMagpie(cur) && !seen[cur] {
out = append([]Option{{Value: cur, Icon: modelIcon("", cur)}}, out...)
}
return out
}
// mothxOffered are the levels MothX offers for model when it is one of
// magpie's whose efforts magpie knows: those of them MothX has, none read
// as off. nil for any other model, which is offered MothX's whole list.
func mothxOffered(model string) []string {
ref, ok := strings.CutPrefix(model, magpieID+"/")
if !ok {
return nil
}
for _, m := range magpieModels("mothx") {
if m.ID != ref || len(m.Efforts) == 0 {
continue
}
var out []string
for _, e := range m.Efforts {
if e == "none" {
e = "off"
}
if slices.Contains(mothxLevels, e) && !slices.Contains(out, e) {
out = append(out, e)
}
}
if len(out) > 0 {
return out
}
}
return nil
}