Skip to content

Commit 129443a

Browse files
authored
Merge pull request DeusData#2801 from DeusData/feat/copilot-posttooluse-hook
feat(cli): give Copilot searches graph context through a PostToolUse hook
2 parents 8530c8c + 5350b5b commit 129443a

7 files changed

Lines changed: 436 additions & 62 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,14 +695,14 @@ overwrite user-modified agents.
695695
| Agent | Activation | MCP config | Durable context / augmentation |
696696
|-------|------------|------------|--------------------------------|
697697
| Claude Code | Detected | `~/.claude.json` | Skill + three exact-tool graph agents; `SessionStart`, `SubagentStart`, non-blocking `PreToolUse` for `Grep`/`Glob`/`Bash`, and post-`Read` coverage |
698-
| Codex CLI | Detected | `$CODEX_HOME/config.toml` | Managed `AGENTS.md` activation pointer, skill, three read-only agents; `SessionStart` + `SubagentStart` |
698+
| Codex CLI | Detected | `$CODEX_HOME/config.toml` | Managed `AGENTS.md` activation pointer, skill, three read-only agents; `SessionStart` + `SubagentStart`, `PreToolUse` graph lookup for `Bash` searches |
699699
| Gemini CLI | Detected | `.gemini/settings.json` | `GEMINI.md`, three explicit read/graph-tool subagents; `BeforeTool`, `AfterTool` `read_file` coverage, and `SessionStart` |
700700
| Zed | Detected | platform `settings.json` (JSONC) | `AGENTS.md` + shared skill |
701701
| OpenCode | Detected | `$OPENCODE_CONFIG` or resolved global config | `AGENTS.md`, skill, three deny-by-default read-only agents; plugin adds grep/glob graph lookup, post-`read` coverage, first-tool-result session context, and post-compaction reinjection |
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` |
@@ -751,7 +751,11 @@ Hooks installed by this project are fail-open and context-only. Claude Code's
751751
`PreToolUse` observes `Grep`/`Glob`/`Bash` and injects matching graph symbols as
752752
`additionalContext`; `PostToolUse` on `Read` adds targeted coverage context when
753753
the graph could not fully parse or index that file. It never denies or replaces
754-
the requested tool call.
754+
the requested tool call. Codex CLI's `PreToolUse` on `Bash` gives its shell
755+
searches the same context. GitHub Copilot CLI and VS Code's Copilot runtime get
756+
it after the search: their `preToolUse` can only decide permissions, so a
757+
`PostToolUse` entry for `Bash`, `Grep` and `Glob` appends the graph symbols to
758+
the tool result.
755759

756760
Claude Code, Codex CLI, Qwen Code, GitHub Copilot CLI, and VS Code's Copilot
757761
runtime receive paired session/subagent context where the vendor exposes a

‎scripts/memory-core-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal/cbm/sqlite_writer.c 74
1616
src/cli/activation_transaction.c 80
1717
src/cli/agent_clients.c 73
1818
src/cli/agent_profiles.c 7
19-
src/cli/cli.c 170
19+
src/cli/cli.c 166
2020
src/cli/client_adapter.c 3
2121
src/cli/config_edit_path.c 2
2222
src/cli/config_json_like.c 142

‎src/cli/cli.c‎

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "foundation/compat.h"
2323
#include "foundation/platform.h"
2424
#include "foundation/constants.h"
25+
#include "foundation/json_writer.h"
2526
#include "foundation/log.h"
2627
#include "foundation/mem_core.h"
2728
#include "foundation/sha256.h"
@@ -4198,7 +4199,8 @@ enum { COPILOT_HOOK_TIMEOUT_SEC = 5 };
41984199
static int cbm_build_copilot_hook_command(const char *binary_path, const char *event,
41994200
bool powershell, char *out, size_t out_size) {
42004201
if (!binary_path || !event || !out ||
4201-
(strcmp(event, "SessionStart") != 0 && strcmp(event, "SubagentStart") != 0)) {
4202+
(strcmp(event, "SessionStart") != 0 && strcmp(event, "SubagentStart") != 0 &&
4203+
strcmp(event, "PostToolUse") != 0)) {
42024204
return CLI_ERR;
42034205
}
42044206
char quoted[CLI_BUF_8K];
@@ -4215,11 +4217,12 @@ static int cbm_build_copilot_hook_command(const char *binary_path, const char *e
42154217
}
42164218

42174219
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) {
4220+
const char *event_key, const char *matcher,
4221+
const char *bash_command, const char *powershell_command) {
42204222
yyjson_mut_val *entries = yyjson_mut_arr(doc);
42214223
yyjson_mut_val *entry = yyjson_mut_obj(doc);
42224224
if (!entries || !entry || !yyjson_mut_obj_add_str(doc, entry, "type", "command") ||
4225+
(matcher && !yyjson_mut_obj_add_str(doc, entry, "matcher", matcher)) ||
42234226
!yyjson_mut_obj_add_str(doc, entry, "bash", bash_command) ||
42244227
!yyjson_mut_obj_add_str(doc, entry, "powershell", powershell_command) ||
42254228
!yyjson_mut_obj_add_int(doc, entry, "timeoutSec", COPILOT_HOOK_TIMEOUT_SEC) ||
@@ -4230,19 +4233,37 @@ static bool cbm_copilot_add_hook_event(yyjson_mut_doc *doc, yyjson_mut_val *hook
42304233
return true;
42314234
}
42324235

4233-
static char *cbm_build_copilot_hook_manifest(const char *binary_path) {
4236+
/* Copilot CLI's preToolUse cannot add model context (its output is only a
4237+
* permission decision) and fails closed on hook errors, so searches are
4238+
* augmented after they run: postToolUse additionalContext is appended to the
4239+
* tool result the model sees. The PascalCase event name selects the
4240+
* Claude-format payload (tool_name Bash/Grep/Glob, tool_input), and the matcher
4241+
* names the Claude and the runtime tool names so either matcher reading fires. */
4242+
#define COPILOT_SEARCH_MATCHER "Bash|Grep|Glob|bash|powershell|grep|rg|glob"
4243+
4244+
/* include_posttool renders the current manifest; without it the function
4245+
* renders the previous lifecycle-only manifest, which installs up to v0.12 wrote
4246+
* and which upgrades and uninstalls must still recognize as owned. The result is
4247+
* released with cbm_free(CBM_MEM_CLASS_OTHER, ...). */
4248+
static char *cbm_build_copilot_hook_manifest(const char *binary_path, bool include_posttool) {
42344249
char session_bash[CLI_BUF_8K];
42354250
char session_powershell[CLI_BUF_8K];
42364251
char subagent_bash[CLI_BUF_8K];
42374252
char subagent_powershell[CLI_BUF_8K];
4253+
char post_bash[CLI_BUF_8K];
4254+
char post_powershell[CLI_BUF_8K];
42384255
if (cbm_build_copilot_hook_command(binary_path, "SessionStart", false, session_bash,
42394256
sizeof(session_bash)) != CLI_OK ||
42404257
cbm_build_copilot_hook_command(binary_path, "SessionStart", true, session_powershell,
42414258
sizeof(session_powershell)) != CLI_OK ||
42424259
cbm_build_copilot_hook_command(binary_path, "SubagentStart", false, subagent_bash,
42434260
sizeof(subagent_bash)) != CLI_OK ||
42444261
cbm_build_copilot_hook_command(binary_path, "SubagentStart", true, subagent_powershell,
4245-
sizeof(subagent_powershell)) != CLI_OK) {
4262+
sizeof(subagent_powershell)) != CLI_OK ||
4263+
cbm_build_copilot_hook_command(binary_path, "PostToolUse", false, post_bash,
4264+
sizeof(post_bash)) != CLI_OK ||
4265+
cbm_build_copilot_hook_command(binary_path, "PostToolUse", true, post_powershell,
4266+
sizeof(post_powershell)) != CLI_OK) {
42464267
return NULL;
42474268
}
42484269

@@ -4256,39 +4277,59 @@ static char *cbm_build_copilot_hook_manifest(const char *binary_path) {
42564277
return NULL;
42574278
}
42584279
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);
4265-
char *manifest = ok ? yyjson_mut_write(doc, YYJSON_WRITE_PRETTY, NULL) : NULL;
4280+
bool ok = yyjson_mut_obj_add_int(doc, root, "version", 1) &&
4281+
cbm_copilot_add_hook_event(doc, hooks, "sessionStart", NULL, session_bash,
4282+
session_powershell) &&
4283+
cbm_copilot_add_hook_event(doc, hooks, "subagentStart", NULL, subagent_bash,
4284+
subagent_powershell) &&
4285+
(!include_posttool ||
4286+
cbm_copilot_add_hook_event(doc, hooks, "PostToolUse", COPILOT_SEARCH_MATCHER,
4287+
post_bash, post_powershell)) &&
4288+
yyjson_mut_obj_add_val(doc, root, "hooks", hooks);
4289+
char *manifest = ok ? yyjson_mut_write_opts(doc, YYJSON_WRITE_PRETTY,
4290+
cbm_json_writer_allocator(), NULL, NULL)
4291+
: NULL;
42664292
yyjson_mut_doc_free(doc);
42674293
return manifest;
42684294
}
42694295

4296+
#ifdef CBM_CLI_ENABLE_TEST_API
4297+
char *cbm_build_copilot_hook_manifest_for_testing(const char *binary_path, bool include_posttool) {
4298+
return cbm_build_copilot_hook_manifest(binary_path, include_posttool);
4299+
}
4300+
#endif
4301+
42704302
/* 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. */
4303+
* filename as an exact-owned document: a foreign collision fails closed. The
4304+
* current manifest or the exact previous one is ours, so install upgrades the
4305+
* previous one in place and uninstall removes either. */
42734306
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) {
4276-
free(manifest);
4307+
char *manifest = cbm_build_copilot_hook_manifest(binary_path, true);
4308+
char *previous = cbm_build_copilot_hook_manifest(binary_path, false);
4309+
if (!manifest || !previous || !manifest_path) {
4310+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4311+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
42774312
return CLI_ERR;
42784313
}
4279-
int rc = cbm_text_ensure_owned_document(manifest_path, manifest);
4280-
free(manifest);
4314+
const char *const released[] = {previous};
4315+
int rc = cbm_text_migrate_owned_document(manifest_path, manifest, released, 1U);
4316+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4317+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
42814318
return rc == 0 ? CLI_OK : CLI_ERR;
42824319
}
42834320

42844321
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) {
4287-
free(manifest);
4322+
char *manifest = cbm_build_copilot_hook_manifest(binary_path, true);
4323+
char *previous = cbm_build_copilot_hook_manifest(binary_path, false);
4324+
if (!manifest || !previous || !manifest_path) {
4325+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4326+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
42884327
return CLI_ERR;
42894328
}
4290-
int rc = cbm_text_remove_owned_document(manifest_path, manifest);
4291-
free(manifest);
4329+
const char *const released[] = {previous};
4330+
int rc = cbm_text_remove_owned_document_any(manifest_path, manifest, released, 1U);
4331+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4332+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
42924333
return rc >= 0 ? CLI_OK : CLI_ERR;
42934334
}
42944335

@@ -9193,7 +9234,8 @@ static void install_copilot_durable_context(const char *home, const char *binary
91939234
record_agent_config_error(false, "Copilot", "lifecycle_hook_install", hook_path);
91949235
}
91959236
if (hook_ok) {
9196-
printf(" hooks: SessionStart + SubagentStart (dynamic graph context)\n");
9237+
printf(" hooks: SessionStart + SubagentStart + PostToolUse searches (dynamic graph "
9238+
"context)\n");
91979239
}
91989240
}
91999241

‎src/cli/cli.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ 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. Release
287+
* the result with cbm_free(CBM_MEM_CLASS_OTHER, ...). */
288+
char *cbm_build_copilot_hook_manifest_for_testing(const char *binary_path, bool include_posttool);
286289
bool cbm_mcp_command_path_probe_safe_for_testing(const char *command, bool windows);
287290
void cbm_set_mcp_command_path_probe_counter_for_testing(int *counter);
288291
int cbm_install_editor_mcp_with_previous_for_testing(const char *binary_path,
@@ -591,8 +594,11 @@ void cbm_hook_augment_arm_deadline(void);
591594
int cbm_hook_augment_deadline_ms_for_testing(void);
592595
#endif
593596
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);
597+
/* Pure no-op gate for the hook-client fast path (see hook_augment.c). It also
598+
* covers a forced search event, such as Copilot's PostToolUse; forced_event and
599+
* dialect_name are borrowed and may be NULL. */
600+
bool cbm_hook_augment_input_is_noop_for(const char *input, const char *forced_event,
601+
const char *dialect_name);
596602
/* Hand back stdin bytes main() consumed early; the next read returns them. */
597603
void cbm_hook_augment_prefetch_stdin(char *owned);
598604

0 commit comments

Comments
 (0)