molianyin4227 (Discord #help): models differ, so one compaction size
for every agent fits none; they asked for Codex, Claude Code and Grok
Build to each have their own, falling back to the shared one.
- settings.AgentCompacts[agent] {At, Full} for the agents that write a
compaction window (CompactAgents: codex, claude, grok); Settings.
ForAgent(agent) lays it over CompactAt/FullContext for that agent's
readers only: codexcat.entriesAt, provider codexListed, Claude Code's
writeCompact and desktopPlan, grokModelTables, the route list's
buildModels, Codex's window line (api.go) and Tuned for you
(gui/tune.go, agent/tune.go). A model's or provider's own Compact at
still comes first. An agent with none follows the shared value, so
nothing new is written for it, and an agent outside the three has no
such key and is refused (provider.SetAgentCompact).
- POST /api/settings/agent-compact {agent, own, full, at}; own false
takes the agent's value away. settingsapi.Merge keeps the map.
- GUI: a row "Long conversations in <agent>" under Long conversations,
in the agent's opened row and its sheet: Every agent / Compact at N /
Full window (segs, no select), a size box when Compact at. It lives in
agentcompact.js, imported when the agent's settings hold Long
conversations, then put in synchronously (an async put-in after a
redraw moved the page in chromium: codex-memories-settings' scroll
check). It draws again only when its own values change. i18n in
zh, zh-TW, ja, de.
- Tuned for you's Use on an agent with its own value changes that one,
stashed under tune.long.compact.<agent>; Undo puts it back.
- codex-memories-settings' "no Codex" check counts Long conversations
alone again (:not(.agent-compact)) and waits for the new row.
- docs/subsystems/agent-wiring.md: a row for it.
Core size: app.js 1,311,919 bytes, 109 under origin/main 0ee424e3e
(1,312,028): the two calls are paid for by four comments cut. The core
total is 1,534,045, still over its budget as on main (#5139), 109 less
than main's 1,534,154. No budget raised.
Run at this tree (based on origin/main 0ee424e3e, fetched just before
the commit), all on mote via mote-run:
- go test settings, settingsapi, codexcat; provider/agent/gui with
-run "Compact|Tune|Grok|SettingsSave|CodexWindow|Long": ok.
- New tests broken by hand, each red: ForAgent taken out of codexcat
(TestCodexCatalogCompactAt), grok.go (TestGrokWindowFollowsLong
Conversations), claude.go (TestClaudeCompactAt); tune's per-agent key
turned off (TestTuneAgentOwnLong: "after apply: codex {At:0
Full:true}"); the row's import call taken out of app.js
(agent-compact.test.cjs 4 of 4 red in chromium).
- GUI, chromium + webkit (en/zh/zh-TW/ja/de as each file has, narrow
and wide): agent-compact, agent-prefs, compact-at, compact-one-value,
settings-general, codex-async-compact, codex-compact-held,
codex-memories-settings, context-tune, segs-glide, i18n-keys,
old-webkit: 432 of 432 ok.
- gofmt -l clean; go vet darwin and GOOS=windows on mote, linux with
-tags gtk3 on magpie-desktop (wsl-run): ok.
- TestCoreScriptBudget: red, 1,534,045 > 1,533,802, as on main (#5139),
109 bytes lower than main.
Not tried: real Codex, Claude Code or Grok Build reading the values
(the config writers are unit-tested); the full go test ./... (after
the push). `magpie model compact` has no per-agent flag, and t3code's
route list reads the shared value.
445 lines
14 KiB
Go
445 lines
14 KiB
Go
package gui
|
||
|
||
// The Context tab's advice: for each agent, the settings that would have
|
||
// cost this user the fewest tokens over the period, worked out by
|
||
// replaying their own calls (internal/tune) against what the agent's
|
||
// config has now, and putting one in, or back, on their say.
|
||
|
||
import (
|
||
"encoding/json"
|
||
"hash/fnv"
|
||
"net/http"
|
||
"slices"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/agent"
|
||
"github.com/yetone/magpie/internal/provider"
|
||
"github.com/yetone/magpie/internal/sessions"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
"github.com/yetone/magpie/internal/tune"
|
||
)
|
||
|
||
type tuneAdvice struct {
|
||
Knob string `json:"knob"`
|
||
Model string `json:"model,omitempty"`
|
||
Setting agent.TuneSetting `json:"setting"`
|
||
Compact *tune.Compaction `json:"compact,omitempty"`
|
||
TTL *tune.TTL `json:"ttl,omitempty"`
|
||
// Own is the model whose own Compact at (its provider's, or one set
|
||
// on it) is where Codex runs now, over Long conversations: putting a
|
||
// window in Long conversations doesn't move it (hoobnn on Discord: a
|
||
// card said "In use" for 170K beside "Now 430K")
|
||
Own *tuneOwn `json:"own,omitempty"`
|
||
// Apart are the models of the range with a Compact at of their own
|
||
// beside others Long conversations moves: a Use moves those others,
|
||
// and these keep compacting where they did (hoobnn on Discord, 11-01)
|
||
Apart []tuneOwn `json:"apart,omitempty"`
|
||
// Async is Codex's background compaction at the advice, its foreground
|
||
// limit kept as the backstop; nil where it can't help
|
||
Async *tuneAsync `json:"async,omitempty"`
|
||
}
|
||
|
||
// tuneAsync is the advice as Codex's background compaction takes it: a
|
||
// share of the model's window (Percent), about Value tokens of Window.
|
||
// Held are the Codexes here too old for the share, or of no known version.
|
||
type tuneAsync struct {
|
||
Percent int `json:"percent"`
|
||
Value int `json:"value"`
|
||
Window int `json:"window"`
|
||
Setting agent.TuneSetting `json:"setting"`
|
||
Held []agent.CodexReader `json:"held,omitempty"`
|
||
}
|
||
|
||
// codexAsyncAt is the advice r as a share of the window Codex's
|
||
// background compaction starts at: the fewest tokens (a compaction in the
|
||
// background keeps no one waiting, so the wait the advice weighs doesn't
|
||
// count), rounded down, so it starts no later than that. ok is false
|
||
// where it would start no sooner than the foreground does, at the lesser
|
||
// of the limit now and 90% of the window (codex-rs ModelInfo
|
||
// auto_compact_token_limit), and where the window isn't known.
|
||
func codexAsyncAt(r *tune.Compaction) (percent, value int, ok bool) {
|
||
if r == nil || r.Window <= 0 {
|
||
return 0, 0, false
|
||
}
|
||
v := r.Best
|
||
if r.Cheapest > 0 && r.Facts.Took > 0 {
|
||
v = r.Cheapest
|
||
}
|
||
percent = max(1, min(100, v*100/r.Window))
|
||
value = r.Window * percent / 100
|
||
fg := r.Window * 9 / 10
|
||
if r.Current > 0 {
|
||
fg = min(fg, r.Current)
|
||
}
|
||
return percent, value, value < fg
|
||
}
|
||
|
||
type tuneOwn struct {
|
||
Model string `json:"model"`
|
||
Value int `json:"value"`
|
||
}
|
||
|
||
type tuneAgent struct {
|
||
Agent string `json:"agent"`
|
||
Calls int `json:"calls"`
|
||
Advice []tuneAdvice `json:"advice"`
|
||
}
|
||
|
||
type tuneState struct {
|
||
Days int `json:"days"`
|
||
Agents []tuneAgent `json:"agents"`
|
||
}
|
||
|
||
// tuneOf works the advice out of the calls the agents' files hold.
|
||
func tuneOf(cs []sessions.Call, days int, home string) tuneState {
|
||
by := map[string][]tune.Call{}
|
||
for _, c := range cs {
|
||
if c.Agent == "claude" || c.Agent == "codex" {
|
||
by[c.Agent] = append(by[c.Agent], tune.FromCalls([]sessions.Call{c})...)
|
||
}
|
||
}
|
||
return adviseOf(by, days, home)
|
||
}
|
||
|
||
// adviseOf is tuneOf from each agent's calls made tune's already.
|
||
func adviseOf(by map[string][]tune.Call, days int, home string) tuneState {
|
||
out := tuneState{Days: days, Agents: []tuneAgent{}}
|
||
for _, name := range []string{"claude", "codex"} {
|
||
calls := by[name]
|
||
if len(calls) == 0 {
|
||
continue
|
||
}
|
||
ta := tuneAgent{Agent: name, Calls: len(calls), Advice: []tuneAdvice{}}
|
||
switch name {
|
||
case "claude":
|
||
models := map[string][]tune.Call{}
|
||
var main, side []tune.Call
|
||
for _, c := range calls {
|
||
models[c.Model] = append(models[c.Model], c)
|
||
if c.Sub {
|
||
side = append(side, c)
|
||
} else {
|
||
main = append(main, c)
|
||
}
|
||
}
|
||
for m, mc := range models {
|
||
s, err := agent.TuneRead(home, name, agent.TuneCompact, m)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
// a model asked for with [1m], or seen past 200K, has the
|
||
// whole 1M; another the 200K Claude Code's own models start with
|
||
window := 200000
|
||
for _, c := range mc {
|
||
if c.Long || c.Prompt > window {
|
||
window = 1000000
|
||
break
|
||
}
|
||
}
|
||
cur, _ := strconv.Atoi(s.Value)
|
||
o := tune.CompactOpts{Min: 100000, Max: 1000000, Step: 10000, Reserve: 33000, Window: window, Current: cur}
|
||
if r := tune.Compact(mc, o); r != nil {
|
||
ta.Advice = append(ta.Advice, tuneAdvice{Knob: agent.TuneCompact, Model: m, Setting: s, Compact: r})
|
||
}
|
||
}
|
||
sort.SliceStable(ta.Advice, func(i, j int) bool {
|
||
return ta.Advice[i].Compact.Facts.Calls > ta.Advice[j].Compact.Facts.Calls
|
||
})
|
||
for _, k := range []struct {
|
||
knob string
|
||
calls []tune.Call
|
||
}{{agent.TuneCache, main}, {agent.TuneSubCache, side}} {
|
||
s, err := agent.TuneRead(home, name, k.knob, "")
|
||
if err != nil {
|
||
continue
|
||
}
|
||
if r := tune.CacheTTL(k.calls, s.Value); r != nil {
|
||
ta.Advice = append(ta.Advice, tuneAdvice{Knob: k.knob, Setting: s, TTL: r})
|
||
}
|
||
}
|
||
case "codex":
|
||
s, err := agent.TuneRead(home, name, agent.TuneCompact, "")
|
||
if err != nil {
|
||
continue
|
||
}
|
||
st := settings.Load().ForAgent("codex")
|
||
window, told, own, apart := codexWindow(calls, st, provider.Catalog(), provider.GroupFinder())
|
||
// Long conversations' value is the setting; where Codex runs
|
||
// now is that as magpie tells it for these models (their
|
||
// windows, a provider's own Compact at)
|
||
cur, _ := strconv.Atoi(s.Value)
|
||
if cur == 0 || s.Source == agent.TuneLong {
|
||
cur = told
|
||
}
|
||
// a value the user chose is never past the last one offered
|
||
o := tune.CompactOpts{Min: 50000, Max: max(1000000, window, cur, st.CompactAt), Step: 10000, Window: window, Current: cur}
|
||
if r := tune.Compact(calls, o); r != nil {
|
||
ad := tuneAdvice{Knob: agent.TuneCompact, Setting: s, Compact: r}
|
||
if own != "" && s.Source == agent.TuneLong && r.Current == told {
|
||
ad.Own = &tuneOwn{Model: own, Value: told}
|
||
}
|
||
if s.Source == agent.TuneLong {
|
||
ad.Apart = apart
|
||
}
|
||
if pct, v, ok := codexAsyncAt(r); ok {
|
||
as, _ := agent.TuneRead(home, name, agent.TuneAsync, "")
|
||
ad.Async = &tuneAsync{Percent: pct, Value: v, Window: r.Window, Setting: as, Held: agent.CodexAsyncHeldHere(home)}
|
||
}
|
||
ta.Advice = append(ta.Advice, ad)
|
||
}
|
||
}
|
||
out.Agents = append(out.Agents, ta)
|
||
}
|
||
return out
|
||
}
|
||
|
||
// codexWindow is the window Codex runs the calls' models to, and where
|
||
// magpie has it compact them when its config.toml says nothing (told: the
|
||
// largest of the models Long conversations moves, 0 when magpie tells it
|
||
// none). A model outside the catalog, or one whose window magpie doesn't
|
||
// know, leaves told to the others: it once made told 0 for the whole
|
||
// range, so "Now" was guessed from the prompts instead, and the same
|
||
// setting read 380K over 7 days and 430K today (hoobnn on Discord).
|
||
//
|
||
// A model with a Compact at of its own (provider.CompactSetIn, on it or
|
||
// its provider) compacts there whatever Long conversations says, so it
|
||
// isn't in told while another model is: over 7 days such a model's 430K
|
||
// was "Now", a Use moved every other model to the advice, and the card
|
||
// still read 430K (hoobnn, 11-01). Those models are apart, each with
|
||
// where it compacts. Only when every model of the range has one is told
|
||
// the largest of theirs, and own the model it comes from: no Use moves
|
||
// it. A window the user typed is not one of these: it gives way to a size
|
||
// tune puts in Long conversations (CompactFor's rule).
|
||
//
|
||
// A model of magpie's catalog has the window it is listed with, one the
|
||
// user typed or the vendor's, and is told the threshold the user set on
|
||
// it, else Settings › Long conversations (provider.CompactFor, as
|
||
// codexcat writes it). One whose window magpie doesn't know caps nothing
|
||
// (window 0): the largest prompt it had been sent was taken for its
|
||
// window, and a 1M model set to compact at 750K was offered no more than
|
||
// 412K (zhemu on Discord). Codex's own models, outside magpie's catalog,
|
||
// run to 272K or the largest prompt seen, as before.
|
||
func codexWindow(calls []tune.Call, st settings.Settings, entries []provider.Entry, find func(string) (provider.Group, []provider.Member, bool)) (window, told int, own string, apart []tuneOwn) {
|
||
byID := make(map[string]provider.Entry, len(entries))
|
||
for _, e := range entries {
|
||
byID[strings.ToLower(e.ID)] = e
|
||
}
|
||
peak := map[string]int{}
|
||
for _, c := range calls {
|
||
peak[c.Model] = max(peak[c.Model], c.Prompt)
|
||
}
|
||
unknown, moved, held := false, false, 0
|
||
for m, p := range peak {
|
||
e, ok := byID[strings.TrimPrefix(m, "magpie/")]
|
||
switch {
|
||
case !ok:
|
||
window = max(window, 272000, p)
|
||
case e.Context <= 0:
|
||
unknown = true
|
||
default:
|
||
window = max(window, e.Context, p)
|
||
if n := provider.CompactSetIn(st, e.ID, find); n > 0 {
|
||
at := min(e.Context, n)
|
||
apart = append(apart, tuneOwn{Model: e.ID, Value: at})
|
||
held = max(held, at)
|
||
continue
|
||
}
|
||
at := st.Working(e.Context)
|
||
if n := provider.CompactFor(st, e, find); n > 0 {
|
||
at = min(e.Context, n)
|
||
}
|
||
told, moved = max(told, at), true
|
||
}
|
||
}
|
||
// the same models, in the same order, in every read
|
||
slices.SortFunc(apart, func(a, b tuneOwn) int {
|
||
if a.Value != b.Value {
|
||
return b.Value - a.Value
|
||
}
|
||
return strings.Compare(a.Model, b.Model)
|
||
})
|
||
if !moved && len(apart) > 0 {
|
||
told, own, apart = held, apart[0].Model, nil
|
||
}
|
||
if unknown {
|
||
window = 0
|
||
}
|
||
return window, told, own, apart
|
||
}
|
||
|
||
func tuneRoutes(mux *http.ServeMux) {
|
||
mux.HandleFunc("GET /api/tune", func(rw http.ResponseWriter, r *http.Request) {
|
||
days, _ := strconv.Atoi(r.URL.Query().Get("days"))
|
||
if days <= 0 || days > 30 {
|
||
days = 7
|
||
}
|
||
writeJSON(rw, tuneFor(days))
|
||
})
|
||
type change struct {
|
||
Agent, Knob, Model, Value string
|
||
}
|
||
mux.HandleFunc("POST /api/tune/apply", func(rw http.ResponseWriter, r *http.Request) {
|
||
var in change
|
||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
if err := agent.TuneApply("", in.Agent, in.Knob, in.Model, in.Value); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
tuneForget()
|
||
s, _ := agent.TuneRead("", in.Agent, in.Knob, in.Model)
|
||
writeJSON(rw, s)
|
||
})
|
||
mux.HandleFunc("POST /api/tune/undo", func(rw http.ResponseWriter, r *http.Request) {
|
||
var in change
|
||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
if err := agent.TuneUndo("", in.Agent, in.Knob, in.Model); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
tuneForget()
|
||
s, _ := agent.TuneRead("", in.Agent, in.Knob, in.Model)
|
||
writeJSON(rw, s)
|
||
})
|
||
}
|
||
|
||
// tuneKept is what tune has read of the Claude Code and Codex files: each
|
||
// file's calls made tune's, by the size and time it had. The Context tab
|
||
// asks for the advice at every refresh, 5s by default, and each ask read
|
||
// every session file of the range whole into sessions.Call (300k calls
|
||
// were ~500 MB at once, a second of CPU each time): a small server went
|
||
// down with the tab open (Yayoi_no_yume on X). Now a file is read again
|
||
// only when it changed, one at a time, and what is kept of a call is
|
||
// tune's ~100 bytes.
|
||
var tuneKept = struct {
|
||
sync.Mutex
|
||
files map[string]tuneFile
|
||
}{files: map[string]tuneFile{}}
|
||
|
||
type tuneFile struct {
|
||
size int64
|
||
mod time.Time
|
||
agent string
|
||
calls []tune.Call
|
||
// msgs: each of its calls' message, hashed, the failed ones too; at
|
||
// is where calls[i]'s own is
|
||
msgs []uint64
|
||
at []int
|
||
}
|
||
|
||
// tuneAnswer is the advice last given. Working it out goes over every
|
||
// call of the range, a week's 300k of them half a second of CPU and a few
|
||
// hundred MB at once, too much for each refresh of the Usage page (5s): it
|
||
// stands for ctxAgain, and a setting tune changes lets it go at once.
|
||
var tuneAnswer struct {
|
||
sync.Mutex
|
||
days int
|
||
at time.Time
|
||
out tuneState
|
||
}
|
||
|
||
// tuneFor is the advice over the last days.
|
||
func tuneFor(days int) tuneState {
|
||
tuneAnswer.Lock()
|
||
defer tuneAnswer.Unlock()
|
||
if tuneAnswer.days == days && time.Since(tuneAnswer.at) < ctxAgain {
|
||
return tuneAnswer.out
|
||
}
|
||
out := adviseOf(tuneCallsSince(time.Now().AddDate(0, 0, -days)), days, "")
|
||
tuneAnswer.days, tuneAnswer.at, tuneAnswer.out = days, time.Now(), out
|
||
return out
|
||
}
|
||
|
||
func tuneForget() {
|
||
tuneAnswer.Lock()
|
||
tuneAnswer.at = time.Time{}
|
||
tuneAnswer.Unlock()
|
||
}
|
||
|
||
// tuneRead, in tests, is told each file tune reads.
|
||
var tuneRead func(path string)
|
||
|
||
func msgHash(s string) uint64 {
|
||
if s == "" {
|
||
return 0
|
||
}
|
||
h := fnv.New64a()
|
||
h.Write([]byte(s))
|
||
return h.Sum64() | 1
|
||
}
|
||
|
||
// tuneCallsSince is each agent's calls since, as sessions.Calls has them:
|
||
// a message a resumed Claude Code session copied is the earlier file's.
|
||
func tuneCallsSince(since time.Time) map[string][]tune.Call {
|
||
var srcs []sessions.CallSource
|
||
for _, s := range sessions.CallSources() {
|
||
if (s.Agent == "claude" || s.Agent == "codex") && !s.Modified.Before(since) {
|
||
srcs = append(srcs, s)
|
||
}
|
||
}
|
||
// earlier files own messages copied into a resumed Claude session
|
||
sort.Slice(srcs, func(i, j int) bool {
|
||
if !srcs[i].Modified.Equal(srcs[j].Modified) {
|
||
return srcs[i].Modified.Before(srcs[j].Modified)
|
||
}
|
||
return srcs[i].Path < srcs[j].Path
|
||
})
|
||
tuneKept.Lock()
|
||
defer tuneKept.Unlock()
|
||
on := make(map[string]bool, len(srcs))
|
||
for _, s := range srcs {
|
||
on[s.Path] = true
|
||
if f, ok := tuneKept.files[s.Path]; ok && f.size == s.Size && f.mod.Equal(s.Modified) && f.agent == s.Agent {
|
||
continue
|
||
}
|
||
if tuneRead != nil {
|
||
tuneRead(s.Path)
|
||
}
|
||
f := tuneFile{size: s.Size, mod: s.Modified, agent: s.Agent}
|
||
cs := sessions.ReadCallSource(s)
|
||
f.msgs = make([]uint64, len(cs))
|
||
for i, c := range cs {
|
||
f.msgs[i] = msgHash(c.Msg)
|
||
if tc := tune.FromCalls(cs[i : i+1]); len(tc) == 1 {
|
||
f.calls = append(f.calls, tc[0])
|
||
f.at = append(f.at, i)
|
||
}
|
||
}
|
||
tuneKept.files[s.Path] = f
|
||
}
|
||
for p := range tuneKept.files {
|
||
if !on[p] {
|
||
delete(tuneKept.files, p)
|
||
}
|
||
}
|
||
by := map[string][]tune.Call{}
|
||
seen := map[uint64]bool{}
|
||
for _, s := range srcs {
|
||
f := tuneKept.files[s.Path]
|
||
for i, c := range f.calls {
|
||
if m := f.msgs[f.at[i]]; m != 0 && seen[m] || c.Time.Before(since) {
|
||
continue
|
||
}
|
||
by[f.agent] = append(by[f.agent], c)
|
||
}
|
||
for _, m := range f.msgs {
|
||
if m != 0 {
|
||
seen[m] = true
|
||
}
|
||
}
|
||
}
|
||
for _, cs := range by {
|
||
slices.SortStableFunc(cs, func(a, b tune.Call) int { return b.Time.Compare(a.Time) })
|
||
}
|
||
return by
|
||
}
|