diff --git a/internal/gui/assets/context.js b/internal/gui/assets/context.js index 9cca13a4e..25d585705 100644 --- a/internal/gui/assets/context.js +++ b/internal/gui/assets/context.js @@ -1422,7 +1422,12 @@ const same = String(cur) === String(best); const saves = r.cost > 0 ? (r.cost - r.low) / r.cost : 0; const set = a.setting || {}; - const applied = set.ours && String(set.value) === String(best); + // a model's own Compact at is where Codex runs, over Long conversations: + // the value put in there isn't "In use", and putting another in moves + // nothing (hoobnn on Discord: "In use" for 170K beside "Now 430K") + const own = a.own; + const held = !!(set.locked || own); + const applied = !own && set.ours && String(set.value) === String(best); const row = el("div", "ctx-tune-row" + (same || applied ? " done" : "")); const title = el("div", "ctx-tune-title"); @@ -1497,6 +1502,7 @@ const note = el("div", "ctx-tune-src"); title.after(note); if (set.locked) note.textContent = t("{name} is set and comes first", { name: set.locked }); + else if (own) note.textContent = t("{model} compacts at its own {value}, which comes first", { model: own.model, value: fmtK(own.value) }); else if (set.source == "long") note.textContent = t("Long conversations, shared by Codex, Claude Code and Grok Build"); else if (set.ours) note.textContent = t("Set by magpie"); else if (set.value && a.compact && +set.value > a.compact.window && a.compact.window) note.textContent = t("You set {value}; the model's {window} comes first", { value: show(set.value), window: fmtK(a.compact.window) }); @@ -1522,7 +1528,7 @@ undo.onclick = () => send("undo"); foot.append(undo); } - if (!same && !applied && !set.locked) { + if (!same && !applied && !held) { const go = el("button", "text primary", t("Use {value}", { value: show(best) })); go.type = "button"; go.disabled = tuneBusy === key; @@ -1530,7 +1536,7 @@ go.onclick = () => send("apply", best); foot.append(go); } - if (lean && !set.locked) { + if (lean && !held) { const go = el("button", "text primary", t("Use {value}", { value: show(lean.value) })); go.type = "button"; go.disabled = tuneBusy === key; @@ -1543,7 +1549,7 @@ // a window of the reader's own (#1860), from the same replay: each // value's tokens past the leanest and compactions, both estimates const pts = (a.compact && a.compact.curve) || []; - if (pts.length > 1 && !set.locked) { + if (pts.length > 1 && !held) { const other = el("button", "text ctx-tune-pick", t("Other window…")); other.type = "button"; other.disabled = tuneBusy === key; diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 54c361e9f..e3475887c 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -136,6 +136,7 @@ const I18N = { "Fewer tokens, but more compactions to wait on": "Token 更少,但要多等几次压缩", "{pct} fewer tokens · {tokens} over the range": "少 {pct} Token · 这段时间省 {tokens}", "{name} is set and comes first": "已设置 {name},它优先", + "{model} compacts at its own {value}, which comes first": "{model} 有自己的压缩阈值 {value},它优先", "Set by magpie": "由 magpie 设置", "You set {value}": "你设置的是 {value}", "The agent's default": "Agent 的默认值", @@ -4233,6 +4234,7 @@ const I18N = { "Fewer tokens, but more compactions to wait on": "Token 更少,但要多等幾次壓縮", "{pct} fewer tokens · {tokens} over the range": "少 {pct} Token · 這段時間省 {tokens}", "{name} is set and comes first": "已設定 {name},它優先", + "{model} compacts at its own {value}, which comes first": "{model} 有自己的壓縮閾值 {value},它優先", "Set by magpie": "由 magpie 設定", "You set {value}": "你設定的是 {value}", "The agent's default": "Agent 的預設值", @@ -8305,6 +8307,7 @@ const I18N = { "Fewer tokens, but more compactions to wait on": "トークンは減りますが、コンパクトの待ちが増えます", "{pct} fewer tokens · {tokens} over the range": "トークン {pct} 減 · この期間で {tokens}", "{name} is set and comes first": "{name} が設定されていて優先されます", + "{model} compacts at its own {value}, which comes first": "{model} には独自の圧縮しきい値 {value} があり、そちらが優先されます", "Set by magpie": "magpie が設定", "You set {value}": "あなたの設定は {value}", "The agent's default": "エージェントの既定値", @@ -12384,6 +12387,7 @@ const I18N = { "Fewer tokens, but more compactions to wait on": "Weniger Tokens, aber mehr Kompaktierungen, auf die du wartest", "{pct} fewer tokens · {tokens} over the range": "{pct} weniger Tokens · {tokens} im Zeitraum", "{name} is set and comes first": "{name} ist gesetzt und hat Vorrang", + "{model} compacts at its own {value}, which comes first": "{model} hat ein eigenes „Komprimieren ab“ ({value}), das Vorrang hat", "Set by magpie": "Von magpie gesetzt", "You set {value}": "Du hast {value} gesetzt", "The agent's default": "Standard des Agenten", diff --git a/internal/gui/tests/context-tune.test.cjs b/internal/gui/tests/context-tune.test.cjs index 9598f38a4..2133eef9e 100644 --- a/internal/gui/tests/context-tune.test.cjs +++ b/internal/gui/tests/context-tune.test.cjs @@ -539,3 +539,74 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(errors, []); }); } + +// A model's own Compact at, over Long conversations (hoobnn on Discord): +// Long conversations held the 170K magpie put in, the advice was 170K, and +// the row said "In use" beside "Now 430K", where the provider's own +// threshold has Codex compact. It now names that threshold, as it does a +// variable that comes first, and offers no window Long conversations +// can't make so; Undo still puts Long conversations back. Without one, +// Codex runs at the advice, which says so. In both engines, every language, a phone's +// width and a desk's. +const ownTune = (own) => ({ + days: 1, + agents: [{ agent: "codex", calls: 2808, advice: [ + { knob: "compact", setting: { value: "170000", source: "long", ours: true }, ...(own ? { own: { model: "cpa/gpt-6-astra", value: 430000 } } : {}), + compact: { model: "cpa/gpt-6-astra", current: own ? 430000 : 170000, window: 440000, best: 170000, cost: own ? 1.4e9 : 1.02e9, low: 1.02e9, curve: curve(90000, 440000, 170000), facts } }, + ] }], +}); +const ownNote = { + en: "cpa/gpt-6-astra compacts at its own 430K, which comes first", + zh: "cpa/gpt-6-astra 有自己的压缩阈值 430K,它优先", + "zh-TW": "cpa/gpt-6-astra 有自己的壓縮閾值 430K,它優先", + ja: "cpa/gpt-6-astra には独自の圧縮しきい値 430K があり、そちらが優先されます", + de: "cpa/gpt-6-astra hat ein eigenes „Komprimieren ab“ (430K), das Vorrang hat", +}; +const inUse = { en: "In use", zh: "使用中", "zh-TW": "使用中", ja: "使用中", de: "In Verwendung" }; +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: a model's own Compact at isn't called In use`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const errors = []; + t.after(() => browser.close()); + for (const lang of Object.keys(heading)) for (const [width, height] of [[360, 760], [1100, 760]]) { + await t.test(`${lang}, ${width}x${height}`, async () => { + const ctx = await browser.newContext({ viewport: { width, height }, reducedMotion: "reduce" }); + await ctx.addInitScript(() => { try { localStorage.setItem("magpie.usageTab", "context"); } catch {} }); + const page = await ctx.newPage(); + page.setDefaultTimeout(5000); + page.on("pageerror", (e) => errors.push(e.message)); + let own = true; + const base = serve(lang, []); + await page.route("**/*", (r) => new URL(r.request().url()).pathname === "/api/tune" ? r.fulfill({ json: ownTune(own) }) : base(r)); + await page.goto("http://magpie.test/?view=usage"); + const row = page.locator(".ctx-tune-row").nth(0); + await row.locator(".ctx-tune-foot .ctx-chip").waitFor(); + assert.equal(await row.locator(".ctx-tune-src").innerText(), ownNote[lang]); + assert.notEqual(await row.locator(".ctx-tune-foot .ctx-chip").innerText(), inUse[lang]); + assert.equal(await row.locator(".ctx-tune-foot .ctx-chip").getAttribute("class"), "ctx-chip info"); + assert.equal(await row.getAttribute("class"), "ctx-tune-row", "Now is struck through: it isn't the advice"); + assert.equal(await row.locator(".ctx-tune-foot button.primary").count(), 0); + assert.equal(await row.locator(".ctx-tune-pick").count(), 0); + assert.equal(await row.locator(".ctx-tune-foot button.text:not(.primary)").count(), 1, "Undo stays"); + const over = await page.evaluate(() => { + const view = document.querySelector("#view-usage"); + const out = view.scrollWidth > view.clientWidth + 1 ? ["page " + view.scrollWidth + ">" + view.clientWidth] : []; + const b = document.querySelector(".ctx-tune").getBoundingClientRect(); + for (const e of document.querySelectorAll(".ctx-tune *")) { + const r = e.getBoundingClientRect(); + if (r.width && (r.right > b.right + 1 || r.left < b.left - 1)) out.push((e.className.baseVal ?? e.className) + " " + Math.round(r.left) + "–" + Math.round(r.right)); + } + return out; + }); + assert.deepEqual(over, []); + // Long conversations is where Codex runs: it runs at the advice + own = false; + await page.evaluate(() => window.loadContext()); + await page.locator(".ctx-tune-row.done .ctx-chip.good").waitFor(); + assert.equal(await page.locator(".ctx-tune-row .ctx-tune-foot .ctx-chip").innerText(), leanest[lang]); + await ctx.close(); + }); + } + assert.deepEqual(errors, []); + }); +} diff --git a/internal/gui/tune.go b/internal/gui/tune.go index 2d36b4f96..3c6e29c15 100644 --- a/internal/gui/tune.go +++ b/internal/gui/tune.go @@ -26,6 +26,16 @@ type tuneAdvice struct { 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"` +} + +type tuneOwn struct { + Model string `json:"model"` + Value int `json:"value"` } type tuneAgent struct { @@ -107,7 +117,7 @@ func tuneOf(cs []sessions.Call, days int, home string) tuneState { continue } st := settings.Load() - window, told := codexWindow(calls, st, provider.Catalog(), provider.GroupFinder()) + window, told, own := 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) @@ -118,7 +128,11 @@ func tuneOf(cs []sessions.Call, days int, home string) tuneState { // 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 { - ta.Advice = append(ta.Advice, tuneAdvice{Knob: agent.TuneCompact, Setting: s, Compact: r}) + 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} + } + ta.Advice = append(ta.Advice, ad) } } out.Agents = append(out.Agents, ta) @@ -127,8 +141,15 @@ func tuneOf(cs []sessions.Call, days int, home string) tuneState { } // codexWindow is the window Codex runs the calls' models to, and where -// magpie has it compact them when its config.toml says nothing (told; 0 -// when that isn't known for every model). A model of magpie's catalog +// magpie has it compact them when its config.toml says nothing (told: the +// largest of the models magpie tells a window, 0 when it tells 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). own is the model +// told comes from when that is its own threshold over Long conversations +// (provider.CompactFor with a Compact at of its own set), which Long +// conversations doesn't move. 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 @@ -137,7 +158,7 @@ func tuneOf(cs []sessions.Call, days int, home string) tuneState { // 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) { +func codexWindow(calls []tune.Call, st settings.Settings, entries []provider.Entry, find func(string) (provider.Group, []provider.Member, bool)) (window, told int, own string) { byID := make(map[string]provider.Entry, len(entries)) for _, e := range entries { byID[strings.ToLower(e.ID)] = e @@ -146,30 +167,37 @@ func codexWindow(calls []tune.Call, st settings.Settings, entries []provider.Ent for _, c := range calls { peak[c.Model] = max(peak[c.Model], c.Prompt) } - unknown, all := false, true + unknown := false for m, p := range peak { e, ok := byID[strings.TrimPrefix(m, "magpie/")] switch { case !ok: - window, all = max(window, 272000, p), false + window = max(window, 272000, p) case e.Context <= 0: - unknown, all = true, false + unknown = true default: window = max(window, e.Context, p) at := st.Working(e.Context) if n := provider.CompactFor(st, e, find); n > 0 { at = min(e.Context, n) } - told = max(told, at) + // past what Long conversations has it at, with a size of the + // user's in Long conversations: the model's own Compact at + // (with none, a window the user typed gives way to one tune + // puts in, CompactFor's rule) + mine := st.CompactAt > 0 && at > st.Working(e.Context) + if at > told || at == told && own != "" && (!mine || e.ID < own) { + told, own = at, "" + if mine { + own = e.ID + } + } } } if unknown { window = 0 } - if !all { - told = 0 - } - return window, told + return window, told, own } func tuneRoutes(mux *http.ServeMux) { diff --git a/internal/gui/tune_now_test.go b/internal/gui/tune_now_test.go new file mode 100644 index 000000000..9319e6c79 --- /dev/null +++ b/internal/gui/tune_now_test.go @@ -0,0 +1,65 @@ +package gui + +import ( + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// Where Tuned for you says Codex runs now is worked out the same way in +// every range (hoobnn on Discord). Over 7 days the calls held one of +// Codex's own models as well, outside magpie's catalog, which made "Now" +// unknown for the whole range: it was guessed from the prompts (380K) +// while today's calls, on magpie's models alone, read the 430K magpie +// tells Codex. And a model's own Compact at is where Codex runs, over Long +// conversations: the card says so instead of "In use" for a Long +// conversations value that doesn't move it. +func TestTuneCodexNowIsTheSameInEveryRange(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + home := t.TempDir() + if err := provider.Save(provider.Provider{ID: "cpa", Name: "CPA", Key: "k", Chat: "http://127.0.0.1:1/v1", + Models: []string{"gpt-cpa-1m", "gpt-cpa-own"}, + Contexts: map[string]int{"gpt-cpa-1m": 1000000, "gpt-cpa-own": 440000}}); err != nil { + t.Fatal(err) + } + if err := provider.SetCompactAt(170000); err != nil { + t.Fatal(err) + } + advice := func(models ...string) tuneAdvice { + t.Helper() + var cs = tuneCalls("codex", models[0], 80) + for _, m := range models[1:] { + cs = append(cs, tuneCalls("codex", m, 80)...) + } + st := tuneOf(cs, 7, home) + if len(st.Agents) != 1 || len(st.Agents[0].Advice) != 1 || st.Agents[0].Advice[0].Compact == nil { + t.Fatalf("%v: no compaction advice for Codex: %+v", models, st) + } + return st.Agents[0].Advice[0] + } + + // Long conversations at 170K: magpie's model runs at 170K, with one of + // Codex's own models beside it or not + today, week := advice("cpa/gpt-cpa-1m"), advice("cpa/gpt-cpa-1m", "gpt-5.5") + if today.Compact.Current != 170000 || week.Compact.Current != 170000 { + t.Errorf("Codex now: %d on magpie's model, %d with Codex's own beside it; want 170000 both", today.Compact.Current, week.Compact.Current) + } + if today.Own != nil || week.Own != nil { + t.Errorf("Long conversations is where Codex runs, yet the advice names a model's own threshold: %+v, %+v", today.Own, week.Own) + } + + // the provider's own Compact at, 430K, over Long conversations' 170K + if err := provider.SetModelCompacts("cpa", map[string]int{"*": 430000}); err != nil { + t.Fatal(err) + } + for _, ms := range [][]string{{"cpa/gpt-cpa-own"}, {"cpa/gpt-cpa-own", "gpt-5.5"}} { + a := advice(ms...) + if a.Compact.Current != 430000 { + t.Errorf("%v: Codex now %d; want the provider's 430000", ms, a.Compact.Current) + } + if a.Own == nil || a.Own.Model != "cpa/gpt-cpa-own" || a.Own.Value != 430000 { + t.Errorf("%v: own threshold %+v; want cpa/gpt-cpa-own at 430000", ms, a.Own) + } + } +}