@@ -510,7 +510,11 @@ static const tool_def_t TOOLS[] = {
510510 "filtered out. When true, test nodes are included with a test column/marker.\"},"
511511 "\"format\":{\"type\":\"string\",\"enum\":[\"tree\",\"json\"],\"default\":\"tree\","
512512 "\"description\":\"Response encoding. tree (default): prefix-grouped text rows. "
513- "json: the SAME tree model as structured JSON (groups + column-ordered row arrays).\"}},"
513+ "json: the SAME tree model as structured JSON (groups + column-ordered row arrays).\"},"
514+ "\"include_evidence\":{\"type\":\"boolean\",\"default\":false,"
515+ "\"description\":\"Add how each hop was resolved: a strategy class (lsp | language_rule | "
516+ "heuristic | unresolved) and the resolver's confidence. Off by default — it adds two "
517+ "columns per row. Use it to judge whether an edge is trustworthy, not to find edges.\"}},"
514518 "\"required\":[\"function_name\",\"project\"]}" },
515519
516520 {"get_code_snippet" , "Get code snippet" ,
@@ -5648,6 +5652,96 @@ static const char *bfs_edge_args_for_hop(cbm_traverse_result_t *tr, int64_t hop_
56485652 return NULL ;
56495653}
56505654
5655+ /* Classify a resolver strategy into the CLOSED public vocabulary.
5656+ *
5657+ * The indexer records ~20 internal strategy names on CALLS edges
5658+ * (lsp_trait_dispatch, php_self_static, callee_suffix, ...) and the set grows
5659+ * with every language. Publishing those verbatim would make each internal
5660+ * resolver name public API by accident, so a rename would silently change a
5661+ * user-visible field. We publish the CLASS instead: adding lsp_foo_dispatch
5662+ * maps automatically, while a genuinely new KIND of resolution fails the
5663+ * pinning test in tests/test_mcp.c and forces a deliberate decision.
5664+ *
5665+ * Returns NULL only for a NULL/empty strategy — every non-empty value lands in
5666+ * a class, so an unmapped strategy can never silently vanish from output. */
5667+ const char * cbm_mcp_edge_strategy_class (const char * strategy ) {
5668+ if (!strategy || !strategy [0 ]) {
5669+ return NULL ;
5670+ }
5671+ /* Order matters: lsp_unresolved is an LSP strategy that failed, and the
5672+ * caller cares that it did NOT resolve — so it classifies as unresolved. */
5673+ if (strcmp (strategy , "lsp_unresolved" ) == 0 || strcmp (strategy , "unknown" ) == 0 ) {
5674+ return "unresolved" ;
5675+ }
5676+ if (strncmp (strategy , "lsp_" , 4 ) == 0 ) {
5677+ return "lsp" ;
5678+ }
5679+ if (strncmp (strategy , "php_" , 4 ) == 0 || strncmp (strategy , "perl_" , 5 ) == 0 ) {
5680+ return "language_rule" ;
5681+ }
5682+ /* Everything else is a name/shape heuristic: callee_suffix,
5683+ * field_type_hint, service_pattern, fastapi_depends, unique_name, ... */
5684+ return "heuristic" ;
5685+ }
5686+
5687+ /* Find the resolution evidence on the edge that leads to the given hop node —
5688+ * the same endpoint-matching rule as bfs_edge_args_for_hop (target for an
5689+ * outbound trace, source for inbound, so both directions surface it).
5690+ *
5691+ * Reads only from tr->edges, and callers pass the PAGINATED view, so evidence
5692+ * is emitted for the rows on this page and nothing else.
5693+ *
5694+ * Returns false when the edge carries no strategy (non-CALLS edges do not). */
5695+ static bool bfs_edge_evidence_for_hop (cbm_traverse_result_t * tr , int64_t hop_node_id ,
5696+ const char * * class_out , double * confidence_out ) {
5697+ for (int e = 0 ; e < tr -> edge_count ; e ++ ) {
5698+ if (tr -> edges [e ].target_id != hop_node_id && tr -> edges [e ].source_id != hop_node_id ) {
5699+ continue ;
5700+ }
5701+ const char * pj = tr -> edges [e ].properties_json ;
5702+ if (!pj ) {
5703+ continue ;
5704+ }
5705+ const char * key = strstr (pj , "\"strategy\"" );
5706+ if (!key ) {
5707+ continue ;
5708+ }
5709+ const char * open = strchr (key + 10 , '"' );
5710+ if (!open ) {
5711+ continue ;
5712+ }
5713+ open ++ ;
5714+ const char * close = strchr (open , '"' );
5715+ if (!close || close == open ) {
5716+ continue ;
5717+ }
5718+ char raw [CBM_SZ_64 ];
5719+ size_t len = (size_t )(close - open );
5720+ if (len >= sizeof (raw )) {
5721+ len = sizeof (raw ) - 1 ;
5722+ }
5723+ memcpy (raw , open , len );
5724+ raw [len ] = '\0' ;
5725+ const char * cls = cbm_mcp_edge_strategy_class (raw );
5726+ if (!cls ) {
5727+ continue ;
5728+ }
5729+ * class_out = cls ;
5730+ /* Confidence rides on the same edge; absent is reported as -1 so a
5731+ * genuine 0.0 stays distinguishable from "not recorded". */
5732+ * confidence_out = -1.0 ;
5733+ const char * conf = strstr (pj , "\"confidence\"" );
5734+ if (conf ) {
5735+ const char * colon = strchr (conf , ':' );
5736+ if (colon ) {
5737+ * confidence_out = strtod (colon + 1 , NULL );
5738+ }
5739+ }
5740+ return true;
5741+ }
5742+ return false;
5743+ }
5744+
56515745/* TOON table for one trace direction: callees[N]{qn,hop,...} with optional
56525746 * risk / test / args columns. `name` is omitted (it is the qn's last
56535747 * segment); the per-item JSON key envelope was 84% of the legacy payload. */
@@ -6061,7 +6155,7 @@ static int tree_hop_cmp_qn(const void *pa, const void *pb) {
60616155}
60626156
60636157static void bfs_to_tree_table (cbm_sb_t * sb , const char * key , cbm_traverse_result_t * tr ,
6064- bool include_tests ) {
6158+ bool include_tests , bool include_evidence ) {
60656159 int visible = 0 ;
60666160 for (int i = 0 ; i < tr -> visited_count ; i ++ ) {
60676161 if (!include_tests && is_test_file (tr -> visited [i ].node .file_path )) {
@@ -6070,8 +6164,12 @@ static void bfs_to_tree_table(cbm_sb_t *sb, const char *key, cbm_traverse_result
60706164 visible ++ ;
60716165 }
60726166 char buf [CBM_SZ_256 ];
6073- snprintf (buf , sizeof (buf ), "%s: %d (rows: name hop; qn = group prefix + \".\" + name)\n" , key ,
6074- visible );
6167+ snprintf (buf , sizeof (buf ),
6168+ include_evidence
6169+ ? "%s: %d (rows: name hop strategy confidence; qn = group prefix + \".\" + "
6170+ "name)\n"
6171+ : "%s: %d (rows: name hop; qn = group prefix + \".\" + name)\n" ,
6172+ key , visible );
60756173 cbm_sb_append (sb , buf );
60766174 if (tr -> visited_count > 1 ) {
60776175 qsort (tr -> visited , (size_t )tr -> visited_count , sizeof (cbm_node_hop_t ), tree_hop_cmp_qn );
@@ -6093,7 +6191,26 @@ static void bfs_to_tree_table(cbm_sb_t *sb, const char *key, cbm_traverse_result
60936191 cbm_sb_append (sb , ":\n" );
60946192 }
60956193 char row [CBM_SZ_512 ];
6096- snprintf (row , sizeof (row ), " %s %d\n" , plen ? qn + plen + 1 : qn , tr -> visited [i ].hop );
6194+ const char * ev_class = NULL ;
6195+ double ev_conf = -1.0 ;
6196+ if (include_evidence &&
6197+ bfs_edge_evidence_for_hop (tr , tr -> visited [i ].node .id , & ev_class , & ev_conf )) {
6198+ if (ev_conf >= 0.0 ) {
6199+ snprintf (row , sizeof (row ), " %s %d %s %.2f\n" , plen ? qn + plen + 1 : qn ,
6200+ tr -> visited [i ].hop , ev_class , ev_conf );
6201+ } else {
6202+ snprintf (row , sizeof (row ), " %s %d %s -\n" , plen ? qn + plen + 1 : qn ,
6203+ tr -> visited [i ].hop , ev_class );
6204+ }
6205+ } else if (include_evidence ) {
6206+ /* The root hop has no inbound edge, and non-CALLS edges record no
6207+ * strategy. Emit placeholders so the column count stays fixed —
6208+ * a ragged table is worse to parse than an explicit "-". */
6209+ snprintf (row , sizeof (row ), " %s %d - -\n" , plen ? qn + plen + 1 : qn ,
6210+ tr -> visited [i ].hop );
6211+ } else {
6212+ snprintf (row , sizeof (row ), " %s %d\n" , plen ? qn + plen + 1 : qn , tr -> visited [i ].hop );
6213+ }
60976214 cbm_sb_append (sb , row );
60986215 }
60996216}
@@ -6137,6 +6254,10 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
61376254 }
61386255 bool risk_labels = cbm_mcp_get_bool_arg (args , "risk_labels" );
61396256 bool include_tests = cbm_mcp_get_bool_arg (args , "include_tests" );
6257+ /* Off by default: two extra columns on every row is exactly the kind of
6258+ * inflation the tree format exists to avoid. Opt in when you need to judge
6259+ * whether an edge is trustworthy. */
6260+ bool include_evidence = cbm_mcp_get_bool_arg (args , "include_evidence" );
61406261
61416262 if (!func_name ) {
61426263 free (project );
@@ -6424,15 +6545,15 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
64246545 if (flat_trace ) {
64256546 bfs_to_toon_table (& sb , "callees" , & view_out , risk_labels , include_tests , data_flow );
64266547 } else {
6427- bfs_to_tree_table (& sb , "callees" , & view_out , include_tests );
6548+ bfs_to_tree_table (& sb , "callees" , & view_out , include_tests , include_evidence );
64286549 }
64296550 }
64306551 if (do_inbound ) {
64316552 cbm_tree_scalar_int (& sb , "callers_total" , in_total );
64326553 if (flat_trace ) {
64336554 bfs_to_toon_table (& sb , "callers" , & view_in , risk_labels , include_tests , data_flow );
64346555 } else {
6435- bfs_to_tree_table (& sb , "callers" , & view_in , include_tests );
6556+ bfs_to_tree_table (& sb , "callers" , & view_in , include_tests , include_evidence );
64366557 }
64376558 }
64386559 if (more_rows ) {
0 commit comments