Skip to content

feat(agent): resume conversations on relaunch and ask after a mid-turn crash - #409

Merged
fcoury merged 14 commits into
mainfrom
feat/agent-conversation-continuity
Oct 18, 2026
Merged

fcoury merged 14 commits into
mainfrom
feat/agent-conversation-continuity

Conversation

@fcoury

@fcoury fcoury commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

An end-to-end evaluation of pair mode against real Codex found two gaps in how the Agent pane keeps a conversation going.

Why

A crash in the middle of a turn was silent. When the Codex app-server died while a reply streamed, Red resumed the thread correctly, but the partial reply vanished from the pane (the restore replaced the transcript with Codex's saved history, which has no incomplete turn). The only sign was a passing "Agent session restored" message. Worse, queued follow-ups were sent as soon as the thread came back, on top of a turn that had stopped halfway and might have applied some of its edits. A stopped turn should stop everything, explain what happened, and ask.

Quitting and relaunching started a new Codex thread. A real thread/resume only happened with --resume or after a detached editor restarted. A normal quit and relaunch archived the transcript and replayed a recap of up to 6,000 characters into a fresh thread, so the agent lost everything beyond that recap even though Codex still had the full thread on disk.

What changed

Interrupted turns (src/editor/agent_pane.rs, plugins/agent.hk):

  • When Codex stops while a turn is running, the core lifecycle holds that turn's message in a new interrupted snapshot field. The thread still resumes in the background, but nothing is sent, queued follow-ups included, until the user answers.
  • The pane keeps what already streamed and adds a notice: Codex stopped before finishing, edits it made are still applied, and nothing more will be sent until you choose. Below it are three choices: Retry the message, Ask Codex to continue where it stopped, and Leave it here. You can pick one with Tab then ]l and Enter, with :AgentRecover or Space A (which open a picker), or with a click. Typing a new message also moves on. The status line says "Codex stopped mid-reply · Space A to choose what to do", so a hidden pane still gets the message.
  • Retry resends the message and Continue sends a short "continue" message. Both tell Codex that earlier edits are already applied and that it should check the files before redoing work. After either one, the queue drains as usual. Leave it keeps the queue paused, so :AgentSendQueued still works.
  • A turn lost when the app-server dies and no thread can be restored is held the same way. Before, it was dropped with just an error. Retry then continues in a new thread with the recap.
  • A crash while idle resumes quietly, and the pane keeps its transcript and activity rather than reloading Codex's copy.
  • While Codex reconnects, the composer stays open ("Reconnecting to Codex… you can keep typing"). A message typed then is sent as soon as the thread is back. Before, the composer was disabled.

Resume on relaunch (src/editor/agent_continuity.rs, new):

  • Each workspace saves its conversation (thread id, cwd, transcript items, model, tool-contract version, annotations) to <config>/agent/conversations/<sha256 of workspace>.json. The file is owner-only and written atomically. Saves happen at most once a second while idle and every 10 seconds while a reply streams, plus once on exit.
  • A normal launch in the same workspace loads the saved conversation and sends agent:conversation_resumable. Resuming is lazy: the thread resumes when the pane opens (AgentPrepare) or when the first message is sent, so a launch that never uses the agent never starts Codex. The pane shows "↺ Picked up where you left off 2 hours ago" and the composer footer shows "Continuing · :AgentNew starts fresh".
  • :AgentNew forgets the thread and deletes its saved conversation, so the old thread never comes back. If the thread can't be resumed, for example because of an incompatible tool contract or a missing thread, the message falls back to today's recap in a new thread. Prompts waiting on a failed resume now go to that new thread instead of sitting queued.
  • Two editors in one workspace: the first editor to use the conversation holds an OS lock on <hash>.lock until it exits. A second Red in that workspace, including one started with --resume, doesn't adopt or overwrite the saved conversation. It starts its own thread, says so in the status line ("Agent conversation is open in another Red window; this window starts its own"), and never saves that thread, even after the first editor exits. The operating system releases the lock when its editor exits or crashes, so there is no stale claim to detect.
  • AgentManager keeps a conversation_revision counter, so the editor can tell whether the saved copy is stale without comparing transcripts.

Host API 0.22.0 (src/plugin/host_api.json, docs/PLUGIN_API.md, docs/plugin_api_changes.json):

  • New calls: AgentPrepare() and AgentResolveInterruption(choice, transcript).
  • New event: agent:conversation_resumable, which carries the conversation plus an age such as "2 hours ago".
  • New snapshot fields: agent:pane_state.interrupted and resume_pending.
  • 0.21.0 stays in the supported list.

User docs in docs/AGENT_WORKFLOW.md describe both behaviours, the ownership rule and :AgentRecover.

How to Test

  1. Crash mid-reply. Press Space A and ask "Remember HERON. Reply OK". Then send "Without tools, write a 500-word essay about ledgers". While it streams, send "reply only QUEUED", then kill that red's codex app-server child (pkill -9 -P <red pid> -f app-server).
    • Expected: the streamed half of the essay stays, followed by the "Codex stopped before finishing this reply" notice, the three choices, and "1 queued · paused".
    • Wait 10 s. Expected: the queued "QUEUED" message is not sent.
    • Run :AgentRecover and pick Retry. Expected: the essay is rewritten, then "QUEUED" follows.
    • Repeat the crash and pick Continue. Expected: the reply picks up where it stopped.
    • Repeat and pick Leave it here. Expected: the queue stays paused until :AgentSendQueued.
    • The newest ~/.codex/sessions/**/rollout-*.jsonl for the workspace holds all of this in one thread.
  2. Idle crash. Kill the app-server while nothing runs. Expected: no notice. The next question ("what codeword?") answers HERON.
  3. Relaunch.
    • Ask "Remember PLOVER. Reply OK", then run :q! and start red again in the same directory. Expected: no codex app-server child until you press Space A. The pane then shows the earlier messages and "↺ Picked up where you left off …".
    • Ask "What codeword?". Expected: "PLOVER", and one rollout file holds both turns.
  4. Start fresh. Run :AgentNew, then :q! and relaunch. Expected: no resume divider. Asked about any codeword, the agent says it doesn't know one, in a new rollout file.
  5. Two editors. With red open, start a second red in the same directory. Expected: the status line says the conversation is open in another Red, and the first editor's saved conversation is untouched.

All five flows were run against real Codex (codex-cli 0.163.0) in tmux with an isolated config.

Targeted tests:

  • cargo test --lib agent_pane::tests (crash and resume transitions)
  • cargo test --lib agent_continuity (save, adopt, ownership, forget, incompatible contract)
  • cargo test --lib bundled_agent (pane rendering: partial reply kept, choices, nothing sent until chosen, Space A picker, Leave keeps the queue paused, saved conversation resumes on open)

…n crash

When Codex stopped in the middle of a turn, the pane silently dropped the
partial reply and sent queued follow-ups as soon as the thread was back.
Now the partial reply stays, a notice explains what happened, and nothing
is sent until the user chooses Retry, Continue, or Leave it.

A clean quit used to archive the conversation and start a new Codex thread
with a recap. Each workspace now saves its conversation and the next
launch resumes the same thread when the pane opens or the first message is
sent. One editor at a time owns a workspace's conversation.

Host API 0.22.0 adds AgentPrepare, AgentResolveInterruption,
agent:conversation_resumable, and the interrupted / resume_pending
snapshot fields.
The owner claim treated every recorded pid as live off unix, so a claim
left by a crashed editor was never taken over on Windows. Query the
process with OpenProcess and GetExitCodeProcess instead.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T04:01:51.305230Z c479797 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2b0d8f7211

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/plugin/registry.rs Resolved
Comment thread src/editor/agent_continuity.rs Resolved
Comment thread plugins/agent.hk Resolved
- Keep host API 0.21.0: it is still unreleased, so the new calls, event
  and snapshot fields join that version instead of bumping to 0.22.0.
- A failed save or removal no longer counts as saved, so it is retried
  and the forced exit save still runs; a failed removal keeps the
  workspace so the forgotten record is removed later.
- :AgentNew no longer resumes the saved thread it is about to forget,
  and a late restore failure for a thread the pane left is ignored.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: da6180585f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread plugins/agent.hk Resolved
Comment thread src/editor/agent_continuity.rs Resolved
…omic

- A transcript loaded from an earlier launch may miss the last reply if
  Red quit between saves, so the resumed thread's history replaces it;
  only a same-process app-server restart keeps the visible transcript.
- Owner claims are read and replaced under an OS lock on <key>.lock and
  the pid is published by an atomic rename, so two editors racing for a
  workspace can no longer both own it.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

