GitNexus/gitnexus/hooks
ming1523 b7d6db29ed
fix(cursor): keep cold npx hooks within host deadlines (#3799)
* fix(cursor-hook): keep npx fallback under the postToolUse host budget

hooks.json grants postToolUse a 10s budget. The npx fallback branch
added a flat +5s on top of the inner 7s spawnSync budget, reaching
12s — past the host deadline. When the local gitnexus package is
absent and npx cold-starts, Cursor kills the hook before the child
finishes, so the augmentation result is always lost on exactly the
machines the fallback exists for.

Cap the fallback at 10000 - 2000 = 8000ms (host budget minus
headroom for node startup and the final stdout write) via
Math.min, and add a source-level regression test asserting the cap
wiring so the fallback can never exceed the host budget again.

* style: collapse Math.min call to satisfy prettier formatting

* fix(cursor-hook): size npx fallback from the real host budget and KILL the npx tree

Review follow-up on #3292 (P1 + 2xP2):

- hooks.json now ships postToolUse timeout 60s: a cold `npx -y gitnexus`
  (download + install) can never fit inside 10s, so shrinking the child
  timer could only convert a late success into a guaranteed failure.
- runGitNexusCli derives the npx budget at call time: hooks.json seconds
  x1000, minus 5s headroom for node startup and the stdout write, minus
  the hook's own elapsed time — and floors at 1s.
- The npx grandchild (npx -> gitnexus) is wrapped in a coreutils
  `timeout -s KILL -k 1` guard resolved from standard locations
  (GITNEXUS_HOOK_TIMEOUT_PATH override / disabled honored), mirroring the
  Claude adapter: a plain SIGTERM killed only the obedient npx parent and
  left the grandchild holding the LadybugDB lock after the hook released
  its slot. Windows stays unwrapped (npx is a .cmd there).
- The regression tests now read the shipped hooks.json, assert the
  budget derivation is actually wired into the spawn (sliced function
  body), and assert the -s KILL wrap instead of detached substring
  checks.

* test(cursor-hook): accept either quote style in the -s KILL assertions

CI runs prettier before vitest, which normalizes the wrapped spawnSync
arguments to single quotes; the double-quoted substring assertions then
fail on all platforms. Match both quote styles instead.

* fix(cursor-hook): self-test the guard, size npx budget from the real host window

Four fixes to the Cursor postToolUse hook's npx fallback.

F1 — Homebrew installs `gtimeout`, not `timeout`. The builtin candidate
list named `/opt/homebrew/bin/timeout` and `/usr/local/bin/timeout`, which
exist on no Homebrew install, so on Apple Silicon the `-s KILL`
orphan-containment wrap silently never engaged. Adopt the canonical list:
`/usr/bin/timeout`, `/bin/timeout`, `/opt/homebrew/bin/gtimeout`,
`/usr/local/bin/gtimeout`.

F2 — candidates were adopted on mere existence. `GITNEXUS_HOOK_TIMEOUT_PATH`
pointing at an always-exit-0 binary passed `existsSync`, and the wrapped
spawn then exited 0 without ever running npx — status 0 and empty stderr
satisfy main()'s success check, so the hook emitted no augmentation at all.
Port `passesGuardSelfTest` from hook-db-lock-probe.cjs: a candidate is
adopted only when `timeout -k 1 1 /bin/sh -c 'exit 42'` exits 42, i.e. it
both runs the wrapped command and propagates its status. This also rejects
busybox <1.34 / toybox, which lack `-k`. The override is resolved with
`path.resolve` because runGitNexusCli spawns it with the tool request's cwd.
Resolution is memoized per hook process.

F3 — `Math.max(1000, ...)` handed the guard 1000ms of an already-expired
host window (3000ms with the spawnSync allowance), so the host killed the
hook before the guard returned or the final stdout write landed. Skip the
npx spawn when no usable budget remains; return the same shape main()
already handles so the hook slot is still released.

F4 — `timeout + 5000` capped every npx fallback at 12s (main passes 7000),
defeating the 60s hooks.json manifest that exists to accommodate the cold
`npx -y gitnexus` install. Size the npx arm from the remaining host budget
instead, clamped by a documented max so one slow install cannot hold a
per-repo hook slot for the whole window.

Tests: pin the Homebrew gtimeout candidates, the always-exit-0 stub
rejection, the exhausted-budget skip, and the uncapped budget. The budget
arithmetic and the guard resolver are now exported so the tests exercise
them directly instead of matching substrings.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix: address reviewer feedback for #3292

---------

Co-authored-by: luyua9 <luyua9@foxmail.com>
Co-authored-by: haimingZZ <haimingz@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-02 09:07:27 +00:00
..
antigravity feat(storage): add configurable index storage and content retention tiers (#3060) 2026-09-12 20:31:55 +00:00
claude fix(cursor): keep cold npx hooks within host deadlines (#3799) 2026-11-02 09:07:27 +00:00
opencode feat(opencode): ship hooks as auto-loaded global plugin for full support (#3619) 2026-10-28 13:45:08 +00:00