Skip to content

[Fix] omp-hooks.ts: Stop git guards from screaming outside repos #147

Closed

Description

@ak-a-ra

[Fix] omp-hooks.ts: Stop git guards from screaming outside repos

The problem

omp-hooks.ts was running git rev-parse --abbrev-ref HEAD on every bash command — even when you're not in a git repo. Result: stderr spam on every command:

fatal: not a git repository (or any parent up to mount point)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM)

Annoying, and it happens constantly when working outside repos.

Environment where observed: pi coding agent on Termux (Android)

Root cause

currentBranch() fired unconditionally inside the tool_call handler. No check for "am I even in a git repo?"

Fix

  1. Added inGitRepo(cwd) — runs git rev-parse --git-dir with stdio: "ignore" so it fails silently
  2. Early exit in the handler if not in a repo — zero git commands run
  3. Pass cwd from event.context?.cwd to all git calls

What changed in extensions/omp-hooks.ts

  • New inGitRepo(cwd?: string): boolean helper
  • currentBranch(cwd?: string) now checks inGitRepo first, returns "" if outside a repo
  • tool_call handler grabs cwd from event context, bails early if !inGitRepo(cwd)

Testing

  • Run any command outside a git repo → silent, no errors
  • Run git status inside a repo → guards work normally (dangerous patterns, protected branches, Conventional Commits)

Breaking changes

None. Inside repos, behavior is identical. Outside, it just shuts up.


Note: The guard-git skill's hook scripts (scripts/hooks/*/pre-tool-git-guard.sh) already handle this correctly. This brings omp-hooks.ts in line.

