Skip to content

Commit fb88365

Browse files
committed
fix(cli): add no OpenCode 2 compaction note until one has run on a real host
The V2 compaction hook pushed a plain object into the compaction request's event.messages. OpenCode's Message is a schema class at v2.2.5, and nobody has run this path on a real 2.x host. The try/catch only guards the array push, which cannot throw. If OpenCode rejects the object, that happens inside its own compaction, out of the plugin's reach, so an untested note could break compaction for every OpenCode 2 user. Losing the note costs far less. Remove the V2 compaction registration and the sessionHooksRegistered flag it needed. The V2 tool hook, proven on OpenCode 2.2.5, stays, and so does the 1.18.x experimental.session.compacting path. OpenCode 2 sessions still get the session note on the first tool result of a session. A later change can add a note after compaction together with a real 2.x run. The replacement test pins that the generated module registers no session hook and touches neither event.messages, a system property nor an instruction entry. Refs DeusData#2204 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent d3d0bc8 commit fb88365

4 files changed

Lines changed: 34 additions & 82 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ overwrite user-modified agents.
722722
| 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 |
723723
| Gemini CLI | Detected | `.gemini/settings.json` | `GEMINI.md`, three explicit read/graph-tool subagents; `BeforeTool`, `AfterTool` `read_file` coverage, and `SessionStart` |
724724
| Zed | Detected | platform `settings.json` (JSONC) | `AGENTS.md` + shared skill |
725-
| 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 |
725+
| 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 on OpenCode 1.18.x |
726726
| Antigravity | Detected | `.gemini/config/mcp_config.json` | `.gemini/GEMINI.md` |
727727
| Aider | Detected | none | `CONVENTIONS.md` via `.aider.conf.yml` |
728728
| KiloCode | Detected | `.config/kilo/kilo.jsonc` | Rule + three graph-tool subagents with deny-by-default permissions |

‎src/cli/client_adapter.c‎

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,15 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
428428
"// context surfaces other clients get through hook configuration: graph\n"
429429
"// lookup after grep/glob, index-coverage notes after read, session-start\n"
430430
"// tier routing (carried on the first tool result of each session), and\n"
431-
"// reinjection after compaction inside the compaction request itself.\n"
431+
"// on OpenCode 1.18.x reinjection after compaction.\n"
432432
"//\n"
433433
"// Two runtimes load this module:\n"
434434
"// - OpenCode 1.18.x dispatches the hooks returned by server(ctx).\n"
435435
"// - OpenCode 2 ignores server and calls setup(ctx), which registers the\n"
436-
"// same behaviour on the context's tool and session hook domains. Each\n"
437-
"// registration fails open, and the matching server() hook yields once\n"
438-
"// its domain has registered.\n");
436+
"// tool hook on the context's tool domain. The registration fails open,\n"
437+
"// and the server() tool hook yields once it has registered. OpenCode 2\n"
438+
"// gets no note after compaction: no compaction channel has been run on\n"
439+
"// a real 2.x host yet.\n");
439440
sb_append(&sb, "import { spawn } from 'node:child_process';\n\n");
440441
sb_append(&sb, "const BIN = '");
441442
sb_append(&sb, bin);
@@ -519,11 +520,10 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
519520
" return result;\n"
520521
"}\n\n");
521522

522-
/* Per-domain registration flags: a host that dispatches both server() and
523-
* setup() with domains must not append the note twice. The server() hook
524-
* for a domain yields once setup() registered that same domain. */
525-
sb_append(&sb, "let toolHooksRegistered = false;\n"
526-
"let sessionHooksRegistered = false;\n\n");
523+
/* A host that dispatches both server() and setup() with a tool domain must
524+
* not append the note twice: the server() tool hook yields once setup()
525+
* registered the tool domain. */
526+
sb_append(&sb, "let toolHooksRegistered = false;\n\n");
527527

