Commit Graph
7 Commits
Author SHA1 Message Date
yetone f02390544d edit: a JSONC delete takes the last member's trailing comma, so the MCP hub never leaves OpenCode's mcp as {,}
chenxiang0010 (Discord #general, Windows): turning the Library's MCP hub on
for OpenCode and off again left a comma at the end of opencode.jsonc's
"mcp", and OpenCode said "opencode.jsonc: invalid"; deleting the comma by
hand fixed it, and a second try didn't reproduce.

Cause: delJSONBytes looked for the deleted member's trailing comma in the
tidwall/jsonc copy, which blanks every comma before a closing bracket.
So when the member was the last one and had a trailing comma (as OpenCode,
editors and users allow), the comma was left behind; when it was the only
member, it was left with nothing before it: "mcp": {\n,\n}. The hub sync
then inserted the magpie entry in front of it ("magpie": {...},\n,) — a
double comma. OpenCode parses with jsonc-parser allowTrailingComma
(packages/opencode/src/config/parse.ts at v1.19.5, packages/core/src/config.ts
on dev): a trailing comma after a value is fine, a lone one is an error.
magpie's own validity check went through the same tidwall copy, so it
passed the broken file. Once the comma was deleted by hand there was no
trailing comma left, which is why it didn't happen again.

- delJSONBytes takes the last member's own trailing comma (nextToken, as
  delItem already did for arrays), and its line with trailing blanks.
- insertKey adds no comma after the first member of an object that holds
  comments only (it used to, leaving a trailing comma only some apps read).
- edit.ValidJSONC reads JSONC as jsonc-parser with allowTrailingComma does.
  SetJSON, DelJSON, PatchJSON, SetJSONItem and DelJSONItem refuse an edit
  that would turn a file the app reads into one it can't (checkJSONC):
  nothing is written, and the error names the file.
- A lone comma an older magpie left is dropped by the next object edit of
  that file (dropLoneCommas), so users already hit are mended by the next
  sync that writes it. A file nothing writes again keeps it until then.

Every JSON/JSONC object writer goes through SetJSON/DelJSON/PatchJSON
(library MCP for OpenCode, Zed, VS Code, Claude, etc., agent wiring for
DevEco, MiMo Code, OpenCode, ...), so all of them get it.

Tests (red on the old json.go/jsonarray.go, put back by hand, green now):
- TestHubToggleLeavesValidJSONC: 12 file shapes (LF, CRLF, tabs, compact,
  trailing commas, comment-only, comment after, comma after a comment) x 7
  add/delete orders; old code: invalid in the trailing-comma and
  comment-only shapes.
- TestDelOnlyMemberTakesItsTrailingComma, TestInsertIntoCommentOnly-
  ObjectNoTrailingComma, TestLoneCommaMendedOnNextEdit: red on old code.
- TestValidJSONC, TestInvalidEditNotWritten.
- internal/library TestHubToggleKeepsOpenCodeJSONCValid: the real
  SetMCPHub on/off twice for OpenCode, LF and CRLF; old code: invalid at
  the first "hub on" ("magpie": {...},\n,\n}).
