@nifrajs/cli 5.7.0 → 5.9.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 (253) hide show
  1. package/CHANGELOG.md +150 -0
  2. package/dist/agent-files.d.ts +0 -22
  3. package/dist/agent-files.d.ts.map +1 -1
  4. package/dist/agent-files.js +3 -1
  5. package/dist/agent-files.js.map +1 -1
  6. package/dist/assure-hydration.d.ts.map +1 -1
  7. package/dist/assure-hydration.js +2 -10
  8. package/dist/assure-hydration.js.map +1 -1
  9. package/dist/assure.d.ts.map +1 -1
  10. package/dist/assure.js +3 -3
  11. package/dist/assure.js.map +1 -1
  12. package/dist/check-scan.d.ts +2 -0
  13. package/dist/check-scan.d.ts.map +1 -1
  14. package/dist/check-scan.js +54 -86
  15. package/dist/check-scan.js.map +1 -1
  16. package/dist/check.d.ts +3 -0
  17. package/dist/check.d.ts.map +1 -1
  18. package/dist/check.js +20 -23
  19. package/dist/check.js.map +1 -1
  20. package/dist/cli.d.ts +8 -0
  21. package/dist/cli.d.ts.map +1 -1
  22. package/dist/cli.js +42 -13
  23. package/dist/cli.js.map +1 -1
  24. package/dist/command-catalog.d.ts.map +1 -1
  25. package/dist/command-catalog.js +20 -30
  26. package/dist/command-catalog.js.map +1 -1
  27. package/dist/config-target.js +1 -1
  28. package/dist/config-target.js.map +1 -1
  29. package/dist/db-child.js +3 -10
  30. package/dist/db-child.js.map +1 -1
  31. package/dist/db-config.d.ts +2 -0
  32. package/dist/db-config.d.ts.map +1 -1
  33. package/dist/db-config.js +2 -0
  34. package/dist/db-config.js.map +1 -1
  35. package/dist/doctor.d.ts +14 -3
  36. package/dist/doctor.d.ts.map +1 -1
  37. package/dist/doctor.js +91 -6
  38. package/dist/doctor.js.map +1 -1
  39. package/dist/frontend-guidance.d.ts +19 -0
  40. package/dist/frontend-guidance.d.ts.map +1 -1
  41. package/dist/frontend-guidance.js +267 -23
  42. package/dist/frontend-guidance.js.map +1 -1
  43. package/dist/hand-built-manifest.d.ts.map +1 -1
  44. package/dist/hand-built-manifest.js +1 -1
  45. package/dist/hand-built-manifest.js.map +1 -1
  46. package/dist/internal/control-character.d.ts +3 -0
  47. package/dist/internal/control-character.d.ts.map +1 -0
  48. package/dist/internal/control-character.js +10 -0
  49. package/dist/internal/control-character.js.map +1 -0
  50. package/dist/internal/escape-regexp.d.ts +3 -0
  51. package/dist/internal/escape-regexp.d.ts.map +1 -0
  52. package/dist/internal/escape-regexp.js +5 -0
  53. package/dist/internal/escape-regexp.js.map +1 -0
  54. package/dist/internal/line-at.d.ts +3 -0
  55. package/dist/internal/line-at.d.ts.map +1 -0
  56. package/dist/internal/line-at.js +5 -0
  57. package/dist/internal/line-at.js.map +1 -0
  58. package/dist/internal/nifra-project.d.ts.map +1 -1
  59. package/dist/internal/nifra-project.js +35 -41
  60. package/dist/internal/nifra-project.js.map +1 -1
  61. package/dist/internal/posix-path.d.ts +3 -0
  62. package/dist/internal/posix-path.d.ts.map +1 -0
  63. package/dist/internal/posix-path.js +5 -0
  64. package/dist/internal/posix-path.js.map +1 -0
  65. package/dist/internal/project-specifiers.d.ts.map +1 -1
  66. package/dist/internal/project-specifiers.js +3 -10
  67. package/dist/internal/project-specifiers.js.map +1 -1
  68. package/dist/internal/realpath-or-self.d.ts +3 -0
  69. package/dist/internal/realpath-or-self.d.ts.map +1 -0
  70. package/dist/internal/realpath-or-self.js +11 -0
  71. package/dist/internal/realpath-or-self.js.map +1 -0
  72. package/dist/internal/record.d.ts +3 -0
  73. package/dist/internal/record.d.ts.map +1 -0
  74. package/dist/internal/record.js +5 -0
  75. package/dist/internal/record.js.map +1 -0
  76. package/dist/internal/source-facts.d.ts.map +1 -1
  77. package/dist/internal/source-facts.js +28 -25
  78. package/dist/internal/source-facts.js.map +1 -1
  79. package/dist/introspect.d.ts +0 -22
  80. package/dist/introspect.d.ts.map +1 -1
  81. package/dist/introspect.js +11 -151
  82. package/dist/introspect.js.map +1 -1
  83. package/dist/levels-tool.d.ts +2 -0
  84. package/dist/levels-tool.d.ts.map +1 -1
  85. package/dist/levels-tool.js +54 -64
  86. package/dist/levels-tool.js.map +1 -1
  87. package/dist/load.d.ts +6 -1
  88. package/dist/load.d.ts.map +1 -1
  89. package/dist/load.js.map +1 -1
  90. package/dist/local-cli.d.ts +7 -0
  91. package/dist/local-cli.d.ts.map +1 -1
  92. package/dist/local-cli.js +13 -0
  93. package/dist/local-cli.js.map +1 -1
  94. package/dist/mcp-docs-tools.d.ts +1 -1
  95. package/dist/mcp-docs-tools.d.ts.map +1 -1
  96. package/dist/mcp-docs-tools.js +46 -3
  97. package/dist/mcp-docs-tools.js.map +1 -1
  98. package/dist/mcp-http.d.ts +1 -1
  99. package/dist/mcp-http.js +2 -2
  100. package/dist/mcp-io.d.ts +16 -0
  101. package/dist/mcp-io.d.ts.map +1 -1
  102. package/dist/mcp-io.js +63 -0
  103. package/dist/mcp-io.js.map +1 -1
  104. package/dist/mcp-isolate.d.ts.map +1 -1
  105. package/dist/mcp-isolate.js +2 -1
  106. package/dist/mcp-isolate.js.map +1 -1
  107. package/dist/mcp-project-child.d.ts.map +1 -1
  108. package/dist/mcp-project-child.js +5 -12
  109. package/dist/mcp-project-child.js.map +1 -1
  110. package/dist/mcp-render.d.ts.map +1 -1
  111. package/dist/mcp-render.js +9 -51
  112. package/dist/mcp-render.js.map +1 -1
  113. package/dist/mcp-root.js +1 -1
  114. package/dist/mcp-root.js.map +1 -1
  115. package/dist/mcp-run.d.ts.map +1 -1
  116. package/dist/mcp-run.js +3 -46
  117. package/dist/mcp-run.js.map +1 -1
  118. package/dist/mcp-ws.d.ts.map +1 -1
  119. package/dist/mcp-ws.js +2 -18
  120. package/dist/mcp-ws.js.map +1 -1
  121. package/dist/migrate-layout.d.ts.map +1 -1
  122. package/dist/migrate-layout.js +36 -57
  123. package/dist/migrate-layout.js.map +1 -1
  124. package/dist/output-schemas.d.ts.map +1 -1
  125. package/dist/output-schemas.js +7 -7
  126. package/dist/output-schemas.js.map +1 -1
  127. package/dist/port.d.ts.map +1 -1
  128. package/dist/port.js +1 -2
  129. package/dist/port.js.map +1 -1
  130. package/dist/ppr-read-fix.js +2 -2
  131. package/dist/ppr-read-fix.js.map +1 -1
  132. package/dist/recipes/5.8.0.d.ts +9 -0
  133. package/dist/recipes/5.8.0.d.ts.map +1 -0
  134. package/dist/recipes/5.8.0.js +20 -0
  135. package/dist/recipes/5.8.0.js.map +1 -0
  136. package/dist/recipes/5.9.0.d.ts +10 -0
  137. package/dist/recipes/5.9.0.d.ts.map +1 -0
  138. package/dist/recipes/5.9.0.js +41 -0
  139. package/dist/recipes/5.9.0.js.map +1 -0
  140. package/dist/recipes/index.d.ts +5 -0
  141. package/dist/recipes/index.d.ts.map +1 -1
  142. package/dist/recipes/index.js +4 -0
  143. package/dist/recipes/index.js.map +1 -1
  144. package/dist/review.d.ts.map +1 -1
  145. package/dist/review.js +9 -13
  146. package/dist/review.js.map +1 -1
  147. package/dist/rules/contract-visibility.d.ts.map +1 -1
  148. package/dist/rules/contract-visibility.js +3 -9
  149. package/dist/rules/contract-visibility.js.map +1 -1
  150. package/dist/rules/data-guard.d.ts.map +1 -1
  151. package/dist/rules/data-guard.js +3 -10
  152. package/dist/rules/data-guard.js.map +1 -1
  153. package/dist/rules/pages.d.ts.map +1 -1
  154. package/dist/rules/pages.js +41 -58
  155. package/dist/rules/pages.js.map +1 -1
  156. package/dist/rules/routes.d.ts.map +1 -1
  157. package/dist/rules/routes.js +20 -27
  158. package/dist/rules/routes.js.map +1 -1
  159. package/dist/rules/security.d.ts.map +1 -1
  160. package/dist/rules/security.js +206 -313
  161. package/dist/rules/security.js.map +1 -1
  162. package/dist/rules/zones.d.ts.map +1 -1
  163. package/dist/rules/zones.js +3 -10
  164. package/dist/rules/zones.js.map +1 -1
  165. package/dist/runtime-differences.d.ts +62 -0
  166. package/dist/runtime-differences.d.ts.map +1 -0
  167. package/dist/runtime-differences.js +222 -0
  168. package/dist/runtime-differences.js.map +1 -0
  169. package/dist/sdk.d.ts.map +1 -1
  170. package/dist/sdk.js +43 -43
  171. package/dist/sdk.js.map +1 -1
  172. package/dist/smoke.d.ts.map +1 -1
  173. package/dist/smoke.js +4 -12
  174. package/dist/smoke.js.map +1 -1
  175. package/dist/source-rewrites.d.ts +27 -0
  176. package/dist/source-rewrites.d.ts.map +1 -0
  177. package/dist/source-rewrites.js +402 -0
  178. package/dist/source-rewrites.js.map +1 -0
  179. package/dist/stylex-migrate.d.ts.map +1 -1
  180. package/dist/stylex-migrate.js +1 -1
  181. package/dist/stylex-migrate.js.map +1 -1
  182. package/dist/symptom-search.d.ts +5 -0
  183. package/dist/symptom-search.d.ts.map +1 -0
  184. package/dist/symptom-search.js +21 -0
  185. package/dist/symptom-search.js.map +1 -0
  186. package/dist/upgrade.d.ts +28 -3
  187. package/dist/upgrade.d.ts.map +1 -1
  188. package/dist/upgrade.js +153 -64
  189. package/dist/upgrade.js.map +1 -1
  190. package/dist/verification-plan.d.ts.map +1 -1
  191. package/dist/verification-plan.js +5 -3
  192. package/dist/verification-plan.js.map +1 -1
  193. package/docs/examples.json +32 -2
  194. package/docs/llms-full.txt +749 -95
  195. package/docs/types.json +300 -61
  196. package/package.json +11 -11
  197. package/src/agent-files.ts +4 -1
  198. package/src/assure-hydration.ts +2 -9
  199. package/src/assure.ts +3 -4
  200. package/src/check-scan.ts +65 -82
  201. package/src/check.ts +25 -23
  202. package/src/cli.ts +61 -13
  203. package/src/command-catalog.ts +22 -32
  204. package/src/config-target.ts +1 -1
  205. package/src/db-child.ts +3 -10
  206. package/src/db-config.ts +4 -0
  207. package/src/doctor.ts +128 -12
  208. package/src/frontend-guidance.ts +344 -25
  209. package/src/hand-built-manifest.ts +1 -2
  210. package/src/internal/control-character.ts +8 -0
  211. package/src/internal/escape-regexp.ts +4 -0
  212. package/src/internal/line-at.ts +4 -0
  213. package/src/internal/nifra-project.ts +39 -35
  214. package/src/internal/posix-path.ts +4 -0
  215. package/src/internal/project-specifiers.ts +3 -10
  216. package/src/internal/realpath-or-self.ts +10 -0
  217. package/src/internal/record.ts +4 -0
  218. package/src/internal/source-facts.ts +39 -29
  219. package/src/introspect.ts +16 -156
  220. package/src/levels-tool.ts +71 -78
  221. package/src/load.ts +6 -1
  222. package/src/local-cli.ts +12 -0
  223. package/src/mcp-docs-tools.ts +51 -3
  224. package/src/mcp-http.ts +2 -2
  225. package/src/mcp-io.ts +77 -0
  226. package/src/mcp-isolate.ts +2 -3
  227. package/src/mcp-project-child.ts +5 -14
  228. package/src/mcp-render.ts +11 -59
  229. package/src/mcp-root.ts +1 -1
  230. package/src/mcp-run.ts +5 -54
  231. package/src/mcp-ws.ts +2 -19
  232. package/src/migrate-layout.ts +52 -72
  233. package/src/output-schemas.ts +7 -8
  234. package/src/port.ts +1 -4
  235. package/src/ppr-read-fix.ts +2 -2
  236. package/src/recipes/5.8.0.ts +21 -0
  237. package/src/recipes/5.9.0.ts +42 -0
  238. package/src/recipes/index.ts +9 -0
  239. package/src/review.ts +12 -15
  240. package/src/rules/contract-visibility.ts +3 -8
  241. package/src/rules/data-guard.ts +3 -10
  242. package/src/rules/pages.ts +47 -58
  243. package/src/rules/routes.ts +21 -27
  244. package/src/rules/security.ts +224 -255
  245. package/src/rules/zones.ts +3 -10
  246. package/src/runtime-differences.ts +302 -0
  247. package/src/sdk.ts +55 -58
  248. package/src/smoke.ts +4 -13
  249. package/src/source-rewrites.ts +463 -0
  250. package/src/stylex-migrate.ts +1 -3
  251. package/src/symptom-search.ts +27 -0
  252. package/src/upgrade.ts +194 -74
  253. package/src/verification-plan.ts +10 -4
package/src/mcp-http.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The PUBLIC nifra docs MCP - Streamable-HTTP transport exposing the project-INDEPENDENT tools
3
- * (`nifra_docs`, `nifra_example`, `nifra_types`, `nifra_learn`, `nifra_frontend` + the `nifra_gallery`
3
+ * (`nifra_docs`, `nifra_example`, `nifra_types`, `nifra_learn`, `nifra_frontend`, `nifra_runtime` + the `nifra_gallery`
4
4
  * MCP Apps widget)
5
5
  * so any remote AI agent can learn nifra without a local checkout. They read the bundled corpus
6
6
  * (llms-full.txt / examples.json / types.json shipped in this package) and ignore `cwd`, so they reuse the
@@ -30,7 +30,7 @@ export { docsTools } from "./mcp-docs-tools.ts"
30
30
  export type { TypeEntry } from "./types-search.ts"
31
31
 
32
32
  // Kept in lockstep with packages/cli/package.json by check:publish's version-consistency gate.
33
- const VERSION = "5.7.0"
33
+ const VERSION = "5.9.0"
34
34
  const SERVER_INFO = { name: "nifra-docs", version: VERSION }
35
35
  // Derive the GET/health tool list from the tools actually served, so the line can never drift from them.
36
36
  const docsHealth = (tools: McpTool[]): string =>
package/src/mcp-io.ts CHANGED
@@ -5,6 +5,8 @@
5
5
  * these limits and stream readers so an untrusted local endpoint or child process cannot exhaust MCP.
6
6
  */
7
7
 
8
+ import { isRecord } from "./internal/record.ts"
9
+
8
10
  export const CHILD_TIMEOUT_MS = 30_000
9
11
  /** Maximum stdout or stderr retained from a project subprocess. The process is killed at the limit. */
10
12
  export const CHILD_OUTPUT_MAX_BYTES = 1_048_576
@@ -380,3 +382,78 @@ export async function readBoundedStream(
380
382
  reader.releaseLock()
381
383
  }
382
384
  }
