Skip to content

fix(cli): rewind resets to before the turn and refills its prompt - #700

Merged
Astro-Han merged 2 commits into
mainfrom
fix/cli-rewind-before-turn
Jul 9, 2026
Merged

Astro-Han merged 2 commits into
mainfrom
fix/cli-rewind-before-turn

Conversation

@Astro-Han

@Astro-Han Astro-Han commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Rewind (double-Esc / /rewind) now resets the conversation to just before the chosen turn and refills the editor with that turn's prompt for an edit-and-resend, instead of branching through the turn. Every prompted turn — including the latest one — is a valid target.

Why

Refs: interrupting the latest turn left its aborted turn_state as the last message, and listRewindTargets excluded the "head" turn. So the very turn the user had just interrupted (the one they wanted to redo) was dropped from the rewind list — they could only rewind to the turn before it.

Rather than patch the head-exclusion, switch rewind to the semantics the gesture already implies (matching rewind elsewhere): reset to before the turn + refill its prompt. With that, the head concept — and the bug — disappears.

Scope

Changed:

  • packages/runtime/src/session-manager.ts — add branchBeforeTurn, the exclusive dual of branchFromTurn (unchanged; desktop still uses it). Both share a new private createBranchSession. copyMessagesBeforeTurn slices by turn membership, not array position.
  • packages/cli/src/session-driver.ts — listRewindTargets lists every prompted turn newest-first; rewindToTurn branches before the turn and returns its full prompt (MakaSessionRewindResult).
  • packages/cli/src/pi-tui-runner.ts — refill the editor via editor.setText, update notice/picker copy.
  • Tests in runtime + cli.

Not included:

  • No desktop wiring for branchBeforeTurn (CLI-only bug; the runtime primitive is there if desktop wants it later). branchFromTurn semantics untouched.

Verification

  • npm run -w @maka/runtime test → 1126 pass, 0 fail (2 skipped)
  • npm run -w maka-agent test → 187 pass, 0 fail
  • npm run typecheck → clean across all workspaces

User-facing impact

Behavior change (intended): rewinding to a turn now discards that turn and refills its prompt, so a single-turn session becomes rewindable (empty branch + refilled prompt), and the picker/notice copy changed to say so. Non-destructive as before — the original session, including the discarded turn, is preserved.

Reviewer notes

The one subtle bit is copyMessagesBeforeTurn: getSessionView does not return messages in strict chronological order (a user prompt can sort after its turn's turn_state), so a positional "slice before first occurrence" would drop an earlier turn's prompt. It slices by turn membership instead. There's a runtime test that locks this exact case.

Interrupting the latest turn left its aborted turn_state as the last
message, and listRewindTargets excluded the "head" turn — so the turn the
user just interrupted, the one they wanted to redo, was dropped from the
rewind list. They could only rewind to the turn before it.

Switch rewind to the semantics the gesture implies: rewinding to a turn now
resets to the state just *before* it (discarding it and everything after)
and refills the editor with its prompt for an edit-and-resend, matching how
rewind works elsewhere. With that, every prompted turn — including the
latest/interrupted one — is a valid target, and the head-exclusion (and the
bug) goes away.

- runtime: add branchBeforeTurn, the exclusive dual of branchFromTurn
  (kept unchanged; desktop still uses it). Both share createBranchSession.
  copyMessagesBeforeTurn slices by turn membership, not array position,
  because the read model does not guarantee a turn's messages are contiguous
  or that a user prompt precedes its turn_state. An empty copy (first turn)
  is a valid branch into empty context.
- cli: listRewindTargets lists all prompted turns newest-first; rewindToTurn
  branches before the turn and returns its full prompt; the runner refills
  the editor via editor.setText and updates the notice/picker copy.
@Astro-Han
Astro-Han merged commit 1075b62 into main Jul 9, 2026
3 checks passed
@Astro-Han
Astro-Han deleted the fix/cli-rewind-before-turn branch July 9, 2026 18:05
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