Skip to content

The agent that grows with you

fix(desktop,gateway): never aim Desktop /undo, /retry or /rollback at an archived row

David Dudok de Wit3b332359da6e
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-commands.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-commands.ts
index bb3702993a4..bf6509372a1 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-commands.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-commands.ts
@@ -22,6 +22,7 @@ const TEXT_PARTS = new Set(['input_text', 'output_text', 'text'])
const PREVIEW_CHARS = 50
interface SnapshotRow {
+ archived?: unknown
content?: unknown
display_kind?: unknown
role?: unknown
@@ -70,6 +71,8 @@ function isUserTurn(row: SnapshotRow): boolean {
row.role === 'user' &&
(!row.display_kind || row.display_kind === 'steer') &&
Number.isSafeInteger(row.row_id) &&
+ // A live row only: a row a compression archived is history the snapshot shows, never a turn to rewind to.
+ !row.archived &&
!(typeof row.content === 'string' && COMPACTION_MARKERS.some(marker => String(row.content).includes(marker)))
)
}
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-user-turns.test.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-user-turns.test.ts
new file mode 100644
index 00000000000..0309a0c50a9
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-user-turns.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from 'vitest'
+
+import { userTurnFromEnd } from './slash-session-commands'
+
+// After a compression the owner's resume snapshot also carries the turns it summarized away, marked
+// `archived`: history to show, never an /undo or /retry target (the owner refuses a rewind to one).
+const rows = [
+ { archived: true, content: 'old question', role: 'user', row_id: 1 },
+ { archived: true, content: 'old answer', role: 'assistant', row_id: 2 },
+ { content: '[CONTEXT COMPACTION] earlier turns', role: 'user', row_id: 3 },
+ { content: 'live question', role: 'user', row_id: 4 },
+ { content: 'live answer', role: 'assistant', row_id: 5 }
+]
+
+describe('userTurnFromEnd', () => {
+ it('chooses only among live user turns', () => {
+ expect(userTurnFromEnd(rows, 1)?.row_id).toBe(4)
+ expect(userTurnFromEnd(rows, 2)).toBeNull()
+ })
+
+ it('finds nothing to rewind once the live turns are gone', () => {
+ expect(userTurnFromEnd(rows.slice(0, 3), 1)).toBeNull()
+ })
+})
diff --git a/gateway/session_tui_ops.py b/gateway/session_tui_ops.py
index d85afda07b3..ede599354ed 100644
--- a/gateway/session_tui_ops.py
+++ b/gateway/session_tui_ops.py
@@ -249,8 +249,8 @@ async def _rewind_last_turn(connection, ref):
from agent.context_compressor import user_originated_turn_view
from gateway.session_local_recovery import local_history
rows = await asyncio.to_thread(local_history, authority, ref)
- target = next((row['row_id'] for row in reversed(rows)
- if isinstance(row.get('row_id'), int) and user_originated_turn_view(row) is not None), None)
+ target = next((row['row_id'] for row in reversed(rows) if isinstance(row.get('row_id'), int)
+ and not row.get('archived') and user_originated_turn_view(row) is not None), None)
if target is None:
return 0
from gateway.session_mutations import mutate_session
diff --git a/tests/gateway/test_resume_display_history.py b/tests/gateway/test_resume_display_history.py
index a813cf1d59c..60d499425d6 100644
--- a/tests/gateway/test_resume_display_history.py
+++ b/tests/gateway/test_resume_display_history.py
@@ -225,3 +225,24 @@ async def test_a_delete_during_the_resume_prepare_answers_not_found(tmp_path, mo
reply = await connection().dispatch({'id': 1, 'method': 'session.resume', 'params': {'session_id': ref.session_id}})
assert 'result' in deleted[0], deleted # the delete really committed inside the window
assert reply.get('error', {}).get('message') == 'not_found', reply
+
+
[email protected]('mode', ['in_place', 'rotated'])
+async def test_rollback_turn_rewind_never_targets_an_archived_turn(tmp_path, monkeypatch, mode):
+ """/rollback's history rewind picks the newest live user turn; with none left (the last one undone after
+ a compression) it rewinds nothing instead of aiming the owner's rewind at an archived row it refuses."""
+ from gateway.session_mutations import mutate_session
+ from gateway.session_tui_ops import _rewind_last_turn
+ authority, actor, ref, physical = await _local_session(tmp_path, monkeypatch)
+ db = authority.db
+ _append(db, physical, 'OLD_Q', 'OLD_A', 'TAIL_Q', 'TAIL_A')
+ tip = _compact(db, physical, mode)
+ handle = authority._handle(ref)
+ await mutate_session(authority, actor, ref, {
+ 'session_id': ref.session_id, 'request_id': 'undo', 'operation': 'rewind',
+ 'expected_revision': handle.revision, 'expected_generation': handle.execution_generation,
+ 'payload': {'target_message_id': next(m['id'] for m in db.get_messages(tip) if m['content'] == 'TAIL_Q')}})
+ live = [m['content'] for m in db.get_messages(tip)]
+ assert await _rewind_last_turn(SimpleNamespace(authority=authority, actor=actor), ref) == 0
+ assert [m['content'] for m in db.get_messages(tip)] == live == [SUMMARY]