Skip to content

fix(codex): place -C working-dir flag before exec subcommand (#959)#984

Closed
chr1syy wants to merge 1 commit into
RunMaestro:rcfrom
chr1syy:fix/959-codex-cwd-flag-placement
Closed

fix(codex): place -C working-dir flag before exec subcommand (#959)#984
chr1syy wants to merge 1 commit into
RunMaestro:rcfrom
chr1syy:fix/959-codex-cwd-flag-placement

Conversation

@chr1syy
Copy link
Copy Markdown
Contributor

@chr1syy chr1syy commented May 11, 2026

Summary

Fixes #959. The Codex CLI parses -C / --cd as a root-level global flag and silently ignores it when it appears after the exec subcommand. Both buildAgentArgs (desktop / Auto Run / Cue) and the CLI's spawnJsonLineAgent (maestro-cli send / playbooks) were appending workingDirArgs after batchModePrefix, producing:

codex exec --dangerously-bypass-... --skip-git-repo-check --json -C D:\Projects\MyProject -- "prompt"

Codex silently ignored the -C and fell back to its own cwd resolution — most visible on Windows where Codex also has process-cwd resolution bugs (openai/codex#9671).

After:

codex -C D:\Projects\MyProject exec --dangerously-bypass-... --skip-git-repo-check --json -- "prompt"

Implementation

  • Added an opt-in workingDirArgsBeforeBatchPrefix?: boolean on the agent definition. Codex sets it to true.
  • buildAgentArgs now prepends workingDirArgs before batchModePrefix when this flag is set; otherwise it preserves the current append-after-prefix behavior.
  • CLI spawnJsonLineAgent does the same for Codex — preserves the existing Codex-only scoping so Droid's CLI path is unchanged (Droid's --cwd is a subcommand-scoped flag and was never applied by the CLI path).
  • Droid (--cwd) and Gemini (--include-directories) remain unaffected.

Tests

  • agent-args.test.ts: new tests verifying (1) Codex ordering with -C before exec, (2) Droid ordering preserves --cwd after exec, (3) no duplication when flag is set.
  • definitions.test.ts: asserts Codex has workingDirArgsBeforeBatchPrefix: true and Droid does not.
  • agent-spawner.test.ts: extended the existing Codex spawn test to assert -C precedes exec.

Test plan

  • npx vitest run src/__tests__/main/utils/agent-args.test.ts src/__tests__/main/agents/definitions.test.ts — 101 passing
  • npx vitest run src/__tests__/cli/services/agent-spawner.test.ts — 113 passing
  • npx vitest run src/__tests__/main/ipc/handlers/agents.test.ts src/__tests__/integration/provider-integration.test.ts src/__tests__/integration/group-chat-integration.test.ts — 73 passing
  • Prettier and ESLint clean on touched files
  • Manual verification on Windows with a Codex agent in a non-default project root (recommended)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Fixed Codex's working directory flag handling to ensure correct command-line argument ordering, resolving cases where the flag would be ignored.
  • Tests

    • Added comprehensive regression test coverage for working directory argument placement across different agent configurations to prevent future regressions.

Review Change Stack

…tro#959)

Codex CLI parses -C / --cd as a root-level global flag and silently ignores
it when it appears after the `exec` subcommand. Both `buildAgentArgs` and the
CLI's `spawnJsonLineAgent` were appending workingDirArgs after batchModePrefix,
producing `codex exec ... -C <dir> ...` which left Codex running in the wrong
cwd — most visible on Windows where Codex also has process-cwd resolution
bugs (openai/codex#9671).

Add an opt-in `workingDirArgsBeforeBatchPrefix` flag on the agent definition
and set it on Codex. Subcommand-scoped working-dir flags (Droid `--cwd`,
Gemini `--include-directories`) keep their current append-after-subcommand
position.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

📝 Walkthrough

Walkthrough

This PR fixes issue #959 by enabling Codex's working directory flag (-C) to precede the exec subcommand in CLI arguments. It introduces a new workingDirArgsBeforeBatchPrefix configuration flag, updates argument-building logic to conditionally place working-directory args before batch-mode prefixes, and adds comprehensive regression tests covering Codex, factory-droid, and deduplication scenarios.

Changes

Codex working-directory argument ordering

Layer / File(s) Summary
Agent configuration model
src/main/agents/definitions.ts
AgentConfig interface adds optional workingDirArgsBeforeBatchPrefix?: boolean field; Codex agent definition sets workingDirArgsBeforeBatchPrefix: true with documentation explaining -C/--cd must precede exec.
Argument building conditional logic
src/main/utils/agent-args.ts
buildAgentArgs prepends workingDirArgs(options.cwd) before batch-mode parsing when agent.workingDirArgsBeforeBatchPrefix is true; post-batch append path is gated to run only when the flag is false.
Agent spawn command integration
src/cli/services/agent-spawner.ts
spawnJsonLineAgent conditionally inserts working-directory args earlier (before batchModePrefix); prior insertion point is removed from its later location in the flow.
Regression test coverage
src/__tests__/main/agents/definitions.test.ts, src/__tests__/main/utils/agent-args.test.ts, src/__tests__/cli/services/agent-spawner.test.ts
Five test cases verify Codex argument ordering, factory-droid subcommand-scoped behavior, deduplication, and spawn-level assertion that -C precedes exec.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • RunMaestro/Maestro#959: Directly implements the fix for Codex's working-directory flag ordering, where -C was being ignored when placed after the exec subcommand.

Possibly related PRs

  • RunMaestro/Maestro#862: Modifies the same agent-spawning code paths and extends agent definitions to control CLI batch-spawn behavior.
  • RunMaestro/Maestro#888: Updates agent spawn argument construction and environment/spawn handling in related utility functions.

Suggested labels

ready to merge

Poem

🐇 A flag that falls right before the exec,
No more ignored working dirs—now what the heck!
Codex commands flow in the proper way,
Regression tests guard the path, hooray!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and clearly summarizes the main change: fixing Codex's working directory flag placement before the exec subcommand to resolve issue #959.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 11, 2026

Greptile Summary

This PR fixes a silent argument-ordering bug where Codex's -C working-directory flag was appended after the exec subcommand, causing Codex to ignore it (most visibly on Windows). The fix moves workingDirArgs before batchModePrefix for agents that opt in via a new workingDirArgsBeforeBatchPrefix flag.

  • buildAgentArgs now prepends workingDirArgs before batchModePrefix when workingDirArgsBeforeBatchPrefix is true, and guards the existing post-append path with the inverse condition to prevent duplication.
  • spawnJsonLineAgent (CLI path) moves the Codex working-dir args to be pushed before batchModePrefix, but uses a hardcoded toolType === 'codex' guard rather than the generic flag — creating a divergence between the two code paths that could silently break a future agent that also sets the flag.
  • Tests cover Codex ordering, Droid ordering preservation, no-duplication, and extend the existing CLI spawner test to assert -C precedes exec.

Confidence Score: 4/5

Safe to merge — the core ordering fix is correct in both code paths and is well-covered by new tests.

The spawnJsonLineAgent CLI path uses a hardcoded toolType === 'codex' guard rather than the generic workingDirArgsBeforeBatchPrefix flag introduced in this PR. Any future agent that sets the flag will work correctly through buildAgentArgs but will have its working-dir args silently misplaced through the CLI path until a developer notices and manually adds another condition.

src/cli/services/agent-spawner.ts — the hardcoded toolType check diverges from the generic flag-based approach used everywhere else.

Important Files Changed

Filename Overview
src/main/utils/agent-args.ts Adds pre-batchModePrefix insertion of workingDirArgs when workingDirArgsBeforeBatchPrefix is set; guards the existing post-append path with the inverse condition to prevent duplication. Logic is correct.
src/main/agents/definitions.ts Adds workingDirArgsBeforeBatchPrefix field to AgentConfig and sets it to true on the Codex definition. Clean, minimal change.
src/cli/services/agent-spawner.ts Moves Codex workingDirArgs prepend to before batchModePrefix using a hardcoded toolType === 'codex' check, diverging from the generic flag-based approach used in buildAgentArgs.
src/tests/main/utils/agent-args.test.ts Three new tests cover Codex ordering, Droid ordering preservation, and no-duplication; all look correct and sufficient.
src/tests/main/agents/definitions.test.ts Two new tests assert Codex has workingDirArgsBeforeBatchPrefix: true and Droid does not. Good regression guards.
src/tests/cli/services/agent-spawner.test.ts Extends existing Codex spawn test to assert -C precedes exec; directly validates the bug fix.

Reviews (1): Last reviewed commit: "fix(codex): place -C working-dir flag be..." | Re-trigger Greptile

Comment on lines +583 to +585
if (toolType === 'codex' && def?.workingDirArgs) {
preOverrideArgs.push(...def.workingDirArgs(cwd));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Using the generic def?.workingDirArgsBeforeBatchPrefix flag instead of a hardcoded toolType === 'codex' check would keep this path consistent with buildAgentArgs and make the opt-in self-describing — any future agent with the flag set would be handled automatically.

Suggested change
if (toolType === 'codex' && def?.workingDirArgs) {
preOverrideArgs.push(...def.workingDirArgs(cwd));
}
if (def?.workingDirArgsBeforeBatchPrefix && def?.workingDirArgs) {
preOverrideArgs.push(...def.workingDirArgs(cwd));
}

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/cli/services/agent-spawner.ts`:
- Around line 577-586: The hardcoded check for toolType === 'codex' should be
replaced by the definition flag; update the conditional in agent-spawner.ts to
use def?.workingDirArgsBeforeBatchPrefix (and def?.workingDirArgs) instead of
toolType === 'codex' so that when a definition opts into
workingDirArgsBeforeBatchPrefix you call
preOverrideArgs.push(...def.workingDirArgs(cwd)); keep the existing push
behavior and null-safe checks (use def?.workingDirArgs) and do not change
ordering logic beyond switching the guard to
def.workingDirArgsBeforeBatchPrefix.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f54f8187-3d2d-4bef-acf5-0e53a1f2044a

📥 Commits

Reviewing files that changed from the base of the PR and between 56ece61 and 5bfd143.

📒 Files selected for processing (6)
  • src/__tests__/cli/services/agent-spawner.test.ts
  • src/__tests__/main/agents/definitions.test.ts
  • src/__tests__/main/utils/agent-args.test.ts
  • src/cli/services/agent-spawner.ts
  • src/main/agents/definitions.ts
  • src/main/utils/agent-args.ts

Comment on lines +577 to +586

// Codex's `-C` / `--cd` is a root-level global flag and is silently ignored
// when it appears after the `exec` subcommand (issue #959 /
// https://github.com/openai/codex/issues/9671). Prepend the working-dir
// args BEFORE batchModePrefix so the final ordering is
// `codex -C <dir> exec ...` instead of `codex exec ... -C <dir>`.
if (toolType === 'codex' && def?.workingDirArgs) {
preOverrideArgs.push(...def.workingDirArgs(cwd));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Use workingDirArgsBeforeBatchPrefix flag instead of hardcoding agent type.

The hardcoded check toolType === 'codex' defeats the purpose of introducing the generic workingDirArgsBeforeBatchPrefix configuration flag in definitions.ts. If another agent needs the same behavior in the future, this code would require modification.

♻️ Refactor to use the flag
-	// Codex's `-C` / `--cd` is a root-level global flag and is silently ignored
-	// when it appears after the `exec` subcommand (issue `#959` /
-	// https://github.com/openai/codex/issues/9671). Prepend the working-dir
-	// args BEFORE batchModePrefix so the final ordering is
-	// `codex -C <dir> exec ...` instead of `codex exec ... -C <dir>`.
-	if (toolType === 'codex' && def?.workingDirArgs) {
+	// Some agents (e.g., Codex) require workingDirArgs to precede the batch-mode
+	// subcommand because the flag is parsed as a root-level global. Prepend here
+	// when workingDirArgsBeforeBatchPrefix is set (see definitions.ts and `#959`).
+	if (def?.workingDirArgs && def?.workingDirArgsBeforeBatchPrefix) {
 		preOverrideArgs.push(...def.workingDirArgs(cwd));
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Codex's `-C` / `--cd` is a root-level global flag and is silently ignored
// when it appears after the `exec` subcommand (issue #959 /
// https://github.com/openai/codex/issues/9671). Prepend the working-dir
// args BEFORE batchModePrefix so the final ordering is
// `codex -C <dir> exec ...` instead of `codex exec ... -C <dir>`.
if (toolType === 'codex' && def?.workingDirArgs) {
preOverrideArgs.push(...def.workingDirArgs(cwd));
}
// Some agents (e.g., Codex) require workingDirArgs to precede the batch-mode
// subcommand because the flag is parsed as a root-level global. Prepend here
// when workingDirArgsBeforeBatchPrefix is set (see definitions.ts and `#959`).
if (def?.workingDirArgs && def?.workingDirArgsBeforeBatchPrefix) {
preOverrideArgs.push(...def.workingDirArgs(cwd));
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/cli/services/agent-spawner.ts` around lines 577 - 586, The hardcoded
check for toolType === 'codex' should be replaced by the definition flag; update
the conditional in agent-spawner.ts to use def?.workingDirArgsBeforeBatchPrefix
(and def?.workingDirArgs) instead of toolType === 'codex' so that when a
definition opts into workingDirArgsBeforeBatchPrefix you call
preOverrideArgs.push(...def.workingDirArgs(cwd)); keep the existing push
behavior and null-safe checks (use def?.workingDirArgs) and do not change
ordering logic beyond switching the guard to
def.workingDirArgsBeforeBatchPrefix.

@chr1syy
Copy link
Copy Markdown
Contributor Author

chr1syy commented May 11, 2026

Closing as a duplicate of #960 (which already addresses issue #959). Apologies for missing the existing PR during triage.

@chr1syy chr1syy closed this May 11, 2026
@chr1syy chr1syy deleted the fix/959-codex-cwd-flag-placement branch May 11, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant