codex icon indicating copy to clipboard operation
codex copied to clipboard

Runtime-enforced event-driven agent orchestration: End parent turns after delegation and eliminate unnecessary inference

Open BrunoP9 opened this issue 1 week ago • 4 comments

What variant of Codex are you using?

26.1117.41037

What feature would you like to see?

Summary

Codex should support a runtime-enforced, event-driven orchestration mode in which a parent agent can delegate work, deterministically complete its current turn, and remain idle until a relevant event requires further reasoning.

Currently, a parent agent can repeatedly invoke wait tools, poll subagent status, and trigger additional model inference without receiving any new information relevant to its task.

For long-running, multi-agent engineering workflows, this creates unnecessary inference, repeated context processing, usage consumption, and orchestration overhead.

This should not depend on the model's ability to follow orchestration instructions. It should be an enforceable property of the runtime.

I have implemented and successfully tested a working proof of concept by modifying the Codex Rust core and running it within the regular Windows Codex Desktop application.

Problem

I use Codex Desktop as part of CapaLynx, a multi-agent engineering system with dedicated orchestration, architecture, implementation, and quality assurance roles.

My orchestrators delegate work to native Codex subagents as well as externally executed CLI agents through native runner agents.

Despite explicit orchestration instructions, parent agents can continue to perform actions after successful delegation:

  • Repeatedly invoke collaboration.wait_agent.
  • Request shorter waiting periods than defined by the orchestration contract.
  • Poll subagent status through list_agents.
  • Inspect external worker status and output files.
  • Trigger additional inference without receiving new actionable information.

The fundamental problem is not that a blocking wait itself consumes tokens. It is that a technical timeout returns control to the model, potentially triggering another inference request with a substantial conversation context.

A parent agent that has delegated all remaining work has no reason to perform additional inference until something relevant changes.

Increasing the wait timeout reduces the frequency of this behavior but does not eliminate it. Prompt instructions are also insufficient because they remain subject to model interpretation.

Evidence from a real engineering workflow

During a production engineering workflow on November 22, 2026, my orchestration contract specified a ten-minute waiting period.

The parent agent nevertheless explicitly requested nine 60-second waits.

Observed execution metrics:

  • Requested 60-second waits: 9
  • Timer expirations without a relevant event: 6
  • Subsequent parent model requests: 6
  • Additional wait calls: 4
  • Additional status queries: 2
  • Total input tokens across those six requests: 806,401
  • Cached input tokens: 804,608
  • Output tokens: 390

All six subsequent model requests resulted in either another wait or a status query.

The parent received no new actionable subagent event from those six timer expirations.

These figures represent observed token usage from one real workflow, not an estimate. I am not claiming that cached and uncached tokens have identical economic costs or that these figures establish an exact monetary saving.

However, the six additional inference requests demonstrate a concrete opportunity to eliminate redundant orchestration work.

Why this belongs in the runtime

The underlying architectural principle is straightforward:

The model should decide what to delegate. The runtime should enforce the delegation lifecycle.

An agent's decision to delegate work and the technical lifecycle of that delegation are separate concerns.

Relying on the model to repeatedly decide whether it should continue waiting introduces unnecessary nondeterminism into an otherwise deterministic coordination problem.

Once delegation has been successfully completed, the runtime already has the information necessary to determine whether the parent should remain idle or resume execution.

A technical timer expiration is not necessarily a meaningful orchestration event.

Proposed feature

Introduce an optional, project-level or thread-level event-driven orchestration mode.

The mode should support an explicit delegation-and-yield operation, allowing an orchestrator to start or resume one or multiple subagents and complete its current turn as part of the same runtime-controlled operation.

The desired lifecycle is:

  1. The parent agent prepares the next group of delegated tasks.
  2. The runtime starts or resumes the corresponding subagents.
  3. The runtime registers the successfully delegated work and the corresponding parent-child relationships.
  4. Following a successful handoff, the runtime completes the parent turn.
  5. Subagents continue executing independently.
  6. The parent remains idle without further inference or polling.
  7. A relevant event triggers a new parent turn.

