Skip to content

Commit b3d7940

Browse files
authored
Merge branch 'main' into fix/issue-1682-dotted-ts-imports
2 parents dd74440 + ae5de7f commit b3d7940

25 files changed

Lines changed: 1368 additions & 55 deletions

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ overwrite user-modified agents.
488488
| Codex CLI | Detected | `$CODEX_HOME/config.toml` | `AGENTS.md`, skill, three read-only agents; `SessionStart` + `SubagentStart` |
489489
| Gemini CLI | Detected | `.gemini/settings.json` | `GEMINI.md`, three explicit read/graph-tool subagents; `BeforeTool`, `AfterTool` `read_file` coverage, and `SessionStart` |
490490
| Zed | Detected | platform `settings.json` (JSONC) | `AGENTS.md` + shared skill |
491-
| OpenCode | Detected | `$OPENCODE_CONFIG` or resolved global config | `AGENTS.md`, skill, three deny-by-default read-only agents |
491+
| OpenCode | Detected | `$OPENCODE_CONFIG` or resolved global config | `AGENTS.md`, skill, three deny-by-default read-only agents; plugin adds grep/glob graph lookup, post-`read` coverage, first-tool-result session context, and post-compaction reinjection |
492492
| Antigravity | Detected | `.gemini/config/mcp_config.json` | `.gemini/GEMINI.md` |
493493
| Aider | Detected | — | `CONVENTIONS.md` via `.aider.conf.yml` |
494494
| KiloCode | Detected | `.config/kilo/kilo.jsonc` | Rule + three graph-tool subagents with deny-by-default permissions |

‎internal/cbm/extract_defs.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4780,6 +4780,20 @@ static TSNode find_class_member_body(TSNode class_node, CBMLanguage lang) {
47804780
return ts_node_is_null(declarations) ? body : declarations;
47814781
}
47824782

4783+
/* Go structs keep their field_declaration nodes one level below the body that
4784+
* find_class_body() returns: type_spec's `type` child is a struct_type whose
4785+
* only named child is a field_declaration_list. Interfaces need no such step --
4786+
* interface_type holds its method specs directly, which is why interface members
4787+
* extracted correctly while every struct field was silently skipped. Normalize
4788+
* here, the same way the Java enum_body_declarations step above does. */
4789+
static TSNode go_normalize_struct_body(TSNode body) {
4790+
if (ts_node_is_null(body) || strcmp(ts_node_type(body), "struct_type") != 0) {
4791+
return body;
4792+
}
4793+
TSNode list = cbm_find_child_by_kind(body, "field_declaration_list");
4794+
return ts_node_is_null(list) ? body : list;
4795+
}
4796+
47834797
// Dart: resolve method name from method_signature/function_signature.
47844798
static TSNode resolve_dart_method_name(TSNode child, const char *ck) {
47854799
if (strcmp(ck, "method_signature") == 0) {
@@ -6656,6 +6670,9 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
66566670
}
66576671

66586672
TSNode body = find_class_member_body(class_node, ctx->language);
6673+
if (ctx->language == CBM_LANG_GO) {
6674+
body = go_normalize_struct_body(body);
6675+
}
66596676
if (ts_node_is_null(body)) {
66606677
return;
66616678
}
@@ -6945,6 +6962,13 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
69456962
continue;
69466963
}
69476964

6965+
/* Go: `_` is the blank identifier, used for explicit struct padding in
6966+
* generated code. It is not a referenceable field, and emitting it gives
6967+
* every `_` in the repository a same-named node to collide with. */
6968+
if (ctx->language == CBM_LANG_GO && strcmp(name, "_") == 0) {
6969+
continue;
6970+
}
6971+
69486972
const char *field_qn = cbm_arena_sprintf(a, "%s.%s", class_qn, name);
69496973

69506974
CBMDefinition def;

‎src/cli/client_adapter.c‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,23 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
265265

266266
adapter_sb_t sb = {0};
267267
emit_header(&sb, "OpenCode");
268-
sb_append(&sb, "// OpenCode already reaches every tool over MCP; this adds only the\n"
269-
"// automatic graph lookup before a grep/glob, which other clients get\n"
270-
"// through their own hook configuration.\n");
268+
sb_append(&sb, "// OpenCode already reaches every tool over MCP; this module adds the\n"
269+
"// context surfaces other clients get through hook configuration: graph\n"
270+
"// lookup after grep/glob, index-coverage notes after read, session-start\n"
271+
"// tier routing (carried on the first tool result of each session, since\n"
272+
"// OpenCode documents no context-output lifecycle hook), and reinjection\n"
273+
"// after compaction via the documented experimental surface.\n");
271274
sb_append(&sb, "import { spawn } from 'node:child_process';\n\n");
272275
sb_append(&sb, "const BIN = '");
273276
sb_append(&sb, bin);
274277
sb_append(&sb, "';\n\n");
275278

