// WHERE A FORUM THREAD'S WALK STANDS (release 37 lane G) — `posts-state.json`'s // `forum`, pure: no fs, client-safe (the channel page's status line reads it). // // A thread is read newest first, and two walks share the pages: // // CATCH-UP — from the thread's last page down to the first page holding an // archived post at or below `newestPostId` (the MARK: every post up to it is // held, from `historyPage` up). It is what every run does first, so the // newest posts always come first. A capped or interrupted catch-up keeps its // own resume point (`catchUpPage`) and never touches `historyPage`. // // HISTORY — from `historyPage` down to page 1. Archived posts mean nothing // here (pages shift down as posts are deleted); a page whose posts are all // archived is stepped past without counting against a run's caps. It // advances only `historyPage`, and ends with `complete`. // // One cursor used to serve both walks: a capped catch-up stored its own // position as THE cursor and the next run re-read hundreds of held pages as // "history". Two positions, and the posts themselves as the check on them. // // DERIVE, DON'T TRUST. At the start of every run `historyPage` is re-derived // from the archived posts' pages (`ForumPostInfo.page`): it is never above one // page past the top of the held band — the contiguous run of held pages down // from the newest — so a lost or overwritten position heals from the posts. // Drift only moves posts to LOWER page numbers (a deletion shifts every later // post back), so a page number read earlier is never below where the post is // now: starting a page or two ABOVE the band's foot re-reads held pages // (skipped, uncounted) and can never step over an unread one. export type ForumWalkState = { // The thread's last page, as the last run saw it. newestPage?: number; // THE MARK: the newest post of the held band. A catch-up ends at the first // page holding an archived post at or below it. Absent before the first // complete catch-up (a state from before release 37): then any archived post // ends the catch-up, as it always did. newestPostId?: string; // An unfinished catch-up's next page: a capped or interrupted catch-up read // the pages above it, and the band between it and the mark is still owed. catchUpPage?: number; // The next older page the history walk reads. Absent when `complete`. historyPage?: number; // The lowest page any walk has read. oldestPageReached?: number; // The history walk has read page 1. complete?: boolean; }; // How many pages above the held band's foot a derived history start re-reads. export const FORUM_HISTORY_OVERLAP = 2; // The contiguous run of pages down from the highest one held. An isolated low // page — an article thread's first post, shown atop every page and stored with // page 1, or a range imported from saved pages — is not part of it. export function heldBand(pages: Iterable): { low: number; high: number } | null { const set = new Set(); for (const p of pages) if (Number.isInteger(p) && p >= 1) set.add(p); if (set.size === 0) return null; const high = Math.max(...set); let low = high; while (set.has(low - 1)) low--; return { low, high }; } export type DerivedForumWalk = { walk: ForumWalkState; // What the derivation changed, as sentences for the job log. notes: string[]; }; // The positions a run starts from: the stored ones (or, for a state written // before release 37, its single `cursor` read as the history position), // checked against the pages the archive actually holds. export function deriveForumWalk( stored: ForumWalkState | undefined, legacyCursor: string | undefined, heldPages: Iterable, ): DerivedForumWalk { const notes: string[] = []; const walk: ForumWalkState = { ...(stored ?? {}) }; if (!stored && legacyCursor !== undefined) { const n = Number(legacyCursor); if (Number.isInteger(n) && n >= 1) { walk.historyPage = n; notes.push(`Read the old single resume point (page ${n}) as the history position.`); } } const held = [...heldPages]; const band = heldBand(held); if (!band) return { walk, notes }; // An unfinished catch-up owes the pages from `catchUpPage` down to the mark, // so the gap under the band it read is ITS gap, not the history's: the // history is judged by the pages at or below its resume point. (2026-10-26: // a restart cut a catch-up at page 988 of a complete thread whose page 987 // was still owed; the next run unmarked the history and re-read 150 held // pages before it was stopped.) const owed = walk.catchUpPage; if (walk.complete && owed !== undefined && band.low > 1) { const below = heldBand(held.filter((p) => p <= owed)); if (below && below.low <= 1) { walk.oldestPageReached = 1; walk.newestPage = Math.max(walk.newestPage ?? band.high, band.high); delete walk.historyPage; return { walk, notes }; } } walk.oldestPageReached = Math.min(walk.oldestPageReached ?? band.low, band.low); walk.newestPage = Math.max(walk.newestPage ?? band.high, band.high); if (band.low <= 1) { // Every page from 1 to the newest held is held: the history is done. if (!walk.complete) notes.push("The archive holds every page from page 1 up: history complete."); walk.complete = true; delete walk.historyPage; return { walk, notes }; } const fromPosts = band.low + FORUM_HISTORY_OVERLAP - 1; if (walk.complete) { // Complete, yet the held band stops above page 1: the posts win. walk.complete = false; walk.historyPage = fromPosts; notes.push( `Marked complete, but the archived pages stop at page ${band.low}: history resumes at page ${fromPosts}.`, ); return { walk, notes }; } if (walk.historyPage === undefined || walk.historyPage > fromPosts) { if (walk.historyPage !== undefined) { notes.push( `The stored history position (page ${walk.historyPage}) is above the archived pages (held down to page ${band.low}): history resumes at page ${fromPosts}.`, ); } else { notes.push(`No history position stored: resuming at page ${fromPosts}, from the archived pages (held down to page ${band.low}).`); } walk.historyPage = fromPosts; } return { walk, notes }; } // The log line a run starts with. export function forumWalkStartLine(walk: ForumWalkState): string { const parts: string[] = []; parts.push(walk.newestPage ? `caught up to page ${walk.newestPage}` : "nothing read yet"); if (walk.catchUpPage) parts.push(`an unfinished catch-up continues at page ${walk.catchUpPage}`); parts.push( walk.complete ? "history complete" : walk.historyPage ? `history resumes at page ${walk.historyPage}` : "history from the last page", ); return parts.join("; "); } // A forum walk's pause between two page loads (xenforoFetcher.ts), and the // floor a channel's `postPagePauseSeconds` is held to. export const FORUM_DEFAULT_PAGE_PAUSE_SECONDS = 12; export const FORUM_MIN_PAGE_PAUSE_SECONDS = 5; // The mean pause a paced walk waits per page: the jitter (xenforoFetcher.ts, // jitteredPause) spans [0.85, 1.65) of the base, so its mean is 1.25×. export function meanPagePauseSeconds(baseSeconds: number, floorSeconds: number): number { return Math.max(floorSeconds, baseSeconds) * 1.25; } // "~8 h", "~25 min", "~40 s". export function roughDuration(seconds: number): string { if (seconds < 90) return `~${Math.max(1, Math.round(seconds))} s`; if (seconds < 90 * 60) return `~${Math.round(seconds / 60)} min`; const h = seconds / 3600; return `~${h < 10 ? Math.round(h * 10) / 10 : Math.round(h)} h`; } // The channel page's status line: "pages read 2338–2600 of 2600 · history to // page 1 remaining: 2337 pages (~8 h)". Null before any page was read. export function describeForumWalk( walk: ForumWalkState | undefined, pageSeconds: number, ): string | null { if (!walk || (!walk.newestPage && !walk.oldestPageReached)) return null; const top = walk.newestPage ?? walk.oldestPageReached!; const low = walk.complete ? 1 : (walk.oldestPageReached ?? top); const parts = [`pages read ${low}–${top} of ${top}`]; if (walk.catchUpPage) parts.push(`catch-up unfinished at page ${walk.catchUpPage}`); if (walk.complete) { parts.push("history complete"); } else if (walk.historyPage) { parts.push( `history to page 1 remaining: ${walk.historyPage} page${walk.historyPage === 1 ? "" : "s"} (${roughDuration(walk.historyPage * pageSeconds)})`, ); } return parts.join(" · "); } // --- retry on a block --------------------------------------------------------------- // `fetch-posts {retryOnBlock}` and `settings.social.xenforo.retryOnBlock`: a // challenge or a refusal stops the walk, the job waits `minutes` and runs // again, `max` tries in all (the first included). export type RetryOnBlock = { minutes: number; max: number }; // The shortest wait a retry may use, the longest, and the most tries. export const RETRY_MIN_MINUTES = 5; export const RETRY_MAX_MINUTES = 24 * 60; export const RETRY_MAX_TRIES = 48; export function retryOnBlockProblem(r: RetryOnBlock | undefined): string | null { if (!r) return null; if (!Number.isInteger(r.minutes) || r.minutes < RETRY_MIN_MINUTES || r.minutes > RETRY_MAX_MINUTES) { return `retryOnBlock.minutes must be a whole number from ${RETRY_MIN_MINUTES} to ${RETRY_MAX_MINUTES}.`; } if (!Number.isInteger(r.max) || r.max < 1 || r.max > RETRY_MAX_TRIES) { return `retryOnBlock.max (the tries in all, the first included) must be a whole number from 1 to ${RETRY_MAX_TRIES}.`; } return null; } // Total over `unknown`: a valid `{minutes, max}`, else undefined. export function parseRetryOnBlock(v: unknown): RetryOnBlock | undefined { if (!v || typeof v !== "object" || Array.isArray(v)) return undefined; const r = v as Record; const out = { minutes: Number(r.minutes), max: Number(r.max) }; return retryOnBlockProblem(out) ? undefined : out; } // "wait 30 min, up to 12 tries" / "off". export function describeRetryOnBlock(r: RetryOnBlock | undefined): string { return r ? `wait ${r.minutes} min after a block, up to ${r.max} tr${r.max === 1 ? "y" : "ies"}` : "off"; }