Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Merge branch 'worktree-agent-a5c94df323642630e' | 1 | /** |
| 2 | * Repository pages that update live: which changes each page cares about, | |
| 3 | * and when a page may refresh itself. Pure; the hook that uses them is | |
| 4 | * lib/use-repo-live.ts. | |
| 5 | * | |
| 6 | * A tab on a repository page watches the repository over the live socket | |
| 7 | * (`/-/live`, services/notify): the site checks the person may read it, | |
| 8 | * then every push, pull request, issue, comment, review, check, run, | |
| 9 | * release and security alert there arrives as a small `RepoChange`. A page | |
| 10 | * refreshes its loader when a change matches it, a second after the last | |
| 11 | * one, and never under someone typing or with a dialog open. Signed out, | |
| 12 | * or while the socket is down, it refreshes when the tab comes back into | |
| 13 | * view and once a minute while it is in view instead. | |
| 14 | */ | |
| 15 | import type { RepoChange } from "@g1t/contracts"; | |
| 16 | ||
| 17 | export type { RepoChange }; | |
| 18 | ||
| 19 | /** Which changes a page shows. */ | |
| 20 | export type RepoMatch = (change: RepoChange) => boolean; | |
| 21 | ||
| 22 | /** How long after the last matching change the page refreshes. */ | |
| 23 | export const LIVE_DEBOUNCE_MS = 1000; | |
| 24 | /** However steady the changes, a page waits no longer than this before refreshing. */ | |
| 25 | export const LIVE_MAX_WAIT_MS = 5000; | |
| 26 | /** How often a page held back (someone typing, a dialog open) looks again. */ | |
| 27 | export const LIVE_HOLD_CHECK_MS = 1000; | |
| 28 | /** A tab out of view refreshes at most this often. */ | |
| 29 | export const HIDDEN_MIN_MS = 60_000; | |
| 30 | /** Without the socket: coming back into view refreshes at most this often. */ | |
| 31 | export const FALLBACK_FOCUS_MS = 30_000; | |
| 32 | /** Without the socket: how often a page in view refreshes. */ | |
| 33 | export const FALLBACK_POLL_MS = 60_000; | |
| 34 | /** A watch is asked for again this often while the page stays open (it lasts REPO_WATCH_MS). */ | |
| 35 | export const REWATCH_MS = 10 * 60_000; | |
| 36 | ||
| 37 | // ── Matching ───────────────────────────────────────────────────────────── | |
| 38 | ||
| 39 | const HEADS = "refs/heads/"; | |
| 40 | const TAGS = "refs/tags/"; | |
| 41 | ||
| 42 | /** The branch a full ref names, or null for a tag or anything else. */ | |
| 43 | export function branchOf(ref: string | null | undefined): string | null { | |
| 44 | return ref?.startsWith(HEADS) ? ref.slice(HEADS.length) : null; | |
| 45 | } | |
| 46 | ||
| 47 | /** The tag a full ref names, or null. */ | |
| 48 | export function tagOf(ref: string | null | undefined): string | null { | |
| 49 | return ref?.startsWith(TAGS) ? ref.slice(TAGS.length) : null; | |
| 50 | } | |
| 51 | ||
| 52 | function family(change: RepoChange): string { | |
| 53 | return change.kind.split(".")[0] ?? ""; | |
| 54 | } | |
| 55 | ||
| 56 | /** A push or a deletion of a ref. */ | |
| 57 | export function isRefChange(change: RepoChange): boolean { | |
| 58 | return change.kind === "git.push" || change.kind === "git.deleted" || change.kind === "branch.renamed" || change.kind === "repo.default_branch_changed"; | |
| 59 | } | |
| 60 | ||
| 61 | /** A push to branch `branch` (or its deletion). */ | |
| 62 | export function isPushTo(change: RepoChange, branch: string | null | undefined): boolean { | |
| 63 | return (change.kind === "git.push" || change.kind === "git.deleted") && !!branch && branchOf(change.ref) === branch; | |
| 64 | } | |
| 65 | ||
| 66 | /** Something reported on a commit: a status, a check, a run. */ | |
| 67 | export function isCheck(change: RepoChange): boolean { | |
| 68 | return ["status", "check_run", "check_suite", "checks", "workflow", "run"].includes(family(change)); | |
| 69 | } | |
| 70 | ||
| 71 | const ALERTS = ["secret_scanning_alert", "code_scanning_alert", "vulnerability_alert"]; | |
| 72 | ||
| 73 | /** What each kind of repository page shows. */ | |
| 74 | export const repoPages = { | |
| 75 | /** | |
| 76 | * Files, the overview and a folder: any ref moving (the files and latest | |
| 77 | * commit, the branch menu, recent pushes), checks on commits, releases | |
| 78 | * and deployments. | |
| 79 | */ | |
| 80 | files(): RepoMatch { | |
| 81 | return (change) => isRefChange(change) || isCheck(change) || ["release", "deployment", "deployment_status"].includes(family(change)); | |
| 82 | }, | |
| 83 | /** Commits: pushes to the branch shown, and checks on commits. */ | |
| 84 | commits(branch: string | null): RepoMatch { | |
| 85 | return (change) => (branch == null ? change.kind === "git.push" && !tagOf(change.ref) : isPushTo(change, branch)) || isCheck(change); | |
| 86 | }, | |
| 87 | /** Branches and tags: any ref moving, pull requests (a branch's), checks. */ | |
| 88 | refs(): RepoMatch { | |
| 89 | return (change) => isRefChange(change) || isCheck(change) || family(change) === "pull" || family(change) === "release"; | |
| 90 | }, | |
| 91 | /** Tags and releases. */ | |
| 92 | tags(): RepoMatch { | |
| 93 | return (change) => (isRefChange(change) && (tagOf(change.ref) != null || change.ref == null)) || family(change) === "release"; | |
| 94 | }, | |
| 95 | /** The pull requests list: pull requests and what is said and checked on them. */ | |
| 96 | pulls(): RepoMatch { | |
| 97 | return (change) => ["pull", "review", "checks", "queue"].includes(family(change)) || (family(change) === "comment" && change.number != null) || isCheck(change); | |
| 98 | }, | |
| 99 | /** The issues list: issues, comments on them, and pull requests that close them. */ | |
| 100 | issues(): RepoMatch { | |
| 101 | return (change) => ["issue", "comment"].includes(family(change)) || ["pull.opened", "pull.merged", "pull.closed", "pull.reopened"].includes(change.kind); | |
| 102 | }, | |
| 103 | /** | |
| 104 | * A pull request: its own changes (commits, reviews, comments, checks, | |
| 105 | * merged or closed), pushes to its head branch, and what is reported on | |
| 106 | * its head commit. | |
| 107 | */ | |
| 108 | pull(number: number, head?: { branch?: string | null; sha?: string | null }): RepoMatch { | |
| 109 | return (change) => | |
| 110 | (change.number === number && family(change) !== "issue") || | |
| 111 | (!!head?.branch && isPushTo(change, head.branch)) || | |
| 112 | (!!head?.sha && change.sha === head.sha && isCheck(change)) || | |
| 113 | change.kind === "queue.changed"; | |
| 114 | }, | |
| 115 | /** An issue: its own changes and comments, and the pull requests linked to it. */ | |
| 116 | issue(number: number, linked: number[] = []): RepoMatch { | |
| 117 | return (change) => change.number != null && (change.number === number || linked.includes(change.number)); | |
| 118 | }, | |
| 119 | /** The runs list: runs starting, moving and finishing, and what starts them. */ | |
| 120 | runs(): RepoMatch { | |
| 121 | return (change) => ["workflow", "run", "status", "check_suite"].includes(family(change)); | |
| 122 | }, | |
| 123 | /** A run: its own jobs and its end, and what is reported on its commit. */ | |
| 124 | run(id: string, sha?: string | null): RepoMatch { | |
| 125 | return (change) => change.run === id || (!!sha && change.sha === sha && family(change) === "status"); | |
| 126 | }, | |
| 127 | /** Security: alerts opening, closing and being dismissed, and the pull requests that fix them. */ | |
| 128 | security(): RepoMatch { | |
| 129 | return (change) => ALERTS.includes(family(change)) || change.kind === "pull.merged" || change.kind === "pull.opened"; | |
| 130 | }, | |
| 131 | }; | |
| 132 | ||
| 133 | /** Commits a set of changes brought to a branch, for "N new commits": null when uncounted. */ | |
| 134 | export function newCommits(changes: RepoChange[], branch: string | null): number | null { | |
| 135 | let total = 0; | |
| 136 | for (const change of changes) { | |
| 137 | if (change.kind !== "git.push" || (branch != null && branchOf(change.ref) !== branch)) continue; | |
| 138 | if (change.commits == null) return null; | |
| 139 | total += change.commits; | |
| 140 | } | |
| 141 | return total; | |
| 142 | } | |
| 143 | ||
| 144 | // ── When to refresh ────────────────────────────────────────────────────── | |
| 145 | ||
| 146 | /** What a page knows when deciding whether to refresh now. */ | |
| 147 | export type GateState = { | |
| 148 | /** When the first change still waiting arrived, or null when none is. */ | |
| 149 | pendingSince: number | null; | |
| 150 | /** When the latest change arrived. */ | |
| 151 | lastChangeAt: number | null; | |
| 152 | /** When the page last refreshed. */ | |
| 153 | lastRunAt: number | null; | |
| 154 | }; | |
| 155 | ||
| 156 | export type GateInput = { | |
| 157 | now: number; | |
| 158 | /** Whether the tab is in view. */ | |
| 159 | visible: boolean; | |
| 160 | /** Someone is typing on the page, or a dialog is open. */ | |
| 161 | busy: boolean; | |
| 162 | /** A navigation or a submission is under way: the router reloads the page itself. */ | |
| 163 | navigating: boolean; | |
| 164 | }; | |
| 165 | ||
| 166 | export type GateDecision = { action: "idle" } | { action: "run" } | { action: "wait"; ms: number }; | |
| 167 | ||
| 168 | /** | |
| 169 | * Whether to refresh now for the changes waiting, or how long to wait | |
| 170 | * before asking again. A second after the last change (at most five after | |
| 171 | * the first), never under someone typing or with a dialog open, and at | |
| 172 | * most once a minute for a tab out of view, which refreshes as soon as it | |
| 173 | * comes back instead. | |
| 174 | */ | |
| 175 | export function gate(state: GateState, input: GateInput): GateDecision { | |
| 176 | if (state.pendingSince == null) return { action: "idle" }; | |
| 177 | if (input.busy || input.navigating) return { action: "wait", ms: LIVE_HOLD_CHECK_MS }; | |
| 178 | if (!input.visible && state.lastRunAt != null && input.now - state.lastRunAt < HIDDEN_MIN_MS) { | |
| 179 | return { action: "wait", ms: HIDDEN_MIN_MS - (input.now - state.lastRunAt) }; | |
| 180 | } | |
| 181 | const quiet = input.now - (state.lastChangeAt ?? state.pendingSince); | |
| 182 | const waited = input.now - state.pendingSince; | |
| 183 | if (quiet < LIVE_DEBOUNCE_MS && waited < LIVE_MAX_WAIT_MS) { | |
| 184 | return { action: "wait", ms: Math.min(LIVE_DEBOUNCE_MS - quiet, LIVE_MAX_WAIT_MS - waited) }; | |
| 185 | } | |
| 186 | return { action: "run" }; | |
| 187 | } | |
| 188 | ||
| 189 | /** | |
| 190 | * Without the socket (signed out, or while it is down): whether to refresh | |
| 191 | * now. `focus` when the tab comes back into view, at most every 30 s; | |
| 192 | * `poll` once a minute while it is in view. Never with the socket up. | |
| 193 | */ | |
| 194 | export function fallbackDue(input: { live: boolean; visible: boolean; now: number; lastRunAt: number | null; reason: "focus" | "poll" }): boolean { | |
| 195 | if (input.live || !input.visible) return false; | |
| 196 | const since = input.lastRunAt == null ? Infinity : input.now - input.lastRunAt; | |
| 197 | return since >= (input.reason === "focus" ? FALLBACK_FOCUS_MS : FALLBACK_POLL_MS); | |
| 198 | } | |
| 199 | ||
| 200 | /** What has focus, as `isTyping` reads it. */ | |
| 201 | export type FocusedElement = { tagName: string; type?: string | null; isContentEditable?: boolean; readOnly?: boolean; disabled?: boolean } | null; | |
| 202 | ||
| 203 | const NOT_TYPED = new Set(["button", "checkbox", "radio", "submit", "reset", "file", "image", "color", "range", "hidden"]); | |
| 204 | ||
| 205 | /** Whether the focused element is somewhere one types: a text box, a text area, an editor. */ | |
| 206 | export function isTyping(focused: FocusedElement): boolean { | |
| 207 | if (!focused || focused.disabled || focused.readOnly) return false; | |
| 208 | if (focused.isContentEditable) return true; | |
| 209 | const tag = focused.tagName.toUpperCase(); | |
| 210 | if (tag === "TEXTAREA") return true; | |
| 211 | if (tag === "INPUT") return !NOT_TYPED.has((focused.type ?? "text").toLowerCase()); | |
| 212 | return false; | |
| 213 | } | |
| 214 | ||
| 215 | /** Whether a page that tells it is at its bottom: then new timeline items are followed. */ | |
| 216 | export function atBottom(scroll: { top: number; height: number; viewport: number }, slack = 120): boolean { | |
| 217 | return scroll.top + scroll.viewport >= scroll.height - slack; | |
| 218 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.