ADR-R2-16. Tiered analysis scheduler
Source:
docs/decisions/ADR-R2-16-tiered-analysis-scheduler.md. Generated at build time; edit the source file.
Status: accepted (selection rules, Task 26; the job, the imported status and the integration, Task 29).
Selection rules (pure, scheduler::select)
Section titled “Selection rules (pure, scheduler::select)”Values are TS M2 Task 23 verbatim: free 1000/50/0/no P2/20 per week, pro 10 000/200/100/P2/300 per week; 15 per month, 36 months, 20 recent, 90 days, 10 plies, batch 25.
- Primary time class. The class with the most games in the last 90 days (ties to the longer class). A dormant account (nothing in 90 days) uses the last 365 days, then all time. P0 then takes the newest games regardless of date, and a dormant account has only its primary as active. Future-dated games (
played_at > now) are ignored and logged. - P3 is rated only (every new rated game created after the first scheduling run, within the weekly budget).
- P0 before P3. A new game goes to P0 while P0 has room (a free user with 30 analysed games sends new games to P0 until 50). This is the plan’s literal behaviour, pinned by a test.
- P2. Calendar months are taken in UTC, up to and including 36 months back. The 15-per-month quota counts every analysed game of that month, whatever its tier, plus this run’s picks. Rated games are taken first and spaced chronologically; casual games only fill what rated cannot. Picks are emitted newest first.
- P2 for the primary time class runs in the same pass as P0 (P0 takes the newest 200, P2 up to 25 older). Kept on purpose; pinned by a test.
- Equal
played_atis broken by game id. - Candidates are
Pendingwith no tier inselect_for_analysis; the schedule job mapsimportedrows onto that (below).
The imported status and the import (Task 29)
Section titled “The imported status and the import (Task 29)”- The importer stores new games as
importedwithply_count(SAN tokens of the main line: headers, comments, variations, NAGs, move numbers and the result are not moves). It enqueues no analyse job. After the run commits (the run row isdone), it enqueues oneschedule-analysisjob, deduped onschedule:<account>; a failed enqueue retries the import, which enqueues again. - A payload without
maximports withImportAccountDeps::default_max = None: no limit on the beta plan (below). With per-user limits it becomestier_limits(plan).import_max. An explicitmax(the CLI’s--max) is R1’s budget: onemaxshared by the passes of a run, new games first, and a pass cut off bymaxkeeps its frontier, so a largermaxlater continues below it. - R1 rows stay valid: a
pendinggame with no tier was already enqueued by R1 and is never re-selected; the import no longer re-enqueues existingpendinggames (the reconciler does).
The schedule-analysis job (scheduler::schedule_job)
Section titled “The schedule-analysis job (scheduler::schedule_job)”plancomes from the payload ("beta"or none = beta,"free","pro") until per-user limits exist. Any other value is unrecoverable.- Beta plan (owner ruling, ADR 0041: private beta). Until plans exist there is no plan limit.
Plan::Betais the default for every account and for the CLI. It selects everyimportedgame (no 50-game P0 cap, no weekly P3 budget, no batch size, no stopping, no bullet or 36-month or length exclusion), bounded only by the import’s--max. Each game gets the tier the plan rules would give it first (0 in the primary class, 3 for a new rated game in an active class, 1 in another active class, else 2) and pro’s priority bases, and the analyse jobs run in that priority order. The free and pro rules are kept, tested, and unused by default. Pinned bycli_max_500_on_500_imported_games_schedules_all_500. - One transaction: lock the account row, load every game of the account and its weakness estimates, select, then set the selected games
pendingwithanalysis_tier,analysis_priority,selected_atandpending_since = now(), fenced onanalysis_status = 'imported'. Analyse jobs are enqueued after the commit at the stored priority (oneenqueue_manybatch, the same rows asenqueue_analyse_with_priority). A lost enqueue leavespendinggames, which the reconciler re-enqueues atcoalesce(analysis_priority, 100). - Priority table (
job_priority, lower first):BASE * 2 + (0 with Lichess evals, else 1), BASE tier 0 {pro 1, free 2}, tier 3 {pro 3, free 4}, tier 1 5, tier 2 10. Previews run at 0 and reference games at 200. R1 games keep 100. - Batch-wise adaptive stopping. P1/P2 add at most 25 games per time class per run, and only when no tier-1/2 game of that class is still
pendingoranalysing. A time class stops getting backfill once every rankable weakness estimate of it is precise (is_precise: width at most a quarter of the band median, orn_unweightedat least the key’s minimum evidence). Non-weakness series (motifs, openings, endgames) are not precision targets. - P3 is a rolling 7-day window: the budget is
p3_per_weekminus the tier-3 games whoseselected_atis within the last 7 days. - “Created after the first run” is
played_at.gameshas no insertion time. A game played after the first run was necessarily imported after it, and an older game found by a later backfill never spends the new-games budget.analysis_scheduled_atis set on the account’s first run only and is thefirst_scheduled_at. - Previews on the first run. When
analysis_scheduled_atwas NULL, the job enqueues preview jobs (ADR-R2-15) for the account’s firstPREVIEW_GAMES = 20tier-0 games still to be analysed, read back from the database in analysis order (priority, rated first, newest first).analysis_scheduled_atis set only after that enqueue succeeds, so a failed preview enqueue leaves the account on its first run and the retry enqueues the same previews (preview:<game>dedupes a repeat). Later runs enqueue none. - No primary time class (every game future-dated): nothing is selected and the markers are kept, so a later run reconsiders the games.
No imported game is lost (reconciler, Task 29)
Section titled “No imported game is lost (reconciler, Task 29)”importedgames are never enqueued for analysis by the sweep.pending_sinceon animportedrow means “waiting for the scheduler since”: the column default sets it at insert, and every schedule run clears it on eachimportedgame it read and did not select. A game committed after a run read the account (its import’s schedule enqueue deduped onto that running job), or the games of an import that failed for good, keep it.- Each sweep gives a
schedule-analysisjob to every account with such a game older thanpending_grace(2 min) and no live import job (a running import enqueues it when it ends). The dedupe key makes this idempotent. - Cost: migration
20261121020300adds the partial indexgames_imported_waiting_idx (account_id) WHERE analysis_status = 'imported' AND pending_since IS NOT NULL. The sweep reads only the waiting rows (SELECT DISTINCT account_id ...), so its cost is the number of stragglers, not of imported games. Plan-guarded (custom and generic plans, 50k seen imported rows): the index is used andgamesis never seq-scanned.
Worker wiring (Task 29)
Section titled “Worker wiring (Task 29)”- The worker runs a
schedule-analysisrunner (2 at once, 2 min hard timeout) and apreview-gamerunner (1 at once, 5 min hard timeout) next to import, analyse and reconcile.POOL_HEADROOMgrows from 8 to 13 connections for them. - Only the analyse runner claims with
FairRoundRobin(ADR-R2-15):analyse-gameis the only kind whose handler recordsanalysis_started_at, which the round robin counts as served. Every other runner, including R3’sinteractive-analyse, keeps R1’sPriorityFifo, set explicitly in each runner config.fair_order_sql()is exact only for the first4 x limitrows; callers that need a deeper order pass a larger limit (Task 30). - The CLI’s
analysecommand waits for import, schedule-analysis and analyse jobs, and does not wait forimportedgames: those were not selected and wait for a later run.