Activity

  1. tharanee-bit commented on Oct 17, 2026

    @tharanee-bit

    Also reproduced on macOS (Darwin 27, bigpowers 2.88.9, Pi). When Pi's working directory is not a Git repository, every bash tool call prints fatal: not a git repository (or any of the parent directories): .git over the Pi editor/status area.

    The cause is currentBranch() in extensions/omp-hooks.ts. It catches the exception, but execSync inherits stderr, so git's message still reaches the terminal. A one-line change fixes it for me and keeps branch detection working inside repos:

    return execSync("git rev-parse --abbrev-ref HEAD", {
      encoding: "utf-8",
      timeout: 3000,
      stdio: ["ignore", "pipe", "ignore"],
    }).trim();

    #146 had this exact change plus a regression test in scripts/omp-smoke.ts, but it was closed without review. Could #146 be reopened or the fix landed? Happy to help test.

  2. danielvm-git commented on Oct 25, 2026

    @danielvm-git
    Owner

    @ak-a-ra — Thank you for filing this from Termux on Android — that alone shows dedication. Your inGitRepo(cwd) approach is exactly right: a cheap rev-parse --git-dir with stdio:ignore makes the happy path zero-cost and silent. You made this better for every constrained or unusual environment. Applying your fix now.

  3. github-actions commented on Oct 25, 2026

    @github-actions

    🎉 This issue has been resolved in version 2.88.11 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  4. ak-a-ra commented on Oct 25, 2026

    @ak-a-ra
    Author

    Thanks for the quick response and for incorporating the fix! 🙏 I appreciate you addressing this issue and getting the fix into v2.88.11. I'll test the release on my Termux setup. Thanks again for maintaining Bigpowers!

  5. ak-a-ra commented on Oct 27, 2026

    @ak-a-ra
    Author

    Reproduced on Termux/Android with bigpowers 2.88.16 (current npm @latest) — so this is not the stale-install case, and it is not fully covered by the fix that shipped in 2.88.11.

    What I found

    The 2.88.11 fix added inGitRepo() + early exit, using git rev-parse --git-dir. That covers not-a-repo. It does not cover a repo with zero commits (unborn HEAD), because git rev-parse --git-dir succeeds there (exit 0, prints .git). The guard passes, then currentBranch() (extensions/omp-hooks.ts:207) still runs:

    return execSync("git rev-parse --abbrev-ref HEAD", {
      encoding: "utf-8",
      timeout: 3000,
      cwd,
    }).trim();

    Two consequences:

    1. stderr leaks — no stdio override, so git's fatal reaches the terminal on every bash tool call in a fresh repo, even for ls/echo. The sibling helper inGitRepo() already does this right (stdio: "ignore").
    2. protected-branch guard silently bypassed — the catch returns "unknown", so PROTECTED_BRANCHES / branch === "main" never match. First git commit on master/main in a brand-new repo is not blocked. This is the security-relevant half.

    Confirmed still unfixed on main today (currentBranch at line 207 has no stdio; handler lines 418–420 call it unconditionally once inGitRepo passes).

    This is also the exact leak flagged in the comment above (stdio: ["ignore","pipe","ignore"] on currentBranch) and in #146 — that half never landed.

    Proof

    Repro:

    D=$(mktemp -d) && cd "$D" && git init -q .   # zero commits => unborn HEAD
    git rev-parse --git-dir          # exit 0   => inGitRepo() passes
    git rev-parse --abbrev-ref HEAD  # exit 128, prints the fatal below
    fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'
    

    Ran the real tool_call handler (loaded via scripts/omp-smoke.ts-style harness) with process.chdir() into that unborn repo, before vs after the patch:

    currentBranch() impl stderr echo hi git commit -m "feat: initial"
    main (rev-parse --abbrev-ref HEAD) 2× the fatal above allowed allowed — guard missed
    patched (symbolic-ref --short -q HEAD + stdio: ["ignore","pipe","ignore"]) silent allowed BLOCKED: Direct commits to 'master'

    Regression test — core of that harness, drop into a scripts/omp-smoke.ts-style setup (handler = the registered tool_call listener, plus the node:fs / node:os / node:path / node:child_process imports it needs):

    // A git repo with zero commits: `rev-parse --git-dir` succeeds, so the
    // inGitRepo guard passes, but `rev-parse --abbrev-ref HEAD` exits 128.
    const repo = mkdtempSync(join(tmpdir(), "bigpowers-unborn-"));
    execSync("git init -q .", { cwd: repo, stdio: "ignore" });
    const previousCwd = process.cwd();
    process.chdir(repo);
    
    try {
      // Ordinary command must stay unblocked.
      if (handler({ toolName: "bash", input: { command: "echo hi" } }) !== undefined) {
        throw new Error("unborn HEAD: benign command was blocked");
      }
    
      // First commit on the unborn protected branch MUST be blocked.
      const commit = handler({
        toolName: "bash",
        input: { command: 'git commit -m "feat: initial"' },
      }) as { block?: boolean; reason?: string } | undefined;
    
      if (!commit?.block) {
        throw new Error(
          "unborn HEAD: first commit on protected branch was NOT blocked " +
            "(currentBranch resolved to 'unknown' instead of the branch name)",
        );
      }
      if (!/master|main/.test(commit.reason ?? "")) {
        throw new Error(`unborn HEAD: block reason lacks branch name: ${commit.reason}`);
      }
    } finally {
      process.chdir(previousCwd);
      rmSync(repo, { recursive: true, force: true });
    }

    Result: RED on main (first commit on protected branch was NOT blocked (currentBranch resolved to 'unknown' instead of the branch name)), GREEN with the patch (PASS). scripts/omp-smoke.ts still passes with the patch applied.

    What I patched

    extensions/omp-hooks.ts:207:

     function currentBranch(cwd: string): string {
       try {
    -    return execSync("git rev-parse --abbrev-ref HEAD", {
    -      encoding: "utf-8",
    -      timeout: 3000,
    -      cwd,
    -    }).trim();
    +    // `git symbolic-ref` — NOT `git rev-parse --abbrev-ref HEAD`.
    +    // In a repo with no commits yet (unborn HEAD), rev-parse exits 128 with
    +    // `fatal: ambiguous argument 'HEAD': unknown revision or path not in
    +    // the working tree.` on stderr. Because stderr was not suppressed here,
    +    // that fatal leaked into every bash tool call in a fresh repo, and the
    +    // caught failure also returned "unknown" — silently disabling the
    +    // protected-branch commit guard for first commits on main/master.
    +    // `symbolic-ref --short -q HEAD` resolves the branch name even when HEAD
    +    // is unborn, is silent on detached HEAD, and stderr is ignored.
    +    return execSync("git symbolic-ref --short -q HEAD", {
    +      encoding: "utf-8",
    +      timeout: 3000,
    +      cwd,
    +      stdio: ["ignore", "pipe", "ignore"],
    +    }).trim();
       } catch {
         return "unknown";
       }
     }

    Behavior of git symbolic-ref --short -q HEAD:

    repo state output exit
    unborn HEAD (0 commits) master 0
    normal master 0
    detached HEAD (empty) 1 → "unknown", same as before

    No change inside repos that already have commits; guards keep working on fresh ones.

    Where the patch currently lives: my installed copy, ~/.pi/agent/npm/node_modules/bigpowers/extensions/omp-hooks.ts — applied there to validate it. That edit is wiped by the next npm i -g bigpowers, which is why I'm reporting it rather than just fixing it locally.

    One extra finding from the same file, lower severity: line 418 uses process.cwd() for the repo gate rather than event.context?.cwd (as proposed earlier in this issue). Fails closed, but a bash command that cds into a different repo makes the gate answer with the wrong repo's branch. Worth folding into the same change.

    Happy to open the PR against main with the diff + regression test wired into scripts/omp-smoke.ts — just confirm and the story tag if you want one.

  6. added a commit that references this issue on Oct 28, 2026
    9e1b11a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions