From 1929a82ee2c8257be0b93dae13480e4b60f469cb Mon Sep 17 00:00:00 2001 From: Darknode-Official Date: Sun, 22 Nov 2026 19:43:25 -0800 Subject: [PATCH] fix(nexus): visual audit fixes - F1/F10/F15: local loop only sends a request back for a tool call when it is clearly about the files here (one nudge), honours "do not use tools", skips a thought that repeats the answer, never prints "done" as an answer; -p exits 1 when the model gives no answer - F2: every place that creates .nexus/ goes through ensureNexusDir, which drops .nexus/.gitignore ("*") without touching the project's .gitignore - F3/F5/F11: files count as written (footer, undo checkpoint, recap, dashboard, usage) only after a successful write; a non-zero exit is a failed command; a denied command is not counted as run; dashboard turns survive /clear; "1 file" - F4: loop-guard notice shows once as a dim note inside the turn - F6: an interrupted local turn ends flagged interrupted, without "done" - F7: no recap for interrupted or empty turns; auto-recap off by default on the local engine unless the recap setting is chosen - F8: /model runs as typed; arg completion prefers prefix matches - F9: ordered lists keep their numbers, bold is painted across wrapped rows, list items get a hanging indent - F12: Esc pops only dismissable panels and does not arm the double-Esc rewind; Esc Esc in one read still clears the input - F13: --continue / -c alias --resume; unknown flags exit 2; --tui never runs a one-shot task - F14: session saved on exit, after setting/theme changes and after a rewind; a plain --resume says "previous session restored" - F16: darknode --help wraps to the terminal; shift+tab text lists ask mode; hints recommend qwen2.5-coder like first-run setup - F17: local engine asks before commands in accept-edits mode and before file changes too in normal mode (Allow once / for this session / Deny); usage log names bypass mode - F18: /help settings usage, /mcp "+N more", /clear resets the tab title, /model checks Ollama tags, bare /pager opens the last reply, dismissed ask_user card reads as text, /bisect slash wording, /settings columns at narrow widths, Ctrl+C exit hint is transient, tips end with an ellipsis, footer/recap keep a gap before the scroll rail, /keys Up/Down wording --- darknode.js | 230 +++++++++++++++++++++++--------------- docs/NEXUS-COMMANDS.md | 6 +- lib/cli/command-help.js | 3 +- lib/cli/md-render.js | 13 ++- lib/cli/tui-format.js | 26 ++++- lib/governance/policy.js | 2 +- lib/governance/usage.js | 3 +- lib/nexus/bisect.js | 2 +- lib/nexus/code-search.js | 2 +- lib/nexus/costsave.js | 2 +- lib/nexus/dashboard.js | 2 +- lib/nexus/local-loop.js | 29 ++++- lib/nexus/loopguard.js | 9 +- lib/nexus/nexus-dir.js | 23 ++++ package.json | 2 +- test/run.js | 4 +- test/session-save.test.js | 4 +- test/undo-audit.test.js | 2 +- test/visual-audit.test.js | 188 +++++++++++++++++++++++++++++++ 19 files changed, 438 insertions(+), 114 deletions(-) create mode 100644 lib/nexus/nexus-dir.js create mode 100644 test/visual-audit.test.js diff --git a/darknode.js b/darknode.js index 322b72e..2db93de 100755 --- a/darknode.js +++ b/darknode.js @@ -63,7 +63,7 @@ const { getSnippet, saveBody, expandSnippet } = require("./lib/cli/snippets"); / const { expandAts } = require("./lib/cli/at-mentions"); // @path → inlined file contents (TUI + line mode) const Branches = require("./lib/cli/branches"); // /branch files: what is saved, what a switch restores const { conversationText, exportMarkdown } = require("./lib/cli/convo-text"); // /find: the whole conversation (with tool output) as searchable text // /pager: read a whole tool output in-place (less-style keys + search) // click a URL / CVE / GHSA / CWE in the transcript to open it // answer markdown: highlighted code, tables, quotes, links -const { asText, sanitizeTerm, charWidth, strWidth, cutIdx, toolResultView, bootHeader, welcomeCard, idleHome, MARK_ORBIT, orbitGlyph, activityLabel, footerLine, helpLines, padRight: padRightV, ansiWrap, modelLabel, effortLabel, explainEngineError } = require("./lib/cli/tui-format"); // pure TUI layout helpers (compact boot header + model/effort display names) +const { asText, sanitizeTerm, charWidth, strWidth, cutIdx, toolResultView, bootHeader, welcomeCard, idleHome, MARK_ORBIT, orbitGlyph, activityLabel, footerLine, helpLines, padRight: padRightV, ansiWrap, modelLabel, effortLabel, explainEngineError, fitWidth, wrapHelp } = require("./lib/cli/tui-format"); // pure TUI layout helpers (compact boot header + model/effort display names) const Composer = require("./lib/cli/composer"); // pure composer model + key dispatch + per-project history (lib/cli/composer.js) const Ask = require("./lib/cli/ask"); // pure renderer + key reducer for the model's ask_user multiple-choice prompt (lib/cli/ask.js) const CompactAnim = require("./lib/cli/compact-anim"); @@ -75,7 +75,7 @@ const Agents = require("./lib/cli/agents"); // pure renderer + key reducer for t const { idleWatchdog, engineWaits } = require("./lib/nexus/engine-watchdog"); // engine hang guard: idle-based (ask_user no longer holds the engine open) const AskDefer = require("./lib/nexus/ask-defer"); // ask_user never blocks the engine: answer later, sent as the next prompt const { createAskServer } = require("./lib/nexus/ask-bridge"); // socket that lets external engines raise the real popup (lib/nexus/ask-bridge.js) -const { buildEngineMcpServers, writeMcpConfigFile, toOpencodeConfig, toGeminiServers, writeEngineConfigFile } = require("./lib/nexus/engine-mcp"); // MCP-server set for external engines (lib/nexus/engine-mcp.js); codexMcpArgs is used inside engines.js +const { buildEngineMcpServers, writeMcpConfigFile, toOpencodeConfig, toGeminiServers, writeEngineConfigFile } = require("./lib/nexus/engine-mcp"); const { ensureNexusDir } = require("./lib/nexus/nexus-dir"); // MCP-server set for external engines (lib/nexus/engine-mcp.js); codexMcpArgs is used inside engines.js const ASK_MCP_PATH = require("path").join(__dirname, "lib", "nexus", "ask-mcp.js"); // stdio MCP shim spawned by the engine const Term = require("./lib/cli/term"); // terminal-mode registry: always restore modes, even on a hard crash (lib/cli/term.js) const Acp = require("./lib/nexus/acp-client"); // Agent Client Protocol transport for engines that speak it (lib/nexus/acp-client.js) @@ -1002,7 +1002,7 @@ async function cliRun(args, gate) { else if (cmd === "logout") console.log(nexusLogout() ? " " + green("signed out.") : " " + gray("was not signed in.")); else if (cmd === "whoami") { const a = nexusAuth(); console.log(a ? " " + (a.name || a.email || a.uid) + gray(" " + String(a.provider).replace(".com", "")) : " " + gray("not signed in")); } else if (cmd === "policy") { - if (rest[0] === "init") { const fs = require("fs"), path = require("path"); const dir = path.join(process.cwd(), ".nexus"), f = path.join(dir, "policy.json"); if (fs.existsSync(f)) console.log(" " + yellow(".nexus/policy.json already exists — not overwriting")); else { fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(f, JSON.stringify(POLICY_DEFAULTS, null, 2) + "\n"); console.log(" " + green("created ") + ".nexus/policy.json" + gray(" — a starting policy; tighten protected paths, denied commands, and limits, then ") + cyan("darknode policy") + gray(" to review")); } return; } + if (rest[0] === "init") { const fs = require("fs"), path = require("path"); const dir = path.join(process.cwd(), ".nexus"), f = path.join(dir, "policy.json"); if (fs.existsSync(f)) console.log(" " + yellow(".nexus/policy.json already exists — not overwriting")); else { ensureNexusDir(process.cwd()); fs.writeFileSync(f, JSON.stringify(POLICY_DEFAULTS, null, 2) + "\n"); console.log(" " + green("created ") + ".nexus/policy.json" + gray(" — a starting policy; tighten protected paths, denied commands, and limits, then ") + cyan("darknode policy") + gray(" to review")); } return; } const p = loadPolicy(process.cwd()); if (rest[0] === "--json") { console.log(JSON.stringify(p, null, 2)); } else { banner(); h1("Effective security policy" + (p.org ? " (ORG-ENFORCED)" : "")); const row = (k, v) => console.log(" " + k.padEnd(22) + v); row("source", p.org ? "org floor (~/.darknode/policy.json) + local .nexus/policy.json" : ".nexus/policy.json (or defaults)"); row("protected paths", (p.protectedPaths || []).length + " " + gray((p.protectedPaths || []).slice(0, 6).join(", ") + ((p.protectedPaths || []).length > 6 ? " …" : ""))); row("denied commands", (p.deniedCommands || []).length ? p.deniedCommands.join(", ") : gray("(built-in destructive guard only)")); row("max files / turn", p.maxFilesPerTurn || gray("unlimited")); row("block secret writes", p.blockSecrets ? green("on") : red("off")); row("network", p.allowNetwork ? "allowed" : red("blocked")); row("audit", p.audit ? "on (.nexus/audit.jsonl, hash-chained)" : gray("off")); const warns = policyWarnings(process.cwd()); if (warns.length) { console.log("\n " + yellow("config warnings:")); warns.forEach((wn) => console.log(" " + yellow("• " + wn))); } console.log("\n " + gray("machine-readable: ") + cyan("darknode policy --json") + "\n"); } @@ -1527,7 +1527,7 @@ const NEXUS_DOCS = { __proto__: null, overview: { title: "Overview", body: "Nexus is a terminal AI coding agent built into the Darknode CLI. It drives one of six\nengines from a single dependency-free binary:\n claude — the Claude Code CLI (needs `claude` installed + logged in)\n gemini · codex · opencode · aider — those vendors' CLIs, same interface\n ollama — a free, private, local model on your machine (needs Ollama)\n\nIt reads & writes files, runs commands, checkpoints every change, shows real token cost\nlive, saves tokens by delegating cheap work to a weaker/local model, and keeps secrets off\nthe cloud. Launch it with `darknode nexus --tui`. Type / inside for the command menu." }, quickstart: { title: "Quick start", body: - " darknode init scaffold .nexus/ (project context Nexus reads each session)\n darknode nexus --tui full-screen agent (Claude if installed, else local)\n darknode nexus --tui -e ollama drive a 100% local, private, free agent\n darknode nexus \"add tests to server.js and run them\" one-shot task\n\nFree local setup:\n curl -fsSL https://ollama.com/install.sh | sh\n ollama pull gpt-oss:20b # fast local agent (~14GB); pull gpt-oss:120b for max power (~63GB)" }, + " darknode init scaffold .nexus/ (project context Nexus reads each session)\n darknode nexus --tui full-screen agent (Claude if installed, else local)\n darknode nexus --tui -e ollama drive a 100% local, private, free agent\n darknode nexus \"add tests to server.js and run them\" one-shot task\n\nFree local setup:\n curl -fsSL https://ollama.com/install.sh | sh\n ollama pull qwen2.5-coder # fast local agent (~14GB); pull gpt-oss:120b for max power (~63GB)" }, engines: { title: "Engines & models", body: " /engine switch AI: claude · gemini · codex · opencode · aider · ollama\n /model [name] show/set the model\n /models list cloud tiers + installed local models\n /fallback auto-retry on a cheaper model when rate-limited\n /cowork strong model codes; weak (cheaper Claude tier OR\n a free local model via ollama:) does cheap work\n Aliases: opus · sonnet · haiku (or full ids like claude-opus-5-5)" }, cost: { title: "Saving cost", body: @@ -1590,12 +1590,13 @@ function nexusHelp() { console.log(" global git config need an explicit -y)"); console.log(" --lean minimal output (also /lean in the session)"); console.log(" --resume / --new reopen the last chat (default) / start blank"); + console.log(" -c, --continue same as --resume"); console.log(" -- the rest is the task, even if it starts with -"); console.log(" -v, --version print the version"); console.log(" -h, --help show this help\n"); console.log(" " + bold("IN THE SESSION")); console.log(" /help lists every command · /model · /clear · /undo · /diff · /exit"); - console.log(" shift+tab cycles permissions (accept edits · plan · bypass)"); + console.log(" shift+tab cycles permissions: ask first · accept edits · plan · bypass"); console.log(" " + gray("@path attaches a file: fix the bug in @src/app.js") + "\n"); console.log(" " + bold("ENVIRONMENT")); console.log(" DARKNODE_API_BASE, DARKNODE_API_KEY any OpenAI-compatible API (or /api)"); @@ -1609,7 +1610,7 @@ function nexusHelp() { console.log(" " + gray("Prompts go to the engine you pick: -e ollama keeps everything on this machine.") + "\n"); } -const NEXUS_FLAGS = ["--yes", "--skip-permissions", "--dangerously-skip-permissions", "--print", "--lean", "--tui", "--ui", "--resume", "--new", "--fresh", "--model", "--engine", "--help", "--version"]; +const NEXUS_FLAGS = ["--yes", "--skip-permissions", "--dangerously-skip-permissions", "--print", "--lean", "--tui", "--ui", "--resume", "--continue", "--new", "--fresh", "--model", "--engine", "--help", "--version"]; async function aiCoder(argv) { const fs = require("fs"), path = require("path"); @@ -1624,22 +1625,22 @@ async function aiCoder(argv) { else if (a === "--print" || a === "-p") { printMode = true; autoApprove = true; } else if (a === "--lean") leanFlag = true; else if (a === "--tui" || a === "--ui") tuiFlag = true; - else if (a === "--resume") resumeFlag = true; + else if (a === "--resume" || a === "--continue" || a === "-c") resumeFlag = true; // --continue / -c: the spelling other coding CLIs use else if (a === "--new" || a === "--fresh") resumeFlag = "new"; else if (a === "-m" || a === "--model" || a === "-e" || a === "--engine") { const v = arr[++i]; if (!v || v[0] === "-") { console.error(" " + red(a + " needs a value") + gray(a[1] === "e" || a === "--engine" ? " — e.g. -e ollama (" + ENGINE_ORDER.join(", ") + ", api)" : " — e.g. -m gpt-oss:20b")); process.exitCode = 2; return; } if (a === "-m" || a === "--model") modelOverride = v; else enginePref = v; } else if (a === "-h" || a === "--help") return nexusHelp(); else if (a === "--version" || a === "-v" || a === "-V") { console.log("nexus " + VERSION); return; } else if (a === "--") { parts.push(...arr.slice(i + 1)); break; } // end of options: what follows is task text even if it starts with "-" - else { if (a[0] === "-" && a.length > 1 && !/^-\d/.test(a)) unknownFlags.push(a); parts.push(a); } // a flag-looking token is probably a typo (e.g. --modell); warn but still pass it through as task text so nothing silently breaks + else { if (a[0] === "-" && a.length > 1 && !/^-\d/.test(a)) unknownFlags.push(a); parts.push(a); } // a flag-looking token is probably a typo (e.g. --modell); it stops below, before any turn is spent } // A clear misspelling of a real option (--prnt, --modell=x) stops before any turn - // is spent; anything else is passed through as task text with a note. + // is spent, with a suggestion. const typo = unknownFlags.map((f) => [f, nearestFlag(f, NEXUS_FLAGS)]).find(([, n]) => n); if (typo) { console.error(" " + red("unknown option " + typo[0]) + " — did you mean " + cyan(typo[1]) + "?" + gray(" (to send it as task text, put it after --)")); process.exitCode = 2; return; } - // Surface likely typos: an unrecognized --flag otherwise vanishes into the prompt - // and silently wastes a (paid) turn. We keep passing it through — just say so. + // Any other unrecognized --flag stops too: taken as task text it started a one-shot + // agent run on the words "--continue" (and spent a turn on it). parts = parts.filter((x) => String(x).trim()); // nexus --print "" is no task, not an empty one - if (unknownFlags.length) console.error(" " + yellow("note: unrecognized option" + (unknownFlags.length > 1 ? "s " : " ") + unknownFlags.join(", ") + " — treated as task text. See `nexus --help`.")); + if (unknownFlags.length) { console.error(" " + red("unknown option" + (unknownFlags.length > 1 ? "s " : " ") + unknownFlags.join(", ")) + gray(" — see nexus --help (to send it as task text, put it after --)")); process.exitCode = 2; return; } let cfg = {}; try { cfg = JSON.parse(stripBom(fs.readFileSync(path.join(cwd, ".nexus", "config.json"), "utf8"))); } catch (_) {} const lean = leanFlag || !!cfg.lean; // minimal-output mode (also used for read/step budgets) // Any-model support: an OpenAI-compatible endpoint (from .nexus/config.json or @@ -1685,8 +1686,8 @@ async function aiCoder(argv) { if (!parts.length) { console.error(" " + red("--print needs a task") + gray(" — e.g. nexus --print \"add tests for utils.js\" (or pipe it in: echo task | nexus --print)")); process.exitCode = 2; return; } } const oneShot = parts.length > 0; - if (process.stdout.isTTY && !printMode && !oneShot) return nexusTui(engine, cwd, nexusMd, resumeFlag === "new" ? false : (resumeFlag ? "restart" : "launch"), { model: modelOverride, lean: leanFlag, yes: autoApprove }); // like `claude -c`: the last chat comes back on every launch; --new starts blank - if (tuiFlag && !process.stdout.isTTY) console.log(" " + gray("--tui needs an interactive terminal.")); + if (tuiFlag && !printMode && (!process.stdout.isTTY || oneShot)) { console.error(" " + red(!process.stdout.isTTY ? "--tui needs an interactive terminal" : "--tui opens the session and takes no task text") + gray(!process.stdout.isTTY ? "" : " — type the task in the session, or drop --tui to run it once")); process.exitCode = 2; return; } // --tui never falls through to a one-shot run + if (process.stdout.isTTY && !printMode && !oneShot) return nexusTui(engine, cwd, nexusMd, resumeFlag === "new" ? false : (resumeFlag && process.env.NEXUS_SUPERVISED ? "restart" : "launch"), { model: modelOverride, lean: leanFlag, yes: autoApprove }); // like `claude -c`: the last chat comes back on every launch; --new starts blank // --print is for scripts: stdout carries only the answer (raw), everything else — header, tool steps, errors — goes to stderr const answer = (t) => process.stdout.write(String(t).trim() + "\n"); if (printMode) console.log = console.error.bind(console); @@ -1702,7 +1703,7 @@ async function aiCoder(argv) { if (explicit && /^darknode(:|$)/i.test(explicit)) { if (await ensureOllamaServer(dlog)) { if (await ensureDarknodeModel(dlog)) modelList = await ollamaTags(); } } else if (!explicit && chooseCoderToPull(modelList)) { if (await ensureOllamaServer(dlog)) { if (await ensureCoderModel(dlog)) modelList = await ollamaTags(); } } } - if (!modelList.length && !apiConfigured()) { if (!printMode) banner(); const oa = ollamaAddr(); console.log(" " + red(await ollamaReachable() ? "No local model. Run `ollama pull gpt-oss:20b`, or use --engine claude." : "Can't reach Ollama at " + oa.host + ":" + oa.port + ". Start it with `ollama serve` (or set OLLAMA_HOST), or use --engine claude.")); process.exitCode = 1; return; } model = modelOverride || cfg.apiModel || cfg.model || process.env.DARKNODE_MODEL || pickCoderModel(modelList); if (!model) { if (!printMode) banner(); console.log(" " + red(apiConfigured() ? "An API is set but no model — pass -m (e.g. -m gpt-4o-mini) or set apiModel in .nexus/config.json." : "No local model. Install Ollama + `ollama pull gpt-oss:20b`.")); process.exitCode = 1; return; } } + if (!modelList.length && !apiConfigured()) { if (!printMode) banner(); const oa = ollamaAddr(); console.log(" " + red(await ollamaReachable() ? "No local model. Run `ollama pull qwen2.5-coder`, or use --engine claude." : "Can't reach Ollama at " + oa.host + ":" + oa.port + ". Start it with `ollama serve` (or set OLLAMA_HOST), or use --engine claude.")); process.exitCode = 1; return; } model = modelOverride || cfg.apiModel || cfg.model || process.env.DARKNODE_MODEL || pickCoderModel(modelList); if (!model) { if (!printMode) banner(); console.log(" " + red(apiConfigured() ? "An API is set but no model — pass -m (e.g. -m gpt-4o-mini) or set apiModel in .nexus/config.json." : "No local model. Install Ollama + `ollama pull qwen2.5-coder`.")); process.exitCode = 1; return; } } if (!printMode) { const paint = useColor ? { brand: bold, accent: mag, muted: gray, faint: dim, rule: gray, value: cyan } : null; const cols = (process.stdout.columns || 80); @@ -1762,7 +1763,7 @@ async function aiCoder(argv) { const r = (reply || "").trim() || "Hi! I'm Nexus — ask me anything, or give me a task and I'll use tools to do it."; if (printMode) answer(r); else console.log(" " + r + "\n"); if (printMode) return; continue; } - let didTool = false, nudges = 0; const protoSt = { bad: 0 }, loopGuard = makeLoopGuard(); // same stall breaker as the TUI and sub-agent loops + let didTool = false, nudges = 0, noTools = LocalLoop.forbidsTools(userMsg) ? 2 : 0; const protoSt = { bad: 0 }, loopGuard = makeLoopGuard(); // same stall breaker as the TUI and sub-agent loops for (let step = 1; step <= (lean ? 12 : 25); step++) { let raw; try { raw = await ollamaChat(model, messages, CODER_SCHEMA); } catch (e) { console.log(" " + red(e.message)); if (printMode) process.exitCode = 1; break; } let o; try { o = JSON.parse(raw); } catch (_) { o = extractJson(raw, null); } // recover fenced/prose-wrapped JSON from lenient providers @@ -1774,12 +1775,16 @@ async function aiCoder(argv) { if (o.args) compact.args = o.args; if (o.final) compact.final = o.final; messages.push({ role: "assistant", content: JSON.stringify(compact) }); - if (o.thought && !printMode) console.log(" " + gray("think: " + o.thought.slice(0, 120))); + if (o.thought && !printMode && !(o.action === "final" && LocalLoop.sameText(o.thought, o.final))) console.log(ansiWrap("think: " + oneline(o.thought, 200), Math.max(20, (process.stdout.columns || 80) - 11)).map((l, k) => (k ? " " : " ") + gray(l.trim())).join("\n")); // wrapped under the text, not into column 0 if (o.action === "final") { - if (!didTool && !isChitchat(userMsg) && nudges++ < 3) { messages.push({ role: "tool", content: "Do the real work first." }); continue; } - if (printMode) answer(o.final || "done"); else console.log("\n " + green("done: ") + bold(o.final || "done") + "\n"); break; + // Only a request that is clearly about the files here is sent back for a look first: + // "Do the real work first." on every answer made small models invent an edit to satisfy it. + if (!didTool && LocalLoop.wantsToolWork(userMsg) && nudges++ < 1) { messages.push({ role: "tool", content: LocalLoop.NUDGE }); continue; } + const fin = (typeof o.final === "string" ? o.final : o.final == null ? "" : JSON.stringify(o.final, null, 2)).trim(); + if (printMode) { if (fin) answer(fin); else { console.error(" " + red("no answer: the model ended without a reply")); process.exitCode = 1; } } else console.log("\n " + (fin ? bold(fin) : gray("(no answer: the model ended without a reply)")) + "\n"); break; // never the literal "done" as if it were the answer } const { name, a } = normalizeToolCall(o); let result; + if (noTools && name && name !== "ask_user") { noTools--; messages.push({ role: "tool", content: LocalLoop.NO_TOOLS_REPLY }); continue; } // the user said not to use tools: the call is not run // same gate as the sub-agent runner: .nexus/policy.json and the destructive-command guard apply even when -y / -p auto-approve const _pa = isExecTool(name) ? { type: "run", command: execCommand(name, a) } : name === "write_file" ? { type: "write", path: a && a.path } : name === "edit_file" ? { type: "edit", path: a && a.path } : null; let _pblk = null; @@ -2048,7 +2053,7 @@ function nexusCheckpoint(cwd) { const path = require("path"), fs = require("fs"); _cp.execSync("git rev-parse --is-inside-work-tree", { cwd, stdio: "ignore" }); const idx = path.join(cwd, ".nexus", "ckpt.index"); - fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); + ensureNexusDir(cwd); // This runs synchronously on every message, so it must be cheap. The temp index is // KEPT between turns (seeded once from the repo's own index) so git's stat cache // skips unchanged files, and .nexus is excluded up front so a large session.json is @@ -2399,7 +2404,7 @@ async function deviceTool(name, a, cwd) { const st = typeof t === "object" ? String(t.status || t.state || "pending") : "pending"; if (content) AGENT_TODOS.push({ content: String(content).slice(0, 200), status: /\b(?:done|complete|completed|finish|finished)\b/i.test(st) ? "done" : /prog|doing|active|current/i.test(st) ? "in_progress" : "pending" }); } - try { fs.mkdirSync(rp(".nexus"), { recursive: true }); fs.writeFileSync(rp(".nexus/todos.json"), JSON.stringify(AGENT_TODOS)); } catch (_) {} + try { ensureNexusDir(rp(".")); fs.writeFileSync(rp(".nexus/todos.json"), JSON.stringify(AGENT_TODOS)); } catch (_) {} const rendered = AGENT_TODOS.map((t) => (t.status === "done" ? "[x] " : t.status === "in_progress" ? "[~] " : "[ ] ") + t.content).join("\n"); return { ok: true, count: AGENT_TODOS.length, done: AGENT_TODOS.filter((t) => t.status === "done").length, todos: AGENT_TODOS, rendered }; } @@ -2593,7 +2598,7 @@ async function ollamaExec(model, task, ctx, cwd, signal) { const compact = { action: o.action }; if (o.tool) compact.tool = o.tool; if (o.args) compact.args = o.args; if (o.final) compact.final = o.final; messages.push({ role: "assistant", content: JSON.stringify(compact) }); if (o.thought) console.log(" " + gray("think: " + (o.thought || "").slice(0, 120))); - if (o.action === "final") { if (!didTool && !isChitchat(task) && step < 3) { messages.push({ role: "tool", content: "Do the real work first." }); continue; } return { ok: true, output: log + "\n" + (o.final || "") }; } + if (o.action === "final") { if (!didTool && LocalLoop.wantsToolWork(task) && step < 2) { messages.push({ role: "tool", content: LocalLoop.NUDGE }); continue; } return { ok: true, output: log + "\n" + (o.final || "") }; } const { name, a } = normalizeToolCall(o); let result; // enterprise policy guardrails (.nexus/policy.json) — the same gate the in-process loop applies, so a // sub-agent / local engine can't write a protected path, plant a secret, run a denied command or fetch off-net. @@ -2680,7 +2685,7 @@ async function nexusRun(argv) { else if (a === "-h" || a === "--help") return nexusRunHelp(); else goalParts.push(a); } - const cwd = process.cwd(), NEXUS = path.join(cwd, ".nexus"); fs.mkdirSync(NEXUS, { recursive: true }); secureNexus(cwd); + const cwd = process.cwd(), NEXUS = path.join(cwd, ".nexus"); ensureNexusDir(cwd); secureNexus(cwd); const stateFile = path.join(NEXUS, "run.json"), reportFile = path.join(NEXUS, "report.md"), memFile = path.join(NEXUS, "memory.md"); const memory = (() => { try { return fs.readFileSync(memFile, "utf8"); } catch (_) { return ""; } })(); const avail = {}; for (const e of ENGINE_ORDER) avail[e] = engineAvail(e); @@ -2844,6 +2849,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // keyboard (-1 = composer has focus); `agentView` is the open transcript // overlay { i, scroll } or null. addAgent/finishAgent feed the registry. let agents = [], agentSel = -1, agentView = null; + let clearedTurns = 0; // turns wiped by /clear: the dashboard's Turns stays session-wide like its token and cost totals let turnLog = []; // one entry per finished paid/local turn this session — feeds /turns + /costtips (never persisted, no prompt text beyond a short label) const cacheWarm = { lastAt: 0, ttl: 0 }; // when a claude call last touched the prompt cache + which TTL it wrote (status chip + /warm) let bypassCacheOnce = false; // /retry sets this so the next send skips the exact-repeat cache and really re-runs @@ -3008,7 +3014,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { let bangLog = []; const runBang = (c) => { const dg = classifyDanger(c); if (dg.level !== "ok" && guard !== "off") transcript.push({ role: "system", text: "Darknode: this looks destructive (" + dg.why + ") — running it because you typed it directly" }); const blk = { role: "system", text: "$ " + c + "\nrunning…" }; transcript.push(blk); busy = true; ctl = makeCtl(); scroll = 0; render(); const lt = Tail.createTail(); coderShell(c, cwd, { ctl, onData: (d) => { Tail.pushTail(lt, d); blk.text = "$ " + c + "\n" + (Tail.tailLines(lt, 6).join("\n") || "running…"); renderSoon(); } }).then((r) => { const o = (r.output || "").replace(/[ \t\r\n]+$/, "").split("\n").map((l) => { l = l.replace(/\r+$/, ""); const k = l.lastIndexOf("\r"); return k >= 0 ? l.slice(k + 1) : l; }).join("\n"); /* a \r progress bar keeps its last frame */ blk.text = "$ " + c + "\n" + (o || "(no output)") + (r.code ? "\n[exit " + r.code + "]" : ""); bangLog.push({ cmd: c, output: o.length > 2000 ? o.slice(0, 2000) + "\n…[truncated]" : o, code: r.code }); if (bangLog.length > 10) bangLog.splice(0, bangLog.length - 10); busy = false; ctl = null; render(); }).catch((e) => { const msg = "error: " + (e && e.message || e); blk.text = "$ " + c + "\n" + msg; bangLog.push({ cmd: c, output: msg, code: 1 }); if (bangLog.length > 10) bangLog.splice(0, bangLog.length - 10); busy = false; ctl = null; render(); }); }; // #note appends a durable memory to .nexus/NEXUS.md - const addMemory = (line) => { try { const dir = path.join(cwd, ".nexus"); fs.mkdirSync(dir, { recursive: true }); const md = path.join(dir, "NEXUS.md"); let c = ""; try { c = fs.readFileSync(md, "utf8"); } catch (_) {} fs.writeFileSync(md, addNote(c, line)); /* into the ## Notes section, wherever it sits */ transcript.push({ role: "system", text: "remembered → .nexus/NEXUS.md: " + line }); } catch (e) { transcript.push({ role: "system", text: "could not save note: " + e.message }); } }; + const addMemory = (line) => { try { const dir = ensureNexusDir(cwd); const md = path.join(dir, "NEXUS.md"); let c = ""; try { c = fs.readFileSync(md, "utf8"); } catch (_) {} fs.writeFileSync(md, addNote(c, line)); /* into the ## Notes section, wherever it sits */ transcript.push({ role: "system", text: "remembered → .nexus/NEXUS.md: " + line }); } catch (e) { transcript.push({ role: "system", text: "could not save note: " + e.message }); } }; // ---- ansi-aware width helpers ---- const stripA = (s) => String(s).replace(/\x1b\[[0-9;]*m/g, ""); const clip = (s, n) => { if (n <= 0) return ""; let o = "", v = 0, hasColor = false; for (const part of String(s).split(/(\x1b\[[0-9;]*m)/)) { if (/^\x1b/.test(part)) { o += part; hasColor = true; continue; } for (const ch of part) { const cw = charWidth(ch); if (v + cw > n) return o + (hasColor ? "\x1b[0m" : ""); o += ch; v += cw; } } return o; }; @@ -3083,7 +3089,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { ["/autoupdate", "auto-update on launch: /autoupdate on | check | off"], ["/next", "queue a prompt after the current turn (Ctrl+X / Ctrl+S runs it now; clear / rm to manage)"], ["/btw", "side question — answered from context, never saved to history"], - ["/find", "search the whole conversation, tool output included: /find (n/N step through matches, q closes)"], ["/pager", "read the newest tool output in full: /pager [n] | answer (j/k, space, g/G, / search, q)"], + ["/find", "search the whole conversation, tool output included: /find (n/N step through matches, q closes)"], ["/pager", "read the last reply in full, or a tool output: /pager [n] (j/k, space, g/G, / search, q)"], ["/compat", "which features this model supports (nothing is hidden)"], ["/mode", "capability preset: /mode power|cost|speed|reset (model-compatible only)"], ["/god-prompt", "arm max-quality mode for your NEXT message only (gold border, auto-reverts)"], @@ -3109,7 +3115,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { { title: "Send & edit", rows: [["Enter","send the message"],["Alt+Enter / Shift+Enter / Ctrl+J","insert a newline"],["Home / End","start / end of the line"],["Ctrl+A / Ctrl+E","start / end of the line"],["Ctrl+← / → · Alt+B / F","jump a word"],["Alt+Backspace · Ctrl+W","delete the word before the cursor"],["Ctrl+U / Ctrl+K","kill to start / end of line"],["Ctrl+Y","yank back what was killed"],["Ctrl+_","undo the last edit"]] }, { title: "History", rows: [["Ctrl+P / Ctrl+N","previous / next command (your draft is kept)"],["Up / Down (while typing)","step through history"],["Ctrl+R","reverse-search history"]] }, { title: "Session", rows: [["Shift+Tab","cycle mode: normal / accept edits / plan / bypass"],["Ctrl+O","expand or collapse tool detail"],["Esc","interrupt a running turn"],["Ctrl+X / Ctrl+S","send a typed/queued prompt now (interrupts the current turn)"],["Esc Esc","rewind: undo last turn's file changes + edit the prompt (/redo re-applies)"],["Esc Esc","clear the input (idle)"],["Ctrl+C","interrupt, then clear, then exit"],["Ctrl+D","exit on an empty line"],["Ctrl+G","edit the message in $EDITOR"],["Ctrl+L","redraw the screen"]] }, - { title: "Scroll, select & copy", rows: [["Mouse wheel","scroll the transcript"],["Up / Down (empty input)","scroll line by line"],["PgUp / PgDn","scroll a page"],["Home","jump to the oldest"],["End","jump to the latest"],["Select with the mouse","just drag to select, then copy — the mouse is yours by default"],["/mouse on","re-enable click + wheel capture (selection then needs Shift+drag)"]] }, + { title: "Scroll, select & copy", rows: [["Mouse wheel","scroll the transcript"],["Up / Down (empty input)","recall earlier prompts (the wheel scrolls)"],["PgUp / PgDn","scroll a page"],["Home","jump to the oldest"],["End","jump to the latest"],["Select with the mouse","just drag to select, then copy — the mouse is yours by default"],["/mouse on","re-enable click + wheel capture (selection then needs Shift+drag)"]] }, ]; // Custom project slash commands (Claude-Code style): each .md file in // .nexus/commands/ or .claude/commands/ becomes /; its body is the prompt, @@ -3149,9 +3155,9 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const mcpToolList = () => mcpServers.filter((s) => !s.error).flatMap((s) => (s.tools || []).map((t) => ({ full: "mcp__" + s.name + "__" + t.name, srv: s, name: t.name, desc: t.description || "" }))); // saved prompt macros (.nexus/snippets.json) + a lightweight local codebase index (.nexus/index.json) let snippets = {}; try { snippets = JSON.parse(stripBom(fs.readFileSync(path.join(cwd, ".nexus", "snippets.json"), "utf8"))) || {}; } catch (_) {} - const saveSnippets = () => { try { fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(path.join(cwd, ".nexus", "snippets.json"), JSON.stringify(snippets, null, 2)); } catch (_) {} }; + const saveSnippets = () => { try { ensureNexusDir(cwd); fs.writeFileSync(path.join(cwd, ".nexus", "snippets.json"), JSON.stringify(snippets, null, 2)); } catch (_) {} }; let plan = []; try { const pj = JSON.parse(stripBom(fs.readFileSync(path.join(cwd, ".nexus", "plan.json"), "utf8"))); if (Array.isArray(pj)) plan = pj; } catch (_) {} - const savePlan = () => { try { fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(path.join(cwd, ".nexus", "plan.json"), JSON.stringify(plan, null, 2)); } catch (_) {} }; + const savePlan = () => { try { ensureNexusDir(cwd); fs.writeFileSync(path.join(cwd, ".nexus", "plan.json"), JSON.stringify(plan, null, 2)); } catch (_) {} }; // .nexus/index.json: a BM25 index over ~60-line chunks (lib/nexus/bm25.js). An index from an // older Nexus (a per-file keyword list) is ignored until /index is run again. let index = null; try { const raw = JSON.parse(stripBom(fs.readFileSync(path.join(cwd, ".nexus", "index.json"), "utf8"))); if (raw && raw.version === bm25.VERSION && raw.docs) index = raw; } catch (_) {} @@ -3184,7 +3190,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const detectVerify = () => { let entries = []; try { entries = fs.readdirSync(cwd); } catch (_) {} const files = {}; for (const n of ["package.json", "Makefile", "makefile", "GNUmakefile"]) if (entries.includes(n)) { try { files[n] = fs.readFileSync(path.join(cwd, n), "utf8"); } catch (_) {} } return detectProjectCommands(entries, files); }; // ---- inline markdown / command coloring ---- const paintCode = (c) => (/(^|\s)(node|npm|npx|git|python3?|pip3?|bash|sh|cd|ls|cat|make|cargo|go|docker|curl|grep|sed|rm|mkdir|chmod|sudo)\b/.test(c) || /\s--?\w/.test(c)) ? blue(c) : mag(c); - const colorMd = (line, inCode) => { if (inCode) return blue(line); const hm = line.match(/^(#{1,6})\s+(.*)/); if (hm) { const lvl = hm[1].length; const txt = hm[2].replace(/\*\*([^*]+)\*\*/g, "$1").replace(/`([^`]+)`/g, (_, c) => paintCode(c)); return (lvl <= 2 ? bold(cyan("▍ ")) : " ") + bold(cyan(txt)); } let s = line.replace(/\*\*([^*]+)\*\*/g, (_, c) => "\x1b[1m" + c.replace(/`([^`]+)`/g, (__, ic) => "\x1b[0m" + paintCode(ic) + "\x1b[1m") + "\x1b[0m").replace(/`([^`]+)`/g, (_, c) => paintCode(c)); s = s.replace(/^(\s*)\d+\.\s/, (_, sp) => sp + cyan("• ")); return s.replace(/^(\s*[-*]\s)/, (_, b) => cyan(b.replace(/[-*]/, "•"))); }; + const colorMd = (line, inCode) => { if (inCode) return blue(line); const hm = line.match(/^(#{1,6})\s+(.*)/); if (hm) { const lvl = hm[1].length; const txt = hm[2].replace(/\*\*([^*]+)\*\*/g, "$1").replace(/`([^`]+)`/g, (_, c) => paintCode(c)); return (lvl <= 2 ? bold(cyan("▍ ")) : " ") + bold(cyan(txt)); } let s = line.replace(/\*\*([^*]+)\*\*/g, (_, c) => "\x1b[1m" + c.replace(/`([^`]+)`/g, (__, ic) => "\x1b[0m" + paintCode(ic) + "\x1b[1m") + "\x1b[0m").replace(/`([^`]+)`/g, (_, c) => paintCode(c)); s = s.replace(/^(\s*)(\d+[.)])(\s)/, (_, sp, n, w) => sp + cyan(n) + w); return s.replace(/^(\s*[-*+]\s)/, (_, b) => cyan(b.replace(/[-*+]/, "•"))); }; // ordered lists keep their numbers // Rendered answer rows, memoised per text item: the frame redraws on every // keystroke and spinner tick, so re-highlighting unchanged answers is waste. const MDC = { inline: (l) => colorMd(l, false), gray, dim, mag, green, yellow, cyan, red, bold, italic: (x) => p("\x1b[3m", x), underline: (x) => p("\x1b[4m", x) }; @@ -3192,7 +3198,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const mdLines = (owner, text, w) => { const c = owner && _mdMemo.get(owner); if (c && c.w === w && c.text === text) return c.lines; const lines = renderMd(text, w, MDC); if (owner) _mdMemo.set(owner, { w, text, lines }); return lines; }; const wrap = (text, pre = 2) => ansiWrap(text, Math.max(1, cols() - 2 - pre)); // pre = visible width of the row prefix; body rows hold cols()-2 cells. // ANSI-aware: measures visible width, never slices a color escape, carries color across lines // Rendered like Claude Code: gray ※, bold "recap:", italic body, then the disable hint. - const recapLines = (t) => { const it = (x) => "\x1b[3m" + gray(x) + "\x1b[23m"; return wrap("recap: " + t + " (disable recaps in /config)").map((ln, j) => j ? " " + it(ln) : gray("※") + " " + bold(gray("recap:")) + it(ln.slice(6))); }; + const recapLines = (t) => { const it = (x) => "\x1b[3m" + gray(x) + "\x1b[23m"; return wrap("recap: " + t + " (disable recaps in /config)", 3).map((ln, j) => j ? " " + it(ln) : gray("※") + " " + bold(gray("recap:")) + it(ln.slice(6))); }; // ---- tool-card labels (Claude-Code style) ---- // ---- edit diff: a unified +/- preview for Write/Edit/MultiEdit, the way // Claude Code shows an Update card (line counts + red/green lines) instead of @@ -3433,6 +3439,10 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { L.push(dim(gray("· thinking" + (expanded ? "" : " ctrl+o to read")))); if (expanded) for (const ln of wrap(it.full).slice(0, 24)) L.push(" " + dim(gray(ln))); prevType = "thinking"; + } else if (it.type === "note") { // a dim in-turn notice (loop guard) + if (prevType) L.push(""); + for (const ln of wrap(it.text, 3)) L.push(" " + dim(gray(ln))); + prevType = "note"; } else if (it.type === "tool") { if (prevType) L.push(""); const dot = it.status === "run" || it.status === "wait" ? workGlyph() : it.status === "err" ? red("✗") : green("✓"); @@ -3509,9 +3519,9 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { for (let k = 0; k < Math.max(NEXUS_TIPS.length, et.length); k++) { if (et[k]) tips.push(et[k]); if (NEXUS_TIPS[k]) tips.push(NEXUS_TIPS[k]); } // interleave so an engine-specific tip shows first L.push(""); L.push(workGlyph() + " " + cyan(clip(busyWord || activityLabel(m.items), Math.max(10, cols() - 34))) + gray(" " + fmtElapsed(el) + tok + " · esc to stop")); - if (tips.length) L.push(" " + gray("╰─ ") + dim(gray("tip " + tips[Math.floor(el / 6) % tips.length]))); + if (tips.length) L.push(" " + gray("╰─ ") + dim(gray(fitWidth("tip " + tips[Math.floor(el / 6) % tips.length], Math.max(8, cols() - 8))))); } else if (m.summary) { - wrap(m.summary, 5).forEach((x, k) => L.push((k ? " " : " " + cyan("╰─") + " ") + gray(x))); + wrap(m.summary, 6).forEach((x, k) => L.push((k ? " " : " " + cyan("╰─") + " ") + gray(x))); // 6, not 5: a column of air before the scroll rail } if (m.recap && !(busy && m === cur())) recapLines(m.recap).forEach((x) => L.push(x)); L.push(""); @@ -3671,7 +3681,11 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { if (!gen) return []; const items = gen(); if (!partial) return items; - return items.filter((it) => it[0].toLowerCase().startsWith(partial) || it[0].toLowerCase().includes(partial)); + // Names that start with what you typed come first; a number ("/model 3" picks by + // number) never matches inside a name, where "3" pulled up llama3.1 and Enter took it. + const pre = items.filter((it) => it[0].toLowerCase().startsWith(partial)); + if (/^\d+$/.test(partial)) return pre; + return pre.concat(items.filter((it) => !it[0].toLowerCase().startsWith(partial) && it[0].toLowerCase().includes(partial))); }; const refreshArgCache = () => { if (Date.now() - argCacheTs > 10000) { argCacheTs = Date.now(); ollamaTags().then((ms) => { argCache = ms || []; }).catch(() => {}); } }; // ---- input syntax highlighting ---- @@ -3822,9 +3836,10 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } catch (err) { done("error: " + (err && err.message || err)); } })(); }; + const sessAllow = new Set(); // "Allow for this session" picks at the local engine's permission prompt (never saved) let perms = { allow: [], deny: [] }, heldAllow = []; try { const p = JSON.parse(stripBom(fs.readFileSync(path.join(cwd, ".nexus", "permissions.json"), "utf8"))); const strs = (v) => (Array.isArray(v) ? v : []).filter((r) => typeof r === "string" && r.trim()); perms = { allow: strs(p && p.allow), deny: strs(p && p.deny) }; } catch (_) {} // permission allowlist if (perms.allow.length && !isRepoTrusted(cwd)) { heldAllow = perms.allow; perms.allow = []; _untrustedRepoConfig = true; } // a cloned repo's allow rules would skip the guard - held until /trust (deny rules only restrict, so they load) - const savePerms = () => keepTrust(cwd, () => { try { fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(path.join(cwd, ".nexus", "permissions.json"), JSON.stringify({ allow: heldAllow.concat(perms.allow.filter((r) => !heldAllow.includes(r))), deny: perms.deny }, null, 2)); } catch (_) {} }); + const savePerms = () => keepTrust(cwd, () => { try { ensureNexusDir(cwd); fs.writeFileSync(path.join(cwd, ".nexus", "permissions.json"), JSON.stringify({ allow: heldAllow.concat(perms.allow.filter((r) => !heldAllow.includes(r))), deny: perms.deny }, null, 2)); } catch (_) {} }); let curBranch = "main"; // conversation branch (session branching) const imgPaths = new Map(); // [Image#N] -> the image file it stands for (dropped path or Ctrl+V capture), resolved at submit let nextQueue = []; // /next queue — prompts to run after the current turn (declared here so the status bar can show the count) @@ -4016,7 +4031,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { case "/lean": return lean; case "/cache": return cacheOn; case "/offline": return offline; - case "/recap": return autoRecap; + case "/recap": return recapActive(); case "/vim": return !!composer.vim; case "/mouse": return mouseOn; case "/tight": return tightEdits; @@ -4053,8 +4068,8 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { frame.push(str); } else { const _sb = statusBar(), _room = C - 4 - strWidth(stripA(_sb)) - 5, _hl = hint(_room), _hw = strWidth(stripA(_hl)); - // a mode that doesn't fit shortens the status side instead of being cut or dropped - frame.push(footerLine(_hl, (hintIsMode && _hw > _room ? clip(_sb, Math.max(0, C - 4 - _hw - 5 - 1)) + (useColor ? "\x1b[0m" : "") + gray("…") : _sb) + gray(" ···"), C)); + // a mode (or a passing hint such as "Press Ctrl+C again to exit") that doesn't fit shortens the status side instead of being cut or dropped + frame.push(footerLine(_hl, ((hintIsMode || linkNotice) && _hw > _room ? clip(_sb, Math.max(0, C - 4 - _hw - 5 - 1)) + (useColor ? "\x1b[0m" : "") + gray("…") : _sb) + gray(" ···"), C)); clickZones.push({ row, c0: C - 4, c1: C - 1, kind: "toggle" }); } } @@ -4138,7 +4153,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const b = ensureAskBridge(); if (!b) return null; try { return buildEngineMcpServers({ sockPath: b.sockPath, askMcpPath: ASK_MCP_PATH, userServers: loadMcpConfig(cwd) || {}, readonly: !!readonly }); } catch (_) { return null; } }; - const engineMcpConfigPath = (readonly) => { const servers = engineMcpServers(readonly); if (!servers) return null; try { return writeMcpConfigFile(path.join(cwd, ".nexus"), servers); } catch (_) { return null; } }; + const engineMcpConfigPath = (readonly) => { const servers = engineMcpServers(readonly); if (!servers) return null; try { return writeMcpConfigFile(ensureNexusDir(cwd), servers); } catch (_) { return null; } }; // Per-engine injection of the ask bridge for the CLI engines (claude uses // --mcp-config directly). Returns the `extra` to hand runEngineTask: codex // takes -c overrides via mcpServers; opencode is pointed at a merged config @@ -4155,7 +4170,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } else { // claude-style { mcpServers } (gemini's system-overrides settings file — gemini merges it, so only 'nexus' is added) obj = { mcpServers: toGeminiServers(servers) }; fname = ".gemini-settings.json"; } - const p = writeEngineConfigFile(path.join(cwd, ".nexus"), fname, obj); + try { ensureNexusDir(cwd); } catch (_) {} const p = writeEngineConfigFile(path.join(cwd, ".nexus"), fname, obj); return (p && m.mcpEnvVar) ? { env: { [m.mcpEnvVar]: p } } : undefined; } return { mcpServers: servers }; // cArgs (codex): -c overrides via args() @@ -4399,7 +4414,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { }; const onResize = () => { lastLines = null; if (!loading) render(); }; // geometry changed — force a full repaint let sizePoll = null; // re-reads the window size: a missed SIGWINCH otherwise leaves every row drawn for the old width, and the terminal wraps them - const cleanup = () => { if (cleaned) return; cleaned = true; tuiActive = false; if (tick) { clearInterval(tick); tick = null; } if (_rsT) { clearTimeout(_rsT); _rsT = null; } if (sizePoll) { clearInterval(sizePoll); sizePoll = null; } if (_drainT) { clearTimeout(_drainT); _drainT = null; } if (_escTimer) { clearTimeout(_escTimer); _escTimer = null; } if (compact.cur) { try { compact.cur.ctl.kill(); } catch (_) {} } if (ctl && ctl.kill) try { ctl.kill(); } catch (_) {} try { bgJobs.killAll(); } catch (_) {} try { if (dashServer) dashServer.close(); } catch (_) {} for (const s of mcpServers) { try { s.cp && s.cp.kill(); } catch (_) {} } try { askBridge && askBridge.close(); } catch (_) {} try { process.stdin.setRawMode(false); } catch (_) {} process.stdin.pause(); process.stdin.removeAllListeners("data"); out.removeListener("resize", onResize); process.removeListener("exit", cleanup); process.removeListener("SIGINT", onSigint); process.removeListener("SIGTERM", onSigterm); process.removeListener("SIGHUP", onHup); process.removeListener("uncaughtException", onFatal); process.removeListener("unhandledRejection", onRejection); try { term.teardown(); } catch (_) {} }; + const cleanup = () => { if (cleaned) return; cleaned = true; tuiActive = false; try { saveChat(); } catch (_) {} /* notes since the last turn (settings, theme, rewinds) survive a resume */ if (tick) { clearInterval(tick); tick = null; } if (_rsT) { clearTimeout(_rsT); _rsT = null; } if (sizePoll) { clearInterval(sizePoll); sizePoll = null; } if (_drainT) { clearTimeout(_drainT); _drainT = null; } if (_escTimer) { clearTimeout(_escTimer); _escTimer = null; } if (compact.cur) { try { compact.cur.ctl.kill(); } catch (_) {} } if (ctl && ctl.kill) try { ctl.kill(); } catch (_) {} try { bgJobs.killAll(); } catch (_) {} try { if (dashServer) dashServer.close(); } catch (_) {} for (const s of mcpServers) { try { s.cp && s.cp.kill(); } catch (_) {} } try { askBridge && askBridge.close(); } catch (_) {} try { process.stdin.setRawMode(false); } catch (_) {} process.stdin.pause(); process.stdin.removeAllListeners("data"); out.removeListener("resize", onResize); process.removeListener("exit", cleanup); process.removeListener("SIGINT", onSigint); process.removeListener("SIGTERM", onSigterm); process.removeListener("SIGHUP", onHup); process.removeListener("uncaughtException", onFatal); process.removeListener("unhandledRejection", onRejection); try { term.teardown(); } catch (_) {} }; tuiActive = true; const onFatal = (e) => { const sig = e === "SIGINT" || e === "SIGTERM"; try { term.teardown(); } catch (_) {} try { cleanup(); } catch (_) {} if (e && e instanceof Error) { try { process.stderr.write("\nNexus exited on error: " + e.message + "\n"); } catch (_) {} } try { resolve(); } catch (_) {} if (sig) process.exit(0); }; const onSigint = () => onFatal("SIGINT"), onSigterm = () => onFatal("SIGTERM"); @@ -4507,7 +4522,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { try { const _cr = (sess.byModel && Object.values(sess.byModel).reduce((a, m) => a + (m.cacheRead || 0), 0)) || 0; turnLog.push({ t: Date.now(), inTok: Math.max(0, din), outTok: Math.max(0, dout), cost: Math.max(0, dcost), dt: +dt || 0, model: sess.model || engine, engine, label: oneline(text || "", 64), paid: !!PAID[engine], cacheRead: Math.max(0, _cr - (stat.cacheRead0 || 0)) }); if (turnLog.length > 300) turnLog.shift(); } catch (_) {} if (PAID[engine]) { impact.cloudTurns++; impact.cloudInTok += Math.max(0, din); impact.cloudOutTok += Math.max(0, dout); impact.cloudCost += Math.max(0, dcost); } else { impact.localTurns++; impact.localTok += Math.max(0, din + dout); } // Chargeback ledger: one non-sensitive metrics line per turn (no prompt text, no paths). - try { appendUsage(cwd, { ts: new Date().toISOString(), operator: ident.operator, team: ident.team, engine, model: sess.model || engine, mode: ["normal", "auto", "plan"][mode] || "normal", inTok: Math.max(0, din), outTok: Math.max(0, dout), cost: Math.max(0, dcost), seconds: +dt, files: stat.files.size, commands: stat.cmds, interrupted: !!(res && res.interrupted) }); } catch (_) {} + try { appendUsage(cwd, { ts: new Date().toISOString(), operator: ident.operator, team: ident.team, engine, model: sess.model || engine, mode: ["normal", "auto", "plan", "bypass"][mode] || "normal", inTok: Math.max(0, din), outTok: Math.max(0, dout), cost: Math.max(0, dcost), seconds: +dt, files: stat.files.size, commands: stat.cmds, interrupted: !!(res && res.interrupted) }); } catch (_) {} for (const p of stat.paths) if (p) sessFiles.add(p); // accumulate session-wide file + tool activity for the dashboard for (const it of block.items) if (it.type === "tool" && it.name) toolUse[it.name] = (toolUse[it.name] || 0) + 1; const bits = []; @@ -4558,7 +4573,8 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { transcript.push({ role: "system", text: yellow("Claude Code ended its session while " + what + " " + (bgCut === 1 ? "was" : "were") + " still working, which stops them.") + gray(" Resuming them automatically (" + bgResumes + " of " + BG.RESUME_LIMIT + "). Esc stops it.") }); } else transcript.push({ role: "system", text: yellow("Claude Code ended its session while " + what + " " + (bgCut === 1 ? "was" : "were") + " still working, which stops them.") + gray(" Nexus already resumed " + BG.RESUME_LIMIT + " times for this message; type continue to resume again.") }); } - 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 }); + const _realReply = block.items.some((i) => i.type === "text" && i.full && i.full.trim() && !/^\((interrupted|no output)\)$/.test(i.full.trim())); + if (!(res && res.interrupted) && (_realReply || stat.files.size) && (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 }); // nothing to recap from an empty or stopped turn: a model asked to recap nothing invents it 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); @@ -4670,7 +4686,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { let _tags = null; if (!mdl) { _tags = await ollamaTags(); mdl = pickCoderModel(_tags); } sess.model = mdl || engine; - if (!mdl) { textItem().full = apiConfigured() ? "An API is configured but no model is set — run /model (e.g. /model gpt-4o-mini), or /api ." : _tags && _tags.unreachable ? "Can't reach Ollama at " + ollamaAddr().host + ":" + ollamaAddr().port + " — start it with `ollama serve` (or point OLLAMA_HOST at it), or use /engine claude." : "No local model found. Install Ollama and pull one, e.g. `ollama pull gpt-oss:20b`, or use /engine claude."; return finish({ output: "" }); } + if (!mdl) { textItem().full = apiConfigured() ? "An API is configured but no model is set — run /model (e.g. /model gpt-4o-mini), or /api ." : _tags && _tags.unreachable ? "Can't reach Ollama at " + ollamaAddr().host + ":" + ollamaAddr().port + " — start it with `ollama serve` (or point OLLAMA_HOST at it), or use /engine claude." : "No local model found. Install Ollama and pull one, e.g. `ollama pull qwen2.5-coder`, or use /engine claude."; return finish({ output: "" }); } // Greeting / trivial message → reply conversationally and NEVER enter the tool // loop (so "hi" can't make a weak model start probing the system). if (isChitchat(promptText) && !imgAttach.length) { @@ -4682,7 +4698,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // later turn either, not just within one turn. Safe because doCompact // clears it whenever it trims oMsgs — so we never tell the model a file // is "already in context" after that content was pruned away. - let didTool = false, nudges = 0, filesThisTurn = 0; const readCache = sess.readCache || (sess.readCache = {}); const fileSnap = sess.fileSnap || (sess.fileSnap = new Map()); const toolMemo = createToolMemo(); // per turn: the user may edit files between turns + let didTool = false, nudges = 0, noTools = LocalLoop.forbidsTools(text) ? 2 : 0, filesThisTurn = 0; const readCache = sess.readCache || (sess.readCache = {}); const fileSnap = sess.fileSnap || (sess.fileSnap = new Map()); const toolMemo = createToolMemo(); // per turn: the user may edit files between turns const loopGuard = makeLoopGuard(); // break the agent out of repeat/oscillation stalls const protoSt = { bad: 0 }; // consecutive replies Nexus could not use (see lib/nexus/proto-reply.js) const stepLimit = lean ? 12 : (stepCap ? 20 : 100000); // step cap is opt-in: off by default = run until the model stops (lean keeps its own 12 cap) @@ -4711,11 +4727,12 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // as context) so past chain-of-thought isn't resent on every step. const _ctx = { action: o.action }; if (o.tool) _ctx.tool = o.tool; if (o.args) _ctx.args = o.args; if (o.final) _ctx.final = o.final; oMsgs.push({ role: "assistant", content: JSON.stringify(_ctx) }); - if (o.thought) { const t = textItem(); t.full += (t.full ? "\n" : "") + o.thought; renderSoon(); } - if (o.action === "final") { if (!didTool && !isChitchat(promptText) && nudges++ < 2) { oMsgs.push({ role: "tool", content: "Do the real work with tools first." }); continue; } if (o.final) { const t = textItem(); t.full += (t.full ? "\n\n" : "") + (typeof o.final === "string" ? o.final : JSON.stringify(o.final, null, 2)); } break; } + if (o.thought && !(o.action === "final" && LocalLoop.sameText(o.thought, o.final))) { const t = textItem(); t.full += (t.full ? "\n" : "") + o.thought; renderSoon(); } // small models often repeat the answer as the thought + if (o.action === "final") { if (!didTool && LocalLoop.wantsToolWork(text) && nudges++ < 1) { oMsgs.push({ role: "tool", content: LocalLoop.NUDGE }); continue; } if (o.final) { const t = textItem(); t.full += (t.full ? "\n\n" : "") + (typeof o.final === "string" ? o.final : JSON.stringify(o.final, null, 2)); } break; } const { name, a } = normalizeToolCall(o); if (!name) { if (PR.missingTool(o, oMsgs, protoSt).stop) { textItem().full += (textItem().full ? "\n" : "") + PR.GAVE_UP; break; } continue; } // action "tool" with no tool name protoSt.bad = 0; + if (noTools && name !== "ask_user") { noTools--; oMsgs.push({ role: "tool", content: LocalLoop.NO_TOOLS_REPLY }); continue; } // the user said not to use tools: the call is not run const card = { type: "tool", id: "o" + step, name, input: a, label: olbl(name, a), status: "run", start: Date.now(), detail: JSON.stringify(a).slice(0, 400) }; if (a.path && name === "write_file") card.diff = editDiff("Write", { file_path: a.path, content: a.content }); else if (a.path && name === "edit_file") card.diff = editDiff("Edit", { file_path: a.path, old_string: a.find, new_string: a.replace }); @@ -4723,7 +4740,6 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { block.items.push(card); if (name === "run_command") { runningShells++; stat.cmds++; } if (name === "spawn_agents") activeAgents += (a.tasks || []).length; - if (["write_file", "edit_file"].includes(name) && a.path) { stat.files.add(base(a.path)); const r = relOf(a.path); if (r) stat.paths.add(r); } render(); let result, blocked = false; // Path jail BEFORE any permission/policy check: resolve the real (symlink- @@ -4761,6 +4777,23 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { a.__act = act; // stash for post-exec audit } if (!blocked && hooks) { const hr = runHooks(hooks, "PreToolUse", { TOOL_NAME: name, TOOL_ARGS: JSON.stringify(a) }, cwd); if (hr.block) { result = { error: "blocked by PreToolUse hook" + (hr.out ? ": " + hr.out : "") }; blocked = true; } } + // Permission prompt: normal mode asks before any file change or command, accept-edits + // only before commands (and sub-agents, which run both unasked). An allow rule + // (/allow) or an earlier "Allow for this session" skips it; plan and bypass never ask. + if (!blocked && _perm !== "allow" && (mode === 0 || mode === 1)) { + const _act = a.__act, _isCmd = !!(_act && _act.type === "run"), _isFile = !!(_act && /^(write|edit|delete|move)$/.test(_act.type)), _isAg = name === "spawn_agents"; + const _key = _isCmd ? "run:" + (_act.command || name) : _isFile ? "file:" + (_act.path || name) : _isAg ? "agents" : null; + if (_key && (mode === 0 || !_isFile) && !sessAllow.has(_key)) { + const _dg = _isCmd ? classifyDanger(_act.command || "") : null; + const _what = _isCmd ? "run: " + oneline(_act.command || name, 80) : _isAg ? "start " + (a.tasks || []).length + " sub-agent(s), which edit files and run commands without asking" : _act.type + " " + (_act.path ? relP(_act.path) : "files"); + card.status = "wait"; render(); + const _ans = await askUser("Allow Nexus to " + _what + "?" + (_dg && _dg.level !== "ok" ? " (" + _dg.why + ")" : ""), ["Allow once", "Allow for this session", "Deny"]); + card.status = "run"; + const _pick = _ans && !_ans.cancelled && !_ans.typed ? _ans.index : 2; + if (_pick === 1) sessAllow.add(_key); + else if (_pick !== 0) { result = { error: "denied at the permission prompt" + (_ans && _ans.typed && _ans.value ? " — the user said: " + oneline(String(_ans.value), 200) : "") + ". Don't retry it; carry on without it or ask the user how to proceed." }; blocked = true; card.status = "err"; } + } + } const _undoSnap = !blocked && a.__act && (a.__act.type === "write" || a.__act.type === "edit" || a.__act.type === "delete" || a.__act.type === "move") && name !== "undo_edit" ? editUndo.snapshot(a.__act.path ? (a.__act.type === "move" && a.__act.from ? [path.resolve(cwd, a.__act.from), require("./lib/nexus/fs-tools").destOf(path.resolve(cwd, a.__act.from), path.resolve(cwd, a.__act.path))] : [path.resolve(cwd, a.__act.path)]) : (() => { try { return parsePatch(a.patch || a.diff || a.content || a.input || "").filter((f) => f.file).map((f) => path.resolve(cwd, f.file)); } catch (_) { return []; } })()) : null; @@ -4817,7 +4850,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { else if (name === "repo_map" || name === "repomap" || name === "map") { const rm = getRepoMap({ fresh: !!a.fresh }); result = rm && rm.map ? { map: rm.text, files: rm.map.fileCount, symbols: rm.map.symbolCount } : { error: "could not build a repo map here" }; } else if (name === "find_symbol" || name === "symbol" || name === "goto") { const rm = getRepoMap(); const q = a.name || a.symbol || a.query || a.q || ""; const hits = rm && rm.map ? findSymbol(rm.map, q, { limit: 20 }) : []; result = hits.length ? { symbol: q, definitions: hits } : { symbol: q, definitions: [], note: "no definition found in the repo map — try search{pattern} for usages" }; } else if (name === "verify" || name === "run_tests" || name === "test_project") { const cmds = detectVerify(); const kind = (a.kind || "").toLowerCase(); const chosen = (kind && cmds[kind]) ? { kind, cmd: cmds[kind] } : (a.command ? { kind: "custom", cmd: String(a.command) } : pickVerify(cmds)); if (!chosen) { result = { error: "no test/build command detected for this project — pass command to run a specific one", detected: cmds }; } else { const r = await coderShell(chosen.cmd, cwd, { ctl, timeoutMs: (+a.timeout_s || 600) * 1000 }); const _vout = compactOutput(r.output, lean ? 2500 : (trimOutput ? 6000 : 1e9)); const _vd = r.code !== 0 ? diagnose(_vout, r.code, chosen.cmd) : null; result = { ran: chosen.cmd, kind: chosen.kind, code: r.code, ok: r.code === 0, output: _vout + diagAnnotate(_vd) }; if (_vd) result.diagnosis = _vd.category; } } - else if (name === "remember") { const note = a.text || a.note || a.content || a.fact || a.memory || ""; let md0 = ""; try { md0 = fs.readFileSync(path.join(cwd, ".nexus", "NEXUS.md"), "utf8"); } catch (_) {} const rr = mergeMemory(md0, note); if (rr.added) { fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(path.join(cwd, ".nexus", "NEXUS.md"), rr.md); result = { ok: true, remembered: true }; transcript.push({ role: "system", text: "remembered → .nexus/NEXUS.md: " + oneline(note, 60) }); } else result = { ok: true, remembered: false, note: rr.reason || "already known" }; } + else if (name === "remember") { const note = a.text || a.note || a.content || a.fact || a.memory || ""; let md0 = ""; try { md0 = fs.readFileSync(path.join(cwd, ".nexus", "NEXUS.md"), "utf8"); } catch (_) {} const rr = mergeMemory(md0, note); if (rr.added) { ensureNexusDir(cwd); fs.writeFileSync(path.join(cwd, ".nexus", "NEXUS.md"), rr.md); result = { ok: true, remembered: true }; transcript.push({ role: "system", text: "remembered → .nexus/NEXUS.md: " + oneline(note, 60) }); } else result = { ok: true, remembered: false, note: rr.reason || "already known" }; } else if (name === "ask_user" || name === "ask_question" || name === "ask") { // Pause the loop and ask the user a multiple-choice question, // rendered exactly like the Claude-Code "pick one" prompt. The @@ -4857,7 +4890,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { else if (name === "http_fetch" || name === "web_fetch" || name === "fetch_url" || name === "http") { // loopback only for a server one of OUR background jobs started; everything else local/private stays blocked const _own = (href) => { try { const u = new URL(href); if (!/^(localhost|127(\.\d+){3}|\[::1\])$/i.test(u.hostname)) return false; return require("./lib/nexus/procs").portOwnedBy(+(u.port || (u.protocol === "https:" ? 443 : 80)), bgJobs.pids()); } catch (_) { return false; } }; result = await require("./lib/nexus/http-fetch").httpFetchTool(a, { isLocal: (href) => _ssrfLocal(href) && !_own(href) }); } - else { const dr = await deviceTool(name, a, cwd); result = dr !== null ? dr : { error: "unknown tool " + name }; if (dr !== null && ["move", "copy", "copy_file", "move_file", "rename", "make_dir", "mkdir", "delete", "delete_file", "rm"].includes(name) && (a.path || a.to || a.dest)) { const fp = a.to || a.dest || a.path; stat.files.add(base(fp)); const r = relOf(fp); if (r) stat.paths.add(r); invalidateRepoMap(); } } + else { const dr = await deviceTool(name, a, cwd); result = dr !== null ? dr : { error: "unknown tool " + name }; if (dr !== null && !(dr && dr.error) && ["move", "copy", "copy_file", "move_file", "rename", "make_dir", "mkdir", "delete", "delete_file", "rm"].includes(name) && (a.path || a.to || a.dest)) { const fp = a.to || a.dest || a.path; stat.files.add(base(fp)); const r = relOf(fp); if (r) stat.paths.add(r); invalidateRepoMap(); } } } catch (e) { result = { error: e.message }; } if (_syn.length && result && !result.error) { const _w = _syn.map((x) => SyntaxGate.afterEdit(x.cap, x.f)).filter(Boolean).join(" "); if (_w) { if (typeof result === "object") result.syntax = _w; else result = { result, syntax: _w }; } } if (_undoSnap) editUndo.commit(_undoSnap); @@ -4867,9 +4900,12 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { if (result && result.__vision) { const v = result.__vision; if (apiConfigured()) oMsgs.push({ role: "user", content: [{ type: "text", text: "[image " + v.name + "]" }, v.openai] }); else oMsgs.push({ role: "user", content: "[image " + v.name + "]", images: [v.b64] }); transcript.push({ role: "system", text: "view_image → " + v.name + " attached for the vision model" }); delete result.__vision; } if (!(result && typeof result.error === "string" && result.error.startsWith("unknown tool"))) didTool = true; if (name === "run_command" && runningShells) runningShells--; + if (name === "run_command" && blocked && stat.cmds) stat.cmds--; // denied or blocked: it never ran, so the footer must not say "Ran 1 command" if (name === "spawn_agents") activeAgents = Math.max(0, activeAgents - (a.tasks || []).length); if (hooks && !blocked) try { runHooks(hooks, "PostToolUse", { TOOL_NAME: name, TOOL_ARGS: JSON.stringify(a), TOOL_RESULT: JSON.stringify(result).slice(0, 2000) }, cwd); } catch (_) {} - card.status = result.error ? "err" : "ok"; card.end = Date.now(); card.result = result; card.detail = oneline(resultText(JSON.stringify(result)), 260); + const _failed = !!(result && (result.error || (name === "run_command" && typeof result.code === "number" && result.code !== 0))); // a non-zero exit is a failed command, not a green card + card.status = _failed ? "err" : "ok"; if (ctl && ctl.stopped && isExecTool(name)) { card.status = "err"; card.interrupted = true; } card.end = Date.now(); card.result = result; card.detail = oneline(resultText(JSON.stringify(result)), 260); + if (!_failed && !blocked && ["write_file", "edit_file"].includes(name) && a.path && !(result && typeof result.note === "string" && /^no change/.test(result.note))) { stat.files.add(base(a.path)); const r = relOf(a.path); if (r) stat.paths.add(r); } // only a write that happened: a failed or blocked one is not "Wrote x", not undoable, not in the recap or the ledger if (a.__act && !blocked) { if (policy.audit) auditLog(cwd, { engine, tool: name, path: a.__act.path, cmd: a.__act.command, status: result && result.error ? "error" : "ok" }); if ((a.__act.type === "write" || a.__act.type === "edit") && result && !result.error) filesThisTurn++; } let _obsRes = result; // what the MODEL gets; the card keeps the full output for you if (!blocked && result && typeof result === "object") { const _c = Object.assign({}, result), _sv = collapseToolOutput(_c); if (_sv) { impact.repeatTok += _sv; _obsRes = _c; } } // identical lines → 1 + count, counting runs → first/last + exact step, node-internal frames → count (costsave.js) @@ -4897,9 +4933,9 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // going anyway, end the turn cleanly instead of burning the whole step budget. const _lg = loopGuard.note(loopGuard.signature(name, a), !(result && (result.error || result.code)), loopGuard.hashStr(JSON.stringify(result))); if (_lg) { - transcript.push({ role: "system", text: _lg.message }); oMsgs.push({ role: "tool", content: "[loop guard] " + _lg.message }); if (_lg.action === "break") { textItem().full += (textItem().full.trim() ? "\n" : "") + _lg.message; render(); break; } + const _nt = _lg.notice || _lg.message; if (!block.items.some((i) => i.type === "note" && i.text === _nt)) block.items.push({ type: "note", text: _nt }); // inside the running turn (a top-level block hid the spinner), shown once, worded for you } render(); } @@ -4907,7 +4943,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // from cumulative inTok+outTok — otherwise doCompact's trim never sticks (ctxUsed would // bounce back to the ever-growing cumulative total next turn → perpetual re-compaction). sess.ctxUsed = oMsgs.reduce((n, m) => n + Math.ceil((typeof m.content === "string" ? m.content.length : JSON.stringify(m.content || "").length) / 4), 0); - finish({ output: "done" }); + finish({ output: "", interrupted: !!(ctl && ctl.stopped) }); })(); } }; @@ -5311,7 +5347,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const sessPaths = () => { const sp = path.join(cwd, ".nexus", "session.json"); return { sp, lp: path.join(cwd, ".nexus", "session.log"), tmp: sp + ".tmp-" + process.pid }; }; const statStamp = (f) => { try { const st = fs.statSync(f); return st.size + ":" + st.mtimeMs; } catch (_) { return ""; } }; const saveSession = () => { const { sp, lp, tmp } = sessPaths(); try { - fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); + ensureNexusDir(cwd); const { readCache: _rc, fileSnap: _fs, ...persistSess } = sess; const metaStr = JSON.stringify({ engine, model: sess.model, sess: persistSess, claudeSid }); const list = transcript.filter((b) => !(b && b.ephemeral)); @@ -5336,6 +5372,9 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } secureNexus(cwd); } catch (_) { _disk.written = null; try { fs.unlinkSync(tmp); } catch (__) {} } finally { _fullSave = true; } }; + // Save outside a turn (exit, setting changes) so a resume shows what was last on screen. + // A blank session that never chatted leaves the last saved chat alone. + const saveChat = () => { try { if (_disk.written || transcript.some((b) => b && (b.role === "user" || b.role === "nexus"))) saveSession(); } catch (_) {} }; // Read the snapshot and replay its log. A torn last line (killed mid-append) ends the // replay; lines from another generation are skipped. const loadSessionFile = () => { @@ -5427,7 +5466,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { cache: engine === "claude" ? { lastAt: cacheWarm.lastAt || 0, ttl: cacheWarm.ttl || 0 } : null, toolCalls: Object.keys(toolUse).reduce((a, k) => a + toolUse[k], 0), title: sessionTitle || "", - turns: transcript.filter((m) => m.role === "user" && m.text && String(m.text).trim()).length, + turns: clearedTurns + transcript.filter((m) => m.role === "user" && m.text && String(m.text).trim()).length, engine, model: sess.model || engine, paid: !!PAID[engine], inTok: sess.inTok || 0, outTok: sess.outTok || 0, cost: sess.cost || 0, ctxUsed: sess.ctxUsed || 0, ctxWindow: sess.ctxWindow || CTXW[engine] || 0, @@ -5474,6 +5513,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { const cmd = (sp === -1 ? t : t.slice(0, sp)).toLowerCase(); const argstr = sp === -1 ? "" : t.slice(sp + 1).trim(); const arg = argstr.split(/\s+/)[0]; + if (/^\/(settings|options|config|theme|color|recap)$/.test(cmd)) setTimeout(() => { if (!busy) saveChat(); }, 0); // keep the saved session in step with setting changes // Any command other than a repeated /clear cancels a pending clear confirmation. const clearWasPending = clearPending; if (cmd !== "/clear" && cmd !== "/new") clearPending = false; if (aliases[cmd] && !builtinNames().has(cmd)) { let body = String(aliases[cmd]).replace(/\$ARGUMENTS/g, argstr).replace(/\$(\d+)/g, (_, n) => argstr.split(/\s+/)[+n - 1] || ""); if (!/\$ARGUMENTS|\$\d/.test(aliases[cmd]) && argstr) body += " " + argstr; if (body.trim()[0] === "/") { if (_aliasDepth >= 5) { transcript.push({ role: "system", text: "alias " + cmd + " loops back on itself (aliases calling aliases more than 5 deep) — fix it with /alias" }); return; } _aliasDepth++; try { handleSlash(body.trim()); } finally { _aliasDepth--; } } else submit(body.trim()); return; } @@ -5573,7 +5613,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } else if (cmd === "/mcp") { const ps = (argstr || "").trim().split(/\s+/).filter(Boolean); const sub = (ps[0] || "").toLowerCase(); - const writeMcp = (cfg) => keepTrust(cwd, () => { try { fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(path.join(cwd, ".nexus", "mcp.json"), JSON.stringify(cfg, null, 2)); return true; } catch (_) { return false; } }); + const writeMcp = (cfg) => keepTrust(cwd, () => { try { ensureNexusDir(cwd); fs.writeFileSync(path.join(cwd, ".nexus", "mcp.json"), JSON.stringify(cfg, null, 2)); return true; } catch (_) { return false; } }); if (sub === "catalog" || sub === "list") transcript.push({ role: "system", text: "MCP catalog — one-command connect with /mcp add " + green("(★ = bundled, on by default)") + ":\n" + Object.keys(MCP_CATALOG).map((k) => " " + (isBundled(k) ? green("★") : " ") + " " + cyan(k.padEnd(20)) + gray(MCP_CATALOG[k].desc) + (MCP_CATALOG[k].needsEnv ? yellow(" [needs " + MCP_CATALOG[k].needsEnv.join(", ") + "]") : "")).join("\n") + "\n any model then uses them as mcp____. Bundled ones auto-connect (DARKNODE_NO_DEFAULT_MCP=1 to disable)." }); else if (sub === "add") { const name = (ps[1] || "").toLowerCase(); const e = catalogGet(name); @@ -5585,7 +5625,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { else if (sub === "connect" || sub === "reconnect") { mcpServers = []; connectMcp().then(() => render()); transcript.push({ role: "system", text: "reconnecting MCP servers (bundled + .nexus/mcp.json)…" }); } else if (!mcpServers.length && !loadMcpConfig(cwd)) transcript.push({ role: "system", text: (process.env.DARKNODE_NO_DEFAULT_MCP ? "bundled MCP servers are disabled (DARKNODE_NO_DEFAULT_MCP).\n " : "bundled MCP servers still connecting… (first launch npx/uvx-installs them). Run /mcp again in a moment.\n ") + cyan("/mcp add github") + gray(" · or playwright · exa · notion · postgres · … · /mcp catalog to browse") + "\n The claude engine AND any local/API model get the tools." }); else if (!mcpServers.length) transcript.push({ role: "system", text: "MCP servers are still connecting… run /mcp again in a moment" }); - else transcript.push({ role: "system", text: "MCP servers " + green("(★ = bundled with Nexus)") + ":\n" + mcpServers.map((s) => s.error ? (" " + red("×") + " " + s.name + (s.bundled ? green(" ★") : "") + " — " + s.error) : (" " + green("●") + " " + s.name + (s.bundled ? green(" ★") : "") + " — " + (s.tools || []).length + " tool(s): " + (s.tools || []).map((t) => t.name).slice(0, 8).join(", "))).join("\n") + "\n any model calls these as mcp____ · /mcp add · /mcp catalog · /mcp remove " + (mcpServers.some((s) => s.error) ? " · /mcp reconnect retries the failed ones" : "") }); } + else transcript.push({ role: "system", text: "MCP servers " + green("(★ = bundled with Nexus)") + ":\n" + mcpServers.map((s) => s.error ? (" " + red("×") + " " + s.name + (s.bundled ? green(" ★") : "") + " — " + s.error) : (" " + green("●") + " " + s.name + (s.bundled ? green(" ★") : "") + " — " + (s.tools || []).length + " tool(s): " + (s.tools || []).map((t) => t.name).slice(0, 8).join(", ") + ((s.tools || []).length > 8 ? gray(" +" + ((s.tools || []).length - 8) + " more") : ""))).join("\n") + "\n any model calls these as mcp____ · /mcp add · /mcp catalog · /mcp remove " + (mcpServers.some((s) => s.error) ? " · /mcp reconnect retries the failed ones" : "") }); } else if (cmd === "/agents" || cmd === "/parallel") { const tasks = argstr.split(/\s*;;\s*/).map((s) => s.trim()).filter(Boolean); if (tasks.length < 2) transcript.push({ role: "system", text: "usage: /agents ;; ;; … — runs each task in parallel on the " + engine + " engine, each in its own isolated git worktree, then merges the changes back" }); else spawnAgents(tasks); } else if (cmd === "/hooks") { transcript.push({ role: "system", text: hooks ? ("hooks (.nexus/hooks.json) active for events: " + Object.keys(hooks).join(", ") + "\n PreToolUse/PostToolUse run for the local engine; UserPromptSubmit & Stop run for every engine") : "no hooks configured. Create .nexus/hooks.json:\n { \"PreToolUse\": [ { \"matcher\": \"run_command|write_file\", \"command\": \"echo $TOOL_NAME >> .nexus/audit.log\" } ] }\n events: UserPromptSubmit · PreToolUse · PostToolUse · Stop (non-zero exit on PreToolUse/UserPromptSubmit blocks the action)" }); } else if (cmd === "/status") transcript.push({ role: "system", text: "status:\n engine " + engine + "\n model " + (modelLabel(shownModel()) || "engine default") + "\n dir " + cwd + "\n mode " + MODES[mode].k + "\n context " + Math.round((sess.ctxUsed / (sess.ctxWindow || 1)) * 100) + "% of " + fmtK(sess.ctxWindow) + "\n tokens ↑" + fmtK(sess.inTok) + " ↓" + fmtK(sess.outTok) + (PAID[engine] ? (sess.cost ? " · $" + sess.cost.toFixed(4) : " · billed") : " · local · free") + "\n budget " + (costCap ? "$" + costCap.toFixed(2) + " cap" : "none") + "\n undo " + checkpoints.length + " checkpoint(s)" }); @@ -5628,7 +5668,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { blk.text += lines.join("\n"); render(); ollamaTags().then((ms) => { if (ms.length) blk.text += "\n " + ok(ms.length + " model(s): " + ms.slice(0, 6).join(", ") + (ms.length > 6 ? " …" : "")); - else blk.text += "\n " + (apiConfigured() ? ok("API: " + API_BASE()) : no("no models — `ollama pull gpt-oss:20b`")); + else blk.text += "\n " + (apiConfigured() ? ok("API: " + API_BASE()) : no("no models — `ollama pull qwen2.5-coder`")); blk.text += "\n\n " + gray("all green? you're ready to go. fix ○ items to unlock that feature."); render(); }).catch(() => { blk.text += "\n " + no("Ollama unreachable"); render(); }); @@ -5657,12 +5697,12 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { transcript.push({ role: "system", text: L.join("\n") }); } else if (cmd === "/resume") { try { const s = loadSessionFile(); restoreSession(s); scroll = 0; transcript.push({ role: "system", text: "resumed the saved session from " + new Date(s.ts).toLocaleString() + " (" + engine + ")" }); } catch (e) { transcript.push({ role: "system", text: e && e.code === "ENOENT" ? "no saved session to resume (sessions are saved to .nexus/session.json after each turn)" : "the saved session (.nexus/session.json) could not be read: " + String((e && e.message) || e).split("\n")[0] }); } } - else if (cmd === "/export") { try { const md = exportMarkdown(transcript, { date: new Date().toLocaleString(), engine, cwd }); const dir = path.join(cwd, ".nexus"); try { fs.mkdirSync(dir, { recursive: true }); } catch (_) {} const fp = argstr ? path.resolve(cwd, argstr.replace(/^~(?=\/|$)/, require("os").homedir())) : path.join(dir, "export-" + new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19) + ".md"); fs.writeFileSync(fp, md); /* argstr, not arg: a path with spaces is one path */ /* /export writes there (its folder must exist) */ transcript.push({ role: "system", text: "exported conversation → " + path.relative(cwd, fp) + " (" + fp + ")" }); } catch (e) { transcript.push({ role: "system", text: "export failed: " + e.message }); } } + else if (cmd === "/export") { try { const md = exportMarkdown(transcript, { date: new Date().toLocaleString(), engine, cwd }); const dir = path.join(cwd, ".nexus"); try { ensureNexusDir(cwd); } catch (_) {} const fp = argstr ? path.resolve(cwd, argstr.replace(/^~(?=\/|$)/, require("os").homedir())) : path.join(dir, "export-" + new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19) + ".md"); fs.writeFileSync(fp, md); /* argstr, not arg: a path with spaces is one path */ /* /export writes there (its folder must exist) */ transcript.push({ role: "system", text: "exported conversation → " + path.relative(cwd, fp) + " (" + fp + ")" }); } catch (e) { transcript.push({ role: "system", text: "export failed: " + e.message }); } } else if (cmd === "/copy") { let last = ""; for (let i = transcript.length - 1; i >= 0; i--) { const m = transcript[i]; if (m.role === "nexus") { last = (m.items || []).filter((it) => it.type === "text").map((it) => it.full).join("\n").trim(); if (last) break; } } if (last) { const via = copyToClipboard(last, out); transcript.push({ role: "system", text: "copied Nexus's last reply to the " + via }); } else transcript.push({ role: "system", text: "nothing to copy yet" }); } else if (cmd === "/rewind") { if (!checkpoints.length) transcript.push({ role: "system", text: "no checkpoints to rewind to" }); else { const n = parseInt(arg, 10); const idx = isNaN(n) ? checkpoints.length - 1 : n - 1; const ck = checkpoints[idx]; if (!ck) transcript.push({ role: "system", text: "no checkpoint #" + arg + " — /checkpoints to list them" }); else { const rwPaths = [...new Set(checkpoints.slice(idx).flatMap((c) => c.paths || []))]; /* every file a turn from here on changed, not just this turn's: otherwise later turns' files stay at their newest version */ const ok = nexusRestore(cwd, ck.tree, rwPaths); if (ok) checkpoints.length = idx; transcript.push({ role: "system", text: ok ? ("rewound to checkpoint #" + (idx + 1) + " (before \"" + ck.label + "\") — restored " + (rwPaths.length ? rwPaths.length + " file(s) Nexus changed since" : "nothing (no file Nexus tracked changed since)") + keptNote()) : "rewind failed (git error)" }); } } } else if (cmd === "/clear" || cmd === "/new") { const hasContent = oMsgs.length > 1 || transcript.some((m) => m.role === "user" || m.role === "nexus"); - const doClear = () => { clearPending = false; transcript.length = 0; transcript.push({ role: "art" }); cont = false; claudeSid = ""; oneShotRan = false; carryNote = ""; cacheWarm.lastAt = 0; oMsgs.length = 1; sess.ctxUsed = 0; scroll = 0; sess.readCache = {}; if (sess.fileSnap) sess.fileSnap.clear(); saveSession(); /* else quitting now restores the cleared chat on next launch */ render(); }; + const doClear = () => { clearPending = false; titleEpoch++; if (!titleManual) { sessionTitle = ""; titleAt = 0; idleTitle(); } /* a new topic gets a new name; a title still being made for the old chat is dropped */ clearedTurns += transcript.filter((m) => m.role === "user" && m.text && String(m.text).trim()).length; transcript.length = 0; transcript.push({ role: "art" }); cont = false; claudeSid = ""; oneShotRan = false; carryNote = ""; cacheWarm.lastAt = 0; oMsgs.length = 1; sess.ctxUsed = 0; scroll = 0; sess.readCache = {}; if (sess.fileSnap) sess.fileSnap.clear(); saveSession(); /* else quitting now restores the cleared chat on next launch */ render(); }; if (!hasContent) doClear(); // nothing to lose — just reset else askUser("Clear the whole conversation? This wipes the chat and the model's memory of it.", ["Keep it", "Clear everything"]).then((ans) => { if (!ans.cancelled && ans.index === 1) doClear(); @@ -5760,7 +5800,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { transcript.push({ role: "system", text: txt }); } else if (cmd === "/savings" || cmd === "/impact") { const parts = argstr.split(/\s+/).filter(Boolean); const si = parts.indexOf("since"); const since = si >= 0 ? parts[si + 1] : null; if (si >= 0 && !isIsoSince(since)) { transcript.push({ role: "system", text: "usage: " + cmd + " since — an ISO date like 2026-08-01 (or 2026-08)" }); render(); return; } const recs = loadUsage(cwd, since ? { since } : {}); if (!recs.length) { transcript.push({ role: "system", text: "no usage recorded yet — the savings analysis needs some Nexus turns in .nexus/usage.jsonl first" }); } else { const a = analyzeSavings(recs); transcript.push({ role: "system", text: (parts[0] === "json" ? JSON.stringify(a, null, 2) : renderSavings(a, { project: path.basename(cwd) }) + "\n\n headless: darknode savings") }); } } - else if (cmd === "/compliance") { try { const key = process.env.DARKNODE_SIGNING_KEY || ""; const b = buildBundle(cwd, { operator: ident.operator, team: ident.team, signingKey: key || undefined }); const stamp = new Date().toISOString().replace(/[:.]/g, "-"); const base = path.join(cwd, ".nexus", "compliance-" + stamp); fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(base + ".json", JSON.stringify(b, null, 2)); fs.writeFileSync(base + ".md", renderBundleMd(b)); transcript.push({ role: "system", text: "compliance bundle written (operator " + ident.operator + (ident.team ? " · team " + ident.team : "") + "):\n " + base + ".json\n " + base + ".md\n audit chain: " + (b.auditChain.present ? (b.auditChain.verified ? "verified (" + b.auditChain.records + " records)" : "BROKEN — " + b.auditChain.reason) : "none yet") + "\n integrity: sha256 " + b.integrity.hash.slice(0, 16) + "…" + (b.signature ? "\n signed: hmac-sha256 (DARKNODE_SIGNING_KEY)" : "\n tip: set DARKNODE_SIGNING_KEY to HMAC-sign the bundle") + "\n verify: darknode compliance verify " + base + ".json" }); } catch (e) { transcript.push({ role: "system", text: "compliance export failed: " + (e && e.message || e) }); } } + else if (cmd === "/compliance") { try { const key = process.env.DARKNODE_SIGNING_KEY || ""; const b = buildBundle(cwd, { operator: ident.operator, team: ident.team, signingKey: key || undefined }); const stamp = new Date().toISOString().replace(/[:.]/g, "-"); const base = path.join(cwd, ".nexus", "compliance-" + stamp); ensureNexusDir(cwd); fs.writeFileSync(base + ".json", JSON.stringify(b, null, 2)); fs.writeFileSync(base + ".md", renderBundleMd(b)); transcript.push({ role: "system", text: "compliance bundle written (operator " + ident.operator + (ident.team ? " · team " + ident.team : "") + "):\n " + base + ".json\n " + base + ".md\n audit chain: " + (b.auditChain.present ? (b.auditChain.verified ? "verified (" + b.auditChain.records + " records)" : "BROKEN — " + b.auditChain.reason) : "none yet") + "\n integrity: sha256 " + b.integrity.hash.slice(0, 16) + "…" + (b.signature ? "\n signed: hmac-sha256 (DARKNODE_SIGNING_KEY)" : "\n tip: set DARKNODE_SIGNING_KEY to HMAC-sign the bundle") + "\n verify: darknode compliance verify " + base + ".json" }); } catch (e) { transcript.push({ role: "system", text: "compliance export failed: " + (e && e.message || e) }); } } else if (cmd === "/undo") { if (!checkpoints.length) transcript.push({ role: "system", text: "nothing to undo (no checkpoints this session, or not a git repo)" }); else { const ck = checkpoints[checkpoints.length - 1]; const nowTree = nexusCheckpoint(cwd); const ok = nexusRestore(cwd, ck.tree, ck.paths); if (ok) checkpoints.pop(); if (ok && nowTree) redoStack.push({ tree: nowTree, before: ck.tree, label: ck.label, paths: ck.paths }); transcript.push({ role: "system", text: ok ? ("undid \"" + ck.label + "\" — restored " + ((ck.paths && ck.paths.length) ? ck.paths.length + " file(s) Nexus changed this turn (unrelated edits untouched)" : "nothing (that turn changed no file Nexus tracked)") + keptNote() + (nowTree ? " (/redo to reapply)" : " (no redo: the current state could not be snapshotted)")) : "undo failed (git error) — nothing changed; the checkpoint is kept, so /undo can be retried" }); } } else if (cmd === "/redo") { const r = redoStack[redoStack.length - 1]; if (!r) transcript.push({ role: "system", text: "nothing to redo" }); else { const ok = nexusRestore(cwd, r.tree, r.paths); if (ok) { redoStack.pop(); checkpoints.push({ tree: r.before || r.tree, label: r.label, ts: Date.now(), paths: r.paths }); } /* the checkpoint holds the state BEFORE the turn, so a later /undo reverts it again */ transcript.push({ role: "system", text: ok ? ("redid — reapplied the changes from \"" + r.label + "\"") : "redo failed (git error)" }); } } else if (cmd === "/race") { if (!argstr) transcript.push({ role: "system", text: "usage: /race — runs it on every available engine (claude/local/opencode) at once and shows all answers" }); else raceEngines(argstr); } @@ -5916,8 +5956,8 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { else { let nv; const raw = (parts[1] || "").toLowerCase(); if (key === "budgetCap") { const n = parseUsd(parts[1]); nv = (raw === "off" || n === 0 || isNaN(n)) ? 0 : n; costCap = nv; } - else { nv = raw === "on" ? true : raw === "off" ? false : key === "recap" ? !autoRecap : !settingsStore.get(key); } - if (key === "recap") autoRecap = nv; else if (key === "autoCompact") autoCompact = nv; else if (key === "minimizeCalls") minimizeCalls = nv; else if (key === "stepCap") stepCap = nv; else if (key === "trimOutput") trimOutput = nv; else if (key === "cache") cacheOn = nv; else if (key === "map") { repoMapOn = nv; if (nv) repoMapSent = false; } else if (key === "lean") lean = nv; + else { nv = raw === "on" ? true : raw === "off" ? false : key === "recap" ? !recapActive() : !settingsStore.get(key); } + if (key === "recap") { autoRecap = nv; recapChosen = true; } else if (key === "autoCompact") autoCompact = nv; else if (key === "minimizeCalls") minimizeCalls = nv; else if (key === "stepCap") stepCap = nv; else if (key === "trimOutput") trimOutput = nv; else if (key === "cache") cacheOn = nv; else if (key === "map") { repoMapOn = nv; if (nv) repoMapSent = false; } else if (key === "lean") lean = nv; saveSetting(key, nv); transcript.push({ role: "system", text: key + " = " + (key === "budgetCap" ? (nv ? "$" + Number(nv).toFixed(2) : "off (unlimited)") : (nv ? "ON" : "off")) + gray(" — saved to ~/.nexus/settings.json") }); } @@ -5927,18 +5967,24 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // on/off values read green/red at a glance, matching the slash-menu toggle colours const onOff = (x) => /^on\b/i.test(x) ? green(x) : /^off$/i.test(x) ? red(x) : x; let text = bold("Settings") + gray(" change any option with the command shown"); - for (const s of describeSettings(v)) { text += "\n\n" + bold(cyan(s.group)); for (const r of s.rows) { const gap = " ".repeat(Math.max(2, 20 - r.value.length)); text += "\n " + r.label.padEnd(26) + bold(onOff(r.value)) + gap + gray(r.cmd); } } + const fitW = cols() - 4; // a row wider than this was hard-wrapped to column 3, breaking the columns + for (const s of describeSettings(v)) { text += "\n\n" + bold(cyan(s.group)); for (const r of s.rows) { const gap = " ".repeat(Math.max(2, 20 - r.value.length)), head = " " + r.label.padEnd(26) + bold(onOff(r.value)); text += "\n" + head + (strWidth(head + gap + r.cmd) <= fitW ? gap : "\n ") + gray(r.cmd); } } // Governed limits — all OFF by default; each shows its live value and where it came from. const liveGov = { budgetCap: costCap, autoCompact, minimizeCalls, stepCap, trimOutput, cache: cacheOn, map: repoMapOn, lean }; text += "\n\n" + bold(cyan("Limits")) + gray(" off by default · saved to ~/.nexus/settings.json · an env var overrides one launch"); + const envLater = []; for (const row of settingsStore.describe()) { if (!(row.key in liveGov)) continue; const val = liveGov[row.key]; const shown = row.key === "budgetCap" ? (val ? "$" + Number(val).toFixed(2) : "off") : (val ? "on" : "off"); const srcTxt = Ssrc[row.key] === "env" ? " env" : Ssrc[row.key] === "saved" ? " saved" : " default", src = gray(srcTxt) + " ".repeat(Math.max(2, 14 - shown.length - srcTxt.length)); const envName = nexusSettings.ENV_NAME[row.key]; - text += "\n " + row.key.padEnd(16) + bold(onOff(shown)) + src + gray("/settings " + row.key + (row.key === "budgetCap" ? " |off" : " on|off") + (envName ? " " + envName : "")); + const rowHead = " " + row.key.padEnd(16) + bold(onOff(shown)) + src, rowCmd = "/settings " + row.key + (row.key === "budgetCap" ? " |off" : " on|off"); + const rowTxt = rowHead + (strWidth(rowHead + rowCmd) <= fitW ? "" : "\n ") + gray(rowCmd); + if (envName && strWidth(rowTxt + " " + envName) > fitW) envLater.push(envName); // listed below instead of wrapping onto its own row + text += "\n" + rowTxt + (envName && !envLater.includes(envName) ? gray(" " + envName) : ""); } + if (envLater.length) text += "\n" + ansiWrap("env vars: " + envLater.join(" · "), Math.max(20, fitW - 2)).map((l) => " " + gray(l.trim())).join("\n"); text += "\n " + gray("colour: NO_COLOR=1 disables, FORCE_COLOR=1 forces (NO_COLOR wins)."); transcript.push({ role: "system", text }); } @@ -5962,12 +6008,12 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { else pager = Pager.createPager(txt, q ? "find \"" + q + "\"" : "conversation", q ? { query: q, at: "last" } : { at: "last" }); } else if (cmd === "/pager" || cmd === "/less") { - // Newest first: every finished tool result, plus Nexus answers for "/pager answer". - const a = (argstr || "").trim().toLowerCase(), want = a === "answer" ? "text" : "tool", n = Math.max(1, parseInt(a, 10) || 1); + // Bare /pager (and /pager answer) opens the last reply; /pager the n-th newest tool result. + const a = (argstr || "").trim().toLowerCase(), hasReply = transcript.some((b) => (b.items || []).some((it) => it.type === "text" && it.full)), want = a === "answer" || (!a && hasReply) ? "text" : "tool", n = want === "text" ? 1 : Math.max(1, parseInt(a, 10) || 1); const found = []; for (let bi = transcript.length - 1; bi >= 0 && found.length < n; bi--) { const its = transcript[bi].items || []; for (let ii = its.length - 1; ii >= 0 && found.length < n; ii--) { const it = its[ii]; if (want === "text" ? it.type === "text" && it.full : it.type === "tool" && (it.result !== undefined || it.live)) found.push(it); } } const it = found[n - 1]; - if (!it) transcript.push({ role: "system", text: want === "text" ? "no answer to page yet" : found.length ? "only " + found.length + " tool output" + (found.length > 1 ? "s" : "") + " so far — /pager 1 is the newest" : "no tool output yet — /pager opens the newest tool result (/pager 2 the one before, /pager answer the last answer)" }); + if (!it) transcript.push({ role: "system", text: want === "text" ? "no answer to page yet" : found.length ? "only " + found.length + " tool output" + (found.length > 1 ? "s" : "") + " so far — /pager 1 is the newest" : "no reply or tool output yet — /pager opens the last reply, /pager 1 the newest tool result (/pager 2 the one before)" }); else { const live = want === "tool" && it.result === undefined && it.live; const txt = want === "text" ? it.full : live ? Tail.tailText(it.live) : asText(it.result); pager = Pager.createPager(txt || "(empty)", want === "text" ? "answer" : (it.label || it.name || "output") + (live ? " · running, snapshot" : "")); } } else if (cmd === "/compat") { @@ -6013,7 +6059,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } else if (cmd === "/recap") { const a2 = (argstr || "").trim().toLowerCase(); - if (a2 === "off" || a2 === "on") { autoRecap = a2 === "on"; saveSetting("recap", autoRecap); transcript.push({ role: "system", text: "task recaps " + (autoRecap ? green("ON") + " — a short ※ recap appears after each finished task (small off-ledger model call)" : red("OFF")) + gray(" · saved") }); } + if (a2 === "off" || a2 === "on") { autoRecap = a2 === "on"; recapChosen = true; saveSetting("recap", autoRecap); transcript.push({ role: "system", text: "task recaps " + (autoRecap ? green("ON") + " — a short ※ recap appears after each finished task (small off-ledger model call)" : red("OFF")) + gray(" · saved") }); } else { const users = transcript.filter((m) => m.role === "user" && m.text && m.text.trim()); if (!users.length) transcript.push({ role: "system", text: "nothing to recap yet" }); @@ -6136,7 +6182,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { ollamaTags().then((ms) => { const lines = ENGINE_ORDER.map((e) => { const m = ENGINES[e], on = engineAvail(e), cur = e === engine ? " " + cyan("(current)") : ""; - const cat = e === "ollama" ? (ms.length ? ms.join(", ") : gray("none — `ollama pull gpt-oss:20b`")) + const cat = e === "ollama" ? (ms.length ? ms.join(", ") : gray("none — `ollama pull qwen2.5-coder`")) : (m.models && m.models.length) ? m.models.join(" · ") : gray("configured inside the tool itself"); return " " + (on ? green("●") : gray("○")) + " " + e + gray(" · " + m.label) + cur + "\n " + cat; }).join("\n"); @@ -6158,7 +6204,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { let out = ""; try { out = cowork.on ? await weakChat(dreamPrompt) : await engineAnswer(offline ? "ollama" : engine, dreamPrompt); } catch (_) {} const bullets = (out || "").split("\n").map((l) => l.trim()).filter((l) => /^[-*]\s/.test(l)).map((l) => l.replace(/^[*]\s/, "- ")); card.status = "ok"; card.end = Date.now(); - if (bullets.length) { try { const md = path.join(cwd, ".nexus", "NEXUS.md"); fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); let c = ""; try { c = fs.readFileSync(md, "utf8"); } catch (_) {} if (!/\n##\s*Notes/.test(c)) c += (c && !c.endsWith("\n") ? "\n" : "") + "\n## Notes\n"; const lc = c.toLowerCase(); const added = []; for (const b of bullets) { const key = b.toLowerCase().replace(/[^a-z0-9 ]/g, "").slice(0, 28); if (key.length > 8 && !lc.includes(key)) { c += b + "\n"; added.push(b); } } fs.writeFileSync(md, c); ensureText(block).full = added.length ? ("consolidated " + added.length + " new fact(s) into .nexus/NEXUS.md:\n\n" + added.join("\n")) : "reviewed the session — everything's already in NEXUS.md"; } catch (e) { ensureText(block).full = "could not write memory: " + e.message; } } else ensureText(block).full = "no durable facts found to consolidate."; + if (bullets.length) { try { const md = path.join(cwd, ".nexus", "NEXUS.md"); ensureNexusDir(cwd); let c = ""; try { c = fs.readFileSync(md, "utf8"); } catch (_) {} if (!/\n##\s*Notes/.test(c)) c += (c && !c.endsWith("\n") ? "\n" : "") + "\n## Notes\n"; const lc = c.toLowerCase(); const added = []; for (const b of bullets) { const key = b.toLowerCase().replace(/[^a-z0-9 ]/g, "").slice(0, 28); if (key.length > 8 && !lc.includes(key)) { c += b + "\n"; added.push(b); } } fs.writeFileSync(md, c); ensureText(block).full = added.length ? ("consolidated " + added.length + " new fact(s) into .nexus/NEXUS.md:\n\n" + added.join("\n")) : "reviewed the session — everything's already in NEXUS.md"; } catch (e) { ensureText(block).full = "could not write memory: " + e.message; } } else ensureText(block).full = "no durable facts found to consolidate."; busy = false; ctl = null; try { saveSession(); } catch (_) {} render(); })(); } else if (cmd === "/plan") { const s = arg; @@ -6233,12 +6279,12 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // attest: capability manifest from the hash-chained audit log (what this run touched + chain verdict). else if (cmd === "/attest") { deviceTool("attest", {}, cwd).then((r) => { transcript.push({ role: "system", text: formatToolLines("attest", r).join("\n") }); render(); }).catch((e) => { transcript.push({ role: "system", text: "attest failed: " + (e && e.message || e) }); render(); }); } // bisect: binary-search the per-turn checkpoint timeline to the exact turn that broke the test. - else if (cmd === "/bisect") { transcript.push({ role: "system", text: "bisecting the checkpoint timeline" + (argstr ? " with '" + argstr + "'" : " (auto-detecting the test command)") + "... (this re-runs the test at each probe; usage: /bisect [test command])" }); render(); deviceTool("bisect", { test: argstr || undefined }, cwd).then((r) => { transcript.push({ role: "system", text: formatToolLines("bisect", r).join("\n") }); render(); }).catch((e) => { transcript.push({ role: "system", text: "bisect failed: " + (e && e.message || e) }); render(); }); } + else if (cmd === "/bisect") { transcript.push({ role: "system", text: "bisecting the checkpoint timeline" + (argstr ? " with '" + argstr + "'" : " (auto-detecting the test command)") + "... (this re-runs the test at each probe; usage: /bisect [test command])" }); render(); deviceTool("bisect", { test: argstr || undefined }, cwd).then((r) => { if (r && typeof r.error === "string" && /^no test command/.test(r.error)) r = Object.assign({}, r, { error: "no test command to bisect with — run /bisect (e.g. /bisect npm test), or add a test script the project can detect" }); /* the tool's own message names the agent-tool syntax */ transcript.push({ role: "system", text: formatToolLines("bisect", r).join("\n") }); render(); }).catch((e) => { transcript.push({ role: "system", text: "bisect failed: " + (e && e.message || e) }); render(); }); } // whatbroke: instant static "git blame for the agent's turns" (no re-run); explain-break tool. else if (cmd === "/whatbroke") { transcript.push({ role: "system", text: "correlating the failing test to the turns that changed the implicated files" + (argstr ? " (running '" + argstr + "' to capture the failure)" : "") + "... (usage: /whatbroke [test command])" }); render(); deviceTool("explain_break", { test: argstr || undefined }, cwd).then((r) => { transcript.push({ role: "system", text: formatToolLines("whatbroke", r).join("\n") }); render(); }).catch((e) => { transcript.push({ role: "system", text: "whatbroke failed: " + (e && e.message || e) }); render(); }); } // shrink: delta-debug (ddmin) the agent's edits to the minimal subset of files that still breaks it. else if (cmd === "/shrink") { transcript.push({ role: "system", text: "delta-debugging the changed files to the minimal breaking set" + (argstr ? " with '" + argstr + "'" : " (auto-detecting the test command)") + "... (usage: /shrink [test command])" }); render(); deviceTool("shrink", { test: argstr || undefined }, cwd).then((r) => { transcript.push({ role: "system", text: formatToolLines("shrink", r).join("\n") }); render(); }).catch((e) => { transcript.push({ role: "system", text: "shrink failed: " + (e && e.message || e) }); render(); }); } - else if (cmd === "/init") { try { const dir = path.join(cwd, ".nexus"); fs.mkdirSync(dir, { recursive: true }); const md = path.join(dir, "NEXUS.md"), cfg = path.join(dir, "config.json"); const made = []; if (!fs.existsSync(md)) { fs.writeFileSync(md, "# Nexus project instructions\n\nNexus loads this file every session.\n\n## Project\n- (describe your project)\n\n## Conventions\n- (style, patterns to follow)\n\n## Build / run / test\n- (commands)\n"); made.push("NEXUS.md"); } if (!fs.existsSync(cfg)) { fs.writeFileSync(cfg, JSON.stringify({ engine, model: "" }, null, 2) + "\n"); made.push("config.json"); } secureNexus(cwd); if (gitignoreNexus(cwd)) made.push(".gitignore"); transcript.push({ role: "system", text: made.length ? "initialized .nexus/ (" + made.join(", ") + ") — edit NEXUS.md to give Nexus project context" : ".nexus/ already exists" }); } catch (e) { transcript.push({ role: "system", text: "init failed: " + e.message }); } } + else if (cmd === "/init") { try { const dir = ensureNexusDir(cwd); const md = path.join(dir, "NEXUS.md"), cfg = path.join(dir, "config.json"); const made = []; if (!fs.existsSync(md)) { fs.writeFileSync(md, "# Nexus project instructions\n\nNexus loads this file every session.\n\n## Project\n- (describe your project)\n\n## Conventions\n- (style, patterns to follow)\n\n## Build / run / test\n- (commands)\n"); made.push("NEXUS.md"); } if (!fs.existsSync(cfg)) { fs.writeFileSync(cfg, JSON.stringify({ engine, model: "" }, null, 2) + "\n"); made.push("config.json"); } secureNexus(cwd); if (gitignoreNexus(cwd)) made.push(".gitignore"); transcript.push({ role: "system", text: made.length ? "initialized .nexus/ (" + made.join(", ") + ") — edit NEXUS.md to give Nexus project context" : ".nexus/ already exists" }); } catch (e) { transcript.push({ role: "system", text: "init failed: " + e.message }); } } else if (cmd === "/login") { const a0 = (arg || "").trim(); if (!a0) { const a = nexusAuth(); transcript.push({ role: "system", text: (a ? "signed in as " + (a.name || a.email || a.uid) + "\n" : "") + "paste your code from the website (Settings → Nexus CLI): type /login \n(advanced: /login google or /login github)" }); } @@ -6273,6 +6319,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // a number picks from the list /model shows; with no list yet, build the same one first const pickNth = (names) => { modelPick = names.slice(); if (names[+a - 1]) setModel(names[+a - 1]); else transcript.push({ role: "system", text: names.length ? "no model #" + a + " — /model lists " + names.length : "no numbered list for " + engine + " — use /model " }); render(); }; if (/^\d+$/.test(a)) { if (modelPick[+a - 1]) setModel(modelPick[+a - 1]); else if (engine === "ollama") ollamaTags().then((ms) => pickNth(ms || [])).catch(() => pickNth([])); else pickNth(eng.models || []); } + else if (a && engine === "ollama" && !apiConfigured()) ollamaTags().then((ms) => { ms = ms || []; if (!ms.length || ms.includes(a) || ms.includes(a + ":latest")) { setModel(a); render(); } else { transcript.push({ role: "system", text: "no local model '" + a + "' — nothing changed. Installed: " + ms.slice(0, 8).join(", ") + (ms.length > 8 ? " +" + (ms.length - 8) + " more" : "") + gray(" · get it with: ollama pull " + a) }); render(); } }).catch(() => { setModel(a); render(); }); // checked now, not on the next prompt; Ollama unreachable keeps the old behaviour else if (a) setModel(a); else if (engine === "ollama") { transcript.push({ role: "system", text: "listing local models…" }); render(); ollamaTags().then((ms) => show(ms, "(installed)")).catch(() => show([], "(Ollama unreachable)")); } else show(eng.models || [], (eng.models && eng.models.length) ? "" : "(configured inside the tool)"); @@ -6339,7 +6386,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { else if (cmd === "/cron") { const cronFile = path.join(cwd, ".nexus", "cron.json"); const loadCron = () => { try { return JSON.parse(stripBom(fs.readFileSync(cronFile, "utf8"))); } catch (_) { return []; } }; - const saveCron = (jobs) => { try { fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(cronFile, JSON.stringify(jobs, null, 2)); } catch (_) {} }; + const saveCron = (jobs) => { try { ensureNexusDir(cwd); fs.writeFileSync(cronFile, JSON.stringify(jobs, null, 2)); } catch (_) {} }; const sub = (arg || "").toLowerCase(); const cronMatch = (expr) => { try { const parts = expr.split(/\s+/); if (parts.length !== 5) return false; return parts.every((p) => /^[\d*,\/-]+$/.test(p)); } catch (_) { return false; } }; if (sub === "add" || sub === "new") { @@ -6527,7 +6574,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { if (API_BASE()) opts.push({ name: (sess.model && sess.model !== engine && !/^claude/i.test(sess.model) ? sess.model : "api-model"), note: API_BASE() + " · likely billed", paid: true, apply: () => { engine = "ollama"; if (!sess.model || /^claude/i.test(sess.model) || sess.model === engine) sess.model = ""; reset(); } }); const selArg = /^claude\s+\S/i.test(arg) ? "claude" : arg; if (!selArg) { - if (!opts.length) transcript.push({ role: "system", text: "No AIs connected yet — connect one:\n local: " + cyan("ollama pull gpt-oss:20b") + gray(" (then /connect)") + "\n Claude: " + cyan("/connect claude ") + gray(" (or export ANTHROPIC_API_KEY)") + "\n any: " + cyan("/api [model]") }); + if (!opts.length) transcript.push({ role: "system", text: "No AIs connected yet — connect one:\n local: " + cyan("ollama pull qwen2.5-coder") + gray(" (then /connect)") + "\n Claude: " + cyan("/connect claude ") + gray(" (or export ANTHROPIC_API_KEY)") + "\n any: " + cyan("/api [model]") }); else transcript.push({ role: "system", text: bold("Connect an AI") + gray(" /connect ") + "\n" + opts.map((o, i) => " " + cyan((i + 1) + ")") + " " + bold(o.name) + gray(" " + o.note)).join("\n") + "\n " + gray("current: ") + green(sess.model || engine) }); } else { let sel = /^\d+$/.test(selArg) ? opts[+selArg - 1] : opts.find((o) => o.name.toLowerCase().includes(selArg.toLowerCase())); @@ -6580,7 +6627,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // name every few turns so it tracks where the session actually goes. It never // shows the folder name. The call stays off the cost ledger (never touches // `sess`). A /title pins a manual name (titleManual) and stops refresh. - let sessionTitle = "", titleAt = 0, titleManual = false, autoTitle = S.autoTitle !== false; + let sessionTitle = "", titleAt = 0, titleEpoch = 0, titleManual = false, autoTitle = S.autoTitle !== false; const titleClean = (s) => String(s || "").replace(/["'`*_#>]+/g, " ").replace(/^\s*(tab\s*name|title|name|session)\s*[:\-]\s*/i, "").replace(/\s+/g, " ").replace(/^[\s.:\-–—]+|[\s.:\-–—]+$/g, "").trim(); // A compact brief of what this session is about: the opening request plus the // current focus — enough for the model to name the real subject, not guess. @@ -6622,8 +6669,8 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { if (!users.length) return; const n = users.length; if (!force && sessionTitle && (n - titleAt) < 4) return; // refresh ~every 4 user turns as the topic evolves - titleAt = n; - genAiTitle(sessionDigest(users)).then((t) => { if (t) { sessionTitle = t; if (!busy) idleTitle(); } }).catch(() => {}); // the AI (re)names the tab in the background + titleAt = n; const ep = titleEpoch; + genAiTitle(sessionDigest(users)).then((t) => { if (t && ep === titleEpoch) { sessionTitle = t; if (!busy) idleTitle(); } }).catch(() => {}); // the AI (re)names the tab in the background }; const idleTitle = () => setTitle(sessionTitle || "Nexus"); // Claude Code-style recap: once a real task finishes (edits, commands, or a @@ -6631,6 +6678,8 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // what got done and what's next. Engines without a quiet one-shot path get // an extractive recap from the reply itself. /recap [on|off] toggles. let autoRecap = S.recap !== false; + let recapChosen = typeof S.recap === "boolean"; // never set: recaps stay off on a local model (a small one invents what got done); /recap on enables them there too + const recapActive = () => autoRecap && (recapChosen || engine !== "ollama" || apiConfigured()); const recapClean = (s) => String(s || "").replace(/\x1b\[[0-9;]*m/g, "").replace(/[*`#>]+/g, "").replace(/^\s*(recap|summary)\s*[:\-]\s*/i, "").replace(/\s+/g, " ").trim().split(/(?<=[.!?])\s+/).filter((x) => !/\?["')]*$/.test(x)).join(" "); // a recap only reports - drop any question const extractRecap = (reply) => { const paras = String(reply || "").replace(/\x1b\[[0-9;]*m/g, "").split(/\n\s*\n/).map((x) => x.trim()).filter((x) => x && !/^[|`\-*#]/.test(x)); const p = paras[0] || ""; const m = p.match(/^.{20,240}?[.!?](\s|$)/); return recapClean(m ? m[0] : p.slice(0, 200)); }; const genRecap = async (brief) => { @@ -6650,7 +6699,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } catch (_) { return ""; } }; const maybeRecap = (blk, ask, info) => { - if (!autoRecap || !blk) return; + if (!recapActive() || !blk) return; const reply = (blk.items || []).filter((i) => i.type === "text").map((i) => i.full).join("\n").trim(); const brief = "Request: " + String(ask || "").replace(/\s+/g, " ").slice(0, 600) + (info.files.length ? "\nFiles changed: " + info.files.slice(0, 12).join(", ") : "") + (info.cmds ? "\nCommands run: " + info.cmds : "") + @@ -6802,6 +6851,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { _escHold = true; _escTimer = setTimeout(() => { _escHold = false; _escTimer = null; try { _escFlushing = true; process.stdin.emit("data", "\x1b"); } finally { _escFlushing = false; } }, 40); return; } + if (s.length > 1 && /^\x1b+$/.test(s)) { try { _escFlushing = true; for (let k = 0; k < s.length; k++) process.stdin.emit("data", "\x1b"); } finally { _escFlushing = false; } return; } // a fast Esc Esc lands in one read: deliver it as two Escs // Bracketed paste split across reads: a big paste arrives as "\x1b[200~part1", // "part2", … "partN\x1b[201~" (the end marker itself can be split). Without // buffering, each piece was handled on its own — several paste markers, and @@ -6972,7 +7022,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { if (s === "\x16" && !isMouse) { const cb = readClipboard(); if (cb.image) { - try { const dir = path.join(cwd, ".nexus", "pastes"); fs.mkdirSync(dir, { recursive: true }); const fp = path.join(dir, "paste-" + Date.now() + ".png"); fs.writeFileSync(fp, cb.image); composer.insert("[Image#" + imgKey(fp) + "]"); flush(); } + try { const dir = ensureNexusDir(cwd, "pastes"); const fp = path.join(dir, "paste-" + Date.now() + ".png"); fs.writeFileSync(fp, cb.image); composer.insert("[Image#" + imgKey(fp) + "]"); flush(); } catch (e) { pasteNotice = "could not save the pasted image: " + e.message; } } else if (cb.text) { const pr = composer.paste(cb.text.replace(/[ \t\r\n]+$/, "")); flush(); const rep = pr && pr.report; pasteNotice = rep && rep.flagged ? rep.reasons.join(" · ") : ""; } else transcript.push({ role: "system", dismissable: true, text: cb.error }); // too long for the hint bar, which clips it @@ -6990,13 +7040,12 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { // ---- Esc dismisses an open popup before the composer's own Esc handling ---- if (s === "\x1b" && navOpen) { navOpen = false; render(); return; } if (s === "\x1b" && argMatches().length) { const sp = input.indexOf(" "); if (sp > 0) { input = input.slice(0, sp) + " "; curPos = input.length; } menuSel = 0; render(); return; } - // Esc on an idle empty buffer closes the last pop-up info panel — any - // trailing system panel (/mcp, /status, /doctor, /cost, /pricing, /help, - // /keys, /settings…), like closing an overlay in Claude Code. (Streaming - // output and real conversation turns are not system panels, so they stay.) + // Esc on an idle empty buffer closes the last pop-up info panel — one flagged + // dismissable (/help, /keys, /settings…), like closing an overlay in Claude Code. + // Other command output stays in the transcript, /export and the saved session. if (s === "\x1b" && !busy && !input.length && !slashMatches().length) { const now = Date.now(); const _last = transcript[transcript.length - 1]; - if (_last && (_last.dismissable || _last.role === "system")) { transcript.pop(); clearPending = false; scroll = 0; _lastEscTs = now; render(); return; } + if (_last && _last.dismissable) { transcript.pop(); clearPending = false; scroll = 0; _lastEscTs = 0; render(); return; } // a dismiss is not the first half of a rewind // double-Esc = rewind (Claude Code): undo the last turn's file changes // (reversible with /redo) and put that prompt back in the box to edit. if (now - _lastEscTs < 650 && checkpoints.length) { @@ -7005,6 +7054,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { let ui = -1; for (let i = transcript.length - 1; i >= 0; i--) { if (transcript[i].role === "user" && transcript[i].text) { ui = i; break; } } if (ui >= 0) { input = transcript[ui].text; curPos = input.length; composer.text = input; composer.cursor = curPos; composer.histIndex = history.length; } transcript.push({ role: "system", dismissable: true, text: ok ? gray("rewound — restored files from before \"" + (ck.label || "last turn") + "\"" + keptNote() + "; prompt is back in the box to edit · /redo re-applies") : gray("rewound the prompt; the files could not be restored (git error), the checkpoint is kept for /undo") }); + try { saveSession(); } catch (_) {} // a resume must show the rewound state, not the turn that was undone scroll = 0; render(); return; } _lastEscTs = now; render(); return; @@ -7103,12 +7153,12 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { case "history-down": { composer.text = input; composer.cursor = curPos; composer.historyNext(); flush(); hIdx = composer.histIndex; menuSel = 0; render(); return; } // Down walks input history back toward the live draft (Claude Code). Transcript scroll is the wheel / PageDown. case "interrupt": if (ctl && ctl.kill) { ctl.kill(); transcript.push({ role: "system", text: "interrupting current turn…" }); } render(); return; case "exit": cleanup(); resolve(); return; // ctrl+d empty / 2nd ctrl+c - case "exit-confirm": transcript.push({ role: "system", text: "Press Ctrl+C again to exit" }); render(); return; + case "exit-confirm": { const _n = { text: "Press Ctrl+C again to exit", ok: false }; linkNotice = _n; setTimeout(() => { if (linkNotice === _n) { linkNotice = null; render(); } }, 2000); render(); return; } // a hint, not a transcript line (it ended up in /export and the dashboard) case "confirm-submit": pasteNotice = "shell-looking paste — press Enter again to send"; render(); return; case "submit": { const _am = argMatches(), _part = input.indexOf(" ") > 0 ? input.slice(input.indexOf(" ") + 1).trim() : ""; // the argument menu as the user saw it let t = (eff.text || "").trim(); input = ""; curPos = 0; composer.text = ""; composer.cursor = 0; const pick = menuSel; menuSel = 0; - if (_am.length && !/\s/.test(_part) && (pick > 0 || (_part && !_am.some((it) => it[0].toLowerCase() === _part.toLowerCase())))) t = t.slice(0, t.indexOf(" ")) + " " + (_am[pick] || _am[0])[0]; // Enter takes the highlighted argument (typed "/engine gem" runs gemini), unless what you typed is already exact + if (_am.length && !/\s/.test(_part) && (pick > 0 || (_part && !/^\d+$/.test(_part) && !_am.some((it) => it[0].toLowerCase() === _part.toLowerCase())))) t = t.slice(0, t.indexOf(" ")) + " " + (_am[pick] || _am[0])[0]; // Enter takes the highlighted argument (typed "/engine gem" runs gemini), unless what you typed is already exact or a number curTurnFromQueue = false; // a directly-typed action is never a drained queued prompt (keeps Ctrl+X able to interrupt it) if (/^\/(exit|quit|q)$/i.test(t)) { cleanup(); resolve(); return; } if (t.startsWith("/")) { let cmd = t; if (!/\s/.test(t)) { const mm = fuzzyCmds(t); if (mm.length) cmd = (mm[pick] || mm[0])[0]; } handleSlash(cmd); render(); return; } @@ -7169,7 +7219,7 @@ function nexusInit() { const fs = require("fs"), path = require("path"); const cwd = process.cwd(), NEXUS = path.join(cwd, ".nexus"); gitignoreNexus(cwd); - fs.mkdirSync(NEXUS, { recursive: true }); + ensureNexusDir(cwd); const mdPath = path.join(NEXUS, "NEXUS.md"), cfgPath = path.join(NEXUS, "config.json"); const detected = []; try { if (fs.existsSync("package.json")) { const p = JSON.parse(stripBom(fs.readFileSync("package.json", "utf8"))); detected.push("Node project" + (p.name ? " (" + p.name + ")" : "")); if (p.scripts) detected.push("npm scripts: " + Object.keys(p.scripts).slice(0, 8).join(", ")); } } catch (_) {} @@ -7192,7 +7242,7 @@ function nexusInit() { } function usage() { banner(); - console.log(` ${bold("USAGE")} + console.log(wrapHelp(` ${bold("USAGE")} darknode [command] [args] run a command, or just ${cyan("darknode")} for the interactive menu ${bold("QUICK START")} ${gray("— new here? try one of these")} @@ -7201,7 +7251,7 @@ function usage() { ${cyan("darknode")} ${gray("browse every tool from an interactive menu")} ${cyan("darknode docs quickstart")} ${gray("the full getting-started guide")} ${gray(" No AI engine yet? Free & private, runs on your machine:")} - ${gray("$")} curl -fsSL https://ollama.com/install.sh | sh ${gray("&&")} ollama pull gpt-oss:20b + ${gray("$")} curl -fsSL https://ollama.com/install.sh | sh ${gray("&&")} ollama pull qwen2.5-coder ${bold("COMMANDS")} ${renderCommands(COMMAND_GROUPS, { color: cyan, cheats: Object.keys(CHEATS).join(", ") })} @@ -7221,7 +7271,7 @@ ${renderCommands(COMMAND_GROUPS, { color: cyan, cheats: Object.keys(CHEATS).join ${gray("$")} darknode revshell bash 10.10.14.7 4444 ${gray("# reverse-shell one-liner")} ${gray("Use only on systems you are authorized to test.")} -`); +`, process.stdout.columns || 80)); // wrapped to the terminal: a long row hard-wrapped mid-word into column 0 } // ---------- entry ---------- diff --git a/docs/NEXUS-COMMANDS.md b/docs/NEXUS-COMMANDS.md index 6b31bff..467a057 100644 --- a/docs/NEXUS-COMMANDS.md +++ b/docs/NEXUS-COMMANDS.md @@ -197,7 +197,7 @@ File tools are confined to the project folder; secrets are blocked from being wr - `/next` — queue a prompt after the current turn (Ctrl+X / Ctrl+S runs it now; clear / rm to manage) - `/btw` — side question — answered from context, never saved to history - `/find` — search the whole conversation, tool output included: /find (n/N step through matches, q closes) -- `/pager` — read the newest tool output in full: /pager [n] | answer (j/k, space, g/G, / search, q) +- `/pager` — read the last reply in full, or a tool output: /pager [n] (j/k, space, g/G, / search, q) - `/compat` — which features this model supports (nothing is hidden) (opens in the side panel) - `/mode` — capability preset: /mode power|cost|speed|reset (model-compatible only) - `/god-prompt` — arm max-quality mode for your NEXT message only (gold border, auto-reverts) @@ -308,7 +308,7 @@ File tools are confined to the project folder; secrets are blocked from being wr ### Scroll, select & copy - `Mouse wheel` — scroll the transcript -- `Up / Down (empty input)` — scroll line by line +- `Up / Down (empty input)` — recall earlier prompts (the wheel scrolls) - `PgUp / PgDn` — scroll a page - `Home` — jump to the oldest - `End` — jump to the latest @@ -341,7 +341,7 @@ the cloud. Launch it with `darknode nexus --tui`. Type / inside for the command Free local setup: curl -fsSL https://ollama.com/install.sh | sh - ollama pull gpt-oss:20b # fast local agent (~14GB); pull gpt-oss:120b for max power (~63GB) + ollama pull qwen2.5-coder # fast local agent (~14GB); pull gpt-oss:120b for max power (~63GB) ``` ### Engines & models (`/docs engines`) diff --git a/lib/cli/command-help.js b/lib/cli/command-help.js index 3f66f3f..133d8c4 100644 --- a/lib/cli/command-help.js +++ b/lib/cli/command-help.js @@ -23,6 +23,7 @@ const HELP = { "/connect": { u: "", ex: ["/connect"], see: ["/engine", "/model", "/api"] }, "/alias": { u: " = | rm ", a: [["name = …", "what it expands to — plain text or another /command"], ["rm ", "delete an alias (bare /alias lists them)"]], ex: ["/alias ship = /commit", "/alias rv = review this diff for security", "/alias rm ship"] }, "/retry": { u: "", ex: ["/retry"] }, + "/settings": { u: "[ on|off | budget |off]", a: [["", "a row under Limits in /settings, or recap"], ["budget |off", "session spend cap"]], ex: ["/settings", "/settings lean on", "/settings budget 5"], see: ["/budget"] }, "/budget": { u: "|off", a: [["", "stop warning past this session spend"], ["off", "remove the cap"]], ex: ["/budget 5", "/budget off"], see: ["/cost", "/savings"] }, "/report": { u: "[json|save|since ]", a: [["json", "machine-readable output"], ["save", "write it to a file"], ["since ", "only usage on/after YYYY-MM-DD"]], ex: ["/report", "/report json", "/report since 2026-10-01"], see: ["/cost", "/compliance"] }, "/compliance": { u: "[verify ]", a: [["verify ", "check a bundle's signature + hash chain"]], ex: ["/compliance", "/compliance verify audit.json"], see: ["/audit", "/report"] }, @@ -93,7 +94,7 @@ const HELP = { "/autoupdate": { u: "on|check|off", ex: ["/autoupdate on", "/autoupdate off"] }, "/next": { u: " | clear | rm ", ex: ["/next run the tests", "/next clear"], see: ["/loop"] }, "/find": { u: "", a: [["n/N", "step through matches"], ["q", "close"]], ex: ["/find TODO", "/find race condition"], see: ["/pager", "/index"] }, - "/pager": { u: "[n]", a: [["n", "which recent tool output; omit for the newest"]], ex: ["/pager", "/pager 2"], see: ["/find", "/expand"] }, + "/pager": { u: "[n | answer]", a: [["n", "the n-th newest tool output (1 = newest)"], ["answer", "the last reply (also what bare /pager opens)"]], ex: ["/pager", "/pager 1", "/pager 2"], see: ["/find", "/expand"] }, "/mode": { u: "power|cost|speed|reset", ex: ["/mode power", "/mode reset"], see: ["/effort", "/model"] }, "/autocompact": { u: "auto|off|", ex: ["/autocompact auto", "/autocompact off", "/autocompact 80%"], see: ["/compact", "/recap"] }, "/recap": { u: "[on|off]", a: [["(no arg)", "recap now"], ["on|off", "toggle the after-task recap"]], ex: ["/recap", "/recap off"] }, diff --git a/lib/cli/md-render.js b/lib/cli/md-render.js index c2f4473..1636355 100644 --- a/lib/cli/md-render.js +++ b/lib/cli/md-render.js @@ -6,7 +6,8 @@ // // C supplies the painters, so colour stays the caller's decision (NO_COLOR, themes): // { inline(line), gray, dim, mag, green, yellow, cyan, red, bold, italic, underline } -// `inline` paints one prose row (headings, bold, inline code, bullets). +// `inline` paints one source line (headings, bold, inline code, list markers) before it is +// wrapped, so bold that crosses a row boundary is painted on both rows. // // Handles: fenced code (language-aware highlighting, gutter, label), ```diff, // pipe tables (aligned; stacked records when too wide), block quotes, rules and @@ -16,6 +17,7 @@ const { ansiWrap, strWidth, sanitizeTerm } = require("./tui-format"); const { trimUrl } = require("./links"); const id = (s) => s; +const LIST_RE = /^\s*(?:[-*+]|\d{1,3}[.)])\s+(?=\S)/; // "- x", "* x", "1. x", "2) x" (indent kept) const stripSgr = (s) => String(s).replace(/\x1b\[[0-9;]*m/g, ""); // ---- syntax highlighting ---------------------------------------------------- @@ -172,7 +174,14 @@ function renderMd(text, width, C) { width = Math.max(10, width | 0); const src = String(text == null ? "" : text).replace(/\r\n?/g, "\n").split("\n"); const out = []; - const prose = (l) => { for (const w of ansiWrap(italicize(linkify(l, C), C), width)) out.push(C.inline(w)); }; + // Paint first, then wrap (ansiWrap carries the colour onto the next row). A list item's + // marker is painted alone and its wrapped rows hang under the text, not under the marker. + const prose = (l) => { + const lm = LIST_RE.exec(l); + if (!lm) { for (const w of ansiWrap(C.inline(italicize(linkify(l, C), C)), width)) out.push(w); return; } + const mark = C.inline(lm[0]), hang = strWidth(mark); + ansiWrap(C.inline(italicize(linkify(l.slice(lm[0].length), C), C)), Math.max(4, width - hang)).forEach((w, k) => out.push((k ? " ".repeat(hang) : mark) + w)); + }; let fence = null; // { mark, lang, st, diff } for (let i = 0; i < src.length; i++) { const line = src[i]; diff --git a/lib/cli/tui-format.js b/lib/cli/tui-format.js index 041c735..fbe3836 100644 --- a/lib/cli/tui-format.js +++ b/lib/cli/tui-format.js @@ -527,7 +527,31 @@ function toolResultView(name, input, content, opts) { if (N === "WebSearch") return { lines: [{ k: "text", text: "Did 1 search" + (o.secs ? " in " + o.secs : "") }], more: 0 }; if (N === "Task") { const v = o.expanded ? clipOut(all.filter((l) => l.trim()), "out") : { lines: [], more: 0 }; v.lines.unshift({ k: "text", text: "Done" }); return v; } if (obj && obj.ok === true && Object.keys(obj).length === 1) return { lines: [{ k: "text", text: "Done" }], more: 0 }; + if (obj && obj.skipped === true) return { lines: [{ k: "text", text: "Dismissed without an answer" }], more: 0 }; // ask_user: the note inside is for the model return clipOut(all.filter((l) => l.trim()), "out"); } -module.exports = { asText, sanitizeTerm, charWidth, strWidth, cutIdx, toolResultView, ccName, stripAnsi, visLen, fitWidth, ansiWrap, padRight, shortenPath, bootHeader, welcomeCard, idleHome, logoMark, MARK_ORBIT, orbitGlyph, activityLabel, footerLine, helpLines, modelLabel, effortLabel, explainEngineError }; +// Wrap a two-column help screen (` command description`) to `width`: a row +// that doesn't fit wraps its description under the description column (a +// hanging indent), so nothing hard-wraps mid-word into column 0. A row with no +// second column wraps under its own indent + 2. ANSI-aware; rows that fit are +// returned untouched. +function wrapHelp(text, width) { + width = Math.max(30, width | 0); + const rawAt = (line, col) => { let v = 0, i = 0; while (i < line.length && v < col) { const m = /^\x1b\[[0-9;]*m/.exec(line.slice(i)); if (m) { i += m[0].length; continue; } const ch = String.fromCodePoint(line.codePointAt(i)); v += charWidth(ch); i += ch.length; } while (i < line.length) { const m = /^\x1b\[[0-9;]*m/.exec(line.slice(i)); if (!m) break; i += m[0].length; } return i; }; + return String(text).split("\n").map((line) => { + const vis = stripAnsi(line); + if (strWidth(vis) <= width) return line; + const m = /^(\s*\S.*?\S)(\s{2,})(?=\S)/.exec(vis); + let col = m ? strWidth(m[1]) + m[2].length : -1; + if (col < 0 || col > width - 16) col = -1; + const ind = col >= 0 ? col : Math.min(vis.match(/^\s*/)[0].length + 2, Math.floor(width / 2)); + const head = col >= 0 ? line.slice(0, rawAt(line, col)) : ""; + const body = col >= 0 ? line.slice(rawAt(line, col)) : line.slice(rawAt(line, vis.match(/^\s*/)[0].length)); + const lead = col >= 0 ? head : " ".repeat(vis.match(/^\s*/)[0].length); + const rows = ansiWrap(body, width - ind); + return rows.map((r, k) => (k ? " ".repeat(ind) : lead) + r).join("\n"); + }).join("\n"); +} + +module.exports = { wrapHelp, asText, sanitizeTerm, charWidth, strWidth, cutIdx, toolResultView, ccName, stripAnsi, visLen, fitWidth, ansiWrap, padRight, shortenPath, bootHeader, welcomeCard, idleHome, logoMark, MARK_ORBIT, orbitGlyph, activityLabel, footerLine, helpLines, modelLabel, effortLabel, explainEngineError }; diff --git a/lib/governance/policy.js b/lib/governance/policy.js index 03a6ef0..c2dfd8e 100644 --- a/lib/governance/policy.js +++ b/lib/governance/policy.js @@ -100,7 +100,7 @@ function policyCheck(policy, action) { function auditLog(cwd, entry) { try { const fs = require("fs"), path = require("path"), crypto = require("crypto"); - fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); + require("../nexus/nexus-dir").ensureNexusDir(cwd); const file = path.join(cwd, ".nexus", "audit.jsonl"); let prevHash = "0".repeat(64), seq = 0; try { const lines = fs.readFileSync(file, "utf8").trim().split("\n").filter(Boolean); if (lines.length) { const last = JSON.parse(lines[lines.length - 1]); prevHash = last.hash || prevHash; seq = (last.seq || 0) + 1; } } catch (_) {} diff --git a/lib/governance/usage.js b/lib/governance/usage.js index b372922..67aedbc 100644 --- a/lib/governance/usage.js +++ b/lib/governance/usage.js @@ -14,8 +14,7 @@ const TOK = (n) => { n = n || 0; return n >= 1e6 ? (n / 1e6).toFixed(2) + "M" : // appendUsage(cwd, record) — append one JSON line. Best-effort; never throws. function appendUsage(cwd, rec) { try { - const dir = path.join(cwd, ".nexus"); - fs.mkdirSync(dir, { recursive: true }); + const dir = require("../nexus/nexus-dir").ensureNexusDir(cwd); fs.appendFileSync(path.join(dir, "usage.jsonl"), JSON.stringify(rec) + "\n"); return true; } catch (_) { return false; } diff --git a/lib/nexus/bisect.js b/lib/nexus/bisect.js index 206f7f2..9db7110 100644 --- a/lib/nexus/bisect.js +++ b/lib/nexus/bisect.js @@ -102,7 +102,7 @@ function persistCheckpoint(cwd, ck, deps) { if (!ck || !ck.tree || !Array.isArray(ck.paths) || !ck.paths.length) return false; // only bisectable (path-scoped) checkpoints try { const dir = path.join(cwd, ".nexus"); - fs.mkdirSync(dir, { recursive: true }); + if (deps.fs) fs.mkdirSync(dir, { recursive: true }); else require("./nexus-dir").ensureNexusDir(cwd); const file = path.join(dir, "checkpoints.jsonl"); const rec = { tree: ck.tree, label: String(ck.label || ""), ts: ck.ts || Date.now(), paths: ck.paths }; fs.appendFileSync(file, JSON.stringify(rec) + "\n"); diff --git a/lib/nexus/code-search.js b/lib/nexus/code-search.js index 72b9aaa..a5b8964 100644 --- a/lib/nexus/code-search.js +++ b/lib/nexus/code-search.js @@ -47,7 +47,7 @@ function loadIndex(cwd) { function buildAndSave(cwd) { const list = collectFiles(cwd); const index = Object.assign(bm25.buildIndex(list), { files: list.length, capped: !!list.capped, ts: Date.now() }); - try { fs.mkdirSync(path.join(cwd, ".nexus"), { recursive: true }); fs.writeFileSync(indexPath(cwd), JSON.stringify(index)); } catch (_) {} + try { require("./nexus-dir").ensureNexusDir(cwd); fs.writeFileSync(indexPath(cwd), JSON.stringify(index)); } catch (_) {} return index; } diff --git a/lib/nexus/costsave.js b/lib/nexus/costsave.js index d3e5cf6..9e86f8f 100644 --- a/lib/nexus/costsave.js +++ b/lib/nexus/costsave.js @@ -63,7 +63,7 @@ function cacheGet(cwd, key, ttlMs) { try { const f = path.join(cacheDir(cwd), key + ".json"); const st = fs.statSync(f); if (ttlMs && Date.now() - st.mtimeMs > ttlMs) return null; return JSON.parse(fs.readFileSync(f, "utf8")); } catch (_) { return null; } } function cachePut(cwd, key, rec) { - try { const d = cacheDir(cwd); fs.mkdirSync(d, { recursive: true }); fs.writeFileSync(path.join(d, key + ".json"), JSON.stringify(rec)); return true; } catch (_) { return false; } + try { const d = require("./nexus-dir").ensureNexusDir(cwd, "cache"); fs.writeFileSync(path.join(d, key + ".json"), JSON.stringify(rec)); return true; } catch (_) { return false; } } function cacheClear(cwd) { try { fs.rmSync(cacheDir(cwd), { recursive: true, force: true }); return true; } catch (_) { return false; } } function cacheStats(cwd) { diff --git a/lib/nexus/dashboard.js b/lib/nexus/dashboard.js index ef10045..69a25ea 100644 --- a/lib/nexus/dashboard.js +++ b/lib/nexus/dashboard.js @@ -164,7 +164,7 @@ V.terminal=function(){var rs=D.terminal.filter(match); return bar()+(rs.length?'
'+rs.map(function(r){return'
$ '+esc(r.command)+''+(r.by==="you"?"you, with !":"agent")+(r.status&&r.status!=="done"?" · "+r.status:"")+(r.end&&r.start?" · "+dur(r.end-r.start):"")+'
'+(showOut?'
'+esc(r.output||(r.status==="running"?"(running)":"(no output)"))+'
':"")}).join("")+'
':'
'+(q?"Nothing matches.":"No shell commands yet.")+'
')}; V.files=function(){var f=D.files,rows=f.files.filter(function(x){return!q||x.path.toLowerCase().indexOf(q)>=0});var diffs=""; if(selFile){D.conversation.forEach(function(m){(m.items||[]).forEach(function(i){if(i.type==="tool"&&i.file===selFile&&i.diff)diffs+='
'+esc(i.label)+' · '+clock(i.start)+'
'+diffHTML(i.diff)})})} - return'
'+f.files.length+' files touched · click one to see its changes
' + return'
'+f.files.length+' file'+(f.files.length===1?'':'s')+' touched · click one to see its changes
' +'

