Skip to content

Commit ae95649

Browse files
authored
Merge pull request #1030 from DeusData/fix/898-sequential-json
fix(pipeline): valid JSON for sequential-path service edges and Route nodes
2 parents 8f0f14b + 93740ce commit ae95649

2 files changed

Lines changed: 120 additions & 13 deletions

File tree

src/pipeline/pass_calls.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,19 @@ static int64_t create_svc_route_node(cbm_pipeline_ctx_t *ctx, const char *url, c
248248
prefix = broker ? broker : "async";
249249
}
250250
snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", prefix, qpath);
251-
const char *rp;
252-
if (svc == CBM_SVC_HTTP) {
253-
rp = method ? method : "{}";
251+
/* Valid-JSON route properties, byte-identical to the parallel path's
252+
* build_service_route. The old code stored the RAW method/broker string
253+
* (literally `bullmq`), which broke json_extract, the edges generated
254+
* columns, and PRAGMA quick_check on every such cache (#898). */
255+
char route_props[CBM_SZ_256];
256+
if (method) {
257+
snprintf(route_props, sizeof(route_props), "{\"method\":\"%s\"}", method);
258+
} else if (broker) {
259+
snprintf(route_props, sizeof(route_props), "{\"broker\":\"%s\"}", broker);
254260
} else {
255-
rp = broker ? broker : "{}";
261+
snprintf(route_props, sizeof(route_props), "{}");
256262
}
257-
return cbm_gbuf_upsert_node(ctx->gbuf, "Route", url, route_qn, "", 0, 0, rp);
263+
return cbm_gbuf_upsert_node(ctx->gbuf, "Route", url, route_qn, "", 0, 0, route_props);
258264
}
259265

260266
/* Insert an edge, splicing the call-site line (,"line":N) in before the closing
@@ -362,15 +368,23 @@ static void emit_http_async_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
362368
char esc_url[CBM_SZ_256];
363369
cbm_json_escape(esc_callee, sizeof(esc_callee), call->callee_name);
364370
cbm_json_escape(esc_url, sizeof(esc_url), url_or_topic);
371+
/* Incremental build mirroring the parallel path's
372+
* emit_http_async_service_edge: the old single format string closed the
373+
* method value's quote but not the broker's, emitting
374+
* "broker":"bullmq} on EVERY brokered ASYNC_CALLS edge — and its fixup
375+
* only handled truncation, which never fires in the normal case (#898). */
365376
char props[CBM_SZ_512];
366-
snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"url_path\":\"%s\"%s%s%s%s%s}", esc_callee,
367-
esc_url, method ? ",\"method\":\"" : "", method ? method : "", method ? "\"" : "",
368-
broker ? ",\"broker\":\"" : "", broker ? broker : "");
369-
if (broker) {
370-
size_t plen = strlen(props);
371-
if (plen > 0 && props[plen - SKIP_ONE] != '}') {
372-
snprintf(props + plen - 1, sizeof(props) - plen + SKIP_ONE, "\"}");
373-
}
377+
int n = snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"url_path\":\"%s\"", esc_callee,
378+
esc_url);
379+
if (method && n > 0 && (size_t)n < sizeof(props)) {
380+
n += snprintf(props + n, sizeof(props) - (size_t)n, ",\"method\":\"%s\"", method);
381+
}
382+
if (broker && n > 0 && (size_t)n < sizeof(props)) {
383+
n += snprintf(props + n, sizeof(props) - (size_t)n, ",\"broker\":\"%s\"", broker);
384+
}
385+
if (n > 0 && (size_t)n < sizeof(props) - 1) {
386+
props[n] = '}';
387+
props[n + 1] = '\0';
374388
}
375389
calls_emit_edge(ctx->gbuf, source->id, route_id, edge_type, props, sizeof(props), call);
376390
}

tests/test_mcp.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Covers: JSON-RPC parsing, MCP protocol, tool dispatch, tool handlers.
55
*/
66
#include "../src/foundation/compat.h"
7+
#include <sqlite3.h>
78
#include "../src/foundation/compat_fs.h" /* cbm_unlink / cbm_rmdir */
89
#include "../src/foundation/constants.h"
910
#include "../src/foundation/log.h"
@@ -4964,6 +4965,97 @@ static int idx773_double_index_check(const char *dir_a, const char *dir_b) {
49644965
}
49654966
#endif /* !_WIN32 */
49664967

4968+
/* #898: the SEQUENTIAL pipeline emitted malformed JSON for brokered
4969+
* ASYNC_CALLS edges ("broker":"bullmq} — missing closing quote) and stored
4970+
* the RAW broker/method string as the synthesized Route node's properties
4971+
* (literally `bullmq` instead of {"broker":"bullmq"}). json_extract over
4972+
* those rows errors, generated-column indexes fail, and PRAGMA quick_check
4973+
* aborts with "malformed JSON" — which since the artifact deep-integrity
4974+
* check also means such caches are refused at import. The parallel path
4975+
* was correct; both pipelines must emit identical, valid JSON. */
4976+
TEST(sequential_service_edge_props_are_valid_json_issue898) {
4977+
char tmp[CBM_SZ_256];
4978+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_seq898_XXXXXX");
4979+
if (!cbm_mkdtemp(tmp)) {
4980+
FAIL("mkdtemp failed");
4981+
}
4982+
char src_path[CBM_SZ_512];
4983+
snprintf(src_path, sizeof(src_path), "%s/queue.py", tmp);
4984+
FILE *f = fopen(src_path, "w");
4985+
ASSERT_NOT_NULL(f);
4986+
/* celery.Celery("tasks") resolves through the import map to a QN the
4987+
* service-pattern table classifies as ASYNC with broker "celery". */
4988+
fputs("import celery\n"
4989+
"\n"
4990+
"def enqueue():\n"
4991+
" celery.Celery(\"tasks\")\n",
4992+
f);
4993+
fclose(f);
4994+
4995+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4996+
ASSERT_NOT_NULL(srv);
4997+
char args[CBM_SZ_512];
4998+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", tmp);
4999+
char *resp = cbm_mcp_handle_tool(srv, "index_repository", args);
5000+
ASSERT_NOT_NULL(resp);
5001+
ASSERT_NOT_NULL(strstr(resp, "indexed"));
5002+
free(resp);
5003+
5004+
cbm_store_t *store = cbm_mcp_server_store(srv);
5005+
ASSERT_NOT_NULL(store);
5006+
struct sqlite3 *db = cbm_store_get_db(store);
5007+
ASSERT_NOT_NULL(db);
5008+
5009+
/* Non-vacuous: the fixture must actually produce a brokered edge. */
5010+
sqlite3_stmt *stmt = NULL;
5011+
ASSERT_EQ(sqlite3_prepare_v2(db, "SELECT count(*) FROM edges WHERE type='ASYNC_CALLS';", -1,
5012+
&stmt, NULL),
5013+
SQLITE_OK);
5014+
ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);
5015+
int async_edges = sqlite3_column_int(stmt, 0);
5016+
sqlite3_finalize(stmt);
5017+
ASSERT_TRUE(async_edges >= 1);
5018+
5019+
/* THE BUG: malformed properties on edges (broker quote) and Route nodes
5020+
* (raw string). Every properties blob must be valid JSON. */
5021+
ASSERT_EQ(sqlite3_prepare_v2(db,
5022+
"SELECT count(*) FROM edges WHERE properties IS NOT NULL "
5023+
"AND properties != '' AND json_valid(properties)=0;",
5024+
-1, &stmt, NULL),
5025+
SQLITE_OK);
5026+
ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);
5027+
int bad_edges = sqlite3_column_int(stmt, 0);
5028+
sqlite3_finalize(stmt);
5029+
ASSERT_EQ(bad_edges, 0);
5030+
5031+
ASSERT_EQ(sqlite3_prepare_v2(db,
5032+
"SELECT count(*) FROM nodes WHERE properties IS NOT NULL "
5033+
"AND properties != '' AND json_valid(properties)=0;",
5034+
-1, &stmt, NULL),
5035+
SQLITE_OK);
5036+
ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);
5037+
int bad_nodes = sqlite3_column_int(stmt, 0);
5038+
sqlite3_finalize(stmt);
5039+
ASSERT_EQ(bad_nodes, 0);
5040+
5041+
/* Pipeline parity: the broker must be extractable exactly like the
5042+
* parallel path emits it. */
5043+
ASSERT_EQ(sqlite3_prepare_v2(db,
5044+
"SELECT count(*) FROM edges WHERE type='ASYNC_CALLS' AND "
5045+
"json_extract(properties,'$.broker')='celery';",
5046+
-1, &stmt, NULL),
5047+
SQLITE_OK);
5048+
ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);
5049+
int brokered = sqlite3_column_int(stmt, 0);
5050+
sqlite3_finalize(stmt);
5051+
ASSERT_TRUE(brokered >= 1);
5052+
5053+
cbm_mcp_server_free(srv);
5054+
unlink(src_path);
5055+
cbm_rmdir(tmp);
5056+
PASS();
5057+
}
5058+
49675059
TEST(index_second_inprocess_run_survives_issue773) {
49685060
#ifdef _WIN32
49695061
SKIP_PLATFORM("fork-isolated crash guard (POSIX-only)");
@@ -5615,6 +5707,7 @@ SUITE(mcp) {
56155707
RUN_TEST(index_repository_cli_name_override_issue823);
56165708
RUN_TEST(index_supervisor_gate_requires_marked_host_issue845);
56175709
RUN_TEST(index_bg_paths_route_through_supervisor_issue832);
5710+
RUN_TEST(sequential_service_edge_props_are_valid_json_issue898);
56185711
RUN_TEST(index_second_inprocess_run_survives_issue773);
56195712
RUN_TEST(index_recovery_parallel_quarantines_crasher);
56205713
RUN_TEST(tool_manage_adr_not_found_rich_error);

0 commit comments

Comments
 (0)