Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/ai-persistence/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ function resumeToolStateFromPending(
): ChatResumeToolState | undefined {
const approvals = new Map<string, ToolApprovalResolution>()
const clientToolResults = new Map<string, unknown>()
const cancelledToolCallIds = new Set<string>()

for (const interrupt of pending) {
const entry = resumeByInterruptId.get(interrupt.interruptId)
Expand All @@ -405,6 +406,10 @@ function resumeToolStateFromPending(
const reason = stringField(interrupt.payload, 'reason')
const toolCallId = stringField(interrupt.payload, 'toolCallId')

if (entry.status === 'cancelled' && toolCallId) {
cancelledToolCallIds.add(toolCallId)
}

if (kind === 'approval' || reason === 'approval_required') {
approvals.set(interrupt.interruptId, resolvedApprovalDecision(entry))
continue
Expand All @@ -419,8 +424,14 @@ function resumeToolStateFromPending(
}
}

if (approvals.size === 0 && clientToolResults.size === 0) return undefined
return { approvals, clientToolResults }
if (
approvals.size === 0 &&
clientToolResults.size === 0 &&
cancelledToolCallIds.size === 0
) {
return undefined
}
return { approvals, clientToolResults, cancelledToolCallIds }
}

/**
Expand Down
22 changes: 19 additions & 3 deletions packages/ai-persistence/tests/interrupts.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { EventType, chat } from '@tanstack/ai'
import { EventType, chat, defineChatMiddleware } from '@tanstack/ai'
import type { AnyTextAdapter, StreamChunk, Tool } from '@tanstack/ai'
import { memoryPersistence } from '../src/memory'
import { withPersistence } from '../src/middleware'
Expand Down Expand Up @@ -775,21 +775,29 @@ describe('interrupt persistence', () => {
})

const run = mockAdapter([[runStarted(), text('ok'), runFinished('r1')]])
const resumeStates: Array<ReadonlySet<string> | undefined> = []
const observeResumeState = defineChatMiddleware({
name: 'observe-resume-state',
onConfig(_ctx, config) {
resumeStates.push(config.resumeToolState?.cancelledToolCallIds)
},
})
await collect(
chat({
adapter: run.adapter,
messages: [],
runId: 'r1',
threadId: 't1',
resume: [{ interruptId: 'approval-1', status: 'cancelled' }],
middleware: [withPersistence(persistence)],
middleware: [withPersistence(persistence), observeResumeState],
}) as AsyncIterable<StreamChunk>,
)

const approvals = (
run.calls[0] as { approvals?: ReadonlyMap<string, boolean> }
).approvals
expect(approvals?.get('approval-1')).toBe(false)
expect(resumeStates[0]?.has('tc1')).toBe(true)
expect(
(await persistence.stores.interrupts!.get('approval-1'))?.status,
).toBe('cancelled')
Expand All @@ -806,6 +814,13 @@ describe('interrupt persistence', () => {
})

const run = mockAdapter([[runStarted(), text('ok'), runFinished('r1')]])
const resumeStates: Array<ReadonlySet<string> | undefined> = []
const observeResumeState = defineChatMiddleware({
name: 'observe-resume-state',
onConfig(_ctx, config) {
resumeStates.push(config.resumeToolState?.cancelledToolCallIds)
},
})
await collect(
chat({
adapter: run.adapter,
Expand All @@ -819,7 +834,7 @@ describe('interrupt persistence', () => {
payload: { answer: 99 },
},
],
middleware: [withPersistence(persistence)],
middleware: [withPersistence(persistence), observeResumeState],
}) as AsyncIterable<StreamChunk>,
)

Expand All @@ -829,6 +844,7 @@ describe('interrupt persistence', () => {
run.calls[0] as { clientToolResults?: ReadonlyMap<string, unknown> }
).clientToolResults

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] The new assertions only prove the translator wrote cancelledToolCallIds. Both cancel tests still use empty messages, no tools, and no stored thread with a pending assistant tool call. The engine therefore never reaches checkForPendingToolCalls() / executeToolCalls(), so these tests cannot fail if a cancelled hydrated client tool still re-interrupts. The file already has the right shape for that path in applies persisted approval and client-tool resume decisions with empty client messages (persist tool-call turn, resume with empty client messages, assert chunks).

Suggestion: Add a regression that mirrors that hydrate path for status: 'cancelled': persist a client-tool interrupt plus the assistant tool-call message, resume with empty messages and the same tools, and assert a TOOL_CALL_RESULT of Tool execution cancelled plus no new client_tool_* interrupt. Keep the current onConfig Set check if you want, but do not treat it as coverage for #1088.

expect(clientToolResults?.get('tc1')).toBeUndefined()
expect(resumeStates[0]?.has('tc1')).toBe(true)
expect((await persistence.stores.interrupts!.get('client-1'))?.status).toBe(
'cancelled',
)
Expand Down