perf(context): memoize idempotent git/gh probes within process#586
Merged
NagyVikt merged 1 commit intoMay 16, 2026
Merged
Conversation
Add a process-scoped cache in src/context.js that wraps cp.spawnSync via a
new cachedSpawn helper, then route git/index.js and doctor/index.js spawns
through it. Cache lifetime is the lifetime of one Node process (a single
gx invocation) -- no disk cache, no TTL beyond process exit.
The allowlist is intentionally narrow and only covers probes whose answer
is invariant within a single gx run under gx's own writes:
* git rev-parse --show-toplevel | --git-common-dir | --git-dir |
--show-cdup | --show-superproject-working-tree | --is-inside-work-tree |
--is-inside-git-dir | --is-bare-repository | --show-prefix
* git --version, gh --version
* which / command / type lookups
Everything else (git status, diff, for-each-ref, rev-list, ls-files,
worktree list, branch --show-current, show-ref, merge-base, config --get,
remote get-url, ls-remote, gh auth status, gh pr/repo/issue view/list,
gh api *) falls through to a real spawn each time. Doctor and similar
flows mutate refs, the index, and config mid-run, so caching those reads
would feed stale answers to later steps.
Writes are never cached: anything taking stdin, anything with non-pipe
stdio, every git write verb (commit, push, pull, fetch, merge, rebase,
checkout, switch, reset, restore, clean, stash, tag, add, rm, mv, apply,
worktree add/remove/prune, branch -d/-D, config set/unset, submodule
update), every gh write verb (pr create/merge/close/edit/review/comment,
issue *, release *, repo create/delete, auth login/logout, api -X
POST/PATCH/PUT/DELETE) is rejected by the allowlist.
Honors two env knobs:
* GUARDEX_PROBE_TRACE=1 prints [probe] / [probe-hit] lines on stderr.
Made it possible to count duplicate spawns in gx doctor (23 baseline
spawnSync calls -> 18 real spawns + 5 cache hits) without altering
user-facing output.
* GUARDEX_PROBE_CACHE=0 disables the cache entirely (escape hatch).
Verification: node --test test/*.test.js shows the same set of pre-
existing failures vs an unmodified tree (no new regressions). gx doctor
and gx status produce identical user-facing output before and after.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Automated by gx branch finish (PR flow).