@nifrajs/cli 2.2.0 → 2.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 (117) hide show
  1. package/README.md +25 -25
  2. package/dist/assure.d.ts +2 -2
  3. package/dist/assure.d.ts.map +1 -1
  4. package/dist/assure.js +17 -10
  5. package/dist/assure.js.map +1 -1
  6. package/dist/capabilities-tool.d.ts +7 -0
  7. package/dist/capabilities-tool.d.ts.map +1 -1
  8. package/dist/capabilities-tool.js +68 -16
  9. package/dist/capabilities-tool.js.map +1 -1
  10. package/dist/check.d.ts +61 -22
  11. package/dist/check.d.ts.map +1 -1
  12. package/dist/check.js +332 -86
  13. package/dist/check.js.map +1 -1
  14. package/dist/cli.d.ts +30 -2
  15. package/dist/cli.d.ts.map +1 -1
  16. package/dist/cli.js +132 -36
  17. package/dist/cli.js.map +1 -1
  18. package/dist/diff-tool.d.ts +3 -3
  19. package/dist/diff-tool.js +7 -7
  20. package/dist/docs-search.d.ts +3 -3
  21. package/dist/docs-search.js +6 -6
  22. package/dist/doctor.d.ts +3 -3
  23. package/dist/doctor.js +8 -8
  24. package/dist/examples.d.ts +5 -5
  25. package/dist/examples.js +5 -5
  26. package/dist/init-agents.d.ts +14 -14
  27. package/dist/init-agents.js +14 -14
  28. package/dist/internal/typescript-import.d.ts +24 -0
  29. package/dist/internal/typescript-import.d.ts.map +1 -0
  30. package/dist/internal/typescript-import.js +32 -0
  31. package/dist/internal/typescript-import.js.map +1 -0
  32. package/dist/introspect.d.ts +12 -12
  33. package/dist/introspect.js +24 -24
  34. package/dist/levels-tool.d.ts +8 -8
  35. package/dist/levels-tool.d.ts.map +1 -1
  36. package/dist/levels-tool.js +33 -30
  37. package/dist/levels-tool.js.map +1 -1
  38. package/dist/load.d.ts +6 -6
  39. package/dist/load.d.ts.map +1 -1
  40. package/dist/load.js +15 -7
  41. package/dist/load.js.map +1 -1
  42. package/dist/manifest-tool.d.ts +1 -1
  43. package/dist/manifest-tool.d.ts.map +1 -1
  44. package/dist/manifest-tool.js +4 -2
  45. package/dist/manifest-tool.js.map +1 -1
  46. package/dist/mcp-docs-tools.d.ts +1 -1
  47. package/dist/mcp-docs-tools.js +9 -9
  48. package/dist/mcp-http.d.ts +5 -5
  49. package/dist/mcp-http.js +7 -7
  50. package/dist/mcp-protocol.d.ts +2 -2
  51. package/dist/mcp-protocol.js +2 -2
  52. package/dist/mcp-render.d.ts +3 -3
  53. package/dist/mcp-render.js +6 -6
  54. package/dist/mcp-run.d.ts +2 -2
  55. package/dist/mcp-run.js +2 -2
  56. package/dist/mcp.d.ts +14 -14
  57. package/dist/mcp.d.ts.map +1 -1
  58. package/dist/mcp.js +75 -45
  59. package/dist/mcp.js.map +1 -1
  60. package/dist/openapi-tool.d.ts +1 -1
  61. package/dist/openapi-tool.js +2 -2
  62. package/dist/pipeline-guard.d.ts +1 -1
  63. package/dist/pipeline-guard.js +1 -1
  64. package/dist/port.d.ts +9 -9
  65. package/dist/port.js +27 -27
  66. package/dist/recipes/1.8.0.d.ts +2 -2
  67. package/dist/recipes/1.8.0.js +5 -5
  68. package/dist/recipes/index.d.ts +2 -2
  69. package/dist/recipes/index.js +1 -1
  70. package/dist/scaffold.d.ts +3 -3
  71. package/dist/scaffold.js +8 -8
  72. package/dist/sync-routes.d.ts +28 -0
  73. package/dist/sync-routes.d.ts.map +1 -0
  74. package/dist/sync-routes.js +55 -0
  75. package/dist/sync-routes.js.map +1 -0
  76. package/dist/types-search.d.ts +5 -5
  77. package/dist/types-search.js +7 -7
  78. package/dist/upgrade.d.ts +7 -7
  79. package/dist/upgrade.js +12 -12
  80. package/dist/verification.d.ts +51 -0
  81. package/dist/verification.d.ts.map +1 -0
  82. package/dist/verification.js +80 -0
  83. package/dist/verification.js.map +1 -0
  84. package/docs/examples.json +174 -90
  85. package/docs/llms-full.txt +1696 -747
  86. package/docs/types.json +1892 -3105
  87. package/package.json +22 -11
  88. package/src/assure.ts +17 -18
  89. package/src/capabilities-tool.ts +82 -16
  90. package/src/check.ts +405 -103
  91. package/src/cli.ts +141 -38
  92. package/src/diff-tool.ts +8 -8
  93. package/src/docs-search.ts +6 -6
  94. package/src/doctor.ts +10 -10
  95. package/src/examples.ts +7 -7
  96. package/src/init-agents.ts +17 -17
  97. package/src/internal/typescript-import.ts +33 -0
  98. package/src/introspect.ts +28 -28
  99. package/src/levels-tool.ts +37 -31
  100. package/src/load.ts +21 -12
  101. package/src/manifest-tool.ts +4 -2
  102. package/src/mcp-docs-tools.ts +9 -9
  103. package/src/mcp-http.ts +7 -7
  104. package/src/mcp-protocol.ts +2 -2
  105. package/src/mcp-render.ts +6 -6
  106. package/src/mcp-run.ts +2 -2
  107. package/src/mcp.ts +82 -45
  108. package/src/openapi-tool.ts +2 -2
  109. package/src/pipeline-guard.ts +1 -1
  110. package/src/port.ts +31 -31
  111. package/src/recipes/1.8.0.ts +5 -5
  112. package/src/recipes/index.ts +2 -2
  113. package/src/scaffold.ts +8 -8
  114. package/src/sync-routes.ts +69 -0
  115. package/src/types-search.ts +8 -8
  116. package/src/upgrade.ts +12 -12
  117. package/src/verification.ts +123 -0
