Skip to content

Commit fcc51bc

Browse files
ngallodevclaude
andauthored
fix(pipeline): decode persisted LSP arrays linearly (#4)
## Summary - Decode persisted LSP definition and string arrays with `yyjson_arr_iter` to avoid repeated scans of non-flat arrays. - Add a regression check for definition order, null values, and empty parameter arrays. - Adapt upstream codebase-memory-mcp commit `d7eba5a7` for the CLI fork. ## Review and validation - Source review found no actionable correctness issues. - `CBM_NO_CCACHE=1 scripts/build-dev.sh` passed. - `ASAN_OPTIONS=detect_leaks=0 CBM_NO_CCACHE=1 scripts/test.sh --suites pipeline` passed: 274 tests. This validation ran before the request to stop running tests; no tests were run after that instruction. - `git diff origin/main...HEAD --check` passed. - CodeRabbit review could not start because browser authentication is unavailable in this environment. This PR does not claim Agent-Workflow evaluation, acceptance, or merge approval. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e35f1e0 commit fcc51bc

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎src/pipeline/lsp_surface.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,13 @@ static const char **arena_str_array(CBMArena *arena, yyjson_val *arr, bool null_
198198
if (!items) {
199199
return NULL;
200200
}
201-
for (int i = 0; i < count; i++) {
202-
const char *s = yyjson_get_str(yyjson_arr_get(arr, (size_t)i));
203-
items[i] = s ? cbm_arena_strdup(arena, s) : "?";
201+
/* Indexed access rescans non-flat arrays; iterate once to keep this O(n). */
202+
yyjson_arr_iter iter = yyjson_arr_iter_with(arr);
203+
int i = 0;
204+
yyjson_val *item;
205+
while ((item = yyjson_arr_iter_next(&iter))) {
206+
const char *s = yyjson_get_str(item);
207+
items[i++] = s ? cbm_arena_strdup(arena, s) : "?";
204208
}
205209
if (null_terminated) {
206210
items[count] = NULL;
@@ -236,9 +240,12 @@ int cbm_lsp_surface_defs_from_json(CBMArena *arena, const char *defs_json, CBMLS
236240
return -1;
237241
}
238242
memset(defs, 0, (size_t)count * sizeof(CBMLSPDef));
239-
for (int i = 0; i < count; i++) {
240-
yyjson_val *o = yyjson_arr_get(lsp, (size_t)i);
241-
CBMLSPDef *d = &defs[i];
243+
/* `lsp` contains objects, so yyjson_arr_get(i) makes this decode O(n^2). */
244+
yyjson_arr_iter iter = yyjson_arr_iter_with(lsp);
245+
int i = 0;
246+
yyjson_val *o;
247+
while ((o = yyjson_arr_iter_next(&iter))) {
248+
CBMLSPDef *d = &defs[i++];
242249
d->qualified_name = arena_str_or_null(arena, yyjson_obj_get(o, "qn"));
243250
d->short_name = arena_str_or_null(arena, yyjson_obj_get(o, "sn"));
244251
d->label = arena_str_or_null(arena, yyjson_obj_get(o, "lb"));

‎tests/test_pipeline.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "test_framework.h"
1010
#include "test_helpers.h"
1111
#include "foundation/mem.h" // cbm_mem_init/budget (back-pressure futile-nap test)
12+
#include "foundation/arena.h"
1213
#include "pipeline/pipeline.h"
14+
#include "pipeline/lsp_surface.h"
1315
#include "pipeline/pipeline_internal.h"
1416
#include "pipeline/artifact.h"
1517
#include "store/store.h"
@@ -13008,6 +13010,31 @@ TEST(pipeline_lsp_surface_persisted_and_body_edit_invariant) {
1300813010
PASS();
1300913011
}
1301013012

13013+
TEST(pipeline_lsp_surface_decode_preserves_array_order_and_nulls) {
13014+
const char *json =
13015+
"{\"v\":1,\"lsp\":["
13016+
"{\"qn\":\"first\",\"sn\":\"one\",\"lb\":\"Function\",\"rt\":null,\"spt\":[\"A\",null,\"C\"]},"
13017+
"{\"qn\":\"second\",\"sn\":\"two\",\"lb\":\"Method\",\"rt\":\"Receiver\",\"spt\":[\"D\"]},"
13018+
"{\"qn\":\"third\",\"sn\":\"three\",\"lb\":\"Class\",\"rt\":null,\"spt\":[]}]}";
13019+
CBMArena arena;
13020+
cbm_arena_init(&arena);
13021+
CBMLSPDef *defs = NULL;
13022+
ASSERT_EQ(cbm_lsp_surface_defs_from_json(&arena, json, &defs), 3);
13023+
ASSERT_NOT_NULL(defs);
13024+
ASSERT_STR_EQ(defs[0].qualified_name, "first");
13025+
ASSERT_STR_EQ(defs[1].qualified_name, "second");
13026+
ASSERT_STR_EQ(defs[2].qualified_name, "third");
13027+
ASSERT_TRUE(defs[0].receiver_type == NULL);
13028+
ASSERT_STR_EQ(defs[1].receiver_type, "Receiver");
13029+
ASSERT_EQ(defs[0].signature_param_count, 3);
13030+
ASSERT_STR_EQ(defs[0].signature_param_types[0], "A");
13031+
ASSERT_STR_EQ(defs[0].signature_param_types[1], "?");
13032+
ASSERT_STR_EQ(defs[0].signature_param_types[2], "C");
13033+
ASSERT_EQ(defs[2].signature_param_count, 0);
13034+
cbm_arena_destroy(&arena);
13035+
PASS();
13036+
}
13037+
1301113038
TEST(pipeline_ensemble_routing_edges) {
1301213039
char tmpdir[256];
1301313040
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_ens_XXXXXX");
@@ -13662,6 +13689,7 @@ TEST(pipeline_objectscript_export_range_join_keeps_one_trailing_marker) {
1366213689
#endif
1366313690

1366413691
SUITE(pipeline) {
13692+
RUN_TEST(pipeline_lsp_surface_decode_preserves_array_order_and_nulls);
1366513693
RUN_TEST(pipeline_lsp_surface_persisted_and_body_edit_invariant);
1366613694
/* Index lock */
1366713695
RUN_TEST(pipeline_lock_try_acquire);

0 commit comments

Comments
 (0)