Skip to content

feat(agent): minimal agent edits, ask before saving unsaved work, readable history - #410

Merged
fcoury merged 15 commits into
mainfrom
feat/agent-edit-ux
Oct 18, 2026
Merged

fcoury merged 15 commits into
mainfrom
feat/agent-edit-ux

Conversation

@fcoury

@fcoury fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

The real-Codex pair-mode evaluation found three problems with how agent edits land in the editor. Each one comes from the agent's write replacing the whole buffer and then saving it:

  • Cursor jumps. An agent edit reset the cursor column (26:22 became 26:1), and undoing it sent the cursor to 1:1.
  • Unsaved work reached disk. An agent edit to a buffer with unsaved changes saved the whole buffer, the user's work in progress included, without asking.
  • :AgentHistory was unusable. Rows showed only a › marker. The detail was the whole file before and after with newlines stripped, so a one-line change read as two very long lines. The history also covered only the active buffer.

What changed

Minimal edits (src/editing/line_hunks.rs, apply_agent_contents in src/editor.rs)

  • Agent writes are reduced to the changed line runs with similar and applied hunk by hunk inside the same single agent transaction.
  • The cursor and viewport stay on their text. Lines inserted or removed above them shift them, and the column is kept.
  • One u still undoes the whole agent edit and restores the cursor where it was.
  • All existing guards are unchanged: stale revision, preserves_unsaved_edits, external change, NUL bytes, receipt capacity, and inline-history recording.

Asking about unsaved changes (src/editor/agent_edit_decision.rs, src/ui/confirmation.rs)

  • If a write targets a buffer the user has changed, and it would apply cleanly and keep those changes, Red holds the tool call and asks. The three choices are:
    • u · Apply, keep unsaved: the default, and what Enter picks.
    • Apply and save: today's behaviour. It has no hotkey, so you have to select it.
    • n · Don't apply: also what Esc picks.
  • The answer is remembered for that conversation and file until the file is next saved, so a multi-edit turn asks only once. Buffer::save_generation tracks the saves.
  • The dialog may appear while you are typing, so it waits for a 0.7 s pause in input and ignores keys for 350 ms after it opens.
  • The tool result tells the agent exactly what happened: saved: false with "left unsaved at the user's request", or declined: true. INSTRUCTIONS tells it to report unsaved edits and never retry a declined one.
  • write_file and apply_edits may now wait up to 10 minutes, instead of 30 s, in both the Codex dispatcher and EditorToolHost.
  • A held write is released, and its dialog closed, if:
    • the turn is cancelled;
    • the app-server stops;
    • the Codex side timed out (the response channel closed).
  • New tool requests are not taken while a write is held, so tool calls stay in order.
  • Confirmation gains new_choices for any number of options, each with an optional hotkey, h/l/Tab navigation, a safe default and a designated cancel option.

Edit history (src/editor/edit_history.rs, plugins/agent.hk, host API 0.22.0)

  • EditHistory(callback, scope?) takes a new "workspace" scope that covers every open file, newest first. Each entry gains these fields:
    • file: workspace-relative path.
    • buffer_id.
    • applied: false once the edit is undone.
    • age: for example "5m ago".
    • added and removed: line counts.
    • document: a ready-to-show unified diff with hunk headers, three context lines and line numbers.
    • turn and prompt: for agent edits.
  • Agent edits keep an exact diff captured when they were applied. Other transactions are rendered from their recorded replacements.
  • :AgentHistory renders:
    • Labelled rows, such as ◆ src/util.rs +4 turn 1 · just now for agent edits and · src/report.rs +1 you · 5m ago for yours.
    • A header such as "3 edits across 2 files · 2 by the agent".
    • The diff in the workspace's themed diff view, titled with the file, turn and prompt.
    • An a toggle that shows agent edits only.
    • An empty state that explains what will appear.
  • RevertTransaction now finds the buffer that owns the transaction, so revert works on any open file without moving focus. It reports what it did, or why it can't revert.
  • docs/PLUGIN_API.md has a new "Edit history" section. 0.21.0 stays in the supported list.

