The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): Ink /retry validates the target turn before rewinding it
Teknium2bb7bccce0fe
diff --git a/locales/_keys.tui.json b/locales/_keys.tui.jsonindex 2f94cd77b87..d11c07e3c4a 100644--- a/locales/_keys.tui.json+++ b/locales/_keys.tui.json@@ -93,6 +93,7 @@"canonical.controls.modelUsage","canonical.controls.notAvailable","canonical.controls.prefNotSaved",+ "canonical.controls.retryUnsupported","canonical.controls.rowCost","canonical.controls.unsupportedModelOption","canonical.controls.usageLastTurnTitle",diff --git a/ui-tui/src/__tests__/canonicalRetryValidate.test.ts b/ui-tui/src/__tests__/canonicalRetryValidate.test.tsnew file mode 100644index 00000000000..04f4faaa61f--- /dev/null+++ b/ui-tui/src/__tests__/canonicalRetryValidate.test.ts@@ -0,0 +1,94 @@+import { beforeEach, expect, it, vi } from 'vitest'++import { createSlashHandler } from '../app/createSlashHandler.js'+import { patchUiState, resetUiState } from '../app/uiStore.js'++// dokterdok N92 (Ink half): /retry validates the target turn BEFORE rewinding it, as the classic chat+// and main's retry_last do (agent/context_compressor_retry.py::retryable_user_text): an image turn is+// refused with nothing archived; a text-parts turn is resubmitted as its own text, never as whatever+// this view typed last.++const flush = () => new Promise(resolve => setImmediate(resolve))++function harness(content: unknown) {+ const messages = [+ { role: 'user', content: 'first', row_id: 11 },+ { role: 'assistant', content: 'a1', row_id: 12 },+ { role: 'user', content, row_id: 13 },+ { role: 'assistant', content: 'a2', row_id: 14 }+ ]++ const request = vi.fn(async (method: string, params: any) => {+ if (method === 'session.resume') {+ return { session_id: 'owner', revision: 7, execution_generation: 3, messages }+ }++ if (method === 'session.mutate' && params.operation === 'rewind') {+ return {+ session_id: 'owner',+ operation: 'rewind',+ revision: 8,+ execution_generation: 4,+ rewound_count: 2,+ target_message: { role: 'user', content }+ }+ }++ throw new Error(`unknown_method: ${method}`)+ })++ const sys = vi.fn()+ const send = vi.fn()++ const ctx = {+ slashFlightRef: { current: 0 },+ gateway: { gw: { isCanonical: true, request }, rpc: request },+ transcript: {+ page: vi.fn(),+ panel: vi.fn(),+ send,+ sys,+ setHistoryItems: vi.fn(),+ trimLastExchange: (i: unknown[]) => i+ },+ local: { getHistoryItems: () => messages, getLastUserMsg: () => 'typed elsewhere', maybeWarn: vi.fn() },+ session: { resumeById: vi.fn() },+ composer: { enqueue: vi.fn() }+ }++ return { request, send, slash: createSlashHandler(ctx as any), sys }+}++beforeEach(() => {+ resetUiState()+ patchUiState({+ sid: 'owner',+ info: { model: 'm', skills: {}, tools: {}, execution_epoch: '1', execution_generation: 3 } as any+ })+})++it('/retry refuses an image turn before any rewind and resubmits a text-parts turn as its text', async () => {+ const image = harness([+ { type: 'text', text: 'what is this?' },+ { type: 'image_url', image_url: { url: 'data:x' } }+ ])++ image.slash('/retry')+ await flush()+ expect(image.request.mock.calls.some(([method]) => method === 'session.mutate')).toBe(false)+ expect(image.send).not.toHaveBeenCalled()+ expect(image.sys).toHaveBeenCalledWith(expect.stringContaining('retry does not support media'))++ const parts = harness([+ { type: 'text', text: 'what is ' },+ { type: 'input_text', text: 'this?' }+ ])++ parts.slash('/retry')+ await flush()+ expect(parts.request).toHaveBeenCalledWith(+ 'session.mutate',+ expect.objectContaining({ payload: { target_message_id: 13 } })+ )+ expect(parts.send).toHaveBeenCalledWith('what is this?')+})diff --git a/ui-tui/src/app/slash/canonicalSessionCommands.ts b/ui-tui/src/app/slash/canonicalSessionCommands.tsindex 308e94c5e66..3d3038c7d48 100644--- a/ui-tui/src/app/slash/canonicalSessionCommands.ts+++ b/ui-tui/src/app/slash/canonicalSessionCommands.ts@@ -29,6 +29,43 @@ export function lastTurnRewind(snapshot: MutationSnapshot): null | { target_messreturn target ? { target_message_id: target.row_id as number } : null}+const TEXT_PARTS = new Set(['input_text', 'output_text', 'text'])++/** Lossless /retry text (agent/context_compressor_retry.py::retryable_user_text): '' when the turn has+ * no text, null when it carries media, unknown or annotated parts (no replay protocol: fail closed). */+function retryText(content: unknown): null | string {+ let text = ''++ for (const part of typeof content === 'string' ? [content] : Array.isArray(content) ? content : [null]) {+ if (typeof part === 'string') {+ text += part+ } else if (+ part?.type &&+ TEXT_PARTS.has(part.type) &&+ typeof part.text === 'string' &&+ Object.keys(part).length === 2+ ) {+ text += part.text+ } else {+ return null+ }+ }++ return text.trim() ? text : ''+}++/** /retry's rewind payload: validated on the CAS snapshot, so a refusal archives nothing. */+export function retryTurnRewind(snapshot: MutationSnapshot): null | { target_message_id: number } {+ const target = (snapshot.messages ?? []).findLast(isUserTurn)+ const text = target ? retryText(target.content) : ''++ if (text === null) {+ throw new Error(t('canonical.controls.retryUnsupported'))+ }++ return target && text ? { target_message_id: target.row_id as number } : null+}+// sid → the last admission whose completion this view saw; `/usage` reads its committed result.const lastAdmission = new Map<string, string>()@@ -115,7 +152,7 @@ export function canonicalReasoning(payload: { scope?: string; value: string }, cexport function canonicalRewind(intent: 'retry' | 'undo', ctx: SlashRunCtx) {const sid = ctx.sid!- void mutateCanonical(ctx.gateway.gw, sid, 'rewind', lastTurnRewind, ctx.stale)+ void mutateCanonical(ctx.gateway.gw, sid, 'rewind', intent === 'retry' ? retryTurnRewind : lastTurnRewind, ctx.stale).then(mutation => {if (!mutation || ctx.stale()) {return@@ -137,8 +174,7 @@ export function canonicalRewind(intent: 'retry' | 'undo', ctx: SlashRunCtx) {}// Main's /retry: the rewound user turn's own text, not whatever this view typed last.- const content = (result.target_message as undefined | { content?: unknown })?.content- const text = typeof content === 'string' && content.trim() ? content : ctx.local.getLastUserMsg()+ const text = retryText((result.target_message as undefined | { content?: unknown })?.content)return text ? ctx.transcript.send(text) : ctx.transcript.sys(t('slashCmd.core.retry.nothing'))})diff --git a/ui-tui/src/i18n/en/canonical.ts b/ui-tui/src/i18n/en/canonical.tsindex a8985a9c3df..953dde9a382 100644--- a/ui-tui/src/i18n/en/canonical.ts+++ b/ui-tui/src/i18n/en/canonical.ts@@ -18,6 +18,7 @@ export const canonicalEn = {usageLastTurnTitle: 'Usage · last turn',usageNoTurn: 'no completed turn in this view yet',usageTotalsUnavailable: 'session totals are not available on the shared gateway yet',+ retryUnsupported: '/retry: retry does not support media or unknown content parts',rowCost: 'Estimated cost'},// app/submissionCore.ts — durable admission receipts.