Relevant wake-up events should include:

  • A message or clarification request from a subagent.
  • Successful subagent completion.
  • Subagent failure or cancellation.
  • Completion or failure of an external job managed through a native runner.
  • New user input or an explicit status request.
  • User-initiated interruption or cancellation.

Internal timer maintenance should not require another model request unless the timeout itself represents an actionable event, such as an explicitly configured task deadline.

Runtime guarantees

The proposed mode should provide the following guarantees:

  • No further parent inference within the completed delegation turn.
  • No further tool execution from that turn after successful handoff, including additional tool calls emitted in the same model response.
  • No parent-side polling solely to discover whether a registered child has completed.
  • Prompt event delivery without an artificial minimum waiting period.
  • Reliable registration of child identities before the parent turn completes.
  • Correct handling of events arriving at the turn-completion boundary.
  • Explicit reporting of partially successful batch delegations, including the identities of children already started.
  • Preservation of user interruption and cancellation semantics.
  • Defined recovery and event-delivery behavior when sessions or the app-server are resumed.

This should be an opt-in capability rather than a breaking change to existing subagent workflows. Agents that need to perform productive work concurrently with their children should remain able to do so.

Working proof of concept

I have implemented this architecture locally using a modified Codex Rust core.

The regular Windows Store Desktop application remains unchanged. It communicates with my locally built, version-qualified core.

My implementation introduces a project-specific batch delegation tool:

capalynx_delivery.dispatch_batch

In designated orchestration threads, this tool replaces individual native agent start and resume operations.

After successful delegation, the core:

  • Registers the delegated work and its parent-child relationships.
  • Completes the parent turn deterministically.
  • Prevents further tool execution from the completed turn.
  • Prevents additional model inference within that turn.
  • Allows child agents to continue independently.
  • Uses the native event inbox and wake_on_agent_messages functionality to resume the parent when a relevant event arrives.

If only part of a delegation batch succeeds, the parent receives the successful child registrations and failure information instead of being forced to terminate its turn. This avoids blindly duplicating already running work.

The implementation does not depend on the model voluntarily complying with a prompt-based waiting policy.

Validation and measured results

The modified core was validated through 29 native test scenarios, including:

  • Multiple subagents.
  • Partial delegation failures.
  • Additional tool calls emitted in the same model response.
  • User STOP behavior.
  • Cold session resumption.
  • Project and thread eligibility checks.

I also conducted a controlled comparison using the original, unmodified Codex core and subsequently validated the implementation with two actual Codex Desktop orchestrators.

Desktop end-to-end results

Implementation orchestrator:

  • Further parent tool calls after handoff and before child completion: 0
  • Further parent model requests during that interval: 0
  • New parent turns triggered by the observed child completion: 1
  • Child completion to new parent turn initiation: 59 ms

Specification orchestrator:

  • Further parent tool calls after handoff and before child completion: 0
  • Further parent model requests during that interval: 0
  • New parent turns triggered by the observed child completion: 1
  • Child completion to new parent turn initiation: 112 ms

The implementation orchestrator delegated a real shell task to a native runner.

The specification orchestrator delegated work to a restricted external Codex CLI author through WSL, supervised by a native runner.

Both completed successfully in the regular Desktop environment.

The latency measurements represent the time between the recorded native child completion event and initiation of the new parent turn, not the time required to generate a complete model response.

These two measurements establish end-to-end feasibility on the tested build. They are not intended as general latency benchmarks or scalability guarantees.

Reproduction with the unmodified Codex core

Environment

  • Platform: Windows x64
  • Codex Desktop Store package: 26.1117.4957.0
  • Core version: rust-v0.171.0-alpha.13
  • Multi-Agent v2 enabled
  • Upstream revision: 581161ab41e643d942e1e1b5f561034060bf18e3

Scenario A: Timeout-driven inference

  1. Start a parent agent and a native subagent.
  2. Keep the child running without producing messages or completing.
  3. Invoke collaboration.wait_agent with timeout_ms = 60000, using a configuration that permits this timeout.
  4. Allow the timer to expire without any relevant child or user event.
  5. Observe the tool result and subsequent parent model request.

Current behavior: The technical timeout can trigger another model inference request despite the absence of new actionable information.

Desired behavior in event-driven mode: The parent remains idle until a meaningful event or an explicitly configured actionable deadline occurs.

Scenario B: Continued execution after delegation

  1. Start the original app-server with agent wake-up notifications enabled.
  2. Create a parent thread and start a native subagent.
  3. Keep the child running.
  4. Use a controlled test provider to request list_agents in the next model response of the same parent turn.
  5. Observe tool execution, model requests, and turn-completion events.

Current behavior: The original core permits additional parent actions within the same turn after successful delegation. My controlled comparison reproduced this behavior.

Desired behavior when using an explicit delegation-and-yield operation: The runtime completes the parent turn immediately after successful delegation and registration. No further tools or inference execute within that turn.

A subsequent relevant child event initiates a new parent turn.

The existing wait behavior is consistent with the current implementation. This request therefore proposes an additional orchestration capability rather than classifying the documented timeout behavior itself as a bug.

Relevant upstream implementation:

https://github.com/openai/codex/blob/581161ab41e643d942e1e1b5f561034060bf18e3/codex-rs/core/src/tools/handlers/multi_agents_v2/wait.rs

Observability

An additional improvement would be structured runtime telemetry for delegation, waiting, and wake-up events.

Useful information would include:

  • Parent thread and turn identifiers.
  • Registered child identifiers.
  • Delegation and turn-completion timestamps.
  • Requested and effective wait timeouts.
  • Actual wake-up reason.
  • Correlation between the wake-up event and subsequent inference request.

This would allow developers to distinguish productive inference from timer-driven orchestration overhead and independently validate runtime behavior.

Benefits

For users:

  • Reduced unnecessary usage consumption.
  • More predictable orchestration behavior.
  • Less dependence on model-specific instruction-following capabilities.
  • More reliable long-running agent workflows.
  • Improved observability and troubleshooting.

For OpenAI:

  • Fewer unnecessary inference requests.
  • Less repeated context processing.
  • Potentially more efficient inference infrastructure utilization.
  • A stronger runtime foundation for enterprise-grade multi-agent systems.
  • Improved support for complex orchestration workloads without relying on increasingly elaborate prompt instructions.

The benefits should become particularly relevant as agentic systems scale to larger numbers of concurrent workers, longer execution times, and more complex orchestration hierarchies.

Why upstream support matters

My local implementation demonstrates that this architecture is feasible within the existing Codex ecosystem.

However, maintaining a modified core requires compatibility validation, rebuilding, testing, and qualification for each new Desktop package version.

My implementation is intentionally version-bound and includes a tested rollback mechanism. It is currently qualified only for the specified Windows environment.

This is not a sustainable integration model for users who need reliable, long-running agent orchestration across regular Codex updates.

A first-class upstream capability would eliminate the need for maintaining a custom runtime while making the same efficiency improvements available to other Codex users.

I can provide additional implementation details, sanitized execution traces, reproduction artifacts, and information about the core patch if useful to the Codex engineering team.

Related issue

Related to #61901 — Support automatic parent wake-up for create_thread assignments.

The two requests address complementary parts of the agent orchestration lifecycle:

  • #61901 addresses how and when the parent receives events and wakes up.
  • This request addresses how and when the parent deterministically completes its turn after delegation.

Reliable event delivery and deterministic turn completion together provide the foundation for genuinely event-driven agent orchestration.

Closing thought

Agentic systems should reason when there is something to reason about.

Waiting for delegated work is a runtime responsibility, not a reasoning task.

Intelligence should be used for decisions, not polling.

Additional information

No response

BrunoP9 avatar Nov 23 '26 23:11 BrunoP9

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #62611

Powered by Codex Action

github-actions[bot] avatar Nov 23 '26 23:11 github-actions[bot]

Additional clarification regarding existing native configuration options

Before implementing the core modification, I had already successfully configured and validated:

  • default_wait_timeout_ms
  • min_wait_timeout_ms
  • max_wait_timeout_ms
  • wake_on_agent_messages
  • defer_mailbox_preemption

The native dispatcher correctly enforced a ten-minute minimum wait while allowing actual child messages to interrupt the wait immediately.

The original core's continued parent execution after delegation was also reproduced in a trusted project with danger-full-access and approval_policy = "never".

The request is therefore not about introducing configurable waiting or automatic agent wake-ups. These capabilities already exist and work.

The missing capability is a deterministic, runtime-enforced delegation-and-yield operation that registers the delegated children and completes the active parent turn without requiring another model decision.

My implementation deliberately builds upon the existing native wake-up mechanism rather than replacing it.

BrunoP9 avatar Nov 23 '26 23:11 BrunoP9

Thanks for flagging #62611. I reviewed the request and agree that there is meaningful overlap regarding event-driven execution and the elimination of unnecessary inference.

However, I believe the requests are complementary rather than duplicates.

#62611 primarily addresses intelligent suspension and automatic resumption for long-running CI/CD workflows, quota resets and cloud-hosted tasks.

This request addresses a more specific runtime-level capability: deterministic parent turn completion as part of a successful subagent delegation operation.

The distinct requirements include:

  • Runtime-enforced batch delegation and yield.
  • Registration of child identities before parent turn completion.
  • Prevention of subsequent tool execution, including additional tool calls emitted in the same model response.
  • Prevention of further inference within the completed delegation turn.
  • Explicit handling of partially successful delegation batches.
  • Correct event handling at the turn-completion boundary.

Furthermore, this request includes a working implementation based on a modified Codex Rust core, validated through 29 native test scenarios and two actual Codex Desktop end-to-end tests.

Both existing native wait-timeout configuration and automatic agent wake-up functionality were successfully configured and validated before implementing the core modification.

The proposed capability builds upon those existing mechanisms rather than replacing them.

I therefore consider #62611 related but not a duplicate and would like to keep this issue open for technical review.

BrunoP9 avatar Nov 24 '26 00:11 BrunoP9

Additional validation: Scenario #30, Mid-execution failure with partial worktree modifications

Result: PASS - All seven acceptance criteria satisfied.

Following a technical discussion on LinkedIn about this feature request, an important additional failure scenario was raised:

What happens when a subagent fails halfway through its implementation after already modifying code? What state does the orchestrator wake up to?

The original 29 validation scenarios covered partial batch delegation failures, worker interruption, cold session resumption and several event-delivery boundary conditions.

However, a partially successful delegation is fundamentally different from a successfully delegated worker that subsequently fails during implementation, leaving behind partial, uncommitted code changes.

I therefore added and executed a dedicated end-to-end validation scenario on November 24, 2026.

Test objective

Verify that runtime-enforced parent turn completion does not compromise failure handling, engineering state visibility or subsequent recovery decisions.

Specifically, the orchestrator must be able to resume after a child failure, identify the failed child, and inspect its actual engineering state before deciding whether to continue, retry or roll back the work.

Successful delegation must not be confused with successful or atomic task execution.

Test environment

  • Platform: Windows x64
  • Modified Codex core: 0.171.0-alpha.13
  • Same qualified core binary used for the original Desktop validation
  • Native Codex app-server and collaboration mechanisms
  • Real, isolated Git repository
  • Separate parent and child Git worktrees
  • Deterministic local Responses endpoint providing controlled model responses
  • Real filesystem, shell and Git operations

The test deliberately uses controlled model responses to validate runtime semantics independently of probabilistic model behavior.

Test scenario

  1. The orchestrator delegates an implementation task to a native subagent using the runtime-enforced batch delegation mechanism.

  2. The runtime registers the child identity and completes the parent turn.

  3. The subagent continues independently in its assigned Git worktree.

  4. The implementation task requires modifications to two JavaScript functions: formatLabel and formatUnit.

  5. The subagent modifies formatLabel but leaves formatUnit unchanged.

  6. A real code check detects the incomplete implementation and terminates with exit code 7.

  7. On the subsequent child model request, the controlled Responses endpoint injects a terminal error, causing the native child turn to terminate with status failed.

  8. The existing native agent notification mechanism delivers the failure event to the orchestrator.

  9. The orchestrator initiates a new turn and inspects the actual Git diff in the failed child's worktree.

  10. The test verifies that the original partial changes remain intact and that no automatic retry or rollback occurs during the observed execution.

