codex icon indicating copy to clipboard operation
codex copied to clipboard

codex exec exits after the first turn and interrupts an active goal's automatic continuation

Open jonathanpwang opened this issue 3 weeks ago • 4 comments

What version of Codex CLI is running?

codex-cli 0.167.0 (also reproduced on 0.159.0 and 0.164.0) ###

What subscription do you have?

GPT Pro

Which model were you using?

gpt-6-astra (model_reasoning_effort=low); first seen with gpt-6.1-sol at xhigh

What platform is your computer?

Linux 6.8.0 x86_64 (Ubuntu)

What terminal emulator and version are you using (if applicable)?

No response

Codex doctor report


What issue are you seeing?

codex exec ends the process when the first turn completes, even if the thread has an active goal. The goal runtime does start its automatic continuation turn, but exec's shutdown interrupts it a few milliseconds later. The process exits 0 with a single turn.completed, and the goal is left active and unfinished.

The session rollout shows the sequence (0.167.0):

23:56:29.016 task_started
             (model calls create_goal → goal status "active")
23:56:35.924 task_complete
23:56:35.931 task_started      ← automatic goal continuation
23:56:35.935 turn_aborted      reason: "interrupted"   (4 ms later)

The --json stdout contains only one turn.started / turn.completed pair, and thread_goals in goals_1.sqlite still shows the goal as active.

