Skip to content

Commit 8f3c150

Browse files
committed
fix(cli): emit the OpenCode plugin as a V2 default export
OpenCode's V2 loader validates only the module's default export, expecting {id, setup()|effect()}. cbm_client_adapter_opencode still emitted the V1 named export (export const CodebaseMemory = ...), so every server start failed with SchemaError: Missing key at ["default"]. Wrap the existing hook logic in a default export with an id and a setup(ctx) function; the returned hooks object is unchanged. Fixes DeusData#2077 Signed-off-by: Amir Fathi <amirfathi.me@gmail.com>
1 parent 055fbb7 commit 8f3c150

2 files changed

Lines changed: 68 additions & 51 deletions

File tree

‎src/cli/client_adapter.c‎

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -300,57 +300,60 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
300300
" });\n"
301301
"}\n\n");
302302

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

355358
if (sb.failed) {
356359
free(sb.buf);

‎tests/test_agent_clients.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,19 @@ TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction) {
13161316
PASS();
13171317
}
13181318

1319+
/* #2077: OpenCode's V2 loader only reads the default export and needs an
1320+
* id plus a setup()/effect() function; the old named export had neither. */
1321+
TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
1322+
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
1323+
ASSERT_NOT_NULL(js);
1324+
ASSERT_NOT_NULL(strstr(js, "export default {"));
1325+
ASSERT_NOT_NULL(strstr(js, "id: 'codebase-memory-augment'"));
1326+
ASSERT_NOT_NULL(strstr(js, "async setup(ctx) {"));
1327+
ASSERT_NULL(strstr(js, "export const CodebaseMemory"));
1328+
free(js);
1329+
PASS();
1330+
}
1331+
13191332
/* Empty/NULL inputs must not produce a module at all. */
13201333
TEST(client_adapter_rejects_missing_binary_path) {
13211334
ASSERT_NULL(cbm_client_adapter_pi(NULL));
@@ -1362,5 +1375,6 @@ SUITE(agent_clients) {
13621375
RUN_TEST(client_adapter_escapes_windows_paths_and_quotes);
13631376
RUN_TEST(client_adapter_opencode_sends_the_required_hook_event);
13641377
RUN_TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction);
1378+
RUN_TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077);
13651379
RUN_TEST(client_adapter_rejects_missing_binary_path);
13661380
}

0 commit comments

Comments
 (0)