✨ feat(pre-push): niyam pre-push refuses a push to a branch whose pull request merged

Merged
AT
3d ago
+148-19

Slice: #583

Part of #394 (spec v1, lesson push-to-merged-branch). Decision D1: it refuses from the start.

What it does

  • niyam pre-push (src/work.ts, prePush) runs refuseRetired from src/pr-autocreate.ts as it is, with --root and --remote. Nothing is copied, and pr-autocreate.ts is unchanged. The check runs right after the identity guard, before the hand-off gate, the work rules and any feature check.
  • A push to origin of a branch whose pull request merged, with none open, is refused. It prints refuse-retired's message and the client's prAutocreate.retire command when one is set, then pre-push: refused; a merged pull request retired this branch. It judges by retiredBy: a branch with an open pull request is live, and one with only closed pull requests was never retired.
  • GitHub is asked once per judged branch, with the session's own token (sessionToken), never gh's signed-in account. The base, protected and skipped branches are not judged, as in refuse-retired. When GitHub cannot be asked, the session has no token, or the check crashes, one warning line says so and the push goes on to the work rules.
  • src/run.ts passes git's remote name (the hook's $1) as well as the URL, because refuse-retired judges only pushes to origin.
  • README: the pre-push section and the refuse-retired paragraph say so.

Tests (src/work.test.ts, "a push to a branch whose pull request merged"):

  • a retired branch is refused with the exact message, before any git log, after one listing on the agent's token;
  • the client's retire command is named;
  • a branch with an open pull request, or with only closed ones, passes;
  • a GitHub error (502, a thrown request) or a crash warns in one line and passes;
  • another remote, a deletion and the base branch ask nothing.

FakeSys now answers git config --get remote.origin.url and lists no pull requests for a branch unless a test routes them. Two existing tests now expect the new step: the personal-repository pre-push test (one more request) and the no-GH_TOKEN test (one warning line).

Evidence
One run of git push --dry-run from a clone of https://github.com/veda-hq/niyam-sandbox in an agent session (CLAUDECODE=1, NIYAM_AGENT=AthyaBot), with a new commit on each branch's remote tip. git runs the pre-push hook on a dry run too. The hook ran this branch's dist/cli.js pre-push "$@", as onboard's hook runs the released CLI.

env: CLAUDECODE=1 NIYAM_AGENT=AthyaBot
== git push --dry-run origin HEAD:refs/heads/w56-2-fix   (a commit on top of 48a9ba7)
❌ Error: w56-2-fix already has MERGED PR #35 and no open PR. Pushing it
   recreates a branch the merge retired, and the commits after the merge land in no PR.
   Move the commits missing from the PR head to a fresh branch, then push that.
pre-push: refused; a merged pull request retired this branch
error: failed to push some refs to 'https://github.com/veda-hq/niyam-sandbox.git'
exit: 1
== git push --dry-run origin HEAD:refs/heads/w56-1-proof   (a commit on top of d8ff5e8)
✗ #583 is not an issue in veda-hq/niyam-sandbox
pre-push: refused by the work rules
error: failed to push some refs to 'https://github.com/veda-hq/niyam-sandbox.git'
exit: 1

The full gate passed locally on this commit: pnpm provenance && pnpm typecheck && pnpm lint && pnpm test && pnpm test:scripts && pnpm pins && pnpm pariksha && pnpm hooks:check && pnpm build && pnpm smoke.

Cost: one GitHub list per pushed branch on every agent push. athya_mono asks twice until athya-rules drops its own refuse-retired call, which is out of scope here (spec v1, "Not in this Work item").

Alok approved the slice with "Decision: approve spec v1 #583 … #590" on https://github.com/veda-hq/niyam/issues/394 (2026-11-24).

🤖 Generated with Claude Code