Skip to content

fix(guard): resolve git branch per command target, not process cwd - #154

Merged
danielvm-git merged 1 commit into
danielvm-git:mainfrom
Juzverej:fix/guard-command-cwd
Oct 25, 2026
Merged

danielvm-git merged 1 commit into
danielvm-git:mainfrom
Juzverej:fix/guard-command-cwd

Conversation

@Juzverej

Copy link
Copy Markdown

Problem

The tool_call safety guard in extensions/omp-hooks.ts resolves the current git branch like this:

execSync("git rev-parse --abbrev-ref HEAD", { encoding: "utf-8", timeout: 3000 })  // no cwd

Without a cwd option, this reads the branch of the agent process working directory — the session root — rather than the repository the guarded bash command actually targets (cd <repo> && git commit … or git -C <repo> …).

Consequence: when a session is rooted in a parent repository that sits on a protected branch (main/master), every commit in every other repository is blocked, even on a feature branch:

BLOCKED: Direct commits to 'main' are forbidden. Use a feature branch.

This makes bigpowers unusable for the common monorepo / git-submodule layout where the agent session is rooted at a parent repo and works inside nested repositories. (It also silently under-protects the inverse case: a session rooted on a feature branch could commit directly to a nested repo's protected branch.)

Fix

  • commandGitCwd(command, upTo, fallback) — resolve the directory a git commit / git push occurrence will actually run in: the last cd <path> before it within the command prefix, overridden by the last git -C <path> anywhere in the command. Quoted paths (with spaces) supported; tokens containing command substitution ($, backticks) are declined.
  • currentBranch(cwd) — rev-parse with an explicit cwd.
  • Commit guard: every git commit occurrence in a chained command is evaluated against its own target repository (a chain touching two repos is judged per repo).
  • Push guard: the bare-push branch rule now uses the resolved target branch. The explicit-ref rule and Conventional Commits checks are unchanged.

Safety posture: fail-closed. If a path cannot be resolved (quoting/substitution edge cases, missing directory), the fallback is the process cwd — i.e. exactly the previous behavior. The patch can therefore only relax over-blocking; it cannot introduce an under-blocking path that did not already exist.

Verification

10 scenarios replayed against the patched functions (extracted verbatim from the installed file, executed through jiti) on throwaway fixtures with real git repositories:

# Scenario Result
T1 session root on protected branch, subproject on feature branch, cd + commit allowed (was: blocked)
T2 subproject itself on its protected branch, commit still blocked
T3 no cd, session root on protected branch, commit still blocked
T4 git -C <feature-branch-repo> commit allowed
T5 chain: commit in feature-branch repo, then commit in protected-branch repo blocked (per-occurrence)
T6 quoted path with a space, commit allowed
T7 unresolvable path fail-closed → blocked (previous behavior)
T8 bare push from a feature branch allowed
T9 explicit push of the protected ref still blocked
T10 non-conventional commit message in a feature-branch repo still blocked

All 10 pass. Also verified live: after /reload, a subproject commit landed from a parent-rooted agent session (the exact scenario that used to misfire).

⚠️ Disclosure

This change was generated by an AI coding agent (pi + bigpowers skills) and has been reviewed only by a hobbyist owner, not by a professional programmer. The verification above is machine-run and machine-reported. Please review with extra scrutiny — especially the regex path-parsing in commandGitCwd (known limits: no tilde expansion, no command-substitution parsing, git -Cfoo without a space is not recognized — all fall back fail-closed).

currentBranch() ran git rev-parse --abbrev-ref HEAD without a cwd option,
so the tool_call guard judged every commit and push against the agent
process working directory (the session root) instead of the repository the
bash command actually targets.

Consequence: with a session rooted in a parent repository that sits on a
protected branch, commits are blocked in every other repository — even on a
feature branch (entering another repo before committing misfires as a
direct commit to the protected branch). Monorepo and submodule layouts hit
this constantly.

Fix:

- commandGitCwd(command, upTo, fallback): resolve the directory a commit or
  push occurrence will run in — the last directory-change before it within
  the command prefix, overridden by the last git -C path in the command;
  quoted paths supported; command substitution declined.
- currentBranch(cwd): rev-parse with an explicit cwd.
- commit guard: evaluate EVERY commit occurrence in a chained command
  against its own target repo (per-occurrence protection).
- push guard: the bare-push branch rule now uses the resolved target
  branch.

Safety posture: fail-closed — unresolvable paths fall back to the process
cwd (previous behavior: over-blocks, never under-blocks). The explicit-ref
push rule and Conventional Commits checks are unchanged.

Verified with 10 scenarios (throwaway fixtures with real git repositories):
a parent session on a protected branch plus a subproject feature-branch
commit is now allowed; a subproject that is itself on its protected branch
is still blocked; a commit without a directory change on the session root
is still blocked; the -C flag is respected; multi-repo chains are checked
per occurrence; quoted paths with spaces work; an unresolvable path falls
back (fail-closed); a bare push from a feature branch is allowed; an
explicit push to the protected branch is still blocked; the
conventional-commit rule is intact.
@danielvm-git

Copy link
Copy Markdown
Owner

The commandGitCwd approach — scanning for cd <path> and git -C <path> tokens, handling quoted paths, evaluating each git commit in a chain against its own target — is meaningfully more robust than the problem description required. You fixed the bug and handled the edge cases. Thank you for the high-quality work.

@danielvm-git
danielvm-git merged commit 07c7498 into danielvm-git:main Oct 25, 2026
github-actions Bot pushed a commit that referenced this pull request Oct 25, 2026
## [2.88.11](v2.88.10...v2.88.11) (2026-10-25)

### Bug Fixes

* **guard:** resolve git branch per command target, not process cwd ([#154](#154)) ([07c7498](07c7498))
* **lockfile:** add missing slash in skill path + self-contained resolve_repo_root ([#156](#156)) ([04bda15](04bda15))
* **omp-hooks:** inGitRepo bail-early, accept type!(scope) form, echo subject ([#157](#157)) ([9e1b11a](9e1b11a)), closes [#147](#147) [#151](#151) [#151](#151)

### Performance Improvements

* **sync:** render all skills in one bash process per adapter ([#155](#155)) ([1b56463](1b56463)), closes [#137](#137)
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.88.11 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants