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(hetero): return undelivered steer messages when a run is stopped (#22824)

Rdmclin294e025109958
diff --git a/apps/server/src/services/aiAgent/intervention/InterventionController.test.ts b/apps/server/src/services/aiAgent/intervention/InterventionController.test.ts
new file mode 100644
index 00000000000..b7ef461ae16
--- /dev/null
+++ b/apps/server/src/services/aiAgent/intervention/InterventionController.test.ts
@@ -0,0 +1,103 @@
+// @vitest-environment node
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const { executeToolCall, releaseHeteroSteers, settleAgentRunFee } = vi.hoisted(() => ({
+ executeToolCall: vi.fn(),
+ releaseHeteroSteers: vi.fn(async () => {}),
+ settleAgentRunFee: vi.fn(async () => {}),
+}));
+
+vi.mock('@/server/services/deviceGateway', () => ({ deviceGateway: { executeToolCall } }));
+vi.mock('@/business/server/agent-run/runFee', () => ({ settleAgentRunFee }));
+vi.mock('../steerQueue/hetero', () => ({ releaseHeteroSteers }));
+
+// eslint-disable-next-line import/first
+import { InterventionController } from './InterventionController';
+
+const OPERATION_ID = 'op_running';
+const TOPIC_ID = 'tpc_topic';
+
+const createController = () => {
+ const deps = {
+ agentOperationModel: {
+ findById: vi.fn(async () => ({ createdAt: new Date(), id: OPERATION_ID })),
+ settleRunning: vi.fn(async () => true),
+ },
+ agentRuntimeService: { interruptOperation: vi.fn(async () => false) },
+ db: {},
+ messageModel: {},
+ resolveDeviceWorkspaceId: vi.fn(async () => undefined),
+ threadModel: { findById: vi.fn(), update: vi.fn() },
+ topicModel: {
+ findById: vi.fn(async () => ({
+ id: TOPIC_ID,
+ metadata: {
+ runningOperation: {
+ deviceId: 'device_1',
+ heteroType: 'claude-code',
+ operationId: OPERATION_ID,
+ },
+ },
+ })),
+ settleRunningOperation: vi.fn(async () => true),
+ },
+ userId: 'user_1',
+ };
+
+ return { controller: new InterventionController(deps as any), deps };
+};
+
+describe('InterventionController.interruptTask', () => {
+ beforeEach(() => {
+ executeToolCall.mockReset();
+ releaseHeteroSteers.mockReset();
+ releaseHeteroSteers.mockImplementation(async () => {});
+ settleAgentRunFee.mockClear();
+ });
+
+ it('returns the follow-ups a stopped device CLI run never picked up', async () => {
+ executeToolCall.mockResolvedValue({ state: { exited: true }, success: true });
+ const { controller, deps } = createController();
+
+ const result = await controller.interruptTask({
+ operationId: OPERATION_ID,
+ topicId: TOPIC_ID,
+ });
+
+ expect(result).toMatchObject({ deviceCancellationConfirmed: true, success: true });
+ expect(deps.agentOperationModel.settleRunning).toHaveBeenCalledWith(
+ OPERATION_ID,
+ 'interrupted',
+ );
+ expect(releaseHeteroSteers).toHaveBeenCalledWith(OPERATION_ID);
+ });
+
+ it('keeps the follow-ups held while the device has not confirmed the exit', async () => {
+ executeToolCall.mockResolvedValue({ state: { exited: false }, success: true });
+ const { controller } = createController();
+
+ const result = await controller.interruptTask({
+ operationId: OPERATION_ID,
+ topicId: TOPIC_ID,
+ });
+
+ expect(result).toMatchObject({ deviceCancellationConfirmed: false, success: false });
+ expect(releaseHeteroSteers).not.toHaveBeenCalled();
+ });
+
+ it('still confirms the stop when returning the follow-ups fails', async () => {
+ executeToolCall.mockResolvedValue({ state: { exited: true }, success: true });
+ releaseHeteroSteers.mockRejectedValue(new Error('redis down'));
+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
+ const { controller } = createController();
+
+ const result = await controller.interruptTask({
+ operationId: OPERATION_ID,
+ topicId: TOPIC_ID,
+ });
+
+ expect(result).toMatchObject({ deviceCancellationConfirmed: true, success: true });
+ expect(consoleError).toHaveBeenCalled();
+ consoleError.mockRestore();
+ });
+});
diff --git a/apps/server/src/services/aiAgent/intervention/InterventionController.ts b/apps/server/src/services/aiAgent/intervention/InterventionController.ts
index a1eeab703ee..ec8e4fe3273 100644
--- a/apps/server/src/services/aiAgent/intervention/InterventionController.ts
+++ b/apps/server/src/services/aiAgent/intervention/InterventionController.ts
@@ -22,6 +22,7 @@ import type { AgentRuntimeService } from '@/server/services/agentRuntime';
import { deviceGateway } from '@/server/services/deviceGateway';
import { STOPPED_TOOL_CONTENT } from '../helpers/agentFactory';
+import { releaseHeteroSteers } from '../steerQueue/hetero';
const log = debug('lobe-server:ai-agent-service');
@@ -263,6 +264,19 @@ export class InterventionController {
'active',
);
}
+ // For the same reason, nothing else returns the follow-ups this run had
+ // typed into its session but the agent never picked up. Left alone, they
+ // stay "being delivered" until their lease lapses: a "Send now" of one
+ // is refused as already sent, and every new message queues behind it
+ // with no run left to take it. The process has exited, and the device
+ // confirms an exit only after draining its terminal callback, so none of
+ // them can still be picked up; put them back in the queue now.
+ await releaseHeteroSteers(resolvedOperationId).catch((error) => {
+ console.error('[InterventionController] releaseHeteroSteers failed', {
+ error,
+ operationId: resolvedOperationId,
+ });
+ });
}
if (!interrupted && deviceCancellationConfirmed !== true) {