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): stop re-warning long-dead concurrent runs (#22208)

Arvin Xu214fc514ad9b
diff --git a/apps/server/src/services/agentRuntime/StaleOperationReaper.ts b/apps/server/src/services/agentRuntime/StaleOperationReaper.ts
index 7c9833537f2..93cb9029d98 100644
--- a/apps/server/src/services/agentRuntime/StaleOperationReaper.ts
+++ b/apps/server/src/services/agentRuntime/StaleOperationReaper.ts
@@ -77,7 +77,8 @@ export interface ReapStaleOperationsResult {
* run was never asked to stop. Composer sends supersede the previous run, so
* such an overlap means a start path slipped past that and both runs are
* spending on the same conversation. An older run that already carries the
- * interrupt sentinel is only finishing its current step and is not counted.
+ * interrupt sentinel is only finishing its current step and is not counted,
+ * nor is a run whose lease froze past the hetero silence window (dead).
* Detection only: nothing is interrupted.
*/
concurrentForegroundTopics: number;
@@ -175,7 +176,8 @@ export class StaleOperationReaper {
}
try {
- result.concurrentForegroundTopics = await this.reportConcurrentForegroundRuns();
+ result.concurrentForegroundTopics =
+ await this.reportConcurrentForegroundRuns(heteroStaleBefore);
} catch (e) {
log('concurrent foreground patrol failed: %O', e);
}
@@ -188,8 +190,15 @@ export class StaleOperationReaper {
* Warn about topics where more than one foreground run is live. Top-level
* runs only: sub-agent children (`parentOperationId`) and thread runs
* (`threadId`) legitimately run beside the topic's main run.
+ *
+ * Only runs whose lease moved within the hetero silence window count. A
+ * `running` row frozen longer than that is dead, not spending, and the
+ * recovery above never settles a dead row past its lookback: unbounded, the
+ * same months-old topics were re-reported on every once-a-minute sweep and
+ * made up the bulk of production warn logs. Every live run refreshes its
+ * lease well inside this window (90s heartbeat, or a hetero ingest batch).
*/
- private async reportConcurrentForegroundRuns(): Promise<number> {
+ private async reportConcurrentForegroundRuns(liveSince: Date): Promise<number> {
const rows = await this.db
.select({
operationIds: sql<
@@ -204,6 +213,7 @@ export class StaleOperationReaper {
isNotNull(agentOperations.topicId),
isNull(agentOperations.threadId),
isNull(agentOperations.parentOperationId),
+ gte(agentOperations.updatedAt, liveSince),
or(
isNull(agentOperations.trigger),
notInArray(agentOperations.trigger, [...BACKGROUND_OPERATION_TRIGGERS]),
diff --git a/apps/server/src/services/agentRuntime/__tests__/StaleOperationReaper.test.ts b/apps/server/src/services/agentRuntime/__tests__/StaleOperationReaper.test.ts
index a225188593a..e9e603c31c4 100644
--- a/apps/server/src/services/agentRuntime/__tests__/StaleOperationReaper.test.ts
+++ b/apps/server/src/services/agentRuntime/__tests__/StaleOperationReaper.test.ts
@@ -400,6 +400,26 @@ describe('StaleOperationReaper', () => {
warn.mockRestore();
});
+ it('only counts foreground runs whose lease moved within the hetero silence window', async () => {
+ // Production re-warned the same months-old dead `running` rows on every
+ // once-a-minute sweep: nothing settles them, and the patrol had no floor.
+ const before = Date.now();
+ const reaper = new StaleOperationReaper(buildDb([]), {
+ coordinator: buildCoordinator(null) as any,
+ queueService: buildQueue() as any,
+ });
+
+ await reaper.sweep();
+
+ const { params, sql } = new PgDialect().sqlToQuery(whereConditions[1]);
+ expect(sql).toMatch(/"updated_at" >= \$\d+/);
+ const floor = params.find(
+ (p) => typeof p === 'string' && new Date(p).getTime() <= before - 59 * 60_000,
+ );
+ expect(floor).toBeDefined();
+ expect(new Date(floor as string).getTime()).toBeGreaterThanOrEqual(before - 60 * 60_000 - 1000);
+ });
+
it('ignores an overlap whose older runs were already asked to stop', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const overlap = { operationIds: ['op_a', 'op_b'], topicId: 'tpc_x' };