276-
/* hook-augment requires hook_event_name and accepts Grep/Glob only under
277-
* PreToolUse; omitting it makes the whole hook a silent no-op. */
278-
sb_append(&sb, "function augment(tool, args) {\n"
279+
/* hook-augment requires hook_event_name; its default dialect accepts
280+
* Grep/Glob under PreToolUse, Read under PostToolUse, and the
281+
* SessionStart lifecycle event — and emits the Claude JSON envelope, so
282+
* the plugin unwraps additionalContext instead of pasting raw JSON into
283+
* the tool output. Every failure path resolves to '' (fail open). */
284+
sb_append(&sb, "function augment(payload) {\n"
279285
" return new Promise((resolve) => {\n"
280286
" const child = spawn(BIN, ['hook-augment'], {\n"
281287
" stdio: ['pipe', 'pipe', 'ignore'],\n"
@@ -284,27 +290,67 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
284290
" let out = '';\n"
285291
" child.stdout.on('data', (d) => (out += d.toString()));\n"
286292
" child.on('error', () => resolve(''));\n"
287-
" child.on('close', () => resolve(out));\n"
288-
" child.stdin.end(JSON.stringify({\n"
289-
" hook_event_name: 'PreToolUse',\n"
290-
" tool_name: tool,\n"
291-
" tool_input: args ?? {},\n"
292-
" }));\n"
293+
" child.on('close', () => {\n"
294+
" try {\n"
295+
" const ctx = JSON.parse(out)?.hookSpecificOutput?.additionalContext;\n"
296+
" resolve(typeof ctx === 'string' ? ctx : '');\n"
297+
" } catch { resolve(''); }\n"
298+
" });\n"
299+
" child.stdin.end(JSON.stringify(payload));\n"
293300
" });\n"
294301
"}\n\n");
295302

296303
sb_append(&sb,
297-
"export const CodebaseMemory = async () => ({\n"
298-
" 'tool.execute.after': async (input, output) => {\n"
299-
" const tool = input?.tool === 'grep' ? 'Grep' : input?.tool === 'glob' ? 'Glob' "
300-
": null;\n"
301-
" if (!tool) return;\n"
302-
" const extra = await augment(tool, output?.args);\n"
303-
" if (extra && typeof output?.output === 'string') {\n"
304-
" output.output += '\\n' + extra;\n"
305-
" }\n"
306-
" },\n"
307-
"});\n");
304+
"export const CodebaseMemory = async (ctx) => {\n"
305+
" const dir = ctx?.directory;\n"
306+
" const seen = new Set();\n"
307+
" const lifecycle = () =>\n"
308+
" augment({ hook_event_name: 'SessionStart', cwd: dir });\n"
309+
" return {\n"
310+
" 'tool.execute.after': async (input, output) => {\n"
311+
" if (typeof output?.output !== 'string') return;\n"
312+
" const pieces = [];\n"
313+
" const sid = input?.sessionID;\n"
314+
" if (typeof sid === 'string' && !seen.has(sid)) {\n"
315+
" seen.add(sid);\n"
316+
" pieces.push(await lifecycle());\n"
317+
" }\n"
318+
" const args = output?.args ?? {};\n"
319+
" const search =\n"
320+
" input?.tool === 'grep' ? 'Grep' : input?.tool === 'glob' ? 'Glob' : null;\n"
321+
" if (search) {\n"
322+
" pieces.push(await augment({\n"
323+
" hook_event_name: 'PreToolUse',\n"
324+
" tool_name: search,\n"
325+
" tool_input: args,\n"
326+
" cwd: dir,\n"
327+
" }));\n"
328+
" } else if (input?.tool === 'read') {\n"
329+
" const filePath = args.filePath ?? args.file_path ?? args.path;\n"
330+
" if (typeof filePath === 'string' && filePath) {\n"
331+
" pieces.push(await augment({\n"
332+
" hook_event_name: 'PostToolUse',\n"
333+
" tool_name: 'Read',\n"
334+
" tool_input: { file_path: filePath },\n"
335+
" cwd: dir,\n"
336+
" }));\n"
337+
" }\n"
338+
" }\n"
339+
" const extra = pieces.filter(Boolean).join('\\n');\n"
340+
" if (extra) {\n"
341+
" output.output += '\\n' + extra;\n"
342+
" }\n"
343+
" },\n"
344+
" // Documented (experimental) compaction surface: output.context is the\n"
345+
" // mutable array of context strings for the rebuilt session.\n"
346+
" 'experimental.session.compacting': async (_input, output) => {\n"
347+
" const note = await lifecycle();\n"
348+
" if (note && Array.isArray(output?.context)) {\n"
349+
" output.context.push(note);\n"
350+
" }\n"
351+
" },\n"
352+
" };\n"
353+
"};\n");
308354

