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(history): 流式交接保留历史消息的权威顺序

Dashad2b0e63cd37
diff --git a/apps/mobile/src/__tests__/mobileHistoryHandoff.test.ts b/apps/mobile/src/__tests__/mobileHistoryHandoff.test.ts
index 3b3b98faba..14beb5883e 100644
--- a/apps/mobile/src/__tests__/mobileHistoryHandoff.test.ts
+++ b/apps/mobile/src/__tests__/mobileHistoryHandoff.test.ts
@@ -13,6 +13,38 @@ const row = (id: string, streaming = false): RemoteMessage => ({
const createHandoff = () => new HistoryViewHandoff<RemoteMessage>((row) => row.agentMeta?.isStreaming === true);
describe('mobile history handoff', () => {
+ it.each([true, false])('keeps text order when history timestamps arrive before live persistence (handoff=%s)', async (useHandoff) => {
+ const message = (id: string, second: number, streaming = false): RemoteMessage => ({
+ ...row(id, streaming), content: id,
+ createdAt: new Date(Date.UTC(2026, 8, 8, 0, 0, second)).toISOString(),
+ });
+ let history = [message('A', 0)];
+ 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,
+ });
+ await view.refresh();
+ const handoff = createHandoff();
+ const provisional = message('B', 0, true);
+ const render = (raw: RemoteMessage[]) => {
+ const state = useHandoff ? handoff.reconcile(view.getSnapshot(), raw)
+ : { messages: raw, pending: new Set<string>() };
+ return buildMobileHistoryRenderItems({ view, snapshot: view.getSnapshot(),
+ messages: state.messages, pendingHandoff: state.pending, streaming: true, sessionId: 's',
+ }).filter(item => item.type === 'message').map(item => item.message.source);
+ };
+ expect(render([message('A', 0), provisional]).map(item => item.clientId)).toEqual(['A', 'B']);
+ history = [message('A', 1), { ...message('B', 2), content: 'older history text', rowid: 42 }];
+ await view.refresh();
+ const during = render([message('A', 1), provisional]);
+ expect(during.map(item => item.clientId)).toEqual(['A', 'B']);
+ expect(during[1]).toMatchObject({ content: 'B', createdAt: history[1].createdAt, rowid: 42 });
+ expect(provisional.createdAt).toBe(message('B', 0).createdAt);
+ expect(history[1].content).toBe('older history text');
+ expect(render([message('A', 1), message('B', 2)]).map(item => item.clientId)).toEqual(['A', 'B']);
+ view.setActive(false);
+ });
it('retains text finalized before the first history page arrives, including an older provisional time anchor', async () => {
const view = new HistoryViewController<RemoteMessage>({
page: async () => ({ version: 1, items: projectHistoryView([row('user')], false), hasMore: false, nextCursor: null }),
diff --git a/packages/maker-shared/src/historyViewHandoff.ts b/packages/maker-shared/src/historyViewHandoff.ts
index 021f8e759b..b8ee4a8f2c 100644
--- a/packages/maker-shared/src/historyViewHandoff.ts
+++ b/packages/maker-shared/src/historyViewHandoff.ts
@@ -1,6 +1,12 @@
import { historyViewLeaves, type HistoryMessageSource } from './historyView.js';
import type { HistoryViewSnapshot } from './historyViewController.js';
+/** Live content may be newer, but its provisional timestamp must not replace known history order. */
+export function liveContentWithHistoryOrder<T extends HistoryMessageSource>(live: T, history: T): T {
+ if (live.createdAt === history.createdAt && live.rowid === history.rowid) return live;
+ return { ...live, createdAt: history.createdAt, rowid: history.rowid };
+}
+
/** View-local identities only; message bodies remain in the existing raw store.
* A finalized live row stays visible until history takes over. Removing it from
* the raw store (delete/rewind/clear) cancels the handoff rather than reviving it.
@@ -26,7 +32,10 @@ export class HistoryViewHandoff<T extends HistoryMessageSource> {
}
const pending = new Set(this.pending);
if (!snapshot.ready) return { messages: raw, pending };
- for (const row of raw) if (pending.has(row.clientId)) history.set(row.clientId, row);
+ for (const row of raw) if (pending.has(row.clientId)) {
+ const known = history.get(row.clientId);
+ history.set(row.clientId, known ? liveContentWithHistoryOrder(row, known) : row);
+ }
const messages = [...history.values()].sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt) || (a.rowid ?? 0) - (b.rowid ?? 0));
return { messages, pending };
}
diff --git a/packages/maker-shared/src/historyViewRender.ts b/packages/maker-shared/src/historyViewRender.ts
index 9067414047..40b05ea68f 100644
--- a/packages/maker-shared/src/historyViewRender.ts
+++ b/packages/maker-shared/src/historyViewRender.ts
@@ -1,5 +1,6 @@
import { historyViewLeaves, type HistoryMessageSource, type HistoryWorkSummary, type DeferredHistoryWork } from './historyView.js';
import type { HistoryViewController, HistoryViewSnapshot } from './historyViewController.js';
+import { liveContentWithHistoryOrder } from './historyViewHandoff.js';
/** The original renderer owns all grouping. References only substitute unread bodies. */
export function renderHistoryView<T extends HistoryMessageSource, TItem>(options: {
@@ -33,7 +34,8 @@ export function renderHistoryView<T extends HistoryMessageSource, TItem>(options
for (const item of leaves) {
if (item.type === 'messages') {
for (const row of item.messages) {
- rows.push(live.get(row.clientId) ?? row);
+ const current = live.get(row.clientId);
+ rows.push(current ? liveContentWithHistoryOrder(current, row) : row);
seen.add(row.clientId);
endMs = Math.max(endMs, Date.parse(row.createdAt));
}