Skip to content

Commit f531be2

Browse files
committed
fix(cli): keep the OpenCode 2 compaction note inside the compaction
The OpenCode 2 compaction hook receives the compaction request, whose messages are the input the summary is built from. Add the post-compaction session note there as a text message, mirroring the 1.18.x path that pushes it into output.context. Session instruction entries are rendered into the system array of every later request, so the note no longer goes through ctx.session.instructions.entry, and event.system stays untouched. Registration and the push still fail open. Pin it in a new agent_clients test: the push sits inside the compaction registration, carries a user text part, is guarded by an array check, and the generated module names neither a system property nor an instruction entry. The #2077 test drops the assertions that pinned the old channel. Refs #2204 Signed-off-by: Martin Vogel <[email protected]>
1 parent 98a66b7 commit f531be2

3 files changed

Lines changed: 54 additions & 23 deletions

File tree

‎src/cli/client_adapter.c‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ 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 through the session context channel.\n"
431+
"// reinjection after compaction inside the compaction request itself.\n"
432432
"//\n"
433433
"// Two runtimes load this module:\n"
434434
"// - OpenCode 1.18.x dispatches the hooks returned by server(ctx).\n"
@@ -642,26 +642,24 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
642642
" // Fail open: the server() tool hook stays active.\n"
643643
" }\n"
644644
" }\n"
645-
" // OpenCode 2 compaction: persist the note as a keyed session\n"
646-
" // instruction (a <context> entry) instead of editing the system\n"
647-
" // prompt; entries survive compaction. Hosts without the channel\n"
648-
" // skip reinjection rather than failing the plugin.\n"
645+
" // OpenCode 2 compaction: the hook receives the compaction request, and\n"
646+
" // its messages are the input the summary is built from. The note is\n"
647+
" // added there as a text message, as the server() path adds it to\n"
648+
" // output.context, so it stays inside the compaction and out of the\n"
649+
" // system prompt. Hosts without the hook or the array skip reinjection\n"
650+
" // rather than failing the plugin.\n"
649651
" if (ctx?.session?.hook) {\n"
650652
" try {\n"
651653
" await ctx.session.hook('compaction', async (event) => {\n"
652-
" const sessionID = event?.sessionID;\n"
653-
" if (typeof sessionID !== 'string' || sessionID === '') return;\n"
654-
" const entry = ctx?.session?.instructions?.entry;\n"
655-
" if (typeof entry?.put !== 'function') return;\n"
656-
" const dir = await dirFor(sessionID);\n"
654+
" if (!Array.isArray(event?.messages)) return;\n"
655+
" const dir = await dirFor(event?.sessionID);\n"
657656
" if (!dir) return;\n"
658657
" const note = await lifecycle(dir);\n"
659658
" if (!note) return;\n"
660659
" try {\n"
661-
" await entry.put({\n"
662-
" sessionID,\n"
663-
" key: 'codebase-memory-session-note',\n"
664-
" value: note,\n"
660+
" event.messages.push({\n"
661+
" role: 'user',\n"
662+
" content: [{ type: 'text', text: note }],\n"
665663
" });\n"
666664
" } catch {\n"
667665
" // Fail open: reinjection is best-effort.\n"

‎src/cli/client_adapter.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ char *cbm_client_adapter_pi(const char *binary_path);
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
5959
* `execute.after` and `compaction` on the context domains. Each registration
60-
* fails open, and the post-compaction note is stored as a keyed session
61-
* instruction (`<context>` entry) instead of the system prompt.
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`.
6262
*
6363
* Returns NULL on allocation failure or when binary_path is NULL/empty. */
6464
char *cbm_client_adapter_opencode(const char *binary_path);

‎tests/test_agent_clients.c‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,8 +1347,8 @@ TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction) {
13471347
* ignores `server` and calls `setup(ctx)`, whose context provides the tool and
13481348
* session hook domains. Registrations fail open per domain, so the matching
13491349
* server() hook yields once its domain is registered. The post-compaction note
1350-
* goes through a keyed session instruction (a <context> entry that survives
1351-
* compaction), never the system prompt. */
1350+
* goes into the compaction request's messages, never the system prompt (pinned
1351+
* by the compaction test below). */
13521352
TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13531353
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
13541354
ASSERT_NOT_NULL(js);
@@ -1370,10 +1370,8 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13701370
ASSERT_NOT_NULL(output_fallback);
13711371
ASSERT_TRUE(content_first < output_fallback);
13721372
ASSERT_NULL(strstr(js, "if (result.content === undefined)"));
1373-
/* Compaction uses the durable session context channel, not the system
1374-
* prompt; older hosts without it skip reinjection. */
1373+
/* The V2 hooks read the session id with optional chaining (fail open). */
13751374
ASSERT_NOT_NULL(strstr(js, "event?.sessionID"));
1376-
ASSERT_NOT_NULL(strstr(js, "ctx?.session?.instructions?.entry"));
13771375
/* The plugin instance's location is not the session's location in a
13781376
* multi-project server: resolve each session's directory. */
13791377
ASSERT_NOT_NULL(strstr(js, "const dir = await dirFor("));
@@ -1383,8 +1381,6 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13831381
/* No fallback to the plugin instance's location: that is not the session's
13841382
* location in a multi-project server. */
13851383
ASSERT_NULL(strstr(js, "ctx?.location?.directory"));
1386-
ASSERT_NOT_NULL(strstr(js, "key: 'codebase-memory-session-note'"));
1387-
ASSERT_NULL(strstr(js, "event.system.push"));
13881384
ASSERT_NULL(strstr(js, "pendingReinject"));
13891385
/* Registrations fail open per domain; the server() hooks yield per domain. */
13901386
ASSERT_NOT_NULL(strstr(js, "toolHooksRegistered = true;"));
@@ -1400,6 +1396,42 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
14001396
PASS();
14011397
}
14021398

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) {
1407+
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
1408+
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. */
1428+
ASSERT_NULL(strstr(js, ".system"));
1429+
ASSERT_NULL(strstr(js, "instructions"));
1430+
ASSERT_NULL(strstr(js, "entry.put"));
1431+
free(js);
1432+
PASS();
1433+
}
1434+
14031435
/* augment() is awaited inline inside 'tool.execute.after', so a hook-augment
14041436
* that never exits would stall every grep/glob/read completion in the host
14051437
* session until the agent is restarted. The generated plugin must bound the
@@ -1472,6 +1504,7 @@ SUITE(agent_clients) {
14721504
RUN_TEST(client_adapter_opencode_sends_the_required_hook_event);
14731505
RUN_TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction);
14741506
RUN_TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077);
1507+
RUN_TEST(client_adapter_opencode_v2_compaction_note_goes_into_the_compaction_messages);
14751508
RUN_TEST(client_adapter_opencode_bounds_a_hung_hook_augment_child);
14761509
RUN_TEST(client_adapter_rejects_missing_binary_path);
14771510
}

0 commit comments

Comments
 (0)