The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(ink): edited queued input keeps its own session when focus moves before retirement
teknium1349f39a0c043
diff --git a/ui-tui/src/__tests__/busyQueueAdmission.test.tsx b/ui-tui/src/__tests__/busyQueueAdmission.test.tsxindex 5a8676cc509..ed370d9472d 100644--- a/ui-tui/src/__tests__/busyQueueAdmission.test.tsx+++ b/ui-tui/src/__tests__/busyQueueAdmission.test.tsx@@ -158,6 +158,7 @@ it('retains disconnected input durably on its old destination without dispatchinit('refuses unbound Enter without clearing the draft or claiming a queued input', () => {const h = mount()+try {patchUiState({ sid: null })h.submission.dispatchSubmission('unbound draft')@@ -265,3 +266,23 @@ it('keeps the edited text as an unconfirmed durable draft when the store refusesexpect(loadPendingInputs(captureDestination()).map(item => [item.text, item.failed])).toEqual([['EDITED_EFFECT', true]])} finally { h.cleanup() }})++it('re-admits an edited row to the session it was edited in, even when the user has switched to another busy session before retirement', async () => {+ let release!: (value: unknown) => void+ const h = mount('queue', () => new Promise(resolve => { release = resolve }))++ try {+ h.fanout([row('adm-orig', 'in-orig', 'ORIGINAL_A')])+ await expect.poll(() => h.queue.queuedDisplay).toEqual(['[queued] ORIGINAL_A'])+ h.queue.setQueueEdit(0)+ h.submission.dispatchSubmission('EDITED_A')+ await expect.poll(() => h.calls.filter(c => c.method === 'prompt.cancel').length).toBe(1)+ // Focus moves to a different busy session while A's retirement is pending.+ patchUiState({ sid: 'session-B', busy: true,+ info: { ...$uiState.get().info!, stored_session_id: 'stored-B', execution_generation: 9 } })+ await new Promise(resolve => setImmediate(resolve))+ release({ admission_id: 'adm-orig', status: 'terminal', outcome: 'cancelled' })+ await expect.poll(() => h.calls.filter(c => c.method === 'prompt.submit').length).toBe(1)+ expect(h.calls.find(c => c.method === 'prompt.submit')!.params).toMatchObject({ session_id: 'owner', text: 'EDITED_A' })+ } finally { h.cleanup() }+})diff --git a/ui-tui/src/app/useSubmission.ts b/ui-tui/src/app/useSubmission.tsindex fe7a90c662f..c79b425043e 100644--- a/ui-tui/src/app/useSubmission.ts+++ b/ui-tui/src/app/useSubmission.ts@@ -12,7 +12,7 @@ import { asRpcResult } from '../lib/rpc.js'import { hasInterpolation, INTERPOLATION_RE } from '../protocol/interpolation.js'import type { Msg } from '../types.js'-import type { ComposerActions, ComposerRefs, ComposerState, ComposerToken, SlashHandler } from './interfaces.js'+import type { BusyInputMode, ComposerActions, ComposerRefs, ComposerState, ComposerToken, SlashHandler } from './interfaces.js'import { submitPrompt } from './submissionCore.js'import { captureDestination, isCurrentDestination, type SubmissionDestination } from './submissionDestination.js'import { turnController } from './turnController.js'@@ -64,6 +64,24 @@ export const prepareSlashSubmission = (display: string, tokens: ComposerToken[])export const shouldInterpolateSubmission = (display: string) => hasInterpolation(display)+/**+ * Where and how a busy-time input is admitted. Captured synchronously at the+ * moment the user commits the input: a continuation that runs after an await+ * (an edited row waiting for its original to retire) must not re-read the+ * live focus, which may have moved to another session in the meantime.+ */+export interface BusyControl {+ destination: SubmissionDestination+ mode: BusyInputMode+ executionGeneration?: number+}++export const captureBusyControl = (): BusyControl => {+ const live = getUiState()++ return { destination: captureDestination(), mode: live.busyInputMode, executionGeneration: live.info?.execution_generation }+}+export function useSubmission(opts: UseSubmissionOptions) {const { appendMessage, composerActions, composerRefs, composerState, gw, setLastUserMsg, slashRef, submitRef, sys } =opts@@ -242,10 +260,8 @@ export function useSubmission(opts: UseSubmissionOptions) {// `opts.fallbackToFront` re-inserts at the queue head (queue-edit picks keep// their position); the mainline submit path appends.const handleBusyInput = useCallback(- (item: QueueItem, opts: { fallbackToFront?: boolean } = {}) => {- const live = getUiState()- const destination = captureDestination()- const mode = live.busyInputMode+ (item: QueueItem, opts: { fallbackToFront?: boolean; control?: BusyControl } = {}) => {+ const { destination, mode, executionGeneration } = opts.control ?? captureBusyControl()const enqueueText = () => {if (opts.fallbackToFront) {@@ -260,7 +276,7 @@ export function useSubmission(opts: UseSubmissionOptions) {if (!staged) { return }staged.controlMethod ??= mode === 'steer' ? 'session.steer' : 'session.redirect'- staged.executionGeneration ??= live.info?.execution_generation+ staged.executionGeneration ??= executionGenerationstaged.attachments ??= item.attachmentsreturn send(item.text, true, item.display, value => value, {@@ -391,20 +407,25 @@ export function useSubmission(opts: UseSubmissionOptions) {}// An edited authority row is admitted only after its original retires.+ // The edit belongs to the session it was made in: focus may move to+ // another (busy) session before the retirement resolves.+ const control = captureBusyControl()+ const editedWhileBusy = live.busy+return void Promise.resolve(picked).then(item => {if (!item) {return}- if (getUiState().busy) {+ if (editedWhileBusy) {// 'interrupt' / 'steer' should reach the live turn instead of// silently going back to the queue. handleBusyInput resolves// mode-specific behavior (interrupt-and-send, steer, or queue).- if (getUiState().busyInputMode === 'queue' && !gw.isCanonical) {- return composerActions.prependQueue(item)+ if (control.mode === 'queue' && !gw.isCanonical) {+ return composerActions.prependQueue(item, control.destination)}- return handleBusyInput(item, { fallbackToFront: true })+ return handleBusyInput(item, { fallbackToFront: true, control })}return sendQueued(item)