codex-plugin-cc icon indicating copy to clipboard operation
codex-plugin-cc copied to clipboard

/codex:rescue subagent silently fails to delegate — ${CLAUDE_PLUGIN_ROOT} is empty in subagent Bash

Open namzzz opened this issue 5 months ago • 2 comments

Summary

When /codex:rescue routes to the codex:codex-rescue subagent, the subagent's forward command fails because ${CLAUDE_PLUGIN_ROOT} resolves to an empty string in the subagent's Bash environment. The command becomes node "/scripts/codex-companion.mjs" task … → Cannot find module '/scripts/codex-companion.mjs'. The subagent then silently falls back to doing its own grep-based investigation and returns a non-Codex answer, so no Codex job is ever created and the user is unaware Codex was never invoked.

Environment

  • Plugin codex v1.0.4 (marketplace openai/codex-plugin-cc)
  • Claude Code 2.1.186 (Agent SDK 0.3.186), VS Code extension
  • codex-cli 0.133.0, Node v20.20.2, Linux (WSL2)
  • ChatGPT auth, plugin enabled, project trusted, review-gate off

Repro

  1. Install/enable the plugin; confirm /codex:setup reports ready.
  2. Run a substantial /codex:rescue <multi-file investigation prompt>.
  3. Observe the subagent returns a plausible answer fairly quickly.
  4. Run /codex:status → no job exists (running: [], recent: [], latestFinished: null). The returned answer was authored by the subagent, not Codex.

Root cause

${CLAUDE_PLUGIN_ROOT} is a harness template token that Claude Code substitutes when rendering plugin commands and hooks (textually, per-plugin). It is not exported as a real environment variable into the Bash environment of a spawned subagent.

  • plugins/codex/agents/codex-rescue.md instructs the subagent to run node "${CLAUDE_PLUGIN_ROOT}/scripts/codex-companion.mjs" task … via Bash.
  • In the subagent's shell, ${CLAUDE_PLUGIN_ROOT} expands to empty → module-not-found.

Verified:

  • In a subagent Bash, echo "[${CLAUDE_PLUGIN_ROOT}]" prints [] (empty) by default.
  • Running the exact forward command with an empty var reproduces Cannot find module '/scripts/codex-companion.mjs'.
  • Exporting CLAUDE_PLUGIN_ROOT=<plugin-root> (e.g. via a project env setting) makes the var available in subagent Bash, and /codex:rescue then forwards correctly and creates a real Codex job.

Impact

Silent, dangerous failure mode: the user believes Codex ran when it did not. This directly violates the plugin's own codex-result-handling rule ("if Codex was never successfully invoked, do not generate a substitute answer"), because the substitute is produced by the subagent before any result-handling guidance applies.

Suggested fixes

  • Have the subagent resolve the script path robustly instead of relying on ${CLAUDE_PLUGIN_ROOT} being present in the shell env — e.g. pass the absolute companion path into the subagent prompt at command-render time (the slash-command layer already receives the expanded path), or derive it without the env var.
  • Alternatively, make the subagent hard-fail (return nothing) when the forward command errors, rather than silently self-investigating — at minimum this surfaces the problem instead of masking it.

namzzz avatar Jun 23 '26 12:06 namzzz

Independent repro on plugin v1.0.5 with a different trigger: the same "subagent does the work itself, Codex never invoked" outcome occurs even when ${CLAUDE_PLUGIN_ROOT} resolves correctly and the companion script is fully reachable. The prose-only guardrails in agents/codex-rescue.md appear to be the underlying weakness — the env-var failure in the OP is one trigger, but not a necessary one.

Environment

  • Plugin codex v1.0.5 (marketplace openai/codex-plugin-cc), macOS (darwin 27.0.0)
  • Claude Code CLI, codex-cli 0.142.5, Node v22.22.3, ChatGPT auth, review gate off
  • Env-var substitution verified working in this setup: a subsequent codex:codex-rescue run in the same session invoked the fully-expanded companion path successfully (see below)

