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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions darknode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5023,7 +5023,32 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) {
// Redact secrets out of every transcript block's string fields before the
// session is written to disk (NSAFE-060). The in-memory transcript keeps the
// real text for this session; only the durable session.json copy is redacted.
const redactBlock = (b) => { if (!b || typeof b !== "object") return b; let c = null; for (const k of Object.keys(b)) { if (k !== "role" && typeof b[k] === "string" && b[k]) { const r = Redact.emit("transcript", b[k]); if (r !== b[k]) { c = c || Object.assign({}, b); c[k] = r; } } } return c || b; };
// session.json size control: tool output dominates a long session (one 16MB result was
// seen), and every save re-serializes + re-redacts all of it. Saved copies keep the
// head and tail of an oversized result; the live transcript keeps the full text.
const SAVE_CAP = 8000, OLD_CAP = 1500, RECENT = 40; // blocks older than the last RECENT get the tighter cap
const capText = (t, cap = SAVE_CAP) => t.length <= cap ? t : t.slice(0, Math.round(cap * 0.7)) + "\n[... " + (t.length - Math.round(cap * 0.95)) + " chars trimmed when the session was saved ...]\n" + t.slice(-Math.round(cap * 0.25));
const capResult = (v, cap = SAVE_CAP) => { if (v == null) return v; if (typeof v === "string") return capText(v, cap); let j = ""; try { j = JSON.stringify(v) || ""; } catch (_) {} return j.length > cap ? capText(asText(v), cap) : v; };
const saveRedact = (s) => Redact.emit("transcript", s);
// Nested items (tool result/input/diff, text/thinking) were never redacted. Each saved
// item copy is cached until the item changes, so a save only re-processes new output.
const _saveCache = new WeakMap();
const saveItem = (it, cap) => {
if (!it || typeof it !== "object") return it;
const h = _saveCache.get(it), fl = typeof it.full === "string" ? it.full.length : -1;
if (h && h.cap === cap && h.status === it.status && h.fl === fl && h.result === it.result && h.input === it.input && h.diff === it.diff && h.detail === it.detail && h.label === it.label) return h.out;
let c = null; const set = (k, v) => { if (v !== it[k]) { c = c || Object.assign({}, it); c[k] = v; } };
for (const k of Object.keys(it)) {
const v = it[k];
if (k === "result") { let r = capResult(v, cap); if (typeof r === "string") r = saveRedact(r); else if (r != null) { const t = asText(r), rt = saveRedact(t); if (rt !== t) r = rt; } set(k, r); }
else if (k === "input" && v && typeof v === "object" && !Array.isArray(v)) { let o = null; for (const ik of Object.keys(v)) if (typeof v[ik] === "string" && v[ik]) { const r = saveRedact(capText(v[ik], cap)); if (r !== v[ik]) { o = o || Object.assign({}, v); o[ik] = r; } } if (o) set(k, o); }
else if (typeof v === "string" && v && k !== "type" && k !== "id" && k !== "name" && k !== "status") set(k, saveRedact(k === "diff" || k === "input" ? capText(v, cap) : v));
}
const out = c || it;
if (it.status !== "run") _saveCache.set(it, { cap, status: it.status, fl, result: it.result, input: it.input, diff: it.diff, detail: it.detail, label: it.label, out });
return out;
};
const redactBlock = (b, i, arr) => { if (!b || typeof b !== "object") return b; let c = null; for (const k of Object.keys(b)) { if (k !== "role" && typeof b[k] === "string" && b[k]) { const r = saveRedact(b[k]); if (r !== b[k]) { c = c || Object.assign({}, b); c[k] = r; } } } if (Array.isArray(b.items)) { const cap = arr && i < arr.length - RECENT ? OLD_CAP : SAVE_CAP, ni = b.items.map((it) => saveItem(it, cap)); if (ni.some((x, i) => x !== b.items[i])) { c = c || Object.assign({}, b); c.items = ni; } } return c || b; };
const restoreSession = (s) => {
transcript.length = 0;
for (const b of s.transcript || []) {
Expand All @@ -5033,7 +5058,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) {
// render frozen/truncated forever since no turn is driving it. Make history final.
if (b && Array.isArray(b.items)) for (const it of b.items) {
if (it.type === "text") it.shown = (it.full || "").length;
else if (it.type === "tool" && it.status === "run") { it.status = "ok"; if (!it.end) it.end = it.start || Date.now(); }
else if (it.type === "tool") { if (it.status === "run") { it.status = "ok"; if (!it.end) it.end = it.start || Date.now(); } if (it.result != null) it.result = capResult(it.result); } // older saves hold untrimmed output
}
transcript.push(b);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"scripts": {
"start": "node darknode.js",
"build": "pkg . --out-path dist",
"test": "node test/run.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",
"test": "node test/run.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",
"postinstall": "node scripts/postinstall.js",
"docs": "node scripts/gen-nexus-docs.js"
},
Expand Down
41 changes: 41 additions & 0 deletions test/session-save.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";
// session-save suite: the saved-transcript transform in darknode.js (size caps +
// nested-item redaction). Extracts the helper block so it tests the shipped code.
const fs = require("fs"), path = require("path");
const Redact = require("../lib/nexus/redact");
const { asText } = require("../lib/cli/tui-format");

let pass = 0, fail = 0;
const ok = (name, cond) => { if (cond) pass++; else { fail++; console.log(" \x1b[31mFAIL\x1b[0m " + name); } };

const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8");
const a = src.indexOf(" const SAVE_CAP = 8000,"), b = src.indexOf(" const restoreSession");
ok("found the save helpers", a > 0 && b > a);
const { redactBlock, capResult } = new Function("Redact", "asText", src.slice(a, b) + "\nreturn { redactBlock, capResult };")(Redact, asText);

const secret = "ghp_" + "A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8";
const big = "HEAD-" + "x".repeat(50000) + "-TAIL";
const mk = () => ({ role: "nexus", items: [
{ type: "tool", id: "t1", name: "Bash", status: "ok", input: { command: "echo " + secret }, result: big, detail: "d", label: "Bash" },
{ type: "text", full: "token " + secret, shown: 10 },
{ type: "tool", id: "t2", name: "Read", status: "ok", result: [{ type: "text", text: big }] },
] });

const blk = mk(), saved = redactBlock(blk, 0, [blk]);
const r0 = saved.items[0].result;
ok("oversized string result is capped", typeof r0 === "string" && r0.length < 9000);
ok("capped result keeps head and tail", r0.startsWith("HEAD-") && r0.endsWith("-TAIL") && r0.includes("chars trimmed when the session was saved"));
ok("oversized array result becomes capped text", typeof saved.items[2].result === "string" && saved.items[2].result.length < 9000);
ok("secret in nested tool input is redacted", !JSON.stringify(saved.items[0].input).includes(secret));
ok("secret in nested text is redacted", !saved.items[1].full.includes(secret));
ok("live transcript keeps the full result", blk.items[0].result === big && blk.items[1].full.includes(secret));
ok("unchanged items are reused across saves", redactBlock(blk, 0, [blk]).items[0] === saved.items[0]);

const arr = Array.from({ length: 50 }, mk), oldSaved = redactBlock(arr[0], 0, arr), newSaved = redactBlock(arr[49], 49, arr);
ok("older blocks get the tighter cap", oldSaved.items[0].result.length < 2000 && newSaved.items[0].result.length > 5000);

ok("small results pass through untouched", capResult("short") === "short" && JSON.stringify(capResult({ output: "x" })) === '{"output":"x"}');
ok("null result stays null", capResult(null) === null && capResult(undefined) === undefined);

if (fail) { console.log("session-save: " + pass + " passed, " + fail + " FAILED"); process.exit(1); }
console.log("session-save: " + pass + " passed");
Loading