@ultimat3/cli 22.14.0 → 23.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/CLAUDE.md +25 -8
  2. package/README.md +28 -5
  3. package/package.json +29 -28
  4. package/src/admin-catalog-keys.ts +16 -0
  5. package/src/admin-registration.ts +192 -0
  6. package/src/api-registration.ts +36 -11
  7. package/src/app-boundaries.ts +6 -20
  8. package/src/app-load.ts +55 -11
  9. package/src/app-manifest.ts +14 -0
  10. package/src/app-permissions.ts +0 -0
  11. package/src/app-reload-graph.ts +7 -30
  12. package/src/app-transport.ts +219 -0
  13. package/src/boundary-findings.ts +16 -4
  14. package/src/browser-transport.ts +160 -0
  15. package/src/budgets.ts +35 -3
  16. package/src/cmd-build-spec.ts +10 -3
  17. package/src/cmd-build.ts +56 -9
  18. package/src/cmd-db.ts +60 -11
  19. package/src/cmd-dev.ts +2 -2
  20. package/src/cmd-doctor.ts +35 -11
  21. package/src/cmd-generate.ts +118 -17
  22. package/src/cmd-jobs-spec.ts +19 -3
  23. package/src/cmd-jobs.ts +120 -3
  24. package/src/cmd-new.ts +15 -1
  25. package/src/cmd-routes.ts +15 -1
  26. package/src/cmd-secrets-ring.ts +76 -0
  27. package/src/cmd-secrets-spec.ts +10 -2
  28. package/src/cmd-secrets.ts +48 -12
  29. package/src/cmd-tasks.ts +84 -15
  30. package/src/cmd-verify.ts +10 -2
  31. package/src/comment-safe.ts +16 -0
  32. package/src/coverage-floor.ts +334 -0
  33. package/src/coverage-lcov.ts +193 -0
  34. package/src/coverage-source.ts +136 -0
  35. package/src/db-object-drift.ts +30 -0
  36. package/src/db-replay-engine.ts +115 -0
  37. package/src/db-schema-dump.ts +150 -0
  38. package/src/db-schema-refresh.ts +122 -0
  39. package/src/dev-dashboard.ts +2 -2
  40. package/src/dev-route-table.ts +30 -6
  41. package/src/doctor-coverage.ts +39 -0
  42. package/src/doctor-guards.ts +54 -0
  43. package/src/doctor-sealed.ts +132 -0
  44. package/src/document-graph.ts +82 -0
  45. package/src/document-options.ts +64 -0
  46. package/src/error-codes.ts +32 -0
  47. package/src/framework-schema.ts +15 -0
  48. package/src/generate-files.ts +76 -10
  49. package/src/generate-grant-findings.ts +76 -0
  50. package/src/generate-grants.ts +77 -3
  51. package/src/generate-kinds.ts +19 -0
  52. package/src/generate-write.ts +15 -27
  53. package/src/generated-imports.ts +59 -0
  54. package/src/guard-sources.ts +104 -0
  55. package/src/guards.ts +23 -7
  56. package/src/handle-registration.ts +217 -0
  57. package/src/i18n-registration.ts +36 -0
  58. package/src/image-prepare.ts +51 -8
  59. package/src/import-closure.ts +165 -0
  60. package/src/index.ts +11 -2
  61. package/src/island-store.ts +24 -16
  62. package/src/jobs-drain.ts +14 -7
  63. package/src/jobs-json.ts +50 -0
  64. package/src/jobs-report.ts +3 -0
  65. package/src/json-merge.ts +46 -0
  66. package/src/mcp-errors.ts +29 -1
  67. package/src/messages.ts +33 -0
  68. package/src/metrics-endpoint.ts +71 -24
  69. package/src/migration-extensions.ts +29 -0
  70. package/src/module-imports.ts +47 -0
  71. package/src/output.ts +18 -9
  72. package/src/page-speculation.ts +124 -0
  73. package/src/permission-grants.ts +41 -9
  74. package/src/prerender.ts +9 -5
  75. package/src/publish-closure.ts +101 -0
  76. package/src/role-load.ts +143 -0
  77. package/src/role-start.ts +18 -1
  78. package/src/role-wake.ts +45 -0
  79. package/src/runtime-admin.ts +141 -0
  80. package/src/runtime-hooks.ts +5 -2
  81. package/src/runtime-queue.ts +8 -1
  82. package/src/runtime-render.ts +36 -60
  83. package/src/runtime-services.ts +2 -1
  84. package/src/scaffold-typecheck.ts +1 -0
  85. package/src/schema-dump-drift.ts +64 -0
  86. package/src/serve-boot.ts +85 -147
  87. package/src/serve-prebuilt-paths.ts +19 -0
  88. package/src/serve-prebuilt.ts +84 -0
  89. package/src/serve-web.ts +164 -0
  90. package/src/serve.ts +6 -9
  91. package/src/server-barrels.ts +54 -0
  92. package/src/source-edit.ts +49 -0
  93. package/src/static-document.ts +108 -0
  94. package/src/tasks-facts.ts +4 -1
  95. package/src/templates/action.ts +144 -11
  96. package/src/templates/admin-catalog.ts +58 -0
  97. package/src/templates/admin-page.ts +36 -8
  98. package/src/templates/admin.ts +57 -17
  99. package/src/templates/backfill.ts +121 -87
  100. package/src/templates/entity.ts +140 -44
  101. package/src/templates/guard-animated-layout-property.ts +4 -11
  102. package/src/templates/guard-bare-error.ts +9 -13
  103. package/src/templates/guard-focus-visible.ts +4 -11
  104. package/src/templates/guard-image-dimensions.ts +6 -6
  105. package/src/templates/guard-island-without-states.ts +2 -4
  106. package/src/templates/guard-raw-breakpoint.ts +210 -0
  107. package/src/templates/guard-raw-colour.ts +4 -11
  108. package/src/templates/guard-raw-length.ts +375 -0
  109. package/src/templates/guard-raw-motion.ts +213 -0
  110. package/src/templates/guard-raw-shadow.ts +187 -0
  111. package/src/templates/guard-raw-z-index.ts +166 -0
  112. package/src/templates/guard-repo-raw-sql.ts +345 -0
  113. package/src/templates/guard-semantic-interactive.ts +6 -6
  114. package/src/templates/guard-undeclared-custom-property.ts +217 -0
  115. package/src/templates/guard-undefined-style-class.ts +215 -0
  116. package/src/templates/guard-untranslated-string.ts +6 -6
  117. package/src/templates/guard-unzoned-date.ts +9 -13
  118. package/src/templates/index.ts +3 -2
  119. package/src/templates/island.ts +34 -51
  120. package/src/templates/job-body-test.ts +121 -0
  121. package/src/templates/job.ts +23 -8
  122. package/src/templates/policy.ts +16 -9
  123. package/src/templates/query.ts +242 -26
  124. package/src/templates/resource-create.ts +62 -18
  125. package/src/templates/resource-form-island.ts +58 -80
  126. package/src/templates/resource-page.ts +269 -0
  127. package/src/templates/resource-service.ts +96 -0
  128. package/src/templates/resource.ts +86 -61
  129. package/src/templates/route.ts +38 -29
  130. package/src/templates/scaffold-api.ts +55 -1
  131. package/src/templates/scaffold-app.ts +171 -51
  132. package/src/templates/scaffold-auth.ts +83 -19
  133. package/src/templates/scaffold-browser-client.ts +90 -0
  134. package/src/templates/scaffold-container.ts +16 -0
  135. package/src/templates/scaffold-dashboard-bare.ts +64 -2
  136. package/src/templates/scaffold-dashboard-example.ts +124 -31
  137. package/src/templates/scaffold-dashboard-shared.ts +1 -20
  138. package/src/templates/scaffold-dashboard.ts +1 -2
  139. package/src/templates/scaffold-db-client.ts +142 -0
  140. package/src/templates/scaffold-db-package.ts +90 -8
  141. package/src/templates/scaffold-docs.ts +50 -5
  142. package/src/templates/scaffold-guards.ts +54 -11
  143. package/src/templates/scaffold-http.ts +9 -7
  144. package/src/templates/scaffold-repo.ts +52 -2
  145. package/src/templates/scaffold-roles.ts +56 -9
  146. package/src/templates/scaffold-shell.ts +36 -70
  147. package/src/templates/scaffold-site.ts +45 -19
  148. package/src/templates/scaffold-ui-package.ts +18 -0
  149. package/src/templates/slice-foundation.ts +46 -8
  150. package/src/test-counts.ts +18 -2
  151. package/src/transport-calls.ts +105 -0
  152. package/src/unscanned-admin.ts +54 -0
  153. package/src/verify-checks.ts +26 -7
  154. package/src/verify-coverage-run.ts +190 -0
  155. package/src/verify-deadline.ts +256 -0
  156. package/src/verify-errors.ts +15 -6
  157. package/src/verify-floor.ts +36 -9
  158. package/src/verify-merge.ts +117 -24
  159. package/src/verify-progress.ts +25 -0
  160. package/src/verify-role-load-probe.ts +54 -0
  161. package/src/verify-role-load.ts +148 -0
  162. package/src/verify-run.ts +93 -35
  163. package/src/verify-shard.ts +20 -9
  164. package/src/verify-step.ts +42 -1
  165. package/src/verify-tests.ts +78 -6
  166. package/src/workspace-checks.ts +50 -8
  167. package/src/workspace-dep-edit.ts +127 -0
  168. package/src/cmd-dev-fixture.ts +0 -204
  169. package/src/dev-live-fixture.ts +0 -122
  170. package/src/policy-fixture.ts +0 -78
  171. package/src/realtime-browser-probe-fixture.ts +0 -9
  172. package/src/role-start-fixture.ts +0 -78
  173. package/src/scaffold-fixture.ts +0 -160
  174. package/src/templates/island-fixture.ts +0 -76
