Skip to content

The agent that grows with you

Merge pull request #141492 from NousResearch/fix/pkg13-rt40

Tekniumc9dce2c26f8d
diff --cc apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-commands.test.tsx
index 98878fbd5ed,e7a4922b939..f161dc37016
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-commands.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-session-commands.test.tsx
@@@ -235,79 -217,280 +252,351 @@@ describe('/retry on a canonical session
expect(callsOf(requestGateway, 'prompt.submit')).toEqual([])
expect(renderedText(handle)).toContain(copy.retryUnsupported)
})
+
+ it('refuses during a running turn with main’s stop-first guidance and resubmits nothing', async () => {
+ const requestGateway = gateway(method => {
+ if (method === 'session.resume') {
+ return SNAPSHOT
+ }
+
+ if (method === 'session.rewind') {
+ throw BUSY
+ }
+ })
+
+ publishSessionState(RUNTIME_SESSION_ID, { ...createClientSessionState(RUNTIME_SESSION_ID), busy: true })
+ const handle = await mount(requestGateway)
+ await handle.submitText('/retry')
+
+ expect(renderedText(handle)).toContain('Stop the current reply first')
+ expect(renderedText(handle)).toContain('then run /retry')
+ expect(callsOf(requestGateway, 'prompt.submit')).toEqual([])
+ expect(callsOf(requestGateway, 'session.interrupt')).toEqual([])
+ expect(queuedTexts()).toEqual([])
+ })
+})
+
+describe('/undo and /retry while a compression holds the session', () => {
+ beforeEach(canonical)
+
+ for (const command of ['undo', 'retry'] as const) {
+ it(`/${command} says to wait for the compression instead of pressing Stop`, async () => {
+ const requestGateway = gateway(method => {
+ if (method === 'session.resume') {
+ return SNAPSHOT
+ }
+
+ if (method === 'session.rewind') {
+ throw COMPRESSING
+ }
+ })
+
+ const handle = await mount(requestGateway)
+ await handle.submitText(`/${command}`)
+
+ expect(renderedText(handle)).toContain(`a /compress is running; run /${command} when it finishes`)
+ expect(renderedText(handle)).not.toContain('Stop the current reply first')
+ expect(callsOf(requestGateway, 'session.interrupt')).toEqual([])
+ expect(callsOf(requestGateway, 'prompt.submit')).toEqual([])
+ })
+ }
+
+ it('keeps the wait text for a busy refusal only, not another refusal naming the same field', async () => {
+ const other = new JsonRpcGatewayError('invalid_params', {
+ code: 4001,
+ data: { field: 'compression', reason: 'invalid_params' }
+ })
+
+ const requestGateway = gateway(method => {
+ if (method === 'session.resume') {
+ return SNAPSHOT
+ }
+
+ if (method === 'session.rewind') {
+ throw other
+ }
+ })
+
+ const handle = await mount(requestGateway)
+ await handle.submitText('/undo')
+
+ expect(renderedText(handle)).toContain('error: invalid_params')
+ expect(renderedText(handle)).not.toContain('a /compress is running')
+ })
})
+ // The real CanonicalDesktopProtocol in front of a revision-fenced owner that answers an exact retry with
+ // its original receipt (the authority's durable receipt by request_id). The first rewind commits and its
+ // reply is lost; the user repeats the command.
+ const THREE_EXCHANGES: { content: unknown; role: string }[] = [
+ { role: 'user', content: 'first question' },
+ { role: 'assistant', content: 'first answer' },
+ { role: 'user', content: 'second question' },
+ { role: 'assistant', content: 'second answer' },
+ { role: 'user', content: 'third question' },
+ { role: 'assistant', content: 'third answer' }
+ ]
+
+ function lostRewindReply(transcript = THREE_EXCHANGES) {
+ const protocol = new CanonicalDesktopProtocol()
+ const rows = transcript.map((row, index) => ({ ...row, row_id: 11 + index, active: true }))
+
+ const receipts = new Map<unknown, Record<string, unknown>>()
+ let revision = 4
+ let loseReply = true
+
+ const owner = (wire: string, params: Record<string, unknown>) => {
+ if (wire === 'session.resume') {
+ const messages = rows.filter(row => row.active).map(({ active: _active, ...row }) => row)
+
+ return { session_id: RUNTIME_SESSION_ID, revision, execution_generation: 2, messages }
+ }
+
+ const replay = receipts.get(params.request_id)
+
+ if (replay) {
+ return replay
+ }
+
+ if (params.expected_revision !== revision) {
+ throw Object.assign(new Error('revision_conflict'), { data: { reason: 'revision_conflict', revision } })
+ }
+
+ const target = (params.payload as { target_message_id: number }).target_message_id
+ const rewound = rows.filter(row => row.active && row.row_id >= target)
+ rewound.forEach(row => (row.active = false))
+ revision += 1
+
+ const receipt = {
+ session_id: RUNTIME_SESSION_ID,
+ revision,
+ rewound_count: rewound.length,
+ target_message: { id: target, content: rewound[0].content }
+ }
+
+ receipts.set(params.request_id, receipt)
+
+ if (loseReply) {
+ loseReply = false
+
+ throw new Error('Hermes gateway connection lost')
+ }
+
+ return receipt
+ }
+
+ const requestGateway = vi.fn(async (method: string, params: Record<string, unknown> = {}) => {
+ if (method !== 'session.resume' && method !== 'session.rewind') {
+ return {} as never
+ }
+
+ const local = protocol.localAnswer(method, params)
+
+ if (local) {
+ return local as never
+ }
+
+ const prepared = protocol.prepare(method, params)
+
+ try {
+ return protocol.result(method, prepared, owner(protocol.wire(method, prepared), prepared)) as never
+ } catch (error) {
+ protocol.failure(prepared, error)
+
+ throw error
+ }
+ })
+
+ return { requestGateway, active: () => rows.filter(row => row.active).map(row => row.content) }
+ }
+
+ /** Rendered bubbles bound to the owner's durable rows (a hydrated or refreshed transcript). */
+ const boundMessages = (texts: string[]) =>
+ texts.map((text, index) => ({
+ id: `m${index}`,
+ parts: [textPart(text)],
+ role: index % 2 ? ('assistant' as const) : ('user' as const),
+ rowId: 11 + index,
+ timestamp: index
+ }))
+
+ async function mountWith(requestGateway: ReturnType<typeof gateway>, seedMessages: unknown[]) {
+ let handle: HarnessHandle | null = null
+ await actRender(
+ <Harness
+ onReady={h => (handle = h)}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ seedMessages={seedMessages}
+ />
+ )
+
+ return handle!
+ }
+
+ describe('a repeat after a lost rewind reply on a canonical session', () => {
+ beforeEach(canonical)
+
+ it('/undo removes only the newest exchange and restores its text', async () => {
+ const owner = lostRewindReply()
+ let handle: HarnessHandle | null = null
+
+ const seedMessages = [
+ 'first question',
+ 'first answer',
+ 'second question',
+ 'second answer',
+ 'third question',
+ 'third answer'
+ ].map((text, index) => ({
+ id: `m${index}`,
+ parts: [textPart(text)],
+ role: index % 2 ? ('assistant' as const) : ('user' as const),
+ rowId: 11 + index,
+ timestamp: index
+ }))
+
+ await actRender(
+ <Harness
+ onReady={h => (handle = h)}
+ refreshSessions={async () => undefined}
+ requestGateway={owner.requestGateway}
+ seedMessages={seedMessages}
+ />
+ )
+
+ await handle!.submitText('/undo')
+ expect(renderedText(handle!)).toContain('Hermes gateway connection lost')
+
+ await handle!.submitText('/undo')
+
+ expect(owner.active()).toEqual(['first question', 'first answer', 'second question', 'second answer'])
+ expect($composerDraft.get()).toBe('third question')
+ expect(renderedText(handle!)).toContain(copy.undidOther('2'))
+ // The view drops the rewound rows the owner named, not the turn this repeat's snapshot pointed at.
+ expect(renderedText(handle!)).toContain('second answer')
+ expect(renderedText(handle!)).not.toContain('third question')
+ })
+
+ it('/retry resends the newest question, not the one before it', async () => {
+ const owner = lostRewindReply()
+ const handle = await mount(owner.requestGateway)
+
+ await handle.submitText('/retry')
+ await handle.submitText('/retry')
+
+ expect(owner.active()).toEqual(['first question', 'first answer', 'second question', 'second answer'])
+ expect(callsOf(owner.requestGateway, 'prompt.submit')).toEqual([
+ expect.objectContaining({ session_id: RUNTIME_SESSION_ID, text: 'third question' })
+ ])
+ })
+
+ // The repeat re-presents the retained rewind before deriving (or checking) a target of its own:
+ // the rewound transcript may hold no turn the command would accept.
+ it('/retry that removed the only user turn resends it', async () => {
+ const owner = lostRewindReply(THREE_EXCHANGES.slice(0, 2))
+ const handle = await mount(owner.requestGateway)
+
+ await handle.submitText('/retry')
+ await handle.submitText('/retry')
+
+ expect(renderedText(handle)).not.toContain(copy.retryNothing)
+ expect(callsOf(owner.requestGateway, 'prompt.submit')).toEqual([
+ expect.objectContaining({ session_id: RUNTIME_SESSION_ID, text: 'first question' })
+ ])
+ })
+
+ it('/undo N restores the removed question when fewer than N turns remain', async () => {
+ const owner = lostRewindReply()
+ const handle = await mount(owner.requestGateway)
+
+ await handle.submitText('/undo 2')
+ await handle.submitText('/undo 2')
+
+ expect(owner.active()).toEqual(['first question', 'first answer'])
+ expect($composerDraft.get()).toBe('second question')
+ expect(renderedText(handle)).toContain(copy.undidOther('4'))
+ })
+
+ it('a text-only /retry is not blocked by an older media turn', async () => {
+ const media = [{ type: 'image_url', image_url: 'x' }]
+ const owner = lostRewindReply([{ role: 'user', content: media }, ...THREE_EXCHANGES.slice(1, 4)])
+ const handle = await mount(owner.requestGateway)
+
+ await handle.submitText('/retry')
+ await handle.submitText('/retry')
+
+ expect(renderedText(handle)).not.toContain(copy.retryUnsupported)
+ expect(callsOf(owner.requestGateway, 'prompt.submit')).toEqual([
+ expect.objectContaining({ session_id: RUNTIME_SESSION_ID, text: 'second question' })
+ ])
+ })
+
+ it('a replayed /undo 2 cuts both turns from a view without row ids', async () => {
+ const owner = lostRewindReply()
+
+ const unbound = THREE_EXCHANGES.map((row, index) => ({
+ id: `u${index}`,
+ parts: [textPart(String(row.content))],
+ role: row.role as 'assistant' | 'user',
+ timestamp: index
+ }))
+
+ const handle = await mountWith(owner.requestGateway, unbound)
+
+ await handle.submitText('/undo 2')
+ await handle.submitText('/undo')
+
+ expect(owner.active()).toEqual(['first question', 'first answer'])
+ expect(renderedText(handle)).toContain('first answer')
+ expect(renderedText(handle)).not.toContain('second question')
+ expect(renderedText(handle)).not.toContain('third question')
+ })
+
+ it('a view refreshed after the reconnect keeps every exchange the owner still holds', async () => {
+ const owner = lostRewindReply()
+
+ // The reconnect refresh already shows the owner's rewound transcript (the third exchange gone).
+ const handle = await mountWith(
+ owner.requestGateway,
+ boundMessages(['first question', 'first answer', 'second question', 'second answer'])
+ )
+
+ await handle.submitText('/undo')
+ await handle.submitText('/undo')
+
+ expect(owner.active()).toEqual(['first question', 'first answer', 'second question', 'second answer'])
+ expect(renderedText(handle)).toContain('second question')
+ expect(renderedText(handle)).toContain('second answer')
+ })
+ })
+
+ describe('truncateRewoundMessages', () => {
+ const live = (text: string, role: 'assistant' | 'user') => ({ id: text, parts: [textPart(text)], role, timestamp: 9 })
+
+ it('is a no-op when the bound view no longer holds the rewound row', () => {
+ const messages = boundMessages(['first question', 'first answer', 'second question', 'second answer'])
+
+ expect(truncateRewoundMessages(messages as never, 15, 1)).toEqual(messages)
+ })
+
+ it('cuts unbound live bubbles after the bound history by position', () => {
+ const messages = [
+ ...boundMessages(['first question', 'first answer']),
+ live('new question', 'user'),
+ live('new answer', 'assistant')
+ ]
+
+ expect(truncateRewoundMessages(messages as never, 13, 1).map(message => message.id)).toEqual(['m0', 'm1'])
+ })
+
+ it('falls back by position on a wholly unbound view', () => {
+ const messages = [live('a', 'user'), live('b', 'assistant'), live('c', 'user'), live('d', 'assistant')]
+
+ expect(truncateRewoundMessages(messages as never, 13, 1).map(message => message.id)).toEqual(['a', 'b'])
+ })
+ })
+
describe('/steer', () => {
it('steers the running turn through session.steer', async () => {
publishSessionState(RUNTIME_SESSION_ID, { ...createClientSessionState(RUNTIME_SESSION_ID), busy: true })