@@ -1,18 +1,18 @@
1
1
  /**
2
- * `nifra init-agents` — retrofit an EXISTING app with the agent-discovery files a freshly scaffolded
2
+ * `nifra init-agents` - retrofit an EXISTING app with the agent-discovery files a freshly scaffolded
3
3
  * app ships, so an already-built project adopts the nifra MCP in one command:
4
4
  *
5
- * .mcp.json — Claude Code's project MCP registry (launches `bunx @nifrajs/cli mcp`)
6
- * .cursor/mcp.json — Cursor's MCP registry (same server config)
7
- * CLAUDE.md — Claude's MCP-first preamble + `@AGENTS.md` import
8
- * AGENTS.md — a `## MCP server` section appended (or a minimal file if none exists)
5
+ * .mcp.json - Claude Code's project MCP registry (launches `bunx @nifrajs/cli mcp`)
6
+ * .cursor/mcp.json - Cursor's MCP registry (same server config)
7
+ * CLAUDE.md - Claude's MCP-first preamble + `@AGENTS.md` import
8
+ * AGENTS.md - a `## MCP server` section appended (or a minimal file if none exists)
9
9
  *
10
- * The generators are imported from `create-nifra/agent-files` — the SAME source of truth `create-nifra`
11
- * uses at scaffold time — so a retrofitted app and a freshly scaffolded one get byte-identical configs.
10
+ * The generators are imported from `create-nifra/agent-files` - the SAME source of truth `create-nifra`
11
+ * uses at scaffold time - so a retrofitted app and a freshly scaffolded one get byte-identical configs.
12
12
  *
13
13
  * Safety: this writes into the user's existing tree, so it NEVER silently clobbers a file they may have
14
14
  * customized. By default an existing `.mcp.json` / `CLAUDE.md` / `.cursor/mcp.json` is SKIPPED with a
15
- * notice; `--force` overwrites. `AGENTS.md` is special-cased — if it already has the MCP section it's
15
+ * notice; `--force` overwrites. `AGENTS.md` is special-cased - if it already has the MCP section it's
16
16
  * left alone, otherwise the section is APPENDED (never overwriting the user's conventions), and `--force`
17
17
  * is not needed for that append since it's additive. Every write path is resolved + confined under the
18
18
  * cwd (no `..` traversal escaping the project root).
@@ -34,10 +34,10 @@ import {
34
34
  export interface InitAgentsFileResult {
35
35
  /** Project-root-relative POSIX path. */
36
36
  readonly path: string
37
- /** `wrote` — created or (with --force) overwrote; `appended` — added the MCP section to an existing
38
- * AGENTS.md; `skipped` — already present and not forced; `present` — MCP section already there. */
37
+ /** `wrote` - created or (with --force) overwrote; `appended` - added the MCP section to an existing
38
+ * AGENTS.md; `skipped` - already present and not forced; `present` - MCP section already there. */
39
39
  readonly action: "wrote" | "appended" | "skipped" | "present"
40
- /** Why it was skipped/left, for the notice (e.g. "exists — pass --force to overwrite"). */
40
+ /** Why it was skipped/left, for the notice (e.g. "exists - pass --force to overwrite"). */
41
41
  readonly note?: string
42
42
  }
43
43
 
@@ -52,7 +52,7 @@ export interface InitAgentsOptions {
52
52
  }
53
53
 
54
54
  /** Confine a project-relative path under `cwd` and return the absolute path. Rejects a spec that escapes
55
- * the root (defense-in-depth — these specs are constants, but the cwd-confinement invariant is enforced
55
+ * the root (defense-in-depth - these specs are constants, but the cwd-confinement invariant is enforced
56
56
  * at the seam, not assumed). Exported so the invariant is directly testable. */
