Skip to content

Commit 6bda6e2

Browse files
committed
fix(watcher): report a tree-polled non-git root as strategy "tree"
Since #1948 a non-git root can be polled by a file-tree scan when watch_non_git is on. index_status still reported such a root as strategy "none", which the enum documents as "not a git project: never polled", so a root that is actively polled looked unwatched. publish_status now maps a tree-polled root to the new CBM_WATCHER_STRATEGY_TREE, rendered as "tree" (the value the watcher.baseline log already uses). "none" keeps its meaning: a registered non-git root that is never polled. CONFIGURATION.md lists the four values. Test: daemon_application_index_status_reports_tree_strategy_issue2167 fails without the production change and passes with it; suites daemon_application, watcher and mcp: 507 passed, 4 platform skips. Signed-off-by: Martin Vogel <[email protected]>
1 parent 3794085 commit 6bda6e2

6 files changed

Lines changed: 104 additions & 4 deletions

File tree

‎docs/CONFIGURATION.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ Current keys:
9393
| `index_max_files` | `off` | Optional maximum number of accepted source files in one discovery run. |
9494
| `index_max_source_mb` | `off` | Optional maximum accepted source size in MiB in one discovery run. |
9595

96+
For a watched project, `index_status` names the polling method in `watch.strategy`:
97+
`git` (HEAD and dirty-state polling), `tree` (a non-git root polled by the
98+
`watch_non_git` file-tree scan), `none` (a non-git root that is registered but never
99+
polled, the default for a plain directory) or `pending` (registered; the first poll
100+
has not run yet).
101+
96102
> **`watcher_enabled` vs `auto_watch`.** `watcher_enabled` controls whether the
97103
> watcher *subsystem* starts at all (the background poll thread). `auto_watch` is
98104
> narrower: it only controls whether a connecting session registers *its own*

