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 切换提示排在本次消息之前

Dash37e9985d882e
diff --git a/apps/desktop/src/renderer/__tests__/localHistoryViewUserEcho.test.ts b/apps/desktop/src/renderer/__tests__/localHistoryViewUserEcho.test.ts
index 8ca1d26139..470736e55e 100644
--- a/apps/desktop/src/renderer/__tests__/localHistoryViewUserEcho.test.ts
+++ b/apps/desktop/src/renderer/__tests__/localHistoryViewUserEcho.test.ts
@@ -229,6 +229,23 @@ describe('local history view keeps command cards', () => {
});
describe('local history view keeps user rows through the DB echo', () => {
+ it('keeps a harness boundary before a local pending send and its durable echo', async () => {
+ const s = sid();
+ await openSession(s);
+ await makerChatStore.sendMessage(s, 'new question', 'claude', '', 'default', WD);
+ const item = enqueue.mock.calls[0][1];
+ persist(dbMessage(s, 'switch', JSON.stringify({ fromAgentKind: 'cc', toAgentKind: 'codex' }),
+ new Date(Date.now() + 1000).toISOString(), 'agent_switch'));
+ await getRemoteHistoryView(s)!.refresh();
+ const expected = ['old-user', 'old-answer', 'switch', item.clientId];
+ expect(renderedClientIds(s)).toEqual(expected);
+ persistQueued(s, item, new Date(Date.now() + 2000).toISOString());
+ expect(renderedClientIds(s)).toEqual(expected);
+ await getRemoteHistoryView(s)!.refresh();
+ expect(renderedClientIds(s)).toEqual(expected);
+ makerChatStore.purgeSession(s);
+ });
+
it('keeps an ordinary send visible between the echo and the history refresh', async () => {
const s = sid();
await openSession(s);
diff --git a/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts b/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts
index b62c9165ab..2366507682 100644
--- a/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts
+++ b/apps/desktop/src/renderer/__tests__/remoteHistoryHandoff.test.ts
@@ -53,6 +53,42 @@ function fixture() {
}
describe('desktop remote history uses the shared live-to-history handoff', () => {
+ it.each([false, true])('places a late harness boundary before the reserved send (history ready=%s)', async (ready) => {
+ const { view, render, setSource } = fixture();
+ if (ready) await view.refresh();
+ const previous = row('user');
+ const sent = reserveRemoteUser({ ...row('sent'), role: 'user' as const, isPendingPersist: true }, [previous]);
+ const boundary: HistoryChatMessage = { ...row('switch'), systemCardType: 'agent-switch',
+ createdAt: '2026-09-08T00:00:02Z' };
+ const raw = [previous, sent, boundary];
+ const ids = () => render(raw).items.filter((item) => item.type === 'message').map((item) => item.message.clientId);
+ // Local Desktop sends have no explicit reservation until their DB echo.
+ const { localSendPrecedingClientIds: _reservation, ...localPending } = sent;
+ expect(projectRemoteUsers([previous, localPending, boundary]).map((message) => message.clientId))
+ .toEqual(['user', 'switch', 'sent']);
+ expect(projectRemoteUsers(raw).map((message) => message.clientId)).toEqual(['user', 'switch', 'sent']);
+ if (!ready) expect(ids()).toEqual(['user', 'switch', 'sent']);
+ setSource([previous, boundary]);
+ await view.refresh();
+ expect(ids()).toEqual(['user', 'switch', 'sent']);
+ raw[1] = { ...sent, id: 'db-sent', isPendingPersist: undefined };
+ expect(ids()).toEqual(['user', 'switch', 'sent']);
+ setSource([previous, boundary, raw[1]]);
+ await view.refresh();
+ expect(ids()).toEqual(['user', 'switch', 'sent']);
+ view.setActive(false);
+ });
+
+ it('does not pull a later harness switch across a reply into an earlier send', () => {
+ const previous = row('user');
+ const sent = reserveRemoteUser({ ...row('sent'), role: 'user' as const }, [previous]);
+ const reply = row('reply');
+ const boundary = { ...row('switch'), systemCardType: 'agent-switch' as const };
+ const next = reserveRemoteUser({ ...row('next'), role: 'user' as const }, [previous, sent, reply]);
+ expect(projectRemoteUsers([previous, sent, reply, next, boundary]).map((message) => message.clientId))
+ .toEqual(['user', 'sent', 'reply', 'switch', 'next']);
+ });
+
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 c5d63d1336..bdaf51c33b 100644
--- a/apps/desktop/src/renderer/lib/remoteUserHandoff.ts
+++ b/apps/desktop/src/renderer/lib/remoteUserHandoff.ts
@@ -2,6 +2,8 @@
export interface LocalUserHandoff {
clientId: string;
role: string;
+ systemCardType?: string;
+ isPendingPersist?: boolean;
localSendPrecedingClientIds?: readonly string[];
isLocalSystemCard?: boolean;
}
@@ -20,12 +22,15 @@ export function projectRemoteUsers<T extends LocalUserHandoff>(
// after history confirms the last local user and retires its reservation.
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);
+ const reservations = messages.filter((row) => row.role === 'user'
+ && (row.localSendPrecedingClientIds || row.isPendingPersist));
// 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);
+ reservations.sort((a, b) => b.localSendPrecedingClientIds?.includes(a.clientId) ? -1
+ : a.localSendPrecedingClientIds?.includes(b.clientId) ? 1 : 0);
for (const row of reservations) {
- const preceding = new Set(row.localSendPrecedingClientIds);
+ // 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));
result.splice(
result.findIndex((message) => message.clientId === row.clientId),
1,
@@ -36,6 +41,9 @@ export function projectRemoteUsers<T extends LocalUserHandoff>(
// arrived. The whole authoritative prefix is still before this local send.
if (preceding.has(result[index].clientId) || historical(result[index])) after = index;
}
+ // 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++;
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 32e599d515..16f1c58531 100644
--- a/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
+++ b/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
@@ -23,6 +23,56 @@ const queueInput = (current: readonly QueuedRemoteMessage[] = [], overrides: Par
});
describe('sent message handoff', () => {
+ it.each([false, true])('places a late harness boundary before the reserved send (history ready=%s)', async (ready) => {
+ const previous: RemoteMessage = { ...echo, id: 'previous', clientId: 'previous', role: 'assistant',
+ createdAt: '2026-09-16T23:59:00Z' };
+ const boundary: RemoteMessage = { ...echo, id: 'switch', clientId: 'switch', role: 'agent_switch',
+ content: { fromAgentKind: 'cc', toAgentKind: 'codex' }, createdAt: '2026-09-16T23:59:59Z' };
+ let history = [previous];
+ const view = new HistoryViewController<RemoteMessage>({
+ page: async () => ({ version: 1, items: projectHistoryView(history, false), hasMore: false, nextCursor: null }),
+ details: async () => ({ version: 1, messages: [], hasMore: false, nextCursor: null }),
+ expanded: async () => undefined,
+ });
+ if (ready) await view.refresh();
+ let slots = appendOptimisticUserMessage([], [previous], queued, 's');
+ const raw = [previous, boundary];
+ const handoff = new HistoryViewHandoff<RemoteMessage>(() => false);
+ const render = () => {
+ const snapshot = view.getSnapshot();
+ const state = handoff.reconcile(snapshot, raw);
+ slots = reconcileOptimisticUserMessages(slots, raw, new Set(['sent']), confirmedHistoryUserClientIds(snapshot, raw));
+ const messages = projectOptimisticUserMessages(state.messages, slots);
+ return (snapshot.ready ? buildMobileHistoryRenderItems({ view, snapshot, messages,
+ pendingHandoff: state.pending, staleTail: state.tail,
+ localUserClientIds: new Set(slots.map((entry) => entry.message.clientId)), streaming: false, sessionId: 's' })
+ : buildMobileMessageRenderItems(messages, { preserveSourceOrder: true })).map((item) => item.key);
+ };
+ const expected = ['message-previous', 'message-switch', 'message-sent'];
+ expect(projectOptimisticUserMessages(raw, slots).map((message) => message.clientId))
+ .toEqual(['previous', 'switch', 'sent']);
+ if (!ready) expect(render()).toEqual(expected);
+ history = [previous, boundary];
+ await view.refresh();
+ expect(render()).toEqual(expected);
+ raw.push(echo);
+ expect(render()).toEqual(expected);
+ history = [...raw];
+ await view.refresh();
+ expect(render()).toEqual(expected);
+ expect(slots).toEqual([]);
+ view.setActive(false);
+ });
+
+ it('does not pull a later harness switch across a reply into an earlier send', () => {
+ const reply: RemoteMessage = { ...echo, id: 'reply', clientId: 'reply', role: 'assistant' };
+ const boundary: RemoteMessage = { ...echo, id: 'switch', clientId: 'switch', role: 'agent_switch' };
+ const first = appendOptimisticUserMessage([], [], queued, 's');
+ const slots = appendOptimisticUserMessage(first, [echo, reply], { ...queued, clientId: 'next' }, 's');
+ expect(projectOptimisticUserMessages([echo, reply, boundary], slots).map((message) => message.clientId))
+ .toEqual(['sent', 'reply', 'switch', 'next']);
+ });
+
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 5f37c51eac..4dc6c3d8b9 100644
--- a/apps/mobile/src/session/optimisticUserMessages.ts
+++ b/apps/mobile/src/session/optimisticUserMessages.ts
@@ -89,6 +89,9 @@ export function projectOptimisticUserMessages(
for (let index = 0; index < result.length; index++) {
if (entry.precedingClientIds.has(result[index].clientId)) after = index;
}
+ // 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++;
result.splice(after + 1, 0, message);
}
return result;