From db4aea5e640b4cacdfb8603c12ceb99b46947ec3 Mon Sep 17 00:00:00 2001 From: Darknode-Official Date: Sat, 31 Oct 2026 17:46:37 -0700 Subject: [PATCH] Make the per-message undo checkpoint incremental (fixes ~1s Enter lag) Every message runs nexusCheckpoint() synchronously before it is sent. It built the snapshot with `git add -A` into a temp index that was deleted each time, so git had no stat cache and rehashed the entire working tree on every Enter - including .nexus/session.json, which can grow to tens of MB, before removing .nexus from the snapshot again. On a large repo with a long session that was ~1.3s per message. - keep the temp index between turns, seeded once from the repo's own index, so only changed files are rehashed; - exclude .nexus in the add pathspec so the session file is never hashed. Snapshots are byte-identical to before (verified, including after an edit, a new file and a deletion). Measured with a real 90 MB session: Enter to sent went from ~1350 ms to ~67 ms; checkpoint alone on a darknode-web clone from 290-1440 ms to ~16 ms. --- darknode.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/darknode.js b/darknode.js index f06ab0f..f3e2e76 100755 --- a/darknode.js +++ b/darknode.js @@ -2019,12 +2019,15 @@ function nexusCheckpoint(cwd) { _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 }); - try { fs.unlinkSync(idx); } catch (_) {} + // 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 + // never hashed. Recreating the index each time rehashed the whole tree: ~1s per Enter. + if (!fs.existsSync(idx)) { try { const real = _cp.execSync("git rev-parse --git-path index", { cwd, stdio: ["ignore", "pipe", "ignore"] }).toString().trim(); fs.copyFileSync(path.resolve(cwd, real), idx); } catch (_) {} } const env = Object.assign({}, process.env, { GIT_INDEX_FILE: idx }); - _cp.execSync("git add -A", { cwd, env, stdio: "ignore" }); + _cp.execSync("git add -A -- :/ \":(exclude).nexus\"", { cwd, env, stdio: "ignore" }); try { _cp.execSync("git rm -r --cached --ignore-unmatch --quiet .nexus", { cwd, env, stdio: "ignore" }); } catch (_) {} // never snapshot/restore Nexus's own state dir const tree = _cp.execSync("git write-tree", { cwd, env, stdio: ["ignore", "pipe", "pipe"] }).toString().trim(); - try { fs.unlinkSync(idx); } catch (_) {} return tree || null; } catch (_) { return null; } }