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 };
41984199static 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
42174219static 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. */
42734306static 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
42844321static 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
0 commit comments