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
105 lines
5.3 KiB
JavaScript
105 lines
5.3 KiB
JavaScript
// An agent's own Long conversations (molianyin4227 on Discord: models
|
|
// differ, so one size for every agent fits none): Every agent follows the
|
|
// row above, else this agent compacts at its own size or runs to the whole
|
|
// window (settings.AgentCompacts). app.js imports this when an agent's
|
|
// sheet holds Long conversations, so it weighs nothing on the core.
|
|
window.agentCompactFill = function (list, a) {
|
|
const kind = kindOf(a);
|
|
const row = el("div", "row pref agent-compact");
|
|
row.dataset.agent = kind;
|
|
const who = el("div", "who"), box = el("div");
|
|
who.append(el("div", "name", t("Long conversations in {agent}", { agent: a.name })), el("div", "sub", t("Its own size, for models that differ; Every agent follows Long conversations above")));
|
|
row.append(who, box);
|
|
let drawn;
|
|
const draw = () => {
|
|
// the opened row is built before the settings are read; drawn again then,
|
|
// and only when its own values changed, so the page under it holds still
|
|
const s = prefs || {}, own = (s.agentCompacts || {})[kind];
|
|
const key = JSON.stringify([own, s.compactAt]);
|
|
if (key === drawn) return;
|
|
drawn = key;
|
|
const mode = !own ? "all" : own.full ? "full" : "at";
|
|
// a size of its own starts from the one every agent has
|
|
const at = own ? own.at || 0 : s.compactAt || 0;
|
|
const set = (body) => writingPrefs(api("settings/agent-compact", { agent: kind, ...body }))
|
|
.then((ns) => { prefs = ns; renderSettings(); if (row.isConnected) draw(); }).catch(settingsErr);
|
|
const size = contextsText({ "*": at || 272000 });
|
|
const sg = segs([["all", t("Every agent")], ["at", t("Compact at {n}", { n: size.toUpperCase() })], ["full", t("Full window")]], mode,
|
|
(v) => set(v === "all" ? { own: false } : { own: true, full: v === "full", at }));
|
|
const num = input(at && own ? size : "", "272k");
|
|
num.className = "words compact-num";
|
|
num.hidden = mode !== "at";
|
|
num.title = t("Compact at");
|
|
num.setAttribute("aria-label", t("Compact at"));
|
|
num.onchange = () => {
|
|
const r = parseContexts(num.value);
|
|
if (r.error || Object.keys(r.map).some((k) => k !== "*")) return status(t("Compact at: {v} is not a number of tokens like 272k", { v: num.value.trim() }), "err");
|
|
set({ own: true, full: false, at: r.map["*"] || 0 });
|
|
};
|
|
const line = el("div", "compact-at");
|
|
line.append(sg, num);
|
|
box.replaceChildren(line);
|
|
};
|
|
draw();
|
|
list.querySelector(".agent-compact")?.remove();
|
|
list.querySelectorAll(".claude-cache").forEach((r) => r.remove());
|
|
const above = list.querySelector("#fullContextRow");
|
|
above ? above.after(row) : list.append(row);
|
|
if (kind === "claude") row.after(...claudeCacheRows(list));
|
|
// every agent's size, in its label, changes when the row above is drawn
|
|
const seen = new MutationObserver(() => row.isConnected ? draw() : seen.disconnect());
|
|
// Details can build this list before attaching it to the page.
|
|
const segsBox = list.querySelector("#fullContextSegs");
|
|
if (segsBox) seen.observe(segsBox, { childList: true });
|
|
};
|
|
|
|
// Claude Code's prompt cache, beside its compaction (Linka on Discord: the
|
|
// Usage card offers no way to change one that is already the cheapest):
|
|
// the main conversation's and the subagents', each 5 min, 1 hour or
|
|
// Claude Code's own default, which takes the key out of its settings.json
|
|
// (agent.ClaudeCacheSet). One a variable sets shows it, unchangeable here.
|
|
function claudeCacheRows(list) {
|
|
const KNOBS = [["cache_ttl", "Prompt cache · main conversation"], ["subagent_cache_ttl", "Prompt cache · subagents"]];
|
|
const OPTS = [["", "Claude Code's default"], ["5m", "5 min"], ["1h", "1 hour"]];
|
|
return KNOBS.map(([knob, label]) => {
|
|
const row = el("div", "row pref claude-cache");
|
|
row.dataset.knob = knob;
|
|
const who = el("div", "who"), val = el("div", "val");
|
|
who.append(el("div", "name", t(label)));
|
|
row.append(who, val);
|
|
let drawn;
|
|
const draw = () => {
|
|
const c = ((prefs || {}).claudeCache || []).find((x) => x.knob === knob) || {};
|
|
const key = JSON.stringify(c);
|
|
if (key === drawn) return;
|
|
drawn = key;
|
|
const cur = c.value || "", known = OPTS.find(([v]) => v === cur);
|
|
const b = el("button", "sess-pick");
|
|
b.type = "button";
|
|
b.setAttribute("aria-label", t(label));
|
|
b.setAttribute("aria-haspopup", "menu");
|
|
b.setAttribute("aria-expanded", "false");
|
|
b.append(el("span", "", known ? t(known[1]) : cur), svg(CHEV, 11, 1.6));
|
|
b.disabled = !!c.locked;
|
|
b.onclick = (e) => {
|
|
e.stopPropagation();
|
|
if (b.classList.contains("open")) return closeProtoMenu();
|
|
openProtoMenu(b, OPTS.map(([v, name]) => ({ v, name, note: "" })), cur, (v) => {
|
|
if (v === cur) return;
|
|
writingPrefs(api("settings/claude-cache", { knob, value: v }))
|
|
.then((ns) => { prefs = ns; renderSettings(); if (row.isConnected) draw(); }).catch(settingsErr);
|
|
}, label, "sess-menu", "right");
|
|
};
|
|
val.replaceChildren(b);
|
|
who.querySelector(".sub")?.remove();
|
|
if (c.locked) who.append(el("div", "sub", t("{name} is set and comes first", { name: c.locked })));
|
|
};
|
|
draw();
|
|
// drawn again when the settings are read again
|
|
const seen = new MutationObserver(() => row.isConnected ? draw() : seen.disconnect());
|
|
const box = list.querySelector("#fullContextSegs");
|
|
if (box) seen.observe(box, { childList: true });
|
|
return row;
|
|
});
|
|
}
|