How to Test

  1. Cursor stays put. Open src/report.rs in a Rust project and put the cursor at 24:13 inside format_cents. In the Agent pane, ask "Add a one-line doc comment directly above pub fn summarize. Change nothing else."
    • Expected: the statusline reads 25:13 and the cursor is on the same character.
    • Press u. Expected: the comment is gone and the cursor is back at 24:13.
  2. Unsaved buffer. Append // USER-UNSAVED to that file without saving, then ask the agent for another doc comment.
    • Expected: a dialog titled "Unsaved changes in src/report.rs" with [ u · Apply, keep unsaved ] [ Apply and save ] [ n · Don't apply ].
    • Press Enter. Expected: the buffer shows [+], the file on disk has neither change, and the agent reports that it left the buffer unsaved.
    • Ask for another edit to the same file. Expected: no second question.
    • Repeat in a fresh session with Tab then Enter. Expected: both changes are on disk.
    • Repeat with n. Expected: nothing changes, and the agent describes the edit instead of retrying it.
  3. History. After the agent edits two files, type in one of them and run :AgentHistory.
    • Expected: rows are labelled as above, and each diff has @@ -2,3 +2,7 @@ headers, context and line numbers.
    • Press a to show only agent edits. Select an agent edit to another file and press r. Expected: "Reverted the agent edit in src/util.rs; the buffer is unsaved and u in it undoes the revert", and focus stays where it was.

I ran all three flows against a real Codex app-server in tmux (gpt-6.1-sol).

Targeted tests:

  • cargo test --lib line_hunks
  • cargo test --lib agent_edits_rewrite_only_changed_lines_and_keep_the_cursor_on_its_text
  • cargo test --lib agent_edits_to_unsaved_buffers_ask_once_per_save_and_honour_each_choice
  • cargo test --lib an_unsaved_edit_question_waits_for_a_typing_pause_and_ends_with_its_tool_call
  • cargo test --lib choice_dialogs_ignore_early_keys
  • cargo test --lib workspace_history_labels_agent_turns_shows_context_and_reverts_other_files
  • cargo test --lib bundled_agent_history_labels_rows_and_shows_a_line_diff

…dable history

Agent writes replaced the whole buffer, so the cursor jumped, undo
recorded the entire file, and :AgentHistory showed two huge lines. They
also saved the buffer, taking the user's unsaved changes to disk.

- Apply agent writes as line hunks inside one transaction. The cursor
  and viewport stay on their text, and undo restores the cursor.
- When a write targets a buffer with unsaved user changes, hold it and
  ask: apply and keep unsaved (default), apply and save, or don't apply.
  The answer is remembered per conversation and file until the next
  save. The question waits for a pause in typing and ignores keys for a
  moment; saving has no hotkey. Edit tools may wait 10 minutes; cancel,
  a lost app-server or a timed-out call closes the question.
- Confirmation gains a multi-choice variant.
- EditHistory (host API 0.22.0) takes scope "workspace" and returns file,
  turn, prompt, age, line counts and a unified diff with context per
  entry. :AgentHistory uses it: labelled rows across files, a themed
  diff, an agent-only filter, an empty state, and revert works in any
  open file with clear feedback.
@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-18T03:15:37.375728Z 91ae60f 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: c4288c4188

ℹ️ 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/host_api.json Resolved
Comment thread src/editor/edit_history.rs Resolved
Comment thread src/editor.rs Resolved
Comment thread plugins/agent.hk Resolved
- Keep host API 0.21.0: it is unreleased, so EditHistory.scope and the
  new entry fields join the existing 0.21.0 entry instead of a bump.
- Bound history previews inside a single large replacement while still
  counting every added and removed line.
- Release a pending unsaved-edit question when its conversation is
  closed, archived or reset, so later sessions are not held behind it.
- Count only edits in the history summary, not the truncation notice.
@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: c4288c4188

ℹ️ 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/edit_history.rs Resolved
Comment thread plugins/agent.hk Resolved
A capped preview reported total_lines as the kept length, and setting
truncated made the workspace offer an "L full patch" action history
cannot provide. Keep the full rendered length, end a capped preview with
a note about the omitted lines, and leave the document untruncated.
@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: d925053b89

ℹ️ 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/edit_history.rs Resolved
- Show the selected row's diff whenever the selection moves, by mouse,
  half-page, gg/G, scrolling or filtering, so r reverts the edit on
  screen.
- Workspace history entries drop the unbounded edits texts in favour of
  the bounded document, and an oversized result is trimmed to its newest
  entries instead of being dropped.
@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: 928de2e483

ℹ️ 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_edit_decision.rs Resolved
Comment thread src/editing/line_hunks.rs Resolved
…ines

- After the user answers the unsaved-changes question, the status line
  reports what the write did (applied, saved, save failed, not applied)
  instead of the choice, which could claim success for a refused write.
- Diff previews clip each line to 1,000 characters, so a changed line in
  a minified file does not keep the whole file in every preview.
@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: fb458c5676

ℹ️ 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/edit_history.rs Resolved
Choose the newest 500 transactions from parent links and timestamps
first, then serialize and diff only those, so a long history of large
edits does not stall the editor thread building entries it drops.
@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: dd5a3b4d15

ℹ️ 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/edit_history.rs Resolved
Only the new workspace scope is sorted newest first and capped; an
unscoped request still returns the active buffer's whole undo tree in
node order, as existing plugins expect.
@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: 9c5d065582

ℹ️ 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/runtime.rs Resolved
History results now carry their scope, and heap trimming applies only to
workspace results, which promise a newest-first prefix and a total. An
unscoped result is delivered whole or not at all, as before.
@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: 7298366e7e

ℹ️ 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/edit_history.rs Resolved
Comment thread src/editor/edit_history.rs Resolved
- Unscoped EditHistory entries keep exactly their shipped shape; the
  diff document and labels are only added for the workspace scope, so a
  legacy result is no larger than before.
- Diffs rendered from recorded replacements list hunks in document
  order, since agent hunks are applied bottom to top.
@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: 505f0ca22a

ℹ️ 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_edit_decision.rs Resolved
If the buffer was saved while the unsaved-changes question was open, the
held write saw a clean buffer and was applied and saved, ignoring a
"Don't apply". The answer now governs the write it was given for.
Also make the typing-pause test independent of how slowly it runs.
@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: 9fab75f63e

ℹ️ 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/edit_history.rs Resolved
The workspace hides meta lines by default once a diff has hunks, so the
note about a capped preview was invisible. Render it as an unnumbered
context line instead.
@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: 5e53ce6d0a

ℹ️ 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.rs Resolved
Revert looked up the transaction's origin, and its buffer, by cloning
the whole undo tree with every edit's text. Borrow the one transaction
with UndoHistory::transaction_by_id instead.
@fcoury

fcoury commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

# Conflicts:
#	docs/plugin_api_changes.json
#	src/codex/mod.rs

@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: 91ae60fd5b

ℹ️ 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 docs/plugin_api_changes.json Resolved
@fcoury
fcoury merged commit 3fe3a7c into main Oct 18, 2026
19 checks passed
@fcoury
fcoury deleted the feat/agent-edit-ux branch October 18, 2026 03:41
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