@@ -10380,6 +10380,175 @@ TEST(cli_hook_session_exhausts_project_registry_pages) {
1038010380 PASS();
1038110381}
1038210382
10383+ #ifndef _WIN32
10384+ /* A linked git worktree is indexed as its OWN project, keyed by its checkout
10385+ * path. The SessionStart hook must return that project for a payload cwd inside
10386+ * the worktree (or a subdir of it) — never the main checkout's — and the main
10387+ * checkout's own resolution must be unchanged. Pins the payload-cwd → project
10388+ * resolution so a worktree can never regress to resolving a sibling checkout or
10389+ * to nothing (the historical silent-0-bytes worktree bug). */
10390+ TEST(cli_hook_worktree_cwd_resolves_own_indexed_project) {
10391+ char tmpdir[256];
10392+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-worktree-XXXXXX");
10393+ if (!cbm_mkdtemp(tmpdir))
10394+ FAIL("cbm_mkdtemp failed");
10395+ char cache[512];
10396+ char maindir[512];
10397+ char wtdir[512];
10398+ char subdir[600];
10399+ snprintf(cache, sizeof(cache), "%s/cache", tmpdir);
10400+ snprintf(maindir, sizeof(maindir), "%s/checkout-main", tmpdir);
10401+ snprintf(wtdir, sizeof(wtdir), "%s/checkout-side", tmpdir);
10402+ snprintf(subdir, sizeof(subdir), "%s/nested", wtdir);
10403+ test_mkdirp(cache);
10404+ test_mkdirp(maindir);
10405+
10406+ /* A real linked worktree so is_worktree/git_common_dir/canonical_root are
10407+ * exactly what the hook resolves against in the field. */
10408+ char cmd[2048];
10409+ snprintf(cmd, sizeof(cmd),
10410+ "cd \"%s\" && git init -q && git config user.email t@t && git config user.name t && "
10411+ ": > f.txt && git add f.txt && git commit -qm init && "
10412+ "git worktree add -q \"%s\" -b agl7branch",
10413+ maindir, wtdir);
10414+ if (system(cmd) != 0) {
10415+ test_rmdir_r(tmpdir);
10416+ FAIL("git worktree setup failed");
10417+ }
10418+ test_mkdirp(subdir);
10419+
10420+ /* Register each checkout as its own indexed project under the exact
10421+ * path-derived name the indexer uses (the db filename == that name). */
10422+ char *main_name = cbm_project_name_from_path(maindir);
10423+ char *wt_name = cbm_project_name_from_path(wtdir);
10424+ ASSERT_NOT_NULL(main_name);
10425+ ASSERT_NOT_NULL(wt_name);
10426+ ASSERT_STR_NEQ(main_name, wt_name);
10427+ char db_main[900];
10428+ char db_wt[900];
10429+ snprintf(db_main, sizeof(db_main), "%s/%s.db", cache, main_name);
10430+ snprintf(db_wt, sizeof(db_wt), "%s/%s.db", cache, wt_name);
10431+ cbm_store_t *sm = cbm_store_open_path(db_main);
10432+ ASSERT_NOT_NULL(sm);
10433+ ASSERT_EQ(cbm_store_upsert_project(sm, main_name, maindir), CBM_STORE_OK);
10434+ cbm_store_close(sm);
10435+ cbm_store_t *sw = cbm_store_open_path(db_wt);
10436+ ASSERT_NOT_NULL(sw);
10437+ ASSERT_EQ(cbm_store_upsert_project(sw, wt_name, wtdir), CBM_STORE_OK);
10438+ cbm_store_close(sw);
10439+
10440+ char *saved_cache = save_test_env("CBM_CACHE_DIR");
10441+ cbm_setenv("CBM_CACHE_DIR", cache, 1);
10442+
10443+ /* The two names share a prefix but diverge (checkout-main vs checkout-side),
10444+ * so a bare-name strstr for one never matches the other's context. */
10445+ char input[1024];
10446+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", wtdir);
10447+ char *wt_out = cbm_hook_augment_lifecycle_json(input);
10448+ bool wt_ok = wt_out && strstr(wt_out, wt_name) && strstr(wt_out, "is indexed") &&
10449+ !strstr(wt_out, main_name);
10450+ free(wt_out);
10451+
10452+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", subdir);
10453+ char *sub_out = cbm_hook_augment_lifecycle_json(input);
10454+ bool sub_ok = sub_out && strstr(sub_out, wt_name) && strstr(sub_out, "is indexed");
10455+ free(sub_out);
10456+
10457+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",
10458+ maindir);
10459+ char *main_out = cbm_hook_augment_lifecycle_json(input);
10460+ bool main_ok = main_out && strstr(main_out, main_name) && strstr(main_out, "is indexed") &&
10461+ !strstr(main_out, wt_name);
10462+ free(main_out);
10463+
10464+ restore_test_env("CBM_CACHE_DIR", saved_cache);
10465+ free(main_name);
10466+ free(wt_name);
10467+ snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree remove --force \"%s\" >/dev/null 2>&1",
10468+ maindir, wtdir);
10469+ (void)system(cmd);
10470+ test_rmdir_r(tmpdir);
10471+
10472+ if (!wt_ok)
10473+ FAIL("worktree cwd must resolve the worktree's own indexed project");
10474+ if (!sub_ok)
10475+ FAIL("a subdir of the worktree must resolve the worktree's own project");
10476+ if (!main_ok)
10477+ FAIL("main-checkout resolution must be unchanged and not leak the worktree project");
10478+ PASS();
10479+ }
10480+
10481+ /* Completes the payload-cwd matrix (with the indexed-worktree test above and the
10482+ * deadline/logging contract in cli_hook_augment_deadline_breadcrumb_issue858): a
10483+ * linked worktree that was never indexed must yield a deliberate, non-empty "no
10484+ * project matched — run index_repository" notice, never a silent 0-byte reply
10485+ * that an agent cannot distinguish from "no matches". */
10486+ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {
10487+ char tmpdir[256];
10488+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-unindexed-wt-XXXXXX");
10489+ if (!cbm_mkdtemp(tmpdir))
10490+ FAIL("cbm_mkdtemp failed");
10491+ char cache[512];
10492+ char maindir[512];
10493+ char wtdir[512];
10494+ snprintf(cache, sizeof(cache), "%s/cache", tmpdir);
10495+ snprintf(maindir, sizeof(maindir), "%s/checkout-main", tmpdir);
10496+ snprintf(wtdir, sizeof(wtdir), "%s/checkout-side", tmpdir);
10497+ test_mkdirp(cache);
10498+ test_mkdirp(maindir);
10499+
10500+ char cmd[2048];
10501+ snprintf(cmd, sizeof(cmd),
10502+ "cd \"%s\" && git init -q && git config user.email t@t && git config user.name t && "
10503+ ": > f.txt && git add f.txt && git commit -qm init && "
10504+ "git worktree add -q \"%s\" -b agl7unindexed",
10505+ maindir, wtdir);
10506+ if (system(cmd) != 0) {
10507+ test_rmdir_r(tmpdir);
10508+ FAIL("git worktree setup failed");
10509+ }
10510+
10511+ /* Index ONLY the main checkout; the worktree is deliberately left out. */
10512+ char *main_name = cbm_project_name_from_path(maindir);
10513+ ASSERT_NOT_NULL(main_name);
10514+ char db_main[900];
10515+ snprintf(db_main, sizeof(db_main), "%s/%s.db", cache, main_name);
10516+ cbm_store_t *sm = cbm_store_open_path(db_main);
10517+ ASSERT_NOT_NULL(sm);
10518+ ASSERT_EQ(cbm_store_upsert_project(sm, main_name, maindir), CBM_STORE_OK);
10519+ cbm_store_close(sm);
10520+
10521+ char *saved_cache = save_test_env("CBM_CACHE_DIR");
10522+ cbm_setenv("CBM_CACHE_DIR", cache, 1);
10523+
10524+ char input[1024];
10525+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", wtdir);
10526+ char *wt_out = cbm_hook_augment_lifecycle_json(input);
10527+ bool deliberate = wt_out && wt_out[0] && strstr(wt_out, "no indexed graph project matched") &&
10528+ !strstr(wt_out, "is indexed");
10529+ free(wt_out);
10530+
10531+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",
10532+ maindir);
10533+ char *main_out = cbm_hook_augment_lifecycle_json(input);
10534+ bool main_ok = main_out && strstr(main_out, main_name) && strstr(main_out, "is indexed");
10535+ free(main_out);
10536+
10537+ restore_test_env("CBM_CACHE_DIR", saved_cache);
10538+ free(main_name);
10539+ snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree remove --force \"%s\" >/dev/null 2>&1",
10540+ maindir, wtdir);
10541+ (void)system(cmd);
10542+ test_rmdir_r(tmpdir);
10543+
10544+ if (!deliberate)
10545+ FAIL("unindexed worktree must get a deliberate no-match notice, never a silent 0 bytes");
10546+ if (!main_ok)
10547+ FAIL("the indexed main checkout must still resolve in the same matrix");
10548+ PASS();
10549+ }
10550+ #endif
10551+
1038310552TEST(cli_hook_session_sanitizes_untrusted_project_metadata) {
1038410553 char tmpdir[256];
1038510554 snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-untrusted-project-XXXXXX");
@@ -16232,6 +16401,10 @@ SUITE(cli) {
1623216401 RUN_TEST(cli_augment_session_uses_workspace_roots);
1623316402 RUN_TEST(cli_hook_session_resolves_custom_named_index_by_root_path);
1623416403 RUN_TEST(cli_hook_session_exhausts_project_registry_pages);
16404+ #ifndef _WIN32
16405+ RUN_TEST(cli_hook_worktree_cwd_resolves_own_indexed_project);
16406+ RUN_TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent);
16407+ #endif
1623516408 RUN_TEST(cli_hook_session_sanitizes_untrusted_project_metadata);
1623616409 RUN_TEST(cli_hook_ownership_requires_exact_command_identity);
1623716410 RUN_TEST(cli_gemini_hook_upgrade_migrates_released_exact_commands);
0 commit comments