Skip to content

#473: an undo that removes a message needs Delete - #768

Merged
benandez merged 7 commits into
UCSLabs:mainfrom
vangel76:feat/473-undo-that-removes-needs-delete
Nov 4, 2026
Merged

benandez merged 7 commits into
UCSLabs:mainfrom
vangel76:feat/473-undo-that-removes-needs-delete

Conversation

@vangel76

@vangel76 vangel76 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #473

Cause: an agent's edit.undo or edit.redo that takes a message away (undoing a copy or an import, redoing a permanent delete) was classed as safe, so an agent without the Delete permission could remove mail through undo. Redo of a permanent delete was a gap too.

Fix:

  • Session::undo_takes_mail runs the real undo merge (keep_in_step, the same call Session::swap makes) on a copy of the mailbox and checks whether a current message is missing afterwards. A plain key comparison would ask about mail that arrived after the step, which the real undo keeps. undo_reverts returns Class::Delete for it, on both the Safe and the Asked path.
  • describe keeps the Accounts and Send wording ahead of the mail text, so a step that takes mail and also reverts an account change or a send does not hide the stronger effect. The text now reads "takes away messages".
  • edit.undo and edit.redo refuse before anything moves when an agent lacks Delete and the step takes mail.
  • docs/threat-model.md has one more sentence: on a server account the undo of a copy goes to Deleted Items and is not counted as a removal.