Files

'+(rows.length?''+rows.map(function(x){return''}).join("")+'
PathReadsEditsLast
'+esc(x.path)+''+x.read+''+x.edit+''+clock(x.last)+'
':'
No files read or changed yet.
')+'
' +'

'+(selFile?esc(selFile):"Changes")+'

'+(selFile?(diffs||'
Read only: no edits to this file.
'):'
Pick a file.
')+'
' +'

Searches

'+(f.searches.length?f.searches.slice().reverse().map(function(s){return'
'+esc(s.label)+''+s.status+'
'}).join(""):'
None.
')+'
' diff --git a/lib/nexus/local-loop.js b/lib/nexus/local-loop.js index dbb319c..b623273 100644 --- a/lib/nexus/local-loop.js +++ b/lib/nexus/local-loop.js @@ -6,6 +6,12 @@ // compactTail(msgs, keep) -> the last `keep` messages after the system message, // starting at a user message when there is one, else // at the model's own step: never an orphaned tool result +// wantsToolWork(text) -> true when the request clearly asks for work on the +// project (a file/path, or a verb aimed at code, files, +// the repo or the shell) and doesn't rule tools out +// forbidsTools(text) -> the user said not to use tools +// sameText(a, b) -> b already says a (whitespace/case-insensitive), so a +// final step's `thought` needn't be shown above it // A plain .slice() of the JSON cut the tail off silently: read_file caps a file at // 10,000 chars, but JSON escaping (\n, \", \\) can push the observation past the @@ -40,4 +46,25 @@ function compactTail(msgs, keep) { return i < 0 ? [] : tail.slice(i); } -module.exports = { capObservation, compactTail }; +// The "look before you answer" reminder for a local model that answers a project question +// from memory. A plain question ("explain closures", "write a haiku", "what is 2+2") must +// never be pushed into tool calls: the model then invents one (a pointless grep, or an +// edit) just to satisfy the reminder. So it fires only for clear project work. +const NO_TOOLS = /\b(?:no|without(?:\s+using)?|don'?t\s+(?:use|call|run)|do\s+not\s+(?:use|call|run)|never\s+(?:use|call)|avoid(?:\s+using)?)\s+(?:any\s+)?tools?\b|\bno\s+tool\s+(?:calls?|use)\b/i; +function forbidsTools(text) { return NO_TOOLS.test(String(text || "")); } +const FILE_REF = /(?:^|[\s"'`(@])(?:\.{0,2}\/)?[\w.-]+\/[\w./-]+|\b[\w-]+\.(?:m?js|cjs|tsx?|jsx|py|go|rs|rb|java|kt|c|h|cc|cpp|hpp|cs|php|sh|bash|json|ya?ml|toml|ini|cfg|conf|md|txt|html?|css|scss|sql|lock|env|xml|gradle|dockerfile)\b|\b(?:Makefile|Dockerfile|README)\b/i; +const FS_WORK = /\b(?:run|execute|install|build|compile|test|lint|commit|grep|search|find|list|read|open|show|edit|modify|change|fix|refactor|rename|delete|remove|create|write|add|save|update|implement|debug|scan|check|look\s+at|inspect)\b[^.?!\n]*\b(?:files?|folders?|director(?:y|ies)|dirs?|repo(?:sitory)?|project|codebase|workspace|branch(?:es)?|commits?)\b/i; +const CODE_WORK = /\b(?:run|execute|install|build|compile|test|lint|edit|modify|change|fix|refactor|rename|delete|remove|update|debug|scan|check|look\s+at|inspect)\b[^.?!\n]*\b(?:code|functions?|class(?:es)?|modules?|scripts?|tests?|commands?|shell|packages?|dependenc(?:y|ies))\b/i; +const HERE = /\b(?:in|from|of|at|inside)\s+(?:this|the|my|our)\s+(?:repo(?:sitory)?|project|codebase|folder|directory|code|workspace)\b|\b(?:files?|folders?)\s+here\b/i; +function wantsToolWork(text) { + const t = String(text || ""); + if (!t.trim() || forbidsTools(t)) return false; + return FILE_REF.test(t) || FS_WORK.test(t) || CODE_WORK.test(t) || HERE.test(t); +} +const NO_TOOLS_REPLY = "Not run: the user asked you not to use tools for this. Answer directly with action \"final\"."; +const NUDGE = "You answered without looking at the project. This request is about the files here: use a tool first (read_file, list_dir, search or run_command), then give your final answer."; + +const _norm = (x) => String(x == null ? "" : x).replace(/\s+/g, " ").trim().toLowerCase(); +function sameText(a, b) { const x = _norm(a), y = _norm(b); return !!x && !!y && y.includes(x); } + +module.exports = { capObservation, compactTail, wantsToolWork, forbidsTools, sameText, NUDGE, NO_TOOLS_REPLY }; diff --git a/lib/nexus/loopguard.js b/lib/nexus/loopguard.js index 8ae66f9..1ef59b1 100644 --- a/lib/nexus/loopguard.js +++ b/lib/nexus/loopguard.js @@ -92,7 +92,8 @@ function makeLoopGuard(opts) { if (!nudged.has(key)) { nudged.add(key); return { action: "nudge", kind: "repeat-failure", sig, - message: "LOOP GUARD: this exact action has now failed " + fails + " times and keeps producing the same error. Stop repeating it. Diagnose WHY it fails (re-read the file/error, check the path or syntax) and take a DIFFERENT action — or if it can't work, say so in your final answer." }; + message: "LOOP GUARD: this exact action has now failed " + fails + " times and keeps producing the same error. Stop repeating it. Diagnose WHY it fails (re-read the file/error, check the path or syntax) and take a DIFFERENT action — or if it can't work, say so in your final answer.", + notice: "loop guard: the same step failed " + fails + " times with the same error, so Nexus told the model to try a different approach" }; } return { action: "break", kind: "repeat-failure", sig, message: "Stopped: the same action failed " + fails + " times even after a warning, so Nexus ended the turn instead of looping. Last error stands — try a different approach or ask the operator." }; @@ -107,7 +108,8 @@ function makeLoopGuard(opts) { if (!nudged.has(key)) { nudged.add(key); return { action: "nudge", kind: "oscillation", sig: osc, - message: "LOOP GUARD: you're bouncing between the same two actions over and over. Break the cycle — step back, reconsider the plan, and take a genuinely different step." }; + message: "LOOP GUARD: you're bouncing between the same two actions over and over. Break the cycle — step back, reconsider the plan, and take a genuinely different step.", + notice: "loop guard: the model kept switching between the same two steps, so Nexus told it to try something different" }; } return { action: "break", kind: "oscillation", sig: osc, message: "Stopped: oscillating between the same two actions even after a warning, so Nexus ended the turn." }; @@ -120,7 +122,8 @@ function makeLoopGuard(opts) { if (!nudged.has(key)) { nudged.add(key); return { action: "nudge", kind: "repeat", sig, - message: "LOOP GUARD: you've run this same action " + repeats + " times in a row without making progress. It is not moving the task forward — change approach, move to the next step, or finish." }; + message: "LOOP GUARD: you've run this same action " + repeats + " times in a row without making progress. It is not moving the task forward — change approach, move to the next step, or finish.", + notice: "loop guard: the same step ran " + repeats + " times without progress, so Nexus told the model to change approach" }; } return { action: "break", kind: "repeat", sig, message: "Stopped: the same action repeated " + repeats + " times with no progress even after a warning, so Nexus ended the turn." }; diff --git a/lib/nexus/nexus-dir.js b/lib/nexus/nexus-dir.js new file mode 100644 index 0000000..1f0a835 --- /dev/null +++ b/lib/nexus/nexus-dir.js @@ -0,0 +1,23 @@ +"use strict"; +// The project's .nexus/ holds session transcripts, usage, checkpoints and caches. Every +// place that creates it goes through ensureNexusDir so the folder carries its own +// ".gitignore" with "*": git ignores the whole folder without Nexus editing the user's +// own .gitignore, and `git add -A` can't commit a conversation by accident. +const fs = require("fs"), path = require("path"); + +function ignoreNexusDir(dir) { + const gi = path.join(dir, ".gitignore"); + try { if (!fs.existsSync(gi)) fs.writeFileSync(gi, "*\n"); } catch (_) {} +} + +// mkdir -p /.nexus[/sub...] and make sure .nexus/.gitignore exists. Returns the +// created path. Throws like fs.mkdirSync (callers already wrap it). +function ensureNexusDir(cwd, ...sub) { + const dir = path.join(cwd, ".nexus"); + const full = sub.length ? path.join(dir, ...sub) : dir; + fs.mkdirSync(full, { recursive: true }); + ignoreNexusDir(dir); + return full; +} + +module.exports = { ensureNexusDir, ignoreNexusDir }; diff --git a/package.json b/package.json index b18b21c..774c892 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "scripts": { "start": "node darknode.js", "build": "pkg . --out-path dist", - "test": "node test/run.js && node test/bg-cutoff.test.js && node test/usage-ping.test.js && node test/authz.test.js && node test/composer.test.js && node test/term.test.js && node test/md-render.test.js && node test/links.test.js && node test/pager.test.js && node test/tool-tail.test.js && node test/os-notify.test.js && node test/repo-map-rank.test.js && node test/bm25.test.js && node test/danger-audit.test.js && node test/url-guard-audit.test.js && node test/edit-audit.test.js && node test/jail-audit.test.js && node test/mcp-audit.test.js && node test/git-search-audit.test.js && node test/state-audit.test.js && node test/exec-audit.test.js && node test/cli-audit.test.js && node test/governance-audit.test.js && node test/toolkit-audit.test.js && node test/devhelpers-audit.test.js && node test/radar-audit.test.js && node test/pentest-audit.test.js && node test/shellfix-audit.test.js && node test/convo-lifecycle-audit.test.js && node test/local-loop-audit.test.js && node test/undo-audit.test.js && node test/at-mentions-audit.test.js && node test/clipboard-audit.test.js && node test/notes-audit.test.js && node test/snippets-audit.test.js && node test/typed-names-audit.test.js && node test/sighup-audit.test.js && node test/tui-stderr-audit.test.js && node test/piped-stdin-audit.test.js && node test/oneshot-escape-audit.test.js && node test/hot-path-speed-audit.test.js && node test/command-help.test.js && node test/session-save.test.js && node test/agents-panel.test.js && node test/tui-layout.test.js && node test/hub.test.js && node test/dashboard.test.js && node test/acp-sandbox.test.js", + "test": "node test/run.js && node test/bg-cutoff.test.js && node test/usage-ping.test.js && node test/authz.test.js && node test/composer.test.js && node test/term.test.js && node test/md-render.test.js && node test/links.test.js && node test/pager.test.js && node test/tool-tail.test.js && node test/os-notify.test.js && node test/repo-map-rank.test.js && node test/bm25.test.js && node test/danger-audit.test.js && node test/url-guard-audit.test.js && node test/edit-audit.test.js && node test/jail-audit.test.js && node test/mcp-audit.test.js && node test/git-search-audit.test.js && node test/state-audit.test.js && node test/exec-audit.test.js && node test/cli-audit.test.js && node test/governance-audit.test.js && node test/toolkit-audit.test.js && node test/devhelpers-audit.test.js && node test/radar-audit.test.js && node test/pentest-audit.test.js && node test/shellfix-audit.test.js && node test/convo-lifecycle-audit.test.js && node test/local-loop-audit.test.js && node test/undo-audit.test.js && node test/at-mentions-audit.test.js && node test/clipboard-audit.test.js && node test/notes-audit.test.js && node test/snippets-audit.test.js && node test/typed-names-audit.test.js && node test/sighup-audit.test.js && node test/tui-stderr-audit.test.js && node test/piped-stdin-audit.test.js && node test/oneshot-escape-audit.test.js && node test/hot-path-speed-audit.test.js && node test/command-help.test.js && node test/session-save.test.js && node test/agents-panel.test.js && node test/tui-layout.test.js && node test/hub.test.js && node test/dashboard.test.js && node test/acp-sandbox.test.js && node test/visual-audit.test.js", "postinstall": "node scripts/postinstall.js", "docs": "node scripts/gen-nexus-docs.js" }, diff --git a/test/run.js b/test/run.js index 2f8e3fc..ea57852 100644 --- a/test/run.js +++ b/test/run.js @@ -1135,7 +1135,7 @@ group("-m/--model reaches the interactive TUI"); ok("--print exits nonzero when the engine fails (and its error is shown once)", /if \(er && !er\.ok && !er\.interrupted\) \{ process\.exitCode = 1; const t = er\.streamed && !hold \? ""/.test(src) && /streamed: !!out\.trim\(\)/.test(src)); ok("--print exits nonzero on an Ollama error or no local model", /catch \(e\) \{ console\.log\(" " \+ red\(e\.message\)\); if \(printMode\) process\.exitCode = 1; break; \}/.test(src) && (src.match(/No local model\. Install Ollama[^\n]*?process\.exitCode = 1; return;/g) || []).length >= 1); ok("one-shot passes -m to delegated engines", /runEngineTask\(engine, userMsg, cwd, autoApprove, delegated, [^\n]*?, null, modelOverride \|\| undefined\)/.test(src)); - ok("--print keeps stdout to the raw answer (progress to stderr, engine output unindented)", /if \(printMode\) console\.log = console\.error\.bind\(console\)/.test(src) && /delegated, printMode \? \(hold \? \(\) => \{\} : \(c\) => process\.stdout\.write\(c\)\) : null/.test(src) && /if \(printMode\) answer\(o\.final \|\| "done"\)/.test(src)); + ok("--print keeps stdout to the raw answer (progress to stderr, engine output unindented)", /if \(printMode\) console\.log = console\.error\.bind\(console\)/.test(src) && /delegated, printMode \? \(hold \? \(\) => \{\} : \(c\) => process\.stdout\.write\(c\)\) : null/.test(src) && /if \(printMode\) \{ if \(fin\) answer\(fin\); else \{ console\.error\(/.test(src) && !/answer\(o\.final \|\| "done"\)/.test(src)); ok("-p adds piped stdin to the task, only from a pipe or file (never waits on a parent's open socket), capped for argv", /if \(printMode && parts\.length && !process\.stdin\.isTTY\)/.test(src) && /st\.isFIFO\(\) \|\| st\.isFile\(\)/.test(src) && /--- stdin ---/.test(src) && /const CAP = 100000/.test(src)); ok("-- ends options (task text may start with -), and nexus serve passes the goal after it with an optional model", /else if \(a === "--"\) \{ parts\.push\(\.\.\.arr\.slice\(i \+ 1\)\); break; \}/.test(src) && /"--print", "-e", eng, \.\.\.model, \.\.\.yes, "--", goal\]/.test(src)); ok("darknode nexus --version / -v / -V print the version instead of sending it to the model", /a === "--version" \|\| a === "-v" \|\| a === "-V"\) \{ console\.log\("nexus " \+ VERSION\); return; \}/.test(src)); @@ -1648,7 +1648,7 @@ group("help reference (single source of truth)"); ok("redo re-adds the checkpoint at the state before the turn, so /undo after /redo reverts again", /redoStack\.push\(\{ tree: nowTree, before: ck\.tree,/.test(src) && /checkpoints\.push\(\{ tree: r\.before \|\| r\.tree,/.test(src) && /if \(ok\) \{ redoStack\.pop\(\);/.test(src)); { // functional: a turn that edits a pre-existing git-ignored file must not delete it on /undo - const fns = new Function("_cp", "require", src.match(/function nexusCheckpoint\(cwd\) \{[\s\S]*?\n\}\n/)[0] + src.match(/function nexusRestore\(cwd, tree, paths\) \{[\s\S]*?\n\}\n/)[0] + src.match(/function inSnapshotScope\(cwd, p\) \{[\s\S]*?\n\}\n/)[0] + "return { nexusCheckpoint, nexusRestore };")(require("child_process"), require); + const fns = new Function("_cp", "require", "ensureNexusDir", src.match(/function nexusCheckpoint\(cwd\) \{[\s\S]*?\n\}\n/)[0] + src.match(/function nexusRestore\(cwd, tree, paths\) \{[\s\S]*?\n\}\n/)[0] + src.match(/function inSnapshotScope\(cwd, p\) \{[\s\S]*?\n\}\n/)[0] + "return { nexusCheckpoint, nexusRestore };")(require("child_process"), require, require("../lib/nexus/nexus-dir").ensureNexusDir); const fsx = require("fs"), px = require("path"), d = fsx.mkdtempSync(px.join(require("os").tmpdir(), "nx-undo-")); const g = (...a) => require("child_process").execFileSync("git", a, { cwd: d, stdio: "ignore" }); g("init", "-q"); fsx.writeFileSync(px.join(d, ".gitignore"), "local.cfg\n"); fsx.writeFileSync(px.join(d, "a.txt"), "a\n"); diff --git a/test/session-save.test.js b/test/session-save.test.js index f470b3e..f3f4e61 100644 --- a/test/session-save.test.js +++ b/test/session-save.test.js @@ -44,8 +44,8 @@ ok("null result stays null", capResult(null) === null && capResult(undefined) == ok("found the incremental save code", d0 > 0 && d1 > d0); const dir = fs.mkdtempSync(path.join(os.tmpdir(), "nx-sess-")); const T = [], S = { model: "m" }; - const api = new Function("Redact", "asText", "fs", "path", "cwd", "stripBom", "secureNexus", "transcript", "sess", "engine", "claudeSid", - src.slice(a, b) + src.slice(d0, d1) + "\nreturn { saveSession, loadSessionFile, blockJson, _disk };")(Redact, asText, fs, path, dir, (x) => x, () => {}, T, S, "claude", null); + const api = new Function("Redact", "asText", "fs", "path", "cwd", "stripBom", "secureNexus", "transcript", "sess", "engine", "claudeSid", "ensureNexusDir", + src.slice(a, b) + src.slice(d0, d1) + "\nreturn { saveSession, loadSessionFile, blockJson, _disk };")(Redact, asText, fs, path, dir, (x) => x, () => {}, T, S, "claude", null, require("../lib/nexus/nexus-dir").ensureNexusDir); const sp = path.join(dir, ".nexus", "session.json"), lp = path.join(dir, ".nexus", "session.log"); const expect = () => JSON.stringify(JSON.parse("[" + T.filter((x) => !x.ephemeral).map(api.blockJson).join(",") + "]")); const loaded = () => JSON.stringify(api.loadSessionFile().transcript); diff --git a/test/undo-audit.test.js b/test/undo-audit.test.js index 83d555b..97423f2 100644 --- a/test/undo-audit.test.js +++ b/test/undo-audit.test.js @@ -9,7 +9,7 @@ const ok = (name, cond) => { if (cond) pass++; else { fail++; console.log(" \x1 const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8"); const fnSrc = (name) => { const i = src.indexOf("function " + name + "("); if (i < 0) throw new Error("missing " + name); let d = 0, j = src.indexOf("{", i); for (let k = j; k < src.length; k++) { if (src[k] === "{") d++; else if (src[k] === "}" && --d === 0) return src.slice(i, k + 1); } throw new Error("unbalanced " + name); }; -const api = new Function("_cp", "require", ["nexusCheckpoint", "inSnapshotScope", "nexusRestore"].map(fnSrc).join("\n") + "\nreturn { nexusCheckpoint, nexusRestore, inSnapshotScope };")(cp, require); +const api = new Function("_cp", "require", "ensureNexusDir", ["nexusCheckpoint", "inSnapshotScope", "nexusRestore"].map(fnSrc).join("\n") + "\nreturn { nexusCheckpoint, nexusRestore, inSnapshotScope };")(cp, require, require("../lib/nexus/nexus-dir").ensureNexusDir); const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "nx-undo-"))); const proj = path.join(root, "proj"), outside = path.join(root, "outside"); diff --git a/test/visual-audit.test.js b/test/visual-audit.test.js new file mode 100644 index 0000000..9f7565f --- /dev/null +++ b/test/visual-audit.test.js @@ -0,0 +1,188 @@ +"use strict"; +// Regression tests for the Nexus visual-audit fixes (F1-F18): pure helpers are driven +// directly, the CLI is spawned against a scripted fake Ollama, and the TUI wiring that +// only exists inside darknode.js is checked from its source. +const fs = require("fs"); +const os = require("os"); +const path = require("path"); +const http = require("http"); +const { spawn, spawnSync } = require("child_process"); + +let pass = 0, fail = 0; +const ok = (name, cond) => { if (cond) pass++; else { fail++; console.log(" FAIL " + name); } }; +const ROOT = path.join(__dirname, ".."); +const BIN = path.join(ROOT, "darknode.js"); +const src = fs.readFileSync(BIN, "utf8"); +const strip = (s) => String(s).replace(/\x1b\[[0-9;]*m/g, ""); +const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nx-vaudit-")); +const HOME = path.join(tmp, "home"); fs.mkdirSync(HOME); +const env = (extra) => Object.assign({}, process.env, { HOME, USERPROFILE: HOME, NO_COLOR: "1", DARKNODE_FAST: "1", NO_MOTION: "1", DARKNODE_API_BASE: "", OPENAI_BASE_URL: "", OPENAI_API_BASE: "" }, extra || {}); + +// A scripted Ollama: /api/chat answers with the next reply in the list. +function fakeOllama(replies) { + const calls = []; + const srv = http.createServer((req, res) => { + let b = ""; req.on("data", (c) => (b += c)); req.on("end", () => { + if (req.url === "/api/tags") return res.end(JSON.stringify({ models: [{ name: "fake:latest" }] })); + if (req.url === "/api/version") return res.end(JSON.stringify({ version: "0.9.0" })); + if (req.url === "/api/chat") { let j = {}; try { j = JSON.parse(b); } catch (_) {} calls.push(j.messages || []); const r = replies[Math.min(calls.length - 1, replies.length - 1)]; return res.end(JSON.stringify({ message: { role: "assistant", content: typeof r === "string" ? r : JSON.stringify(r) }, done: true })); } + res.statusCode = 404; res.end("{}"); + }); + }); + return new Promise((resolve) => srv.listen(0, "127.0.0.1", () => resolve({ port: srv.address().port, calls, close: () => new Promise((r) => srv.close(r)) }))); +} +function run(args, extra, cwd) { + return new Promise((resolve) => { + const p = spawn(process.execPath, [BIN].concat(args), { cwd: cwd || tmp, env: env(extra), stdio: ["ignore", "pipe", "pipe"] }); + let out = "", err = ""; + p.stdout.on("data", (c) => (out += c)); p.stderr.on("data", (c) => (err += c)); + const t = setTimeout(() => { try { p.kill(); } catch (_) {} }, 60000); + p.on("close", (code) => { clearTimeout(t); resolve({ code, out: strip(out), err: strip(err) }); }); + }); +} +async function printTurn(prompt, replies) { + const fo = await fakeOllama(replies); + const proj = fs.mkdtempSync(path.join(tmp, "p-")); + fs.writeFileSync(path.join(proj, "app.js"), "module.exports = 1;\n"); + const r = await run(["nexus", "-p", prompt, "-e", "ollama", "-m", "fake:latest"], { OLLAMA_HOST: "127.0.0.1:" + fo.port }, proj); + await fo.close(); + return Object.assign(r, { calls: fo.calls, proj }); +} + +(async () => { + // ---- F2: .nexus/ ignores itself ---- + { + const ND = require("../lib/nexus/nexus-dir"); + const d = fs.mkdtempSync(path.join(tmp, "nd-")); + fs.writeFileSync(path.join(d, ".gitignore"), "node_modules\n"); + const sub = ND.ensureNexusDir(d, "cache"); + ok("F2: ensureNexusDir creates .nexus/ and returns its path", sub === path.join(d, ".nexus", "cache") && fs.statSync(sub).isDirectory()); + ok("F2: .nexus/.gitignore holds '*' and the project's own .gitignore is untouched", fs.readFileSync(path.join(d, ".nexus", ".gitignore"), "utf8") === "*\n" && fs.readFileSync(path.join(d, ".gitignore"), "utf8") === "node_modules\n"); + fs.writeFileSync(path.join(d, ".nexus", ".gitignore"), "custom\n"); ND.ensureNexusDir(d); + ok("F2: an existing .nexus/.gitignore is never overwritten", fs.readFileSync(path.join(d, ".nexus", ".gitignore"), "utf8") === "custom\n"); + const d2 = fs.mkdtempSync(path.join(tmp, "nd-")); + require("../lib/governance/usage").appendUsage(d2, { engine: "ollama", ok: true }); + ok("F2: writing usage.jsonl (the first thing a turn writes) also drops .nexus/.gitignore", fs.existsSync(path.join(d2, ".nexus", ".gitignore"))); + const g = spawnSync("git", ["init", "-q"], { cwd: d2 }); + if (!g.error && g.status === 0) { const st = spawnSync("git", ["status", "--porcelain", "--untracked-files=all"], { cwd: d2, encoding: "utf8" }); ok("F2: git status shows nothing from .nexus/", !/\.nexus/.test(st.stdout || "")); } + ok("F2: darknode.js has no bare mkdir of .nexus left (all go through ensureNexusDir)", !/fs\.mkdirSync\(path\.join\(cwd, "\.nexus"\), \{ recursive: true \}\)/.test(src) && (src.match(/ensureNexusDir\(/g) || []).length >= 15); + } + + // ---- F4: loop guard notice ---- + { + const { makeLoopGuard } = require("../lib/nexus/loopguard"); + const lg = makeLoopGuard(); let r = null; + for (let i = 0; i < 6 && !r; i++) r = lg.note(lg.signature("read_file", { path: "a" }), true, lg.hashStr("same")); + ok("F4: a loop-guard nudge carries a user-facing notice (the model message stays separate)", r && r.action === "nudge" && /^loop guard: /.test(r.notice) && r.notice !== r.message && !/\bYOU\b/.test(r.notice)); + ok("F4: the TUI shows the notice once as a dim note in the turn, not as a model reply", /const _nt = _lg\.notice \|\| _lg\.message; if \(!block\.items\.some\(\(i\) => i\.type === "note" && i\.text === _nt\)\) block\.items\.push\(\{ type: "note", text: _nt \}\);/.test(src) && /else if \(it\.type === "note"\) \{[^\n]*\n\s*if \(prevType\) L\.push\(""\);\s*for \(const ln of wrap\(it\.text, 3\)\) L\.push\(" " \+ dim\(gray\(ln\)\)\);/.test(src)); + } + + // ---- F1/F10/F15: local loop helpers ---- + { + const LL = require("../lib/nexus/local-loop"); + ok("F10: requests about the files here want tools", ["fix the bug in app.js", "list the files here", "run the tests", "refactor the auth module", "what is in this repo?"].every(LL.wantsToolWork)); + ok("F10: general questions and 'no tools' requests are not sent back to use a tool", !["say hi", "what is 2+2", "explain closures", "write a python function that reverses a string", "Do not use tools. Reply with a table of 3 fruits"].some(LL.wantsToolWork)); + ok("F10: forbidsTools spots 'do not use tools' / 'without tools' / 'no tool calls'", LL.forbidsTools("Do not use tools.") && LL.forbidsTools("answer without using tools") && LL.forbidsTools("no tool calls please") && !LL.forbidsTools("use the tools you need")); + ok("F1: sameText spots a thought that only repeats the answer", LL.sameText("the answer is 4", "The answer is 4.") && !LL.sameText("I will read app.js first", "4") && !LL.sameText("", "4")); + ok("F1: the TUI skips a thought that only repeats the final answer", /if \(o\.thought && !\(o\.action === "final" && LocalLoop\.sameText\(o\.thought, o\.final\)\)\)/.test(src)); + ok("F10/F15: no loop still sends 'Do the real work first.'", !/content: "Do the real work first\."/.test(src)); + } + + // ---- F15: nexus -p against a scripted model ---- + { + let r = await printTurn("what is 2+2", [{ action: "final", final: "4" }]); + ok("F15: -p answers a plain question on the first reply (no hidden nudge round)", r.code === 0 && r.out.trim() === "4" && r.calls.length === 1); + r = await printTurn("what is 2+2", [{ action: "final", final: "" }]); + ok("F15: -p with no answer prints nothing on stdout, says so on stderr and exits 1 (never the literal 'done')", r.code === 1 && r.out.trim() === "" && /no answer/.test(r.err) && !/\bdone\b/.test(r.out)); + r = await printTurn("list the files here", [{ action: "final", final: "I can't see files." }, { action: "tool", tool: "list_dir", args: { path: "." } }, { action: "final", final: "app.js" }]); + ok("F15: a request about the files here is sent back once to look first", r.code === 0 && r.calls.length === 3 && r.calls[1].some((m) => m.role === "tool" && /use a tool first/.test(m.content)) && /app\.js/.test(r.out)); + r = await printTurn("Do not use tools. Name one fruit.", [{ action: "tool", tool: "write_file", args: { path: "x.txt", content: "y" } }, { action: "final", final: "apple" }]); + ok("F10: with 'do not use tools' a tool call is not run and the model is told to answer directly", r.code === 0 && r.out.trim() === "apple" && !fs.existsSync(path.join(r.proj, "x.txt")) && r.calls[1].some((m) => m.role === "tool" && /asked you not to use tools/.test(m.content))); + } + + // ---- F13: flags ---- + { + let r = await run(["nexus", "--frobnicate"]); + ok("F13: an unknown --flag exits 2 with a usage hint instead of becoming a one-shot task", r.code === 2 && /unknown option --frobnicate/.test(r.err) && /nexus --help/.test(r.err)); + r = await run(["nexus", "--continu"]); + ok("F13: a near-miss suggests the real option", r.code === 2 && /did you mean --continue/.test(r.err)); + r = await run(["nexus", "--continue", "--help"]); + ok("F13: --continue is a known option (parsed, not task text)", r.code === 0 && /Nexus/.test(r.out) && /-c, --continue/.test(r.out)); + r = await run(["nexus", "-c", "--help"]); + ok("F13: -c is a known option", r.code === 0 && /-c, --continue/.test(r.out)); + r = await run(["nexus", "--tui", "hello"]); + ok("F13: --tui never falls through to a one-shot run", r.code === 2 && /--tui/.test(r.err) && !/▎ nexus/.test(r.out)); + ok("F13: --continue / -c / --resume map to the same resume, and a plain --resume launch is not labelled a restart", /a === "--resume" \|\| a === "--continue" \|\| a === "-c"\) resumeFlag = true;/.test(src) && /resumeFlag && process\.env\.NEXUS_SUPERVISED \? "restart" : "launch"/.test(src)); + } + + // ---- F16: help text ---- + { + const { wrapHelp } = require("../lib/cli/tui-format"); + const w = wrapHelp(" nexus [opts] [task] Nexus AI coder chat: -e claude|gemini|codex|opencode|aider|ollama, @file refs, /undo /diff /commit", 60).split("\n"); + ok("F16: wrapHelp keeps every row within the width and hangs wrapped text under the description column", w.length > 1 && w.every((l) => strip(l).length <= 60) && w.slice(1).every((l) => /^ {38}\S/.test(l))); + const r = await run(["--help"]); + const lines = r.out.split("\n"); + ok("F16: darknode --help fits an 80-column terminal", r.code === 0 && lines.length > 20 && lines.every((l) => l.length <= 80)); + ok("F16: the help hints recommend the same local model as first-run setup", !/ollama pull gpt-oss:20b/.test(src) && /ollama pull qwen2\.5-coder/.test(src)); + const nh = await run(["nexus", "--help"]); + ok("F16: nexus --help lists the default ask mode in the shift+tab cycle", /shift\+tab cycles permissions: ask first · accept edits · plan · bypass/.test(nh.out)); + } + + // ---- F9: markdown answers ---- + { + const { renderMd } = require("../lib/cli/md-render"); + const B = (c) => "\x1b[1m" + c + "\x1b[0m"; + const inline = (l) => l.replace(/\*\*([^*]+)\*\*/g, (_, c) => B(c)).replace(/^(\s*)(\d+[.)])(\s)/, "$1$2$3").replace(/^(\s*[-*+]\s)/, (b) => b.replace(/[-*+]/, "•")); + const out = renderMd("1. Open the config file and find the section called **database connection settings** which controls pooling\n2. Run tests\n- a very long bullet item that keeps going and going onto the next line of the terminal ok\n\nThe final thing to note here is that you should remember the docs and **this important bold phrase that wraps across the row boundary here** at the end. 2. and - here", 56, { inline }); + const plain = out.map(strip); + ok("F9: ordered lists keep their numbers", plain[0].startsWith("1. ") && plain.some((l) => l.startsWith("2. Run tests"))); + ok("F9: list items wrap with a hanging indent under the text", plain[1].startsWith(" ") && plain.some((l, i) => i > 0 && plain[i - 1].startsWith("• ") && l.startsWith(" ") && !l.startsWith(" "))); + ok("F9: bold that wraps across rows is painted on both rows, with no raw ** left", !plain.some((l) => l.includes("**")) && out.filter((l) => l.includes("\x1b[1m")).length >= 3); + ok("F9: a wrapped prose row starting with 'N. ' or '- ' gets no bullet", !plain.some((l) => /^•/.test(l) && /here$/.test(l)) && plain[plain.length - 1].includes("2. and - here")); + ok("F9: colorMd keeps the number of an ordered item", /s = s\.replace\(\/\^\(\\s\*\)\(\\d\+\[\.\)\]\)\(\\s\)\/, \(_, sp, n, w\) => sp \+ cyan\(n\) \+ w\)/.test(src)); + } + + // ---- F3/F5/F11: only real changes count ---- + ok("F3: a file is counted as written only after the tool succeeded (and not for 'no change')", /if \(!_failed && !blocked && \["write_file", "edit_file"\]\.includes\(name\) && a\.path && !\(result && typeof result\.note === "string" && \/\^no change\/\.test\(result\.note\)\)\) \{ stat\.files\.add\(base\(a\.path\)\);/.test(src)); + ok("F3: a run_command with a non-zero exit is a failed step", /const _failed = !!\(result && \(result\.error \|\| \(name === "run_command" && typeof result\.code === "number" && result\.code !== 0\)\)\);/.test(src)); + ok("F11: the dashboard turn count survives /clear (like the turn ledger) and 'file' is singular for one", /turns: clearedTurns \+ transcript\.filter/.test(src) && /' file'\+\(f\.files\.length===1\?'':'s'\)/.test(fs.readFileSync(path.join(ROOT, "lib", "nexus", "dashboard.js"), "utf8"))); + + // ---- F6/F7: interrupted turns and recaps ---- + ok("F6: an interrupted local turn ends flagged interrupted with no 'done' text", /finish\(\{ output: "", interrupted: !!\(ctl && ctl\.stopped\) \}\);/.test(src) && !/finish\(\{ output: "done" \}\)/.test(src)); + ok("F7: no recap for an interrupted turn or one with no real output", /if \(!\(res && res\.interrupted\) && \(_realReply \|\| stat\.files\.size\) &&/.test(src)); + ok("F7: auto-recap is off by default on the local engine unless the recap setting was chosen", /const recapActive = \(\) => autoRecap && \(recapChosen \|\| engine !== "ollama" \|\| apiConfigured\(\)\);/.test(src) && /let recapChosen = typeof S\.recap === "boolean";/.test(src)); + + // ---- F8: /model ---- + ok("F8: arg completion prefers names that start with the text and never matches a number inside a name", /const pre = items\.filter\(\(it\) => it\[0\]\.toLowerCase\(\)\.startsWith\(partial\)\);\s*if \(\/\^\\d\+\$\/\.test\(partial\)\) return pre;/.test(src)); + ok("F8: Enter runs a typed number as typed (/model 3 picks #3)", /\(_part && !\/\^\\d\+\$\/\.test\(_part\) && !_am\.some/.test(src)); + + // ---- F12/F14: Esc and saving ---- + ok("F12: Esc only pops entries flagged dismissable, and that Esc does not arm the double-Esc rewind", /_last\.dismissable/.test(src) && /_lastEscTs = 0/.test(src)); + ok("F12: Esc Esc in one read is split so the idle double-Esc still clears the input", /if \(s\.length > 1 && \/\^\\x1b\+\$\/\.test\(s\)\) \{ try \{ _escFlushing = true;/.test(src)); + ok("F14: the session is saved on exit, after setting changes and after a rewind", /const cleanup = \(\) => \{ if \(cleaned\) return; cleaned = true; tuiActive = false; try \{ saveChat\(\); \} catch/.test(src) && /\(settings\|options\|config\|theme\|color\|recap\)\$\/\.test\(cmd\)\) setTimeout\(\(\) => \{ if \(!busy\) saveChat\(\); \}, 0\)/.test(src) && /try \{ saveSession\(\); \} catch \(_\) \{\} \/\/ a resume must show the rewound state/.test(src)); + + // ---- F17: permission prompts on the local engine ---- + ok("F17: modes 0 and 1 ask before a command; mode 0 also asks before file changes", /if \(!blocked && _perm !== "allow" && \(mode === 0 \|\| mode === 1\)\) \{/.test(src) && /if \(_key && \(mode === 0 \|\| !_isFile\) && !sessAllow\.has\(_key\)\) \{/.test(src) && /askUser\("Allow Nexus to " \+ _what/.test(src)); + ok("F17: usage.jsonl logs mode 3 as bypass", /mode: \["normal", "auto", "plan", "bypass"\]\[mode\] \|\| "normal"/.test(src)); + + // ---- F18 ---- + { + const TF = require("../lib/cli/tui-format"); + const v = TF.toolResultView("ask_user", {}, { skipped: true, note: "the user dismissed the question" }); + ok("F18: a dismissed ask_user card reads as text, not raw JSON", v.lines.length === 1 && v.lines[0].text === "Dismissed without an answer"); + const H = require("../lib/cli/command-help"); + ok("F18: /help settings shows its arguments", /usage \/settings \[ on\|off/.test(strip([].concat(H.card("/settings", "")).join("\n")))); + ok("F18: /pager with no argument opens the last reply", /want = a === "answer" \|\| \(!a && hasReply\) \? "text" : "tool"/.test(src)); + ok("F18: /mcp says how many tools it did not list", /\(s\.tools \|\| \[\]\)\.length > 8 \? gray\(" \+" \+/.test(src)); + ok("F18: /clear resets the tab title and drops a title still being made for the old chat", /titleEpoch\+\+; if \(!titleManual\) \{ sessionTitle = ""; titleAt = 0; idleTitle\(\); \}/.test(src) && /if \(t && ep === titleEpoch\)/.test(src)); + ok("F18: /model on Ollama is checked against the installed models", /no local model '" \+ a \+ "' — nothing changed/.test(src)); + ok("F18: /bisect's missing-test message uses the slash form", /run \/bisect \(e\.g\. \/bisect npm test\)/.test(src)); + ok("F18: Ctrl+C on an empty prompt shows a passing hint, not a transcript line", /case "exit-confirm": \{ const _n = \{ text: "Press Ctrl\+C again to exit", ok: false \}; linkNotice = _n;/.test(src)); + ok("F18: rotating tips end with an ellipsis instead of being cut mid-word", /fitWidth\("tip " \+ tips\[/.test(src)); + ok("F18: /settings rows keep their columns on a narrow terminal", /const fitW = cols\(\) - 4;/.test(src) && /"env vars: " \+ envLater\.join/.test(src)); + } + + try { fs.rmSync(tmp, { recursive: true, force: true }); } catch (_) {} + console.log("visual-audit: " + pass + " passed, " + fail + " failed"); + if (fail) process.exit(1); +})();