Skip to content

Commit 5350b5b

Browse files
committed
fix(cli): render the Copilot manifest through the memory core
The PostToolUse change rendered the manifest twice per install and uninstall (current and previous generation) and freed both with free(), which grew the raw allocator count in src/cli/cli.c past its baseline (lint-memory-core). The manifest is now written with cbm_json_writer_allocator() and released with cbm_free(CBM_MEM_CLASS_OTHER, ...), including the frees that predate the change, so cli.c drops from 170 to 166 raw sites and its baseline is lowered to match. The rendered bytes are unchanged, so byte-exact ownership of manifests written by earlier installs still holds. Refs DeusData#509 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent d093794 commit 5350b5b

4 files changed

Lines changed: 24 additions & 18 deletions

File tree

‎scripts/memory-core-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal/cbm/sqlite_writer.c 74
1616
src/cli/activation_transaction.c 80
1717
src/cli/agent_clients.c 73
1818
src/cli/agent_profiles.c 7
19-
src/cli/cli.c 170
19+
src/cli/cli.c 166
2020
src/cli/client_adapter.c 3
2121
src/cli/config_edit_path.c 2
2222
src/cli/config_json_like.c 142

‎src/cli/cli.c‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "foundation/compat.h"
2323
#include "foundation/platform.h"
2424
#include "foundation/constants.h"
25+
#include "foundation/json_writer.h"
2526
#include "foundation/log.h"
2627
#include "foundation/mem_core.h"
2728
#include "foundation/sha256.h"
@@ -4242,7 +4243,8 @@ static bool cbm_copilot_add_hook_event(yyjson_mut_doc *doc, yyjson_mut_val *hook
42424243

42434244
/* include_posttool renders the current manifest; without it the function
42444245
* renders the previous lifecycle-only manifest, which installs up to v0.12 wrote
4245-
* and which upgrades and uninstalls must still recognize as owned. */
4246+
* and which upgrades and uninstalls must still recognize as owned. The result is
4247+
* released with cbm_free(CBM_MEM_CLASS_OTHER, ...). */
42464248
static char *cbm_build_copilot_hook_manifest(const char *binary_path, bool include_posttool) {
42474249
char session_bash[CLI_BUF_8K];
42484250
char session_powershell[CLI_BUF_8K];
@@ -4284,7 +4286,9 @@ static char *cbm_build_copilot_hook_manifest(const char *binary_path, bool inclu
42844286
cbm_copilot_add_hook_event(doc, hooks, "PostToolUse", COPILOT_SEARCH_MATCHER,
42854287
post_bash, post_powershell)) &&
42864288
yyjson_mut_obj_add_val(doc, root, "hooks", hooks);
4287-
char *manifest = ok ? yyjson_mut_write(doc, YYJSON_WRITE_PRETTY, NULL) : NULL;
4289+
char *manifest = ok ? yyjson_mut_write_opts(doc, YYJSON_WRITE_PRETTY,
4290+
cbm_json_writer_allocator(), NULL, NULL)
4291+
: NULL;
42884292
yyjson_mut_doc_free(doc);
42894293
return manifest;
42904294
}
@@ -4303,29 +4307,29 @@ static int cbm_upsert_copilot_hooks(const char *binary_path, const char *manifes
43034307
char *manifest = cbm_build_copilot_hook_manifest(binary_path, true);
43044308
char *previous = cbm_build_copilot_hook_manifest(binary_path, false);
43054309
if (!manifest || !previous || !manifest_path) {
4306-
free(manifest);
4307-
free(previous);
4310+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4311+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
43084312
return CLI_ERR;
43094313
}
43104314
const char *const released[] = {previous};
43114315
int rc = cbm_text_migrate_owned_document(manifest_path, manifest, released, 1U);
4312-
free(manifest);
4313-
free(previous);
4316+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4317+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
43144318
return rc == 0 ? CLI_OK : CLI_ERR;
43154319
}
43164320

43174321
static int cbm_remove_copilot_hooks(const char *manifest_path, const char *binary_path) {
43184322
char *manifest = cbm_build_copilot_hook_manifest(binary_path, true);
43194323
char *previous = cbm_build_copilot_hook_manifest(binary_path, false);
43204324
if (!manifest || !previous || !manifest_path) {
4321-
free(manifest);
4322-
free(previous);
4325+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4326+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
43234327
return CLI_ERR;
43244328
}
43254329
const char *const released[] = {previous};
43264330
int rc = cbm_text_remove_owned_document_any(manifest_path, manifest, released, 1U);
4327-
free(manifest);
4328-
free(previous);
4331+
cbm_free(CBM_MEM_CLASS_OTHER, manifest);
4332+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
43294333
return rc >= 0 ? CLI_OK : CLI_ERR;
43304334
}
43314335

‎src/cli/cli.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,
283283
bool case_insensitive);
284284
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event);
285285
bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz);
286-
/* include_posttool false renders the previous lifecycle-only manifest. */
286+
/* include_posttool false renders the previous lifecycle-only manifest. Release
287+
* the result with cbm_free(CBM_MEM_CLASS_OTHER, ...). */
287288
char *cbm_build_copilot_hook_manifest_for_testing(const char *binary_path, bool include_posttool);
288289
bool cbm_mcp_command_path_probe_safe_for_testing(const char *command, bool windows);
289290
void cbm_set_mcp_command_path_probe_counter_for_testing(int *counter);

‎tests/test_cli.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <daemon/version_cohort.h>
2424
#include <foundation/constants.h>
2525
#include <foundation/log.h>
26+
#include <foundation/mem_core.h>
2627
#include <foundation/platform.h>
2728
#include <foundation/private_file_lock.h>
2829
#include <foundation/sha256.h>
@@ -12907,8 +12908,8 @@ TEST(cli_copilot_manifest_adds_posttooluse_search_hook) {
1290712908
bool previous_shape = strstr(previous, "sessionStart") && strstr(previous, "subagentStart") &&
1290812909
!strstr(previous, "PostToolUse");
1290912910
yyjson_doc_free(doc);
12910-
free(current);
12911-
free(previous);
12911+
cbm_free(CBM_MEM_CLASS_OTHER, current);
12912+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
1291212913
if (!shaped || !previous_shape)
1291312914
FAIL("Copilot manifest must add one PostToolUse search hook beside the lifecycle pair");
1291412915
PASS();
@@ -12939,8 +12940,8 @@ TEST(cli_copilot_install_upgrades_previous_manifest) {
1293912940
char *previous = cbm_build_copilot_hook_manifest_for_testing(binary, false);
1294012941
char *current = cbm_build_copilot_hook_manifest_for_testing(binary, true);
1294112942
if (!previous || !current) {
12942-
free(previous);
12943-
free(current);
12943+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
12944+
cbm_free(CBM_MEM_CLASS_OTHER, current);
1294412945
test_rmdir_r(tmpdir);
1294512946
FAIL("Copilot manifest rendering failed");
1294612947
}
@@ -12967,8 +12968,8 @@ TEST(cli_copilot_install_upgrades_previous_manifest) {
1296712968

1296812969
free(upgraded);
1296912970
free(again);
12970-
free(previous);
12971-
free(current);
12971+
cbm_free(CBM_MEM_CLASS_OTHER, previous);
12972+
cbm_free(CBM_MEM_CLASS_OTHER, current);
1297212973
restore_test_env("HOME", saved_home);
1297312974
restore_test_env("PATH", saved_path);
1297412975
restore_test_env("COPILOT_HOME", saved_copilot);

0 commit comments

Comments
 (0)