Skip to content

fix(hooks): render CodeBuddy's Windows hooks in POSIX, not cmd.exe - #989

Merged
jeff-r2026 merged 2 commits into
Tencent:mainfrom
CarlosWonMore:fix/codebuddy-hooks-posix
Oct 8, 2026
Merged

jeff-r2026 merged 2 commits into
Tencent:mainfrom
CarlosWonMore:fix/codebuddy-hooks-posix

Conversation

@CarlosWonMore

@CarlosWonMore CarlosWonMore commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CodeBuddy runs its Windows hook commands through Git Bash, but teamai rendered them in cmd.exe syntax. Run by Git Bash, 2>nul is a file redirect, not the null device, so every hook invocation created a real 0-byte file named nul in the hook's working directory — and because the PostToolUse wildcard hook fires after every tool call, the project root got one continuously. The same rendering also made the PATH prepend a no-op and turned the deliberate "always exit 0" || exit /b 0 into an exit 2, which is exactly what makes CodeBuddy read a hook as allowed:false.

This makes CodeBuddy use the POSIX renderings — built-in dispatch and the team-hook project gate — like every other tool, and resolves its shell to Git Bash.

The gate that shipped with the first version of this PR still could not fire. It compares $PWD, and an MSYS shell spells a drive-letter path /c/proj, never the C:\proj the root resolves to — so every project-scoped team hook stayed disabled. That is the review finding below, fixed in the follow-up commit 01cb44e: the gate is now rendered in both spellings, and a real-shell execution test covers it. Replacing the original execution test with shape assertions is what had let the broken gate pass; that coverage is restored.

Supersedes #933 (closed unmerged). That attempt carried the same fix but was never merged; this rebases it onto main and resolves the conflicts. The core src/bundled-runtime.ts was untouched upstream then and is still conflict-free here.

Fixes #932.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

What changed

Drop the cmd.exe renderer. toolUsesCmdShell() and the cmd branch of gateTeamHookCommand() are gone; every tool now gets the POSIX gate. CodeBuddy resolves its hook shell through the Git Bash it already requires on Windows (resolveCodebuddyShell() → findGitBashWindows()), returning null — which correctly skips injection with the existing warning — when Git Bash is absent, instead of installing hooks that could never run.

Render the gate root in the shell's spelling (01cb44e). msysProjectRoot() rewrites a drive-letter root to what MSYS reports (C:\proj → /c/proj, C:\ → /c), and the gate tests both that and the native form. Keeping the native form is what lets a re-render recognise and replace the entries earlier versions wrote instead of stacking a second gate beside them; a root without a drive letter (Linux, macOS) is unchanged, and the gate still carries a single test there.

Keep the legacy cmd gate readable. cmdProjectGate() stays as a read-side recogniser in isGatedForProject(): entries written by 0.26.0 are still on disk, and matching them is what lets a re-render replace the old gate rather than stack a second one on top.

Preserve the Cursor/Copilot wrapper. skipWhenAnotherHostLoadsClaudeSettings() (#950) is orthogonal and still keyed on tool; only the gate lost its per-tool form. Resolving the conflict kept it intact.

Test Plan

  • npx tsc --noEmit — passes
  • npm run lint — 0 warnings, 0 errors on 811 files. Note: --type-aware (tsgolint) could not run in my sandbox — its subprocess fails to spawn with os error 231 (all pipe instances busy), which is an environment limit rather than a code signal, so that half is unverified here.
  • npx vitest run on the 4 test files touching this area — 67 passed, 3 skipped, 0 failed (hooks-golden, hooks-shell-check, hooks-windows-bash, hooks-reconcile-scope). The full suite is not a usable gate on Windows; unrelated pre-existing /bin/sh, chmod and file-mode cases fail identically on main.
  • Added/updated tests for the change

The project gate — real-shell execution

src/__tests__/hooks-reconcile-scope.test.ts runs a freshly rendered gate through the runner's shell and asserts what the harness has to see: the payload runs inside the project (root and subdirectory) and not outside it (the parent and a sibling that merely shares the prefix), with exit 0 either way — a non-zero hook status is what CodeBuddy reads as allowed:false. Directory names carrying &, a space and an injection canary are covered, so the quoting is exercised too.

Two things that test needs, both worth knowing:

  • Directories outside os.tmpdir(). MSYS mounts the Windows temp directory at /tmp, so a root under it is reported as /tmp/... and no gate rendered from its native path can match there. The cases build under node_modules, which is on the workspace drive like a real project and is never committed.
  • Asynchronous spawn. A sandbox can fail the synchronous spawn with EBUSY, which surfaces as an empty stdout — indistinguishable from a gate that never matched. (This is what made the first version of this test look like a logic failure.)

Environment-independent gate setup

The block used to assert the rendered gate by reading back ~/.codebuddy/settings.json, but never staged a shell for the mocked win32 platform — so whether the case ran at all depended on the developer's Git install location. On a machine where Git lives outside ProgramFiles/LOCALAPPDATA and can only be found via the HKLM\SOFTWARE\GitForWindows registry key, findGitBashWindows() returned null, skipToolsWithoutShell() dropped both tools, and the read failed with ENOENT.

It now clears the three environment variables and stages both candidates under the mocked home (CodeBuddy's bash.exe, WorkBuddy's PortableGit sh.exe), matching what hooks-shell-check.test.ts already does, so the setup is the same on any developer box and on CI.

Real-CLI end-to-end verification

Built with npm run build, then ran the built CLI against a throwaway HOME (a temp dir) with an already-initialised config and a local team repo, so the real injection path ran:

$ HOME=<sandbox> node dist/index.js hooks inject
⚠ Skipping hook injection for workbuddy: no shell is available in this environment to execute hooks. …
✔ Updated teamai hooks in <sandbox>\.codebuddy\settings.json
✔ Hooks injected into all AI tool settings

codebuddy is no longer skipped (its Git Bash resolves), and <sandbox>\.codebuddy\settings.json now holds:

"command": "PATH=\"$HOME/.teamai/bin:$PATH\" teamai hook-dispatch post-tool-use --tool codebuddy 2>/dev/null || true"

Before this change the same run produced (captured from a real Windows machine, and pinned by the old assertions):

"command": "set \"PATH=%HOME%/.teamai/bin;%PATH%\" && teamai hook-dispatch post-tool-use --tool codebuddy 2>nul || exit /b 0"

2>nul under Git Bash creates a file named nul; set "PATH=…;%PATH%" never prepends anything; || exit /b 0 exits 2 on a failing dispatch, which CodeBuddy reads as allowed:false.

The project gate moved the same way. It now carries the shell's spelling of the root, which is the part that makes it match at all:

if [ "$PWD" = 'C:\proj' ] || [ "$PWD" = '/c/proj' ] || case "$PWD" in 'C:\proj'/*|'/c/proj'/*) true;; *) false;; esac; then (python3 .docs/script/inject-telemetry.py); fi

Notes for review

  • The gate's exit-status contract is unchanged: outside the project it is a no-op exiting 0, inside it the payload's status passes through. A mismatch returning non-zero would make CodeBuddy read the hook as allowed:false and block every UserPromptSubmit outside the project.
  • gateTeamHookCommand() is shared by every tool, so the path-spelling fix is not codebuddy-specific. On Windows the POSIX gate was already what claude, workbuddy, codex and the rest were given (toolUsesCmdShell() was true only for codebuddy), which means their project-scoped team hooks were silently disabled on Windows too, for as long as the POSIX gate has existed. Those tools get working gates from this change as well; before it, only codebuddy's cmd gate matched.
  • The cmd form was deliberately not ${gate} || exit /b 0 && (…): the || would swallow a genuine payload failure along with the mismatch. With only one rendering left, the POSIX if …; then …; fi gives that pass-through directly.
  • Not covered: WSL (/mnt/c/...) and Cygwin (/cygdrive/c/...) mount points, and UNC roots (\\server\share), which pass through unchanged. They do not match today either, so nothing regresses; a native POSIX root (Linux, macOS) is unaffected.

CodeBuddy runs a hook's `command` string through Git Bash on Windows — it
dropped PowerShell/CMD for hooks in 2.52.4 and forces Git Bash since 2.55.1 —
so the cmd-syntax commands added in Tencent#637 misbehave there:

- `2>nul` is a file redirect for bash, not the null device, so every hook
  invocation leaves a 0-byte file named `nul` in the hook's working directory.
  PostToolUse (matcher `*`) fires after every tool call, so it reappears
  continuously in the project root, and a reserved device name cannot be
  removed with `del` or Explorer.
- `set "PATH=…;%PATH%"` never prepends PATH under bash.
- `|| exit /b 0` makes bash exit 2 on a failing dispatch, which CodeBuddy
  reads as `allowed:false` and would block a UserPromptSubmit with.

The project gate is affected the same way: `cd| findstr … >nul` run by Git
Bash errors on `findstr`, exits non-zero and leaves another `nul`.

Render the POSIX wrapper form for CodeBuddy, which leaves the cmd renderer
unused: drop it, and resolve CodeBuddy's shell to Git Bash the way WorkBuddy's
resolver already asks for its bundled MSYS sh. Null when Git Bash is absent,
which skips injection with the existing warning instead of installing hooks
that could never run. Legacy cmd gates stay recognised on read, so entries
written by 0.26.0 are replaced on the next pull rather than duplicated.
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
  • [P1 blocking] src/hooks.ts:420 compares Git Bash’s $PWD with a native Windows path. For a project rooted at C:\proj, canonicalProjectRoot() renders C:\proj, while Git Bash exposes $PWD as an MSYS path such as /c/proj. The condition therefore remains false even inside the project, so project-scoped CodeBuddy team hooks never execute. Convert the root to Git Bash/MSYS form before rendering the POSIX gate, and cover this with an actual Git Bash execution test.

@CarlosWonMore

Copy link
Copy Markdown
Contributor Author

Confirmed — this is a real defect, not a false positive, and the coverage gap it points at was mine.

I reproduced it before changing anything: canonicalProjectRoot() returns the native spelling (D:\proj), Git Bash reports $PWD as /d/proj, and the gate never matched. Running the rendered gate in a real Git Bash from the project root and a subdirectory gave no match; the same gate with the MSYS spelling matched.

The second half of your point is the one that stung: the PR replaced project gate — real cmd.exe execution (win32) — which actually ran the gate and asserted the inside/outside behaviour — with assertions on the command's shape. The string looked right and the gate could never fire, so the test could not have caught it. That is restored as a real execution test through the runner's shell, over directory names carrying &, a space and an injection canary, asserting the payload runs inside (root and subdirectory) and not outside (parent and a prefix-sharing sibling), exit 0 either way.

Fixed in 01cb44e:

  • msysProjectRoot() rewrites a drive-letter root to the MSYS spelling (C:\proj → /c/proj, C:\ → /c).
  • The gate tests both spellings. The native one stays so a re-render recognises and replaces what earlier versions wrote rather than stacking a second gate beside it; the read side (isGatedForProject, POSIX_GATE_ROOT_RE/gatedProjectRoot) still reads the native form back, so nothing downstream changed.
  • A root without a drive letter is returned unchanged, so Linux/macOS renders exactly as before and the gate there still carries a single test.

Two things worth knowing about the new test, in case they bite anyone else:

  • It has to build its directories outside os.tmpdir(). MSYS mounts the Windows temp directory at /tmp, so a root under it is reported as /tmp/... and no gate rendered from its native path can match there — that is what made my first attempt fail for the wrong reason and nearly sent me looking in the wrong place.
  • It spawns asynchronously. In the sandbox I work in the synchronous spawn fails with EBUSY, which surfaces as an empty stdout — indistinguishable from a gate that never matched.

One thing worth flagging for scope: gateTeamHookCommand() is shared, so this is not codebuddy-specific. On Windows every other tool (claude, workbuddy, codex, …) was already given the POSIX gate — toolUsesCmdShell() was true only for codebuddy — which means their project-scoped team hooks were silently disabled on Windows too, for as long as the POSIX gate has existed. Before this change only codebuddy's cmd gate matched; now everything does.

Not covered, and unchanged from before: WSL (/mnt/c/...) and Cygwin (/cygdrive/c/...) mount points, and UNC roots (\\server\share).

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
  • [P2 non-blocking] src/hooks.ts:372 only converts drive-letter paths to /c/.... Git Bash can expose paths through MSYS aliases—for example, a project under Windows %TEMP% has $PWD=/tmp/..., as the new test comments acknowledge. CodeBuddy previously used the native cmd.exe gate there, but now neither the native nor /c/... comparison matches, so project-scoped hooks silently never execute. Resolve the actual shell path or support MSYS mount aliases.
  • The earlier $PWD versus native drive-path finding is resolved for ordinary drive-letter projects.
  • The PR description includes the required representative real-CLI end-to-end verification.

The gate tests `$PWD`, and an MSYS shell spells a drive-letter path `/c/proj`,
never the `C:/proj` a Windows root resolves to. A gate rendered in the native
spelling only therefore never matched, so every project-scoped team hook
silently never ran.

Render the root in both spellings: the native one, which is what earlier
versions wrote and what the read side keeps recognising, so a re-render replaces
those entries instead of stacking beside them, plus the MSYS one the runner's
`$PWD` actually holds. Drive roots lose their trailing separator (`C:/` ->
`/c`). A root without a drive letter (Linux, macOS) is unchanged and the gate
still carries a single test there.

Restore the execution coverage this PR had dropped. The cmd.exe gate used to be
executed for real by `project gate — real cmd.exe execution (win32)`; replacing
that with shape assertions is what let a gate that could never match pass. Run
the rendered gate through the runner's shell — from inside and outside the
project, over directory names carrying the characters that would split the
command if the root were not quoted.

Two environment notes on that test: it builds its directories outside
`os.tmpdir()`, because MSYS mounts the Windows temp directory at `/tmp` and a
root under it is reported as `/tmp/...`, where no native-path gate can match;
and it spawns asynchronously, because a sandbox can fail the synchronous spawn
with EBUSY, which would show up as an empty stdout — the signature of a gate
that never matched.
@CarlosWonMore
CarlosWonMore force-pushed the fix/codebuddy-hooks-posix branch from 01cb44e to 3bbe9d2 Compare October 8, 2026 01:32
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
  • [P2 non-blocking] src/hooks.ts:372 only converts drive-letter paths to /c/.... Git Bash exposes Windows temporary directories through aliases such as /tmp/..., so projects under %TEMP% still never satisfy the gate and their project-scoped hooks silently remain disabled. The new test explicitly avoids this case rather than supporting the shell’s actual path.
  • [P2 non-blocking] src/__tests__/hooks-reconcile-scope.test.ts:1045 uses findOnPath('bash'), which can select Windows’ WSL bash.exe instead of Git Bash. On a machine where System32 precedes Git Bash, $PWD becomes /mnt/c/..., causing this supposed Git Bash execution test to fail despite the production /c/... behavior. Resolve Git Bash explicitly.

The earlier native drive-path mismatch is resolved. The PR description includes sufficient representative real-CLI verification.

@CarlosWonMore

Copy link
Copy Markdown
Contributor Author

Heads-up on the one red check, so it is not misread as coming from this PR.

Lint & Test (Node 20 on macos-latest) failed, but every test passed:

 Test Files  392 passed | 1 skipped (393)
      Tests  8128 passed | 19 skipped (8147)
     Errors  1 error

That single error is an unhandled rejection, and Vitest names its origin:

This error originated in "src/__tests__/recall-attribution.test.ts" test file.
The latest test that might've caused the error is "11: SubagentStop credits a read
locally and pushes nothing mid-turn; the next Stop pushes it".

Error: ENOENT: no such file or directory, open
  '…/T/teamai-recall-attr-Q7alMi/home/.teamai/projects/…/votes/tester.yaml.lock'

A lock write racing the fixture teardown — recall-attribution, which this PR does not touch (it changes src/hooks.ts and hooks-reconcile-scope.test.ts only). The same commit passes on the other five matrices: ubuntu ×3 and macOS Node 22 and Node 24.

Worth flagging that this matrix is not clean on main either — 37580055664 also failed on a macOS Lint & Test job, though with a different symptom (1 failed test file rather than a dangling rejection), so the flakiness looks pre-existing and broader than this one case.

A re-run of that job should clear it; I can't trigger one from a fork. Happy to rebase if main moves first.

@jeff-r2026
jeff-r2026 merged commit a7aee94 into Tencent:main Oct 8, 2026
20 of 21 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…encent#989)

* fix(hooks): render CodeBuddy's Windows hooks in POSIX, not cmd.exe

CodeBuddy runs a hook's `command` string through Git Bash on Windows — it
dropped PowerShell/CMD for hooks in 2.52.4 and forces Git Bash since 2.55.1 —
so the cmd-syntax commands added in Tencent#637 misbehave there:

- `2>nul` is a file redirect for bash, not the null device, so every hook
  invocation leaves a 0-byte file named `nul` in the hook's working directory.
  PostToolUse (matcher `*`) fires after every tool call, so it reappears
  continuously in the project root, and a reserved device name cannot be
  removed with `del` or Explorer.
- `set "PATH=…;%PATH%"` never prepends PATH under bash.
- `|| exit /b 0` makes bash exit 2 on a failing dispatch, which CodeBuddy
  reads as `allowed:false` and would block a UserPromptSubmit with.

The project gate is affected the same way: `cd| findstr … >nul` run by Git
Bash errors on `findstr`, exits non-zero and leaves another `nul`.

Render the POSIX wrapper form for CodeBuddy, which leaves the cmd renderer
unused: drop it, and resolve CodeBuddy's shell to Git Bash the way WorkBuddy's
resolver already asks for its bundled MSYS sh. Null when Git Bash is absent,
which skips injection with the existing warning instead of installing hooks
that could never run. Legacy cmd gates stay recognised on read, so entries
written by 0.26.0 are replaced on the next pull rather than duplicated.

* fix(hooks): compare the project gate in the shell's path spelling

The gate tests `$PWD`, and an MSYS shell spells a drive-letter path `/c/proj`,
never the `C:/proj` a Windows root resolves to. A gate rendered in the native
spelling only therefore never matched, so every project-scoped team hook
silently never ran.

Render the root in both spellings: the native one, which is what earlier
versions wrote and what the read side keeps recognising, so a re-render replaces
those entries instead of stacking beside them, plus the MSYS one the runner's
`$PWD` actually holds. Drive roots lose their trailing separator (`C:/` ->
`/c`). A root without a drive letter (Linux, macOS) is unchanged and the gate
still carries a single test there.

Restore the execution coverage this PR had dropped. The cmd.exe gate used to be
executed for real by `project gate — real cmd.exe execution (win32)`; replacing
that with shape assertions is what let a gate that could never match pass. Run
the rendered gate through the runner's shell — from inside and outside the
project, over directory names carrying the characters that would split the
command if the root were not quoted.

Two environment notes on that test: it builds its directories outside
`os.tmpdir()`, because MSYS mounts the Windows temp directory at `/tmp` and a
root under it is reported as `/tmp/...`, where no native-path gate can match;
and it spawns asynchronously, because a sandbox can fail the synchronous spawn
with EBUSY, which would show up as an empty stdout — the signature of a gate
that never matched.
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.

[bug] CodeBuddy hooks are rendered in cmd.exe syntax and leave a junk nul file on every tool call

2 participants