Skip to content

Commit ba79798

Browse files
juncursoragent
authored andcommitted
fix(cli): refuse JSON spellings doctor ignored and usage-exit-1 on desktop
`--json=true` and Unicode dashes still ran doctor and printed prose as success. Desktop status/show threw a generic Error so usage exited 1. Co-authored-by: Cursor <[email protected]>
1 parent 32c1b02 commit ba79798

5 files changed

Lines changed: 81 additions & 15 deletions

File tree

‎src/cli/claude-desktop.ts‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { writeDesktop3pConfig, type Desktop3pConfigMode, parseDesktop3pModeArgs
1414
import { filterCatalogVisibleModels, desktopVisibleNativeSlugs, nativeContextLimits } from "../codex/catalog";
1515
import { buildClaudeDesktopState, fetchAllModels } from "../server/management-api";
1616
import { findLiveProxy } from "../server/proxy-liveness";
17-
import { runtimeRequest } from "./runtime-api";
17+
import { CliUsageError, runtimeRequest, takeJsonFlag } from "./runtime-api";
1818
import { OPENAI_CODEX_PROVIDER_ID } from "../providers/openai-tiers";
1919

2020
function isFamily(value: string | undefined): value is DesktopFamily {
@@ -119,10 +119,10 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
119119
const nonMode = argv.filter(arg => !["apply", "--static", "--hybrid", "--discovery-only"].includes(arg));
120120
if (nonMode.length > 0) {
121121
console.error(`알 수 없는 인자: ${nonMode.join(" ")}`);
122-
return 1;
122+
return 2;
123123
}
124124
const parsedMode = parseDesktop3pModeArgs(legacyFlags);
125-
if ("error" in parsedMode) { console.error(parsedMode.error); return 1; }
125+
if ("error" in parsedMode) { console.error(parsedMode.error); return 2; }
126126
try {
127127
const config = loadConfig();
128128
const state = await buildClaudeDesktopState(config);
@@ -151,8 +151,9 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
151151
// including staleness, drift and health, which only the running proxy knows. `show`
152152
// reports what this machine would write; `status` reports what is actually in effect.
153153
if (command === "status") {
154-
const wantsJson = argv[1] === "--json";
155-
if (argv.length > 2 || (argv[1] && !wantsJson)) throw new Error("Usage: ocx claude desktop status [--json]");
154+
const rest = argv.slice(1);
155+
const wantsJson = takeJsonFlag(rest);
156+
if (rest.length > 0) throw new CliUsageError("Usage: ocx claude desktop status [--json]");
156157
const live = await runtimeRequest<Record<string, unknown>>("/api/claude-desktop/status", {});
157158
if (wantsJson) console.log(JSON.stringify(live, null, 2));
158159
else {
@@ -164,8 +165,10 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
164165
}
165166
const state = await buildClaudeDesktopState(config);
166167
if (command === "show") {
167-
if (argv.length > 2 || (argv[1] && argv[1] !== "--json")) throw new Error("Usage: ocx claude desktop show [--json]");
168-
if (argv[1] === "--json") console.log(JSON.stringify(state));
168+
const rest = argv.slice(1);
169+
const wantsJson = takeJsonFlag(rest);
170+
if (rest.length > 0) throw new CliUsageError("Usage: ocx claude desktop show [--json]");
171+
if (wantsJson) console.log(JSON.stringify(state));
169172
else {
170173
for (const family of DESKTOP_FAMILIES) {
171174
console.log(`${family.toUpperCase()}${state.profile.defaults[family] ? ` (default: ${state.profile.defaults[family]})` : ""}`);
@@ -178,7 +181,7 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
178181
}
179182
if (command === "move") {
180183
const [, route, familyRaw, ...flags] = argv;
181-
if (!route || !isFamily(familyRaw) || flags.some(flag => flag !== "--default")) throw new Error("Usage: ocx claude desktop move <route> <family> [--default]");
184+
if (!route || !isFamily(familyRaw) || flags.some(flag => flag !== "--default")) throw new CliUsageError("Usage: ocx claude desktop move <route> <family> [--default]");
182185
if (!state.models.some(model => model.route === route && model.available)) throw new Error(`현재 사용할 수 없는 모델입니다: ${route}`);
183186
const profile = moveDesktopRoute(state.profile, route, familyRaw, flags.includes("--default"));
184187
config.claudeCode = { ...(config.claudeCode ?? {}), desktopProfile: profile };
@@ -188,7 +191,7 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
188191
}
189192
if (command === "default") {
190193
const [, familyRaw, routeRaw] = argv;
191-
if (!isFamily(familyRaw) || !routeRaw || argv.length !== 3) throw new Error("Usage: ocx claude desktop default <family> <route|none>");
194+
if (!isFamily(familyRaw) || !routeRaw || argv.length !== 3) throw new CliUsageError("Usage: ocx claude desktop default <family> <route|none>");
192195
const route = routeRaw === "none" ? null : routeRaw;
193196
if (route && !state.models.some(model => model.route === route && model.available)) throw new Error(`현재 사용할 수 없는 모델입니다: ${route}`);
194197
const profile = setDesktopFamilyDefault(state.profile, familyRaw, route);
@@ -199,7 +202,7 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
199202
}
200203
if (command === "export") {
201204
const target = argv[1];
202-
if (!target || argv.length !== 2) throw new Error("Usage: ocx claude desktop export <path|->");
205+
if (!target || argv.length !== 2) throw new CliUsageError("Usage: ocx claude desktop export <path|->");
203206
const json = JSON.stringify(state.profile, null, 2) + "\n";
204207
if (target === "-") process.stdout.write(json);
205208
else writeFileSync(resolve(target), json, { encoding: "utf8", mode: 0o600 });
@@ -208,7 +211,7 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
208211
if (command === "import") {
209212
const source = argv[1];
210213
const flags = argv.slice(2);
211-
if (!source || flags.some(flag => flag !== "--apply")) throw new Error("Usage: ocx claude desktop import <path> [--apply]");
214+
if (!source || flags.some(flag => flag !== "--apply")) throw new CliUsageError("Usage: ocx claude desktop import <path> [--apply]");
212215
const profile = parseDesktopProfile(JSON.parse(readFileSync(resolve(source), "utf8")));
213216
const reconciled = (await buildClaudeDesktopState(config, profile)).profile;
214217
config.claudeCode = { ...(config.claudeCode ?? {}), desktopProfile: reconciled };
@@ -222,9 +225,9 @@ export async function handleClaudeDesktopCommand(argv: string[], deps: ApplyProf
222225
return 0;
223226
}
224227
printDesktopHelp();
225-
return 1;
228+
return 2;
226229
} catch (error) {
227230
console.error(error instanceof Error ? error.message : String(error));
228-
return 1;
231+
return error instanceof CliUsageError ? 2 : 1;
229232
}
230233
}

‎src/cli/dispatch.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { restoreNativeCodexAsync } from "../codex/inject";
2222
import { stripGrokConfig } from "../grok/inject";
2323
import { afterCatalogWriteHandleAppServers } from "../codex/app-server-processes";
2424
import { normalizeUpdateChannel, runGuiUpdateWorker } from "../update/job";
25-
import { takeFlag } from "./runtime-api";
25+
import { isJsonOption, takeFlag } from "./runtime-api";
2626

2727
export interface CliDispatchDeps {
2828
args: string[];
@@ -188,7 +188,7 @@ const commandRunners: Record<string, CommandRunner> = {
188188
// the Codex Log Guard's human output after it returns, so emitting a JSON document here
189189
// would interleave prose with JSON on one stdout -- unparseable, which is worse than the
190190
// ignored flag. The structured-report refactor is tracked as its own work-phase.
191-
if (doctorArgs.includes("--json")) {
191+
if (doctorArgs.some(isJsonOption)) {
192192
console.error("ocx doctor does not support --json yet. Run `ocx doctor` for the human report, or use `ocx status --json` and `ocx ready --json` for machine-readable health.");
193193
return 2;
194194
}

‎src/cli/runtime-api.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ export function takeFlag(args: string[], flag: string): boolean {
116116
return true;
117117
}
118118

119+
/** Unicode dashes that copy-paste and IME input substitute for ASCII `-`. */
120+
const DASH_CLASS = /[\u2010-\u2015\u2212]/g;
121+
122+
/**
123+
* True for `--json`, `--json=true`, `-json`, and Unicode-dash spellings.
124+
* Matching only the exact token `--json` is the same defect logout had: each
125+
* spelling that slips through is a silent success (or, for doctor, prose on
126+
* stdout after the caller asked for JSON).
127+
*/
128+
export function isJsonOption(arg: string): boolean {
129+
const body = arg.replace(DASH_CLASS, "-").replace(/^-+/, "");
130+
return body === "json" || body.startsWith("json=");
131+
}
132+
133+
/** Remove one JSON-request spelling from `args`. Returns whether one was present. */
134+
export function takeJsonFlag(args: string[]): boolean {
135+
const index = args.findIndex(isJsonOption);
136+
if (index === -1) return false;
137+
args.splice(index, 1);
138+
return true;
139+
}
140+
119141
export function takeOption(args: string[], flag: string): string | undefined {
120142
const index = args.indexOf(flag);
121143
if (index === -1) return undefined;

‎tests/claude-desktop-cli.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,19 @@ test("no-arg and legacy mode flags apply Desktop config", async () => {
152152
error.mockRestore();
153153
}
154154
});
155+
156+
test("usage errors on desktop verbs exit 2, not 1", async () => {
157+
const log = spyOn(console, "log").mockImplementation(() => {});
158+
const error = spyOn(console, "error").mockImplementation(() => {});
159+
try {
160+
expect(await handleClaudeDesktopCommand(["status", "--wat"])).toBe(2);
161+
expect(await handleClaudeDesktopCommand(["status", "extra"])).toBe(2);
162+
expect(await handleClaudeDesktopCommand(["show", "--wat"])).toBe(2);
163+
expect(await handleClaudeDesktopCommand(["move"])).toBe(2);
164+
expect(await handleClaudeDesktopCommand(["nope"])).toBe(2);
165+
expect(await handleClaudeDesktopCommand(["apply", "--wat"])).toBe(2);
166+
} finally {
167+
log.mockRestore();
168+
error.mockRestore();
169+
}
170+
});

‎tests/cli-dispatch.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,28 @@ describe("logout rejects anything that is not a possible provider id", () => {
390390
expect(getAccountSet("github-copilot")).toBeNull();
391391
});
392392
});
393+
394+
describe("doctor refuses --json rather than printing prose as success", () => {
395+
const runDoctor = async (flag: string): Promise<number> => {
396+
const argv = ["doctor", flag];
397+
const log = console.log;
398+
const error = console.error;
399+
console.log = () => {};
400+
console.error = () => {};
401+
try {
402+
return await dispatchCommand(
403+
{ kind: "command", command: "doctor", args: argv },
404+
{ ...fakeDeps, args: argv } as unknown as CliDispatchDeps,
405+
);
406+
} finally {
407+
console.log = log;
408+
console.error = error;
409+
}
410+
};
411+
412+
test("exact --json, --json=true, and Unicode dashes all exit 2", async () => {
413+
for (const flag of ["--json", "--json=true", "\u2014json"]) {
414+
expect(await runDoctor(flag), `${JSON.stringify(flag)} must be refused`).toBe(2);
415+
}
416+
});
417+
});

0 commit comments

Comments
 (0)