Skip to content

fix(chat): branch a history edit only when it is sent - #967

Closed
uvforce wants to merge 2 commits into
agegr:mainfrom
uvforce:pr/defer-history-edit-branch
Closed

uvforce wants to merge 2 commits into
agegr:mainfrom
uvforce:pr/defer-history-edit-branch

Conversation

@uvforce

@uvforce uvforce commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #958.

Problem

"Edit from here" calls navigate_tree as soon as it is clicked. For a user message, pi moves the session leaf to that message's parent and persists the move, so the session is already on the new branch before the user has decided to send anything.

  • There is no way to back out. Clearing the composer leaves the leaf where it is.
  • A reload shows the conversation cut off at the edited message. Nothing is deleted from the JSONL, but the rest of the turn is only reachable through the branch navigator.
  • If the composer already holds a draft, replaceMessage refuses to overwrite it, but the navigation has already happened.

Fix

Branch only when the edit is actually sent.

  • Clicking "Edit from here" prefills the composer and records the target entry in useAgentSession. No RPC is sent. If the composer already has text or images, nothing happens: replaceMessage now reports whether it prefilled, and the edit starts only when it did.
  • While an edit is pending, the composer shows an "Edit from here" banner with a Cancel button. Cancel clears the prefill and the pending edit; the session is untouched. A reload also leaves the session untouched.
  • Sending navigates first and then takes the normal prompt path. If the navigation fails, the draft and the pending edit are restored and no prompt is sent.
  • The pending edit is kept per session alongside the in-memory draft, so switching away and back keeps it for as long as the draft exists.
  • A pending edit is never sent as steer or follow-up. If a run starts while an edit is pending, the text stays in the composer until the run ends.

ChatWindow only rewires the hook callbacks. MessageView no longer receives onNavigate; the edit button is shown when onEditContent is present.

Edit banner

Known limitation

Navigation and the prompt are still two RPCs. If the prompt is rejected after the navigation succeeded, the leaf stays on the new branch; making both atomic needs a server-side change and is out of scope here.

Alternative considered

The patch attached to #958 keeps the navigation on click and adds a Cancel that navigates back to the previous leaf. That still loses the view if the page is reloaded, closed or crashes before Cancel, and navigating back to a user-message leaf lands on its parent again, because navigateTree treats a user message as "edit from here".

Verification

  • npm test (1308/1308), tsc --noEmit and npm run lint pass on this branch. A new source test covers the click, Cancel and send paths.
  • Browser check against a fixture session with no model configured: clicking "Edit from here" prefills without changing the leaf; Cancel and a reload keep the leaf and the original reply visible; an existing draft is not overwritten; switching sessions and back keeps the pending edit.
  • With /api/agent requests intercepted: a rejected navigate_tree restores the edited text and the banner without sending a prompt; a successful send issues navigate_tree and then prompt, in that order.

"Edit from here" called navigate_tree on click, which persisted a leaf
move before the user committed to the edit. There was no way back, and
a reload showed the conversation cut off at the edited message (agegr#958).

Clicking now only prefills the composer and records the target entry in
useAgentSession. The composer shows an edit banner with Cancel, which
clears the prefill without touching the session. Sending navigates first
and then takes the normal prompt path; a failed navigation restores the
draft and the edit state. The pending edit follows the session's
in-memory draft across session switches, and a pending edit is never
sent as steer or follow-up.

Navigation and prompt remain two RPCs, so a prompt rejected after the
navigation keeps the new leaf.
Kano-u added a commit to Kano-u/pi-web that referenced this pull request Sep 26, 2026

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks @uvforce! #958 is real, and deferring the branch until send is the right fix. It's better than navigating back on Cancel, for the reload reason you gave.

I couldn't push to your fork, so it continues in #1009 with your commits kept, plus two changes:

  • Reverted "expose history edit actions on touch screens". Always-visible 44px Copy / Edit / New session buttons under every user message on phones is a separate layout decision, and a tap already reveals them.
  • Merged main and resolved the ChatInput props conflict with the default-model star.

Closing in favor of #1009.


Generated by Claude Code

@agegr agegr closed this Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…#907 agegr#941 agegr#946 agegr#961 agegr#968 agegr#969 agegr#1007 agegr#1008 agegr#1009 agegr#1010)

上游已把我们提前摘取的 agegr#884 / agegr#871 / agegr#996 / agegr#961 原样合并(agegr#961 逐字节相同),
agegr#967 与 agegr#963 由上游的 agegr#1009 / agegr#1008 接管。

冲突解决:
- demo/ 对齐上游 fe288c0,撤销 agegr#963 对 demo 的两处改动(`@/lib/chat-phase-label`
  在 demo/lib 下不存在,会打断 Pages 构建)。
- AGENTS.md:保留我们的 default-project 条目,open-folder 换成上游的
  open-in-explorer。
- MessageView.tsx / SessionSidebar.tsx / FileExplorer.tsx:以上游为准,
  DismissButton 采用上游的独立文件,保留我们的思考卡片与文件管理改动。
- SessionSidebar 移除引用已删函数的旧 agegr#951 工作区按钮(agegr#951 的其他入口另行收尾)。
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
agegr#967 / agegr#963 已由上游的 agegr#1009 / agegr#1008 落地,agegr#961 上游原样合并(逐字节相同),
agegr#951 已收敛到上游的 agegr#907,agegr#960 上下游都拒绝——这些都与上游一致,按台账规则删行。
agegr#899 上游关闭但本地保留,状态注明为永久分叉。
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.

「从此处编辑」后无法取消,刷新后其后整段对话从视图消失

2 participants