package/src/serve-boot.ts CHANGED
@@ -1,45 +1,69 @@
1
- // The web and background roles' boot, after the services are up: the app loaded, its route table
2
- // built, the roles started. Split from `serve.ts` (the entry points) at its line ceiling; the rules
3
- // that file's header states — the SAME boot `x dev` runs, minus the watcher, `/_x` and `dev: true`
4
- // — are this file's.
1
+ // The serving roles' boot: the services started, the app loaded, its route table built, the roles
2
+ // started. Behind ONE `await import()` in `serve.ts` (the entry points), so `ROLE=migrate` loads
3
+ // none of it; the rules that file's header states — the SAME boot `x dev` runs, minus the watcher,
4
+ // `/_x` and `dev: true` — are this file's. A role imports what it runs: the web surface
5
+ // (`serve-web.ts`) and the manifest projection are behind `await import()` in turn, so a worker's
6
+ // module graph holds neither.
5
7
 
6
8
  import type { Role } from '@ultimat3/core';
7
- import { configureLifecycle } from '@ultimat3/core';
8
- import type { Route } from '@ultimat3/http';
9
- import { describeRoutes } from '@ultimat3/render';
10
- import { createIsrController } from '@ultimat3/render/server';
11
- import { apiMountRoutes, apiRoutes, pagePostRoutes } from './api-routes';
12
- import { loadSignInPath } from './app-auth';
13
- import { loadApp } from './app-load';
14
- import { appManifest } from './app-manifest';
15
- import { mountAppMcp } from './app-mcp';
16
- import { errorPageStyleSources } from './error-page-csp';
17
- import { islandRoutes } from './island-routes';
18
- import { loadOrBuildIslands } from './island-store';
9
+ import { configureLifecycle, logger } from '@ultimat3/core';
19
10
  import type { MetricsEndpoint } from './metrics-endpoint';
20
11
  import { startOtlpExport } from './otlp-export';
21
- import { loadNavigation, pageNavigation } from './page-navigation';
22
- import { pageSync } from './page-sync';
23
- import { loadPwaArtifacts } from './pwa-artifacts';
12
+ import { loadAppForRole, roleLoadFor } from './role-load';
24
13
  import { startRoles } from './role-start';
25
- import { assetRoutes } from './runtime-assets';
26
- import { appRoutes } from './runtime-render';
14
+ import { resolveServices } from './runtime-bindings';
27
15
  import { replicaOverrides } from './runtime-replica';
28
16
  import type { RunningServices } from './runtime-services';
29
- import { servedStorage, storageRoutes } from './runtime-storage';
30
- import { seoRoutes } from './seo-routes';
17
+ import { startServices } from './runtime-services';
31
18
  import { loadDrainConfig, loadHealthConfig } from './serve-drain';
32
19
  import { configureReporting, containerBinding, metricsPortFor, portFromEnv } from './serve-env';
20
+ import { adoptPrebuiltStyles, reportBootBuilds, watchBootBuilds } from './serve-prebuilt';
33
21
  import type { ServedApp, ServeOptions } from './serve-types';
34
- import { loadSiteSettings, publicOrigin } from './site-config';
35
- import { styleBundle } from './style-bundle';
36
- import { styleRoutes } from './style-routes';
37
- import { serviceWorkerArtifacts } from './sw-artifacts';
38
- import { serviceWorkerRoutes } from './sw-routes';
39
- import { loadThemeMode, themeBoot } from './theme-boot';
22
+
23
+ /**
24
+ * The build stamps `BUILD_ID` into the image; unstamped, the manifest's content hash is the same
25
+ * answer computed here, so `x-ultimate-build` is never absent and never a lie. Projected only when
26
+ * unstamped, because a stamped image already paid for it at build time and a replica's boot should
27
+ * not repeat it. The projection describes the WHOLE app — routes included — so an unstamped worker
28
+ * imports everything its own load left out, and says so.
29
+ */
30
+ async function buildIdFor(options: ServeOptions, role: Role): Promise<string> {
31
+ const stamped = options.env['BUILD_ID'];
32
+ if (stamped !== undefined && stamped.length > 0) return stamped;
33
+ if (roleLoadFor(role) === 'background') {
34
+ logger.warn('ultimate build id unstamped', {
35
+ role,
36
+ cause:
37
+ 'BUILD_ID is unset, so this role imported every page of the app to compute the build id the web role serves',
38
+ fix: 'x build --target docker',
39
+ });
40
+ }
41
+ const { appManifest } = await import('./app-manifest');
42
+ return (await appManifest(options.root)).manifest.buildId;
43
+ }
44
+
45
+ /**
46
+ * Everything `serveApp` does after the scrape listener is open: the services, then the roles over
47
+ * them. One export, so `serve.ts` reaches the whole serving graph through ONE `await import()`.
48
+ */
49
+ export async function bootServing(boot: {
50
+ readonly options: ServeOptions;
51
+ readonly role: Role;
52
+ readonly acquired: (() => void | Promise<void>)[];
53
+ readonly metrics: MetricsEndpoint;
54
+ }): Promise<ServedApp> {
55
+ const { options } = boot;
56
+ const runtime = await startServices(
57
+ resolveServices(options.root, options.env),
58
+ options.env,
59
+ options.runtime,
60
+ );
61
+ boot.acquired.push(() => runtime.stop());
62
+ return bootRoles({ ...boot, runtime });
63
+ }
40
64
 
41
65
  /** The half of `serveApp` whose every acquisition is registered for rollback. */
