Skip to content

Commit 306c02d

Browse files
authored
Merge branch 'main' into fix/yaml-remove-absent-config
2 parents 3007dbf + 745794d commit 306c02d

8 files changed

Lines changed: 427 additions & 63 deletions

File tree

‎scripts/run-test-wave.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import signal
1818
import subprocess
1919
import sys
20+
import tempfile
2021
import time
2122
from dataclasses import dataclass
2223

@@ -105,6 +106,31 @@ def append_log(path: pathlib.Path, message: str) -> None:
105106
stream.write("\n")
106107

107108

109+
def publish_barrier_file(path: pathlib.Path, text: str) -> None:
110+
"""Publish a barrier file so a poller sees either no file or its content.
111+
112+
Path.write_text creates and truncates before it writes, so a reader that
113+
polls for existence and then parses the content can observe the zero-byte
114+
window in between. Write next to the destination and rename into place.
115+
"""
116+
with tempfile.NamedTemporaryFile(
117+
mode="w",
118+
encoding="utf-8",
119+
newline="\n",
120+
dir=path.parent,
121+
prefix=f".{path.name}.",
122+
delete=False,
123+
) as temporary:
124+
temporary.write(text)
125+
temporary.flush()
126+
temporary_path = pathlib.Path(temporary.name)
127+
try:
128+
os.replace(temporary_path, path)
129+
except BaseException:
130+
temporary_path.unlink(missing_ok=True)
131+
raise
132+
133+
108134
def start_suite(
109135
suite: str,
110136
runner_command: list[str],
@@ -288,12 +314,12 @@ def wait_for_test_pre_terminate_barrier(
288314
ready = barrier_dir / f"{active.name}.ready"
289315
leader_exited = barrier_dir / f"{active.name}.leader-exited"
290316
release = barrier_dir / f"{active.name}.release"
291-
ready.write_text(f"{active.process.pid}\n", encoding="utf-8")
317+
publish_barrier_file(ready, f"{active.process.pid}\n")
292318
deadline = time.monotonic() + 10
293319
while not release.exists():
294320
returncode = active.process.poll()
295321
if returncode is not None and not leader_exited.exists():
296-
leader_exited.write_text(f"{returncode}\n", encoding="utf-8")
322+
publish_barrier_file(leader_exited, f"{returncode}\n")
297323
if time.monotonic() >= deadline:
298324
raise RuntimeError(
299325
f"test pre-terminate barrier for {active.name!r} was not released"
@@ -312,7 +338,7 @@ def wait_for_test_post_exit_barrier(
312338
return
313339
ready = barrier_dir / f"{suite}.ready"
314340
release = barrier_dir / f"{suite}.release"
315-
ready.write_text("child exited; result intentionally not recorded\n", encoding="utf-8")
341+
publish_barrier_file(ready, "child exited; result intentionally not recorded\n")
316342
deadline = time.monotonic() + 10
317343
while not release.exists():
318344
if time.monotonic() >= deadline:

‎src/discover/discover.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,18 @@ static int wide_stat(const char *path, struct stat *st) {
758758
/* Stat a path, skipping symlinks (POSIX) and junctions / reparse points
759759
* (Windows). Returns 0 on success, -1 to skip. Skipping reparse points keeps
760760
* discovery from walking through a junction that points outside the project
761-
* root, mirroring the POSIX S_ISLNK skip. */
762-
static int safe_stat(const char *abs_path, struct stat *st) {
761+
* root, mirroring the POSIX S_ISLNK skip. *is_symlink reports whether the
762+
* skip (if any) was specifically the symlink/reparse-point check, as
763+
* opposed to some other stat failure (permissions, a race with a delete). */
764+
static int safe_stat(const char *abs_path, struct stat *st, bool *is_symlink) {
765+
*is_symlink = false;
763766
#ifdef _WIN32
764767
wchar_t *wpath = cbm_path_to_wide(abs_path);
765768
if (wpath) {
766769
DWORD attr = GetFileAttributesW(wpath);
767770
free(wpath);
768771
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_REPARSE_POINT)) {
772+
*is_symlink = true;
769773
return CBM_NOT_FOUND;
770774
}
771775
}
@@ -775,6 +779,7 @@ static int safe_stat(const char *abs_path, struct stat *st) {
775779
return CBM_NOT_FOUND;
776780
}
777781
if (S_ISLNK(st->st_mode)) {
782+
*is_symlink = true;
778783
return CBM_NOT_FOUND;
779784
}
780785
return 0;
@@ -891,9 +896,15 @@ static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *fram
891896
}
892897

893898
struct stat st;
894-
if (safe_stat(abs_path, &st) != 0) {
899+
bool is_symlink = false;
900+
if (safe_stat(abs_path, &st, &is_symlink) != 0) {
895901
if (out->count_only) {
896902
out->failed = true;
903+
} else if (is_symlink) {
904+
/* Deliberately not indexed (#963): record so callers can
905+
* surface it, matching the directory-exclusion and file
906+
* skip_reason paths a few lines below. */
907+
file_list_add_ignored(out, rel_path, "symlink");
897908
}
898909
return;
899910
}

‎src/mcp/mcp.c‎

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,18 +4195,19 @@ static char *bm25_search(cbm_store_t *store, const char *project, const char *qu
41954195
}
41964196

41974197
/* Extract keyword strings from a yyjson array into `keywords`. Returns the
4198-
* number of strings copied (capped at `max_out`). */
4198+
* number of strings copied (capped at `max_out`), or -1 when any element is
4199+
* not a string: a mixed-type array is a caller error and is never silently
4200+
* narrowed to its string members. */
41994201
static int extract_semantic_keywords(yyjson_val *sq_val, const char **keywords, int max_out) {
4200-
int kw_count = (int)yyjson_arr_size(sq_val);
4201-
if (kw_count > max_out) {
4202-
kw_count = max_out;
4203-
}
42044202
size_t kw_idx = 0;
42054203
size_t kw_max = 0;
42064204
yyjson_val *kw_val;
42074205
int ki = 0;
42084206
yyjson_arr_foreach(sq_val, kw_idx, kw_max, kw_val) {
4209-
if (ki < kw_count && yyjson_is_str(kw_val)) {
4207+
if (!yyjson_is_str(kw_val)) {
4208+
return -1;
4209+
}
4210+
if (ki < max_out) {
42104211
keywords[ki++] = yyjson_get_str(kw_val);
42114212
}
42124213
}
@@ -4237,13 +4238,20 @@ static void emit_semantic_results(yyjson_mut_doc *doc, yyjson_mut_val *root,
42374238
yyjson_mut_obj_add_val(doc, root, "semantic", sem);
42384239
}
42394240

4241+
typedef enum {
4242+
SQ_RUN_OK = 0,
4243+
SQ_RUN_TYPE_ERROR, /* semantic_query is not an array of strings */
4244+
SQ_RUN_STORE_ERROR, /* the vector scan itself failed */
4245+
} sq_run_status_t;
4246+
42404247
/* Run the semantic_query vector search from raw args. Sets *out_vresults /
42414248
* *out_vcount (caller frees via cbm_store_free_vector_results when vcount>0).
4242-
* Returns true if semantic_query was provided as a non-array (type error —
4243-
* caller should surface to the user). */
4244-
static bool run_semantic_query_core(const char *args, cbm_store_t *store, const char *project,
4245-
int materialize_limit, cbm_vector_result_t **out_vresults,
4246-
int *out_vcount, bool *out_present) {
4249+
* A store without a vector table (lean index) is an empty result; a failed
4250+
* scan is SQ_RUN_STORE_ERROR and must never be rendered as zero matches. */
4251+
static sq_run_status_t run_semantic_query_core(const char *args, cbm_store_t *store,
4252+
const char *project, int materialize_limit,
4253+
cbm_vector_result_t **out_vresults, int *out_vcount,
4254+
bool *out_present) {
42474255
enum { MAX_KW_SEARCH = 32 };
42484256
*out_vresults = NULL;
42494257
*out_vcount = 0;
@@ -4256,25 +4264,31 @@ static bool run_semantic_query_core(const char *args, cbm_store_t *store, const
42564264
if (out_present && sq_val) {
42574265
*out_present = true;
42584266
}
4259-
bool type_error = false;
4267+
sq_run_status_t status = SQ_RUN_OK;
42604268
if (sq_val && !yyjson_is_arr(sq_val)) {
4261-
type_error = true;
4269+
status = SQ_RUN_TYPE_ERROR;
42624270
} else if (sq_val && yyjson_arr_size(sq_val) > 0) {
42634271
const char *keywords[MAX_KW_SEARCH];
42644272
int ki = extract_semantic_keywords(sq_val, keywords, MAX_KW_SEARCH);
4265-
cbm_vector_result_t *vresults = NULL;
4266-
int vcount = 0;
4267-
if (cbm_store_vector_search(store, project, keywords, ki, materialize_limit, &vresults,
4268-
&vcount) == CBM_STORE_OK &&
4269-
vcount > 0) {
4270-
*out_vresults = vresults;
4271-
*out_vcount = vcount;
4273+
if (ki < 0) {
4274+
status = SQ_RUN_TYPE_ERROR;
4275+
} else {
4276+
cbm_vector_result_t *vresults = NULL;
4277+
int vcount = 0;
4278+
int rc = cbm_store_vector_search(store, project, keywords, ki, materialize_limit,
4279+
&vresults, &vcount);
4280+
if (rc == CBM_STORE_ERR) {
4281+
status = SQ_RUN_STORE_ERROR;
4282+
} else if (rc == CBM_STORE_OK && vcount > 0) {
4283+
*out_vresults = vresults;
4284+
*out_vcount = vcount;
4285+
}
42724286
}
42734287
}
42744288
if (args_doc) {
42754289
yyjson_doc_free(args_doc);
42764290
}
4277-
return type_error;
4291+
return status;
42784292
}
42794293

42804294
static bool search_graph_arg_present(const char *args, const char *name) {
@@ -5134,9 +5148,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
51345148
cbm_vector_result_t *vresults = NULL;
51355149
int vcount = 0;
51365150
bool sq_present = false;
5137-
bool sq_type_error = run_semantic_query_core(args, store, project, semantic_materialize_limit,
5138-
&vresults, &vcount, &sq_present);
5139-
if (sq_type_error) {
5151+
sq_run_status_t sq_status = run_semantic_query_core(
5152+
args, store, project, semantic_materialize_limit, &vresults, &vcount, &sq_present);
5153+
if (sq_status != SQ_RUN_OK) {
51405154
if (fields_owner) {
51415155
yyjson_doc_free(fields_owner);
51425156
}
@@ -5146,11 +5160,17 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
51465160
free(qn_pattern);
51475161
free(file_pattern);
51485162
free(relationship);
5163+
if (sq_status == SQ_RUN_STORE_ERROR) {
5164+
return cbm_mcp_text_result(
5165+
"semantic search failed: the vector index could not be scanned — see the "
5166+
"server log for the SQLite error; reindex the project if it persists.",
5167+
true);
5168+
}
51495169
return cbm_mcp_text_result(
51505170
"semantic_query must be an array of keyword strings, e.g. "
5151-
"[\"send\",\"pubsub\",\"publish\"] — not a single string. Split your query "
5152-
"into individual keywords; each is scored independently via per-keyword "
5153-
"min-cosine.",
5171+
"[\"send\",\"pubsub\",\"publish\"] — not a single string, and every element "
5172+
"must be a string. Split your query into individual keywords; each is scored "
5173+
"independently via per-keyword min-cosine.",
51545174
true);
51555175
}
51565176

0 commit comments

Comments
 (0)