Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions darknode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions lib/nexus/cheap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 };
3 changes: 3 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading