Public: anyone can read this; the crew can change it.
Nothing optional loads at boot: route modules and warm-ups (search, samplers, crew) start on first use #16908
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/16852-nothing-optional-loads-at-boot-route-mod → main 0 commits fork
11 files
changed +417
−30
AGENTS.md +2
| @@ -47,6 +47,8 @@ The check queue hands work to a runner through `src/checks/runner.ts` (#13652, R | ||
| 47 | 47 | |
| 48 | 48 | `src/core/transfer.ts`: the old owner/name pairs a transfer left behind (`repo_redirect`), held per database after the first request reads them and dropped by `transferRepo`, so a request under a path nothing moved from costs no statement (#569). |
| 49 | 49 | |
| 50 | A route family whose module loads on its first request (#16852) is a list in `src/http/lazy_lists.ts` and one `lazyRoutes` line in `src/http/server.ts` (`/settings…`, `/forges…` and MCP so far); `test/lazy-first-use.test.ts` holds each list to its module's own routes and the boot's imports clear of the module. The search warm starts on the first search anyone makes (`warmOnFirstSearch`, `src/search/warm.ts`), not at boot. The 15 s machine sampler and the memory sampler stay at boot: their history is what a page reads later, and a sampler started on first use would have none. | |
| 51 | ||
| 50 | 52 | `src/http/mcp/load.ts`: `loading`, the one import of the MCP server and its tool index (#12521), started by the first request that needs the tools; when it settles it hands the list to /llms.txt (`setMcpTools`) and the cards (`setCardTools`), so a reader of those lists awaits `loadMcp()` first. A failed import is dropped and tried again; gone on restart. |
| 51 | 53 | |
| 52 | 54 | `src/actions/order.ts`: `spawningNow`, per forge, the orders whose spawn this process is running, so a slow spawn that reads as stale by its row's age is never started twice by a reload or a retry (#14779); one id per spawn in flight, gone when it settles and on restart (when a `spawning` row past `STALE_MS` is a lost spawn, and is retried). |
src/actions/search.ts +18
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | import { isActive } from "../core/users.ts"; |
| 15 | 15 | import { canSeeSealed } from "../core/sealed.ts"; |
| 16 | 16 | import type { App } from "../app.ts"; |
| 17 | import { during } from "../perf/jobs.ts"; | |
| 17 | 18 | import { appsForRepos } from "../core/apps.ts"; |
| 18 | 19 | import { listOrigins } from "../core/origins.ts"; |
| 19 | 20 | import { canRead, enterableApps, listReposVisibleTo } from "../core/repos.ts"; |
| @@ -118,6 +119,18 @@ function hasEnterableApp(app: App, actor: User | undefined, repo: Repo): boolean | ||
| 118 | 119 | ); |
| 119 | 120 | } |
| 120 | 121 | |
| 122 | /** What each forge runs once, after its first non-empty search (`onFirstSearch`). */ | |
| 123 | const afterFirst = new WeakMap<App, () => void>(); | |
| 124 | ||
| 125 | /** | |
| 126 | * Run `start` once, on a later turn, after the first search this forge answers (#16852): work a | |
| 127 | * search benefits from (`search/warm.ts`) starts when someone first searches, not at boot. One | |
| 128 | * entry per forge, dropped when it fires. | |
| 129 | */ | |
| 130 | export function onFirstSearch(app: App, start: () => void): void { | |
| 131 | afterFirst.set(app, start); | |
| 132 | } | |
| 133 | ||
| 121 | 134 | export async function search( |
| 122 | 135 | app: App, |
| 123 | 136 | actor: User | undefined, |
| @@ -139,6 +152,11 @@ export async function search( | ||
| 139 | 152 | codeSkipped: false, |
| 140 | 153 | }; |
| 141 | 154 | if (q === "") return empty; |
| 155 | const first = afterFirst.get(app); | |
| 156 | if (first !== undefined) { | |
| 157 | afterFirst.delete(app); | |
| 158 | setTimeout(() => during("search-warm-start", first, undefined, app), 0); | |
| 159 | } | |
| 142 | 160 | |
| 143 | 161 | const wants = (which: SearchKind) => kind === "all" || kind === which; |
| 144 | 162 |
src/http/lazy_lists.ts added +257
| @@ -0,0 +1,257 @@ | ||
| 1 | /** | |
| 2 | * The routes of the families whose module loads on its first request (`lazyRoutes`, #16852), | |
| 3 | * registered at boot in front of the module so matching, the admin gate and 405s answer as they | |
| 4 | * did. `test/boot-lazy.test.ts` holds each list to its module's own, so a route added there and | |
| 5 | * not here fails a test, not a visitor. Order is the module's, and it matters beside a `:name`. | |
| 6 | */ | |
| 7 | ||
| 8 | /** `settingsRoutes` (`settings_routes.ts`): every `/settings…` page. */ | |
| 9 | export const SETTINGS_ROUTES = [ | |
| 10 | ["GET", "/settings/trusted"], | |
| 11 | ["POST", "/settings/trusted"], | |
| 12 | ["POST", "/settings/trusted/stop"], | |
| 13 | ["POST", "/settings/trusted/revert"], | |
| 14 | ["POST", "/settings/trusted/:id/remove"], | |
| 15 | ["GET", "/settings/runners"], | |
| 16 | ["POST", "/settings/runners/:id/remove"], | |
| 17 | ["POST", "/settings/runners/:id/revoke"], | |
| 18 | ["POST", "/settings/runners/:id/resume"], | |
| 19 | ["GET", "/settings/telemetry"], | |
| 20 | ["POST", "/settings/telemetry"], | |
| 21 | ["GET", "/settings"], | |
| 22 | ["POST", "/settings"], | |
| 23 | ["POST", "/settings/account/close"], | |
| 24 | ["POST", "/settings/account/keep"], | |
| 25 | ["POST", "/settings/account/close/options"], | |
| 26 | ["POST", "/settings/notify"], | |
| 27 | ["POST", "/settings/focus"], | |
| 28 | ["POST", "/settings/dotfiles"], | |
| 29 | ["POST", "/settings/edge/link"], | |
| 30 | ["POST", "/settings/edge/unlink"], | |
| 31 | ["POST", "/settings/parent/link"], | |
| 32 | ["POST", "/settings/parent/unlink"], | |
| 33 | ["POST", "/settings/email/verify"], | |
| 34 | ["GET", "/settings/password"], | |
| 35 | ["POST", "/settings/password"], | |
| 36 | ["GET", "/settings/everyday"], | |
| 37 | ["POST", "/settings/everyday"], | |
| 38 | ["POST", "/settings/everyday/options"], | |
| 39 | ["POST", "/settings/everyday/switch"], | |
| 40 | ["POST", "/settings/everyday/to-admin"], | |
| 41 | ["POST", "/settings/everyday/keep"], | |
| 42 | ["GET", "/settings/appearance"], | |
| 43 | ["GET", "/settings/payments"], | |
| 44 | ["GET", "/settings/payments.csv"], | |
| 45 | ["GET", "/settings/shells"], | |
| 46 | ["POST", "/settings/shells/stop-idle"], | |
| 47 | ["POST", "/settings/shells/:id/stop"], | |
| 48 | ["GET", "/settings/tokens"], | |
| 49 | ["POST", "/settings/tokens"], | |
| 50 | ["POST", "/settings/tokens/:id/delete"], | |
| 51 | ["GET", "/settings/grants"], | |
| 52 | ["POST", "/settings/grants/:id/revoke"], | |
| 53 | ["POST", "/settings/sessions/end-others"], | |
| 54 | ["POST", "/settings/sessions/:id/delete"], | |
| 55 | ["POST", "/settings/gifts"], | |
| 56 | ["GET", "/settings/confirms"], | |
| 57 | ["POST", "/settings/confirms/:id"], | |
| 58 | ["GET", "/settings/passkeys"], | |
| 59 | ["POST", "/settings/passkeys/options"], | |
| 60 | ["POST", "/settings/passkeys"], | |
| 61 | ["POST", "/settings/passkeys/:id/delete"], | |
| 62 | ["GET", "/settings/subscriptions"], | |
| 63 | ["POST", "/settings/subscriptions"], | |
| 64 | ["POST", "/settings/crew/key"], | |
| 65 | ["POST", "/settings/crew/budgets"], | |
| 66 | ["POST", "/settings/subscriptions/:id/delete"], | |
| 67 | ["POST", "/settings/subscriptions/allow"], | |
| 68 | ["POST", "/settings/subscriptions/:id/wake"], | |
| 69 | ["POST", "/settings/subscriptions/:id/headroom"], | |
| 70 | ["POST", "/settings/subscriptions/:id/budget"], | |
| 71 | ["GET", "/settings/secrets"], | |
| 72 | ["POST", "/settings/secrets"], | |
| 73 | ["POST", "/settings/secrets/delete"], | |
| 74 | ["GET", "/settings/devices"], | |
| 75 | ["POST", "/settings/devices"], | |
| 76 | ["POST", "/settings/devices/delete"], | |
| 77 | ["POST", "/settings/devices/grant"], | |
| 78 | ["POST", "/settings/devices/revoke"], | |
| 79 | ["POST", "/settings/devices/cap"], | |
| 80 | ["GET", "/settings/cloud"], | |
| 81 | ["POST", "/settings/cloud"], | |
| 82 | ["POST", "/settings/cloud/verify"], | |
| 83 | ["POST", "/settings/cloud/write"], | |
| 84 | ["POST", "/settings/cloud/ceiling"], | |
| 85 | ["POST", "/settings/cloud/watch-ack"], | |
| 86 | ["POST", "/settings/cloud/project"], | |
| 87 | ["POST", "/settings/cloud/audit"], | |
| 88 | ["GET", "/settings/cloud/audit"], | |
| 89 | ["POST", "/settings/cloud/rotate"], | |
| 90 | ["POST", "/settings/cloud/rename"], | |
| 91 | ["POST", "/settings/cloud/remove"], | |
| 92 | ["GET", "/settings/access"], | |
| 93 | ["GET", "/settings/resilience"], | |
| 94 | ["GET", "/settings/hosted-forges"], | |
| 95 | ["POST", "/settings/hosted-forges/reach"], | |
| 96 | ["POST", "/settings/hosted-forges/reach/add"], | |
| 97 | ["POST", "/settings/hosted-forges/reach/remove"], | |
| 98 | ["GET", "/settings/capacity"], | |
| 99 | ["POST", "/settings/capacity"], | |
| 100 | ["GET", "/settings/economics"], | |
| 101 | ["POST", "/settings/economics"], | |
| 102 | ["GET", "/settings/billing"], | |
| 103 | ["GET", "/settings/billing/change"], | |
| 104 | ["POST", "/settings/billing/grant"], | |
| 105 | ["POST", "/settings/billing/links"], | |
| 106 | ["POST", "/settings/billing/portal"], | |
| 107 | ["POST", "/settings/billing/stripe/connect"], | |
| 108 | ["POST", "/settings/billing/stripe/secret"], | |
| 109 | ["POST", "/settings/billing/stripe/disconnect"], | |
| 110 | ["POST", "/settings/billing/orders/retry"], | |
| 111 | ["POST", "/settings/billing/codes"], | |
| 112 | ["POST", "/settings/billing/codes/revoke"], | |
| 113 | ["GET", "/settings/flags"], | |
| 114 | ["GET", "/settings/captain"], | |
| 115 | ["POST", "/settings/captain"], | |
| 116 | ["GET", "/settings/packs"], | |
| 117 | ["POST", "/settings/packs"], | |
| 118 | ["GET", "/settings/features"], | |
| 119 | ["GET", "/settings/features/:slug"], | |
| 120 | ["POST", "/settings/features/:slug"], | |
| 121 | ["POST", "/settings/features/:slug/run/:tool"], | |
| 122 | ["POST", "/settings/features/:slug/absorb"], | |
| 123 | ["POST", "/settings/features/:slug/unabsorb"], | |
| 124 | ["POST", "/settings/features/:slug/accept"], | |
| 125 | ["POST", "/settings/flags"], | |
| 126 | ["GET", "/settings/app-templates"], | |
| 127 | ["POST", "/settings/app-templates"], | |
| 128 | ["GET", "/settings/usage"], | |
| 129 | ["GET", "/settings/spend"], | |
| 130 | ["GET", "/settings/traffic"], | |
| 131 | ["POST", "/settings/traffic/returning"], | |
| 132 | ["GET", "/settings/traffic/sources"], | |
| 133 | ["GET", "/settings/traffic/scrapes"], | |
| 134 | ["GET", "/settings/traffic/scrapes/export"], | |
| 135 | ["POST", "/settings/traffic/scrapes/capture"], | |
| 136 | ["POST", "/settings/traffic/scrapes/stop"], | |
| 137 | ["POST", "/settings/traffic/scrapes/networks"], | |
| 138 | ["GET", "/settings/security"], | |
| 139 | ["GET", "/settings/security/controls"], | |
| 140 | ["POST", "/settings/security/shield"], | |
| 141 | ["GET", "/settings/backups"], | |
| 142 | ["GET", "/settings/backups/live"], | |
| 143 | ["POST", "/settings/backups/now"], | |
| 144 | ["POST", "/settings/backups/drill"], | |
| 145 | ["POST", "/settings/backups/destination"], | |
| 146 | ["POST", "/settings/backups/test"], | |
| 147 | ["POST", "/settings/backups/clear"], | |
| 148 | ["POST", "/settings/backups/kit"], | |
| 149 | ["POST", "/settings/backups/kit/key"], | |
| 150 | ["POST", "/settings/backups/mirror"], | |
| 151 | ["POST", "/settings/backups/mirror/clear"], | |
| 152 | ["POST", "/settings/backups/mirror/now"], | |
| 153 | ["GET", "/settings/accounts"], | |
| 154 | ["POST", "/settings/accounts/:username/allow"], | |
| 155 | ["POST", "/settings/accounts/:username/decline"], | |
| 156 | ["GET", "/settings/people"], | |
| 157 | ["POST", "/settings/people/:username/role"], | |
| 158 | ["GET", "/settings/model"], | |
| 159 | ["POST", "/settings/model"], | |
| 160 | ["POST", "/settings/model/connections"], | |
| 161 | ["POST", "/settings/model/connections/:id/test"], | |
| 162 | ["POST", "/settings/model/connections/:id/remove"], | |
| 163 | ["POST", "/settings/accounts/:username/model"], | |
| 164 | ["POST", "/settings/accounts/:username/ceiling"], | |
| 165 | ["GET", "/settings/accounts/:username"], | |
| 166 | ["POST", "/settings/accounts/:username/reset"], | |
| 167 | ["POST", "/settings/accounts/:username/end-sessions"], | |
| 168 | ["POST", "/settings/accounts/:username/tokens/:id/delete"], | |
| 169 | ["POST", "/settings/accounts/:username/sessions/:id/delete"], | |
| 170 | ["POST", "/settings/accounts/:username/suspend"], | |
| 171 | ["POST", "/settings/accounts/:username/unsuspend"], | |
| 172 | ["POST", "/settings/accounts/:username/remove"], | |
| 173 | ["GET", "/settings/accounts/:username/retire"], | |
| 174 | ["POST", "/settings/accounts/:username/retire"], | |
| 175 | ["POST", "/settings/accounts/:username/verify"], | |
| 176 | ["POST", "/settings/accounts/:username/way-back"], | |
| 177 | ["GET", "/settings/crew"], | |
| 178 | ["POST", "/settings/crew/knobs"], | |
| 179 | ["POST", "/settings/crew"], | |
| 180 | ["GET", "/settings/teams"], | |
| 181 | ["GET", "/settings/teams/stream"], | |
| 182 | ["GET", "/settings/teams/:slug"], | |
| 183 | ["POST", "/settings/teams/:slug"], | |
| 184 | ["GET", "/settings/crew/:username"], | |
| 185 | ["POST", "/settings/crew/:username"], | |
| 186 | ["POST", "/settings/crew/:username/delete"], | |
| 187 | ["POST", "/settings/crew/try-again/:owner/:name/:number"], | |
| 188 | ["POST", "/settings/crew/model-review/:id/take"], | |
| 189 | ["POST", "/settings/crew/model-review/:id/decline"], | |
| 190 | ["GET", "/settings/origins/callback"], | |
| 191 | ["GET", "/settings/origins/broker/callback"], | |
| 192 | ["GET", "/settings/origins"], | |
| 193 | ["POST", "/settings/origins/token"], | |
| 194 | ["POST", "/settings/origins/oauth"], | |
| 195 | ["POST", "/settings/origins/reach"], | |
| 196 | ["POST", "/settings/origins/reach/dismiss"], | |
| 197 | ["POST", "/settings/origins/apps"], | |
| 198 | ["POST", "/settings/origins/trusted-hosts"], | |
| 199 | ["POST", "/settings/origins/apps/:id/delete"], | |
| 200 | ["GET", "/settings/origins/flows/:id"], | |
| 201 | ["POST", "/settings/origins/flows/:id/poll"], | |
| 202 | ["GET", "/settings/origins/flows/:id/stream"], | |
| 203 | ["GET", "/settings/origins/:id/repos"], | |
| 204 | ["GET", "/settings/origins/:id/archived"], | |
| 205 | ["POST", "/settings/origins/:id/archived"], | |
| 206 | ["POST", "/settings/origins/:id/mirror"], | |
| 207 | ["GET", "/settings/origins/:id/mirror/:job"], | |
| 208 | ["POST", "/settings/origins/:id/sync"], | |
| 209 | ["POST", "/settings/origins/:id/check"], | |
| 210 | ["POST", "/settings/origins/:id/delete"], | |
| 211 | ["POST", "/settings/origins/:id/token"], | |
| 212 | ["GET", "/settings/keys"], | |
| 213 | ["POST", "/settings/keys"], | |
| 214 | ["POST", "/settings/keys/:id/delete"], | |
| 215 | ] as const; | |
| 216 | ||
| 217 | /** `forgesRoutes` (`forges_routes.ts`): `/forges…`, `/family`, `/pricing…` and `/templates…`. */ | |
| 218 | export const FORGES_ROUTES = [ | |
| 219 | ["GET", "/forges"], | |
| 220 | ["GET", "/forges/export.csv"], | |
| 221 | ["GET", "/forges/refused"], | |
| 222 | ["GET", "/forges/archive"], | |
| 223 | ["POST", "/forges/archive"], | |
| 224 | ["POST", "/forges/:name/restore"], | |
| 225 | ["GET", "/family"], | |
| 226 | ["GET", "/forges/name/:name"], | |
| 227 | ["GET", "/forges/:name/claim-link"], | |
| 228 | ["GET", "/forges/:name/recovery-link"], | |
| 229 | ["POST", "/forges/:name/recovery-link"], | |
| 230 | ["GET", "/forges/:name/bill"], | |
| 231 | ["POST", "/forges/:name/claim-link"], | |
| 232 | ["GET", "/forges/:name/revoke-link"], | |
| 233 | ["POST", "/forges/:name/revoke-link"], | |
| 234 | ["POST", "/forges/:name/undo-link"], | |
| 235 | ["POST", "/forges/:name/stop"], | |
| 236 | ["POST", "/forges/:name/sandbox"], | |
| 237 | ["POST", "/forges/:name/reach/allow"], | |
| 238 | ["POST", "/forges/:name/reach/later"], | |
| 239 | ["POST", "/forges/:name/reach/revoke"], | |
| 240 | ["POST", "/forges/:name/own-code"], | |
| 241 | ["POST", "/forges/:name/size"], | |
| 242 | ["GET", "/forges/:name/remove"], | |
| 243 | ["POST", "/forges/:name/remove"], | |
| 244 | ["GET", "/forges/:name/ready"], | |
| 245 | ["GET", "/pricing"], | |
| 246 | ["POST", "/pricing/order"], | |
| 247 | ["GET", "/pricing/done"], | |
| 248 | ["GET", "/pricing/orders/:id"], | |
| 249 | ["GET", "/pricing/orders/:id/receipt.html"], | |
| 250 | ["GET", "/pricing/resend"], | |
| 251 | ["POST", "/pricing/resend"], | |
| 252 | ["GET", "/templates"], | |
| 253 | ["GET", "/templates.json"], | |
| 254 | ["GET", "/forges/new"], | |
| 255 | ["POST", "/forges/new"], | |
| 256 | ["POST", "/forges/start"], | |
| 257 | ] as const; |
src/http/server.ts +8 −4
| @@ -67,8 +67,7 @@ import { measureSubmit } from "./measure.ts"; | ||
| 67 | 67 | import { cspReportSubmit } from "./csp_report.ts"; |
| 68 | 68 | import { scriptErrorSubmit } from "./script_errors.ts"; |
| 69 | 69 | import { stripeHook } from "./stripe_hook.ts"; |
| 70 | import { settingsRoutes } from "./settings_routes.ts"; | |
| 71 | import { forgesRoutes } from "./forges_routes.ts"; | |
| 70 | import { FORGES_ROUTES, SETTINGS_ROUTES } from "./lazy_lists.ts"; | |
| 72 | 71 | import { pluginRoutes } from "./plugin_routes.ts"; |
| 73 | 72 | import { atomRoutes } from "./atom_routes.ts"; |
| 74 | 73 | import { pinnedRoutes } from "./pinned_routes.ts"; |
| @@ -1007,7 +1006,8 @@ export function buildRouter(): Router<Handler> { | ||
| 1007 | 1006 | // "+ New forge": the spawn door as a page, any signed-in person (spawn/door.ts). |
| 1008 | 1007 | // Every /forges…, /family, /pricing, /templates and "+ New forge" route, in its own module |
| 1009 | 1008 | // (forges_routes.ts); literals, above /:owner. |
| 1010 | forgesRoutes(router); | |
| 1009 | // Its module loads on the first request to one of its paths (#16852). | |
| 1010 | lazyRoutes(router, "forges", FORGES_ROUTES, async () => (await import("./forges_routes.ts")).forgesRoutes); | |
| 1011 | 1011 | // The front page as a stranger sees it, for anyone; a site admin there |
| 1012 | 1012 | // also gets the controls that decide it (#686). |
| 1013 | 1013 | router.add("GET", "/front", studioDoor); |
| @@ -1169,7 +1169,11 @@ export function buildRouter(): Router<Handler> { | ||
| 1169 | 1169 | router.add("GET", "/broker/origins/start", (ctx) => brokerStart(ctx, oauthCallbackUri(ctx.url, ctx.app.config.host))); |
| 1170 | 1170 | // Every /settings… page, in its own module (settings_routes.ts); literal, above /:owner. |
| 1171 | 1171 | // The usage page is handed the pattern list, so settings_routes.ts never imports server.ts. |
| 1172 | settingsRoutes(router, knownPatterns); | |
| 1172 | // Its module loads on the first request under /settings (#16852). | |
| 1173 | lazyRoutes(router, "settings", SETTINGS_ROUTES, async () => { | |
| 1174 | const { settingsRoutes } = await import("./settings_routes.ts"); | |
| 1175 | return (own) => settingsRoutes(own, knownPatterns); | |
| 1176 | }); | |
| 1173 | 1177 | // Connect a cloud (#16696): the guided front door to building on a cloud; /infra links here. |
| 1174 | 1178 | router.add("GET", "/cloud/connect", cloudConnectPage); |
| 1175 | 1179 | router.add("GET", "/cloud/connect/:provider", cloudConnectProvider); |
src/main.ts +3 −3
| @@ -29,7 +29,7 @@ import { createApp } from "./app.ts"; | ||
| 29 | 29 | import { survivesFull } from "./kernel/full_disk.ts"; |
| 30 | 30 | import { readOnlyFrom } from "./http/read_only.ts"; |
| 31 | 31 | import { warmDisplays } from "./display/known.ts"; |
| 32 | import { warmSearch } from "./search/warm.ts"; | |
| 32 | import { warmOnFirstSearch } from "./search/warm.ts"; | |
| 33 | 33 | import { warmFlow } from "./system/flow_cache.ts"; |
| 34 | 34 | import { noteDbSize } from "./system/db_size.ts"; |
| 35 | 35 | import { refreshCatalogQuietly } from "./packs/catalog.ts"; |
| @@ -832,8 +832,8 @@ const previewForge = (process.env.FORGE_APP_ENV ?? "").trim().toLowerCase() === | ||
| 832 | 832 | void (previewForge ? Promise.resolve() : keepSandboxing(app)).finally(() => bootHostedChildren(app)); |
| 833 | 833 | // The pipeline answer and every repository's display read (#8978, #11007) were filled here, behind |
| 834 | 834 | // the listener; they are read before it opens now (above). The release probe's search, grepped once |
| 835 | // behind the listener so the first visit reads the cache (#11059). | |
| 836 | void warmSearch(app); | |
| 835 | // on the first search anyone makes, not at boot (#11059, #16852). | |
| 836 | warmOnFirstSearch(app); | |
| 837 | 837 | // A spawned child's first boot: its direction, opened once for its planner |
| 838 | 838 | // (spawn/first-boot.ts). A forge that was not spawned is untouched. |
| 839 | 839 | await firstBoot(app); |
src/search/warm.ts +10 −1
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | import type { App } from "../app.ts"; |
| 14 | import { search } from "../actions/search.ts"; | |
| 14 | import { onFirstSearch, search } from "../actions/search.ts"; | |
| 15 | 15 | import { budgetTimer, type BudgetTimer } from "../core/search.ts"; |
| 16 | 16 | |
| 17 | 17 | /** A query long enough to grep (`MIN_CODE_QUERY`), so the warm fills the grep cache. */ |
| @@ -69,3 +69,12 @@ export async function warmSearch( | ||
| 69 | 69 | else if (raced === "done") app.log.info("search warmed", { ms }); |
| 70 | 70 | return report; |
| 71 | 71 | } |
| 72 | ||
| 73 | /** | |
| 74 | * Warm on first use (#16852): nothing is read at boot; the first search anyone makes starts the | |
| 75 | * warm on the next turn, so a forge nobody searches never pays for it. The first search itself is | |
| 76 | * answered as it always was, cold and budgeted. | |
| 77 | */ | |
| 78 | export function warmOnFirstSearch(app: App, options: WarmOptions = {}): void { | |
| 79 | onFirstSearch(app, () => void warmSearch(app, WARM_QUERY, options)); | |
| 80 | } |
test/boot-idle-memory.test.ts +4 −3
| @@ -18,10 +18,11 @@ import { productionEnv } from "./helpers/production-env.ts"; | ||
| 18 | 18 | const MAIN = join(import.meta.dir, "..", "src", "main.ts"); |
| 19 | 19 | const MIB = 1048576; |
| 20 | 20 | /** |
| 21 | * The most RSS an idle fresh forge may settle at: 281 MiB measured on Linux 2026-11-30, plus a | |
| 22 | * margin for a loaded host. | |
| 21 | * The most RSS an idle fresh forge may settle at: 255–265 MiB measured on Linux 2026-11-30 once | |
| 22 | * the settings and forges route modules and the search warm stopped loading at boot (#16852; 281 | |
| 23 | * before), plus a margin for a loaded host. | |
| 23 | 24 | */ |
| 24 | const IDLE_RSS_BUDGET = 400 * MIB; | |
| 25 | const IDLE_RSS_BUDGET = 360 * MIB; | |
| 25 | 26 | /** Why the budget is not held off Linux. */ |
| 26 | 27 | const IDLE_SKIP_REASON = "RSS is measured from /proc on Linux; other hosts count resident memory differently"; |
| 27 | 28 | /** Real time, named: RSS settles over seconds of a real process, there is no event to await. */ |
test/boot-lazy.test.ts +3 −19
| @@ -4,28 +4,12 @@ | ||
| 4 | 4 | * request that needs them, not by the boot's static imports. |
| 5 | 5 | */ |
| 6 | 6 | import { describe, expect, test } from "bun:test"; |
| 7 | import { existsSync, readFileSync } from "node:fs"; | |
| 8 | import { dirname, resolve } from "node:path"; | |
| 7 | import { readFileSync } from "node:fs"; | |
| 8 | import { resolve } from "node:path"; | |
| 9 | 9 | import type { Ctx, Handler } from "../src/http/context.ts"; |
| 10 | 10 | import { lazyRoutes } from "../src/http/lazy_routes.ts"; |
| 11 | 11 | import { Router } from "../src/http/router.ts"; |
| 12 | ||
| 13 | const FROM = /(?:^|\n)\s*(?:import|export)\s+(?!type\b)[^;]*?from\s+"(\.[^"]+)"|(?:^|\n)\s*import\s+"(\.[^"]+)"/g; | |
| 14 | ||
| 15 | function staticClosure(entry: string): Set<string> { | |
| 16 | const seen = new Set<string>(); | |
| 17 | const todo = [entry]; | |
| 18 | while (todo.length) { | |
| 19 | const file = todo.pop()!; | |
| 20 | if (seen.has(file)) continue; | |
| 21 | seen.add(file); | |
| 22 | for (const m of readFileSync(file, "utf8").matchAll(FROM)) { | |
| 23 | const path = resolve(dirname(file), m[1] ?? m[2]!); | |
| 24 | if (existsSync(path)) todo.push(path); | |
| 25 | } | |
| 26 | } | |
| 27 | return seen; | |
| 28 | } | |
| 12 | import { staticClosure } from "./helpers/static-closure.ts"; | |
| 29 | 13 | |
| 30 | 14 | test("the boot's static imports do not reach the terminal UI", () => { |
| 31 | 15 | const closure = [...staticClosure(resolve("src/main.ts"))]; |
test/helpers/static-closure.ts added +23
| @@ -0,0 +1,23 @@ | ||
| 1 | /** | |
| 2 | * The files a module reaches through static imports and re-exports (dynamic `import()` is not | |
| 3 | * followed). | |
| 4 | */ | |
| 5 | import { existsSync, readFileSync } from "node:fs"; | |
| 6 | import { dirname, resolve } from "node:path"; | |
| 7 | ||
| 8 | const FROM = /(?:^|\n)\s*(?:import|export)\s+(?!type\b)[^;]*?from\s+"(\.[^"]+)"|(?:^|\n)\s*import\s+"(\.[^"]+)"/g; | |
| 9 | ||
| 10 | export function staticClosure(entry: string): Set<string> { | |
| 11 | const seen = new Set<string>(); | |
| 12 | const todo = [entry]; | |
| 13 | while (todo.length) { | |
| 14 | const file = todo.pop()!; | |
| 15 | if (seen.has(file)) continue; | |
| 16 | seen.add(file); | |
| 17 | for (const m of readFileSync(file, "utf8").matchAll(FROM)) { | |
| 18 | const path = resolve(dirname(file), m[1] ?? m[2]!); | |
| 19 | if (existsSync(path)) todo.push(path); | |
| 20 | } | |
| 21 | } | |
| 22 | return seen; | |
| 23 | } |
test/job-slices.test.ts +1
| @@ -275,6 +275,7 @@ const UNTIMED: Record<string, string> = { | ||
| 275 | 275 | "pinned-watch": "a one-shot timer between the pinned watch's looks at a pinned deploy (actions/pinned_watch.ts WATCH.sleep); it only resolves the sleep, no work of its own; pinned-watch tests drive the watch with a short sleep", |
| 276 | 276 | "runner-wait": "one-shot timers that only resolve a promise: a runner's claim held open, a check's look at its job each second (checks/runner_jobs.ts, runner_step.ts) and the runner program's own pauses; off unless FORGE_CHECKS_PLACEMENT=runner, and checks-runner-placement.test.ts drives all three", |
| 277 | 277 | "git-pace": "a one-shot timer in git/pace.ts that only resolves the promise a paced pack write waits on: no work of its own, armed per pack chunk while a page response is in flight; git-pace.test.ts drives the pacer with a fake clock", |
| 278 | "search-warm-start": "a one-shot zero-delay timer that starts the search warm-up once, after the first search a forge answers (actions/search.ts onFirstSearch); the work itself is search/warm.ts, timed by its own tests; search warm-on-first-use tests drive it", | |
| 278 | 279 | "dataset-export": "every 10 minutes reads the last export's time and returns unless a night has passed; the publish itself runs once a night in actions/dataset.ts publishDataset and is off without a dataset repository; dataset tests drive publishDataset directly", |
| 279 | 280 | "drill-watch": "a /proc read of the drill child's memory and a state write every WATCH_MS, only while a restore drill runs in its own process; drill-job tests drive runDrillInChild directly", |
| 280 | 281 | "run-compute": "reads the watch's counters every 5 s and adds them to the few runs in flight (core/run_compute.ts sampleCompute): memory only, no query or write; run-compute.test.ts drives sampleCompute", |
test/lazy-first-use.test.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** | |
| 2 | * Nothing optional loads at boot (#16852): a route family's module and a warm-up start on first | |
| 3 | * use, and the first use still answers. Each lazy family's route list is its module's own; the | |
| 4 | * boot's static imports do not reach the module; the first request to a lazy prefix answers | |
| 5 | * inside the page budget; the search warm waits for the first search. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join, resolve } from "node:path"; | |
| 11 | import { createApp } from "../src/app.ts"; | |
| 12 | import { search } from "../src/actions/search.ts"; | |
| 13 | import type { Handler } from "../src/http/context.ts"; | |
| 14 | import { FORGES_ROUTES, SETTINGS_ROUTES } from "../src/http/lazy_lists.ts"; | |
| 15 | import { Router } from "../src/http/router.ts"; | |
| 16 | import { serve } from "../src/http/server.ts"; | |
| 17 | import { forgesRoutes } from "../src/http/forges_routes.ts"; | |
| 18 | import { settingsRoutes } from "../src/http/settings_routes.ts"; | |
| 19 | import { warmOnFirstSearch } from "../src/search/warm.ts"; | |
| 20 | import { pollUntil } from "./helpers/poll.ts"; | |
| 21 | import { staticClosure } from "./helpers/static-closure.ts"; | |
| 22 | ||
| 23 | /** | |
| 24 | * The first request to a lazily loaded prefix includes its import; this is ten times the warm page | |
| 25 | * budget. | |
| 26 | */ | |
| 27 | const FIRST_REQUEST_MS = 2_000; | |
| 28 | ||
| 29 | const listed = (rows: readonly (readonly [string, string])[]) => rows.map(([method, pattern]) => ({ method, pattern })); | |
| 30 | const key = (rows: readonly { method: string; pattern: string }[]) => rows.map((r) => `${r.method} ${r.pattern}`); | |
| 31 | const added = (register: (r: Router<Handler>) => void): string[] => { | |
| 32 | const table = new Router<Handler>(); | |
| 33 | register(table); | |
| 34 | return key(table.routes()); | |
| 35 | }; | |
| 36 | ||
| 37 | test("each lazy family's route list is the one its module adds", () => { | |
| 38 | expect(key(listed(SETTINGS_ROUTES))).toEqual(added((r) => settingsRoutes(r, () => []))); | |
| 39 | expect(key(listed(FORGES_ROUTES))).toEqual(added(forgesRoutes)); | |
| 40 | }); | |
| 41 | ||
| 42 | test("the boot's static imports do not reach a lazy family's module", () => { | |
| 43 | const closure = staticClosure(resolve("src/main.ts")); | |
| 44 | for (const file of ["src/http/settings_routes.ts", "src/http/forges_routes.ts"]) expect(closure.has(resolve(file))).toBe(false); | |
| 45 | }); | |
| 46 | ||
| 47 | let dir = ""; | |
| 48 | let app: ReturnType<typeof createApp>; | |
| 49 | beforeAll(() => { | |
| 50 | dir = mkdtempSync(join(tmpdir(), "forge-lazy-first-use-")); | |
| 51 | app = createApp({ dataDir: dir, port: 0 }); | |
| 52 | }); | |
| 53 | afterAll(() => { | |
| 54 | app.close(); | |
| 55 | rmSync(dir, { recursive: true, force: true }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("the first request to /settings, /pricing and /templates loads its module and answers in the page budget", async () => { | |
| 59 | const servers = await serve(app); | |
| 60 | try { | |
| 61 | for (const path of ["/settings/tokens", "/pricing", "/templates"]) { | |
| 62 | const started = performance.now(); | |
| 63 | const res = await fetch(`${servers.url}${path}`, { redirect: "manual" }); | |
| 64 | const ms = performance.now() - started; | |
| 65 | expect(res.status, path).not.toBe(404); | |
| 66 | expect(res.status, path).toBeLessThan(500); | |
| 67 | expect(ms, `${path} took ${Math.round(ms)} ms`).toBeLessThan(FIRST_REQUEST_MS); | |
| 68 | await res.arrayBuffer(); | |
| 69 | } | |
| 70 | } finally { | |
| 71 | servers.stop(); | |
| 72 | } | |
| 73 | }); | |
| 74 | ||
| 75 | test("the search warm starts on the first search, not at arming, and a search still answers", async () => { | |
| 76 | const asked: string[] = []; | |
| 77 | warmOnFirstSearch(app, { | |
| 78 | search: async (_app, _actor, input) => void asked.push(input.q), | |
| 79 | timer: () => ({ done: new Promise<void>(() => {}), clear() {} }), | |
| 80 | }); | |
| 81 | expect(asked).toEqual([]); // arming reads nothing | |
| 82 | expect((await search(app, undefined, { q: "" })).repos).toEqual([]); // an empty query is not a use | |
| 83 | expect(asked).toEqual([]); | |
| 84 | const answered = await search(app, undefined, { q: "anything" }); // the first use answers as it always did | |
| 85 | expect(answered.repos).toEqual([]); | |
| 86 | await pollUntil(() => asked.length === 1, "the warm to start"); | |
| 87 | expect(asked).toEqual(["an"]); | |
| 88 | }); |
new above
offline