@nifrajs/cli 5.1.0 → 5.2.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 (201) hide show
  1. package/CHANGELOG.md +3211 -0
  2. package/README.md +3 -0
  3. package/dist/agent-files.d.ts +19 -11
  4. package/dist/agent-files.d.ts.map +1 -1
  5. package/dist/agent-files.js +26 -18
  6. package/dist/agent-files.js.map +1 -1
  7. package/dist/assure-hydration.d.ts.map +1 -1
  8. package/dist/assure-hydration.js +1 -0
  9. package/dist/assure-hydration.js.map +1 -1
  10. package/dist/assure.d.ts.map +1 -1
  11. package/dist/assure.js +14 -1
  12. package/dist/assure.js.map +1 -1
  13. package/dist/capabilities-tool.d.ts.map +1 -1
  14. package/dist/capabilities-tool.js +131 -45
  15. package/dist/capabilities-tool.js.map +1 -1
  16. package/dist/check-diagnostics.d.ts +5 -0
  17. package/dist/check-diagnostics.d.ts.map +1 -1
  18. package/dist/check-diagnostics.js +3 -0
  19. package/dist/check-diagnostics.js.map +1 -1
  20. package/dist/check-scan.d.ts.map +1 -1
  21. package/dist/check-scan.js +6 -3
  22. package/dist/check-scan.js.map +1 -1
  23. package/dist/check.d.ts.map +1 -1
  24. package/dist/check.js +52 -12
  25. package/dist/check.js.map +1 -1
  26. package/dist/cli.d.ts.map +1 -1
  27. package/dist/cli.js +23 -12
  28. package/dist/cli.js.map +1 -1
  29. package/dist/command-catalog.d.ts.map +1 -1
  30. package/dist/command-catalog.js +9 -5
  31. package/dist/command-catalog.js.map +1 -1
  32. package/dist/dev-bun-config.d.ts +6 -3
  33. package/dist/dev-bun-config.d.ts.map +1 -1
  34. package/dist/dev-bun-config.js +13 -10
  35. package/dist/dev-bun-config.js.map +1 -1
  36. package/dist/dev-feed-tool.d.ts +3 -2
  37. package/dist/dev-feed-tool.d.ts.map +1 -1
  38. package/dist/dev-feed-tool.js +26 -5
  39. package/dist/dev-feed-tool.js.map +1 -1
  40. package/dist/doctor.d.ts +15 -1
  41. package/dist/doctor.d.ts.map +1 -1
  42. package/dist/doctor.js +79 -61
  43. package/dist/doctor.js.map +1 -1
  44. package/dist/i18n-check.d.ts +11 -1
  45. package/dist/i18n-check.d.ts.map +1 -1
  46. package/dist/i18n-check.js +170 -17
  47. package/dist/i18n-check.js.map +1 -1
  48. package/dist/init-agents.d.ts +46 -6
  49. package/dist/init-agents.d.ts.map +1 -1
  50. package/dist/init-agents.js +193 -31
  51. package/dist/init-agents.js.map +1 -1
  52. package/dist/internal/project-specifiers.d.ts.map +1 -1
  53. package/dist/internal/project-specifiers.js +9 -1
  54. package/dist/internal/project-specifiers.js.map +1 -1
  55. package/dist/internal/source-facts.d.ts +6 -0
  56. package/dist/internal/source-facts.d.ts.map +1 -1
  57. package/dist/internal/source-facts.js +106 -0
  58. package/dist/internal/source-facts.js.map +1 -1
  59. package/dist/internal/workspace-packages.d.ts +22 -0
  60. package/dist/internal/workspace-packages.d.ts.map +1 -0
  61. package/dist/internal/workspace-packages.js +215 -0
  62. package/dist/internal/workspace-packages.js.map +1 -0
  63. package/dist/load.d.ts +7 -2
  64. package/dist/load.d.ts.map +1 -1
  65. package/dist/load.js +11 -2
  66. package/dist/load.js.map +1 -1
  67. package/dist/lock-links.d.ts +42 -0
  68. package/dist/lock-links.d.ts.map +1 -0
  69. package/dist/lock-links.js +144 -0
  70. package/dist/lock-links.js.map +1 -0
  71. package/dist/mcp-exec.d.ts.map +1 -1
  72. package/dist/mcp-exec.js +5 -2
  73. package/dist/mcp-exec.js.map +1 -1
  74. package/dist/mcp-http.js +1 -1
  75. package/dist/mcp-root.d.ts +6 -4
  76. package/dist/mcp-root.d.ts.map +1 -1
  77. package/dist/mcp-root.js +12 -8
  78. package/dist/mcp-root.js.map +1 -1
  79. package/dist/migrate-layout.d.ts +2 -1
  80. package/dist/migrate-layout.d.ts.map +1 -1
  81. package/dist/migrate-layout.js +434 -29
  82. package/dist/migrate-layout.js.map +1 -1
  83. package/dist/project-site.d.ts +7 -0
  84. package/dist/project-site.d.ts.map +1 -0
  85. package/dist/project-site.js +19 -0
  86. package/dist/project-site.js.map +1 -0
  87. package/dist/recipes/4.0.0.d.ts.map +1 -1
  88. package/dist/recipes/4.0.0.js +2 -0
  89. package/dist/recipes/4.0.0.js.map +1 -1
  90. package/dist/recipes/5.1.0.d.ts +9 -0
  91. package/dist/recipes/5.1.0.d.ts.map +1 -0
  92. package/dist/recipes/5.1.0.js +22 -0
  93. package/dist/recipes/5.1.0.js.map +1 -0
  94. package/dist/recipes/5.2.0.d.ts +10 -0
  95. package/dist/recipes/5.2.0.d.ts.map +1 -0
  96. package/dist/recipes/5.2.0.js +32 -0
  97. package/dist/recipes/5.2.0.js.map +1 -0
  98. package/dist/recipes/index.d.ts +3 -0
  99. package/dist/recipes/index.d.ts.map +1 -1
  100. package/dist/recipes/index.js +7 -0
  101. package/dist/recipes/index.js.map +1 -1
  102. package/dist/review.d.ts.map +1 -1
  103. package/dist/review.js +2 -0
  104. package/dist/review.js.map +1 -1
  105. package/dist/rule-explain.d.ts +26 -0
  106. package/dist/rule-explain.d.ts.map +1 -0
  107. package/dist/rule-explain.js +49 -0
  108. package/dist/rule-explain.js.map +1 -0
  109. package/dist/rules/codes.d.ts +2 -0
  110. package/dist/rules/codes.d.ts.map +1 -1
  111. package/dist/rules/codes.js +2 -0
  112. package/dist/rules/codes.js.map +1 -1
  113. package/dist/rules/contract-visibility.d.ts.map +1 -1
  114. package/dist/rules/contract-visibility.js +93 -21
  115. package/dist/rules/contract-visibility.js.map +1 -1
  116. package/dist/rules/explanations.d.ts +14 -0
  117. package/dist/rules/explanations.d.ts.map +1 -0
  118. package/dist/rules/explanations.js +316 -0
  119. package/dist/rules/explanations.js.map +1 -0
  120. package/dist/rules/legacy.d.ts.map +1 -1
  121. package/dist/rules/legacy.js +24 -1
  122. package/dist/rules/legacy.js.map +1 -1
  123. package/dist/rules/merge-auth.d.ts +12 -0
  124. package/dist/rules/merge-auth.d.ts.map +1 -0
  125. package/dist/rules/merge-auth.js +52 -0
  126. package/dist/rules/merge-auth.js.map +1 -0
  127. package/dist/rules/pages.d.ts +27 -0
  128. package/dist/rules/pages.d.ts.map +1 -1
  129. package/dist/rules/pages.js +177 -2
  130. package/dist/rules/pages.js.map +1 -1
  131. package/dist/rules/routes.d.ts +3 -2
  132. package/dist/rules/routes.d.ts.map +1 -1
  133. package/dist/rules/routes.js +4 -3
  134. package/dist/rules/routes.js.map +1 -1
  135. package/dist/rules/zones.d.ts.map +1 -1
  136. package/dist/rules/zones.js +3 -2
  137. package/dist/rules/zones.js.map +1 -1
  138. package/dist/typecheck-coverage.d.ts +11 -0
  139. package/dist/typecheck-coverage.d.ts.map +1 -1
  140. package/dist/typecheck-coverage.js +50 -7
  141. package/dist/typecheck-coverage.js.map +1 -1
  142. package/dist/upgrade.d.ts +37 -1
  143. package/dist/upgrade.d.ts.map +1 -1
  144. package/dist/upgrade.js +171 -20
  145. package/dist/upgrade.js.map +1 -1
  146. package/dist/verification-plan.d.ts.map +1 -1
  147. package/dist/verification-plan.js +2 -0
  148. package/dist/verification-plan.js.map +1 -1
  149. package/dist/version-drift.d.ts.map +1 -1
  150. package/dist/version-drift.js +12 -1
  151. package/dist/version-drift.js.map +1 -1
  152. package/dist/web-app-options.d.ts +1 -1
  153. package/dist/web-app-options.d.ts.map +1 -1
  154. package/dist/web-app-options.js.map +1 -1
  155. package/docs/examples.json +19 -1
  156. package/docs/llms-full.txt +625 -130
  157. package/docs/types.json +396 -50
  158. package/package.json +13 -12
  159. package/src/agent-files.ts +27 -17
  160. package/src/assure-hydration.ts +1 -0
  161. package/src/assure.ts +11 -1
  162. package/src/capabilities-tool.ts +159 -50
  163. package/src/check-diagnostics.ts +8 -0
  164. package/src/check-scan.ts +9 -3
  165. package/src/check.ts +67 -12
  166. package/src/cli.ts +24 -11
  167. package/src/command-catalog.ts +16 -8
  168. package/src/dev-bun-config.ts +13 -9
  169. package/src/dev-feed-tool.ts +40 -6
  170. package/src/doctor.ts +102 -66
  171. package/src/i18n-check.ts +254 -18
  172. package/src/init-agents.ts +221 -41
  173. package/src/internal/project-specifiers.ts +9 -1
  174. package/src/internal/source-facts.ts +124 -0
  175. package/src/internal/workspace-packages.ts +218 -0
  176. package/src/load.ts +19 -3
  177. package/src/lock-links.ts +173 -0
  178. package/src/mcp-exec.ts +5 -2
  179. package/src/mcp-http.ts +1 -1
  180. package/src/mcp-root.ts +14 -8
  181. package/src/migrate-layout.ts +495 -31
  182. package/src/project-site.ts +18 -0
  183. package/src/recipes/4.0.0.ts +2 -0
  184. package/src/recipes/5.1.0.ts +23 -0
  185. package/src/recipes/5.2.0.ts +33 -0
  186. package/src/recipes/index.ts +7 -0
  187. package/src/review.ts +2 -0
  188. package/src/rule-explain.ts +75 -0
  189. package/src/rules/codes.ts +2 -0
  190. package/src/rules/contract-visibility.ts +118 -20
  191. package/src/rules/explanations.ts +329 -0
  192. package/src/rules/legacy.ts +24 -1
  193. package/src/rules/merge-auth.ts +55 -0
  194. package/src/rules/pages.ts +221 -2
  195. package/src/rules/routes.ts +4 -3
  196. package/src/rules/zones.ts +3 -2
  197. package/src/typecheck-coverage.ts +51 -6
  198. package/src/upgrade.ts +227 -22
  199. package/src/verification-plan.ts +6 -0
  200. package/src/version-drift.ts +15 -0
  201. package/src/web-app-options.ts +9 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nifrajs/cli",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "The nifra CLI - zero-config dev/build/start for a nifra app (`nifra dev`, `nifra build`, `nifra start`).",
