Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(chat): 保留连续发送的 Harness 边界归属

Dash2099c9d5295d
diff --git a/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts b/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts
index 2366507682..6396aebdd8 100644
--- a/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts
+++ b/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts
@@ -89,6 +89,22 @@ describe('desktop remote history uses the shared live-to-history handoff', () =>
.toEqual(['user', 'sent', 'reply', 'switch', 'next']);
});
+ it.each([false, true])('keeps consecutive switches with their sends without a reply (next echoed=%s)', (echoed) => {
+ const previous = row('user');
+ const firstSwitch = { ...row('switch-first'), systemCardType: 'agent-switch' as const };
+ const secondSwitch = { ...row('switch-next'), systemCardType: 'agent-switch' as const };
+ // The first DB echo has arrived, but history has not retired its reservation.
+ const first = reserveRemoteUser({ ...row('first'), role: 'user' as const }, [previous]);
+ const next = reserveRemoteUser({ ...row('next'), role: 'user' as const,
+ isPendingPersist: !echoed }, [previous, firstSwitch, first]);
+ const raw = [previous, firstSwitch, first, secondSwitch, next];
+ const expected = ['user', 'switch-first', 'first', 'switch-next', 'next'];
+ expect(projectRemoteUsers(raw).map((message) => message.clientId)).toEqual(expected);
+ expect(projectRemoteUsers(raw, new Set(['user', 'switch-first'])).map((message) => message.clientId)).toEqual(expected);
+ const confirmedIds = new Set(expected);
+ expect(projectRemoteUsers(confirmRemoteUsers(raw, confirmedIds), confirmedIds).map((message) => message.clientId)).toEqual(expected);
+ });
+
it('shows a list-pushed reply after a restored snapshot until the page read covers it', async () => {
const { view, render, setSource } = fixture();
const reply = { ...row('reply'), content: 'prefetched reply', createdAt: '2026-09-08T00:00:09Z' };
diff --git a/apps/desktop/src/renderer/lib/remoteUserHandoff.ts b/apps/desktop/src/renderer/lib/remoteUserHandoff.ts
index bdaf51c33b..db71c53162 100644
--- a/apps/desktop/src/renderer/lib/remoteUserHandoff.ts
+++ b/apps/desktop/src/renderer/lib/remoteUserHandoff.ts
@@ -23,14 +23,18 @@ export function projectRemoteUsers<T extends LocalUserHandoff>(
const historical = (row: T) => historyClientIds.has(row.clientId) && !row.localSendPrecedingClientIds;
const result = [...messages.filter(historical), ...messages.filter((row) => !historical(row))];
const reservations = messages.filter((row) => row.role === 'user'
- && (row.localSendPrecedingClientIds || row.isPendingPersist));
+ && (row.localSendPrecedingClientIds || row.isPendingPersist)).map((row) => ({
+ row,
+ // Local sends retain insertion order until their echo creates a reservation.
+ preceding: new Set(row.localSendPrecedingClientIds
+ ?? messages.slice(0, messages.indexOf(row)).map((message) => message.clientId)),
+ }));
// Place earlier sends first: the raw timestamp order may put a later send first.
- reservations.sort((a, b) => b.localSendPrecedingClientIds?.includes(a.clientId) ? -1
- : a.localSendPrecedingClientIds?.includes(b.clientId) ? 1 : 0);
- for (const row of reservations) {
- // Local sends retain insertion order until their echo creates a reservation.
- const preceding = new Set(row.localSendPrecedingClientIds
- ?? messages.slice(0, messages.indexOf(row)).map((message) => message.clientId));
+ reservations.sort((a, b) => b.preceding.has(a.row.clientId) ? -1
+ : a.preceding.has(b.row.clientId) ? 1 : 0);
+ for (let index = 0; index < reservations.length; index++) {
+ const { row, preceding } = reservations[index];
+ const nextSend = reservations[index + 1];
result.splice(
result.findIndex((message) => message.clientId === row.clientId),
1,
@@ -43,7 +47,10 @@ export function projectRemoteUsers<T extends LocalUserHandoff>(
}
// Switching is applied after enqueue but before this send. Its boundary
// belongs before the reserved bubble; never cross ordinary turn activity.
- while (result[after + 1]?.systemCardType === 'agent-switch') after++;
+ // A boundary not observed by the next send belongs to that later send,
+ // even when this row already has a DB echo and no reply has arrived yet.
+ while (result[after + 1]?.systemCardType === 'agent-switch'
+ && (!nextSend || nextSend.preceding.has(result[after + 1].clientId))) after++;
result.splice(after + 1, 0, row);
}
// Command cards never acquire a history row. Keep their slots even after a
diff --git a/apps/mobile/src/__tests__/sentMessageHandoff.test.ts b/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
index 16f1c58531..8cd1b6de04 100644
--- a/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
+++ b/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
@@ -73,6 +73,24 @@ describe('sent message handoff', () => {
.toEqual(['sent', 'reply', 'switch', 'next']);
});
+ it.each([false, true])('keeps consecutive switches with their sends without a reply (next echoed=%s)', (echoed) => {
+ const firstSwitch: RemoteMessage = { ...echo, id: 'switch-first', clientId: 'switch-first', role: 'agent_switch' };
+ const secondSwitch: RemoteMessage = { ...firstSwitch, id: 'switch-next', clientId: 'switch-next' };
+ const nextEcho: RemoteMessage = { ...echo, id: 'db-next', clientId: 'next' };
+ const first = reconcileOptimisticUserMessages(appendOptimisticUserMessage([], [], queued, 's'), [echo], none, none);
+ let slots = appendOptimisticUserMessage(first, [firstSwitch, echo], { ...queued, clientId: 'next' }, 's');
+ const raw = [firstSwitch, echo, secondSwitch, ...(echoed ? [nextEcho] : [])];
+ const expected = ['switch-first', 'sent', 'switch-next', 'next'];
+ expect(projectOptimisticUserMessages(raw, slots).map((message) => message.clientId)).toEqual(expected);
+ // A stale history page can still omit the first echo held by its reservation.
+ expect(projectOptimisticUserMessages(raw.filter((message) => message.clientId !== 'sent'), slots)
+ .map((message) => message.clientId)).toEqual(expected);
+ const history = [firstSwitch, echo, secondSwitch, nextEcho];
+ slots = reconcileOptimisticUserMessages(slots, history, none, new Set(expected));
+ expect(slots).toEqual([]);
+ expect(projectOptimisticUserMessages(history, slots).map((message) => message.clientId)).toEqual(expected);
+ });
+
it.each([false, true])('keeps a new send after history-only replies (echo=%s)', async (hasEcho) => {
const previousUser = { ...echo, id: 'previous-user', clientId: 'previous-user', createdAt: '2026-09-16T23:59:00Z' };
const previousReply: RemoteMessage = { ...echo, id: 'previous-reply', clientId: 'previous-reply', role: 'assistant', createdAt: '2026-09-16T23:59:10Z' };
diff --git a/apps/mobile/src/session/optimisticUserMessages.ts b/apps/mobile/src/session/optimisticUserMessages.ts
index 4dc6c3d8b9..8be6e63ddb 100644
--- a/apps/mobile/src/session/optimisticUserMessages.ts
+++ b/apps/mobile/src/session/optimisticUserMessages.ts
@@ -82,7 +82,9 @@ export function projectOptimisticUserMessages(
): readonly RemoteMessage[] {
if (optimistic.length === 0) return messages;
const result = [...messages];
- for (const entry of optimistic) {
+ for (let index = 0; index < optimistic.length; index++) {
+ const entry = optimistic[index];
+ const nextSend = optimistic[index + 1];
const echo = result.findIndex((message) => message.clientId === entry.message.clientId);
const message = echo < 0 ? entry.message : result.splice(echo, 1)[0];
let after = -1;
@@ -91,7 +93,10 @@ export function projectOptimisticUserMessages(
}
// Switching is applied after enqueue but before this send. Its boundary
// belongs before the reserved bubble; never cross ordinary turn activity.
- while (result[after + 1]?.role === 'agent_switch') after++;
+ // A boundary not observed by the next send belongs to that later send,
+ // even when this row already has a DB echo and no reply has arrived yet.
+ while (result[after + 1]?.role === 'agent_switch'
+ && (!nextSend || nextSend.precedingClientIds.has(result[after + 1].clientId))) after++;
result.splice(after + 1, 0, message);
}
return result;