From c594326f42dd25f1f990044d1047751c75430cb2 Mon Sep 17 00:00:00 2001 From: Darknode-Official Date: Thu, 12 Nov 2026 20:59:09 -0800 Subject: [PATCH] /cheap: three fixes from an audit - The unused-skill list only refreshed when /cheap was typed, so it went stale: a skill you started using later stayed name-only. It now refreshes (at most daily, in the background) after claude messages. - Keep-warm reused the last launch even after /model, /effort or a setting changed the prompt, so it could refresh a cache the next message would not read. It now compares the launch the next message would make with the last one and skips the refresh when they differ (the model is compared as a model: "fable" vs "claude-fable-5-2"). The launch options are built in one place for both. - The skill list was rewritten to disk on every message; now only when it changes. Measured, no change needed: the daily skill scan reads 2.7 GB of transcripts in 3.3 s and never blocks the screen for more than 16 ms; changing which skills are trimmed keeps the cache (Haiku: full prefix read on the next two messages). --- darknode.js | 10 ++++++++-- lib/nexus/cheap.js | 13 +++++++++++-- test/run.js | 3 +++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/darknode.js b/darknode.js index 5bf7a1f..dcb7b96 100755 --- a/darknode.js +++ b/darknode.js @@ -2936,7 +2936,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const sessStart = Date.now(); // wall-clock start of this TUI session (dashboard uptime) const sessFiles = new Set(); // every repo-relative file the agent touched this session (for the dashboard) const toolUse = Object.create(null); // tool name -> call count this session (for the dashboard) - let cheapPrev = settingsStore.getSaved("cheapPrev") || null, modelHold = null, lastClaudeOpts = null, lastAskAt = 0, warming = false; // lastClaudeOpts/lastAskAt/warming: keep-warm reuses the last real launch exactly; settings to restore on /cheap off; a model switch waiting for its confirming repeat + let cheapPrev = settingsStore.getSaved("cheapPrev") || null, modelHold = null, lastClaudeOpts = null, lastAskAt = 0, warming = false, warmSkipAt = 0; // lastClaudeOpts/lastAskAt/warming: keep-warm reuses the last real launch exactly; settings to restore on /cheap off; a model switch waiting for its confirming repeat let cacheOn = !!S.cache; // response cache: serve exact-repeat read-only answers for free (off by default — opt in with /cache on) let cowork = { on: false, strong: "", weak: "", weakKind: "claude" }; // strong codes, weak does cheap work; weakKind: claude|ollama(local, free) const auxModel = () => (cowork.on && cowork.weakKind === "claude" && engine === "claude") ? cowork.weak : undefined; // weak CLAUDE model for aux calls @@ -4127,9 +4127,14 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const revealing = () => { const b = activeBlock || cur(); if (!b || !b.items) return false; return b.items.some((it) => it.type === "text" && it.shown < it.full.length); }; const startTick = () => { if (tick) return; tick = setInterval(() => { const b = activeBlock || cur(); if (b && b.items) for (const it of b.items) if (it.type === "text" && it.shown < it.full.length) { let ns = Math.min(it.full.length, it.shown + Math.max(2, Math.ceil((it.full.length - it.shown) / 22))); const cc = it.full.charCodeAt(ns - 1); if (cc >= 0xD800 && cc <= 0xDBFF && ns < it.full.length) ns++; it.shown = ns; } render(); if (!busy && !revealing() && !compact.on) { clearInterval(tick); tick = null; } }, 45); }; // idle refresh for the prompt-cache chip: re-render only when its text changes (minute steps, then seconds in the last minute) + // Everything a claude launch depends on besides the prompt. Keep-warm rebuilds it to check + // that the next real message would still match the cached prefix it is about to refresh. + const claudeOpts = (resume) => ({ resume, model: cowork.on ? cowork.strong : ((sess.userModel && sess.model && sess.model !== engine) ? sess.model : undefined), small: cowork.on && cowork.weakKind === "claude" ? cowork.weak : undefined, effort: effort || undefined, cheap: !!(cacheOn && tightEdits && cheapSide), skillOverrides: cacheOn && tightEdits && cheapSide ? Cheap.overridesFor(NHOME, cwd) : null, appendSystemPrompt: require("./lib/nexus/bg-cutoff").HOST_NOTE + "\n" + ((nexusMd ? "Project (.nexus/NEXUS.md):\n" + nexusMd.slice(0, 1000) : "") + (tightEdits ? "\n" + TIGHT_DIR : "") + (minimizeCalls ? "\nMinimize tool calls: chain shell commands, read only needed lines, never re-read after write. 2-5 calls for simple tasks." : "") + (lean ? "\nTerse output, no preamble." : "") + (styleDir(style) ? "\n" + styleDir(style) : "") + (hack ? "\n" + HACK_DIR : "") + policyPrompt() + ASK_ENGINE_NOTE) || undefined, mcpConfigPath: engineMcpConfigPath(), fallbackModel: fallback || undefined, maxBudgetUsd: costCap ? Math.max(0.01, costCap - sess.cost).toFixed(2) : undefined, disallow: mode === 2 ? READONLY_TOOLS : undefined }); // /cheap keep-warm: refresh a 1-hour cache just before it expires while you are away const keepWarm = () => { if (warming || !lastClaudeOpts || !Cheap.keepWarmDue({ cheap: !!(cacheOn && tightEdits && cheapSide), engine, sid: claudeSid, busy, lastAt: cacheWarm.lastAt, ttl: cacheWarm.ttl, now: Date.now(), lastHumanAt: lastAskAt, ctxTok: sess.ctxUsed })) return; + if (warmSkipAt === cacheWarm.lastAt) return; // already decided for this cache window + if (Cheap.prefixKey(claudeOpts(claudeSid)) !== Cheap.prefixKey(lastClaudeOpts)) { warmSkipAt = cacheWarm.lastAt; return; } // model, effort or a setting changed since: the next message will not read this cache, so refreshing it is wasted warming = true; const sid = claudeSid, model = sess.resolvedModel || sess.model; runClaudeStream(Cheap.KEEP_WARM_PROMPT, cwd, false, { onResult: (ev) => { if (claudeSid !== sid) return; // the chat was cleared meanwhile @@ -4436,6 +4441,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } if (!(res && res.interrupted) && (stat.files.size || stat.cmds || +dt >= 20 || block.items.filter((i) => i.type === "tool").length >= 3)) maybeRecap(block, text, { files: [...stat.files], cmds: stat.cmds }); maybeAutoCompact(); maybeAutoTitle(); idleTitle(); render(); refreshGit(); + if (engine === "claude" && cacheOn && tightEdits && cheapSide) Cheap.refreshUse(NHOME).catch(() => {}); // keeps the unused-skill list current (at most daily); it only ran when /cheap was typed if (nextQueue.length && !busy && !compact.on) scheduleDrain(300); }; let lastCtx = 0; // context size from the latest main-thread API call this turn @@ -4488,7 +4494,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { addUsage(_mu, u, sess.resolvedModel || sess.model); const mu = ev.modelUsage && ev.modelUsage[sess.model]; if (mu && mu.contextWindow) sess.ctxWindow = mu.contextWindow; }, - }, ctl, lastClaudeOpts = { resume: _resumeSid, model: cowork.on ? cowork.strong : ((sess.userModel && sess.model && sess.model !== engine) ? sess.model : undefined), small: cowork.on && cowork.weakKind === "claude" ? cowork.weak : undefined, effort: effort || undefined, cheap: !!(cacheOn && tightEdits && cheapSide), skillOverrides: cacheOn && tightEdits && cheapSide ? Cheap.overridesFor(NHOME, cwd) : null, appendSystemPrompt: require("./lib/nexus/bg-cutoff").HOST_NOTE + "\n" + ((nexusMd ? "Project (.nexus/NEXUS.md):\n" + nexusMd.slice(0, 1000) : "") + (tightEdits ? "\n" + TIGHT_DIR : "") + (minimizeCalls ? "\nMinimize tool calls: chain shell commands, read only needed lines, never re-read after write. 2-5 calls for simple tasks." : "") + (lean ? "\nTerse output, no preamble." : "") + (styleDir(style) ? "\n" + styleDir(style) : "") + (hack ? "\n" + HACK_DIR : "") + policyPrompt() + ASK_ENGINE_NOTE) || undefined, mcpConfigPath: engineMcpConfigPath(), fallbackModel: fallback || undefined, maxBudgetUsd: costCap ? Math.max(0.01, costCap - sess.cost).toFixed(2) : undefined, disallow: mode === 2 ? READONLY_TOOLS : undefined }).then(finish); + }, ctl, lastClaudeOpts = claudeOpts(_resumeSid)).then(finish); } else if (ENGINES[engine] && ENGINES[engine].kind === "cli") { // gemini / codex / opencode / aider const eng = ENGINES[engine]; const mdl = sess.model && sess.model !== engine ? sess.model : undefined; // only pass a model the user actually chose diff --git a/lib/nexus/cheap.js b/lib/nexus/cheap.js index a304ace..1cca422 100644 --- a/lib/nexus/cheap.js +++ b/lib/nexus/cheap.js @@ -46,6 +46,15 @@ function keepWarmDue(o) { if (left <= 0 || left > KEEP_WARM_LEAD_MS) return false; // expired already, or not yet near expiry return o.now - (o.lastHumanAt || o.lastAt) < KEEP_WARM_CAP_MS; } +// What decides whether the next message reuses the cached prefix: launch options +// minus the session id, the budget and the skill overrides (changing those keeps +// the cache, measured). The model is compared as a model, because after a turn +// Nexus holds the resolved id ("fable" becomes "claude-fable-5-2"). +function prefixKey(o) { + o = o || {}; + const k = o.model && claudeKey(o.model); + return JSON.stringify(Object.assign({}, o, { model: k ? k.key : o.model, resume: undefined, maxBudgetUsd: undefined, skillOverrides: undefined })); +} const keepWarmFlags = () => ["--fork-session", "--no-session-persistence", "--max-turns", "1"]; // Every skill's long description is resent in every session, used or not. @@ -98,7 +107,7 @@ const storeFile = (home) => path.join(home, ".nexus", "cheap-skills.json"); const readStore = (home) => { try { return JSON.parse(fs.readFileSync(storeFile(home), "utf8")) || {}; } catch (_) { return {}; } }; const writeStore = (home, patch) => { try { fs.mkdirSync(path.dirname(storeFile(home)), { recursive: true }); fs.writeFileSync(storeFile(home), JSON.stringify(Object.assign(readStore(home), patch))); } catch (_) {} }; -function rememberListed(home, skills) { if (Array.isArray(skills) && skills.length) writeStore(home, { listed: skills }); } +function rememberListed(home, skills) { if (Array.isArray(skills) && skills.length && JSON.stringify(skills) !== JSON.stringify(readStore(home).listed)) writeStore(home, { listed: skills }); } // every message reports the list: write only when it changed function overridesFor(home, cwd) { const s = readStore(home); return skillOverrides(s.listed, s.used, projectSkills(cwd)); } // Refresh the usage scan at most once a day, off the hot path. @@ -163,4 +172,4 @@ function switchHold(o) { return { hold: true, pending: { to: o.to, at: o.now }, cost: switchCost(o) }; } -module.exports = { PRESET_KEYS, HOLD_MS, KEEP_WARM_PROMPT, KEEP_WARM_CAP_MS, keepWarmDue, keepWarmFlags, skillOverrides, projectSkills, scanSkillUse, rememberListed, overridesFor, refreshUse, launchFlags, mainBucket, spendReport, switchCost, switchHold }; +module.exports = { PRESET_KEYS, HOLD_MS, KEEP_WARM_PROMPT, KEEP_WARM_CAP_MS, keepWarmDue, keepWarmFlags, prefixKey, skillOverrides, projectSkills, scanSkillUse, rememberListed, overridesFor, refreshUse, launchFlags, mainBucket, spendReport, switchCost, switchHold }; diff --git a/test/run.js b/test/run.js index 3c7b6d9..334a134 100644 --- a/test/run.js +++ b/test/run.js @@ -3732,6 +3732,9 @@ ok("shouldCheck: first launch runs; throttled within 4h; runs after", shouldChec eq("overrides ride on --settings", CH.launchFlags(true, { pdf: "name-only" }).slice(1), ["--settings", '{"skillOverrides":{"pdf":"name-only"}}']); eq("overrides ignored when off", CH.launchFlags(false, { pdf: "name-only" }), []); eq("keep-warm ping turns hooks off alongside overrides", CH.launchFlags(true, { pdf: "name-only" }, true).slice(1), ["--settings", '{"skillOverrides":{"pdf":"name-only"},"disableAllHooks":true}']); + const pk = { model: "fable", effort: "high", appendSystemPrompt: "x", cheap: true, skillOverrides: { a: "name-only" }, resume: "s1", maxBudgetUsd: "3.00" }; + ok("keep-warm: same launch = same prefix, even after the model alias resolves to its id", CH.prefixKey(pk) === CH.prefixKey(Object.assign({}, pk, { model: "claude-fable-5-2", resume: "s2", maxBudgetUsd: "2.50", skillOverrides: null }))); + ok("keep-warm: a new model, effort or system prompt is a different prefix", CH.prefixKey(pk) !== CH.prefixKey(Object.assign({}, pk, { model: "claude-opus-5-5" })) && CH.prefixKey(pk) !== CH.prefixKey(Object.assign({}, pk, { effort: "max" })) && CH.prefixKey(pk) !== CH.prefixKey(Object.assign({}, pk, { appendSystemPrompt: "y" }))); eq("keep-warm ping runs one turn on an unsaved fork", CH.keepWarmFlags(), ["--fork-session", "--no-session-persistence", "--max-turns", "1"]); const H = 3600000, kw = { cheap: true, engine: "claude", sid: "s1", busy: false, lastAt: H, ttl: H, now: 2 * H - 120000, lastHumanAt: H, ctxTok: 80000 }; const due = (o) => CH.keepWarmDue(Object.assign({}, kw, o));