Skip to content

🤯 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.

🐛 fix(agent-runtime): carry persisted assistant id into state.messages (#13841)

Arvin Xuf99935e9928a
diff --git a/src/server/modules/AgentRuntime/RuntimeExecutors.ts b/src/server/modules/AgentRuntime/RuntimeExecutors.ts
index a97c5d54ffc..b2261ae357c 100644
--- a/src/server/modules/AgentRuntime/RuntimeExecutors.ts
+++ b/src/server/modules/AgentRuntime/RuntimeExecutors.ts
@@ -964,8 +964,16 @@ export const createRuntimeExecutors = (
// ===== 2. Then accumulate to AgentState =====
const newState = structuredClone(state);
+ // Carry the persisted DB id so downstream executors (notably
+ // `request_human_approve`) can look up the parent assistant from
+ // `state.messages` without an extra DB round-trip. Without the id
+ // the lookup at `request_human_approve` (which filters on `m.id`)
+ // falls through to a DB query; when human-approve fires on the
+ // fresh LLM turn, both code paths miss and the op errors with
+ // "No assistant message found as parent for pending tool messages".
newState.messages.push({
content,
+ id: assistantMessageItem.id,
reasoning: finalReasoning,
role: 'assistant',
tool_calls: tool_calls.length > 0 ? tool_calls : undefined,
diff --git a/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts b/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
index a2527e7a220..4a1e58ffeec 100644
--- a/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
+++ b/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
@@ -361,6 +361,7 @@ describe('RuntimeExecutors', () => {
expect(result.newState.messages.at(-1)).toEqual(
expect.objectContaining({
+ id: 'msg-123',
reasoning: { content: 'Need to inspect the search results first.' },
role: 'assistant',
tool_calls: [expect.objectContaining({ id: 'call_1' })],
@@ -368,6 +369,52 @@ describe('RuntimeExecutors', () => {
);
});
+ it('should push assistant message with persisted DB id so request_human_approve can find parent', async () => {
+ const toolCallPayload = [
+ {
+ function: { arguments: '{}', name: 'search' },
+ id: 'call_sensitive',
+ type: 'function',
+ },
+ ];
+
+ const mockChat = vi.fn().mockImplementation(async (_payload, options) => {
+ await options?.callback?.onToolsCalling?.({ toolsCalling: toolCallPayload });
+ await options?.callback?.onCompletion?.({
+ usage: { totalInputTokens: 1, totalOutputTokens: 2, totalTokens: 3 },
+ });
+ return new Response('done');
+ });
+ vi.mocked(initModelRuntimeFromDB).mockResolvedValueOnce({ chat: mockChat } as any);
+
+ mockMessageModel.create.mockResolvedValueOnce({ id: 'persisted-assistant-id' });
+
+ const executors = createRuntimeExecutors(ctx);
+ const state = createMockState();
+
+ const result = await executors.call_llm!(
+ {
+ payload: {
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'gpt-4',
+ provider: 'openai',
+ tools: [],
+ },
+ type: 'call_llm' as const,
+ },
+ state,
+ );
+
+ const lastAssistant = result.newState.messages.at(-1);
+ expect(lastAssistant).toMatchObject({
+ id: 'persisted-assistant-id',
+ role: 'assistant',
+ });
+ // The id must match the same message that nextContext exposes as
+ // parentMessageId, so request_human_approve sees a single source of truth.
+ expect((result.nextContext?.payload as any).parentMessageId).toBe('persisted-assistant-id');
+ });
+
it('should execute compress_context and return compression_result', async () => {
const mockChat = vi.fn().mockImplementation(async (_payload, options) => {
await options?.callback?.onText?.('summary');