|
9 | 9 | #include "../src/foundation/constants.h" |
10 | 10 | #include "../src/foundation/log.h" |
11 | 11 | #include "../src/foundation/platform.h" /* cbm_file_size */ |
| 12 | +#include "../src/foundation/mem.h" /* cbm_mem_set_budget_for_tests — over-budget seam */ |
12 | 13 | #include "../src/foundation/subprocess.h" |
13 | 14 | #include "../src/foundation/workspace.h" |
14 | 15 | #include "../src/mcp/compact_out.h" |
@@ -17670,6 +17671,161 @@ static int idx823_supervised_name_override_check(const char *repo_dir, const cha |
17670 | 17671 | } |
17671 | 17672 | #endif |
17672 | 17673 |
|
| 17674 | +/* Write the 64-file Go fixture (above MIN_FILES_FOR_PARALLEL, so the gated |
| 17675 | + * parallel extract path runs). generation 2 gives every file a second |
| 17676 | + * definition so a re-run must re-extract all of them — an unchanged repo is an |
| 17677 | + * incremental no-op and never reaches the memory gate. */ |
| 17678 | +static bool budget_fixture_write(const char *repo, int generation) { |
| 17679 | + for (int i = 0; i < 64; i++) { |
| 17680 | + char path[CBM_SZ_1K]; |
| 17681 | + char body[CBM_SZ_256]; |
| 17682 | + snprintf(path, sizeof(path), "%s/f%02d.go", repo, i); |
| 17683 | + if (generation == 1) { |
| 17684 | + snprintf(body, sizeof(body), "package main\n\nfunc F%02d() int {\n\treturn %d\n}\n", i, |
| 17685 | + i); |
| 17686 | + } else { |
| 17687 | + snprintf(body, sizeof(body), |
| 17688 | + "package main\n\nfunc F%02d() int {\n\treturn %d\n}\n\n" |
| 17689 | + "func G%02d() int {\n\treturn F%02d() + 1\n}\n", |
| 17690 | + i, i, i, i); |
| 17691 | + } |
| 17692 | + if (th_write_file(path, body) != 0) { |
| 17693 | + return false; |
| 17694 | + } |
| 17695 | + } |
| 17696 | + return true; |
| 17697 | +} |
| 17698 | + |
| 17699 | +/* Parse the JSON object inside an MCP tool result. NULL when absent. */ |
| 17700 | +static yyjson_doc *budget_result_doc(const char *result) { |
| 17701 | + char *text = extract_text_content(result); |
| 17702 | + yyjson_doc *doc = text ? yyjson_read(text, strlen(text), 0) : NULL; |
| 17703 | + free(text); |
| 17704 | + return doc; |
| 17705 | +} |
| 17706 | + |
| 17707 | +static const char *budget_doc_str(yyjson_doc *doc, const char *key) { |
| 17708 | + return doc ? yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), key)) : NULL; |
| 17709 | +} |
| 17710 | + |
| 17711 | +static int budget_doc_int(yyjson_doc *doc, const char *key, int missing) { |
| 17712 | + yyjson_val *val = doc ? yyjson_obj_get(yyjson_doc_get_root(doc), key) : NULL; |
| 17713 | + return val && yyjson_is_int(val) ? (int)yyjson_get_int(val) : missing; |
| 17714 | +} |
| 17715 | + |
| 17716 | +/* Decision A (#1997 #832): an attempt that stays over the memory budget after |
| 17717 | + * back-pressure fails WHOLE and the response says so — reason, numbers, hint, |
| 17718 | + * and that the previous index still serves — instead of the generic "check |
| 17719 | + * repo_path" error. In-process: the runner is never a marked supervisor host, |
| 17720 | + * and the budget seam stands in for a 1 MiB machine. */ |
| 17721 | +TEST(index_repository_over_budget_reports_named_reason) { |
| 17722 | + char repo[CBM_SZ_1K]; |
| 17723 | + char cache[CBM_SZ_1K]; |
| 17724 | + snprintf(repo, sizeof(repo), "%s/cbm-mcp-budget-repo-XXXXXX", cbm_tmpdir()); |
| 17725 | + snprintf(cache, sizeof(cache), "%s/cbm-mcp-budget-cache-XXXXXX", cbm_tmpdir()); |
| 17726 | + if (!cbm_mkdtemp(repo)) { |
| 17727 | + FAIL("cbm_mkdtemp repo failed"); |
| 17728 | + } |
| 17729 | + if (!cbm_mkdtemp(cache)) { |
| 17730 | + th_rmtree(repo); |
| 17731 | + FAIL("cbm_mkdtemp cache failed"); |
| 17732 | + } |
| 17733 | + const char *saved_cache = getenv("CBM_CACHE_DIR"); |
| 17734 | + char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL; |
| 17735 | + const char *saved_workers = getenv("CBM_WORKERS"); |
| 17736 | + char *saved_workers_copy = saved_workers ? strdup(saved_workers) : NULL; |
| 17737 | + cbm_setenv("CBM_CACHE_DIR", cache, 1); |
| 17738 | + cbm_setenv("CBM_WORKERS", "4", 1); |
| 17739 | + |
| 17740 | + bool files_ok = budget_fixture_write(repo, 1); |
| 17741 | + cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); |
| 17742 | + bool had_server = srv != NULL; |
| 17743 | + char args[CBM_SZ_2K]; |
| 17744 | + snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", repo); |
| 17745 | + |
| 17746 | + /* Generation 1 at the process budget publishes. */ |
| 17747 | + char *first = srv && files_ok ? cbm_mcp_handle_tool(srv, "index_repository", args) : NULL; |
| 17748 | + yyjson_doc *first_doc = budget_result_doc(first); |
| 17749 | + const char *first_status = budget_doc_str(first_doc, "status"); |
| 17750 | + bool first_indexed = first_status && strcmp(first_status, "indexed") == 0; |
| 17751 | + int nodes_first = budget_doc_int(first_doc, "nodes", -1); |
| 17752 | + const char *first_project = budget_doc_str(first_doc, "project"); |
| 17753 | + char *project = first_project ? strdup(first_project) : NULL; |
| 17754 | + yyjson_doc_free(first_doc); |
| 17755 | + free(first); |
| 17756 | + char db_path[CBM_SZ_2K]; |
| 17757 | + snprintf(db_path, sizeof(db_path), "%s/%s.db", cache, project ? project : "missing"); |
| 17758 | + long db_size_before = (long)cbm_file_size(db_path); |
| 17759 | + |
| 17760 | + /* Generation 2 at 1 MiB over a changed repo fails whole. */ |
| 17761 | + files_ok = files_ok && budget_fixture_write(repo, 2); |
| 17762 | + size_t saved_budget = cbm_mem_budget(); |
| 17763 | + cbm_mem_set_budget_for_tests((size_t)1024 * 1024); |
| 17764 | + char *second = srv && files_ok ? cbm_mcp_handle_tool(srv, "index_repository", args) : NULL; |
| 17765 | + cbm_mem_set_budget_for_tests(saved_budget); |
| 17766 | + bool second_is_error = second && strstr(second, "\"isError\":true") != NULL; |
| 17767 | + yyjson_doc *second_doc = budget_result_doc(second); |
| 17768 | + const char *second_status = budget_doc_str(second_doc, "status"); |
| 17769 | + const char *second_reason = budget_doc_str(second_doc, "reason"); |
| 17770 | + const char *second_previous = budget_doc_str(second_doc, "previous_index"); |
| 17771 | + const char *second_hint = budget_doc_str(second_doc, "hint"); |
| 17772 | + bool status_error = second_status && strcmp(second_status, "error") == 0; |
| 17773 | + bool reason_named = second_reason && strcmp(second_reason, "over_memory_budget") == 0; |
| 17774 | + bool previous_preserved = second_previous && strcmp(second_previous, "preserved") == 0; |
| 17775 | + bool hint_names_knob = second_hint && strstr(second_hint, "CBM_MEM_BUDGET_MB") != NULL; |
| 17776 | + int budget_mb = budget_doc_int(second_doc, "budget_mb", -1); |
| 17777 | + int peak_rss_mb = budget_doc_int(second_doc, "peak_rss_mb", -1); |
| 17778 | + yyjson_doc_free(second_doc); |
| 17779 | + free(second); |
| 17780 | + long db_size_after = (long)cbm_file_size(db_path); |
| 17781 | + |
| 17782 | + /* The previous generation still answers with its full graph. */ |
| 17783 | + char status_args[CBM_SZ_2K]; |
| 17784 | + snprintf(status_args, sizeof(status_args), "{\"project\":\"%s\"}", project ? project : ""); |
| 17785 | + char *status = srv && project ? cbm_mcp_handle_tool(srv, "index_status", status_args) : NULL; |
| 17786 | + /* index_status answers in the tree format ("nodes: <n>" on its own line). */ |
| 17787 | + char *status_text = extract_text_content(status); |
| 17788 | + const char *nodes_line = status_text ? strstr(status_text, "\nnodes: ") : NULL; |
| 17789 | + int nodes_after = nodes_line ? (int)strtol(nodes_line + strlen("\nnodes: "), NULL, 10) : -1; |
| 17790 | + if (nodes_after != nodes_first) { |
| 17791 | + printf(" index_status after the failed attempt: %.400s\n", |
| 17792 | + status_text ? status_text : "(null)"); |
| 17793 | + } |
| 17794 | + free(status_text); |
| 17795 | + free(status); |
| 17796 | + |
| 17797 | + cbm_mcp_server_free(srv); |
| 17798 | + cleanup_project_db(cache, project); |
| 17799 | + free(project); |
| 17800 | + if (saved_workers_copy) { |
| 17801 | + cbm_setenv("CBM_WORKERS", saved_workers_copy, 1); |
| 17802 | + } else { |
| 17803 | + cbm_unsetenv("CBM_WORKERS"); |
| 17804 | + } |
| 17805 | + free(saved_workers_copy); |
| 17806 | + restore_cache_dir(saved_cache_copy); |
| 17807 | + free(saved_cache_copy); |
| 17808 | + th_rmtree(cache); |
| 17809 | + th_rmtree(repo); |
| 17810 | + |
| 17811 | + ASSERT_TRUE(files_ok); |
| 17812 | + ASSERT_TRUE(had_server); |
| 17813 | + ASSERT_TRUE(first_indexed); |
| 17814 | + ASSERT_GT(nodes_first, 0); |
| 17815 | + ASSERT_TRUE(second_is_error); |
| 17816 | + ASSERT_TRUE(status_error); |
| 17817 | + ASSERT_TRUE(reason_named); |
| 17818 | + ASSERT_TRUE(previous_preserved); |
| 17819 | + ASSERT_TRUE(hint_names_knob); |
| 17820 | + ASSERT_EQ(budget_mb, 1); |
| 17821 | + ASSERT_GT(peak_rss_mb, budget_mb); |
| 17822 | + /* Preserved on disk and still served: same file size, same node count. */ |
| 17823 | + ASSERT_GT(db_size_before, 0L); |
| 17824 | + ASSERT_EQ(db_size_after, db_size_before); |
| 17825 | + ASSERT_EQ(nodes_after, nodes_first); |
| 17826 | + PASS(); |
| 17827 | +} |
| 17828 | + |
17673 | 17829 | TEST(index_repository_cli_name_override_issue823) { |
17674 | 17830 | #ifdef _WIN32 |
17675 | 17831 | SKIP_PLATFORM("POSIX fork harness required to isolate supervisor host mark"); |
@@ -20238,6 +20394,7 @@ SUITE(mcp) { |
20238 | 20394 | RUN_TEST(index_repository_relative_path_uses_explicit_session_root); |
20239 | 20395 | RUN_TEST(index_repository_supervisor_uses_canonical_session_path); |
20240 | 20396 | RUN_TEST(index_repository_cli_name_override_issue823); |
| 20397 | + RUN_TEST(index_repository_over_budget_reports_named_reason); |
20241 | 20398 | RUN_TEST(index_supervisor_unsafe_clean_is_never_fallback_or_recovery); |
20242 | 20399 | RUN_TEST(index_supervisor_gate_requires_marked_host_issue845); |
20243 | 20400 | RUN_TEST(index_supervisor_start_failure_is_fail_closed_in_real_host); |
|
0 commit comments