309355
if (sb.failed) {
310356
free(sb.buf);

‎src/cypher/cypher.c‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,6 +4894,164 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
48944894

48954895
/* ── Main entry point ─────────────────────────────────────────── */
48964896

4897+
/* ── Is every name a RETURN or WITH item uses actually in scope? ──
4898+
*
4899+
* An unbound variable resolves to NULL and renders as "" on purpose: an
4900+
* OPTIONAL MATCH target that found no row has to project a blank rather than
4901+
* drop the row. The cost of that convention is that a name the query NEVER
4902+
* carried through looks exactly the same on screen. A typo, or a variable a
4903+
* WITH dropped, then reads as "the graph holds no such data" instead of "your
4904+
* query named something that is not there" (#1919). Same failure shape as
4905+
* #373, and the same answer: say so out loud.
4906+
*
4907+
* The check runs on the parsed query, not on the run-time bindings, and that
4908+
* is the whole trick. It asks whether the query DECLARED the name — a
4909+
* different question from whether a row happened to bind it. So the OPTIONAL
4910+
* MATCH convention above is untouched: a target that did not match is still
4911+
* declared, still legal, and still projects "".
4912+
*/
4913+
4914+
/* Cap for one query's declared names. CYP_MAX_VARS is the binding cap for
4915+
* nodes alone; a pattern also names edges, so this is the roomier of the two.
4916+
* A query that overruns it skips the check rather than guessing — a wrong
4917+
* refusal costs the caller a working query, which is worse than the silence
4918+
* this guard removes. */
4919+
enum { CYP_SCOPE_MAX_NAMES = 32 };
4920+
4921+
static bool scope_holds(const char *const *names, int count, const char *want) {
4922+
for (int i = 0; i < count; i++) {
4923+
if (names[i] && strcmp(names[i], want) == 0) {
4924+
return true;
4925+
}
4926+
}
4927+
return false;
4928+
}
4929+
4930+
/* Every name the query's patterns declare, plus an UNWIND alias.
4931+
* Answers -1 when there are more names than the cap holds. */
4932+
static int collect_declared_names(const cbm_query_t *q, const char **out, int cap) {
4933+
int n = 0;
4934+
for (int pi = 0; pi < q->pattern_count; pi++) {
4935+
const cbm_pattern_t *pat = &q->patterns[pi];
4936+
for (int ni = 0; ni < pat->node_count; ni++) {
4937+
const char *var = pat->nodes[ni].variable;
4938+
if (var && !scope_holds(out, n, var)) {
4939+
if (n >= cap) {
4940+
return -1;
4941+
}
4942+
out[n++] = var;
4943+
}
4944+
}
4945+
for (int ri = 0; ri < pat->rel_count; ri++) {
4946+
const char *var = pat->rels[ri].variable;
4947+
if (var && !scope_holds(out, n, var)) {
4948+
if (n >= cap) {
4949+
return -1;
4950+
}
4951+
out[n++] = var;
4952+
}
4953+
}
4954+
}
4955+
if (q->unwind_alias && !scope_holds(out, n, q->unwind_alias)) {
4956+
if (n >= cap) {
4957+
return -1;
4958+
}
4959+
out[n++] = q->unwind_alias;
4960+
}
4961+
return n;
4962+
}
4963+
4964+
/* What a WITH leaves behind: its alias where it made one, and the plain
4965+
* variable where it carried one through whole. `WITH f.name AS caller` leaves
4966+
* `caller` and nothing else — `f` is gone, which is the case #1919 is about. */
4967+
static int collect_with_names(const cbm_return_clause_t *wc, const char **out, int cap) {
4968+
int n = 0;
4969+
for (int i = 0; i < wc->count; i++) {
4970+
const cbm_return_item_t *item = &wc->items[i];
4971+
const char *name = NULL;
4972+
if (item->alias) {
4973+
name = item->alias;
4974+
} else if (item->variable && !item->property && !item->func) {
4975+
name = item->variable;
4976+
}
4977+
if (name && !scope_holds(out, n, name)) {
4978+
if (n >= cap) {
4979+
return -1;
4980+
}
4981+
out[n++] = name;
4982+
}
4983+
}
4984+
return n;
4985+
}
4986+
4987+
/* Build the message. It names the variable and the clause, because an error
4988+
* that does not say WHICH name is wrong sends the reader back to guessing. */
4989+
static char *scope_error(const char *var, const char *clause, const char *why) {
4990+
char buf[CBM_SZ_256];
4991+
snprintf(buf, sizeof(buf), "variable '%s' is not in scope for %s — %s", var, clause, why);
4992+
return heap_strdup(buf);
4993+
}
4994+
4995+
/* The variable this item really references, or NULL when it references none.
4996+
* Two items carry a placeholder in that field rather than a name the query
4997+
* declared: `count(*)` stores "*", and a CASE expression stores "CASE". Both
4998+
* would read as an unknown variable, so neither is checkable here. */
4999+
static const char *scope_checkable_var(const cbm_return_item_t *item) {
5000+
if (item->kase || !item->variable) {
5001+
return NULL;
5002+
}
5003+
if (strcmp(item->variable, "*") == 0) {
5004+
return NULL;
5005+
}
5006+
return item->variable;
5007+
}
5008+
5009+
/* Answers NULL when the query is fine, or a heap message naming the first
5010+
* variable that is not in scope. Checks one query; the caller walks a UNION. */
5011+
static char *check_projection_scope(const cbm_query_t *q) {
5012+
const char *declared[CYP_SCOPE_MAX_NAMES];
5013+
int declared_n = collect_declared_names(q, declared, CYP_SCOPE_MAX_NAMES);
5014+
if (declared_n < 0) {
5015+
return NULL; /* too many names to model — stay quiet rather than guess */
5016+
}
5017+
5018+
/* A WITH still reads the pattern variables. */
5019+
if (q->with_clause && !q->with_clause->star) {
5020+
for (int i = 0; i < q->with_clause->count; i++) {
5021+
const char *var = scope_checkable_var(&q->with_clause->items[i]);
5022+
if (var && !scope_holds(declared, declared_n, var)) {
5023+
return scope_error(var, "WITH", "no pattern in this query names it");
5024+
}
5025+
}
5026+
}
5027+
5028+
if (!q->ret || q->ret->star) {
5029+
return NULL;
5030+
}
5031+
5032+
/* A RETURN after a WITH reads only what the WITH left behind. */
5033+
const char *after_with[CYP_SCOPE_MAX_NAMES];
5034+
const char *const *scope = declared;
5035+
int scope_n = declared_n;
5036+
if (q->with_clause) {
5037+
scope_n = collect_with_names(q->with_clause, after_with, CYP_SCOPE_MAX_NAMES);
5038+
if (scope_n < 0) {
5039+
return NULL;
5040+
}
5041+
scope = after_with;
5042+
}
5043+
5044+
for (int i = 0; i < q->ret->count; i++) {
5045+
const char *var = scope_checkable_var(&q->ret->items[i]);
5046+
if (var && !scope_holds(scope, scope_n, var)) {
5047+
return scope_error(var, "RETURN",
5048+
q->with_clause ? "the WITH clause did not carry it through"
5049+
: "no pattern in this query names it");
5050+
}
5051+
}
5052+
return NULL;
5053+
}
5054+
48975055
int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *project, int max_rows,
48985056
cbm_cypher_result_t *out) {
48995057
memset(out, 0, sizeof(*out));
@@ -4911,6 +5069,15 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projec
49115069
return CBM_NOT_FOUND;
49125070
}
49135071

5072+
for (const cbm_query_t *sq = q; sq; sq = sq->union_next) {
5073+
char *scope_err = check_projection_scope(sq);
5074+
if (scope_err) {
5075+
cbm_query_free(q);
5076+
out->error = scope_err;
5077+
return CBM_NOT_FOUND;
5078+
}
5079+
}
5080+
49145081
result_builder_t rb = {0};
49155082
if (execute_single(store, q, project, max_rows, &rb) < 0) {
49165083
rb_free(&rb);

0 commit comments

Comments
 (0)