npm.io
0.1.0 • Published 3d ago

pi-undo

Licence
MIT
Version
0.1.0
Deps
0
Size
5 kB
Vulns
0
Weekly
0

pi-undo

A pi extension that undoes the last user turn in the current session via /undo.

Install

# local path
pi install /absolute/path/to/pi-undo

# or try once without installing
pi -e /absolute/path/to/pi-undo

Usage

Command Effect
/undo Undo the last user turn
/undo 2 Undo the last two user turns

Behavior matches selecting the previous user message in /tree:

  1. Moves the session leaf to the parent of that user message (append-only branch).
  2. Puts the undone prompt text back in the editor so you can edit and resubmit.
  3. Leaves the abandoned path in the session tree (nothing is deleted).

What it does not do

  • Does not revert filesystem changes from tools (edit, write, bash, …). Session history and disk state are separate. Use something like the git-checkpoint example if you want code restored when branching.
  • Does not run while the agent is streaming. Abort first, then /undo.

Prompt cache / read-write cache safety

Undo goes through ctx.navigateTree():

  • Session storage stays append-only (tree branch, no rewrite/truncation of JSONL).
  • Agent message state is rebuilt from the retained branch prefix.
  • The same session id is kept, so provider prompt-cache affinity remains valid for the shared prefix (everything still on the active path after the rewind).

Avoid hand-editing the session file or clearing messages outside the tree API — that is what breaks cache alignment.

How it works

user₁ → asst₁ → user₂ → asst₂   ← leaf
                 ▲
                 └── /undo targets this user message

After /undo:
user₁ → asst₁   ← new leaf
                 └── (user₂ → asst₂ still in tree as abandoned branch)

Editor contains the text of user₂ for re-edit/resubmit.

License

MIT

Keywords