Skip to content

The agent that grows with you

fix(desktop): /rollback reloads the transcript after a restore that rewound history

Teknium9620bffbf385
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.test.tsx
index 342e9a21978..0f40345be9f 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.test.tsx
@@ -1,6 +1,7 @@
import { cleanup } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { toChatMessages } from '@/lib/chat-messages'
import { resolveDesktopCommand } from '@/lib/desktop-slash-commands'
import { $connection } from '@/store/session'
import { dropSessionState } from '@/store/session-states'
@@ -31,10 +32,15 @@ function gateway(respond: Responder) {
return vi.fn(async (method: string, params?: Record<string, unknown>) => (respond(method, params) ?? {}) as never)
}
-async function mount(requestGateway: ReturnType<typeof gateway>) {
+async function mount(requestGateway: ReturnType<typeof gateway>, seedMessages?: unknown[]) {
let handle: HarnessHandle | null = null
await actRender(
- <Harness onReady={h => (handle = h)} refreshSessions={async () => undefined} requestGateway={requestGateway} />
+ <Harness
+ onReady={h => (handle = h)}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ seedMessages={seedMessages}
+ />
)
return handle!
@@ -147,6 +153,41 @@ describe('/rollback', () => {
expect(text).toContain('Invalid checkpoint number')
expect(text).toContain('+new')
})
+
+ it('reloads the transcript after a restore that rewound history, but not after a file-only restore', async () => {
+ const rows = [
+ { role: 'user', content: 'KEPT QUESTION', row_id: 1 },
+ { role: 'assistant', content: 'KEPT ANSWER', row_id: 2 },
+ { role: 'user', content: 'REMOVED QUESTION', row_id: 3 },
+ { role: 'assistant', content: 'REMOVED ANSWER', row_id: 4 }
+ ]
+
+ const requestGateway = gateway((method, params) => {
+ if (method === 'rollback.restore') {
+ return params?.file_path
+ ? { success: true, restored_to: 'bbbb2222' }
+ : { success: true, restored_to: 'aaaa1111', history_removed: 2 }
+ }
+
+ if (method === 'session.resume') {
+ return { messages: rows.slice(0, 2) }
+ }
+ })
+
+ const handle = await mount(requestGateway, toChatMessages(rows as never))
+
+ await handle.submitText('/rollback 1 src/app.py')
+ expect(callsOf(requestGateway, 'session.resume')).toEqual([])
+ expect(renderedText(handle)).toContain('REMOVED QUESTION')
+
+ await handle.submitText('/rollback 1')
+ expect(callsOf(requestGateway, 'session.resume')).toEqual([{ session_id: RUNTIME_SESSION_ID }])
+ const text = renderedText(handle)
+ expect(text).toContain('KEPT ANSWER')
+ expect(text).not.toContain('REMOVED QUESTION')
+ expect(text).not.toContain('REMOVED ANSWER')
+ expect(text).toContain('Rewound 2 message(s)')
+ })
})
describe('/agents', () => {
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.ts
index ab520b2283d..72781d26d72 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-desktop-commands.ts
@@ -1,8 +1,12 @@
+import { toChatMessages } from '@/lib/chat-messages'
import { isMissingRpcMethod } from '@/lib/gateway-rpc'
import { openCommandPalette } from '@/store/command-palette'
import { $displayTimestamps } from '@/store/display-timestamps'
import { runExportProfileFlow, runImportProfileFlow } from '@/store/profile-share'
import { $connection } from '@/store/session'
+import type { SessionMessage } from '@/types/hermes'
+
+import type { ClientSessionState } from '../../../types'
import type { GatewayRequest } from './utils'
@@ -16,10 +20,16 @@ import type { GatewayRequest } from './utils'
interface SlashOutput {
render: (text: string) => void
sessionId: string
+ storedSessionId?: null | string
}
export interface DesktopReadCommandEnv<C extends { arg: string }> {
requestGateway: GatewayRequest
+ updateSessionState: (
+ sessionId: string,
+ updater: (state: ClientSessionState) => ClientSessionState,
+ storedSessionId?: null | string
+ ) => ClientSessionState
withSlashOutput: (ctx: C) => Promise<null | SlashOutput>
}
@@ -251,7 +261,7 @@ export function formatHistory(rows: HistoryRow[]): string {
}
export function desktopReadCommandHandlers<C extends { arg: string }>(env: DesktopReadCommandEnv<C>) {
- const { requestGateway } = env
+ const { requestGateway, updateSessionState } = env
return {
rollback: async (ctx: C) => {
@@ -280,7 +290,27 @@ export function desktopReadCommandHandlers<C extends { arg: string }>(env: Deskt
return out.render([diff?.stat, diff?.diff].filter(Boolean).join('\n') || 'No changes since that checkpoint.')
}
- out.render(formatRestore(result as RestoreResult | null))
+ const restore = result as RestoreResult | null
+ let text = formatRestore(restore)
+
+ // A whole-tree restore also rewound the conversation on the owner: reload the target
+ // session's transcript so the removed turns leave the screen. File-only restores keep it.
+ if (restore?.success && restore.history_removed && !request.params.file_path) {
+ try {
+ const snapshot = await requestGateway<{ messages?: SessionMessage[] }>('session.resume', {
+ session_id: out.sessionId
+ })
+
+ if (Array.isArray(snapshot?.messages)) {
+ const messages = toChatMessages(snapshot.messages)
+ updateSessionState(out.sessionId, state => ({ ...state, messages }), out.storedSessionId)
+ }
+ } catch {
+ text += '\nReopen this chat to see the rewound conversation.'
+ }
+ }
+
+ out.render(text)
} catch (err) {
out.render(errorText(err))
}
@@ -450,7 +480,7 @@ export function desktopReadCommandHandlers<C extends { arg: string }>(env: Deskt
}
try {
- const snapshot = await requestGateway<{ messages?: HistoryRow[] }>('session.resume', {
+ const snapshot = await requestGateway<{ messages?: SessionMessage[] }>('session.resume', {
session_id: out.sessionId
})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
index 9ca3b84513b..17707c676ef 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
@@ -643,7 +643,7 @@ export function useSlashCommand(deps: SlashCommandDeps) {
updateSessionState,
withSlashOutput
}),
- ...desktopReadCommandHandlers<SlashActionCtx>({ requestGateway, withSlashOutput }),
+ ...desktopReadCommandHandlers<SlashActionCtx>({ requestGateway, updateSessionState, withSlashOutput }),
new: async () => {
prepareDefaultNewSession()
startFreshSessionDraft()