Skip to content

Commit cbda1b1

Browse files
committed
Merge fu/toon-output into fu/integration
Resolve src/operations/read.c conflicts for detail, diagnostics and project cleanup by retaining cbm_operation_arg_free; release the new format argument with the same class. Keep stable project tree tables and the unchanged explicit-JSON construction. Resolve src/operations/search.c copy_text and cleanup conflicts with CBM_MEM_CLASS_OPERATION_ARG allocation/releases while retaining grouped/flat and ranked tree rendering, output budgets and continuation. Release both added format arguments through cbm_operation_arg_free. Keep string builders owning temporary buffers until cbm_sb_free and reuse result disposal for encoded metadata and oversized responses to hold the raw-allocation ratchet. Normalize libc glob output into memory-core ownership before BM25 cleanup. Resolve tests/test_integration.c include conflict by retaining result_wire.h and cbm.h and adding mem_core.h. Keep both inherited tests and TOON tests; extend the existing BM25 regression to compare wildcard/literal file-filter JSON bytes and verify operation arguments are released. src/operations/index.c auto-merges the explicit JSON internal listing with the argument migration. src/main.c retains outer --json format selection. Scan all four both-side changed files, including Windows and Apple conditionals, for duplicate functions/declarations/TEST bodies/RUN_TEST registrations; none added. Regenerate and strictly lint the baseline: search stays at 5, read drops from 7 to 6, no file exceeds the argparse branch. Vendored contents unchanged.
2 parents 55d7c9b + 03d01b2 commit cbda1b1

8 files changed

Lines changed: 836 additions & 66 deletions

File tree

‎scripts/memory-core-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ src/operations/index.c 56
6363
src/operations/index_supervisor.c 8
6464
src/operations/project_arg.c 3
6565
src/operations/query.c 4
66-
src/operations/read.c 7
66+
src/operations/read.c 6
6767
src/operations/reliability_events.c 4
6868
src/operations/result_wire.c 1
6969
src/operations/schema.c 3

‎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
@@ -1045,7 +1045,8 @@ static char *index_root_project(const char *path, char *error, size_t error_size
10451045
bool ambiguous = false;
10461046
for (int offset = 0; derived;) {
10471047
char args[CBM_SZ_256];
1048-
snprintf(args, sizeof(args), "{\"metadata_only\":true,\"limit\":500,\"offset\":%d}",
1048+
snprintf(args, sizeof(args),
1049+
"{\"metadata_only\":true,\"format\":\"json\",\"limit\":500,\"offset\":%d}",
10491050
offset);
10501051
cbm_operation_result_t listing =
10511052
cbm_read_operation_execute(CBM_OPERATION_PROJECTS, args, NULL);

‎src/operations/read.c‎

Lines changed: 66 additions & 4 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"
@@ -113,7 +115,7 @@ static char *copy_string(const char *text) {
113115
return NULL;
114116
}
115117
size_t len = strlen(text);
116-
char *copy = malloc(len + 1U);
118+
char *copy = cbm_alloc(CBM_MEM_CLASS_OTHER, len + 1U);
117119
if (copy) {
118120
memcpy(copy, text, len + 1U);
119121
}
@@ -126,8 +128,8 @@ typedef struct {
126128
} project_ref_t;
127129

128130
static void project_ref_clear(project_ref_t *ref) {
129-
free(ref->name);
130-
free(ref->db_file);
131+
cbm_free(CBM_MEM_CLASS_OTHER, ref->name);
132+
cbm_free(CBM_MEM_CLASS_OTHER, ref->db_file);
131133
ref->name = NULL;
132134
ref->db_file = NULL;
133135
}
@@ -212,6 +214,58 @@ static void add_project_entry(yyjson_mut_doc *doc, yyjson_mut_val *array, const
212214
cbm_store_close(store);
213215
}
214216

217+
/* Stable union schema, even when some roots have no Git branch. */
218+
static char *projects_tree(yyjson_mut_doc *doc, yyjson_mut_val *projects, bool details,
219+
bool metadata_only) {
220+
static const char *const cols[] = {"name", "root_path", "branch",
221+
"nodes", "edges", "size_bytes"};
222+
static const bool strings[] = {true, true, true, false, false, false};
223+
static const bool prefixes[] = {false, true, false, false, false, false};
224+
int ncols = metadata_only ? 2 : details ? 6 : 3;
225+
int rows = (int)yyjson_mut_arr_size(projects);
226+
size_t count = (size_t)rows * (size_t)ncols;
227+
const char **cells = cbm_calloc(CBM_MEM_CLASS_OTHER, count * sizeof(*cells));
228+
char (*numbers)[32] = cbm_calloc(CBM_MEM_CLASS_OTHER, count * sizeof(*numbers));
229+
if (!cells || !numbers) {
230+
cbm_free(CBM_MEM_CLASS_OTHER, cells);
231+
cbm_free(CBM_MEM_CLASS_OTHER, numbers);
232+
return NULL;
233+
}
234+
for (int row = 0; row < rows; ++row) {
235+
yyjson_mut_val *item = yyjson_mut_arr_get(projects, (size_t)row);
236+
for (int col = 0; col < ncols; ++col) {
237+
size_t i = (size_t)row * (size_t)ncols + (size_t)col;
238+
yyjson_mut_val *value = yyjson_mut_obj_get(item, cols[col]);
239+
if (strings[col]) {
240+
cells[i] = value ? yyjson_mut_get_str(value) : "";
241+
} else {
242+
snprintf(numbers[i], sizeof(numbers[i]), "%lld",
243+
(long long)yyjson_mut_get_sint(value));
244+
cells[i] = numbers[i];
245+
}
246+
}
247+
}
248+
cbm_sb_t sb;
249+
cbm_sb_init(&sb);
250+
cbm_tree_table_rows_profiled(&sb, "projects", rows, cols, ncols, cells, strings, prefixes);
251+
cbm_free(CBM_MEM_CLASS_OTHER, cells);
252+
cbm_free(CBM_MEM_CLASS_OTHER, numbers);
253+
yyjson_mut_val *root = yyjson_mut_doc_get_root(doc);
254+
yyjson_mut_obj_remove_key(root, "projects");
255+
yyjson_mut_obj_remove_key(root, "offset");
256+
yyjson_mut_obj_remove_key(root, "limit");
257+
char *json = cbm_operation_json_write(doc);
258+
char *metadata = json ? cbm_json_to_tree(json) : NULL;
259+
free(json);
260+
if (!metadata) {
261+
cbm_sb_free(&sb);
262+
return NULL;
263+
}
264+
cbm_sb_append(&sb, metadata);
265+
free(metadata);
266+
return cbm_sb_finish(&sb);
267+
}
268+
215269
static cbm_operation_result_t execute_projects(const char *args_json) {
216270
int offset = json_int_arg(args_json, "offset", 0);
217271
int limit = json_int_arg(args_json, "limit", 50);
@@ -341,7 +395,15 @@ static cbm_operation_result_t execute_projects(const char *args_json) {
341395
yyjson_mut_obj_add_str(doc, root, "hint",
342396
"No projects indexed. Run 'codebase-memory-cli index .' first.");
343397
}
344-
return json_doc_result(doc, false);
398+
char *format = json_string_arg(args_json, "format");
399+
bool wants_json = format && strcmp(format, "json") == 0;
400+
cbm_operation_arg_free(format);
401+
if (wants_json)
402+
return json_doc_result(doc, false);
403+
char *tree = projects_tree(doc, projects, include_details, metadata_only);
404+
yyjson_mut_doc_free(doc);
405+
return tree ? cbm_operation_result_take(tree, false)
406+
: json_error("result encoding failed", NULL);
345407
}
346408

347409
static void add_status_coverage(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_store_t *store,

0 commit comments

Comments
 (0)