@@ -3134,13 +3134,96 @@ static void add_excluded_summary(yyjson_mut_doc *doc, yyjson_mut_val *root, char
31343134 yyjson_mut_obj_add_val (doc , root , "excluded" , excluded );
31353135}
31363136
3137+ /* Cap on per-file skips embedded in the JSON response — keep it compact on
3138+ * large repos. The FULL, uncapped list always goes to the per-run logfile;
3139+ * the JSON carries "count" + "truncated" so nothing is silently hidden. */
3140+ enum { INDEX_SKIPPED_FILE_CAP = 50 };
3141+
3142+ /* Attach a summary of per-file skips (Stage 2 / Track B). Always emits a
3143+ * top-level "skipped_count" (0 on clean runs) so consumers can rely on it.
3144+ * When there are skips, also emits:
3145+ * "skipped": {"files":[{path,reason,phase}..(<=50)], "count":N, "truncated":bool}
3146+ * and, if a per-run logfile was written, "logfile": "<path>".
3147+ * The run status stays "indexed" — a skipped file is the expected handled
3148+ * outcome, not a failure. errs[] is borrowed (copied into doc). */
3149+ static void add_skipped_summary (yyjson_mut_doc * doc , yyjson_mut_val * root ,
3150+ const cbm_file_error_t * errs , int count , const char * logfile ) {
3151+ yyjson_mut_obj_add_int (doc , root , "skipped_count" , count < 0 ? 0 : count );
3152+ if (!errs || count <= 0 ) {
3153+ return ;
3154+ }
3155+ yyjson_mut_val * skipped = yyjson_mut_obj (doc );
3156+ yyjson_mut_val * files = yyjson_mut_arr (doc );
3157+ int shown = count < INDEX_SKIPPED_FILE_CAP ? count : INDEX_SKIPPED_FILE_CAP ;
3158+ for (int i = 0 ; i < shown ; i ++ ) {
3159+ yyjson_mut_val * fe = yyjson_mut_obj (doc );
3160+ yyjson_mut_obj_add_strcpy (doc , fe , "path" , errs [i ].path ? errs [i ].path : "" );
3161+ yyjson_mut_obj_add_strcpy (doc , fe , "reason" , errs [i ].reason ? errs [i ].reason : "" );
3162+ yyjson_mut_obj_add_strcpy (doc , fe , "phase" , errs [i ].phase ? errs [i ].phase : "" );
3163+ yyjson_mut_arr_add_val (files , fe );
3164+ }
3165+ yyjson_mut_obj_add_val (doc , skipped , "files" , files );
3166+ yyjson_mut_obj_add_int (doc , skipped , "count" , count );
3167+ yyjson_mut_obj_add_bool (doc , skipped , "truncated" , count > INDEX_SKIPPED_FILE_CAP );
3168+ yyjson_mut_obj_add_val (doc , root , "skipped" , skipped );
3169+ if (logfile && logfile [0 ]) {
3170+ yyjson_mut_obj_add_strcpy (doc , root , "logfile" , logfile );
3171+ }
3172+ }
3173+
3174+ /* Write the FULL (uncapped) skip list to a per-run logfile — ONLY when >=1 file
3175+ * was skipped (no logfile on a clean run). Location:
3176+ * $CBM_INDEX_LOG (override) else <cache_dir>/logs/<project>-<epoch>.log
3177+ * Returns true and fills out_path on success. */
3178+ static bool write_skip_logfile (const char * project , const cbm_file_error_t * errs , int count ,
3179+ char * out_path , size_t out_sz ) {
3180+ if (!errs || count <= 0 ) {
3181+ return false;
3182+ }
3183+ char path [CBM_SZ_1K ];
3184+ const char * override = getenv ("CBM_INDEX_LOG" );
3185+ if (override && override [0 ]) {
3186+ snprintf (path , sizeof (path ), "%s" , override );
3187+ } else {
3188+ const char * cdir = cbm_resolve_cache_dir ();
3189+ if (!cdir ) {
3190+ return false;
3191+ }
3192+ char logdir [CBM_SZ_1K ];
3193+ snprintf (logdir , sizeof (logdir ), "%s/logs" , cdir );
3194+ cbm_mkdir_p (logdir , 0755 );
3195+ snprintf (path , sizeof (path ), "%s/%s-%lld.log" , logdir , project ? project : "index" ,
3196+ (long long )time (NULL ));
3197+ }
3198+ FILE * f = cbm_fopen (path , "wb" );
3199+ if (!f ) {
3200+ cbm_log_warn ("index.logfile_open_fail" , "path" , path );
3201+ return false;
3202+ }
3203+ (void )fprintf (f , "# codebase-memory-mcp index skip report\n" );
3204+ (void )fprintf (f , "# project=%s skipped=%d\n" , project ? project : "" , count );
3205+ (void )fprintf (f , "# columns: phase\treason\tpath\n" );
3206+ for (int i = 0 ; i < count ; i ++ ) {
3207+ (void )fprintf (f , "%s\t%s\t%s\n" , errs [i ].phase ? errs [i ].phase : "" ,
3208+ errs [i ].reason ? errs [i ].reason : "" , errs [i ].path ? errs [i ].path : "" );
3209+ }
3210+ (void )fclose (f );
3211+ if (out_path && out_sz ) {
3212+ snprintf (out_path , out_sz , "%s" , path );
3213+ }
3214+ return true;
3215+ }
3216+
31373217/* Build the success portion of the index_repository response.
31383218 * Returns true when status should be "degraded" (#334 plausibility gate). */
31393219static bool build_index_success_response (cbm_mcp_server_t * srv , yyjson_mut_doc * doc ,
31403220 yyjson_mut_val * root , const char * project_name ,
31413221 const char * repo_path , bool persistence , cbm_pipeline_t * p ,
3142- char * * excluded_dirs , int excluded_count ) {
3222+ char * * excluded_dirs , int excluded_count ,
3223+ const cbm_file_error_t * file_errors , int file_error_count ,
3224+ const char * logfile ) {
31433225 add_excluded_summary (doc , root , excluded_dirs , excluded_count );
3226+ add_skipped_summary (doc , root , file_errors , file_error_count , logfile );
31443227
31453228 int exp_nodes = -1 ;
31463229 int exp_edges = -1 ;
@@ -3302,6 +3385,12 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
33023385 int excluded_count = 0 ;
33033386 cbm_pipeline_get_excluded (p , & excluded_dirs , & excluded_count );
33043387
3388+ /* Capture the per-file skip list (Stage 2 / Track B) while the pipeline
3389+ * still owns the strings; the response builder copies them into the doc. */
3390+ cbm_file_error_t * file_errors = NULL ;
3391+ int file_error_count = 0 ;
3392+ cbm_pipeline_get_file_errors (p , & file_errors , & file_error_count );
3393+
33053394 cbm_mem_collect (); /* return mimalloc pages to OS after large indexing */
33063395
33073396 /* Invalidate cached store so next query reopens the fresh database */
@@ -3319,8 +3408,15 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
33193408 yyjson_mut_obj_add_str (doc , root , "project" , project_name );
33203409
33213410 if (rc == 0 ) {
3322- bool degraded = build_index_success_response (srv , doc , root , project_name , repo_path ,
3323- persistence , p , excluded_dirs , excluded_count );
3411+ /* Write the per-run logfile ONLY when there were skips (no logfile on a
3412+ * clean run). The FULL list goes to the file; the JSON caps at 50. */
3413+ char logfile_path [CBM_SZ_1K ];
3414+ logfile_path [0 ] = '\0' ;
3415+ bool has_logfile = write_skip_logfile (project_name , file_errors , file_error_count ,
3416+ logfile_path , sizeof (logfile_path ));
3417+ bool degraded = build_index_success_response (
3418+ srv , doc , root , project_name , repo_path , persistence , p , excluded_dirs , excluded_count ,
3419+ file_errors , file_error_count , has_logfile ? logfile_path : NULL );
33243420 yyjson_mut_obj_add_str (doc , root , "status" , degraded ? "degraded" : "indexed" );
33253421 } else {
33263422 yyjson_mut_obj_add_str (doc , root , "status" , "error" );
0 commit comments