Feature: PreSession hook that can set working directory

Resolved 💬 3 comments Opened Feb 18, 2026 by skmixx Closed Feb 22, 2026

Problem

When running concurrent Claude Code sessions on the same project, agents need isolated working directories (git worktrees) to avoid stepping on each other's branches and uncommitted changes. The current SessionStart hook runs after the session is already bound to a directory, so it can't change the working directory — it can only print "please cd here," which the agent ignores.

The only reliable workaround today is a shell function that wraps the claude command:

claude() {
    if [ -f "./local/init-worktree.sh" ]; then
        bash ./local/init-worktree.sh "$@"
    else
        command claude "$@"
    fi
}

This works but is fragile — it requires every user to modify their shell config, and muscle memory means people will forget to install it.

Proposed Solution

A PreSession hook (or similar mechanism) that:

  1. Runs before the session starts (before the agent's cwd is set)
  2. Can return a JSON field like "cwd": "/path/to/worktree" that tells Claude Code where to run
  3. Has access to the project directory and session metadata (new vs resume)

Example hook configuration:

{
  "hooks": {
    "PreSession": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "bash \"$CLAUDE_PROJECT_DIR/local/init-worktree.sh\"",
            "timeout": 15000
          }
        ]
      }
    ]
  }
}

The hook script would output:

{
  "cwd": "/tmp/project-worktree-20260218-123456",
  "additionalContext": "Working in isolated worktree. Branch: session/20260218-123456"
}

For resume (claude -c), the hook would receive session metadata and return the previously-created worktree path instead of creating a new one.

Use Case

We've built an agent behavioral architecture that includes:

  • Git worktree isolation for concurrent sessions
  • PostToolUse hooks that surface protocol reminders when lint/test/build fails
  • PreToolUse hooks for comment freshness checks
  • Cross-session memory system

The worktree isolation is the foundation — without it, concurrent sessions corrupt each other's work (wrong branches, merge conflicts, overwritten files). Every other hook depends on the agent being in the right directory. Currently, this is the one piece that requires a shell wrapper outside of Claude Code's hook system.

Alternatives Considered

  • SessionStart hook + "please cd" instruction: Agent ignores it (Level 2 instruction vs Level 4 structural enforcement)
  • Shell function wrapper: Works but requires manual shell config, easy to forget
  • PreToolUse hooks that rewrite file paths: Too complex and fragile

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