magpie icon indicating copy to clipboard operation
magpie copied to clipboard

plugin: switching an agent plugin off leaves the parent objects its removed keys emptied (e.g. provider.magpie.options {})

Open yetone opened this issue 1 month ago • 1 comments

Found while reviewing magpie-community/plugins#90 (Kilo). The agent plugin's connect writes nested keys into kilo.jsonc. Switching off removes those keys one by one through magpie's JSON writer, but a parent object that becomes empty is left behind: "provider": {"magpie": {"options": {}}} stays in the user's file. The README promises everything is put back. Kilo ignores it, but the file is no longer byte-identical to the user's own.

Any agent plugin that writes nested keys will hit this. Expected: an object that magpie's own removals emptied (and that was absent before connect) is removed too; an object that existed before connect (even empty) is kept. Test: connect then disconnect over a user file with comments and own providers, and compare bytes.

yetone avatar Oct 12 '26 14:10 yetone

Picked this up while it was open and unclaimed (still open, no maintainer fix as of posting).

Root cause

internal/agent/plugged.go stashed only the leaf key paths connect returned (pluggedStash.Had), and restore deleted them with edit.DelJSON. delJSONBytes (internal/edit/json.go:306, the collapse step at :364) collapses only the direct parent of a removed key to {} and leaves it — so for Kilo's nested provider.magpie.options, unwiring left "provider": {"magpie": {}} behind, and nothing above the leaf was ever removed. restore had no record of which parent objects magpie itself had created, so it could not clean up.

Change (branch fix/1816 on my fork, single commit ba653678):

  • pluggedStash gains Parents []string (internal/agent/plugged.go:60): the objects above a nested key that the file had nothing of when magpie first wrote it — magpie's own making, recorded at wire time (plugged.go:382). A parent the user already had, empty or not, is never recorded.
  • After deleting the leaf keys, restore (and wire on re-config, where a model move can empty a branch) runs dropEmpty (plugged.go:252): every recorded parent that its removals left as a {} shell is taken out, innermost first, so a parent emptied by its own emptied child goes too. One the user filled in the meantime isn't a shell and stays; one that can't be read back is unknown, not empty, and stays.
  • edit.SplitPath is exported (internal/edit/json.go) so the JSON key path splits the way the writer splits it (a \.-escaped dot stays one key).
  • DelJSON's documented collapse-to-{} behavior for its other callers is unchanged.

Red → green

TestPluggedEmptyParentsGoWithTheirKeys (new, internal/agent/plugged_1816_test.go) wires a Kilo-shaped plugin's nested keys over a user file with a comment, the user's own provider and the user's own empty object, unwires, and compares the file byte for byte; it then rewires and unwires a second time. On the pre-fix code it fails with exactly the reported residue:

"provider": { "magpie": {}, ... }
"extra": { "magpie": {} }

With the change it passes, and the user's file comes back as it was.

Verification

  • go test -tags nogui ./internal/agent ./internal/edit -count=1 → 1706 passed
  • go test -tags nogui ./internal/agentplug -count=1 → passed
  • gofmt -l on the changed files → clean
  • GOOS=windows go vet -tags nogui ./internal/agent ./internal/edit → clean; same for GOOS=linux
  • Not run: the GUI/TUI/CLI suites, and a real Kilo plugin

Sibling sites checked: the Library's writers that delete magpie's own keys already collapse the parent they emptied (internal/library/rtk.go:422-428, internal/library/hooks.go:757-763, internal/library/mcp_tools_off.go:300-304); the agent sites that delete a user-owned object (model in qoder, env in Claude's settings) leave it, as this issue's rule requires. plugged was the one site with no such handling.

Cherry-pick:

git fetch https://github.com/Niko-Wu/magpie.git fix/1816 && git cherry-pick ba653678

Niko-Wu avatar Oct 15 '26 03:10 Niko-Wu