/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/queue/frontier/revisit * * Revisit policy of the crawl frontier. * * Page changes are modelled as a Poisson process with rate λ (changes per ms). * * - With few observations, λ̂ = (changes + α) / (elapsed + β), a Gamma-prior * smoothed estimate that never collapses to 0 or ∞. * - Once `minSamples` revisits exist, the Cho & Garcia-Molina estimator for * regular sampling is used (ACM TOIT 2003, "Estimating frequency of change"): * λ̂ = −ln((n − X + 0.5) / (n + 0.5)) / I, where `n` is the number of * revisits, `X` the number of revisits that detected a change and `I` the * mean access interval. It corrects the bias of counting at most one change * per interval. * * The next revisit interval is `1 / (λ̂ · w)` with `w = importance / importanceRef` * (clamped to [0.1, 10]), so the revisit priority is proportional to * `λ · importance`, then clamped to `[minIntervalMs, maxIntervalMs]`. */ /** Fetch / change counters stored for each URL. */ export interface ChangeHistory { /** Total successful fetches (including the first one). */ fetchCount: number; /** Fetches that detected a content change. */ changeCount: number; firstFetchedAt: number | null; lastFetchedAt: number | null; } /** Revisit scheduler tuning. */ export interface RevisitPolicy { /** Lower bound of a revisit interval. Default: 5 minutes. */ minIntervalMs: number; /** Upper bound of a revisit interval. Default: 30 days. */ maxIntervalMs: number; /** Prior pseudo-count of changes (α). Default: 1. */ priorChanges: number; /** Prior pseudo-duration in ms (β). Default: 7 days. */ priorIntervalMs: number; /** Revisits required before switching to the Cho estimator. Default: 3. */ minSamples: number; /** Importance that maps to weight 1. Default: 1 (the cash of a seed). */ importanceRef: number; } const MINUTE = 60_000; const DAY = 86_400_000; export const DEFAULT_REVISIT_POLICY: Readonly = Object.freeze({ minIntervalMs: 5 * MINUTE, maxIntervalMs: 30 * DAY, priorChanges: 1, priorIntervalMs: 7 * DAY, minSamples: 3, importanceRef: 1, }); /** Estimated change rate λ̂ in changes per millisecond (always finite, ≥ 0). */ export function estimateChangeRate( history: ChangeHistory, policy: RevisitPolicy = DEFAULT_REVISIT_POLICY, ): number { const first = history.firstFetchedAt; const last = history.lastFetchedAt; const elapsed = first !== null && last !== null ? Math.max(0, last - first) : 0; const revisits = Math.max(0, history.fetchCount - 1); if (revisits >= policy.minSamples && elapsed > 0) { const changes = Math.min(Math.max(0, history.changeCount), revisits); const meanInterval = elapsed / revisits; const rate = -Math.log((revisits - changes + 0.5) / (revisits + 0.5)) / meanInterval; // A page that never changed still gets the (tiny) prior rate so it is // eventually revisited at `maxIntervalMs`. const prior = policy.priorChanges / (elapsed + policy.priorIntervalMs); return Math.max(rate, changes === 0 ? prior * 0.1 : 0); } return ( (Math.max(0, history.changeCount) + policy.priorChanges) / (elapsed + policy.priorIntervalMs) ); } /** Relative importance weight clamped to [0.1, 10]. */ export function importanceWeight( importance: number, policy: RevisitPolicy = DEFAULT_REVISIT_POLICY, ): number { const ref = policy.importanceRef > 0 ? policy.importanceRef : 1; const w = importance / ref; if (!Number.isFinite(w) || w < 0.1) return 0.1; return w > 10 ? 10 : w; } /** Revisit interval in ms for a change rate and an importance. */ export function revisitIntervalMs( rate: number, importance: number, policy: RevisitPolicy = DEFAULT_REVISIT_POLICY, ): number { if (!(rate > 0)) return policy.maxIntervalMs; const interval = 1 / (rate * importanceWeight(importance, policy)); if (interval < policy.minIntervalMs) return policy.minIntervalMs; return interval > policy.maxIntervalMs ? policy.maxIntervalMs : interval; } /** Probability that a page with rate λ changed within `elapsedMs`. */ export function changeProbability(rate: number, elapsedMs: number): number { if (!(rate > 0) || !(elapsedMs > 0)) return 0; return 1 - Math.exp(-rate * elapsedMs); } /** * Expected freshness gain of revisiting now: importance weight × probability * that the page changed since the last fetch. Used as `revisitPriority`. */ export function revisitPriority( rate: number, importance: number, elapsedMs: number, policy: RevisitPolicy = DEFAULT_REVISIT_POLICY, ): number { return importanceWeight(importance, policy) * changeProbability(rate, elapsedMs); }