Skip to content

Commit 03d01b2

Browse files
committed
feat(operations): port compact search and project tree encodings
Port upstream's grouped/flat search rendering and stable project identity and stats tables, reusing the existing response-local prefix directory. Keep typed property values and full tree identities lossless, and trim whole result rows with truthful continuation metadata under output budgets. Preserve the legacy JSON payload model and make the outer CLI --json flag select that encoding explicitly. Keep internal index-root project discovery on JSON so changing the public default does not break index admission. Adapt upstream guards to disk-backed neutral operations and add a real CLI/daemon regression for tree defaults, plain --json, and byte budgets. Validate JSON bytes against the pre-change implementation.
1 parent 0311189 commit 03d01b2

7 files changed

Lines changed: 827 additions & 76 deletions

File tree

‎src/main.c‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "daemon/project_lock.h"
3131
#include "daemon/version_cohort.h"
3232
#include "foundation/json_args.h"
33+
#include "foundation/mem_core.h"
34+
#include "operations/tool_catalog.h"
3335
#include "operations/result_wire.h"
3436
#include "operations/operation.h"
3537
#include "operations/reliability_events.h"
@@ -847,7 +849,7 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
847849
return SKIP_ONE;
848850
}
849851

850-
(void)cli_strip_flag(&argc, argv, "--json");
852+
bool json_requested = cli_strip_flag(&argc, argv, "--json");
851853

852854
/* Supervisor worker role: when this process was spawned as a supervised index
853855
* worker, run indexing in-process (never re-supervise) and write the result to
@@ -954,6 +956,32 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
954956
}
955957
}
956958

959+
/* Outer --json keeps canonical automation on the JSON payload surface. */
960+
const char *schema = cbm_tool_catalog_input_schema(tool_name);
961+
if (json_requested && schema && strstr(schema, "\"format\"")) {
962+
yyjson_doc *input = yyjson_read(args_json, strlen(args_json), 0);
963+
yyjson_mut_doc *doc = input ? yyjson_doc_mut_copy(input, NULL) : NULL;
964+
if (input)
965+
yyjson_doc_free(input);
966+
yyjson_mut_val *root = doc ? yyjson_mut_doc_get_root(doc) : NULL;
967+
char *encoded = NULL;
968+
if (yyjson_mut_is_obj(root)) {
969+
yyjson_mut_obj_remove_key(root, "format");
970+
if (yyjson_mut_obj_add_str(doc, root, "format", "json"))
971+
encoded = yyjson_mut_write(doc, 0, NULL);
972+
}
973+
if (doc)
974+
yyjson_mut_doc_free(doc);
975+
if (!encoded) {
976+
fprintf(stderr, "error: could not select JSON output\n");
977+
free(heap_args);
978+
return SKIP_ONE;
979+
}
980+
free(heap_args);
981+
heap_args = encoded;
982+
args_json = heap_args;
983+
}
984+
957985
bool progress =
958986
!index_worker && cbm_cli_progress_enabled(output_flags.progress_requested,
959987
output_flags.quiet_requested, cli_isatty(2) != 0);
@@ -1120,7 +1148,7 @@ static int run_named_cli(const cbm_cli_command_alias_t *alias, int argc, char **
11201148

11211149
size_t extra = has_positional ? 2U : 0U;
11221150
size_t mapped_count = 1U + (size_t)argc + extra;
1123-
char **mapped = calloc(mapped_count + 1U, sizeof(*mapped));
1151+
char **mapped = cbm_calloc(CBM_MEM_CLASS_OTHER, (mapped_count + 1U) * sizeof(*mapped));
11241152
if (!mapped) {
11251153
(void)fprintf(stderr, "error: out of memory while preparing '%s'\n", alias->command);
11261154
return SKIP_ONE;
@@ -1139,7 +1167,7 @@ static int run_named_cli(const cbm_cli_command_alias_t *alias, int argc, char **
11391167
}
11401168
}
11411169
int result = run_cli((int)out, mapped, project_locks, maintenance_context, true);
1142-
free(mapped);
1170+
cbm_free(CBM_MEM_CLASS_OTHER, mapped);
11431171
return result;
11441172
}
11451173

‎src/operations/index.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,8 @@ static char *index_root_project(const char *path, char *error, size_t error_size
10401040
bool ambiguous = false;
10411041
for (int offset = 0; derived;) {
10421042
char args[CBM_SZ_256];
1043-
snprintf(args, sizeof(args), "{\"metadata_only\":true,\"limit\":500,\"offset\":%d}",
1043+
snprintf(args, sizeof(args),
1044+
"{\"metadata_only\":true,\"format\":\"json\",\"limit\":500,\"offset\":%d}",
10441045
offset);
10451046
cbm_operation_result_t listing =
10461047
cbm_read_operation_execute(CBM_OPERATION_PROJECTS, args, NULL);

‎src/operations/read.c‎

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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

127129
static 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+
214268
static 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

346408
static 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
490552
static 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

Comments
 (0)