// WHERE A SOCIAL TIMELINE'S TWO WALKS STAND (X, Bluesky, Nitter) — // `posts-state.json`'s `backfill` and `sync`. Pure: no fs, client-safe (the // channel page's status line reads it). // // Two actions, for two purposes (operator, 6 Nov 2026: "We should have // distinct 'sync new' and 'resume backfill' actions, they are different // actions for different purposes"): // // SYNC NEW — from the top of the timeline down to the newest archived post // (the `since` watermark). What a plain fetch-posts, the scheduler and the // Sync button run. It never reads the backfill's resume point and never // clears it. A sync that stops part-way (its budget, a cancel, a drain, a // cap) keeps ITS OWN resume point in `sync`, with the watermark it was // walking down to: the next sync continues there, down to that same // watermark, rather than stopping at the posts it just wrote and leaving // the gap between them and the archive unread. // // RESUME BACKFILL — the full-history walk continued from its saved resume // point (`backfill.cursor`), with no watermark. Run only when asked // (fetch-posts "backfill": true, the ops action resume-backfill, the channel // page's Resume backfill). It never touches the sync's position. // // The backfill's resume point is written by a FULL-HISTORY walk that did not // finish: a channel's first fetch (nothing archived, so no watermark), a // --full re-walk, or a resumed backfill. // // One field, `cursor`, used to serve both: a stored cursor made the next // plain fetch resume the backfill and skip the watermark — so a fetch meant // for new posts walked old history, missed the new ones, and on finishing // cleared the cursor (2026-11-06, madamsavvy-x: "resuming an unfinished // backfill; 9936 already archived … 0 new", 85 newer posts missed). A state // written before the split has only `cursor`; it is read as the BACKFILL's // position (timelineWalks), which is what it most often was, and is dropped // from the file on the next timeline write. Read so, nothing is lost: were it // really an unfinished sync's, resuming the backfill from it walks the same // gap down to the archive. // // The older-posts walk (`older`, search windows below the oldest archived // post) and a forum thread's two walks (`forum`, lib/forumWalk.ts) are their // own and untouched by this. // The full-history walk's resume point. export type TimelineBackfillState = { // The fetcher's opaque resume point (gallery-dl's extractor cursor, a // Bluesky feed cursor, a Nitter "Load more" cursor). cursor: string; // When the point was saved. savedAt?: string; }; // An unfinished sync's resume point, and the watermark it walks down to. export type TimelineSyncState = { cursor: string; // The newest archived post's time when the sync started (ISO): the walk // ends there. Absent only for a state no sync wrote. since?: string; savedAt?: string; }; export type TimelineWalkFields = { // LEGACY (before 2026-11-06): the one resume point both walks shared. cursor?: string; backfill?: TimelineBackfillState; sync?: TimelineSyncState; }; export type TimelineWalks = { backfill?: TimelineBackfillState; sync?: TimelineSyncState; // The backfill came from the legacy `cursor`. migrated?: boolean; }; const cursorOf = (v: unknown): string | undefined => typeof v === "string" && v.length > 0 ? v : undefined; // The two positions a state holds, with a legacy `cursor` read as the // backfill's. A `backfill` already present wins over a stray `cursor` (only an // older build, run after this one, would write both). export function timelineWalks(state: TimelineWalkFields | null | undefined): TimelineWalks { if (!state) return {}; const out: TimelineWalks = {}; const backfill = state.backfill && cursorOf(state.backfill.cursor) ? state.backfill : undefined; if (backfill) out.backfill = { ...backfill }; else { const legacy = cursorOf(state.cursor); if (legacy) { out.backfill = { cursor: legacy }; out.migrated = true; } } if (state.sync && cursorOf(state.sync.cursor)) out.sync = { ...state.sync }; return out; } // The two positions as fields to write: `cursor` is never written again. export function timelineWalkFields(walks: TimelineWalks): Pick { return { ...(walks.backfill ? { backfill: walks.backfill } : {}), ...(walks.sync ? { sync: walks.sync } : {}), }; } // Whether a state holds any timeline resume point (either walk's, or the // legacy one). export function hasTimelineResumePoint(state: TimelineWalkFields | null | undefined): boolean { const w = timelineWalks(state); return Boolean(w.backfill || w.sync); } export type TimelineMode = "sync" | "backfill" | "full"; export const NO_BACKFILL_TO_RESUME = "There is no unfinished backfill to resume: the timeline's full-history walk is not part-way. " + 'A plain fetch-posts syncs new posts; "full": true re-walks the whole timeline; "older": true walks back below the oldest archived post.'; export const BACKFILL_AND_FULL_REFUSAL = 'Resume backfill continues the saved history walk; a full re-walk ("full") starts it over — run one.'; export const BACKFILL_AND_OLDER_REFUSAL = 'Resume backfill continues the timeline\'s history walk; "older" is the search walk below the oldest archived post — run one at a time.'; // What one run reads from and where an unfinished run's resume point goes. export type TimelineRunPlan = { mode: TimelineMode; // The resume point handed to the fetcher (none: from the top). cursor?: string; // The watermark handed to the fetcher (none: no stop below). since?: string; // Where this run's resume point is saved when it stops part-way. saveTo: "sync" | "backfill"; }; // The run a mode makes, given the positions and the newest archived post's // time (`watermark`; null/undefined when nothing is archived). export function planTimelineRun( mode: TimelineMode, walks: TimelineWalks, watermark: string | null | undefined, ): TimelineRunPlan | { error: string } { if (mode === "full") return { mode, saveTo: "backfill" }; if (mode === "backfill") { if (!walks.backfill) return { error: NO_BACKFILL_TO_RESUME }; return { mode, cursor: walks.backfill.cursor, saveTo: "backfill" }; } // SYNC NEW: an unfinished sync continues down to the watermark it began // with; else from the top down to the newest archived post. Nothing // archived and no backfill part-way: the walk is the channel's first // full-history walk, and its resume point is the backfill's. With a backfill // part-way a sync keeps to its own slot whatever the archive holds, so it // can never move or clear the backfill's place. const since = walks.sync?.since ?? watermark ?? undefined; const cursor = walks.sync?.cursor; return { mode, ...(cursor ? { cursor } : {}), ...(since ? { since } : {}), saveTo: since || walks.backfill ? "sync" : "backfill", }; } // The positions after a run, from the fetcher's outcome. A run that finished // clears its own slot (a full re-walk that finished clears both: it read // everything); one that stopped part-way saves its resume point there, or // keeps the one it had when it learned none. The other slot is never touched. // // A BACKFILL IS NEVER FINISHED BY MEETING KNOWN POSTS (lane x-followups): a // run that ended "caught up" (`caughtUp`: a run of posts already archived, a // sync's stop) says nothing of the history below them. A resumed backfill // that ends so keeps the resume point it had. A resumed walk once re-read // archived posts, was taken for finished, and its point was cleared. export function settleTimelineRun( plan: TimelineRunPlan, prior: TimelineWalks, outcome: { complete: boolean; cursor?: string; caughtUp?: boolean }, now: string, ): TimelineWalks { const next: TimelineWalks = { ...(prior.backfill ? { backfill: prior.backfill } : {}), ...(prior.sync ? { sync: prior.sync } : {}), }; if (outcome.complete && outcome.caughtUp && plan.mode === "backfill") return next; if (outcome.complete) { if (plan.mode === "full") return {}; if (plan.saveTo === "sync") delete next.sync; else delete next.backfill; // A sync that had no watermark (nothing archived) and finished walked the // whole timeline: an old sync position under it is spent too. if (plan.mode === "sync" && plan.saveTo === "backfill") delete next.sync; return next; } const cursor = cursorOf(outcome.cursor); if (!cursor) return next; return withResumePoint(plan, next, cursor, now); } // A resume point saved mid-run (a checkpoint) or at its end. export function withResumePoint( plan: TimelineRunPlan, walks: TimelineWalks, cursor: string, now: string, ): TimelineWalks { const next: TimelineWalks = { ...(walks.backfill ? { backfill: walks.backfill } : {}), ...(walks.sync ? { sync: walks.sync } : {}), }; if (plan.saveTo === "sync") next.sync = { cursor, ...(plan.since ? { since: plan.since } : {}), savedAt: now }; else { next.backfill = { cursor, savedAt: now }; // A first walk (sync with no watermark) that left a backfill behind has // no sync of its own to continue. if (plan.mode === "sync") delete next.sync; } return next; } // The job log's opening words for a run: the mode, named. export function timelineRunLine(plan: TimelineRunPlan): string { if (plan.mode === "full") return "full re-walk of the timeline, from the top"; if (plan.mode === "backfill") return "resume backfill, from its saved resume point"; if (plan.cursor && plan.since) return `sync new, continuing the unfinished sync down to ${plan.since}`; if (plan.since) return `sync new, since ${plan.since}`; if (plan.saveTo === "sync") return "sync new — nothing archived yet, from the top"; return "sync new — nothing archived yet, so this is the first full-history walk"; } // The channel page's status line for the timeline's history walk. export function describeTimelineBackfill(walks: TimelineWalks, archived: number): string { if (walks.backfill) { return `backfill unfinished — ${archived} archived; Resume backfill continues it`; } return "no backfill part-way"; } // The channel page's line for an unfinished sync, or null. export function describeTimelineSync(walks: TimelineWalks): string | null { if (!walks.sync) return null; return walks.sync.since ? `a sync stopped part-way; the next Sync new continues it down to ${walks.sync.since}` : "a sync stopped part-way; the next Sync new continues it"; }