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
This commit is contained in:
bigwalkdoe committed 2026-11-14 12:24:38 +05:00
1 parent d2ef268059
commit 9ce5cd92e7
2 files changed
+39 -2

No files matched your search

+31
View File
@@ -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(