Skip to content

Commit c4e1ad4

Browse files
authored
Merge pull request DeusData#2448 from DeusData/fix/hook-augment-log-path
fix(hook-augment): log under the cache dir, never at a cut-off path
2 parents 6644fc4 + 8292e3e commit c4e1ad4

4 files changed

Lines changed: 397 additions & 9 deletions

File tree

‎src/cli/hook_augment.c‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
*
6060
* Observability (#858): a fired deadline is otherwise indistinguishable from
6161
* "no matches", so the handler first write()s a pre-formatted breadcrumb to
62-
* ~/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log (fd and message
63-
* prepared at arm time — only async-signal-safe write/_exit in the handler). */
62+
* <cache_dir>/logs/hook-augment-timeouts.log (by default
63+
* ~/.cache/codebase-memory-mcp/logs; fd and message prepared at arm time —
64+
* only async-signal-safe write/_exit in the handler). */
6465
#ifndef _WIN32
6566
#define HA_DEADLINE_DEFAULT_MS 2000 /* in-process budget; see ha_deadline_ms() */
6667
#define HA_DEADLINE_MIN_MS 50
@@ -103,20 +104,31 @@ static void ha_deadline_exit(int sig) {
103104
_exit(0);
104105
}
105106

107+
/* Whether snprintf wrote the whole string into a buffer of `cap` bytes. */
108+
static bool ha_fits(int written, size_t cap) {
109+
return written > 0 && (size_t)written < cap;
110+
}
111+
106112
static void ha_open_crumb_log(int deadline_ms) {
107113
const char *override = getenv("CBM_HOOK_TIMEOUT_LOG"); /* tests + power users */
108114
char path[CBM_SZ_1K];
115+
/* A path cut off at the buffer names some other file, and O_CREAT would
116+
* create it. Such a path means no breadcrumb; the hook itself runs on. */
109117
if (override && override[0]) {
110-
snprintf(path, sizeof(path), "%s", override);
111-
} else {
112-
const char *home = getenv("HOME");
113-
if (!home || !home[0]) {
118+
if (!ha_fits(snprintf(path, sizeof(path), "%s", override), sizeof(path))) {
114119
return;
115120
}
121+
} else {
122+
/* <cache_dir>/logs, where every cbm component logs: the cache dir is
123+
* CBM_CACHE_DIR, else HOME (then USERPROFILE) + the default. */
124+
const char *cache = cbm_resolve_cache_dir();
116125
char dir[CBM_SZ_1K];
117-
snprintf(dir, sizeof(dir), "%s/.cache/codebase-memory-mcp/logs", home);
126+
if (!cache || !ha_fits(snprintf(dir, sizeof(dir), "%s/logs", cache), sizeof(dir)) ||
127+
!ha_fits(snprintf(path, sizeof(path), "%s/hook-augment-timeouts.log", dir),
128+
sizeof(path))) {
129+
return;
130+
}
118131
cbm_mkdir_p_ex(dir, 0755, CBM_MKDIR_FOLLOW_OWNED);
119-
snprintf(path, sizeof(path), "%s/hook-augment-timeouts.log", dir);
120132
}
121133
g_ha_crumb_fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
122134
if (g_ha_crumb_fd < 0) {

‎src/mcp/mcp.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10445,7 +10445,13 @@ static bool write_skip_logfile(const char *project, const cbm_file_error_t *errs
1044510445
char path[CBM_SZ_1K];
1044610446
const char *override = getenv("CBM_INDEX_LOG");
1044710447
if (override && override[0]) {
10448-
snprintf(path, sizeof(path), "%s", override);
10448+
/* An override that does not fit is cut off into another file's name,
10449+
* and "wb" would create or truncate that file: no logfile then. */
10450+
int written = snprintf(path, sizeof(path), "%s", override);
10451+
if (written <= 0 || (size_t)written >= sizeof(path)) {
10452+
cbm_log_warn("index.logfile_path_too_long", "source", "CBM_INDEX_LOG");
10453+
return false;
10454+
}
1044910455
} else {
1045010456
const char *cdir = cbm_resolve_cache_dir();
1045110457
if (!cdir) {

‎tests/test_cli.c‎

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14985,6 +14985,260 @@ TEST(cli_hook_augment_deadline_breadcrumb_issue858) {
1498514985
#endif
1498614986
}
1498714987

14988+
/* ── hook-augment breadcrumb log location ──────────────────────────
14989+
* Arming the deadline opens the breadcrumb log with O_CREAT. Its path was
14990+
* "$HOME/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log" built in
14991+
* 1 KiB buffers with no length check, so a long HOME created a file at a
14992+
* cut-off name, and CBM_CACHE_DIR, which every other cbm log honours, was
14993+
* ignored. These run the real entry point in a child with a no-op Bash
14994+
* payload: the deadline is armed (the log opened), then the hook passes
14995+
* through with exit 0. POSIX only: the in-process deadline and its log are
14996+
* compiled out on Windows. */
14997+
#ifndef _WIN32
14998+
enum { HL_PATH_CAP = 1024, HL_SEGMENT = 200 };
14999+
15000+
/* Exit code of `hook-augment` run in a child with HOME = home and
15001+
* CBM_CACHE_DIR = cache_dir (unset when NULL), or -1 when it did not exit. */
15002+
static int hl_run_hook(const char *home, const char *cache_dir) {
15003+
static const char payload[] = "{\"hook_event_name\":\"PreToolUse\",\"tool_name\":\"Bash\","
15004+
"\"tool_input\":{\"command\":\"ls\"}}";
15005+
int fds[2];
15006+
if (pipe(fds) != 0) {
15007+
return -1;
15008+
}
15009+
/* Queued before the fork, so no write can meet a reader that has gone. */
15010+
ssize_t queued = write(fds[1], payload, sizeof(payload) - 1);
15011+
close(fds[1]);
15012+
if (queued != (ssize_t)(sizeof(payload) - 1)) {
15013+
close(fds[0]);
15014+
return -1;
15015+
}
15016+
fflush(NULL);
15017+
pid_t pid = fork();
15018+
if (pid == 0) {
15019+
dup2(fds[0], STDIN_FILENO);
15020+
close(fds[0]);
15021+
cbm_setenv("HOME", home, 1);
15022+
cbm_unsetenv("USERPROFILE");
15023+
cbm_unsetenv("CBM_HOOK_TIMEOUT_LOG");
15024+
if (cache_dir) {
15025+
cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
15026+
} else {
15027+
cbm_unsetenv("CBM_CACHE_DIR");
15028+
}
15029+
_exit(cbm_cmd_hook_augment(0, NULL));
15030+
}
15031+
close(fds[0]);
15032+
int status = 0;
15033+
if (pid < 0 || waitpid(pid, &status, 0) != pid || !WIFEXITED(status)) {
15034+
return -1;
15035+
}
15036+
return WEXITSTATUS(status);
15037+
}
15038+
15039+
/* A fresh synthetic root with its links resolved. The kernel bounds the
15040+
* EXPANDED path, and macOS /tmp is a link to /private/tmp: under the link a
15041+
* near-1 KiB path can be neither created nor probed, which hides the very
15042+
* cut-off file these tests look for. */
15043+
static bool hl_make_root(char *root, size_t root_sz) {
15044+
char tmpl[256];
15045+
char real[4096];
15046+
snprintf(tmpl, sizeof(tmpl), "/tmp/cbm-hooklog-XXXXXX");
15047+
if (!cbm_mkdtemp(tmpl)) {
15048+
return false;
15049+
}
15050+
if (!cbm_canonical_path(tmpl, real, sizeof(real))) {
15051+
th_rmtree(tmpl);
15052+
return false;
15053+
}
15054+
int n = snprintf(root, root_sz, "%s", real);
15055+
return n > 0 && (size_t)n < root_sz;
15056+
}
15057+
15058+
/* "<root>/hhh…/hhh…" of exactly `len` bytes, every component short of
15059+
* NAME_MAX. False when it cannot be built. */
15060+
static bool hl_long_dir(char *out, size_t out_sz, const char *root, size_t len) {
15061+
int n = snprintf(out, out_sz, "%s", root);
15062+
if (n <= 0 || (size_t)n >= len || len >= out_sz) {
15063+
return false;
15064+
}
15065+
size_t at = (size_t)n;
15066+
while (at < len) {
15067+
out[at++] = '/';
15068+
for (int seg = 0; seg < HL_SEGMENT && at < len; seg++) {
15069+
out[at++] = 'h';
15070+
}
15071+
}
15072+
out[at] = '\0';
15073+
return out[len - 1] != '/';
15074+
}
15075+
15076+
/* The first HL_PATH_CAP - 1 bytes of "<base>/<suffix>": the name the old
15077+
* unchecked 1 KiB buffer cut that path down to. */
15078+
static void hl_cut_name(char *out, const char *base, const char *suffix) {
15079+
char full[2 * HL_PATH_CAP];
15080+
snprintf(full, sizeof(full), "%s/%s", base, suffix);
15081+
snprintf(out, HL_PATH_CAP, "%.*s", HL_PATH_CAP - 1, full);
15082+
}
15083+
15084+
static bool hl_is_ancestor_or_self(const char *path, const char *of) {
15085+
size_t n = strlen(path);
15086+
return of && strncmp(of, path, n) == 0 && (of[n] == '/' || of[n] == '\0');
15087+
}
15088+
15089+
/* Entries under dir that are neither keep_a, keep_b nor one of their
15090+
* ancestors: whatever the hook created. Each is printed by its tail. */
15091+
static int hl_count_strays(const char *dir, const char *keep_a, const char *keep_b) {
15092+
cbm_dir_t *d = cbm_opendir(dir);
15093+
if (!d) {
15094+
return 0;
15095+
}
15096+
int strays = 0;
15097+
cbm_dirent_t *entry;
15098+
while ((entry = cbm_readdir(d)) != NULL) {
15099+
if (strcmp(entry->name, ".") == 0 || strcmp(entry->name, "..") == 0) {
15100+
continue;
15101+
}
15102+
char child[HL_PATH_CAP];
15103+
int n = snprintf(child, sizeof(child), "%s/%s", dir, entry->name);
15104+
bool named = n > 0 && (size_t)n < sizeof(child);
15105+
if (named &&
15106+
(hl_is_ancestor_or_self(child, keep_a) || hl_is_ancestor_or_self(child, keep_b))) {
15107+
if (entry->is_dir) {
15108+
strays += hl_count_strays(child, keep_a, keep_b);
15109+
}
15110+
continue;
15111+
}
15112+
size_t len = named ? (size_t)n : strlen(child);
15113+
printf(" stray entry: ...%s\n", child + (len > 60 ? len - 60 : 0));
15114+
strays++;
15115+
}
15116+
cbm_closedir(d);
15117+
return strays;
15118+
}
15119+
#endif
15120+
15121+
/* A HOME of ~1 KiB: the log path "<HOME>/.cache/codebase-memory-mcp/logs/
15122+
* hook-augment-timeouts.log" does not fit the buffer. The old code opened its
15123+
* cut-off prefix ".../logs/hook-augme" with O_CREAT. Nothing may be created at
15124+
* a cut path (nothing at all under the synthetic root), and the hook must
15125+
* still exit 0: it never fails the host agent's hook call. */
15126+
TEST(cli_hook_augment_log_long_home_creates_no_cut_path) {
15127+
#ifdef _WIN32
15128+
SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
15129+
#else
15130+
enum { HL_HOME_LEN = 980 }; /* "<HOME>/.cache/.../logs" fits 1 KiB, the file name does not */
15131+
char root[256];
15132+
if (!hl_make_root(root, sizeof(root))) {
15133+
FAIL("synthetic root failed");
15134+
}
15135+
char home[HL_PATH_CAP];
15136+
bool built = hl_long_dir(home, sizeof(home), root, HL_HOME_LEN) && th_mkdir_p(home) == 0;
15137+
char cut[HL_PATH_CAP];
15138+
hl_cut_name(cut, home, ".cache/codebase-memory-mcp/logs/hook-augment-timeouts.log");
15139+
15140+
int rc = built ? hl_run_hook(home, NULL) : -1;
15141+
bool cut_exists = cbm_file_exists(cut);
15142+
int strays = hl_count_strays(root, home, NULL);
15143+
test_rmdir_r(root);
15144+
15145+
ASSERT_TRUE(built);
15146+
ASSERT_EQ(rc, 0);
15147+
if (cut_exists) {
15148+
printf(" log opened at the cut-off name ...%s\n", cut + strlen(cut) - 40);
15149+
}
15150+
ASSERT_FALSE(cut_exists);
15151+
ASSERT_EQ(strays, 0);
15152+
PASS();
15153+
#endif
15154+
}
15155+
15156+
/* CBM_CACHE_DIR is where every cbm log goes (<cache_dir>/logs), so the hook's
15157+
* breadcrumb goes there too, and not under HOME. A CBM_CACHE_DIR of ~1 KiB
15158+
* whose log path does not fit is no log at all: no cut-off file, no stray
15159+
* directory, and the hook still exits 0. */
15160+
TEST(cli_hook_augment_log_honours_cache_dir) {
15161+
#ifdef _WIN32
15162+
SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
15163+
#else
15164+
enum { HL_CACHE_LEN = 1000 }; /* "<cache>/logs" fits 1 KiB, the file name does not */
15165+
char root[256];
15166+
if (!hl_make_root(root, sizeof(root))) {
15167+
FAIL("synthetic root failed");
15168+
}
15169+
char home[512];
15170+
char cache[512];
15171+
char expected[640];
15172+
snprintf(home, sizeof(home), "%s/home", root);
15173+
snprintf(cache, sizeof(cache), "%s/cache", root);
15174+
snprintf(expected, sizeof(expected), "%s/logs/hook-augment-timeouts.log", cache);
15175+
bool made = th_mkdir_p(home) == 0 && th_mkdir_p(cache) == 0;
15176+
15177+
/* A configured cache dir holds the log; HOME is left alone. */
15178+
int rc = made ? hl_run_hook(home, cache) : -1;
15179+
bool in_cache = cbm_file_exists(expected);
15180+
int strays = hl_count_strays(root, home, expected);
15181+
15182+
/* A configured cache dir too long for the log path: no log anywhere. */
15183+
char long_cache[HL_PATH_CAP];
15184+
char cut[HL_PATH_CAP];
15185+
bool long_built = hl_long_dir(long_cache, sizeof(long_cache), root, HL_CACHE_LEN) &&
15186+
th_mkdir_p(long_cache) == 0;
15187+
hl_cut_name(cut, long_cache, "logs/hook-augment-timeouts.log");
15188+
bool reset = th_rmtree(home) == 0 && th_rmtree(cache) == 0 && th_mkdir_p(home) == 0;
15189+
int long_rc = long_built && reset ? hl_run_hook(home, long_cache) : -1;
15190+
bool cut_exists = cbm_file_exists(cut);
15191+
int long_strays = hl_count_strays(root, home, long_cache);
15192+
test_rmdir_r(root);
15193+
15194+
ASSERT_TRUE(made);
15195+
ASSERT_EQ(rc, 0);
15196+
if (!in_cache) {
15197+
printf(" no breadcrumb log under CBM_CACHE_DIR/logs\n");
15198+
}
15199+
ASSERT_TRUE(in_cache);
15200+
ASSERT_EQ(strays, 0);
15201+
ASSERT_TRUE(long_built);
15202+
ASSERT_TRUE(reset);
15203+
ASSERT_EQ(long_rc, 0);
15204+
ASSERT_FALSE(cut_exists);
15205+
ASSERT_EQ(long_strays, 0);
15206+
PASS();
15207+
#endif
15208+
}
15209+
15210+
/* Positive control: with a normal HOME and no CBM_CACHE_DIR the breadcrumb log
15211+
* stays exactly where it always was,
15212+
* "$HOME/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log", and the
15213+
* hook creates nothing else. */
15214+
TEST(cli_hook_augment_log_default_location_unchanged) {
15215+
#ifdef _WIN32
15216+
SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
15217+
#else
15218+
char root[256];
15219+
if (!hl_make_root(root, sizeof(root))) {
15220+
FAIL("synthetic root failed");
15221+
}
15222+
char home[512];
15223+
char expected[640];
15224+
snprintf(home, sizeof(home), "%s/home", root);
15225+
snprintf(expected, sizeof(expected),
15226+
"%s/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log", home);
15227+
bool made = th_mkdir_p(home) == 0;
15228+
15229+
int rc = made ? hl_run_hook(home, NULL) : -1;
15230+
bool at_default = cbm_file_exists(expected);
15231+
int strays = hl_count_strays(root, expected, NULL);
15232+
test_rmdir_r(root);
15233+
15234+
ASSERT_TRUE(made);
15235+
ASSERT_EQ(rc, 0);
15236+
ASSERT_TRUE(at_default);
15237+
ASSERT_EQ(strays, 0);
15238+
PASS();
15239+
#endif
15240+
}
15241+
1498815242
TEST(cli_upsert_claude_hook_existing) {
1498915243
char tmpdir[256];
1499015244
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-XXXXXX");
@@ -17094,6 +17348,9 @@ SUITE(cli) {
1709417348
RUN_TEST(cli_claude_exec_hooks_custom_dir_uninstall_issue1733);
1709517349
RUN_TEST(cli_hook_augment_path_is_abs);
1709617350
RUN_TEST(cli_hook_augment_deadline_breadcrumb_issue858);
17351+
RUN_TEST(cli_hook_augment_log_long_home_creates_no_cut_path);
17352+
RUN_TEST(cli_hook_augment_log_honours_cache_dir);
17353+
RUN_TEST(cli_hook_augment_log_default_location_unchanged);
1709717354
RUN_TEST(cli_upsert_claude_hook_fresh);
1709817355
RUN_TEST(cli_upsert_claude_hook_existing);
1709917356
RUN_TEST(cli_tool_hooks_preserve_foreign_same_matcher);

0 commit comments

Comments
 (0)