Skip to content

Commit 7865b87

Browse files
authored
Merge pull request DeusData#1473 from DeusData/fix/dryrun-predicts-hook-refusal
fix(cli): make --dry-run predict a refused hook-script rewrite (DeusData#1387)
2 parents 4f7db18 + d35589c commit 7865b87

4 files changed

Lines changed: 209 additions & 0 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5294,6 +5294,53 @@ static bool cbm_install_subagent_reminder_script(const char *home, const char *b
52945294
return cbm_write_owned_hook_script_with_legacy(script_path, script, legacy, 1U);
52955295
}
52965296

5297+
/* #1387 dry-run predicate: would writing this hook script succeed, or would
5298+
* the owned-document migration refuse it because the file on disk is not ours?
5299+
* Read-only — it must never touch the filesystem, since a dry run promises
5300+
* exactly that. An unreadable/unsafe state counts as "would not succeed": the
5301+
* preview should warn rather than promise. */
5302+
static bool cbm_hook_script_write_would_succeed(const char *home, const char *binary_path,
5303+
const char *script_name) {
5304+
if (!home || !binary_path || !script_name) {
5305+
return false;
5306+
}
5307+
char config_dir[CLI_BUF_1K];
5308+
cbm_claude_config_dir(home, config_dir, sizeof(config_dir));
5309+
if (!config_dir[0]) {
5310+
return false;
5311+
}
5312+
char script_path[CLI_BUF_1K];
5313+
int written =
5314+
snprintf(script_path, sizeof(script_path), "%s/hooks/%s", config_dir, script_name);
5315+
if (written <= 0 || (size_t)written >= sizeof(script_path)) {
5316+
return false;
5317+
}
5318+
const char *prefix = cmm_gate_script_prefix;
5319+
if (strcmp(script_name, CMM_SESSION_REMINDER_SCRIPT) == 0) {
5320+
prefix = cmm_session_script_prefix;
5321+
} else if (strcmp(script_name, CMM_SUBAGENT_REMINDER_SCRIPT) == 0) {
5322+
prefix = cmm_subagent_script_prefix;
5323+
}
5324+
char script[CLI_BUF_8K];
5325+
if (cbm_build_current_hook_script(prefix, binary_path, script, sizeof(script)) != CLI_OK) {
5326+
return false;
5327+
}
5328+
/* Released shapes are accepted by the real write, so they must be accepted
5329+
* here too or the preview would warn about a script that upgrades fine. */
5330+
char released[CLI_BUF_8K];
5331+
const char *candidates[2];
5332+
size_t candidate_count = 0U;
5333+
if (strcmp(script_name, CMM_HOOK_GATE_SCRIPT) == 0 &&
5334+
cbm_build_released_gate_script(binary_path, released, sizeof(released)) == CLI_OK) {
5335+
candidates[candidate_count++] = released;
5336+
} else if (strcmp(script_name, CMM_SESSION_REMINDER_SCRIPT) == 0) {
5337+
candidates[candidate_count++] = cmm_released_session_script;
5338+
} else if (strcmp(script_name, CMM_SUBAGENT_REMINDER_SCRIPT) == 0) {
5339+
candidates[candidate_count++] = cmm_released_subagent_script;
5340+
}
5341+
return cbm_text_owned_document_status(script_path, script, candidates, candidate_count) == 0;
5342+
}
5343+
52975344
int cbm_upsert_claude_subagent_hooks(const char *settings_path) {
52985345
char command[CLI_BUF_8K];
52995346
char previous_command[CLI_BUF_8K];
@@ -7191,6 +7238,18 @@ static void install_claude_code_config(const char *home, const char *binary_path
71917238
bool gate_ok = dry_run;
71927239
bool session_ok = dry_run;
71937240
bool subagent_ok = dry_run;
7241+
if (dry_run) {
7242+
/* #1387 (second half): the dry run claimed EVERY hook group as
7243+
* installable because these flags were simply `true`. When the on-disk
7244+
* script is not ours the real install refuses to rewrite it, so the
7245+
* preview promised what the run could not deliver and the reporter had
7246+
* no way to see the loss coming. Predict each refusal read-only. */
7247+
gate_ok = cbm_hook_script_write_would_succeed(home, binary_path, CMM_HOOK_GATE_SCRIPT);
7248+
session_ok =
7249+
cbm_hook_script_write_would_succeed(home, binary_path, CMM_SESSION_REMINDER_SCRIPT);
7250+
subagent_ok =
7251+
cbm_hook_script_write_would_succeed(home, binary_path, CMM_SUBAGENT_REMINDER_SCRIPT);
7252+
}
71947253
if (!dry_run) {
71957254
char hook_path[CLI_BUF_1K];
71967255
gate_ok = cbm_install_hook_gate_script(home, binary_path);
@@ -7237,6 +7296,22 @@ static void install_claude_code_config(const char *home, const char *binary_path
72377296
if (subagent_ok) {
72387297
printf(" hooks: SubagentStart (MCP usage reminder for subagents)\n");
72397298
}
7299+
if (dry_run) {
7300+
/* Name every script the real run would refuse. Silence is what made
7301+
* #1387 invisible in advance: the group simply went unmentioned, which
7302+
* reads as "nothing to do" rather than "this will be skipped". */
7303+
static const char *const preview_scripts[] = {
7304+
CMM_HOOK_GATE_SCRIPT, CMM_SESSION_REMINDER_SCRIPT, CMM_SUBAGENT_REMINDER_SCRIPT};
7305+
const bool preview_ok[] = {gate_ok, session_ok, subagent_ok};
7306+
for (size_t i = 0U; i < sizeof(preview_scripts) / sizeof(preview_scripts[0]); i++) {
7307+
if (!preview_ok[i]) {
7308+
printf(" hooks: %s/hooks/%s would be skipped — the file there is not ours "
7309+
"(modified, or written by another install), so the rewrite is refused "
7310+
"and existing hook entries are left untouched\n",
7311+
config_dir, preview_scripts[i]);
7312+
}
7313+
}
7314+
}
72407315

72417316
/* Migration nudge: when CLAUDE_CONFIG_DIR is set and a legacy ~/.claude tree
72427317
* still exists, mention it so users can clean up stale artifacts. */

‎src/cli/config_text_edit.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,58 @@ static int text_matches_candidate(const char *data, size_t data_len, const char
12221222
return TEXT_OK;
12231223
}
12241224

1225+
/* Read-only classification: WOULD a migrate of this document succeed?
1226+
* Returns TEXT_OK when the path is absent or already byte-identical to the
1227+
* current or an exact released document (a write would proceed), TEXT_UNOWNED
1228+
* when it holds other bytes (a write would be refused), TEXT_ERROR on unsafe
1229+
* state. Mirrors text_migrate_owned_document's decision WITHOUT writing, so
1230+
* `install --dry-run` can predict a refusal instead of promising an install it
1231+
* cannot deliver (#1387). */
1232+
int cbm_text_owned_document_status(const char *file_path, const char *current_content,
1233+
const char *const *released_contents, size_t released_count) {
1234+
size_t current_len = 0U;
1235+
if (!text_valid_path(file_path) ||
1236+
text_bounded_strlen(current_content, TEXT_MAX_BYTES, &current_len) != TEXT_OK ||
1237+
text_validate_bytes(current_content, current_len, 1) != TEXT_OK ||
1238+
(released_count > 0U && !released_contents)) {
1239+
return TEXT_ERROR;
1240+
}
1241+
1242+
char *old_data = NULL;
1243+
size_t old_len = 0U;
1244+
text_file_snapshot_t snapshot;
1245+
if (text_read_file(file_path, &old_data, &old_len, &snapshot) != TEXT_OK ||
1246+
text_validate_bytes(old_data, old_len, 1) != TEXT_OK) {
1247+
free(old_data);
1248+
return TEXT_ERROR;
1249+
}
1250+
if (!snapshot.exists) {
1251+
free(old_data);
1252+
return TEXT_OK; /* absent -> the write creates it */
1253+
}
1254+
bool matches = false;
1255+
if (text_matches_candidate(old_data, old_len, current_content, &matches) != TEXT_OK) {
1256+
free(old_data);
1257+
return TEXT_ERROR;
1258+
}
1259+
if (matches) {
1260+
free(old_data);
1261+
return TEXT_OK;
1262+
}
1263+
for (size_t i = 0U; i < released_count; i++) {
1264+
if (text_matches_candidate(old_data, old_len, released_contents[i], &matches) != TEXT_OK) {
1265+
free(old_data);
1266+
return TEXT_ERROR;
1267+
}
1268+
if (matches) {
1269+
free(old_data);
1270+
return TEXT_OK; /* an exact released doc upgrades */
1271+
}
1272+
}
1273+
free(old_data);
1274+
return TEXT_UNOWNED;
1275+
}
1276+
12251277
static int text_migrate_owned_document(const char *file_path, const char *current_content,
12261278
const char *const *released_contents, size_t released_count,
12271279
int override_mode, unsigned int requested_mode) {

‎src/cli/config_text_edit.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ int cbm_text_ensure_owned_document(const char *file_path, const char *owned_cont
2929
/* Create current content, preserve it when already current, or atomically
3030
* upgrade only an exact byte-for-byte previously released document. Returns
3131
* 1 for user-modified/unowned content. */
32+
/* Read-only: would a migrate succeed? 0 = yes (absent/current/exact released),
33+
* 1 = refused (other bytes present), -1 = unsafe. Lets `install --dry-run`
34+
* predict a refusal instead of promising an install it cannot deliver. */
35+
int cbm_text_owned_document_status(const char *file_path, const char *current_content,
36+
const char *const *released_contents, size_t released_count);
3237
int cbm_text_migrate_owned_document(const char *file_path, const char *current_content,
3338
const char *const *released_contents, size_t released_count);
3439
/* As above, but publish the owned document with the requested low permission

‎tests/test_cli.c‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5955,6 +5955,80 @@ TEST(cli_detected_agent_summary_includes_registry_clients) {
59555955
}
59565956
#endif
59575957

5958+
#ifndef _WIN32
5959+
/* Regression for #1387 (second half): `install --dry-run` printed all three
5960+
* hook groups as if they would install, even when the on-disk hook script is
5961+
* NOT ours and the real run would refuse to rewrite it. The dry run is exactly
5962+
* where that has to be visible - the reporter could not see the loss coming. */
5963+
TEST(cli_dry_run_predicts_refused_hook_script_issue1387) {
5964+
char tmpdir[256];
5965+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-dryrun-refusal-XXXXXX");
5966+
if (!cbm_mkdtemp(tmpdir))
5967+
FAIL("cbm_mkdtemp failed");
5968+
5969+
char hooks_dir[512];
5970+
snprintf(hooks_dir, sizeof(hooks_dir), "%s/.claude/hooks", tmpdir);
5971+
if (!cbm_mkdir_p(hooks_dir, 0755))
5972+
FAIL("mkdir hooks_dir failed");
5973+
/* A gate script that is NOT ours: a manual install pointing at another
5974+
* binary. The real install refuses to rewrite it (TEXT_UNOWNED). */
5975+
char gate_path[768];
5976+
snprintf(gate_path, sizeof(gate_path), "%s/cbm-code-discovery-gate", hooks_dir);
5977+
write_test_file(gate_path, "#!/usr/bin/env bash\n"
5978+
"# codebase-memory-mcp search augmenter (Claude Code PreToolUse).\n"
5979+
"BIN=\"/opt/tools/cbm/codebase-memory-mcp\"\n"
5980+
"exec 0\n");
5981+
5982+
const char *const env_names[] = {"HOME", "PATH", "CLAUDE_CONFIG_DIR"};
5983+
char *saved[sizeof(env_names) / sizeof(env_names[0])];
5984+
for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) {
5985+
saved[i] = save_test_env(env_names[i]);
5986+
cbm_unsetenv(env_names[i]);
5987+
}
5988+
cbm_setenv("HOME", tmpdir, 1);
5989+
cbm_setenv("PATH", tmpdir, 1);
5990+
5991+
FILE *capture = tmpfile();
5992+
int saved_stdout = capture ? dup(STDOUT_FILENO) : -1;
5993+
bool redirected = false;
5994+
if (capture && saved_stdout >= 0) {
5995+
fflush(stdout);
5996+
redirected = dup2(fileno(capture), STDOUT_FILENO) >= 0;
5997+
}
5998+
if (redirected) {
5999+
(void)cbm_install_agent_configs(tmpdir, "/opt/other/codebase-memory-mcp", false, true);
6000+
fflush(stdout);
6001+
(void)dup2(saved_stdout, STDOUT_FILENO);
6002+
}
6003+
if (saved_stdout >= 0) {
6004+
close(saved_stdout);
6005+
}
6006+
char output[16384] = {0};
6007+
if (capture) {
6008+
rewind(capture);
6009+
size_t count = fread(output, 1, sizeof(output) - 1U, capture);
6010+
output[count] = '\0';
6011+
fclose(capture);
6012+
}
6013+
6014+
/* The dry run must WARN about the script it cannot rewrite, and must not
6015+
* claim the search-augmentation hook group as installable. */
6016+
bool warned = strstr(output, "cbm-code-discovery-gate") != NULL &&
6017+
(strstr(output, "not ours") != NULL ||
6018+
strstr(output, "would be skipped") != NULL || strstr(output, "refuse") != NULL);
6019+
6020+
for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) {
6021+
restore_test_env(env_names[i], saved[i]);
6022+
}
6023+
test_rmdir_r(tmpdir);
6024+
if (!redirected)
6025+
FAIL("stdout capture failed");
6026+
if (!warned)
6027+
FAIL("dry-run must predict a refused hook-script rewrite (#1387)");
6028+
PASS();
6029+
}
6030+
#endif
6031+
59586032
TEST(cli_agent_client_registry_routes_plan_install_and_uninstall) {
59596033
char tmpdir[256];
59606034
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-agent-registry-XXXXXX");
@@ -12117,6 +12191,9 @@ SUITE(cli) {
1211712191
RUN_TEST(cli_hermes_stable_shell_context_contract);
1211812192
#ifndef _WIN32
1211912193
RUN_TEST(cli_detected_agent_summary_includes_registry_clients);
12194+
#endif
12195+
#ifndef _WIN32
12196+
RUN_TEST(cli_dry_run_predicts_refused_hook_script_issue1387);
1212012197
#endif
1212112198
RUN_TEST(cli_agent_client_registry_routes_plan_install_and_uninstall);
1212212199
RUN_TEST(cli_registry_installs_kimi_rovo_amp_durable_context);

0 commit comments

Comments
 (0)