Skip to content

Commit 455e4fb

Browse files
committed
fix(mcp): report freshness with the indexer's own mtime source
check_index_coverage freshness recomputed mtime_ns from struct stat, which on Windows truncates to seconds, while the indexer records cbm_path_info_utf8's FILETIME-derived nanosecond value. A byte-identical file therefore never matched and every path was reported metadata_changed, so the freshness field carried no signal on Windows. The pipeline incremental change-detection had the same truncation in its hash comparison. Both readers now compare against the same source the writer uses. Fixes DeusData#1714 Signed-off-by: Ulises Millan Guerrero <ulises.millanguerrero@gmail.com>
1 parent 010569f commit 455e4fb

3 files changed

Lines changed: 77 additions & 43 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4204,17 +4204,6 @@ static coverage_path_result_t coverage_normalize_rel(const char *input, bool all
42044204
return written > 0U || allow_root ? COVERAGE_PATH_OK : COVERAGE_PATH_INVALID;
42054205
}
42064206

4207-
static int64_t coverage_stat_mtime_ns(const struct stat *st) {
4208-
#ifdef __APPLE__
4209-
return ((int64_t)st->st_mtimespec.tv_sec * (int64_t)CBM_NSEC_PER_SEC) +
4210-
(int64_t)st->st_mtimespec.tv_nsec;
4211-
#elif defined(_WIN32)
4212-
return (int64_t)st->st_mtime * (int64_t)CBM_NSEC_PER_SEC;
4213-
#else
4214-
return ((int64_t)st->st_mtim.tv_sec * (int64_t)CBM_NSEC_PER_SEC) + (int64_t)st->st_mtim.tv_nsec;
4215-
#endif
4216-
}
4217-
42184207
static const char *coverage_path_freshness(cbm_store_t *store, const char *project,
42194208
const char *root_path, const char *rel_path,
42204209
bool *outside) {
@@ -4228,8 +4217,13 @@ static const char *coverage_path_freshness(cbm_store_t *store, const char *proje
42284217
if (n < 0 || (size_t)n >= sizeof(abs_path)) {
42294218
return "unavailable";
42304219
}
4231-
struct stat st;
4232-
if (stat(abs_path, &st) != 0) {
4220+
/* #1714: mtime_ns must come from the SAME source the indexer recorded it
4221+
* with — cbm_path_info_utf8. Recomputing from struct stat truncates to
4222+
* seconds on Windows (st_mtime), while the file_hashes record carries
4223+
* FILETIME-resolution nanoseconds, so a byte-identical file never matched
4224+
* and every path was reported metadata_changed. */
4225+
cbm_path_info_t info;
4226+
if (cbm_path_info_utf8(abs_path, &info) != 0) {
42334227
return "missing";
42344228
}
42354229
if (!cbm_path_within_root(root_path, abs_path)) {
@@ -4245,7 +4239,7 @@ static const char *coverage_path_freshness(cbm_store_t *store, const char *proje
42454239
if (rc != CBM_STORE_OK) {
42464240
return "unavailable";
42474241
}
4248-
bool matches = hash.mtime_ns == coverage_stat_mtime_ns(&st) && hash.size == st.st_size;
4242+
bool matches = hash.mtime_ns == info.mtime_ns && hash.size == info.size;
42494243
cbm_store_clear_file_hash(&hash);
42504244
return matches ? "metadata_match" : "metadata_changed";
42514245
}

‎src/pipeline/pipeline_incremental.c‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ static const char *itoa_buf(int v) {
102102
return buf[idx];
103103
}
104104

105-
/* ── Platform-portable mtime_ns ──────────────────────────────────── */
106-
107-
static int64_t stat_mtime_ns(const struct stat *st) {
108-
#ifdef __APPLE__
109-
return ((int64_t)st->st_mtimespec.tv_sec * CBM_NS_PER_SEC) + (int64_t)st->st_mtimespec.tv_nsec;
110-
#elif defined(_WIN32)
111-
return (int64_t)st->st_mtime * CBM_NS_PER_SEC;
112-
#else
113-
return ((int64_t)st->st_mtim.tv_sec * CBM_NS_PER_SEC) + (int64_t)st->st_mtim.tv_nsec;
114-
#endif
115-
}
116-
117105
static const char *incr_mode_name(int mode) {
118106
switch (mode) {
119107
case CBM_MODE_FULL:
@@ -683,14 +671,20 @@ static bool *classify_files(cbm_file_info_t *files, int file_count, cbm_file_has
683671
continue;
684672
}
685673

686-
struct stat st;
687-
if (stat(files[i].path, &st) != 0) {
674+
/* #1714: compare against the SAME source the hash writer used. The
675+
* manifest hash is written from cbm_path_info_utf8 (see
676+
* semantic_manifest_hash_file), so a stat()-based comparison is
677+
* internally inconsistent: on Windows stat() truncates mtime to
678+
* seconds while the recorded value carries FILETIME nanoseconds, which
679+
* made every file look changed on each incremental pass. */
680+
cbm_path_info_t info;
681+
if (cbm_path_info_utf8(files[i].path, &info) != 0) {
688682
changed[i] = true;
689683
n_changed++;
690684
continue;
691685
}
692686

693-
if (stat_mtime_ns(&st) != h->mtime_ns || st.st_size != h->size) {
687+
if (info.mtime_ns != h->mtime_ns || info.size != h->size) {
694688
changed[i] = true;
695689
n_changed++;
696690
} else {

‎tests/test_mcp.c‎

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,20 +2727,15 @@ TEST(tool_check_index_coverage_accepts_truncated_ignored_catalog_for_fresh_path_
27272727
ASSERT_NOT_NULL(store);
27282728
char source_path[512];
27292729
snprintf(source_path, sizeof(source_path), "%s/project/main.go", tmp);
2730-
struct stat source_stat;
2731-
ASSERT_EQ(stat(source_path, &source_stat), 0);
2732-
#ifdef __APPLE__
2733-
int64_t source_mtime_ns =
2734-
((int64_t)source_stat.st_mtimespec.tv_sec * (int64_t)CBM_NSEC_PER_SEC) +
2735-
(int64_t)source_stat.st_mtimespec.tv_nsec;
2736-
#elif defined(_WIN32)
2737-
int64_t source_mtime_ns = (int64_t)source_stat.st_mtime * (int64_t)CBM_NSEC_PER_SEC;
2738-
#else
2739-
int64_t source_mtime_ns = ((int64_t)source_stat.st_mtim.tv_sec * (int64_t)CBM_NSEC_PER_SEC) +
2740-
(int64_t)source_stat.st_mtim.tv_nsec;
2741-
#endif
2742-
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "", source_mtime_ns,
2743-
source_stat.st_size),
2730+
/* The fixture must record the hash with the SAME mtime source the indexer
2731+
* writes with — cbm_path_info_utf8 — not struct stat. On Windows stat
2732+
* truncates to seconds while the stored record carries FILETIME-derived
2733+
* nanoseconds, so a stat-written fixture would never compare equal and the
2734+
* metadata_match contract below would fail. */
2735+
cbm_path_info_t path_info;
2736+
ASSERT_EQ(cbm_path_info_utf8(source_path, &path_info), 0);
2737+
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "", path_info.mtime_ns,
2738+
path_info.size),
27442739
CBM_STORE_OK);
27452740
cbm_project_t project = {0};
27462741
ASSERT_EQ(cbm_store_get_project(store, "test-project", &project), CBM_STORE_OK);
@@ -2782,6 +2777,56 @@ TEST(tool_check_index_coverage_accepts_truncated_ignored_catalog_for_fresh_path_
27822777
PASS();
27832778
}
27842779

2780+
/* #1714: coverage freshness must compare mtime_ns at the SAME precision the
2781+
* indexer records it. The pipeline records cbm_path_info_utf8's value (which
2782+
* on Windows derives from FILETIME — nanosecond), while the freshness reader
2783+
* used to recompute from struct stat, which on Windows truncates to seconds
2784+
* (st_mtime). A byte-identical file therefore never matched and every path was
2785+
* reported metadata_changed. The reader now uses the indexer's own source. */
2786+
TEST(tool_check_index_coverage_freshness_uses_indexer_mtime_source_issue1714) {
2787+
char tmp[256];
2788+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
2789+
ASSERT_NOT_NULL(srv);
2790+
cbm_store_t *store = cbm_mcp_server_store(srv);
2791+
ASSERT_NOT_NULL(store);
2792+
2793+
char source_path[512];
2794+
snprintf(source_path, sizeof(source_path), "%s/project/main.go", tmp);
2795+
cbm_path_info_t info;
2796+
ASSERT_EQ(cbm_path_info_utf8(source_path, &info), 0);
2797+
2798+
/* The hash exactly as the indexer writes it: same source, ns precision. */
2799+
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "", info.mtime_ns,
2800+
info.size),
2801+
CBM_STORE_OK);
2802+
2803+
char *response = cbm_mcp_handle_tool(
2804+
srv, "check_index_coverage", "{\"project\":\"test-project\",\"paths\":[\"main.go\"]}");
2805+
ASSERT_NOT_NULL(response);
2806+
ASSERT_NOT_NULL(strstr(response, "\"freshness\":\"metadata_match\""));
2807+
free(response);
2808+
2809+
/* A hash stored at seconds precision — what a stat-based reader previously
2810+
* compared against — must NOT match an unchanged file: the comparison must
2811+
* stay nanosecond-exact, or part of mtime resolution is silently dropped. */
2812+
int64_t seconds_mtime_ns = (info.mtime_ns / (int64_t)CBM_NSEC_PER_SEC) *
2813+
(int64_t)CBM_NSEC_PER_SEC;
2814+
if (seconds_mtime_ns != info.mtime_ns) {
2815+
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "",
2816+
seconds_mtime_ns, info.size),
2817+
CBM_STORE_OK);
2818+
response = cbm_mcp_handle_tool(
2819+
srv, "check_index_coverage", "{\"project\":\"test-project\",\"paths\":[\"main.go\"]}");
2820+
ASSERT_NOT_NULL(response);
2821+
ASSERT_NOT_NULL(strstr(response, "\"freshness\":\"metadata_changed\""));
2822+
free(response);
2823+
}
2824+
2825+
cleanup_snippet_dir(tmp);
2826+
cbm_mcp_server_free(srv);
2827+
PASS();
2828+
}
2829+
27852830
TEST(tool_check_index_coverage_rejects_stale_generation) {
27862831
char tmp[256];
27872832
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
@@ -11332,6 +11377,7 @@ SUITE(mcp) {
1133211377
RUN_TEST(tool_check_index_coverage_preserves_multiple_scope_labels);
1133311378
RUN_TEST(tool_check_index_coverage_accepts_truncated_ignored_catalog_for_fresh_path_issue1613);
1133411379
RUN_TEST(tool_check_index_coverage_rejects_stale_generation);
11380+
RUN_TEST(tool_check_index_coverage_freshness_uses_indexer_mtime_source_issue1714);
1133511381
RUN_TEST(tool_check_index_coverage_requires_source_when_file_metadata_changed);
1133611382
RUN_TEST(tool_check_index_coverage_surfaces_lookup_errors);
1133711383
RUN_TEST(tool_index_status_includes_git_metadata);

0 commit comments

Comments
 (0)