agents: a reply limit typed by hand into Pi or OpenCode is kept
sayagoodbyeplz on Discord set Pi's maxTokens for WorkBuddy's
deepseek-v4.1-flash to 128000 in models.json, and magpie's next sync
wrote models.dev's 384000 back. magpie writes its models block whole,
so a hand edit inside it was never kept.
output_kept.go records, in the stash, per file and model, every reply
limit magpie has wanted there (the last 8). A value in the file that is
none of them is the user's, and the next sync keeps it. Pi's
providers.magpie.models[].maxTokens and OpenCode's
provider.magpie.models.<id>.limit.output go through it. A Max output the
user sets in magpie (Model settings, Advanced, or `magpie model output`)
is still written over the hand edit, since that is their word in magpie.
With no record (a model new to the file, an unreadable stash) the value
is magpie's, as before. The record keeps every wanted value, not only
the last one written, because the same block is also built for Drift
and never written.
Other agents that write a reply limit are not covered yet: #5693.
Run (mote, at this tree):
- go test ./internal/agent/ (whole package): ok, again at the rebased
head on a5c97cd83 (333s); on WSL, -tags nogui, the same run is ok but
for TestGrokInstalled, red there from Windows binfmt interop (#3548)
- TestPiKeepsAMaxTokensTypedByHand, TestOpenCodeKeepsALimitOutputTypedByHand
(new): models.dev's value is followed while nothing is typed, a hand
128000 stays through syncs and a models.dev change, the user's magpie
Max output wins, and taking it away gives models.dev's again
- go vet on darwin (mote), linux (-tags nogui) and windows (WSL)
- WSL magpie-desktop, -tags nogui: go test -run 'Output|Kept|Pi|OpenCode'
./internal/agent/: ok
Broken by hand: with the piOutputsKept and openCodeOutputsKept calls
taken out, both tests fail ("written over: 300000" / "384000").
Not tried: a real Pi or OpenCode session reading the kept value.
Full suite not run at push.
Reported-by: sayagoodbyeplz (Discord)
This commit is contained in:
@@ -649,10 +649,12 @@ func magpieProviderJSONAt(shape, catalog, gw string) any {
|
||||
|
||||
// piBlockKept is theirsKept for magpie's provider in a Pi models.json at
|
||||
// path, where a sendSessionAffinityHeaders the file already has, the
|
||||
// user's own or pi-cache-optimizer's (#1103), stays as it is.
|
||||
// user's own or pi-cache-optimizer's (#1103), stays as it is, as does a
|
||||
// model's maxTokens the user typed there (piOutputsKept).
|
||||
func piBlockKept(path string, value func() any) func() any {
|
||||
return theirsKept(path, "providers."+magpieID, func() any {
|
||||
v := value()
|
||||
piOutputsKept(path, v)
|
||||
if _, set := edit.GetJSON(path, "providers."+magpieID+".compat.sendSessionAffinityHeaders"); !set {
|
||||
return v
|
||||
}
|
||||
@@ -675,10 +677,16 @@ func piBlockKept(path string, value func() any) func() any {
|
||||
// writes it under its name is magpie's: one the model has no longer, a
|
||||
// level gone or a fast mode lost, goes.
|
||||
//
|
||||
// A setCacheKey the file has already is the user's, false too (#5468).
|
||||
// A setCacheKey the file has already is the user's, false too (#5468), and
|
||||
// so is a model's limit.output they typed there (openCodeOutputsKept).
|
||||
func openCodeKept(path string, value func() any) func() any {
|
||||
return theirsKept(path, "provider."+magpieID, func() any {
|
||||
v := value()
|
||||
if block, ok := v.(map[string]any); ok {
|
||||
if ms, ok := block["models"].(orderedJSON); ok {
|
||||
openCodeOutputsKept(path, ms)
|
||||
}
|
||||
}
|
||||
if _, set := edit.GetJSON(path, "provider."+magpieID+".options.setCacheKey"); set {
|
||||
v = withoutOption(v, "options", "setCacheKey")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
// A reply limit typed by hand into an agent's file stays (sayagoodbyeplz
|
||||
// on Discord: WorkBuddy's deepseek-v4.1-flash replies 128000 tokens, not
|
||||
// the 384000 models.dev says, and Pi's maxTokens set to 128000 was written
|
||||
// back to 384000 on the next sync). magpie writes its models whole, so a
|
||||
// value in the file is told apart by what magpie has wanted there: the
|
||||
// stash keeps, per file and model, the reply limits magpie handed it
|
||||
// (outputsWantedKey), and one in the file that is none of them is the
|
||||
// user's. It stays until the user sets the model's Max output in magpie
|
||||
// (the editor's Model settings, or `magpie model output`), which is their
|
||||
// word on it there and is written over it, as is magpie's when no record
|
||||
// says what magpie wanted (a model new to the file, a stash that can't be
|
||||
// read: unknown is magpie's, as before).
|
||||
//
|
||||
// The record is of every value wanted, not only the last written: an
|
||||
// agent's block is also built to be compared (Drift) and never written,
|
||||
// and an old value of magpie's left in the file is still magpie's.
|
||||
|
||||
// outputsWantedKey is where the stash keeps the reply limits magpie has
|
||||
// wanted in the file at path, as JSON: {model: [n, …]}, newest last.
|
||||
func outputsWantedKey(path string) string { return "output:" + path + ":wanted" }
|
||||
|
||||
// outputsRemembered is how many values a model's record keeps.
|
||||
const outputsRemembered = 8
|
||||
|
||||
// outputsKept is want, the reply limit magpie hands each model of the file
|
||||
// at path, with have's, the file's own now, kept where it is the user's.
|
||||
// It records want's values as wanted.
|
||||
func outputsKept(path string, want, have map[string]int) map[string]int {
|
||||
key := outputsWantedKey(path)
|
||||
rec := map[string][]int{}
|
||||
if v := stashShared()[key]; v != "" && json.Unmarshal([]byte(v), &rec) != nil {
|
||||
rec = map[string][]int{}
|
||||
}
|
||||
out := make(map[string]int, len(want))
|
||||
next := make(map[string][]int, len(want))
|
||||
var s *settings.Settings
|
||||
for id, w := range want {
|
||||
out[id] = w
|
||||
was, known := rec[id]
|
||||
if f := have[id]; known && f > 0 && f != w && !slices.Contains(was, f) {
|
||||
if s == nil {
|
||||
l := settings.Load()
|
||||
s = &l
|
||||
}
|
||||
if !outputSetIn(*s, id) {
|
||||
out[id] = f
|
||||
}
|
||||
}
|
||||
if w <= 0 || slices.Contains(was, w) {
|
||||
next[id] = was
|
||||
continue
|
||||
}
|
||||
was = append(slices.Clone(was), w)
|
||||
if len(was) > outputsRemembered {
|
||||
was = was[len(was)-outputsRemembered:]
|
||||
}
|
||||
next[id] = was
|
||||
}
|
||||
for id, v := range next {
|
||||
if len(v) == 0 {
|
||||
delete(next, id)
|
||||
}
|
||||
}
|
||||
if !maps.EqualFunc(rec, next, slices.Equal) {
|
||||
b, _ := json.Marshal(next)
|
||||
if len(next) == 0 {
|
||||
b = nil
|
||||
}
|
||||
stash(map[string]string{key: string(b)})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// outputSetIn says the user set a Max output in magpie for the model an
|
||||
// agent lists as id, "provider/model": its own or its provider's.
|
||||
func outputSetIn(s settings.Settings, id string) bool {
|
||||
prov, _, ok := strings.Cut(id, "/")
|
||||
return ok && (s.ModelOutputs[id] > 0 || s.ModelOutputs[prov+"/*"] > 0)
|
||||
}
|
||||
|
||||
// numberOf is a JSON number as an int, 0 for anything else.
|
||||
func numberOf(v any) int {
|
||||
switch n := v.(type) {
|
||||
case int:
|
||||
return n
|
||||
case float64:
|
||||
return int(n)
|
||||
case json.Number:
|
||||
i, _ := n.Int64()
|
||||
return int(i)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// piOutputsKept keeps a maxTokens the user typed into magpie's models in
|
||||
// Pi's models.json at path (outputsKept); v is the block magpie writes.
|
||||
func piOutputsKept(path string, v any) {
|
||||
block, _ := v.(map[string]any)
|
||||
ms, _ := block["models"].([]map[string]any)
|
||||
want := map[string]int{}
|
||||
for _, e := range ms {
|
||||
if id, _ := e["id"].(string); id != "" {
|
||||
if n := numberOf(e["maxTokens"]); n > 0 {
|
||||
want[id] = n
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(want) == 0 {
|
||||
return
|
||||
}
|
||||
have := map[string]int{}
|
||||
if raw, ok := edit.GetJSON(path, "providers."+magpieID+".models"); ok {
|
||||
var cur []struct {
|
||||
ID string `json:"id"`
|
||||
MaxTokens int `json:"maxTokens"`
|
||||
}
|
||||
if json.Unmarshal([]byte(raw), &cur) == nil {
|
||||
for _, c := range cur {
|
||||
have[c.ID] = c.MaxTokens
|
||||
}
|
||||
}
|
||||
}
|
||||
kept := outputsKept(path, want, have)
|
||||
for _, e := range ms {
|
||||
if id, _ := e["id"].(string); kept[id] > 0 {
|
||||
e["maxTokens"] = kept[id]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// openCodeOutputsKept keeps a limit.output the user typed into magpie's
|
||||
// models in an OpenCode config at path (outputsKept); ms are the models
|
||||
// magpie writes.
|
||||
func openCodeOutputsKept(path string, ms orderedJSON) {
|
||||
want := map[string]int{}
|
||||
for _, p := range ms {
|
||||
e, _ := p.v.(map[string]any)
|
||||
l, _ := e["limit"].(map[string]any)
|
||||
if n := numberOf(l["output"]); n > 0 {
|
||||
want[p.k] = n
|
||||
}
|
||||
}
|
||||
if len(want) == 0 {
|
||||
return
|
||||
}
|
||||
have := map[string]int{}
|
||||
if raw, ok := edit.GetJSON(path, "provider."+magpieID+".models"); ok {
|
||||
var cur map[string]struct {
|
||||
Limit struct {
|
||||
Output int `json:"output"`
|
||||
} `json:"limit"`
|
||||
}
|
||||
if json.Unmarshal([]byte(raw), &cur) == nil {
|
||||
for id, c := range cur {
|
||||
have[id] = c.Limit.Output
|
||||
}
|
||||
}
|
||||
}
|
||||
kept := outputsKept(path, want, have)
|
||||
for _, p := range ms {
|
||||
e, _ := p.v.(map[string]any)
|
||||
if l, _ := e["limit"].(map[string]any); l != nil && kept[p.k] > 0 {
|
||||
l["output"] = kept[p.k]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// outputHome is syncHome with a model models.dev says replies 384000
|
||||
// tokens, as it says of deepseek-v4.1-flash, served by the relay.
|
||||
func outputHome(t *testing.T) string {
|
||||
t.Helper()
|
||||
home := syncHome(t)
|
||||
listOutput(t, 384000)
|
||||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"deepseek-v4.1-flash"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return home
|
||||
}
|
||||
|
||||
// listOutput is models.dev saying the model replies n tokens.
|
||||
func listOutput(t *testing.T, n int) {
|
||||
t.Helper()
|
||||
os.WriteFile(catalog.CachePath(), []byte(`{"deepseek":{"models":{"deepseek-v4.1-flash":{"id":"deepseek-v4.1-flash","name":"DeepSeek V4.1 Flash","limit":{"context":1000000,"output":`+strconv.Itoa(n)+`}}}}}`), 0o644)
|
||||
catalog.Reset()
|
||||
}
|
||||
|
||||
// sayagoodbyeplz on Discord: WorkBuddy's deepseek-v4.1-flash replies
|
||||
// 128000 tokens, not the 384000 models.dev says, and a maxTokens set to
|
||||
// 128000 by hand in Pi's models.json was written back to 384000 by the
|
||||
// next sync. One typed there stays; a Max output set in magpie is written
|
||||
// over it; and with neither, magpie's own follows models.dev.
|
||||
func TestPiKeepsAMaxTokensTypedByHand(t *testing.T) {
|
||||
home := outputHome(t)
|
||||
piModels := filepath.Join(home, ".pi", "agent", "models.json")
|
||||
writeFile(t, filepath.Join(home, ".pi", "agent", "settings.json"), `{"defaultProvider":"magpie","defaultModel":"relay/deepseek-v4.1-flash"}`)
|
||||
writeFile(t, piModels, `{"providers":{"magpie":{"name":"magpie","models":[]}}}`)
|
||||
setUpInMagpie("pi")
|
||||
const at = "providers.magpie.models.0.maxTokens"
|
||||
got := func() string { v, _ := edit.GetJSON(piModels, at); return v }
|
||||
SyncCatalog()
|
||||
if got() != "384000" {
|
||||
t.Fatalf("magpie's maxTokens = %s, want models.dev's 384000\n%s", got(), readFile(piModels))
|
||||
}
|
||||
|
||||
// models.dev changes, nothing typed: magpie's follows it
|
||||
listOutput(t, 300000)
|
||||
SyncCatalog()
|
||||
if got() != "300000" {
|
||||
t.Fatalf("after models.dev changed, maxTokens = %s, want 300000", got())
|
||||
}
|
||||
|
||||
// typed by hand: it stays through syncs, and models.dev changing
|
||||
if err := edit.SetJSON(piModels, edit.KV{Path: at, Value: 128000}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
SyncCatalog()
|
||||
if got() != "128000" {
|
||||
t.Fatalf("a maxTokens typed by hand was written over: %s", got())
|
||||
}
|
||||
listOutput(t, 384000)
|
||||
SyncCatalog()
|
||||
if got() != "128000" {
|
||||
t.Fatalf("a maxTokens typed by hand was written over when models.dev changed: %s", got())
|
||||
}
|
||||
|
||||
// set in magpie: the user's word there is written over it
|
||||
if err := provider.SetModelOutput("relay/deepseek-v4.1-flash", 100000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
SyncCatalog()
|
||||
if got() != "100000" {
|
||||
t.Fatalf("a Max output set in magpie didn't reach Pi: %s", got())
|
||||
}
|
||||
// and taken away, magpie's own again
|
||||
if err := provider.SetModelOutput("relay/deepseek-v4.1-flash", 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
SyncCatalog()
|
||||
if got() != "384000" {
|
||||
t.Fatalf("with no Max output set, maxTokens = %s, want 384000", got())
|
||||
}
|
||||
}
|
||||
|
||||
// The same for OpenCode's limit.output.
|
||||
func TestOpenCodeKeepsALimitOutputTypedByHand(t *testing.T) {
|
||||
home := outputHome(t)
|
||||
cfg := filepath.Join(home, ".config", "opencode", "opencode.json")
|
||||
writeFile(t, cfg, `{"model":"magpie/relay/deepseek-v4.1-flash","provider":{"magpie":{"name":"magpie","npm":"@ai-sdk/openai-compatible","options":{"baseURL":"http://127.0.0.1:3425/v1","apiKey":"magpie"},"models":{}}}}`)
|
||||
oc := opencode(home, filepath.Join(home, ".config"))
|
||||
const at = "provider.magpie.models.relay/deepseek-v4\\.1-flash.limit.output"
|
||||
got := func() string { v, _ := edit.GetJSON(cfg, at); return v }
|
||||
if err := oc.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got() != "384000" {
|
||||
t.Fatalf("magpie's limit.output = %q, want 384000\n%s", got(), readFile(cfg))
|
||||
}
|
||||
if err := edit.SetJSON(cfg, edit.KV{Path: at, Value: 128000}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := oc.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got() != "128000" {
|
||||
t.Fatalf("a limit.output typed by hand was written over: %s", got())
|
||||
}
|
||||
if err := provider.SetModelOutput("relay/deepseek-v4.1-flash", 100000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := oc.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got() != "100000" {
|
||||
t.Fatalf("a Max output set in magpie didn't reach OpenCode: %s", got())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user