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(aiAgent): supersede a live agent_signal run when the composer sends (#20396)

Arvin Xuf6be2b8a7b6b
diff --git a/apps/server/src/services/agentRuntime/foregroundOperation.ts b/apps/server/src/services/agentRuntime/foregroundOperation.ts
index 82d256bf978..1179ec14948 100644
--- a/apps/server/src/services/agentRuntime/foregroundOperation.ts
+++ b/apps/server/src/services/agentRuntime/foregroundOperation.ts
@@ -25,6 +25,21 @@ export const BACKGROUND_OPERATION_TRIGGERS: ReadonlySet<string> = new Set<string
export const isForegroundOperationTrigger = (trigger: string | null | undefined): boolean =>
!trigger || !BACKGROUND_OPERATION_TRIGGERS.has(trigger);
+/**
+ * Whether a composer send should retire this run when it still owns the
+ * topic's `runningOperation` marker.
+ *
+ * Foreground runs, plus Agent Signal runs: a run that holds the main-spine
+ * marker with that trigger (e.g. the creator wakeup that processes task
+ * results) continues the user's own conversation, yet the server started it,
+ * so the client has no local op to queue behind. Leaving it live next to the
+ * send forks the spine, and the user's message ends up on a branch the
+ * conversation never shows. Agent Signal runs on isolation threads never own
+ * the marker, so they are unaffected.
+ */
+export const isComposerSupersedableTrigger = (trigger: string | null | undefined): boolean =>
+ isForegroundOperationTrigger(trigger) || trigger === RequestTrigger.AgentSignal;
+
/**
* How a composer send found the topic's previous foreground run still `running`.
*
diff --git a/apps/server/src/services/aiAgent/__tests__/execAgent.supersedeRunningOperation.test.ts b/apps/server/src/services/aiAgent/__tests__/execAgent.supersedeRunningOperation.test.ts
index 986d1d9cfb9..fdfa5665209 100644
--- a/apps/server/src/services/aiAgent/__tests__/execAgent.supersedeRunningOperation.test.ts
+++ b/apps/server/src/services/aiAgent/__tests__/execAgent.supersedeRunningOperation.test.ts
@@ -325,6 +325,33 @@ describe('AiAgentService.execAgent - supersede running foreground operation', ()
expect(interruptTask).toHaveBeenCalledWith({ operationId: 'op-live', topicId: 'topic-1' });
});
+ // A task-result wakeup runs on the main spine with `agent_signal`. The client
+ // never started it, so it cannot queue the send behind it; leaving both live
+ // forked the conversation and hid the user's message on the losing branch.
+ it('interrupts a running agent_signal holder on the main spine', async () => {
+ mockFindOperationById.mockResolvedValue({
+ id: 'op-live',
+ status: 'running',
+ topicId: 'topic-1',
+ trigger: 'agent_signal',
+ });
+
+ await service.execAgent({
+ agentId: 'agent-1',
+ appContext: { topicId: 'topic-1' },
+ interactiveStart: true,
+ prompt: 'please hurry',
+ });
+
+ expect(interruptTask).toHaveBeenCalledWith({ operationId: 'op-live', topicId: 'topic-1' });
+ expect(mockMergeMetadata).toHaveBeenCalledWith(
+ expect.any(String),
+ expect.objectContaining({
+ supersede: expect.objectContaining({ supersededOperationId: 'op-live' }),
+ }),
+ );
+ });
+
it('leaves a device-hosted heterogeneous holder to the replacement path', async () => {
mockFindTopicById.mockResolvedValue({
id: 'topic-1',
diff --git a/apps/server/src/services/aiAgent/index.ts b/apps/server/src/services/aiAgent/index.ts
index 598c62c5c8f..35da99bed6e 100644
--- a/apps/server/src/services/aiAgent/index.ts
+++ b/apps/server/src/services/aiAgent/index.ts
@@ -46,7 +46,7 @@ import { AgentRuntimeService } from '@/server/services/agentRuntime';
import { getAbortError, throwIfAborted } from '@/server/services/agentRuntime/abort';
// Imported from the module itself: tests mock the `agentRuntime` barrel.
import {
- isForegroundOperationTrigger,
+ isComposerSupersedableTrigger,
type SupersedeKind,
type SupersedeRecord,
} from '@/server/services/agentRuntime/foregroundOperation';
@@ -692,7 +692,9 @@ export class AiAgentService {
* keep the existing behavior:
* - a parked run (`waiting_for_human` etc.) — the send may be its answer;
* - a background producer's run (task, cron, bot, …), see
- * `BACKGROUND_OPERATION_TRIGGERS` in `agentRuntime/foregroundOperation`;
+ * `isComposerSupersedableTrigger` in `agentRuntime/foregroundOperation`.
+ * An Agent Signal run on the main spine (task-result wakeup) is retired
+ * like a foreground run: the client never tracks it, so it cannot queue;
* - a device-hosted Claude Code / Codex run: cancelling it waits up to 10s
* for the device, far past the reservation's ~3s retry budget, so a
* concurrent send would fail. Those settle through `replacesOperationId`
@@ -712,7 +714,7 @@ export class AiAgentService {
const holder = await this.agentOperationModel.findById(holderId);
if (holder?.status !== 'running') return;
- if (!isForegroundOperationTrigger(holder.trigger)) return;
+ if (!isComposerSupersedableTrigger(holder.trigger)) return;
// Read before interrupting: afterwards the sentinel is always set. The
// read is diagnostic only, so a failure must not fail the send.