Skip to content

The agent that grows with you

fix(tui): the composer keeps its draft until the pending-input journal write succeeds

teknium17eda859d430b
diff --git a/ui-tui/src/__tests__/slashAttachmentRenderer.test.tsx b/ui-tui/src/__tests__/slashAttachmentRenderer.test.tsx
index 4576f566b4c..145ef924e5e 100644
--- a/ui-tui/src/__tests__/slashAttachmentRenderer.test.tsx
+++ b/ui-tui/src/__tests__/slashAttachmentRenderer.test.tsx
@@ -1,4 +1,4 @@
-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { PassThrough } from 'node:stream'
@@ -214,3 +214,27 @@ it('a skill slash command keeps the staged image descriptors through the expande
expect(h.composer.refs.tokensRef.current).toEqual([])
} finally { h.close() }
})
+
+it('keeps the caption and its image token in the composer when the pending-input journal cannot be written', async () => {
+ const h = mount(true)
+ vi.stubEnv('HERMES_TUI_GATEWAY_URL', '')
+ const path = join(h.home, 'shot.png')
+ writeFileSync(path, Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl2nL8AAAAASUVORK5CYII=', 'base64'))
+ const journal = join(h.home, 'tui-pending-inputs')
+ mkdirSync(journal, { recursive: true })
+
+ try {
+ await h.submit(`/image ${path}`)
+ await vi.waitFor(() => expect(h.composer.state.input).toContain('[[ Image 1 ]]'))
+ chmodSync(journal, 0o500)
+ await h.submit('caption [[ Image 1 ]]')
+ expect(h.request.mock.calls.some(([method]) => method === 'prompt.submit')).toBe(false)
+ expect(h.composer.state.input).toBe('caption [[ Image 1 ]]')
+ expect(h.composer.refs.tokensRef.current).toEqual([expect.objectContaining({ kind: 'image', mime: 'image/png' })])
+ expect(getUiState().status).toBe('input not saved')
+ chmodSync(journal, 0o700)
+ await h.submit('caption [[ Image 1 ]]')
+ await vi.waitFor(() => expect(h.request.mock.calls.some(([method]) => method === 'prompt.submit')).toBe(true))
+ expect(h.request.mock.calls.find(([method]) => method === 'prompt.submit')![1].attachments).toHaveLength(1)
+ } finally { chmodSync(journal, 0o700); h.close() }
+})
diff --git a/ui-tui/src/app/useSubmission.ts b/ui-tui/src/app/useSubmission.ts
index 4018df22448..ab6fd6d9e23 100644
--- a/ui-tui/src/app/useSubmission.ts
+++ b/ui-tui/src/app/useSubmission.ts
@@ -351,21 +351,38 @@ export function useSubmission(opts: UseSubmissionOptions) {
if (!live.sid) { return sys('session not ready — draft kept; reconnect or choose a session') }
+ // The composer is the only copy of a draft until the pending-input
+ // journal holds it, so it is cleared after that first durable write and
+ // kept — text and image tokens — when the write fails.
+ const journaled = <T,>(write: () => T): { value: T } | undefined => {
+ try {
+ const value = write()
+ composerActions.clearIn()
+
+ return { value }
+ } catch (error) {
+ sys(`input not saved: ${(error as Error).message} — draft kept`)
+ patchUiState({ status: 'input not saved' })
+
+ return undefined
+ }
+ }
+
if (live.gatewayConnected === false) {
composerActions.pushHistory(toHistory)
- const retained = composerActions.enqueue(submission.text, submission.display, destination)
+ journaled(() => {
+ const retained = composerActions.enqueue(submission.text, submission.display, destination)
- if (retained) { retained.attachments = submission.attachments; savePendingInput(retained) }
- composerActions.clearIn()
+ if (retained) { retained.attachments = submission.attachments; savePendingInput(retained) }
+ })
return
}
const editIdx = composerRefs.queueEditRef.current
- composerActions.clearIn()
if (editIdx !== null) {
- const picked = composerActions.takeQueue(editIdx, full)
+ const picked = journaled(() => composerActions.takeQueue(editIdx, full))?.value
composerActions.setQueueEdit(null)
if (!picked || !live.sid) {
@@ -396,13 +413,16 @@ export function useSubmission(opts: UseSubmissionOptions) {
composerActions.pushHistory(toHistory)
if (getUiState().busy) {
- return handleBusyInput({ ...queueItem(submission.text, submission.display), attachments: submission.attachments })
+ return void journaled(() =>
+ handleBusyInput({ ...queueItem(submission.text, submission.display), attachments: submission.attachments }))
}
if (shouldInterpolateSubmission(full)) {
- patchUiState({ busy: true })
+ const staged = journaled(() => composerActions.stage?.(submission.text, submission.display, destination))
- const item = composerActions.stage?.(submission.text, submission.display, destination)
+ if (!staged) { return }
+ patchUiState({ busy: true })
+ const item = staged.value
return interpolate(
full,
@@ -415,7 +435,7 @@ export function useSubmission(opts: UseSubmissionOptions) {
)
}
- send(submission.text, true, submission.display, value => value, { attachments: submission.attachments })
+ journaled(() => send(submission.text, true, submission.display, value => value, { attachments: submission.attachments }))
},
[
appendMessage,