if busy || (self.paused && !self.queue.is_empty()) {
self.queue.push_back(prompt);
return (AgentSubmitResult::Queued, Vec::new());

P2 Badge Unpause the queue when a new message answers an interruption

When a mid-turn crash has at least one queued follow-up, restore_pending leaves the lifecycle paused. Typing the advertised alternative to choosing Retry/Continue/Dismiss clears interrupted, but this condition then appends the new message and returns without unpausing or dispatching anything; the interruption choices disappear while every prompt remains stuck until the user separately invokes :AgentSendQueued. Treat this submission as the explicit continuation promised by the UI and advance the queue.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editor/agent_continuity.rs Resolved
If :AgentNew's replacement thread was created before the next save, the
empty conversation counted as saved and the forgotten record survived a
quit, so the next launch resumed the thread the user had abandoned.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c3704702a2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editor/agent_continuity.rs Resolved
Comment thread src/editor/agent_continuity.rs Resolved
- A conversation recovered by --resume or a detached restart now claims
  its workspace first; if another live editor owns it, the recovered
  thread is not resumed and its transcript stays as archived context.
- An editor denied ownership remembers it, so its separate thread is
  never saved over the owner's conversation, even after the owner exits.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0d62995eb9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editor/agent_continuity.rs Resolved
Comment thread src/editor/agent_continuity.rs Resolved
…ersations

- A recovered conversation whose ownership cannot be checked is not
  resumed, the same as one another editor owns.
- A saved conversation from an older tool contract becomes the archived
  transcript (unless the pane kept one), so its history is recapped in
  the new thread instead of being lost.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 61b9c3fd0d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editor/agent_continuity.rs Resolved
Comment thread src/editor/agent_continuity.rs Resolved
Comment thread src/editor/agent_continuity.rs Resolved
- A saved conversation whose claim fails at launch is archived as the
  transcript and this editor never saves over it.
- When the conversation moves to another workspace, the old record is
  removed and its claim released; a failed removal stays pending and is
  retried on later saves and on exit.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

self.in_flight = None;

P2 Badge Preserve unexpected stopped turns as interruptions

When an active turn ends unexpectedly with AgentTurnOutcome::Stopped—for example, a session-scoped agent:error or a non-completed agent:completed status while the app-server remains alive—this unconditionally discards the in-flight prompt. The queue is paused, but interrupted is never populated, so the new Retry/Continue recovery UI does not appear and cannot resend the original message; preserve the prompt for unexpected stopped outcomes while still excluding user-requested stops.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

A turn that ended with an error or an unrequested stop while Codex kept
running paused the queue but dropped its message, so the new recovery
choices never appeared. It now becomes an interruption too; the pane shows
just the choices under the error, since the error already explains what
happened, and :AgentSendQueued counts as moving on.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Re: Codex review 5561149396 (agent_pane.rs L361, "Preserve unexpected stopped turns as interruptions"). Fixed in 42bd6e3: turn_ended now keeps the in-flight message as interrupted when the outcome is Stopped and the user didn't request the stop, so Retry and Continue can resend it. A stop the user asked for still just pauses. The pane shows only the three choices under the existing error (no crash notice, and Space A keeps focusing the composer), and :AgentSendQueued now counts as moving on. Test: a_turn_that_fails_on_its_own_asks_but_a_requested_stop_does_not; bundled_agent_completion_places_late_activity_before_answer_and_preserves_terminal_errors now also checks that the choices follow the error.

@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 42bd6e33ba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editor/agent_continuity.rs Resolved
Comment thread plugins/agent.hk Resolved
- Adopting a saved conversation at launch rebuilds its source
  annotations, and syncing records keeps those on files that are not
  open, so a relaunch no longer deletes them.
- A failed lazy resume clears the continuation hint and the composer
  keeps saying the conversation is archived.
Ownership was a pid file checked for liveness under a lock, with
takeover of stale claims and per-platform process checks. Holding the
lock for the editor's lifetime is the claim itself: the OS releases it
when Red exits or crashes, so there is no stale owner to detect.
…on-continuity

# Conflicts:
#	docs/plugin_api_changes.json
#	src/editor.rs
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c479797a8b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editor/agent_continuity.rs Resolved
An owner still exiting could save a newer record between the peek and
the claim; adopting the peeked copy would later overwrite it.
@fcoury
fcoury merged commit 494305c into main Oct 18, 2026
19 checks passed
@fcoury
fcoury deleted the feat/agent-conversation-continuity branch October 18, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant