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

postToolUse hook additionalContext not injected into agent context window

Open jalvarezsamayoa opened this issue 7 months ago • 3 comments

Describe the bug

Bug Report: postToolUse hook additionalContext not injected into agent context window

Summary

When a postToolUse hook emits a JSON response containing an additionalContext field, the Copilot CLI captures the hook output but does not forward the additionalContext value into the agent's context window. As a result, the agent never sees lint diagnostics, warnings, or any other feedback the hook intended to surface, and cannot self-correct.


Environment

Field Value
CLI GitHub Copilot CLI
OS macOS (Darwin)
Hook type postToolUse
Triggering tool create (also expected to affect edit)
Hook script any postToolUse hook emitting {"additionalContext": "..."}

Minimal Reproduction

1. Create the hook script

Save the following as ~/.copilot/hooks/post-tool-use-test.sh and make it executable (chmod +x):

#!/usr/bin/env bash
# Minimal postToolUse hook to reproduce the additionalContext injection bug.
# Emits additionalContext whenever the agent uses the 'create' tool.

input=$(cat)
tool_name=$(echo "${input}" | jq -r '.toolName // empty')

if [[ "${tool_name}" != "create" ]]; then
  echo '{}'
  exit 0
fi

# Emit additionalContext — this should be injected into the agent's context window.
echo '{"additionalContext": "HOOK FEEDBACK: This message should appear in the agent context."}'

2. Register the hook

In your Copilot CLI hooks configuration, register the script as a postToolUse hook:

{
  "hooks": [
    {
      "event": "postToolUse",
      "command": "~/.copilot/hooks/post-tool-use-test.sh"
    }
  ]
}

3. Trigger it

Start a Copilot CLI session and ask the agent to create any file:

"Create a file called test.txt containing the word hello."

4. Observe the failure

  • ✅ The hook runs (visible in debug logs via --debug flag).
  • ✅ The hook stdout contains {"additionalContext": "HOOK FEEDBACK: ..."}.
  • ❌ The agent's next response shows no awareness of the hook message. It does not mention, react to, or incorporate "HOOK FEEDBACK: ..." in any way.

To confirm with debug logs, run with --debug and look for:

[hook stdout] {"additionalContext": "HOOK FEEDBACK: This message should appear in the agent context."}

This line will be present, proving the CLI received the output — but the agent never sees it.


Expected Behavior

Per the documented postToolUse contract and the hook's own inline documentation:

"it emits {"additionalContext": "..."} so the Copilot CLI injects the ShellCheck diagnostics into the agent's context window, enabling self-correction."

The additionalContext string should be injected into the agent's context window as a system message or tool result annotation after the tool call completes, so the agent can:

  • Read the diagnostic feedback.
  • Self-correct its output (e.g., fix the linting issues before the task is considered done).

Actual Behavior

The Copilot CLI runtime:

  1. ✅ Spawns the hook process correctly.
  2. ✅ Receives the hook's JSON stdout (confirmed via debug logs).
  3. ❌ Does NOT inject additionalContext into the agent's context window.

The agent completes the task with no awareness that the hook ran or that it produced feedback.

Relevant Debug Log Evidence

[hook stderr] bash-shellcheck hook: ShellCheck validation failed for /path/to/hello.sh
[hook stderr] [INFO] Linting: /path/to/hello.sh

In /path/to/hello.sh line 3:
        echo $x
             ^-- SC2086 (info): Double quote to prevent globbing and word splitting.
             ^-- SC2250 (style): Prefer putting braces around variable references...
✗ /path/to/hello.sh failed

[hook stdout] {
  "additionalContext": "[INFO] Linting: ... SC2086 ... SC2250 ... ✗ failed"
}

The CLI received [hook stdout] containing the populated additionalContext field, but the agent received no message containing this content. The agent's next response showed no awareness of the lint failure.


Impact

  • Self-correction loop is broken. The entire purpose of additionalContext in postToolUse hooks is to enable agents to detect and fix their own mistakes automatically. This mechanism is silently non-functional.
  • Silent failure. There is no error, warning, or indication to the user or agent that additionalContext was dropped. Users who build hooks relying on this feature will believe it works (the hook runs, the JSON is correct) but the agent will never self-correct.
  • Plugin ecosystem impact. Any plugin that relies on postToolUse + additionalContext for agent feedback (linters, formatters, validators, security scanners) is affected.

Root Cause Hypothesis

The Copilot CLI runtime correctly spawns postToolUse hooks and captures their stdout, but the code path that reads the parsed JSON response and injects additionalContext into the agent's next context turn appears to be missing or not wired up. The data is captured (visible in debug logs) but never routed to the agent.

Likely location of the bug: the hook result post-processing step, where the runtime should take response.additionalContext and append it to the agent's pending context before the next LLM call.


Suggested Fix

After a postToolUse hook completes, if the parsed JSON response contains a non-empty additionalContext string, the runtime should inject it into the agent's context window — for example as a system notification, a tool result annotation, or a synthetic user message — before the agent produces its next response.


Workaround

None available. The additionalContext field in preToolUse hooks does work (it can block tool calls), but postToolUse has no equivalent workaround since it runs after the tool has already executed.

Affected version

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

Additional context

No response

jalvarezsamayoa avatar Apr 26 '26 19:04 jalvarezsamayoa

+1

I understand that the current hooks reference says postToolUse has Output processed: No, so this may be a feature request rather than a bug under the current spec.

That said, being able to feed postToolUse results back into the agent is very important for coding-agent workflows. A common use case is running formatters, linters, or validators after file edits and giving the agent immediate feedback so it can re-read changed files and self-correct before ending the turn.

Without this, postToolUse can run checks or even modify files, but the agent has no reliable way to observe the result. That makes self-correction loops difficult and can leave hidden changes or diagnostics outside the agent’s context.

I’d like to support adding additionalContext or an equivalent output mechanism for postToolUse, even if it is introduced as an explicit feature/change to the current hook contract.

torumakabe avatar May 16 '26 23:05 torumakabe

Also experiencing this. I'd like to introduce a linting hook that shows failures after Copilot makes an edit, but in my experience this currently isn't possible because:

  • PostToolUse does not feed additionalContext to the model
  • ~~although the docs say Stop hooks can force another turn, they do not, and they also don't feed back additionalContext and encourage the agent to continue.~~ This was user error, there are different schemas for hooks between VSCode and Copilot CLI.

finalburn avatar May 18 '26 15:05 finalburn

This appears to be resolved as of GitHub Copilot CLI v1.0.87. Please update to that release or a newer stable version. We're closing this as completed. If it still happens, please comment with your version and reproduction steps.

copilot-cli-bot[bot] avatar Sep 22 '26 10:09 copilot-cli-bot[bot]