Skip to content

Commit f347d59

Browse files
authored
Merge pull request DeusData#1787 from umi008/fix/issue-1714-freshness-mtime
fix(mcp): report freshness with the indexer's own mtime source (DeusData#1714)
2 parents d6082f2 + 455e4fb commit f347d59

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
@@ -6265,17 +6265,6 @@ static coverage_path_result_t coverage_normalize_rel(const char *input, bool all
62656265
return written > 0U || allow_root ? COVERAGE_PATH_OK : COVERAGE_PATH_INVALID;
62666266
}
62676267

6268-
static int64_t coverage_stat_mtime_ns(const struct stat *st) {
6269-
#ifdef __APPLE__
6270-
return ((int64_t)st->st_mtimespec.tv_sec * (int64_t)CBM_NSEC_PER_SEC) +
6271-
(int64_t)st->st_mtimespec.tv_nsec;
6272-
#elif defined(_WIN32)
6273-
return (int64_t)st->st_mtime * (int64_t)CBM_NSEC_PER_SEC;
6274-
#else
6275-
return ((int64_t)st->st_mtim.tv_sec * (int64_t)CBM_NSEC_PER_SEC) + (int64_t)st->st_mtim.tv_nsec;
6276-
#endif
6277-
}
6278-
62796268
static const char *coverage_path_freshness(cbm_store_t *store, const char *project,
62806269
const char *root_path, const char *rel_path,
62816270
bool *outside) {
@@ -6289,8 +6278,13 @@ static const char *coverage_path_freshness(cbm_store_t *store, const char *proje
62896278
if (n < 0 || (size_t)n >= sizeof(abs_path)) {
62906279
return "unavailable";
62916280
}
6292-
struct stat st;
6293-
if (stat(abs_path, &st) != 0) {
6281+
/* #1714: mtime_ns must come from the SAME source the indexer recorded it
6282+
* with — cbm_path_info_utf8. Recomputing from struct stat truncates to
6283+
* seconds on Windows (st_mtime), while the file_hashes record carries
6284+
* FILETIME-resolution nanoseconds, so a byte-identical file never matched
6285+
* and every path was reported metadata_changed. */
6286+
cbm_path_info_t info;
6287+
if (cbm_path_info_utf8(abs_path, &info) != 0) {
62946288
return "missing";
62956289
}
62966290
if (!cbm_path_within_root(root_path, abs_path)) {
@@ -6306,7 +6300,7 @@ static const char *coverage_path_freshness(cbm_store_t *store, const char *proje
63066300
if (rc != CBM_STORE_OK) {
63076301
return "unavailable";
63086302
}
6309-
bool matches = hash.mtime_ns == coverage_stat_mtime_ns(&st) && hash.size == st.st_size;
6303+
bool matches = hash.mtime_ns == info.mtime_ns && hash.size == info.size;
63106304
cbm_store_clear_file_hash(&hash);
63116305
return matches ? "metadata_match" : "metadata_changed";
63126306
}

‎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:
@@ -687,14 +675,20 @@ static bool *classify_files(cbm_file_info_t *files, int file_count, cbm_file_has
687675
continue;
688676
}
689677

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

697-
if (stat_mtime_ns(&st) != h->mtime_ns || st.st_size != h->size) {
691+
if (info.mtime_ns != h->mtime_ns || info.size != h->size) {
698692
changed[i] = true;
699693
n_changed++;
700694
} else {

‎tests/test_mcp.c‎

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5726,20 +5726,15 @@ TEST(tool_check_index_coverage_accepts_truncated_ignored_catalog_for_fresh_path_
57265726
ASSERT_NOT_NULL(store);
57275727
char source_path[512];
57285728
snprintf(source_path, sizeof(source_path), "%s/project/main.go", tmp);
5729-
struct stat source_stat;
5730-
ASSERT_EQ(stat(source_path, &source_stat), 0);
5731-
#ifdef __APPLE__
5732-
int64_t source_mtime_ns =
5733-
((int64_t)source_stat.st_mtimespec.tv_sec * (int64_t)CBM_NSEC_PER_SEC) +
5734-
(int64_t)source_stat.st_mtimespec.tv_nsec;
5735-
#elif defined(_WIN32)
5736-
int64_t source_mtime_ns = (int64_t)source_stat.st_mtime * (int64_t)CBM_NSEC_PER_SEC;
5737-
#else
5738-
int64_t source_mtime_ns = ((int64_t)source_stat.st_mtim.tv_sec * (int64_t)CBM_NSEC_PER_SEC) +
5739-
(int64_t)source_stat.st_mtim.tv_nsec;
5740-
#endif
5741-
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "", source_mtime_ns,
5742-
source_stat.st_size),
5729+
/* The fixture must record the hash with the SAME mtime source the indexer
5730+
* writes with — cbm_path_info_utf8 — not struct stat. On Windows stat
5731+
* truncates to seconds while the stored record carries FILETIME-derived
5732+
* nanoseconds, so a stat-written fixture would never compare equal and the
5733+
* metadata_match contract below would fail. */
5734+
cbm_path_info_t path_info;
5735+
ASSERT_EQ(cbm_path_info_utf8(source_path, &path_info), 0);
5736+
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "", path_info.mtime_ns,
5737+
path_info.size),
57435738
CBM_STORE_OK);
57445739
cbm_project_t project = {0};
57455740
ASSERT_EQ(cbm_store_get_project(store, "test-project", &project), CBM_STORE_OK);
@@ -5782,6 +5777,56 @@ TEST(tool_check_index_coverage_accepts_truncated_ignored_catalog_for_fresh_path_
57825777
PASS();
57835778
}
57845779

5780+
/* #1714: coverage freshness must compare mtime_ns at the SAME precision the
5781+
* indexer records it. The pipeline records cbm_path_info_utf8's value (which
5782+
* on Windows derives from FILETIME — nanosecond), while the freshness reader
5783+
* used to recompute from struct stat, which on Windows truncates to seconds
5784+
* (st_mtime). A byte-identical file therefore never matched and every path was
5785+
* reported metadata_changed. The reader now uses the indexer's own source. */
5786+
TEST(tool_check_index_coverage_freshness_uses_indexer_mtime_source_issue1714) {
5787+
char tmp[256];
5788+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
5789+
ASSERT_NOT_NULL(srv);
5790+
cbm_store_t *store = cbm_mcp_server_store(srv);
5791+
ASSERT_NOT_NULL(store);
5792+
5793+
char source_path[512];
5794+
snprintf(source_path, sizeof(source_path), "%s/project/main.go", tmp);
5795+
cbm_path_info_t info;
5796+
ASSERT_EQ(cbm_path_info_utf8(source_path, &info), 0);
5797+
5798+
/* The hash exactly as the indexer writes it: same source, ns precision. */
5799+
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "", info.mtime_ns,
5800+
info.size),
5801+
CBM_STORE_OK);
5802+
5803+
char *response = cbm_mcp_handle_tool(
5804+
srv, "check_index_coverage", "{\"project\":\"test-project\",\"paths\":[\"main.go\"]}");
5805+
ASSERT_NOT_NULL(response);
5806+
ASSERT_NOT_NULL(strstr(response, "\"freshness\":\"metadata_match\""));
5807+
free(response);
5808+
5809+
/* A hash stored at seconds precision — what a stat-based reader previously
5810+
* compared against — must NOT match an unchanged file: the comparison must
5811+
* stay nanosecond-exact, or part of mtime resolution is silently dropped. */
5812+
int64_t seconds_mtime_ns = (info.mtime_ns / (int64_t)CBM_NSEC_PER_SEC) *
5813+
(int64_t)CBM_NSEC_PER_SEC;
5814+
if (seconds_mtime_ns != info.mtime_ns) {
5815+
ASSERT_EQ(cbm_store_upsert_file_hash(store, "test-project", "main.go", "",
5816+
seconds_mtime_ns, info.size),
5817+
CBM_STORE_OK);
5818+
response = cbm_mcp_handle_tool(
5819+
srv, "check_index_coverage", "{\"project\":\"test-project\",\"paths\":[\"main.go\"]}");
5820+
ASSERT_NOT_NULL(response);
5821+
ASSERT_NOT_NULL(strstr(response, "\"freshness\":\"metadata_changed\""));
5822+
free(response);
5823+
}
5824+
5825+
cleanup_snippet_dir(tmp);
5826+
cbm_mcp_server_free(srv);
5827+
PASS();
5828+
}
5829+
57855830
TEST(tool_check_index_coverage_rejects_stale_generation) {
57865831
char tmp[256];
57875832
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
@@ -20389,6 +20434,7 @@ SUITE(mcp) {
2038920434
RUN_TEST(tool_check_index_coverage_preserves_multiple_scope_labels);
2039020435
RUN_TEST(tool_check_index_coverage_accepts_truncated_ignored_catalog_for_fresh_path_issue1613);
2039120436
RUN_TEST(tool_check_index_coverage_rejects_stale_generation);
20437+
RUN_TEST(tool_check_index_coverage_freshness_uses_indexer_mtime_source_issue1714);
2039220438
RUN_TEST(tool_check_index_coverage_requires_source_when_file_metadata_changed);
2039320439
RUN_TEST(tool_check_index_coverage_surfaces_lookup_errors);
2039420440
RUN_TEST(tool_index_status_includes_git_metadata);

0 commit comments

Comments
 (0)