@nifrajs/cli 5.0.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 (296) hide show
  1. package/CHANGELOG.md +3211 -0
  2. package/README.md +10 -2
  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 +5 -2
  9. package/dist/assure-hydration.js.map +1 -1
  10. package/dist/assure.d.ts +12 -2
  11. package/dist/assure.d.ts.map +1 -1
  12. package/dist/assure.js +73 -9
  13. package/dist/assure.js.map +1 -1
  14. package/dist/capabilities-tool.d.ts.map +1 -1
  15. package/dist/capabilities-tool.js +152 -49
  16. package/dist/capabilities-tool.js.map +1 -1
  17. package/dist/check-diagnostics.d.ts +7 -0
  18. package/dist/check-diagnostics.d.ts.map +1 -1
  19. package/dist/check-diagnostics.js +3 -0
  20. package/dist/check-diagnostics.js.map +1 -1
  21. package/dist/check-scan.d.ts +8 -2
  22. package/dist/check-scan.d.ts.map +1 -1
  23. package/dist/check-scan.js +49 -13
  24. package/dist/check-scan.js.map +1 -1
  25. package/dist/check.d.ts.map +1 -1
  26. package/dist/check.js +91 -45
  27. package/dist/check.js.map +1 -1
  28. package/dist/cli.d.ts +2 -0
  29. package/dist/cli.d.ts.map +1 -1
  30. package/dist/cli.js +128 -59
  31. package/dist/cli.js.map +1 -1
  32. package/dist/client-module.d.ts +5 -3
  33. package/dist/client-module.d.ts.map +1 -1
  34. package/dist/client-module.js +34 -10
  35. package/dist/client-module.js.map +1 -1
  36. package/dist/command-catalog.d.ts +9 -1
  37. package/dist/command-catalog.d.ts.map +1 -1
  38. package/dist/command-catalog.js +75 -26
  39. package/dist/command-catalog.js.map +1 -1
  40. package/dist/dev-bun-config.d.ts +6 -3
  41. package/dist/dev-bun-config.d.ts.map +1 -1
  42. package/dist/dev-bun-config.js +13 -10
  43. package/dist/dev-bun-config.js.map +1 -1
  44. package/dist/dev-feed-tool.d.ts +19 -1
  45. package/dist/dev-feed-tool.d.ts.map +1 -1
  46. package/dist/dev-feed-tool.js +114 -0
  47. package/dist/dev-feed-tool.js.map +1 -1
  48. package/dist/doctor.d.ts +15 -1
  49. package/dist/doctor.d.ts.map +1 -1
  50. package/dist/doctor.js +79 -61
  51. package/dist/doctor.js.map +1 -1
  52. package/dist/fix-recipes.d.ts +2 -0
  53. package/dist/fix-recipes.d.ts.map +1 -1
  54. package/dist/fix-recipes.js +93 -12
  55. package/dist/fix-recipes.js.map +1 -1
  56. package/dist/i18n-check.d.ts +11 -1
  57. package/dist/i18n-check.d.ts.map +1 -1
  58. package/dist/i18n-check.js +170 -17
  59. package/dist/i18n-check.js.map +1 -1
  60. package/dist/init-agents.d.ts +60 -4
  61. package/dist/init-agents.d.ts.map +1 -1
  62. package/dist/init-agents.js +196 -33
  63. package/dist/init-agents.js.map +1 -1
  64. package/dist/internal/config-paths.d.ts +16 -0
  65. package/dist/internal/config-paths.d.ts.map +1 -0
  66. package/dist/internal/config-paths.js +124 -0
  67. package/dist/internal/config-paths.js.map +1 -0
  68. package/dist/internal/installed-package.d.ts +4 -0
  69. package/dist/internal/installed-package.d.ts.map +1 -0
  70. package/dist/internal/installed-package.js +17 -0
  71. package/dist/internal/installed-package.js.map +1 -0
  72. package/dist/internal/project-specifiers.d.ts +14 -1
  73. package/dist/internal/project-specifiers.d.ts.map +1 -1
  74. package/dist/internal/project-specifiers.js +63 -16
  75. package/dist/internal/project-specifiers.js.map +1 -1
  76. package/dist/internal/source-facts.d.ts +12 -1
  77. package/dist/internal/source-facts.d.ts.map +1 -1
  78. package/dist/internal/source-facts.js +117 -5
  79. package/dist/internal/source-facts.js.map +1 -1
  80. package/dist/internal/typescript-import.d.ts.map +1 -1
  81. package/dist/internal/typescript-import.js +5 -3
  82. package/dist/internal/typescript-import.js.map +1 -1
  83. package/dist/internal/workspace-packages.d.ts +22 -0
  84. package/dist/internal/workspace-packages.d.ts.map +1 -0
  85. package/dist/internal/workspace-packages.js +215 -0
  86. package/dist/internal/workspace-packages.js.map +1 -0
  87. package/dist/introspect.d.ts +9 -0
  88. package/dist/introspect.d.ts.map +1 -1
  89. package/dist/introspect.js +32 -1
  90. package/dist/introspect.js.map +1 -1
  91. package/dist/levels-tool.d.ts.map +1 -1
  92. package/dist/levels-tool.js +5 -8
  93. package/dist/levels-tool.js.map +1 -1
  94. package/dist/load.d.ts +9 -4
  95. package/dist/load.d.ts.map +1 -1
  96. package/dist/load.js +32 -3
  97. package/dist/load.js.map +1 -1
  98. package/dist/lock-links.d.ts +42 -0
  99. package/dist/lock-links.d.ts.map +1 -0
  100. package/dist/lock-links.js +144 -0
  101. package/dist/lock-links.js.map +1 -0
  102. package/dist/mcp-context.d.ts.map +1 -1
  103. package/dist/mcp-context.js +6 -21
  104. package/dist/mcp-context.js.map +1 -1
  105. package/dist/mcp-delegate.d.ts +2 -2
  106. package/dist/mcp-delegate.d.ts.map +1 -1
  107. package/dist/mcp-delegate.js +19 -7
  108. package/dist/mcp-delegate.js.map +1 -1
  109. package/dist/mcp-exec.d.ts +1 -0
  110. package/dist/mcp-exec.d.ts.map +1 -1
  111. package/dist/mcp-exec.js +13 -5
  112. package/dist/mcp-exec.js.map +1 -1
  113. package/dist/mcp-http.js +1 -1
  114. package/dist/mcp-io.d.ts +13 -0
  115. package/dist/mcp-io.d.ts.map +1 -1
  116. package/dist/mcp-io.js +23 -0
  117. package/dist/mcp-io.js.map +1 -1
  118. package/dist/mcp-path.d.ts +2 -1
  119. package/dist/mcp-path.d.ts.map +1 -1
  120. package/dist/mcp-path.js +9 -24
  121. package/dist/mcp-path.js.map +1 -1
  122. package/dist/mcp-root.d.ts +6 -4
  123. package/dist/mcp-root.d.ts.map +1 -1
  124. package/dist/mcp-root.js +12 -8
  125. package/dist/mcp-root.js.map +1 -1
  126. package/dist/mcp.d.ts.map +1 -1
  127. package/dist/mcp.js +40 -17
  128. package/dist/mcp.js.map +1 -1
  129. package/dist/migrate-layout.d.ts +41 -1
  130. package/dist/migrate-layout.d.ts.map +1 -1
  131. package/dist/migrate-layout.js +1729 -236
  132. package/dist/migrate-layout.js.map +1 -1
  133. package/dist/output-schemas.d.ts +34 -0
  134. package/dist/output-schemas.d.ts.map +1 -1
  135. package/dist/output-schemas.js +132 -21
  136. package/dist/output-schemas.js.map +1 -1
  137. package/dist/project-path.d.ts +6 -0
  138. package/dist/project-path.d.ts.map +1 -1
  139. package/dist/project-path.js +48 -14
  140. package/dist/project-path.js.map +1 -1
  141. package/dist/project-site.d.ts +7 -0
  142. package/dist/project-site.d.ts.map +1 -0
  143. package/dist/project-site.js +19 -0
  144. package/dist/project-site.js.map +1 -0
  145. package/dist/recipes/4.0.0.d.ts.map +1 -1
  146. package/dist/recipes/4.0.0.js +2 -0
  147. package/dist/recipes/4.0.0.js.map +1 -1
  148. package/dist/recipes/5.1.0.d.ts +9 -0
  149. package/dist/recipes/5.1.0.d.ts.map +1 -0
  150. package/dist/recipes/5.1.0.js +22 -0
  151. package/dist/recipes/5.1.0.js.map +1 -0
  152. package/dist/recipes/5.2.0.d.ts +10 -0
  153. package/dist/recipes/5.2.0.d.ts.map +1 -0
  154. package/dist/recipes/5.2.0.js +32 -0
  155. package/dist/recipes/5.2.0.js.map +1 -0
  156. package/dist/recipes/index.d.ts +3 -0
  157. package/dist/recipes/index.d.ts.map +1 -1
  158. package/dist/recipes/index.js +7 -0
  159. package/dist/recipes/index.js.map +1 -1
  160. package/dist/release-verification.d.ts +5 -0
  161. package/dist/release-verification.d.ts.map +1 -1
  162. package/dist/release-verification.js +14 -0
  163. package/dist/release-verification.js.map +1 -1
  164. package/dist/review.d.ts.map +1 -1
  165. package/dist/review.js +7 -0
  166. package/dist/review.js.map +1 -1
  167. package/dist/rule-explain.d.ts +26 -0
  168. package/dist/rule-explain.d.ts.map +1 -0
  169. package/dist/rule-explain.js +49 -0
  170. package/dist/rule-explain.js.map +1 -0
  171. package/dist/rules/codes.d.ts +5 -1
  172. package/dist/rules/codes.d.ts.map +1 -1
  173. package/dist/rules/codes.js +5 -1
  174. package/dist/rules/codes.js.map +1 -1
  175. package/dist/rules/contract-visibility.d.ts.map +1 -1
  176. package/dist/rules/contract-visibility.js +93 -21
  177. package/dist/rules/contract-visibility.js.map +1 -1
  178. package/dist/rules/data-guard.d.ts +6 -0
  179. package/dist/rules/data-guard.d.ts.map +1 -1
  180. package/dist/rules/data-guard.js +37 -0
  181. package/dist/rules/data-guard.js.map +1 -1
  182. package/dist/rules/explanations.d.ts +14 -0
  183. package/dist/rules/explanations.d.ts.map +1 -0
  184. package/dist/rules/explanations.js +316 -0
  185. package/dist/rules/explanations.js.map +1 -0
  186. package/dist/rules/legacy.d.ts.map +1 -1
  187. package/dist/rules/legacy.js +44 -2
  188. package/dist/rules/legacy.js.map +1 -1
  189. package/dist/rules/merge-auth.d.ts +12 -0
  190. package/dist/rules/merge-auth.d.ts.map +1 -0
  191. package/dist/rules/merge-auth.js +52 -0
  192. package/dist/rules/merge-auth.js.map +1 -0
  193. package/dist/rules/pages.d.ts +56 -3
  194. package/dist/rules/pages.d.ts.map +1 -1
  195. package/dist/rules/pages.js +412 -23
  196. package/dist/rules/pages.js.map +1 -1
  197. package/dist/rules/routes.d.ts +3 -2
  198. package/dist/rules/routes.d.ts.map +1 -1
  199. package/dist/rules/routes.js +4 -3
  200. package/dist/rules/routes.js.map +1 -1
  201. package/dist/rules/security.d.ts +3 -2
  202. package/dist/rules/security.d.ts.map +1 -1
  203. package/dist/rules/security.js +42 -11
  204. package/dist/rules/security.js.map +1 -1
  205. package/dist/rules/zones.d.ts.map +1 -1
  206. package/dist/rules/zones.js +34 -11
  207. package/dist/rules/zones.js.map +1 -1
  208. package/dist/stylex-migrate.d.ts.map +1 -1
  209. package/dist/stylex-migrate.js +127 -17
  210. package/dist/stylex-migrate.js.map +1 -1
  211. package/dist/test-tool.d.ts.map +1 -1
  212. package/dist/test-tool.js +3 -9
  213. package/dist/test-tool.js.map +1 -1
  214. package/dist/typecheck-coverage.d.ts +11 -0
  215. package/dist/typecheck-coverage.d.ts.map +1 -1
  216. package/dist/typecheck-coverage.js +50 -7
  217. package/dist/typecheck-coverage.js.map +1 -1
  218. package/dist/upgrade.d.ts +57 -6
  219. package/dist/upgrade.d.ts.map +1 -1
  220. package/dist/upgrade.js +247 -19
  221. package/dist/upgrade.js.map +1 -1
  222. package/dist/verification-plan.d.ts.map +1 -1
  223. package/dist/verification-plan.js +7 -2
  224. package/dist/verification-plan.js.map +1 -1
  225. package/dist/version-drift.d.ts.map +1 -1
  226. package/dist/version-drift.js +66 -3
  227. package/dist/version-drift.js.map +1 -1
  228. package/dist/web-app-options.d.ts +1 -1
  229. package/dist/web-app-options.d.ts.map +1 -1
  230. package/dist/web-app-options.js.map +1 -1
  231. package/docs/examples.json +21 -3
  232. package/docs/llms-full.txt +1083 -168
  233. package/docs/types.json +535 -112
  234. package/package.json +13 -12
  235. package/src/agent-files.ts +27 -17
  236. package/src/assure-hydration.ts +5 -1
  237. package/src/assure.ts +99 -11
  238. package/src/capabilities-tool.ts +188 -53
  239. package/src/check-diagnostics.ts +10 -0
  240. package/src/check-scan.ts +65 -12
  241. package/src/check.ts +122 -51
  242. package/src/cli.ts +127 -45
  243. package/src/client-module.ts +45 -9
  244. package/src/command-catalog.ts +92 -28
  245. package/src/dev-bun-config.ts +13 -9
  246. package/src/dev-feed-tool.ts +146 -1
  247. package/src/doctor.ts +102 -66
  248. package/src/fix-recipes.ts +101 -10
  249. package/src/i18n-check.ts +254 -18
  250. package/src/init-agents.ts +225 -44
  251. package/src/internal/config-paths.ts +154 -0
  252. package/src/internal/installed-package.ts +15 -0
  253. package/src/internal/project-specifiers.ts +78 -17
  254. package/src/internal/source-facts.ts +147 -5
  255. package/src/internal/typescript-import.ts +5 -3
  256. package/src/internal/workspace-packages.ts +218 -0
  257. package/src/introspect.ts +39 -1
  258. package/src/levels-tool.ts +7 -9
  259. package/src/load.ts +46 -4
  260. package/src/lock-links.ts +173 -0
  261. package/src/mcp-context.ts +5 -18
  262. package/src/mcp-delegate.ts +22 -10
  263. package/src/mcp-exec.ts +14 -4
  264. package/src/mcp-http.ts +1 -1
  265. package/src/mcp-io.ts +33 -0
  266. package/src/mcp-path.ts +8 -22
  267. package/src/mcp-root.ts +14 -8
  268. package/src/mcp.ts +48 -19
  269. package/src/migrate-layout.ts +1968 -245
  270. package/src/output-schemas.ts +179 -23
  271. package/src/project-path.ts +44 -13
  272. package/src/project-site.ts +18 -0
  273. package/src/recipes/4.0.0.ts +2 -0
  274. package/src/recipes/5.1.0.ts +23 -0
  275. package/src/recipes/5.2.0.ts +33 -0
  276. package/src/recipes/index.ts +7 -0
  277. package/src/release-verification.ts +20 -0
  278. package/src/review.ts +7 -0
  279. package/src/rule-explain.ts +75 -0
  280. package/src/rules/codes.ts +5 -1
  281. package/src/rules/contract-visibility.ts +118 -20
  282. package/src/rules/data-guard.ts +40 -0
  283. package/src/rules/explanations.ts +329 -0
  284. package/src/rules/legacy.ts +46 -2
  285. package/src/rules/merge-auth.ts +55 -0
  286. package/src/rules/pages.ts +486 -25
  287. package/src/rules/routes.ts +4 -3
  288. package/src/rules/security.ts +49 -11
  289. package/src/rules/zones.ts +33 -9
  290. package/src/stylex-migrate.ts +117 -17
  291. package/src/test-tool.ts +3 -9
  292. package/src/typecheck-coverage.ts +51 -6
  293. package/src/upgrade.ts +312 -19
  294. package/src/verification-plan.ts +15 -2
  295. package/src/version-drift.ts +68 -2
  296. package/src/web-app-options.ts +9 -1