Repro

  1. /codex:rescue add a one-line comment to TEST_PLAN.md noting the current date
  2. Subagent returns success; the file is edited exactly as requested — looks like Codex did it.
  3. But /codex:status shows no task job, and the subagent transcript shows its only Bash calls were:
cat /Users/.../TEST_PLAN.md
sed -i '' '1a\
<!-- Test run last updated: 2026-07-06 -->\
' TEST_PLAN.md

No codex-companion.mjs invocation was attempted at all — no failed call, no module error. The subagent (model: sonnet) simply performed the trivial edit directly, despite the agent definition's "Use exactly one Bash call to invoke codex-companion... Do not inspect the repository."

Downstream breakage

  1. /codex:rescue --resume also fix any typo in that comment → "No previous Codex task thread was found for this repository."

This resume attempt forwarded correctly (task --resume-last --write "..." — proving ${CLAUDE_PLUGIN_ROOT} works here), but there was no thread to resume because step 1 never created one. Companion persistence itself is fine: a direct foreground codex-companion.mjs task run persisted a job with threadId, and task-resume-candidate immediately returned available: true.

Takeaway

Two distinct triggers (empty CLAUDE_PLUGIN_ROOT in the OP; plain instruction non-adherence on trivial tasks here) produce the same silent non-delegation, and the user can't tell — the result looks identical to a successful Codex run. Suggests the fix should make delegation structurally verifiable rather than relying on prompt guardrails, e.g.:

  • have the wrapper (or the command layer) verify a job record / thread id exists after the subagent returns, and surface an error if not;
  • and/or restrict the subagent so arbitrary Bash beyond the companion invocation isn't available to it.

Skeptomenos avatar Jul 06 '26 21:07 Skeptomenos

I reproduced this on a clean, unpatched v1.0.4 install (no local modifications) and wanted to share some additional evidence that might point to a second, distinct root cause alongside the ${CLAUDE_PLUGIN_ROOT} expansion issue.

Setup: Called codex:codex-rescue via the Agent tool with subagent_type: "codex:codex-rescue", exactly as commands/rescue.md instructs. No patches applied to agents/codex-rescue.md or commands/rescue.md.

Case 1 — trivial prompt ("what is 1+1? answer with a single digit"): The subagent's transcript shows zero tool calls. It went straight to a text response ("1", which is also wrong — 2 is correct). If the root cause were purely ${CLAUDE_PLUGIN_ROOT} being empty, I'd expect at least one attempted Bash invocation that fails. Here there's no attempt at all.

Case 2 — a real, file-reading task (asking it to build a small input/output map of a source file based on a config file): The transcript shows the subagent using Bash (find, grep) and Read directly — investigating the codebase itself and producing a full answer — again with no attempt to invoke codex-companion.mjs.

Both cases match the "silently fails to delegate, returns a plausible-looking self-authored answer" symptom described here, but neither shows a failed script invocation — the subagent simply never tries.

My working hypothesis: agents/codex-rescue.md's "Selection guidance" section —

Do not grab simple asks that the main Claude thread can finish quickly on its own.

— is ambiguous about who it applies to. It reads like guidance for the caller deciding whether to invoke this subagent at all, but at runtime the subagent itself appears to sometimes apply it to itself post-invocation, treating "this looks simple/complex enough that I can just handle it" as license to skip the one Bash call it's supposed to always make.

If that's right, this Issue may have (at least) two independent contributing causes:

  1. ${CLAUDE_PLUGIN_ROOT} not being exported into the subagent's Bash environment (as already diagnosed here)
  2. Ambiguous role-scoping language in the subagent prompt, causing self-judgment fallback even when the script path itself would have resolved fine

Happy to share full transcripts (tool-call sequences) if useful for debugging.

harutonatu avatar Jul 24 '26 03:07 harutonatu