From a0d4f6c513e9ed95a29fd8e02832a929bc2b2c0a Mon Sep 17 00:00:00 2001 From: quark404 Date: Sun, 1 Nov 2026 07:22:38 +0000 Subject: [PATCH 1/6] fix(pi): restore args for placeholder-less commands Pi drops arguments for command templates that do not contain an $ARGUMENTS/$@/$N/${@} placeholder. This adapter now handles the input event before template expansion and rewrites such commands to the command body plus an explicit ARGUMENTS block, matching Claude Code. Also makes /ecc-doctor display immediately instead of being queued until the next prompt on Pi 1.x. Closes #3826 --- .pi/extensions/index.ts | 72 ++++++++++++++++++++++--- tests/pi/pi-extension-adapter.test.js | 77 +++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 8 deletions(-) diff --git a/.pi/extensions/index.ts b/.pi/extensions/index.ts index 241deabce..dc185c9be 100644 --- a/.pi/extensions/index.ts +++ b/.pi/extensions/index.ts @@ -70,7 +70,17 @@ interface BeforeAgentStartResult { systemPrompt?: string } +interface InputEvent { + text: string +} + +type InputEventResult = { action: "continue" } | { action: "transform"; text: string } + interface ExtensionAPI { + on( + event: "input", + handler: (event: InputEvent, ctx: ExtensionContext) => Promise | InputEventResult + ): void on( event: "session_start", handler: (event: SessionStartEvent, ctx: ExtensionContext) => Promise | void @@ -670,7 +680,52 @@ function buildDoctorReport(ctx: ExtensionContext): string { return lines.join("\n") } +/** Same placeholder set Pi's prompt-template substitution understands. */ +const ARG_PLACEHOLDER = /\$ARGUMENTS|\$@|\$\d|\$\{@/ + +const commandBodyCache = new Map() + +/** + * Body of an ECC command that has NO argument placeholder, or null. + * + * Claude Code appends `ARGUMENTS: ` when a command lacks `$ARGUMENTS`; + * Pi's template expansion silently drops the arguments instead. Most ECC + * commands rely on the Claude behaviour, so the adapter restores it. + */ +function loadPlaceholderlessCommand(name: string): string | null { + if (commandBodyCache.has(name)) { + return commandBodyCache.get(name) ?? null + } + let body: string | null = null + if (/^[A-Za-z0-9._-]+$/.test(name)) { + try { + const raw = fs.readFileSync(path.join(ECC_ROOT, "commands", `${name}.md`), "utf8") + const stripped = raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() + body = ARG_PLACEHOLDER.test(stripped) ? null : stripped + } catch { + body = null + } + } + commandBodyCache.set(name, body) + return body +} + export default function (pi: ExtensionAPI): void { + pi.on("input", event => { + if (isDisabledByEnv(process.env.ECC_PI_COMMAND_ARGS)) { + return { action: "continue" } + } + const match = event.text.match(/^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/) + if (!match) { + return { action: "continue" } + } + const body = loadPlaceholderlessCommand(match[1]) + if (!body) { + return { action: "continue" } + } + return { action: "transform", text: `${body}\n\nARGUMENTS: ${match[2].trim()}` } + }) + /** * ECC's SessionStart hook returns context for the model, but Pi has no * equivalent of Claude Code's `additionalContext` field. It is held here and @@ -753,14 +808,15 @@ export default function (pi: ExtensionAPI): void { pi.registerCommand("ecc-doctor", { description: "Report ECC adapter status: package root, canonical resources, hooks, companions", handler: async (_args, ctx) => { - pi.sendMessage( - { - customType: "ecc-doctor", - content: buildDoctorReport(ctx), - display: true, - }, - { deliverAs: "nextTurn" } - ) + // No deliverAs: "nextTurn" here. In Pi 1.x that queues the message until + // the user's next prompt, so the report never appears when the command runs. + // Without options (and not streaming) Pi appends and displays it immediately + // without triggering a model turn. + pi.sendMessage({ + customType: "ecc-doctor", + content: buildDoctorReport(ctx), + display: true, + }) }, }) } diff --git a/tests/pi/pi-extension-adapter.test.js b/tests/pi/pi-extension-adapter.test.js index b900fc7d6..fb3bcacf9 100644 --- a/tests/pi/pi-extension-adapter.test.js +++ b/tests/pi/pi-extension-adapter.test.js @@ -1665,6 +1665,83 @@ async function main() { "systemPrompt-override return in before_agent_start" ) }], + + ["registers the Pi input event and restores placeholder-less command arguments", () => { + assert.ok( + extensionSource.includes('pi.on("input"'), + "expected .pi/extensions/index.ts to register an input handler via pi.on(...); " + + "Pi runs input handlers before prompt-template expansion, which is the only " + + "place ECC can restore the ARGUMENTS payload Pi otherwise drops" + ) + assert.ok( + extensionSource.includes("ECC_PI_COMMAND_ARGS"), + "expected .pi/extensions/index.ts to honor an ECC_PI_COMMAND_ARGS opt-out for " + + "argument restoration; an irreversible transform would make the workaround mandatory" + ) + assert.ok( + extensionSource.includes('action: "transform"') && + extensionSource.includes('action: "continue"'), + "expected the input handler to return Pi's action result shape, either " + + "transform (replace the command with its body plus ARGUMENTS) or continue " + + "(leave Pi's normal expansion untouched)" + ) + assert.ok( + extensionSource.includes("ARG_PLACEHOLDER = /\\$ARGUMENTS|\\$@|\\$\\d|\\$\\{@/"), + "expected the placeholder test to recognize $ARGUMENTS, $@, positional $N, and ${@}; " + + "missing any one of Pi's supported placeholders would transform commands Pi already expands" + ) + assert.ok( + extensionSource.includes('path.join(ECC_ROOT, "commands"') && + extensionSource.includes("${name}.md"), + "expected the adapter to read the canonical commands/ directory relative to ECC_ROOT; " + + "reading from cwd would break global installs exactly like the original hook-root defect" + ) + assert.ok( + extensionSource.includes("commandBodyCache"), + "expected the placeholder-less command lookup to cache command bodies; the input " + + "handler runs on every prompt and should not re-read command markdown every time" + ) + }], + + ["placeholder-less command transformation (behavioral mirror): /plan args appends ARGUMENTS, /sessions stays untouched", () => { + const placeholder = /\$ARGUMENTS|\$@|\$\d|\$\{@/ + const stripFrontmatter = raw => raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() + + const planRaw = fs.readFileSync(path.join(repoRoot, "commands", "plan.md"), "utf8") + const planBody = stripFrontmatter(planRaw) + assert.ok( + planBody && !placeholder.test(planBody), + "commands/plan.md must be placeholder-less in this repo for the test to be meaningful; " + + "if it gains a placeholder the transformation should no longer apply" + ) + + const input = "/plan add a hello world CLI" + const args = "add a hello world CLI" + assert.ok( + /^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/.test(input), + "the command-with-arguments shape should match the handler regex" + ) + const transformed = `${planBody}\n\nARGUMENTS: ${args}` + assert.ok( + transformed.includes("ARGUMENTS: add a hello world CLI"), + "transforming a placeholder-less command should append ARGUMENTS with the user text" + ) + assert.ok( + transformed.startsWith("# Plan Command"), + "the transformed input should be the command body itself, not the slash command name" + ) + + const sessionsRaw = fs.readFileSync(path.join(repoRoot, "commands", "sessions.md"), "utf8") + const sessionsBody = stripFrontmatter(sessionsRaw) + assert.ok( + placeholder.test(sessionsBody), + "commands/sessions.md must contain a placeholder in this repo for the test to be meaningful" + ) + assert.ok( + placeholder.test(sessionsBody), + "a command with $ARGUMENTS/$@/$N/${@} must fall through to Pi unchanged" + ) + }], ] for (const [name, fn] of tests) { From 2c12e43ea3465be4406318296ec7f6550715e6e1 Mon Sep 17 00:00:00 2001 From: quark404 Date: Sun, 1 Nov 2026 08:56:29 +0000 Subject: [PATCH 2/6] test(pi): exercise real input handler pass-through paths --- .pi/extensions/index.ts | 47 ++---------- .pi/extensions/input-args.js | 67 ++++++++++++++++ tests/pi/pi-extension-adapter.test.js | 105 ++++++++++++++++++-------- 3 files changed, 147 insertions(+), 72 deletions(-) create mode 100644 .pi/extensions/input-args.js diff --git a/.pi/extensions/index.ts b/.pi/extensions/index.ts index dc185c9be..01f04d55a 100644 --- a/.pi/extensions/index.ts +++ b/.pi/extensions/index.ts @@ -29,6 +29,7 @@ import * as fs from "node:fs" import * as os from "node:os" import * as path from "node:path" import { resolveHookRuntime } from "./hook-runtime.js" +import { handlePiInput } from "./input-args.js" /** * Minimal structural types mirroring `@earendil-works/pi-coding-agent`. @@ -680,50 +681,16 @@ function buildDoctorReport(ctx: ExtensionContext): string { return lines.join("\n") } -/** Same placeholder set Pi's prompt-template substitution understands. */ -const ARG_PLACEHOLDER = /\$ARGUMENTS|\$@|\$\d|\$\{@/ - const commandBodyCache = new Map() -/** - * Body of an ECC command that has NO argument placeholder, or null. - * - * Claude Code appends `ARGUMENTS: ` when a command lacks `$ARGUMENTS`; - * Pi's template expansion silently drops the arguments instead. Most ECC - * commands rely on the Claude behaviour, so the adapter restores it. - */ -function loadPlaceholderlessCommand(name: string): string | null { - if (commandBodyCache.has(name)) { - return commandBodyCache.get(name) ?? null - } - let body: string | null = null - if (/^[A-Za-z0-9._-]+$/.test(name)) { - try { - const raw = fs.readFileSync(path.join(ECC_ROOT, "commands", `${name}.md`), "utf8") - const stripped = raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() - body = ARG_PLACEHOLDER.test(stripped) ? null : stripped - } catch { - body = null - } - } - commandBodyCache.set(name, body) - return body -} - export default function (pi: ExtensionAPI): void { pi.on("input", event => { - if (isDisabledByEnv(process.env.ECC_PI_COMMAND_ARGS)) { - return { action: "continue" } - } - const match = event.text.match(/^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/) - if (!match) { - return { action: "continue" } - } - const body = loadPlaceholderlessCommand(match[1]) - if (!body) { - return { action: "continue" } - } - return { action: "transform", text: `${body}\n\nARGUMENTS: ${match[2].trim()}` } + return handlePiInput({ + text: event.text, + eccRoot: ECC_ROOT, + env: process.env, + cache: commandBodyCache, + }) }) /** diff --git a/.pi/extensions/input-args.js b/.pi/extensions/input-args.js new file mode 100644 index 000000000..e80cb28e1 --- /dev/null +++ b/.pi/extensions/input-args.js @@ -0,0 +1,67 @@ +const fs = require("node:fs") +const path = require("node:path") + +/** Same placeholder set Pi's prompt-template substitution understands. */ +const ARG_PLACEHOLDER = /\$ARGUMENTS|\$@|\$\d|\$\{@/ + +/** Values ECC treats as "off" across its existing environment switches. */ +const DISABLED_VALUES = new Set(["0", "false", "off", "none", "disabled"]) + +function isDisabledByEnv(value) { + return typeof value === "string" && DISABLED_VALUES.has(value.trim().toLowerCase()) +} + +/** + * Body of an ECC command that has NO argument placeholder, or null. + * + * Claude Code appends `ARGUMENTS: ` when a command lacks `$ARGUMENTS`; + * Pi's template expansion silently drops the arguments instead. Most ECC + * commands rely on the Claude behaviour, so the adapter restores it. + */ +function loadPlaceholderlessCommand(name, eccRoot, cache) { + if (cache.has(name)) { + return cache.get(name) ?? null + } + let body = null + if (/^[A-Za-z0-9._-]+$/.test(name)) { + try { + const raw = fs.readFileSync(path.join(eccRoot, "commands", `${name}.md`), "utf8") + const stripped = raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() + body = ARG_PLACEHOLDER.test(stripped) ? null : stripped + } catch { + body = null + } + } + cache.set(name, body) + return body +} + +/** + * Pi input-handler transform shared by the TypeScript extension and tests. + * + * Keep this in a CommonJS module so the real registered handler can be loaded + * and exercised by the plain Node test suite; the extension itself delegates + * directly to this function. + */ +function handlePiInput({ text, eccRoot, env, cache }) { + if (isDisabledByEnv(env.ECC_PI_COMMAND_ARGS)) { + return { action: "continue" } + } + const match = text.match(/^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/) + if (!match) { + return { action: "continue" } + } + const body = loadPlaceholderlessCommand(match[1], eccRoot, cache) + if (!body) { + return { action: "continue" } + } + return { action: "transform", text: `${body}\n\nARGUMENTS: ${match[2].trim()}` } +} + +module.exports = { + ARG_PLACEHOLDER, + DISABLED_VALUES, + isDisabledByEnv, + loadPlaceholderlessCommand, + handlePiInput, +} diff --git a/tests/pi/pi-extension-adapter.test.js b/tests/pi/pi-extension-adapter.test.js index fb3bcacf9..31da3cbca 100644 --- a/tests/pi/pi-extension-adapter.test.js +++ b/tests/pi/pi-extension-adapter.test.js @@ -48,6 +48,9 @@ const { spawnSync, execFile } = require("child_process") const { resolveHookRuntime } = require( path.join(__dirname, "..", "..", ".pi", "extensions", "hook-runtime.js") ) +const { handlePiInput } = require( + path.join(__dirname, "..", "..", ".pi", "extensions", "input-args.js") +) /** Run a single adapter test and report the result. */ async function runTest(name, fn) { @@ -352,6 +355,8 @@ async function main() { const repoRoot = path.join(__dirname, "..", "..") const extensionPath = path.join(repoRoot, ".pi", "extensions", "index.ts") const extensionSource = fs.readFileSync(extensionPath, "utf8") + const inputArgsPath = path.join(repoRoot, ".pi", "extensions", "input-args.js") + const inputArgsSource = fs.readFileSync(inputArgsPath, "utf8") const tests = [ // ---- Group 1: source contract ------------------------------------- @@ -1666,7 +1671,7 @@ async function main() { ) }], - ["registers the Pi input event and restores placeholder-less command arguments", () => { + ["registers the Pi input event and delegates to the testable input-args module", () => { assert.ok( extensionSource.includes('pi.on("input"'), "expected .pi/extensions/index.ts to register an input handler via pi.on(...); " + @@ -1674,72 +1679,108 @@ async function main() { "place ECC can restore the ARGUMENTS payload Pi otherwise drops" ) assert.ok( - extensionSource.includes("ECC_PI_COMMAND_ARGS"), - "expected .pi/extensions/index.ts to honor an ECC_PI_COMMAND_ARGS opt-out for " + + extensionSource.includes("handlePiInput") && + extensionSource.includes('from "./input-args.js"'), + "expected .pi/extensions/index.ts to delegate the input handler to input-args.js " + + "so tests can load and exercise the real handler rather than a copy" + ) + assert.ok( + inputArgsSource.includes("ECC_PI_COMMAND_ARGS"), + "expected input-args.js to honor an ECC_PI_COMMAND_ARGS opt-out for " + "argument restoration; an irreversible transform would make the workaround mandatory" ) assert.ok( - extensionSource.includes('action: "transform"') && - extensionSource.includes('action: "continue"'), + inputArgsSource.includes('action: "transform"') && + inputArgsSource.includes('action: "continue"'), "expected the input handler to return Pi's action result shape, either " + "transform (replace the command with its body plus ARGUMENTS) or continue " + "(leave Pi's normal expansion untouched)" ) assert.ok( - extensionSource.includes("ARG_PLACEHOLDER = /\\$ARGUMENTS|\\$@|\\$\\d|\\$\\{@/"), + inputArgsSource.includes("ARG_PLACEHOLDER = /\\$ARGUMENTS|\\$@|\\$\\d|\\$\\{@/"), "expected the placeholder test to recognize $ARGUMENTS, $@, positional $N, and ${@}; " + "missing any one of Pi's supported placeholders would transform commands Pi already expands" ) assert.ok( - extensionSource.includes('path.join(ECC_ROOT, "commands"') && - extensionSource.includes("${name}.md"), - "expected the adapter to read the canonical commands/ directory relative to ECC_ROOT; " + + inputArgsSource.includes('path.join(eccRoot, "commands"') && + inputArgsSource.includes("${name}.md"), + "expected input-args.js to read the canonical commands/ directory relative to eccRoot; " + "reading from cwd would break global installs exactly like the original hook-root defect" ) assert.ok( - extensionSource.includes("commandBodyCache"), + inputArgsSource.includes("cache.set(name, body)"), "expected the placeholder-less command lookup to cache command bodies; the input " + "handler runs on every prompt and should not re-read command markdown every time" ) }], - ["placeholder-less command transformation (behavioral mirror): /plan args appends ARGUMENTS, /sessions stays untouched", () => { - const placeholder = /\$ARGUMENTS|\$@|\$\d|\$\{@/ + ["input handler transforms /plan args using the real command body", () => { const stripFrontmatter = raw => raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() - const planRaw = fs.readFileSync(path.join(repoRoot, "commands", "plan.md"), "utf8") const planBody = stripFrontmatter(planRaw) assert.ok( - planBody && !placeholder.test(planBody), + planBody && !/\$ARGUMENTS|\$@|\$\d|\$\{@/.test(planBody), "commands/plan.md must be placeholder-less in this repo for the test to be meaningful; " + "if it gains a placeholder the transformation should no longer apply" ) - const input = "/plan add a hello world CLI" - const args = "add a hello world CLI" + const result = handlePiInput({ + text: "/plan add a hello world CLI", + eccRoot: repoRoot, + env: {}, + cache: new Map(), + }) + + assert.strictEqual(result.action, "transform") assert.ok( - /^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/.test(input), - "the command-with-arguments shape should match the handler regex" - ) - const transformed = `${planBody}\n\nARGUMENTS: ${args}` - assert.ok( - transformed.includes("ARGUMENTS: add a hello world CLI"), + result.text.includes("ARGUMENTS: add a hello world CLI"), "transforming a placeholder-less command should append ARGUMENTS with the user text" ) assert.ok( - transformed.startsWith("# Plan Command"), - "the transformed input should be the command body itself, not the slash command name" + result.text.startsWith(planBody.split("\n")[0]), + "the transformed input should start with the real command body, not the slash command name" + ) + }], + + ["input handler passes /sessions through because the command has a placeholder", () => { + const stripFrontmatter = raw => raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() + const sessionsRaw = fs.readFileSync(path.join(repoRoot, "commands", "sessions.md"), "utf8") + assert.ok( + /\$ARGUMENTS|\$@|\$\d|\$\{@/.test(stripFrontmatter(sessionsRaw)), + "commands/sessions.md must contain a placeholder in this repo for the pass-through test to be meaningful" ) - const sessionsRaw = fs.readFileSync(path.join(repoRoot, "commands", "sessions.md"), "utf8") - const sessionsBody = stripFrontmatter(sessionsRaw) - assert.ok( - placeholder.test(sessionsBody), - "commands/sessions.md must contain a placeholder in this repo for the test to be meaningful" + assert.deepStrictEqual( + handlePiInput({ + text: "/sessions recent", + eccRoot: repoRoot, + env: {}, + cache: new Map(), + }), + { action: "continue" } ) - assert.ok( - placeholder.test(sessionsBody), - "a command with $ARGUMENTS/$@/$N/${@} must fall through to Pi unchanged" + }], + + ["input handler passes through bare and unknown commands", () => { + assert.deepStrictEqual( + handlePiInput({ text: "just a normal prompt", eccRoot: repoRoot, env: {}, cache: new Map() }), + { action: "continue" } + ) + assert.deepStrictEqual( + handlePiInput({ text: "/not-a-real-ecc-command args", eccRoot: repoRoot, env: {}, cache: new Map() }), + { action: "continue" } + ) + }], + + ["input handler honors ECC_PI_COMMAND_ARGS=off", () => { + assert.deepStrictEqual( + handlePiInput({ + text: "/plan add a hello world CLI", + eccRoot: repoRoot, + env: { ECC_PI_COMMAND_ARGS: "off" }, + cache: new Map(), + }), + { action: "continue" } ) }], ] From 818fc7306d909cfde598fa9845a2c63e8352560c Mon Sep 17 00:00:00 2001 From: quark404 Date: Sun, 1 Nov 2026 09:40:51 +0000 Subject: [PATCH 3/6] fix(pi): match Pi placeholders and require active ECC prompt Recognize Pi's braced positional/slice placeholders, gate restoration on pi.getCommands() provenance, and make the command body cache immutable. Extend Pi adapter tests for the new placeholder forms and pass-through cases. --- .pi/extensions/index.ts | 18 ++-- .pi/extensions/input-args.js | 103 ++++++++++++++---- tests/pi/pi-extension-adapter.test.js | 149 +++++++++++++++++++++++--- 3 files changed, 229 insertions(+), 41 deletions(-) diff --git a/.pi/extensions/index.ts b/.pi/extensions/index.ts index 01f04d55a..d67cc3219 100644 --- a/.pi/extensions/index.ts +++ b/.pi/extensions/index.ts @@ -29,7 +29,7 @@ import * as fs from "node:fs" import * as os from "node:os" import * as path from "node:path" import { resolveHookRuntime } from "./hook-runtime.js" -import { handlePiInput } from "./input-args.js" +import { createCommandBodyReader, handlePiInput } from "./input-args.js" /** * Minimal structural types mirroring `@earendil-works/pi-coding-agent`. @@ -71,13 +71,14 @@ interface BeforeAgentStartResult { systemPrompt?: string } -interface InputEvent { - text: string -} +interface PiSlashCommandInfo { name: string; source: "extension" | "prompt" | "skill"; sourceInfo: { path: string } } + +interface InputEvent { text: string; source: "interactive" | "rpc" | "extension"; streamingBehavior?: "steer" | "followUp" } type InputEventResult = { action: "continue" } | { action: "transform"; text: string } interface ExtensionAPI { + getCommands(): PiSlashCommandInfo[] on( event: "input", handler: (event: InputEvent, ctx: ExtensionContext) => Promise | InputEventResult @@ -681,15 +682,20 @@ function buildDoctorReport(ctx: ExtensionContext): string { return lines.join("\n") } -const commandBodyCache = new Map() +const getCommandBody = createCommandBodyReader(ECC_ROOT) export default function (pi: ExtensionAPI): void { pi.on("input", event => { + if (event.source === "extension" || event.streamingBehavior === "steer" || typeof pi.getCommands !== "function") { + return { action: "continue" } + } + return handlePiInput({ text: event.text, eccRoot: ECC_ROOT, env: process.env, - cache: commandBodyCache, + activeCommands: pi.getCommands(), + getBody: getCommandBody, }) }) diff --git a/.pi/extensions/input-args.js b/.pi/extensions/input-args.js index e80cb28e1..9f2145200 100644 --- a/.pi/extensions/input-args.js +++ b/.pi/extensions/input-args.js @@ -1,8 +1,14 @@ const fs = require("node:fs") const path = require("node:path") -/** Same placeholder set Pi's prompt-template substitution understands. */ -const ARG_PLACEHOLDER = /\$ARGUMENTS|\$@|\$\d|\$\{@/ +/** + * Same placeholder set Pi's `substituteArgs` understands. + * + * Pi expands every form below, so a command body containing any of them must + * be left for Pi to expand rather than treated as placeholder-less: + * $ARGUMENTS, $@, $1, ${1:-default}, ${@:N}, ${@:N:L} + */ +const ARG_PLACEHOLDER = /\$\{(\d+|ARGUMENTS|@):-[^}]*\}|\$\{@:\d+(?::\d+)?\}|\$(ARGUMENTS|@|\d+)/ /** Values ECC treats as "off" across its existing environment switches. */ const DISABLED_VALUES = new Set(["0", "false", "off", "none", "disabled"]) @@ -11,6 +17,10 @@ function isDisabledByEnv(value) { return typeof value === "string" && DISABLED_VALUES.has(value.trim().toLowerCase()) } +function stripFrontmatter(raw) { + return raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() +} + /** * Body of an ECC command that has NO argument placeholder, or null. * @@ -18,22 +28,48 @@ function isDisabledByEnv(value) { * Pi's template expansion silently drops the arguments instead. Most ECC * commands rely on the Claude behaviour, so the adapter restores it. */ -function loadPlaceholderlessCommand(name, eccRoot, cache) { - if (cache.has(name)) { - return cache.get(name) ?? null +function loadPlaceholderlessCommand(name, eccRoot, commandPath) { + if (!/^[A-Za-z0-9._-]+$/.test(name)) { + return null } - let body = null - if (/^[A-Za-z0-9._-]+$/.test(name)) { - try { - const raw = fs.readFileSync(path.join(eccRoot, "commands", `${name}.md`), "utf8") - const stripped = raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() - body = ARG_PLACEHOLDER.test(stripped) ? null : stripped - } catch { - body = null - } + + const resolvedPath = commandPath || path.join(eccRoot, "commands", `${name}.md`) + try { + const raw = fs.readFileSync(resolvedPath, "utf8") + const stripped = stripFrontmatter(raw) + return ARG_PLACEHOLDER.test(stripped) ? null : stripped + } catch { + return null } - cache.set(name, body) - return body +} + +/** + * Returns the canonical ECC command path only when Pi's active prompt for + * `name` resolves to ECC's canonical `commands/.md`. + * + * The input event fires before Pi expands prompt templates and does not expose + * the selected template directly. `pi.getCommands()` does expose the active + * prompt list (including source metadata), so this is the provenance check + * that keeps ECC from overriding a disabled, differently resolved, or + * extension-registered command with the same name. + */ +function activeEccPromptPath(name, eccRoot, activeCommands) { + if (!/^[A-Za-z0-9._-]+$/.test(name)) { + return null + } + + const activePrompt = (activeCommands || []).find( + command => command && command.name === name && command.source === "prompt" + ) + if (!activePrompt || !activePrompt.sourceInfo) { + return null + } + + const expectedPath = path.resolve(path.join(eccRoot, "commands", `${name}.md`)) + if (path.resolve(activePrompt.sourceInfo.path || "") !== expectedPath) { + return null + } + return expectedPath } /** @@ -43,25 +79,56 @@ function loadPlaceholderlessCommand(name, eccRoot, cache) { * and exercised by the plain Node test suite; the extension itself delegates * directly to this function. */ -function handlePiInput({ text, eccRoot, env, cache }) { +function handlePiInput({ text, eccRoot, env, activeCommands, getBody }) { if (isDisabledByEnv(env.ECC_PI_COMMAND_ARGS)) { return { action: "continue" } } + const match = text.match(/^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/) if (!match) { return { action: "continue" } } - const body = loadPlaceholderlessCommand(match[1], eccRoot, cache) + + const commandPath = activeEccPromptPath(match[1], eccRoot, activeCommands) + if (!commandPath) { + return { action: "continue" } + } + + const body = getBody + ? getBody(match[1], commandPath) + : loadPlaceholderlessCommand(match[1], eccRoot, commandPath) if (!body) { return { action: "continue" } } + return { action: "transform", text: `${body}\n\nARGUMENTS: ${match[2].trim()}` } } +/** + * Creates a body reader with its own cache. + * + * The returned function returns a fresh Map on each new lookup rather than + * mutating the previous cache, satisfying ECC's immutability rule. + */ +function createCommandBodyReader(eccRoot) { + let cache = new Map() + return name => { + if (cache.has(name)) { + return cache.get(name) ?? null + } + const body = loadPlaceholderlessCommand(name, eccRoot) + cache = new Map(cache).set(name, body) + return body + } +} + module.exports = { ARG_PLACEHOLDER, DISABLED_VALUES, isDisabledByEnv, + stripFrontmatter, loadPlaceholderlessCommand, + createCommandBodyReader, + activeEccPromptPath, handlePiInput, } diff --git a/tests/pi/pi-extension-adapter.test.js b/tests/pi/pi-extension-adapter.test.js index 31da3cbca..5236f52ae 100644 --- a/tests/pi/pi-extension-adapter.test.js +++ b/tests/pi/pi-extension-adapter.test.js @@ -48,7 +48,12 @@ const { spawnSync, execFile } = require("child_process") const { resolveHookRuntime } = require( path.join(__dirname, "..", "..", ".pi", "extensions", "hook-runtime.js") ) -const { handlePiInput } = require( +const { + ARG_PLACEHOLDER, + loadPlaceholderlessCommand, + createCommandBodyReader, + handlePiInput, +} = require( path.join(__dirname, "..", "..", ".pi", "extensions", "input-args.js") ) @@ -358,6 +363,12 @@ async function main() { const inputArgsPath = path.join(repoRoot, ".pi", "extensions", "input-args.js") const inputArgsSource = fs.readFileSync(inputArgsPath, "utf8") + const eccPrompt = name => ({ + name, + source: "prompt", + sourceInfo: { path: path.join(repoRoot, "commands", `${name}.md`) }, + }) + const tests = [ // ---- Group 1: source contract ------------------------------------- @@ -1684,6 +1695,17 @@ async function main() { "expected .pi/extensions/index.ts to delegate the input handler to input-args.js " + "so tests can load and exercise the real handler rather than a copy" ) + assert.ok( + extensionSource.includes('event.source === "extension"'), + "expected the registered handler to skip extension-injected input; transforming " + + "an extension message as if it were user input can double-expand ECC commands" + ) + assert.ok( + extensionSource.includes("pi.getCommands()") && + extensionSource.includes("activeCommands:"), + "expected the registered handler to resolve active commands via pi.getCommands() " + + "and pass them into the testable input-args handler for provenance checks" + ) assert.ok( inputArgsSource.includes("ECC_PI_COMMAND_ARGS"), "expected input-args.js to honor an ECC_PI_COMMAND_ARGS opt-out for " + @@ -1697,9 +1719,12 @@ async function main() { "(leave Pi's normal expansion untouched)" ) assert.ok( - inputArgsSource.includes("ARG_PLACEHOLDER = /\\$ARGUMENTS|\\$@|\\$\\d|\\$\\{@/"), - "expected the placeholder test to recognize $ARGUMENTS, $@, positional $N, and ${@}; " + - "missing any one of Pi's supported placeholders would transform commands Pi already expands" + inputArgsSource.includes(":-[^}]*") && + inputArgsSource.includes("(?::\\d+)?") && + inputArgsSource.includes("$(ARGUMENTS|@|\\d+)"), + "expected ARG_PLACEHOLDER to match Pi's full placeholder set, including " + + "${1:-default}, ${@:N}, and ${@:N:L}; missing those would transform commands " + + "Pi already expands" ) assert.ok( inputArgsSource.includes('path.join(eccRoot, "commands"') && @@ -1708,18 +1733,21 @@ async function main() { "reading from cwd would break global installs exactly like the original hook-root defect" ) assert.ok( - inputArgsSource.includes("cache.set(name, body)"), - "expected the placeholder-less command lookup to cache command bodies; the input " + - "handler runs on every prompt and should not re-read command markdown every time" + inputArgsSource.includes("new Map(cache).set(name, body)"), + "expected the command body cache to replace the old Map rather than mutate it; " + + "ECC's immutability rule forbids mutating an existing cache object" ) }], - ["input handler transforms /plan args using the real command body", () => { - const stripFrontmatter = raw => raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() + ["input handler transforms /plan args when Pi's active prompt resolves to ECC", () => { const planRaw = fs.readFileSync(path.join(repoRoot, "commands", "plan.md"), "utf8") - const planBody = stripFrontmatter(planRaw) + const planBody = ARG_PLACEHOLDER.test( + planRaw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() + ) + ? "" + : planRaw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() assert.ok( - planBody && !/\$ARGUMENTS|\$@|\$\d|\$\{@/.test(planBody), + planBody, "commands/plan.md must be placeholder-less in this repo for the test to be meaningful; " + "if it gains a placeholder the transformation should no longer apply" ) @@ -1728,7 +1756,7 @@ async function main() { text: "/plan add a hello world CLI", eccRoot: repoRoot, env: {}, - cache: new Map(), + activeCommands: [eccPrompt("plan")], }) assert.strictEqual(result.action, "transform") @@ -1746,7 +1774,7 @@ async function main() { const stripFrontmatter = raw => raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim() const sessionsRaw = fs.readFileSync(path.join(repoRoot, "commands", "sessions.md"), "utf8") assert.ok( - /\$ARGUMENTS|\$@|\$\d|\$\{@/.test(stripFrontmatter(sessionsRaw)), + ARG_PLACEHOLDER.test(stripFrontmatter(sessionsRaw)), "commands/sessions.md must contain a placeholder in this repo for the pass-through test to be meaningful" ) @@ -1755,7 +1783,7 @@ async function main() { text: "/sessions recent", eccRoot: repoRoot, env: {}, - cache: new Map(), + activeCommands: [eccPrompt("sessions")], }), { action: "continue" } ) @@ -1763,11 +1791,21 @@ async function main() { ["input handler passes through bare and unknown commands", () => { assert.deepStrictEqual( - handlePiInput({ text: "just a normal prompt", eccRoot: repoRoot, env: {}, cache: new Map() }), + handlePiInput({ + text: "just a normal prompt", + eccRoot: repoRoot, + env: {}, + activeCommands: [], + }), { action: "continue" } ) assert.deepStrictEqual( - handlePiInput({ text: "/not-a-real-ecc-command args", eccRoot: repoRoot, env: {}, cache: new Map() }), + handlePiInput({ + text: "/not-a-real-ecc-command args", + eccRoot: repoRoot, + env: {}, + activeCommands: [eccPrompt("plan")], + }), { action: "continue" } ) }], @@ -1778,11 +1816,88 @@ async function main() { text: "/plan add a hello world CLI", eccRoot: repoRoot, env: { ECC_PI_COMMAND_ARGS: "off" }, - cache: new Map(), + activeCommands: [eccPrompt("plan")], }), { action: "continue" } ) }], + + ["placeholder detection recognizes Pi's braced positional and slice forms", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "ecc-pi-args-")) + const commandsDir = path.join(root, "commands") + fs.mkdirSync(commandsDir, { recursive: true }) + + try { + fs.writeFileSync( + path.join(commandsDir, "braced.md"), + "---\ndescription: braced\n---\n${1:-default} plus ${@:2:3}\n" + ) + assert.strictEqual( + loadPlaceholderlessCommand("braced", root), + null, + "${1:-default} and ${@:2:3} are placeholders Pi expands, not placeholder-less bodies" + ) + + fs.writeFileSync( + path.join(commandsDir, "plain.md"), + "---\ndescription: plain\n---\nno placeholders here\n" + ) + assert.strictEqual( + loadPlaceholderlessCommand("plain", root), + "no placeholders here", + "a body without placeholders should be returned for argument restoration" + ) + } finally { + fs.rmSync(root, { recursive: true, force: true }) + } + }], + + ["input handler leaves disabled, missing, and differently resolved prompts unchanged", () => { + const otherRoot = fs.mkdtempSync(path.join(os.tmpdir(), "ecc-pi-other-")) + const otherCommands = path.join(otherRoot, "commands") + fs.mkdirSync(otherCommands, { recursive: true }) + const otherPlan = path.join(otherCommands, "plan.md") + fs.writeFileSync(otherPlan, "---\ndescription: user plan\n---\nuser plan body\n") + + try { + assert.deepStrictEqual( + handlePiInput({ + text: "/plan add a hello world CLI", + eccRoot: repoRoot, + env: {}, + activeCommands: [], + }), + { action: "continue" }, + "a disabled or missing Pi prompt must not be overridden by ECC's canonical file" + ) + assert.deepStrictEqual( + handlePiInput({ + text: "/plan add a hello world CLI", + eccRoot: repoRoot, + env: {}, + activeCommands: [{ name: "plan", source: "prompt", sourceInfo: { path: otherPlan } }], + }), + { action: "continue" }, + "a same-named prompt resolved from outside ECC must be left for Pi to expand" + ) + } finally { + fs.rmSync(otherRoot, { recursive: true, force: true }) + } + }], + + ["command body cache replaces rather than mutates its previous Map", () => { + const readBody = createCommandBodyReader(repoRoot) + const first = readBody("plan") + assert.ok( + typeof first === "string" && first.includes("# Plan Command"), + "the body reader should return the real canonical plan body" + ) + assert.strictEqual( + readBody("plan"), + first, + "the cache should return the same body on subsequent lookups" + ) + }], ] for (const [name, fn] of tests) { From c8cbb0ee05c00a51a4cf60623c02ee9a80344184 Mon Sep 17 00:00:00 2001 From: quark404 Date: Sun, 1 Nov 2026 10:11:28 +0000 Subject: [PATCH 4/6] refactor(pi): make registered input handler directly testable Extract the Pi input-event callback into input-args.js and add behavioral coverage for its guard branches and transform path. --- .pi/extensions/index.ts | 14 ++---- .pi/extensions/input-args.js | 27 +++++++++++ tests/pi/pi-extension-adapter.test.js | 69 +++++++++++++++++++++++---- 3 files changed, 92 insertions(+), 18 deletions(-) diff --git a/.pi/extensions/index.ts b/.pi/extensions/index.ts index d67cc3219..c459dfe05 100644 --- a/.pi/extensions/index.ts +++ b/.pi/extensions/index.ts @@ -29,7 +29,7 @@ import * as fs from "node:fs" import * as os from "node:os" import * as path from "node:path" import { resolveHookRuntime } from "./hook-runtime.js" -import { createCommandBodyReader, handlePiInput } from "./input-args.js" +import { createCommandBodyReader, handlePiInputEvent } from "./input-args.js" /** * Minimal structural types mirroring `@earendil-works/pi-coding-agent`. @@ -685,19 +685,13 @@ function buildDoctorReport(ctx: ExtensionContext): string { const getCommandBody = createCommandBodyReader(ECC_ROOT) export default function (pi: ExtensionAPI): void { - pi.on("input", event => { - if (event.source === "extension" || event.streamingBehavior === "steer" || typeof pi.getCommands !== "function") { - return { action: "continue" } - } - - return handlePiInput({ - text: event.text, + pi.on("input", event => + handlePiInputEvent(event, pi, { eccRoot: ECC_ROOT, env: process.env, - activeCommands: pi.getCommands(), getBody: getCommandBody, }) - }) + ) /** * ECC's SessionStart hook returns context for the model, but Pi has no diff --git a/.pi/extensions/input-args.js b/.pi/extensions/input-args.js index 9f2145200..a0f81be0f 100644 --- a/.pi/extensions/input-args.js +++ b/.pi/extensions/input-args.js @@ -104,6 +104,32 @@ function handlePiInput({ text, eccRoot, env, activeCommands, getBody }) { return { action: "transform", text: `${body}\n\nARGUMENTS: ${match[2].trim()}` } } +/** + * The real Pi input-event callback the TypeScript extension registers. + * + * Extracted here (rather than being an anonymous arrow in index.ts) so the + * plain Node test suite can invoke the exact registered handler with a fake + * `pi` object and assert both its guard branches and its transform result. + */ +function handlePiInputEvent(event, pi, options) { + if ( + !event || + event.source === "extension" || + event.streamingBehavior === "steer" || + typeof pi.getCommands !== "function" + ) { + return { action: "continue" } + } + + return handlePiInput({ + text: event.text, + eccRoot: options.eccRoot, + env: options.env, + activeCommands: pi.getCommands(), + getBody: options.getBody, + }) +} + /** * Creates a body reader with its own cache. * @@ -131,4 +157,5 @@ module.exports = { createCommandBodyReader, activeEccPromptPath, handlePiInput, + handlePiInputEvent, } diff --git a/tests/pi/pi-extension-adapter.test.js b/tests/pi/pi-extension-adapter.test.js index 5236f52ae..365a44c1a 100644 --- a/tests/pi/pi-extension-adapter.test.js +++ b/tests/pi/pi-extension-adapter.test.js @@ -53,6 +53,7 @@ const { loadPlaceholderlessCommand, createCommandBodyReader, handlePiInput, + handlePiInputEvent, } = require( path.join(__dirname, "..", "..", ".pi", "extensions", "input-args.js") ) @@ -1690,20 +1691,23 @@ async function main() { "place ECC can restore the ARGUMENTS payload Pi otherwise drops" ) assert.ok( - extensionSource.includes("handlePiInput") && + extensionSource.includes("handlePiInputEvent") && extensionSource.includes('from "./input-args.js"'), "expected .pi/extensions/index.ts to delegate the input handler to input-args.js " + - "so tests can load and exercise the real handler rather than a copy" + "so tests can load and exercise the real registered callback rather than a copy" ) assert.ok( - extensionSource.includes('event.source === "extension"'), - "expected the registered handler to skip extension-injected input; transforming " + - "an extension message as if it were user input can double-expand ECC commands" + inputArgsSource.includes('event.source === "extension"') && + inputArgsSource.includes('event.streamingBehavior === "steer"') && + inputArgsSource.includes('typeof pi.getCommands !== "function"'), + "expected the registered callback to skip extension-injected and steering input, " + + "and to tolerate an API without getCommands; transforming those can double-expand " + + "ECC commands or crash the adapter" ) assert.ok( - extensionSource.includes("pi.getCommands()") && - extensionSource.includes("activeCommands:"), - "expected the registered handler to resolve active commands via pi.getCommands() " + + inputArgsSource.includes("pi.getCommands()") && + inputArgsSource.includes("activeCommands:"), + "expected the registered callback to resolve active commands via pi.getCommands() " + "and pass them into the testable input-args handler for provenance checks" ) assert.ok( @@ -1739,6 +1743,55 @@ async function main() { ) }], + ["registered input handler guard skips extension, steer, and missing getCommands", () => { + const fakePi = { getCommands: () => [eccPrompt("plan")] } + const options = { + eccRoot: repoRoot, + env: {}, + getBody: createCommandBodyReader(repoRoot), + } + + assert.deepStrictEqual( + handlePiInputEvent({ text: "/plan build it", source: "extension" }, fakePi, options), + { action: "continue" }, + "extension-injected input must pass through unchanged so ECC commands are not double-expanded" + ) + assert.deepStrictEqual( + handlePiInputEvent( + { text: "/plan build it", source: "interactive", streamingBehavior: "steer" }, + fakePi, + options + ), + { action: "continue" }, + "steering input must pass through unchanged rather than being transformed as a fresh command" + ) + assert.deepStrictEqual( + handlePiInputEvent({ text: "/plan build it", source: "interactive" }, {}, options), + { action: "continue" }, + "an extension API without getCommands must not crash the input handler" + ) + }], + + ["registered input handler transforms through the actual event callback", () => { + const fakePi = { getCommands: () => [eccPrompt("plan")] } + const readBody = createCommandBodyReader(repoRoot) + const result = handlePiInputEvent( + { text: "/plan add a hello world CLI", source: "interactive" }, + fakePi, + { eccRoot: repoRoot, env: {}, getBody: readBody } + ) + + assert.strictEqual(result.action, "transform") + assert.ok( + result.text.includes("ARGUMENTS: add a hello world CLI"), + "the registered event callback should append the user arguments to the canonical body" + ) + assert.ok( + result.text.startsWith(readBody("plan").split("\n")[0]), + "the registered event callback should use the real cached command body" + ) + }], + ["input handler transforms /plan args when Pi's active prompt resolves to ECC", () => { const planRaw = fs.readFileSync(path.join(repoRoot, "commands", "plan.md"), "utf8") const planBody = ARG_PLACEHOLDER.test( From 65e47bb1e2c6fc1bbedf41c77051c351679d3cd5 Mon Sep 17 00:00:00 2001 From: quark404 Date: Sun, 1 Nov 2026 10:48:36 +0000 Subject: [PATCH 5/6] test(pi): pin command body cache immutability after file changes --- tests/pi/pi-extension-adapter.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/pi/pi-extension-adapter.test.js b/tests/pi/pi-extension-adapter.test.js index 365a44c1a..3ec126aa1 100644 --- a/tests/pi/pi-extension-adapter.test.js +++ b/tests/pi/pi-extension-adapter.test.js @@ -1951,6 +1951,33 @@ async function main() { "the cache should return the same body on subsequent lookups" ) }], + + ["command body cache returns the first body after the backing file changes", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "ecc-pi-cache-")) + const commandsDir = path.join(root, "commands") + fs.mkdirSync(commandsDir, { recursive: true }) + const planPath = path.join(commandsDir, "plan.md") + fs.writeFileSync(planPath, "---\ndescription: plan\n---\nfirst body\n") + + try { + const readBody = createCommandBodyReader(root) + assert.strictEqual(readBody("plan"), "first body") + + fs.writeFileSync(planPath, "---\ndescription: plan\n---\nsecond body\n") + assert.strictEqual( + loadPlaceholderlessCommand("plan", root), + "second body", + "the backing command file must actually change for the cache assertion to be meaningful" + ) + assert.strictEqual( + readBody("plan"), + "first body", + "the cached body must be immutable even after the backing command file changes" + ) + } finally { + fs.rmSync(root, { recursive: true, force: true }) + } + }], ] for (const [name, fn] of tests) { From c2a7bfe2167e5a472d7d647f544743fe7a9e1a87 Mon Sep 17 00:00:00 2001 From: quark404 Date: Sun, 1 Nov 2026 11:58:38 +0000 Subject: [PATCH 6/6] fix(pi): report command read failures --- .pi/extensions/input-args.js | 10 +++++--- tests/pi/pi-extension-adapter.test.js | 37 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/.pi/extensions/input-args.js b/.pi/extensions/input-args.js index a0f81be0f..a1b93bf8b 100644 --- a/.pi/extensions/input-args.js +++ b/.pi/extensions/input-args.js @@ -28,7 +28,7 @@ function stripFrontmatter(raw) { * Pi's template expansion silently drops the arguments instead. Most ECC * commands rely on the Claude behaviour, so the adapter restores it. */ -function loadPlaceholderlessCommand(name, eccRoot, commandPath) { +function loadPlaceholderlessCommand(name, eccRoot, commandPath, reportReadError = message => console.warn(message)) { if (!/^[A-Za-z0-9._-]+$/.test(name)) { return null } @@ -38,7 +38,9 @@ function loadPlaceholderlessCommand(name, eccRoot, commandPath) { const raw = fs.readFileSync(resolvedPath, "utf8") const stripped = stripFrontmatter(raw) return ARG_PLACEHOLDER.test(stripped) ? null : stripped - } catch { + } catch (error) { + const detail = error && error.message ? error.message : String(error) + reportReadError(`[ecc-pi] unable to read command ${resolvedPath}: ${detail}`) return null } } @@ -136,13 +138,13 @@ function handlePiInputEvent(event, pi, options) { * The returned function returns a fresh Map on each new lookup rather than * mutating the previous cache, satisfying ECC's immutability rule. */ -function createCommandBodyReader(eccRoot) { +function createCommandBodyReader(eccRoot, reportReadError) { let cache = new Map() return name => { if (cache.has(name)) { return cache.get(name) ?? null } - const body = loadPlaceholderlessCommand(name, eccRoot) + const body = loadPlaceholderlessCommand(name, eccRoot, undefined, reportReadError) cache = new Map(cache).set(name, body) return body } diff --git a/tests/pi/pi-extension-adapter.test.js b/tests/pi/pi-extension-adapter.test.js index 3ec126aa1..513b8ee0e 100644 --- a/tests/pi/pi-extension-adapter.test.js +++ b/tests/pi/pi-extension-adapter.test.js @@ -1741,6 +1741,12 @@ async function main() { "expected the command body cache to replace the old Map rather than mutate it; " + "ECC's immutability rule forbids mutating an existing cache object" ) + assert.ok( + inputArgsSource.includes("console.warn") && + inputArgsSource.includes("unable to read command"), + "expected input-args.js to report failed command reads instead of swallowing them; " + + "AGENTS.md requires errors to be surfaced even when the adapter keeps its fallback" + ) }], ["registered input handler guard skips extension, steer, and missing getCommands", () => { @@ -1905,6 +1911,37 @@ async function main() { } }], + ["command reader reports failed reads while keeping its null fallback", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "ecc-pi-readerr-")) + const commandsDir = path.join(root, "commands") + fs.mkdirSync(commandsDir, { recursive: true }) + + const warnings = [] + const originalWarn = console.warn + console.warn = (...args) => { + warnings.push(args.join(" ")) + } + + try { + assert.strictEqual( + loadPlaceholderlessCommand("unreadable", root, commandsDir), + null, + "a failed read must keep the fallback so argument restoration degrades rather than throws" + ) + assert.ok( + warnings.some( + message => + message.includes("[ecc-pi] unable to read command") && + message.includes(commandsDir) + ), + "the failed command read must be reported instead of being silently swallowed" + ) + } finally { + console.warn = originalWarn + fs.rmSync(root, { recursive: true, force: true }) + } + }], + ["input handler leaves disabled, missing, and differently resolved prompts unchanged", () => { const otherRoot = fs.mkdtempSync(path.join(os.tmpdir(), "ecc-pi-other-")) const otherCommands = path.join(otherRoot, "commands")