Skip to content

fix(redact): install-prepush-hook never refreshes a stale managed hook, so shipped wrapper fixes never reach an existing install - #2731

Closed
schienbiz wants to merge 1 commit into
garrytan:mainfrom
schienbiz:fix-refresh-stale-managed-prepush-hook
Closed

schienbiz wants to merge 1 commit into
garrytan:mainfrom
schienbiz:fix-refresh-stale-managed-prepush-hook

Conversation

@schienbiz

Copy link
Copy Markdown
Contributor

The bug

installPrepushHook returns before the only writeFileSync whenever the existing hook carries the managed marker:

if (existing.includes(MANAGED_MARKER)) {
  process.stdout.write("gstack-redact: pre-push hook already installed.\n");
  return;          // ← the only writer is below this line
}

There is no version stamp in the marker and no comparison against what the current version generates, so the wrapper a repo receives on its first install is the wrapper it keeps forever. No migration touches pre-push either (all 12 under gstack-upgrade/migrations/ checked), and ./setup does not call the command.

That has already cost one shipped security fix. _input="$(cat; printf x)" — the sentinel that stops a chained pre-push.local from silently failing open — landed in v1.64.0.0. Sixteen repos here got the hook before that and were still running _input="$(cat)" after upgrading v1.60.1.0 → v1.72.0.0. Eight versions, zero delivery. Measured, one shape per column, with a chained .local built on a bare while read:

installed wrapper refs the chained .local sees
$(cat) (pre-v1.64) 0
$(cat; printf x) (v1.64+) 1

Zero refs, exit 0 — the guard reports success having scanned nothing, which is precisely the failure printf x was added to close. bin/gstack-redact-prepush itself is unaffected (it reads stdin as readFileSync(0).split("\n"), so a missing trailing newline costs it no line), so this only bites the chained-hook seam.

The same freeze hits a second, non-security case: prepushBin is baked in as an absolute path. Move or reinstall gstack elsewhere and the stale wrapper keeps naming a bun script that is no longer there, with no way to repair it short of uninstall + install — and uninstall renames pre-push.local back over pre-push, so that route eats the user's chained hook.

This is the same shape as #2444 (link_codex_skill_dirs skipping already-installed skill dirs, "reports ready but never refreshes"), also filed against 1.60.1.0.

The fix

Hoist the wrapper construction above the existence check, then compare bodies:

  • existing === wrapper → already installed, no write, no mtime change (idempotent, as before).
  • marker present but body drifted → rewrite, print refreshed stale managed pre-push hook at <path>.
  • no marker → unchanged: preserve as pre-push.local and chain it.

pre-push.local is never read, renamed, or written on any of the three paths — it is the user's, not gstack's.

Content equality rather than a version stamp keeps the marker format untouched and means a wrapper is judged by what it actually is, not by what it claims. The trade-off, stated plainly: a hand-edited hook still carrying the managed marker is overwritten. That seems right — the marker is gstack's ownership claim and pre-push.local is the documented seam for user code — but the refresh says so on stdout rather than doing it silently, and I'll take a different policy if you prefer one.

One consequence worth naming: with both a global and a vendored gstack installed, whichever one runs the command last now owns the prepushBin path, where before it was frozen at whichever installed first. Last-writer-wins is the lesser evil against frozen-at-a-dead-path, but it is a behaviour change.

Tests

Two added to test/redact-prepush-hook.test.ts:

  • a stale managed hook is rewritten, and the refresh delivers the newline fix — plants the verbatim v1.63-era wrapper plus a bare-while read .local, runs install, and asserts the body gains printf x, the .local survives untouched, and — the half that matters — the refreshed wrapper actually delivers the final ref line to that .local.
  • install stays idempotent: an up-to-date managed hook is not rewritten — second run prints already installed, never refreshed, with byte-identical content and unchanged mtime.

Receipts, per the standard in the v1.69 notes — the first test fails on a scratch worktree of b5a951e for the right reason:

✗ install / chaining > a stale managed hook is rewritten, and the refresh delivers the newline fix
  Expected to contain: "refreshed stale managed pre-push hook"
  Received: "gstack-redact: pre-push hook already installed.\n"
  27 pass, 1 fail

With the fix: 28 pass, 0 fail in that file, and 200 pass, 0 fail across redact-prepush-hook, fs-utils, gstack-redact-cli, redact-engine, redact-engine-autoredact, redact-pattern-lint, redact-doc-resolver.

bun run test:free is not clean on this machine at b5a951e itself — I ran it on a pristine worktree of upstream HEAD to get a baseline before reading my own run. Baseline fails 22 tests (bun-polyfill spawn/serve, gstack-developer-profile --migrate, bump:/DRIFT-REPAIR, a browser-skills E2E); my branch fails a subset of the same set. The one name appearing on my side and not on the baseline's is codex-hardening's bash-native watchdog deadline test, which fails 1-in-3 on a pristine b5a951e and 1-in-3 on this branch, and contains zero references to redact. No test fails on this branch that does not also fail upstream.

Happy to add a CHANGELOG entry / VERSION bump if you'd rather this land as its own release than be absorbed into a wave.

The marker check returned before the only writer, so once a repo had the
hook, no later change to the wrapper could ever reach it. The `printf x`
fail-open fix (v1.64.0.0) has still not landed in any repo that received
the hook before it, and a wrapper naming a gstack that has since moved
stays pointed at a dead path for the same reason.

Compare the body against what this version generates: rewrite on drift,
stay a no-op when identical. The chained pre-push.local is untouched on
both paths.

The existing trailing-newline regression test cannot catch this — it
installs into a repo with no prior managed hook, the one case that was
never broken.
@trunk-io

trunk-io Bot commented Aug 29, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@capy-ai

capy-ai Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for this, @schienbiz. The fix is now on main: gstack-redact rewrites the managed pre-push hook when its body has drifted (fixed in v1.78.0.0, #2752). Closing as superseded; if you still see the problem on the latest gstack, please open an issue with ./setup --status output.

@capy-ai capy-ai Bot closed this Oct 3, 2026
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