copilot-cli icon indicating copy to clipboard operation
copilot-cli copied to clipboard

sessionStart hook in .github/hooks/ does not fire in Copilot CLI (v0.0.420)

Open jiaczh opened this issue 9 months ago • 8 comments

Describe the bug

sessionStart hooks defined in .github/hooks/*.json do not execute when starting a Copilot CLI session.

Environment

  • CLI version: v0.0.420
  • OS: Windows 11
  • Shell: PowerShell 7

Hook configuration

File: .github/hooks/test-hook.json

{
  "version": 1,
  "hooks": {
    "sessionStart": [
      {
        "type": "command",
        "bash": "echo \"Session started: $(date)\" >> logs/session.log",
        "powershell": "Add-Content -Path logs/session.log -Value \"Session started: $(Get-Date)\"",
        "cwd": ".",
        "timeoutSec": 10
      }
    ]
  }
}

Expected behavior

logs/session.log should be created with a timestamp entry when a CLI session starts.

Actual behavior

logs/session.log is never created. The sessionStart hook does not fire.

Additional context

jiaczh avatar Feb 28 '26 01:02 jiaczh

Additional context: nested .github/ in a monorepo

Important detail — our Copilot CLI agent workspace lives deep inside a monorepo, not at the git root. The .github/hooks/ directory with the hooks is nested, and we cd into the agent subdirectory before launching copilot:

my-monorepo/                          ← git root
├── .github/
│   ├── copilot-instructions.md
│   ├── hooks/                        ← (empty, no hook files here)
│   ├── agents/
│   ├── skills/
│   └── scripts/
├── src/
│   └── services/
│       └── my-agent/
│           └── agent/                ← cwd when launching `copilot`
│               ├── .github/
│               │   ├── copilot-instructions.md
│               │   ├── hooks/
│               │   │   ├── test-hook.json          ← sessionStart (NOT firing ❌)
│               │   ├── agents/
│               │   │   └── *.agent.md
│               │   ├── skills/
│               │   │   └── */SKILL.md
│               │   └── TSGs/
│               │       └── *.md
│               ├── scripts/
│               │   └── hooks/
│               │       ├── inject_skill_principles.py
│               │       ├── validate_on_edit.py
│               │       └── validation.log          ← proof postToolUse fires
│               ├── logs/
│               │   └── (empty — session.log never created)
│               ├── setup.ps1
│               └── README.md

Why this matters

  • The git root .github/hooks/ is empty — no hooks defined there
  • The nested .github/hooks/ (relative to cwd) contains both hook files
  • preToolUse/postToolUse hooks from the nested .github/hooks/ do fire, proving the CLI finds and loads hooks from the cwd-relative .github/hooks/
  • sessionStart fails to trigger from this nested location

jiaczh avatar Feb 28 '26 03:02 jiaczh

Update: Source code investigation + corrected repro

I investigated the github/copilot-cli repo — it's closed-source (only README, changelog, LICENSE, install script). So I can't trace the hook loading code directly. However, reviewing the changelog and official docs reveals important details:

Changelog hook timeline

Version Date Hook feature
v0.0.396 2026-01-27 preToolUse hooks can deny tool execution and modify arguments
v0.0.401 2026-02-03 Add agentStop and subagentStop hooks
v0.0.402 2026-02-03 Plugins can provide hooks for session lifecycle events

The key entry is v0.0.402: "Plugins can provide hooks for session lifecycle events". This says plugins can provide session lifecycle hooks — it doesn't mention .github/hooks/ JSON files supporting sessionStart.

Updated finding: NONE of our hooks fire

After further testing, I can confirm no hooks fire at all — not just sessionStart. The preToolUse/postToolUse entries in validation.log I saw earlier were from a previous session (~45 minutes before), not the current one.

Possible root cause: nested .github/hooks/ in a monorepo

Per the docs:

Copilot agents support hooks stored in JSON files in your repository at .github/hooks/*.json.

And from the CLI tutorial:

For GitHub Copilot CLI, hooks are loaded from your current working directory.

Our setup is a monorepo where Copilot CLI is launched from a nested subdirectory (src/services/my-agent/agent/). The .github/hooks/ with hook files is relative to this cwd — NOT at the git root.

Hypothesis: The CLI may resolve .github/hooks/ from the git root rather than from cwd, or there's a conflict when both exist (our git root has an empty .github/hooks/ directory).

Questions for the team

  1. Does the CLI load hooks from cwd/.github/hooks/ or from git-root/.github/hooks/?
  2. If both exist, which takes precedence? Are they merged?
  3. Does an empty .github/hooks/ at the git root shadow/block a populated one in a subdirectory?

jiaczh avatar Mar 02 '26 19:03 jiaczh

From my extensive testing, hooks will only run from <repo root>/.github/hooks, not your current working directory, not the Copilot CLI configuration home directory, nor anywhere else. 😁

My workaround for this was to symlink <repo root>/.github/hooks to X:\Copilot\Hooks (where I keep my cross-project global hooks). This seems to work well.

There are bugs here, though, notably:

  • The documentation is inconsistent/unclear
  • Hooks should be loaded in a sensible hierarchy/order from multiple places, including:
    • CWD/PWD
    • Repo/Project Root
    • Config Home if overridden (--config-dir or COPILOT_HOME or COPILOT_CUSTOM_INSTRUCTIONS_DIRS or COPILOT_SKILLS_DIRS or introduce a new COPILOT_HOOKS_DIRS)

ASIAubrie avatar Mar 16 '26 20:03 ASIAubrie

observed the same behavior after following https://docs.github.com/en/copilot/tutorials/copilot-cli-hooks . The userPromptSubmitted and preToolUse triggers fine. The sessionStart never works.

saumyajyoti avatar Mar 19 '26 12:03 saumyajyoti

From my experience, hooks in copilot-cli in repository are documentation hallucinations! Works in plugins but not when set in <REPO_ROOT>/.github/hooks.

rxe2fe avatar Mar 20 '26 13:03 rxe2fe

Data point from v1.0.75: user-level hooks DO load. The bug is specific to the sessionStart event, not to hook location.

Most of this thread debates where hooks are discovered (git-root vs cwd vs .github/hooks). I have a setup that rules location out entirely and isolates the problem to the sessionStart event itself.

  • CLI version: 1.0.75
  • OS: Windows 11, Windows PowerShell 5.1
  • Hook location: user-level ~/.copilot/hooks/memoria.json (not .github/hooks)

A single file declares both a sessionStart and a sessionEnd command hook:

{
  "version": 1,
  "hooks": {
    "sessionStart": [{ "type": "command", "powershell": "& '...\carregar-indice.ps1'", "timeoutSec": 15 }],
    "sessionEnd":   [{ "type": "command", "powershell": "& '...\sync.ps1'",           "timeoutSec": 90 }]
  }
}

Both scripts append a timestamped line to a log file as their very first action.

Result, reproduced across 3 sessions:

  • sessionEnd fires every time; the log is written and the script's git commit lands (verified by commit trailer).
  • sessionStart never fires; its log file is never even created.

Since both hooks live in the same file at the same user-level path, and sessionEnd runs from there, this shows:

  1. User-level ~/.copilot/hooks/*.json is discovered and executed in 1.0.75 (contrary to the earlier comment that only <repo-root>/.github/hooks works).
  2. The failure is specific to the sessionStart event, independent of hook location. userPromptSubmitted/preToolUse fire, but sessionStart never does.

Still open on 1.0.75.

wefgoncalves avatar Jul 27 '26 19:07 wefgoncalves

Controlled test on 1.0.78, macOS: the same sessionStart hook fires from the user-level directory and not from .github/hooks/.

The previous comment isolates the event by holding location constant. This does the opposite, so the two together may be describing different faults: that report is Windows with powershell: handlers, this is macOS with bash:.

One session, three identical hooks, distinguishable tracers:

Location Fired
<repo root>/.github/hooks/trace.json no
<repo root>/.github/copilot/settings.json, inline hooks no
$COPILOT_HOME/hooks/trace.json yes
  • CLI version: 1.0.78
  • OS: macOS 26.5.2 (Darwin arm64), bash
  • Invocation: copilot -p "…" --allow-all-tools --no-ask-user
  • Fresh git init, everything committed, branch main, COPILOT_HOME pointed at a throwaway directory so nothing else could interfere.

Every hook was byte-identical apart from the tracer label:

{
  "version": 1,
  "hooks": {
    "sessionStart": [
      { "type": "command", "bash": "/abs/path/trace.sh <label>", "timeoutSec": 20 }
    ]
  }
}
#!/usr/bin/env bash
cat > "$COPILOT_TRACE_DIR/$1-$(date +%s%N).json"
exit 0

So on this platform and version, sessionStart is not broken as an event. It runs from $COPILOT_HOME/hooks/. It does not run from either documented repository-level location.

It is not only sessionStart. Earlier runs in the same setup registered preToolUse and PreToolUse in .github/hooks/*.json, and neither fired for a real Bash or Write tool call. Moving the identical definitions to $COPILOT_HOME/hooks/ made both fire, with the documented payloads: the capitalised event sending tool_input as an object, the camelCase one sending toolArgs as a JSON string. So the pattern looks like location rather than event, at least here.

Worth noting that copilot help config currently documents both of the locations that do not work:

hooks: inline hook definitions, keyed by event name (same schema as .github/hooks/*.json). In global config.json these act as user-level hooks; in repo settings.json they act as repo-level hooks

This matters beyond a single project: an installer that follows the documentation writes to .github/hooks/ and produces a configuration that looks correct and never runs. That is hard to distinguish from a hook that ran and found nothing to say.

Happy to re-run against a newer build, or to test a specific variation, if that would help narrow it.

nordscope-fi avatar Aug 09 '26 12:08 nordscope-fi

From my extensive testing, hooks will only run from <repo root>/.github/hooks, not your current working directory, not the Copilot CLI configuration home directory, nor anywhere else. 😁

My workaround for this was to symlink <repo root>/.github/hooks to X:\Copilot\Hooks (where I keep my cross-project global hooks). This seems to work well.

There are bugs here, though, notably:

  • The documentation is inconsistent/unclear

  • Hooks should be loaded in a sensible hierarchy/order from multiple places, including:

    • CWD/PWD
    • Repo/Project Root
    • Config Home if overridden (--config-dir or COPILOT_HOME or COPILOT_CUSTOM_INSTRUCTIONS_DIRS or COPILOT_SKILLS_DIRS or introduce a new COPILOT_HOOKS_DIRS)

To add - I observed only the preToolUse hook type here specifically, not sessionStart or any others.

ASIAubrie avatar Aug 12 '26 17:08 ASIAubrie