diff --git a/src/cli/cliResultOutput.ts b/src/cli/cliResultOutput.ts index c49ce9ab97d..34506f0ed67 100644 --- a/src/cli/cliResultOutput.ts +++ b/src/cli/cliResultOutput.ts @@ -4,6 +4,7 @@ import type { Writable } from "node:stream"; import { estimateTokenCount } from "tokenx"; import { analysisErrorProjectionSchema } from "../domain/analysisErrorProjectionSchema.js"; +import { isRecord } from "../kernel/isRecord.js"; import type { ParsedCliOutputArguments } from "./cliOutput.js"; import { cliOperationFailureIdentity, @@ -82,6 +83,111 @@ const resultMeta = (metadata: CliResultOutputMetadata) => ({ : { cli_controls: { ...metadata.cliControls } }), }); +/** CLI-only issue tracker URL appended to human-TTY refusal hints. */ +const CLI_ISSUE_REPORT_URL = "https://github.com/morluto/rea/issues"; + +const ISSUE_HINT_ERROR_CODES: ReadonlySet = + new Set(["unsupported_target", "target_unavailable", "execution_failure"]); + +const shouldAppendIssueReportHint = ( + projection: AnalysisErrorProjection, +): boolean => + ISSUE_HINT_ERROR_CODES.has(projection.code) || + projection.details?.reason === "file_format_rejected" || + projection.details?.constraint === "file_format_rejected"; + +/** + * Interactive terminal presentation gate for command-execution failures: + * activates only when stdout is a TTY and no explicit format, full-output, + * token, or filter control was requested. + */ +const isHumanTerminalFailurePresentation = ( + options: ParsedCliOutputArguments, + destination: Writable, +): boolean => + isTerminalOutput(destination) && + !options.formatExplicit && + !options.fullOutput && + !options.tokenCount && + !options.tokenWindow && + options.filterOutput === undefined; + +/** + * Render an `AnalysisErrorProjection` as concise plain text for a human + * reading an interactive terminal. Never used for piped output, explicit + * machine formats, or MCP responses. + */ +const renderHumanAnalysisError = ( + projection: AnalysisErrorProjection, +): string => { + const lines: string[] = [`Error (${projection.code}): ${projection.message}`]; + const action = projection.remediation.action.trim(); + if (action.length > 0 && action !== projection.message.trim()) + lines.push(`Next step: ${action}`); + if (shouldAppendIssueReportHint(projection)) + lines.push( + `If this refusal is unexpected, please report it at ${CLI_ISSUE_REPORT_URL}`, + ); + return `${lines.join("\n")}\n`; +}; + +/** + * Concise human-TTY rendering prints only code, message, and remediation. + * Bypass it whenever the failure carries preserved local evidence, inline + * observations, published bundle receipts/errors, or incomplete-cleanup + * resource records that would otherwise be dropped before exit. + */ +const hasPreservedFailureEvidence = ( + projection: AnalysisErrorProjection, +): boolean => { + if (projection.code === "cleanup_incomplete") return true; + const details = projection.details; + if (details === undefined) return false; + if ( + details.partial_observation !== undefined || + details.completed_observation !== undefined || + details.argument_observations !== undefined || + details.captured_output !== undefined || + details.cleanup === "incomplete" + ) + return true; + const diagnostics = details.diagnostics; + if ( + isRecord(diagnostics) && + (diagnostics.evidence_bundle !== undefined || + diagnostics.evidence_bundle_error !== undefined) + ) + return true; + const secondary = details.secondary_failures; + if (Array.isArray(secondary)) { + for (const item of secondary) { + const parsed = analysisErrorProjectionSchema.safeParse(item); + if (parsed.success && hasPreservedFailureEvidence(parsed.data)) + return true; + } + } + return false; +}; + +const resolveHumanFailureProjection = ( + value: unknown, + metadata: CliResultOutputMetadata, + options: ParsedCliOutputArguments, + destination: Writable, +): AnalysisErrorProjection | undefined => { + if (!isHumanTerminalFailurePresentation(options, destination)) + return undefined; + const candidate = metadata.failure + ? value + : cliOperationFailurePayload(value); + if (candidate === undefined || isCliCompletedFailureReport(value)) + return undefined; + const parsed = analysisErrorProjectionSchema.safeParse(candidate); + if (!parsed.success || hasPreservedFailureEvidence(parsed.data)) + return undefined; + return parsed.data; +}; + /** Optional executable-owned result surface for one CLI invocation. */ export interface CliResultOutput { readonly handled: boolean; @@ -249,15 +355,19 @@ export const createCliResultOutput = ( ownsDocument && selectedMetadata.cta !== undefined ? { ...selectedMetadata, cta: undefined } : selectedMetadata; + let renderedHumanFailure = false; try { const written = await writeResult( value, presentedMetadata, operationFailure, + () => { + renderedHumanFailure = true; + }, ); if ( metadata.cta !== undefined && - (!written || !ownsDocument) && + (!written || (!ownsDocument && !renderedHumanFailure)) && error === undefined && !options.fullOutput && !options.tokenCount @@ -312,12 +422,33 @@ export const createCliResultOutput = ( value: unknown, metadata: CliResultOutputMetadata, reportedFailure: unknown, + onHumanFailure: () => void, ): Promise { completed = { metadata, receipt: cliEvidencePublicationReceipt(), originalFailure: cliOperationFailureIdentity(value), }; + const humanFailure = resolveHumanFailureProjection( + value, + metadata, + options, + destination, + ); + if (humanFailure !== undefined) { + onHumanFailure(); + handled = true; + try { + await writeOutputText( + destination, + renderHumanAnalysisError(humanFailure), + ); + } catch (cause: unknown) { + failed = true; + throw cause; + } + return true; + } const filterPaths = options.filterPaths?.ok ? options.filterPaths.value : undefined; diff --git a/tests/boundary/cli/cliHumanFailureOutput.test.ts b/tests/boundary/cli/cliHumanFailureOutput.test.ts new file mode 100644 index 00000000000..015ec126b34 --- /dev/null +++ b/tests/boundary/cli/cliHumanFailureOutput.test.ts @@ -0,0 +1,510 @@ +import { Writable } from "node:stream"; + +import { describe, expect, it } from "vitest"; +import { parse as parseYaml } from "yaml"; +import { z } from "zod"; + +import { parseCliOutputArguments } from "../../../src/cli/cliOutput.js"; +import { createCliResultOutput } from "../../../src/cli/cliResultOutput.js"; +import { registerCliCommand } from "../../../src/cli/commandRegistry.js"; +import { logCliCommand } from "../../../src/cliLogging.js"; +import { + AnalysisInputError, + AnalysisUnsupportedTargetError, +} from "../../../src/domain/analysisErrorCore.js"; +import { + projectAnalysisError, + type AnalysisErrorProjection, +} from "../../../src/domain/analysisErrorProjection.js"; +import { BinaryTargetError } from "../../../src/domain/configurationErrors.js"; +import { silentLogger } from "../../../src/logger.js"; +import { createCommandCli } from "../../support/cli/commandCli.js"; + +const CLI_ISSUE_REPORT_URL = "https://github.com/morluto/rea/issues"; + +const makeDestination = (terminal: boolean) => { + const chunks: Buffer[] = []; + const destination = new Writable({ + write(chunk: Buffer, _encoding, callback) { + chunks.push(chunk); + callback(); + }, + }); + if (terminal) Object.defineProperty(destination, "isTTY", { value: true }); + return { + destination, + text: () => Buffer.concat(chunks).toString("utf8"), + }; +}; + +const unsupportedTargetProjection = (): AnalysisErrorProjection => + projectAnalysisError( + new AnalysisUnsupportedTargetError( + "inspect-target", + "/tmp/sample.bin", + "Container format is unsupported.", + ), + "invocation", + ); + +const targetUnavailableProjection = (): AnalysisErrorProjection => + projectAnalysisError( + new BinaryTargetError("/tmp/missing.bin", "missing", { + remediationAction: "Verify the target path exists and is readable.", + }), + "invocation", + ); + +const fileFormatRejectedProjection = (): AnalysisErrorProjection => ({ + code: "invalid_request", + category: "invalid_input", + message: "The target format was rejected by the loader.", + retryable: false, + remediation: { + action: "Check the file header and retry with a supported artifact.", + }, + details: { + path: "/tmp/unknown.bin", + reason: "file_format_rejected", + }, +}); + +const executionFailureProjection = (): AnalysisErrorProjection => ({ + code: "execution_failure", + category: "execution_failure", + message: "The analysis backend failed unexpectedly.", + retryable: false, + remediation: { + action: "Retry with --format json for full diagnostics.", + }, +}); + +const ordinaryInputProjection = (): AnalysisErrorProjection => + projectAnalysisError( + new AnalysisInputError("inspect-target", { + userMessage: "The --limit option must be a positive integer.", + remediationAction: "Pass a positive integer for --limit.", + }), + "invocation", + ); + +describe("CLI human-TTY failure rendering", () => { + it("renders concise human text with CLI-only issue hint for unsupported_target on a TTY", async () => { + const projection = unsupportedTargetProjection(); + expect(projection.code).toBe("unsupported_target"); + + const { destination, text } = makeDestination(true); + const output = createCliResultOutput( + parseCliOutputArguments(["inspect-target"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + const cli = createCommandCli(output); + registerCliCommand(cli, "inspect-target", { + args: z.object({}), + run: () => + logCliCommand(silentLogger, "inspect-target", async () => projection), + }); + + const previousExitCode = process.exitCode; + process.exitCode = 0; + try { + await cli.serve(["inspect-target"]); + expect(process.exitCode).toBe(1); + } finally { + process.exitCode = previousExitCode; + } + + expect(output.handled).toBe(true); + expect(output.failed).toBe(false); + const rendered = text(); + expect(rendered).toContain( + `Error (${projection.code}): ${projection.message}`, + ); + expect(rendered).toContain(`Next step: ${projection.remediation.action}`); + expect(rendered).toContain(CLI_ISSUE_REPORT_URL); + expect(rendered).not.toContain("retryable:"); + expect(rendered).not.toContain("category:"); + expect(rendered.endsWith("\n")).toBe(true); + }); + + it.each([ + { name: "target_unavailable", projection: targetUnavailableProjection }, + { + name: "details.reason === file_format_rejected", + projection: fileFormatRejectedProjection, + }, + { name: "execution_failure", projection: executionFailureProjection }, + ])( + "appends the CLI-only issue hint in TTY human mode for $name", + async ({ projection: build }) => { + const projection = build(); + const { destination, text } = makeDestination(true); + const output = createCliResultOutput( + parseCliOutputArguments(["inspect-target"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + expect( + await output.write(projection, { + command: "inspect-target", + duration: "2ms", + }), + ).toBe(true); + expect(output.handled).toBe(true); + expect(output.failed).toBe(false); + const rendered = text(); + expect(rendered).toContain( + `Error (${projection.code}): ${projection.message}`, + ); + expect(rendered).toContain(`Next step: ${projection.remediation.action}`); + expect(rendered).toContain(CLI_ISSUE_REPORT_URL); + }, + ); + + it("omits the GitHub issue URL for ordinary expected input errors in TTY human mode", async () => { + const projection = ordinaryInputProjection(); + expect(projection.code).toBe("invalid_request"); + + const { destination, text } = makeDestination(true); + const output = createCliResultOutput( + parseCliOutputArguments(["inspect-target"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + expect( + await output.write(projection, { + command: "inspect-target", + duration: "1ms", + }), + ).toBe(true); + const rendered = text(); + expect(rendered).toContain( + `Error (${projection.code}): ${projection.message}`, + ); + expect(rendered).toContain(`Next step: ${projection.remediation.action}`); + expect(rendered).not.toContain(CLI_ISSUE_REPORT_URL); + expect(rendered).not.toContain("retryable:"); + }); + + it("keeps piped (non-TTY) stdout as machine-readable TOON without human prose or issue URL", async () => { + const projection = unsupportedTargetProjection(); + const { destination, text } = makeDestination(false); + const output = createCliResultOutput( + parseCliOutputArguments(["inspect-target"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + expect( + await output.write(projection, { + command: "inspect-target", + duration: "1ms", + }), + ).toBe(true); + const rendered = text(); + expect(rendered).toContain("code: unsupported_target"); + expect(rendered).toContain("category: unsupported_target"); + expect(rendered).not.toContain("Error (unsupported_target):"); + expect(rendered).not.toContain(CLI_ISSUE_REPORT_URL); + }); + + it.each([ + { name: "--json", args: ["--json"] }, + { name: "--format json", args: ["--format", "json"] }, + { name: "--format yaml", args: ["--format", "yaml"] }, + { name: "--format toon", args: ["--format", "toon"] }, + { name: "--format jsonl", args: ["--format", "jsonl"] }, + { name: "--format md", args: ["--format", "md"] }, + { + name: "--format csv", + args: ["--format", "csv", "--filter-output", "rows", "--columns", "id"], + }, + { + name: "--format tsv", + args: ["--format", "tsv", "--filter-output", "rows", "--columns", "id"], + }, + { name: "--full-output", args: ["--full-output"] }, + { name: "--token-count", args: ["--token-count"] }, + { name: "--token-limit", args: ["--token-limit", "50"] }, + { name: "--token-offset", args: ["--token-offset", "0"] }, + { name: "--filter-output", args: ["--filter-output", "code"] }, + ])( + "bypasses human rendering and emits no issue URL on a TTY when $name is passed", + async ({ args }) => { + const projection = unsupportedTargetProjection(); + const { destination, text } = makeDestination(true); + const parsed = parseCliOutputArguments(["inspect-target", ...args]); + expect(parsed.validation.ok).toBe(true); + const output = createCliResultOutput(parsed, destination); + if (output === undefined) throw new Error("Missing CLI result output"); + + expect( + await output.write(projection, { + command: "inspect-target", + duration: "1ms", + }), + ).toBe(true); + expect(output.handled).toBe(true); + expect(output.failed).toBe(false); + + const rendered = text(); + expect(rendered).not.toContain("Error (unsupported_target):"); + expect(rendered).not.toContain(CLI_ISSUE_REPORT_URL); + + if (parsed.format === "json") { + expect(JSON.parse(rendered)).toMatchObject({ + code: "unsupported_target", + }); + } else if (parsed.format === "yaml") { + expect(parseYaml(rendered)).toMatchObject({ + code: "unsupported_target", + }); + } + }, + ); + + it("retains handled and failed destination ownership when a TTY write fails", async () => { + const cause = Object.assign(new Error("broken pipe"), { code: "EPIPE" }); + const destination = new Writable({ + write(_chunk: Buffer, _encoding, callback) { + callback(cause); + }, + }); + Object.defineProperty(destination, "isTTY", { value: true }); + + const output = createCliResultOutput( + parseCliOutputArguments(["inspect-target"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + await expect( + output.write(unsupportedTargetProjection(), { + command: "inspect-target", + duration: "1ms", + }), + ).rejects.toBe(cause); + expect(output.handled).toBe(true); + expect(output.failed).toBe(true); + }); + + it("does not misclassify an unhealthy completed report as an AnalysisErrorProjection on a TTY", async () => { + const { destination, text } = makeDestination(true); + const output = createCliResultOutput( + parseCliOutputArguments(["doctor"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + expect( + await output.write( + { healthy: false, summary: "missing provider" }, + { command: "doctor", duration: "1ms" }, + ), + ).toBe(true); + const rendered = text(); + expect(rendered).toContain("healthy: false"); + expect(rendered).not.toContain("Error ("); + expect(rendered).not.toContain(CLI_ISSUE_REPORT_URL); + }); + + it("re-evaluates the human presentation gate across begin() invocations", async () => { + const projection = unsupportedTargetProjection(); + const { destination, text } = makeDestination(true); + const output = createCliResultOutput( + parseCliOutputArguments(["inspect-target", "--json"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + output.begin(parseCliOutputArguments(["inspect-target"])); + expect( + await output.write(projection, { + command: "inspect-target", + duration: "1ms", + }), + ).toBe(true); + expect(text()).toContain(CLI_ISSUE_REPORT_URL); + }); + + it.each([ + { + name: "cancelled failure with details.partial_observation", + projection: (): AnalysisErrorProjection => ({ + code: "cancelled", + category: "cancelled", + message: "JavaScript application analysis was cancelled.", + retryable: true, + remediation: { + action: + "Reuse partial_observation Evidence from this result or retry.", + }, + details: { + partial_observation: { + operation: "analyze_javascript_application", + parsed_sources: ["app.js"], + }, + }, + }), + expectedToken: "partial_observation:", + expectedValue: "app.js", + }, + { + name: "failure with details.completed_observation", + projection: (): AnalysisErrorProjection => ({ + code: "execution_failure", + category: "execution_failure", + message: "A follow-up step failed after source analysis completed.", + retryable: false, + remediation: { + action: "Inspect completed_observation before retrying.", + }, + details: { + completed_observation: { + operation: "inspect_target", + digest: "sha256:abc123", + }, + }, + }), + expectedToken: "completed_observation:", + expectedValue: "sha256:abc123", + }, + { + name: "failure with details.argument_observations", + projection: (): AnalysisErrorProjection => ({ + code: "execution_failure", + category: "execution_failure", + message: "Comparison failed after reading the first argument.", + retryable: false, + remediation: { + action: "Inspect argument_observations before retrying.", + }, + details: { + argument_observations: [ + { argument: "before", observation: { path: "/tmp/before.bin" } }, + ], + }, + }), + expectedToken: "argument_observations", + expectedValue: "/tmp/before.bin", + }, + { + name: "failure with details.captured_output", + projection: (): AnalysisErrorProjection => ({ + code: "execution_failure", + category: "execution_failure", + message: "The native backend exited with status 2.", + retryable: false, + remediation: { + action: "Review captured_output for backend diagnostics.", + }, + details: { + captured_output: { + stderr: "loader: unsupported relocation 0x42", + }, + }, + }), + expectedToken: "captured_output:", + expectedValue: "unsupported relocation 0x42", + }, + { + name: "cleanup_incomplete failure with remaining resources", + projection: (): AnalysisErrorProjection => ({ + code: "cleanup_incomplete", + category: "execution_failure", + message: "Temporary staging directory could not be removed.", + retryable: false, + remediation: { + action: "Review the reported resources before retrying.", + }, + details: { + cleanup: "incomplete", + resources: ["/tmp/rea-staging-42"], + }, + }), + expectedToken: "resources", + expectedValue: "/tmp/rea-staging-42", + }, + { + name: "failure with published evidence_bundle receipt", + projection: (): AnalysisErrorProjection => ({ + code: "execution_failure", + category: "execution_failure", + message: "Analysis stopped after publishing partial evidence.", + retryable: false, + remediation: { + action: "Inspect the published evidence bundle.", + }, + details: { + diagnostics: { + evidence_bundle: { + path: "/tmp/partial-evidence.json", + bytes: 512, + }, + }, + }, + }), + expectedToken: "evidence_bundle:", + expectedValue: "/tmp/partial-evidence.json", + }, + { + name: "failure with secondary_failures carrying partial_observation", + projection: (): AnalysisErrorProjection => ({ + code: "execution_failure", + category: "execution_failure", + message: "Multiple operations failed.", + retryable: false, + remediation: { + action: "Inspect secondary_failures for retained partial results.", + }, + details: { + secondary_failures: [ + { + code: "cancelled", + category: "cancelled", + message: "Secondary step cancelled.", + retryable: true, + remediation: { action: "Reuse partial_observation." }, + details: { + partial_observation: { parsed_sources: ["worker.js"] }, + }, + }, + ], + }, + }), + expectedToken: "partial_observation:", + expectedValue: "worker.js", + }, + ])( + "preserves complete structured output on a TTY for $name", + async ({ projection: build, expectedToken, expectedValue }) => { + const projection = build(); + const { destination, text } = makeDestination(true); + const output = createCliResultOutput( + parseCliOutputArguments(["analyze-javascript-application"]), + destination, + ); + if (output === undefined) throw new Error("Missing CLI result output"); + + expect( + await output.write(projection, { + command: "analyze-javascript-application", + duration: "12ms", + }), + ).toBe(true); + expect(output.handled).toBe(true); + expect(output.failed).toBe(false); + + const rendered = text(); + expect(rendered).toContain(`code: ${projection.code}`); + expect(rendered).toContain(expectedToken); + expect(rendered).toContain(expectedValue); + expect(rendered).not.toContain(`Error (${projection.code}):`); + expect(rendered).not.toContain(CLI_ISSUE_REPORT_URL); + }, + ); +}); diff --git a/tests/boundary/mcp/errorHintExclusionMcp.test.ts b/tests/boundary/mcp/errorHintExclusionMcp.test.ts new file mode 100644 index 00000000000..4cb4a55c190 --- /dev/null +++ b/tests/boundary/mcp/errorHintExclusionMcp.test.ts @@ -0,0 +1,54 @@ +import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server"; +import { describe, expect, it } from "vitest"; + +import { AnalysisUnsupportedTargetError } from "../../../src/domain/analysisErrorCore.js"; +import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; +import { BinaryTargetError } from "../../../src/domain/configurationErrors.js"; +import { createToolResultDelivery } from "../../../src/server/toolResult.js"; + +const CLI_ISSUE_REPORT_URL = "https://github.com/morluto/rea/issues"; + +const delivery = createToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE); + +describe("MCP error delivery excludes CLI-only issue hints", () => { + it.each([ + { + name: "unsupported_target", + error: new AnalysisUnsupportedTargetError( + "inspect-target", + "/tmp/sample.bin", + "Container format is unsupported.", + ), + expectedCode: "unsupported_target", + }, + { + name: "target_unavailable", + error: new BinaryTargetError("/tmp/missing.bin", "missing", { + remediationAction: "Check that the file exists.", + }), + expectedCode: "target_unavailable", + }, + { + name: "file_format_rejected", + error: new BinaryTargetError("/tmp/unknown.bin", "refused", { + constraint: "file_format_rejected", + remediationAction: "Provide a supported binary format.", + }), + expectedCode: "target_unavailable", + }, + ])( + "emits no GitHub issue URL in MCP toErrorToolResult or AnalysisErrorProjection for $name", + ({ error, expectedCode }) => { + const projection = projectAnalysisError(error); + expect(projection.code).toBe(expectedCode); + expect(JSON.stringify(projection)).not.toContain(CLI_ISSUE_REPORT_URL); + + const toolResult = delivery.toErrorToolResult(error, false); + expect(toolResult.isError).toBe(true); + const serialized = JSON.stringify(toolResult); + expect(serialized).toContain(expectedCode); + expect(serialized).not.toContain(CLI_ISSUE_REPORT_URL); + expect(serialized).not.toContain("github.com/morluto/rea/issues"); + }, + ); +});