A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI] Fail fast on CLI flags with missing or invalid values (#2216)
Seth Kartene334a8b460f0
diff --git a/packages/coding-agent/.changes/eng-6082-cli-strict-value-flags.md b/packages/coding-agent/.changes/eng-6082-cli-strict-value-flags.mdnew file mode 100644index 0000000000..d498c60459--- /dev/null+++ b/packages/coding-agent/.changes/eng-6082-cli-strict-value-flags.md@@ -0,0 +1 @@+- Fixed CLI value flags (--model, --provider, and 15 others) being silently swallowed when their value was missing, and invalid --mode values being ignored; both now fail with a clear error.diff --git a/packages/coding-agent/src/cli/args.ts b/packages/coding-agent/src/cli/args.tsindex d35576304b..7f1ec2418f 100644--- a/packages/coding-agent/src/cli/args.ts+++ b/packages/coding-agent/src/cli/args.ts@@ -62,6 +62,10 @@ export interface Args {const REMOVED_BUILTIN_TOOL_NAMES = new Set(["read", "write", "grep", "find", "ls"]);const BUILTIN_TOOL_NAMES = ["ipython"];+/** Value flags whose free-form text (an objective, a gate command) may legitimately start with a dash. */+const FREEFORM_VALUE_FLAGS = new Set(["--goal", "--autonomous-gate"]);+/** Prompt value flags whose text may start with a long-option-looking token, e.g. YAML frontmatter ("---"). */+const PROMPT_VALUE_FLAGS = new Set(["--system-prompt", "--append-system-prompt"]);export const INTERNAL_RUNTIME_COMMAND_MARKER = "\0prime-agent-runtime-command";@@ -99,13 +103,22 @@ export function parseArgs(args: string[]): Args {result.help = true;} else if (arg === "--version" || arg === "-v") {result.version = true;- } else if (arg === "--mode" && i + 1 < args.length) {- const mode = args[++i];- if (mode === "text" || mode === "json" || mode === "rpc" || mode === "acp" || mode === "daemon") {- result.mode = mode;+ } else if (arg === "--mode") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ const mode = args[++i];+ if (mode === "text" || mode === "json" || mode === "rpc" || mode === "acp" || mode === "daemon") {+ result.mode = mode;+ } else {+ result.diagnostics.push({+ type: "error",+ message: `Invalid --mode "${mode}". Valid values: text, json, rpc, acp, daemon`,+ });+ }+ }+ } else if (arg === "--daemon-socket") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.daemonSocket = args[++i];}- } else if (arg === "--daemon-socket" && i + 1 < args.length) {- result.daemonSocket = args[++i];} else if (arg === "--continue" || arg === "-c") {result.continue = true;} else if (arg === "--resume" || arg === "-r") {@@ -128,52 +141,74 @@ export function parseArgs(args: string[]): Args {} else {result.resume = value;}- } else if (arg === "--provider" && i + 1 < args.length) {- result.provider = args[++i];- } else if (arg === "--model" && i + 1 < args.length) {- result.model = args[++i];- } else if (arg === "--api-key" && i + 1 < args.length) {- result.apiKey = args[++i];- } else if (arg === "--cwd" && i + 1 < args.length) {- result.cwd = args[++i];- } else if (arg === "--system-prompt" && i + 1 < args.length) {- result.systemPrompt = args[++i];- } else if (arg === "--append-system-prompt" && i + 1 < args.length) {- result.appendSystemPrompt = result.appendSystemPrompt ?? [];- result.appendSystemPrompt.push(args[++i]);+ } else if (arg === "--provider") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.provider = args[++i];+ }+ } else if (arg === "--model") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.model = args[++i];+ }+ } else if (arg === "--api-key") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.apiKey = args[++i];+ }+ } else if (arg === "--cwd") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.cwd = args[++i];+ }+ } else if (arg === "--system-prompt") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.systemPrompt = args[++i];+ }+ } else if (arg === "--append-system-prompt") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.appendSystemPrompt = result.appendSystemPrompt ?? [];+ result.appendSystemPrompt.push(args[++i]);+ }} else if (arg === "--no-session") {result.noSession = true;- } else if (arg === "--fork" && i + 1 < args.length) {- result.fork = args[++i];- } else if (arg === "--session-dir" && i + 1 < args.length) {- result.sessionDir = args[++i];- } else if (arg === "--models" && i + 1 < args.length) {- result.models = args[++i].split(",").map((s) => s.trim());+ } else if (arg === "--fork") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.fork = args[++i];+ }+ } else if (arg === "--session-dir") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.sessionDir = args[++i];+ }+ } else if (arg === "--models") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.models = args[++i].split(",").map((s) => s.trim());+ }} else if (arg === "--no-tools" || arg === "-nt") {result.noTools = true;} else if (arg === "--no-builtin-tools" || arg === "-nbt") {result.noBuiltinTools = true;- } else if ((arg === "--tools" || arg === "-t") && i + 1 < args.length) {- result.tools = args[++i]- .split(",")- .map((s) => s.trim())- .filter((name) => name.length > 0);- const removedTools = result.tools.filter((name) => REMOVED_BUILTIN_TOOL_NAMES.has(name));- if (removedTools.length > 0) {- result.diagnostics.push({- type: "error",- message: `Unknown built-in tool(s): ${removedTools.join(", ")}. Available built-in tools: ${BUILTIN_TOOL_NAMES.join(", ")}`,- });+ } else if (arg === "--tools" || arg === "-t") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.tools = args[++i]+ .split(",")+ .map((s) => s.trim())+ .filter((name) => name.length > 0);+ const removedTools = result.tools.filter((name) => REMOVED_BUILTIN_TOOL_NAMES.has(name));+ if (removedTools.length > 0) {+ result.diagnostics.push({+ type: "error",+ message: `Unknown built-in tool(s): ${removedTools.join(", ")}. Available built-in tools: ${BUILTIN_TOOL_NAMES.join(", ")}`,+ });+ }}- } else if (arg === "--thinking" && i + 1 < args.length) {- const level = args[++i];- if (isValidThinkingLevel(level)) {- result.thinking = level;- } else {- result.diagnostics.push({- type: "error",- message: `Invalid thinking level "${level}". Valid values: ${THINKING_LEVELS.join(", ")}`,- });+ } else if (arg === "--thinking") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ const level = args[++i];+ if (isValidThinkingLevel(level)) {+ result.thinking = level;+ } else {+ result.diagnostics.push({+ type: "error",+ message: `Invalid thinking level "${level}". Valid values: ${THINKING_LEVELS.join(", ")}`,+ });+ }}} else if (arg === "--print" || arg === "-p") {result.print = true;@@ -199,20 +234,28 @@ export function parseArgs(args: string[]): Args {type: "error",message: `--export was removed. Use "${APP_NAME} session export <file> [output]".`,});- } else if ((arg === "--extension" || arg === "-e") && i + 1 < args.length) {- result.extensions = result.extensions ?? [];- result.extensions.push(args[++i]);+ } else if (arg === "--extension" || arg === "-e") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.extensions = result.extensions ?? [];+ result.extensions.push(args[++i]);+ }} else if (arg === "--no-extensions" || arg === "-ne") {result.noExtensions = true;- } else if (arg === "--skill" && i + 1 < args.length) {- result.skills = result.skills ?? [];- result.skills.push(args[++i]);- } else if (arg === "--prompt-template" && i + 1 < args.length) {- result.promptTemplates = result.promptTemplates ?? [];- result.promptTemplates.push(args[++i]);- } else if (arg === "--theme" && i + 1 < args.length) {- result.themes = result.themes ?? [];- result.themes.push(args[++i]);+ } else if (arg === "--skill") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.skills = result.skills ?? [];+ result.skills.push(args[++i]);+ }+ } else if (arg === "--prompt-template") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.promptTemplates = result.promptTemplates ?? [];+ result.promptTemplates.push(args[++i]);+ }+ } else if (arg === "--theme") {+ if (hasRequiredOptionValue(args, i, arg, result)) {+ result.themes = result.themes ?? [];+ result.themes.push(args[++i]);+ }} else if (arg === "--no-skills" || arg === "-ns") {result.noSkills = true;} else if (arg === "--no-prompt-templates" || arg === "-np") {@@ -329,7 +372,19 @@ export function parseArgs(args: string[]): Args {function hasRequiredOptionValue(args: string[], index: number, flag: string, result: Args): boolean {const next = args[index + 1];- if (next === undefined || next.startsWith("--")) {+ // Prompt values are arbitrary text, even long-option-looking text such as+ // YAML frontmatter ("---"); free-form value flags accept dash-prefixed+ // text; every other value flag treats an option-looking token as a missing+ // value so it still parses as a flag. The standalone "--" delimiter is+ // never a value; the main loop consumes it so the remaining tokens stay+ // positional.+ const valueMayStartWithDash = FREEFORM_VALUE_FLAGS.has(flag);+ const valueIsArbitraryPromptText = PROMPT_VALUE_FLAGS.has(flag);+ if (+ next === undefined ||+ next === "--" ||+ (!valueIsArbitraryPromptText && next.startsWith(valueMayStartWithDash ? "--" : "-"))+ ) {result.diagnostics.push({ type: "error", message: `${flag} requires a value` });return false;}diff --git a/packages/coding-agent/test/args.test.ts b/packages/coding-agent/test/args.test.tsindex 4aa4ae0f9f..af71ed7362 100644--- a/packages/coding-agent/test/args.test.ts+++ b/packages/coding-agent/test/args.test.ts@@ -610,6 +610,19 @@ describe("parseArgs", () => {expect(result.unknownFlags.size).toBe(0);});+ test("value flags do not consume the end-of-options delimiter as a value", () => {+ const prompt = parseArgs(["--system-prompt", "--", "--model", "foo"]);+ expect(prompt.systemPrompt).toBeUndefined();+ expect(prompt.model).toBeUndefined();+ expect(prompt.messages).toEqual(["--model", "foo"]);+ expect(prompt.diagnostics).toEqual([{ type: "error", message: "--system-prompt requires a value" }]);++ const appended = parseArgs(["--append-system-prompt", "--", "Run the suite"]);+ expect(appended.appendSystemPrompt).toBeUndefined();+ expect(appended.messages).toEqual(["Run the suite"]);+ expect(appended.diagnostics).toEqual([{ type: "error", message: "--append-system-prompt requires a value" }]);+ });+test("parses --goal as a string", () => {const result = parseArgs(["--goal", "Write a paper"]);expect(result.goal).toBe("Write a paper");@@ -680,3 +693,147 @@ describe("parseArgs", () => {});});});++describe("value flags require values", () => {+ test("--model without a value is an error, not an extension flag", () => {+ const result = parseArgs(["--model"]);+ expect(result.model).toBeUndefined();+ expect(result.unknownFlags.has("model")).toBe(false);+ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: "--model requires a value",+ });+ });++ test("value flags followed by another flag report missing values", () => {+ const result = parseArgs(["--model", "--provider", "anthropic"]);+ expect(result.provider).toBe("anthropic");+ expect(result.model).toBeUndefined();+ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: "--model requires a value",+ });+ });++ test.each([+ "--provider",+ "--api-key",+ "--cwd",+ "--fork",+ "--session-dir",+ "--models",+ "--daemon-socket",+ "--system-prompt",+ ])("%s without a value reports a missing-value error", (flag) => {+ const result = parseArgs([flag]);+ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: `${flag} requires a value`,+ });+ expect(result.unknownFlags.has(flag.slice(2))).toBe(false);+ });++ test("invalid --mode reports valid values instead of being ignored", () => {+ const result = parseArgs(["--mode", "interactive"]);+ expect(result.mode).toBeUndefined();+ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: 'Invalid --mode "interactive". Valid values: text, json, rpc, acp, daemon',+ });+ });++ test("--mode still accepts valid values", () => {+ const result = parseArgs(["--mode", "json"]);+ expect(result.mode).toBe("json");+ expect(result.diagnostics.some((d) => d.type === "error")).toBe(false);+ });++ test("list-style flags without values report missing values", () => {+ const result = parseArgs(["--theme"]);+ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: "--theme requires a value",+ });+ });++ test("values are still consumed when present", () => {+ const result = parseArgs(["--model", "claude-sonnet-4-5", "--fork", "abc"]);+ expect(result.model).toBe("claude-sonnet-4-5");+ expect(result.fork).toBe("abc");+ expect(result.diagnostics.some((d) => d.type === "error")).toBe(false);+ });++ test("--model followed by a short option is not consumed as its value", () => {+ const result = parseArgs(["--model", "-t", "ipython"]);++ expect(result.model).toBeUndefined();+ expect(result.tools).toEqual(["ipython"]);+ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: "--model requires a value",+ });+ });++ test("value flags followed by a short option report missing values", () => {+ const result = parseArgs(["--thinking", "-x"]);++ expect(result.thinking).toBeUndefined();+ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: "--thinking requires a value",+ });+ });++ test.each(["--provider", "--api-key", "--cwd", "--fork", "--session-dir", "--models", "--daemon-socket"])(+ "%s followed by a short option reports a missing value",+ (flag) => {+ const result = parseArgs([flag, "-x"]);++ expect(result.diagnostics).toContainEqual({+ type: "error",+ message: `${flag} requires a value`,+ });+ },+ );++ test("free-form value flags keep accepting dash-prefixed values", () => {+ const goal = parseArgs(["--goal", "-p"]);+ expect(goal.goal).toBe("-p");+ expect(goal.print).toBeUndefined();+ expect(goal.diagnostics.some((d) => d.type === "error")).toBe(false);++ const gate = parseArgs(["--autonomous-gate", "-x npm test"]);+ expect(gate.autonomousGates).toEqual(["-x npm test"]);+ expect(gate.diagnostics.some((d) => d.type === "error")).toBe(false);++ const prompt = parseArgs(["--system-prompt", "- Respond only with JSON"]);+ expect(prompt.systemPrompt).toBe("- Respond only with JSON");+ expect(prompt.diagnostics.some((d) => d.type === "error")).toBe(false);++ const appended = parseArgs(["--append-system-prompt", "- Be terse"]);+ expect(appended.appendSystemPrompt).toEqual(["- Be terse"]);+ expect(appended.diagnostics.some((d) => d.type === "error")).toBe(false);+ });++ test("prompt value flags keep accepting YAML frontmatter", () => {+ const frontmatter = "---\nname: strict\n---\nYou output only JSON.";++ const prompt = parseArgs(["--system-prompt", frontmatter]);+ expect(prompt.systemPrompt).toBe(frontmatter);+ expect(prompt.diagnostics.some((d) => d.type === "error")).toBe(false);++ const appended = parseArgs(["--append-system-prompt", frontmatter]);+ expect(appended.appendSystemPrompt).toEqual([frontmatter]);+ expect(appended.diagnostics.some((d) => d.type === "error")).toBe(false);+ });++ test("goal and gate flags still reject long-option-looking values", () => {+ const goal = parseArgs(["--goal", "--verbose"]);+ expect(goal.goal).toBeUndefined();+ expect(goal.diagnostics).toContainEqual({ type: "error", message: "--goal requires a value" });++ const gate = parseArgs(["--autonomous-gate", "---run"]);+ expect(gate.autonomousGates).toBeUndefined();+ expect(gate.diagnostics).toContainEqual({ type: "error", message: "--autonomous-gate requires a value" });+ });+});