‎src/daemon/application.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ static const char *application_watch_strategy_name(cbm_watcher_strategy_t strate
569569
switch (strategy) {
570570
case CBM_WATCHER_STRATEGY_GIT:
571571
return "git";
572+
case CBM_WATCHER_STRATEGY_TREE:
573+
return "tree";
572574
case CBM_WATCHER_STRATEGY_NONE:
573575
return "none";
574576
default:

‎src/mcp/mcp.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void cbm_mcp_server_set_watcher(cbm_mcp_server_t *srv, struct cbm_watcher *w);
186186
typedef struct {
187187
bool watched;
188188
const char *reason; /* why not watched (set when !watched) */
189-
const char *strategy; /* "pending" | "git" | "none" (set when watched) */
189+
const char *strategy; /* "pending" | "git" | "tree" | "none" (set when watched) */
190190
int poll_interval_ms; /* current adaptive cadence (when watched) */
191191
int64_t last_scan_unix_s; /* last completed scan, wall clock; 0 = none yet */
192192
} cbm_mcp_watch_status_t;

‎src/watcher/watcher.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,10 +1303,16 @@ int cbm_watcher_watch_count(cbm_watcher_t *w) {
13031303
/* ── Single poll cycle ──────────────────────────────────────────── */
13041304

13051305
/* Publish what index_status reports: the strategy, the current cadence and,
1306-
* when a check just completed, the wall-clock time of that scan. */
1306+
* when a check just completed, the wall-clock time of that scan. A non-git
1307+
* root is "tree" when watch_non_git polls it (#1948), else "none". */
13071308
static void publish_status(project_state_t *s, bool scanned) {
1308-
atomic_store(&s->status_strategy,
1309-
s->is_git ? CBM_WATCHER_STRATEGY_GIT : CBM_WATCHER_STRATEGY_NONE);
1309+
cbm_watcher_strategy_t strategy = CBM_WATCHER_STRATEGY_NONE;
1310+
if (s->is_git) {
1311+
strategy = CBM_WATCHER_STRATEGY_GIT;
1312+
} else if (s->tree_poll) {
1313+
strategy = CBM_WATCHER_STRATEGY_TREE;
1314+
}
1315+
atomic_store(&s->status_strategy, strategy);
13101316
atomic_store(&s->status_interval_ms, s->interval_ms);
13111317
if (scanned) {
13121318
atomic_store(&s->status_last_scan_s, (int64_t)time(NULL));

‎src/watcher/watcher.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ typedef enum {
135135
CBM_WATCHER_STRATEGY_PENDING = 0, /* registered; first poll not done yet */
136136
CBM_WATCHER_STRATEGY_GIT = 1, /* git HEAD + dirty-state polling */
137137
CBM_WATCHER_STRATEGY_NONE = 2, /* not a git project: never polled */
138+
CBM_WATCHER_STRATEGY_TREE = 3, /* not a git project: file-tree polling (watch_non_git) */
138139
} cbm_watcher_strategy_t;
139140

140141
typedef struct {

‎tests/test_daemon_application.c‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,90 @@ TEST(daemon_application_index_status_watcher_disabled_issue2167) {
13011301
PASS();
13021302
}
13031303

1304+
/* #2167 with #1948: a non-git root that watch_non_git polls by file-tree scan
1305+
* reports strategy "tree". "none" stays reserved for a registered root that is
1306+
* never polled (a plain directory without the opt-in). */
1307+
TEST(daemon_application_index_status_reports_tree_strategy_issue2167) {
1308+
const char *old_cache = getenv("CBM_CACHE_DIR");
1309+
bool had_cache = old_cache != NULL;
1310+
char *saved_cache = old_cache ? cbm_strdup(old_cache) : NULL;
1311+
char root[APP_TEST_PATH_CAP];
1312+
char cache[APP_TEST_PATH_CAP];
1313+
snprintf(root, sizeof(root), "%s/cbm-app-tree-root-XXXXXX", cbm_tmpdir());
1314+
snprintf(cache, sizeof(cache), "%s/cbm-app-tree-cache-XXXXXX", cbm_tmpdir());
1315+
bool dirs_ok = cbm_mkdtemp(root) != NULL && cbm_mkdtemp(cache) != NULL;
1316+
bool env_ok = (!had_cache || saved_cache) && cbm_setenv("CBM_CACHE_DIR", cache, 1) == 0;
1317+
char *project = dirs_ok ? cbm_project_name_from_path(root) : NULL;
1318+
char db_path[APP_TEST_PATH_CAP] = {0};
1319+
bool seeded =
1320+
project && app_test_seed_project_db(cache, project, root, db_path, sizeof(db_path));
1321+
cbm_store_t *store = cbm_store_open_memory();
1322+
cbm_watcher_t *watcher = cbm_watcher_new(store, app_test_index_noop, NULL);
1323+
/* What the daemon host does at startup when watch_non_git is true. */
1324+
cbm_watcher_set_poll_non_git(watcher, true);
1325+
cbm_daemon_application_config_t config = {.watcher = watcher, .config = NULL};
1326+
cbm_daemon_application_t *application = cbm_daemon_application_new(&config);
1327+
cbm_daemon_runtime_application_callbacks_t callbacks =
1328+
cbm_daemon_application_runtime_callbacks(application);
1329+
cbm_daemon_runtime_application_session_t *session = app_test_open(&callbacks, 23);
1330+
uint8_t *context = NULL;
1331+
uint32_t context_length = 0;
1332+
uint8_t *ping = NULL;
1333+
uint32_t ping_length = 0;
1334+
uint8_t *response = NULL;
1335+
uint32_t response_length = 0;
1336+
/* Context + one MCP request registers the session root's watch. */
1337+
bool registered = seeded && app_test_context_request(root, root, &context, &context_length) &&
1338+
app_test_text_request(CBM_DAEMON_APPLICATION_REQUEST_MCP,
1339+
"{\"jsonrpc\":\"2.0\",\"id\":7,\"method\":\"ping\"}",
1340+
&ping, &ping_length) &&
1341+
app_test_request(&callbacks, session, context, context_length, &response,
1342+
&response_length) == CBM_DAEMON_RUNTIME_APPLICATION_OK;
1343+
free(response);
1344+
response = NULL;
1345+
registered =
1346+
registered && app_test_request(&callbacks, session, ping, ping_length, &response,
1347+
&response_length) == CBM_DAEMON_RUNTIME_APPLICATION_OK;
1348+
free(response);
1349+
response = NULL;
1350+
registered = registered && cbm_watcher_watch_count(watcher) == 1;
1351+
/* The first poll completes the baseline: the plain directory gets tree
1352+
* polling and its strategy is published. */
1353+
if (registered) {
1354+
(void)cbm_watcher_poll_once(watcher);
1355+
}
1356+
1357+
char *status = registered ? app_test_index_status(&callbacks, session, project) : NULL;
1358+
bool tree = app_test_response_has(status, "watched", "true") &&
1359+
app_test_response_has(status, "strategy", "\\\"tree\\\"");
1360+
1361+
callbacks.session_cancel(callbacks.context, session);
1362+
callbacks.session_close(callbacks.context, session);
1363+
cbm_daemon_application_free(application);
1364+
cbm_watcher_stop(watcher);
1365+
cbm_watcher_free(watcher);
1366+
cbm_store_close(store);
1367+
free(status);
1368+
free(context);
1369+
free(ping);
1370+
free(project);
1371+
app_test_unlink_project_db(db_path);
1372+
(void)cbm_rmdir(root);
1373+
(void)cbm_rmdir(cache);
1374+
if (saved_cache) {
1375+
(void)cbm_setenv("CBM_CACHE_DIR", saved_cache, 1);
1376+
} else if (!had_cache) {
1377+
(void)cbm_unsetenv("CBM_CACHE_DIR");
1378+
}
1379+
free(saved_cache);
1380+
1381+
ASSERT_TRUE(env_ok);
1382+
ASSERT_TRUE(seeded);
1383+
ASSERT_TRUE(registered);
1384+
ASSERT_TRUE(tree);
1385+
PASS();
1386+
}
1387+
13041388
TEST(daemon_application_free_releases_live_watch_once) {
13051389
const char *old_cache = getenv("CBM_CACHE_DIR");
13061390
bool had_cache = old_cache != NULL;
@@ -6705,6 +6789,7 @@ SUITE(daemon_application) {
67056789
RUN_TEST(daemon_application_reference_counts_one_shared_watch);
67066790
RUN_TEST(daemon_application_index_status_reports_watch_issue2167);
67076791
RUN_TEST(daemon_application_index_status_watcher_disabled_issue2167);
6792+
RUN_TEST(daemon_application_index_status_reports_tree_strategy_issue2167);
67086793
RUN_TEST(daemon_application_free_releases_live_watch_once);
67096794
RUN_TEST(daemon_application_prune_clears_logical_watch_for_reregistration);
67106795
RUN_TEST(daemon_application_initialize_coalesces_auto_index_for_full_sessions);

0 commit comments

Comments
 (0)