Files
magpie/internal/agent/plugged_order_test.go
T
yetone c3bbf16022 agentplug: an agent plugin's keys are written in the order it gave them (#1890)
What: connect's result is taken from the module as JSON text (moejs
AppendJSON keeps JS insertion order) instead of rt.ToGo, which made a Go
map at every level. An object or array connect returns reaches the writer
as a json.RawMessage, so edit.SetJSON writes it in the plugin's order;
strings, numbers, booleans and null are their Go values as before (the
gateway-key mark still matches a string). edit.SetYAML now takes a
json.RawMessage too, as a block-style node in the text's order (it would
have written the bytes as a list of ints), and the env format writes it
compact as before.

Why: Kilo's agent plugin (magpie-community/plugins#90) returns each
model's effort variants weakest first, low, medium, high, the order Kilo
lists them in. Go maps marshal sorted, so Kilo showed high, low, medium.
magpie's own OpenCode wiring already keeps order (openCodeVariants).
The key paths connect returns are still applied in sorted order: each is
a separate edit, and the user's file around them is untouched.

Siblings: rt.ToGo's other caller (middleware's string lists) writes no
file; the Library's MCP block for a plugin agent is built by magpie, not
returned by the plugin.

Run:
- TestPluggedKeepsPluginOrder (Kilo-shaped connect, a kilo.jsonc with a
  comment, a theme, $schema, a provider of the user's own with keys
  zeta, alpha and a trailing key) and TestPluggedKeepsPluginOrderYAMLAndEnv.
  With agentplug.go put back: FAIL, variants written high, low, medium
  and provider keys sorted. With yaml.go put back: FAIL, the object
  written as a list of byte values. Both pass with the change.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., the three builds: OK.
- go test -tags nogui -timeout 40m ./... under a scratch HOME: all ok.
  (A first run with the default 10m timeout timed out in
  internal/gateway while four other sessions' suites ran on this box and
  the disk was full; the gateway package took 973s under that load and
  passed.)
- Real use, sandbox HOME: this build, `magpie plugin add` of
  packages/kilo at plugins PR #90 head b67360cb7, a provider whose
  fake-chat has efforts stored high, low, medium; `magpie kilo
  magpie/fakeup/fake-chat` wrote variants low, medium, high with the
  user's comment, $schema, theme and own provider in place, and
  @kilocode/cli 7.8.8 `kilo models magpie --verbose` lists them low,
  medium, high. `magpie kilo default` put the file back; the only
  difference is Kilo's own one-time permission migration.
2026-10-13 10:55:45 +08:00

119 lines
4.1 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
)
// kiloSrc is the shape of magpie-community/plugins' Kilo agent (#90): one
// provider block whose models' effort variants go weakest first, the order
// Kilo lists them in.
const kiloSrc = `
export const agent = { id: "kilo", name: "Kilo Code", config: "~/.config/kilo/kilo.jsonc", format: "json", model: "model", prefix: "magpie/" };
export function connect({ gateway, models }) {
const catalog = {};
for (const m of models) {
catalog[m.id] = { name: m.name ?? m.id, variants: { low: { reasoningEffort: "low" }, medium: { reasoningEffort: "medium" }, high: { reasoningEffort: "high" } } };
}
return { "provider.magpie": { name: "magpie", npm: "@ai-sdk/openai-compatible", options: { baseURL: gateway.v1, apiKey: gateway.key }, models: catalog } };
}
`
// inOrder fails unless each of words is in s, each after the one before.
func inOrder(t *testing.T, s string, words ...string) {
t.Helper()
at := -1
for _, w := range words {
i := strings.Index(s[at+1:], w)
if i < 0 {
t.Fatalf("%s missing or out of order (want %q):\n%s", w, words, s)
}
at += 1 + i
}
}
// A plugin's keys are written in the order the plugin gives them, not
// sorted (#1890): Kilo showed its variants high, low, medium. The user's
// own keys, comments and order stay as they were.
func TestPluggedKeepsPluginOrder(t *testing.T) {
home := pluggedHome(t, kiloSrc)
path := filepath.Join(home, ".config", "kilo", "kilo.jsonc")
os.MkdirAll(filepath.Dir(path), 0o755)
mine := `{
"$schema": "https://kilo.ai/config.json",
// mine
"theme": "tokyonight",
"provider": {
"mine": { "zeta": 1, "alpha": 2 }
},
"autoupdate": false
}
`
os.WriteFile(path, []byte(mine), 0o644)
a, err := Find("kilo")
if err != nil {
t.Fatal(err)
}
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(path)
s := string(b)
inOrder(t, s, `"$schema"`, "// mine", `"theme": "tokyonight"`, `"provider"`, `"zeta": 1`, `"alpha": 2`, `"autoupdate": false`)
inOrder(t, s, `"magpie"`, `"name": "magpie"`, `"npm"`, `"options"`, `"baseURL"`, `"apiKey"`, `"models"`)
// magpie's catalog order, then each model's variants weakest first
inOrder(t, s, `"deepseek/pro"`, `"low"`, `"medium"`, `"high"`, `"deepseek/flash"`, `"low"`, `"medium"`, `"high"`)
// its own model puts the file back as it was
if err := a.Field("model").Set("anthropic/claude"); err != nil {
t.Fatal(err)
}
b, _ = os.ReadFile(path)
if s := string(b); strings.Contains(s, `"magpie"`) || !strings.Contains(s, `"mine": { "zeta": 1, "alpha": 2 }`) || !strings.Contains(s, "// mine") {
t.Fatalf("own:\n%s", s)
}
}
// The same in a YAML file and an env file: an object a plugin gives keeps
// its order, and a string that reads as another type stays a string.
func TestPluggedKeepsPluginOrderYAMLAndEnv(t *testing.T) {
home := pluggedHome(t, `
export const agent = { id: "yam", config: "~/.yam.yml", model: "model" };
export function connect({ gateway }) {
return { "llm": { zeta: "1", alpha: true, mid: [3, "x"], key: gateway.key } };
}`, `
export const agent = { id: "envy", config: "~/.envy", format: "env", model: "MODEL" };
export function connect() {
return { "EXTRA": { zeta: 1, alpha: 2 }, "N": 5 };
}`)
yml := filepath.Join(home, ".yam.yml")
os.WriteFile(yml, []byte("# mine\ntheme: dark\n"), 0o644)
y, err := Find("yam")
if err != nil {
t.Fatal(err)
}
if err := y.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(yml)
inOrder(t, string(b), "# mine", "theme: dark", "llm:", `zeta: "1"`, "alpha: true", "mid:", "- 3", "- x", "key: ")
if strings.Contains(string(b), "{") {
t.Fatalf("flow style:\n%s", b)
}
e, err := Find("envy")
if err != nil {
t.Fatal(err)
}
if err := e.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
b, _ = os.ReadFile(filepath.Join(home, ".envy"))
if !strings.Contains(string(b), `EXTRA={"zeta":1,"alpha":2}`) && !strings.Contains(string(b), `EXTRA='{"zeta":1,"alpha":2}'`) {
t.Fatalf("env:\n%s", b)
}
inOrder(t, string(b), "N=5")
}