Skip to content

The agent that grows with you

fix(desktop): edit asks before a deep cut; a server 4033 asks once and retries

kshitijk4poorc72f4fa1ddee
diff --git a/apps/desktop/src/app/chat/session-tile-actions.ts b/apps/desktop/src/app/chat/session-tile-actions.ts
index 19bafc82ee5..4c654f7d639 100644
--- a/apps/desktop/src/app/chat/session-tile-actions.ts
+++ b/apps/desktop/src/app/chat/session-tile-actions.ts
@@ -55,15 +55,17 @@ import {
deepReloadConfirmRequest,
durableRowIdsForRebind,
finalizeStoppedMessages,
+ planConfirmedEdit,
planConfirmedReload,
- planEdit,
planRestore,
rebindSurvivorRowIds,
runRewindSubmit,
+ submitWithDeepTruncateConfirm,
type SurvivorUserRowIds
} from '../session/hooks/use-prompt-actions/rewind'
import { useSubmitPrompt } from '../session/hooks/use-prompt-actions/submit'
import {
+ isDeepTruncateRefusal,
markSessionRecentlyInterrupted,
shouldInterruptBeforeRewind,
type SubmitTextOptions,
@@ -573,15 +575,20 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
// runRewindSubmit (withSessionNotFoundResume + runtime rebind), so the
// PR-era inline prompt.submit wrapper is superseded on current main.
applySurvivorRowIds(
- await submitRewind(
- plan.text,
- plan.truncateOrdinal,
- false,
- plan.truncateMessageId,
- plan.truncateRowId,
- plan.sourceText,
- durableRowIdsForRebind(messages),
- plan.confirmDeepTruncate
+ await submitWithDeepTruncateConfirm(
+ confirmed =>
+ submitRewind(
+ plan.text,
+ plan.truncateOrdinal,
+ false,
+ plan.truncateMessageId,
+ plan.truncateRowId,
+ plan.sourceText,
+ durableRowIdsForRebind(messages),
+ confirmed
+ ),
+ plan.confirmDeepTruncate,
+ () => confirm(deepReloadConfirmRequest(t.assistant.thread))
)
)
} catch (err) {
@@ -595,7 +602,11 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
turnStartedAt: null,
messages
}))
- notifyError(err, copy.regenerateFailed)
+
+ // A declined deep-cut confirm is the user's answer, not a failure.
+ if (!isDeepTruncateRefusal(err)) {
+ notifyError(err, copy.regenerateFailed)
+ }
}
},
[applySurvivorRowIds, copy.regenerateFailed, readState, submitRewind, t, update]
@@ -693,7 +704,8 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
const editMessage = useCallback(
async (edited: AppendMessage) => {
const messages = readMessages()
- const plan = planEdit(messages, edited)
+ const confirmDeep = () => confirm(deepReloadConfirmRequest(t.assistant.thread))
+ const plan = await planConfirmedEdit(messages, edited, confirmDeep)
if (!plan) {
return
@@ -714,15 +726,20 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
try {
applySurvivorRowIds(
- await submitRewind(
- plan.text,
- plan.truncateOrdinal,
- interruptFirst,
- plan.truncateMessageId,
- plan.truncateRowId,
- plan.sourceText,
- durableRowIdsForRebind(messages),
- true
+ await submitWithDeepTruncateConfirm(
+ confirmed =>
+ submitRewind(
+ plan.text,
+ plan.truncateOrdinal,
+ interruptFirst,
+ plan.truncateMessageId,
+ plan.truncateRowId,
+ plan.sourceText,
+ durableRowIdsForRebind(messages),
+ confirmed
+ ),
+ plan.confirmDeepTruncate,
+ confirmDeep
)
)
} catch (err) {
@@ -734,10 +751,13 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
turnStartedAt: null,
messages
}))
- notifyError(err, copy.editFailed)
+
+ if (!isDeepTruncateRefusal(err)) {
+ notifyError(err, copy.editFailed)
+ }
}
},
- [applySurvivorRowIds, copy.editFailed, readMessages, readState, submitRewind, update]
+ [applySurvivorRowIds, copy.editFailed, readMessages, readState, submitRewind, t, update]
)
// Branch-visibility sync (assistant-ui hides non-active branches).
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index 7220000e6b6..33af24dc2e1 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -6024,9 +6024,10 @@ describe('usePromptActions stale-closure session routing', () => {
expect(updated).toContain(RUNTIME_SESSION_B)
expect(updated).not.toContain(RUNTIME_SESSION_ID)
- expect(gatewayCalls(requestGateway).find(([m]) => m === 'prompt.submit')?.[1]).toMatchObject({
- confirm_deep_truncate: true
- })
+ // A tail edit archives no later user turn, so it asks nothing and sends no deep confirm.
+ expect(gatewayCalls(requestGateway).find(([m]) => m === 'prompt.submit')?.[1]).not.toHaveProperty(
+ 'confirm_deep_truncate'
+ )
for (const [, params] of gatewayCalls(requestGateway)) {
if (params && 'session_id' in params) {
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index 63b1e669636..a09dec2062b 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -64,11 +64,13 @@ import {
deepReloadConfirmRequest,
durableRowIdsForRebind,
finalizeStoppedMessages,
+ planConfirmedEdit,
planConfirmedReload,
planEdit,
planRestore,
rebindSurvivorRowIds,
runRewindSubmit,
+ submitWithDeepTruncateConfirm,
type SurvivorUserRowIds
} from './rewind'
import { useSlashCommand } from './slash'
@@ -80,6 +82,7 @@ import {
friendlyRemoteAttachError,
type GatewayRequest,
inlineErrorMessage,
+ isDeepTruncateRefusal,
markSessionRecentlyInterrupted,
readFileDataUrlForAttach,
readImageForRemoteAttach,
@@ -1020,16 +1023,21 @@ export function usePromptActions({
updateSessionState(sessionId, state => applyReloadOptimistic(state, plan))
try {
- const survivorRowIds = await submitRewindPrompt(
- sessionId,
- plan.text,
- plan.truncateOrdinal,
- plan.truncateMessageId,
- false,
- plan.truncateRowId,
- plan.sourceText,
- durableRowIdsForRebind(messages),
- plan.confirmDeepTruncate
+ const survivorRowIds = await submitWithDeepTruncateConfirm(
+ confirmed =>
+ submitRewindPrompt(
+ sessionId,
+ plan.text,
+ plan.truncateOrdinal,
+ plan.truncateMessageId,
+ false,
+ plan.truncateRowId,
+ plan.sourceText,
+ durableRowIdsForRebind(messages),
+ confirmed
+ ),
+ plan.confirmDeepTruncate,
+ () => confirm(deepReloadConfirmRequest(t.assistant.thread))
)
applySurvivorRowIds(sessionId, survivorRowIds)
@@ -1045,7 +1053,11 @@ export function usePromptActions({
turnStartedAt: null,
messages
}))
- notifyError(err, copy.regenerateFailed)
+
+ // A declined deep-cut confirm is the user's answer, not a failure.
+ if (!isDeepTruncateRefusal(err)) {
+ notifyError(err, copy.regenerateFailed)
+ }
}
},
[activeSessionIdRef, applySurvivorRowIds, copy.regenerateFailed, submitRewindPrompt, t, updateSessionState]
@@ -1192,7 +1204,8 @@ export function usePromptActions({
// Same dual-store read as reloadFromMessage (#68734).
const messages = (sessionId ? $sessionStates.get()[sessionId]?.messages : null) ?? $messages.get()
- const plan = sessionId ? planEdit(messages, edited) : null
+ const confirmDeep = () => confirm(deepReloadConfirmRequest(t.assistant.thread))
+ const plan = sessionId ? await planConfirmedEdit(messages, edited, confirmDeep) : null
if (!sessionId || !plan) {
return
@@ -1235,16 +1248,21 @@ export function usePromptActions({
}
try {
- const survivorRowIds = await submitRewindPrompt(
- sessionId,
- plan.text,
- plan.truncateOrdinal,
- plan.truncateMessageId,
- interruptFirst,
- plan.truncateRowId,
- plan.sourceText,
- durableRowIdsForRebind(messages),
- true
+ const survivorRowIds = await submitWithDeepTruncateConfirm(
+ confirmed =>
+ submitRewindPrompt(
+ sessionId,
+ plan.text,
+ plan.truncateOrdinal,
+ plan.truncateMessageId,
+ interruptFirst,
+ plan.truncateRowId,
+ plan.sourceText,
+ durableRowIdsForRebind(messages),
+ confirmed
+ ),
+ plan.confirmDeepTruncate,
+ confirmDeep
)
applySurvivorRowIds(sessionId, survivorRowIds)
@@ -1268,18 +1286,25 @@ export function usePromptActions({
const refreshed = $messages.get()
const retryPlan = planEdit(refreshed, edited)
+ // planEdit, not planConfirmedEdit: the user already answered for this edit; the retry only
+ // re-addresses the same turn after a history refresh.
if (retryPlan && !retryPlan.isFailedTurn) {
- const survivorRowIds = await submitRewindPrompt(
- sessionId,
- retryPlan.text,
- retryPlan.truncateOrdinal,
- retryPlan.truncateMessageId,
- false,
- retryPlan.truncateRowId,
- retryPlan.sourceText,
- durableRowIdsForRebind(refreshed),
- true
+ const survivorRowIds = await submitWithDeepTruncateConfirm(
+ confirmed =>
+ submitRewindPrompt(
+ sessionId,
+ retryPlan.text,
+ retryPlan.truncateOrdinal,
+ retryPlan.truncateMessageId,
+ false,
+ retryPlan.truncateRowId,
+ retryPlan.sourceText,
+ durableRowIdsForRebind(refreshed),
+ confirmed
+ ),
+ plan.confirmDeepTruncate,
+ confirmDeep
)
applySurvivorRowIds(sessionId, survivorRowIds)
@@ -1308,7 +1333,11 @@ export function usePromptActions({
turnStartedAt: null,
messages
}))
- notifyError(surfaced, unavailable ? copy.editTurnUnavailable : copy.editFailed)
+
+ // A declined deep-cut confirm is the user's answer, not a failure.
+ if (!isDeepTruncateRefusal(surfaced)) {
+ notifyError(surfaced, unavailable ? copy.editTurnUnavailable : copy.editFailed)
+ }
}
},
[
@@ -1320,6 +1349,7 @@ export function usePromptActions({
resumeStoredSession,
selectedStoredSessionIdRef,
submitRewindPrompt,
+ t,
updateSessionState
]
)
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.test.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.test.ts
index ab4b2f1d34a..060c91ecade 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.test.ts
@@ -1,3 +1,4 @@
+import { JsonRpcGatewayError } from '@hermes/shared'
import { describe, expect, it } from 'vitest'
import { type ChatMessage, finalizeInterruptedMessages, textPart } from '@/lib/chat-messages'
@@ -9,6 +10,7 @@ import {
applyRewindOptimistic,
finalizeStoppedMessages,
finalizeUserInterruptedMessages,
+ planConfirmedEdit,
planConfirmedReload,
planEdit,
planReload,
@@ -16,6 +18,7 @@ import {
rebindSurvivorRowIds,
resolveDurableRowId,
runRewindSubmit,
+ submitWithDeepTruncateConfirm,
survivorRowIdsFrom,
truncateSubmitParams
} from './rewind'
@@ -775,3 +778,65 @@ describe('planConfirmedReload (#133716)', () => {
expect(asked).toBe(2)
})
})
+
+describe('edit and server-refusal confirms (#133716 review)', () => {
+ const transcript = [
+ row('u1', 'user', 'old prompt', { rowId: 11 }),
+ row('a1', 'assistant', 'old reply'),
+ row('u2', 'user', 'latest prompt', { rowId: 13 }),
+ row('a2', 'assistant', 'latest reply')
+ ]
+
+ const editOf = (sourceId: string) =>
+ ({ role: 'user', sourceId, parentId: null, content: [{ type: 'text', text: 'edited' }] }) as never
+
+ it('asks before an edit that archives later user turns; a tail edit sends no deep confirm', async () => {
+ let asked = 0
+
+ const decline = async () => {
+ asked += 1
+
+ return false
+ }
+
+ expect(await planConfirmedEdit(transcript, editOf('u1'), decline)).toBeNull()
+ expect(asked).toBe(1)
+ expect(await planConfirmedEdit(transcript, editOf('u1'), async () => true)).toMatchObject({
+ confirmDeepTruncate: true,
+ truncateRowId: 11
+ })
+
+ expect(await planConfirmedEdit(transcript, editOf('u2'), decline)).toMatchObject({ confirmDeepTruncate: false })
+ expect(asked).toBe(1)
+ })
+
+ it('a 4033 the client did not predict asks once and retries confirmed; other errors pass through', async () => {
+ const refusal = new JsonRpcGatewayError('truncation would archive later user turns', { code: 4033 })
+ const sent: boolean[] = []
+
+ const submit = async (confirmed: boolean) => {
+ sent.push(confirmed)
+
+ if (!confirmed) {
+ throw refusal
+ }
+
+ return 'ok'
+ }
+
+ expect(await submitWithDeepTruncateConfirm(submit, false, async () => true)).toBe('ok')
+ expect(sent).toEqual([false, true])
+
+ await expect(submitWithDeepTruncateConfirm(submit, false, async () => false)).rejects.toBe(refusal)
+ expect(sent).toEqual([false, true, false])
+
+ const busy = new Error('session busy')
+ await expect(
+ submitWithDeepTruncateConfirm(
+ async () => Promise.reject(busy),
+ false,
+ async () => true
+ )
+ ).rejects.toBe(busy)
+ })
+})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.ts
index 7a9fd08cdbd..478ecc2d7e6 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.ts
@@ -25,6 +25,7 @@ import type { ConfirmRequest } from '@/store/confirm'
import {
appendText,
+ isDeepTruncateRefusal,
isFailedUserTurn,
isSessionBusyError,
isVisibleUserMessage,
@@ -580,6 +581,28 @@ export async function planConfirmedReload(
return (await confirmDeep()) ? { ...plan, confirmDeepTruncate: true } : null
}
+/**
+ * The server counts later user turns with its own predicate (steer rows, compaction carriers) and
+ * refuses an unconfirmed deep cut with 4033. Client and server can disagree, so a refusal asks the
+ * same destructive confirm once and retries confirmed. A decline rethrows the 4033, which callers
+ * roll back silently (`isDeepTruncateRefusal`).
+ */
+export async function submitWithDeepTruncateConfirm<T>(
+ submit: (confirmDeepTruncate: boolean) => Promise<T>,
+ confirmed: boolean,
+ confirmDeep: () => Promise<boolean>
+): Promise<T> {
+ try {
+ return await submit(confirmed)
+ } catch (err) {
+ if (confirmed || !isDeepTruncateRefusal(err) || !(await confirmDeep())) {
+ throw err
+ }
+
+ return submit(true)
+ }
+}
+
/** Optimistic reload state: keep the user turn, hide the branch's assistants. */
export function applyReloadOptimistic(state: ClientSessionState, plan: ReloadPlan): ClientSessionState {
const nextUserIndex = state.messages.findIndex((m, i) => i > plan.userIndex && m.role === 'user')
@@ -716,6 +739,29 @@ export function planEdit(messages: ChatMessage[], edited: AppendMessage): EditPl
}
}
+/**
+ * Edit is a rewind too: an edit of an older turn archives every later turn, which used to bypass the
+ * server gate with an unconditional confirm flag (#133716 review). Ask first, exactly as regenerate
+ * does; a tail edit (nothing after the source) and a failed turn need no confirm. Null when declined.
+ */
+export async function planConfirmedEdit(
+ messages: ChatMessage[],
+ edited: AppendMessage,
+ confirmDeep: () => Promise<boolean>
+): Promise<null | (EditPlan & { confirmDeepTruncate: boolean })> {
+ const plan = planEdit(messages, edited)
+
+ if (!plan) {
+ return null
+ }
+
+ if (plan.truncateOrdinal === undefined || !messages.some((m, i) => i > plan.sourceIndex && isVisibleUserMessage(m))) {
+ return { ...plan, confirmDeepTruncate: false }
+ }
+
+ return (await confirmDeep()) ? { ...plan, confirmDeepTruncate: true } : null
+}
+
/** Optimistic rewind-to state for restore/edit: drop everything after the
* source turn (edit swaps in the edited message; restore keeps the original). */
export function applyRewindOptimistic(
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
index ffff70f40f8..8519ee7723a 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
@@ -279,6 +279,11 @@ export function isGatewayTimeoutError(error: unknown): boolean {
export const SESSION_BUSY_RETRY_TIMEOUT_MS = 6_000
export const SESSION_BUSY_RETRY_INTERVAL_MS = 150
+/** prompt.submit refused an unconfirmed truncation that would archive later user turns (#133716). */
+export function isDeepTruncateRefusal(error: unknown): boolean {
+ return error instanceof JsonRpcGatewayError && error.code === 4033
+}
+
export function isSessionBusyError(error: unknown): boolean {
return /session busy/i.test(error instanceof Error ? error.message : String(error))
}