Practical technical guides / AI engineering

Deliver Messages to Running Agents Without Losing the Final Update

A message sent while an agent works needs admission, boundary delivery that drops stale tool calls, and an atomic close so nothing accepted during the final flush is lost.

By Sandeep Belgavi · Published 2026-11-13

A message sent while an agent works needs admission, boundary delivery that drops stale tool calls, and an atomic close so nothing accepted during the final flush is lost.

Input that arrives while the agent is working

A person watching a coding or support agent work will often want to correct it mid-turn: "only CASE-7031, not CASE-7030", "use the new return policy", "stop, that is the wrong order". Agent hosts increasingly accept this input while a turn is running instead of making the user wait for it to finish. Codex calls such a message a steer.

Accepting input mid-turn turns a simple request-response loop into a small concurrent system. A message can arrive while the model is sampling, while a tool is running, while the final answer is being written, or after the turn has ended. Each moment needs a rule, and several of the bugs fixed recently in coding agents come from a missing or racy rule.

Three Codex pull requests merged on 2 November 2026 and shipped in Codex CLI rust-v0.168.0 on 13 November show the shape of the problems. Pull request 58877 fixes input accepted while a turn was flushing its transcript: it could miss the final pending-input check and stay unprocessed when the turn completed; the fix checks pending input atomically with closing steer admission and, if input was accepted, resumes sampling in the same turn. Pull request 58988 makes injected agent messages reach active sampling before stale tool calls execute, while messages queued around a final answer wait for the next task without forcing another response. Pull request 58998 fixes steering after switching back to a long-running thread, when the turn-started notification had fallen out of a bounded replay buffer. Claude Code v2.1.323, released on 12 November, lists a related fix: a resumed session joining the user's next message to an old interrupted message that was not resumed.

This guide turns those fixes into a design you can apply to any agent host, with a scripted model that reproduces the race.

Admission and delivery are different things

Two separate questions are easy to blur.

Admission is whether the running turn accepts a message at all. While admission is open, a message goes into the turn's mailbox and the sender is told it was accepted. Once the turn is closing, new messages belong to the next task.

Delivery is when an accepted message actually reaches the model. The model is not interrupted in the middle of generating a token, and a tool call that is already executing usually cannot be unwound. So delivery happens at a boundary: before the next model sample, and before the next tool call starts.

The second half of that rule is the one pull request 58988 is about. Suppose the model planned two tool calls, look up the order and draft a refund, and the user's correction arrives before either has run. If the host executes both and only then shows the model the correction, it has drafted a refund for the wrong case. The tool calls were planned from a view of the conversation that is now stale. Delivering the steer first, and dropping the planned calls so the model can re-plan, is the safe default.

The worked example

The fictional Belgavi's Book Store help desk uses an agent that, for a refund request, plans: sample, look up the order, draft the refund, sample, final answer. In the companion model the plan is a list, not a model call.

  • A steer arrives before the order lookup: "Only CASE-7031, not CASE-7030". At the next boundary the host delivers it and drops both planned tool calls. The executed steps are sample, sample, final.
  • A steer arrives while the order lookup is running. The lookup finishes; at the boundary before the refund draft, the steer is delivered and the draft is dropped. Executed: sample, lookup, sample, final.
  • No steer: the plan runs as written.

Dropping a planned call is safe only because nothing has happened yet. If a planned call has side effects and your host cannot tell whether it started, treat it as started and let the model see its result alongside the steer.

The final-flush race

The end of a turn is where messages get lost. The turn has produced its final answer and is writing the transcript. The code checks the mailbox, finds it empty, and then closes admission. Between the check and the close, a message is accepted. The sender was told it was accepted; the turn has ended; nothing will ever deliver it.

The companion model makes the interleaving explicit. With check-then-close, the steer "Also mention the return window" is accepted during the flush and lost. With an atomic close, admission is closed and the mailbox inspected as one step: the steer is found, the host resumes sampling in the same turn, and the executed steps become sample, final, sample, final. This is the fix pull request 58877 describes: check pending input atomically with closing admission.

def close_turn(turn):
    with turn.lock:                 # the same lock steer() takes
        turn.admission_open = False
        pending = list(turn.mailbox)
        turn.mailbox.clear()
    if pending:
        turn.admission_open = True  # reopen, deliver, sample again
        return deliver_and_resume(turn, pending)
    return finish(turn)

The important property is that steer() and close_turn() take the same lock, so a message is either seen by the close or rejected to the next task. There is no third state.

After the final answer

A message that arrives after admission closes is not an error and not a steer. It is the first input of the next task. Pull request 58988 adds a second rule here: such messages should not reopen delivery or force a follow-up response from the turn that just ended. In the model, "Tomorrow, check CASE-7032" sent after the final answer is queued for the next task and the finished turn stays finished.

Tell the user which happened. "Added to the current reply" and "Will be handled next" are different promises.

Resume and replay

Steering also needs the host to know which turn is running. A client that reconnects to a long-running thread typically rebuilds its view from a replay of recent events. If the event that said "turn 42 started" has been evicted from a bounded buffer, the client no longer knows turn 42 is active, and its steers have nowhere to go. Pull request 58998 retains the active turn outside the bounded buffer until it completes. The general rule: the active turn's identity is state, not history, and must not be subject to history's retention limits.

The Claude Code fix points at the mirror image. When a session is resumed after an interruption, an interrupted message that was not resumed must not be merged into the next message the user types. Keep interrupted input as a separate, visible item the user can resend or discard.

What stays deterministic

Everything in this guide is host code: the mailbox, the lock, the boundaries, the stale-call rule, the next-task queue and the replay retention. The model only ever sees delivered messages, in order. Do not ask the model to decide whether a late message belongs to this turn; it cannot see the race.

Migration steps

  • Give each turn an explicit admission state and a mailbox, guarded by one lock.
  • Deliver at boundaries before every model sample and every tool call; drop or re-plan tool calls planned before the delivered message.
  • Close admission and inspect the mailbox atomically; on pending input, resume in the same turn.
  • Route post-close input to the next task and tell the sender.
  • Retain the active turn outside any bounded replay buffer.
  • On resume, keep interrupted input separate from new input.

Failure modes

  • Accepted input that is never delivered, from check-then-close.
  • A tool call executed with a plan the user already corrected.
  • A late message forces an extra model response after the final answer.
  • A reconnected client cannot steer because the turn start was evicted.
  • An interrupted message is concatenated with a new one on resume.

When this is unnecessary

If your agent runs unattended, or users can only send a new message after the turn ends, you do not need steering. A simpler alternative is a cancel button plus a new message; it costs the work already done but has no races. Add steering when turns are long and corrections are common.

Verify it

The companion's six tests cover delivery before tools, delivery during a tool, the lost-message race, the atomic close and the next-task queue. Against a real host, inject a message at every boundary with a scripted model, including during the final flush, and assert that every accepted message reaches the model exactly once.

Run the example yourself

The scripted turn with a mailbox, boundary delivery, stale tool-call dropping, the check-then-close race and the atomic close, with six tests. It calls no provider. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Primary references and authorship

Sources read 2026-11-13. Original explanation and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.