fix(core): preserve private history paging failures

Split larger private history reads into bounded pages and honor the end of history. Surface rejected or stalled pages instead of returning misleading empty or partial results.

Fixes #605
This commit is contained in:
DrSmoothl committed 2026-10-31 18:28:05 +08:00
1 parent 26639ffef7
commit 304f3e772f
5 files changed
+104 -9

No files matched your search

+3 -7
View File
@@ -254,6 +254,7 @@ type FriendMessage = Extract<QQEventVariant, { kind: 'friend_message' }>;
// ── Group-history fetch guards (the server frequency-limits / kicks abusive
// pulls, so keep each request small, bound the loop, and space the sends). ──
const HISTORY_MAX_COUNT = 200; // hard cap on messages returned per call
const PRIVATE_HISTORY_PAGE_SIZE = 100;
const HISTORY_CHUNK_SEQ = 30; // sequence span per server request (small packet)
const HISTORY_MAX_REQUESTS = 12; // bound the walk-back loop
const HISTORY_MIN_GAP_MS = 300; // minimum spacing between SsoGetGroupMsg sends
@@ -1543,7 +1544,7 @@ export class MessageApi {
selfUin,
friendUid,
cursor.time,
want - collected.size,
Math.min(want - collected.size, PRIVATE_HISTORY_PAGE_SIZE),
cursor.random,
),
);
@@ -1564,7 +1565,7 @@ export class MessageApi {
break;
}
if (page.cursor.time === 0 && page.cursor.random === 0) {
if (page.isComplete || (page.cursor.time === 0 && page.cursor.random === 0)) {
reachedEnd = true;
break;
}
@@ -1572,11 +1573,6 @@ export class MessageApi {
const nextCursorKey = `${page.cursor.time}:${page.cursor.random}`;
if (seenCursors.has(nextCursorKey)) {
if (page.messages.length === 0) {
reachedEnd = true;
break;
}
throw new Error(
`private roam history cursor did not advance from ${cursor.time}/${cursor.random}`,
);
@@ -208,6 +208,56 @@ describe('MessageApi history pagination', () => {
expect(result.map((message) => message.ntMsgSeq)).toEqual([63213, 63214, 63215]);
});
it('splits a 200-message private history request into bounded pages', async () => {
mockC2cRoamFetch.mockImplementation(
async (_sender, _identity, _self, _friend, time, count) => ({
messages: Array.from({ length: count }, (_, index) => ({
...gm(time - index),
kind: 'friend_message',
ntMsgSeq: time - index,
})),
cursor: { time: time - count, random: 1 },
isComplete: false,
}),
);
const result = await runHistory(
new MessageApi(ctx).getC2cLatestHistory('u_friend', 200, 10001, 1000),
);
expect(result).toHaveLength(200);
expect(mockC2cRoamFetch.mock.calls.map((call) => call[5])).toEqual([100, 100]);
expect(result.map((message) => message.ntMsgSeq)).toEqual(
Array.from({ length: 200 }, (_, index) => 801 + index),
);
});
it('honors completion even when the last page still includes a cursor', async () => {
mockC2cRoamFetch.mockResolvedValue({
messages: [{ ...gm(1), kind: 'friend_message', ntMsgSeq: 1 }],
cursor: { time: 100, random: 2 },
isComplete: true,
});
const result = await runHistory(
new MessageApi(ctx).getC2cLatestHistory('u_friend', 20, 10001, 1000),
);
expect(result).toHaveLength(1);
expect(mockC2cRoamFetch).toHaveBeenCalledOnce();
});
it('does not return partial history after a later request fails', async () => {
mockC2cRoamFetch
.mockResolvedValueOnce({
messages: [{ ...gm(1), kind: 'friend_message', ntMsgSeq: 1 }],
cursor: { time: 100, random: 2 },
isComplete: false,
})
.mockRejectedValueOnce(new Error('SsoGetRoamMsg failed: result=123'));
const assertion = expect(
new MessageApi(ctx).getC2cLatestHistory('u_friend', 20, 10001, 1000),
).rejects.toThrow('result=123');
await vi.runAllTimersAsync();
await assertion;
});
it('rejects a stalled private roam cursor instead of returning a partial page', async () => {
mockC2cRoamFetch.mockResolvedValue({
cursor: { time: 1700000100, random: 0 },