feat(onebot): backfill cloud history after login

Add an opt-in per-account login history backfill that waits for roster warmup and a 30-second grace period before silently filling local message history. The setting defaults to disabled and applies on the next account lifecycle.

Run one account at a time, prioritize known local gaps, and process at most three groups plus three friends with one 20-message page per session. Failures are logged and never retried during the same login.

Persist reconstructed history without dispatching OneBot events or changing QQ read state. Keep private metadata and event bodies atomic, honor recall tombstones, suppress live-only history side effects, avoid extra media requests, and stop queued, in-flight, or converting work when the account closes.

Share the persistence path with manual history actions and add lifecycle, cancellation, corruption, partial-failure, peer-identity, and storage-atomicity regression coverage. Refs #280.
This commit is contained in:
DrSmoothl committed 2026-07-25 23:44:28 +08:00
1 parent b71279dcc3
commit dc2ec1235a
15 files changed
+1810 -68

No files matched your search

@@ -1,7 +1,10 @@
import { describe, expect, it, vi } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { loginHistorySyncCoordinator } from '../src/history-sync';
import { OneBotInstance } from '../src/instance';
import type { NetworkShutdownResult } from '../src/network';
afterEach(() => vi.restoreAllMocks());
function fabricatedInstance(results: NetworkShutdownResult[]) {
const instance = Object.create(OneBotInstance.prototype) as OneBotInstance;
const closeMessage = vi.fn();
@@ -19,6 +22,8 @@ function fabricatedInstance(results: NetworkShutdownResult[]) {
disposeRequested: false,
acceptingActions: true,
inFlightActions: new Set<Promise<void>>(),
historySyncAbortController: new AbortController(),
historySyncTask: null,
lifecycleTail: Promise.resolve(),
storeCloseState: { message: false, media: false, reaction: false },
apiHandler: { quiesce: vi.fn() },
@@ -32,6 +37,46 @@ function fabricatedInstance(results: NetworkShutdownResult[]) {
}
describe('OneBotInstance.dispose network/store ordering', () => {
it('uses the startup opt-in snapshot and starts history sync at most once', async () => {
const schedule = vi.spyOn(loginHistorySyncCoordinator, 'schedule')
.mockResolvedValue({
scannedGroups: 0,
scannedPrivateUsers: 0,
selectedGroups: 0,
selectedPrivateUsers: 0,
fetchedMessages: 0,
storedMessages: 0,
failedSessions: 0,
truncatedGroups: false,
truncatedPrivateUsers: false,
});
const disabled = fabricatedInstance([{ closed: true, errors: [] }]).instance;
Object.assign(disabled as unknown as Record<string, unknown>, {
historySyncEnabledAtStartup: false,
historySyncStartRequested: false,
bridge: {},
converterCtx: {},
log: { debug: vi.fn(), error: vi.fn() },
});
disabled.startLoginHistorySync();
expect(schedule).not.toHaveBeenCalled();
const enabled = fabricatedInstance([{ closed: true, errors: [] }]).instance;
Object.assign(enabled as unknown as Record<string, unknown>, {
historySyncEnabledAtStartup: true,
historySyncStartRequested: false,
bridge: {},
converterCtx: {},
log: { debug: vi.fn(), error: vi.fn() },
});
enabled.startLoginHistorySync();
enabled.startLoginHistorySync();
await Promise.resolve();
expect(schedule).toHaveBeenCalledOnce();
});
it('keeps stores open after an incomplete network shutdown and retries later', async () => {
const failure = {
name: 'http',
@@ -141,4 +186,27 @@ describe('OneBotInstance.dispose network/store ordering', () => {
expect(f.closeMedia).toHaveBeenCalledTimes(1);
expect(f.closeReaction).toHaveBeenCalledTimes(1);
});
it('aborts and drains login history sync before closing message storage', async () => {
const f = fabricatedInstance([{ closed: true, errors: [] }]);
let finishSync!: () => void;
const historySyncTask = new Promise<void>((resolve) => { finishSync = resolve; });
const controller = new AbortController();
Object.assign(f.instance as unknown as Record<string, unknown>, {
historySyncAbortController: controller,
historySyncTask,
});
const disposing = f.instance.dispose();
expect(controller.signal.aborted).toBe(true);
await Promise.resolve();
await Promise.resolve();
expect(f.shutdown).not.toHaveBeenCalled();
expect(f.closeMessage).not.toHaveBeenCalled();
finishSync();
await disposing;
expect(f.shutdown).toHaveBeenCalledOnce();
expect(f.closeMessage).toHaveBeenCalledOnce();
});
});