Critical Issue: /rewind n rewinds to a wrong number
In a new session, the first time a user runs /rewind n rewinds to the prompt directly before the one included in the command (instead of message 5, message 4 for example). This is undoable using the /rewind undo command, which works fine. Any attempts at rewinding to a specific number, however, are absolutely broken, where any number the user inputs to the /rewind command gets completely discarded and the result is that the chat is rewinded to the message the user rewinded to the first time (in the upper example, after an undo, /rewind 11 would still rewind back to message 4), completely butchering over the chat history and possible the context window of the session. I do see that issue #432 is a duplicate, however the issue still persists and needs another bugfix.
Thanks for reporting this, and for separating it from #432 clearly. Agreed this is serious: losing transcript is not recoverable if undo also misbehaves.
I dug into the rewind path and found two plausible mechanisms, but I do not want to guess at a fix that touches transcript truncation, so a couple of specifics would let me pin it exactly:
- Are you on a local session or attached to a running server? The two paths differ. The remote path numbers rendered display messages client-side (
remote_rewindable_messages) and sends that index; the server then re-derives its own target list from stored messages (rewind_target_stored_indices). If those two lists disagree,/rewind 11legitimately lands somewhere else. That mismatch is my leading suspect. - Does
/rewind(with no argument) print the numbered list you expect right before the bad/rewind N? If the printed numbering is correct but the jump is wrong, it is the client/server index mismatch above. If the printed list itself is wrong, it is the target-derivation. - Is the session tool-heavy, and does the rewind you actually land on always coincide with the last message before a tool-call cluster?
- If you can share
~/.jcode/logs/jcode-<date>.loglines containingRewound session, they record the requested index and the removed count, which distinguishes "wrong index requested" from "wrong index applied".
Meanwhile: sessions are persisted, so if you hit this again, avoid further turns and the pre-rewind transcript may still be recoverable from ~/.jcode/sessions/.
Labeling as a high-priority bug so it does not get lost.
— Jcode agent (automated triage), on behalf of @1jehuang
Update: I dug further and can now narrow this down, which should make your answers to the questions above much more targeted.
I added a test (0ffe9f484) that drives the exact sequence you described against the session layer: build a 12-entry transcript, /rewind 4, undo, then /rewind 11. It passes. After an undo, the target list is recomputed against the restored transcript and /rewind 11 keeps exactly 11 entries rather than falling back to the earlier target of 4.
That is a useful negative result: the storage/truncation layer is not where your bug lives. Combined with the existing #432 coverage of first-rewind numbering, it points at the client/server index path:
- the remote client numbers its own
display_messages()filtered to roleuser/assistant(remote_rewindable_messages), then sends that ordinal to the server; - the server independently re-derives targets from the stored transcript via
rewind_target_stored_indices(), keeping only rendered entries that carry astored_index.
Those two lists are built from different sources, so anything present in one and not the other shifts every subsequent number. Locally-committed streaming messages, compaction notices, and system-reminder filtering are all plausible ways for them to drift apart, which fits your symptom of the number being "discarded" (it is not discarded, it just means something different on each side).
So the single most valuable thing you can tell me is question 1: are you in a local session, or attached to a running server (jcode connect / a reused daemon)? If local, my analysis above is wrong and I will look again at the local path. If remote, that is very likely it.
Two more things that would pin it exactly:
- The
Rewound session ... to message N (removed M)line from~/.jcode/logs/jcode-<date>.log. If the logged N differs from what you typed, it is the client; if N matches but the result is wrong, it is the server. - Whether the session is tool-heavy or long enough to have compacted (a "Earlier conversation compacted" banner in the transcript).
Thanks for your patience. I would rather land a fix I can prove than guess at code that truncates transcripts.
— Jcode agent (automated triage), on behalf of @1jehuang
Happy to take a look at this. Based on the description, it sounds like the rewind target index isn't being recalculated fresh on each /rewind n call — instead it looks like the index from the first rewind gets cached/reused, and the initial calculation is also off by one. That would explain both symptoms: the first /rewind n landing one message early, and every subsequent /rewind n (regardless of n) landing back on that same wrong message until /rewind undo is used.
I'll dig into the rewind command handler and session history logic to confirm and put together a fix, plus a regression test covering repeated /rewind n calls in a single session. Will follow up with a PR referencing this issue and #432.
Following up on my earlier comment: my caching hypothesis was wrong. Nothing is cached — the undo snapshot and the target list are both recomputed on every /rewind. Your test in 0ffe9f484 is right, and so is the negative result you drew from it. But the bug is real, and it is one layer above where that test looks.
Root cause
Rewind targets resolve to a stored message, and the cut executes as truncate_messages(stored_index + 1). A stored message can render as several numbered transcript entries: an assistant turn whose text is split around an inline tool result renders as text-before-tool, the tool card, then text-after-tool — all backed by one stored message, all mapping to the same stored index.
Driving the real /rewind dispatcher (not the Session primitives) over such a transcript, the targets are [0, 1, 2, 3, 3, 4, 5] for 7 numbered entries — note the repeated 3:
FULL: [prompt-1, answer-1, prompt-2, before-tool, after-tool, prompt-3, answer-3]
/rewind 4 -> [prompt-1, answer-1, prompt-2, before-tool, after-tool] <- kept entry 5
/rewind 5 -> [prompt-1, answer-1, prompt-2, before-tool, after-tool] <- identical to /rewind 4
That is both reported symptoms from one defect:
- the off-by-one — rewinding to the first of the shared entries keeps the later one, so the cut lands past the entry the user numbered;
- "the new N is ignored" — distinct N collapse onto one truncation point, so a later
/rewind Mreproduces an earlier/rewind Nexactly. A tool-heavy session contains many such collisions, which is why it reads as "it always goes back to the first target."
This also explains why it reproduces on a transcript with tool-using turns and not on the plain 12-entry one: with no split turns there are no duplicate indices, and /rewind 4 -> undo -> /rewind 11 behaves correctly. Your test passes because it is correct, not because the bug is absent.
Why #432 did not close it
#432 fixed the numbering — it made the numbered /rewind list and the executor agree on which rendered entries are numbered, instead of counting raw stored messages. It left the execution at whole-stored-message granularity. Its own doc comment records that several entries "map to the same stored index" as acceptable; that collapse is the remaining bug.
Fix
Branch: https://github.com/1jehuang/jcode/compare/master...tirthfx:fix/688-rewind-target-block-boundary
RenderedMessagecarriesstored_block_len— how many leading content blocks of its stored message must be kept for the transcript to end exactly at that entry. For an entry flushed at an inline tool result this excludes the tool-call blocks that follow, so a cut also cannot leave atool_usewith notool_resultin the provider payload.Session::rewind_targets()returnsRewindTarget { stored_index, block_len };Session::truncate_to_rewind_target()performs the cut and drops a message left with no content blocks.- Both executors use it: the local TUI
/rewind Nand the server-authoritativeAgent::rewind_to_message, which backsRequest::Rewindon the remote path.
After the fix every N lands on exactly entry N, and no two N share a transcript.
Tests
To your point about layers — the existing 0ffe9f484 case only exercises Session methods, so it passes without proving the dispatcher is fixed. Coverage is added where a rewind is actually executed:
- jcode-tui —
/rewind Nthrough the real command dispatcher: every N lands on entry N, no two N share a transcript,/rewind N -> /rewind undo -> /rewind Mhonours M, and cutting before a tool call leaves no danglingtool_use. - jcode-app-core —
Agent::rewind_to_message/undo_rewindover the same sequence, covering the remote path. - jcode-base — the split-turn primitives; the existing #688 case retargeted onto the new API.
15/15 rewind tests pass across the three crates; cargo check --workspace --all-targets is clean.
Caveats
- I could not run the full suite to completion. Unrelated tests fail or hang in my environment — missing provider credentials, macOS
/var->/private/varpath comparisons, temp-cwd races, token-cap assertions on themacos_computer_usedescription, and account-picker/update-check tests that hang. None touch session rendering or rewind, but I did not run a clean baseline to confirm they are pre-existing. - On your question about local vs remote: the same whole-message truncation was in both
Agent::rewind_to_messageand the local/rewind, so the fix covers both regardless of which mode the reporter used. Separately, the remote client still derives the index it sends from its owndisplay_messages()rather than the server's canonical render. I chased that as a suspect and could not construct a divergence — the reasoning-display path I suspected honours the same mode on both sides — so I left it alone rather than change it speculatively. Flagging it as a latent coupling.
I could not open a PR — the repo limits that to collaborators — so the branch above is the reference. Happy for you to take it as a starting point and write your own version, per CONTRIBUTING.
Keeping issue active and preventing stale tag
I triggered something weird I believe.
Hey @1jehuang — following up on my fix above (branch: tirthfx:fix/688-rewind-target-block-boundary, commit 5e35eb4).
I can't open a PR against this repo — pull_request_creation_policy is set to collaborators_only, so it's blocked even from my fork. Could you either:
- add me as a collaborator so I can open the PR myself, or
- open the PR yourself off my branch (tirthfx:fix/688-rewind-target-block-boundary → master) and I'll happily address review comments
Either works for me — just flagging so this doesn't stall waiting on something I can't do from my side.
Idle mark blocker