528528
sb_append(
529529
&sb,
@@ -575,7 +575,6 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
575575
" // Documented (experimental) compaction surface: output.context is the\n"
576576
" // mutable array of context strings for the rebuilt session.\n"
577577
" 'experimental.session.compacting': async (_input, output) => {\n"
578-
" if (sessionHooksRegistered) return;\n"
579578
" const note = await lifecycle();\n"
580579
" if (note && Array.isArray(output?.context)) {\n"
581580
" output.context.push(note);\n"
@@ -584,8 +583,8 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
584583
" };\n"
585584
" },\n"
586585
" // OpenCode 2 runtime: default.server is ignored and setup(ctx) registers\n"
587-
" // the hooks on the context domains. Each registration fails open, so one\n"
588-
" // missing domain cannot break the host or the other domain.\n"
586+
" // the tool hook on the context's tool domain. The registration fails\n"
587+
" // open, so a missing domain cannot break the host.\n"
589588
" async setup(ctx) {\n"
590589
" const seen = new Set();\n"
591590
" const dirs = new Map();\n"
@@ -646,34 +645,6 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
646645
" // Fail open: the server() tool hook stays active.\n"
647646
" }\n"
648647
" }\n"
649-
" // OpenCode 2 compaction: the hook receives the compaction request, and\n"
650-
" // its messages are the input the summary is built from. The note is\n"
651-
" // added there as a text message, as the server() path adds it to\n"
652-
" // output.context, so it stays inside the compaction and out of the\n"
653-
" // system prompt. Hosts without the hook or the array skip reinjection\n"
654-
" // rather than failing the plugin.\n"
655-
" if (ctx?.session?.hook) {\n"
656-
" try {\n"
657-
" await ctx.session.hook('compaction', async (event) => {\n"
658-
" if (!Array.isArray(event?.messages)) return;\n"
659-
" const dir = await dirFor(event?.sessionID);\n"
660-
" if (!dir) return;\n"
661-
" const note = await lifecycle(dir);\n"
662-
" if (!note) return;\n"
663-
" try {\n"
664-
" event.messages.push({\n"
665-
" role: 'user',\n"
666-
" content: [{ type: 'text', text: note }],\n"
667-
" });\n"
668-
" } catch {\n"
669-
" // Fail open: reinjection is best-effort.\n"
670-
" }\n"
671-
" });\n"
672-
" sessionHooksRegistered = true;\n"
673-
" } catch {\n"
674-
" // Fail open: the server() compaction hook stays active.\n"
675-
" }\n"
676-
" }\n"
677648
" },\n"
678649
"};\n");
679650

‎src/cli/client_adapter.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ char *cbm_client_adapter_pi(const char *binary_path);
5656
* silently — no error, no signal. That risk is accepted deliberately for the
5757
* server() path and recorded here so it is not rediscovered as a mystery.
5858
* OpenCode 2 ignores `server` and calls `setup(ctx)`, which registers
59-
* `execute.after` and `compaction` on the context domains. Each registration
60-
* fails open, and the post-compaction note is added as a text message to the
61-
* compaction request's `messages` (the summary input), never to `system`.
59+
* `execute.after` on the context's tool domain and fails open. It registers no
60+
* `compaction` hook: pushing a plain object into `event.messages` would fail,
61+
* if at all, inside OpenCode's own compaction where the plugin cannot catch it,
62+
* so a note after compaction needs a real 2.x run first.
6263
*
6364
* Returns NULL on allocation failure or when binary_path is NULL/empty. */
6465
char *cbm_client_adapter_opencode(const char *binary_path);

‎tests/test_agent_clients.c‎

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,11 +1344,10 @@ TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction) {
13441344

13451345
/* #2077/#2089/#2204: the two OpenCode runtimes read different entries of the
13461346
* default export. 1.18.x dispatches the hooks returned by `server(ctx)`; 2.x
1347-
* ignores `server` and calls `setup(ctx)`, whose context provides the tool and
1348-
* session hook domains. Registrations fail open per domain, so the matching
1349-
* server() hook yields once its domain is registered. The post-compaction note
1350-
* goes into the compaction request's messages, never the system prompt (pinned
1351-
* by the compaction test below). */
1347+
* ignores `server` and calls `setup(ctx)`, whose context provides the tool hook
1348+
* domain. The registration fails open, and the server() tool hook yields once
1349+
* it is registered. OpenCode 2 adds no note to a compaction (pinned by the
1350+
* compaction test below). */
13521351
TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13531352
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
13541353
ASSERT_NOT_NULL(js);
@@ -1360,8 +1359,6 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13601359
* 2.x, so setup is no longer the empty placeholder #2089 pinned. */
13611360
ASSERT_NOT_NULL(strstr(js, "async setup(ctx) {"));
13621361
ASSERT_NOT_NULL(strstr(js, "ctx.tool.hook('execute.after'"));
1363-
ASSERT_NOT_NULL(strstr(js, "ctx.session.hook('compaction'"));
1364-
ASSERT_NULL(strstr(js, "ctx.session.hook('context'"));
13651362
/* content is the surface the model reads; prefer it over output, and never
13661363
* synthesize content that would hide the tool's own result. */
13671364
const char *content_first = strstr(js, "if (typeof result.content === 'string') {");
@@ -1382,11 +1379,9 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13821379
* location in a multi-project server. */
13831380
ASSERT_NULL(strstr(js, "ctx?.location?.directory"));
13841381
ASSERT_NULL(strstr(js, "pendingReinject"));
1385-
/* Registrations fail open per domain; the server() hooks yield per domain. */
1382+
/* The registration fails open; the server() tool hook yields once it is in. */
13861383
ASSERT_NOT_NULL(strstr(js, "toolHooksRegistered = true;"));
1387-
ASSERT_NOT_NULL(strstr(js, "sessionHooksRegistered = true;"));
13881384
ASSERT_NOT_NULL(strstr(js, "if (toolHooksRegistered) return;"));
1389-
ASSERT_NOT_NULL(strstr(js, "if (sessionHooksRegistered) return;"));
13901385
ASSERT_NOT_NULL(strstr(js, "catch {"));
13911386
ASSERT_NOT_NULL(strstr(js, "event.result = appendResult(event.result, extra);"));
13921387
/* The empty setup placeholder and the old named export must both be gone. */
@@ -1396,38 +1391,23 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13961391
PASS();
13971392
}
13981393

