From f36a075226d9ee76ab028d39c3fbc80dad62ef26 Mon Sep 17 00:00:00 2001 From: Darknode-Official Date: Wed, 11 Nov 2026 10:14:02 -0800 Subject: [PATCH] TUI: re-read the window size so rows never overflow a smaller window Every row is clipped to the width Nexus believes the window has, which it only learned from SIGWINCH. When that signal is missed, the width stays stale: after shrinking a window, reply lines, the ask card and the status bar were still drawn for the old width and the terminal wrapped them (text spilling onto the next row, stray scroll-rail marks mid-screen). The TUI now re-reads the size every 500 ms with the stream's own refresh, which emits "resize" only when the size really changed, so the existing full repaint runs. The poll is unref'd and stopped on exit. Checked under a real pty: with the signal handler gone, columns stayed at 120 after the window went to 78; the refresh read 78 and fired resize. --- darknode.js | 4 +++- test/run.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/darknode.js b/darknode.js index eb2255a..b198fef 100755 --- a/darknode.js +++ b/darknode.js @@ -4274,7 +4274,8 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { if (sess.ctxUsed > compactAt()) doCompact(true); }; const onResize = () => { lastLines = null; if (!loading) render(); }; // geometry changed — force a full repaint - const cleanup = () => { if (cleaned) return; cleaned = true; tuiActive = false; if (tick) { clearInterval(tick); tick = null; } if (_rsT) { clearTimeout(_rsT); _rsT = 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 (_) {} }; + 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 (_) {} }; 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"); @@ -6950,6 +6951,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) { } } catch (err) { busy = false; try { transcript.push({ role: "system", text: "internal error: " + (err && err.message || err) }); render(); } catch (_) {} } }); out.on("resize", onResize); + if (typeof out._refreshSize === "function") { sizePoll = setInterval(() => { try { out._refreshSize(); } catch (_) {} }, 500); if (sizePoll.unref) sizePoll.unref(); } // emits "resize" only when the size really changed // ---- boot ---- // No animation, no starfield, no latency: render the compact header (see the // "art" transcript row) straight away. fastBoot/NO_MOTION kept for parity. diff --git a/test/run.js b/test/run.js index 0cc5638..87f1a6e 100644 --- a/test/run.js +++ b/test/run.js @@ -233,6 +233,7 @@ group("hard-deny floor (NSAFE-016)"); ok("floor denies editing the safety settings", denied({ type: "edit", path: "/home/u/.nexus/settings.json" }) === "tamper-safety-config"); ok("floor denies a shell write to permissions.json, and a file-tool write to hooks.json", denied({ type: "run", command: "echo '{\"allow\":[\"*\"]}' > .nexus/permissions.json" }) === "tamper-policy" && denied({ type: "run", command: "sed -i s/x/y/ .nexus/policy.json" }) === "tamper-policy" && denied({ type: "write", path: "/p/.nexus/hooks.json" }) === "tamper-policy" && denied({ type: "run", command: "cp evil.json .nexus/hooks.json" }) === "tamper-policy"); ok("darknode serve: a malformed %-escape is a 400 (it threw and crashed the server) and listing names are escaped + URL-encoded", (() => { const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8"); const f = src.slice(src.indexOf("function serveDir("), src.indexOf("function listen(")); return /try \{ rel = decodeURIComponent/.test(f) && /writeHead\(400\)/.test(f) && /encodeURIComponent\(i\)/.test(f) && /\$\{h\(i\)\}/.test(f); })()); + ok("TUI re-reads the window size, so a missed SIGWINCH cannot leave rows drawn wider than the window, and the poll stops on exit", (() => { const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8"); return /sizePoll = setInterval\(\(\) => \{ try \{ out\._refreshSize\(\); \} catch \(_\) \{\} \}, 500\)/.test(src) && /if \(sizePoll\) \{ clearInterval\(sizePoll\); sizePoll = null; \}/.test(src) && src.indexOf('out.on("resize", onResize);') < src.indexOf("sizePoll = setInterval"); })()); ok("an untrusted repo's .nexus/policy.json may tighten blockSecrets/audit but not switch them off", (() => { const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8"); return /const relax = isRepoTrusted\(cwd\);/.test(src) && /local\[key\] !== undefined && \(relax \|\| local\[key\] === true\)/.test(src); })()); ok("workspace trust is bound to the content of mcp/hooks/permissions/policy: a change after /trust (git pull, agent edit) re-prompts; legacy path entries are pinned; Nexus's own /allow and /mcp writes keep trust", (() => { const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8"); return /const TRUST_FILES = \[".nexus\/mcp.json", ".mcp.json", ".nexus\/hooks.json", ".nexus\/permissions.json", ".nexus\/policy.json"\];/.test(src) && /return e\.digest === trustDigest\(cwd\) \? "ok" : "changed";/.test(src) && /if \(typeof e === "string"\) \{ trustRepo\(cwd\); return "ok"; \}/.test(src) && /const savePerms = \(\) => keepTrust\(cwd,/.test(src) && /const writeMcp = \(cfg\) => keepTrust\(cwd,/.test(src); })()); ok("read_file refuses a binary file and cuts a single over-long line (minified bundle) at the cap with a note instead of sending it whole", (() => { const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8"); return /is a binary file \(" \+ fs\.statSync\(fp\)\.size/.test(src) && /if \(_k === _off && _ls\[_off - 1\]\.length > CAP\) result = \{ content: _ls\[_off - 1\]\.slice\(0, CAP\)/.test(src) && /_txt\.slice\(0, 8000\)\.indexOf\("\\u0000"\) >= 0\) return \{ error:/.test(src); })());