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): unblock planning turns stale-flagged by same-status graph churn (#20719)

Arvin Xu6ef8278cc86c
diff --git a/apps/server/src/services/goal/manager.test.ts b/apps/server/src/services/goal/manager.test.ts
index dd60ed7a41b..f7bb1ac485b 100644
--- a/apps/server/src/services/goal/manager.test.ts
+++ b/apps/server/src/services/goal/manager.test.ts
@@ -726,6 +726,19 @@ describe('CLI main Agent planning', () => {
expect((await service().graph(id)).nodes.filter((n) => n.kind === 'task')).toHaveLength(0);
});
+ it('keeps accepting submits when a background loop rewrites node rows without planning changes', async () => {
+ // A retry loop re-activating an already-active task node used to bump
+ // `goal_nodes.updated_at` between dispatch and submit; the manager hashed
+ // whole node rows, so every in-flight planning turn was rejected with
+ // "Stale planning input" no matter what the agent did.
+ const { id, state, op } = await start();
+ // No-op writes to the very rows the snapshot covers, mid-turn.
+ for (const node of (await service().graph(id)).nodes) {
+ await new GoalGraphModel(db, userId).updateNodeStatus(id, node.id, node.status, 'retry churn');
+ }
+ await expect(manager().submit(id, state.token, op.id, taskPlan)).resolves.toBeTruthy();
+ });
+
it('adopts a dispatch with a lost response rather than launching another Agent', async () => {
const original = vi.mocked(AiAgentService.prototype.execAgent).getMockImplementation()!;
vi.mocked(AiAgentService.prototype.execAgent).mockImplementation(async (params) => {
@@ -2346,4 +2359,4 @@ describe('decideFailedTurn', () => {
decideFailedTurn({ category: 'quota', body: { rateLimitInfo: { resetsAt: past } } }, {}, now),
).toMatchObject({ action: 'retry', charged: true });
});
-});
+});
\ No newline at end of file
diff --git a/apps/server/src/services/goal/manager.ts b/apps/server/src/services/goal/manager.ts
index 0ddf05a9fe4..29c0b3fbfb6 100644
--- a/apps/server/src/services/goal/manager.ts
+++ b/apps/server/src/services/goal/manager.ts
@@ -252,7 +252,16 @@ export const decideFailedTurn = (
*/
const managerTurnToken = (goalId: string) => `${goalId}_${randomUUID()}`;
-/** Excludes only the manager's own receipt. Concurrent policy/graph changes invalidate its plan. */
+/**
+ * Excludes only the manager's own receipt. Concurrent policy/graph changes
+ * invalidate its plan.
+ *
+ * Node rows are projected, not hashed whole: `updatedAt` is bookkeeping that a
+ * same-status rewrite bumps without changing any planning input, and hashing it
+ * made an in-flight planning turn go stale whenever a background loop rewrote a
+ * node it did not own (a coordinator retry re-activating an already-active task
+ * rejected every submit with "Stale planning input" until the turn expired).
+ */
export const managerSnapshot = (graph: GoalGraphSnapshot) => {
// Coordinator receipts are not planning input: arming a Task's quota wake while a
// turn runs must not make that turn's plan stale.
@@ -264,7 +273,7 @@ export const managerSnapshot = (graph: GoalGraphSnapshot) => {
config,
maxRounds: graph.goal.maxRounds,
maxTotalCost: graph.goal.maxTotalCost,
- nodes: graph.nodes,
+ nodes: graph.nodes.map(({ updatedAt: _updatedAt, ...planning }) => planning),
edges: graph.edges,
decisions: graph.decisions,
versions: graph.workVersions.map(({ nodeId, workVersionId, relation }) => ({
@@ -1517,4 +1526,4 @@ export class GoalManagerService {
);
return result;
};
-}
+}
\ No newline at end of file
diff --git a/packages/database/src/models/__tests__/goalGraph.test.ts b/packages/database/src/models/__tests__/goalGraph.test.ts
index 06ac3b4d76c..ff217f836a1 100644
--- a/packages/database/src/models/__tests__/goalGraph.test.ts
+++ b/packages/database/src/models/__tests__/goalGraph.test.ts
@@ -271,6 +271,24 @@ describe('GoalGraphModel', () => {
expect(await graphModel.getNodeStatus(goal.id, node!.id)).toBe('retired');
});
+ it('treats a same-status write as a no-op instead of churning the graph', async () => {
+ // Re-activating an already-active node must not bump `updatedAt` or append
+ // an event: a coordinator retry loop does this on every tick, and the
+ // planning manager hashes these rows — churn made every in-flight
+ // planning turn submit into a stale snapshot.
+ const goal = await goalModel.create({ subjectType: 'standalone', title: 'No churn' });
+ const node = await graphModel.createNode(goal.id, { kind: 'task', title: 'Looped' });
+ await graphModel.updateNodeStatus(goal.id, node!.id, 'active', 'first start');
+ const [before] = (await graphModel.getGraph(goal.id))!.nodes;
+ const eventsBefore = (await graphModel.getGraph(goal.id))!.events.length;
+
+ await graphModel.updateNodeStatus(goal.id, node!.id, 'active', 'retry loop tick');
+
+ const [after] = (await graphModel.getGraph(goal.id))!.nodes;
+ expect(after.updatedAt.getTime()).toBe(before.updatedAt.getTime());
+ expect((await graphModel.getGraph(goal.id))!.events.length).toBe(eventsBefore);
+ });
+
it('refuses to bind a task to a node that is not a task node', async () => {
// This used to be a CHECK constraint. It lives in `bindTask`'s WHERE now,
// so the rule needs a test on the write path or nothing enforces it.
@@ -497,4 +515,4 @@ it.skipIf(process.env.TEST_SERVER_DB !== '1')(
await writer;
}
},
-);
+);
\ No newline at end of file
diff --git a/packages/database/src/models/goalGraph.ts b/packages/database/src/models/goalGraph.ts
index 4f63c5bd2f8..7e58eff4465 100644
--- a/packages/database/src/models/goalGraph.ts
+++ b/packages/database/src/models/goalGraph.ts
@@ -744,6 +744,13 @@ export class GoalGraphModel {
and(
eq(goalNodes.goalId, goalId),
eq(goalNodes.id, nodeId),
+ // A write that does not change the status is a no-op: rewriting the
+ // same status would bump `updatedAt` and append a duplicate event
+ // for nothing. A coordinator retry loop re-activating an
+ // already-active node on every tick would then churn the graph
+ // until no in-flight planning turn could submit a plan (the
+ // manager's snapshot hash reads these rows).
+ ne(goalNodes.status, status),
// Retirement is a person's final word on a node. A coordinator tick
// that loaded the node before it was retired must not write it back
// to `resolved` / `active` afterwards; the goal row lock taken above
@@ -893,4 +900,4 @@ export class GoalGraphModel {
await settleDecisionBriefs(tx, decision.id, 'canceled', reason);
return decision;
});
-}
+}
\ No newline at end of file