@nifrajs/cli 5.1.0 → 5.3.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 (230) hide show
  1. package/CHANGELOG.md +3313 -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 +7 -2
  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 +5 -0
  19. package/dist/check-diagnostics.js.map +1 -1
  20. package/dist/check-scan.d.ts +9 -3
  21. package/dist/check-scan.d.ts.map +1 -1
  22. package/dist/check-scan.js +61 -21
  23. package/dist/check-scan.js.map +1 -1
  24. package/dist/check.d.ts.map +1 -1
  25. package/dist/check.js +55 -15
  26. package/dist/check.js.map +1 -1
  27. package/dist/cli.d.ts.map +1 -1
  28. package/dist/cli.js +37 -18
  29. package/dist/cli.js.map +1 -1
  30. package/dist/command-catalog.d.ts.map +1 -1
  31. package/dist/command-catalog.js +9 -5
  32. package/dist/command-catalog.js.map +1 -1
  33. package/dist/db-tool.d.ts.map +1 -1
  34. package/dist/db-tool.js +2 -1
  35. package/dist/db-tool.js.map +1 -1
  36. package/dist/dev-bun-config.d.ts +6 -3
  37. package/dist/dev-bun-config.d.ts.map +1 -1
  38. package/dist/dev-bun-config.js +13 -10
  39. package/dist/dev-bun-config.js.map +1 -1
  40. package/dist/dev-feed-tool.d.ts +3 -2
  41. package/dist/dev-feed-tool.d.ts.map +1 -1
  42. package/dist/dev-feed-tool.js +26 -5
  43. package/dist/dev-feed-tool.js.map +1 -1
  44. package/dist/doctor.d.ts +15 -1
  45. package/dist/doctor.d.ts.map +1 -1
  46. package/dist/doctor.js +79 -61
  47. package/dist/doctor.js.map +1 -1
  48. package/dist/env-file.d.ts.map +1 -1
  49. package/dist/env-file.js +41 -8
  50. package/dist/env-file.js.map +1 -1
  51. package/dist/i18n-check.d.ts +11 -1
  52. package/dist/i18n-check.d.ts.map +1 -1
  53. package/dist/i18n-check.js +170 -17
  54. package/dist/i18n-check.js.map +1 -1
  55. package/dist/init-agents.d.ts +46 -6
  56. package/dist/init-agents.d.ts.map +1 -1
  57. package/dist/init-agents.js +193 -31
  58. package/dist/init-agents.js.map +1 -1
  59. package/dist/internal/project-specifiers.d.ts.map +1 -1
  60. package/dist/internal/project-specifiers.js +9 -1
  61. package/dist/internal/project-specifiers.js.map +1 -1
  62. package/dist/internal/source-facts.d.ts +22 -0
  63. package/dist/internal/source-facts.d.ts.map +1 -1
  64. package/dist/internal/source-facts.js +322 -0
  65. package/dist/internal/source-facts.js.map +1 -1
  66. package/dist/internal/workspace-packages.d.ts +22 -0
  67. package/dist/internal/workspace-packages.d.ts.map +1 -0
  68. package/dist/internal/workspace-packages.js +215 -0
  69. package/dist/internal/workspace-packages.js.map +1 -0
  70. package/dist/load.d.ts +7 -2
  71. package/dist/load.d.ts.map +1 -1
  72. package/dist/load.js +11 -2
  73. package/dist/load.js.map +1 -1
  74. package/dist/lock-links.d.ts +42 -0
  75. package/dist/lock-links.d.ts.map +1 -0
  76. package/dist/lock-links.js +144 -0
  77. package/dist/lock-links.js.map +1 -0
  78. package/dist/mcp-exec.d.ts +1 -0
  79. package/dist/mcp-exec.d.ts.map +1 -1
  80. package/dist/mcp-exec.js +16 -5
  81. package/dist/mcp-exec.js.map +1 -1
  82. package/dist/mcp-http.js +1 -1
  83. package/dist/mcp-io.d.ts +23 -3
  84. package/dist/mcp-io.d.ts.map +1 -1
  85. package/dist/mcp-io.js +59 -12
  86. package/dist/mcp-io.js.map +1 -1
  87. package/dist/mcp-isolate.d.ts.map +1 -1
  88. package/dist/mcp-isolate.js +2 -1
  89. package/dist/mcp-isolate.js.map +1 -1
  90. package/dist/mcp-root.d.ts +6 -4
  91. package/dist/mcp-root.d.ts.map +1 -1
  92. package/dist/mcp-root.js +12 -8
  93. package/dist/mcp-root.js.map +1 -1
  94. package/dist/migrate-layout.d.ts +2 -1
  95. package/dist/migrate-layout.d.ts.map +1 -1
  96. package/dist/migrate-layout.js +435 -29
  97. package/dist/migrate-layout.js.map +1 -1
  98. package/dist/project-site.d.ts +7 -0
  99. package/dist/project-site.d.ts.map +1 -0
  100. package/dist/project-site.js +19 -0
  101. package/dist/project-site.js.map +1 -0
  102. package/dist/recipes/4.0.0.d.ts.map +1 -1
  103. package/dist/recipes/4.0.0.js +2 -0
  104. package/dist/recipes/4.0.0.js.map +1 -1
  105. package/dist/recipes/5.1.0.d.ts +9 -0
  106. package/dist/recipes/5.1.0.d.ts.map +1 -0
  107. package/dist/recipes/5.1.0.js +22 -0
  108. package/dist/recipes/5.1.0.js.map +1 -0
  109. package/dist/recipes/5.2.0.d.ts +10 -0
  110. package/dist/recipes/5.2.0.d.ts.map +1 -0
  111. package/dist/recipes/5.2.0.js +32 -0
  112. package/dist/recipes/5.2.0.js.map +1 -0
  113. package/dist/recipes/5.3.0.d.ts +11 -0
  114. package/dist/recipes/5.3.0.d.ts.map +1 -0
  115. package/dist/recipes/5.3.0.js +35 -0
  116. package/dist/recipes/5.3.0.js.map +1 -0
  117. package/dist/recipes/index.d.ts +3 -0
  118. package/dist/recipes/index.d.ts.map +1 -1
  119. package/dist/recipes/index.js +9 -0
  120. package/dist/recipes/index.js.map +1 -1
  121. package/dist/review.d.ts.map +1 -1
  122. package/dist/review.js +4 -0
  123. package/dist/review.js.map +1 -1
  124. package/dist/rule-explain.d.ts +26 -0
  125. package/dist/rule-explain.d.ts.map +1 -0
  126. package/dist/rule-explain.js +49 -0
  127. package/dist/rule-explain.js.map +1 -0
  128. package/dist/rules/codes.d.ts +4 -0
  129. package/dist/rules/codes.d.ts.map +1 -1
  130. package/dist/rules/codes.js +4 -0
  131. package/dist/rules/codes.js.map +1 -1
  132. package/dist/rules/contract-visibility.d.ts.map +1 -1
  133. package/dist/rules/contract-visibility.js +93 -21
  134. package/dist/rules/contract-visibility.js.map +1 -1
  135. package/dist/rules/explanations.d.ts +14 -0
  136. package/dist/rules/explanations.d.ts.map +1 -0
  137. package/dist/rules/explanations.js +326 -0
  138. package/dist/rules/explanations.js.map +1 -0
  139. package/dist/rules/legacy.d.ts.map +1 -1
  140. package/dist/rules/legacy.js +24 -1
  141. package/dist/rules/legacy.js.map +1 -1
  142. package/dist/rules/merge-auth.d.ts +12 -0
  143. package/dist/rules/merge-auth.d.ts.map +1 -0
  144. package/dist/rules/merge-auth.js +52 -0
  145. package/dist/rules/merge-auth.js.map +1 -0
  146. package/dist/rules/pages.d.ts +51 -0
  147. package/dist/rules/pages.d.ts.map +1 -1
  148. package/dist/rules/pages.js +293 -2
  149. package/dist/rules/pages.js.map +1 -1
  150. package/dist/rules/route-statement.d.ts +10 -0
  151. package/dist/rules/route-statement.d.ts.map +1 -0
  152. package/dist/rules/route-statement.js +150 -0
  153. package/dist/rules/route-statement.js.map +1 -0
  154. package/dist/rules/routes.d.ts +3 -2
  155. package/dist/rules/routes.d.ts.map +1 -1
  156. package/dist/rules/routes.js +4 -3
  157. package/dist/rules/routes.js.map +1 -1
  158. package/dist/rules/zones.d.ts.map +1 -1
  159. package/dist/rules/zones.js +3 -2
  160. package/dist/rules/zones.js.map +1 -1
  161. package/dist/typecheck-coverage.d.ts +11 -0
  162. package/dist/typecheck-coverage.d.ts.map +1 -1
  163. package/dist/typecheck-coverage.js +50 -7
  164. package/dist/typecheck-coverage.js.map +1 -1
  165. package/dist/upgrade.d.ts +80 -11
  166. package/dist/upgrade.d.ts.map +1 -1
  167. package/dist/upgrade.js +332 -65
  168. package/dist/upgrade.js.map +1 -1
  169. package/dist/verification-plan.d.ts.map +1 -1
  170. package/dist/verification-plan.js +2 -0
  171. package/dist/verification-plan.js.map +1 -1
  172. package/dist/version-drift.d.ts.map +1 -1
  173. package/dist/version-drift.js +12 -1
  174. package/dist/version-drift.js.map +1 -1
  175. package/dist/web-app-options.d.ts +1 -1
  176. package/dist/web-app-options.d.ts.map +1 -1
  177. package/dist/web-app-options.js.map +1 -1
  178. package/docs/examples.json +19 -1
  179. package/docs/llms-full.txt +716 -138
  180. package/docs/types.json +516 -74
  181. package/package.json +13 -12
  182. package/src/agent-files.ts +27 -17
  183. package/src/assure-hydration.ts +8 -1
  184. package/src/assure.ts +11 -1
  185. package/src/capabilities-tool.ts +159 -50
  186. package/src/check-diagnostics.ts +10 -0
  187. package/src/check-scan.ts +64 -19
  188. package/src/check.ts +75 -15
  189. package/src/cli.ts +38 -17
  190. package/src/command-catalog.ts +16 -8
  191. package/src/db-tool.ts +2 -0
  192. package/src/dev-bun-config.ts +13 -9
  193. package/src/dev-feed-tool.ts +40 -6
  194. package/src/doctor.ts +102 -66
  195. package/src/env-file.ts +42 -7
  196. package/src/i18n-check.ts +254 -18
  197. package/src/init-agents.ts +221 -41
  198. package/src/internal/project-specifiers.ts +9 -1
  199. package/src/internal/source-facts.ts +397 -0
  200. package/src/internal/workspace-packages.ts +218 -0
  201. package/src/load.ts +19 -3
  202. package/src/lock-links.ts +173 -0
  203. package/src/mcp-exec.ts +18 -4
  204. package/src/mcp-http.ts +1 -1
  205. package/src/mcp-io.ts +72 -12
  206. package/src/mcp-isolate.ts +2 -0
  207. package/src/mcp-root.ts +14 -8
  208. package/src/migrate-layout.ts +496 -31
  209. package/src/project-site.ts +18 -0
  210. package/src/recipes/4.0.0.ts +2 -0
  211. package/src/recipes/5.1.0.ts +23 -0
  212. package/src/recipes/5.2.0.ts +33 -0
  213. package/src/recipes/5.3.0.ts +36 -0
  214. package/src/recipes/index.ts +9 -0
  215. package/src/review.ts +4 -0
  216. package/src/rule-explain.ts +75 -0
  217. package/src/rules/codes.ts +4 -0
  218. package/src/rules/contract-visibility.ts +118 -20
  219. package/src/rules/explanations.ts +339 -0
  220. package/src/rules/legacy.ts +24 -1
  221. package/src/rules/merge-auth.ts +55 -0
  222. package/src/rules/pages.ts +381 -2
  223. package/src/rules/route-statement.ts +170 -0
  224. package/src/rules/routes.ts +4 -3
  225. package/src/rules/zones.ts +3 -2
  226. package/src/typecheck-coverage.ts +51 -6
  227. package/src/upgrade.ts +430 -70
  228. package/src/verification-plan.ts +6 -0
  229. package/src/version-drift.ts +15 -0
  230. 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
  }
