Skip to content

Commit c4bec4a

Browse files
committed
test(mcp): over-long grep records never invent matches (DeusData#2011)
A grep record longer than the read buffer used to be split across two reads, and the continuation was parsed as a fresh file:line:content record — a fragment of matched content became a file path and the text after the next delimiter became a line number. An agent reading that result called get_code_snippet on a path that was never in the repository. The same split could also lose a real match, when the tail held fewer than two delimiters and the record was dropped instead. The production fix landed via DeusData#1597 (3c7427e): search_code now reads each record with cbm_getline into a growable buffer, so a record can no longer split. That closed DeusData#2011 as a side effect, and nothing on main guards the shape — the buffer could be reintroduced by a future rewrite of the read loop with no test to catch it. Add the regression test the fix never got. It writes a >2 KiB colon-laden line plus a normal one, calls search_code through cbm_mcp_server_handle, and asserts the repository's two real matches are reported as two — not three — and that no row carries the line 0 the fabricated record used to produce. Signed-off-by: kavish-19 <63698788+kavish-19@users.noreply.github.com>
1 parent fe85a6b commit c4bec4a

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

‎tests/test_mcp.c‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10118,6 +10118,73 @@ TEST(search_code_path_filter_prefilter_keeps_matches) {
1011810118
PASS();
1011910119
}
1012010120

10121+
/* #2011: a grep record longer than the read buffer used to be split across two
10122+
* fgets calls, and the continuation was parsed as a fresh file:line:content
10123+
* record — inventing a file path out of matched content and a line number of 0.
10124+
* A single >2 KiB line containing colons reproduces it: the repository holds
10125+
* two matches, and the split used to report three. */
10126+
TEST(search_code_long_line_does_not_invent_matches) {
10127+
char tmp[512];
10128+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_srch_longline_XXXXXX");
10129+
ASSERT_TRUE(cbm_mkdtemp(tmp) != NULL);
10130+
10131+
char big_path[768], normal_path[768];
10132+
snprintf(big_path, sizeof(big_path), "%s/big.js", tmp);
10133+
snprintf(normal_path, sizeof(normal_path), "%s/normal.js", tmp);
10134+
10135+
/* One line well over the 2 KiB read buffer, full of colons, with the
10136+
* needle at the very end so the match lands past the split point. */
10137+
FILE *fp = fopen(big_path, "w");
10138+
ASSERT_NOT_NULL(fp);
10139+
fprintf(fp, "var CFG=({");
10140+
for (int i = 0; i < 260; i++) {
10141+
fprintf(fp, "k%d:\"v%d\",", i, i);
10142+
}
10143+
fprintf(fp, "NEEDLEmarker:1});\n");
10144+
fclose(fp);
10145+
10146+
fp = fopen(normal_path, "w");
10147+
ASSERT_NOT_NULL(fp);
10148+
fprintf(fp, "const NEEDLEmarker = 42;\n");
10149+
fclose(fp);
10150+
10151+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
10152+
ASSERT_NOT_NULL(srv);
10153+
const char *proj = "longline-search";
10154+
cbm_mcp_server_set_project(srv, proj);
10155+
cbm_store_upsert_project(cbm_mcp_server_store(srv), proj, tmp);
10156+
10157+
char *resp = cbm_mcp_server_handle(
10158+
srv, "{\"jsonrpc\":\"2.0\",\"id\":96,\"method\":\"tools/call\","
10159+
"\"params\":{\"name\":\"search_code\","
10160+
"\"arguments\":{\"pattern\":\"NEEDLEmarker\",\"project\":\"longline-search\"}}}");
10161+
ASSERT_NOT_NULL(resp);
10162+
char *inner = extract_text_content(resp);
10163+
ASSERT_NOT_NULL(inner);
10164+
10165+
/* Exactly the two real matches — the split must not produce a third. */
10166+
int grep_matches = -1;
10167+
const char *g = strstr(inner, "\"total_grep_matches\":");
10168+
if (g) {
10169+
sscanf(g, "\"total_grep_matches\":%d", &grep_matches);
10170+
} else if ((g = strstr(inner, "total_grep_matches: ")) != NULL) {
10171+
sscanf(g, "total_grep_matches: %d", &grep_matches);
10172+
}
10173+
ASSERT_EQ(grep_matches, 2);
10174+
10175+
/* Every reported line number belongs to a real line: the fabricated row
10176+
* carried line 0, which no grep -n record can produce. */
10177+
ASSERT_TRUE(strstr(inner, "\"line\":0") == NULL);
10178+
10179+
free(inner);
10180+
free(resp);
10181+
cbm_mcp_server_free(srv);
10182+
unlink(big_path);
10183+
unlink(normal_path);
10184+
rmdir(tmp);
10185+
PASS();
10186+
}
10187+
1012110188
/* PR #756 (distilled): path_filter matching ZERO indexed files. With the
1012210189
* prefilter the scoped filelist has 0 records, and handle_search_code now
1012310190
* skips the grep subprocess entirely (xargs on an empty filelist is
@@ -19923,6 +19990,7 @@ SUITE(mcp) {
1992319990
RUN_TEST(search_code_scoped_path_with_cjk_root_issue903);
1992419991
#endif
1992519992
RUN_TEST(search_code_path_filter_prefilter_keeps_matches);
19993+
RUN_TEST(search_code_long_line_does_not_invent_matches);
1992619994
RUN_TEST(search_code_path_filter_matches_nothing);
1992719995
RUN_TEST(search_code_file_pattern_prefilter_boundaries);
1992819996
RUN_TEST(search_code_windows_scope_prefilter_removes_pipeline_filter);

0 commit comments

Comments
 (0)