3434import tempfile
3535
3636sys .path .insert (0 , os .path .dirname (os .path .abspath (__file__ )))
37- from mcp_stdio import McpServer # noqa: E402
37+ from mcp_stdio import McpServer , wait_projects_with_stats # noqa: E402
3838
3939MATH_TS = (
4040 "export function add(a: number, b: number): number { return a + b; }\n "
@@ -246,6 +246,37 @@ def make_fixture(root):
246246 f .write (text .encode ("utf-8" )) # exact bytes, identical across copies
247247
248248
249+ def no_project_error (index_txt , repo , cache ):
250+ """Assemble the venue diagnostics for an index that left no project row.
251+
252+ The count summary cannot explain a venue-specific empty listing; carry the
253+ index response, the cache contents and the supervisor's worker logs (the
254+ only record of the pipeline's own error) into the CI log.
255+ """
256+ try :
257+ cache_entries = sorted (os .listdir (cache ))
258+ except OSError as exc :
259+ cache_entries = ["<listdir failed: %s>" % exc ]
260+ log_tails = []
261+ logs_dir = os .path .join (cache , "logs" )
262+ if os .path .isdir (logs_dir ):
263+ for log_name in sorted (os .listdir (logs_dir )):
264+ try :
265+ with open (os .path .join (logs_dir , log_name ), "rb" ) as lf :
266+ tail = lf .read ()[- 800 :].decode ("utf-8" , "replace" )
267+ log_tails .append ("%s: %s" % (log_name , tail ))
268+ except OSError as exc :
269+ log_tails .append ("%s: <unreadable: %s>" % (log_name , exc ))
270+ try :
271+ repo_entries = sorted (os .listdir (repo ))
272+ except OSError as exc :
273+ repo_entries = ["<listdir failed: %s>" % exc ]
274+ return {"error" : "no project listed after index; index said %r; "
275+ "cache holds %r; repo holds %r; worker logs: %s"
276+ % (index_txt [:400 ], cache_entries , repo_entries ,
277+ " | " .join (log_tails ) or "<none>" )}
278+
279+
249280def index_and_count (binary , repo , cache ):
250281 """Index `repo` into an isolated cache and return label-resolved counts."""
251282 os .makedirs (cache , exist_ok = True )
@@ -255,42 +286,29 @@ def index_and_count(binary, repo, cache):
255286 index_txt , err = s .tool_text (resp )
256287 if err :
257288 return {"error" : "index tools/call error: %r" % err }
258- lp = s .call_tool ("list_projects" , {}, timeout = 60 )
259- lp_txt , _ = s .tool_text (lp )
260- projects = json .loads (lp_txt ).get ("projects" ) or []
261- if not projects :
262- # The count summary cannot explain a venue-specific empty listing;
263- # carry the index response and the cache contents into the log.
264- try :
265- cache_entries = sorted (os .listdir (cache ))
266- except OSError as exc :
267- cache_entries = ["<listdir failed: %s>" % exc ]
268- # The supervisor's worker logs carry the actual pipeline error.
269- log_tails = []
270- logs_dir = os .path .join (cache , "logs" )
271- if os .path .isdir (logs_dir ):
272- for log_name in sorted (os .listdir (logs_dir )):
273- try :
274- with open (os .path .join (logs_dir , log_name ), "rb" ) as lf :
275- tail = lf .read ()[- 800 :].decode ("utf-8" , "replace" )
276- log_tails .append ("%s: %s" % (log_name , tail ))
277- except OSError as exc :
278- log_tails .append ("%s: <unreadable: %s>" % (log_name , exc ))
279- try :
280- repo_entries = sorted (os .listdir (repo ))
281- except OSError as exc :
282- repo_entries = ["<listdir failed: %s>" % exc ]
283- return {"error" : "no project listed after index; index said %r; "
284- "cache holds %r; repo holds %r; worker logs: %s"
285- % (index_txt [:400 ], cache_entries , repo_entries ,
286- " | " .join (log_tails ) or "<none>" )}
287- p = projects [0 ]
288- out = {"name" : p .get ("name" ), "nodes" : p .get ("nodes" ),
289- "edges" : p .get ("edges" )}
289+ # The index response itself carries the synchronous, authoritative
290+ # counts ("nodes"/"edges"). list_projects publishes its stats columns
291+ # asynchronously — on some venues never within a one-shot session — so
292+ # gating on it misreads a healthy index as a setup failure (#1952).
293+ try :
294+ summary = json .loads (index_txt )
295+ except ValueError :
296+ summary = {}
297+ out = {"name" : summary .get ("project" ), "nodes" : summary .get ("nodes" ),
298+ "edges" : summary .get ("edges" )}
299+ if out ["nodes" ] is None :
300+ # Payload without counts: fall back to list_projects, polled
301+ # because its stats row can trail the index on slow runners.
302+ projects , _ = wait_projects_with_stats (s )
303+ if not projects :
304+ return no_project_error (index_txt , repo , cache )
305+ p = projects [0 ]
306+ out = {"name" : p .get ("name" ), "nodes" : p .get ("nodes" ),
307+ "edges" : p .get ("edges" )}
290308 # Definition-level counts prove the parser ran (not just discovery).
291309 # query_graph defaults to TOON text; this scripted consumer requests
292310 # format="json" ({"columns":[...],"rows":[["<n>"]],...}) explicitly.
293- name = p . get ( "name" )
311+ name = out [ "name" ]
294312 defs = 0
295313 for label in ("Function" , "Class" , "Method" ):
296314 q = "MATCH (n:%s) RETURN count(n)" % label
@@ -331,7 +349,17 @@ def main():
331349
332350 ascii_repo = os .path .join (work , "ascii_repo" )
333351 make_fixture (ascii_repo )
334- base = index_and_count (binary , ascii_repo , os .path .join (work , "c_ascii" ))
352+ # The baseline is setup, not the surface under test: a cold runner can
353+ # lose the first index to daemon startup latency (#1952). One retry
354+ # against a fresh cache separates that environmental window from a
355+ # real indexing failure before the guard declares a precondition skip.
356+ base = {}
357+ for attempt in ("c_ascii" , "c_ascii_retry" ):
358+ base = index_and_count (binary , ascii_repo , os .path .join (work , attempt ))
359+ if not base .get ("error" ) and base .get ("nodes" ):
360+ break
361+ print ("SETUP: ASCII baseline attempt %r did not index: %r"
362+ % (attempt , base ))
335363 if base .get ("error" ) or not base .get ("nodes" ):
336364 print ("SETUP FAIL: ASCII baseline did not index: %r" % base )
337365 return 2
0 commit comments