usage: Tuned for you tells Claude Code's cache here with no calls, and an hour of the user's has Use 5 min
linkaww (Discord, #help) set Claude Code's prompt cache to 1 hour from Usage > Context > Tuned for you and asked how to go back to 5 minutes. On the computer that sends its requests to a Remote magpie the card was empty, so there was nothing to see or undo. Cause: the card's rows came only from advice, which needs 50 calls read from this computer's session files, or from a knob tune put in itself (TuneKept, v0.1.3120). A Remote-magpie client with too few calls read here, or a 1 hour it didn't put in through magpie (set on the other computer's card and copied, set by hand, or stashed by a version before 3120), had no row. The setting itself is Claude Code's settings.json on the computer where Claude Code runs (promptCacheTtl, subagentPromptCacheTtl); a Remote magpie's requests read B's, not A's. Now: - agent.TuneCacheHere reads Claude Code's two cache lifetimes on this computer whoever set them: the main conversation's always, the subagents' when something sets it, nothing when there is no ~/.claude. adviseOf adds them to the kept knobs, so Claude Code's card shows with no calls at all, each row's Now saying 1 hour, 5 minutes or Default. - A row with too few calls at 1 hour that magpie didn't put in has Use 5 minutes (tooltip: Replaces your 1 hour; Undo puts it back); after it, Undo gives the hour back from the stash. A variable that comes first (CLAUDE_CODE_PROMPT_CACHE_TTL, FORCE_PROMPT_CACHING_5M) still locks it. - A row with too few calls no longer reads "Already the leanest". - The three Use buttons share one helper, which pays for the new one: context.js is 82 bytes smaller, TestCoreScriptBudget's budget untouched. No new strings. Run (mote, via mote-run): - go test ./internal/agent ./internal/gui -count=1: agent ok; gui's only reds are TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27 on mote) and, before the helper, TestCoreScriptBudget (fixed as above); after: -run 'CoreScriptBudget|Tune' ok. - context-tune.test.cjs in Chromium and WebKit, all five languages, 360 and 1100 wide: 24 of 24 ok. i18n-keys: every test ok but "a plugin's words are its own", red on main for three strings in other files (#5332, #5426, #5482). - gofmt -l clean; go vet darwin (touched packages), GOOS=linux -tags nogui ./... and GOOS=windows ./... ok; builds for all three ok. Broken by hand: - old internal/gui/tune.go: TestTuneTellsTheCacheHere red ("the user's own 1h, no calls: rows []") and TestTuneKeepsWhatItPutIn red. - old context.js: the new GUI test red in both engines (Now read "—" where Default is expected). Not tried: a real Remote magpie pair (two computers) with linkaww's settings, and a real Claude Code session reading the 5m after the press; the whole GUI suite ran after the push, not before (suite lock held). Reported-by: linkaww (Discord)
This commit is contained in:
@@ -456,6 +456,32 @@ func TuneKept(home, agent string) []TuneKnob {
|
||||
return out
|
||||
}
|
||||
|
||||
// TuneCacheHere is Claude Code's two prompt-cache lifetimes as this
|
||||
// computer's Claude Code reads them, whoever set them, when its folder is
|
||||
// here: the main conversation's always, the subagents' when something
|
||||
// sets it. They change what this computer's Claude Code sends, whichever
|
||||
// magpie its requests go on to (linkaww on Discord: Claude Code through a
|
||||
// Remote magpie, too few calls read here to advise on, and the card had
|
||||
// no row to tell the cache it keeps, or a way back to 5 minutes).
|
||||
func TuneCacheHere(home string) []TuneKnob {
|
||||
p, err := tuneWhere(home, "claude", TuneCache, "")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if fi, err := os.Stat(filepath.Dir(p.path)); err != nil || !fi.IsDir() {
|
||||
return nil
|
||||
}
|
||||
var out []TuneKnob
|
||||
for _, knob := range []string{TuneCache, TuneSubCache} {
|
||||
s, err := TuneRead(home, "claude", knob, "")
|
||||
if err != nil || knob == TuneSubCache && s.Value == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, TuneKnob{Knob: knob, Setting: s})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TuneUndo puts back what a setting was before tune put one in, unless
|
||||
// the user has changed it since: then it is theirs, and only forgotten.
|
||||
func TuneUndo(home, agent, knob, model string) error {
|
||||
|
||||
@@ -1275,7 +1275,7 @@
|
||||
// few: put in by tune, too few calls to advise on; kept for Undo (linkaww)
|
||||
const r = a.compact || a.ttl || { current: a.setting.value, best: a.setting.value, cost: 0, low: 0 };
|
||||
const ttl = a.knob != "compact";
|
||||
const show = (v) => ttl ? ttlName(v) : fmtK(+v);
|
||||
const show = (v) => ttl ? (v ? ttlName(v) : t("Default")) : fmtK(+v);
|
||||
const cur = ttl ? r.current : r.current, best = r.best;
|
||||
const same = String(cur) === String(best);
|
||||
const saves = r.cost > 0 ? (r.cost - r.low) / r.cost : 0;
|
||||
@@ -1360,6 +1360,7 @@
|
||||
: el("span", "ctx-chip info", "");
|
||||
verdict.append(el("i"), lean ? t("Not the leanest: {range} spends the fewest tokens", { range: bandName })
|
||||
: applied ? t("In use") : same ? t("Already the leanest for how you work") : t("{pct} fewer tokens · {tokens} over the range", { pct: pct(saves), tokens: fmtK(Math.round(r.cost - r.low)) }));
|
||||
verdict.hidden = !!a.few && !applied;
|
||||
foot.append(verdict, el("span", "grow"));
|
||||
const note = el("div", "ctx-tune-src");
|
||||
title.after(note);
|
||||
@@ -1390,24 +1391,19 @@
|
||||
undo.onclick = () => send("undo");
|
||||
foot.append(undo);
|
||||
}
|
||||
if (!same && !applied && !held) {
|
||||
const go = el("button", "text primary", t("Use {value}", { value: show(best) }));
|
||||
const use = (v, tt) => {
|
||||
const go = el("button", "text primary", t("Use {value}", { value: show(v) }));
|
||||
go.type = "button";
|
||||
go.disabled = tuneBusy === key;
|
||||
if (set.value && !set.ours) go.dataset.tt = t("Replaces your {value}; Undo puts it back", { value: show(set.value) });
|
||||
go.onclick = () => send("apply", best);
|
||||
if (tt) go.dataset.tt = tt;
|
||||
go.onclick = () => send("apply", v);
|
||||
foot.append(go);
|
||||
}
|
||||
if (lean && !held) {
|
||||
const go = el("button", "text primary", t("Use {value}", { value: show(lean.value) }));
|
||||
go.type = "button";
|
||||
go.disabled = tuneBusy === key;
|
||||
go.dataset.tt = set.value && !set.ours
|
||||
? t("Replaces your {value}; Undo puts it back", { value: show(set.value) })
|
||||
: t("Fewer tokens, but more compactions to wait on");
|
||||
go.onclick = () => send("apply", lean.value);
|
||||
foot.append(go);
|
||||
}
|
||||
};
|
||||
const yours = set.value && !set.ours && t("Replaces your {value}; Undo puts it back", { value: show(set.value) });
|
||||
if (!same && !applied && !held) use(best, yours);
|
||||
// an hour no Undo puts back, too few calls to weigh (linkaww)
|
||||
if (a.few && ttl && cur === "1h" && !held && !set.ours) use("5m", yours);
|
||||
if (lean && !held) use(lean.value, yours || t("Fewer tokens, but more compactions to wait on"));
|
||||
// a window of the reader's own (#1860), from the same replay: each
|
||||
// value's tokens against the setting now, which the reader picks away
|
||||
// from (hayden4608 on Discord: against the leanest, every line read as
|
||||
|
||||
@@ -960,3 +960,70 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
|
||||
// Claude Code's cache as this computer has it is on the card with no calls
|
||||
// read here, whoever set it (linkaww on Discord: through a Remote magpie,
|
||||
// "这里什么都没有"): the user's own 1 hour has Use 5 min, which puts in 5m;
|
||||
// nothing set reads Default, with nothing to press and no verdict
|
||||
const def = { en: "Default", zh: "默认", "zh-TW": "預設", ja: "デフォルト", de: "Standard" };
|
||||
const five = { en: /5 min/, zh: /5 分钟/, "zh-TW": /5 分鐘/, ja: /5 分/, de: /5 Min/ };
|
||||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||||
test(`${engine}: the cache here is told with no calls, and a 1 hour of the user's goes back to 5 min`, async (t) => {
|
||||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||||
const errors = [];
|
||||
t.after(async () => {
|
||||
if (errors.length) console.log(errors);
|
||||
await browser.close();
|
||||
});
|
||||
for (const lang of Object.keys(def)) 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.setDefaultNavigationTimeout(60000);
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
const posts = [];
|
||||
const base = serve(lang, posts);
|
||||
let main = { value: "1h", source: "settings" };
|
||||
await page.route("**/*", async (r) => {
|
||||
const p = new URL(r.request().url()).pathname;
|
||||
if (p === "/api/tune/apply") main = { value: "5m", source: "settings", ours: true };
|
||||
if (p !== "/api/tune") return base(r);
|
||||
return r.fulfill({ json: { days: 1, agents: [{ agent: "claude", calls: 0, advice: [
|
||||
{ knob: "cache_ttl", setting: main, few: true },
|
||||
{ knob: "subagent_cache_ttl", setting: {}, few: true },
|
||||
] }] } });
|
||||
});
|
||||
await page.goto("http://magpie.test/?view=usage");
|
||||
const rows = page.locator(".ctx-tune-row");
|
||||
await rows.first().waitFor();
|
||||
assert.equal(await rows.count(), 2);
|
||||
// nothing set: Default, nothing to press, no verdict
|
||||
const sub = rows.nth(1);
|
||||
assert.equal(await sub.locator(".ctx-tune-val.now b").innerText(), def[lang]);
|
||||
assert.equal(await sub.locator("button").count(), 0);
|
||||
assert.equal(await sub.locator(".ctx-chip:visible").count(), 0);
|
||||
assert.equal(await sub.locator(".ctx-tune-why").innerText(), few[lang]);
|
||||
// the user's own hour: Use 5 min, no verdict
|
||||
const one = rows.nth(0);
|
||||
assert.match(await one.locator(".ctx-tune-val.now b").innerText(), { en: /1 hour/i, zh: /1 小时/, "zh-TW": /1 小時/, ja: /1 時間/, de: /1 Stunde/ }[lang]);
|
||||
assert.equal(await one.locator(".ctx-chip:visible").count(), 0);
|
||||
const go = one.locator("button");
|
||||
assert.equal(await go.count(), 1);
|
||||
assert.match(await go.innerText(), five[lang]);
|
||||
for (const r of [one, sub]) assert.doesNotMatch(await r.innerText(), /NaN|undefined|—/);
|
||||
const over = await page.evaluate(() => {
|
||||
const view = document.querySelector("#view-usage");
|
||||
return view.scrollWidth > view.clientWidth + 1 ? view.scrollWidth + ">" + view.clientWidth : "";
|
||||
});
|
||||
assert.equal(over, "");
|
||||
await go.evaluate((b) => b.click());
|
||||
await page.waitForFunction(() => /undo|撤销|復原|取り消す|Rückgängig/i.test(document.querySelector(".ctx-tune-row button")?.textContent || ""));
|
||||
assert.deepEqual(posts.map((p) => [p.path, p.knob, p.value]), [["/api/tune/apply", "cache_ttl", "5m"]]);
|
||||
await ctx.close();
|
||||
});
|
||||
}
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
|
||||
+16
-3
@@ -41,8 +41,9 @@ type tuneAdvice struct {
|
||||
// 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"`
|
||||
// Few: a setting tune put in, kept on the card with nothing to advise
|
||||
// (too few calls in the range), so its Undo is still there
|
||||
// Few: a setting tune put in, or Claude Code's cache as it is here,
|
||||
// kept on the card with nothing to advise (too few calls in the
|
||||
// range), so its Undo, or its way back to 5 minutes, is still there
|
||||
Few bool `json:"few,omitempty"`
|
||||
}
|
||||
|
||||
@@ -114,6 +115,17 @@ func adviseOf(by map[string][]tune.Call, days int, home string) tuneState {
|
||||
for _, name := range []string{"claude", "codex"} {
|
||||
calls := by[name]
|
||||
kept := agent.TuneKept(home, name)
|
||||
if name == "claude" {
|
||||
// the cache this computer's Claude Code keeps, with or without
|
||||
// calls read here, its way back to 5 minutes with it
|
||||
here := agent.TuneCacheHere(home)
|
||||
for _, k := range kept {
|
||||
if !slices.ContainsFunc(here, func(o agent.TuneKnob) bool { return o.Knob == k.Knob && o.Model == k.Model }) {
|
||||
here = append(here, k)
|
||||
}
|
||||
}
|
||||
kept = here
|
||||
}
|
||||
if len(calls) == 0 && len(kept) == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -197,7 +209,8 @@ func adviseOf(by map[string][]tune.Call, days int, home string) tuneState {
|
||||
}
|
||||
}
|
||||
// what tune put in stays on the card, Undo with it, when the range
|
||||
// has too few calls to advise on it (linkaww on Discord)
|
||||
// has too few calls to advise on it (linkaww on Discord), and so
|
||||
// does Claude Code's cache as it is here
|
||||
for _, k := range kept {
|
||||
if !slices.ContainsFunc(ta.Advice, func(a tuneAdvice) bool { return a.Knob == k.Knob && a.Model == k.Model }) {
|
||||
ta.Advice = append(ta.Advice, tuneAdvice{Knob: k.Knob, Model: k.Model, Setting: k.Setting, Few: true})
|
||||
|
||||
@@ -78,21 +78,63 @@ func TestTuneKeepsWhatItPutIn(t *testing.T) {
|
||||
if err := agent.TuneUndo(home, "claude", agent.TuneCompact, "claude-opus-4-5"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := kept(nil); len(got) != 0 {
|
||||
t.Fatalf("put back, yet kept: %+v", got)
|
||||
// put back, Claude Code's cache is its default again, still told
|
||||
if got := kept(nil); len(got) != 1 || got[0].Knob != agent.TuneCache || got[0].Setting.Value != "" || got[0].Setting.Ours {
|
||||
t.Fatalf("put back: kept %+v; want the cache at Claude Code's default alone", got)
|
||||
}
|
||||
}
|
||||
|
||||
// linkaww on Discord (11-06), again: Claude Code here, through a Remote
|
||||
// magpie, with too few calls read on this computer, and "这里什么都没有".
|
||||
// The cache this computer's Claude Code keeps is on the card whoever set
|
||||
// it, with no calls at all: the user's own 1h too, which Use 5 min puts
|
||||
// at 5m and Undo puts back. No Claude Code folder here, no card.
|
||||
func TestTuneTellsTheCacheHere(t *testing.T) {
|
||||
sandboxHome(t)
|
||||
home := t.TempDir()
|
||||
rows := func() []tuneAdvice {
|
||||
t.Helper()
|
||||
for _, a := range tuneOf(nil, 1, home).Agents {
|
||||
if a.Agent == "claude" {
|
||||
return a.Advice
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if st := tuneOf(nil, 1, home); len(st.Agents) != 0 {
|
||||
t.Errorf("no calls and nothing put in, yet agents: %+v", st.Agents)
|
||||
t.Fatalf("no Claude Code here, yet agents: %+v", st.Agents)
|
||||
}
|
||||
settingsPath := filepath.Join(home, ".claude", "settings.json")
|
||||
if err := edit.SetJSON(settingsPath, edit.KV{Path: "promptCacheTtl", Value: "1h"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := rows()
|
||||
if len(got) != 1 || got[0].Knob != agent.TuneCache || !got[0].Few || got[0].Setting.Value != "1h" || got[0].Setting.Ours || got[0].Setting.Source != "settings" {
|
||||
t.Fatalf("the user's own 1h, no calls: rows %+v; want the main cache at 1h, theirs", got)
|
||||
}
|
||||
// Use 5 min, then Undo: their 1h back
|
||||
if err := agent.TuneApply(home, "claude", agent.TuneCache, "", "5m"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := rows(); len(got) != 1 || got[0].Setting.Value != "5m" || !got[0].Setting.Ours {
|
||||
t.Fatalf("after 5m: rows %+v; want 5m, magpie's", got)
|
||||
}
|
||||
if err := agent.TuneUndo(home, "claude", agent.TuneCache, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, _ := edit.GetJSON(settingsPath, "promptCacheTtl"); v != `"1h"` && v != "1h" {
|
||||
t.Fatalf("undone: promptCacheTtl %q; want their 1h back", v)
|
||||
}
|
||||
|
||||
// the user's own 1h is theirs to change in their file: no kept row
|
||||
if err := edit.SetJSON(filepath.Join(home, ".claude", "settings.json"), edit.KV{Path: "promptCacheTtl", Value: "1h"}); err != nil {
|
||||
// the subagents' cache is told when something sets it
|
||||
if err := edit.SetJSON(settingsPath, edit.KV{Path: "subagentPromptCacheTtl", Value: "1h"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s, _ := agent.TuneRead(home, "claude", agent.TuneCache, ""); s.Value != "1h" {
|
||||
t.Fatalf("the user's own 1h reads %+v", s)
|
||||
if got := rows(); len(got) != 2 || got[1].Knob != agent.TuneSubCache || got[1].Setting.Value != "1h" {
|
||||
t.Fatalf("subagents at 1h: rows %+v; want both caches", got)
|
||||
}
|
||||
if got := kept(few); len(got) != 0 {
|
||||
t.Errorf("the user's own 1h: kept %+v; want none", got)
|
||||
// a variable before the setting is told, locked
|
||||
t.Setenv("CLAUDE_CODE_PROMPT_CACHE_TTL", "1h")
|
||||
if got := rows(); len(got) == 0 || got[0].Setting.Locked != "CLAUDE_CODE_PROMPT_CACHE_TTL" {
|
||||
t.Fatalf("env 1h: rows %+v; want the main cache locked by it", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user