Skip to content

The agent that grows with you

fix(tui): a busy correction refused undelivered is re-admitted as a prompt

Tekniume9530b875758
diff --git a/ui-tui/src/__tests__/submissionCoreUndeliveredCorrection.test.ts b/ui-tui/src/__tests__/submissionCoreUndeliveredCorrection.test.ts
new file mode 100644
index 00000000000..e42ed43a59d
--- /dev/null
+++ b/ui-tui/src/__tests__/submissionCoreUndeliveredCorrection.test.ts
@@ -0,0 +1,100 @@
+import { mkdtempSync, rmSync } from 'node:fs'
+import { join } from 'node:path'
+
+import { JsonRpcGatewayError } from '@hermes/shared/json-rpc-channel'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import { submitPrompt, type SubmitPromptDeps } from '../app/submissionCore.js'
+import { captureDestination } from '../app/submissionDestination.js'
+import { patchUiState, resetUiState } from '../app/uiStore.js'
+import type { GatewayClient } from '../gatewayClient.js'
+import type { QueueItem } from '../hooks/useQueue.js'
+import { loadPendingInputs } from '../lib/pendingInputs.js'
+
+// #141511: a busy correction the owner refused before any agent saw it (the turn the TUI believed
+// busy had ended) stayed in the journal as a control naming that dead generation, so every retry
+// was refused again and the words were never delivered. It must be re-admitted as a prompt.
+
+let home: string
+
+beforeEach(() => {
+ home = mkdtempSync(join(process.env.TMPDIR ?? '.', 'ink-undelivered-'))
+ vi.stubEnv('HERMES_HOME', home)
+ resetUiState()
+ patchUiState({ sid: 'owner', busy: true, status: 'running…' })
+})
+
+afterEach(() => {
+ resetUiState()
+ vi.unstubAllEnvs()
+ rmSync(home, { recursive: true, force: true })
+})
+
+function deps(gw: GatewayClient): SubmitPromptDeps {
+ return { appendMessage: vi.fn(), enqueue: vi.fn(), expand: t => t, gw, setLastUserMsg: vi.fn(), sys: vi.fn() }
+}
+
+function correction(settle: (accepted: boolean) => void): QueueItem {
+ return {
+ controlMethod: 'session.redirect',
+ destination: captureDestination(),
+ display: 'next thought',
+ executionGeneration: 4,
+ settle,
+ submissionId: 'sub-1',
+ text: 'next thought'
+ }
+}
+
+const refusals: Array<[string, () => Promise<unknown>]> = [
+ ['stale_generation', async () => {
+ throw new JsonRpcGatewayError('stale_generation', { code: 4001, data: { reason: 'stale_generation' } })
+ }],
+ ['execution_not_ready', async () => {
+ throw new JsonRpcGatewayError('execution_not_ready', { code: 4001, data: { reason: 'execution_not_ready' } })
+ }],
+ ['rejected', async () => ({ status: 'rejected', text: 'next thought', execution_generation: 4 })]
+]
+
+it.each(refusals)('re-admits a correction refused undelivered (%s) as a prompt with the same submission_id', async (_name, refuse) => {
+ const calls: Array<{ method: string; params: Record<string, unknown> }> = []
+ const settle = vi.fn()
+
+ const request = vi.fn(async (method: string, params: Record<string, unknown>) => {
+ calls.push({ method, params })
+
+ return method === 'prompt.submit' ? { status: 'queued' } : refuse()
+ })
+
+ const item = correction(settle)
+ submitPrompt('next thought', deps({ isCanonical: true, request } as unknown as GatewayClient), true, undefined, {
+ behindTurn: true,
+ queueItem: item,
+ skipDetectDrop: true
+ })
+
+ await vi.waitFor(() => expect(calls.map(c => c.method)).toEqual(['session.redirect', 'prompt.submit']))
+ expect(calls[1]!.params).toMatchObject({ session_id: 'owner', text: 'next thought', submission_id: 'sub-1', queued: true })
+ expect(calls[1]!.params).not.toHaveProperty('execution_generation')
+ // The journal no longer holds a control a later retry would replay against the dead generation.
+ expect(loadPendingInputs(captureDestination()).find(row => row.submissionId === 'sub-1')?.controlMethod).toBeUndefined()
+})
+
+it('keeps an ambiguous transport failure as a retained control, never replayed as a prompt', async () => {
+ const calls: string[] = []
+ const settle = vi.fn()
+
+ const request = vi.fn(async (method: string) => {
+ calls.push(method)
+ throw new Error('request timed out after 120s: session.redirect')
+ })
+
+ submitPrompt('next thought', deps({ isCanonical: true, request } as unknown as GatewayClient), true, undefined, {
+ behindTurn: true,
+ queueItem: correction(settle),
+ skipDetectDrop: true
+ })
+
+ await vi.waitFor(() => expect(settle).toHaveBeenCalledWith(false))
+ expect(calls).toEqual(['session.redirect'])
+})
diff --git a/ui-tui/src/app/submissionCore.ts b/ui-tui/src/app/submissionCore.ts
index 2157b415f23..d90aff90b63 100644
--- a/ui-tui/src/app/submissionCore.ts
+++ b/ui-tui/src/app/submissionCore.ts
@@ -1,3 +1,5 @@
+import { isUndeliveredCorrectionRefusal } from '@hermes/shared/json-rpc-channel'
+
import type { GatewayClient } from '../gatewayClient.js'
import type { InputDetectDropResponse, PromptSubmitResponse, SessionActivateResponse } from '../gatewayTypes.js'
import type { QueueItem } from '../hooks/useQueue.js'
@@ -135,13 +137,25 @@ function submitParams(
}
}
+/** A correction the owner confirmed it never delivered becomes the next prompt: the same row and
+ * submission_id, admitted durably behind any running turn. Kept as a control it would be refused
+ * again on every retry, since the generation it names is over (#141511). */
+export function readmitAsPrompt(item: QueueItem): void {
+ delete item.controlMethod
+ delete item.executionGeneration
+ item.legacyAttempted = false
+ item.queued = true
+ savePendingInput(item)
+}
+
+/** Settles a busy-correction reply; false when the owner refused it before any agent saw the text. */
function settleControlReply(
item: QueueItem,
r: PromptSubmitResponse,
deps: SubmitPromptDeps,
focused: () => boolean,
sid: string
-): void {
+): boolean {
// A bare stop phrase typed as a busy correction ended the voice chat and reached
// no turn (TUI-26): settled, rendered as the stop-phrase event, the turn untouched.
if (r?.voice_stopped === true) {
@@ -151,11 +165,16 @@ function settleControlReply(
deps.gw.publishLocalEvent({ payload: { stop_phrase: true, typed: true }, session_id: sid, type: 'voice.transcript' })
}
- return
+ return true
}
const control = r as PromptSubmitResponse & { execution_generation?: number }
+ // 'rejected' is the owner's "not corrected, queue it as the next prompt" (no live agent took it).
+ if (control?.status === 'rejected' && control.execution_generation === item.executionGeneration) {
+ return false
+ }
+
const accepted =
control.execution_generation === item.executionGeneration && ['queued', 'redirected'].includes(control.status ?? '')
@@ -164,6 +183,8 @@ function settleControlReply(
if (focused()) {
deps.sys(accepted ? t('canonical.submit.correction', control.status) : t('canonical.submit.correctionRejected'))
}
+
+ return true
}
interface AdmissionContext {
@@ -333,6 +354,16 @@ export function submitPrompt(
markSubmitting()
}
+ const readmit = (item: QueueItem) => {
+ readmitAsPrompt(item)
+ submitPrompt(text, deps, showUserMessage, displayOverride, {
+ behindTurn: true,
+ destination,
+ queueItem: item,
+ skipDetectDrop: true
+ })
+ }
+
const startSubmit = (displayText: string, submitText: string, show = true) => {
if (ownsTurn()) {
beginOwnedTurn(deps, text, displayOverride, displayText, show)
@@ -362,7 +393,9 @@ export function submitPrompt(
)
.then(r => {
if (item?.controlMethod) {
- settleControlReply(item, r, deps, focused, sid)
+ if (!settleControlReply(item, r, deps, focused, sid)) {
+ readmit(item)
+ }
return
}
@@ -409,6 +442,12 @@ export function submitPrompt(
return
}
+ if (item?.controlMethod && isUndeliveredCorrectionRefusal(e)) {
+ readmit(item)
+
+ return
+ }
+
if (item) {
item.settle?.(false)