Files
magpie/internal/agent/kimi_subagents_test.go
T
yetone 618bfc1a95 kimi: pick Kimi Code's subagent pool and its effort (XiaoLing_Arisa on X)
XiaoLing_Arisa asked on X for Kimi Code's subagent pool (子代理池) on the
Agents page: which models its subagents may run on, and how hard they
think. Kimi Code 2.x keeps them in config.toml's [secondary_model]:
default_model (the legacy `model` stands for it), the pool
[secondary_model.models] ("alias" = "when to use it") and default_effort.

- internal/agent/kimi_secondary.go: three fields, subagent_model,
  subagent_pool and subagent_effort, written the way Kimi Code takes
  them. Its validator refuses a pool that lacks the default, a name with
  no [models."…"] table, and an effort a pool model can't take, so a
  default goes into the pool, a pool without the default gets its first,
  and only the levels every pool model takes are offered
  (kimiPoolLevels). A magpie model goes in only while [providers.magpie]
  is there. Keys are set in place; hints, other keys and comments stay.
  Before Sync or Unwire drops a magpie model table, its name leaves the
  section (kimiPruneSecondary). The default goes back to the user's own
  (stashed at kimi:<path>:secondary) or to the pool's first, and an empty
  section goes. The user's own pool of their own models is never touched.
  kimi-cli (legacy) has no such section. A stopped WSL distro shows none
  of the three, since each reads the config (#3896).
- GUI: the three fields form one Subagents line in the opened row: the
  model, then the effort (disabled until a model is set), then a
  multi-pick pool menu that stays open between ticks. The model's tick
  can't be removed. The picker's note says an unset model runs each
  subagent on the lead's model. The line is drawn by kimisub.js, which
  Details imports before its first slide so the sheet measures it.
- Size budgets +457 (reason in core_budget_test.go). Translations in
  zh, zh-TW, ja and de. kimisub.js is added to old-webkit's list of
  scripts loaded on demand. The Kimi bullet in agent-wiring.md is
  updated.

Run at this tree on mica (mica-run):
- go test -tags nogui -count=1 ./internal/agent ./internal/gui: ok,
  including TestKimiSubagentPool*, TestWSLColdPlaceOpensNothing and
  TestCoreScriptBudget. An earlier run of these packages found the cold
  read and the budget overrun; both are fixed here.
- go vet ./... on darwin, GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: ok. go build: ok. gofmt -l .: empty.
- GUI: kimi-subagent-pool passes 20 of 20 (Chromium and WebKit, 5
  languages, 1100 and 560px). kimi-thinking passes. agent-details-ready,
  old-webkit and i18n-keys have 35 reds, and origin/main ff88ec264 has
  the same 35, all filed: #6373, #6369 (agent-details-ready), #6030,
  #5919 (old-webkit), #6877 (i18n-keys).
- Broken by hand: with Details not waiting for kimisub.js,
  kimi-subagent-pool goes 10 of 10 red in Chromium ("the Subagents line
  is there when Details first draws").
- Real Kimi Code 2.6.2 (npm) in a sandbox HOME with a fake SSE server:
  the pool and effort written here are read and used. A pool naming a
  model with no table is refused when a subagent starts. A bad effort is
  not refused at runtime, but magpie refuses it anyway, as the validator
  does.

Not run or tried:
- The full go test ./... and the full GUI suite: mica and mote were both
  past their load limits with other sessions' suites. Only the packages
  and GUI files above were run.
- A real Moonshot account. The subagent's "timed out" in the sandbox run
  came from the fake server.
2026-11-17 14:30:55 +08:00

295 lines
9.3 KiB
Go

package agent
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/pelletier/go-toml/v2"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// kimiSubagentHome is a sandbox HOME with a Kimi Code config holding the
// user's own model, and magpie's provider "think" with three models: deep
// (high, max), wide (low, medium) and plain (no thinking).
func kimiSubagentHome(t *testing.T, config string) (home, path string) {
t.Helper()
home = t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("KIMI_SHARE_DIR", "")
t.Setenv("KIMI_CODE_HOME", "")
if err := provider.Save(provider.Provider{
ID: "think", Name: "Think", Chat: "https://example.test/v1", Key: "key",
Models: []string{"deep", "wide", "plain"},
}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("think", "https://example.test/v1", []catalog.Model{
{ID: "deep", Efforts: []string{"high", "max"}},
{ID: "wide", Efforts: []string{"low", "medium"}},
{ID: "plain"},
}); err != nil {
t.Fatal(err)
}
path = filepath.Join(home, ".kimi-code", "config.toml")
writeFile(t, path, config)
return home, path
}
// kimiOwnConfig is Kimi Code after its own sign-in, with a model of the
// user's own that thinks at low to high.
const kimiOwnConfig = `default_model = "kimi-code/kimi-for-coding"
[models."kimi-code/kimi-for-coding"]
provider = "managed:kimi-code"
model = "kimi-for-coding"
max_context_size = 262144
capabilities = ["thinking", "tool_use"]
support_efforts = ["low", "medium", "high"]
[providers."managed:kimi-code"]
type = "kimi"
base_url = "https://api.kimi.com/coding/v1"
api_key = ""
`
// kimiSubagentsValid fails the test where Kimi Code 2.6.2 would refuse the
// config at a session's start (session/subagent/configSection.ts,
// assertValidSubagentModelPool and assertValidSubagentDefaultEffort), and
// returns its [secondary_model].
func kimiSubagentsValid(t *testing.T, path string) map[string]any {
t.Helper()
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var c struct {
Secondary map[string]any `toml:"secondary_model"`
Models map[string]struct {
Capabilities []string `toml:"capabilities"`
SupportEfforts []string `toml:"support_efforts"`
} `toml:"models"`
}
if err := toml.Unmarshal(b, &c); err != nil {
t.Fatalf("%v\n%s", err, b)
}
s := c.Secondary
if s == nil {
return nil
}
def, _ := s["default_model"].(string)
if def == "" {
def, _ = s["model"].(string)
}
var pool []string
if m, ok := s["models"].(map[string]any); ok {
if def == "" {
t.Fatalf("a pool with no default_model:\n%s", b)
}
if _, ok := m[def]; !ok {
t.Fatalf("default_model %q not in the pool:\n%s", def, b)
}
for k := range m {
pool = append(pool, k)
}
} else if def != "" {
pool = []string{def}
}
for _, k := range pool {
if _, ok := c.Models[k]; !ok {
t.Fatalf("pool entry %q names no model:\n%s", k, b)
}
}
eff, _ := s["default_effort"].(string)
if eff == "" || pool == nil {
return s
}
for _, k := range pool {
m := c.Models[k]
always := slices.Contains(m.Capabilities, "always_thinking")
thinks := always || slices.Contains(m.Capabilities, "thinking")
switch {
case eff == "off" && always:
t.Fatalf("off on %q, which always thinks:\n%s", k, b)
case eff == "off":
case !thinks:
t.Fatalf("effort %q on %q, which doesn't think:\n%s", eff, k, b)
case len(m.SupportEfforts) > 0 && eff != "on" && !slices.Contains(m.SupportEfforts, eff):
t.Fatalf("effort %q not among %q's %v:\n%s", eff, k, m.SupportEfforts, b)
}
}
return s
}
func kimiOptionValues(f *Field, vals map[string]string) []string {
var out []string
for _, o := range f.Options(vals) {
out = append(out, o.Value)
}
return out
}
// XiaoLing_Arisa on X: Kimi Code's subagent pool and its effort, picked
// from magpie's models, written as Kimi reads them; each write is one Kimi
// takes, and Disconnect puts back the subagent model the user had.
func TestKimiSubagentPool(t *testing.T) {
home, path := kimiSubagentHome(t, kimiOwnConfig+`
[secondary_model]
default_model = "kimi-code/kimi-for-coding"
`)
a := kimi(home)
sub, pool, eff := a.Field("subagent_model"), a.Field("subagent_pool"), a.Field("subagent_effort")
if sub == nil || pool == nil || eff == nil {
t.Fatal("no subagent fields")
}
if sub.Get() != "kimi-code/kimi-for-coding" || pool.Get() != "" {
t.Fatalf("read: %q %q", sub.Get(), pool.Get())
}
// not connected: magpie's models have no tables Kimi could resolve
if err := sub.Set("magpie/think/deep"); err == nil {
t.Fatal("a magpie model went in with magpie not connected")
}
if slices.ContainsFunc(kimiOptionValues(sub, a.Values()), func(v string) bool { return strings.HasPrefix(v, "magpie/") }) {
t.Fatal("magpie's models offered before it is connected")
}
if err := a.Apply("model", "magpie/think/deep"); err != nil {
t.Fatal(err)
}
if !slices.Contains(kimiOptionValues(pool, a.Values()), "magpie/think/wide") {
t.Fatalf("pool options: %v", kimiOptionValues(pool, a.Values()))
}
// a pool of magpie's: the user's default isn't in it, so the first is
if err := pool.Set("magpie/think/deep\nmagpie/think/wide"); err != nil {
t.Fatal(err)
}
s := kimiSubagentsValid(t, path)
if s["default_model"] != "magpie/think/deep" || pool.Get() != "magpie/think/deep\nmagpie/think/wide" {
t.Fatalf("pool: %v %q", s, pool.Get())
}
// deep and wide share no level: off only
if got := kimiOptionValues(eff, a.Values()); !slices.Equal(got, []string{"off"}) {
t.Fatalf("effort options for deep+wide: %v", got)
}
if err := eff.Set("high"); err == nil {
t.Fatal("high taken for wide, which hasn't it")
}
// the user's description of an entry stays through a change
b, _ := os.ReadFile(path)
os.WriteFile(path, []byte(strings.Replace(string(b), `"magpie/think/deep" = ""`, `"magpie/think/deep" = "hard problems"`, 1)), 0o644)
if err := pool.Set("magpie/think/deep"); err != nil {
t.Fatal(err)
}
s = kimiSubagentsValid(t, path)
if m, _ := s["models"].(map[string]any); m["magpie/think/deep"] != "hard problems" || len(m) != 1 {
t.Fatalf("description: %v", s)
}
if got := kimiOptionValues(eff, a.Values()); !slices.Equal(got, []string{"off", "high", "max"}) {
t.Fatalf("effort options for deep: %v", got)
}
if err := eff.Set("MAX"); err != nil {
t.Fatal(err)
}
if s = kimiSubagentsValid(t, path); s["default_effort"] != "max" {
t.Fatalf("effort: %v", s)
}
// wide joins: max is none of its, so the effort goes rather than
// leaving a config Kimi refuses
if err := pool.Set("magpie/think/deep\nmagpie/think/wide"); err != nil {
t.Fatal(err)
}
if s = kimiSubagentsValid(t, path); s["default_effort"] != nil {
t.Fatalf("max kept for wide: %v", s)
}
// the default picked as Kimi's own /secondary-model does: into the pool
if err := sub.Set("magpie/think/plain"); err != nil {
t.Fatal(err)
}
s = kimiSubagentsValid(t, path)
if m, _ := s["models"].(map[string]any); s["default_model"] != "magpie/think/plain" || len(m) != 3 {
t.Fatalf("default: %v", s)
}
// Disconnect: magpie's models out, the user's subagent model back
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
s = kimiSubagentsValid(t, path)
b, _ = os.ReadFile(path)
if s["default_model"] != "kimi-code/kimi-for-coding" || s["models"] != nil || strings.Contains(string(b), "magpie") {
t.Fatalf("disconnect:\n%s", b)
}
}
// A model leaving magpie's catalog leaves the pool before its table goes,
// so Kimi never meets a pool naming a model it lacks.
func TestKimiSubagentPoolSync(t *testing.T) {
home, path := kimiSubagentHome(t, kimiOwnConfig)
a := kimi(home)
if err := a.Apply("model", "magpie/think/deep"); err != nil {
t.Fatal(err)
}
if err := a.Field("subagent_pool").Set("magpie/think/wide\nkimi-code/kimi-for-coding"); err != nil {
t.Fatal(err)
}
if s := kimiSubagentsValid(t, path); s["default_model"] != "magpie/think/wide" {
t.Fatalf("pool: %v", s)
}
if err := provider.Save(provider.Provider{
ID: "think", Name: "Think", Chat: "https://example.test/v1", Key: "key",
Models: []string{"deep", "plain"},
}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
s := kimiSubagentsValid(t, path)
m, _ := s["models"].(map[string]any)
if s["default_model"] != "kimi-code/kimi-for-coding" || len(m) != 1 {
b, _ := os.ReadFile(path)
t.Fatalf("after sync:\n%s", b)
}
// Default: subagents on the lead's model, the section gone
if err := a.Field("subagent_model").Set(""); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(path); strings.Contains(string(b), "secondary_model") {
t.Fatalf("default:\n%s", b)
}
}
// The user's own pool, of their own models, is theirs: connecting and
// disconnecting leave it as written, comments and all.
func TestKimiSubagentPoolKeepsTheUsers(t *testing.T) {
own := kimiOwnConfig + `
[secondary_model]
# subagents on the plan's model
default_model = "kimi-code/kimi-for-coding"
default_effort = "medium"
[secondary_model.models]
"kimi-code/kimi-for-coding" = "everything"
`
home, path := kimiSubagentHome(t, own)
a := kimi(home)
if err := a.Apply("model", "magpie/think/deep"); err != nil {
t.Fatal(err)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(path); string(b) != own {
t.Fatalf("the user's pool changed:\n%s", b)
}
}