As far as I can tell, the cause is in exec itself. The JSONL event processor returns CodexStatus::InitiateShutdown on TurnStatus::Completed (event_processor_with_jsonl_output.rs#L528-L538). The main loop then unsubscribes from the thread, leaves the loop, and shuts down the in-process app-server (lib.rs#L1389-L1412). Nothing checks whether a goal is still active first.

This matters for the approach suggested in #26966 ("just tell the agent to create a goal for itself" in codex exec). The model does create the goal, but exec never lets the goal continue. In our case, an unattended eval run with codex exec stopped after 30 minutes on an unfinished task because the continuation was interrupted.

What steps can reproduce the bug?

The goal needs at least two turns: the prompt allows one line per turn.

mkdir -p /tmp/goal-exec-repro && cd /tmp/goal-exec-repro && rm -f count.txt

codex exec --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check \
  -C /tmp/goal-exec-repro --model gpt-6-astra --json \
  -c model_reasoning_effort=low -- \
  'First call the create_goal tool with objective "count.txt in /tmp/goal-exec-repro contains the lines 1, 2, 3". Then: in each turn you may append exactly ONE line to /tmp/goal-exec-repro/count.txt, and then you must end your turn immediately with a one-line message. Never append more than one line per turn.' \
  </dev/null > out.jsonl

grep -o '"type":"turn\.[a-z]*"' out.jsonl | sort | uniq -c   # 1 turn.started, 1 turn.completed
cat count.txt                                                 # just "1"

# Turn lifecycle from the session rollout:
TID=$(grep -o '"thread_id":"[^"]*"' out.jsonl | head -1 | cut -d'"' -f4)
jq -c 'select(.payload.type=="task_started" or .payload.type=="task_complete" or .payload.type=="turn_aborted")
       | [.timestamp, .payload.type, .payload.reason]' $(find ~/.codex/sessions -name "*$TID*")
# ["…:29.016Z","task_started",null]
# ["…:35.924Z","task_complete",null]
# ["…:35.931Z","task_started",null]       ← goal continuation
# ["…:35.935Z","turn_aborted","interrupted"]

--dangerously-bypass-approvals-and-sandbox is only there because bubblewrap can't create namespaces on my host. The behavior doesn't depend on the sandbox.

Control: the same binary, model and prompt driven through codex app-server behaves correctly. The client sends initialize, thread/start and one turn/start, then only listens. The goal runtime starts each continuation on its own (each turn/completed is followed by turn/started within ~20 ms). It runs 4 turns, count.txt ends up 1 2 3, and thread/goal/updated reports status: "complete". So the goal runtime works, and only exec's exit path cuts it off.

What is the expected behavior?

If the thread has an active goal when exec's initial turn completes, codex exec should keep running and let the goal's automatic continuations proceed, the same way they do under app-server. It should exit when the goal is completed, blocked, paused or out of budget, or when the user interrupts (Ctrl-C). It should stream every continuation turn's events in --json mode.

If exiting after one turn is intended, it would help to:

  • document it,
  • emit a warning when exec exits with an active goal, and
  • provide an opt-in flag to wait for the goal.

Additional information

  • Related: #26966 / #26949, "Add --goal support to codex exec" (closed as not planned). The answer there was that the agent can create a goal itself in exec; this issue shows that the goal then doesn't continue.
  • Our current workaround is to re-run codex exec resume <thread_id> while the goal is still active.
  • Same result on 0.159.0, 0.164.0 and 0.167.0.

jonathanpwang avatar Nov 09 '26 00:11 jonathanpwang

Duplicate assessment (Muse lane, 2026-11-08): NOT a duplicate — same exec-shutdown family, different trigger and symptom.

I read #60308 in full and checked the closest live reports just now — all verified open unless noted:

  • #50585 (open) — codex exec --json shuts down on an interrupted turn without emitting a terminal turn event. Source-inspection report about JSONL stream completeness: TurnStatus::Interrupted returns InitiateShutdown with no turn.* terminal event, so a consumer cannot tell from the stream that the turn was interrupted.
  • #26966 (closed) — "Add --goal support to codex exec." The feature whose approach #60308 relies on ("just tell the agent to create a goal for itself"), not this bug.
  • #41984 (open) — codex exec review reports a failed Git command but never ends the turn, so the process hangs forever. Opposite failure mode (hang vs. premature exit).

What makes #60308 distinct: codex exec exits 0 after the first turn completes while the thread has an active goal — the goal runtime does start its automatic continuation turn, but exec's shutdown interrupts it 4 ms later (turn_aborted, reason "interrupted"), and goals_1.sqlite still shows the goal as active and unfinished. This is a runtime reproduction with rollout evidence (0.167.0, also 0.159.0/0.164.0), not source inspection, and the defect is exec never checking for an active goal before shutting down on TurnStatus::Completed. #50585 is about a missing terminal event for an already-interrupted turn; #60308 is about a completed turn killing a live goal continuation — different trigger, different symptom, different fix.

Group with #50585 for the exec-shutdown investigation; do not merge — the active-goal check before shutdown here is its own lead.

— Muse (from the Muse↔Penny coordination hub, grtninja/Starframe-PC-Control #238)

grtninja avatar Nov 09 '26 00:11 grtninja

There is also a turn-ID filter to cover when testing a fix. At 25f4940, exec passes the original task_id to should_process_notification. That function accepts turn lifecycle, item, and usage events only when they match the supplied turn ID. Keeping the process alive alone would therefore not make a continuation with a new turn ID visible through this path. This is source inspection, not an additional runtime reproduction.

A deterministic regression test could hold the automatic continuation behind a barrier after the first turn/completed, assert that exec does not unsubscribe while the goal remains active, then release it and assert that the second turn's start, items, and completion reach JSONL despite its different ID. That would exercise the quiet interval between turns without relying on a sleep or on the continuation winning the shutdown race.

The same test suite should retain the ordinary no-goal exit and cover goal completion, non-running goal states, and Ctrl-C so waiting for a goal cannot turn into an unbounded hang.

DrPei12 avatar Nov 09 '26 01:11 DrPei12

Hi, is there any plan to fix this?

jonathanpwang avatar Nov 20 '26 17:11 jonathanpwang

I reproduced the early exit on the released Codex CLI 0.171.0 for Linux arm64, using a scripted loopback Responses endpoint with features.goals=true. The fixture issues create_goal explicitly and finishes the first turn with the goal active.

  • codex exec --json control without a goal — exit 0, one turn.started and one turn.completed.
  • codex exec --json with the active goal — exit 0 and the same one-turn lifecycle in JSONL. The saved goal was still active at exit. The rollout records the first task_complete, followed by task_started for a different turn ID and turn_aborted with reason=interrupted. The loopback endpoint received no request for that second turn in either run.
  • Stdio codex app-server with the same scripted responses and initial turnTrigger: "exec" — both turn IDs completed and thread/goal/get returned complete.

I also took the saved thread ID from the interrupted exec run and used thread/resume on a new stdio app-server process. That alone started a goal turn. Keeping the connection open through turn/completed let the same goal reach complete, without submitting a fresh turn/start or adding a synthetic user prompt.

The distinct continuation turn ID matches the earlier observation about JSONL filtering. The 0.171.0 exec loop still filters notifications against the initial task_id. Release source · Turn notification filter

AI-assisted reproduction with synthetic provider responses.

r266-tech avatar Nov 20 '26 19:11 r266-tech