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): resume parked parent operation after a callAgent hetero child completes (#18668)

LiJianc2b1e945cfdf
diff --git a/apps/server/src/services/aiAgent/__tests__/execAgent.heteroFiles.test.ts b/apps/server/src/services/aiAgent/__tests__/execAgent.heteroFiles.test.ts
index 5ac238f9589..f945b62d2d9 100644
--- a/apps/server/src/services/aiAgent/__tests__/execAgent.heteroFiles.test.ts
+++ b/apps/server/src/services/aiAgent/__tests__/execAgent.heteroFiles.test.ts
@@ -1418,5 +1418,65 @@ describe('AiAgentService.execAgent - hetero early-exit file attachments', () =>
expect(mockExecuteToolCall).not.toHaveBeenCalled();
});
+
+ // Regression guard for LOBE-13477: a callAgent/callSubAgent-spawned hetero
+ // child (isolation-thread, no topicStartOwnerOperationId) must still get
+ // its userId/workspaceId written to the state-manager metadata store β€”
+ // subAgentCallback reads it to authorize resuming the parked parent
+ // operation. Without it, the completion webhook 401s, the parent is
+ // never resumed, and it stays parked until the inactivity watchdog
+ // abandons it ~10 minutes later.
+ it('persists userId/workspaceId metadata for a callAgent-spawned hetero child', async () => {
+ await service.execAgent({
+ agentId: 'agent-1',
+ appContext: { isolationThread: true, topicId: 'topic-1' },
+ parentOperationId: 'parent-operation',
+ prompt: 'do the task as a callAgent child',
+ } as any);
+
+ const call = mockCreateOperationMetadata.mock.calls.find(
+ ([, data]) => data.userId === 'test-user-id',
+ );
+ expect(call).toBeDefined();
+ // No topic-owner mirror target on this path β€” mirrorToOperationId must
+ // stay unset rather than being derived from parentOperationId.
+ expect(call?.[1]).not.toHaveProperty('mirrorToOperationId');
+ });
+
+ it('nests an isolation-thread child under the parent marker instead of claiming topic root', async () => {
+ // heteroIngest/heteroFinish resolve an operationId via
+ // topic.metadata.runningOperation (root or childOperations) β€” a plain
+ // updateMetadata() here would clobber the parent's own root marker
+ // instead of nesting under it.
+ await service.execAgent({
+ agentId: 'agent-1',
+ appContext: { isolationThread: true, topicId: 'topic-1' },
+ parentOperationId: 'parent-operation',
+ prompt: 'do the task as a callAgent child',
+ } as any);
+
+ expect(topicMock.appendRunningOperationChild).toHaveBeenCalledWith(
+ 'topic-1',
+ 'parent-operation',
+ expect.objectContaining({ operationId: expect.stringContaining('op_') }),
+ );
+ // Not claimed as the topic's own root marker.
+ expect(findRunningOpSeed()).toBeUndefined();
+ });
+
+ it('falls back to claiming the topic marker when the parent is not the current root (e.g. nested isolation chain)', async () => {
+ topicMock.appendRunningOperationChild.mockResolvedValueOnce(false);
+
+ await service.execAgent({
+ agentId: 'agent-1',
+ appContext: { isolationThread: true, topicId: 'topic-1' },
+ parentOperationId: 'parent-operation',
+ prompt: 'do the task as a callAgent child',
+ } as any);
+
+ // Otherwise this child would never be recognized by
+ // heteroIngest/heteroFinish at all.
+ expect(findRunningOpSeed()).toBeDefined();
+ });
});
});
diff --git a/apps/server/src/services/aiAgent/index.ts b/apps/server/src/services/aiAgent/index.ts
index ce841a3d98d..0538c859da3 100644
--- a/apps/server/src/services/aiAgent/index.ts
+++ b/apps/server/src/services/aiAgent/index.ts
@@ -2591,20 +2591,52 @@ export class AiAgentService {
userMessageId: userMessageRecord?.id ?? parentMessageId ?? '',
};
}
- } else {
+ } else if (appContext?.isolationThread && parentOperationId) {
+ // Isolation-thread children (callAgent / callSubAgent) run on the
+ // SPAWNER's topic and finish long before it does. heteroIngest and
+ // heteroFinish both require this child's operationId to resolve via
+ // topic.metadata.runningOperation (root or childOperations) β€” see
+ // the comment above childOperation β€” or every streamed batch is
+ // dropped as stale and the terminal onComplete hooks (including the
+ // callAgent resume bridge) never fire. Nest under the parent's own
+ // marker instead of claiming the topic-level root outright, so the
+ // parent's marker survives for the rest of its still-running turn.
+ const attachedToParent = await this.topicModel.appendRunningOperationChild(
+ topicId,
+ parentOperationId,
+ childOperation,
+ );
+ if (!attachedToParent) {
+ // Parent isn't (or is no longer) the topic's current root marker β€”
+ // e.g. a nested isolation chain, or the parent already settled.
+ // Fall back to claiming the marker directly so this child is still
+ // discoverable by its own operationId, rather than permanently
+ // unrecognized by heteroIngest/heteroFinish.
+ await this.topicModel.updateMetadata(topicId, { runningOperation: childOperation });
+ }
+ } else if (!appContext?.isolationThread) {
await this.topicModel.updateMetadata(topicId, { runningOperation: childOperation });
}
- const persistMirrorTarget = async () => {
- if (!params.topicStartOwnerOperationId) return;
+ // Always persist operation metadata (userId/workspaceId) to the state
+ // manager, not just for topic-owner-mirrored runs. `subAgentCallback`
+ // (the QStash-delivered completion bridge for callAgent/callSubAgent
+ // children) resolves `userId` from this same store to authorize
+ // resuming the parent β€” without it, a hetero child spawned via
+ // callAgent has no metadata row, the callback 401s, and the parent
+ // operation is never resumed (stays parked until the inactivity
+ // watchdog abandons it).
+ const persistOperationMetadata = async () => {
try {
await createAgentStateManager().createOperationMetadata(operationId, {
- mirrorToOperationId: params.topicStartOwnerOperationId,
+ ...(params.topicStartOwnerOperationId && {
+ mirrorToOperationId: params.topicStartOwnerOperationId,
+ }),
userId: this.userId,
workspaceId: this.workspaceId,
});
} catch (err) {
- log('execAgent: failed to persist hetero mirror target: %O', err);
+ log('execAgent: failed to persist hetero operation metadata: %O', err);
}
};
@@ -2698,7 +2730,7 @@ export class AiAgentService {
// Open the stream channel so the gateway WS subscription can receive
// notify_update events published by agentNotify.notify.
- await persistMirrorTarget();
+ await persistOperationMetadata();
const streamManager = createStreamEventManager();
await streamManager
.publishAgentRuntimeInit(operationId, {
@@ -2801,7 +2833,7 @@ export class AiAgentService {
// the init only powers reconnect, not the run. `createStreamEventManager`
// probes Redis synchronously, so guard construction too, not just publish.
try {
- await persistMirrorTarget();
+ await persistOperationMetadata();
await createStreamEventManager().publishAgentRuntimeInit(operationId, {
agentId: resolvedAgentId,
assistantMessageId: assistantMessageRecord.id,