Skip to content

Commit e8dcb28

Browse files
committed
test(hook-augment): pin worktree payload-cwd resolution
The hook resolves a payload cwd inside a linked git worktree to that worktree own indexed project (and a subdir of it), never a sibling checkout or nothing, and an unindexed worktree gets a deliberate "no indexed graph project matched" notice rather than a silent 0 bytes. Main-checkout resolution is unchanged. These regression tests use a real linked worktree and lock the payload-cwd -> project invariant. Fixes DeusData#1752 Signed-off-by: Brian McNaboe <bmcnaboe@gmail.com>
1 parent b5185d1 commit e8dcb28

1 file changed

Lines changed: 173 additions & 0 deletions

File tree

‎tests/test_cli.c‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9342,6 +9342,175 @@ TEST(cli_hook_session_exhausts_project_registry_pages) {
93429342
PASS();
93439343
}
93449344

9345+
#ifndef _WIN32
9346+
/* A linked git worktree is indexed as its OWN project, keyed by its checkout
9347+
* path. The SessionStart hook must return that project for a payload cwd inside
9348+
* the worktree (or a subdir of it) — never the main checkout's — and the main
9349+
* checkout's own resolution must be unchanged. Pins the payload-cwd → project
9350+
* resolution so a worktree can never regress to resolving a sibling checkout or
9351+
* to nothing (the historical silent-0-bytes worktree bug). */
9352+
TEST(cli_hook_worktree_cwd_resolves_own_indexed_project) {
9353+
char tmpdir[256];
9354+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-worktree-XXXXXX");
9355+
if (!cbm_mkdtemp(tmpdir))
9356+
FAIL("cbm_mkdtemp failed");
9357+
char cache[512];
9358+
char maindir[512];
9359+
char wtdir[512];
9360+
char subdir[600];
9361+
snprintf(cache, sizeof(cache), "%s/cache", tmpdir);
9362+
snprintf(maindir, sizeof(maindir), "%s/checkout-main", tmpdir);
9363+
snprintf(wtdir, sizeof(wtdir), "%s/checkout-side", tmpdir);
9364+
snprintf(subdir, sizeof(subdir), "%s/nested", wtdir);
9365+
test_mkdirp(cache);
9366+
test_mkdirp(maindir);
9367+
9368+
/* A real linked worktree so is_worktree/git_common_dir/canonical_root are
9369+
* exactly what the hook resolves against in the field. */
9370+
char cmd[2048];
9371+
snprintf(cmd, sizeof(cmd),
9372+
"cd \"%s\" && git init -q && git config user.email t@t && git config user.name t && "
9373+
": > f.txt && git add f.txt && git commit -qm init && "
9374+
"git worktree add -q \"%s\" -b agl7branch",
9375+
maindir, wtdir);
9376+
if (system(cmd) != 0) {
9377+
test_rmdir_r(tmpdir);
9378+
FAIL("git worktree setup failed");
9379+
}
9380+
test_mkdirp(subdir);
9381+
9382+
/* Register each checkout as its own indexed project under the exact
9383+
* path-derived name the indexer uses (the db filename == that name). */
9384+
char *main_name = cbm_project_name_from_path(maindir);
9385+
char *wt_name = cbm_project_name_from_path(wtdir);
9386+
ASSERT_NOT_NULL(main_name);
9387+
ASSERT_NOT_NULL(wt_name);
9388+
ASSERT_STR_NEQ(main_name, wt_name);
9389+
char db_main[900];
9390+
char db_wt[900];
9391+
snprintf(db_main, sizeof(db_main), "%s/%s.db", cache, main_name);
9392+
snprintf(db_wt, sizeof(db_wt), "%s/%s.db", cache, wt_name);
9393+
cbm_store_t *sm = cbm_store_open_path(db_main);
9394+
ASSERT_NOT_NULL(sm);
9395+
ASSERT_EQ(cbm_store_upsert_project(sm, main_name, maindir), CBM_STORE_OK);
9396+
cbm_store_close(sm);
9397+
cbm_store_t *sw = cbm_store_open_path(db_wt);
9398+
ASSERT_NOT_NULL(sw);
9399+
ASSERT_EQ(cbm_store_upsert_project(sw, wt_name, wtdir), CBM_STORE_OK);
9400+
cbm_store_close(sw);
9401+
9402+
char *saved_cache = save_test_env("CBM_CACHE_DIR");
9403+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
9404+
9405+
/* The two names share a prefix but diverge (checkout-main vs checkout-side),
9406+
* so a bare-name strstr for one never matches the other's context. */
9407+
char input[1024];
9408+
snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", wtdir);
9409+
char *wt_out = cbm_hook_augment_lifecycle_json(input);
9410+
bool wt_ok = wt_out && strstr(wt_out, wt_name) && strstr(wt_out, "is indexed") &&
9411+
!strstr(wt_out, main_name);
9412+
free(wt_out);
9413+
9414+
snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", subdir);
9415+
char *sub_out = cbm_hook_augment_lifecycle_json(input);
9416+
bool sub_ok = sub_out && strstr(sub_out, wt_name) && strstr(sub_out, "is indexed");
9417+
free(sub_out);
9418+
9419+
snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",
9420+
maindir);
9421+
char *main_out = cbm_hook_augment_lifecycle_json(input);
9422+
bool main_ok = main_out && strstr(main_out, main_name) && strstr(main_out, "is indexed") &&
9423+
!strstr(main_out, wt_name);
9424+
free(main_out);
9425+
9426+
restore_test_env("CBM_CACHE_DIR", saved_cache);
9427+
free(main_name);
9428+
free(wt_name);
9429+
snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree remove --force \"%s\" >/dev/null 2>&1",
9430+
maindir, wtdir);
9431+
(void)system(cmd);
9432+
test_rmdir_r(tmpdir);
9433+
9434+
if (!wt_ok)
9435+
FAIL("worktree cwd must resolve the worktree's own indexed project");
9436+
if (!sub_ok)
9437+
FAIL("a subdir of the worktree must resolve the worktree's own project");
9438+
if (!main_ok)
9439+
FAIL("main-checkout resolution must be unchanged and not leak the worktree project");
9440+
PASS();
9441+
}
9442+
9443+
/* Completes the payload-cwd matrix (with the indexed-worktree test above and the
9444+
* deadline/logging contract in cli_hook_augment_deadline_breadcrumb_issue858): a
9445+
* linked worktree that was never indexed must yield a deliberate, non-empty "no
9446+
* project matched — run index_repository" notice, never a silent 0-byte reply
9447+
* that an agent cannot distinguish from "no matches". */
9448+
TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {
9449+
char tmpdir[256];
9450+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-unindexed-wt-XXXXXX");
9451+
if (!cbm_mkdtemp(tmpdir))
9452+
FAIL("cbm_mkdtemp failed");
9453+
char cache[512];
9454+
char maindir[512];
9455+
char wtdir[512];
9456+
snprintf(cache, sizeof(cache), "%s/cache", tmpdir);
9457+
snprintf(maindir, sizeof(maindir), "%s/checkout-main", tmpdir);
9458+
snprintf(wtdir, sizeof(wtdir), "%s/checkout-side", tmpdir);
9459+
test_mkdirp(cache);
9460+
test_mkdirp(maindir);
9461+
9462+
char cmd[2048];
9463+
snprintf(cmd, sizeof(cmd),
9464+
"cd \"%s\" && git init -q && git config user.email t@t && git config user.name t && "
9465+
": > f.txt && git add f.txt && git commit -qm init && "
9466+
"git worktree add -q \"%s\" -b agl7unindexed",
9467+
maindir, wtdir);
9468+
if (system(cmd) != 0) {
9469+
test_rmdir_r(tmpdir);
9470+
FAIL("git worktree setup failed");
9471+
}
9472+
9473+
/* Index ONLY the main checkout; the worktree is deliberately left out. */
9474+
char *main_name = cbm_project_name_from_path(maindir);
9475+
ASSERT_NOT_NULL(main_name);
9476+
char db_main[900];
9477+
snprintf(db_main, sizeof(db_main), "%s/%s.db", cache, main_name);
9478+
cbm_store_t *sm = cbm_store_open_path(db_main);
9479+
ASSERT_NOT_NULL(sm);
9480+
ASSERT_EQ(cbm_store_upsert_project(sm, main_name, maindir), CBM_STORE_OK);
9481+
cbm_store_close(sm);
9482+
9483+
char *saved_cache = save_test_env("CBM_CACHE_DIR");
9484+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
9485+
9486+
char input[1024];
9487+
snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", wtdir);
9488+
char *wt_out = cbm_hook_augment_lifecycle_json(input);
9489+
bool deliberate = wt_out && wt_out[0] && strstr(wt_out, "no indexed graph project matched") &&
9490+
!strstr(wt_out, "is indexed");
9491+
free(wt_out);
9492+
9493+
snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",
9494+
maindir);
9495+
char *main_out = cbm_hook_augment_lifecycle_json(input);
9496+
bool main_ok = main_out && strstr(main_out, main_name) && strstr(main_out, "is indexed");
9497+
free(main_out);
9498+
9499+
restore_test_env("CBM_CACHE_DIR", saved_cache);
9500+
free(main_name);
9501+
snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree remove --force \"%s\" >/dev/null 2>&1",
9502+
maindir, wtdir);
9503+
(void)system(cmd);
9504+
test_rmdir_r(tmpdir);
9505+
9506+
if (!deliberate)
9507+
FAIL("unindexed worktree must get a deliberate no-match notice, never a silent 0 bytes");
9508+
if (!main_ok)
9509+
FAIL("the indexed main checkout must still resolve in the same matrix");
9510+
PASS();
9511+
}
9512+
#endif
9513+
93459514
TEST(cli_hook_session_sanitizes_untrusted_project_metadata) {
93469515
char tmpdir[256];
93479516
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-untrusted-project-XXXXXX");
@@ -14984,6 +15153,10 @@ SUITE(cli) {
1498415153
RUN_TEST(cli_augment_session_uses_workspace_roots);
1498515154
RUN_TEST(cli_hook_session_resolves_custom_named_index_by_root_path);
1498615155
RUN_TEST(cli_hook_session_exhausts_project_registry_pages);
15156+
#ifndef _WIN32
15157+
RUN_TEST(cli_hook_worktree_cwd_resolves_own_indexed_project);
15158+
RUN_TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent);
15159+
#endif
1498715160
RUN_TEST(cli_hook_session_sanitizes_untrusted_project_metadata);
1498815161
RUN_TEST(cli_hook_ownership_requires_exact_command_identity);
1498915162
RUN_TEST(cli_gemini_hook_upgrade_migrates_released_exact_commands);

0 commit comments

Comments
 (0)