Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(hook,smoke): teach the last two consumers the tree output shape

Martin Vogelcc5f71bdff75
diff --git a/scripts/smoke-test.sh b/scripts/smoke-test.sh
index ec4e4d581..da1f535af 100755
--- a/scripts/smoke-test.sh
+++ b/scripts/smoke-test.sh
@@ -195,8 +195,9 @@ echo "OK: search_graph found $TOTAL result(s) for 'compute'"
if ! TRACE=$(cli trace_path --project "$PROJECT" --function-name compute --direction inbound --depth 1); then
echo "FAIL: trace_path (flag form) exited non-zero"; cat "$CLI_STDERR"; exit 1
fi
-# trace_path default output is TOON: the callers[N]{...} header carries the count
-CALLERS=$(echo "$TRACE" | sed -n 's/^callers\[\([0-9]*\)\].*/\1/p' | head -1)
+# trace_path default output is the tree format: callers_total carries the
+# exact reachable count (test files excluded by default)
+CALLERS=$(echo "$TRACE" | sed -n 's/^callers_total: \([0-9]*\).*/\1/p' | head -1)
CALLERS=${CALLERS:-0}
if [ "$CALLERS" -lt 1 ]; then
echo "FAIL: trace_path found 0 callers for 'compute'"
@@ -278,7 +279,7 @@ cyp_first_cell() {
# argv token, so string-literal args (e.g. replace(f.name,"a","A")) and Cypher
# metacharacters {}|=~<>" need no JSON escaping.
cli query_graph --project "$PROJECT" --query "$1" |
- sed -n '/^rows\[/{n;p;}' | sed 's/^ //' | sed 's/^"//;s/"$//;s/\\"/"/g'
+ sed -n '/^rows: /{n;p;}' | sed 's/^ //' | sed 's/^"//;s/"$//;s/\\"/"/g'
}
# labels(n) → JSON list like ["Function"]
@@ -366,7 +367,7 @@ LEFTV=$(cyp_first_cell 'MATCH (f:Function) RETURN left(f.name, 3) AS l LIMIT 1')
# NOT EXISTS dead-code query (functions with no caller)
CYPHER_NX=$(cli query_graph --project "$PROJECT" --query "MATCH (f:Function) WHERE NOT EXISTS { (f)<-[:CALLS]-() } RETURN f.name")
-NX_OK=$(echo "$CYPHER_NX" | grep -qE '^rows\[[0-9]+\]\{' && echo "True" || echo "False")
+NX_OK=$(echo "$CYPHER_NX" | grep -qE '^rows: [0-9]+' && echo "True" || echo "False")
[ "$NX_OK" = "True" ] && echo "OK: query_graph NOT EXISTS dead-code query executed" || { echo "FAIL: NOT EXISTS query"; echo "$CYPHER_NX" | head -c 300; exit 1; }
# CASE expression in RETURN
@@ -390,7 +391,7 @@ if ! ARCH=$(cli get_architecture --project "$PROJECT" --aspects clusters); then
echo "FAIL: get_architecture (flag form) exited non-zero"; cat "$CLI_STDERR"; exit 1
fi
# get_architecture default output is TOON: clusters[N]{...} header carries the count
-NCLUST=$(echo "$ARCH" | sed -n 's/^clusters\[\([0-9]*\)\].*/\1/p' | head -1)
+NCLUST=$(echo "$ARCH" | sed -n 's/^clusters: \([0-9]*\).*/\1/p' | head -1)
NCLUST=${NCLUST:-0}
if [ "$NCLUST" -lt 1 ]; then
echo "FAIL: get_architecture returned 0 community clusters"; echo "$ARCH" | head -c 400; exit 1
@@ -431,7 +432,7 @@ echo "=== Phase 3h: CLI input-mode guards (flags / stdin / --args-file / --help
assert_json_obj() { python3 -c "import json,sys; d=json.loads(sys.stdin.read()); sys.exit(0 if isinstance(d,dict) else 1)" 2>/dev/null; }
# search_graph emits TOON by default: a results/semantic table header proves
# the tool parsed its typed flags and produced a well-formed response.
-assert_toon_table() { grep -qE '^(results|semantic)\[[0-9]+\]\{'; }
+assert_toon_table() { grep -qE '^(results|semantic): [0-9]+'; }
# B1: INTEGER flag — --limit is schema-typed integer; must parse and answer.
if ! IM_INT=$(cli search_graph --project "$PROJECT" --name-pattern compute --limit 5); then
@@ -458,7 +459,7 @@ fi
if ! IM_ARR=$(cli search_graph --project "$PROJECT" --semantic-query send --semantic-query publish); then
echo "FAIL B3: search_graph repeated --semantic-query exited non-zero"; cat "$CLI_STDERR"; exit 1
fi
-if echo "$IM_ARR" | grep -qE '^semantic\[[0-9]+\]\{'; then
+if echo "$IM_ARR" | grep -qE '^semantic: [0-9]+'; then
echo "OK B3: ARRAY flag (repeated --semantic-query) → semantic TOON table"
else
echo "FAIL B3: repeated --semantic-query did not produce a semantic table"; echo "$IM_ARR" | head -c 300; exit 1
diff --git a/src/cli/cli.h b/src/cli/cli.h
index de1ac04a2..f45325313 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -190,6 +190,8 @@ int cbm_resolve_claude_hook_command_for_testing(const char *script_name, bool wi
char *command, size_t command_size);
bool cbm_optional_hook_supported_for_testing(const char *agent_name, bool windows);
void cbm_hook_sanitize_metadata_for_testing(const char *input, char *output, size_t output_size);
+char *cbm_hook_augment_format_context_for_testing(const char *envelope, const char *token,
+ bool *is_error);
int cbm_upsert_qwen_lifecycle_hooks_for_testing(const char *settings_path, const char *binary_path,
bool windows);
int cbm_upsert_qoder_context_hooks_for_testing(const char *settings_path, const char *binary_path);
diff --git a/src/cli/hook_augment.c b/src/cli/hook_augment.c
index 029c490e8..7b172b1fe 100644
--- a/src/cli/hook_augment.c
+++ b/src/cli/hook_augment.c
@@ -368,9 +368,23 @@ static char *ha_format_context(const char *envelope, const char *token, bool *is
yyjson_doc_free(edoc);
return NULL;
}
+ /* json-tree shape: {total, cols, groups:[{qn_prefix, file,
+ * rows:[[name,label,lines,in,out],...]}]}. The full qualified name is
+ * qn_prefix + "." + row[0]; label is row[1]; file lives on the group. */
yyjson_val *iroot = yyjson_doc_get_root(idoc);
- yyjson_val *results = yyjson_obj_get(iroot, "results");
- size_t nres = (results && yyjson_is_arr(results)) ? yyjson_arr_size(results) : 0;
+ yyjson_val *groups = yyjson_obj_get(iroot, "groups");
+ size_t nres = 0;
+ size_t gidx;
+ size_t gmax;
+ yyjson_val *g;
+ if (groups && yyjson_is_arr(groups)) {
+ yyjson_arr_foreach(groups, gidx, gmax, g) {
+ yyjson_val *rows = yyjson_obj_get(g, "rows");
+ if (rows && yyjson_is_arr(rows)) {
+ nres += yyjson_arr_size(rows);
+ }
+ }
+ }
if (nres == 0) {
yyjson_doc_free(idoc);
yyjson_doc_free(edoc);
@@ -389,26 +403,37 @@ static char *ha_format_context(const char *envelope, const char *token, bool *is
"(structured context; your search results below are "
"unaffected):",
nres, token);
- size_t idx;
- size_t maxn;
- yyjson_val *r;
- yyjson_arr_foreach(results, idx, maxn, r) {
- if (off < 0 || off >= 3900) {
- break;
+ yyjson_arr_foreach(groups, gidx, gmax, g) {
+ const char *prefix = ha_obj_str(g, "qn_prefix");
+ const char *fp = ha_obj_str(g, "file");
+ yyjson_val *rows = yyjson_obj_get(g, "rows");
+ if (!rows || !yyjson_is_arr(rows)) {
+ continue;
+ }
+ size_t ridx;
+ size_t rmax;
+ yyjson_val *row;
+ yyjson_arr_foreach(rows, ridx, rmax, row) {
+ if (off < 0 || off >= 3900) {
+ break;
+ }
+ const char *nm = yyjson_get_str(yyjson_arr_get(row, 0));
+ const char *lb = yyjson_get_str(yyjson_arr_get(row, 1));
+ char disp[HA_METADATA_CAP];
+ if (prefix && prefix[0] && nm && nm[0]) {
+ snprintf(disp, sizeof(disp), "%s.%s", prefix, nm);
+ } else {
+ snprintf(disp, sizeof(disp), "%s", nm ? nm : "");
+ }
+ char safe_disp[HA_METADATA_CAP];
+ char safe_path[HA_METADATA_CAP];
+ char safe_label[HA_METADATA_CAP];
+ ha_sanitize_metadata(disp, safe_disp, sizeof(safe_disp));
+ ha_sanitize_metadata(fp, safe_path, sizeof(safe_path));
+ ha_sanitize_metadata(lb, safe_label, sizeof(safe_label));
+ off += snprintf(text + off, (size_t)(4096 - off), "\n- %s %s%s%s", safe_disp,
+ safe_path, safe_label[0] ? " " : "", safe_label);
}
- const char *qn = ha_obj_str(r, "qualified_name");
- const char *nm = ha_obj_str(r, "name");
- const char *fp = ha_obj_str(r, "file_path");
- const char *lb = ha_obj_str(r, "label");
- const char *disp = (qn && qn[0]) ? qn : (nm ? nm : "");
- char safe_disp[HA_METADATA_CAP];
- char safe_path[HA_METADATA_CAP];
- char safe_label[HA_METADATA_CAP];
- ha_sanitize_metadata(disp, safe_disp, sizeof(safe_disp));
- ha_sanitize_metadata(fp, safe_path, sizeof(safe_path));
- ha_sanitize_metadata(lb, safe_label, sizeof(safe_label));
- off += snprintf(text + off, (size_t)(4096 - off), "\n- %s %s%s%s", safe_disp, safe_path,
- safe_label[0] ? " " : "", safe_label);
}
yyjson_doc_free(idoc);
@@ -1222,6 +1247,15 @@ char *cbm_hook_augment_lifecycle_json_for_dialect(const char *input, const char
return json;
}
+/* Test seam: run the real envelope->additionalContext formatter. Couples the
+ * test suite to the ACTUAL search_graph response shape — a format change that
+ * breaks this parser breaks a test locally, not just the Windows CI guard. */
+char *cbm_hook_augment_format_context_for_testing(const char *envelope, const char *token,
+ bool *is_error) {
+ bool dummy = false;
+ return ha_format_context(envelope, token, is_error ? is_error : &dummy);
+}
+
char *cbm_hook_augment_tool_json_for_testing(const char *input, const char *dialect_name,
const char *context, char *path, size_t path_size) {
if (!input || !context || !path || path_size == 0U || strlen(input) > HA_STDIN_CAP) {
diff --git a/tests/test_cli.c b/tests/test_cli.c
index bb8a94b6f..c6d72757d 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -13,6 +13,7 @@
#include "test_framework.h"
#include "test_helpers.h"
#include <cli/cli.h>
+#include <mcp/mcp.h>
#include <foundation/yaml.h>
#include <store/store.h>
#include <yyjson/yyjson.h>
@@ -6910,6 +6911,48 @@ TEST(cli_codex_migrates_to_single_hook_representation) {
PASS();
}
+/* The PreToolUse augmenter parses search_graph's format:"json" payload to
+ * build additionalContext. This test feeds it the REAL envelope from a live
+ * in-memory server, so any drift between the response shape and the parser
+ * fails HERE — not only in the Windows CI guard (which caught exactly that:
+ * the json-tree reshape left the parser reading a key that no longer exists,
+ * and the hook silently emitted nothing). */
+TEST(cli_hook_augment_context_tracks_search_json_shape) {
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *st = cbm_mcp_server_store(srv);
+ const char *proj = "hookproj";
+ cbm_mcp_server_set_project(srv, proj);
+ cbm_store_upsert_project(st, proj, "/tmp/hookproj");
+ cbm_node_t n = {.project = proj,
+ .label = "Function",
+ .name = "someIndexedSymbol",
+ .qualified_name = "hookproj.mod.someIndexedSymbol",
+ .file_path = "mod.py",
+ .start_line = 1,
+ .end_line = 4};
+ ASSERT_GT(cbm_store_upsert_node(st, &n), 0);
+
+ /* The exact request ha_build_args produces: format:"json". */
+ char *envelope =
+ cbm_mcp_handle_tool(srv, "search_graph",
+ "{\"project\":\"hookproj\",\"name_pattern\":\".*someIndexedSymbol.*\","
+ "\"limit\":5,\"format\":\"json\"}");
+ ASSERT_NOT_NULL(envelope);
+
+ bool is_error = true;
+ char *ctx =
+ cbm_hook_augment_format_context_for_testing(envelope, "someIndexedSymbol", &is_error);
+ ASSERT_FALSE(is_error);
+ ASSERT_NOT_NULL(ctx); /* one hit MUST produce context — empty = broken hook */
+ ASSERT_NOT_NULL(strstr(ctx, "someIndexedSymbol"));
+ ASSERT_NOT_NULL(strstr(ctx, "mod.py"));
+ free(ctx);
+ free(envelope);
+ cbm_mcp_server_free(srv);
+ PASS();
+}
+
TEST(cli_hook_augment_lifecycle_output_contract) {
static const struct {
const char *event;
@@ -9819,6 +9862,7 @@ SUITE(cli) {
RUN_TEST(cli_claude_hook_scripts_shell_quote_binary_path);
RUN_TEST(cli_claude_hook_commands_shell_quote_custom_config_dir);
RUN_TEST(cli_codex_migrates_to_single_hook_representation);
+ RUN_TEST(cli_hook_augment_context_tracks_search_json_shape);
RUN_TEST(cli_hook_augment_lifecycle_output_contract);
RUN_TEST(cli_hook_augment_subagent_tier_router_contract);
RUN_TEST(cli_hook_augment_subagent_no_project_guidance_is_read_only);