Files
magpie/internal/gui/tune_kept_test.go
T
yetone fcc0daa23e 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)
2026-11-06 18:51:39 +08:00

141 lines
5.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/sessions"
)
// linkaww on Discord (11-06): Usage › Context put Claude Code's cache at
// 1 hour, and the way back to 5 minutes was gone. The card is the one
// place that setting is changed, and its row, Undo with it, came only
// with 50 calls of the range to advise on: Today, or a quiet week, had
// none. A setting tune put in stays on the card, marked few, with too few
// calls or none at all; one the user wrote themselves, and one put back,
// leave it.
func TestTuneKeepsWhatItPutIn(t *testing.T) {
sandboxHome(t)
home := t.TempDir()
kept := func(cs []sessions.Call) []tuneAdvice {
t.Helper()
var out []tuneAdvice
for _, a := range tuneOf(cs, 1, home).Agents {
if a.Agent != "claude" {
continue
}
for _, ad := range a.Advice {
if ad.Few {
out = append(out, ad)
}
}
}
return out
}
few := tuneCalls("claude", "claude-opus-4-5", 5)
if got := kept(few); len(got) != 0 {
t.Fatalf("nothing put in, yet kept rows: %+v", got)
}
if err := agent.TuneApply(home, "claude", agent.TuneCache, "", "1h"); err != nil {
t.Fatal(err)
}
if err := agent.TuneApply(home, "claude", agent.TuneCompact, "claude-opus-4-5", "300000"); err != nil {
t.Fatal(err)
}
for name, cs := range map[string][]sessions.Call{"few calls": few, "no calls": nil} {
got := kept(cs)
if len(got) != 2 {
t.Fatalf("%s: kept %+v; want the cache and the window", name, got)
}
if c := got[0]; c.Knob != agent.TuneCache || c.Setting.Value != "1h" || !c.Setting.Ours || c.TTL != nil {
t.Errorf("%s: cache row %+v; want cache_ttl at 1h, magpie's", name, c)
}
if w := got[1]; w.Knob != agent.TuneCompact || w.Model != "claude-opus-4-5" || w.Setting.Value != "300000" || !w.Setting.Ours {
t.Errorf("%s: window row %+v; want claude-opus-4-5's 300000, magpie's", name, w)
}
}
// with enough calls the cache has its advice, and no second row
for _, a := range tuneOf(tuneCalls("claude", "claude-opus-4-5", 80), 1, home).Agents {
n := 0
for _, ad := range a.Advice {
if ad.Knob == agent.TuneCache {
n++
if ad.Few || ad.TTL == nil {
t.Errorf("80 calls: the cache row %+v; want its advice", ad)
}
}
}
if n != 1 {
t.Errorf("80 calls: %d cache rows; want 1", n)
}
}
if err := agent.TuneUndo(home, "claude", agent.TuneCache, ""); err != nil {
t.Fatal(err)
}
if err := agent.TuneUndo(home, "claude", agent.TuneCompact, "claude-opus-4-5"); err != nil {
t.Fatal(err)
}
// 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.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 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 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)
}
// 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)
}
}