Linka (Discord) asked to set Claude Code's prompt cache TTL from magpie. Until now only Usage › Context's "Tuned for you" card wrote it, and that card offers nothing when the value is already the cheapest it found. Claude Code's opened row and settings sheet now have two rows under its own Long conversations: "Prompt cache · main conversation" and "Prompt cache · subagents". Each opens the app menu with Claude Code's default, 5 min and 1 hour. They write the keys the card writes, promptCacheTtl and subagentPromptCacheTtl in Claude Code's settings.json (agent.ClaudeCacheSet, POST /api/settings/claude-cache). Default takes the key out. A pick is the user's own: it forgets the card's stash, so the card's Undo leaves it. A variable that comes before the setting (FORCE_PROMPT_CACHING_5M, CLAUDE_CODE_PROMPT_CACHE_TTL, CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL, in settings.json's env or magpie's own) disables the row and names the variable, and the route refuses it, so a value the user set by hand is never written over. The names were checked in Claude Code 2.1.316's binary. Its resolver checks FORCE_PROMPT_CACHING_5M, then the env var, then the setting (enum "5m"/"1h"), then an agent's experimental.cacheTtl, then ENABLE_PROMPT_CACHING_1H. With nothing set, the main conversation gets 1h on a subscription within its limits and subagents get 5m. Run (mote, at this tree on2ce44dc28): - go test -count=1 -run 'TestClaudeCache|TestTune' ./internal/agent ./internal/gui: ok; go test ./internal/cli: ok (route parity). - node --test claude-cache.test.cjs: 8/8 (Chromium and WebKit, en and zh, 900 and 420px), with agent-compact, i18n-*, gui-ja/zh-tw/de, old-webkit, agent-details-ready and context-tune: ok. - Whole GUI suite on the Linux VM (wsl-run, concurrency 4), still running at commit: 74 files red there, mostly WebKitGTK. Of those only codex-memories-settings reaches Claude Code's settings: it counted Claude Code's rows as Long conversations alone, and now leaves out the two cache rows and checks they are there (18/18 on mote). desktop-subagent-model, account-eye, allow-ips and builtins pass alone on mote. codex-auto-reset (webkit de, #4790) and i18n-keys (#5327, #5332, #5426, #5482) fail the same way at origin/mainf0620db63on mote. - go test ./internal/gui ./internal/agent ./internal/tune: agent and tune ok; gui red only in TestMainWindowCorners/macOS_26_SDK (#3949). - gofmt -l: clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build ./...: ok. Broken by hand: - Without forget, TestClaudeCacheSet fails ("the pick is the user's, not the card's"). - Without the lock check, it fails ("set under CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL"). - With default leaving the key, TestClaudeCacheSet and TestClaudeCacheRoute fail. - Without the rows in agentcompact.js, claude-cache.test.cjs fails 8/8. Without the same-pick check, it fails on the extra post. - The first GUI run was red in WebKit at 420px: Playwright's own scroll to an off-screen button was taken as the page scrolling. The test now wheels the rows into sight as a reader does, and checks they came into view. Not tried: a real Claude Code session reading the picked value (only the settings.json it reads was checked); the live ~/.claude was never touched. Reported-by: Linka
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package gui
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
)
|
|
|
|
// Linka on Discord: Claude Code's prompt cache is set in its settings,
|
|
// beside its compaction. Reading the settings shows the user's own value
|
|
// and writes nothing; a pick goes through POST /api/settings/claude-cache,
|
|
// and Claude Code's default ("") takes the key out.
|
|
func TestClaudeCacheRoute(t *testing.T) {
|
|
h := sandboxHome(t)
|
|
path := filepath.Join(h, ".claude", "settings.json")
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
own := []byte(`{"theme": "dark", "promptCacheTtl": "1h"}`)
|
|
if err := os.WriteFile(path, own, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := claudeCacheState()
|
|
want := []claudeCacheJSON{{Knob: agent.TuneCache, Value: "1h"}, {Knob: agent.TuneSubCache}}
|
|
if len(got) != 2 || got[0] != want[0] || got[1] != want[1] {
|
|
t.Fatalf("read %+v; want %+v", got, want)
|
|
}
|
|
if b, _ := os.ReadFile(path); !bytes.Equal(b, own) {
|
|
t.Fatalf("reading wrote the user's file:\n%s", b)
|
|
}
|
|
mux := http.NewServeMux()
|
|
tuneRoutes(mux)
|
|
post := func(knob, value string) int {
|
|
body, _ := json.Marshal(map[string]string{"knob": knob, "value": value})
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/settings/claude-cache", bytes.NewReader(body)))
|
|
return rec.Code
|
|
}
|
|
if c := post(agent.TuneSubCache, "5m"); c != 200 {
|
|
t.Fatalf("5m for subagents: %d", c)
|
|
}
|
|
if c := post(agent.TuneCache, ""); c != 200 {
|
|
t.Fatalf("default: %d", c)
|
|
}
|
|
got = claudeCacheState()
|
|
if got[0].Value != "" || got[1].Value != "5m" {
|
|
t.Fatalf("after the picks %+v; want default and 5m", got)
|
|
}
|
|
if b, _ := os.ReadFile(path); bytes.Contains(b, []byte(`"promptCacheTtl"`)) || !bytes.Contains(b, []byte(`"theme"`)) {
|
|
t.Fatalf("default left the key, or lost the user's theme:\n%s", b)
|
|
}
|
|
if c := post(agent.TuneCache, "2h"); c == 200 {
|
|
t.Fatal("took 2h")
|
|
}
|
|
}
|