package/src/env-file.ts CHANGED
@@ -16,7 +16,9 @@
16
16
  * would make the command look like it verified an environment it never loaded.
17
17
  */
18
18
  import { readdir, readFile } from "node:fs/promises"
19
+ import { devNull } from "node:os"
19
20
  import { resolve } from "node:path"
21
+ import { readBoundedStream } from "./mcp-io.ts"
20
22
 
21
23
  const ENV_FILE_FLAG = "--env-file"
22
24
 
@@ -179,24 +181,57 @@ async function envFromFiles(
179
181
  return added
180
182
  }
181
183
 
182
- /** What `process.env` holds in a Bun process started in `cwd` with `env` and the runtime `flags`. */
184
+ const PROBE_TIMEOUT_MS = 10_000
185
+ const PROBE_MAX_BYTES = 4 * 1024 * 1024
186
+
187
+ /**
188
+ * What `process.env` holds in a Bun process started in `cwd` with `env` and the runtime `flags`. The
189
+ * empty `--config` keeps the project's bunfig preloads out: both probes would differ by none of what
190
+ * they set, and one that keeps its process alive held `nifra mcp` before it ever answered. A probe
191
+ * that still fails to answer in time fails the start, rather than serving with values it cannot tell
192
+ * came from a file.
193
+ */
183
194
  async function childEnv(
184
195
  cwd: string,
185
196
  env: Record<string, string | undefined>,
186
197
  flags: readonly string[],
187
198
  ): Promise<Record<string, string>> {
188
199
  const proc = Bun.spawn(
189
- [process.execPath, ...flags, "-e", "process.stdout.write(JSON.stringify(process.env))"],
200
+ [
201
+ process.execPath,
202
+ `--config=${devNull}`,
203
+ ...flags,
204
+ "-e",
205
+ "process.stdout.write(JSON.stringify(process.env), () => process.exit(0))",
206
+ ],
190
207
  { cwd, env, stdin: "ignore", stdout: "pipe", stderr: "ignore" },
191
208
  )
192
- const [text] = await Promise.all([new Response(proc.stdout).text(), proc.exited])
209
+ let failure: string | undefined
210
+ const timer = setTimeout(() => {
211
+ failure = `took longer than ${PROBE_TIMEOUT_MS}ms`
212
+ proc.kill("SIGKILL")
213
+ }, PROBE_TIMEOUT_MS)
193
214
  let value: unknown
194
215
  try {
195
- value = JSON.parse(text)
196
- } catch {
197
- return {}
216
+ const [output, code] = await Promise.all([
217
+ readBoundedStream(proc.stdout, PROBE_MAX_BYTES, () => proc.kill("SIGKILL")),
218
+ proc.exited,
219
+ ])
220
+ if (output.truncated) failure ??= `printed more than ${PROBE_MAX_BYTES} bytes`
221
+ else if (code !== 0) failure ??= `exited with code ${code}`
222
+ else value = JSON.parse(output.text)
223
+ } catch (error) {
224
+ failure ??= error instanceof Error ? error.message : String(error)
225
+ } finally {
226
+ clearTimeout(timer)
227
+ }
228
+ if (failure !== undefined || typeof value !== "object" || value === null) {
229
+ throw new Error(
230
+ `[nifra] could not tell which variables the .env files in ${cwd} set: a Bun probe ${
231
+ failure ?? "printed no environment"
232
+ }`,
233
+ )
198
234
  }
199
- if (typeof value !== "object" || value === null) return {}
200
235
  return Object.fromEntries(
201
236
  Object.entries(value).filter(
202
237
  (entry): entry is [string, string] => typeof entry[1] === "string",