Skip to content

Commit 7cf1e4d

Browse files
authored
Merge pull request DeusData#619 from catsmonster/fix/hook-augment-windows-paths
fix(hook): make hook-augment work on Windows drive-letter paths
2 parents 67099fa + 4bb9eff commit 7cf1e4d

3 files changed

Lines changed: 62 additions & 8 deletions

File tree

‎src/cli/cli.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ int cbm_cmd_config(int argc, char **argv);
321321
* path returns 0 with no stdout output. */
322322
int cbm_cmd_hook_augment(void);
323323

324+
/* True for an absolute path the augmenter can walk up: POSIX "/..." or a
325+
* Windows drive root — "X:/..." or a bare "X:" (callers normalize '\\' to '/'
326+
* first). Exposed for tests — regression coverage for the Windows drive-letter
327+
* no-op (#618). */
328+
bool cbm_hook_path_is_abs(const char *path);
329+
324330
/* Build the agent.install.plan.v1 install receipt for <home> (issue #388):
325331
* a machine-readable JSON list of the config/instruction/hook files `install`
326332
* would write, produced WITHOUT mutating anything. Returns a heap JSON string

‎src/cli/hook_augment.c‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ static void ha_emit(const char *text) {
243243
yyjson_mut_doc_free(doc);
244244
}
245245

246+
/* True for an absolute path we can walk up: POSIX "/..." or a Windows drive
247+
* root — "X:/..." or a bare "X:" (callers normalize '\\' to '/' first).
248+
* Declared in cli.h so the Windows drive-letter handling (#618) has direct
249+
* regression coverage. */
250+
bool cbm_hook_path_is_abs(const char *d) {
251+
if (!d || !d[0]) {
252+
return false;
253+
}
254+
if (d[0] == '/') {
255+
return true;
256+
}
257+
return isalpha((unsigned char)d[0]) && d[1] == ':' && (d[2] == '/' || d[2] == '\0');
258+
}
259+
246260
/* Walk up from `start`, deriving a project name at each level and querying
247261
* search_graph until an indexed project is found (or the walk is exhausted).
248262
* Stops at the first non-error result: a valid project with zero hits is a
@@ -251,7 +265,7 @@ static char *ha_resolve_and_query(cbm_mcp_server_t *srv, const char *start, cons
251265
char dir[4096];
252266
snprintf(dir, sizeof(dir), "%s", start);
253267

254-
for (int level = 0; level < HA_MAX_WALKUP && dir[0] == '/'; level++) {
268+
for (int level = 0; level < HA_MAX_WALKUP && cbm_hook_path_is_abs(dir); level++) {
255269
char *project = cbm_project_name_from_path(dir);
256270
if (project) {
257271
char *args = ha_build_args(project, token);
@@ -275,7 +289,10 @@ static char *ha_resolve_and_query(cbm_mcp_server_t *srv, const char *start, cons
275289
/* Not indexed at this level — climb to the parent. */
276290
char *slash = strrchr(dir, '/');
277291
if (!slash || slash == dir) {
278-
break;
292+
break; /* POSIX root "/" */
293+
}
294+
if (slash == dir + 2 && dir[1] == ':') {
295+
break; /* Windows drive root "X:/" — don't strip to "X:" */
279296
}
280297
*slash = '\0';
281298
}
@@ -313,9 +330,9 @@ int cbm_cmd_hook_augment(void) {
313330
}
314331

315332
const char *cwd = ha_obj_str(root, "cwd");
316-
#ifndef _WIN32
317333
char cwdbuf[4096];
318-
if (!cwd || cwd[0] != '/') {
334+
#ifndef _WIN32
335+
if (!cwd || !cbm_hook_path_is_abs(cwd)) {
319336
if (!getcwd(cwdbuf, sizeof(cwdbuf))) {
320337
yyjson_doc_free(doc);
321338
free(input);
@@ -324,10 +341,20 @@ int cbm_cmd_hook_augment(void) {
324341
cwd = cwdbuf;
325342
}
326343
#else
327-
/* Windows: Claude Code passes cwd in the hook payload. The walk-up loop
328-
* below requires POSIX-style absolute paths ('/'-prefixed), so without a
329-
* usable cwd there is nothing to augment — fail open cleanly. */
330-
if (!cwd || cwd[0] != '/') {
344+
/* Windows: Claude Code passes an absolute drive-letter cwd in the hook
345+
* payload (e.g. C:\repo). Normalize '\\' -> '/' and require an absolute
346+
* path; the walk-up loop handles POSIX and "X:/..." roots alike. Without
347+
* a usable cwd there is nothing to augment — fail open cleanly. */
348+
if (cwd) {
349+
snprintf(cwdbuf, sizeof(cwdbuf), "%s", cwd);
350+
for (char *p = cwdbuf; *p; p++) {
351+
if (*p == '\\') {
352+
*p = '/';
353+
}
354+
}
355+
cwd = cwdbuf;
356+
}
357+
if (!cwd || !cbm_hook_path_is_abs(cwd)) {
331358
yyjson_doc_free(doc);
332359
free(input);
333360
return 0;

‎tests/test_cli.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,6 +2361,26 @@ TEST(cli_hook_gate_script_no_predictable_tmp_issue384) {
23612361
PASS();
23622362
}
23632363

2364+
/* issue #618: hook-augment was a structural no-op on Windows because its path
2365+
* guards required POSIX-style '/'-prefixed absolute paths, so a drive-letter
2366+
* cwd (C:/repo) was rejected before any search_graph query. The predicate must
2367+
* accept POSIX and Windows drive roots alike (callers normalize '\\' to '/'). */
2368+
TEST(cli_hook_augment_path_is_abs) {
2369+
/* POSIX absolute (unchanged behavior) */
2370+
ASSERT(cbm_hook_path_is_abs("/home/u/proj"));
2371+
/* Windows drive roots — the #618 regression */
2372+
ASSERT(cbm_hook_path_is_abs("C:/Users/me/proj"));
2373+
ASSERT(cbm_hook_path_is_abs("C:/"));
2374+
ASSERT(cbm_hook_path_is_abs("C:"));
2375+
ASSERT(cbm_hook_path_is_abs("d:/lowercase/drive"));
2376+
/* Not absolute → augmenter no-ops cleanly */
2377+
ASSERT(!cbm_hook_path_is_abs("relative/path"));
2378+
ASSERT(!cbm_hook_path_is_abs("proj"));
2379+
ASSERT(!cbm_hook_path_is_abs(""));
2380+
ASSERT(!cbm_hook_path_is_abs(NULL));
2381+
PASS();
2382+
}
2383+
23642384
TEST(cli_upsert_claude_hook_existing) {
23652385
char tmpdir[256];
23662386
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-XXXXXX");
@@ -3025,6 +3045,7 @@ SUITE(cli) {
30253045

30263046
/* Claude Code hooks (5 tests — group D) */
30273047
RUN_TEST(cli_hook_gate_script_no_predictable_tmp_issue384);
3048+
RUN_TEST(cli_hook_augment_path_is_abs);
30283049
RUN_TEST(cli_upsert_claude_hook_fresh);
30293050
RUN_TEST(cli_upsert_claude_hook_existing);
30303051
RUN_TEST(cli_upsert_claude_hook_replace);

0 commit comments

Comments
 (0)