Attach runtime observation to a trace ("path") or flow-detail ("steps") * JSON: every hop whose (caller, callee) pair appears in observed_calls * gains "observed": {count, label, last_seen}. Observation is qn-keyed * (reindex-proof), so hop ids resolve to qualified names first. Absence * adds nothing — an unobserved hop stays merely possible, never "dead". */
| 2609 | * (reindex-proof), so hop ids resolve to qualified names first. Absence |
| 2610 | * adds nothing — an unobserved hop stays merely possible, never "dead". */ |
| 2611 | char *cbm_atlas_attach_observed(cbm_store_t *store, const char *project, char *json) { |
| 2612 | if (!json) |
| 2613 | return json; |
| 2614 | yyjson_doc *doc = yyjson_read(json, strlen(json), 0); |
| 2615 | if (!doc) |
| 2616 | return json; |
| 2617 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 2618 | bool is_trace = yyjson_obj_get(root, "path") != NULL; |
| 2619 | bool is_flow = yyjson_obj_get(root, "steps") != NULL; |
| 2620 | if (!is_trace && !is_flow) { |
| 2621 | yyjson_doc_free(doc); |
| 2622 | return json; |
| 2623 | } |
| 2624 | yyjson_mut_doc *mut = yyjson_doc_mut_copy(doc, cbm_json_writer_allocator()); |
| 2625 | yyjson_doc_free(doc); |
| 2626 | if (!mut) |
| 2627 | return json; |
| 2628 | yyjson_mut_val *mroot = yyjson_mut_doc_get_root(mut); |
| 2629 | yyjson_mut_val *arr = yyjson_mut_obj_get(mroot, is_trace ? "path" : "steps"); |
| 2630 | size_t count = arr ? yyjson_mut_arr_size(arr) : 0; |
| 2631 | struct sqlite3 *db = cbm_store_get_db(store); |
| 2632 | sqlite3_stmt *qn_stmt = NULL; |
| 2633 | if (count >= 2 && db) |
| 2634 | (void)sqlite3_prepare_v2(db, "SELECT qualified_name FROM nodes WHERE id=?1 LIMIT 1", -1, |
| 2635 | &qn_stmt, NULL); |
| 2636 | if (qn_stmt) { |
| 2637 | /* Per-request id→qn memo: flows revisit parents for every child. */ |
| 2638 | enum { OBS_MEMO_MAX = 64 }; |
| 2639 | int64_t memo_id[OBS_MEMO_MAX]; |
| 2640 | char memo_qn[OBS_MEMO_MAX][512]; |
| 2641 | int memo_count = 0; |
| 2642 | for (size_t i = is_trace ? 1 : 0; i < count; i++) { |
| 2643 | yyjson_mut_val *step = yyjson_mut_arr_get(arr, i); |
| 2644 | yyjson_mut_val *parent = NULL; |
| 2645 | if (is_trace) { |
| 2646 | parent = yyjson_mut_arr_get(arr, i - 1); |
| 2647 | } else { |
| 2648 | int64_t pidx = yyjson_mut_get_int(yyjson_mut_obj_get(step, "parent")); |
| 2649 | if (pidx < 0 || (size_t)pidx >= count) |
| 2650 | continue; |
| 2651 | parent = yyjson_mut_arr_get(arr, (size_t)pidx); |
| 2652 | } |
| 2653 | const int64_t ids[2] = {yyjson_mut_get_int(yyjson_mut_obj_get(parent, "id")), |
| 2654 | yyjson_mut_get_int(yyjson_mut_obj_get(step, "id"))}; |
| 2655 | const char *qns[2] = {NULL, NULL}; |
| 2656 | for (int side = 0; side < 2; side++) { |
| 2657 | for (int m = 0; m < memo_count; m++) |
| 2658 | if (memo_id[m] == ids[side]) { |
| 2659 | qns[side] = memo_qn[m]; |
| 2660 | break; |
| 2661 | } |
| 2662 | if (qns[side]) |
| 2663 | continue; |
| 2664 | sqlite3_reset(qn_stmt); |
| 2665 | sqlite3_bind_int64(qn_stmt, 1, ids[side]); |
| 2666 | if (sqlite3_step(qn_stmt) == SQLITE_ROW && memo_count < OBS_MEMO_MAX) { |
| 2667 | const char *qn = (const char *)sqlite3_column_text(qn_stmt, 0); |
| 2668 | if (qn) { |
no test coverage detected