Skip to content

Commit a18b4a4

Browse files
committed
feat(cli): give Copilot searches graph context through a PostToolUse hook
Copilot CLI's preToolUse output can only decide permissions, and its command hooks fail closed on errors, so the graph context that Claude Code and Codex searches get could not ride a pre-tool hook there. postToolUse additionalContext is appended to the tool result the model sees on the same turn (github/docs content/copilot/reference/hooks-reference.md). VS Code's agent mode shares the Copilot harness and reads the same ~/.copilot/hooks manifest. - Manifest: $COPILOT_HOME/hooks/codebase-memory-mcp.json adds a PascalCase PostToolUse entry, matcher "Bash|Grep|Glob|bash|powershell|grep|rg|glob", running hook-augment --event PostToolUse --dialect copilot. The previous lifecycle-only manifest, as installs up to v0.12 wrote it, stays recognized as owned: install upgrades it in place and uninstall removes either generation. A foreign manifest still fails closed. - hook-augment: the Copilot dialect reads either payload form (tool_name/tool_input or toolName/toolArgs), maps runtime tool names to the Claude names (bash/powershell to Bash, grep/rg to Grep, glob to Glob), parses arguments passed as a JSON string, and answers with top-level additionalContext. - Fast path: Copilot's forced PostToolUse goes through the no-op gate before executable-identity hashing. The gate also skips tools the augmenter does not read, because VS Code's local hook parser ignores matchers, so a non-search shell call or an edit exits in about 20 ms instead of paying the full round-trip of about 130 ms. - The context header no longer says the search results are "below": after a post-tool hook they are above it. - Tests: the three Copilot foreign-manifest tests now create .copilot/mcp-config.json. Without it Copilot CLI was never detected, so install and uninstall never reached the manifest, and the tests also passed with the ownership checks removed. Refs DeusData#509 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 619192a commit a18b4a4

6 files changed

Lines changed: 418 additions & 54 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ overwrite user-modified agents.
702702
| Antigravity | Detected | `.gemini/config/mcp_config.json` | `.gemini/GEMINI.md` |
703703
| Aider | Detected | none | `CONVENTIONS.md` via `.aider.conf.yml` |
704704
| KiloCode | Detected | `.config/kilo/kilo.jsonc` | Rule + three graph-tool subagents with deny-by-default permissions |
705-
| VS Code | Detected | platform `Code/User/mcp.json` | `~/.copilot/skills`, three read-only agents, `sessionStart` + `subagentStart` |
705+
| VS Code | Detected | platform `Code/User/mcp.json` | `~/.copilot/skills`, three read-only agents, `sessionStart` + `subagentStart`, post-search `PostToolUse` graph lookup |
706706
| Cursor | Detected | `.cursor/mcp.json` | Skill + three read-only parent-handoff agents; context hooks withheld because session injection races and `readonly` blocks MCP |
707707
| Windsurf | Detected | `~/.codeium/windsurf/mcp_config.json` | Always-on `global_rules.md` |
708708
| Augment / Auggie | Detected | `~/.augment/settings.json` | Rule, three read-only handoff subagents, `SessionStart` + post-`view` coverage |
@@ -714,7 +714,7 @@ overwrite user-modified agents.
714714
| Cline | Detected | `~/.cline/mcp.json` + `${CLINE_DATA_DIR:-~/.cline/data}/settings/cline_mcp_settings.json` | Rule + skill; automatic file hooks withheld because they auto-activate and their output is not reliably consumed; child agents cannot use MCP |
715715
| Warp | Detected, skill only | UI, Warp Drive, or per invocation (manual) | Shared `~/.agents/skills/codebase-memory/SKILL.md` |
716716
| Qwen Code | Detected | `.qwen/settings.json` | `QWEN.md`, skill, three explicit read/graph-tool agents; `SessionStart`, `SubagentStart`, and post-`ReadFile` coverage |
717-
| GitHub Copilot CLI | Detected | `$COPILOT_HOME/mcp-config.json` | Instructions, skill, three read-only agents; `sessionStart` + `subagentStart` |
717+
| GitHub Copilot CLI | Detected | `$COPILOT_HOME/mcp-config.json` | Instructions, skill, three read-only agents; `sessionStart` + `subagentStart`, post-search `PostToolUse` graph lookup |
718718
| Factory Droid | Detected | `.factory/mcp.json` | `AGENTS.md`, skill, three droids with exact per-tier graph-tool lists (without additive whole-server exposure); `SessionStart` + post-`Read` coverage on macOS/Linux, withheld on Windows |
719719
| Crush | Detected | `.config/crush/crush.json` | Managed context path with explicit parent-to-child handoff |
720720
| Goose | Detected | `.config/goose/config.yaml` | `.goosehints` |

