Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(cli): give the conflict error to the caller that asked for records

KageBinary41ca58060a85
diff --git a/ix-cli/src/cli/__tests__/context-mode-conflict.test.ts b/ix-cli/src/cli/__tests__/context-mode-conflict.test.ts
index a37b22bb..cc1e55af 100644
--- a/ix-cli/src/cli/__tests__/context-mode-conflict.test.ts
+++ b/ix-cli/src/cli/__tests__/context-mode-conflict.test.ts
@@ -167,10 +167,16 @@ function runProgram(
const stderr: string[] = [];
const origStdout = process.stdout.write.bind(process.stdout);
const origErr = console.error;
+ // Both, and console.log is the one that matters: under vitest `console` is
+ // replaced wholesale, so it never reaches `process.stdout.write` and a
+ // capture that patches only the stream sees nothing — which makes
+ // `expect(stdout).toBe("")` pass no matter what the command printed.
+ const origLog = console.log;
process.stdout.write = ((chunk: unknown) => {
stdout.push(String(chunk));
return true;
}) as typeof process.stdout.write;
+ console.log = (...a: unknown[]) => void stdout.push(a.join(" ") + "\n");
console.error = (...a: unknown[]) => void stderr.push(a.join(" "));
let code: number | string | undefined;
@@ -184,6 +190,7 @@ function runProgram(
} finally {
code = process.exitCode;
process.stdout.write = origStdout;
+ console.log = origLog;
console.error = origErr;
}
return { stderr: stderr.join("\n"), stdout: stdout.join(""), exitCode: code };
@@ -221,6 +228,53 @@ describe("ix diff action surfaces mode conflicts on stderr and exits 1", () => {
});
});
+describe("a caller that asked for records gets the error as a record", () => {
+ // An agent is told to pass `--format llm` unconditionally, so an error it
+ // cannot read is an error it cannot act on. Same shape the rest of the CLI
+ // emits (imports/trace/smells/locate/callers) and the one docs/llm-format.md
+ // specifies: on stdout, in-stream, exit code still non-zero.
+ it("emits an error record for ix context and keeps the exit code", () => {
+ const r = runProgram(registerContextCommand, [
+ "context",
+ "--resume",
+ "x",
+ "--diff",
+ "y",
+ "--format",
+ "llm",
+ ]);
+ expect(r.exitCode).toBe(1);
+ expect(r.stdout).toMatch(/^error code=mode_conflict message="/);
+ // One record, on one line, with the message quoted rather than bare.
+ expect(r.stdout.trimEnd().split("\n")).toHaveLength(1);
+ expect(r.stderr).toBe("");
+ });
+
+ it("emits an error record for ix diff and keeps the exit code", () => {
+ const r = runProgram(registerDiffCommand, [
+ "diff",
+ "3",
+ "5",
+ "--summary",
+ "--content",
+ "--format",
+ "llm",
+ ]);
+ expect(r.exitCode).toBe(1);
+ expect(r.stdout).toMatch(/^error code=mode_conflict message="/);
+ expect(r.stderr).toBe("");
+ });
+
+ it("still writes prose to stderr for every other format", () => {
+ for (const format of ["text", "json"]) {
+ const r = runProgram(registerContextCommand, ["context", "--resume", "x", "--diff", "y", "--format", format]);
+ expect(r.exitCode).toBe(1);
+ expect(r.stderr).toMatch(/^Error:/m);
+ expect(r.stdout).toBe("");
+ }
+ });
+});
+
describe("ix help coverage stays intact (smoke)", () => {
it("context help still lists --resume, --diff, --save, --out", () => {
const program = new Command();
diff --git a/ix-cli/src/cli/commands/context.ts b/ix-cli/src/cli/commands/context.ts
index de771448..ed39b5e0 100644
--- a/ix-cli/src/cli/commands/context.ts
+++ b/ix-cli/src/cli/commands/context.ts
@@ -16,7 +16,7 @@ import type {
} from "../../client/types.js";
import { getEndpoint } from "../config.js";
import { collectFacts, type EntityFacts } from "../explain/facts.js";
-import { printLlmLines } from "../llm.js";
+import { llmError, printLlmLines } from "../llm.js";
import { parsePickOption } from "../options.js";
import { resolveFileOrEntity } from "../resolve.js";
import { createStaleProbe } from "../stale.js";
@@ -140,7 +140,7 @@ export function registerContextCommand(program: Command): void {
.action(async (target: string | undefined, opts: ContextOptions) => {
const conflict = detectContextModeConflict(opts);
if (conflict) {
- reportContextModeConflict(conflict);
+ reportContextModeConflict(conflict, opts.format);
return;
}
if (opts.resume) {
@@ -325,9 +325,18 @@ export function detectContextModeConflict(
return undefined;
}
-/** Render a detected mode conflict and mark the process exit code. */
-export function reportContextModeConflict(message: string): void {
- console.error(chalk.red("Error:"), message);
+/**
+ * Render a detected mode conflict and mark the process exit code.
+ *
+ * `--format llm` gets the record form the rest of the CLI emits for errors —
+ * `error code=... message="..."`, on stdout, with the exit code still non-zero
+ * (imports.ts, trace.ts, smells.ts, locate.ts, callers.ts all do this, and
+ * docs/llm-format.md specifies the shape). An agent is told to pass the flag
+ * unconditionally, so an error it cannot read is an error it cannot act on.
+ */
+export function reportContextModeConflict(message: string, format?: string): void {
+ if (format === "llm") console.log(llmError("mode_conflict", message));
+ else console.error(chalk.red("Error:"), message);
process.exitCode = 1;
}
diff --git a/ix-cli/src/cli/commands/diff.ts b/ix-cli/src/cli/commands/diff.ts
index ab116464..f30a0e61 100644
--- a/ix-cli/src/cli/commands/diff.ts
+++ b/ix-cli/src/cli/commands/diff.ts
@@ -7,6 +7,12 @@ import type { Command } from "commander";
import chalk from "chalk";
import { IxClient } from "../../client/api.js";
+import { getEndpoint } from "../config.js";
+import { resolveFileOrEntity, resolveEntityFull, printResolved, looksFileLike, type ResolvedEntity } from "../resolve.js";
+import { formatDiff, relativePath } from "../format.js";
+import { llmLine, llmError } from "../llm.js";
+import { parsePickOption } from "../options.js";
+
/**
* Subset of `DiffOptions` consumed by `detectDiffModeConflict`. Kept as a
* structural type so the detector can be unit-tested without driving Commander.
@@ -48,16 +54,20 @@ export function detectDiffModeConflict(
return undefined;
}
-/** Render a detected mode conflict and mark the process exit code. */
-export function reportDiffModeConflict(message: string): void {
- console.error(chalk.red("Error:"), message);
+/**
+ * Render a detected mode conflict and mark the process exit code.
+ *
+ * `--format llm` gets the record form the rest of the CLI emits for errors —
+ * `error code=... message="..."`, on stdout, with the exit code still non-zero
+ * (imports.ts, trace.ts, smells.ts, locate.ts, callers.ts all do this, and
+ * docs/llm-format.md specifies the shape). An agent is told to pass the flag
+ * unconditionally, so an error it cannot read is an error it cannot act on.
+ */
+export function reportDiffModeConflict(message: string, format?: string): void {
+ if (format === "llm") console.log(llmError("mode_conflict", message));
+ else console.error(chalk.red("Error:"), message);
process.exitCode = 1;
}
-import { getEndpoint } from "../config.js";
-import { resolveFileOrEntity, resolveEntityFull, printResolved, looksFileLike, type ResolvedEntity } from "../resolve.js";
-import { formatDiff, relativePath } from "../format.js";
-import { llmLine, llmError } from "../llm.js";
-import { parsePickOption } from "../options.js";
/** Render a `--summary` diff as a single llm record. */
function renderDiffSummaryLlm(result: any): string {
@@ -480,7 +490,7 @@ export function registerDiffCommand(program: Command): void {
}) => {
const conflict = detectDiffModeConflict(opts);
if (conflict) {
- reportDiffModeConflict(conflict);
+ reportDiffModeConflict(conflict, opts.format);
return;
}
const client = new IxClient(getEndpoint());