88b4c00ea11c8653cada22e11edf20ab1594de43
10
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
6536982756 |
edit: an agent's config saved with a UTF-8 BOM is edited through it and keeps it — magpie claude <model> no longer says "invalid JSON/JSONC" (#1534, AnxForever)
On Windows, PowerShell 5.1's `Set-Content -Encoding UTF8`, Notepad and
Claude Code channel switchers save ~/.claude/settings.json with a UTF-8
BOM in front. Claude Code reads such a file; magpie's JSON layer handed
the BOM to json.Valid and gjson, so `magpie claude <model>` failed with
"<path>: invalid JSON/JSONC" and the agent couldn't be wired at all. TOML
refused the same with "UTF-8 BOM is not supported", though Codex's own
parser (toml_parser's lexer) skips a BOM, and env/YAML files glued the BOM
to their first key's name.
The fix is at the package's two choke points, so every format and every
caller of edit.Read gets it:
- edit.Read returns the file without a leading BOM (the agent package's
own readers, Claude Desktop, Ante, Empryo, Snow, Pi MCP etc. go through
it).
- WriteAtomic puts the BOM back when the file it replaces starts with one
and the new bytes don't (keepBOM). The user's file keeps the encoding it
was saved in: the tools that wrote it may expect it, and the agent read
it with the mark before, so keeping it is the user's own bytes, not a
guess. A file without a BOM never gets one; an emptied file stays empty.
This is what astrbot.go already did for AstrBot's cmd_config.json.
- PatchJSON (bytes in, bytes out) reads through and keeps a BOM too.
- The TOML "BOM is not supported" error is gone with the case it named.
- Siblings read outside edit.Read: Claude Code's settings.json and CC
Switch's legacy config.json in the importer, and Crush's provider list,
trim it with the new edit.TrimBOM.
- Prime Agent's Disconnect (beforeMagpie.restore, on main since this
work began) compares the file with its copy from before through the
BOM, so a BOM'd settings.json is put back byte for byte and the copy
removed; it used to stay as magpie left it, the copy beside it.
The reporter's fork commit (AnxForever/magpie 0bb750c1) was reviewed: it
trimmed the BOM in the seven JSON entry points and wrote the file back
without it; this change keeps the BOM instead and covers TOML, YAML, env
and the readers outside the JSON layer.
Run:
- New tests built on a Windows-shaped settings.json (BOM + CRLF + the
user's env and permissions): TestJSONSettingsWithBOM, TestPatchJSONKeepsBOM,
TestJSONItemWithBOM, TestTOMLConfigWithBOM, TestEnvAndYAMLWithBOM fail on
the old code ("invalid JSON/JSONC", nothing found) and pass; with keepBOM
disabled by hand four of them go red. TestWriteAtomicAddsNoBOM.
TestPrimeAgentRoundTripWithBOM fails on main (not put back) and passes.
TestClaudeConfigDirImport (BOM case) and TestImportCCSwitchConfigWithBOM
fail on the old importer ("invalid character 'ï'") and pass.
- Full chain in a sandbox HOME: old binary `magpie claude
deepseek/deepseek-flash` on a BOM'd settings.json -> "invalid
JSON/JSONC"; new binary -> "✓ Claude Code model", BOM and CRLF kept, the
user's env untouched; `magpie claude model default` after it leaves no
blank line. Old `magpie codex gpt-5.5` on a BOM'd config.toml -> "UTF-8
BOM is not supported"; new -> set, BOM kept.
- Windows box: the edit package's test binary passes whole; magpie.exe
`claude deepseek/deepseek-flash` on a settings.json written by
PowerShell 5.1 Set-Content -Encoding UTF8 (sandboxed USERPROFILE/APPDATA)
sets the model and keeps ef bb bf.
- go test -tags nogui ./internal/edit ./internal/agent ./internal/provider
./internal/library; gofmt -l; go vet (darwin, GOOS=linux, GOOS=windows)
on the touched packages; the full suite.
|
||
|
|
78b3f1f21a |
agent: an agent pointed at magpie on another machine keeps that address and key through a start's sync (coeo91 on Discord)
coeo91: after an update or a restart, the base URL in an agent's config sometimes went from the LAN address back to 127.0.0.1. Every start of magpie (and a provider added, a catalog fetched) runs SyncCatalog, which runs each agent's Sync to bring magpie's models in its files up to date. Each Sync writes magpie's whole provider block, base URL and key included, from gateway.URL(), so a block the user had pointed at a magpie on another machine (a NAS's, or this machine's LAN address) went back to 127.0.0.1 with magpie's own key. Only ZCode (zcodeAddress), WorkBuddy (buddyWriteAt) and Codex (#816) kept such an address; in a sandbox HOME, 27 files of the other agents went back (Qoder, OpenCode, Pi, Crush, Cline, Qwen Code, Kimi, Grok, Hermes, omp, MiniMax, Snow, Hanako, VS Code, Copilot JetBrains, Goose, dsh, ...). The fix is one place for all of them: SyncCatalog runs each Sync through synced, which passes every file written through keptAway (edit.Filtered, a filter on edit.WriteAtomic while it runs). In a file without the gateway's own address, with an address on another machine and none a WSL distro was seen at, a write that brings gateway.URL() in place of that address keeps the address, and one that brings magpie's own key (gateway.Token, TokenFor, the LAN key, "Bearer " too) in place of another key keeps that key. An address on this machine's loopback (another port, localhost) still follows the gateway, a WSL distro's way to Windows is still magpie's to move, magpie's own files are never filtered, and with two addresses or keys gone the write is left as it is. A pick made in magpie still points the agent at this magpie. Tests: - TestSyncKeepsAnAddressOnAnotherMachine wires every agent in a sandbox HOME, points each file at 192.168.50.10 with a key of its own, adds a provider and runs SyncCatalog: no file is back on 127.0.0.1, the address and key are kept, and the new model is written. With SyncCatalog calling a.Sync() directly it fails on 27 files. - TestKeptAwayOnlyWhatIsSure: old port, localhost, WSL and two-gone cases follow the gateway; a NAS beside a local Ollama is kept; Qwen Code's key written on its own is kept. - TestKeyValuesFormats, TestFilteredWrites. Run: gofmt -l (clean); GOOS=linux and GOOS=windows go vet -tags nogui ./internal/agent ./internal/edit; go test -tags nogui ./... in a temp HOME. |
||
|
|
3d69bde0f6 |
edit: a config shared with a group keeps its group when magpie writes it (#1499, fjrtdk)
WriteAtomic kept a file's mode but not its group: the temp file renamed over it takes its folder's group, so a config shared with a group (Aside's group-writable u/1/models.json, group wheel, on #1499) could end up in the user's own group and the other writers lose it. The temp file is now given the old file's group before the rename, best effort (a group the user isn't in is left as made). Windows has no Unix group; its keepGroup does nothing. Run: go test ./internal/edit (TestWriteAtomicKeepsGroupAndMode passes; with keepGroup commented out it fails with the folder's group); gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/edit. |
||
|
|
89271cd4e8 |
provider, plugin, settings: accounts left all zero by a crash are read from their last good copy, and every state file is flushed before it is renamed in (#1505, Zhangwier)
Zhangwier's Windows machine went down hard, and logins.json (10783 bytes)
and plugin-auth.json (29926 bytes) came back at full length with every byte
0x00. Every account was gone. The rename had reached the disk before the
data. magpie then read "no accounts", and its next write would have made
that final.
What changed:
- Flushed before rename: steady.WriteFile writes a temp file, fsyncs it,
then renames it in. A failed write or flush removes the temp file and
leaves the old file as it was. edit.WriteAtomic, Kiro's writer, the
Codex session rewrite and the plugin host's writeDurable (host.js)
flush the same way. So do writePrivate (providers, logins, migrations),
settings, profiles, plugins.json, plugin-auth.json and mcp-signins.json.
- A last good generation: before each write, lastgood.Keep keeps the
valid file there as <file>.bak. A file that isn't valid is copied aside
as <file>.bad-<time>, with -2, -3 when two land in one second (O_EXCL),
and is never overwritten before that copy exists. The .bak lags one
write behind on purpose, since a crash zeroes the latest write.
- A failed read is unknown, never empty. On a cold start, logins.json,
providers.json, migrations.json, settings.json, profiles.json,
plugins.json, plugin-auth.json (Go and the host) and mcp-signins.json
are read from their .bak when they don't read. The recovery is logged
and listed in /api/state's recovered, and the window says it once in
every language. With no good .bak, a sign-out leaves plugin-auth.json
and mcp-signins.json as they are (the host's changeAuth no longer
writes {} over them). magpie's own sign-out returns the error, and a
sign-in copies the bad file aside before writing.
- caller-keys.json is flushed but never read from a .bak. A key revoked
in the last write would come back, so a store that doesn't read still
refuses every key, as its tests require.
ggbdpq's patch (f230bcae) is where the fsync-before-rename in
writePrivate, writeWhole and writeAuth came from. This change puts it in
one shared helper and reaches every other writer of magpie's state. The
JS write cleans up its temp file. The .bak recovery, the .bad copies and
the GUI notice are added here.
What was run:
- New tests, built from the literal case (10783 and 29926 zero bytes):
- internal/lastgood
- provider: TestZeroedLogins*, TestZeroedMigrations*,
TestZeroedProviders*, TestWritePrivateSyncsBeforeRename
- plugin: TestZeroedPluginAuth* against the real bun host,
TestPluginAuthSyncedBeforeRename with a plugin that makes fsync fail,
TestZeroedPluginList*
- TestZeroedSettings*, TestZeroedProfiles*, TestZeroedMCPSignIns*
- steady TestWriteFileSyncsBeforeRename, edit
TestWriteAtomicSyncsBeforeRename
- gui recovered-file.test.cjs (en/zh/zh-TW/ja/de, Chromium and WebKit)
- Broken by hand, each going red:
- Without the sync: the steady, edit, writePrivate and host fsync tests.
- Without the .bak fallback: every TestZeroed* test.
- The old changeAuth writes "{}\n" over the zeros.
- The old readLogins default.
- KeepBad without suffixes fails the one-second test.
- Without recoveredOnce, the GUI test fails 5/5.
- macOS: go test -tags nogui ./... in a sandbox HOME. The only failures
were the two access tests (then fixed by not recovering caller keys) and
TestProvidersCmdSaysUnreadable, which now removes the .bak to test the
no-backup case. Both packages were rerun and pass. gofmt -l prints
nothing. go vet, and GOOS=linux and GOOS=windows go vet -tags nogui, are
clean on the touched packages.
- GUI: recovered-file, whats-new, click-scroll, gui-de, gui-ja,
gui-zh-tw and i18n-keys pass.
- Windows box (cross-compiled test exes, sandboxed USERPROFILE/APPDATA):
lastgood, steady, provider, plugin (real bun host), settings, mcpauth,
edit and access pass. In profile, TestProfileSwitchesModelList and
TestOldProfileKeepsModelList fail the same on origin/main (filed as
#1517).
Reported-by: Zhangwier
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
|
||
|
|
2a4042a94d | perf: magpie opens and shows the Agents page in a fraction of the time it took (Junjie Zhou on Discord: 8 个 agent 30 个 provider … 每次我打开的时候都会很慢,要好几秒。setting 页面秒进后 我切换到 agent 就有问题) — at start, writing the agents' model lists (agent.SyncCatalog) built every provider's catalog thousands of times with nothing held, each build reading migrations.json, settings and groups again: 6000+ file reads and ~8.5s of CPU in the first 8s with the owner's 30-odd providers, all competing with the first /api/state and the Agents page. Now SyncCatalog, magpieModels and main's start-up moves run under provider.Hold, migrations, groups, settings and a provider's fetched models are held for a second like the rest, and filememo.Hold lets a GET /api/* not stat a file it confirmed unchanged within the last second (any magpie write, Changed() or a recent mtime still reads it again). A big ~/.codex/config.toml parsed in quadratic time (the TOML parser's Shape counts newlines from the start for every key): a 3 MB one took 5.4s, now 0.04s (tomlShaper). theInstalled reads through edit.Read's memo. Measured with a copy of the owner's magpie config and agent configs under a sandbox HOME, the real server in WebKit: CPU in the first 8s 8.59s → 0.39s, page up 475 → 75ms, Agents rows drawn 775 → 325ms after start; the agents' written model files are unchanged (pi byte for byte, opencode the same models). TestTOMLShaperMatchesParser, TestTOMLBigFileParsesInLinearTime (fails on the old code), TestHold (filememo), TestHoldReadsMigrationsOnce. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass. | ||
|
|
7dd809002b | accounts: on Linux an account put first is read first at once, and on Windows a file being replaced isn't read as none (goal: 插件订阅 provider 跟内置的所有逻辑都要一模一样, run on Windows and Linux) — the provider tests run on the Windows test box and in WSL found two bugs of magpie's own files. Linux stamps a file's time from a clock a tick (a few milliseconds) coarse, and filememo took same time and size for the same file: two accounts swapped (Devin, Grok, WorkBuddy, ZCode, Claude…) read back in the old order, so magpie kept using the account the user had just moved down, and a moved-back plugin's accounts read as before. A file written within the last two seconds is now also compared by its bytes. On Windows a file being renamed over refuses to open for a moment ("Access is denied"), and one held open refuses to be renamed over: plugin-auth.json, plugins.json and the plugin providers read as nothing (a usage fetch right after a move showed no cards), logins.json failed to save, and host.js, reading plugin-auth.json as {}, would have written the other accounts away on the next sign-in. New internal/steady waits such a read or rename out (half a second at most, Windows only); filememo, edit.WriteAtomic, writePrivate, Kiro's writes and the plugin store go through it, host.js retries its read and rename, and the plugin store writes its files by a rename. Tests: USERPROFILE beside HOME in the test homes, the fake devin (a shell script) skipped on Windows, plugin-auth.json's mode bits unchecked there. TestReadSameSizeSameTick fails on Linux and TestRenameOverAFileHeldOpen/TestReadAFileHeldOpen on Windows without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; on Linux (WSL) the provider and plugin packages pass whole (13 failed before), on Windows the plugin package and the move/plugin/Grok/Devin/usage tests pass 10 times over (TestMoveToPlugin failed one run in a few before); 15 built-in tests that assume Unix (mode bits, Keychain, shell-script CLIs) still fail there, as before. | ||
|
|
110567472d |
edit: a config that is a hard link stays one when magpie saves or clears it (#328)
* edit: a config that is a hard link stays one when magpie saves or clears it — WriteAtomic renamed a temp file over the path even when the file had other names, so a models.yml that Orca keeps hard-linked into its overlay split in two: omp's name got the new text, the overlay kept the old one, and each later write drifted further; clearing an instructions file unlinked only magpie's name and left the old text under the others. Now a regular file with more than one link (st_nlink on unix, NumberOfLinks from GetFileInformationByHandle on Windows, one elsewhere) is written in place: the new text first goes to a temp file beside it and is synced, then is written over the file, cut to length and synced, and the temp file goes; a crash part way leaves the whole new text beside it, and a failed write keeps it and names it in the error. Mode, owner and every name stay. Remove empties such a file instead of unlinking one name, as it already did for a symlink, and a failed edit rolled back by Atomically goes back in place too. A file with one link is still replaced by rename, atomically, as before. * edit: a read-only hard-linked config is still written, and temp files a crashed write left beside a config go at its next write Review follow-up on #328: - A hard-linked file that can't be opened for writing (0444 on unix, read-only on Windows) fell over with "permission denied" where main's rename replaced it. WriteAtomic now opens it first and, on fs.ErrPermission, falls back to temp file + rename as before, which splits that name from the others as main always did. On Windows main couldn't rename over a read-only file either, so nothing changes there. - writeInPlace says why it writes over and then cuts to length: a reader can briefly see the new text with the old one's tail, but emptying first would show an empty or half file instead, which a tool may load as a config with nothing set; a mangled one fails to parse. - writeInPlace names the trade with hard-link backups (rsnapshot, cp -al, rsync --link-dest): they change with the file, as with vim's backupcopy=auto. - Remove says that emptying every name is meant: with AGENTS.md and CLAUDE.md one file, clearing one empties both, as #323 does for a symlink's target, since deleting one name would leave the text magpie is taking back under the other. - After a write goes through, both paths remove .<name>.<digits>.tmp beside the file older than ten minutes: the file then holds newer text than any of them, a fresh one may be another write's still under way, and the digits keep other files' temps (.models.yml.bak.1.tmp) out. |
||
|
|
8124d3f927 | edit: a config kept as a symlink stays one when magpie saves or clears it (#323, jigaowan) — WriteAtomic wrote a temp file beside the path and renamed it over it, so a linked library.json, instructions.md, CLAUDE.md, settings.json or config.toml became a file of its own and the dotfiles target kept its old text; clearing instructions.md removed the link. Now a write goes to the file at the end of the links (the one it names when nothing is there yet, an error for a loop of links), keeping its mode, and clearing a linked file empties its target and keeps the link (Pi's migrated mcp.json stays as {}); a plain file is removed as before. Sign-ins and the Kiro/StepFun files magpie writes privately follow links the same way, and a failed edit rolled back deletes only what it made behind a link to nothing. TestWriteAtomicFollowsSymlink, TestRemoveKeepsSymlink, TestAtomicallyKeepsSymlink, TestLinkedFilesStayLinked and TestPrivateWritesKeepSymlink fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass. | ||
|
|
d37b821908 |
toml: Codex's config.toml with a multi-line array (notify = [ … ]) is edited right, and an edit that fails leaves every file as it was (#253 kerwinliu00001: "edited TOML is invalid: line 2, column 1: incomplete number", config.toml left with model_provider gone and [model_providers.magpie] added but no model) — the top-level key edits (GetTOMLTop, SetTOMLTop, DelTOMLTop) went line by line with regexes: a new key went after the last line that looked like key = …, which for notify = [ was inside the array; key = value lines inside a multi-line string were read as keys and a line starting with [ as a table; replacing or deleting a multi-line value touched only its first line. They now locate the top-level keys with the TOML parser, as the table edits already did, each with its value's whole range however many lines it takes (parseTOMLFile, the unnamed root table beside the tables); SetTOMLTop finds all its keys in the file as read and writes once. A file the parser refuses is still read line by line, so a broken config keeps showing its model, and writes to it are refused untouched. And a pick is several writes (Codex: drop model_provider, add magpie's provider table, set openai_base_url and model), each checked, so one failing left the earlier ones on disk: edit.Atomically(fn, paths…) puts each file back to its bytes and mode, or removes one it created, when fn fails, and every field set and Sync of Codex (config.toml and magpie-models.json, so Codex in WSL too), Kimi and Grok goes through it, as do Codex's MCP server writes (putCodex, delCodex). Tested in temp dirs and a temp HOME: the reporter's file read, set and deleted directly and through Codex's model field (signed in, with model_provider and model above the array, with magpie as the provider), the array's text kept and the TOML valid, and back to a Codex model; nested arrays, trailing commas, comments in arrays, [x] and a = b in strings, an inline table in an array; """ and ''' strings holding model = … and [table] lines not read as keys, new keys after their closing quotes, one deleted whole; a forced failure of Codex's last write leaving config.toml byte-identical; TestAtomically. They fail on main (the Codex ones with the reporter's error). go vet ./internal/..., go build -tags nogui (and GOOS=linux, GOOS=windows) and go test -tags nogui ./... pass. Not tried against a real ~/.codex, in WSL or in the GUI; multi-line inline tables (TOML 1.1) are refused by the parser and so left untouched with an error.
|
||
|
|
0a234ce41d |
dial: one dial for every coding agent's model
Wails v3 tray panel and window, TUI and CLI. Switches model, effort and provider for Claude Code, Codex, Gemini CLI, OpenCode, Pi, Goose, Cursor, Copilot CLI and Crush with format-preserving config edits. Providers page and registry, stored keys, live vendor model lists, profiles. Co-Authored-By: Claude <noreply@anthropic.com> |