Skip to content

Stale Codex subagents #23700

Open
Open

Description

@Vuk97

What version of Codex CLI is running?

codex-cli 0.131.0

What subscription do you have?

x20

Which model were you using?

gpt 5.5

What platform is your computer?

Darwin 24.6.0 arm64 arm

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

No response

Codex doctor report

What issue are you seeing?

2 issues - not sure which /feedback opted me to open a github issue.

  1. Codex subagents have been going stale and refusing to close for the past week or so, this isn't consistent behavior but when it happens, the whole session is bugged. One stale agents leads to the halt of complete /goal mode.
  2. "⚠ MCP startup interrupted. The following servers were not initialized: codex_apps" on the session that has been running for 5+ hours.

What steps can reproduce the bug?

Uploaded thread: 019e1b66-7a32-7c00-b1ad-c7dabd4cc68d

What is the expected behavior?

No response

Additional information

No response

Activity

  1. added
    bug
    on May 20, 2026
  2. added
    CLI
    subagent
    mcp
    on May 20, 2026
  3. Keesan12 commented on May 20, 2026

    @Keesan12

    Feels like a runtime-control problem more than a /goal UX bug. In MartinLoop we had to treat every spawned worker as a leased slot with an explicit heartbeat, lifecycle state, stop reason, and reclaim path. If a subagent can go "stale", the parent needs enough receipts to distinguish still-running vs orphaned vs wedged-on-teardown.

    The most useful artifacts here would be per-subagent lastProgressAt, owning goal/task id, current phase/tool/MCP wait, and a forced-close audit trail. Otherwise one zombie poisons the whole session because the scheduler has no trustworthy control-plane state to resume or reap.

  4. removed
    CLI
    on May 20, 2026
  5. Keesan12 commented on May 21, 2026

    @Keesan12

    The recurring failure mode here still looks like missing control-plane state, not only missing cleanup. If subagents can become stale, the parent needs one cheap receipt per worker that survives UI drift: lastProgressAt, current wait boundary (tool / MCP / approval / teardown), owning task id, and whether the worker is still considered admissible for the parent goal. Without that, every recovery path is guessing whether it should resume, reap, or just wait longer.

  6. Vuk97 commented on May 21, 2026

    @Vuk97
    Author

    I am able to interupt it - but have to catch it not doing anything manually. That 1 stale agent will be stale for the whole session. Attempting to close it again will make the whole session stuck again.

    Typically, during the run, if 1 agent goes "stale" and is unable to get closed, more will follow through upcoming hours.

    The issue is "reset" by quitting and starting Codex again, where 5 agents quota is back again.

  7. Keesan12 commented on May 21, 2026

    @Keesan12

    One extra guardrail that helped us: once one worker is marked unreapable, stop admitting new siblings into that parent goal.

    Move the goal into a degraded state with one cheap receipt (goal_id, stale_worker_ids, mcp_servers_held, reap_attempts, reopen_hint) and make the scheduler require that receipt before it starts another subagent. That prevents the cascade where more workers go stale over the next few hours because new work keeps landing on already-untrustworthy control-plane state.

  8. Keesan12 commented on May 22, 2026

    @Keesan12

    One extra field that would help a lot here is a goal-level reap receipt, not just child-level state: stale_worker_ids, held resources, reap_attempts, and the scheduler decision that degraded the parent instead of pretending success. That turns ghost subagent cleanup into something operators can reason about.

  9. Keesan12 commented on May 25, 2026

    @Keesan12

    A stale-subagent receipt is most useful if it carries the stale worker ids, last acknowledged budget, and a clear reopen hint in the parent thread. Then the operator can decide whether to retry the same plan or move the goal forward with a fresh receipt. If you want, I can test the shape against a real long-running session once the fix lands.

  10. grnbtqdbyx-create commented on May 31, 2026

    @grnbtqdbyx-create
  11. pomazanbohdan commented on Aug 12, 2026

    @pomazanbohdan

    Additional Windows Desktop evidence: stale agent rows survive terminal state

    This is a sanitized report from a Windows Codex Desktop session using multi-agent orchestration. The visible symptom was a parent thread showing no usable active agents while a fresh agent request still returned agent thread limit reached. Reopening Codex did not reconcile the stale agent roster.

    The local state showed three independent persistence layers retaining the same lifecycle:

    • SQLite threads / thread_spawn_edges rows for thread_source=subagent;
    • parent rollout JSONL sub_agent_activity records;
    • global/session-index UI references.

    A first conservative cleanup removed 118 normally completed child rows, but left 8 rows because 7 had turn_aborted without task_complete and one was a legacy parent/subagent row. That reproduced the core defect: the UI/orchestrator has no reliable terminal garbage-collection path for aborted or otherwise stale children.

    After a full app shutdown, a destructive reconciliation pass removed the remaining 8 rows and 7 edges plus their 8 rollout files. Postconditions were:

    • 18 total thread rows, 0 spawn edges;
    • 0 remaining thread_source=subagent rows;
    • 13 stale sub_agent_activity events removed from 2 surviving parent rollouts;
    • 0 target IDs in global UI state or session_index.jsonl;
    • 0 target activity events and 0 JSONL parse errors;
    • SQLite foreign-key and staged-write checks passed.

    No raw transcripts, local paths, or private rollout files are attached.

    Requested fixes

    1. Make completed, aborted, failed, and cancelled child states terminal and idempotently reapable.
    2. Calculate the agent-thread limit from live/pending handles only; stale historical rows must not consume capacity.
    3. Reconcile thread rows, spawn edges, rollout activity, and UI indexes on app restart.
    4. Add a user-visible “reap stale agents” operation with a dry-run/backup boundary.
    5. Keep historical agent transcripts separate from the active-agent roster so archived history cannot appear as active work.

    This is a local-state/lifecycle issue, not a request to delete user history by default.

  12. pomazanbohdan commented on Aug 12, 2026

    @pomazanbohdan

    User cleanup watcher (destructive)

    Gist: https://gist.github.com/pomazanbohdan/306b3fe6f49e9e3aa3ff14711f6e5598

    Files:

    The gist is secret/unlisted: anyone with the link can read it, but it is not listed in public Gist discovery.

    Important warning

    This performs a full destructive cleanup. It deletes all agent threads from local Codex state, including completed, aborted, failed, paused, and incomplete agents. It also deletes their spawn edges and rollout files and removes persisted agent-activity/UI references. Agent history is not retained and recovery is manual from the generated backup.

    Windows usage

    1. Save the two source files in the same directory.

    2. Open PowerShell while Codex is still running.

    3. Run:

      Set-ExecutionPolicy -Scope Process Bypass
      & .\watch_universal_clean_relaunch.ps1
    4. Close every Codex window. The watcher waits for complete process absence, creates a backup, performs the cleanup, verifies SQLite/JSONL/UI postconditions, and relaunches Codex.

    The backup is written under the watcher's run-<UTC timestamp>\backup directory. If Codex does not fully close, no mutation is performed.

    Dry run

    To inspect what would be removed without mutation:

    python .\universal_cleanup_after_close.py --dry-run --run-dir .\dry-run

    The Python script refuses live mutation unless it is invoked by the watcher after full process absence.

  13. pomazanbohdan commented on Aug 12, 2026

    @pomazanbohdan

    Additional reproduction evidence for the stale-active-subagent bug (Windows, Codex Desktop 26.803.10989.0; CLI 0.147.0-alpha.6.6).

    Reproduction

    1. In parent task 019fea4e-4227-7810-be40-9afcc9a23aad (“Quick Dev 3.0”), spawn subagents.
    2. Let these four children finish normally:
      • Tesla 019ff593-1a6b-7cd3-bfd2-e001212e7603
      • Pasteur 019ff5b2-6ab4-7792-9f33-66b97897d201
      • Dirac 019ff5b2-82ae-7411-84d8-e34c2181004b
      • Archimedes 019ff5b2-9af4-7943-aa94-5c0abd770148
    3. Confirm their child rollouts end with event_msg.payload.type=task_complete.
    4. Fully close/restart Codex and reopen the parent task.

    Observed

    • The UI shows all four children as Active again.
    • thread/read for every child reports status.type=notLoaded; its latest/only turn is status=completed with a completion timestamp.
    • The parent rollout has no terminal sub_agent_activity for these four: the latest parent activity remains started or interacted.
    • The parent’s thread_spawn_edges rows remain status=open.
    • During reopen (approximately 2026-08-12 11:29:43–11:29:58 UTC), logs show parent resume/reconstruction followed by child reads; Tesla also receives thread/resume and startup_prewarm. The child rollout files receive no new output, so this is not a new child turn.

    Likely localization

    Normal child completion is persisted in the child rollout but is not propagated to the parent as a terminal activity and does not close the parent spawn edge. Resume then rebuilds the active overlay from the stale parent activity/edge state without reconciling the child’s terminal event. This produces the ghost Active entries and can also retain stale spawn/quota resources.

    Expected behavior

    On any terminal child state (task_complete, interrupted, cancelled, or failed), Codex should atomically:

    1. close the corresponding thread_spawn_edges row;
    2. persist a terminal parent activity/state;
    3. release the child from active-thread/quota/restore registries.

    On resume/reopen, the UI should derive Active strictly from live handles or reconciled nonterminal child state. A close/reopen regression test covering this sequence would catch the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmcpsubagent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions