/** * WHAT A FIRST PULL CARRIES FIRST (owner, 21 Nov 2026: Chat took about 25 * minutes to look right on an account of 140,000 messages, because a pass * from nothing carried every big table oldest first). * * A kit says, per table, which of its rows a person looks at first: * * "first": { * "thread": { "by": "last_message_at", "unless": ["trashed_at"] }, * "message": { "by": "sent_at", "per": "thread_id", "of": "thread", "rows": 50 } * } * * `by` a column the rows are ordered by, newest first (`"order": "asc"` for * the other way, a script's scenes by position); `rows` how many come first * (of the table, or of each group), absent all; `per` a link column whose * groups, rows of the table `of` in that table's own first order, each bring * their first rows (`groups` how many of them, absent all; the first `early` * of them before the small tables, so the most recent chats look right * before the contacts and pictures have come); `unless` columns * whose rows wait (an archived note); `around` [before, after] days around * today that come first (Days); `late` a table whose rows wait, in every run, * behind the tables that are not late (Comms' mail behind its chats, 21 Nov * 2026: a late table's first rows come after every chat's newest messages, * and its long tail after the chats' older messages). Every other row of the table comes after, in * the same order (the long tail), once the small tables and every other * table have come (`Store.firstPage`). Every column named is one the object * holds in the clear (checked by test/first.test.ts against the seal rule). * * The Worker holds every kit's declarations, so a phone of any build gets the * order: the cursor is the Worker's, and the phone only follows it. */ import { KITS } from "./kits.ts"; import { addDays } from "./protocol.ts"; import type { FirstSpec } from "./store.ts"; /** One table's declaration, as a kit writes it. */ export interface FirstDecl { by: string; order?: "asc" | "desc"; rows?: number; per?: string; of?: string; groups?: number; early?: number; unless?: string[]; around?: [number, number]; late?: boolean; } const NAME = /^[a-z_][a-z0-9_]*$/; /** Why a declaration does not read, or null. */ export function firstProblem(table: string, d: unknown): string | null { if (!NAME.test(table)) return `${table} is not a table id`; if (!d || typeof d !== "object" || Array.isArray(d)) return `${table}: a declaration is an object`; const o = d as Record; const known = new Set(["by", "order", "rows", "per", "of", "groups", "early", "unless", "around", "late"]); for (const k of Object.keys(o)) if (!known.has(k)) return `${table}: ${k} is not a word of first`; if (typeof o.by !== "string" || !NAME.test(o.by)) return `${table}: by names a column`; if (o.order !== undefined && o.order !== "asc" && o.order !== "desc") return `${table}: order is asc or desc`; const count = (v: unknown): boolean => v === undefined || (typeof v === "number" && Number.isInteger(v) && v >= 1); if (!count(o.rows) || !count(o.groups) || !count(o.early)) return `${table}: rows, groups and early are counts`; if ((o.per === undefined) !== (o.of === undefined)) return `${table}: per and of come together`; if (o.per !== undefined && (typeof o.per !== "string" || !NAME.test(o.per) || typeof o.of !== "string" || !NAME.test(o.of))) return `${table}: per names a column and of a table`; if ((o.groups !== undefined || o.early !== undefined) && o.per === undefined) return `${table}: groups and early need per`; if (o.unless !== undefined && !(Array.isArray(o.unless) && o.unless.every((c) => typeof c === "string" && NAME.test(c)))) return `${table}: unless lists columns`; if (o.late !== undefined && typeof o.late !== "boolean") return `${table}: late is true or false`; if (o.around !== undefined && !(Array.isArray(o.around) && o.around.length === 2 && o.around.every((n) => Number.isInteger(n) && n >= 0))) return `${table}: around is [days before, days after]`; return null; } /** Every table's declaration across the shipped kits; a table two kits declare is declared the same in both. */ export function declarations(kits: Record }>): Map { const out = new Map(); for (const [kit, k] of Object.entries(kits)) { for (const [table, d] of Object.entries(k.first ?? {})) { const problem = firstProblem(table, d); if (problem) throw new Error(`kits/${kit} first: ${problem}`); const held = out.get(table); // Two kits may differ in `late` alone (POST BUILD 2, 25 Nov 2026: Comms keeps mail behind its chats, Post carries // nothing but mail first). Late wins: in a pass of an app whose own first tables are only these, nothing else // is first, so being late changes nothing there; in Comms' pass mail still waits behind the chats. const plain = (x: object) => JSON.stringify({ ...x, late: undefined }); if (held && plain(held) !== plain(d as object)) throw new Error(`kits/${kit} first: ${table} is declared otherwise by another kit`); const late = held?.late === true || (d as FirstDecl).late === true; out.set(table, { ...(d as FirstDecl), ...(late ? { late: true } : {}) }); } } for (const [table, d] of out) if (d.of !== undefined && !out.has(d.of)) throw new Error(`first: ${table} is grouped by ${d.of}, which no kit orders`); return out; } /** * COMMS BUILDS FROM BEFORE COMMS WITHOUT MAIL (d0dcfeeace, 27 Nov 2026) still pull mail beside their chats, and the * Worker orders every build's first pull: their mail stays behind their chats (`late`), as their kit declared it. Post * declares the same order without `late`; late wins (`declarations`), and changes nothing in Post's pass, where mail is * the only first table. Kept until no Comms build that pulls mail is in use. */ export const OLD_COMMS_FIRST: Record }> = { "messages-with-mail": { first: { email_thread: { by: "last_at", rows: 500, unless: ["trashed_at"], late: true }, email: { by: "sent_at", per: "thread_id", of: "email_thread", rows: 2, groups: 500, late: true }, }, }, }; /** The shipped kits' declarations, and the old Comms builds' mail order. */ export const FIRST: Map = declarations({ ...KITS, ...OLD_COMMS_FIRST }); /** * The specs a pass from nothing carries first: each declared table the pass * holds rows of (`held`, its rows in the pass), the late tables last, the * tables without groups first (a chat's row before its messages), then by id; one of at most * `small` rows is `soon`. `today` is the object's local date. */ export function planFirst(held: (table: string) => number, today: string, small: number, decls: Map = FIRST): FirstSpec[] { const specs: FirstSpec[] = []; for (const [table, d] of [...decls].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) { const rows = held(table); if (rows <= 0) continue; const of = d.of !== undefined ? decls.get(d.of)! : null; specs.push({ table, by: d.by, asc: d.order === "asc", rows: d.rows ?? null, unless: d.unless ?? [], around: d.around ? { from: addDays(today, -d.around[0]), to: addDays(today, d.around[1] + 1) } : null, per: d.per !== undefined && of ? { column: d.per, of: d.of!, by: of.by, asc: of.order === "asc", groups: d.groups ?? null, early: d.early ?? null } : null, soon: rows <= small, late: d.late === true, }); } // Not late before late; within each, a table without groups before one with (a chat's row before its messages). const rank = (s: FirstSpec): number => Number(s.late === true) * 2 + Number(s.per !== null); return specs.sort((a, b) => rank(a) - rank(b)); }