* agent: Crush's models picked in magpie go to its data file, which it reads after crush.json Crush saves what its own model picker chooses to its data file, $XDG_DATA_HOME/crush/crush.json or ~/.local/share/crush/crush.json (%LOCALAPPDATA%\crush\crush.json on Windows), and loads that file after ~/.config/crush/crush.json, so a models.large or models.small there wins. On Linux and macOS magpie wrote its picks to ~/.config/crush/crush.json only. Once somebody had picked a model inside Crush (its first run asks for one), a model set in magpie never took effect, while magpie read it back from the file it wrote and showed it as set. The picks and the large model's effort are now read as Crush merges the two files, the data file over crush.json, and written to the data file. Unsetting one takes it out of both, and magpie's provider still goes with the last pick on it. The provider stays in crush.json, beside the library's MCP servers. On Windows the two paths are one file, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * agent: a model magpie picks for Crush replaces Crush's whole model object Crush's picker saves the whole models.<type> object in its data file, with its per-model settings (max_tokens, think, reasoning_effort, sampling, provider_options), and applies them to whatever model the object names. setPick only rewrote provider and model, so a max_tokens picked for another model went upstream with magpie's, and magpie's effort row showed an effort it never set. Write the whole object instead. Only an effort magpie set itself is kept: one on a large pick that is already magpie's and carries nothing but provider, model and reasoning_effort. When magpie is first to make Crush's data file, which holds API keys, make it 0600. * agents: Crush's data file reaches crushAt, so WSL's Crush gets its picks where Crush reads them too Main moved Crush's wiring into crushAt(at, path); the data-file path the pick fields read and write is now passed in beside path: crush() works it out as before (XDG_DATA_HOME or ~/.local/share/crush, LOCALAPPDATA's crush on Windows), crushIn uses the WSL home's ~/.local/share/crush. The effort test sandboxes XDG_DATA_HOME as it does the other XDG dirs, since the package's TestMain shares one. * agents: test that Crush in WSL picks in the distro's data file A pick magpie makes for Crush in a WSL distro is read from and written to <distro home>/.local/share/crush/crush.json, never this machine's data file, whatever XDG_DATA_HOME says on it. The test fails when crushIn's data path is the config file or follows the host's XDG_DATA_HOME. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
245 lines
9.0 KiB
Go
245 lines
9.0 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// crushHome is effortHome with Crush's data folder in the sandbox too, and
|
|
// the path of the data file Crush reads there: its GlobalConfigData, which
|
|
// is ~/.local/share/crush/crush.json, or %LOCALAPPDATA%\crush\crush.json on
|
|
// Windows.
|
|
func crushHome(t *testing.T) (home, data string) {
|
|
t.Helper()
|
|
home = effortHome(t)
|
|
t.Setenv("XDG_DATA_HOME", "")
|
|
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
|
|
data = filepath.Join(home, ".local", "share", "crush", "crush.json")
|
|
if runtime.GOOS == "windows" {
|
|
data = filepath.Join(home, "AppData", "Local", "crush", "crush.json")
|
|
}
|
|
return home, data
|
|
}
|
|
|
|
// Crush saves what its own model picker chooses to its data file, and reads
|
|
// that after crush.json, so a pick there wins over one in crush.json. Once
|
|
// somebody has picked a model in Crush (its first run asks for one), the
|
|
// model magpie sets has to go where Crush reads it last, and what magpie
|
|
// shows has to be what Crush will use.
|
|
func TestCrushPickWhereCrushReadsIt(t *testing.T) {
|
|
home, data := crushHome(t)
|
|
cr := crush(home, filepath.Join(home, ".config"))
|
|
writeFile(t, data, `{"models":{"large":{"model":"m-mine","provider":"mine"}}}`)
|
|
|
|
large := cr.Field("model")
|
|
if got := large.Get(); got != "mine/m-mine" {
|
|
t.Fatalf("large reads %q, not the pick Crush uses", got)
|
|
}
|
|
if err := large.Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _ := edit.GetJSON(data, "models.large.provider"); p != "magpie" {
|
|
t.Fatalf("Crush still picks %q from its data file:\n%s", p, readFile(data))
|
|
}
|
|
if m, _ := edit.GetJSON(data, "models.large.model"); m != "deepseek/pro" {
|
|
t.Fatalf("data file model %q:\n%s", m, readFile(data))
|
|
}
|
|
if got := large.Get(); got != "magpie/deepseek/pro" {
|
|
t.Fatalf("large reads %q", got)
|
|
}
|
|
// the provider stays where it was, with the library's MCP servers
|
|
if _, ok := edit.GetJSON(cr.Path, "providers.magpie.base_url"); !ok {
|
|
t.Fatalf("no magpie provider in %s:\n%s", cr.Path, readFile(cr.Path))
|
|
}
|
|
if cr.Check() != "" {
|
|
t.Fatalf("check: %s", cr.Check())
|
|
}
|
|
|
|
// the effort is kept with the pick
|
|
effort := cr.Field("effort")
|
|
if err := effort.Set("high"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e, _ := edit.GetJSON(data, "models.large.reasoning_effort"); e != "high" || effort.Get() != "high" {
|
|
t.Fatalf("effort %q:\n%s", effort.Get(), readFile(data))
|
|
}
|
|
|
|
small := cr.Field("small")
|
|
if err := small.Set("magpie/deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// unset, a pick is gone from both files, and the provider goes with
|
|
// the last pick on it
|
|
if err := large.Set(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if large.Get() != "" || effort.Get() != "" {
|
|
t.Fatalf("large %q, effort %q after unset:\n%s", large.Get(), effort.Get(), readFile(data))
|
|
}
|
|
if _, ok := edit.GetJSON(cr.Path, "providers.magpie"); !ok {
|
|
t.Fatal("the provider went while small still uses it")
|
|
}
|
|
if err := small.Set(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, f := range []string{cr.Path, data} {
|
|
if _, ok := edit.GetJSON(f, "models.small"); ok {
|
|
t.Fatalf("a small pick is left in %s:\n%s", f, readFile(f))
|
|
}
|
|
}
|
|
if _, ok := edit.GetJSON(cr.Path, "providers.magpie"); ok {
|
|
t.Fatalf("the provider is left with no pick on it:\n%s", readFile(cr.Path))
|
|
}
|
|
}
|
|
|
|
// A pick magpie wrote to crush.json before it wrote Crush's data file still
|
|
// reads as set where Crush has picked nothing itself, and one Crush's own
|
|
// pick hides reads as that pick, since it is the one Crush uses.
|
|
func TestCrushPickInCrushJSON(t *testing.T) {
|
|
home, data := crushHome(t)
|
|
cr := crush(home, filepath.Join(home, ".config"))
|
|
if cr.Path == data {
|
|
t.Skip("crush.json is Crush's data file here")
|
|
}
|
|
writeFile(t, cr.Path, `{"providers":{"magpie":{"base_url":"x"}},"models":{"large":{"provider":"magpie","model":"deepseek/pro"}}}`)
|
|
if got := cr.Field("model").Get(); got != "magpie/deepseek/pro" {
|
|
t.Fatalf("large reads %q", got)
|
|
}
|
|
writeFile(t, data, `{"models":{"large":{"model":"m-mine","provider":"mine"}}}`)
|
|
if got := cr.Field("model").Get(); got != "mine/m-mine" {
|
|
t.Fatalf("large reads %q, not the pick Crush uses", got)
|
|
}
|
|
}
|
|
|
|
// Crush's picker saves the whole model object in its data file: the model
|
|
// and the settings it picked for that model. A model magpie picks takes the
|
|
// whole object's place, so none of those settings ride along to it (a
|
|
// max_tokens meant for another model would go upstream with magpie's), and
|
|
// magpie's effort row doesn't show an effort magpie never set.
|
|
func TestCrushPickReplacesCrushsModelObject(t *testing.T) {
|
|
home, data := crushHome(t)
|
|
cr := crush(home, filepath.Join(home, ".config"))
|
|
writeFile(t, data, `{
|
|
"models": {
|
|
"large": {"model":"gpt-5","provider":"openai","reasoning_effort":"medium","max_tokens":128000,"think":true,"temperature":0.2,"top_p":0.9,"provider_options":{"x":1}},
|
|
"small": {"model":"gpt-5-mini","provider":"openai","max_tokens":4096,"think":true}
|
|
},
|
|
"providers": {"openai": {"api_key": "sk-keep"}},
|
|
"recent_models": {"large": [{"model":"gpt-5","provider":"openai"}]}
|
|
}`)
|
|
|
|
if err := cr.Field("model").Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cr.Field("small").Set("magpie/deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, typ := range []string{"large", "small"} {
|
|
raw, _ := edit.GetJSON(data, "models."+typ)
|
|
var obj map[string]any
|
|
if err := json.Unmarshal([]byte(raw), &obj); err != nil {
|
|
t.Fatalf("models.%s: %v\n%s", typ, err, readFile(data))
|
|
}
|
|
if len(obj) != 2 || obj["provider"] != "magpie" {
|
|
t.Fatalf("models.%s after magpie's pick: %s", typ, raw)
|
|
}
|
|
}
|
|
if e := cr.Field("effort").Get(); e != "" {
|
|
t.Fatalf("effort row shows %q, which magpie never set", e)
|
|
}
|
|
// the rest of the data file is Crush's and stays
|
|
if k, _ := edit.GetJSON(data, "providers.openai.api_key"); k != "sk-keep" {
|
|
t.Fatalf("api key gone:\n%s", readFile(data))
|
|
}
|
|
if _, ok := edit.GetJSON(data, "recent_models.large"); !ok {
|
|
t.Fatalf("recent_models gone:\n%s", readFile(data))
|
|
}
|
|
|
|
// an effort magpie set stays with its next pick on magpie
|
|
if err := cr.Field("effort").Set("high"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cr.Field("model").Set("magpie/deepseek/max"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e := cr.Field("effort").Get(); e != "high" {
|
|
t.Fatalf("magpie's own effort %q after a new pick:\n%s", e, readFile(data))
|
|
}
|
|
|
|
// once Crush's picker has saved its own object again, magpie's next pick
|
|
// drops all of it, the effort Crush saved too
|
|
writeFile(t, data, `{"models":{"large":{"model":"deepseek/pro","provider":"magpie","reasoning_effort":"low","max_tokens":64000}}}`)
|
|
if err := cr.Field("model").Set("magpie/deepseek/max"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if raw, _ := edit.GetJSON(data, "models.large"); strings.Contains(raw, "max_tokens") || strings.Contains(raw, "reasoning_effort") {
|
|
t.Fatalf("Crush's settings ride along: %s", raw)
|
|
}
|
|
}
|
|
|
|
// Crush keeps API keys in its data file, so when magpie is first to make it
|
|
// the file is readable by its owner only.
|
|
func TestCrushDataFileMadePrivate(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("no unix permission bits")
|
|
}
|
|
home, data := crushHome(t)
|
|
cr := crush(home, filepath.Join(home, ".config"))
|
|
if err := cr.Field("model").Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st, err := os.Stat(data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if st.Mode().Perm() != 0o600 {
|
|
t.Fatalf("data file mode %v", st.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
// Crush in a WSL distro has the same data file over its crush.json, in the
|
|
// distro's home: <distro home>/.local/share/crush/crush.json, whatever
|
|
// Windows' own XDG_DATA_HOME or LOCALAPPDATA say. A pick made in magpie goes
|
|
// there and is read back from there.
|
|
func TestCrushInWSLPicksInTheDistrosDataFile(t *testing.T) {
|
|
home, _ := crushHome(t)
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "windows-data"))
|
|
root := t.TempDir()
|
|
d := distro{Name: "Ubuntu", Home: "/home/me", Root: root, Running: true, Mirrored: true}
|
|
cr := crushIn(d.place("crush@wsl:Ubuntu"))
|
|
data := d.local("/home/me/.local/share/crush/crush.json")
|
|
if want := d.local("/home/me/.config/crush/crush.json"); cr.Path != want {
|
|
t.Fatalf("config %s, want %s", cr.Path, want)
|
|
}
|
|
writeFile(t, data, `{"models":{"large":{"model":"m-mine","provider":"mine"}}}`)
|
|
|
|
large := cr.Field("model")
|
|
if got := large.Get(); got != "mine/m-mine" {
|
|
t.Fatalf("large reads %q, not the pick in the distro's data file", got)
|
|
}
|
|
if err := large.Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _ := edit.GetJSON(data, "models.large.provider"); p != "magpie" {
|
|
t.Fatalf("the distro's Crush still picks %q:\n%s", p, readFile(data))
|
|
}
|
|
if got := large.Get(); got != "magpie/deepseek/pro" {
|
|
t.Fatalf("large reads %q", got)
|
|
}
|
|
for _, other := range []string{
|
|
filepath.Join(home, "windows-data", "crush", "crush.json"),
|
|
filepath.Join(home, ".local", "share", "crush", "crush.json"),
|
|
filepath.Join(home, "AppData", "Local", "crush", "crush.json"),
|
|
} {
|
|
if _, err := os.Stat(other); err == nil {
|
|
t.Fatalf("the distro's pick went to this machine's %s", other)
|
|
}
|
|
}
|
|
}
|