Skip to content

Commit d3d0bc8

Browse files
committed
fix(cli): never create note-only content for an empty OpenCode 2 result
Tool.Result allows content: [], and for it the array branch of appendResult pushed a text part that held only the session note, so the note became the whole content of the tool result. Add a text part only when the array already holds a part; an empty array now falls through to the string output branch, or the result is returned unchanged. Pin it in a new agent_clients test: the part-adding push sits inside the non-empty array branch, ahead of the string output fallback, and no unguarded array branch is left. Refs #2204 Signed-off-by: Martin Vogel <[email protected]>
1 parent f531be2 commit d3d0bc8

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎src/cli/client_adapter.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,18 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
491491
* string/parts surface the model reads, output is the tool's schema-typed
492492
* value). Append to content first, then a string output; if neither text
493493
* surface exists, leave the result untouched so the tool's own output is
494-
* never hidden. The caller assigns the returned result onto event.result. */
494+
* never hidden. A parts array gets a new text part only when it already
495+
* holds a part: `content: []` is a valid result, and a part added there
496+
* would be the note alone, so an empty array falls through to the string
497+
* output (or the unchanged result). The caller assigns the returned result
498+
* onto event.result. */
495499
sb_append(&sb,
496500
"function appendResult(result, extra) {\n"
497501
" if (!result || typeof result !== 'object') return result;\n"
498502
" if (typeof result.content === 'string') {\n"
499503
" return { ...result, content: result.content + '\\n' + extra };\n"
500504
" }\n"
501-
" if (Array.isArray(result.content)) {\n"
505+
" if (Array.isArray(result.content) && result.content.length > 0) {\n"
502506
" const content = result.content.slice();\n"
503507
" for (let i = content.length - 1; i >= 0; i--) {\n"
504508
" if (content[i] && content[i].type === 'text') {\n"

‎tests/test_agent_clients.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,31 @@ TEST(client_adapter_opencode_v2_compaction_note_goes_into_the_compaction_message
14321432
PASS();
14331433
}
14341434

1435+
/* #2204: Tool.Result allows `content: []`. appendResult adds a text part to a
1436+
* parts array only when the array already holds a part; an empty array falls
1437+
* through to the string `output` (or the unchanged result), so the note never
1438+
* becomes the whole content of a tool result. */
1439+
TEST(client_adapter_opencode_v2_append_result_never_creates_note_only_content) {
1440+
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
1441+
ASSERT_NOT_NULL(js);
1442+
const char *parts =
1443+
strstr(js, "if (Array.isArray(result.content) && result.content.length > 0) {");
1444+
const char *add_part = strstr(js, "content.push({ type: 'text', text: extra });");
1445+
const char *output_fallback = strstr(js, "if (typeof result.output === 'string') {");
1446+
ASSERT_NOT_NULL(parts);
1447+
ASSERT_NOT_NULL(add_part);
1448+
ASSERT_NOT_NULL(output_fallback);
1449+
/* The only part-adding push sits inside the non-empty branch; the string
1450+
* output fallback, where `content: []` lands, comes after it. */
1451+
ASSERT_TRUE(parts < add_part);
1452+
ASSERT_TRUE(add_part < output_fallback);
1453+
ASSERT_NULL(strstr(add_part + 1, "content.push("));
1454+
/* No unguarded array branch is left. */
1455+
ASSERT_NULL(strstr(js, "if (Array.isArray(result.content)) {"));
1456+
free(js);
1457+
PASS();
1458+
}
1459+
14351460
/* augment() is awaited inline inside 'tool.execute.after', so a hook-augment
14361461
* that never exits would stall every grep/glob/read completion in the host
14371462
* session until the agent is restarted. The generated plugin must bound the
@@ -1505,6 +1530,7 @@ SUITE(agent_clients) {
15051530
RUN_TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction);
15061531
RUN_TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077);
15071532
RUN_TEST(client_adapter_opencode_v2_compaction_note_goes_into_the_compaction_messages);
1533+
RUN_TEST(client_adapter_opencode_v2_append_result_never_creates_note_only_content);
15081534
RUN_TEST(client_adapter_opencode_bounds_a_hung_hook_augment_child);
15091535
RUN_TEST(client_adapter_rejects_missing_binary_path);
15101536
}

0 commit comments

Comments
 (0)