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(output): require token-shaped directory savings

Martin Vogel7be95be57002
diff --git a/src/mcp/compact_out.c b/src/mcp/compact_out.c
index 0f22621c2..7a7e415a7 100644
--- a/src/mcp/compact_out.c
+++ b/src/mcp/compact_out.c
@@ -428,6 +428,7 @@ enum {
TREE_PREFIX_MIN_USES = 3,
TREE_PREFIX_MIN_SAVING = 64,
TREE_PREFIX_MIN_PERCENT = 15,
+ TREE_PREFIX_MIN_TOKEN_SHAPE_PERCENT = 1,
TREE_PREFIX_MAX_REFS = 16,
TREE_PREFIX_MAX_SEEDS = 1024,
TREE_PREFIX_MAX_CANDIDATES = 512,
@@ -444,6 +445,45 @@ typedef struct {
bool active;
} tree_prefix_candidate_t;
+/* A dependency-free lower-resolution proxy for modern tokenizer pre-splits.
+ * It deliberately undercounts words (camelCase and Unicode stay whole), while
+ * counting punctuation, whitespace runs, and the common <=3-digit chunks.
+ * Comparing complete renders catches byte wins such as `establishment.` ->
+ * `@0+` that increase real cl100k/o200k tokens despite shorter text. */
+static size_t tree_token_shape_units(const char *text) {
+ const unsigned char *p = (const unsigned char *)text;
+ size_t units = 0;
+ while (p && *p) {
+ if (isdigit(*p)) {
+ size_t digits = 0;
+ while (isdigit(*p)) {
+ digits++;
+ p++;
+ }
+ units += (digits + 2U) / 3U;
+ continue;
+ }
+ if (isalpha(*p) || *p >= 0x80U) {
+ do {
+ p++;
+ } while (*p && (isalpha(*p) || *p >= 0x80U));
+ units++;
+ continue;
+ }
+ if (isspace(*p)) {
+ do {
+ p++;
+ } while (*p && isspace(*p));
+ units++;
+ continue;
+ }
+
+ p++;
+ units++;
+ }
+ return units;
+}
+
static bool tree_column_is_string(const bool *string_cols, int col) {
return !string_cols || string_cols[col];
}
@@ -836,8 +876,15 @@ static void tree_table_rows_impl(cbm_sb_t *sb, const char *key, int nrows, const
size_t plain_len = strlen(plain_text);
size_t compact_len = strlen(compact_text);
size_t saving = plain_len > compact_len ? plain_len - compact_len : 0;
- use_compact =
- saving >= TREE_PREFIX_MIN_SAVING && saving * 100 >= plain_len * TREE_PREFIX_MIN_PERCENT;
+ size_t plain_token_shape = tree_token_shape_units(plain_text);
+ size_t compact_token_shape = tree_token_shape_units(compact_text);
+ size_t token_shape_saving = plain_token_shape > compact_token_shape
+ ? plain_token_shape - compact_token_shape
+ : 0;
+ use_compact = saving >= TREE_PREFIX_MIN_SAVING &&
+ saving * 100 >= plain_len * TREE_PREFIX_MIN_PERCENT &&
+ token_shape_saving * 100 >=
+ plain_token_shape * TREE_PREFIX_MIN_TOKEN_SHAPE_PERCENT;
}
if (use_compact) {
cbm_sb_append(sb, compact_text);
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index c93cb7fb6..e946b96b9 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -978,8 +978,38 @@ TEST(tree_table_uses_prefix_dictionary_only_when_it_materially_wins) {
roundtrip_string_cols, roundtrip_prefix_cols);
out = cbm_sb_finish(&sb);
ASSERT_NOT_NULL(out);
- ASSERT_NOT_NULL(strstr(out, "\"123456789012.\""));
- ASSERT_NOT_NULL(strstr(out, " @0+member_00\n"));
+ /* The lexical proxy may conservatively decline a numeric-prefix directory
+ * even though cl100k/o200k save four tokens here. Either representation is
+ * complete; if compacted, the numeric declaration must stay quoted. */
+ if (strstr(out, "numeric_prefix_refs:")) {
+ ASSERT_NOT_NULL(strstr(out, "\"123456789012.\""));
+ ASSERT_NOT_NULL(strstr(out, " @0+member_00\n"));
+ } else {
+ ASSERT_NOT_NULL(strstr(out, "123456789012.member_00"));
+ }
+ free(out);
+
+ /* A long prefix is not necessarily token-expensive. Both cl100k and
+ * o200k encode `establishment.handler_0000` one token more cheaply than
+ * `@0+handler_0000`; a byte-only winner therefore regresses the actual
+ * context budget at scale. The model-neutral token-shape gate must keep
+ * this table direct even though a directory saves more than 15% of bytes. */
+ enum { TOKEN_CHEAP_ROWS = 200 };
+ char token_cheap_values[TOKEN_CHEAP_ROWS][64];
+ const char *token_cheap_cells[TOKEN_CHEAP_ROWS];
+ for (int i = 0; i < TOKEN_CHEAP_ROWS; i++) {
+ snprintf(token_cheap_values[i], sizeof(token_cheap_values[i]),
+ "establishment.handler_%04d", i);
+ token_cheap_cells[i] = token_cheap_values[i];
+ }
+ cbm_sb_init(&sb);
+ cbm_tree_table_rows_profiled(&sb, "token_cheap", TOKEN_CHEAP_ROWS, roundtrip_cols, 1,
+ token_cheap_cells, roundtrip_string_cols,
+ roundtrip_prefix_cols);
+ out = cbm_sb_finish(&sb);
+ ASSERT_NOT_NULL(out);
+ ASSERT_NULL(strstr(out, "token_cheap_refs:"));
+ ASSERT_NOT_NULL(strstr(out, "establishment.handler_0000"));
free(out);
PASS();
}