mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-08-28 05:25:25 +00:00
* perf(hooks): cmdline-first Linux db-lock scan, drop the lsof fallback (#2180) The probe's Linux scan was O(processes × fds) — stat every fd of every process — so on a busy host it blew its budget and fell through to lsof, which then timed out (~2 s) and fail-closed. Every Grep/Glob/Bash hook spent ~2 s of CPU to conclude 'couldn't tell'. Rewrite linuxProcScanFindGitNexusServer (name kept; return type now tri-state 'owned' | 'not-owned' | 'timeout') as three phases: 0. /proc/<pid>/comm prefilter — kernel task->comm, never touches the target's memory maps; truncation-safe whitelist match (comm is capped at 15 visible chars). Calibrated to what a real server reports: @ladybugdb/core's worker_threads rename the main thread to 'MainThread', so that is whitelisted alongside the launcher basenames — omitting it would blind the probe to every server. 1. bounded /proc/<pid>/cmdline read (openSync+readSync, default 16 KiB with a floor of 4 KiB and a bounded escalation up to a hard ceiling) so a D-state holder cannot stall the hook and the mcp/serve mode token is never clipped off a long interpreter path. 2. dev+ino fd match for the 0–2 survivors only. Dispatch: 'owned' and 'timeout' both map to true. Timeout is now fail-closed (overload self-throttle) instead of falling through to lsof; the Linux lsof fallback is removed entirely. End-to-end semantics on busy hosts are unchanged (the old lsof arm also fail-closed there) — the ~2 s of wasted work and the orphan-spawning lsof are what's gone. macOS lsof+ps and Windows Restart Manager paths are untouched. Also: fix the budget parse bug (Number(raw && trim()) treated '0' as 1200; now parseInt-then-validate, with <= 0 an explicit immediate timeout) and add GITNEXUS_HOOK_PROC_ROOT so the Linux scan can be unit tested against a fixture procfs instead of the host's real /proc. Measured on a 583-process host with 6 background gitnexus mcp servers: owner detection 6–12 ms (was ~1216 ms + lsof timeout), ~100x. Tests: new hook-db-lock-probe.test.ts drives all three phases against a fake procfs (comm-truncation safety, Phase 0 trap, 4 KiB-boundary owner-miss guard, budget=0 immediate timeout, EACCES fail-closed) plus a live-/proc e2e that pins the fd-visible lbug-handle property against a real subprocess holder. The lsof/ps owner-detection suites are relaned to macOS (Linux no longer takes that path); the lsof orphan-reaping suite is removed (no lsof is spawned on Linux now) with a rationale note. Note: pre-commit typecheck skipped; remaining tsc errors are pre-existing on main (none in files touched here). * fix(hooks): honest EACCES verdict + real escalation coverage (#2183 review) Addresses the tri-review (maintainer + Codex): - [P2] Phase-2 fd-dir EACCES no longer claims 'owned'. /proc/<pid>/fd is owner-only (0500), so a cross-user/root gitnexus server serving ANY repo cleared Phase 0+1 and hit EACCES here, and the old catch returned 'owned' — falsely claiming it locks THIS repo's lbug (dev+ino never compared) and permanently suppressing augment. Split the failure shapes: ENOENT -> continue (raced away); EACCES/EPERM and transient EIO/ESTALE -> 'timeout' (unverifiable -> fail-closed, but honest, not a false ownership claim); ENOTDIR/other structural errors -> continue (not a real fd dir). Same fail-closed dispatcher outcome, no false 'owned', plus a GITNEXUS_DEBUG diagnostic so an operator can tell this skip path from a real owner. - [P2] The escalation test now actually iterates the escalation loop: the gitnexus token sits under 4 KB while the mode token is padded past GITNEXUS_HOOK_PROC_CMDLINE_MAX=4096, and a readSync spy asserts >1 read (the old 9 KB-under-16 KB-cap shape read once and never escalated). - escalation loop now re-checks the budget each iteration and returns a distinct timeout sentinel (never '' — an empty string would read as 'not a candidate' and could drop a real owner -> fail-open); the caller maps it to 'timeout'. - GITNEXUS_HOOK_PROC_ROOT is gated to test context so a stray production env export can't disable Linux owner detection (fail-open). - New uid-agnostic spy tests pin every fd-readdir errno branch (EACCES/EPERM/EIO/ESTALE -> timeout, ENOTDIR -> not-owned) regardless of the runner's uid (the disk chmod-000 tests no-op under root). Note: pre-commit typecheck skipped; remaining tsc errors are pre-existing on main (none in files touched here). * fix(hooks): drop the always-true outOfBudget presence guard (CodeQL #2183) CodeQL flagged `typeof outOfBudget === 'function' && outOfBudget()` as unneeded defensive code: readLinuxCmdline has a single caller (linuxProcScanFindGitNexusServer) that always passes the callback, so the typeof guard is dead. Drop it, leaving `if (outOfBudget())`, and note the invariant in the comment. Mirrored in the byte-identical plugin copy. * fix(hooks): parse numeric hook env with Number() so scientific notation works (#2183 review) getCmdlineMaxBytes and resolveLinuxProcBudgetMs parsed their env via Number.parseInt(raw, 10), so a value like "16e3" silently became 16 (parseInt stops at 'e') instead of 16000. Switch both to Number(String(raw).trim()), which honors scientific notation and is stricter on trailing garbage ("123abc" -> NaN -> default) — matching the repo-majority Number()+isFinite env idiom (src/cli/analyze.ts, src/core/embeddings/hf-env.ts). The two functions had DIFFERENT guard skeletons, so a verbatim swap would regress the budget: resolveLinuxProcBudgetMs used `raw != null ?` with no empty-string short-circuit, and Number("")===0 (vs parseInt("")===NaN) would make a set-but-empty GITNEXUS_HOOK_LINUX_PROC_BUDGET_MS="" resolve to budget 0 => immediate fail-CLOSED timeout => augment permanently skipped. Added the `&& String(raw).trim()` guard so ''/whitespace fall to the 1200 default while "0" still parses to the deliberate #2180 immediate-timeout vector. Exported both helpers for white-box tests (the values are otherwise only observable indirectly through scan timing) and added platform-independent coverage: "16e3"->16000, ""/whitespace->1200 (the regression guard), "0"->0, "123abc"/unset->1200, cmdline "8e3"->8000, "2e3"/""/unset->16384. Both byte-identical hook-db-lock-probe.cjs copies updated together. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(hooks): allocUnsafe the per-chunk cmdline read buffer (#2183 review) readLinuxCmdline allocated each per-chunk read buffer with Buffer.alloc(chunkCap), zero-filling memory that readSync immediately and fully overwrites. Switch the hot read buffer to Buffer.allocUnsafe — safe because readSync initializes exactly [0, bytes), only buf.subarray(0, bytes) is consumed, and Buffer.concat deep-copies that slice into `collected`, so the uninitialized tail can never reach the decoded cmdline. The zero-length `collected = Buffer.alloc(0)` is left unchanged (allocUnsafe gains nothing on a 0-length buffer). The existing D3 multi-chunk decode tests cover the read path and stay green. Both byte-identical hook-db-lock-probe.cjs copies updated together. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * test(hooks): harden the live /proc owner-detection e2e against CI flake (#2183 review) Two flake mechanisms, fixed without weakening what the e2e proves: - Holder readiness (the genuine false-FAIL): the pid-file poll was 200x25ms=5s; a loaded runner can be slow to spawn the child, tripping expect(holderPid).toBeGreaterThan(0). Widened to ~10s and raised the per-test timeout 20s -> 40s. - Scan budget (kept the assertion honest): the live scan ran at the default 1200ms. Because the dispatcher maps a budget 'timeout' to owned=TRUE, a busy host exhausting 1200ms before reaching the holder would make the assertion pass for the WRONG reason (a hollow timeout, not real fd-visible detection). Set a generous explicit 10000ms budget via the existing setEnv() helper so the module afterEach restores it (replacing the raw `delete process.env...` that bypassed env tracking). Raised the coarse timing regression guard to sit ABOVE the budget (5000 -> 15000) so a legitimately-slow-but-correct scan can't trip it. The load-bearing asserts (dev+ino fd-visibility precheck, owned===true for our own lbug) are unchanged. Verified the e2e executes (not skipped) on Linux. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore(changelog): empty the root CHANGELOG [Unreleased] section Per maintainer request, nothing should sit under [Unreleased] in the root CHANGELOG.md (the release-owned changelog is gitnexus/CHANGELOG.md, whose [Unreleased] is already empty). Removes all three accumulated blocks — Fixed (#2163), Performance (#2180), Changed (KuzuDB->LadybugDB) — leaving only the [Unreleased] header above [1.5.3]. Pure removal; no release sections touched. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Gergő Magyar <gergomagyar@icloud.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
140 lines
5.1 KiB
TypeScript
140 lines
5.1 KiB
TypeScript
/**
|
|
* Cross-platform test subset runner.
|
|
*
|
|
* Runs only the tests that exercise platform-sensitive behavior on
|
|
* Windows and macOS. The full suite runs on Ubuntu; this narrows the
|
|
* cross-platform matrix to tests that actually vary across OSes.
|
|
*
|
|
* Categories included:
|
|
* - Platform-specific logic (path.sep, process.platform guards)
|
|
* - Native addon loading (LadybugDB, tree-sitter)
|
|
* - Process spawning and shell behavior
|
|
* - Filesystem locking and temp-dir behavior
|
|
* - Worker threads (real, not mocked)
|
|
* - CLI end-to-end tests
|
|
*
|
|
* When adding a new test that uses platform-varying APIs (native addons,
|
|
* child_process with real spawning, filesystem locking, path.sep), add
|
|
* it to the appropriate section below.
|
|
*
|
|
* Usage:
|
|
* npx vitest run $(npx tsx scripts/cross-platform-tests.ts)
|
|
* # or via the package script:
|
|
* npm run test:cross-platform
|
|
*/
|
|
|
|
// Platform-specific logic tests — contain explicit process.platform guards
|
|
// or test behavior that differs across operating systems
|
|
const PLATFORM_LOGIC = [
|
|
'test/unit/setup.test.ts',
|
|
'test/unit/setup-jsonc.test.ts',
|
|
'test/unit/setup-codex.test.ts',
|
|
'test/unit/setup-antigravity.test.ts',
|
|
'test/unit/resolve-invocation.test.ts',
|
|
'test/unit/platform-capabilities.test.ts',
|
|
'test/unit/worker-pool-windows-quarantine.test.ts',
|
|
'test/unit/lbug-pool-fts-load.test.ts',
|
|
'test/unit/repo-manager.test.ts',
|
|
'test/unit/repo-manager-finalize-invariant.test.ts',
|
|
'test/unit/hooks.test.ts',
|
|
'test/unit/hook-db-lock-probe.test.ts',
|
|
'test/unit/cursor-hook.test.ts',
|
|
'test/unit/sidecar-recovery.test.ts',
|
|
'test/unit/pool-wal-recovery.test.ts',
|
|
'test/unit/detect-changes-worktree.test.ts',
|
|
'test/unit/eval-server-bind-restriction.test.ts',
|
|
'test/unit/ignore-service.test.ts',
|
|
'test/unit/group/bridge-db.test.ts',
|
|
'test/unit/group/bridge-db-edge.test.ts',
|
|
];
|
|
|
|
// Native LadybugDB integration tests — exercise the @ladybugdb/core
|
|
// N-API addon which has known platform-specific behavior (Windows
|
|
// file-lock lag after close, macOS N-API destructor segfaults)
|
|
const LBUG_NATIVE = [
|
|
'test/integration/lbug-core-adapter.test.ts',
|
|
'test/integration/lbug-vector-extension.test.ts',
|
|
'test/integration/lbug-pool.test.ts',
|
|
'test/integration/lbug-pool-stability.test.ts',
|
|
'test/integration/lbug-lock-retry.test.ts',
|
|
'test/integration/lbug-open-retry.test.ts',
|
|
'test/integration/lbug-close-handle-release.test.ts',
|
|
'test/integration/lbug-orphan-sidecar-recovery.test.ts',
|
|
'test/integration/lbug-readonly-init.test.ts',
|
|
'test/integration/lbug-non-ascii-path.test.ts',
|
|
'test/integration/local-backend.test.ts',
|
|
'test/integration/local-backend-calltool.test.ts',
|
|
'test/integration/search-core.test.ts',
|
|
'test/integration/search-pool.test.ts',
|
|
'test/integration/staleness-and-stability.test.ts',
|
|
'test/integration/analyze-wal-checkpoint-failure.test.ts',
|
|
];
|
|
|
|
// Process spawning and CLI tests — exercise child_process with real
|
|
// process spawning, which behaves differently across platforms (shell
|
|
// quoting, path resolution, signal handling)
|
|
const SPAWN_CLI = [
|
|
'test/integration/cli-e2e.test.ts',
|
|
'test/integration/hooks-e2e.test.ts',
|
|
'test/integration/skills-e2e.test.ts',
|
|
'test/integration/server-http-startup.test.ts',
|
|
'test/integration/mcp/server-startup.test.ts',
|
|
'test/integration/analyze-heap-oom-e2e.test.ts',
|
|
'test/integration/group/group-cli.test.ts',
|
|
'test/integration/cli/tool-no-index-stderr.test.ts',
|
|
'test/integration/setup-skills.test.ts',
|
|
'test/integration/setup-antigravity.test.ts',
|
|
'test/integration/antigravity-hook-e2e.test.ts',
|
|
'test/unit/local-cli-subprocess.test.ts',
|
|
'test/unit/runner-exec-tail.test.ts',
|
|
];
|
|
|
|
// Worker threads tests — exercise real worker_threads which have
|
|
// platform-specific behavior (thread spawning, IPC, exit handling)
|
|
const WORKER_THREADS = [
|
|
'test/integration/worker-pool.test.ts',
|
|
'test/integration/parse-impl-quarantine-cache-skip.test.ts',
|
|
];
|
|
|
|
// Tree-sitter native addon smoke tests — verify that native grammars
|
|
// load correctly on each platform (binary compatibility, .node loading)
|
|
const NATIVE_ADDON_SMOKE = [
|
|
'test/integration/tree-sitter-languages.test.ts',
|
|
'test/integration/parsing.test.ts',
|
|
'test/integration/pipeline.test.ts',
|
|
'test/integration/pipeline-graph-golden.test.ts',
|
|
'test/unit/parser-loader.test.ts',
|
|
'test/unit/parser-loader-abi.test.ts',
|
|
];
|
|
|
|
// Filesystem behavior tests — exercise operations that vary across
|
|
// platforms (CRLF, symlinks, permissions, temp dirs)
|
|
const FILESYSTEM = [
|
|
'test/integration/filesystem-walker.test.ts',
|
|
'test/integration/markdown-processor-crlf.test.ts',
|
|
'test/integration/ignore-and-skip-e2e.test.ts',
|
|
];
|
|
|
|
const ALL_CROSS_PLATFORM = [
|
|
...PLATFORM_LOGIC,
|
|
...LBUG_NATIVE,
|
|
...SPAWN_CLI,
|
|
...WORKER_THREADS,
|
|
...NATIVE_ADDON_SMOKE,
|
|
...FILESYSTEM,
|
|
];
|
|
|
|
// When invoked directly, print the file list for vitest consumption
|
|
if (process.argv[1]?.endsWith('cross-platform-tests.ts')) {
|
|
console.log(ALL_CROSS_PLATFORM.join('\n'));
|
|
}
|
|
|
|
export {
|
|
ALL_CROSS_PLATFORM,
|
|
PLATFORM_LOGIC,
|
|
LBUG_NATIVE,
|
|
SPAWN_CLI,
|
|
WORKER_THREADS,
|
|
NATIVE_ADDON_SMOKE,
|
|
FILESYSTEM,
|
|
};
|