11#include "operations/result_wire.h"
22#include "operations/read.h"
3+ #include "operations/compact_out.h"
4+ #include "foundation/mem_core.h"
35#include "operations/coverage.h"
46#include "operations/snippet.h"
57#include "operations/search.h"
@@ -112,7 +114,7 @@ static char *copy_string(const char *text) {
112114 return NULL ;
113115 }
114116 size_t len = strlen (text );
115- char * copy = malloc ( len + 1U );
117+ char * copy = cbm_alloc ( CBM_MEM_CLASS_OTHER , len + 1U );
116118 if (copy ) {
117119 memcpy (copy , text , len + 1U );
118120 }
@@ -125,8 +127,8 @@ typedef struct {
125127} project_ref_t ;
126128
127129static void project_ref_clear (project_ref_t * ref ) {
128- free ( ref -> name );
129- free ( ref -> db_file );
130+ cbm_free ( CBM_MEM_CLASS_OTHER , ref -> name );
131+ cbm_free ( CBM_MEM_CLASS_OTHER , ref -> db_file );
130132 ref -> name = NULL ;
131133 ref -> db_file = NULL ;
132134}
@@ -211,6 +213,58 @@ static void add_project_entry(yyjson_mut_doc *doc, yyjson_mut_val *array, const
211213 cbm_store_close (store );
212214}
213215
216+ /* Stable union schema, even when some roots have no Git branch. */
217+ static char * projects_tree (yyjson_mut_doc * doc , yyjson_mut_val * projects , bool details ,
218+ bool metadata_only ) {
219+ static const char * const cols [] = {"name" , "root_path" , "branch" ,
220+ "nodes" , "edges" , "size_bytes" };
221+ static const bool strings [] = {true, true, true, false, false, false};
222+ static const bool prefixes [] = {false, true, false, false, false, false};
223+ int ncols = metadata_only ? 2 : details ? 6 : 3 ;
224+ int rows = (int )yyjson_mut_arr_size (projects );
225+ size_t count = (size_t )rows * (size_t )ncols ;
226+ const char * * cells = cbm_calloc (CBM_MEM_CLASS_OTHER , count * sizeof (* cells ));
227+ char (* numbers )[32 ] = cbm_calloc (CBM_MEM_CLASS_OTHER , count * sizeof (* numbers ));
228+ if (!cells || !numbers ) {
229+ cbm_free (CBM_MEM_CLASS_OTHER , cells );
230+ cbm_free (CBM_MEM_CLASS_OTHER , numbers );
231+ return NULL ;
232+ }
233+ for (int row = 0 ; row < rows ; ++ row ) {
234+ yyjson_mut_val * item = yyjson_mut_arr_get (projects , (size_t )row );
235+ for (int col = 0 ; col < ncols ; ++ col ) {
236+ size_t i = (size_t )row * (size_t )ncols + (size_t )col ;
237+ yyjson_mut_val * value = yyjson_mut_obj_get (item , cols [col ]);
238+ if (strings [col ]) {
239+ cells [i ] = value ? yyjson_mut_get_str (value ) : "" ;
240+ } else {
241+ snprintf (numbers [i ], sizeof (numbers [i ]), "%lld" ,
242+ (long long )yyjson_mut_get_sint (value ));
243+ cells [i ] = numbers [i ];
244+ }
245+ }
246+ }
247+ cbm_sb_t sb ;
248+ cbm_sb_init (& sb );
249+ cbm_tree_table_rows_profiled (& sb , "projects" , rows , cols , ncols , cells , strings , prefixes );
250+ cbm_free (CBM_MEM_CLASS_OTHER , cells );
251+ cbm_free (CBM_MEM_CLASS_OTHER , numbers );
252+ yyjson_mut_val * root = yyjson_mut_doc_get_root (doc );
253+ yyjson_mut_obj_remove_key (root , "projects" );
254+ yyjson_mut_obj_remove_key (root , "offset" );
255+ yyjson_mut_obj_remove_key (root , "limit" );
256+ char * json = cbm_operation_json_write (doc );
257+ char * metadata = json ? cbm_json_to_tree (json ) : NULL ;
258+ free (json );
259+ if (!metadata ) {
260+ cbm_sb_free (& sb );
261+ return NULL ;
262+ }
263+ cbm_sb_append (& sb , metadata );
264+ free (metadata );
265+ return cbm_sb_finish (& sb );
266+ }
267+
214268static cbm_operation_result_t execute_projects (const char * args_json ) {
215269 int offset = json_int_arg (args_json , "offset" , 0 );
216270 int limit = json_int_arg (args_json , "limit" , 50 );
@@ -219,7 +273,7 @@ static cbm_operation_result_t execute_projects(const char *args_json) {
219273 char * detail = json_string_arg (args_json , "detail" );
220274 bool include_details = json_bool_arg (args_json , "include_details" , false) ||
221275 (detail && strcmp (detail , "stats" ) == 0 );
222- free ( detail );
276+ cbm_free ( CBM_MEM_CLASS_OTHER , detail );
223277 bool metadata_only = json_bool_arg (args_json , "metadata_only" , false);
224278 if (metadata_only ) {
225279 include_details = false;
@@ -340,7 +394,15 @@ static cbm_operation_result_t execute_projects(const char *args_json) {
340394 yyjson_mut_obj_add_str (doc , root , "hint" ,
341395 "No projects indexed. Run 'codebase-memory-cli index .' first." );
342396 }
343- return json_doc_result (doc , false);
397+ char * format = json_string_arg (args_json , "format" );
398+ bool wants_json = format && strcmp (format , "json" ) == 0 ;
399+ cbm_free (CBM_MEM_CLASS_OTHER , format );
400+ if (wants_json )
401+ return json_doc_result (doc , false);
402+ char * tree = projects_tree (doc , projects , include_details , metadata_only );
403+ yyjson_mut_doc_free (doc );
404+ return tree ? cbm_operation_result_take (tree , false)
405+ : json_error ("result encoding failed" , NULL );
344406}
345407
346408static void add_status_coverage (yyjson_mut_doc * doc , yyjson_mut_val * root , cbm_store_t * store ,
@@ -490,7 +552,7 @@ static void add_status_coverage(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_s
490552static cbm_operation_result_t execute_status (const char * args_json ) {
491553 char * project = json_string_arg (args_json , "project" );
492554 if (!project || !project [0 ]) {
493- free ( project );
555+ cbm_free ( CBM_MEM_CLASS_OTHER , project );
494556 yyjson_mut_doc * doc = yyjson_mut_doc_new (NULL );
495557 yyjson_mut_val * root = doc ? yyjson_mut_obj (doc ) : NULL ;
496558 if (!doc || !root ) {
@@ -512,7 +574,7 @@ static cbm_operation_result_t execute_status(const char *args_json) {
512574 : json_error ("project not indexed" ,
513575 "Run 'codebase-memory-cli index .' in the repository or specify an "
514576 "indexed --project." );
515- free ( project );
577+ cbm_free ( CBM_MEM_CLASS_OTHER , project );
516578 return error ;
517579 }
518580 int nodes = cbm_store_count_nodes (store , project );
@@ -526,15 +588,15 @@ static cbm_operation_result_t execute_status(const char *args_json) {
526588 coverage_samples = OP_COVERAGE_SUMMARY_SAMPLES ;
527589 else if (diagnostics && strcmp (diagnostics , "full" ) == 0 )
528590 coverage_samples = OP_COVERAGE_FILE_CAP ;
529- free ( diagnostics );
591+ cbm_free ( CBM_MEM_CLASS_OTHER , diagnostics );
530592
531593 yyjson_mut_doc * doc = yyjson_mut_doc_new (NULL );
532594 yyjson_mut_val * root = doc ? yyjson_mut_obj (doc ) : NULL ;
533595 if (!doc || !root ) {
534596 if (doc )
535597 yyjson_mut_doc_free (doc );
536598 cbm_store_close (store );
537- free ( project );
599+ cbm_free ( CBM_MEM_CLASS_OTHER , project );
538600 return json_error ("result allocation failed" , NULL );
539601 }
540602 yyjson_mut_doc_set_root (doc , root );
@@ -594,7 +656,7 @@ static cbm_operation_result_t execute_status(const char *args_json) {
594656 "Project is empty. Re-run 'codebase-memory-cli index .' to populate." );
595657 }
596658 cbm_store_close (store );
597- free ( project );
659+ cbm_free ( CBM_MEM_CLASS_OTHER , project );
598660 return json_doc_result (doc , false);
599661}
600662
0 commit comments