385
+
386
+ /** Send console output to stderr so it never corrupts the newline-delimited JSON on stdout: a loader that
387
+ * logs would otherwise inject noise into a response line. */
388
+ export function redirectConsoleToStderr(): void {
389
+ const write = (level: string, args: unknown[]): void => {
390
+ Bun.stderr.write(`[${level}] ${args.map((arg) => String(arg)).join(" ")}\n`)
391
+ }
392
+ console.debug = (...args: unknown[]) => write("debug", args)
393
+ console.info = (...args: unknown[]) => write("info", args)
394
+ console.log = (...args: unknown[]) => write("log", args)
395
+ console.warn = (...args: unknown[]) => write("warn", args)
396
+ console.error = (...args: unknown[]) => write("error", args)
397
+ }
398
+
399
+ /** For a project child: send `console.log`, `info` and `debug` to stderr as plain lines and return
400
+ * stdout's own writer, taken before project code loads so nothing it patches reaches the answer channel. */
401
+ export function protocolStdout(): typeof process.stdout.write {
402
+ const stdout = process.stdout.write.bind(process.stdout)
403
+ const toStderr = (...args: unknown[]): void => {
404
+ process.stderr.write(`${args.map((arg) => String(arg)).join(" ")}\n`)
405
+ }
406
+ console.log = toStderr
407
+ console.info = toStderr
408
+ console.debug = toStderr
409
+ return stdout
410
+ }
411
+
412
+ function safeResponseId(value: unknown): number | string | null {
413
+ if (typeof value === "number" && Number.isSafeInteger(value)) return value
414
+ if (typeof value === "string" && value.length <= 128) return value
415
+ return null
416
+ }
417
+
418
+ /** A warm worker's reply: one `{ id, output }` line, with an output over the cap answered as an error. */
419
+ export function workerReplyWriter(
420
+ write: (line: string) => void,
421
+ ): (id: unknown, output: unknown) => void {
422
+ return (id, output) => {
423
+ const responseId = safeResponseId(id)
424
+ const encoded = serializeBoundedJson({ id: responseId, output }, CHILD_OUTPUT_MAX_BYTES - 1)
425
+ write(
426
+ `${encoded ?? JSON.stringify({ id: responseId, output: { error: "worker output exceeded the size limit" } })}\n`,
427
+ )
428
+ }
429
+ }
430
+
431
+ /** A warm worker's request line: its `id` and its `input` record, if the line carried one. */
432
+ export interface WorkerRequest {
433
+ readonly id: unknown
434
+ readonly input: Readonly<Record<string, unknown>> | undefined
435
+ }
436
+
437
+ /** Each request line a warm worker reads from stdin; an oversized or non-JSON line is answered through
438
+ * `send` and skipped. */
439
+ export async function* workerRequests(
440
+ send: (id: unknown, output: unknown) => void,
441
+ ): AsyncGenerator<WorkerRequest> {
442
+ for await (const item of readBoundedLines(Bun.stdin.stream(), CHILD_INPUT_MAX_BYTES)) {
443
+ if (item.kind === "too-large") {
444
+ send(null, { error: `worker input exceeded ${CHILD_INPUT_MAX_BYTES} bytes` })
445
+ continue
446
+ }
447
+ const line = item.text.trim()
448
+ if (line === "") continue
449
+ let message: unknown
450
+ try {
451
+ message = JSON.parse(line)
452
+ } catch {
453
+ send(null, { error: "invalid worker input: expected JSON line" })
454
+ continue
455
+ }
456
+ const record = isRecord(message) ? message : undefined
457
+ yield { id: record?.id, input: isRecord(record?.input) ? record.input : undefined }
458
+ }
459
+ }
@@ -11,6 +11,7 @@ import { existsSync } from "node:fs"
11
11
  import { resolve } from "node:path"
