Skip to content

perf(context): memoize idempotent git/gh probes within process#586

Merged
NagyVikt merged 1 commit into
mainfrom
agent/claude/perf-memoize-git-gh-probes-2026-05-16-02-45
May 16, 2026
Merged

perf(context): memoize idempotent git/gh probes within process#586
NagyVikt merged 1 commit into
mainfrom
agent/claude/perf-memoize-git-gh-probes-2026-05-16-02-45

Conversation

@NagyVikt
Copy link
Copy Markdown
Collaborator

Automated by gx branch finish (PR flow).

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.
@NagyVikt NagyVikt merged commit bb616db into main May 16, 2026
@NagyVikt NagyVikt deleted the agent/claude/perf-memoize-git-gh-probes-2026-05-16-02-45 branch May 16, 2026 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant