Skip to content

Commit 6cc63d2

Browse files
committed
Merge origin/main into fix/elixir-extraction-defects
Signed-off-by: Martin Vogel <[email protected]>
2 parents c4bb4b5 + dca2a44 commit 6cc63d2

35 files changed

Lines changed: 3739 additions & 510 deletions

‎README.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ overwrite user-modified agents.
694694

695695
| Agent | Activation | MCP config | Durable context / augmentation |
696696
|-------|------------|------------|--------------------------------|
697-
| Claude Code | Detected | `~/.claude.json` | Skill + three exact-tool graph agents; `SessionStart`, `SubagentStart`, non-blocking `PreToolUse` for `Grep`/`Glob`/`Bash`, and post-`Read` coverage |
697+
| Claude Code | Detected | `~/.claude.json` | Skill + three exact-tool graph agents; `SessionStart`, `SubagentStart`, non-blocking `PreToolUse` for `Grep`/`Glob`/`Bash`, post-`Read` coverage, and a drift note after repeated searches without a graph call |
698698
| Codex CLI | Detected | `$CODEX_HOME/config.toml` | Managed `AGENTS.md` activation pointer, skill, three read-only agents; `SessionStart` + `SubagentStart`, `PreToolUse` graph lookup for `Bash` searches |
699699
| Gemini CLI | Detected | `.gemini/settings.json` | `GEMINI.md`, three explicit read/graph-tool subagents; `BeforeTool`, `AfterTool` `read_file` coverage, and `SessionStart` |
700700
| Zed | Detected | platform `settings.json` (JSONC) | `AGENTS.md` + shared skill |
@@ -755,7 +755,10 @@ the requested tool call. Codex CLI's `PreToolUse` on `Bash` gives its shell
755755
searches the same context. GitHub Copilot CLI and VS Code's Copilot runtime get
756756
it after the search: their `preToolUse` can only decide permissions, so a
757757
`PostToolUse` entry for `Bash`, `Grep` and `Glob` appends the graph symbols to
758-
the tool result.
758+
the tool result. In Claude Code, once a session has run three searches with no
759+
codebase-memory-mcp tool call, the next augmentation adds one line saying so;
760+
the next note comes after six searches, then twelve. Claude's `PostToolUse`
761+
entry also matches the graph tools, and a graph tool call resets the count.
759762

760763
Claude Code, Codex CLI, Qwen Code, GitHub Copilot CLI, and VS Code's Copilot
761764
runtime receive paired session/subagent context where the vendor exposes a

‎internal/cbm/extract_unified.c‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,27 +1668,39 @@ typedef struct {
16681668
TSNode node;
16691669
const char *prefix;
16701670
} yaml_walk_frame_t;
1671-
#define YAML_WALK_STACK_CAP CBM_SZ_256
1672-
1673-
/* Push block_mapping children of a block_node/block_mapping value onto the walk stack. */
1674-
static void push_yaml_block_children(TSNode val, const char *path, yaml_walk_frame_t *stack,
1675-
int *top) {
1671+
/* On-stack first chunk of the key-path walk; wider mappings spill to the heap
1672+
* through cbm_walk_stack_reserve instead of dropping frames (#2388). */
1673+
#define YAML_WALK_STACK_INLINE CBM_SZ_256
1674+
1675+
/* Push block_mapping children of a block_node/block_mapping value onto the walk
1676+
* stack, growing it as needed. False when the stack could not grow (logged by
1677+
* cbm_walk_stack_reserve as extract.walk_stack_alloc_failed). */
1678+
static bool push_yaml_block_children(TSNode val, const char *path, yaml_walk_frame_t **stack,
1679+
int *cap, int *top, const yaml_walk_frame_t *inline_buf) {
16761680
uint32_t vnc = ts_node_named_child_count(val);
1677-
for (int vi = (int)vnc - SKIP_ONE; vi >= 0 && *top < YAML_WALK_STACK_CAP; vi--) {
1681+
if (!cbm_walk_stack_reserve((void **)stack, cap, *top + (int)vnc, sizeof(yaml_walk_frame_t),
1682+
inline_buf, "yaml_key_paths")) {
1683+
return false;
1684+
}
1685+
for (int vi = (int)vnc - SKIP_ONE; vi >= 0; vi--) {
16781686
TSNode vc = ts_node_named_child(val, (uint32_t)vi);
16791687
const char *vctype = ts_node_type(vc);
16801688
if (strcmp(vctype, "block_mapping") == 0 || strcmp(vctype, "block_mapping_pair") == 0) {
1681-
stack[(*top)++] = (yaml_walk_frame_t){vc, path};
1689+
(*stack)[(*top)++] = (yaml_walk_frame_t){vc, path};
16821690
}
16831691
}
1692+
return true;
16841693
}
16851694

16861695
static void walk_yaml_mapping(CBMExtractCtx *ctx, TSNode root, const char *root_prefix) {
1687-
yaml_walk_frame_t stack[YAML_WALK_STACK_CAP];
1696+
yaml_walk_frame_t inline_stack[YAML_WALK_STACK_INLINE];
1697+
yaml_walk_frame_t *stack = inline_stack;
1698+
int cap = YAML_WALK_STACK_INLINE;
16881699
int top = 0;
16891700
stack[top++] = (yaml_walk_frame_t){root, root_prefix};
1701+
bool grown = true;
16901702

1691-
while (top > 0) {
1703+
while (grown && top > 0) {
16921704
yaml_walk_frame_t frame = stack[--top];
16931705
TSNode node = frame.node;
16941706
const char *prefix = frame.prefix;
@@ -1714,12 +1726,16 @@ static void walk_yaml_mapping(CBMExtractCtx *ctx, TSNode root, const char *root_
17141726
}
17151727
const char *vk = ts_node_type(val);
17161728
if (strcmp(vk, "block_node") == 0 || strcmp(vk, "block_mapping") == 0) {
1717-
push_yaml_block_children(val, path, stack, &top);
1729+
grown = push_yaml_block_children(val, path, &stack, &cap, &top, inline_stack);
1730+
if (!grown) {
1731+
break;
1732+
}
17181733
continue;
17191734
}
17201735
emit_yaml_leaf_value(ctx, val, path);
17211736
}
17221737
}
1738+
cbm_walk_stack_release(stack, inline_stack);
17231739
}
17241740

17251741
/* ── Infrastructure binding extraction ─────────────────────────────
@@ -1878,9 +1894,13 @@ static void scan_mapping_for_bindings(CBMExtractCtx *ctx, TSNode mapping) {
18781894
emit_infra_bindings(ctx, sources, source_keys, n_sources, targets, n_targets);
18791895
}
18801896

1881-
#define INFRA_SCAN_STACK_CAP CBM_SZ_512
1897+
/* On-stack first chunk of the binding scan; wider subtrees spill to the heap
1898+
* through cbm_walk_stack_reserve instead of dropping nodes (#2388). */
1899+
#define INFRA_SCAN_STACK_INLINE CBM_SZ_512
18821900
static void scan_yaml_for_infra_bindings(CBMExtractCtx *ctx, TSNode root) {
1883-
TSNode stack[INFRA_SCAN_STACK_CAP];
1901+
TSNode inline_stack[INFRA_SCAN_STACK_INLINE];
1902+
TSNode *stack = inline_stack;
1903+
int cap = INFRA_SCAN_STACK_INLINE;
18841904
int top = 0;
18851905
stack[top++] = root;
18861906
while (top > 0) {
@@ -1889,10 +1909,15 @@ static void scan_yaml_for_infra_bindings(CBMExtractCtx *ctx, TSNode root) {
18891909
scan_mapping_for_bindings(ctx, node);
18901910
}
18911911
uint32_t nc = ts_node_named_child_count(node);
1892-
for (int i = (int)nc - SKIP_ONE; i >= 0 && top < INFRA_SCAN_STACK_CAP; i--) {
1912+
if (!cbm_walk_stack_reserve((void **)&stack, &cap, top + (int)nc, sizeof(TSNode),
1913+
inline_stack, "yaml_infra_bindings")) {
1914+
break;
1915+
}
1916+
for (int i = (int)nc - SKIP_ONE; i >= 0; i--) {
18931917
stack[top++] = ts_node_named_child(node, (uint32_t)i);
18941918
}
18951919
}
1920+
cbm_walk_stack_release(stack, inline_stack);
18961921
}
18971922

18981923
/* ── HCL infrastructure binding extraction ───────────────────────────

‎scripts/memory-core-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal/cbm/lsp/perl_lsp.c 21
1313
internal/cbm/macro_table.c 1
1414
internal/cbm/service_patterns.c 2
1515
internal/cbm/sqlite_writer.c 74
16-
src/cli/activation_transaction.c 80
16+
src/cli/activation_transaction.c 78
1717
src/cli/agent_clients.c 73
1818
src/cli/agent_profiles.c 7
1919
src/cli/cli.c 166

‎scripts/smoke-test.sh‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,22 +1697,24 @@ echo "OK 8c-i: Claude exact-tool graph subagent"
16971697

16981698
# 8d: Claude Code hooks keep search augmentation and read-coverage reporting
16991699
# separate: PreToolUse matches exactly Grep|Glob|Bash, while PostToolUse matches
1700-
# exactly Read. Neither hook may grow a Search or catch-all matcher.
1700+
# exactly Read plus the graph tools, whose calls reset the drift note. Neither
1701+
# hook may grow a Search or catch-all matcher.
17011702
if ! cat "$FAKE_HOME/.claude/settings.json" 2>/dev/null | python3 -c "
17021703
import json, sys
17031704
d = json.load(sys.stdin)
17041705
all_hooks = d.get('hooks', {})
17051706
pre = all_hooks.get('PreToolUse', [])
17061707
post = all_hooks.get('PostToolUse', [])
17071708
ok = (any(h.get('matcher') == 'Grep|Glob|Bash' for h in pre) and
1708-
any(h.get('matcher') == 'Read' for h in post))
1709-
bad = any('Search' in str(h.get('matcher', '')) for h in pre + post)
1709+
any(h.get('matcher') == 'Read|mcp__.*codebase-memory-mcp__.*' for h in post))
1710+
bad = any('Search' in str(h.get('matcher', '')) or h.get('matcher', '') in ('', '*')
1711+
for h in pre + post)
17101712
sys.exit(0 if (ok and not bad) else 1)
17111713
" 2>/dev/null; then
17121714
echo "FAIL 8d: Claude search/read hook matchers are not exact"
17131715
exit 1
17141716
fi
1715-
echo "OK 8d: Claude Code PreToolUse Grep|Glob|Bash + PostToolUse Read"
1717+
echo "OK 8d: Claude Code PreToolUse Grep|Glob|Bash + PostToolUse Read and graph tools"
17161718

17171719
# 8e: Claude Code shim script — must be non-blocking augmenter, not a gate.
17181720
# #929: Windows installs a .cmd script (extensionless bash shims triggered the

‎src/cli/activation_transaction.c‎

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,69 @@ static bool activation_posix_acl_empty(int descriptor) {
899899
return cbm_macos_extended_acl_fd_is_empty(descriptor);
900900
}
901901

902+
#ifdef __linux__
903+
/* Root of the per-user home tree and the account trusted to own it. Root and
904+
* "/home" in production; the test seam substitutes a fixture tree and the test
905+
* account, because the real layout needs root-owned entries to reproduce. */
906+
static const char *g_activation_home_root = "/home";
907+
static uid_t g_activation_home_owner = 0;
908+
909+
#ifdef CBM_ENABLE_TEST_SEAMS
910+
void cbm_activation_transaction_set_home_root_for_testing(const char *home_root,
911+
unsigned long owner_uid) {
912+
g_activation_home_root = home_root ? home_root : "/home";
913+
g_activation_home_owner = home_root ? (uid_t)owner_uid : 0;
914+
}
915+
#endif
916+
#endif
917+
918+
#if defined(__APPLE__) || defined(__linux__)
919+
/* Map a trusted symlink `link` (already lstat'd) onto its resolved target and
920+
* append `rest`. The link and the target must both belong to `owner`; with
921+
* accept_own_target the target may instead belong to the current account.
922+
* Returns NULL when either check fails or the mapped path does not fit. */
923+
static char *activation_alias_map(const char *link, const struct stat *link_status, uid_t owner,
924+
bool accept_own_target, const char *rest) {
925+
char resolved[4096];
926+
struct stat resolved_status;
927+
if (link_status->st_uid != owner || !realpath(link, resolved) ||
928+
lstat(resolved, &resolved_status) != 0 || !S_ISDIR(resolved_status.st_mode)) {
929+
return NULL;
930+
}
931+
if (resolved_status.st_uid != owner &&
932+
!(accept_own_target && resolved_status.st_uid == geteuid())) {
933+
return NULL;
934+
}
935+
char mapped[4096];
936+
int written = snprintf(mapped, sizeof(mapped), "%s%s", resolved, rest);
937+
if (written <= 0 || (size_t)written >= sizeof(mapped)) {
938+
return NULL;
939+
}
940+
return activation_string_copy(mapped);
941+
}
942+
943+
#ifdef CBM_ENABLE_TEST_SEAMS
944+
char *cbm_activation_transaction_map_alias_for_testing(const char *link,
945+
unsigned long link_owner_uid,
946+
unsigned long trusted_owner_uid,
947+
bool accept_own_target, const char *rest) {
948+
/* Only link ownership is synthetic; the shared production helper resolves
949+
* and checks the real target so distinct-owner cases need no chown/root. */
950+
const struct stat link_status = {.st_uid = (uid_t)link_owner_uid};
951+
return activation_alias_map(link, &link_status, (uid_t)trusted_owner_uid, accept_own_target,
952+
rest);
953+
}
954+
#endif
955+
#endif
956+
902957
static char *activation_posix_walk_path(const char *directory) {
903958
#if defined(__APPLE__) || defined(__linux__)
904959
/* macOS and immutable Linux layouts can expose writable trees through
905960
* root-owned aliases (for example /tmp and /home on Atomic systems).
906961
* Resolve only these trusted system aliases; arbitrary user symlinks must
907962
* still fail the O_NOFOLLOW walk below. */
908963
#ifdef __linux__
909-
static const char *const aliases[] = {"/tmp", "/var", "/home"};
964+
const char *const aliases[] = {"/tmp", "/var", g_activation_home_root};
910965
#else
911966
static const char *const aliases[] = {"/tmp", "/var"};
912967
#endif
@@ -917,33 +972,48 @@ static char *activation_posix_walk_path(const char *directory) {
917972
(directory[alias_length] != '\0' && directory[alias_length] != '/')) {
918973
continue;
919974
}
975+
uid_t owner = 0;
976+
#ifdef __linux__
977+
bool is_home = alias == g_activation_home_root;
978+
if (is_home) {
979+
owner = g_activation_home_owner;
980+
}
981+
#endif
920982
struct stat alias_status;
921-
char resolved[4096];
922983
if (lstat(alias, &alias_status) != 0) {
923984
continue;
924985
}
925-
if (!S_ISLNK(alias_status.st_mode)) {
926-
continue;
927-
}
928-
if (alias_status.st_uid != 0 || !realpath(alias, resolved)) {
929-
return NULL;
986+
if (S_ISLNK(alias_status.st_mode)) {
987+
return activation_alias_map(alias, &alias_status, owner, false,
988+
directory + alias_length);
930989
}
931-
struct stat resolved_status;
932-
if (lstat(resolved, &resolved_status) != 0 || !S_ISDIR(resolved_status.st_mode) ||
933-
resolved_status.st_uid != 0) {
934-
return NULL;
990+
#ifdef __linux__
991+
/* Managed Linux hosts often keep /home a real directory and point the
992+
* per-user entry elsewhere (/home/alice -> /local/home/alice, #2306).
993+
* Trust that entry only inside a root-owned /home nobody else can
994+
* write, and only when root owns the entry itself. Unlike the whole-
995+
* alias case above, the target may belong to the current account:
996+
* that is the normal shape of a relocated home, and an account can
997+
* already write its own home, so it gains nothing it did not have. */
998+
const char *name = directory + alias_length;
999+
size_t name_length = *name == '/' ? strcspn(name + 1, "/") : 0;
1000+
if (!is_home || !S_ISDIR(alias_status.st_mode) || alias_status.st_uid != owner ||
1001+
(alias_status.st_mode & 0022) != 0 || name_length == 0) {
1002+
continue;
9351003
}
936-
size_t needed = strlen(resolved) + strlen(directory + alias_length) + 1U;
937-
char *mapped = malloc(needed);
938-
if (!mapped) {
1004+
char entry[4096];
1005+
size_t entry_length = alias_length + 1U + name_length;
1006+
if (entry_length >= sizeof(entry)) {
9391007
return NULL;
9401008
}
941-
int written = snprintf(mapped, needed, "%s%s", resolved, directory + alias_length);
942-
if (written <= 0 || (size_t)written >= needed) {
943-
free(mapped);
944-
return NULL;
1009+
memcpy(entry, directory, entry_length);
1010+
entry[entry_length] = '\0';
1011+
struct stat entry_status;
1012+
if (lstat(entry, &entry_status) == 0 && S_ISLNK(entry_status.st_mode)) {
1013+
return activation_alias_map(entry, &entry_status, owner, true,
1014+
directory + entry_length);
9451015
}
946-
return mapped;
1016+
#endif
9471017
}
9481018
#endif
9491019
return activation_string_copy(directory);

‎src/cli/activation_transaction.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ const char *cbm_activation_transaction_refusal_note(void);
104104
#ifdef CBM_ENABLE_TEST_SEAMS
105105
void cbm_activation_transaction_note_refusal_for_testing(const char *predicate,
106106
unsigned long os_error);
107+
/* Linux only: stand home_root in for /home and owner_uid in for root when
108+
* deciding whether /home or a /home/<name> entry is a trusted alias. NULL
109+
* restores the production values. Inert elsewhere. */
110+
void cbm_activation_transaction_set_home_root_for_testing(const char *home_root,
111+
unsigned long owner_uid);
112+
113+
#if defined(__APPLE__) || defined(__linux__)
114+
/* Exercise the shared alias checks with synthetic link ownership and a real
115+
* target. The caller frees the mapped path; NULL means the alias was refused. */
116+
char *cbm_activation_transaction_map_alias_for_testing(const char *link,
117+
unsigned long link_owner_uid,
118+
unsigned long trusted_owner_uid,
119+
bool accept_own_target, const char *rest);
120+
#endif
121+
107122
void cbm_activation_transaction_write_failures_set_for_testing(unsigned int count, int os_error);
108123
#endif
109124

‎src/cli/cli.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4661,7 +4661,10 @@ static int cbm_remove_grok_mcp_owned(const char *binary_path, const char *config
46614661
/* Search augmentation runs before Grep/Glob/Bash; exact coverage context runs after
46624662
* Read. Both adapters are context-only and fail open. */
46634663
#define CMM_HOOK_SEARCH_MATCHER "Grep|Glob|Bash"
4664-
#define CMM_HOOK_READ_MATCHER "Read"
4664+
/* The PostToolUse entry also matches the graph tools (mcp__<server>__<tool>,
4665+
* or mcp__plugin_<plugin>_<server>__<tool>), whose calls reset the drift
4666+
* reminder's search count (#509). */
4667+
#define CMM_HOOK_READ_MATCHER "Read|mcp__.*codebase-memory-mcp__.*"
46654668
/* Basename only; the full command path is resolved at install time via
46664669
* cbm_resolve_hook_command so $CLAUDE_CONFIG_DIR is honored. */
46674670
#ifdef _WIN32
@@ -4682,6 +4685,11 @@ static const char *const cmm_claude_exec_args[] = {"hook-augment"};
46824685
/* Old matcher values from previous versions — recognized during upgrade so
46834686
* upsert/remove can clean them up before inserting the current matcher.
46844687
* Per-agent lists (no shared global): each caller passes its own. */
4688+
/* Installs up to v0.12 matched only Read in PostToolUse. */
4689+
static const char *const cmm_claude_old_read_matchers[] = {
4690+
"Read",
4691+
NULL,
4692+
};
46854693
static const char *const cmm_claude_old_matchers[] = {
46864694
"Grep|Glob|Read|Search",
46874695
"Grep|Glob|Read",
@@ -5459,7 +5467,7 @@ static int cbm_upsert_claude_hooks_with_binary(const char *settings_path, const
54595467
if (cbm_remove_previous_claude_exec_hook(settings_path, "PreToolUse", CMM_HOOK_SEARCH_MATCHER,
54605468
cmm_claude_old_matchers, binary_path) != CLI_OK ||
54615469
cbm_remove_previous_claude_exec_hook(settings_path, "PostToolUse", CMM_HOOK_READ_MATCHER,
5462-
NULL, binary_path) != CLI_OK) {
5470+
cmm_claude_old_read_matchers, binary_path) != CLI_OK) {
54635471
return CLI_ERR;
54645472
}
54655473
int search_result = upsert_hooks_json((hooks_upsert_args_t){
@@ -5478,6 +5486,7 @@ static int cbm_upsert_claude_hooks_with_binary(const char *settings_path, const
54785486
.settings_path = settings_path,
54795487
.hook_event = "PostToolUse",
54805488
.matcher_str = CMM_HOOK_READ_MATCHER,
5489+
.old_matchers = cmm_claude_old_read_matchers,
54815490
.command_str = command,
54825491
.command_args = command_args,
54835492
.command_arg_count = command_arg_count,
@@ -5532,6 +5541,7 @@ static int cbm_remove_claude_hooks_with_binary(const char *settings_path, const
55325541
.settings_path = settings_path,
55335542
.hook_event = "PostToolUse",
55345543
.matcher_str = CMM_HOOK_READ_MATCHER,
5544+
.old_matchers = cmm_claude_old_read_matchers,
55355545
.old_commands = old_commands,
55365546
.match_command_exact = command,
55375547
.match_args = command_args,

0 commit comments

Comments
 (0)