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): let a stop request retire a dead running operation (#20055)

Arvin Xu38ca9acb60b0
diff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
index 196b0e4d775..5378ca6a2c4 100644
--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
@@ -2550,6 +2550,57 @@ describe('AgentRuntimeService', () => {
expect(mockCoordinator.markInterrupted).not.toHaveBeenCalled();
},
);
+
+ describe('running operation whose runtime state is gone', () => {
+ let settleStale: MockInstance<AgentOperationModel['settleStaleRunning']>;
+
+ beforeEach(() => {
+ mockCoordinator.loadAgentState.mockResolvedValue(null);
+ settleStale = vi.spyOn(AgentOperationModel.prototype, 'settleStaleRunning');
+ });
+
+ afterEach(() => {
+ settleStale.mockRestore();
+ });
+
+ it('retires a dead operation so the stop is confirmed', async () => {
+ const updatedAt = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
+ findOperation.mockResolvedValue({
+ id: 'op-dead',
+ status: 'running',
+ updatedAt,
+ } as Awaited<ReturnType<AgentOperationModel['findById']>>);
+ settleStale.mockResolvedValue(true);
+
+ expect(await service.interruptOperation('op-dead')).toBe(true);
+ expect(settleStale).toHaveBeenCalledWith('op-dead', expect.any(Date));
+ const staleBefore = settleStale.mock.calls[0][1];
+ expect(staleBefore.getTime()).toBeGreaterThan(updatedAt.getTime());
+ expect(staleBefore.getTime()).toBeLessThan(Date.now() - 5 * 60 * 1000);
+ });
+
+ it('keeps refusing while the lease is fresh', async () => {
+ findOperation.mockResolvedValue({
+ id: 'op-live',
+ status: 'running',
+ updatedAt: new Date(Date.now() - 60 * 1000),
+ } as Awaited<ReturnType<AgentOperationModel['findById']>>);
+
+ expect(await service.interruptOperation('op-live')).toBe(false);
+ expect(settleStale).not.toHaveBeenCalled();
+ });
+
+ it('keeps refusing when a heartbeat wins the retirement race', async () => {
+ findOperation.mockResolvedValue({
+ id: 'op-race',
+ status: 'running',
+ updatedAt: new Date(Date.now() - 60 * 60 * 1000),
+ } as Awaited<ReturnType<AgentOperationModel['findById']>>);
+ settleStale.mockResolvedValue(false);
+
+ expect(await service.interruptOperation('op-race')).toBe(false);
+ });
+ });
});
// 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 243bf38ca82..184a83cd3e9 100644
--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
@@ -163,6 +163,12 @@ const LEGACY_INTERRUPT_STATE_POLL_INTERVAL_MS = 30_000;
const STEP_ABORT_POLL_MAX_BACKOFF = 8;
const STEP_LOCK_HEARTBEAT_MS = 30_000;
const DURABLE_LEASE_HEARTBEAT_EVERY_TICKS = 3;
+/**
+ * How long a `running` operation's durable lease may go unrefreshed, with its
+ * runtime state gone, before a stop request treats it as dead. Far above the
+ * heartbeat cadence (STEP_LOCK_HEARTBEAT_MS × DURABLE_LEASE_HEARTBEAT_EVERY_TICKS).
+ */
+const DEAD_OPERATION_LEASE_MS = 10 * 60 * 1000;
const EVAL_TOOL_FORWARDING_HOOK_ID = 'eval-tool-forwarding';
const INTERVENTION_LIFECYCLE_CHECKPOINT_KEY = '_agentInterventionLifecycle';
@@ -627,9 +633,26 @@ export class AgentRuntimeService {
// 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 (!operation) return false;
+ if (['done', 'error', 'interrupted', 'abandoned'].includes(operation.status)) return true;
+
+ // A `running` row with no runtime state whose durable lease stopped
+ // refreshing long ago has nothing executing it: the step heartbeat moves
+ // `updatedAt` every few minutes while a step is live. Without this, the
+ // row stays `running` forever and every stop request on its task is
+ // refused as unconfirmed. Retire it atomically — a concurrent heartbeat
+ // moves `updatedAt` past the cutoff and wins, keeping a live run alive.
+ if (
+ operation.status === 'running' &&
+ operation.updatedAt &&
+ new Date(operation.updatedAt).getTime() < Date.now() - DEAD_OPERATION_LEASE_MS
+ ) {
+ return this.agentOperationModel.settleStaleRunning(
+ operationId,
+ new Date(Date.now() - DEAD_OPERATION_LEASE_MS),
+ );
+ }
+ return false;
}
if (state.status === 'done' || state.status === 'error' || state.status === 'interrupted') {