Skip to content

feat(ai): run postCd hooks before launching AI process#145

Merged
helizaga merged 1 commit intomainfrom
feat/postcd-hooks-ai-launch
Feb 26, 2026
Merged

feat(ai): run postCd hooks before launching AI process#145
helizaga merged 1 commit intomainfrom
feat/postcd-hooks-ai-launch

Conversation

@helizaga
Copy link
Collaborator

@helizaga helizaga commented Feb 26, 2026

Summary

  • gtr ai and gtr new --ai now run postCd hooks before launching the AI process, so env vars set by hooks (e.g., source ./vars.sh) propagate to the AI tool
  • Adds run_hooks_export to lib/hooks.sh — a hook runner that evals without subshell isolation, so export calls persist. Call sites wrap it in an explicit ( ... ) subshell for set -e safety
  • Hook failures warn and continue — they never block the AI launch

Closes #144

Test plan

  • 330 BATS tests pass (6 new for run_hooks_export)
  • Manual: gtr ai with postCd hook → env var propagates to AI process
  • Manual: failing hook warns but AI still launches
  • Manual: no postCd hooks configured → no extra output, clean launch
  • ShellCheck clean on all changed files

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Enhancements

    • AI tool auto-launch now supplies repository context (root path, branch information) when initiated after worktree creation.
  • Tests

    • Added test coverage for hook execution with environment variable propagation and failure scenarios.

When users configure postCd hooks (e.g., source ./vars.sh), gtr cd runs
them but gtr ai did not. This meant AI tools launched via gtr ai didn't
get the worktree's environment setup.

Add run_hooks_export to lib/hooks.sh — a variant of run_hooks that evals
hooks without subshell isolation so exported env vars persist. Call sites
wrap it in an explicit subshell for safety with set -e.

Closes #144
@helizaga helizaga requested a review from NatoBoram as a code owner February 26, 2026 20:22
@coderabbitai
Copy link

coderabbitai bot commented Feb 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between d6fd5da and ee7c8bd.

📒 Files selected for processing (5)
  • lib/commands/ai.sh
  • lib/commands/create.sh
  • lib/hooks.sh
  • lib/launch.sh
  • tests/hooks.bats

Walkthrough

The changes implement postCd hook execution for the AI launch command. A new run_hooks_export() function exports environment variables to hooks in the current shell. The AI command now runs in a subshell that changes to the worktree, executes hooks with context variables, then starts the AI process.

Changes

Cohort / File(s) Summary
Hook Infrastructure
lib/hooks.sh
Added run_hooks_export() function to execute hooks while exporting provided environment variables (REPO_ROOT, WORKTREE_PATH, BRANCH) in the current shell, allowing variables to persist to child processes.
AI Launch Flow
lib/commands/ai.sh, lib/launch.sh
Modified AI startup to wrap invocation in subshell: changes to worktree directory, runs postCd hooks via run_hooks_export() with context variables, then launches ai_start. Added optional repo_root and branch parameters to _auto_launch_ai() to conditionally enable hook execution.
AI Trigger Integration
lib/commands/create.sh
Updated post-worktree creation flow to pass repo_root and branch_name arguments to _auto_launch_ai() call, enabling hook execution during auto-launch.
Hook Function Tests
tests/hooks.bats
Added comprehensive test suite for run_hooks_export() covering: no hooks configured, hook execution, environment variable propagation to child processes, hook failure resilience, context variable passing (REPO_ROOT, WORKTREE_PATH, BRANCH), and environment isolation.

Sequence Diagram

sequenceDiagram
    participant User as User/Create Command
    participant AICmd as AI Command
    participant Subshell as Subshell
    participant Hooks as Hook System
    participant AITool as AI Process

    User->>AICmd: gtr ai [branch]<br/>(with repo_root, branch)
    AICmd->>Subshell: spawn with worktree_path
    Subshell->>Hooks: run_hooks_export(postCd)<br/>export REPO_ROOT, WORKTREE_PATH, BRANCH
    Hooks->>Hooks: source hook script
    Hooks-->>Subshell: variables exported
    Subshell->>AITool: ai_start
    AITool->>AITool: inherits exported vars
    AITool-->>User: AI process launched<br/>with hook env
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 A rabbit hops with glee so bright,
For hooks now run before AI takes flight!
Environment variables flow like clover,
The worktree awakens—from launch to takeover!
PostCd magic, now the AI knows home! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: implementing postCd hook execution before AI process launch.
Linked Issues check ✅ Passed All coding requirements from issue #144 are met: postCd hooks execute before AI launch, environment variables propagate to AI, and hook failures emit warnings without blocking launch.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the stated objective: run postCd hooks before AI launch. No unrelated modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/postcd-hooks-ai-launch

Comment @coderabbitai help to get the list of available commands and usage tips.

@helizaga helizaga merged commit 0403d74 into main Feb 26, 2026
4 checks passed
@helizaga helizaga deleted the feat/postcd-hooks-ai-launch branch February 26, 2026 20:30
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.

Should gtr ai execute postCd hooks before launching the AI process?

1 participant