π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(agent-runtime): keep approval requests queued behind an async tool pause (#19987)
Arvin Xubc36d67d5d25
diff --git a/packages/agent-runtime/src/core/__tests__/asyncToolPauseInstructions.test.ts b/packages/agent-runtime/src/core/__tests__/asyncToolPauseInstructions.test.tsnew file mode 100644index 00000000000..1145b905a47--- /dev/null+++ b/packages/agent-runtime/src/core/__tests__/asyncToolPauseInstructions.test.ts@@ -0,0 +1,216 @@+import type { ChatToolPayload } from '@lobechat/types';+import { describe, expect, it, vi } from 'vitest';++import { GeneralChatAgent } from '../../agents/GeneralChatAgent';+import type {+ AgentInstruction,+ AgentRuntimeContext,+ AgentState,+ InstructionExecutor,+} from '../../types';+import { AgentRuntime } from '../runtime';++const subAgentCall: ChatToolPayload = {+ apiName: 'callSubAgent',+ arguments: '{"instruction":"research"}',+ id: 'call_sub_agent',+ identifier: 'lobe-agent',+ type: 'builtin',+};++const askUserCall: ChatToolPayload = {+ apiName: 'askUserQuestion',+ arguments: '{"question":"Which option?"}',+ id: 'call_ask_user',+ identifier: 'lobe-agent',+ type: 'builtin',+};++const toolManifestMap = {+ 'lobe-agent': {+ api: [+ { description: '', humanIntervention: 'never', name: 'callSubAgent', parameters: {} },+ { description: '', humanIntervention: 'always', name: 'askUserQuestion', parameters: {} },+ ],+ identifier: 'lobe-agent',+ meta: {},+ type: 'builtin',+ },+} as any;++const createAgent = () =>+ new GeneralChatAgent({+ agentConfig: { maxSteps: 100 },+ modelRuntimeConfig: { model: 'gpt-4o-mini', provider: 'openai' },+ operationId: 'op-async-pause',+ });++const createState = (overrides?: Partial<AgentState>): AgentState =>+ AgentRuntime.createInitialState({+ messages: [+ { content: 'Plan the trip', id: 'msg-user', role: 'user' },+ { content: '', id: 'msg-assistant', role: 'assistant' },+ ],+ operationId: 'op-async-pause',+ status: 'running',+ toolManifestMap,+ ...overrides,+ });++const llmResultContext = (toolsCalling: ChatToolPayload[]): AgentRuntimeContext => ({+ payload: {+ hasToolsCalling: true,+ parentMessageId: 'msg-assistant',+ result: { content: '', tool_calls: [] },+ toolsCalling,+ },+ phase: 'llm_result',+ session: { messageCount: 2, sessionId: 'op-async-pause', status: 'running', stepCount: 1 },+});++/** `call_tool` stub that parks the op the way a deferred sub-agent tool does. */+const parkingCallTool = vi.fn<InstructionExecutor>(async (instruction, state) => {+ const { payload } = instruction as Extract<AgentInstruction, { type: 'call_tool' }>;+ const newState = structuredClone(state);+ newState.status = 'waiting_for_async_tool';+ newState.pendingToolsCalling = [(payload as any).toolCalling];+ newState.interruption = {+ canResume: true,+ interruptedAt: new Date().toISOString(),+ reason: 'async_tool',+ };+ return { events: [], newState };+});++const createHumanApprove = () =>+ vi.fn<InstructionExecutor>(async (instruction, state) => {+ const { pendingToolsCalling } = instruction as Extract<+ AgentInstruction,+ { type: 'request_human_approve' }+ >;+ const newState = structuredClone(state);+ newState.status = 'waiting_for_human';+ newState.pendingToolsCalling = pendingToolsCalling;+ return { events: [], newState };+ });++/** What the server does when every deferred tool has delivered its result. */+const resumeFromAsyncTool = (state: AgentState): [AgentState, AgentRuntimeContext] => {+ const resumed = structuredClone(state);+ resumed.status = 'running';+ resumed.pendingToolsCalling = [];+ resumed.interruption = undefined;+ return [+ resumed,+ {+ payload: { parentMessageId: 'msg-sub-agent-tool' },+ phase: 'user_input',+ session: { messageCount: 3, sessionId: 'op-async-pause', status: 'running', stepCount: 2 },+ },+ ];+};++describe('AgentRuntime instructions queued behind an async tool pause', () => {+ it('asks the human-approval question after the async tool resumes instead of dropping it', async () => {+ const agent = createAgent();+ const state = createState();++ const instructions = await agent.runner(+ llmResultContext([subAgentCall, askUserCall]),+ structuredClone(state),+ );+ expect(+ (Array.isArray(instructions) ? instructions : [instructions]).map((i) => i.type),+ ).toEqual(['call_tool', 'request_human_approve']);++ const humanApprove = createHumanApprove();+ const callLlm = vi.fn<InstructionExecutor>(async (_instruction, s) => ({+ events: [],+ newState: structuredClone(s),+ }));+ const runtime = new AgentRuntime(agent, {+ executors: {+ call_llm: callLlm,+ call_tool: parkingCallTool,+ request_human_approve: humanApprove,+ },+ });++ // Step 1: the sub-agent parks the operation.+ const parked = await runtime.step(state, llmResultContext([subAgentCall, askUserCall]));+ expect(parked.newState.status).toBe('waiting_for_async_tool');+ expect(parked.newState.pendingToolsCalling).toEqual([subAgentCall]);+ expect(parked.nextContext).toBeUndefined();++ // Step 2: the sub-agent finished and the op resumes. The question the+ // model asked in the same turn must be put to the user before the LLM+ // runs again β otherwise it never gets a tool message.+ const [resumedState, resumeContext] = resumeFromAsyncTool(parked.newState);+ const resumed = await runtime.step(resumedState, resumeContext);++ expect(humanApprove).toHaveBeenCalledTimes(1);+ expect(humanApprove.mock.calls[0][0]).toMatchObject({+ parentMessageId: 'msg-assistant',+ pendingToolsCalling: [askUserCall],+ type: 'request_human_approve',+ });+ expect(callLlm).not.toHaveBeenCalled();+ expect(resumed.newState.status).toBe('waiting_for_human');+ expect(resumed.newState.pendingToolsCalling).toEqual([askUserCall]);+ expect(resumed.newState.deferredHumanApproval).toBeUndefined();+ });++ it('continues with the LLM on resume when nothing was queued behind the pause', async () => {+ const agent = createAgent();+ const humanApprove = createHumanApprove();+ const callLlm = vi.fn<InstructionExecutor>(async (_instruction, s) => ({+ events: [],+ newState: structuredClone(s),+ }));+ const runtime = new AgentRuntime(agent, {+ executors: {+ call_llm: callLlm,+ call_tool: parkingCallTool,+ request_human_approve: humanApprove,+ },+ });++ const parked = await runtime.step(createState(), llmResultContext([subAgentCall]));+ expect(parked.newState.status).toBe('waiting_for_async_tool');+ expect(parked.newState.deferredHumanApproval).toBeUndefined();++ const [resumedState, resumeContext] = resumeFromAsyncTool(parked.newState);+ await runtime.step(resumedState, resumeContext);++ expect(humanApprove).not.toHaveBeenCalled();+ expect(callLlm).toHaveBeenCalledTimes(1);+ });++ it('still persists blocked results for tools queued behind the pause in headless runs', async () => {+ const agent = createAgent();+ const resolveBlocked = vi.fn<InstructionExecutor>(async (_instruction, s) => ({+ events: [],+ newState: structuredClone(s),+ }));+ const runtime = new AgentRuntime(agent, {+ executors: {+ call_tool: parkingCallTool,+ resolve_blocked_tools: resolveBlocked,+ },+ });++ const result = await runtime.step(+ createState({ userInterventionConfig: { approvalMode: 'headless' } }),+ llmResultContext([subAgentCall, askUserCall]),+ );++ expect(resolveBlocked).toHaveBeenCalledTimes(1);+ expect(resolveBlocked.mock.calls[0][0]).toMatchObject({+ payload: { blockedReason: 'human_intervention_unavailable', toolsCalling: [askUserCall] },+ type: 'resolve_blocked_tools',+ });+ expect(result.newState.status).toBe('waiting_for_async_tool');+ expect(result.newState.pendingToolsCalling).toEqual([subAgentCall]);+ expect(result.nextContext).toBeUndefined();+ });+});diff --git a/packages/agent-runtime/src/core/runtime.ts b/packages/agent-runtime/src/core/runtime.tsindex 73f01a26f5a..4ade262ed3a 100644--- a/packages/agent-runtime/src/core/runtime.ts+++ b/packages/agent-runtime/src/core/runtime.ts@@ -160,6 +160,14 @@ export class AgentRuntime {type: 'call_tool',};}+ } else if (newState.deferredHumanApproval && !isBlockedStatus(newState.status)) {+ // The operation is resuming from an async-tool park whose LLM turn also+ // asked for human approval. Put that request to the user now, before+ // the agent plans another LLM call β the tool call would otherwise+ // never get a result. Once approved, the continuation sees every tool+ // result of the turn and moves on.+ rawInstructions = newState.deferredHumanApproval;+ newState.deferredHumanApproval = undefined;} else {if (runtimeContext.phase === 'tool_result') {const toolResultPayload = runtimeContext.payload as@@ -261,6 +269,14 @@ export class AgentRuntime {// Stop execution if blockedif (isBlockedStatus(currentState.status)) {+ if (currentState.status === 'waiting_for_async_tool') {+ currentState = await this.settleInstructionsBehindAsyncToolPause(+ normalizedInstructions.slice(instructionIndex + 1),+ currentState,+ runtimeContext,+ allEvents,+ );+ }break;}}@@ -290,6 +306,46 @@ export class AgentRuntime {}}+ /**+ * Handle the instructions an LLM turn queued behind a tool that parked the+ * operation for an async result (sub-agent, client tool). Dropping them+ * leaves their tool calls without a result row, and the next LLM call gets a+ * synthetic `tool_result_missing` for each.+ *+ * - `resolve_blocked_tools` only persists rejected results, so it runs now.+ * - `request_human_approve` would park the operation a second time; it is+ * held on the state and issued when the async tool resumes the operation.+ *+ * Anything else keeps the previous behavior and is left to the resumed turn.+ */+ private async settleInstructionsBehindAsyncToolPause(+ queuedInstructions: AgentInstruction[],+ pausedState: AgentState,+ runtimeContext: AgentRuntimeContext,+ events: AgentEvent[],+ ): Promise<AgentState> {+ let currentState = pausedState;++ for (const instruction of queuedInstructions) {+ if (instruction.type === 'request_human_approve') {+ currentState = { ...currentState, deferredHumanApproval: instruction };+ continue;+ }++ if (instruction.type === 'resolve_blocked_tools') {+ const result = await this.executors.resolve_blocked_tools(+ instruction,+ currentState,+ runtimeContext,+ );+ events.push(...result.events);+ currentState = result.newState;+ }+ }++ return currentState;+ }+/*** Convenience method for approving and executing a tool call*/diff --git a/packages/agent-runtime/src/types/state.ts b/packages/agent-runtime/src/types/state.tsindex 2053186cc94..2f6d74eb739 100644--- a/packages/agent-runtime/src/types/state.ts+++ b/packages/agent-runtime/src/types/state.ts@@ -29,6 +29,7 @@ import type {UserInterventionConfig,} from '@lobechat/types';+import type { AgentInstructionRequestHumanApprove } from './instruction';import type { Cost, CostLimit, Usage } from './usage';/**@@ -301,6 +302,13 @@ export interface AgentState {costLimit?: CostLimit;// --- Metadata ---createdAt: string;+ /**+ * Approval request the same LLM turn emitted after a tool that parked the+ * operation (`waiting_for_async_tool`). The step loop stops at the park, so+ * the request is held here and issued by the step that resumes the+ * operation β before the LLM runs again β instead of being dropped.+ */+ deferredHumanApproval?: AgentInstructionRequestHumanApprove;/** @deprecated Use `world.enableExpertise`. */enableExpertise?: boolean;error?: any;