12
12
  import { fileURLToPath } from "node:url"
13
13
  import { CONFIG_FILE } from "./app-files.ts"
14
+ import { isRecord } from "./internal/record.ts"
14
15
  import type { NifraMonorepoConfig } from "./load.ts"
15
16
  import { appFingerprint } from "./mcp-exec.ts"
16
17
  import {
@@ -60,6 +61,7 @@ export const IN_PROCESS_TOOLS: ReadonlySet<string> = new Set([
60
61
  "nifra_types",
61
62
  "nifra_learn",
62
63
  "nifra_frontend",
64
+ "nifra_runtime",
63
65
  ])
64
66
 
65
67
  /** A tool result is bounded again by the stdio message limit when the server sends it. */
@@ -72,9 +74,6 @@ function childPath(): string {
72
74
  )
73
75
  }
74
76
 
75
- const isRecord = (value: unknown): value is Record<string, unknown> =>
76
- typeof value === "object" && value !== null && !Array.isArray(value)
77
-
78
77
  const isAnswer = (value: unknown): value is ProjectChildAnswer =>
79
78
  isRecord(value) &&
80
79
  (value.ok === true || (value.ok === false && typeof value.message === "string"))
@@ -9,10 +9,11 @@
9
9
  * prefixed with the request's token, so anything project code prints cannot be read as an answer.