package/src/doctor.ts CHANGED
@@ -39,6 +39,12 @@ import {
39
39
  import { collectStaleMcpPins, type StaleMcpPin } from "./init-agents.ts"
40
40
  import { codeUnitOrder } from "./internal/code-unit-order.ts"
41
41
  import { createSpecifierResolver } from "./internal/project-specifiers.ts"
42
+ import {
43
+ bunLockDir,
44
+ type StaleLinkLock,
45
+ staleLinkLockMessage,
46
+ staleLinkLocks,
47
+ } from "./lock-links.ts"
42
48
  import { detectToolingDrift, syncMcpCommand, type ToolingDrift } from "./mcp-root.ts"
43
49
  import { collectPipelineReport, type PipelineReport } from "./pipeline-report.ts"
44
50
  import { type ResolvedTarget, resolveTarget } from "./port.ts"
@@ -203,7 +209,18 @@ export interface DoctorResult {
203
209
  * the agent's server answers for a release the code no longer builds with. Advisory (never folded
204
210
  * into `ok`). Present only when at least one file is stale. Computed by {@link collectStaleMcpPins}.
205
211
  */
206
- readonly staleMcpPins?: { readonly target: string; readonly files: readonly StaleMcpPin[] }
212
+ readonly staleMcpPins?: {
213
+ /** The installed version a pin moves to, or `null` when the project declares `@nifrajs/cli` and
214
+ * the launch should drop its pin. */
215
+ readonly target: string | null
216
+ readonly files: readonly StaleMcpPin[]
217
+ }
218
+ /**
219
+ * `file:` entries of the project's bun.lock whose recorded dependencies the linked package.json no
220
+ * longer declares: Bun never refreshes them, so it installs a second copy under the linked package.
221
+ * Advisory (never folded into `ok`). `lock` is the lockfile, relative to the project.
222
+ */
223
+ readonly staleLinkLocks?: { readonly lock: string; readonly entries: readonly StaleLinkLock[] }
207
224
  /** Static production-readiness evidence for the selected deploy target. */
208
225
  readonly readiness?: DoctorReadiness
209
226
  /** The explicit source boundary used by the dependency scan. */
@@ -842,6 +859,8 @@ export async function collectDoctorResult(
842
859
  opts.cliVersion !== undefined ? await detectToolingDrift(cwd, opts.cliVersion) : undefined
843
860
  const staleMcpPins = await collectStaleMcpPins(cwd).catch(() => undefined)
844
861
  const versionDrift = collectVersionDrift(cwd, opts.cliVersion)
862
+ const lockDir = bunLockDir(cwd)
863
+ const linkLocks = lockDir === undefined ? [] : staleLinkLocks(lockDir)
845
864
  const handBuilt = handBuiltManifests(cwd, manifestLists)
846
865
  // `staleDists`, `toolingDrift` and `staleMcpPins` are advisory (see DoctorResult): they never fail `ok`.
847
866
  return {
@@ -863,6 +882,14 @@ export async function collectDoctorResult(
863
882
  ...(versionDrift !== undefined ? { versionDrift } : {}),
864
883
  ...(handBuilt.length > 0 ? { handBuiltManifests: handBuilt } : {}),
865
884
  ...(staleMcpPins !== undefined && staleMcpPins.files.length > 0 ? { staleMcpPins } : {}),
885
+ ...(lockDir !== undefined && linkLocks.length > 0
886
+ ? {
887
+ staleLinkLocks: {
888
+ lock: relative(cwd, join(lockDir, "bun.lock")).split(sep).join("/"),
889
+ entries: linkLocks,
890
+ },
891
+ }
892
+ : {}),
866
893
  ...(pipeline.ran ? { pipeline } : {}),
867
894
  }
868
895
  }
@@ -956,133 +983,142 @@ export async function runDoctor(
956
983
  const result = opts.autoFix
957
984
  ? await applyDoctorAutoFix(cwd, opts)
958
985
  : await collectDoctorResult(cwd, opts)
959
- if (opts.json) {
960
- console.log(JSON.stringify(result, null, 2))
961
- return result.ok
962
- }
963
- console.log("nifra doctor\n")
986
+ console.log(
987
+ opts.json ? JSON.stringify(result, null, 2) : doctorReport(result, opts.cliVersion).join("\n"),
988
+ )
989
+ return result.ok
990
+ }
991
+
992
+ /** The human report of a doctor run, one printed line per entry. */
993
+ export function doctorReport(result: DoctorResult, cliVersion?: string): string[] {
994
+ const lines: string[] = []
995
+ lines.push("nifra doctor\n")
964
996
  if (!result.ran) {
965
- console.log("• no package.json at this directory - nothing to check")
966
- return true
997
+ lines.push("• no package.json at this directory - nothing to check")
998
+ return lines
967
999
  }
968
1000
  if (result.fixed && result.fixed.length > 0) {
969
- console.log("✓ updated package.json dependencies:")
1001
+ lines.push("✓ updated package.json dependencies:")
970
1002
  for (const f of result.fixed) {
971
- console.log(` ${f.package}@${f.version} (${f.source})`)
1003
+ lines.push(` ${f.package}@${f.version} (${f.source})`)
972
1004
  }
973
- console.log("")
1005
+ lines.push("")
974
1006
  }
975
1007
  if (result.skippedFixes && result.skippedFixes.length > 0) {
976
- console.log("• not auto-fixed:")
1008
+ lines.push("• not auto-fixed:")
977
1009
  for (const f of result.skippedFixes) {
978
- console.log(` ${f.package} - ${f.reason}; run \`${f.command.join(" ")}\``)
1010
+ lines.push(` ${f.package} - ${f.reason}; run \`${f.command.join(" ")}\``)
979
1011
  }
980
- console.log("")
1012
+ lines.push("")
981
1013
  }
982
1014
  // Printed before the findings and regardless of ok: which bundler owns this app is the frame the
983
1015
  // rest of the report is read in, not a footnote to it.
984
1016
  if (result.pipeline !== undefined) {
985
1017
  const p = result.pipeline
986
- console.log(
1018
+ lines.push(
987
1019
  p.pipeline === "unknown"
988
1020
  ? `• bundler: could not be read from ${p.configFile} - ${p.reason}\n`
989
1021
  : `• bundler: ${p.pipeline} (${p.reason})\n`,
990
1022
  )
991
1023
  for (const f of p.findings) {
992
- console.log(`${f.severity === "error" ? "✗" : "⚠"} ${f.file}:${f.line ?? 0} ${f.message}`)
993
- console.log(` fix: ${f.fix}\n`)
1024
+ lines.push(`${f.severity === "error" ? "✗" : "⚠"} ${f.file}:${f.line ?? 0} ${f.message}`)
1025
+ lines.push(` fix: ${f.fix}\n`)
994
1026
  }
995
1027
  }
996
1028
  if (result.scanScope !== undefined) {
997
- console.log(
1029
+ lines.push(
998
1030
  `• dependency scan scope: ${result.scanScope.kind} (${result.scanScope.includeTests ? "includes tests" : "production package source only"})`,
999
1031
  )
1000
1032
  if (result.scanScope.excluded.length > 0)
1001
- console.log(` excluded: ${result.scanScope.excluded.join(", ")}`)
1002
- console.log("")
1033
+ lines.push(` excluded: ${result.scanScope.excluded.join(", ")}`)
1034
+ lines.push("")
1003
1035
  }
1004
1036
  // Advisory, printed regardless of ok: a stale linked dist doesn't fail doctor, but silently eating
1005
1037
  // it costs an hour of misdiagnosis when the dev server 500s inside the package.
1006
1038
  if (result.staleDists.length > 0) {
1007
- console.log(
1039
+ lines.push(
1008
1040
  `⚠ ${result.staleDists.length} workspace-linked package(s) with a build artifact older than their source (Bun reads src, Vite SSR/node consumers read dist):\n`,
1009
1041
  )
1010
1042
  for (const f of result.staleDists) {
1011
- console.log(
1043
+ lines.push(
1012
1044
  f.missing
1013
1045
  ? ` ${f.package} - ${f.distFile} was never built`
1014
1046
  : ` ${f.package} - ${f.distFile} is ${f.behindSeconds}s behind ${f.sourceFile}`,
1015
1047
  )
1016
- console.log(` rebuild ${f.package}`)
1048
+ lines.push(` rebuild ${f.package}`)
1017
1049
  }
1018
- console.log("")
1050
+ lines.push("")
1019
1051
  }
1020
1052
  if (result.readiness !== undefined) {
1021
1053
  const readiness = result.readiness
1022
- console.log(
1054
+ lines.push(
1023
1055
  `production readiness (${readiness.target}${readiness.targetSource === null ? "" : `, detected from ${readiness.targetSource}`}):\n`,
1024
1056
  )
1025
1057
  for (const item of readiness.items) {
1026
1058
  const marker =
1027
1059
  item.status === "configured" ? "✓" : item.status === "not-applicable" ? "-" : "✗"
1028
1060
  const evidence = item.evidence === undefined ? "" : ` (${item.evidence})`
1029
- console.log(` ${marker} ${item.label}: ${item.status}${evidence}`)
1061
+ lines.push(` ${marker} ${item.label}: ${item.status}${evidence}`)
1030
1062
  }
1031
1063
  if (readiness.strict && !readiness.ok) {
1032
- console.log("\n --strict: every applicable readiness rule must be configured")
1064
+ lines.push("\n --strict: every applicable readiness rule must be configured")
1033
1065
  }
1034
- console.log("")
1066
+ lines.push("")
1035
1067
  }
1036
1068
  // Advisory, printed regardless of ok: the binary answering is a different feature version than the
1037
1069
  // one the project builds with, so its types/checks/docs describe a surface the code does not have.
1038
1070
  if (result.toolingDrift !== undefined) {
1039
1071
  const drift = result.toolingDrift
1040
- console.log(
1072
+ lines.push(
1041
1073
  `⚠ this CLI is nifra ${drift.cli}, but the project installs ${drift.package} ${drift.project} - ` +
1042
1074
  "its types, checks, and docs may describe a different version than your code builds with.",
1043
1075
  )
1044
- console.log(
1045
- ` fix: \`${syncMcpCommand(drift.cli)}\` re-pins the project's MCP launch to the nifra it installs; ` +
1076
+ lines.push(
1077
+ ` fix: \`${syncMcpCommand(drift.cli)}\` points the project's MCP launch at the nifra it installs; ` +
1046
1078
  "for the CLI itself, run the project's own (`bunx --bun nifra doctor` from the project directory, " +
1047
1079
  "or ./node_modules/.bin/nifra)\n",
1048
1080
  )
1049
1081
  }
1050
1082
  if (result.versionDrift !== undefined) {
1051
- console.log(`⚠ ${versionDriftMessage(result.versionDrift)}\n`)
1083
+ lines.push(`⚠ ${versionDriftMessage(result.versionDrift)}\n`)
1084
+ }
1085
+ if (result.staleLinkLocks !== undefined) {
1086
+ lines.push(`⚠ ${staleLinkLockMessage(result.staleLinkLocks)}\n`)
1052
1087
  }
1053
1088
  for (const finding of result.handBuiltManifests ?? []) {
1054
- console.log(`⚠ ${handBuiltManifestMessage(finding)}\n`)
1089
+ lines.push(`⚠ ${handBuiltManifestMessage(finding)}\n`)
1055
1090
  }
1056
1091
  // Advisory: the agent's MCP server is launched from these pins, so a stale one answers for an old
1057
1092
  // release even when the CLI running doctor matches the project.
1058
1093
  if (result.staleMcpPins !== undefined) {
1059
1094
  const { target, files } = result.staleMcpPins
1060
- console.log(
1061
- `⚠ the MCP launch pins a nifra other than the project's ${target} - an agent's nifra server answers for that release:`,
1095
+ lines.push(
1096
+ target === null
1097
+ ? "⚠ the MCP launch pins a nifra release while the project installs its own CLI - an agent's nifra server answers for the pinned release:"
1098
+ : `⚠ the MCP launch pins a nifra other than the project's ${target} - an agent's nifra server answers for that release:`,
1062
1099
  )
1063
- for (const file of files)
1064
- console.log(` ${file.path}: @nifrajs/cli@${file.pinned.join(", ")}`)
1100
+ for (const file of files) lines.push(` ${file.path}: @nifrajs/cli@${file.pinned.join(", ")}`)
1065
1101
  const command =
1066
- opts.cliVersion !== undefined
1067
- ? syncMcpCommand(opts.cliVersion)
1068
- : "nifra init-agents --sync-mcp"
1069
- console.log(
1070
- ` fix: \`${command}\` rewrites only the pinned version, then restart the agent\n`,
1102
+ cliVersion !== undefined ? syncMcpCommand(cliVersion) : "nifra init-agents --sync-mcp"
1103
+ lines.push(
1104
+ target === null
1105
+ ? ` fix: \`${command}\` drops the pin so the launch runs the installed CLI from then on, then restart the agent\n`
1106
+ : ` fix: \`${command}\` rewrites only the pinned version, then restart the agent\n`,
1071
1107
  )
1072
1108
  }
1073
1109
  if (result.ok) {
1074
- console.log(
1110
+ lines.push(
1075
1111
  "✓ every imported package is declared and identity-sensitive installs are deduplicated",
1076
1112
  )
1077
- if (result.identityBasis !== undefined) console.log(` identity scan: ${result.identityBasis}`)
1113
+ if (result.identityBasis !== undefined) lines.push(` identity scan: ${result.identityBasis}`)
1078
1114
  // A partial scan that prints a bare ✓ is a lie by omission - the packages past the cap were never
1079
1115
  // looked at, and the reader has no way to know that from the tick alone.
1080
1116
  if (result.identityScanTruncated === true) {
1081
- console.log(
1117
+ lines.push(
1082
1118
  " ! the identity scan was truncated, so this is not a clean bill for the whole workspace",
1083
1119
  )
1084
1120
  }
1085
- return true
1121
+ return lines
1086
1122
  }
1087
1123
  // Group by package so the fix ("add X to dependencies") is stated once with its import sites.
1088
1124
  const byPkg = new Map<string, DoctorFinding[]>()
@@ -1092,52 +1128,52 @@ export async function runDoctor(
1092
1128
  byPkg.set(f.package, list)
1093
1129
  }
1094
1130
  if (byPkg.size > 0) {
1095
- console.log(`✗ ${byPkg.size} package(s) imported but not declared in package.json:\n`)
1131
+ lines.push(`✗ ${byPkg.size} package(s) imported but not declared in package.json:\n`)
1096
1132
  for (const [pkg, sites] of [...byPkg.entries()].sort()) {
1097
- console.log(` ${pkg} - add to dependencies (\`bun add ${pkg}\`)`)
1098
- for (const s of sites) console.log(` ${s.file}:${s.line}`)
1133
+ lines.push(` ${pkg} - add to dependencies (\`bun add ${pkg}\`)`)
1134
+ for (const s of sites) lines.push(` ${s.file}:${s.line}`)
1099
1135
  }
1100
1136
  }
1101
1137
  if (result.duplicateInstalls.length > 0) {
1102
- console.log(
1138
+ lines.push(
1103
1139
  `${byPkg.size > 0 ? "\n" : ""}✗ identity-sensitive packages resolve to multiple physical copies:\n`,
1104
1140
  )
1105
- if (result.identityBasis !== undefined) console.log(` scan: ${result.identityBasis}\n`)
1141
+ if (result.identityBasis !== undefined) lines.push(` scan: ${result.identityBasis}\n`)
1106
1142
  for (const finding of result.duplicateInstalls) {
1107
- console.log(` ${finding.package} [${finding.cause}]`)
1108
- console.log(` versions: ${finding.versions.join(", ")}`)
1143
+ lines.push(` ${finding.package} [${finding.cause}]`)
1144
+ lines.push(` versions: ${finding.versions.join(", ")}`)
1109
1145
  for (const copy of finding.copies) {
1110
- console.log(` ${copy.version} at ${copy.path} ← ${copy.importers.join(", ")}`)
1146
+ lines.push(` ${copy.version} at ${copy.path} ← ${copy.importers.join(", ")}`)
1111
1147
  }
1112
- console.log(` ${finding.explanation}`)
1148
+ lines.push(` ${finding.explanation}`)
1113
1149
  // A planted symlink is the cause no reinstall here can see, so it is named before the fix.
1114
- if (finding.provenance !== undefined) console.log(` links: ${finding.provenance}`)
1150
+ if (finding.provenance !== undefined) lines.push(` links: ${finding.provenance}`)
1115
1151
  // The shape of the split decides which fix can work, so it is printed above the fix itself.
1116
- if (finding.topology !== undefined) console.log(` topology: ${finding.topology}`)
1152
+ if (finding.topology !== undefined) lines.push(` topology: ${finding.topology}`)
1117
1153
  // Why a copy this directory never imports still counts. Without it, a workspace-wide answer
1118
1154
  // read from inside one app looks like the tool checking the wrong project.
1119
- if (finding.scope !== undefined) console.log(` scope: ${finding.scope}`)
1120
- console.log(` fix: ${finding.remediation}`)
1155
+ if (finding.scope !== undefined) lines.push(` scope: ${finding.scope}`)
1156
+ lines.push(` fix: ${finding.remediation}`)
1121
1157
  }
1122
1158
  }
1123
1159
  // Not a failure and not silence: the copies are real, and whoever reads this report needs to know
1124
1160
  // the graph is held together by a declaration rather than by the install.
1125
1161
  if (result.deduplicatedInstalls !== undefined && result.deduplicatedInstalls.length > 0) {
1126
- console.log("\n• identity-sensitive packages deduplicated by declaration:\n")
1162
+ lines.push("\n• identity-sensitive packages deduplicated by declaration:\n")
1127
1163
  for (const finding of result.deduplicatedInstalls) {
1128
- console.log(` ${finding.package} @ ${finding.versions.join(", ")}`)
1164
+ lines.push(` ${finding.package} @ ${finding.versions.join(", ")}`)
1129
1165
  for (const copy of finding.copies) {
1130
- console.log(` ${copy.path} ← ${copy.importers.join(", ")}`)
1166
+ lines.push(` ${copy.path} ← ${copy.importers.join(", ")}`)
1131
1167
  }
1132
- console.log(` ${finding.remediation}`)
1168
+ lines.push(` ${finding.remediation}`)
1133
1169
  }
1134
1170
  }
1135
1171
  if (result.readiness?.strict === true && result.readiness.ok === false) {
1136
- console.log("\n✗ production readiness is incomplete under --strict")
1172
+ lines.push("\n✗ production readiness is incomplete under --strict")
1137
1173
  }
1138
1174
  if (byPkg.size > 0)
1139
- console.log(
1175
+ lines.push(
1140
1176
  "\nThese resolve at Bun runtime via hoisting/workspace but break `tsc` and a standalone install.",
1141
1177
  )
1142
- return false
1178
+ return lines
1143
1179
  }
@@ -7,6 +7,8 @@ import { resolveWorkspaceLinkedPackage } from "./workspace-link.ts"
7
7
  export interface FixRecipe {
8
8
  readonly id: string
9
9
  readonly description: string
10
+ /** Run only by `nifra fix --code <code>`: the change it makes is a decision a person reviews first. */
11
+ readonly onlyWhenNamed?: true
10
12
  apply(root: string, diagnostic: Diagnostic): Promise<readonly string[]>
11
13
  readonly verify: string
12
14
  }
@@ -84,22 +86,39 @@ registerFixRecipe({
84
86
  description:
85
87
  "Serve the backend over HTTP at /api by exporting apiPrefix from the framework config.",
86
88
  verify: "nifra check",
87
- async apply(root, diagnostic) {
88
- if (diagnostic.file === undefined) return []
89
- const path = await resolveInsideProject(root, diagnostic.file)
90
- if (path === undefined) return []
91
- const { withDefaultApiPrefix } = await import("./rules/pages.ts")
92
- const file = Bun.file(path)
93
- const next = withDefaultApiPrefix((await file.exists()) ? await file.text() : undefined)
94
- if (next === undefined) return []
95
- await writeFile(path, next, "utf8")
96
- return [diagnostic.file]
89
+ async apply(root) {
90
+ const { CONFIG_FILE, FRAMEWORK_FILE } = await import("./app-files.ts")
91
+ const { apiPrefixFix } = await import("./rules/pages.ts")
92
+ const frameworkPath = await resolveInsideProject(root, FRAMEWORK_FILE)
93
+ const configPath = await resolveInsideProject(root, CONFIG_FILE)
94
+ if (frameworkPath === undefined || configPath === undefined) return []
95
+ const source = async (path: string): Promise<string | undefined> => {
96
+ const file = Bun.file(path)
97
+ return (await file.exists()) ? await file.text() : undefined
98
+ }
99
+ const next = apiPrefixFix({
100
+ framework: await source(frameworkPath),
101
+ config: await source(configPath),
102
+ })
103
+ const edits: readonly (readonly [string, string, string | undefined])[] = [
104
+ [FRAMEWORK_FILE, frameworkPath, next.framework],
105
+ [CONFIG_FILE, configPath, next.config],
106
+ ]
107
+ const changed: string[] = []
108
+ for (const [file, path, text] of edits) {
109
+ if (text === undefined) continue
110
+ await writeFile(path, text, "utf8")
111
+ changed.push(file)
112
+ }
113
+ return changed
97
114
  },
98
115
  })
99
116
 
100
117
  registerFixRecipe({
101
118
  id: "contracts.snapshot",
102
119
  description: "Refresh the opt-in contract lock after reviewing an intentional route change.",
120
+ // Rewriting the lock accepts whatever the routes changed to, which is the review the lock exists for.
121
+ onlyWhenNamed: true,
103
122
  verify: "nifra contracts check",
104
123
  async apply(root) {
105
124
  const { snapshotContracts } = await import("./contracts.ts")
@@ -158,6 +177,78 @@ registerFixRecipe({
158
177
  },
159
178
  })
160
179
 
180
+ registerFixRecipe({
181
+ id: "route.split",
182
+ description:
183
+ "Move a route frontend file's server-only exports, and the code only they use, into its x.backend.ts.",
184
+ verify: "nifra check",
185
+ async apply(root, diagnostic) {
186
+ const file = diagnostic.file
187
+ if (file === undefined || (await resolveInsideProject(root, file)) === undefined) return []
188
+ const { splitRouteFile } = await import("./migrate-layout.ts")
189
+ return splitRouteFile(root, file)
190
+ },
191
+ })
192
+
193
+ registerFixRecipe({
194
+ id: "layout.migrate",
195
+ description:
196
+ "Move the app onto the frontend/backend layout, as `nifra migrate layout --write` does.",
197
+ onlyWhenNamed: true,
198
+ verify: "nifra check --lints-only",
199
+ async apply(root) {
200
+ const { migrateLayout } = await import("./migrate-layout.ts")
201
+ const result = await migrateLayout(root, { write: true })
202
+ return [
203
+ ...new Set([
204
+ ...result.moves.map((move) => move.to),
205
+ ...result.splits.flatMap((split) => [split.file, split.backend]),
206
+ ...result.rewritten,
207
+ ...result.tooling.map((edit) => edit.file),
208
+ ]),
209
+ ].sort()
210
+ },
211
+ })
212
+
213
+ registerFixRecipe({
214
+ id: "schemas.draft",
215
+ description:
216
+ "Draft the missing loader or action output schema from the function's return type, for the author to review.",
217
+ verify: "nifra check --lints-only",
218
+ async apply(root, diagnostic) {
219
+ const file = diagnostic.file
220
+ if (file === undefined) return []
221
+ const path = await resolveInsideProject(root, file)
222
+ if (path === undefined) return []
223
+ const schema = diagnostic.evidence
224
+ ?.find((line) => line.startsWith("missing: "))
225
+ ?.slice("missing: ".length)
226
+ if (schema !== "loaderOutput" && schema !== "actionOutput") return []
227
+ const { planOutputSchemas } = await import("./output-schemas.ts")
228
+ const source = await readFile(path, "utf8")
229
+ // Only this declaration: other schemas in the file, generated or hand-written, stay as they are.
230
+ const plan = await planOutputSchemas(
231
+ root,
232
+ [file],
233
+ (read) => (read === file ? source : undefined),
234
+ {
235
+ draft: (at, name) => at === file && name === schema,
236
+ },
237
+ )
238
+ const blocked =
239
+ plan.warnings.length > 0
240
+ ? plan.warnings.join("; ")
241
+ : plan.entries.find((entry) => entry.schema === schema && entry.reason !== undefined)
242
+ ?.reason
243
+ if (blocked !== undefined)
244
+ throw new Error(`[nifra] no ${schema} was drafted in ${file}: ${blocked}`)
245
+ const drafted = plan.edits.get(file)
246
+ if (drafted === undefined) return []
247
+ await writeFile(path, drafted, "utf8")
248
+ return [file]
249
+ },
250
+ })
251
+
161
252
  /**
162
253
  * How many typecheck passes the reserved-segment codemod will run. A nested collision
163
254
  * (`/api/delete/then`) only reveals its second site once the first is rewritten, so one pass is not