| 5070 | #endif |
| 5071 | |
| 5072 | bool cbm_install_hook_gate_script(const char *home, const char *binary_path) { |
| 5073 | if (!home || !binary_path) { |
| 5074 | return false; |
| 5075 | } |
| 5076 | char config_dir[CLI_BUF_1K]; |
| 5077 | cbm_claude_config_dir(home, config_dir, sizeof(config_dir)); |
| 5078 | if (!config_dir[0]) { |
| 5079 | return false; |
| 5080 | } |
| 5081 | char hooks_dir[CLI_BUF_1K]; |
| 5082 | int hooks_written = snprintf(hooks_dir, sizeof(hooks_dir), "%s/hooks", config_dir); |
| 5083 | if (hooks_written <= 0 || (size_t)hooks_written >= sizeof(hooks_dir)) { |
| 5084 | return false; |
| 5085 | } |
| 5086 | if (!cbm_mkdir_p(hooks_dir, CLI_OCTAL_PERM)) { |
| 5087 | return false; |
| 5088 | } |
| 5089 | |
| 5090 | char script_path[CLI_BUF_1K]; |
| 5091 | int script_written = |
| 5092 | snprintf(script_path, sizeof(script_path), "%s/" CMM_HOOK_GATE_SCRIPT, hooks_dir); |
| 5093 | if (script_written <= 0 || (size_t)script_written >= sizeof(script_path)) { |
| 5094 | return false; |
| 5095 | } |
| 5096 | |
| 5097 | char script[CLI_BUF_8K]; |
| 5098 | if (cbm_build_current_hook_script(cmm_gate_script_prefix, binary_path, script, |
| 5099 | sizeof(script)) != CLI_OK) { |
| 5100 | return false; |
| 5101 | } |
| 5102 | char released_script[CLI_BUF_8K]; |
| 5103 | const char *const legacy[] = {released_script}; |
| 5104 | size_t legacy_count = cbm_build_released_gate_script(binary_path, released_script, |
| 5105 | sizeof(released_script)) == CLI_OK |
| 5106 | ? 1U |
| 5107 | : 0U; |
| 5108 | #ifdef _WIN32 |
| 5109 | if (cbm_remove_owned_legacy_hook_script(hooks_dir, CMM_HOOK_GATE_SCRIPT_LEGACY, script, legacy, |
| 5110 | legacy_count) != CLI_OK) { |
| 5111 | return false; |
| 5112 | } |
| 5113 | #endif |
| 5114 | return cbm_write_owned_hook_script_with_legacy(script_path, script, legacy, legacy_count); |
| 5115 | } |
| 5116 | |
| 5117 | /* SessionStart hook: remind agent to use MCP tools on every context reset. */ |
| 5118 | #ifdef _WIN32 |
no test coverage detected