Skip to content

Commit 63be298

Browse files
JCVIIAmclaude
andcommitted
feat(cli): extend the OpenCode plugin to every documented context surface
The generated OpenCode adapter covered only the grep/glob graph lookup, so OpenCode sessions missed the read-coverage and lifecycle context other clients get through their hook configuration. Extend the plugin to the surfaces OpenCode documents: - unwrap hook-augment's additionalContext envelope so plain text, not raw JSON, lands in the tool output - post-read index-coverage notes via tool.execute.after, mapping OpenCode's filePath argument to the file_path key the default dialect reads - session-start tier routing injected once per sessionID on the first tool result, since OpenCode documents no context-output lifecycle hook - post-compaction reinjection through the documented experimental session.compacting surface (output.context) Every failure path resolves to the empty string: the adapter can degrade to a no-op but never blocks a tool call. Pin the new surfaces in test_agent_clients (31/31 pass; the DeusData#616 payload guard stays green) and record the wider surface in the README client matrix. Signed-off-by: jc <jcvi007@gmail.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 0d6f26a commit 63be298

3 files changed

Lines changed: 105 additions & 34 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ overwrite user-modified agents.
448448
| Codex CLI | Detected | `$CODEX_HOME/config.toml` | `AGENTS.md`, skill, three read-only agents; `SessionStart` + `SubagentStart` |
449449
| Gemini CLI | Detected | `.gemini/settings.json` | `GEMINI.md`, three explicit read/graph-tool subagents; `BeforeTool`, `AfterTool` `read_file` coverage, and `SessionStart` |
450450
| Zed | Detected | platform `settings.json` (JSONC) | `AGENTS.md` + shared skill |
451-
| OpenCode | Detected | `$OPENCODE_CONFIG` or resolved global config | `AGENTS.md`, skill, three deny-by-default read-only agents |
451+
| 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 |
452452
| Antigravity | Detected | `.gemini/config/mcp_config.json` | `.gemini/GEMINI.md` |
453453
| Aider | Detected | — | `CONVENTIONS.md` via `.aider.conf.yml` |
454454
| KiloCode | Detected | `.config/kilo/kilo.jsonc` | Rule + three graph-tool subagents with deny-by-default permissions |

‎src/cli/client_adapter.c‎

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -188,46 +188,94 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
188188

189189
adapter_sb_t sb = {0};
190190
emit_header(&sb, "OpenCode");
191-
sb_append(&sb, "// OpenCode already reaches every tool over MCP; this adds only the\n"
192-
"// automatic graph lookup before a grep/glob, which other clients get\n"
193-
"// through their own hook configuration.\n");
191+
sb_append(&sb,
192+
"// OpenCode already reaches every tool over MCP; this module adds the\n"
193+
"// context surfaces other clients get through hook configuration: graph\n"
194+
"// lookup after grep/glob, index-coverage notes after read, session-start\n"
195+
"// tier routing (carried on the first tool result of each session, since\n"
196+
"// OpenCode documents no context-output lifecycle hook), and reinjection\n"
197+
"// after compaction via the documented experimental surface.\n");
194198
sb_append(&sb, "import { spawn } from 'node:child_process';\n\n");
195199
sb_append(&sb, "const BIN = '");
196200
sb_append(&sb, bin);
197201
sb_append(&sb, "';\n\n");
198202

199-
/* hook-augment requires hook_event_name and accepts Grep/Glob only under
200-
* PreToolUse; omitting it makes the whole hook a silent no-op. */
201-
sb_append(&sb, "function augment(tool, args) {\n"
202-
" return new Promise((resolve) => {\n"
203-
" const child = spawn(BIN, ['hook-augment'], {\n"
204-
" stdio: ['pipe', 'pipe', 'ignore'],\n"
205-
" env: { ...process.env, CBM_LOG_LEVEL: 'error' },\n"
206-
" });\n"
207-
" let out = '';\n"
208-
" child.stdout.on('data', (d) => (out += d.toString()));\n"
209-
" child.on('error', () => resolve(''));\n"
210-
" child.on('close', () => resolve(out));\n"
211-
" child.stdin.end(JSON.stringify({\n"
212-
" hook_event_name: 'PreToolUse',\n"
213-
" tool_name: tool,\n"
214-
" tool_input: args ?? {},\n"
215-
" }));\n"
216-
" });\n"
217-
"}\n\n");
203+
/* hook-augment requires hook_event_name; its default dialect accepts
204+
* Grep/Glob under PreToolUse, Read under PostToolUse, and the
205+
* SessionStart lifecycle event — and emits the Claude JSON envelope, so
206+
* the plugin unwraps additionalContext instead of pasting raw JSON into
207+
* the tool output. Every failure path resolves to '' (fail open). */
208+
sb_append(&sb,
209+
"function augment(payload) {\n"
210+
" return new Promise((resolve) => {\n"
211+
" const child = spawn(BIN, ['hook-augment'], {\n"
212+
" stdio: ['pipe', 'pipe', 'ignore'],\n"
213+
" env: { ...process.env, CBM_LOG_LEVEL: 'error' },\n"
214+
" });\n"
215+
" let out = '';\n"
216+
" child.stdout.on('data', (d) => (out += d.toString()));\n"
217+
" child.on('error', () => resolve(''));\n"
218+
" child.on('close', () => {\n"
219+
" try {\n"
220+
" const ctx = JSON.parse(out)?.hookSpecificOutput?.additionalContext;\n"
221+
" resolve(typeof ctx === 'string' ? ctx : '');\n"
222+
" } catch { resolve(''); }\n"
223+
" });\n"
224+
" child.stdin.end(JSON.stringify(payload));\n"
225+
" });\n"
226+
"}\n\n");
218227

219228
sb_append(&sb,
220-
"export const CodebaseMemory = async () => ({\n"
221-
" 'tool.execute.after': async (input, output) => {\n"
222-
" const tool = input?.tool === 'grep' ? 'Grep' : input?.tool === 'glob' ? 'Glob' "
223-
": null;\n"
224-
" if (!tool) return;\n"
225-
" const extra = await augment(tool, output?.args);\n"
226-
" if (extra && typeof output?.output === 'string') {\n"
227-
" output.output += '\\n' + extra;\n"
228-
" }\n"
229-
" },\n"
230-
"});\n");
229+
"export const CodebaseMemory = async (ctx) => {\n"
230+
" const dir = ctx?.directory;\n"
231+
" const seen = new Set();\n"
232+
" const lifecycle = () =>\n"
233+
" augment({ hook_event_name: 'SessionStart', cwd: dir });\n"
234+
" return {\n"
235+
" 'tool.execute.after': async (input, output) => {\n"
236+
" if (typeof output?.output !== 'string') return;\n"
237+
" const pieces = [];\n"
238+
" const sid = input?.sessionID;\n"
239+
" if (typeof sid === 'string' && !seen.has(sid)) {\n"
240+
" seen.add(sid);\n"
241+
" pieces.push(await lifecycle());\n"
242+
" }\n"
243+
" const args = output?.args ?? {};\n"
244+
" const search =\n"
245+
" input?.tool === 'grep' ? 'Grep' : input?.tool === 'glob' ? 'Glob' : null;\n"
246+
" if (search) {\n"
247+
" pieces.push(await augment({\n"
248+
" hook_event_name: 'PreToolUse',\n"
249+
" tool_name: search,\n"
250+
" tool_input: args,\n"
251+
" cwd: dir,\n"
252+
" }));\n"
253+
" } else if (input?.tool === 'read') {\n"
254+
" const filePath = args.filePath ?? args.file_path ?? args.path;\n"
255+
" if (typeof filePath === 'string' && filePath) {\n"
256+
" pieces.push(await augment({\n"
257+
" hook_event_name: 'PostToolUse',\n"
258+
" tool_name: 'Read',\n"
259+
" tool_input: { file_path: filePath },\n"
260+
" cwd: dir,\n"
261+
" }));\n"
262+
" }\n"
263+
" }\n"
264+
" const extra = pieces.filter(Boolean).join('\\n');\n"
265+
" if (extra) {\n"
266+
" output.output += '\\n' + extra;\n"
267+
" }\n"
268+
" },\n"
269+
" // Documented (experimental) compaction surface: output.context is the\n"
270+
" // mutable array of context strings for the rebuilt session.\n"
271+
" 'experimental.session.compacting': async (_input, output) => {\n"
272+
" const note = await lifecycle();\n"
273+
" if (note && Array.isArray(output?.context)) {\n"
274+
" output.context.push(note);\n"
275+
" }\n"
276+
" },\n"
277+
" };\n"
278+
"};\n");
231279

