| 5830 | #endif |
| 5831 | |
| 5832 | bool cbm_install_hook_gate_script(const char *home, const char *binary_path) { |
| 5833 | if (!home || !binary_path) { |
| 5834 | return false; |
| 5835 | } |
| 5836 | char config_dir[CLI_BUF_1K]; |
| 5837 | cbm_claude_config_dir(home, config_dir, sizeof(config_dir)); |
| 5838 | if (!config_dir[0]) { |
| 5839 | return false; |
| 5840 | } |
| 5841 | char hooks_dir[CLI_BUF_1K]; |
| 5842 | int hooks_written = snprintf(hooks_dir, sizeof(hooks_dir), "%s/hooks", config_dir); |
| 5843 | if (hooks_written <= 0 || (size_t)hooks_written >= sizeof(hooks_dir)) { |
| 5844 | return false; |
| 5845 | } |
| 5846 | if (!cbm_mkdir_p_ex(hooks_dir, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED)) { |
| 5847 | return false; |
| 5848 | } |
| 5849 | |
| 5850 | char script_path[CLI_BUF_1K]; |
| 5851 | int script_written = |
| 5852 | snprintf(script_path, sizeof(script_path), "%s/" CMM_HOOK_GATE_SCRIPT, hooks_dir); |
| 5853 | if (script_written <= 0 || (size_t)script_written >= sizeof(script_path)) { |
| 5854 | return false; |
| 5855 | } |
| 5856 | |
| 5857 | char script[CLI_BUF_8K]; |
| 5858 | if (cbm_build_current_hook_script(cmm_gate_script_prefix, binary_path, script, |
| 5859 | sizeof(script)) != CLI_OK) { |
| 5860 | return false; |
| 5861 | } |
| 5862 | char released_script[CLI_BUF_8K]; |
| 5863 | const char *const legacy[] = {released_script}; |
| 5864 | size_t legacy_count = cbm_build_released_gate_script(binary_path, released_script, |
| 5865 | sizeof(released_script)) == CLI_OK |
| 5866 | ? 1U |
| 5867 | : 0U; |
| 5868 | #ifdef _WIN32 |
| 5869 | if (cbm_remove_owned_legacy_hook_script(hooks_dir, CMM_HOOK_GATE_SCRIPT_LEGACY, script, legacy, |
| 5870 | legacy_count) != CLI_OK) { |
| 5871 | return false; |
| 5872 | } |
| 5873 | #endif |
| 5874 | return cbm_write_owned_hook_script_with_legacy(script_path, script, legacy, legacy_count); |
| 5875 | } |
| 5876 | |
| 5877 | /* SessionStart hook: remind agent to use MCP tools on every context reset. */ |
| 5878 | #ifdef _WIN32 |
no test coverage detected