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: allow stopping and deleting terminal task runs (#19688)

Arvin Xudde87d153453
diff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
index f68bd907ae7..b2878b24e99 100644
--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
@@ -2123,6 +2123,21 @@ describe('AgentRuntimeService', () => {
});
describe('interruptOperation', () => {
+ let findOperation: MockInstance<AgentOperationModel['findById']>;
+
+ beforeEach(() => {
+ findOperation = vi.spyOn(AgentOperationModel.prototype, 'findById');
+ mockDb.select.mockReturnValue({
+ from: vi.fn().mockReturnValue({
+ where: vi.fn().mockReturnValue({ limit: vi.fn().mockResolvedValue([]) }),
+ }),
+ });
+ });
+
+ afterEach(() => {
+ findOperation.mockRestore();
+ });
+
it('should interrupt a running operation', async () => {
mockCoordinator.loadAgentState.mockResolvedValue({
operationId: 'op-1',
@@ -2166,7 +2181,7 @@ describe('AgentRuntimeService', () => {
);
});
- it('should return false when state not found', async () => {
+ it('should return false when state and owned operation are not found', async () => {
mockCoordinator.loadAgentState.mockResolvedValue(null);
const result = await service.interruptOperation('non-existent');
@@ -2176,7 +2191,7 @@ describe('AgentRuntimeService', () => {
expect(mockCoordinator.markInterrupted).not.toHaveBeenCalled();
});
- it('should return false when operation already done', async () => {
+ it('should acknowledge cancellation when operation already done', async () => {
mockCoordinator.loadAgentState.mockResolvedValue({
operationId: 'op-done',
status: 'done',
@@ -2185,11 +2200,11 @@ describe('AgentRuntimeService', () => {
const result = await service.interruptOperation('op-done');
- expect(result).toBe(false);
+ expect(result).toBe(true);
expect(mockCoordinator.saveAgentState).not.toHaveBeenCalled();
});
- it('should return false when operation already in error state', async () => {
+ it('should acknowledge cancellation when operation already in error state', async () => {
mockCoordinator.loadAgentState.mockResolvedValue({
operationId: 'op-err',
status: 'error',
@@ -2198,11 +2213,11 @@ describe('AgentRuntimeService', () => {
const result = await service.interruptOperation('op-err');
- expect(result).toBe(false);
+ expect(result).toBe(true);
expect(mockCoordinator.saveAgentState).not.toHaveBeenCalled();
});
- it('should return false when operation already interrupted', async () => {
+ it('should acknowledge cancellation when operation already interrupted', async () => {
mockCoordinator.loadAgentState.mockResolvedValue({
operationId: 'op-int',
status: 'interrupted',
@@ -2211,9 +2226,37 @@ describe('AgentRuntimeService', () => {
const result = await service.interruptOperation('op-int');
- expect(result).toBe(false);
+ expect(result).toBe(true);
expect(mockCoordinator.saveAgentState).not.toHaveBeenCalled();
});
+
+ it.each(['done', 'error', 'interrupted', 'abandoned'] as const)(
+ 'acknowledges an expired runtime state when the owned operation is %s',
+ async (status) => {
+ mockCoordinator.loadAgentState.mockResolvedValue(null);
+ findOperation.mockResolvedValue({
+ id: 'op-expired',
+ status,
+ } as Awaited<ReturnType<AgentOperationModel['findById']>>);
+ expect(await service.interruptOperation('op-expired')).toBe(true);
+ expect(mockCoordinator.saveAgentState).not.toHaveBeenCalled();
+ expect(mockCoordinator.markInterrupted).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each(['idle', 'running', 'waiting_for_human', 'waiting_for_async_tool'] as const)(
+ 'does not treat missing runtime state as stopped when the owned operation is %s',
+ async (status) => {
+ mockCoordinator.loadAgentState.mockResolvedValue(null);
+ findOperation.mockResolvedValue({
+ id: 'op-active',
+ status,
+ } as Awaited<ReturnType<AgentOperationModel['findById']>>);
+ expect(await service.interruptOperation('op-active')).toBe(false);
+ expect(mockCoordinator.saveAgentState).not.toHaveBeenCalled();
+ expect(mockCoordinator.markInterrupted).not.toHaveBeenCalled();
+ },
+ );
});
// Stream events at step / operation boundaries should carry the canonical
diff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
index f387e4ac759..4fe62b14c08 100644
--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
@@ -597,14 +597,25 @@ export class AgentRuntimeService {
* The agent will stop at the next step boundary (cannot abort an in-flight LLM call).
* Works with both Redis and InMemory state managers via the coordinator abstraction.
*
- * @returns true if the operation was interrupted, false if already in a terminal state or not found
+ * @returns true if interruption is acknowledged (including an already terminal operation),
+ * false if missing runtime state prevents confirming that the operation has stopped.
*/
async interruptOperation(operationId: string): Promise<boolean> {
const state = await this.coordinator.loadAgentState(operationId);
- if (!state) return false;
+ if (!state) {
+ // Runtime state can expire before the task-topic completion callback lands.
+ // Absence alone is not proof of exit (e.g. another in-memory worker owns
+ // the run). Only an owned, durably terminal operation can acknowledge it.
+ const operation = await this.agentOperationModel.findById(operationId);
+ return (
+ !!operation && ['done', 'error', 'interrupted', 'abandoned'].includes(operation.status)
+ );
+ }
if (state.status === 'done' || state.status === 'error' || state.status === 'interrupted') {
- return false;
+ // A retried stop must be able to settle a stale running task-topic without
+ // overwriting the original completion outcome or emitting another stop.
+ return true;
}
// Sentinel FIRST: the poller and the step-boundary check read only the