232280
if (sb.failed) {
233281
free(sb.buf);

‎tests/test_agent_clients.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,28 @@ TEST(client_adapter_opencode_sends_the_required_hook_event) {
11481148
PASS();
11491149
}
11501150

1151+
/* The richer OpenCode adapter carries every context surface the plugin API
1152+
* documents: session-start tier routing on the first tool result of each
1153+
* session, post-read coverage notes, and post-compaction reinjection through
1154+
* the documented experimental surface. It must unwrap hook-augment's Claude
1155+
* JSON envelope so plain text — not raw JSON — reaches the model. */
1156+
TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction) {
1157+
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
1158+
ASSERT_NOT_NULL(js);
1159+
ASSERT_NOT_NULL(strstr(js, "hook_event_name: 'SessionStart'"));
1160+
ASSERT_NOT_NULL(strstr(js, "hook_event_name: 'PostToolUse'"));
1161+
ASSERT_NOT_NULL(strstr(js, "tool_name: 'Read'"));
1162+
ASSERT_NOT_NULL(strstr(js, "'experimental.session.compacting'"));
1163+
ASSERT_NOT_NULL(strstr(js, "additionalContext"));
1164+
/* file_path is the key hook-augment's default dialect reads; OpenCode's
1165+
* read tool argues filePath, so the adapter must map it. */
1166+
ASSERT_NOT_NULL(strstr(js, "file_path: filePath"));
1167+
/* Session context may only be injected once per session id. */
1168+
ASSERT_NOT_NULL(strstr(js, "seen.has(sid)"));
1169+
free(js);
1170+
PASS();
1171+
}
1172+
11511173
/* Empty/NULL inputs must not produce a module at all. */
11521174
TEST(client_adapter_rejects_missing_binary_path) {
11531175
ASSERT_NULL(cbm_client_adapter_pi(NULL));
@@ -1187,5 +1209,6 @@ SUITE(agent_clients) {
11871209
RUN_TEST(client_adapter_pi_registers_every_registry_tool);
11881210
RUN_TEST(client_adapter_escapes_windows_paths_and_quotes);
11891211
RUN_TEST(client_adapter_opencode_sends_the_required_hook_event);
1212+
RUN_TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction);
11901213
RUN_TEST(client_adapter_rejects_missing_binary_path);
11911214
}

0 commit comments

Comments
 (0)