57
57
  export function safeJoin(cwd: string, rel: string): string {
58
58
  const abs = resolve(cwd, rel)
@@ -80,7 +80,7 @@ async function writeOwned(
80
80
  ): Promise<InitAgentsFileResult> {
81
81
  const abs = safeJoin(cwd, rel)
82
82
  if (!force && (await fileExists(abs))) {
83
- return { path: rel, action: "skipped", note: "exists — pass --force to overwrite" }
83
+ return { path: rel, action: "skipped", note: "exists - pass --force to overwrite" }
84
84
  }
85
85
  // `.cursor/mcp.json` needs its parent dir; `recursive` is a no-op for the root-level files.
86
86
  await mkdir(resolve(abs, ".."), { recursive: true })
@@ -91,7 +91,7 @@ async function writeOwned(
91
91
  /**
92
92
  * AGENTS.md is additive, not owned: if it exists and already has the MCP section, leave it; if it exists
93
93
  * without the section, append the section (preserving the user's conventions); if it's absent, write a
94
- * minimal AGENTS.md that is just the MCP section under a heading. `--force` is irrelevant here — we never
94
+ * minimal AGENTS.md that is just the MCP section under a heading. `--force` is irrelevant here - we never
95
95
  * overwrite the user's existing guidance.
96
96
  */
97
97
  async function ensureAgentsMd(cwd: string): Promise<InitAgentsFileResult> {
@@ -121,7 +121,7 @@ async function ensureAgentsMd(cwd: string): Promise<InitAgentsFileResult> {
121
121
 
122
122
  /**
123
123
  * Retrofit `cwd` with the four agent-discovery files. Pure enough to unit-test (no argv, no process.exit,
124
- * no console) — the CLI wrapper handles printing + the exit code.
124
+ * no console) - the CLI wrapper handles printing + the exit code.
125
125
  */
126
126
  export async function initAgents(
127
127
  cwd: string,
@@ -153,13 +153,13 @@ export function renderInitAgents(result: InitAgentsResult): string {
153
153
  const wroteAny = result.files.some((f) => f.action === "wrote" || f.action === "appended")
154
154
  const footer = wroteAny
155
155
  ? "\nThe nifra MCP is now registered. Restart your agent so it picks up .mcp.json, then prefer nifra_docs / nifra_example and gate on nifra check."
156
- : "\nNothing to do — every file was already present (use --force to overwrite the owned ones)."
156
+ : "\nNothing to do - every file was already present (use --force to overwrite the owned ones)."
157
157
  return `nifra init-agents\n\n${lines.join("\n")}\n${footer}`
158
158
  }
159
159
 
160
160
  /**
161
161
  * CLI entry: run the retrofit at `cwd` and print the result. Returns `true` (the command always succeeds
162
- * unless a write throws — which propagates as a non-zero exit via the dispatcher's catch). `--json`
162
+ * unless a write throws - which propagates as a non-zero exit via the dispatcher's catch). `--json`
163
163
  * emits the structured result for agents/CI.
164
164
  */
165
165
  export async function runInitAgents(
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The one place the CLI imports TypeScript from.
3
+ *
4
+ * The interpolated-SQL rule parses source with the TypeScript compiler, because the regex it replaced
5
+ * could not tell `sql\`… ${id} …\`` (bound) from a plain template (injectable) without guessing at
6
+ * string and comment boundaries. That is the right tool, but it is a ~25 MB dependency, and the CLI's
7
+ * own typecheck step already treats `tsc` as something the PROJECT provides rather than something the
8
+ * CLI ships. Making it a hard dependency would have contradicted that for every install.
9
+ *
10
+ * So it is an optional peer, resolved at the moment the rule runs. Every Nifra project has TypeScript
11
+ * - the templates all ship a `typecheck` script - so this resolves in practice; the point is that an
12
+ * install does not pay for it.
13
+ */
14
+
15
+ /** The slice of the compiler the SQL scanner uses. Structural, so the peer stays optional. */
16
+ export type TypeScriptApi = typeof import("typescript")
17
+
18
+ /**
19
+ * Import the project's TypeScript, or `undefined` when it is not installed.
20
+ *
21
+ * Only a RESOLUTION failure is absence. A compiler that resolves and then fails while evaluating is a
22
+ * broken install, not a missing one, and telling that user to install what they already have sends
23
+ * them the wrong way - so that error is rethrown for the caller to report.
24
+ */
25
+ export async function importTypeScript(): Promise<TypeScriptApi | undefined> {
26
+ try {
27
+ return (await import("typescript")) as TypeScriptApi
28
+ } catch (cause) {
29
+ const message = cause instanceof Error ? cause.message : String(cause)
30
+ if (/cannot find (?:package|module)|ERR_MODULE_NOT_FOUND/i.test(message)) return undefined
31
+ throw cause
32
+ }
33
+ }
package/src/introspect.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * Introspect a loaded nifra project into a Markdown brief — the API routes (`backend.routes()`), the
3
- * page routes (`discoverRoutes`), and a conventions summary — for piping into an AI coding agent's
2
+ * Introspect a loaded nifra project into a Markdown brief - the API routes (`backend.routes()`), the
3
+ * page routes (`discoverRoutes`), and a conventions summary - for piping into an AI coding agent's
4
4
  * prompt. Used by `nifra context` (stdout) and the `nifra mcp` server (a tool result), so an agent sees
5
5
  * *this* project's actual surface, not just generic docs.
6
6
  */
@@ -28,7 +28,7 @@ interface JsonSchemaNode {
28
28
  }
29
29
 
30
30
  /**
31
- * Render a JSON Schema as a compact TypeScript-like type (`{ id: string, name?: string }`) — the
31
+ * Render a JSON Schema as a compact TypeScript-like type (`{ id: string, name?: string }`) - the
32
32
  * shape agents already think in, at a fraction of the raw JSON Schema's tokens (~70-80% smaller).
33
33
  * Anything the renderer doesn't model falls back to the raw JSON for that subtree, so the output
34
34
  * is always faithful, just not always minimal.
@@ -74,7 +74,7 @@ export function tsTypeOf(schema: unknown, depth = 0): string {
74
74
  }
75
75
 
76
76
  /** One route's request/response field shapes, indented under its line. Reads the declared schemas off the
77
- * descriptor so an agent gets the exact input + output contract — not just "this route is validated". */
77
+ * descriptor so an agent gets the exact input + output contract - not just "this route is validated". */
78
78
  function schemaLines(schema: ReflectedRoute["schema"]): string[] {
79
79
  const s = schema
80
80
  if (!s) return []
@@ -95,11 +95,11 @@ function schemaLines(schema: ReflectedRoute["schema"]): string[] {
95
95
  const IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/
96
96
 
97
97
  /**
98
- * The typed-client call form for a route — the exact `client<typeof app>` proxy chain an agent should
98
+ * The typed-client call form for a route - the exact `client<typeof app>` proxy chain an agent should
99
99
  * write, derived from the same convention `@nifrajs/client` implements (so it never has to read the
100
100
  * client tests to learn it): a static segment is a property (`.users`), a path param/wildcard is a call
101
101
  * that appends the value (`({ id })`), the root path is `.index`, and the HTTP verb is the terminal call.
102
- * Body verbs (POST/PUT/PATCH) take the body first then call-options; other verbs take call-options first —
102
+ * Body verbs (POST/PUT/PATCH) take the body first then call-options; other verbs take call-options first -
103
103
  * so the `{ query }` argument lands in the right slot for each.
104
104
  */
105
105
  export function clientCall(method: string, path: string, schema: unknown): string {
@@ -140,10 +140,10 @@ export function apiRoutesSection(routes: readonly ReflectedRoute[]): string {
140
140
  ...schemaLines(r.schema),
141
141
  ` - call: \`${clientCall(r.method, r.path, r.schema)}\` → \`{ ok, status, data, error }\``,
142
142
  ])
143
- return `## API routes (backend.ts)\n\nEach route's \`body\`/\`query\`/\`response\` shape is its contract — the typed client derives request inputs and \`res.data\` from these, so a screen built on \`client<typeof app>\` stays in sync automatically. The \`call\` line is the exact \`client<typeof app>\` form: static path segments are properties, a path param is a call (\`({ id })\`), the verb is the terminal call (body first for POST/PUT/PATCH), and every call returns the never-throwing \`{ ok, status, data, error }\` Result.\n\n${lines.join("\n")}`
143
+ return `## API routes (backend.ts)\n\nEach route's \`body\`/\`query\`/\`response\` shape is its contract - the typed client derives request inputs and \`res.data\` from these, so a screen built on \`client<typeof app>\` stays in sync automatically. The \`call\` line is the exact \`client<typeof app>\` form: static path segments are properties, a path param is a call (\`({ id })\`), the verb is the terminal call (body first for POST/PUT/PATCH), and every call returns the never-throwing \`{ ok, status, data, error }\` Result.\n\n${lines.join("\n")}`
144
144
  }
145
145
 
146
- /** Compact API-routes INDEX for the no-arg `nifra_context` call — `METHOD path` per route, WITHOUT the
146
+ /** Compact API-routes INDEX for the no-arg `nifra_context` call - `METHOD path` per route, WITHOUT the
147
147
  * body/query/response shapes or the per-route `call` line. Bounds the first-call payload on a big backend:
148
148
  * an agent gets the full route list (so it knows what's mounted) plus a pointer to fetch any route's full
149
149
  * contract via the `path`/`kind` slice, instead of every schema up front. */
@@ -154,7 +154,7 @@ export function apiRoutesIndexSection(routes: readonly ReflectedRoute[]): string
154
154
  const lines = [...routes]
155
155
  .sort((a, b) => a.path.localeCompare(b.path) || a.method.localeCompare(b.method))
156
156
  .map((r) => `- \`${r.method} ${r.path}\``)
157
- return `## API routes (backend.ts)\n\n${routes.length} route${routes.length === 1 ? "" : "s"}. Call \`nifra_context\` again with \`path\` (a route prefix) and/or \`kind: "api"\` for the body/query/response contracts + the exact \`client<typeof app>\` call form — or \`nifra_routes\` for the same as structured JSON.\n\n${lines.join("\n")}`
157
+ return `## API routes (backend.ts)\n\n${routes.length} route${routes.length === 1 ? "" : "s"}. Call \`nifra_context\` again with \`path\` (a route prefix) and/or \`kind: "api"\` for the body/query/response contracts + the exact \`client<typeof app>\` call form - or \`nifra_routes\` for the same as structured JSON.\n\n${lines.join("\n")}`
158
158
  }
159
159
 
160
160
  /** Markdown section listing the file-routed pages (URL pattern → source file). */
@@ -172,25 +172,25 @@ const CONVENTIONS = `## Conventions (summary)
172
172
 
173
173
  - **Backend:** \`server()\` from \`@nifrajs/core\`; returning a value sends JSON. Validate untrusted input with
174
174
  a route schema (\`{ body, query, params }\` using \`t\` from \`@nifrajs/schema\` or any Standard Schema) and read
175
- the typed \`c.body\` / \`c.query\` / \`c.params\` — never hand-parse.
175
+ the typed \`c.body\` / \`c.query\` / \`c.params\` - never hand-parse.
176
176
  - **Cross-cutting concerns** (rate limit / \`429\`, CORS, auth, CSRF, caching, security headers, body limits)
177
177
  → \`@nifrajs/middleware\`, applied with \`app.use(...)\`. Don't hand-roll them; call \`nifra_docs("middleware")\`
178
178
  for the full list + usage.
179
179
  - **Need the exact TypeScript of a \`@nifrajs/*\` symbol?** Call \`nifra_types({ name })\` (e.g. \`RateLimitStore\`,
180
- \`RouteSchema\`) — the authoritative, complete declaration. Never read \`@nifrajs\` \`.d.ts\` files.
180
+ \`RouteSchema\`) - the authoritative, complete declaration. Never read \`@nifrajs\` \`.d.ts\` files.
181
181
  - **Response contract (no drift):** declare \`{ response: t.object({...}) }\` on a route to lock its output
182
- shape — the handler is type-checked against it and the client sees exactly that shape. The contract above
182
+ shape - the handler is type-checked against it and the client sees exactly that shape. The contract above
183
183
  is the single source of truth for both sides.
184
- - **Client — ALWAYS use this for API calls:** \`client<typeof app>(url)\` derives request inputs AND
184
+ - **Client - ALWAYS use this for API calls:** \`client<typeof app>(url)\` derives request inputs AND
185
185
  \`res.data\` from the backend's route types, so the compiler catches any frontend/backend drift. Never
186
- hand-roll \`fetch\` + ad-hoc response types for an internal API — that's exactly how screens drift. It
186
+ hand-roll \`fetch\` + ad-hoc response types for an internal API - that's exactly how screens drift. It
187
187
  never throws: branch on \`res.ok ? res.data : res.error\`.
188
188
  - **Pages:** file-routed under \`routes/\`; \`loader\`/\`action\` are server-only but the module is also bundled
189
- for the browser — **never top-level-import server-only code** (DB, secrets, \`process.env\`) into a route
189
+ for the browser - **never top-level-import server-only code** (DB, secrets, \`process.env\`) into a route
190
190
  file; reach it via \`ctx.api\` / \`ctx.env\`.
191
191
  - \`app.fetch(Request)\` is the universal entry. Full reference: this repo's \`AGENTS.md\`, or \`llms-full.txt\`.`
192
192
 
193
- /** Optional narrowing for {@link describeProject} — a path prefix and/or one section. A filtered
193
+ /** Optional narrowing for {@link describeProject} - a path prefix and/or one section. A filtered
194
194
  * brief omits the conventions block (the agent already has it from its first full call), so
195
195
  * follow-up calls cost a fraction of the tokens. */
196
196
  export interface ContextFilter {
@@ -199,9 +199,9 @@ export interface ContextFilter {
199
199
  }
200
200
 
201
201
  /**
202
- * Build the project brief as Markdown. A no-arg call returns a tight **INDEX** — the route list (API
202
+ * Build the project brief as Markdown. A no-arg call returns a tight **INDEX** - the route list (API
203
203
  * routes as `METHOD path`, page routes as `pattern → file`) + conventions + a pointer to fetch full
204
- * contracts via the slice — so learning a big project is cheap, not a full-surface schema dump. Passing a
204
+ * contracts via the slice - so learning a big project is cheap, not a full-surface schema dump. Passing a
205
205
  * {@link ContextFilter} (`path` prefix and/or `kind`) returns the narrowed, FULL detail (per-route
206
206
  * body/query/response shapes + the typed-client `call` form), and omits the conventions block (the agent
207
207
  * already has it from the index call).
@@ -211,7 +211,7 @@ export function describeProject(app: LoadedApp, filter?: ContextFilter): string
211
211
  try {
212
212
  manifest = discoverRoutes(app.routesDir)
213
213
  } catch {
214
- // No (or unreadable) routes/ — the page-routes section reports that.
214
+ // No (or unreadable) routes/ - the page-routes section reports that.
215
215
  }
216
216
  const prefix = filter?.path
217
217
  let routes = backendRoutes(app.backend)
@@ -228,10 +228,10 @@ export function describeProject(app: LoadedApp, filter?: ContextFilter): string
228
228
  const sections: string[] = [`# nifra project: ${app.cwd.slice(app.cwd.lastIndexOf("/") + 1)}`]
229
229
  if (isIndex) {
230
230
  sections.push(
231
- "A full-stack nifra app — Web-standard, one app on Bun, Node, Deno, and the edge. This is the route INDEX; fetch a route's full body/query/response contract on demand (see below).",
232
- // Point agents at the verified surface, not recalled APIs — nifra is young, so training data is the
231
+ "A full-stack nifra app - Web-standard, one app on Bun, Node, Deno, and the edge. This is the route INDEX; fetch a route's full body/query/response contract on demand (see below).",
232
+ // Point agents at the verified surface, not recalled APIs - nifra is young, so training data is the
233
233
  // wrong source. The MCP tools (nifra_docs / nifra_example) are checked against the installed version.
234
- "> **Build against THIS surface + the verified tools — do not write nifra APIs from memory** (the\n> framework is young; recalled APIs drift). For framework code, call `nifra_example` (snippets\n> typechecked against the installed version) or `nifra_docs`; verify edits with `nifra_run` + `nifra_check`.",
234
+ "> **Build against THIS surface + the verified tools - do not write nifra APIs from memory** (the\n> framework is young; recalled APIs drift). For framework code, call `nifra_example` (snippets\n> typechecked against the installed version) or `nifra_docs`; verify edits with `nifra_run` + `nifra_check`.",
235
235
  )
236
236
  }
237
237
  if (filter?.kind !== "pages") {
@@ -255,7 +255,7 @@ export interface RouteJson {
255
255
  }
256
256
 
257
257
  /**
258
- * The backend's API routes as structured JSON — what the `nifra_routes` MCP tool returns, so an agent
258
+ * The backend's API routes as structured JSON - what the `nifra_routes` MCP tool returns, so an agent
259
259
  * consumes the contract programmatically (`list_routes` / `get_route_schema`) instead of parsing the
260
260
  * Markdown brief. Optionally filtered to routes whose path starts with `pathPrefix`. Reuses the exact
261
261
  * same `call` form and TS-shaped contracts the Markdown brief shows.
@@ -292,11 +292,11 @@ export function routesToJson(app: LoadedApp, pathPrefix?: string): RouteJson[] {
292
292
  }
293
293
 
294
294
  // ===================================================================================================
295
- // `nifra routes [--json]` — the focused, uniform view of every route the app serves, with methods.
295
+ // `nifra routes [--json]` - the focused, uniform view of every route the app serves, with methods.
296
296
  //
297
297
  // `nifra context` lists page routes + API routes for an agent's prompt (Markdown, with schemas).
298
298
  // `nifra routes` answers a different question: "what does this app actually serve, and with which
299
- // methods?" — so an agent sees instantly that `POST /api/explain` is (or isn't) mounted, instead of
299
+ // methods?" - so an agent sees instantly that `POST /api/explain` is (or isn't) mounted, instead of
300
300
  // discovering it via a 405. It marks which backend routes are AUTO-MOUNTED under the page router's
301
301
  // `apiPrefix` (default `/api`), and `--json` makes it machine-consumable. The table-building +
302
302
  // rendering are pure (no fs / no module loads), so they're unit-tested; the cwd gathering is the thin
@@ -313,7 +313,7 @@ export interface RouteTableEntry {
313
313
  readonly path: string
314
314
  /** Methods this route serves, sorted (page: `GET`, plus `POST` when it has an `action`). */
315
315
  readonly methods: readonly string[]
316
- /** Source file (page only — the route module path relative to `routes/`). */
316
+ /** Source file (page only - the route module path relative to `routes/`). */
317
317
  readonly file?: string
318
318
  /** True when this API route is auto-mounted under the page router's `apiPrefix` (so it's reachable
319
319
  * through the same web app, no hand-wired `if (pathname.startsWith("/api/"))`). Pages are never
@@ -345,7 +345,7 @@ function isUnderPrefix(path: string, prefix: string): boolean {
345
345
  }
346
346
 
347
347
  /**
348
- * Build the unified, sorted route table from page routes + backend API routes. Pure — the caller
348
+ * Build the unified, sorted route table from page routes + backend API routes. Pure - the caller
349
349
  * supplies page `hasAction` (resolved via a module load) and the API descriptors (`backend.routes()`).
350
350
  * Page routes serve `GET` (+ `POST` when they export an `action`); API routes carry their declared
351
351
  * method, and are flagged `autoMounted` when at/under `apiPrefix`. Sorted by path, then kind, then the
@@ -449,7 +449,7 @@ export async function describeRoutes(
449
449
  try {
450
450
  manifest = discoverRoutes(app.routesDir)
451
451
  } catch {
452
- // No (or unreadable) routes/ — fall through with no pages; the table still shows API routes.
452
+ // No (or unreadable) routes/ - fall through with no pages; the table still shows API routes.
453
453
  }
454
454
  const pages: PageRouteInput[] = []
455
455
  for (const route of manifest?.routes ?? []) {
@@ -1,36 +1,34 @@
1
1
  /**
2
- * `nifra levels` — the verification ladder, computed from gates that already exist:
2
+ * `nifra levels` - the verification ladder, computed from gates that already exist:
3
3
  *
4
- * L0 typed contract — `nifra check` passes (compiler-enforced frontend↔backend contract)
5
- * L1 route assurance — `nifra.assurance.ts` present and every route satisfies its policy
6
- * L2 capability lockfile — capability/effect assurance passes and the lockfile is in sync
7
- * L3 route manifest — the emitted manifest hash-verifies and matches the current app
8
- * L4 invariant-tested — every route passes through an explicitly isolated executor
4
+ * L0 typed contract - `nifra check` passes (compiler-enforced frontend↔backend contract)
5
+ * L1 route assurance - `nifra.assurance.ts` present and every route satisfies its policy
6
+ * L2 capability lockfile - capability/effect assurance passes and the lockfile is in sync
7
+ * L3 route manifest - the emitted manifest hash-verifies and matches the current app
8
+ * L4 invariant-tested - every route passes through an explicitly isolated executor
9
9
  *
10
- * A level counts only when every level below it holds — the ladder is cumulative, so "L3" is one
10
+ * A level counts only when every level below it holds - the ladder is cumulative, so "L3" is one
11
11
  * honest word for a whole posture. Levels are computed, never self-declared.
12
12
  */
13
13
 
14
14
  import { existsSync } from "node:fs"
15
15
  import { resolve } from "node:path"
16
- import { evaluateRouteAssurance } from "@nifrajs/core/assurance"
17
- import { type evaluateCapabilityAssurance, snapshotCapabilities } from "@nifrajs/core/capabilities"
16
+ import type { AssuranceReport } from "@nifrajs/core/assurance"
17
+ import { snapshotCapabilities } from "@nifrajs/core/capabilities"
18
18
  import { buildNifraManifest, parseNifraManifest } from "@nifrajs/core/manifest"
19
19
  import { type ContractTestApp, runAdversarialContract } from "@nifrajs/testing"
20
- import { loadAssuranceConfig } from "./assure.ts"
21
20
  import {
22
- collectCapabilityProjectReport,
21
+ type CapabilityProjectReport,
23
22
  diffCapabilitySnapshots,
24
23
  parseCapabilityLockfile,
25
24
  } from "./capabilities-tool.ts"
26
- import { collectCheckResult } from "./check.ts"
27
25
  import { DEFAULT_MANIFEST_FILE } from "./manifest-tool.ts"
28
26
 
29
27
  export interface VerificationLevelStatus {
30
28
  readonly level: number
31
29
  readonly name: string
32
30
  readonly ok: boolean
33
- /** Why the level does not hold — a gate failure or a missing prerequisite. Empty when ok. */
31
+ /** Why the level does not hold - a gate failure or a missing prerequisite. Empty when ok. */
34
32
  readonly reasons: readonly string[]
35
33
  }
36
34
 
@@ -51,10 +49,18 @@ export async function collectVerificationLevels(
51
49
  cwd: string,
52
50
  options: CollectLevelsOptions = {},
53
51
  ): Promise<VerificationLevelsResult> {
52
+ // The ladder is the fullest view over the one project verification: L0 is the check view, L1/L2 read
53
+ // the route-assurance + capability evidence the same collector already computed (no second walk), and
54
+ // only the ladder-specific L3 (manifest) and L4 (invariants) are evaluated here.
55
+ const { collectProjectVerification } = await import("./verification.ts")
56
+ const verification = await collectProjectVerification(cwd, {
57
+ ...(options.config !== undefined ? { config: options.config } : {}),
58
+ maxDiagnostics: 5,
59
+ })
54
60
  const statuses: { level: number; name: string; ok: boolean; reasons: string[] }[] = []
55
61
 
56
- // L0 — typed contract (`nifra check`).
57
- const check = await collectCheckResult(cwd, { maxDiagnostics: 5 })
62
+ // L0 - typed contract (`nifra check`).
63
+ const check = await verification.check()
58
64
  statuses.push({
59
65
  level: 0,
60
66
  name: "typed contract",
@@ -63,11 +69,11 @@ export async function collectVerificationLevels(
63
69
  })
64
70
 
65
71
  // L1-L4 all hang off the assurance config; without it the ladder stops at L0 by definition.
66
- let config: Awaited<ReturnType<typeof loadAssuranceConfig>> | undefined
67
- try {
68
- config = await loadAssuranceConfig(cwd, options.config)
69
- } catch (err) {
70
- const reason = err instanceof Error ? err.message : String(err)
72
+ if (verification.config === undefined) {
73
+ const reason =
74
+ verification.configError instanceof Error
75
+ ? verification.configError.message
76
+ : String(verification.configError)
71
77
  for (const [level, name] of [
72
78
  [1, "route assurance"],
73
79
  [2, "capability lockfile"],
@@ -78,9 +84,10 @@ export async function collectVerificationLevels(
78
84
  }
79
85
  return finalize(statuses)
80
86
  }
87
+ const config = verification.config
81
88
 
82
- // L1 — route assurance.
83
- const assurance = evaluateRouteAssurance(config.source, config.policy)
89
+ // L1 - route assurance.
90
+ const assurance = verification.routeAssurance as AssuranceReport
84
91
  statuses.push({
85
92
  level: 1,
86
93
  name: "route assurance",
@@ -88,14 +95,13 @@ export async function collectVerificationLevels(
88
95
  reasons: assurance.findings.map((f) => f.message).slice(0, 5),
89
96
  })
90
97
 
91
- // L2 — capability assurance + lockfile in sync.
98
+ // L2 - capability assurance + lockfile in sync.
92
99
  const l2Reasons: string[] = []
93
- let capabilityReport: ReturnType<typeof evaluateCapabilityAssurance> | undefined
100
+ const capabilityReport = verification.capability?.report
94
101
  if (config.capabilities === undefined) {
95
102
  l2Reasons.push("nifra.assurance.ts declares no capabilities policy")
96
103
  } else {
97
- const project = await collectCapabilityProjectReport(cwd, config.source, config.capabilities)
98
- capabilityReport = project.report
104
+ const project = verification.capability as CapabilityProjectReport
99
105
  if (!project.report.ok) {
100
106
  l2Reasons.push(...project.report.findings.map((f) => f.message).slice(0, 5))
101
107
  }
@@ -115,11 +121,11 @@ export async function collectVerificationLevels(
115
121
  reasons: l2Reasons,
116
122
  })
117
123
 
118
- // L3 — emitted manifest hash-verifies AND matches the current app.
124
+ // L3 - emitted manifest hash-verifies AND matches the current app.
119
125
  const l3Reasons: string[] = []
120
126
  const manifestPath = resolve(cwd, config.manifest?.path ?? DEFAULT_MANIFEST_FILE)
121
127
  if (!existsSync(manifestPath)) {
122
- l3Reasons.push(`manifest missing (${manifestPath}) — run \`nifra manifest emit\``)
128
+ l3Reasons.push(`manifest missing (${manifestPath}) - run \`nifra manifest emit\``)
123
129
  } else {
124
130
  try {
125
131
  const recorded = await parseNifraManifest(await Bun.file(manifestPath).text(), manifestPath)
@@ -130,7 +136,7 @@ export async function collectVerificationLevels(
130
136
  ...(capabilityReport !== undefined ? { capabilities: capabilityReport } : {}),
131
137
  })
132
138
  if (current.contentHash !== recorded.contentHash) {
133
- l3Reasons.push("manifest is stale — the app changed since it was emitted")
139
+ l3Reasons.push("manifest is stale - the app changed since it was emitted")
134
140
  }
135
141
  } else {
136
142
  l3Reasons.push("manifest cannot be validated while assurance is failing")
@@ -146,11 +152,11 @@ export async function collectVerificationLevels(
146
152
  reasons: l3Reasons,
147
153
  })
148
154
 
149
- // L4 — contract-generated invariants against an explicitly isolated executor.
155
+ // L4 - contract-generated invariants against an explicitly isolated executor.
150
156
  const l4Reasons: string[] = []
151
157
  if (config.invariants === undefined) {
152
158
  l4Reasons.push(
153
- "no isolated invariant executor configured — add `invariants.executor` in nifra.assurance.ts",
159
+ "no isolated invariant executor configured - add `invariants.executor` in nifra.assurance.ts",
154
160
  )
155
161
  } else {
156
162
  const invariants = await runAdversarialContract(config.source as ContractTestApp, {
package/src/load.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * Zero-config app discovery. The CLI reads a few conventions from the project root:
3
- * - `routes/` — the file-based routes (per `@nifrajs/web/fs`).
4
- * - `nifra.config.ts` — the CLI's framework wiring (see {@link NifraFramework}); `framework.ts` is
3
+ * - `routes/` - the file-based routes (per `@nifrajs/web/fs`).
4
+ * - `nifra.config.ts` - the CLI's framework wiring (see {@link NifraFramework}); `framework.ts` is
5
5
  * used as a fallback. One of the two is required.
6
- * - `backend.ts` — exports `backend` (a `@nifrajs/core` server, the contract). Optional.
6
+ * - `backend.ts` - exports `backend` (a `@nifrajs/core` server, the contract). Optional.
7
7
  *
8
8
  * Why two filenames: a multi-target app's edge entry (`_worker.ts`) imports `framework.ts` for the
9
- * render adapter, so `framework.ts` must stay edge-bundlable — it can't reference Vite plugins or the
9
+ * render adapter, so `framework.ts` must stay edge-bundlable - it can't reference Vite plugins or the
10
10
  * SFC compiler (Bun would pull them into the `target:"browser"` worker and fail on `child_process`).
11
11
  * `nifra.config.ts` holds that build/dev tooling instead: it's imported ONLY by this CLI (which runs on
12
12
  * Bun), so its Vite plugin + SFC-compiler imports (`@vitejs/plugin-vue`, `@nifrajs/web-vue/plugin`) never
@@ -17,9 +17,9 @@ import { resolve } from "node:path"
17
17
  import { checkPipelineSeparation } from "./pipeline-guard.ts"
18
18
 
19
19
  /**
20
- * A plugin list — either an array, or a **thunk** that returns one (optionally async). Both forms are
20
+ * A plugin list - either an array, or a **thunk** that returns one (optionally async). Both forms are
21
21
  * accepted; {@link resolvePlugins} normalizes them. The thunk is just a convenience for deferring a
22
- * plugin's construction — it does NOT make a plugin edge-safe (Bun.build follows a dynamic `import()`
22
+ * plugin's construction - it does NOT make a plugin edge-safe (Bun.build follows a dynamic `import()`
23
23
  * even from an unused export, so a Vite/compiler import in an edge-bundled file fails regardless). Edge
24
24
  * safety comes from WHERE the config lives: keep these fields in `nifra.config.ts` (CLI-only), not in
25
25
  * the edge-imported `framework.ts`. See the module header.
@@ -29,7 +29,7 @@ export type PluginsField =
29
29
  | (() => readonly unknown[] | Promise<readonly unknown[]>)
30
30
 
31
31
  /**
32
- * The CLI's framework wiring — exported from `nifra.config.ts` (or `framework.ts` as a fallback).
32
+ * The CLI's framework wiring - exported from `nifra.config.ts` (or `framework.ts` as a fallback).
33
33
  * `create-nifra` generates it. Only `adapter` + `clientModule` are required; the plugin/condition
34
34
  * fields are framework-specific extras (Vue/Svelte/Solid need them; React/Preact don't).
35
35
  */
@@ -43,7 +43,7 @@ export interface NifraFramework {
43
43
  /** Bun build plugins that compile routes for the CLIENT bundle (`nifra build`), e.g.
44
44
  * `[vueBunPlugin("dom")]`. React/Preact JSX is Bun-native → none. */
45
45
  readonly clientPlugins?: PluginsField
46
- /** Bun runtime plugins that compile routes for SSR — `nifra dev` (Bun-native route import) and
46
+ /** Bun runtime plugins that compile routes for SSR - `nifra dev` (Bun-native route import) and
47
47
  * `nifra start` register them via `Bun.plugin`, e.g. `[vueBunPlugin("ssr")]`. React/Preact → none. */
48
48
  readonly serverPlugins?: PluginsField
49
49
  /** Extra Bun.build / Vite resolve conditions, e.g. Solid's `["solid"]`. */
@@ -109,7 +109,7 @@ export interface LoadAppOptions {
109
109
  }
110
110
 
111
111
  /**
112
- * Root-level monorepo config — exported from `nifra.config.ts` at the workspace root.
112
+ * Root-level monorepo config - exported from `nifra.config.ts` at the workspace root.
113
113
  * Each key in `apps` is the short name used to namespace MCP tools (`nifra_<name>_context` etc.);
114
114
  * each value is a path relative to the root that contains its own `nifra.config.ts` / `framework.ts`.
115
115
  *
@@ -145,7 +145,7 @@ export async function detectMonorepo(
145
145
 
146
146
  /**
147
147
  * Load all apps declared in a monorepo config. Returns `{ name, cwd, app }[]` in declaration order.
148
- * Individual app load failures throw — each app must be valid.
148
+ * Individual app load failures throw - each app must be valid.
149
149
  */
150
150
  export async function loadMonorepoApps(
151
151
  rootCwd: string,
@@ -187,7 +187,7 @@ export async function loadApp(
187
187
  }
188
188
  if (!existsSync(resolve(cwd, "routes"))) {
189
189
  throw new Error(
190
- `[nifra] no routes/ directory in ${cwd} — nifra apps are file-routed under routes/.`,
190
+ `[nifra] no routes/ directory in ${cwd} - nifra apps are file-routed under routes/.`,
191
191
  )
192
192
  }
193
193
  const fw = (await importWithQuery(configPath, options.importQuery)) as Partial<NifraFramework>
@@ -196,6 +196,15 @@ export async function loadApp(
196
196
  `[nifra] ${configFile} must export \`adapter\` (a render adapter object) and \`clientModule\` (a string).`,
197
197
  )
198
198
  }
199
+ // A `clientModule` given as a RELATIVE path (a local client entry, e.g. `./src/client.tsx`) is
200
+ // resolved to absolute here. It is embedded verbatim as an import specifier in the generated client
201
+ // entry, and `nifra dev` and `nifra build` write that entry into DIFFERENT directories - so a relative
202
+ // specifier would resolve against different bases and load in one phase but not the other. A bare or
203
+ // package specifier (`@nifrajs/web-react/client`) is location-independent and passes through unchanged.
204
+ const clientModule =
205
+ fw.clientModule.startsWith("./") || fw.clientModule.startsWith("../")
206
+ ? resolve(cwd, fw.clientModule)
207
+ : fw.clientModule
199
208
  const [vitePlugins, clientPlugins, serverPlugins] = await Promise.all([
200
209
  resolvePlugins(fw.vitePlugins),
201
210
  resolvePlugins(fw.clientPlugins),
@@ -215,7 +224,7 @@ export async function loadApp(
215
224
  cwd,
216
225
  routesDir: resolve(cwd, "routes"),
217
226
  outDir: resolve(cwd, outDirName),
218
- framework: fw as NifraFramework,
227
+ framework: { ...fw, clientModule } as NifraFramework,
219
228
  resolvedPlugins,
220
229
  backend,
221
230
  }
@@ -1,4 +1,4 @@
1
- /** `nifra manifest` — emit/sign/diff the deployable route trust artifact. */
1
+ /** `nifra manifest` - emit/sign/diff the deployable route trust artifact. */
2
2
 
3
3
  import { existsSync } from "node:fs"
4
4
  import { resolve } from "node:path"
@@ -46,7 +46,9 @@ export async function runManifestEmit(
46
46
  options: { readonly config?: string; readonly out?: string; readonly sign?: string } = {},
47
47
  ): Promise<boolean> {
48
48
  const config = await loadAssuranceConfig(cwd, options.config)
49
- const assurance = evaluateRouteAssurance(config.source, config.policy)
49
+ const assurance = evaluateRouteAssurance(config.source, config.policy, {
50
+ ...(config.capabilities !== undefined ? { definitions: config.capabilities.definitions } : {}),
51
+ })
50
52
  const capabilityProject =
51
53
  config.capabilities === undefined
52
54
  ? undefined