Skip to content

fix(tui): fetch messages from server in undo/redo to fix trimmed cache bug#28660

Open
lexlian wants to merge 4 commits into
anomalyco:devfrom
lexlian:fix/28257-undo-trimmed-messages
Open

fix(tui): fetch messages from server in undo/redo to fix trimmed cache bug#28660
lexlian wants to merge 4 commits into
anomalyco:devfrom
lexlian:fix/28257-undo-trimmed-messages

Conversation

@lexlian
Copy link
Copy Markdown

@lexlian lexlian commented May 21, 2026

Issue for this PR

Closes #28257

Type of change

  • Bug fix

What does this PR do?

Root cause: The TUI's undo/redo commands searched sync.data.message[sessionID], an in-memory message cache capped at 100 entries per session. When an agent loop generates enough assistant/tool messages to push the total past 100, the oldest user messages are shifted out. /undo then silently fails because it can't find earlier user messages.

Why TUI fails but Web UI works: The web UI doesn't maintain a capped local message cache. Each request to the server fetches complete history from the database, so revert always finds the correct user message. The TUI sync context caps messages at 100 (sync.tsx:272) for performance, but the undo handler depended on that trimmed cache instead of fetching from the server.

Fix: Both undo and redo handlers now call sdk.client.session.messages() to fetch complete message history from the server, then find the target user message. The server always has the full message list from the database. For prompt restoration, a fallback (sync.data.part[id] ?? message.parts) handles trimmed entries.

Files changed:

  • packages/opencode/src/cli/cmd/tui/routes/session/index.tsx — undo handler (line 598) and redo handler (line 637) fetch from server instead of local cache
  • packages/opencode/test/session/revert-trimmed-cache.test.ts — new test verifying server-side revert works with 100+ messages

How did you verify your code works?

  • Added test test/session/revert-trimmed-cache.test.ts: creates 100+ messages with 2 user messages, verifies sequential revert/unrevert works correctly
  • Verified no new TypeScript errors in modified file
  • Followed existing test patterns from test/session/revert-compact.test.ts

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

lexlian added 4 commits May 21, 2026 21:27
…e bug

The /undo and /redo commands searched the TUI's in-memory message cache
which is capped at 100 messages per session. When an agent loop generates
enough assistant/tool messages to push the total past 100, the oldest user
messages are shift()ed out, making undo unavailable for earlier messages.

Fix both undo and redo handlers to fetch messages directly from the server
API (sdk.client.session.messages) instead of relying on the capped local
store. The server always has the complete message history from the database.

Also add a fallback for prompt restoration: try local cache first, then
fall back to server-returned parts when the local cache entry is missing.

Fixes anomalyco#28257
getScrollAcceleration expects Pick<TuiConfig.Info, 'scroll_acceleration' | 'scroll_speed'>
but was passed the full TuiConfig.Resolved type. Pick the two required fields explicitly.
Verifies server-side revert works correctly when session has more than 100
messages, which is the underlying behavior the TUI fix depends on.

The TUI's local message cache caps at 100 entries per session (sync.tsx:272),
so the undo/redo fix fetches from the server which always has the complete
message history.
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.

/Undo command does not work if last user message gets out of visible session history

1 participant