‎src/cli/cli.c‎

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,7 +4198,8 @@ enum { COPILOT_HOOK_TIMEOUT_SEC = 5 };
41984198
static int cbm_build_copilot_hook_command(const char *binary_path, const char *event,
41994199
bool powershell, char *out, size_t out_size) {
42004200
if (!binary_path || !event || !out ||
4201-
(strcmp(event, "SessionStart") != 0 && strcmp(event, "SubagentStart") != 0)) {
4201+
(strcmp(event, "SessionStart") != 0 && strcmp(event, "SubagentStart") != 0 &&
4202+
strcmp(event, "PostToolUse") != 0)) {
42024203
return CLI_ERR;
42034204
}
42044205
char quoted[CLI_BUF_8K];
@@ -4215,11 +4216,12 @@ static int cbm_build_copilot_hook_command(const char *binary_path, const char *e
42154216
}
42164217

42174218
static bool cbm_copilot_add_hook_event(yyjson_mut_doc *doc, yyjson_mut_val *hooks,
4218-
const char *event_key, const char *bash_command,
4219-
const char *powershell_command) {
4219+
const char *event_key, const char *matcher,
4220+
const char *bash_command, const char *powershell_command) {
42204221
yyjson_mut_val *entries = yyjson_mut_arr(doc);
42214222
yyjson_mut_val *entry = yyjson_mut_obj(doc);
42224223
if (!entries || !entry || !yyjson_mut_obj_add_str(doc, entry, "type", "command") ||
4224+
(matcher && !yyjson_mut_obj_add_str(doc, entry, "matcher", matcher)) ||
42234225
!yyjson_mut_obj_add_str(doc, entry, "bash", bash_command) ||
42244226
!yyjson_mut_obj_add_str(doc, entry, "powershell", powershell_command) ||
42254227
!yyjson_mut_obj_add_int(doc, entry, "timeoutSec", COPILOT_HOOK_TIMEOUT_SEC) ||
@@ -4230,19 +4232,36 @@ static bool cbm_copilot_add_hook_event(yyjson_mut_doc *doc, yyjson_mut_val *hook
42304232
return true;
42314233
}
42324234

4233-
static char *cbm_build_copilot_hook_manifest(const char *binary_path) {
4235+
/* Copilot CLI's preToolUse cannot add model context (its output is only a
4236+
* permission decision) and fails closed on hook errors, so searches are
4237+
* augmented after they run: postToolUse additionalContext is appended to the
4238+
* tool result the model sees. The PascalCase event name selects the
4239+
* Claude-format payload (tool_name Bash/Grep/Glob, tool_input), and the matcher
4240+
* names the Claude and the runtime tool names so either matcher reading fires. */
4241+
#define COPILOT_SEARCH_MATCHER "Bash|Grep|Glob|bash|powershell|grep|rg|glob"
4242+
4243+
/* include_posttool renders the current manifest; without it the function
4244+
* renders the previous lifecycle-only manifest, which installs up to v0.12 wrote
4245+
* and which upgrades and uninstalls must still recognize as owned. */
4246+
static char *cbm_build_copilot_hook_manifest(const char *binary_path, bool include_posttool) {
42344247
char session_bash[CLI_BUF_8K];
42354248
char session_powershell[CLI_BUF_8K];
42364249
char subagent_bash[CLI_BUF_8K];
42374250
char subagent_powershell[CLI_BUF_8K];
4251+
char post_bash[CLI_BUF_8K];
4252+
char post_powershell[CLI_BUF_8K];
42384253
if (cbm_build_copilot_hook_command(binary_path, "SessionStart", false, session_bash,
42394254
sizeof(session_bash)) != CLI_OK ||
42404255
cbm_build_copilot_hook_command(binary_path, "SessionStart", true, session_powershell,
42414256
sizeof(session_powershell)) != CLI_OK ||
42424257
cbm_build_copilot_hook_command(binary_path, "SubagentStart", false, subagent_bash,
42434258
sizeof(subagent_bash)) != CLI_OK ||
42444259
cbm_build_copilot_hook_command(binary_path, "SubagentStart", true, subagent_powershell,
4245-
sizeof(subagent_powershell)) != CLI_OK) {
4260+
sizeof(subagent_powershell)) != CLI_OK ||
4261+
cbm_build_copilot_hook_command(binary_path, "PostToolUse", false, post_bash,
4262+
sizeof(post_bash)) != CLI_OK ||
4263+
cbm_build_copilot_hook_command(binary_path, "PostToolUse", true, post_powershell,
4264+
sizeof(post_powershell)) != CLI_OK) {
42464265
return NULL;
42474266
}
42484267

@@ -4256,39 +4275,57 @@ static char *cbm_build_copilot_hook_manifest(const char *binary_path) {
42564275
return NULL;
42574276
}
42584277
yyjson_mut_doc_set_root(doc, root);
4259-
bool ok =
4260-
yyjson_mut_obj_add_int(doc, root, "version", 1) &&
4261-
cbm_copilot_add_hook_event(doc, hooks, "sessionStart", session_bash, session_powershell) &&
4262-
cbm_copilot_add_hook_event(doc, hooks, "subagentStart", subagent_bash,
4263-
subagent_powershell) &&
4264-
yyjson_mut_obj_add_val(doc, root, "hooks", hooks);
4278+
bool ok = yyjson_mut_obj_add_int(doc, root, "version", 1) &&
4279+
cbm_copilot_add_hook_event(doc, hooks, "sessionStart", NULL, session_bash,
4280+
session_powershell) &&
4281+
cbm_copilot_add_hook_event(doc, hooks, "subagentStart", NULL, subagent_bash,
4282+
subagent_powershell) &&
4283+
(!include_posttool ||
4284+
cbm_copilot_add_hook_event(doc, hooks, "PostToolUse", COPILOT_SEARCH_MATCHER,
4285+
post_bash, post_powershell)) &&
4286+
yyjson_mut_obj_add_val(doc, root, "hooks", hooks);
42654287
char *manifest = ok ? yyjson_mut_write(doc, YYJSON_WRITE_PRETTY, NULL) : NULL;
42664288
yyjson_mut_doc_free(doc);
42674289
return manifest;
42684290
}
42694291

4292+
#ifdef CBM_CLI_ENABLE_TEST_API
4293+
char *cbm_build_copilot_hook_manifest_for_testing(const char *binary_path, bool include_posttool) {
4294+
return cbm_build_copilot_hook_manifest(binary_path, include_posttool);
4295+
}
4296+
#endif
4297+
42704298
/* Copilot loads every manifest in $COPILOT_HOME/hooks. Treat our dedicated
4271-
* filename as an exact-owned document: a foreign collision fails closed, and
4272-
* uninstall accepts only the complete generated lifecycle schema. */
4299+
* filename as an exact-owned document: a foreign collision fails closed. The
4300+
* current manifest or the exact previous one is ours, so install upgrades the
4301+
* previous one in place and uninstall removes either. */
42734302
static int cbm_upsert_copilot_hooks(const char *binary_path, const char *manifest_path) {
4274-
char *manifest = cbm_build_copilot_hook_manifest(binary_path);
4275-
if (!manifest || !manifest_path) {
4303+
char *manifest = cbm_build_copilot_hook_manifest(binary_path, true);
4304+
char *previous = cbm_build_copilot_hook_manifest(binary_path, false);
4305+
if (!manifest || !previous || !manifest_path) {
42764306
free(manifest);
4307+
free(previous);
42774308
return CLI_ERR;
42784309
}
4279-
int rc = cbm_text_ensure_owned_document(manifest_path, manifest);
4310+
const char *const released[] = {previous};
4311+
int rc = cbm_text_migrate_owned_document(manifest_path, manifest, released, 1U);
42804312
free(manifest);
4313+
free(previous);
42814314
return rc == 0 ? CLI_OK : CLI_ERR;
42824315
}
42834316

42844317
static int cbm_remove_copilot_hooks(const char *manifest_path, const char *binary_path) {
4285-
char *manifest = cbm_build_copilot_hook_manifest(binary_path);
4286-
if (!manifest || !manifest_path) {
4318+
char *manifest = cbm_build_copilot_hook_manifest(binary_path, true);
4319+
char *previous = cbm_build_copilot_hook_manifest(binary_path, false);
4320+
if (!manifest || !previous || !manifest_path) {
42874321
free(manifest);
4322+
free(previous);
42884323
return CLI_ERR;
42894324
}
4290-
int rc = cbm_text_remove_owned_document(manifest_path, manifest);
4325+
const char *const released[] = {previous};
4326+
int rc = cbm_text_remove_owned_document_any(manifest_path, manifest, released, 1U);
42914327
free(manifest);
4328+
free(previous);
42924329
return rc >= 0 ? CLI_OK : CLI_ERR;
42934330
}
42944331

@@ -9193,7 +9230,8 @@ static void install_copilot_durable_context(const char *home, const char *binary
91939230
record_agent_config_error(false, "Copilot", "lifecycle_hook_install", hook_path);
91949231
}
91959232
if (hook_ok) {
9196-
printf(" hooks: SessionStart + SubagentStart (dynamic graph context)\n");
9233+
printf(" hooks: SessionStart + SubagentStart + PostToolUse searches (dynamic graph "
9234+
"context)\n");
91979235
}
91989236
}
91999237

‎src/cli/cli.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,
283283
bool case_insensitive);
284284
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event);
285285
bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz);
286+
/* include_posttool false renders the previous lifecycle-only manifest. */
287+
char *cbm_build_copilot_hook_manifest_for_testing(const char *binary_path, bool include_posttool);
286288
bool cbm_mcp_command_path_probe_safe_for_testing(const char *command, bool windows);
287289
void cbm_set_mcp_command_path_probe_counter_for_testing(int *counter);
288290
int cbm_install_editor_mcp_with_previous_for_testing(const char *binary_path,
@@ -591,8 +593,11 @@ void cbm_hook_augment_arm_deadline(void);
591593
int cbm_hook_augment_deadline_ms_for_testing(void);
592594
#endif
593595
char *cbm_hook_augment_read_stdin(void);
594-
/* Pure no-op gate for the hook-client fast path (see hook_augment.c). */
595-
bool cbm_hook_augment_input_is_noop_bash(const char *input);
596+
/* Pure no-op gate for the hook-client fast path (see hook_augment.c). It also
597+
* covers a forced search event, such as Copilot's PostToolUse; forced_event and
598+
* dialect_name are borrowed and may be NULL. */
599+
bool cbm_hook_augment_input_is_noop_for(const char *input, const char *forced_event,
600+
const char *dialect_name);
596601
/* Hand back stdin bytes main() consumed early; the next read returns them. */
597602
void cbm_hook_augment_prefetch_stdin(char *owned);
598603

0 commit comments

Comments
 (0)