5
5
  "keywords": [
6
6
  "bun",
@@ -58,7 +58,8 @@
58
58
  "files": [
59
59
  "dist",
60
60
  "src",
61
- "docs"
61
+ "docs",
62
+ "CHANGELOG.md"
62
63
  ],
63
64
  "sideEffects": false,
64
65
  "scripts": {
@@ -69,16 +70,16 @@
69
70
  "access": "public"
70
71
  },
71
72
  "dependencies": {
72
- "@nifrajs/client": "5.1.0",
73
- "@nifrajs/core": "5.1.0",
74
- "@nifrajs/agent-review": "5.1.0",
75
- "@nifrajs/i18n": "5.1.0",
76
- "@nifrajs/mcp": "5.1.0",
77
- "@nifrajs/mcp-db": "5.1.0",
78
- "@nifrajs/runner": "5.1.0",
79
- "@nifrajs/schema": "5.1.0",
80
- "@nifrajs/testing": "5.1.0",
81
- "@nifrajs/web": "5.1.0"
73
+ "@nifrajs/client": "5.2.0",
74
+ "@nifrajs/core": "5.2.0",
75
+ "@nifrajs/agent-review": "5.2.0",
76
+ "@nifrajs/i18n": "5.2.0",
77
+ "@nifrajs/mcp": "5.2.0",
78
+ "@nifrajs/mcp-db": "5.2.0",
79
+ "@nifrajs/runner": "5.2.0",
80
+ "@nifrajs/schema": "5.2.0",
81
+ "@nifrajs/testing": "5.2.0",
82
+ "@nifrajs/web": "5.2.0"
82
83
  },
83
84
  "repository": {
84
85
  "type": "git",
@@ -40,14 +40,17 @@ export const MCP_SERVER_COMMAND = "bunx" as const
40
40
  * (`../package.json` → the same root, since `dist/` sits beside the manifest). npm always ships a
41
41
  * package's own `package.json`, so the read can't miss at install time.
42
42
  *
43
- * Why pin at all instead of `bunx @nifrajs/cli mcp`: `bunx` keys its cache on the exact version spec.
44
- * An UNPINNED spec resolves to the `latest` tag once, then `bunx` reuses that cached copy on every
45
- * later spawn WITHOUT re-checking the registry - so an editor that once launched an older `@nifrajs/cli`
46
- * keeps respawning the stale binary even after a newer one is published (the MCP server silently runs
47
- * old code). Pinning the exact version makes the version part of the cache key, so each release fetches
48
- * fresh and a stale cache can never shadow it. The cost: an already-scaffolded app's `.mcp.json` freezes
49
- * at its scaffold-time version until `nifra init-agents --sync-mcp` re-pins it to the nifra the project
50
- * installs - an acceptable, deterministic trade.
43
+ * Two launch forms. A project that declares `@nifrajs/cli` gets the unpinned
44
+ * `bunx @nifrajs/cli mcp` ({@link MCP_SERVER_ARGS_INSTALLED}): `bunx` runs the copy the project installs
45
+ * (its `node_modules`, or an ancestor's) whenever there is one, so an upgrade moves the server with it
46
+ * and nothing needs re-pinning. A pinned spec never does that, not even at the installed version: it
47
+ * always runs a registry copy. Every scaffold declares the CLI, so every scaffold is unpinned.
48
+ *
49
+ * A project that does not declare it keeps the pin ({@link MCP_SERVER_ARGS}): with no local install an
50
+ * unpinned spec resolves `latest` once and `bunx` reuses that cached copy on every later spawn without
51
+ * re-checking the registry, so the server would silently run old code. The exact version is part of the
52
+ * cache key, so each release fetches fresh; `nifra init-agents --sync-mcp` moves it to the installed
53
+ * nifra, or unpins it once the project declares the CLI.
51
54
  */
52
55
  export const MCP_CLI_VERSION: string = ownVersion()
53
56
 
@@ -66,6 +69,9 @@ function ownVersion(): string {
66
69
  }
67
70
  export const MCP_SERVER_ARGS = [`@nifrajs/cli@${MCP_CLI_VERSION}`, "mcp"] as const
68
71
 
72
+ /** The launch args for a project that declares `@nifrajs/cli`: unpinned, so `bunx` runs its install. */
73
+ export const MCP_SERVER_ARGS_INSTALLED = ["@nifrajs/cli", "mcp"] as const
74
+
69
75
  /** The server entry registered under the `nifra` key in both Claude Code's and Cursor's MCP config. */
70
76
  export interface McpServerConfig {
71
77
  readonly command: string
@@ -77,17 +83,20 @@ export interface McpConfig {
77
83
  readonly mcpServers: Readonly<Record<string, McpServerConfig>>
78
84
  }
79
85
 
80
- /** The one canonical MCP config object both registries serialize - the anti-drift seam. */
81
- export const MCP_CONFIG: McpConfig = {
82
- mcpServers: {
83
- nifra: { command: MCP_SERVER_COMMAND, args: [...MCP_SERVER_ARGS] },
84
- },
86
+ /** The one canonical MCP config both registries serialize - the anti-drift seam. `pin` is the
87
+ * `@nifrajs/cli` version to pin; without one the launch follows the project's install. */
88
+ export function mcpConfig(pin?: string): McpConfig {
89
+ const args = pin === undefined ? MCP_SERVER_ARGS_INSTALLED : [`@nifrajs/cli@${pin}`, "mcp"]
90
+ return { mcpServers: { nifra: { command: MCP_SERVER_COMMAND, args: [...args] } } }
85
91
  }
86
92
 
93
+ /** The config a scaffold writes: every scaffold declares the CLI, so its launch is unpinned. */
94
+ export const MCP_CONFIG: McpConfig = mcpConfig()
95
+
87
96
  /** Serialize the canonical MCP config as the JSON written to `.mcp.json` and `.cursor/mcp.json`.
88
97
  * Trailing newline so the file is POSIX-clean and diffs don't flag a missing EOL. */
89
- export function mcpJson(): string {
90
- return `${JSON.stringify(MCP_CONFIG, null, 2)}\n`
98
+ export function mcpJson(pin?: string): string {
99
+ return `${JSON.stringify(mcpConfig(pin), null, 2)}\n`
91
100
  }
92
101
 
93
102
  const POINTER_INTRO =
@@ -118,10 +127,11 @@ export function copilotInstructions(): string {
118
127
  * agents (Cursor, and anything that reads `AGENTS.md`) also learn the MCP exists and what to prefer.
119
128
  * Mirrors the CLAUDE.md preamble's guidance without the Claude-specific `@import`.
120
129
  */
121
- export function agentsMcpSection(): string {
130
+ export function agentsMcpSection(pin?: string): string {
131
+ const args = pin === undefined ? MCP_SERVER_ARGS_INSTALLED : [`@nifrajs/cli@${pin}`, "mcp"]
122
132
  return `## MCP server
123
133
 
124
- This project ships a **nifra MCP server** - launch it with \`${MCP_SERVER_COMMAND} ${MCP_SERVER_ARGS.join(" ")}\`. It's registered for
134
+ This project ships a **nifra MCP server** - launch it with \`${MCP_SERVER_COMMAND} ${args.join(" ")}\`. It's registered for
125
135
  Claude Code in \`.mcp.json\` and for Cursor in \`.cursor/mcp.json\`; other agents can point their MCP client
126
136
  at that command. **Prefer its tools** over writing nifra from memory - they're typechecked against this
127
137
  project + the installed version, so they beat training-data recall:
@@ -216,6 +216,7 @@ async function buildHydrationApp(cwd: string): Promise<BuiltHydrationApp | { ski
216
216
  manifest: discoverRoutes(loaded.routesDir),
217
217
  clientEntry: client.entry,
218
218
  ...frameworkWebAppOptions(loaded.framework, loaded.backend),
219
+ ...(client.copyChunks === undefined ? {} : { copyChunks: client.copyChunks }),
219
220
  }) as unknown as AppLike
220
221
  return {
221
222
  app: webApp,
package/src/assure.ts CHANGED
@@ -84,7 +84,17 @@ export async function loadAssuranceConfig(
84
84
  }
85
85
  const specifier = pathToFileURL(path)
86
86
  specifier.searchParams.set("nifra-assurance", String(Date.now()))
87
- const loaded = (await import(specifier.href)) as { default?: unknown }
87
+ // Core records each route's registering module while this is set, for the capability walk. Scoped to
88
+ // the load: a module already imported in this process keeps what it recorded then.
89
+ const priorSites = process.env.NIFRA_ROUTE_SITES
90
+ process.env.NIFRA_ROUTE_SITES = "1"
91
+ let loaded: { default?: unknown }
92
+ try {
93
+ loaded = (await import(specifier.href)) as { default?: unknown }
94
+ } finally {
95
+ if (priorSites === undefined) delete process.env.NIFRA_ROUTE_SITES
96
+ else process.env.NIFRA_ROUTE_SITES = priorSites
97
+ }
88
98
  if (!isConfig(loaded.default)) {
89
99
  throw new Error(
90
100
  `[nifra] ${displayPath(path)} must default-export defineAssuranceConfig({ source, policy }).`,
@@ -17,7 +17,12 @@ import {
17
17
  validCapabilityId,
18
18
  } from "@nifrajs/core/capabilities"
19
19
  import { expandOptionalParams } from "@nifrajs/core/pattern"
20
- import { type ReflectedMount, type ReflectedRoute, reflectRoutes } from "@nifrajs/core/reflection"
20
+ import {
21
+ type ReflectedMount,
22
+ type ReflectedRoute,
23
+ reflectRouteSites,
24
+ reflectRoutes,
25
+ } from "@nifrajs/core/reflection"
21
26
  import {
22
27
  scanSkippedRouteText,
23
28
  scanStaticRouteText,
@@ -25,6 +30,7 @@ import {
25
30
  stripComments,
26
31
  walkSource,
27
32
  } from "./check.ts"
33
+ import { projectSite } from "./project-site.ts"
28
34
 
29
35
  // Import and re-export clauses are spelled out (a default binding, then `* as ns` or a `{ … }` list)
30
36
  // rather than scanned lazily, which is quadratic on a long semicolon-free module.
@@ -238,14 +244,113 @@ interface WalkResult {
238
244
 
239
245
  const MAX_PROVENANCE_MODULES = 500
240
246
  const MAX_PROVENANCE_CHAIN = 16
247
+ /** The budget for one installed package's closure, walked once per run whatever reaches it. */
248
+ const MAX_INSTALLED_MODULES = 5000
249
+
250
+ /** An effect import one module makes, and the policy rules it matches. */
251
+ interface EffectHit {
252
+ readonly specifier: string
253
+ readonly module: string
254
+ /** Specifiers from the walk's entry to this import, the import last. */
255
+ readonly chain: readonly string[]
256
+ readonly approved: readonly CapabilityPolicy["provenance"]["imports"][number][]
257
+ readonly forbidden: readonly CapabilityPolicy["provenance"]["forbiddenImports"][number][]
258
+ }
259
+
260
+ /** What a walk reads, shared by every route of one run: sources, and installed closures by entry. */
261
+ interface WalkContext {
262
+ readonly cwd: string
263
+ readonly policy: CapabilityPolicy
264
+ readonly sources: Map<string, string>
265
+ readonly installed: Map<string, Promise<InstalledWalk>>
266
+ }
267
+
268
+ interface InstalledWalk {
269
+ readonly hits: readonly EffectHit[]
270
+ readonly truncated: boolean
271
+ }
272
+
273
+ /** A module under a `node_modules` folder: installed code, the same for every route that reaches it. */
274
+ const isInstalled = (module: string): boolean => module.split("/").includes("node_modules")
275
+
276
+ async function moduleSource(context: WalkContext, module: string): Promise<string | undefined> {
277
+ const cached = context.sources.get(module)
278
+ if (cached !== undefined) return cached
279
+ try {
280
+ const content = await Bun.file(join(context.cwd, module)).text()
281
+ context.sources.set(module, content)
282
+ return content
283
+ } catch {
284
+ return undefined
285
+ }
286
+ }
287
+
288
+ /**
289
+ * The effect imports one module makes, each with the rules it matches, and the modules its other
290
+ * imports load. An approved import is the owned effect seam: its implementation may legitimately
291
+ * import the raw provider, so the walk does not follow it.
292
+ */
293
+ function scanModule(
294
+ context: WalkContext,
295
+ module: string,
296
+ content: string,
297
+ chain: readonly string[],
298
+ ): { readonly hits: EffectHit[]; readonly next: Array<{ module: string; specifier: string }> } {
299
+ const hits: EffectHit[] = []
300
+ const next: Array<{ module: string; specifier: string }> = []
301
+ for (const specifier of scanEffectImports(content)) {
302
+ const approved = context.policy.provenance.imports.filter((rule) =>
303
+ specifierMatches(rule.specifier, specifier),
304
+ )
305
+ const forbidden = context.policy.provenance.forbiddenImports.filter((rule) =>
306
+ specifierMatches(rule.specifier, specifier),
307
+ )
308
+ if (approved.length > 0 || forbidden.length > 0)
309
+ hits.push({ specifier, module, chain: [...chain, specifier], approved, forbidden })
310
+ if (approved.length > 0) continue
311
+ const local = resolveLocalModule(context.cwd, module, specifier, context.sources)
312
+ if (local !== undefined) next.push({ module: local, specifier })
313
+ }
314
+ return { hits, next }
315
+ }
316
+
317
+ /**
318
+ * Every effect import in the closure of the installed module `entry`, walked once per run however many
319
+ * routes reach it. Its chains start after `entry`. A package's own module count stays out of each
320
+ * route's budget: one locale-heavy dependency would otherwise truncate every route that imports it.
321
+ */
322
+ function walkInstalled(context: WalkContext, entry: string): Promise<InstalledWalk> {
323
+ const cached = context.installed.get(entry)
324
+ if (cached !== undefined) return cached
325
+ const walk = (async (): Promise<InstalledWalk> => {
326
+ const hits: EffectHit[] = []
327
+ const visited = new Set<string>()
328
+ const queue: Array<{ module: string; chain: readonly string[] }> = [
329
+ { module: entry, chain: [] },
330
+ ]
331
+ while (queue.length > 0) {
332
+ if (visited.size >= MAX_INSTALLED_MODULES) return { hits, truncated: true }
333
+ const current = queue.shift()
334
+ if (current === undefined || visited.has(current.module)) continue
335
+ visited.add(current.module)
336
+ const content = await moduleSource(context, current.module)
337
+ if (content === undefined) continue
338
+ const scanned = scanModule(context, current.module, content, current.chain)
339
+ hits.push(...scanned.hits)
340
+ for (const { module, specifier } of scanned.next)
341
+ if (!visited.has(module)) queue.push({ module, chain: [...current.chain, specifier] })
342
+ }
343
+ return { hits, truncated: false }
344
+ })()
345
+ context.installed.set(entry, walk)
346
+ return walk
347
+ }
241
348
 
242
349
  async function walkRouteModules(
243
- cwd: string,
350
+ context: WalkContext,
244
351
  modules: readonly string[],
245
352
  method: string,
246
353
  path: string,
247
- policy: CapabilityPolicy,
248
- sources: Map<string, string>,
249
354
  ): Promise<WalkResult> {
250
355
  const queue = modules.map((module) => ({ module, chain: [module] as string[] }))
251
356
  const visited = new Set<string>()
@@ -253,6 +358,25 @@ async function walkRouteModules(
253
358
  const violations: CapabilityImportViolation[] = []
254
359
  const truncations: CapabilityProvenanceTruncation[] = []
255
360
  let covered = false
361
+ const record = (hit: EffectHit, chain: readonly string[]): void => {
362
+ for (const rule of hit.approved) {
363
+ for (const id of rule.capabilities) {
364
+ if (!evidence.some((item) => item.id === id && item.source === hit.specifier)) {
365
+ evidence.push({ id, kind: "static", source: hit.specifier })
366
+ }
367
+ }
368
+ }
369
+ for (const rule of hit.forbidden) {
370
+ violations.push({
371
+ method,
372
+ path,
373
+ module: hit.module,
374
+ specifier: hit.specifier,
375
+ reason: rule.reason,
376
+ chain: Object.freeze([...chain, ...hit.chain]),
377
+ })
378
+ }
379
+ }
256
380
 
257
381
  while (queue.length > 0) {
258
382
  if (visited.size >= MAX_PROVENANCE_MODULES) {
@@ -280,45 +404,30 @@ async function walkRouteModules(
280
404
  continue
281
405
  }
282
406
  visited.add(current.module)
283
- let content = sources.get(current.module)
284
- if (content === undefined) {
285
- try {
286
- content = await Bun.file(join(cwd, current.module)).text()
287
- sources.set(current.module, content)
288
- } catch {
289
- continue
290
- }
291
- }
292
- covered = true
293
- for (const specifier of scanEffectImports(content)) {
294
- let approvedEffectBoundary = false
295
- for (const rule of policy.provenance.imports) {
296
- if (!specifierMatches(rule.specifier, specifier)) continue
297
- approvedEffectBoundary = true
298
- for (const id of rule.capabilities) {
299
- if (!evidence.some((item) => item.id === id && item.source === specifier)) {
300
- evidence.push({ id, kind: "static", source: specifier })
301
- }
302
- }
303
- }
304
- for (const rule of policy.provenance.forbiddenImports) {
305
- if (!specifierMatches(rule.specifier, specifier)) continue
306
- violations.push({
407
+ if (isInstalled(current.module)) {
408
+ const installed = await walkInstalled(context, current.module)
409
+ covered = true
410
+ for (const hit of installed.hits) record(hit, current.chain)
411
+ if (installed.truncated) {
412
+ truncations.push({
307
413
  method,
308
414
  path,
309
- module: current.module,
310
- specifier,
311
- reason: rule.reason,
312
- chain: Object.freeze([...current.chain, specifier]),
415
+ reason: "module-limit",
416
+ chain: Object.freeze([
417
+ ...current.chain,
418
+ `<${MAX_INSTALLED_MODULES}-module limit in ${current.module}>`,
419
+ ]),
313
420
  })
314
421
  }
315
- // An explicitly mapped import is the owned effect seam. Its implementation may legitimately
316
- // import the raw provider; assurance governs whether routes can reach the seam, not its internals.
317
- if (approvedEffectBoundary) continue
318
- const local = resolveLocalModule(cwd, current.module, specifier, sources)
319
- if (local !== undefined && !visited.has(local)) {
320
- queue.push({ module: local, chain: [...current.chain, specifier] })
321
- }
422
+ continue
423
+ }
424
+ const content = await moduleSource(context, current.module)
425
+ if (content === undefined) continue
426
+ covered = true
427
+ const scanned = scanModule(context, current.module, content, [])
428
+ for (const hit of scanned.hits) record(hit, current.chain)
429
+ for (const { module, specifier } of scanned.next) {
430
+ if (!visited.has(module)) queue.push({ module, chain: [...current.chain, specifier] })
322
431
  }
323
432
  }
324
433
  return { covered, evidence, violations, truncations }
@@ -345,6 +454,7 @@ export async function collectCapabilityProjectReport(
345
454
  ): Promise<CapabilityProjectReport> {
346
455
  const policy = defineCapabilityPolicy(policyInput)
347
456
  const sources = await readSources(cwd)
457
+ const context: WalkContext = { cwd, policy, sources, installed: new Map() }
348
458
  const automatic = new Map<string, Set<string>>()
349
459
  const skipped = new Map<string, string>()
350
460
  for (const [file, content] of sources) {
@@ -371,8 +481,13 @@ export async function collectCapabilityProjectReport(
371
481
  const violations: CapabilityImportViolation[] = []
372
482
  const truncations: CapabilityProvenanceTruncation[] = []
373
483
  const reflectedRoutes = options.routes ?? reflectRoutes(source)
484
+ // The module that registered each route, recorded while the app loaded: it covers the paths the
485
+ // text scan cannot read (a template literal, a server a helper builds).
486
+ const registered = reflectRouteSites(source)
374
487
  for (const route of reflectedRoutes) {
375
488
  const modules = new Set(automatic.get(routeKey(route.method, route.path)) ?? [])
489
+ const site = projectSite(cwd, registered.get(`${route.method} ${route.path}`))?.file
490
+ if (site !== undefined && sources.has(site)) modules.add(site)
376
491
  for (const association of policy.provenance.routeModules ?? []) {
377
492
  if (
378
493
  matchesAssuranceSelector(route, {
@@ -385,21 +500,15 @@ export async function collectCapabilityProjectReport(
385
500
  for (const module of association.modules) modules.add(module)
386
501
  }
387
502
  }
388
- const walked = await walkRouteModules(
389
- cwd,
390
- [...modules],
391
- route.method,
392
- route.path,
393
- policy,
394
- sources,
395
- )
396
- const site = modules.size === 0 ? skipped.get(routeKey(route.method, route.path)) : undefined
503
+ const walked = await walkRouteModules(context, [...modules], route.method, route.path)
504
+ const skippedAt =
505
+ modules.size === 0 ? skipped.get(routeKey(route.method, route.path)) : undefined
397
506
  const uncoveredReason =
398
507
  modules.size > 0
399
508
  ? undefined
400
- : site === undefined
509
+ : skippedAt === undefined
401
510
  ? "no source file registers it with a path the static scan can read; map it with capabilities.provenance.routeModules"
402
- : `${site} appears to register it on a server the static scan cannot identify (built by a helper that is neither \`server()\` nor imported from a module that calls it); call server() there, or map the route with capabilities.provenance.routeModules`
511
+ : `${skippedAt} appears to register it on a server the static scan cannot identify (built by a helper that is neither \`server()\` nor imported from a module that calls it); call server() there, or map the route with capabilities.provenance.routeModules`
403
512
  const covered = modules.size > 0 && walked.covered
404
513
  evidenceRoutes.push(
405
514
  uncoveredReason === undefined
@@ -28,6 +28,7 @@ import { parseRulePacks, runRuleRegistry } from "./rules/index.ts"
28
28
  import { islandRules } from "./rules/islands.ts"
29
29
  import { retiredLayoutRule } from "./rules/layout.ts"
30
30
  import { LEGACY_RULE_CODES, LEGACY_RULE_ORDER, legacyRules } from "./rules/legacy.ts"
31
+ import { mergeAuthGapRule } from "./rules/merge-auth.ts"
31
32
  import { nanoRules } from "./rules/nano.ts"
32
33
  import { pageRules } from "./rules/pages.ts"
33
34
  import { routeRules } from "./rules/routes.ts"
@@ -94,6 +95,8 @@ export interface CheckResult {
94
95
  * better-auth). Echoed here so `--json` / the MCP tool / the report can show what the typed-client scan
95
96
  * deliberately skipped - a suppressed prefix stays auditable instead of silently hiding real drift. */
96
97
  readonly externalMounts?: readonly string[]
98
+ /** The tsconfigs `nifra.check.json` names for the typecheck, echoed so a narrowed program is visible. */
99
+ readonly tsconfigs?: readonly string[]
97
100
  /** Mounts declared `opaque` - outside capability assurance on purpose, each with its stated reason.
98
101
  * Listed on every run so the report states its own boundary; never a failure. */
99
102
  readonly knownGaps?: readonly CapabilityGap[]
@@ -160,6 +163,9 @@ export interface CheckAssuranceContext {
160
163
  export interface CheckConfig {
161
164
  readonly externalMounts: readonly string[]
162
165
  readonly rules: Readonly<Record<string, RuleOverride>>
166
+ /** The tsconfigs the typecheck runs, each its own program, project-relative. Unset: `tsconfig.json`
167
+ * and every project it references. */
168
+ readonly tsconfigs?: readonly string[]
163
169
  }
164
170
 
165
171
  /**
@@ -492,6 +498,7 @@ export async function collectCheckDiagnostics(
492
498
  ...routeRules,
493
499
  contractVisibilityRule,
494
500
  ...pageRules,
501
+ mergeAuthGapRule,
495
502
  ...zoneRules,
496
503
  ...dataGuardRules,
497
504
  ...secretRules,
@@ -674,6 +681,7 @@ export async function collectCheckDiagnostics(
674
681
  ...(checkConfig.externalMounts.length === 0
675
682
  ? {}
676
683
  : { externalMounts: checkConfig.externalMounts }),
684
+ ...(checkConfig.tsconfigs === undefined ? {} : { tsconfigs: checkConfig.tsconfigs }),
677
685
  ...(knownGaps.length === 0 ? {} : { knownGaps }),
678
686
  ...(hooks === undefined ? {} : { hooks }),
679
687
  ...(Object.keys(checkConfig.rules).length === 0 ? {} : { ruleOverrides: checkConfig.rules }),
package/src/check-scan.ts CHANGED
@@ -3381,9 +3381,15 @@ export function scanResponseRoutes(
3381
3381
  for (let m = RESPONSE_RETURN.exec(code); m !== null; m = RESPONSE_RETURN.exec(code)) {
3382
3382
  if (facts !== undefined) {
3383
3383
  const source = facts.parse(file, content)
3384
- // A parseable AST rejects `return new Response(` text that only lives in a normal string. On
3385
- // malformed source we keep the lexical candidate; the typecheck gate will report the syntax error.
3386
- if (source !== undefined && facts.isResponseSyntaxAt(source, m.index) !== true) continue
3384
+ // A parseable AST rejects `return new Response(` text that only lives in a normal string, and
3385
+ // one outside a route handler (an auth stage's `rejected` helper, say). On malformed source we
3386
+ // keep the lexical candidate; the typecheck gate will report the syntax error.
3387
+ if (
3388
+ source !== undefined &&
3389
+ (facts.isResponseSyntaxAt(source, m.index) !== true ||
3390
+ !facts.isRouteHandlerAt(source, m.index))
3391
+ )
3392
+ continue
3387
3393
  }
3388
3394
  const line = lineAt(content, m.index)
3389
3395
  // A `// nifra-expect NF-C007` comment (or the `raw-response` slug) marks an intentional raw Response
package/src/check.ts CHANGED
@@ -44,7 +44,7 @@ import { forwardTerminalSignals, OWN_PROCESS_GROUP, signalProcessGroup } from ".
44
44
  import type { ContractCheckFacts, ProjectFactsSeed } from "./project-facts.ts"
45
45
  import { RULE_CODES } from "./rules/codes.ts"
46
46
  import { sourceIndex } from "./rules/index.ts"
47
- import { filesOutsideProgram, splitListedFiles } from "./typecheck-coverage.ts"
47
+ import { filesOutsideProgram, splitListedFiles, typecheckProjects } from "./typecheck-coverage.ts"
48
48
 
49
49
  export type {
50
50
  CheckAnalysisInput,
@@ -124,10 +124,24 @@ function resolveTscBin(cwd: string): string | undefined {
124
124
  }
125
125
  }
126
126
 
127
- /** Run the project's own `tsc --noEmit`, if TypeScript + a tsconfig are present. Never auto-installs. */
128
- async function typecheck(cwd: string, signal?: AbortSignal): Promise<TypecheckResult> {
129
- const tsconfig = join(cwd, "tsconfig.json")
130
- if (!(await Bun.file(tsconfig).exists()))
127
+ /**
128
+ * Run the project's own `tsc --noEmit` over each tsconfig `typecheckProjects` names, one program at a
129
+ * time, if TypeScript and a tsconfig are present. Never auto-installs.
130
+ */
131
+ async function typecheck(
132
+ cwd: string,
133
+ signal?: AbortSignal,
134
+ configured?: readonly string[],
135
+ ): Promise<TypecheckResult> {
136
+ const projects = typecheckProjects(cwd, configured)
137
+ if (projects.missing.length > 0) {
138
+ return {
139
+ ran: true,
140
+ ok: false,
141
+ output: `nifra.check.json "tsconfigs" names ${projects.missing.map((path) => `"${path}"`).join(", ")}, which does not exist`,
142
+ }
143
+ }
144
+ if (projects.paths.length === 0)
131
145
  return { ran: false, ok: true, missingTsconfig: true, note: "no tsconfig.json" }
132
146
  // Routes import their generated `./+types` modules; bring them up to date before tsc reads them.
133
147
  const { refreshRouteTypes } = await import("./route-types.ts")
@@ -141,11 +155,28 @@ async function typecheck(cwd: string, signal?: AbortSignal): Promise<TypecheckRe
141
155
  note: "typescript not installed (run: bun add -d typescript)",
142
156
  }
143
157
  }
144
- const first = await runTsc(cwd, tscBin, tsconfig, true, signal)
145
- // A compiler that rejects `--listFiles` still gets its typecheck; only the coverage check is lost.
146
- if (!first.ok && !first.cancelled && /TS50(?:23|25)\b[^\n]*listFiles/.test(first.output ?? ""))
147
- return runTsc(cwd, tscBin, tsconfig, false, signal)
148
- return first
158
+ const runs: TypecheckResult[] = []
159
+ for (const tsconfig of projects.paths) {
160
+ let run = await runTsc(cwd, tscBin, tsconfig, true, signal)
161
+ // A compiler that rejects `--listFiles` still gets its typecheck; only the coverage check is lost.
162
+ if (!run.ok && !run.cancelled && /TS50(?:23|25)\b[^\n]*listFiles/.test(run.output ?? ""))
163
+ run = await runTsc(cwd, tscBin, tsconfig, false, signal)
164
+ if (run.cancelled === true) return run
165
+ runs.push(run)
166
+ }
167
+ const [only] = runs
168
+ if (runs.length === 1 && only !== undefined) return only
169
+ // A file two programs both hold reports each error twice; keep one copy of each diagnostic block.
170
+ const blocks = new Set(
171
+ runs.flatMap((run) => (run.output ?? "").split(/\n(?=\S)/).filter((block) => block !== "")),
172
+ )
173
+ const programFiles = [...new Set(runs.flatMap((run) => run.programFiles ?? []))]
174
+ const merged: TypecheckResult = {
175
+ ran: true,
176
+ ok: runs.every((run) => run.ok),
177
+ output: [...blocks].join("\n"),
178
+ }
179
+ return programFiles.length > 0 ? { ...merged, programFiles } : merged
149
180
  }
150
181
 
151
182
  async function runTsc(
@@ -221,6 +252,7 @@ async function loadCheckConfig(
221
252
  const parsed = JSON.parse(await Bun.file(path).text()) as {
222
253
  externalMounts?: unknown
223
254
  rules?: unknown
255
+ tsconfigs?: unknown
224
256
  }
225
257
  const raw = Array.isArray(parsed.externalMounts) ? parsed.externalMounts : []
226
258
  const externalMounts = raw
@@ -273,7 +305,25 @@ async function loadCheckConfig(
273
305
  }
274
306
  }
275
307
  }
276
- return { config: { externalMounts, rules }, warnings }
308
+ let tsconfigs: readonly string[] | undefined
309
+ if (parsed.tsconfigs !== undefined) {
310
+ if (
311
+ Array.isArray(parsed.tsconfigs) &&
312
+ parsed.tsconfigs.length > 0 &&
313
+ parsed.tsconfigs.every(
314
+ (entry): entry is string =>
315
+ typeof entry === "string" && entry.endsWith(".json") && !isAbsolute(entry),
316
+ )
317
+ )
318
+ tsconfigs = parsed.tsconfigs
319
+ else
320
+ warnings.push(
321
+ '`tsconfigs` must be a non-empty array of tsconfig paths relative to the project, such as ["tsconfig.json"] - ignored',
322
+ )
323
+ }
324
+ const config: CheckConfig =
325
+ tsconfigs === undefined ? { externalMounts, rules } : { externalMounts, rules, tsconfigs }
326
+ return { config, warnings }
277
327
  } catch (error) {
278
328
  return {
279
329
  config: empty,
@@ -369,7 +419,7 @@ async function buildProjectScan(
369
419
  const [typecheckRun, _, doctor, manifestDrift, contracts] = await Promise.all([
370
420
  opts.lintsOnly
371
421
  ? Promise.resolve<TypecheckResult>({ ran: false, ok: true, note: "lints-only mode" })
372
- : typecheck(cwd, opts.signal),
422
+ : typecheck(cwd, opts.signal, checkConfig.tsconfigs),
373
423
  walkSource(
374
424
  cwd,
375
425
  (rel, content) => {
@@ -607,6 +657,11 @@ export function renderCheckReport(result: CheckResult): string[] {
607
657
  `• intentional external mounts (not typed-client checked): ${result.externalMounts.join(", ")}`,
608
658
  )
609
659
  }
660
+ if (result.tsconfigs !== undefined) {
661
+ lines.push(
662
+ `• typecheck programs (nifra.check.json "tsconfigs"): ${result.tsconfigs.join(", ")}`,
663
+ )
664
+ }
610
665
  for (const gap of result.knownGaps ?? []) {
611
666
  lines.push(`• known gap, not capability-analyzed: ${gap.path} - ${gap.reason}`)
612
667
  }