42
- export async function bootRoles(boot: {
66
+ async function bootRoles(boot: {
43
67
  readonly options: ServeOptions;
44
68
  readonly role: Role;
45
69
  readonly runtime: RunningServices;
@@ -48,18 +72,19 @@ export async function bootRoles(boot: {
48
72
  readonly metrics: MetricsEndpoint;
49
73
  }): Promise<ServedApp> {
50
74
  const { options, role, runtime, acquired, metrics } = boot;
75
+ // The image's compiled stylesheets, before the first page is imported — importing one IS
76
+ // compiling its sheet — and the count of what this boot compiles anyway.
77
+ adoptPrebuiltStyles(options.root);
78
+ const bootBuilds = watchBootBuilds();
51
79
  // Importing the app's modules IS the registration: every route, action and job below is
52
- // whatever this call put in the registries.
53
- await loadApp(options.root);
54
- // The build stamps `BUILD_ID` into the image; unstamped, the manifest's content hash is the same
55
- // answer computed here, so `x-ultimate-build` is never absent and never a lie. Projected only
56
- // when unstamped, because a stamped image already paid for it at build time and a replica's boot
57
- // should not repeat it — the load above is the part every boot needs either way.
58
- const stamped = options.env['BUILD_ID'];
59
- const buildId =
60
- stamped !== undefined && stamped.length > 0
61
- ? stamped
62
- : (await appManifest(options.root)).manifest.buildId;
80
+ // whatever this call put in the registries — for THIS role (`role-load.ts`).
81
+ const loaded = await loadAppForRole(options.root, role);
82
+ // A module that would not import registered nothing, and a process that only logs the roles it
83
+ // started gives no sign of it: each one is a line an operator can act on.
84
+ for (const finding of loaded.findings) {
85
+ logger.error('ultimate app module failed to load', { role, ...finding });
86
+ }
87
+ const buildId = await buildIdFor(options, role);
63
88
  // Before the first socket opens: everything above this line fails loudly into the container's
64
89
  // own logs, everything below it is a served request, a claimed job or a routed frame.
65
90
  configureReporting(options.env, buildId);
@@ -71,8 +96,16 @@ export async function bootRoles(boot: {
71
96
  acquired.push(stopOtlp);
72
97
  // Only the web role serves pages, so only it builds islands and assembles a route table: a
73
98
  // worker, scheduler, sync or replicator pod compiled every island of the app on every boot for
74
- // nothing it would ever serve (plan 101, slice 12 e).
75
- const web = role === 'web' ? await webSurface(options, runtime, buildId) : undefined;
99
+ // nothing it would ever serve (plan 101, slice 12 e). The MODULE is the web role's as well —
100
+ // imported here, it is render, PWA, SEO and MCP code no other role evaluates.
101
+ const web =
102
+ role === 'web'
103
+ ? await (await import('./serve-web')).webSurface(options, runtime, buildId)
104
+ : undefined;
105
+ // Islands and stylesheets are the image build's to make (`x build --target prebuilt`). A role
106
+ // that made either here served correctly and paid for it — seconds of CPU and a compiler's
107
+ // memory, on every start of every replica — so it says so once, with the Dockerfile line.
108
+ reportBootBuilds(role, bootBuilds(web?.islandsBuilt));
76
109
  const port = options.port ?? portFromEnv(options.env);
77
110
  // An in-process caller asking for an ephemeral app port is a test, and a test that grabbed the
78
111
  // fixed 9090 would fail the next suite to boot beside it. An environment that names the port
@@ -92,13 +125,15 @@ export async function bootRoles(boot: {
92
125
  runtime,
93
126
  routes: web?.routes ?? [],
94
127
  env: options.env,
95
- // Same declaration `x dev` reads. Without it a container answers a browser that opened a
96
- // guarded page with the problem document, rendered as raw JSON in the viewport.
97
- signInPath: await loadSignInPath(options.root),
98
- // The enforced policy this process sends must admit the app's own error pages' `<style>` and
99
- // the theme boot the documents carry; `x dev` is report-only, so only here was it a blank page.
100
- inlineStyles: web === undefined ? [] : await errorPageStyleSources(options.root),
101
- inlineScripts: web === undefined ? [] : [web.themeCsp],
128
+ // The three facts only a process serving documents has (`WebSurface`); `startWeb` is their
129
+ // one reader, so every other role hands none.
130
+ ...(web === undefined
131
+ ? {}
132
+ : {
133
+ signInPath: web.signInPath,
134
+ inlineStyles: web.inlineStyles,
135
+ inlineScripts: web.inlineScripts,
136
+ }),
102
137
  // The app's own `apps/web/site/errors/<status>.html`, resolved inside `startWeb` so this
103
138
  // process and `x dev` cannot answer a browser differently.
104
139
  root: options.root,
@@ -128,100 +163,3 @@ export async function bootRoles(boot: {
128
163
  },
129
164
  };
130
165
  }
131
-
132
- /** What only the web role serves: its islands, its documents and every route beside them. */
133
- async function webSurface(
134
- options: ServeOptions,
135
- runtime: RunningServices,
136
- buildId: string,
137
- ): Promise<{ readonly routes: readonly Route[]; readonly themeCsp: string }> {
138
- // Read from the store `x build --target docker` wrote and VERIFIED against this app and this
139
- // runtime (`island-store.ts`); built here only when there is no store to trust, which is correct
140
- // and slower, and logged with its reason.
141
- const islands = await loadOrBuildIslands(options.root);
142
- // The same two strings `x dev` resolves, from the same reader: a `<link rel="manifest">` served
143
- // on a laptop and absent in the image is exactly the dev/prod difference this file exists to
144
- // prevent, and it is the one an operator cannot see without installing the app.
145
- const pwa = await loadPwaArtifacts(options.root);
146
- const theme = themeBoot(await loadThemeMode(options.root));
147
- // `site.origin` and `seo.robots.disallow`: the absolute URLs every document and the sitemap carry.
148
- const site = await loadSiteSettings(options.root);
149
- const origin = publicOrigin(options.env, site);
150
- // The page's sync target and its scripts — the same call `x dev` makes, so the two cannot differ.
151
- // Before the service worker, which precaches those scripts.
152
- const sync = await pageSync(options.root, options.env, buildId, runtime.realtime);
153
- // The client router, when a surface opted in (`navigation.client`) — the same call in both boots.
154
- const navigation = await pageNavigation(
155
- options.root,
156
- await loadNavigation(options.root),
157
- buildId,
158
- );
159
- // The worker, from the SAME route table this process is about to serve — `describeRoutes()` is
160
- // the one projection `x.manifest.json`, `/_x`, the sitemap and `sw.js` are all built from, so a
161
- // route added here cannot be missing from the precache manifest.
162
- const serviceWorker =
163
- pwa === undefined
164
- ? undefined
165
- : serviceWorkerArtifacts({
166
- pwa,
167
- buildId,
168
- routes: describeRoutes(),
169
- islands,
170
- styles: styleBundle(),
171
- scripts: [
172
- ...sync.scripts,
173
- ...(navigation.script === undefined ? [] : [navigation.script]),
174
- ],
175
- });
176
- // The app's own MCP endpoint, through the same call `x dev` makes — see `app-mcp.ts`.
177
- const mcpMount = await mountAppMcp(options.root);
178
- // The app's own disks when it declared any (`defineStorage` in an app module), else this boot's.
179
- const served = servedStorage(runtime.storage);
180
- const routes: readonly Route[] = [
181
- ...apiRoutes(),
182
- // The bearer doors onto a cut of the same routes (`defineApi({ http: { mounts } })`), counted
183
- // on the store this boot's limiter uses, so one token's allowance is one number fleet-wide.
184
- ...apiMountRoutes(options.runtime?.rateLimitStore ?? runtime.rateLimitStore),
185
- ...mcpMount.routes,
186
- // The app's own plain routes (`apps/<app>/runtime.ts` `routes`), before any page can shadow one.
187
- ...(options.runtime?.routes ?? []),
188
- ...(serviceWorker === undefined ? [] : serviceWorkerRoutes(serviceWorker)),
189
- ...assetRoutes({
190
- root: options.root,
191
- storage: served,
192
- ...(options.runtime?.images === undefined ? {} : { images: options.runtime.images }),
193
- ...(pwa === undefined ? {} : { pwa }),
194
- }),
195
- ...storageRoutes({ storage: served }),
196
- ...islandRoutes(() => islands),
197
- // The surface stylesheets the documents link. Built from the registry the `loadApp` above
198
- // filled, so this process serves exactly the CSS it renders against.
199
- ...styleRoutes(() => styleBundle()),
200
- // `robots.txt` and `sitemap.xml`, the same two files the static export writes (`site-seo.ts`).
201
- ...seoRoutes({ env: options.env, site, root: options.root }),
202
- // The page's one socket: its worker script, served beside the islands for their reason.
203
- ...sync.routes,
204
- ...navigation.routes,
205
- // A page's `POST`, bound to an action (`defineRoute({ post })`) — beside the page's `GET`.
206
- ...pagePostRoutes(),
207
- ...appRoutes({
208
- buildId,
209
- resolveIsland: (file) => islands.resolverFor(file),
210
- ...(sync.head === undefined ? {} : { sync: sync.head }),
211
- persisted: sync.persisted,
212
- ...(navigation.head === undefined ? {} : { navigation: navigation.head }),
213
- themeHead: theme.head,
214
- ...(origin === undefined ? {} : { origin }),
215
- ...(pwa === undefined
216
- ? {}
217
- : { pwaHead: (locale: string) => pwa.headFor(locale) + (serviceWorker?.head ?? '') }),
218
- // Only when a store was supplied. `createIsrController` defaults to a per-process memory
219
- // store, so twelve replicas hold twelve of them and a purge tag regenerates one twelfth of
220
- // the fleet while the other eleven keep serving the page it just invalidated.
221
- ...(options.runtime?.isrStore === undefined
222
- ? {}
223
- : { isr: createIsrController({ buildId, store: options.runtime.isrStore }) }),
224
- }),
225
- ];
226
- return { routes, themeCsp: theme.cspSource };
227
- }
@@ -0,0 +1,19 @@
1
+ // Where the image's prebuilt store lives and the one command that writes it. A leaf with no
2
+ // import, so the Dockerfile template, the build command and the container boot all spell them
3
+ // from here without loading each other.
4
+
5
+ /**
6
+ * App-root-relative. NOT under `.x/`: that is the state directory, which the scaffolded compose
7
+ * topology mounts a tmpfs over and every ignore file drops, so a store written there never reached
8
+ * a running container. Under `node_modules` because that is the one directory every tool already
9
+ * leaves alone — git, the linter, every source walker and the image context — and the one the
10
+ * image build has just written, so it is in a layer and readable under a read-only root.
11
+ */
12
+ export const PREBUILT_DIR = 'node_modules/.cache/ultimate';
13
+
14
+ export const PREBUILT_ISLANDS_DIR = `${PREBUILT_DIR}/islands`;
15
+
16
+ export const PREBUILT_SASS_DIR = `${PREBUILT_DIR}/sass`;
17
+
18
+ /** The Dockerfile line that writes the store — one spelling, for the template and every `fix:`. */
19
+ export const PREBUILT_COMMAND = 'bun node_modules/@ultimat3/cli/src/bin.ts build --target prebuilt';
@@ -0,0 +1,84 @@
1
+ // The image's prebuilt store, as a container sees it: where `x build --target prebuilt` wrote the
2
+ // island chunks and the compiled stylesheets during `docker build`, and the one log line a role
3
+ // owes when it had to build either at boot instead. A leaf of the boot graph — the half that
4
+ // WRITES the store (`image-prepare.ts`) carries the island builder and is never loaded here.
5
+
6
+ // why: the store is adopted before the first app module is imported, and that import path is
7
+ // synchronous end to end (`compileStylesheet` runs inside Bun's `onLoad`); Bun has no sync exists.
8
+ import { existsSync } from 'node:fs';
9
+ // why: Bun ships no path-join primitive.
10
+ import { join } from 'node:path';
11
+ import type { Role } from '@ultimat3/core';
12
+ import { logger, UltimateError } from '@ultimat3/core';
13
+ import { sassCompilations, setSassCacheDir } from '@ultimat3/render/server';
14
+ import { PREBUILT_COMMAND, PREBUILT_SASS_DIR } from './serve-prebuilt-paths';
15
+
16
+ /**
17
+ * Points the Sass cache at the image's own entries, when the image has any. Before the app is
18
+ * imported, because importing a page IS compiling its stylesheet. An entry is keyed by the
19
+ * sheet's absolute path, which is why the store is written inside the image build: same `WORKDIR`,
20
+ * same paths. Answers whether there was a store to adopt.
21
+ */
22
+ export function adoptPrebuiltStyles(root: string): boolean {
23
+ const dir = join(root, PREBUILT_SASS_DIR);
24
+ if (!existsSync(dir)) return false;
25
+ setSassCacheDir(dir);
26
+ return true;
27
+ }
28
+
29
+ /** What a role built at boot that the image build exists to have built once. */
30
+ export interface BootBuilds {
31
+ /** Island chunks built by this boot, and the one sentence saying why the store was not served. */
32
+ readonly islands?: { readonly chunks: number; readonly reason: string };
33
+ /** Stylesheets this boot ran Sass for. */
34
+ readonly stylesheets: number;
35
+ readonly elapsedMs: number;
36
+ }
37
+
38
+ /**
39
+ * Logged, never thrown: a build at boot is correct and only slow, so the role serves what it
40
+ * would have served — it pays seconds of CPU and a compiler's worth of memory on every start.
41
+ */
42
+ export class ImageNotPrebuiltError extends UltimateError {
43
+ constructor(input: { readonly role: Role } & BootBuilds) {
44
+ const islands = input.islands;
45
+ const built = [
46
+ ...(islands === undefined
47
+ ? []
48
+ : [`built ${String(islands.chunks)} island chunk(s) because ${islands.reason}`]),
49
+ ...(input.stylesheets === 0
50
+ ? []
51
+ : [`compiled ${String(input.stylesheets)} stylesheet(s) the image holds no result for`]),
52
+ ];
53
+ super({
54
+ code: 'X_IMAGE_NOT_PREBUILT',
55
+ cause: `the ${input.role} role ${built.join(' and ')}, ${String(input.elapsedMs)} ms into its boot — work this image repeats on every start of every replica`,
56
+ fix: `edit docker/Dockerfile: add RUN ${PREBUILT_COMMAND} after COPY . . in the runtime stage, then rebuild the image`,
57
+ meta: {
58
+ role: input.role,
59
+ islands: islands?.chunks ?? 0,
60
+ stylesheets: input.stylesheets,
61
+ elapsedMs: input.elapsedMs,
62
+ },
63
+ });
64
+ }
65
+ }
66
+
67
+ /** Starts counting what a boot compiles; the answer is what `reportBootBuilds` is handed. */
68
+ export function watchBootBuilds(): (islands?: BootBuilds['islands']) => BootBuilds {
69
+ const started = Bun.nanoseconds();
70
+ const compiled = sassCompilations();
71
+ return (islands) => ({
72
+ ...(islands === undefined || islands.chunks === 0 ? {} : { islands }),
73
+ stylesheets: sassCompilations() - compiled,
74
+ elapsedMs: Math.round((Bun.nanoseconds() - started) / 1e6),
75
+ });
76
+ }
77
+
78
+ /** One `error` line when the boot built anything, and nothing when it built nothing. */
79
+ export function reportBootBuilds(role: Role, builds: BootBuilds): boolean {
80
+ if (builds.islands === undefined && builds.stylesheets === 0) return false;
81
+ const error = new ImageNotPrebuiltError({ role, ...builds });
82
+ logger.error(error.format(), { code: error.code, ...error.meta });
83
+ return true;
84
+ }
@@ -0,0 +1,164 @@
1
+ // What only the web role serves: its islands, its documents and every route beside them. A file
2
+ // of its own so the roles that render nothing never import it — `serve-boot.ts` loads it with
3
+ // `await import()` for `ROLE=web` alone, and `serve-graph.test.ts` pins what every other role is
4
+ // spared: render's server half, the PWA, SEO, MCP and island subsystems.
5
+
6
+ import type { Route } from '@ultimat3/http';
7
+ import { describeRoutes } from '@ultimat3/render';
8
+ import { createIsrController } from '@ultimat3/render/server';
9
+ import { apiMountRoutes, apiRoutes, pagePostRoutes } from './api-routes';
10
+ import { loadSignInPath } from './app-auth';
11
+ import { mountAppMcp } from './app-mcp';
12
+ import { errorPageStyleSources } from './error-page-csp';
13
+ import { islandRoutes } from './island-routes';
14
+ import type { LoadedIslands } from './island-store';
15
+ import { loadOrBuildIslands } from './island-store';
16
+ import { loadNavigation, pageNavigation } from './page-navigation';
17
+ import { loadSpeculation, pageSpeculation } from './page-speculation';
18
+ import { pageSync } from './page-sync';
19
+ import { loadPwaArtifacts } from './pwa-artifacts';
20
+ import { adminMountRoutes } from './runtime-admin';
21
+ import { assetRoutes } from './runtime-assets';
22
+ import { appRoutes } from './runtime-render';
23
+ import type { RunningServices } from './runtime-services';
24
+ import { servedStorage, storageRoutes } from './runtime-storage';
25
+ import { seoRoutes } from './seo-routes';
26
+ import type { ServeOptions } from './serve-types';
27
+ import { loadSiteSettings, publicOrigin } from './site-config';
28
+ import { styleBundle } from './style-bundle';
29
+ import { styleRoutes } from './style-routes';
30
+ import { serviceWorkerArtifacts } from './sw-artifacts';
31
+ import { serviceWorkerRoutes } from './sw-routes';
32
+ import { loadThemeMode, themeBoot } from './theme-boot';
33
+
34
+ /** Everything `startRoles` is handed that only a process serving documents has. */
35
+ export interface WebSurface {
36
+ readonly routes: readonly Route[];
37
+ /** The theme boot and the speculation rules: the two inline bodies every document may carry. */
38
+ readonly inlineScripts: readonly string[];
39
+ /** The app's own error pages' `<style>` bodies, which the enforced policy must admit. */
40
+ readonly inlineStyles: readonly string[];
41
+ /** Where a browser that opened a guarded page is sent — the declaration `x dev` reads. */
42
+ readonly signInPath: string | null;
43
+ /** Present when this boot built the islands itself: how many chunks, and why not the store. */
44
+ readonly islandsBuilt?: LoadedIslands['built'];
45
+ }
46
+
47
+ export async function webSurface(
48
+ options: ServeOptions,
49
+ runtime: RunningServices,
50
+ buildId: string,
51
+ ): Promise<WebSurface> {
52
+ // Read from the store the image build wrote and VERIFIED against this app and this runtime
53
+ // (`island-store.ts`); built here only when there is no store to trust, which is correct and
54
+ // slower — the reason travels back to the boot, which reports it.
55
+ const { bundle: islands, built: islandsBuilt } = await loadOrBuildIslands(options.root);
56
+ // The same two strings `x dev` resolves, from the same reader: a `<link rel="manifest">` served
57
+ // on a laptop and absent in the image is exactly the dev/prod difference this file exists to
58
+ // prevent, and it is the one an operator cannot see without installing the app.
59
+ const pwa = await loadPwaArtifacts(options.root);
60
+ const theme = themeBoot(await loadThemeMode(options.root));
61
+ // `site.origin` and `seo.robots.disallow`: the absolute URLs every document and the sitemap carry.
62
+ const site = await loadSiteSettings(options.root);
63
+ const origin = publicOrigin(options.env, site);
64
+ // The page's sync target and its scripts — the same call `x dev` makes, so the two cannot differ.
65
+ // Before the service worker, which precaches those scripts.
66
+ const sync = await pageSync(options.root, options.env, buildId, runtime.realtime);
67
+ // The client router, when a surface opted in (`navigation.client`) — the same call in both boots.
68
+ const declared = await loadNavigation(options.root);
69
+ const navigation = await pageNavigation(options.root, declared, buildId);
70
+ // The browser's own prefetch, for the documents that carry no router (`navigation.speculation`).
71
+ const speculation = pageSpeculation({
72
+ config: await loadSpeculation(options.root),
73
+ client: declared.surfaces,
74
+ });
75
+ // The worker, from the SAME route table this process is about to serve — `describeRoutes()` is
76
+ // the one projection `x.manifest.json`, `/_x`, the sitemap and `sw.js` are all built from, so a
77
+ // route added here cannot be missing from the precache manifest.
78
+ const serviceWorker =
79
+ pwa === undefined
80
+ ? undefined
81
+ : serviceWorkerArtifacts({
82
+ pwa,
83
+ buildId,
84
+ routes: describeRoutes(),
85
+ islands,
86
+ styles: styleBundle(),
87
+ scripts: [
88
+ ...sync.scripts,
89
+ ...(navigation.script === undefined ? [] : [navigation.script]),
90
+ ],
91
+ });
92
+ // The app's own MCP endpoint, through the same call `x dev` makes — see `app-mcp.ts`.
93
+ const mcpMount = await mountAppMcp(options.root);
94
+ // The app's own disks when it declared any (`defineStorage` in an app module), else this boot's.
95
+ const served = servedStorage(runtime.storage);
96
+ const routes: readonly Route[] = [
97
+ ...apiRoutes(),
98
+ // The bearer doors onto a cut of the same routes (`defineApi({ http: { mounts } })`), counted
99
+ // on the store this boot's limiter uses, so one token's allowance is one number fleet-wide.
100
+ ...apiMountRoutes(options.runtime?.rateLimitStore ?? runtime.rateLimitStore),
101
+ ...mcpMount.routes,
102
+ // The app's own plain routes (`apps/<app>/runtime.ts` `routes`), before any page can shadow one.
103
+ ...(options.runtime?.routes ?? []),
104
+ ...(serviceWorker === undefined ? [] : serviceWorkerRoutes(serviceWorker)),
105
+ ...assetRoutes({
106
+ root: options.root,
107
+ storage: served,
108
+ ...(options.runtime?.images === undefined ? {} : { images: options.runtime.images }),
109
+ ...(pwa === undefined ? {} : { pwa }),
110
+ }),
111
+ ...storageRoutes({ storage: served }),
112
+ ...islandRoutes(() => islands),
113
+ // The surface stylesheets the documents link. Built from the registry the `loadApp` above
114
+ // filled, so this process serves exactly the CSS it renders against.
115
+ ...styleRoutes(() => styleBundle()),
116
+ // `robots.txt` and `sitemap.xml`, the same two files the static export writes (`site-seo.ts`).
117
+ ...seoRoutes({ env: options.env, site, root: options.root }),
118
+ // The page's one socket: its worker script, served beside the islands for their reason.
119
+ ...sync.routes,
120
+ ...navigation.routes,
121
+ // A page's `POST`, bound to an action (`defineRoute({ post })`) — beside the page's `GET`.
122
+ ...pagePostRoutes(),
123
+ // The app's generated admin, when it declared one (`defineAdmin`): every screen under its base
124
+ // path, served through the same document builder as the pages below. Nothing for an app with
125
+ // no admin — the registry is asked, the package is not imported.
126
+ ...adminMountRoutes({
127
+ buildId: buildId,
128
+ themeHead: theme.head,
129
+ ...(origin === undefined ? {} : { origin }),
130
+ }),
131
+ ...appRoutes({
132
+ buildId,
133
+ resolveIsland: (file) => islands.resolverFor(file),
134
+ ...(sync.head === undefined ? {} : { sync: sync.head }),
135
+ persisted: sync.persisted,
136
+ ...(navigation.head === undefined ? {} : { navigation: navigation.head }),
137
+ themeHead: theme.head,
138
+ ...(speculation === undefined ? {} : { speculationHead: speculation.head }),
139
+ // A `static` page is the same bytes for every request of this process: rendered once.
140
+ memoStatic: true,
141
+ ...(origin === undefined ? {} : { origin }),
142
+ ...(pwa === undefined
143
+ ? {}
144
+ : { pwaHead: (locale: string) => pwa.headFor(locale) + (serviceWorker?.head ?? '') }),
145
+ // Only when a store was supplied. `createIsrController` defaults to a per-process memory
146
+ // store, so twelve replicas hold twelve of them and a purge tag regenerates one twelfth of
147
+ // the fleet while the other eleven keep serving the page it just invalidated.
148
+ ...(options.runtime?.isrStore === undefined
149
+ ? {}
150
+ : { isr: createIsrController({ buildId, store: options.runtime.isrStore }) }),
151
+ }),
152
+ ];
153
+ return {
154
+ routes,
155
+ inlineScripts: [theme.cspSource, ...(speculation === undefined ? [] : [speculation.cspSource])],
156
+ // The enforced policy this process sends must admit the app's own error pages' `<style>` and
157
+ // the theme boot the documents carry; `x dev` is report-only, so only here was it a blank page.
158
+ inlineStyles: await errorPageStyleSources(options.root),
159
+ // Same declaration `x dev` reads. Without it a container answers a browser that opened a
160
+ // guarded page with the problem document, rendered as raw JSON in the viewport.
161
+ signInPath: await loadSignInPath(options.root),
162
+ ...(islandsBuilt === undefined ? {} : { islandsBuilt }),
163
+ };
164
+ }
package/src/serve.ts CHANGED
@@ -13,8 +13,6 @@ import { startMetricsEndpoint } from './metrics-endpoint';
13
13
  import { readMigrations } from './migrations';
14
14
  import { resolveServices } from './runtime-bindings';
15
15
  import { startQueue } from './runtime-queue';
16
- import { startServices } from './runtime-services';
17
- import { bootRoles } from './serve-boot';
18
16
  import { containerBinding, metricsPortFor, portFromEnv, roleFromEnv } from './serve-env';
19
17
  import type { MigratedApp, ServedApp, ServeOptions, StartedApp } from './serve-types';
20
18
 
@@ -144,17 +142,16 @@ export async function serveApp(input: ServeOptions): Promise<ServedApp> {
144
142
  const metrics = startMetricsEndpoint({
145
143
  port: metricsPortFor(options.env, port, options.metricsPort),
146
144
  hostname: containerBinding(options.env, options.hostname).hostname,
145
+ role,
147
146
  });
148
147
  // Everything acquired from here down, in order, so a throw anywhere below gives it all back.
149
148
  const acquired: (() => void | Promise<void>)[] = [() => metrics.stop()];
150
149
  try {
151
- const runtime = await startServices(
152
- resolveServices(options.root, options.env),
153
- options.env,
154
- options.runtime,
155
- );
156
- acquired.push(() => runtime.stop());
157
- return await bootRoles({ options, role, runtime, acquired, metrics });
150
+ // Behind `await import()`: `ROLE=migrate` runs in this module too, applies SQL files and
151
+ // exits, and the services and the role boot are ~240 modules it never calls
152
+ // (`serve-graph.test.ts` pins the count each role is left with).
153
+ const { bootServing } = await import('./serve-boot');
154
+ return await bootServing({ options, role, acquired, metrics });
158
155
  } catch (error) {
159
156
  await releaseBoot(acquired);
160
157
  throw error;
@@ -0,0 +1,54 @@
1
+ // A package barrel a browser must not import, and the entry it imports instead. `@ultimat3/entity`'s
2
+ // barrel reaches the SQL renderer and `@ultimat3/db` — about a megabyte in an island — while
3
+ // `@ultimat3/entity/record` is the browser half. DERIVED from each manifest's `exports`: a package
4
+ // that publishes a `./record`, `./browser` or `./client` subpath has said its barrel is the server
5
+ // half, so a new browser entry enters this rule by being published, never by being listed here.
6
+
7
+ import { stripComments } from '@ultimat3/core';
8
+ import { lineIndex } from './ts-scan';
9
+
10
+ /** The subpath names that declare "this is the browser entry of the package". */
11
+ export const BROWSER_ENTRY_SUBPATHS: ReadonlySet<string> = new Set(['record', 'browser', 'client']);
12
+
13
+ /** Barrel specifier → the browser entry that replaces it, read off the published specifiers. */
14
+ export function serverBarrels(specifiers: Iterable<string>): ReadonlyMap<string, string> {
15
+ const barrels = new Map<string, string>();
16
+ for (const spec of specifiers) {
17
+ const match = /^(@[\w-]+\/[\w-]+)\/([\w-]+)$/.exec(spec);
18
+ if (match?.[1] === undefined || !BROWSER_ENTRY_SUBPATHS.has(match[2] ?? '')) continue;
19
+ barrels.set(match[1], spec);
20
+ }
21
+ return barrels;
22
+ }
23
+
24
+ export interface BarrelImport {
25
+ readonly line: number;
26
+ readonly barrel: string;
27
+ readonly entry: string;
28
+ }
29
+
30
+ /** `import` / `export … from` / `import()` of a VALUE from the barrel. `import type` runs nothing. */
31
+ const IMPORTS =
32
+ /\b(?:import|export)\s+(?!type\s)[^;'"]*?\bfrom\s*(['"])([^'"]+)\1|\bimport\s*\(\s*(['"])([^'"]+)\3\s*\)/g;
33
+
34
+ export function barrelImports(
35
+ source: string,
36
+ barrels: ReadonlyMap<string, string>,
37
+ ): readonly BarrelImport[] {
38
+ // Most files import no barrel at all; say so before paying for a comment-stripped copy.
39
+ if (![...barrels.keys()].some((barrel) => source.includes(barrel))) return [];
40
+ const code = stripComments(source);
41
+ const lineAt = lineIndex(code);
42
+ const found: BarrelImport[] = [];
43
+ for (const match of code.matchAll(IMPORTS)) {
44
+ const spec = match[2] ?? match[4] ?? '';
45
+ const entry = barrels.get(spec);
46
+ if (entry === undefined) continue;
47
+ // `import { type A, type B } from …` binds nothing at runtime either.
48
+ const clause = /\{([^}]*)\}/.exec(match[0])?.[1];
49
+ const typesOnly = clause?.split(',').every((part) => /^\s*(?:type\s|$)/.test(part));
50
+ if (typesOnly === true) continue;
51
+ found.push({ line: lineAt(match.index), barrel: spec, entry });
52
+ }
53
+ return found;
54
+ }