10
10
  */
11
11
 
12
+ import { isRecord } from "./internal/record.ts"
12
13
  import { detectMonorepo } from "./load.ts"
13
14
  import { projectResources } from "./mcp-context.ts"
14
15
  import { createCachedAppLoader, projectTools } from "./mcp-exec.ts"
15
- import { CHILD_INPUT_MAX_BYTES, readBoundedStream } from "./mcp-io.ts"
16
+ import { CHILD_INPUT_MAX_BYTES, protocolStdout, readBoundedStream } from "./mcp-io.ts"
16
17
  import type {
17
18
  McpPrompt,
18
19
  McpPromptMessage,
@@ -109,9 +110,6 @@ export async function answerProjectRequest(
109
110
  }
110
111
  }
111
112
 
112
- const isArgs = (value: unknown): value is Record<string, unknown> =>
113
- typeof value === "object" && value !== null && !Array.isArray(value)
114
-
115
113
  function parseRequest(text: string): { token: string; request: ProjectChildRequest } | undefined {
116
114
  let value: unknown
117
115
  try {
@@ -119,12 +117,12 @@ function parseRequest(text: string): { token: string; request: ProjectChildReque
119
117
  } catch {
120
118
  return undefined
121
119
  }
122
- if (!isArgs(value)) return undefined
120
+ if (!isRecord(value)) return undefined
123
121
  const { token, op, name, uri, args } = value
124
122
  if (typeof token !== "string" || !/^[0-9a-f-]{16,64}$/.test(token)) return undefined
125
123
  if (op === "monorepo" || op === "surface") return { token, request: { op } }
126
124
  if (op === "resource" && typeof uri === "string") return { token, request: { op, uri } }
127
- if ((op === "tool" || op === "prompt") && typeof name === "string" && isArgs(args)) {
125
+ if ((op === "tool" || op === "prompt") && typeof name === "string" && isRecord(args)) {
128
126
  return { token, request: { op, name, args } }
129
127
  }
130
128
  return undefined
@@ -163,14 +161,7 @@ export async function serveProjectChild(
163
161
  }
164
162
 
165
163
  if (import.meta.main) {
166
- // Bound before project code loads, so nothing it patches can reach the protocol channel.
167
- const stdout = process.stdout.write.bind(process.stdout)
168
- const toStderr = (...args: unknown[]): void => {
169
- process.stderr.write(`${args.map((arg) => String(arg)).join(" ")}\n`)
170
- }
171
- console.log = toStderr
172
- console.info = toStderr
173
- console.debug = toStderr
164
+ const stdout = protocolStdout()
174
165
  const served = await serveProjectChild(
175
166
  process.argv[2] ?? process.cwd(),
176
167
  Bun.stdin.stream(),
package/src/mcp-render.ts CHANGED
@@ -17,9 +17,11 @@ import { loadApp, resolvePlugins } from "./load.ts"
17
17
  import {
18
18
  CHILD_INPUT_MAX_BYTES,
19
19
  CHILD_OUTPUT_MAX_BYTES,
20
- readBoundedLines,
21
20
  readBoundedStream,
21
+ redirectConsoleToStderr,
22
22
  serializeBoundedJson,
23
+ workerReplyWriter,
24
+ workerRequests,
23
25
  } from "./mcp-io.ts"
24
26
  import { frameworkWebAppOptions } from "./web-app-options.ts"
25
27
 
@@ -78,30 +80,6 @@ export async function renderPages(
78
80
  }
79
81
  }
80
82
 
81
- interface RenderWorkerMessage {
82
- readonly id?: unknown
83
- readonly input?: { readonly requests?: unknown }
84
- }
85
-
86
- /** Send console output to stderr so it never corrupts the newline-delimited JSON-RPC on stdout (mirrors
87
- * `mcp-run`'s worker). A loader that logs would otherwise inject noise into a response line. */
88
- function redirectConsoleToStderr(): void {
89
- const write = (level: string, args: unknown[]): void => {
90
- Bun.stderr.write(`[${level}] ${args.map((arg) => String(arg)).join(" ")}\n`)
91
- }
92
- console.debug = (...args: unknown[]) => write("debug", args)
93
- console.info = (...args: unknown[]) => write("info", args)
94
- console.log = (...args: unknown[]) => write("log", args)
95
- console.warn = (...args: unknown[]) => write("warn", args)
96
- console.error = (...args: unknown[]) => write("error", args)
97
- }
98
-
99
- function safeResponseId(value: unknown): number | string | null {
100
- if (typeof value === "number" && Number.isSafeInteger(value)) return value
101
- if (typeof value === "string" && value.length <= 128) return value
102
- return null
103
- }
104
-
105
83
  /**
106
84
  * Warm-worker loop (`--worker`): build the web app ONCE, then SSR each `{ id, input: { requests } }` line
107
85
  * against that hot app and reply `{ id, output }`. The parent (`mcp.ts`'s warm-render handler) spawns this,
@@ -113,51 +91,25 @@ async function runWorker(cwd: string): Promise<void> {
113
91
  // Build once; the same `{ error }` is returned for every request if the build failed, so an agent sees
114
92
  // the actionable message and the parent restarts the worker on the next file change.
115
93
  const built = await buildWebApp(cwd)
116
- const send = (id: unknown, output: unknown): void => {
117
- const responseId = safeResponseId(id)
118
- const encoded = serializeBoundedJson({ id: responseId, output }, CHILD_OUTPUT_MAX_BYTES - 1)
119
- process.stdout.write(
120
- `${encoded ?? JSON.stringify({ id: responseId, output: { error: "worker output exceeded the size limit" } })}\n`,
121
- )
122
- }
123
- for await (const item of readBoundedLines(Bun.stdin.stream(), CHILD_INPUT_MAX_BYTES)) {
124
- if (item.kind === "too-large") {
125
- send(null, { error: `worker input exceeded ${CHILD_INPUT_MAX_BYTES} bytes` })
126
- continue
127
- }
128
- const line = item.text.trim()
129
- if (line === "") continue
130
-
131
- let message: unknown
132
- try {
133
- message = JSON.parse(line) as unknown
134
- } catch {
135
- send(null, { error: "invalid worker input: expected JSON line" })
136
- continue
137
- }
138
- const record =
139
- message !== null && typeof message === "object" && !Array.isArray(message)
140
- ? (message as RenderWorkerMessage)
141
- : undefined
142
- const input =
143
- record?.input !== null && typeof record?.input === "object" && !Array.isArray(record.input)
144
- ? record.input
145
- : undefined
94
+ const send = workerReplyWriter((line) => {
95
+ process.stdout.write(line)
96
+ })
97
+ for await (const { id, input } of workerRequests(send)) {
146
98
  const requests = input?.requests
147
99
  if (!Array.isArray(requests)) {
148
- send(record?.id, { error: "expected { requests: [...] }" })
100
+ send(id, { error: "expected { requests: [...] }" })
149
101
  continue
150
102
  }
151
103
  if ("error" in built) {
152
- send(record?.id, built)
104
+ send(id, built)
153
105
  continue
154
106
  }
155
107
  try {
156
- send(record?.id, {
108
+ send(id, {
157
109
  results: await runApp(built.app, requests as Parameters<typeof runApp>[1]),
158
110
  })
159
111
  } catch (err) {
160
- send(record?.id, { error: errString(err) })
112
+ send(id, { error: errString(err) })
161
113
  }
162
114
  }
163
115
  }
package/src/mcp-root.ts CHANGED
@@ -117,7 +117,7 @@ async function workspaceMatches(
117
117
  ): Promise<string[]> {
118
118
  const segments = pattern
119
119
  .replace(/^\.\//, "")
120
- .replace(/\/+$/, "")
120
+ .replace(/(?<!\/)\/+$/, "")
121
121
  .split("/")
122
122
  .filter((segment) => segment !== "" && segment !== ".")
123
123
  const firstGlob = segments.findIndex((segment) => /[*?[{]/.test(segment))
package/src/mcp-run.ts CHANGED
@@ -21,9 +21,11 @@ import { BACKEND_APP_FILE } from "./app-files.ts"
21
21
  import {
22
22
  CHILD_INPUT_MAX_BYTES,
23
23
  CHILD_OUTPUT_MAX_BYTES,
24
- readBoundedLines,
25
24
  readBoundedStream,
25
+ redirectConsoleToStderr,
26
26
  serializeBoundedJson,
27
+ workerReplyWriter,
28
+ workerRequests,
27
29
  } from "./mcp-io.ts"
28
30
 
29
31
  const ENTRY_CANDIDATES = [BACKEND_APP_FILE, "app.ts", "src/app.ts"]
@@ -163,28 +165,6 @@ function withCapture(feed: DevFeed, requestId: string, result: RunResult): RunRe
163
165
  return { ...result, ...capture }
164
166
  }
165
167
 
166
- interface WorkerMessage {
167
- readonly id?: unknown
168
- readonly input?: { readonly requests?: unknown; readonly entry?: string }
169
- }
170
-
171
- function redirectConsoleToStderr(): void {
172
- const write = (level: string, args: unknown[]): void => {
173
- Bun.stderr.write(`[${level}] ${args.map((arg) => String(arg)).join(" ")}\n`)
174
- }
175
- console.debug = (...args: unknown[]) => write("debug", args)
176
- console.info = (...args: unknown[]) => write("info", args)
177
- console.log = (...args: unknown[]) => write("log", args)
178
- console.warn = (...args: unknown[]) => write("warn", args)
179
- console.error = (...args: unknown[]) => write("error", args)
180
- }
181
-
182
- function safeResponseId(value: unknown): number | string | null {
183
- if (typeof value === "number" && Number.isSafeInteger(value)) return value
184
- if (typeof value === "string" && value.length <= 128) return value
185
- return null
186
- }
187
-
188
168
  /**
189
169
  * Keep stdout for the protocol. The app's console and any direct `process.stdout.write` go to stderr,
190
170
  * or a stray `console.log` in a handler would corrupt the JSON the parent reads. Returns the writer
@@ -206,37 +186,8 @@ function reserveStdout(): (text: string) => void {
206
186
 
207
187
  async function runWorker(cwd: string, protocol: (text: string) => void): Promise<void> {
208
188
  const apps = new Map<string, Promise<{ app: AppLike } | { error: string }>>()
209
- const send = (id: unknown, output: unknown): void => {
210
- const responseId = safeResponseId(id)
211
- const encoded = serializeBoundedJson({ id: responseId, output }, CHILD_OUTPUT_MAX_BYTES - 1)
212
- protocol(
213
- `${encoded ?? JSON.stringify({ id: responseId, output: { error: "worker output exceeded the size limit" } })}\n`,
214
- )
215
- }
216
- for await (const item of readBoundedLines(Bun.stdin.stream(), CHILD_INPUT_MAX_BYTES)) {
217
- if (item.kind === "too-large") {
218
- send(null, { error: `worker input exceeded ${CHILD_INPUT_MAX_BYTES} bytes` })
219
- continue
220
- }
221
- const line = item.text.trim()
222
- if (line === "") continue
223
-
224
- let message: unknown
225
- try {
226
- message = JSON.parse(line) as unknown
227
- } catch {
228
- send(null, { error: "invalid worker input: expected JSON line" })
229
- continue
230
- }
231
- const record =
232
- message !== null && typeof message === "object" && !Array.isArray(message)
233
- ? (message as WorkerMessage)
234
- : undefined
235
- const input =
236
- record?.input !== null && typeof record?.input === "object" && !Array.isArray(record.input)
237
- ? record.input
238
- : undefined
239
- const id = record?.id
189
+ const send = workerReplyWriter(protocol)
190
+ for await (const { id, input } of workerRequests(send)) {
240
191
  const requests = input?.requests
241
192
  const entry = input?.entry
242
193
  if (!Array.isArray(requests)) {
package/src/mcp-ws.ts CHANGED
@@ -6,6 +6,8 @@
6
6
  * general-purpose network client.
7
7
  */
8
8
 
9
+ import { hasControlCharacter } from "./internal/control-character.ts"
10
+ import { redirectConsoleToStderr } from "./mcp-io.ts"
9
11
  import { loadBackend } from "./mcp-run.ts"
10
12
 
11
13
  const DEFAULT_TIMEOUT_MS = 3_000
@@ -53,14 +55,6 @@ export interface WebSocketVerificationResult {
53
55
  const errString = (err: unknown): string =>
54
56
  err instanceof Error ? `${err.name}: ${err.message}` : String(err)
55
57
 
56
- function hasControlCharacter(value: string): boolean {
57
- for (let i = 0; i < value.length; i++) {
58
- const code = value.charCodeAt(i)
59
- if (code < 0x20 || code === 0x7f) return true
60
- }
61
- return false
62
- }
63
-
64
58
  function fail(
65
59
  path: string,
66
60
  sent: readonly string[],
@@ -171,17 +165,6 @@ function closeInfo(event: CloseEvent): CloseInfo {
171
165
  return { code: event.code, reason: event.reason, wasClean: event.wasClean }
172
166
  }
173
167
 
174
- function redirectConsoleToStderr(): void {
175
- const write = (level: string, args: unknown[]): void => {
176
- Bun.stderr.write(`[${level}] ${args.map((arg) => String(arg)).join(" ")}\n`)
177
- }
178
- console.debug = (...args: unknown[]) => write("debug", args)
179
- console.info = (...args: unknown[]) => write("info", args)
180
- console.log = (...args: unknown[]) => write("log", args)
181
- console.warn = (...args: unknown[]) => write("warn", args)
182
- console.error = (...args: unknown[]) => write("error", args)
183
- }
184
-
185
168
  export async function runWebSocket(
186
169
  cwd: string,
187
170
  args: unknown,