1399-
/* #2204: on OpenCode 2 the post-compaction note stays inside the compaction,
1400-
* at the level the 1.18.x path uses (output.context). The `compaction` hook
1401-
* receives the compaction request, whose `messages` are the input the summary
1402-
* is built from, so the note is pushed there as a text message. It must never
1403-
* reach the system prompt: not through `event.system`, and not through a
1404-
* session instruction entry, which OpenCode renders into the system array of
1405-
* every later request. */
1406-
TEST(client_adapter_opencode_v2_compaction_note_goes_into_the_compaction_messages) {
1394+
/* #2204: OpenCode 2 gets no note after compaction until a compaction channel has
1395+
* run on a real 2.x host. A plain object pushed into `event.messages` would be
1396+
* rejected, if at all, inside OpenCode's own compaction, where the plugin's
1397+
* try/catch cannot reach it, so an untested channel could break compaction for
1398+
* every OpenCode 2 user. The note must not reach the system prompt either: not
1399+
* through `event.system`, and not through a session instruction entry. The
1400+
* 1.18.x server() path keeps its documented compaction surface. */
1401+
TEST(client_adapter_opencode_v2_adds_no_note_to_the_compaction) {
14071402
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
14081403
ASSERT_NOT_NULL(js);
1409-
const char *hook = strstr(js, "ctx.session.hook('compaction'");
1410-
const char *push = strstr(js, "event.messages.push({");
1411-
const char *registered = strstr(js, "sessionHooksRegistered = true;");
1412-
ASSERT_NOT_NULL(hook);
1413-
ASSERT_NOT_NULL(push);
1414-
ASSERT_NOT_NULL(registered);
1415-
/* The push lives inside the compaction registration. */
1416-
ASSERT_TRUE(hook < push);
1417-
ASSERT_TRUE(push < registered);
1418-
/* A text message that carries the note. */
1419-
const char *role = strstr(push, "role: 'user',");
1420-
const char *text = strstr(push, "content: [{ type: 'text', text: note }],");
1421-
ASSERT_NOT_NULL(role);
1422-
ASSERT_NOT_NULL(text);
1423-
ASSERT_TRUE(role < registered);
1424-
ASSERT_TRUE(text < registered);
1425-
/* Fail open: a host whose event has no messages array skips reinjection. */
1426-
ASSERT_NOT_NULL(strstr(js, "if (!Array.isArray(event?.messages)) return;"));
1427-
/* Never the system prompt, and no session instruction entry. */
1404+
ASSERT_NULL(strstr(js, "ctx.session.hook("));
1405+
ASSERT_NULL(strstr(js, "event.messages"));
1406+
ASSERT_NULL(strstr(js, "sessionHooksRegistered"));
14281407
ASSERT_NULL(strstr(js, ".system"));
14291408
ASSERT_NULL(strstr(js, "instructions"));
14301409
ASSERT_NULL(strstr(js, "entry.put"));
1410+
ASSERT_NOT_NULL(strstr(js, "'experimental.session.compacting'"));
14311411
free(js);
14321412
PASS();
14331413
}
@@ -1529,7 +1509,7 @@ SUITE(agent_clients) {
15291509
RUN_TEST(client_adapter_opencode_sends_the_required_hook_event);
15301510
RUN_TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction);
15311511
RUN_TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077);
1532-
RUN_TEST(client_adapter_opencode_v2_compaction_note_goes_into_the_compaction_messages);
1512+
RUN_TEST(client_adapter_opencode_v2_adds_no_note_to_the_compaction);
15331513
RUN_TEST(client_adapter_opencode_v2_append_result_never_creates_note_only_content);
15341514
RUN_TEST(client_adapter_opencode_bounds_a_hung_hook_augment_child);
15351515
RUN_TEST(client_adapter_rejects_missing_binary_path);

0 commit comments

Comments
 (0)