From 1a30b5b50a98387accdab1bd4cb4ae68a9345710 Mon Sep 17 00:00:00 2001 From: rebuildup Date: Sat, 21 Nov 2026 05:46:43 +0900 Subject: [PATCH] fix(cli,mcp): refuse valueless target-pin flags + consistent dispatch budgets (#383) --- .../a8n-transport/attached-session.mjs | 18 +++++- .../a8n-transport/dispatch-budget.mjs | 50 +++++++++++++++ .../ae-devtools-cli/a8n-transport/session.mjs | 17 ++++- .../tests/dispatch-budget.test.mjs | 63 +++++++++++++++++++ .../ae-devtools-cli/dev-loop/session-open.mjs | 5 ++ .../ae-devtools-cli/devtools-cli/argv.mjs | 19 ++++++ packages/ae-devtools-cli/devtools-cli/cli.mjs | 12 +++- .../devtools-cli/tests/run.mjs | 27 ++++++++ .../ae-devtools-cli/mcp-server/server.mjs | 12 +++- .../mcp-server/tests/server.test.mjs | 20 ++++++ 10 files changed, 235 insertions(+), 8 deletions(-) create mode 100644 packages/ae-devtools-cli/a8n-transport/dispatch-budget.mjs create mode 100644 packages/ae-devtools-cli/a8n-transport/tests/dispatch-budget.test.mjs diff --git a/packages/ae-devtools-cli/a8n-transport/attached-session.mjs b/packages/ae-devtools-cli/a8n-transport/attached-session.mjs index b952923a..a1551f7d 100644 --- a/packages/ae-devtools-cli/a8n-transport/attached-session.mjs +++ b/packages/ae-devtools-cli/a8n-transport/attached-session.mjs @@ -83,6 +83,7 @@ import { createJsxRunner } from "../trusted-execution/runner.mjs"; import { getOperation } from "../scripting-adapters/registry.mjs"; import { createDispatchStore, defaultDispatchStoreDir } from "../trusted-execution/dispatch-store.mjs"; import { createA8nTransport, EVAL_METHOD } from "./transport.mjs"; +import { resolveDispatchBudgets } from "./dispatch-budget.mjs"; import { openAttachTarget } from "./attach-target.mjs"; import { resolveAeA8n } from "./ae-a8n-resolve.mjs"; import { @@ -264,7 +265,11 @@ async function defaultHostProcessProbe() { * differs refuses the session after attach * workdir — scratch dir for the generated host script * callTimeoutMs — per-dispatch ae.call budget (default 9500) - * deadlineMs — runner deadline (default 10000; keep > callTimeoutMs) + * deadlineMs — runner deadline; absent derives callTimeoutMs + + * margin so the deadline always covers the call + * budget, and an explicit deadline <= callTimeout + * refuses USAGE (Issue #383, shared with the owned + * opener via dispatch-budget.mjs) * aeA8nRoot — local ae-a8n checkout root with a built dist/ * mutatingOps — explicit op-level mutation scope (Issue #366): * array of registered mutating opIds the caller @@ -280,6 +285,13 @@ async function defaultHostProcessProbe() { */ export async function openAttachedSliceSession(options = {}) { const mutatingOps = normalizeMutatingOps(options.mutatingOps); + // Budget validation FIRST (with mutatingOps): an incompatible + // callTimeout/deadline pair refuses before ae-a8n resolution, + // census or attach — same rule as the owned opener (Issue #383). + const budgets = resolveDispatchBudgets({ + callTimeoutMs: options.callTimeoutMs, + deadlineMs: options.deadlineMs, + }); const { AfterEffects, mod: aeA8nMod, cliPath, source: aeA8nSource, head: aeA8nHead } = await resolveAeA8n({ aeA8nRoot: options.aeA8nRoot }); const missing = missingContractExports(aeA8nMod); @@ -705,14 +717,14 @@ export async function openAttachedSliceSession(options = {}) { }; const transport = createA8nTransport({ ae, - callTimeoutMs: options.callTimeoutMs ?? 9_500, + callTimeoutMs: budgets.callTimeoutMs, }); transport.dispatchStore = createDispatchStore({ dir: options.dispatchStoreDir ?? defaultDispatchStoreDir(), }); const runner = createJsxRunner({ transport, - deadlineMs: options.deadlineMs ?? 10_000, + deadlineMs: budgets.deadlineMs, }); const target = { diff --git a/packages/ae-devtools-cli/a8n-transport/dispatch-budget.mjs b/packages/ae-devtools-cli/a8n-transport/dispatch-budget.mjs new file mode 100644 index 00000000..a2a366ac --- /dev/null +++ b/packages/ae-devtools-cli/a8n-transport/dispatch-budget.mjs @@ -0,0 +1,50 @@ +/** + * dispatch-budget.mjs — shared per-call / runner-deadline resolution for + * the a8n session openers (Issue #383). + * + * The runner deadline bounds the WHOLE dispatch (transport send + host + * call + reply); callTimeoutMs bounds only the ae.call leg. When the + * deadline is at or below the call budget, a host operation that would + * have completed inside its budget is quarantined as UNKNOWN at the + * deadline instead — e.g. `--call-timeout-ms 30000` alone used to leave + * the runner on its 10s default, so a 15s host op reported UNKNOWN. + * + * Both openers (owned `session.mjs`, attached `attached-session.mjs`) + * resolve through this one function so the pair can never drift between + * session kinds. An explicitly incompatible pair (deadline <= + * callTimeout) refuses with USAGE before any launch/attach — there is + * no legitimate configuration where the runner must fire first. + */ + +export const DEFAULT_CALL_TIMEOUT_MS = 9_500; +/** Headroom between the call budget and the runner deadline (default pair was 9500/10000). */ +export const DEFAULT_DEADLINE_MARGIN_MS = 500; + +/** + * @param {object} options + * callTimeoutMs — per-dispatch ae.call budget; non-positive/non-finite + * values fall back to the default (CLI positiveMsFlag + * already filters them upstream) + * deadlineMs — runner deadline; absent derives callTimeout + margin + * @returns {{callTimeoutMs:number, deadlineMs:number}} + * @throws {Error} code "USAGE" when an explicit deadlineMs does not + * exceed callTimeoutMs + */ +export function resolveDispatchBudgets({ callTimeoutMs, deadlineMs } = {}) { + const callTimeout = Number.isFinite(callTimeoutMs) && callTimeoutMs > 0 + ? callTimeoutMs + : DEFAULT_CALL_TIMEOUT_MS; + const deadline = Number.isFinite(deadlineMs) && deadlineMs > 0 + ? deadlineMs + : callTimeout + DEFAULT_DEADLINE_MARGIN_MS; + if (!(deadline > callTimeout)) { + const e = new Error( + `deadlineMs (${deadline}) must exceed callTimeoutMs (${callTimeout}): ` + + "a runner deadline at or below the per-call budget reports UNKNOWN " + + "for host calls that would still have completed", + ); + e.code = "USAGE"; + throw e; + } + return { callTimeoutMs: callTimeout, deadlineMs: deadline }; +} diff --git a/packages/ae-devtools-cli/a8n-transport/session.mjs b/packages/ae-devtools-cli/a8n-transport/session.mjs index 5fffd7c4..d0a58fdf 100644 --- a/packages/ae-devtools-cli/a8n-transport/session.mjs +++ b/packages/ae-devtools-cli/a8n-transport/session.mjs @@ -38,6 +38,7 @@ import { promisify } from "node:util"; import { createJsxRunner } from "../trusted-execution/runner.mjs"; import { createDispatchStore, defaultDispatchStoreDir } from "../trusted-execution/dispatch-store.mjs"; import { createA8nTransport } from "./transport.mjs"; +import { resolveDispatchBudgets } from "./dispatch-budget.mjs"; import { resolveAeA8n } from "./ae-a8n-resolve.mjs"; import { missingContractExports, @@ -68,10 +69,20 @@ const EXTENSION_MODULE = join(HERE, "extension.mjs"); * aeA8nRoot — local ae-a8n checkout root with a built dist/; overrides * the pinned npm dependency (recorded in evidence) * callTimeoutMs — per-dispatch ae.call budget (default 9500) - * deadlineMs — runner deadline (default 10000; keep > callTimeoutMs) + * deadlineMs — runner deadline; absent derives callTimeoutMs + + * margin so the deadline always covers the call + * budget, and an explicit deadline <= callTimeout + * refuses USAGE (Issue #383, shared with the + * attached opener via dispatch-budget.mjs) * workdir — scratch dir for the generated host script (default: tmp) */ export async function openSliceSession(options = {}) { + // Budget validation FIRST: an incompatible callTimeout/deadline pair + // refuses before ae-a8n resolution, extension build or launch. + const budgets = resolveDispatchBudgets({ + callTimeoutMs: options.callTimeoutMs, + deadlineMs: options.deadlineMs, + }); const { AfterEffects, mod: aeA8nMod, cliPath, source: aeA8nSource, head: aeA8nHead } = await resolveAeA8n({ aeA8nRoot: options.aeA8nRoot }); // Contract gate (Issue #138): refuse BEFORE extension build / launch when @@ -197,7 +208,7 @@ export async function openSliceSession(options = {}) { }; const transport = createA8nTransport({ ae, - callTimeoutMs: options.callTimeoutMs ?? 9_500, + callTimeoutMs: budgets.callTimeoutMs, }); // Cross-instance dispatch dedup/quarantine (Issue #255): attach the // store to the TRANSPORT, not the runner — every createJsxRunner() @@ -213,7 +224,7 @@ export async function openSliceSession(options = {}) { }); const runner = createJsxRunner({ transport, - deadlineMs: options.deadlineMs ?? 10_000, + deadlineMs: budgets.deadlineMs, }); async function close() { diff --git a/packages/ae-devtools-cli/a8n-transport/tests/dispatch-budget.test.mjs b/packages/ae-devtools-cli/a8n-transport/tests/dispatch-budget.test.mjs new file mode 100644 index 00000000..6c671cb9 --- /dev/null +++ b/packages/ae-devtools-cli/a8n-transport/tests/dispatch-budget.test.mjs @@ -0,0 +1,63 @@ +/** + * dispatch-budget.test.mjs — Issue #383: the runner deadline must + * always cover the per-call budget, for BOTH a8n openers. + * Fake-level only: budget resolution refuses before ae-a8n resolution, + * so no AE and no ae-a8n fixture is needed for the refusal paths. + */ +import test from "node:test"; +import assert from "node:assert/strict"; + +import { + resolveDispatchBudgets, + DEFAULT_CALL_TIMEOUT_MS, + DEFAULT_DEADLINE_MARGIN_MS, +} from "../dispatch-budget.mjs"; +import { openSliceSession } from "../session.mjs"; +import { openAttachedSliceSession } from "../attached-session.mjs"; + +test("defaults preserve the historical 9500/10000 pair", () => { + const b = resolveDispatchBudgets({}); + assert.equal(b.callTimeoutMs, DEFAULT_CALL_TIMEOUT_MS); + assert.equal(b.deadlineMs, DEFAULT_CALL_TIMEOUT_MS + DEFAULT_DEADLINE_MARGIN_MS); +}); + +test("deadline derives from callTimeoutMs when unspecified", () => { + const b = resolveDispatchBudgets({ callTimeoutMs: 30_000 }); + assert.equal(b.callTimeoutMs, 30_000); + assert.equal(b.deadlineMs, 30_000 + DEFAULT_DEADLINE_MARGIN_MS); + assert.ok(b.deadlineMs > b.callTimeoutMs); +}); + +test("explicit compatible pair passes through", () => { + const b = resolveDispatchBudgets({ callTimeoutMs: 5_000, deadlineMs: 20_000 }); + assert.deepEqual(b, { callTimeoutMs: 5_000, deadlineMs: 20_000 }); +}); + +test("explicit deadline <= callTimeout refuses USAGE", () => { + for (const deadlineMs of [1_000, 9_500]) { + assert.throws( + () => resolveDispatchBudgets({ callTimeoutMs: 9_500, deadlineMs }), + (e) => e.code === "USAGE", + ); + } +}); + +test("non-positive/non-finite values fall back to defaults", () => { + const b = resolveDispatchBudgets({ callTimeoutMs: 0, deadlineMs: -5 }); + assert.equal(b.callTimeoutMs, DEFAULT_CALL_TIMEOUT_MS); + assert.equal(b.deadlineMs, DEFAULT_CALL_TIMEOUT_MS + DEFAULT_DEADLINE_MARGIN_MS); +}); + +test("openSliceSession refuses an incompatible pair before ae-a8n resolution", async () => { + await assert.rejects( + () => openSliceSession({ callTimeoutMs: 30_000, deadlineMs: 10_000 }), + (e) => e.code === "USAGE" && /deadlineMs/.test(e.message), + ); +}); + +test("openAttachedSliceSession refuses an incompatible pair before census/attach", async () => { + await assert.rejects( + () => openAttachedSliceSession({ callTimeoutMs: 30_000, deadlineMs: 10_000 }), + (e) => e.code === "USAGE" && /deadlineMs/.test(e.message), + ); +}); diff --git a/packages/ae-devtools-cli/dev-loop/session-open.mjs b/packages/ae-devtools-cli/dev-loop/session-open.mjs index d0d4ab09..4b11f784 100644 --- a/packages/ae-devtools-cli/dev-loop/session-open.mjs +++ b/packages/ae-devtools-cli/dev-loop/session-open.mjs @@ -109,6 +109,11 @@ export async function openSession(kind = "fake", options = {}) { aeA8nRoot: typeof options.aeA8nRoot === "string" ? options.aeA8nRoot : undefined, installDir: typeof options.installDir === "string" ? options.installDir : undefined, workdir: typeof options.workdir === "string" ? options.workdir : undefined, + // Dispatch budgets apply to owned sessions too (Issue #383) — the + // openers derive a compatible deadline/callTimeout pair and refuse + // an explicitly incompatible one. + callTimeoutMs: Number.isFinite(options.callTimeoutMs) ? options.callTimeoutMs : undefined, + deadlineMs: Number.isFinite(options.deadlineMs) ? options.deadlineMs : undefined, }); return { kind: "a8n", diff --git a/packages/ae-devtools-cli/devtools-cli/argv.mjs b/packages/ae-devtools-cli/devtools-cli/argv.mjs index a314a72d..d1ba359a 100644 --- a/packages/ae-devtools-cli/devtools-cli/argv.mjs +++ b/packages/ae-devtools-cli/devtools-cli/argv.mjs @@ -52,6 +52,25 @@ export function hasFlag(flag, argv = process.argv.slice(2)) { return key in parseArgs(argv).flags; } +/** + * Value-required flag check (Issue #383). `flags` is a parseArgs() + * result; returns the first name in `names` that is PRESENT but carries + * no usable string value — a bare `--flag` parses to `true` and + * `--flag=` parses to `""`. Callers that map `typeof v === "string"` to + * "option supplied" would otherwise silently disable the option (e.g. a + * target pin), so pin-type flags must refuse instead. Returns null when + * every listed flag is absent or holds a nonempty string. + */ +export function missingFlagValue(flags, names) { + for (const name of names) { + if (Object.hasOwn(flags, name) && + (typeof flags[name] !== "string" || flags[name].length === 0)) { + return name; + } + } + return null; +} + /** * String value of `--flag` from argv (default: this process's args). * Returns undefined when absent or when the flag was given bare diff --git a/packages/ae-devtools-cli/devtools-cli/cli.mjs b/packages/ae-devtools-cli/devtools-cli/cli.mjs index 7fd171e1..2278a427 100644 --- a/packages/ae-devtools-cli/devtools-cli/cli.mjs +++ b/packages/ae-devtools-cli/devtools-cli/cli.mjs @@ -74,7 +74,7 @@ import { execFileSync } from "node:child_process"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; -import { parseArgs } from "./argv.mjs"; +import { parseArgs, missingFlagValue } from "./argv.mjs"; import { discover, inspect, execute, verify, DEVLOOP_SCHEMA } from "../dev-loop/devloop.mjs"; import { openSession as openSharedSession } from "../dev-loop/session-open.mjs"; import { closeSessionWithTimeout, parseCloseTimeoutMs } from "../dev-loop/session-close.mjs"; @@ -360,6 +360,16 @@ async function main() { emit({ schema: "ae-devtools.cli.error/v1", code: "USAGE", message: "--project-file is only valid with --session a8n-attached" }); return 2; } + // Target pins must carry a value (Issue #383): a bare + // `--project-file` parses to `true` and `--project-file=` to "" — + // both map to "pin absent" in the session openers, silently + // unpinning a granted mutation. Refuse BEFORE the run marker and + // session open. `--ae-version` has the same silent-disable shape. + const valuelessPin = missingFlagValue(flags, ["project-file", "ae-version"]); + if (valuelessPin !== null) { + emit({ schema: "ae-devtools.cli.error/v1", code: "USAGE", message: `--${valuelessPin} requires a nonempty value` }); + return 2; + } // Marker BEFORE session open: a launch that dies mid-open still // leaves an attributable residue record. diff --git a/packages/ae-devtools-cli/devtools-cli/tests/run.mjs b/packages/ae-devtools-cli/devtools-cli/tests/run.mjs index 2d5e28bb..599879e9 100644 --- a/packages/ae-devtools-cli/devtools-cli/tests/run.mjs +++ b/packages/ae-devtools-cli/devtools-cli/tests/run.mjs @@ -187,6 +187,33 @@ t("unknown --session is a usage error and leaves no run marker (Issue #219)", () assert.equal(run(["runs"]).body.runs.length, 0); }); +t("valueless --project-file / --ae-version refuse USAGE before session open (Issue #383)", () => { + freshRunsDir(); + const cases = [ + // bare flag → true; `--flag=` → ""; both must not silently unpin + ["execute", "read.projectSummary", "--session", "a8n-attached", "--project-file", "--consent", "grant"], + ["execute", "read.projectSummary", "--session", "a8n-attached", "--project-file=", "--consent", "grant"], + ["execute", "read.projectSummary", "--session", "a8n-attached", "--ae-version", "--consent", "grant"], + ["execute", "read.projectSummary", "--session", "a8n-attached", "--ae-version="], + ["verify", "--session", "a8n-attached", "--project-file"], + ]; + for (const args of cases) { + const r = run(args); + assert.equal(r.code, 2, `expected exit 2 for ${JSON.stringify(args)}`); + assert.equal(r.body.code, "USAGE", JSON.stringify(r.body)); + assert.match(r.body.message, /requires a nonempty value/); + } + assert.equal(run(["runs"]).body.runs.length, 0, "usage refusal must not leave a run marker"); +}); + +t("a valued --project-file still reaches the session-kind check (Issue #383)", () => { + const r = run(["execute", "read.projectSummary", "--session", "fake", + "--project-file", "/tmp/pin.aep", "--consent", "grant"]); + assert.equal(r.code, 2); + assert.equal(r.body.code, "USAGE"); + assert.match(r.body.message, /only valid with --session a8n-attached/); +}); + await at("SIGTERM during execute runs cleanup then exits 143", async () => { freshRunsDir(); const child = spawn("node", [CLI, "execute", "read.projectSummary", diff --git a/packages/ae-devtools-cli/mcp-server/server.mjs b/packages/ae-devtools-cli/mcp-server/server.mjs index 4fca2c9b..12c1a5c6 100644 --- a/packages/ae-devtools-cli/mcp-server/server.mjs +++ b/packages/ae-devtools-cli/mcp-server/server.mjs @@ -37,7 +37,7 @@ import { fileURLToPath } from "node:url"; import { TOOLS, callTool, MCP_SCHEMA } from "./tools.mjs"; import { openSession } from "./session.mjs"; -import { parseArgs } from "../devtools-cli/argv.mjs"; +import { parseArgs, missingFlagValue } from "../devtools-cli/argv.mjs"; import { closeSessionWithTimeout, parseCloseTimeoutMs } from "../dev-loop/session-close.mjs"; import { createSurfaceGuard } from "../dev-loop/surface-guard.mjs"; import { lazyConsentAudit } from "../trusted-execution/consent-audit.mjs"; @@ -289,6 +289,16 @@ if (isMain) { process.stderr.write("ae-devtools-mcp: --project-file is only valid with --session a8n-attached\n"); process.exit(2); } + // Target pins must carry a value (Issue #383): a bare `--project-file` + // parses to `true` and `--project-file=` to "" — both map to "pin + // absent" in the session openers, silently unpinning a granted + // mutation. `--ae-version` has the same silent-disable shape. Refuse + // before the server (and its lazy session) exists. + const valuelessPin = missingFlagValue(flags, ["project-file", "ae-version"]); + if (valuelessPin !== null) { + process.stderr.write(`ae-devtools-mcp: --${valuelessPin} requires a nonempty value\n`); + process.exit(2); + } const server = createServer({ sessionKind, sessionOptions: { diff --git a/packages/ae-devtools-cli/mcp-server/tests/server.test.mjs b/packages/ae-devtools-cli/mcp-server/tests/server.test.mjs index d9a3169c..21e68969 100644 --- a/packages/ae-devtools-cli/mcp-server/tests/server.test.mjs +++ b/packages/ae-devtools-cli/mcp-server/tests/server.test.mjs @@ -635,6 +635,26 @@ test("spawned server over real stdio: initialize → tools → execute → verif } }); +// ---- entry-flag validation (Issue #383): valueless target pins must +// ---- refuse before the server exists — tested against the real +// ---- spawned entry point, not just createServer ---- + +test("spawned entry refuses valueless --project-file / --ae-version with exit 2", async () => { + for (const argv of [ + ["--session", "a8n-attached", "--project-file"], + ["--session", "a8n-attached", "--project-file="], + ["--session", "a8n-attached", "--ae-version"], + ["--session", "fake", "--ae-version="], + ]) { + const child = spawn(process.execPath, [SERVER, ...argv], { stdio: ["ignore", "pipe", "pipe"] }); + let stderr = ""; + child.stderr.on("data", (d) => { stderr += d; }); + const code = await new Promise((resolve) => child.on("exit", resolve)); + assert.equal(code, 2, `argv ${JSON.stringify(argv)}: ${stderr}`); + assert.match(stderr, /requires a nonempty value/); + } +}); + // ---- outcome normalization consistency (Issue #226) ---- test("lowercase adapter outcome spellings normalize on MCP surface; session meta cannot spoof kind/evidenceLevel", async () => {