diff --git a/.opencode/plugins/ecc-hooks.ts b/.opencode/plugins/ecc-hooks.ts index 75df39c22..e71e0b07b 100644 --- a/.opencode/plugins/ecc-hooks.ts +++ b/.opencode/plugins/ecc-hooks.ts @@ -21,7 +21,10 @@ * Hook Event Mapping (V1 key -> V2 registration): * - tool.execute.before -> ctx.tool.hook("execute.before") * - tool.execute.after -> ctx.tool.hook("execute.after") - * - file.edited / file.watcher / session.* / todo.updated + * (V2 tool id `shell` is matched as V1 `bash`) + * - file.edited -> completed write/edit in execute.after + * (V2 publishes no file.edited event) + * - file.watcher / session.* / todo.updated * -> ctx.event.subscribe() filtered by type * - shell.env -> ctx.shell.hook("create.before") * - experimental.session.compacting -> ctx.session.hook("compaction") @@ -111,12 +114,31 @@ export const ECCHooksPluginDefinition = { const { hooks, worktreePath } = runtime // Normalize a V2 tool-hook event into the V1 ToolInput shape so both - // surfaces share one implementation. - const toToolInput = (event: LooseRecord): ToolInput => ({ - tool: String(event.tool ?? event.name ?? ""), - callID: event.callID ?? event.id, - args: (event.input ?? event.args) as ToolArgs | undefined, - }) + // surfaces share one implementation. OpenCode 2 renamed the V1 `bash` + // tool to `shell` and passes `{ path }` to `write`/`edit`, so map both + // back to the V1 names the shared runtime matches on. + const toToolInput = (event: LooseRecord): ToolInput => { + const rawTool = String(event.tool ?? event.name ?? "") + const tool = rawTool === "shell" ? "bash" : rawTool + let args = (event.input ?? event.args) as ToolArgs | undefined + if ( + (tool === "write" || tool === "edit") && + args && typeof args === "object" && + typeof args.filePath !== "string" && typeof args.path === "string" + ) { + args = { ...args, filePath: args.path } + } + return { tool, callID: event.callID ?? event.id, args } + } + + // OpenCode 2's public event stream has no `file.edited`, so V1's + // post-edit hook (formatter, console.log warning, idle audit input) is + // driven from the completed write/edit tool call instead. + const editedPath = (input: ToolInput): string | null => { + if (input.tool !== "write" && input.tool !== "edit") return null + const p = input.args?.filePath ?? input.args?.path + return typeof p === "string" && p.trim() ? p : null + } logStartup(directory) @@ -147,15 +169,18 @@ export const ECCHooksPluginDefinition = { if (typeof ctx?.tool?.hook === "function") { registrations.push(await ctx.tool.hook("execute.before", safe("execute.before", (event: LooseRecord) => hooks["tool.execute.before"](toToolInput(event), {})))) registrations.push( - await ctx.tool.hook("execute.after", safe("execute.after", (event: LooseRecord) => + await ctx.tool.hook("execute.after", safe("execute.after", async (event: LooseRecord) => { + const input = toToolInput(event) // R2-1: hand the shared handler the tool output object directly. The // skill tracker reads error fields (isError/is_error/status/error) off // `tool_response` itself; a `{ result: … }` wrapper made failed skill // runs look like successes. - hooks["tool.execute.after"](toToolInput(event), event.status === "error" + await hooks["tool.execute.after"](input, event.status === "error" ? { is_error: true } - : event.result ?? event.output), - )), + : event.result ?? event.output) + const edited = event.status === "error" ? null : editedPath(input) + if (edited) await hooks["file.edited"]({ path: edited }) + })), ) } diff --git a/tests/opencode-plugin-hooks.test.js b/tests/opencode-plugin-hooks.test.js index 7560224c1..d1878783c 100644 --- a/tests/opencode-plugin-hooks.test.js +++ b/tests/opencode-plugin-hooks.test.js @@ -1109,6 +1109,71 @@ async function main() { await dispose3() }), ], + [ + "V2 maps the shell tool id and runs post-edit hooks from completed write/edit calls", + async () => withTempProject([], async (projectDir) => { + const mod = await loadPlugin() + const definition = mod.default + fs.mkdirSync(path.join(projectDir, "src"), { recursive: true }) + fs.writeFileSync(path.join(projectDir, "src", "a.ts"), "console.log('debug')\n") + + const previousProfile = process.env.ECC_HOOK_PROFILE + process.env.ECC_HOOK_PROFILE = "strict" + resetSpawns() + const lines = [] + const originals = { info: console.info, warn: console.warn, debug: console.debug } + console.info = (...args) => lines.push(args.join(" ")) + console.warn = (...args) => lines.push(args.join(" ")) + console.debug = () => {} + const ctx = createMockV2Context(projectDir) + let dispose = async () => {} + try { + dispose = await definition.setup(ctx) + + // OpenCode 2 names the V1 `bash` tool `shell`. + await ctx.state.handlers["tool.execute.before"]({ tool: "shell", id: "s1", input: { command: "git push origin main" } }) + assert.ok( + lines.some((line) => line.includes("Remember to review changes before pushing")), + "the git-push reminder must fire for the V2 shell tool", + ) + + // OpenCode 2 publishes no file.edited event; a completed edit with + // V2's `{ path }` input must still run the post-edit hook. + lines.length = 0 + await ctx.state.handlers["tool.execute.after"]({ + tool: "edit", id: "e1", input: { path: "src/a.ts" }, status: "completed", result: {}, + }) + assert.ok( + lines.some((line) => line.includes("console.log found in src/a.ts")), + "the console.log warning must fire for a completed V2 edit", + ) + assert.ok( + spawnCalls.some((call) => call.args.join(" ").includes("prettier")), + "the strict-profile formatter must run for a completed V2 edit", + ) + + // A failed edit must not run the post-edit hook. + lines.length = 0 + resetSpawns() + await ctx.state.handlers["tool.execute.after"]({ + tool: "edit", id: "e2", input: { path: "src/a.ts" }, status: "error", error: { message: "no match" }, + }) + assert.ok( + !lines.some((line) => line.includes("console.log found")), + "a failed V2 edit must not trigger the post-edit hook", + ) + assert.ok( + !spawnCalls.some((call) => call.args.join(" ").includes("prettier")), + "a failed V2 edit must not run the formatter", + ) + } finally { + await dispose() + Object.assign(console, originals) + restoreEnv({ ECC_HOOK_PROFILE: previousProfile }) + resetSpawns() + } + }), + ], [ "setup() rolls back registrations when a later domain fails (T10)", async () => withTempProject([], async (projectDir) => {