Skip to content

Commit 387cc17

Browse files
committed
fix: address review of repair bounds and dry runs
- A dry run over a repair still under way took the chat's row and dropped what that repair had yet to write. It is refused now. - Only errors about the chat itself mark a repair skipped. Any 4xx did, so a broken session (406 AUTH_KEY_DUPLICATED) or a 420 without a wait would have skipped every queued chat in turn instead of retrying. - backfill status names the chat under way before failures, and says missing for a dry run. - Tests cover the schema upgrade of an older archive and the per-chat count of a dry run.
1 parent 2ea92fd commit 387cc17

4 files changed

Lines changed: 122 additions & 4 deletions

File tree

‎cli.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,10 +2599,13 @@ async function runSyncStatus(globalFlags) {
25992599
const counts = REPAIR_STATUS_ORDER.map((status) => `${status}=${repairTotals.byStatus[status] ?? 0}`).join(' ');
26002600
const missing = repairs.some((repair) => repair.dry_run) ? ` missing=${repairTotals.missing}` : '';
26012601
console.log(`REPAIRS: ${repairTotals.chats} chat(s) ${counts} checked=${repairTotals.checked} added=${repairTotals.added}${missing}`);
2602-
const named = repairs.filter((repair) => repair.status === 'in_progress' || repair.status === 'error');
2602+
const named = [
2603+
...repairs.filter((repair) => repair.status === 'in_progress'),
2604+
...repairs.filter((repair) => repair.status === 'error'),
2605+
];
26032606
for (const repair of named.slice(0, REPAIR_STATUS_LINES)) {
26042607
const error = repair.error ? ` error=${repair.error}` : '';
2605-
console.log(` repair ${repairLabel(repair)} [${repair.status}] checked=${repair.checked_count} added=${repair.added_count}${error}`);
2608+
console.log(` repair ${repairLabel(repair)} [${repair.status}] checked=${repair.checked_count} ${addedWord(repair)}=${repair.added_count}${error}`);
26062609
}
26072610
if (named.length > REPAIR_STATUS_LINES) {
26082611
console.log(` … and ${named.length - REPAIR_STATUS_LINES} more; \`tgcli --json backfill status\` lists them all.`);

‎message-sync-service.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,22 @@ function hasSequentialIds(channelId) {
227227

228228
// Telegram refusing this chat will not change soon, unlike a dropped connection, an
229229
// expired session (401) or a failure of its own (5xx), which say nothing about the message.
230+
// Errors about the chat itself, the ones a repair can give up on: the account has left it,
231+
// it is gone, or Telegram does not know it. Anything else, such as a broken session,
232+
// would stop every chat in a row and must stay a failure that is retried.
233+
const CHAT_GONE_ERRORS = new Set([
234+
'CHANNEL_INVALID',
235+
'CHANNEL_PRIVATE',
236+
'CHAT_FORBIDDEN',
237+
'CHAT_ID_INVALID',
238+
'PEER_ID_INVALID',
239+
'USER_BANNED_IN_CHANNEL',
240+
]);
241+
242+
function isChatGone(error) {
243+
return error instanceof MtPeerNotFoundError || CHAT_GONE_ERRORS.has(error?.text);
244+
}
245+
230246
function isRefusedRead(error) {
231247
const code = error?.code;
232248
return (code >= 400 && code < 500 && code !== 401) || error instanceof MtPeerNotFoundError;
@@ -2626,6 +2642,17 @@ export default class MessageSyncService {
26262642
if (!channelIds.length) {
26272643
throw new Error('No chat has sync enabled.');
26282644
}
2645+
if (dryRun) {
2646+
// A dry run takes the chat's row, so over a repair still under way it would drop
2647+
// what that repair has yet to write.
2648+
const open = this.db.prepare(`
2649+
SELECT COUNT(*) AS n FROM archive_repairs
2650+
WHERE status IN (?, ?) AND dry_run = 0 AND channel_id IN (SELECT value FROM json_each(?))
2651+
`).get(JOB_STATUS.PENDING, JOB_STATUS.IN_PROGRESS, JSON.stringify(channelIds)).n;
2652+
if (open) {
2653+
throw new Error(`A repair is under way in ${open} of these chat(s); a dry run would replace it. Wait for it to finish (\`tgcli backfill status\`).`);
2654+
}
2655+
}
26292656
const upsert = this.db.prepare(`
26302657
INSERT INTO archive_repairs (channel_id, status, after_message_id, since_date, dry_run)
26312658
VALUES (?, ?, ?, ?, ?)
@@ -2771,7 +2798,7 @@ export default class MessageSyncService {
27712798
await this._delayUnlessStopped(waitSeconds * 1000);
27722799
return;
27732800
}
2774-
if (isRefusedRead(error)) {
2801+
if (isChatGone(error)) {
27752802
// Not a member any more, or the chat is gone: asking again every hour changes nothing.
27762803
update('status = ?, error = ?', REPAIR_STATUS.SKIPPED, error?.message ?? String(error));
27772804
console.log(`[sync] Channel ${channelId}: repair skipped, Telegram refuses to read it: ${error?.message ?? error}`);

‎tests/backfill-client.test.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,20 @@ describe('backfill repair', () => {
417417
expect(lines).toContain(' … and 2 more; `tgcli --json backfill status` lists them all.');
418418
});
419419

420+
it('status names the chat under way before failures, and says missing for a dry run', async () => {
421+
const failed = Array.from({ length: 11 }, (_, index) => repair({ channel_id: `-${500 + index}`, status: 'error', error: 'boom' }));
422+
services.current = makeFakeServices({
423+
listRepairs: vi.fn(() => [...failed, repair({ peer_title: 'Now', dry_run: 1, checked_count: 50, added_count: 6 })]),
424+
});
425+
426+
await runProgram(['backfill', 'status']);
427+
428+
const lines = logSpy.mock.calls.flat().join('\n').split('\n');
429+
expect(lines.find((line) => line.startsWith(' repair'))).toBe(' repair Now [in_progress] checked=50 missing=6');
430+
expect(lines[0]).not.toContain('missing');
431+
expect(lines.find((line) => line.startsWith('REPAIRS:'))).toContain('added=0 missing=6');
432+
});
433+
420434
it('status --json adds totals to the full list', async () => {
421435
services.current = makeFakeServices({
422436
listRepairs: vi.fn(() => [repair({ status: 'idle', checked_count: 7, added_count: 2 })]),

‎tests/backfill-repair.test.js‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fs from 'node:fs';
55
import os from 'node:os';
66
import path from 'node:path';
77

8-
import { Long, tl } from '@mtcute/core';
8+
import { Long, MtPeerNotFoundError, tl } from '@mtcute/core';
99
import { iterHistory } from '@mtcute/core/methods.js';
1010
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
1111

@@ -208,6 +208,49 @@ describe('backfill repair', () => {
208208
expect(repairRow(service)).toMatchObject({ dry_run: 0, added_count: 2 });
209209
});
210210

211+
// A dry run takes the chat's row: over a repair under way it would drop the rest of it.
212+
it('refuses a dry run over a repair still under way, and takes one after it', async () => {
213+
const service = setUp(range(10, 259), [10]);
214+
service.addRepairs({ chats: [CHANNEL_ID] });
215+
await service._repairNextPage(service._getNextRepair());
216+
217+
expect(() => service.addRepairs({ chats: [CHANNEL_ID], dryRun: true })).toThrow(/repair is under way in 1 of these chat/);
218+
expect(() => service.addRepairs({ all: true, dryRun: true })).toThrow(/under way/);
219+
expect(repairRow(service)).toMatchObject({ status: 'in_progress', dry_run: 0, cursor_message_id: 160 });
220+
221+
await service.processQueue();
222+
expect(service.addRepairs({ chats: [CHANNEL_ID], dryRun: true })).toEqual([CHANNEL_ID]);
223+
});
224+
225+
it('counts in a dry run only this chat\'s rows as present', async () => {
226+
const service = setUp(range(10, 20), [10]);
227+
service.db.prepare('INSERT INTO channels (channel_id, sync_enabled) VALUES (?, 1)').run(OTHER_ID);
228+
service.insertMessagesTx(range(11, 20).map((id) => service._buildMessageRecord(OTHER_ID, { id, date: BASE_DATE + id })));
229+
230+
service.addRepairs({ chats: [CHANNEL_ID], dryRun: true });
231+
await service.processQueue();
232+
233+
expect(repairRow(service)).toMatchObject({ dry_run: 1, added_count: 10 });
234+
});
235+
236+
it('adds what it needs to an archive made before dry runs and dialog membership', async () => {
237+
const first = setUp([10, 11], [10]);
238+
const dbPath = first.db.name;
239+
first.db.exec('ALTER TABLE archive_repairs DROP COLUMN dry_run');
240+
first.db.exec('ALTER TABLE channels DROP COLUMN in_dialogs');
241+
first.close();
242+
243+
const service = makeService();
244+
const columns = (table) => service.db.prepare(`PRAGMA table_info(${table})`).all().map((col) => col.name);
245+
expect(service.db.name).toBe(dbPath);
246+
expect(columns('archive_repairs')).toContain('dry_run');
247+
expect(columns('channels')).toContain('in_dialogs');
248+
249+
service.addRepairs({ chats: [CHANNEL_ID], dryRun: true });
250+
await service.processQueue();
251+
expect(repairRow(service)).toMatchObject({ status: 'idle', dry_run: 1, added_count: 1 });
252+
});
253+
211254
it('stops at the date bound', async () => {
212255
const service = setUp(range(100, 200), [100, 101, 200]);
213256

@@ -374,6 +417,37 @@ describe('backfill repair', () => {
374417
expect(repairRow(service).status).toBe('skipped');
375418
});
376419

420+
it.each([
421+
['CHANNEL_INVALID', () => tl.RpcError.fromTl({ _: 'rpc_error', errorCode: 400, errorMessage: 'CHANNEL_INVALID' })],
422+
['a peer missing from the cache', () => new MtPeerNotFoundError('Peer -1000000000500 is not found in local cache')],
423+
])('skips a chat on %s', async (_, makeError) => {
424+
const service = setUp(range(10, 30), [10]);
425+
answer = () => {
426+
throw makeError();
427+
};
428+
429+
service.addRepairs({ chats: [CHANNEL_ID] });
430+
await service.processQueue();
431+
432+
expect(repairRow(service).status).toBe('skipped');
433+
});
434+
435+
// These break every request, not this chat: skipping would give up on each chat in turn.
436+
it.each([
437+
[406, 'AUTH_KEY_DUPLICATED'],
438+
[420, 'FROZEN_METHOD_INVALID'],
439+
])('keeps %i %s a failure that is retried', async (errorCode, errorMessage) => {
440+
const service = setUp(range(10, 30), [10]);
441+
answer = () => {
442+
throw tl.RpcError.fromTl({ _: 'rpc_error', errorCode, errorMessage });
443+
};
444+
445+
service.addRepairs({ chats: [CHANNEL_ID] });
446+
await service.processQueue();
447+
448+
expect(repairRow(service).status).toBe('error');
449+
});
450+
377451
it('stops for a chat whose sync was turned off, and stays stopped while it is off', async () => {
378452
const service = setUp(range(10, 30), [10]);
379453
service.addRepairs({ chats: [CHANNEL_ID] });

0 commit comments

Comments
 (0)