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(desktop): keep follow-up turns when restart recovery replays a run (#19929)

YuTengjing7ba497db79ec
diff --git a/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.test.ts b/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.test.ts
index 6ae1231542a..cae4c15849d 100644
--- a/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.test.ts
+++ b/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.test.ts
@@ -537,6 +537,49 @@ describe('recoverInterruptedHeteroRuns', () => {
expect(mockRemoveMessages).not.toHaveBeenCalled();
});
+ it('keeps a follow-up turn chained onto the run tail instead of replaying over it', async () => {
+ // Claude Code SDK: the first turn stays open waiting on background tasks,
+ // so it never leaves the ledger, while the user sends and finishes a second
+ // turn whose user row hangs off the first turn's tail.
+ mockListInterruptedRuns.mockResolvedValue([{ ...run, assistantMessageId: 'a1' }]);
+ mockGetTopicDetail.mockResolvedValue({ ...topic, status: 'active' });
+ mockGetMessages.mockResolvedValue([
+ ...messages,
+ { content: 'follow up', createdAt: 300, id: 'u2', parentId: 't1', role: 'user' },
+ { content: 'ok', createdAt: 310, id: 'a2', parentId: 'u2', role: 'assistant' },
+ ]);
+
+ const results = await recoverInterruptedHeteroRuns();
+
+ expect(results).toEqual([
+ { outcome: 'skipped', reason: 'topic-taken-over', topicId: 'topic-1' },
+ ]);
+ expect(mockRemoveMessages).not.toHaveBeenCalled();
+ expect(mockRunHetero).not.toHaveBeenCalled();
+ expect(mockReleaseInterruptedRun).toHaveBeenCalledWith('ipc-1');
+ });
+
+ it('keeps a follow-up turn even when the stuck run wrote a row after it', async () => {
+ // The open SDK run finishes its background task after the follow-up turn
+ // answered, so its own late row is the newest on the topic.
+ mockListInterruptedRuns.mockResolvedValue([{ ...run, assistantMessageId: 'a1' }]);
+ mockGetTopicDetail.mockResolvedValue({ ...topic, status: 'active' });
+ mockGetMessages.mockResolvedValue([
+ ...messages,
+ { content: 'follow up', createdAt: 300, id: 'u2', parentId: 't1', role: 'user' },
+ { content: 'ok', createdAt: 310, id: 'a2', parentId: 'u2', role: 'assistant' },
+ { content: 'late', createdAt: 400, id: 'a3', parentId: 't1', role: 'assistant' },
+ ]);
+
+ const results = await recoverInterruptedHeteroRuns();
+
+ expect(results).toEqual([
+ { outcome: 'skipped', reason: 'topic-taken-over', topicId: 'topic-1' },
+ ]);
+ expect(mockRemoveMessages).not.toHaveBeenCalled();
+ expect(mockRunHetero).not.toHaveBeenCalled();
+ });
+
it('leaves a topic alone when a newer turn took it over while the app was down', async () => {
// Another device started a turn after our run was spawned: its user row is
// newer than the ledger entry. Touching it would delete that live run's
diff --git a/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.ts b/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.ts
index 919db50042a..3f23d602685 100644
--- a/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.ts
+++ b/src/features/Electron/HeterogeneousAgent/RestartRecovery/recoverInterruptedRuns.ts
@@ -86,6 +86,14 @@ const toTime = (value: UIChatMessage['createdAt']): number => {
* The run's own branch: its assistant row plus everything hanging off it. A
* regenerated turn keeps several assistant branches under one user row, and
* only this one belongs to the interrupted run.
+ *
+ * Stops at user rows: a later turn hangs its user row off this run's tail, so
+ * walking past it would claim the follow-up conversation as the run's own β€”
+ * and the replay would delete it. A run never writes user rows of its own
+ * (queued messages are sent as a new turn once it ends), so every user row
+ * below the root is a later turn. This happens with the Claude Code SDK, whose
+ * turn can stay open waiting on background tasks while the user sends the next
+ * one, leaving the earlier run on the ledger (LOBE-14379).
*/
const collectBranch = (messages: UIChatMessage[], rootId: string): string[] => {
const childrenByParent = new Map<string, UIChatMessage[]>();
@@ -104,7 +112,9 @@ const collectBranch = (messages: UIChatMessage[], rootId: string): string[] => {
if (seen.has(id)) continue;
seen.add(id);
ids.push(id);
- for (const child of childrenByParent.get(id) ?? []) queue.push(child.id);
+ for (const child of childrenByParent.get(id) ?? []) {
+ if (child.role !== 'user') queue.push(child.id);
+ }
}
return ids;
};
@@ -182,6 +192,15 @@ const recoverRun = async (run: InterruptedRun): Promise<RestartRecoveryResult> =
if (newest && !ownBranch.has(newest.id)) {
return { outcome: 'skipped', reason: 'topic-taken-over', topicId };
}
+ // The newest row alone is not enough: a run still open after the user sent
+ // the next turn (Claude Code SDK waiting on a background task) can write a
+ // late row into its own branch AFTER that turn finished. A user turn
+ // chained onto our branch means the conversation continued past this run β€”
+ // replaying would delete rows that turn is parented to (LOBE-14379).
+ const hasFollowUpTurn = mainChain.some(
+ (message) => message.role === 'user' && !!message.parentId && ownBranch.has(message.parentId),
+ );
+ if (hasFollowUpTurn) return { outcome: 'skipped', reason: 'topic-taken-over', topicId };
} else {
// Nothing of ours on the topic to anchor against. The spawn time is all
// that is left β€” a cross-clock comparison, so it is only trusted to spot a