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(goal): close a goal whose planning dispatch was refused its topic (#23033)

Rdmclin23318b7cbcf3c
diff --git a/apps/server/src/services/goal/manager.strandedTurn.test.ts b/apps/server/src/services/goal/manager.strandedTurn.test.ts
index cdc2bd6c80f..ba4f223a94c 100644
--- a/apps/server/src/services/goal/manager.strandedTurn.test.ts
+++ b/apps/server/src/services/goal/manager.strandedTurn.test.ts
@@ -16,6 +16,7 @@ import {
users,
} from '@/database/schemas';
import { AiAgentService } from '@/server/services/aiAgent';
+import { TopicStartReservationError } from '@/server/services/aiAgent/topicStartReservation';
import { GoalService } from './index';
import * as scheduler from './scheduler';
@@ -96,6 +97,34 @@ describe('GoalManagerService with a planning turn whose dispatch never wrote a r
expect((await service().close(goalId, 'achieved')).status).toBe('achieved');
});
+ // The planning topic was busy, so the dispatch was refused its reservation
+ // before the planning message or any operation was written β€” the call has
+ // returned, so the turn is over just like a dispatch that failed outright.
+ it('lets the owner close the goal once a dispatch refused its topic is past the turn timeout', async () => {
+ const goalId = await managedGoal();
+ vi.spyOn(AiAgentService.prototype, 'execAgent').mockRejectedValue(
+ new TopicStartReservationError('Topic tpc remained busy while starting operation x'),
+ );
+ await service().tick(goalId);
+ const receipt = (await model().findById(goalId))!.config!.managerState!;
+ expect(receipt.dispatchNeverStarted).toBe(true);
+ expect(receipt.consumed).toBeFalsy();
+
+ await editReceipt(goalId, { startedAt: hoursAgo(2) });
+ expect((await service().close(goalId, 'achieved')).status).toBe('achieved');
+ expect((await model().findById(goalId))!.config!.managerState!.consumed).toBe(true);
+ });
+
+ it('still refuses a dispatch refused its topic while it is inside the turn timeout', async () => {
+ const goalId = await managedGoal();
+ vi.spyOn(AiAgentService.prototype, 'execAgent').mockRejectedValue(
+ new TopicStartReservationError('Topic tpc remained busy while starting operation x'),
+ );
+ await service().tick(goalId);
+
+ await expect(service().close(goalId, 'achieved')).rejects.toThrow(/not closed/);
+ });
+
it('refuses to settle a turn whose dispatch may still be pending', async () => {
const goalId = await managedGoal();
vi.spyOn(AiAgentService.prototype, 'execAgent').mockImplementation(async (params) => {
diff --git a/apps/server/src/services/goal/manager/index.ts b/apps/server/src/services/goal/manager/index.ts
index bd2545a3bea..3927d453af4 100644
--- a/apps/server/src/services/goal/manager/index.ts
+++ b/apps/server/src/services/goal/manager/index.ts
@@ -157,9 +157,10 @@ export class GoalManagerService {
* interrupt β€” its dispatch failed before the run was written, e.g. a database
* timeout in `execAgent`. That person's decision settles it the way
* `confirmTurnExit` does once the dispatch is known to be over: it returned
- * without a run (`dispatchReturnedWithoutRun`) and the turn is past the turn
- * timeout, or no outcome was recorded and the turn is older than any request
- * could run. Otherwise such a Goal could never be closed from the product. A
+ * without a run (`dispatchReturnedWithoutRun`) or was refused its topic
+ * reservation (`dispatchNeverStarted`) and the turn is past the turn timeout,
+ * or no outcome was recorded and the turn is older than any request could
+ * run. Otherwise such a Goal could never be closed from the product. A
* turn that did get an operation id (or was adopted from a local run) may be
* live without a row, so it keeps refusing; should a dispatch still return
* late, `startTurn` stops its run.
@@ -180,7 +181,9 @@ export class GoalManagerService {
!state.operationId &&
!state.adopted &&
Date.now() - Date.parse(state.startedAt) >
- (state.dispatchReturnedWithoutRun ? TIMEOUT_MS : STRANDED_DISPATCH_MS)
+ (state.dispatchReturnedWithoutRun || state.dispatchNeverStarted
+ ? TIMEOUT_MS
+ : STRANDED_DISPATCH_MS)
) {
// Decide again under the goal row lock the dispatch records its
// operation id under: a straggling `execAgent` may have written its run