Please check:

  • The early refusal in edit.rs is there because Session::run leaves the undo and redo stacks swapped when it refuses after the command ran (filed as A refused agent command that ran edit.undo or edit.redo leaves the undo and redo stacks swapped #767). It also covers removing a draft, which the older backstop allowed (the gate already asked for Delete there). I changed one existing test, a_message_whose_draft_was_taken_away_is_saved_again_before_an_agent_leaves_it, to grant Delete.
  • On Graph and Exchange accounts an undo that never reached the server asks for Delete too (it fails closed); a test pins it.
  • The check copies the mailbox, so each agent undo or redo costs O(n) in the number of messages, up to three times per call (gate, description, command). Owners are not affected.

Tests: 13 new tests in undo_removes_mail_tests.rs and 2 in cmd/ews_up_tests.rs, including undo and redo through Session::run with and without the grant. With undo_takes_mail forced to false, 9 fail; with the early refusal removed, both through-run refusal tests fail.

Human-reviewed, AI-assisted, nobody's mailbox harmed.

Summary by CodeRabbit

  • Bug Fixes
    • Undo and redo now require delete permission when an action would remove messages, such as undoing a copy or import or redoing a permanent delete. Without permission, the action is stopped and your mailbox history remains unchanged.
    • Actions that don’t remove messages continue without this additional permission check, including undoing flag changes, moves, or deletions to Deleted Items.

Human-reviewed, AI-assisted, nobody's mailbox harmed.
Human-reviewed, AI-assisted, nobody's mailbox harmed.
Human-reviewed, AI-assisted, nobody's mailbox harmed.
Human-reviewed, AI-assisted, nobody's mailbox harmed.
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Files ignored due to path filters: 1
  • docs/threat-model.md is excluded by !docs/**
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 5e58e24 and 309408b.

Files ignored due to path filters: 1
  • docs/threat-model.md is excluded by !docs/**

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Undo and redo now detect when an operation removes a message and classify it as requiring Delete permission. Commands check agent permission before changing history. Tests cover removal detection, permission outcomes, and server-confirmed copies.

Changes: Undo and redo message-removal permissions

Layer / File(s) Summary
Detect and classify message removal
crates/engine/src/session.rs, crates/engine/src/undo_removes_mail_tests.rs, crates/engine/src/cmd/ews_up_tests.rs, crates/engine/src/lib.rs
Session::undo_takes_mail compares current message IDs with the merged mailbox for an undo or redo step. undo_reverts classifies message removal as Class::Delete. Tests cover removal and preservation cases, including server-confirmed copies.
Check Delete permission before history changes
crates/engine/src/cmd/edit.rs, crates/engine/src/agent.rs, crates/engine/src/cmd/agent_leaves_compose_tests.rs, crates/engine/src/undo_removes_mail_tests.rs, packaging/release-notes.d/fixes/473.md
Undo and redo refuse before changing history when an operation removes mail and the agent lacks Class::Delete. The operation description identifies message removal. Tests cover permission outcomes, and the release note describes the requirement.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant EditCommand as edit.undo or edit.redo
  participant Session
  participant AgentState as agent permission state
  EditCommand->>Session: undo_takes_mail(redo)
  Session-->>EditCommand: whether the operation removes a message
  EditCommand->>AgentState: check Delete permission when mail is removed
  AgentState-->>EditCommand: permission result
  EditCommand->>EditCommand: refuse or continue before changing history
Loading

Possibly related issues

Possibly related PRs

Suggested reviewers: strikerz5

Merge Risk: Low · previous review 5e58e

An owner may approve this combined undo and still see it refused. It is a narrow workflow gap with a permission-grant workaround, so it does not block merging.

Security architecture risk: Low · up to 5e58e

The change strengthens deletion controls and refuses unauthorized operations before changing history. No new security bypass was established in the inspected command path, but external callers and some recovery behavior remain outside the confirmed coverage.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The sensitive outcome is removal of currently present messages by a history step in the open Session. The check spans the mailbox message map, including messages from different accounts, rather than only the current selection.

Security Findings and Attack Paths

  • observed — The new guard blocks an agent lacking Delete from reaching history mutation when the merge would remove any message, including a draft. This strengthens the existing post-execution backstop, which excludes drafts. No introduced bypass was established in this inspected command path.

Trust Boundaries and Controls

  • inferred — Combined effects fail closed: Accounts and Send take precedence over Delete in classification, but approving only that class does not satisfy the independent Delete guard. A mail-removing combined step therefore remains refused unless Delete is also in the execution allowance; the single-class approval does not silently authorize removal.
  • observed — Direct Session undo and redo remain lower-level mutation APIs without Delete checks. Their unchanged behavior is not an introduced PR concern; authorization must continue to be applied by callers exposing them to untrusted requests.

Resilience and Maintainability Implications

  • observed — The removal-permission refusal occurs before either history stack is popped. This avoids needing post-execution rollback for this denial and preserves the original transition for a later authorized attempt.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main change: undo operations that remove messages now require Delete permission.
Description check Passed The description is detailed and on topic. It documents the cause, implementation, user-visible behavior, edge cases, performance impact, linked issue, and regression tests. It does not reproduce the t…
Linked Issues check Passed Issue #473 requires that an agent's undo or redo that removes a message or draft requires Delete, as mail.delete does. The PR summary and description show the following implementation:
- `Session::und…
Out of Scope Changes check Passed All source changes relate to #473:
- session.rs: undo_takes_mail and the undo_reverts classification.
- cmd/edit.rs: the early Delete refusal for undo and redo.
- agent.rs: description wordi…
Full details: Description check

Explanation

The description is detailed and on topic. It documents the cause, implementation, user-visible behavior, edge cases, performance impact, linked issue, and regression tests. It does not reproduce the template headings or administrative checklist, but the required technical information is mostly complete.

Full details: Linked Issues check

Explanation

Issue #473 requires that an agent's undo or redo that removes a message or draft requires Delete, as mail.delete does. The PR summary and description show the following implementation:

  • Session::undo_takes_mail applies keep_in_step to a mailbox copy and checks whether a current message is missing afterwards. Mail that arrived after the step is kept.
  • undo_reverts returns Class::Delete for such steps on both the Safe and Asked paths.
  • edit.undo and edit.redo refuse before any state change when the agent lacks Delete and the step takes mail. This covers drafts.
  • Undo of a copy or import, and redo of a permanent delete, are covered. Undo of flag, move, or move to Deleted Items stays unguarded.
  • Graph and Exchange undo that never reached the server requires Delete (fails closed).
  • Tests cover these cases, including through Session::run with and without the grant.
    The agent-with-grant path proceeds. The agent-without-grant path refuses instead of prompting. The issue wording says 'asks for Delete'. The evidence does not show how mail.delete behaves for an agent without the grant, so this wording match is not fully confirmed. The title and the core requirement (undo needs Delete) are met. This verdict relies on the PR summary and description. No code, diff, or test file was read directly.
Full details: Out of Scope Changes check

Explanation

All source changes relate to #473:

  • session.rs: undo_takes_mail and the undo_reverts classification.
  • cmd/edit.rs: the early Delete refusal for undo and redo.
  • agent.rs: description wording for mail-taking steps, with Accounts and Send kept first. This follows from the classification change.
  • undo_removes_mail_tests.rs, cmd/ews_up_tests.rs, lib.rs: tests for the new behavior.
  • agent_leaves_compose_tests.rs: one existing test now grants Delete. The new refusal makes the grant necessary for the draft-removal path. This is a direct consequence of the change.
  • packaging/release-notes.d/fixes/473.md: release note for the behavior change.
    The docs/threat-model.md change is excluded from review by the docs/** rule. It is documentation only, and no coding requirement depends on it, so this verdict does not depend on it. The O(n) copy cost is a performance trade-off. It does not show unrelated scope. The description cites #767 for the early refusal. That reason is tied to this change and is not separate work.
Full details: Docstring Coverage

Explanation

Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 10 functions across 7 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between c0be0fe and 5e58e24.

Files ignored due to path filters: 1
  • docs/threat-model.md is excluded by !docs/**
Files selected for processing: 2
  • crates/engine/src/agent.rs
  • crates/engine/src/session.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Human-reviewed, AI-assisted, nobody's mailbox harmed.
@vangel76
vangel76 requested a review from benandez as a code owner November 3, 2026 16:57

@benandez benandez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this closes the hole cleanly. Running the real keep_in_step merge on a copy, rather than diffing keys, is the right call: mail that arrived after the step isn't counted, and a Graph/EWS copy that went to Deleted Items isn't either. Refusing inside edit.undo/edit.redo before anything moves keeps both stacks and the mailbox as they were, and the owner's own undo never reaches the check. Two independent reads traced undo and redo, the unsaved path, the window's yes path and headless MCP, and found no bypass. CI is green. Clean-room check done.

Please follow up (not blocking this merge):

  1. docs/mcp.md (the safe/delete rows and the edit.undo sentence) and docs/control-protocol.md still list what undo/redo removes as contacts, events, tasks, calendars, categories or a draft; add messages, with status lines and revision rows.
  2. docs/threat-model.md: "A server account's copy goes to Deleted Items on undo" holds only for Microsoft 365 and Exchange accounts; on IMAP the copy is removed and Delete is asked.
  3. A step that reverts accounts (or a Send change) and also takes mail is asked as Accounts; a yes then hits the new refusal and the window can never ask for Delete for that step. Fails closed; the question could name the messages or ask for both.
  4. describe's drafts branch comes first, so an undo of a copy of a draft plus a normal message says only "takes away the draft … in Drafts".

Human-reviewed, AI-assisted, nobody's mailbox harmed.

@benandez benandez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this closes the hole cleanly. Running the real keep_in_step merge on a copy, rather than diffing keys, is the right call: mail that arrived after the step isn't counted, and a Graph/EWS copy that went to Deleted Items isn't either. Refusing inside edit.undo/edit.redo before anything moves keeps both stacks and the mailbox as they were, and the owner's own undo never reaches the check. Two independent reads traced undo and redo, the unsaved path, the window's yes path and headless MCP, and found no bypass. CI is green. Clean-room check done.

Please follow up (not blocking this merge):

  1. docs/mcp.md (the safe/delete rows and the edit.undo sentence) and docs/control-protocol.md still list what undo/redo removes as contacts, events, tasks, calendars, categories or a draft; add messages, with status lines and revision rows.
  2. docs/threat-model.md: "A server account's copy goes to Deleted Items on undo" holds only for Microsoft 365 and Exchange accounts; on IMAP the copy is removed and Delete is asked.
  3. A step that reverts accounts (or a Send change) and also takes mail is asked as Accounts; a yes then hits the new refusal and the window can never ask for Delete for that step. Fails closed; the question could name the messages or ask for both.
  4. describe's drafts branch comes first, so an undo of a copy of a draft plus a normal message says only "takes away the draft … in Drafts".

Human-reviewed, AI-assisted, nobody's mailbox harmed.

@benandez benandez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this closes the hole cleanly. Running the real keep_in_step merge on a copy, rather than diffing keys, is the right call: mail that arrived after the step isn't counted, and a Graph/EWS copy that went to Deleted Items isn't either. Refusing inside edit.undo/edit.redo before anything moves keeps both stacks and the mailbox as they were, and the owner's own undo never reaches the check. Two independent reads traced undo and redo, the unsaved path, the window's yes path and headless MCP, and found no bypass. CI is green. Clean-room check done.

Please follow up (not blocking this merge):

  1. docs/mcp.md (the safe/delete rows and the edit.undo sentence) and docs/control-protocol.md still list what undo/redo removes as contacts, events, tasks, calendars, categories or a draft; add messages, with status lines and revision rows.
  2. docs/threat-model.md: "A server account's copy goes to Deleted Items on undo" holds only for Microsoft 365 and Exchange accounts; on IMAP the copy is removed and Delete is asked.
  3. A step that reverts accounts (or a Send change) and also takes mail is asked as Accounts; a yes then hits the new refusal and the window can never ask for Delete for that step. Fails closed; the question could name the messages or ask for both.
  4. describe's drafts branch comes first, so an undo of a copy of a draft plus a normal message says only "takes away the draft … in Drafts".

Human-reviewed, AI-assisted, nobody's mailbox harmed.

@benandez
benandez merged commit e92228c into UCSLabs:main Nov 4, 2026
14 checks passed
benandez pushed a commit that referenced this pull request Nov 6, 2026
…asks for Delete too

- An undo or redo classed Accounts or Send that also takes away messages (or drafts,
  contacts and the like) needs Delete as well (`agent::also`): the question names what it
  takes, and the owner's yes covers both. With the other class allowed in advance, Delete is
  asked. The window refuses the yes if the step would by then need more, or take other
  messages, than the question named. Headless, the refusal names every class for `--allow`.
- The question for an undo or redo says everything it does at once: what it puts back, the
  contacts and the like, the messages it takes away (named), and the drafts.
- docs/mcp.md and docs/control-protocol.md name messages among what an undo removes;
  docs/threat-model.md says only a Microsoft or Exchange server account's copy goes to
  Deleted Items on undo.

Human-reviewed, AI-assisted, nobody's mailbox harmed.
benandez added a commit that referenced this pull request Nov 6, 2026
#473: follow-ups from #768: undo questions name everything they take, and a yes covers it
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.

Agents: an undo that removes a message should need Delete

2 participants