Skip to content

Run environment setup and teardown hooks on native Windows - #4658

Merged
SawyerHood merged 3 commits into
mainfrom
bb/windows-setup-hooks-thr_jensq3v9th
Oct 2, 2026
Merged

SawyerHood merged 3 commits into
mainfrom
bb/windows-setup-hooks-thr_jensq3v9th

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

buildLifecycleScriptCommand threw POSIX shell setup scripts are not supported on Windows for every .bb-env-setup.sh and .bb-env-teardown.sh. Setup failure fails provisioning, so on a native Windows host any repository that commits a setup hook could not start a worktree thread. Part of #1206.

What changed

  • apps/host-daemon/src/environment-lifecycle-script.ts: on Windows the hook runs as <bash> <script> where bash is the one Git for Windows installs. resolveWindowsBashPath asks git var GIT_SHELL_PATH (the same lookup runShellPipeline already uses), prefers bash.exe in that directory, and falls back to the reported shell. That directory is put first on the hook's PATH, because a non-login Git bash does not otherwise find head, touch, or bash itself.
  • Timeout and cancellation on Windows stop every process the hook started. Git Bash launches programs such as sleep through a helper process that exits, so Windows no longer links them to the hook and taskkill /T from the hook's root misses them; they also keep the output pipes open, so the hook never returned. New apps/host-daemon/src/git-bash-process-group.ts asks Git Bash for the members of the hook's process group (/proc/<pid>/pgid and winpid), stops each member's tree with taskkill, and repeats until the group is empty. The POSIX path still kills the process group.
  • The hook only counts as stopped when that check comes back empty and the hook's own process exited. Otherwise, or when output stays open for 2 seconds after the stop, the run fails with LifecycleScriptTerminationUnverifiedError and environment.hook.cancel answers unknown, which the server already treats as cleanup-blocking. Before, the stop result was dropped and the cancel answered terminated.
  • Cancellation is checked again after the Git shell lookup, and the lookup itself takes the abort signal, so a hook cancelled during the lookup is not started.
  • packages/process-utils: terminateWindowsProcessTrees (the existing taskkill call, taking several roots) is exported for the above.
  • When Git reports no shell, the hook fails with <script> needs Git for Windows: … instead of the old unsupported-platform message.
  • docs/worktrees.md, docs/platform-support.md, and the bb guide environments template describe the Windows behavior.

macOS and Linux still run env bash <script>; the command text and environment there are unchanged. No wire or CLI changes.

How you verified

  • Windows 11 Enterprise VM (build 26300, Git for Windows 2.56.0), on a branch combining this change with Start bb and run the bb CLI on native Windows #4657 and the Windows path change, since a worktree thread cannot be created on Windows without them: a repository with a tracked .bb-env-setup.sh (echo hook-ran-in $(pwd) with $(bash --version | head -1), then touch hook-marker.txt) provisioned a worktree thread. The thread log showed hook-ran-in /c/bb-data/…/testrepo with GNU bash, version 5.3.15, .bb-env-setup.sh finished (159ms), and hook-marker.txt existed in the worktree.
  • The same run before the PATH change failed with head: command not found and exit code 127, which is what the directory prefix fixes.
  • Cancellation, same VM, on a branch combining all the open Windows changes:
    • vitest run src/environment-lifecycle-script.test.ts in apps/host-daemon on Windows: 13 of 13 pass, including the setup and teardown timeout tests, the cancel test, and a new Windows-only test whose hook starts a background sleep 120 and a background node process, is cancelled, and then finds neither process alive. Before the process-group change the timeout and cancel tests each ran into the 15 second test timeout, and a sleep.exe whose parent process no longer existed was still running after taskkill /T /F reported success.
    • In the running app: a worktree thread whose setup hook runs sleep 300 & and sleep 300 showed two sleep.exe processes; bb thread stop left none, and no bash.exe, 800 ms later.
  • Linux: turbo run lint typecheck test --filter=@bb/host-daemon --filter=@bb/process-utils passes (10 of 10 tasks). The unit tests cover the Windows command, its PATH prefix, the missing-bash message, the rule that only a confirmed stop is accepted, and that a cancel whose stop is unconfirmed answers unknown.

Not verified: the teardown hook on Windows through the app (only through runTeardownScript in the tests above); a hook that leaves a background program running after its own shell exits within the first moments of the run, where the process group lookup may not have finished and the stop reports unverified. The Windows-only test runs only on a Windows machine; CI does not run the host daemon tests on Windows.

🤖 Generated with Claude Code

AGENT GENERATED

`.bb-env-setup.sh` and `.bb-env-teardown.sh` threw on Windows, so a repository
with a setup hook could not provision a worktree there.

Run the scripts with the bash that Git for Windows installs, resolved through
`git var GIT_SHELL_PATH`, with that directory first on PATH so coreutils
resolve. Stop the whole process tree on timeout or cancellation. Fail with a
message naming Git for Windows when no bash is found.

Co-Authored-By: Claude Code <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T01:43:37.333481Z 4a56420 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a5642086c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +208 to +212
void stopProcessGroupLeaderFirst({
child,
timeoutMs: 0,
killGraceMs: 0,
}).catch(() => undefined);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Await Windows process-tree termination before cleanup

When taskkill.exe fails or exceeds its internal timeout, stopWindowsProcessTree reports treeTermination: "unverified" after killing only the root process, but this fire-and-forget call ignores both that result and any rejection. Because isProcessGroupAlive always returns false on Windows, runLifecycleScript can then finish as soon as Bash closes, causing cancelEnvironmentHook to report terminated and allowing workspace cleanup while descendant processes remain active. Retain and await the stop promise, and treat unverified termination as a cleanup-blocking failure; this Windows failure path should also be covered by a test.

AGENTS.md reference: AGENTS.md:L49-L49

Useful? React with 👍 / 👎.

@SawyerHood
SawyerHood marked this pull request as draft October 1, 2026 23:37
@SawyerHood
SawyerHood marked this pull request as ready for review October 2, 2026 01:41

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a5642086c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +159 to +160
windowsBashPath:
process.platform === "win32" ? await resolveWindowsBashPath(env) : null,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recheck cancellation before spawning the Windows hook

If provisioning is cancelled while resolveWindowsBashPath is awaiting git var (which may run for up to 10 seconds), the earlier abort check has already passed and this code proceeds to spawn the setup script. The later args.signal?.aborted check only starts terminating it after launch, so a cancelled setup hook can briefly execute and produce side effects, while cancellation is also delayed by the uncancelled Git lookup. Pass the signal into the lookup and/or call throwIfProvisionAborted again before spawning the hook.

Useful? React with 👍 / 👎.

SawyerHood and others added 2 commits October 2, 2026 02:03
A cancelled or timed-out hook started the tree termination without waiting
for it, so cancellation could report the hook as terminated while its
descendants were still running. Await the stop, and report an unconfirmed
termination as an unknown outcome so cleanup stays blocked. Pass the abort
signal to the Git shell lookup and check it again before spawning.

Co-Authored-By: Claude Code <[email protected]>
Git Bash starts programs such as sleep through a helper process that
exits, so Windows no longer links them to the hook and a tree kill from
the hook's root misses them. They kept the output pipes open and the
cancelled hook never returned. The hook runner now asks Git Bash for the
members of the hook's process group, stops each member's tree, repeats
until the group is empty, and gives up with an unverified result when
output stays open.

Co-Authored-By: Claude Code <[email protected]>
@SawyerHood
SawyerHood merged commit c35bfe5 into main Oct 2, 2026
21 checks passed
@SawyerHood
SawyerHood deleted the bb/windows-setup-hooks-thr_jensq3v9th branch October 2, 2026 03:27
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