| 5008 | #endif |
| 5009 | |
| 5010 | bool cbm_install_hook_gate_script(const char *home, const char *binary_path) { |
| 5011 | if (!home || !binary_path) { |
| 5012 | return false; |
| 5013 | } |
| 5014 | char config_dir[CLI_BUF_1K]; |
| 5015 | cbm_claude_config_dir(home, config_dir, sizeof(config_dir)); |
| 5016 | if (!config_dir[0]) { |
| 5017 | return false; |
| 5018 | } |
| 5019 | char hooks_dir[CLI_BUF_1K]; |
| 5020 | int hooks_written = snprintf(hooks_dir, sizeof(hooks_dir), "%s/hooks", config_dir); |
| 5021 | if (hooks_written <= 0 || (size_t)hooks_written >= sizeof(hooks_dir)) { |
| 5022 | return false; |
| 5023 | } |
| 5024 | if (!cbm_mkdir_p(hooks_dir, CLI_OCTAL_PERM)) { |
| 5025 | return false; |
| 5026 | } |
| 5027 | |
| 5028 | char script_path[CLI_BUF_1K]; |
| 5029 | int script_written = |
| 5030 | snprintf(script_path, sizeof(script_path), "%s/" CMM_HOOK_GATE_SCRIPT, hooks_dir); |
| 5031 | if (script_written <= 0 || (size_t)script_written >= sizeof(script_path)) { |
| 5032 | return false; |
| 5033 | } |
| 5034 | |
| 5035 | char script[CLI_BUF_8K]; |
| 5036 | if (cbm_build_current_hook_script(cmm_gate_script_prefix, binary_path, script, |
| 5037 | sizeof(script)) != CLI_OK) { |
| 5038 | return false; |
| 5039 | } |
| 5040 | char released_script[CLI_BUF_8K]; |
| 5041 | const char *const legacy[] = {released_script}; |
| 5042 | size_t legacy_count = cbm_build_released_gate_script(binary_path, released_script, |
| 5043 | sizeof(released_script)) == CLI_OK |
| 5044 | ? 1U |
| 5045 | : 0U; |
| 5046 | #ifdef _WIN32 |
| 5047 | if (cbm_remove_owned_legacy_hook_script(hooks_dir, CMM_HOOK_GATE_SCRIPT_LEGACY, script, legacy, |
| 5048 | legacy_count) != CLI_OK) { |
| 5049 | return false; |
| 5050 | } |
| 5051 | #endif |
| 5052 | return cbm_write_owned_hook_script_with_legacy(script_path, script, legacy, legacy_count); |
| 5053 | } |
| 5054 | |
| 5055 | /* SessionStart hook: remind agent to use MCP tools on every context reset. */ |
| 5056 | #ifdef _WIN32 |
no test coverage detected