From 9ce5cd92e7cce127bae04a7ee1d424d0b34de5fb Mon Sep 17 00:00:00 2001 From: bigwalkdoe Date: Sat, 14 Nov 2026 12:24:38 +0500 Subject: [PATCH] fix(opencode): guard hooks against undefined output to prevent compaction crash The shell.env and experimental.session.compacting hooks dereferenced output.env and output.prompt unconditionally. When OpenCode invokes a hook without an output object, this threw TypeError: undefined is not an object and aborted compaction entirely. - shell.env: early-return when output is undefined - experimental.session.compacting: guard the output.prompt/output.context branches so a missing output no-ops instead of throwing Add regression tests for both hooks covering the undefined-output case. Fixes #3934 --- .opencode/plugins/ecc-hooks.ts | 10 ++++++++-- tests/opencode-plugin-hooks.test.js | 31 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/.opencode/plugins/ecc-hooks.ts b/.opencode/plugins/ecc-hooks.ts index 86eaa7cdf..00b0ca76c 100644 --- a/.opencode/plugins/ecc-hooks.ts +++ b/.opencode/plugins/ecc-hooks.ts @@ -639,6 +639,10 @@ export const ECCHooksPlugin: ECCHooksPluginFn = async ({ * Action: Sets PROJECT_ROOT, PACKAGE_MANAGER, DETECTED_LANGUAGES, ECC_VERSION */ "shell.env": async (_input: { cwd: string }, output: { env: Record }) => { + // OpenCode may invoke this hook with no output object. Dereferencing + // output.env unconditionally below throws and aborts the shell environment + // hook, so bail out early instead (#3934). + if (!output) return const env: Record = { ECC_VERSION: getECCVersion(eccRoot), ECC_PLUGIN: "true", @@ -744,9 +748,11 @@ export const ECCHooksPlugin: ECCHooksPluginFn = async ({ ] // OpenCode requires output assignment and skips context when a prompt is set. - if (output.prompt !== undefined) { + // Guard against a missing output object so a hook invocation that omits it + // no-ops instead of throwing and aborting compaction (#3934). + if (output && output.prompt !== undefined) { output.prompt = [output.prompt, ...eccContext].join("\n\n") - } else { + } else if (output) { output.context = [...output.context, ...eccContext] } }, diff --git a/tests/opencode-plugin-hooks.test.js b/tests/opencode-plugin-hooks.test.js index dc3dcee23..b2a863933 100644 --- a/tests/opencode-plugin-hooks.test.js +++ b/tests/opencode-plugin-hooks.test.js @@ -347,6 +347,22 @@ async function main() { } }, ], + [ + "shell.env no-ops instead of throwing when the host omits the output object", + async () => withTempProject([], async (projectDir) => { + const client = createClient() + const $ = createFailingShell() + const hooks = await ECCHooksPlugin({ client, $, directory: projectDir }) + + // OpenCode may invoke the hook with no output object; the previous + // implementation dereferenced output.env unconditionally and aborted + // the shell environment hook with TypeError: undefined is not an + // object (#3934). + await hooks["shell.env"]({ cwd: projectDir }, undefined) + + assert.deepStrictEqual($.calls, [], "Expected no shell probes") + }), + ], [ "compacting appends ECC context without replacing the host compaction prompt", async () => withTempProject([], async (projectDir) => { @@ -388,6 +404,21 @@ async function main() { assert.deepStrictEqual($.calls, []) }), ], + [ + "compacting no-ops instead of throwing when the host omits the output object", + async () => withTempProject([], async (projectDir) => { + const client = createClient() + const $ = createFailingShell() + const hooks = await ECCHooksPlugin({ client, $, directory: projectDir }) + + // OpenCode may invoke the hook with no output object; the previous + // implementation dereferenced output.prompt unconditionally and aborted + // compaction with TypeError: undefined is not an object (#3934). + await hooks["experimental.session.compacting"]({ sessionID: "session-1" }, undefined) + + assert.deepStrictEqual($.calls, [], "Expected no shell probes") + }), + ], [ "permission.ask handles read-only tools correctly", async () => withTempProject(