Acceptance criteria and results

All seven acceptance criteria passed:

  • [x] The child performed a real code modification and the code check failed.
  • [x] The native child turn terminated with status failed.
  • [x] The original parent turn completed before the child failure.
  • [x] Exactly one event-driven parent wake-up occurred.
  • [x] The failure notification sender matched the registered child identity and thread.
  • [x] The parent successfully inspected the actual partial Git diff.
  • [x] No retry or rollback occurred, and the partial changes remained intact.

Measured runtime behavior

  • Parent turns throughout the entire scenario: 2
  • Additional parent turns between delegation and child failure: 0
  • Parent wake-ups triggered by the child failure: 1
  • Child failure to new parent turn initiation: approximately 6 ms
  • Child code check exit code: 7
  • Native child turn status: failed
  • Parent Git inspection exit code: 0
  • Automatic retries: 0
  • Automatic rollbacks: 0
  • Additional wake-ups during the five-second post-completion observation period: 0

The first parent turn performed the delegation and completed.

The second parent turn was initiated by the actual native child failure event. No additional user input or parent-side polling was required.

Engineering state preservation

The failed child left behind the following partial implementation:

  • Before: return String(label);
  • After: return String(label).trim();

The second required function remained unchanged.

The first and only tool action in the newly initiated parent turn was a read-only git diff in the failed child's actual Git worktree.

Three independently identified patch states were compared:

  • The expected patch, generated before execution.
  • The patch actually retrieved by the orchestrator.
  • The final patch remaining in the child's worktree.

All three were byte-identical, with matching SHA-256 hashes.

The child's Git HEAD remained at its original baseline commit, and the partial modification remained uncommitted. The parent's own worktree remained clean.

No implicit recovery action modified or discarded the existing engineering state.

What state does the orchestrator wake up to?

The orchestrator receives the native failure notification, including the child identity and the actual failure information.

The persisted delegation record allows the failed child to be correlated with its original registration.

The child's actual engineering state remains available in its Git worktree.

Importantly, the partial code changes are not automatically embedded into the wake-up notification. They are inspected by the orchestrator after reactivation.

This distinction preserves a separation of responsibilities:

Runtime responsibilities:

  • Complete the parent turn deterministically after successful delegation.
  • Maintain the parent-child association.
  • Deliver the relevant failure event.
  • Initiate a new parent turn without polling.

Orchestrator responsibilities:

  • Inspect the actual engineering state.
  • Evaluate the failure and any partial implementation.
  • Determine whether the existing changes are useful.
  • Decide whether to continue, retry, reassign or roll back the task.

The runtime should provide the necessary conditions for informed recovery rather than implicitly deciding how engineering recovery must be performed.

Scope and limitations

This scenario validates actual runtime behavior using real Git worktrees, real code modifications, a real failed code check, native child failure semantics and native event delivery.

The terminal model endpoint error was deliberately injected to produce a reproducible native child failure.

The parent's decision to inspect the worktree using git diff was also deliberately scripted.

Therefore, this test demonstrates that the runtime correctly supports event-driven failure handling and access to preserved partial engineering state.

It does not establish that every freely reasoning model will independently select the correct recovery strategy, nor does a single test establish a universal exactly-once delivery guarantee or a general latency benchmark.

No additional core modification was required to pass this scenario.

Architectural conclusion

The original validation demonstrated that successful delegation can deterministically end a parent turn, eliminating unnecessary inference and polling.

This additional scenario demonstrates that the same architecture also supports reactivation following a native execution failure, including cases where the delegated worker has already produced partial, uncommitted code changes.

The orchestrator does not need to remain active throughout child execution to retain the ability to inspect and reason about failures.

The updated validation now includes 30 native scenarios, in addition to the previously reported two Codex Desktop end-to-end tests.

Runtime-level event delivery. Engineering-level recovery.

The model should decide how to recover. The runtime should ensure that it has the opportunity and information necessary to make that decision.

Additional test artifacts, sanitized native execution traces, Git diffs and machine-readable acceptance results are available if useful to the Codex engineering team.

BrunoP9 avatar Nov 24 '26 22:11 BrunoP9