@nifrajs/cli 5.15.1 → 5.16.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.
- package/CHANGELOG.md +47 -0
- package/README.md +11 -0
- package/dist/build-provenance.d.ts +14 -0
- package/dist/build-provenance.d.ts.map +1 -0
- package/dist/build-provenance.js +82 -0
- package/dist/build-provenance.js.map +1 -0
- package/dist/check-diagnostics.d.ts +1 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +4 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +1 -1
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +6 -5
- package/dist/check-scan.js.map +1 -1
- package/dist/check-timings.d.ts +11 -0
- package/dist/check-timings.d.ts.map +1 -0
- package/dist/check-timings.js +23 -0
- package/dist/check-timings.js.map +1 -0
- package/dist/check.d.ts +3 -0
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +68 -28
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +26 -11
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +3 -0
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +25 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/dev-feed-tool.d.ts.map +1 -1
- package/dist/dev-feed-tool.js +4 -2
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/doctor.d.ts +5 -2
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +27 -9
- package/dist/doctor.js.map +1 -1
- package/dist/internal/named-imports.d.ts +4 -0
- package/dist/internal/named-imports.d.ts.map +1 -0
- package/dist/internal/named-imports.js +19 -0
- package/dist/internal/named-imports.js.map +1 -0
- package/dist/internal/rewrite-kit.d.ts +35 -0
- package/dist/internal/rewrite-kit.d.ts.map +1 -0
- package/dist/internal/rewrite-kit.js +14 -0
- package/dist/internal/rewrite-kit.js.map +1 -0
- package/dist/internal/source-facts.d.ts +37 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +115 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/levels-tool.d.ts +3 -0
- package/dist/levels-tool.d.ts.map +1 -1
- package/dist/levels-tool.js +28 -18
- package/dist/levels-tool.js.map +1 -1
- package/dist/mcp-exec.js +1 -1
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts +3 -1
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +9 -2
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-isolate.d.ts.map +1 -1
- package/dist/mcp-isolate.js +3 -1
- package/dist/mcp-isolate.js.map +1 -1
- package/dist/mcp-project-child.d.ts +10 -0
- package/dist/mcp-project-child.d.ts.map +1 -1
- package/dist/mcp-project-child.js +12 -1
- package/dist/mcp-project-child.js.map +1 -1
- package/dist/mcp-tool-meta.d.ts.map +1 -1
- package/dist/mcp-tool-meta.js +4 -1
- package/dist/mcp-tool-meta.js.map +1 -1
- package/dist/recipes/5.16.0.d.ts +12 -0
- package/dist/recipes/5.16.0.d.ts.map +1 -0
- package/dist/recipes/5.16.0.js +31 -0
- package/dist/recipes/5.16.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +3 -0
- package/dist/review.js.map +1 -1
- package/dist/rules/codes.d.ts +3 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +3 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +15 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/route-hooks.d.ts +10 -0
- package/dist/rules/route-hooks.d.ts.map +1 -0
- package/dist/rules/route-hooks.js +88 -0
- package/dist/rules/route-hooks.js.map +1 -0
- package/dist/rules/silent-sse.d.ts +9 -0
- package/dist/rules/silent-sse.d.ts.map +1 -0
- package/dist/rules/silent-sse.js +89 -0
- package/dist/rules/silent-sse.js.map +1 -0
- package/dist/rules/source-scan.d.ts +4 -2
- package/dist/rules/source-scan.d.ts.map +1 -1
- package/dist/rules/source-scan.js +3 -1
- package/dist/rules/source-scan.js.map +1 -1
- package/dist/source-rewrites.d.ts +2 -20
- package/dist/source-rewrites.d.ts.map +1 -1
- package/dist/source-rewrites.js +3 -13
- package/dist/source-rewrites.js.map +1 -1
- package/dist/typecheck-cache.d.ts +6 -0
- package/dist/typecheck-cache.d.ts.map +1 -0
- package/dist/typecheck-cache.js +37 -0
- package/dist/typecheck-cache.js.map +1 -0
- package/dist/typecheck-coverage.js +1 -1
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/typed-api-rewrites.d.ts +3 -0
- package/dist/typed-api-rewrites.d.ts.map +1 -0
- package/dist/typed-api-rewrites.js +253 -0
- package/dist/typed-api-rewrites.js.map +1 -0
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +3 -1
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-gates.d.ts +23 -0
- package/dist/verification-gates.d.ts.map +1 -0
- package/dist/verification-gates.js +8 -0
- package/dist/verification-gates.js.map +1 -0
- package/dist/verification.d.ts +2 -0
- package/dist/verification.d.ts.map +1 -1
- package/dist/verification.js +6 -0
- package/dist/verification.js.map +1 -1
- package/dist/work-graph.d.ts +17 -8
- package/dist/work-graph.d.ts.map +1 -1
- package/dist/work-graph.js +157 -104
- package/dist/work-graph.js.map +1 -1
- package/docs/examples.json +2 -2
- package/docs/llms-full.txt +563 -46
- package/docs/types.json +269 -84
- package/package.json +12 -12
- package/src/build-provenance.ts +107 -0
- package/src/check-diagnostics.ts +5 -0
- package/src/check-scan.ts +7 -4
- package/src/check-timings.ts +27 -0
- package/src/check.ts +77 -32
- package/src/cli.ts +33 -12
- package/src/command-catalog.ts +28 -4
- package/src/dev-feed-tool.ts +8 -3
- package/src/doctor.ts +37 -18
- package/src/internal/named-imports.ts +26 -0
- package/src/internal/rewrite-kit.ts +49 -0
- package/src/internal/source-facts.ts +158 -0
- package/src/levels-tool.ts +27 -18
- package/src/mcp-exec.ts +1 -1
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +13 -1
- package/src/mcp-isolate.ts +4 -0
- package/src/mcp-project-child.ts +19 -1
- package/src/mcp-tool-meta.ts +5 -1
- package/src/recipes/5.16.0.ts +41 -0
- package/src/recipes/index.ts +2 -0
- package/src/review.ts +3 -0
- package/src/rules/codes.ts +3 -0
- package/src/rules/explanations.ts +15 -0
- package/src/rules/route-hooks.ts +112 -0
- package/src/rules/silent-sse.ts +95 -0
- package/src/rules/source-scan.ts +8 -3
- package/src/source-rewrites.ts +18 -52
- package/src/typecheck-cache.ts +33 -0
- package/src/typecheck-coverage.ts +1 -1
- package/src/typed-api-rewrites.ts +308 -0
- package/src/upgrade.ts +2 -1
- package/src/verification-gates.ts +9 -0
- package/src/verification.ts +8 -0
- package/src/work-graph.ts +200 -121
package/src/work-graph.ts
CHANGED
|
@@ -9,16 +9,25 @@
|
|
|
9
9
|
import { existsSync } from "node:fs"
|
|
10
10
|
import { readFile, stat } from "node:fs/promises"
|
|
11
11
|
import { basename, resolve } from "node:path"
|
|
12
|
+
import { pathToFileURL } from "node:url"
|
|
12
13
|
import {
|
|
13
14
|
type ProjectEvidenceSnapshot,
|
|
14
15
|
reflectedRoutesFromEvidence,
|
|
15
16
|
snapshotProjectEvidence,
|
|
16
17
|
} from "@nifrajs/core/evidence"
|
|
17
18
|
import type { ReflectedRoute } from "@nifrajs/core/reflection"
|
|
18
|
-
import { Glob } from "bun"
|
|
19
19
|
import { BACKEND_APP_FILE, CONFIG_FILE, FRAMEWORK_FILE } from "./app-files.ts"
|
|
20
|
+
import {
|
|
21
|
+
buildOutputFiles,
|
|
22
|
+
fingerprintBuildInputs,
|
|
23
|
+
fingerprintFiles,
|
|
24
|
+
projectSourceFiles,
|
|
25
|
+
readBuildProvenance,
|
|
26
|
+
} from "./build-provenance.ts"
|
|
20
27
|
import { digestRoute } from "./contracts.ts"
|
|
21
28
|
import { codeUnitOrder } from "./internal/code-unit-order.ts"
|
|
29
|
+
import { inProjectChildProcess } from "./mcp-io.ts"
|
|
30
|
+
import { VERIFICATION_GATES, type VerificationGate } from "./verification-gates.ts"
|
|
22
31
|
|
|
23
32
|
export type WorkGraphNodeKind =
|
|
24
33
|
| "route"
|
|
@@ -70,6 +79,8 @@ export interface BuildFreshness {
|
|
|
70
79
|
readonly buildDir?: string
|
|
71
80
|
readonly newestSourceMs?: number
|
|
72
81
|
readonly newestBuildMs?: number
|
|
82
|
+
readonly buildDigest?: string | undefined
|
|
83
|
+
readonly sourceDigest?: string
|
|
73
84
|
readonly reason?: string
|
|
74
85
|
}
|
|
75
86
|
|
|
@@ -86,7 +97,7 @@ export interface ImpactReport {
|
|
|
86
97
|
readonly impactedRoutes: readonly string[]
|
|
87
98
|
}
|
|
88
99
|
|
|
89
|
-
export type ProofKind = "
|
|
100
|
+
export type ProofKind = VerificationGate["kind"]
|
|
90
101
|
|
|
91
102
|
export interface ProofStep {
|
|
92
103
|
readonly id: string
|
|
@@ -94,6 +105,7 @@ export interface ProofStep {
|
|
|
94
105
|
readonly level: number
|
|
95
106
|
readonly cost: number
|
|
96
107
|
readonly command: string
|
|
108
|
+
/** Impact hints. Every ladder gate verifies the project as a whole. */
|
|
97
109
|
readonly nodeIds: readonly string[]
|
|
98
110
|
readonly fix?: string
|
|
99
111
|
}
|
|
@@ -106,17 +118,20 @@ export interface ProofPlan {
|
|
|
106
118
|
export type ProofStatus = "pass" | "fail" | "skip" | "pending"
|
|
107
119
|
|
|
108
120
|
export interface ProofEvidence {
|
|
121
|
+
readonly graphDigest: string
|
|
122
|
+
readonly buildDigest?: string | undefined
|
|
109
123
|
readonly id: string
|
|
110
124
|
readonly status: ProofStatus
|
|
111
125
|
readonly level: number
|
|
112
126
|
readonly nodeIds: readonly string[]
|
|
113
127
|
readonly command: string
|
|
114
|
-
readonly fix?: string
|
|
128
|
+
readonly fix?: string | undefined
|
|
115
129
|
}
|
|
116
130
|
|
|
117
131
|
export interface EvidenceBundle {
|
|
118
|
-
readonly version:
|
|
132
|
+
readonly version: 2
|
|
119
133
|
readonly graphDigest: string
|
|
134
|
+
readonly buildDigest?: string | undefined
|
|
120
135
|
readonly changedFiles: readonly string[]
|
|
121
136
|
readonly targetLevel: number
|
|
122
137
|
readonly plan: ProofPlan
|
|
@@ -135,9 +150,7 @@ export interface ProjectWorkGraphResult {
|
|
|
135
150
|
readonly evidence: EvidenceBundle
|
|
136
151
|
}
|
|
137
152
|
|
|
138
|
-
|
|
139
|
-
const SOURCE_GLOB = new Glob("**/*.{ts,tsx,mts,cts,js,jsx,mjs,cjs,json,svelte,vue,mdx,css}")
|
|
140
|
-
const IGNORED = /(^|\/)(node_modules|dist|build|\.nifra|\.git|\.wrangler|coverage)\//
|
|
153
|
+
const SERVER_ARTIFACT = /\.[cm]?js$/
|
|
141
154
|
const APP_SOURCE_DIR = /^(?:routes|frontend|backend|shared)\//
|
|
142
155
|
const BACKEND_SOURCE_DIR = /^(?:backend|shared)\//
|
|
143
156
|
const MANIFEST_NAMES = new Set([
|
|
@@ -185,27 +198,44 @@ export async function inspectBuildFreshness(cwd: string): Promise<BuildFreshness
|
|
|
185
198
|
const path = resolve(cwd, name)
|
|
186
199
|
if (existsSync(path)) sourcePaths.push(path)
|
|
187
200
|
}
|
|
188
|
-
for
|
|
189
|
-
|
|
190
|
-
if (!IGNORED.test(path) && APP_SOURCE_DIR.test(path)) sourcePaths.push(resolve(cwd, path))
|
|
201
|
+
for (const file of projectSourceFiles(cwd, resolve(cwd, "dist"))) {
|
|
202
|
+
if (APP_SOURCE_DIR.test(file)) sourcePaths.push(resolve(cwd, file))
|
|
191
203
|
}
|
|
192
204
|
const sourceTimes = await fileTimes(sourcePaths)
|
|
193
|
-
for (const buildDirName of ["dist", "build"]) {
|
|
205
|
+
for (const buildDirName of ["dist", "build", ".vercel/output"]) {
|
|
194
206
|
const buildDir = resolve(cwd, buildDirName)
|
|
195
207
|
if (!existsSync(buildDir)) continue
|
|
196
|
-
const artifacts
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
if (artifacts.length === 0) continue
|
|
202
|
-
const buildTimes = await fileTimes(artifacts)
|
|
203
|
-
return evaluateBuildFreshness({
|
|
208
|
+
const artifacts = buildOutputFiles(buildDir)
|
|
209
|
+
const scripts = artifacts.filter((path) => SERVER_ARTIFACT.test(path))
|
|
210
|
+
if (scripts.length === 0) continue
|
|
211
|
+
const freshness = evaluateBuildFreshness({
|
|
204
212
|
hasArtifact: true,
|
|
205
213
|
newestSourceMs: sourceTimes,
|
|
206
|
-
newestBuildMs:
|
|
214
|
+
newestBuildMs: await fileTimes(scripts.map((path) => resolve(buildDir, path))),
|
|
207
215
|
buildDir: buildDirName,
|
|
208
216
|
})
|
|
217
|
+
if (!freshness.ok) return freshness
|
|
218
|
+
|
|
219
|
+
const receipt = await readBuildProvenance(cwd, buildDir)
|
|
220
|
+
if (receipt === undefined) {
|
|
221
|
+
return {
|
|
222
|
+
...freshness,
|
|
223
|
+
ok: false,
|
|
224
|
+
reason: "nifra prove refused: no build provenance; run `nifra build`",
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const [sourceDigest, buildDigest] = await Promise.all([
|
|
228
|
+
fingerprintBuildInputs(cwd, buildDir),
|
|
229
|
+
fingerprintFiles(buildDir, artifacts),
|
|
230
|
+
])
|
|
231
|
+
if (receipt.sourceDigest !== sourceDigest || receipt.buildDigest !== buildDigest) {
|
|
232
|
+
return {
|
|
233
|
+
...freshness,
|
|
234
|
+
ok: false,
|
|
235
|
+
reason: "nifra prove refused: source or build contents changed; run `nifra build`",
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return { ...freshness, buildDigest, sourceDigest }
|
|
209
239
|
}
|
|
210
240
|
return evaluateBuildFreshness({
|
|
211
241
|
hasArtifact: false,
|
|
@@ -214,20 +244,89 @@ export async function inspectBuildFreshness(cwd: string): Promise<BuildFreshness
|
|
|
214
244
|
})
|
|
215
245
|
}
|
|
216
246
|
|
|
247
|
+
interface WorkGraphOptions {
|
|
248
|
+
readonly changedFiles?: readonly string[]
|
|
249
|
+
readonly minLevel?: number
|
|
250
|
+
readonly execute?: boolean
|
|
251
|
+
readonly signal?: AbortSignal
|
|
252
|
+
}
|
|
253
|
+
|
|
217
254
|
export async function collectProjectWorkGraph(
|
|
218
255
|
cwd: string,
|
|
219
|
-
options:
|
|
256
|
+
options: WorkGraphOptions = {},
|
|
220
257
|
): Promise<ProjectWorkGraphResult> {
|
|
221
258
|
const freshness = await inspectBuildFreshness(cwd)
|
|
222
259
|
if (!freshness.ok) throw new StaleBuildError(freshness)
|
|
223
260
|
const backendPath = resolve(cwd, BACKEND_APP_FILE)
|
|
224
261
|
if (!existsSync(backendPath)) throw new Error(`[nifra] no ${BACKEND_APP_FILE} in ${cwd}`)
|
|
225
|
-
const
|
|
262
|
+
const outDir = resolve(cwd, freshness.buildDir ?? "dist")
|
|
263
|
+
const files = await collectProjectFiles(cwd, outDir)
|
|
264
|
+
const verification =
|
|
265
|
+
options.execute === true
|
|
266
|
+
? await verifyProofGates(cwd, options.minLevel ?? 1, options.signal)
|
|
267
|
+
: undefined
|
|
268
|
+
const backendUrl = pathToFileURL(backendPath)
|
|
269
|
+
backendUrl.searchParams.set("nifra-work-graph", crypto.randomUUID())
|
|
270
|
+
const loadedValue: unknown = await import(backendUrl.href)
|
|
226
271
|
const loaded = recordOf(loadedValue)
|
|
227
272
|
if (loaded?.backend === undefined)
|
|
228
273
|
throw new Error(`[nifra] ${backendPath} does not export backend`)
|
|
229
|
-
const
|
|
230
|
-
|
|
274
|
+
const result = await buildProjectWorkGraph({ source: loaded.backend, files, freshness }, options)
|
|
275
|
+
if (verification === undefined) return result
|
|
276
|
+
|
|
277
|
+
const after = await inspectBuildFreshness(cwd)
|
|
278
|
+
if (
|
|
279
|
+
!after.ok ||
|
|
280
|
+
after.buildDigest !== freshness.buildDigest ||
|
|
281
|
+
after.buildDir !== freshness.buildDir ||
|
|
282
|
+
after.sourceDigest !== freshness.sourceDigest ||
|
|
283
|
+
sourceDigest(await collectProjectFiles(cwd, outDir)) !== sourceDigest(files)
|
|
284
|
+
) {
|
|
285
|
+
throw new StaleBuildError({
|
|
286
|
+
...after,
|
|
287
|
+
ok: false,
|
|
288
|
+
reason: "nifra prove refused: source or build changed during verification; rerun the proof",
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
options.signal?.throwIfAborted()
|
|
292
|
+
const proofs = result.plan.steps.map((step): ProofEvidence => {
|
|
293
|
+
const level = verification.find((status) => status.level === step.level)
|
|
294
|
+
return {
|
|
295
|
+
...step,
|
|
296
|
+
graphDigest: result.graph.digest,
|
|
297
|
+
buildDigest: freshness.buildDigest,
|
|
298
|
+
status: level === undefined ? "skip" : level.ok ? "pass" : "fail",
|
|
299
|
+
fix:
|
|
300
|
+
level?.ok === false
|
|
301
|
+
? `Run nifra ${step.level === 0 ? "check" : "levels"} for diagnostics`
|
|
302
|
+
: undefined,
|
|
303
|
+
}
|
|
304
|
+
})
|
|
305
|
+
return Object.freeze({
|
|
306
|
+
...result,
|
|
307
|
+
evidence: createEvidenceBundle(result.graph, result.impact, result.plan, proofs),
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async function verifyProofGates(cwd: string, target: number, signal?: AbortSignal) {
|
|
312
|
+
const throughLevel = VERIFICATION_GATES.find((gate) => gate.level === target)?.level
|
|
313
|
+
if (throughLevel === undefined) throw new RangeError("invalid proof target level")
|
|
314
|
+
signal?.throwIfAborted()
|
|
315
|
+
// Verify before reflecting the backend so the assurance loader can record route sites.
|
|
316
|
+
if (inProjectChildProcess()) {
|
|
317
|
+
const { collectVerificationLevels } = await import("./levels-tool.ts")
|
|
318
|
+
return (await collectVerificationLevels(cwd, { throughLevel })).levels
|
|
319
|
+
}
|
|
320
|
+
const { askProjectChild } = await import("./mcp-isolate.ts")
|
|
321
|
+
const result = await askProjectChild(
|
|
322
|
+
cwd,
|
|
323
|
+
{ op: "proof-gates", level: throughLevel },
|
|
324
|
+
signal === undefined ? {} : { context: { signal, requestId: null, reportProgress: () => {} } },
|
|
325
|
+
)
|
|
326
|
+
signal?.throwIfAborted()
|
|
327
|
+
if (!result.ok || !("proofGates" in result))
|
|
328
|
+
throw new Error("nifra prove: verification process did not return gate results")
|
|
329
|
+
return result.proofGates
|
|
231
330
|
}
|
|
232
331
|
|
|
233
332
|
export async function buildProjectWorkGraph(
|
|
@@ -342,7 +441,11 @@ export async function buildWorkGraph(input: WorkGraphBuildInput): Promise<WorkGr
|
|
|
342
441
|
const sortedEdges = edges.sort((a, b) =>
|
|
343
442
|
codeUnitOrder(`${a.from}\n${a.to}\n${a.relation}`, `${b.from}\n${b.to}\n${b.relation}`),
|
|
344
443
|
)
|
|
345
|
-
const digest = await digestJson({
|
|
444
|
+
const digest = await digestJson({
|
|
445
|
+
nodes: sortedNodes,
|
|
446
|
+
edges: sortedEdges,
|
|
447
|
+
build: input.freshness?.buildDigest,
|
|
448
|
+
})
|
|
346
449
|
return Object.freeze({
|
|
347
450
|
version: 1,
|
|
348
451
|
digest,
|
|
@@ -414,47 +517,17 @@ export function planProofs(graph: WorkGraph, impact: ImpactReport, targetLevel =
|
|
|
414
517
|
const routeIds = graph.nodes
|
|
415
518
|
.filter((node) => node.kind === "route" && nodeIds.includes(node.id))
|
|
416
519
|
.map((node) => node.id)
|
|
417
|
-
const steps
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
id: "proof:contract",
|
|
429
|
-
kind: "contract",
|
|
430
|
-
level: 1,
|
|
431
|
-
cost: 2,
|
|
432
|
-
command: "nifra contracts check --json",
|
|
433
|
-
nodeIds: routeIds,
|
|
434
|
-
})
|
|
435
|
-
steps.push({
|
|
436
|
-
id: "proof:assurance",
|
|
437
|
-
kind: "assurance",
|
|
438
|
-
level: 2,
|
|
439
|
-
cost: 3,
|
|
440
|
-
command: "nifra assure --json --strict",
|
|
441
|
-
nodeIds: routeIds,
|
|
442
|
-
})
|
|
443
|
-
steps.push({
|
|
444
|
-
id: "proof:suite",
|
|
445
|
-
kind: "suite",
|
|
446
|
-
level: 3,
|
|
447
|
-
cost: 4,
|
|
448
|
-
command: "nifra test",
|
|
449
|
-
nodeIds,
|
|
450
|
-
})
|
|
451
|
-
}
|
|
452
|
-
return Object.freeze({
|
|
453
|
-
steps: Object.freeze(
|
|
454
|
-
steps.filter((step) => step.level <= Math.max(targetLevel, 0) || targetLevel > 3),
|
|
455
|
-
),
|
|
456
|
-
targetLevel,
|
|
457
|
-
})
|
|
520
|
+
const steps = VERIFICATION_GATES.filter((gate) => gate.level <= targetLevel).map(
|
|
521
|
+
(gate): ProofStep => ({
|
|
522
|
+
id: `proof:${gate.kind}`,
|
|
523
|
+
kind: gate.kind,
|
|
524
|
+
level: gate.level,
|
|
525
|
+
cost: gate.level + 1,
|
|
526
|
+
command: `nifra prove --run --min ${gate.level} --json`,
|
|
527
|
+
nodeIds: gate.kind === "typecheck" ? nodeIds : routeIds,
|
|
528
|
+
}),
|
|
529
|
+
)
|
|
530
|
+
return Object.freeze({ steps: Object.freeze(steps), targetLevel })
|
|
458
531
|
}
|
|
459
532
|
|
|
460
533
|
export function createEvidenceBundle(
|
|
@@ -463,56 +536,62 @@ export function createEvidenceBundle(
|
|
|
463
536
|
plan: ProofPlan,
|
|
464
537
|
proofs: readonly ProofEvidence[] = [],
|
|
465
538
|
): EvidenceBundle {
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
const
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
.map((step) => step.command)
|
|
479
|
-
const next = done
|
|
480
|
-
? undefined
|
|
481
|
-
: plan.steps.find(
|
|
482
|
-
(step) => !evidence.some((item) => item.id === step.id && item.status === "pass"),
|
|
483
|
-
)
|
|
539
|
+
const byGate = new Map<string, ProofEvidence>()
|
|
540
|
+
for (const proof of proofs) {
|
|
541
|
+
if (proof.graphDigest !== graph.digest || proof.buildDigest !== graph.freshness.buildDigest)
|
|
542
|
+
continue
|
|
543
|
+
const step = plan.steps.find((candidate) => matchesStep(proof, candidate))
|
|
544
|
+
if (step !== undefined) byGate.set(proof.id, { ...proof, nodeIds: step.nodeIds })
|
|
545
|
+
}
|
|
546
|
+
const evidence = [...byGate.values()].sort((a, b) => codeUnitOrder(a.id, b.id))
|
|
547
|
+
const pending = plan.steps.filter(
|
|
548
|
+
(step) => !evidence.some((item) => item.id === step.id && item.status === "pass"),
|
|
549
|
+
)
|
|
550
|
+
const next = pending[0]
|
|
484
551
|
return Object.freeze({
|
|
485
|
-
version:
|
|
552
|
+
version: 2,
|
|
486
553
|
graphDigest: graph.digest,
|
|
554
|
+
buildDigest: graph.freshness.buildDigest,
|
|
487
555
|
changedFiles: impact.changedFiles,
|
|
488
556
|
targetLevel: plan.targetLevel,
|
|
489
557
|
plan,
|
|
490
558
|
proofs: Object.freeze(evidence),
|
|
491
559
|
stop: Object.freeze({
|
|
492
|
-
done,
|
|
493
|
-
missing: Object.freeze(
|
|
560
|
+
done: next === undefined,
|
|
561
|
+
missing: Object.freeze(pending.map((step) => step.command)),
|
|
494
562
|
...(next === undefined ? {} : { next }),
|
|
495
563
|
}),
|
|
496
564
|
})
|
|
497
565
|
}
|
|
498
566
|
|
|
567
|
+
function matchesStep(proof: ProofEvidence, step: ProofStep): boolean {
|
|
568
|
+
return proof.id === step.id && proof.level === step.level && proof.command === step.command
|
|
569
|
+
}
|
|
570
|
+
|
|
499
571
|
export function recordProof(bundle: EvidenceBundle, proof: ProofEvidence): EvidenceBundle {
|
|
572
|
+
if (
|
|
573
|
+
proof.graphDigest !== bundle.graphDigest ||
|
|
574
|
+
proof.buildDigest !== bundle.buildDigest ||
|
|
575
|
+
!bundle.plan.steps.some((step) => matchesStep(proof, step))
|
|
576
|
+
) {
|
|
577
|
+
throw new Error("nifra prove refused: proof belongs to a different snapshot or gate")
|
|
578
|
+
}
|
|
500
579
|
const graph: WorkGraph = {
|
|
501
580
|
version: 1,
|
|
502
581
|
digest: bundle.graphDigest,
|
|
503
582
|
nodes: [],
|
|
504
583
|
edges: [],
|
|
505
|
-
freshness: {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
impactedNodes: bundle.stop.done ? [] : ["pending"],
|
|
510
|
-
impactedRoutes: [],
|
|
584
|
+
freshness: {
|
|
585
|
+
ok: true,
|
|
586
|
+
buildDigest: bundle.buildDigest,
|
|
587
|
+
},
|
|
511
588
|
}
|
|
512
|
-
return createEvidenceBundle(
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
589
|
+
return createEvidenceBundle(
|
|
590
|
+
graph,
|
|
591
|
+
{ changedFiles: bundle.changedFiles, impactedNodes: [], impactedRoutes: [] },
|
|
592
|
+
bundle.plan,
|
|
593
|
+
[...bundle.proofs.filter((item) => item.id !== proof.id), proof],
|
|
594
|
+
)
|
|
516
595
|
}
|
|
517
596
|
|
|
518
597
|
export function renderWorkGraphJson(result: ProjectWorkGraphResult): string {
|
|
@@ -521,11 +600,7 @@ export function renderWorkGraphJson(result: ProjectWorkGraphResult): string {
|
|
|
521
600
|
|
|
522
601
|
export async function runWorkGraph(
|
|
523
602
|
cwd: string,
|
|
524
|
-
options: {
|
|
525
|
-
readonly changedFiles?: readonly string[]
|
|
526
|
-
readonly minLevel?: number
|
|
527
|
-
readonly json?: boolean
|
|
528
|
-
} = {},
|
|
603
|
+
options: WorkGraphOptions & { readonly json?: boolean } = {},
|
|
529
604
|
): Promise<boolean> {
|
|
530
605
|
const result = await collectProjectWorkGraph(cwd, options)
|
|
531
606
|
console.log(options.json === false ? renderWorkGraphText(result) : renderWorkGraphJson(result))
|
|
@@ -539,35 +614,39 @@ export function renderWorkGraphText(result: ProjectWorkGraphResult): string {
|
|
|
539
614
|
`impacted routes: ${result.impact.impactedRoutes.length === 0 ? "none" : result.impact.impactedRoutes.join(", ")}`,
|
|
540
615
|
`stop: ${result.evidence.stop.done ? "done" : "pending"}`,
|
|
541
616
|
]
|
|
617
|
+
for (const proof of result.evidence.proofs) {
|
|
618
|
+
lines.push(`L${proof.level} ${proof.status}${proof.fix === undefined ? "" : ` - ${proof.fix}`}`)
|
|
619
|
+
}
|
|
542
620
|
if (!result.evidence.stop.done)
|
|
543
621
|
lines.push(`next: ${result.evidence.stop.next?.command ?? "no proof available"}`)
|
|
544
622
|
return lines.join("\n")
|
|
545
623
|
}
|
|
546
624
|
|
|
547
|
-
async function collectProjectFiles(cwd: string): Promise<WorkGraphSourceFile[]> {
|
|
625
|
+
async function collectProjectFiles(cwd: string, outDir: string): Promise<WorkGraphSourceFile[]> {
|
|
548
626
|
const files: WorkGraphSourceFile[] = []
|
|
549
|
-
for
|
|
550
|
-
const
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
? { kind: "
|
|
560
|
-
:
|
|
561
|
-
|
|
562
|
-
: {}),
|
|
563
|
-
})
|
|
564
|
-
} catch {
|
|
565
|
-
// A file can disappear while an agent is editing. The next graph run sees the settled tree.
|
|
566
|
-
}
|
|
627
|
+
for (const path of projectSourceFiles(cwd, outDir)) {
|
|
628
|
+
const bytes = await readFile(resolve(cwd, path))
|
|
629
|
+
files.push({
|
|
630
|
+
path,
|
|
631
|
+
content: path.endsWith(".lockb")
|
|
632
|
+
? Bun.CryptoHasher.hash("sha256", bytes, "hex")
|
|
633
|
+
: bytes.toString("utf8"),
|
|
634
|
+
...(isTestFile(path)
|
|
635
|
+
? { kind: "test" as const }
|
|
636
|
+
: MANIFEST_NAMES.has(basename(path))
|
|
637
|
+
? { kind: "manifest" as const }
|
|
638
|
+
: {}),
|
|
639
|
+
})
|
|
567
640
|
}
|
|
568
641
|
return files
|
|
569
642
|
}
|
|
570
643
|
|
|
644
|
+
function sourceDigest(files: readonly WorkGraphSourceFile[]): string {
|
|
645
|
+
const hash = new Bun.CryptoHasher("sha256")
|
|
646
|
+
for (const { path, content } of files) hash.update(JSON.stringify([path, content]))
|
|
647
|
+
return hash.digest("hex")
|
|
648
|
+
}
|
|
649
|
+
|
|
571
650
|
async function fileTimes(paths: readonly string[]): Promise<number> {
|
|
572
651
|
let newest = 0
|
|
573
652
|
for (const path of paths) {
|