2026-10-29 09:33:11 +08:00
yetone 5160809d2e agent: a catalog sync's round looks at each file once; a write reads the disk (#1665)
A catalog sync asked about each agent's files many times a round
(GetJSON, GetTOMLTop, settings.load from each agent's Sync): on Windows
an empty sandbox's start stat'ed settings.json 1065 times and
config.toml 569 in 25s, almost all from SyncCatalog. Each round now
holds what was read (filememo.Hold), as the GUI's read-only requests and
the gateway already do.

What a hold serves may be up to a second old, so nothing is written from
it any more:
- edit.Read, which every Set*/Del* helper and the agents' own
  read-modify-write code start from, looks at the disk even under a
  hold (filememo.ReadNow). The getters (GetJSON, GetJSONRaw,
  GetJSONItem, GetEnvFile, GetYAMLTop, the YAML getters, GetTOMLTop's
  line reader) read through the hold.
- settings: a write's load under fileMu (lockFile) reads the disk too.
Before this, a GUI request's hold could already hand a Set* stale
bytes; that is closed as well.

WriteAtomic forgot every held file even when it left the file as it
was, and a sync writes each agent's files each round, almost always
unchanged: it now forgets only after a real write.

Measured on xiaomi-gpu (Windows 11), as in the steady commit before
this one (second start, 27s, stats counted by an uncommitted build):
  before          2307 stats, sync 1318 of them, round 1308ms
  this alone      1787 stats, sync  864,          round 1273ms
  with steady's    988 stats, sync   96,          round   96ms
First start of an empty sandbox: 2448 stats before, 1041 after.

Run:
- TestHeldReadIsNotWhatAWriteIsMadeFrom (json, toml, yaml),
  TestUnchangedWriteKeepsWhatIsHeld, TestHeldSettingsAreNotWhatAWriteIsMadeFrom:
  with edit.Read and the settings write load put back on the held read
  and WriteAtomic's Forget removed by hand, json and yaml lost the other
  app's key, the settings case wrote "light" over "dark", and the
  forget case went red; green with the change, on mote.
- mote, at the pushed head: gofmt -l internal clean; GOOS=linux/windows/
  darwin go vet of steady, edit, filememo, settings and agent clean;
  go test -count=1 of steady, edit, filememo, settings, agent, provider,
  catalog and library ok; internal/gui red only on TestMainWindowCorners
  (#3949, macOS 27, red on main before this).
- xiaomi-gpu: go test of edit, filememo and settings ok.
2026-10-28 13:43:37 +08:00
ggbdpqandfactory-droid[bot] dca04af14f edit: a comment at the start of a model group's line stays when the group is taken out (#1566, ggbdpq)
What changed: delItem read the "is the element alone on its line" check
from the comment-blanked copy, so a JSONC comment written before the
element on the same line (VS Code's chatLanguageModels.json carries the
user's notes beside their groups) counted as blank space, the delete took
the line from its start, and the comment went with the element. It reads
raw now, as delJSONBytes does for the same check on an object member, so
only the element (and its comma, or its line) goes.

Test: TestDelJSONItemKeepsACommentBeforeTheElement (edit.DelJSONItem,
three shapes) fails on the old code with "the comment before the element
went with it" and passes now; the edit package's other tests, among them
the CRLF and among-others deletes, pass unchanged.

Run: go test -tags nogui -count=1 ./internal/edit ./internal/agent; the
full go test -tags nogui ./... in a sandbox HOME (TestOffline red as on
the baseline; gateway's TestSearchFollowsTheSearchersProxy red once under
the full run, passes alone, no path from this diff); gofmt -l; go vet and
go build on darwin, GOOS=linux -tags nogui and GOOS=windows.

Review: the test fails on main (3f5702421) with "the comment before the
element went with it". Added a fourth shape, a block comment that ends on
the element's line: on the old code DelJSONItem cut from the line start
into the comment and refused the result as "invalid JSON/JSONC", so the
group could not be taken out at all; now it goes and the comment stays.
Re-run: go test ./internal/edit, green with the fix and red with
stripped[ls:a] put back by hand.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2026-10-11 12:00:41 +08:00
yetone 947dc24aa3 edit: a key or array element taken out of a CRLF JSON file takes its whole line, no blank line left behind (found on #1534)
DelJSON, PatchJSON's deletes and DelJSONItem cut a member that sits on a
line of its own together with that line's end, but only looked for a bare
\n after it. In a file with \r\n line ends (a Windows settings.json,
VS Code's chatLanguageModels.json) the \r stopped the line end from going,
so each set-then-delete left one more blank \r\n line, e.g. magpie
connecting and disconnecting Claude Code. pastLineEnd now takes \r\n or \n.

Found while writing #1534's round-trip test on a CRLF settings.json.

Run: TestDelJSONTakesCRLFLine and TestDelJSONItemTakesCRLFLine fail on the
old code (a "\r\n\r\n" left where the key was) and pass now;
go test -tags nogui ./internal/edit/ passes.
2026-10-11 05:41:46 +08:00
Alex 2d9db1b60d edit: malformed JSON configs stay untouched when agents connect (#1242)
Validate the complete comment-stripped document before object and array edits, including previews. Regression tests fail on the old implementation because edits and VS Code Connect succeed and alter malformed files. Targeted edit and VS Code tests pass in an isolated HOME.
2026-10-08 11:52:20 +08:00
kevin 2e8d1a5317 edit: keep a CRLF file's line endings when SetJSONItem adds or replaces an element (#869) 2026-10-05 13:56:48 +08:00
yetoneandClaude Opus 5.5 a370877ca4 agents: VS Code's built-in chat (Copilot Chat) — switched on, magpie's models join Chat's model picker as a Custom Endpoint group in chatLanguageModels.json, with no key to enter and no Copilot sign-in needed (wani on Discord: 可以添加vscode自带的agent的配置吗)
VS Code Stable takes other vendors' models through its Custom Endpoint provider since 1.122 (customoai, the older OpenAI Compatible one, is Insiders only), signed in to GitHub or not. The provider groups live in chatLanguageModels.json beside settings.json in the default profile's User folder (~/Library/Application Support/Code/User, ~/.config/Code/User, %APPDATA%\Code\User), a JSONC array VS Code watches. magpie adds {"vendor":"customendpoint","name":"magpie","apiType":"chat-completions","models":[...]} with each catalog model at the gateway's /v1/chat/completions, toolCalling true (agent mode lists only tool-calling models), vision, contextWindow and maxOutputTokens (capped at a quarter of the window, as VS Code keeps the output budget out of the prompt's), and the model's efforts as supportsReasoningEffort. A group's apiKey is a secret property that VS Code reads from its secret storage only (languageModels.ts _resolveConfiguration), so it can't be written to the file; each model instead sends Authorization: Bearer magpie in requestHeaders, which CustomEndpointOAIEndpoint lets through and which stops it sending its own key header. The model a new chat starts on is chat.defaultModel in settings.json (matched by model id, so the catalog id); its old value is stashed and put back. The group is the agent's Joined: picking one of VS Code's own (auto) keeps magpie's models listed, and switching off (Unwire) takes the group out and puts chat.defaultModel back. Sync rewrites the group as the catalog changes, Check says when its url or header no longer reach the gateway, the Notice says where the models are and to reload the window if they don't show, and requests are credited to it by GitHubCopilotChat/<version>, a User-Agent a model's headers can't change.

internal/edit: SetJSONItem/GetJSONItem/DelJSONItem edit one element of a JSONC file's top-level array, found by its members, keeping comments, trailing commas and the other elements; SetJSON/DelJSON take a backslash-escaped dot as part of a key, as gjson does, for VS Code's flat "chat.defaultModel".

TestVSCode (connect keeps the user's settings and groups with their comments, the group's url/header/toolCalling/window and no apiKey or discovery url, Sync, Check, an own model keeping the group, disconnect back to the user's last own model), TestVSCodeRoundTrip (on and off leaves both files byte for byte; no files at all) and TestVSCodeUA; TestJSONItem, TestDelJSONItemAmongOthers and TestJSONEscapedDot. go vet ./internal/..., go build -tags nogui ./..., GOOS=windows and GOOS=linux builds pass; go test of internal/agent, edit, usage and gateway (each sandboxes HOME in TestMain) and internal/gui's page and agent connect tests pass; the full go test under a temp HOME was not run (the sandbox refused the HOME override). No GUI code changed (the generic connected wording fits), so no webkit test. VS Code itself is not installed here: not tried against a real VS Code.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 08:23:38 +08:00