@boringparts/verule 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/additional-inputs.d.ts +13 -0
- package/dist/additional-inputs.js +68 -0
- package/dist/cli-command-identity.d.ts +13 -0
- package/dist/cli-command-identity.js +15 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +42 -0
- package/dist/config-loader.d.ts +14 -0
- package/dist/config-loader.js +203 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/inline-ignore.d.ts +44 -0
- package/dist/inline-ignore.js +297 -0
- package/dist/main.d.ts +12381 -0
- package/dist/main.js +531 -0
- package/dist/observation.d.ts +93 -0
- package/dist/observation.js +703 -0
- package/dist/projects.d.ts +21 -0
- package/dist/projects.js +171 -0
- package/dist/report-wire.d.ts +1260 -0
- package/dist/report-wire.js +629 -0
- package/dist/session-command.d.ts +48 -0
- package/dist/session-command.js +71 -0
- package/dist/session.d.ts +15 -0
- package/dist/session.js +677 -0
- package/dist/shared-session.d.ts +13 -0
- package/dist/shared-session.js +333 -0
- package/dist/types.d.ts +307 -0
- package/dist/types.js +43 -0
- package/dist/watch-command.d.ts +664 -0
- package/dist/watch-command.js +99 -0
- package/dist/watch.d.ts +57 -0
- package/dist/watch.js +792 -0
- package/package.json +57 -3
- package/README.md +0 -3
package/dist/main.js
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
import { CliWriteError, defineCli, executeCli, helpCapability, parseCliInvocation, schemaCapability, text, versionCapability, } from "@boringparts/cli-contract";
|
|
3
|
+
import { isNodeBrokenPipe } from "@boringparts/cli-contract/node";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { loadProjectConfigWithDependencies } from "./config-loader.js";
|
|
6
|
+
import { REPORT_SCHEMA_VERSION, VeruleExecutionError, getEngineVersion, parseObserveLimit, requestSharedReport, runCheck, } from "./index.js";
|
|
7
|
+
import { appendGapLines, checkFailureSchema, checkReportSchema, formatDiagnosticsLines, formatScopeLines, toWireReport, } from "./report-wire.js";
|
|
8
|
+
import { formatSessionReadyText, sessionReadyRecordSchema, streamSharedSession, } from "./session-command.js";
|
|
9
|
+
import { formatWatchReportText, streamWatchEvents, watchFailureRecordSchema, watchReportRecordSchema, } from "./watch-command.js";
|
|
10
|
+
function appendExplanationLines(lines, exp) {
|
|
11
|
+
const linePart = exp.target.line !== undefined ? `:${exp.target.line}` : "";
|
|
12
|
+
const colPart = exp.target.column !== undefined ? `:${exp.target.column}` : "";
|
|
13
|
+
lines.push(`Explanation [${exp.ruleId}] @ ${exp.target.file}${linePart}${colPart}: ${exp.status} (inCandidates=${String(exp.inCandidates)})`);
|
|
14
|
+
lines.push(` summary: ${exp.summary}`);
|
|
15
|
+
if (exp.inlineIgnore?.matched) {
|
|
16
|
+
const atSuffix = exp.inlineIgnore.commentAt
|
|
17
|
+
? ` at ${exp.inlineIgnore.commentAt.file}:${exp.inlineIgnore.commentAt.line}:${exp.inlineIgnore.commentAt.column}`
|
|
18
|
+
: "";
|
|
19
|
+
const reasonSuffix = exp.inlineIgnore.reason
|
|
20
|
+
? ` -- ${exp.inlineIgnore.reason}`
|
|
21
|
+
: "";
|
|
22
|
+
lines.push(` inlineIgnore: suppressed by verule-ignore-next-code-line${atSuffix}${reasonSuffix}`);
|
|
23
|
+
}
|
|
24
|
+
if (exp.excludedByCheckpoint) {
|
|
25
|
+
const detailSuffix = exp.excludedByCheckpoint.detail !== undefined
|
|
26
|
+
? ` detail=${JSON.stringify(exp.excludedByCheckpoint.detail)}`
|
|
27
|
+
: "";
|
|
28
|
+
lines.push(` excludedByCheckpoint: ${exp.excludedByCheckpoint.name} (${exp.excludedByCheckpoint.outcome})${detailSuffix}`);
|
|
29
|
+
}
|
|
30
|
+
if (exp.excludedByQuery) {
|
|
31
|
+
lines.push(` excludedByQuery: ${exp.excludedByQuery.op} (provider=${exp.excludedByQuery.provider}, cache=${exp.excludedByQuery.cacheStatus}, candidates=${exp.excludedByQuery.candidateCount})`);
|
|
32
|
+
}
|
|
33
|
+
if (exp.providers.length > 0) {
|
|
34
|
+
lines.push(` providers: ${exp.providers.join(", ")}`);
|
|
35
|
+
}
|
|
36
|
+
for (const gap of exp.gaps) {
|
|
37
|
+
const atSuffix = gap.at
|
|
38
|
+
? ` (${gap.at.file}:${gap.at.line}:${gap.at.column})`
|
|
39
|
+
: "";
|
|
40
|
+
lines.push(` gap [${gap.kind}]: ${gap.message}${atSuffix}`);
|
|
41
|
+
}
|
|
42
|
+
for (const cp of exp.cleanupPaths) {
|
|
43
|
+
const sites = cp.cleanupLocations
|
|
44
|
+
.map((loc) => `${loc.file}:${loc.line}:${loc.column}`)
|
|
45
|
+
.join(", ");
|
|
46
|
+
lines.push(` cleanupPath [${cp.instanceKey}]: ${cp.summary} (${sites})`);
|
|
47
|
+
}
|
|
48
|
+
for (const query of exp.queries) {
|
|
49
|
+
lines.push(` query #${query.queryIndex}: ${query.op} [provider=${query.provider}, cache=${query.cacheStatus}] candidates=${query.candidateCount} matchedTarget=${String(query.matchedTarget)}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function appendObservationLines(lines, obs) {
|
|
53
|
+
lines.push(`Observation (${obs.mode}): total=${obs.phases.totalMs}ms (configOpen=${obs.phases.configAndOpenMs}ms, engineQuery=${obs.phases.engineQueryMs}ms, ruleExec=${obs.phases.ruleExecutionMs}ms)`);
|
|
54
|
+
lines.push(` rpc: batches=${obs.rpc.batches} (queryBatches=${obs.rpc.queryBatches}), bytesSent=${obs.rpc.bytesSent}, bytesReceived=${obs.rpc.bytesReceived}`);
|
|
55
|
+
lines.push(` overhead: checkpoint=${obs.overhead.checkpointMs}ms, encode=${obs.overhead.encodeMs}ms, write=${obs.overhead.writeMs}ms`);
|
|
56
|
+
lines.push(` queries: total=${obs.queryCount}, computed=${obs.computedQueryCount}, reused=${obs.reusedQueryCount}, candidates=${obs.candidateCount}, checkpoints=${obs.checkpointCount}`);
|
|
57
|
+
if (obs.providers.length > 0) {
|
|
58
|
+
lines.push(` providers: ${obs.providers.join(", ")}`);
|
|
59
|
+
}
|
|
60
|
+
lines.push(` lowering: functions=${obs.lowering.functionsLowered}, vertices=${obs.lowering.verticesCreated}, duration=${obs.lowering.durationMs}ms`);
|
|
61
|
+
lines.push(` irCache: functionHits=${obs.irCache.functionHits}, functionMisses=${obs.irCache.functionMisses}, filesReused=${obs.irCache.filesReused}, filesInvalidated=${obs.irCache.filesInvalidated}`);
|
|
62
|
+
lines.push(` summaryLookup: lookups=${obs.summaryLookup.lookups}, hits=${obs.summaryLookup.hits}, misses=${obs.summaryLookup.misses} (local=${obs.summaryLookup.hitsBySource.local}, package=${obs.summaryLookup.hitsBySource.package}, community=${obs.summaryLookup.hitsBySource.community}, builtin=${obs.summaryLookup.hitsBySource.builtin})`);
|
|
63
|
+
if (obs.truncated) {
|
|
64
|
+
lines.push(` truncated: true (dropped=${obs.droppedCount})`);
|
|
65
|
+
}
|
|
66
|
+
if (obs.details) {
|
|
67
|
+
for (const detail of obs.details) {
|
|
68
|
+
const meta = [];
|
|
69
|
+
if (detail.ruleId !== undefined)
|
|
70
|
+
meta.push(`rule=${detail.ruleId}`);
|
|
71
|
+
if (detail.provider !== undefined)
|
|
72
|
+
meta.push(`provider=${detail.provider}`);
|
|
73
|
+
if (detail.cacheStatus !== undefined) {
|
|
74
|
+
meta.push(`cache=${detail.cacheStatus}`);
|
|
75
|
+
}
|
|
76
|
+
if (detail.candidateCount !== undefined) {
|
|
77
|
+
meta.push(`candidates=${detail.candidateCount}`);
|
|
78
|
+
}
|
|
79
|
+
const metaSuffix = meta.length > 0 ? ` (${meta.join(", ")})` : "";
|
|
80
|
+
lines.push(` detail [${detail.kind}]${metaSuffix}: ${detail.summary}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function appendExtrasText(lines, report) {
|
|
85
|
+
if (report.explanation) {
|
|
86
|
+
appendExplanationLines(lines, report.explanation);
|
|
87
|
+
}
|
|
88
|
+
else if (report.explanations) {
|
|
89
|
+
for (const exp of report.explanations) {
|
|
90
|
+
appendExplanationLines(lines, exp);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (report.observation) {
|
|
94
|
+
appendObservationLines(lines, report.observation);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function formatCheckCleanText(report) {
|
|
98
|
+
const lines = formatScopeLines(report);
|
|
99
|
+
appendGapLines(lines, report.gaps);
|
|
100
|
+
const summary = report.gaps.length > 0
|
|
101
|
+
? `No violations found across ${report.projects.length} project(s) (${report.gaps.length} gap(s)).`
|
|
102
|
+
: `No violations found across ${report.projects.length} project(s).`;
|
|
103
|
+
const extras = [];
|
|
104
|
+
appendExtrasText(extras, report);
|
|
105
|
+
const [first, ...rest] = [...lines, summary, ...extras];
|
|
106
|
+
return first !== undefined
|
|
107
|
+
? text.lines([first, ...rest])
|
|
108
|
+
: text.line(summary);
|
|
109
|
+
}
|
|
110
|
+
function formatCheckFindingsText(report) {
|
|
111
|
+
const lines = [
|
|
112
|
+
...formatScopeLines(report),
|
|
113
|
+
...formatDiagnosticsLines(report),
|
|
114
|
+
];
|
|
115
|
+
appendGapLines(lines, report.gaps);
|
|
116
|
+
const summary = report.gaps.length > 0
|
|
117
|
+
? `Found ${report.diagnostics.length} violation(s) (${report.gaps.length} gap(s)).`
|
|
118
|
+
: `Found ${report.diagnostics.length} violation(s).`;
|
|
119
|
+
const extras = [];
|
|
120
|
+
appendExtrasText(extras, report);
|
|
121
|
+
const [first, ...rest] = lines;
|
|
122
|
+
return first !== undefined
|
|
123
|
+
? text.lines([first, ...rest, summary, ...extras])
|
|
124
|
+
: text.lines([summary, ...extras]);
|
|
125
|
+
}
|
|
126
|
+
const define = defineCli();
|
|
127
|
+
const versionInput = z.object({});
|
|
128
|
+
const versionOutputSchema = z.object({ cli: z.string(), engine: z.string() });
|
|
129
|
+
const checkInput = z.object({
|
|
130
|
+
config: z.string().min(1).optional(),
|
|
131
|
+
session: z.string().min(1).optional(),
|
|
132
|
+
rule: z.string().min(1).optional(),
|
|
133
|
+
at: z.string().min(1).optional(),
|
|
134
|
+
observe: z.enum(["off", "summary", "detail"]).optional(),
|
|
135
|
+
observeLimit: z.string().min(1).optional(),
|
|
136
|
+
});
|
|
137
|
+
const watchInput = z.object({
|
|
138
|
+
config: z.string().min(1).optional(),
|
|
139
|
+
});
|
|
140
|
+
const sessionInput = z.object({
|
|
141
|
+
config: z.string().min(1).optional(),
|
|
142
|
+
socket: z.string().min(1),
|
|
143
|
+
});
|
|
144
|
+
async function executeCheck(input, workingDirectory) {
|
|
145
|
+
try {
|
|
146
|
+
const observeLimit = input.observeLimit !== undefined
|
|
147
|
+
? parseObserveLimit(input.observeLimit)
|
|
148
|
+
: undefined;
|
|
149
|
+
if (input.session !== undefined) {
|
|
150
|
+
const report = await requestSharedReport({
|
|
151
|
+
endpoint: input.session,
|
|
152
|
+
cwd: workingDirectory,
|
|
153
|
+
...(input.rule !== undefined ? { ruleIds: [input.rule] } : {}),
|
|
154
|
+
...(input.at !== undefined ? { at: input.at } : {}),
|
|
155
|
+
...(input.observe !== undefined ? { observe: input.observe } : {}),
|
|
156
|
+
...(observeLimit !== undefined ? { observeLimit } : {}),
|
|
157
|
+
});
|
|
158
|
+
return { ok: true, report: toWireReport(report) };
|
|
159
|
+
}
|
|
160
|
+
const configStart = performance.now();
|
|
161
|
+
const loaded = await loadProjectConfigWithDependencies(workingDirectory, input.config);
|
|
162
|
+
const configLoadMs = performance.now() - configStart;
|
|
163
|
+
const report = await runCheck({
|
|
164
|
+
cwd: workingDirectory,
|
|
165
|
+
config: loaded.config,
|
|
166
|
+
configLoadMs,
|
|
167
|
+
...(input.rule !== undefined ? { ruleIds: [input.rule] } : {}),
|
|
168
|
+
...(input.at !== undefined ? { at: input.at } : {}),
|
|
169
|
+
...(input.observe !== undefined ? { observe: input.observe } : {}),
|
|
170
|
+
...(observeLimit !== undefined ? { observeLimit } : {}),
|
|
171
|
+
});
|
|
172
|
+
return { ok: true, report: toWireReport(report) };
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
if (err instanceof VeruleExecutionError) {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
failure: {
|
|
179
|
+
reportVersion: REPORT_SCHEMA_VERSION,
|
|
180
|
+
code: err.code,
|
|
181
|
+
message: err.message,
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
failure: {
|
|
188
|
+
reportVersion: REPORT_SCHEMA_VERSION,
|
|
189
|
+
code: "ENGINE_ERROR",
|
|
190
|
+
message: err instanceof Error ? err.message : String(err),
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function createCliContract(identity) {
|
|
196
|
+
return define({
|
|
197
|
+
root: "verule",
|
|
198
|
+
help: helpCapability({
|
|
199
|
+
shortAlias: "-h",
|
|
200
|
+
headings: {
|
|
201
|
+
usage: "Usage",
|
|
202
|
+
commands: "Commands",
|
|
203
|
+
options: "Options",
|
|
204
|
+
constraints: "Constraints",
|
|
205
|
+
schema: "Contract Discovery",
|
|
206
|
+
},
|
|
207
|
+
wording: {
|
|
208
|
+
commandPlaceholder: "command",
|
|
209
|
+
outputModePlaceholder: "mode",
|
|
210
|
+
choices: "{usage} (choices: {choices})",
|
|
211
|
+
choicesAndDefault: "{usage} (choices: {choices}, default: {value})",
|
|
212
|
+
list: "{left}, {right}",
|
|
213
|
+
facts: "{left} with {right}",
|
|
214
|
+
present: "{field}",
|
|
215
|
+
forbiddenCombination: "Cannot combine {facts}.",
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
version: versionCapability({
|
|
219
|
+
value: text.line(`${identity.commandName} ${identity.version}`),
|
|
220
|
+
description: "Print the CLI version.",
|
|
221
|
+
shortAlias: "-V",
|
|
222
|
+
}),
|
|
223
|
+
schema: schemaCapability({
|
|
224
|
+
description: text.line("Inspect the command contract."),
|
|
225
|
+
}),
|
|
226
|
+
usageFailureExitCode: 2,
|
|
227
|
+
commands: {
|
|
228
|
+
verule: {
|
|
229
|
+
kind: "rootGroup",
|
|
230
|
+
name: identity.commandName,
|
|
231
|
+
description: "Static analyzer for TypeScript and JavaScript projects.",
|
|
232
|
+
},
|
|
233
|
+
...define.command("version")({
|
|
234
|
+
kind: "command",
|
|
235
|
+
parent: "verule",
|
|
236
|
+
name: "version",
|
|
237
|
+
description: "Show the installed Verule CLI and analysis engine versions.",
|
|
238
|
+
input: versionInput,
|
|
239
|
+
outputs: (factory) => {
|
|
240
|
+
const execution = factory.atomic({
|
|
241
|
+
success: {
|
|
242
|
+
kind: "data",
|
|
243
|
+
variants: {
|
|
244
|
+
versions: {
|
|
245
|
+
description: "Installed CLI and engine versions.",
|
|
246
|
+
schema: versionOutputSchema,
|
|
247
|
+
exitCode: 0,
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
failures: {},
|
|
252
|
+
handler: async ({ outcome }) => {
|
|
253
|
+
return outcome.data.versions({
|
|
254
|
+
cli: identity.version,
|
|
255
|
+
engine: await getEngineVersion(),
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
return {
|
|
260
|
+
default: "text",
|
|
261
|
+
modes: {
|
|
262
|
+
text: execution.text({
|
|
263
|
+
description: "Display the installed versions.",
|
|
264
|
+
success: {
|
|
265
|
+
versions: ({ cli, engine }) => text.line(`${identity.commandName} ${cli} (engine ${engine})`),
|
|
266
|
+
},
|
|
267
|
+
failures: {},
|
|
268
|
+
}),
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
},
|
|
272
|
+
}),
|
|
273
|
+
...define.command("check")({
|
|
274
|
+
kind: "command",
|
|
275
|
+
parent: "verule",
|
|
276
|
+
name: "check",
|
|
277
|
+
description: "Run configured project rules and report diagnostics.",
|
|
278
|
+
fields: {
|
|
279
|
+
config: {
|
|
280
|
+
kind: "valueOption",
|
|
281
|
+
longOption: "--config",
|
|
282
|
+
shortAlias: "-c",
|
|
283
|
+
description: "Path to the Verule configuration file.",
|
|
284
|
+
},
|
|
285
|
+
session: {
|
|
286
|
+
kind: "valueOption",
|
|
287
|
+
longOption: "--session",
|
|
288
|
+
description: "Query a running shared session socket instead of starting a new engine.",
|
|
289
|
+
},
|
|
290
|
+
rule: {
|
|
291
|
+
kind: "valueOption",
|
|
292
|
+
longOption: "--rule",
|
|
293
|
+
description: "Run only the specified rule ID.",
|
|
294
|
+
},
|
|
295
|
+
at: {
|
|
296
|
+
kind: "valueOption",
|
|
297
|
+
longOption: "--at",
|
|
298
|
+
description: "Explain why diagnostics did or did not trigger at <file[:line[:column]]>.",
|
|
299
|
+
},
|
|
300
|
+
observe: {
|
|
301
|
+
kind: "valueOption",
|
|
302
|
+
longOption: "--observe",
|
|
303
|
+
description: "Observation verbosity level (off, summary, or detail).",
|
|
304
|
+
},
|
|
305
|
+
observeLimit: {
|
|
306
|
+
kind: "valueOption",
|
|
307
|
+
longOption: "--observe-limit",
|
|
308
|
+
description: "Limit detail observation entries (e.g. 20 or 4096b).",
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
usageConstraints: [
|
|
312
|
+
{
|
|
313
|
+
kind: "forbiddenCombination",
|
|
314
|
+
all: [
|
|
315
|
+
{ kind: "present", field: "session" },
|
|
316
|
+
{ kind: "present", field: "config" },
|
|
317
|
+
],
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
input: checkInput,
|
|
321
|
+
outputs: (factory) => {
|
|
322
|
+
const execution = factory.atomic({
|
|
323
|
+
success: {
|
|
324
|
+
kind: "data",
|
|
325
|
+
variants: {
|
|
326
|
+
clean: {
|
|
327
|
+
description: "Analysis completed with zero violations.",
|
|
328
|
+
schema: checkReportSchema,
|
|
329
|
+
exitCode: 0,
|
|
330
|
+
},
|
|
331
|
+
findings: {
|
|
332
|
+
description: "Analysis completed and reported violations.",
|
|
333
|
+
schema: checkReportSchema,
|
|
334
|
+
exitCode: 1,
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
failures: {
|
|
339
|
+
failed: {
|
|
340
|
+
description: "Configuration, project loading, rule execution, or engine failure.",
|
|
341
|
+
schema: checkFailureSchema,
|
|
342
|
+
exitCode: 2,
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
handler: async ({ input, dependencies, outcome }) => {
|
|
346
|
+
const outcomeResult = await executeCheck(input, dependencies.cwd);
|
|
347
|
+
if (!outcomeResult.ok) {
|
|
348
|
+
return outcome.failure.failed(outcomeResult.failure);
|
|
349
|
+
}
|
|
350
|
+
return outcomeResult.report.status === "findings"
|
|
351
|
+
? outcome.data.findings(outcomeResult.report)
|
|
352
|
+
: outcome.data.clean(outcomeResult.report);
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
return {
|
|
356
|
+
default: "text",
|
|
357
|
+
modes: {
|
|
358
|
+
text: execution.text({
|
|
359
|
+
description: "Display check diagnostics as human-readable text.",
|
|
360
|
+
success: {
|
|
361
|
+
clean: formatCheckCleanText,
|
|
362
|
+
findings: formatCheckFindingsText,
|
|
363
|
+
},
|
|
364
|
+
failures: {
|
|
365
|
+
failed: ({ code, message }) => text.line(`Check failed [${code}]: ${message}`),
|
|
366
|
+
},
|
|
367
|
+
}),
|
|
368
|
+
json: execution.structured({
|
|
369
|
+
description: "Emit a versioned JSON check report.",
|
|
370
|
+
}),
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
},
|
|
374
|
+
}),
|
|
375
|
+
...define.command("watch")({
|
|
376
|
+
kind: "command",
|
|
377
|
+
parent: "verule",
|
|
378
|
+
name: "watch",
|
|
379
|
+
description: "Watch project files and emit complete reports on every save.",
|
|
380
|
+
fields: {
|
|
381
|
+
config: {
|
|
382
|
+
kind: "valueOption",
|
|
383
|
+
longOption: "--config",
|
|
384
|
+
shortAlias: "-c",
|
|
385
|
+
description: "Path to the Verule configuration file.",
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
input: watchInput,
|
|
389
|
+
outputs: (factory) => {
|
|
390
|
+
const execution = factory.stream({
|
|
391
|
+
records: {
|
|
392
|
+
report: {
|
|
393
|
+
description: "Complete analysis report for a snapshot.",
|
|
394
|
+
schema: watchReportRecordSchema,
|
|
395
|
+
},
|
|
396
|
+
failed: {
|
|
397
|
+
description: "Single-round check failure.",
|
|
398
|
+
schema: watchFailureRecordSchema,
|
|
399
|
+
},
|
|
400
|
+
},
|
|
401
|
+
failures: {},
|
|
402
|
+
handler: ({ input, dependencies, outcome }) => streamWatchEvents(dependencies.cwd, input.config, dependencies.signal, outcome),
|
|
403
|
+
});
|
|
404
|
+
return {
|
|
405
|
+
default: "text",
|
|
406
|
+
modes: {
|
|
407
|
+
text: execution.text({
|
|
408
|
+
description: "Display watch reports as human-readable text.",
|
|
409
|
+
records: {
|
|
410
|
+
report: formatWatchReportText,
|
|
411
|
+
failed: ({ sequence, code, message }) => text.line(`[watch #${sequence}] Check failed [${code}]: ${message}`),
|
|
412
|
+
},
|
|
413
|
+
failures: {},
|
|
414
|
+
terminal: () => text.line("Watch stopped."),
|
|
415
|
+
}),
|
|
416
|
+
ndjson: execution.structured({
|
|
417
|
+
description: "Emit complete analysis reports as NDJSON stream records.",
|
|
418
|
+
}),
|
|
419
|
+
},
|
|
420
|
+
};
|
|
421
|
+
},
|
|
422
|
+
}),
|
|
423
|
+
...define.command("session")({
|
|
424
|
+
kind: "command",
|
|
425
|
+
parent: "verule",
|
|
426
|
+
name: "session",
|
|
427
|
+
description: "Start an explicit shared analysis session bound to a configuration and socket endpoint.",
|
|
428
|
+
fields: {
|
|
429
|
+
config: {
|
|
430
|
+
kind: "valueOption",
|
|
431
|
+
longOption: "--config",
|
|
432
|
+
shortAlias: "-c",
|
|
433
|
+
description: "Path to the Verule configuration file.",
|
|
434
|
+
},
|
|
435
|
+
socket: {
|
|
436
|
+
kind: "valueOption",
|
|
437
|
+
longOption: "--socket",
|
|
438
|
+
description: "Local Unix domain socket or Windows named pipe path.",
|
|
439
|
+
},
|
|
440
|
+
},
|
|
441
|
+
input: sessionInput,
|
|
442
|
+
outputs: (factory) => {
|
|
443
|
+
const execution = factory.stream({
|
|
444
|
+
records: {
|
|
445
|
+
ready: {
|
|
446
|
+
description: "Shared session is initialized and listening for queries.",
|
|
447
|
+
schema: sessionReadyRecordSchema,
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
failures: {
|
|
451
|
+
failed: {
|
|
452
|
+
description: "Shared session failed to start.",
|
|
453
|
+
schema: checkFailureSchema,
|
|
454
|
+
exitCode: 2,
|
|
455
|
+
},
|
|
456
|
+
},
|
|
457
|
+
handler: ({ input, dependencies, outcome }) => streamSharedSession(dependencies.cwd, input.config, input.socket, dependencies.signal, outcome),
|
|
458
|
+
});
|
|
459
|
+
return {
|
|
460
|
+
default: "text",
|
|
461
|
+
modes: {
|
|
462
|
+
text: execution.text({
|
|
463
|
+
description: "Emit shared session readiness and shutdown text.",
|
|
464
|
+
records: {
|
|
465
|
+
ready: formatSessionReadyText,
|
|
466
|
+
},
|
|
467
|
+
failures: {
|
|
468
|
+
failed: ({ code, message }) => text.line(`Session failed [${code}]: ${message}`),
|
|
469
|
+
},
|
|
470
|
+
terminal: () => text.line("Shared session stopped."),
|
|
471
|
+
}),
|
|
472
|
+
ndjson: execution.structured({
|
|
473
|
+
description: "Emit shared session readiness and lifecycle events as NDJSON.",
|
|
474
|
+
}),
|
|
475
|
+
},
|
|
476
|
+
};
|
|
477
|
+
},
|
|
478
|
+
}),
|
|
479
|
+
},
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
// Cache compiled contracts per identity so repeated CLI invocations do not rebuild schemas.
|
|
483
|
+
const cliContractCache = new Map();
|
|
484
|
+
/** Returns the memoized Verule CLI contract for the given command identity. */
|
|
485
|
+
export function getCliContract(identity) {
|
|
486
|
+
const cacheKey = `${identity.commandName}\0${identity.version}`;
|
|
487
|
+
const cached = cliContractCache.get(cacheKey);
|
|
488
|
+
if (cached !== undefined) {
|
|
489
|
+
return cached;
|
|
490
|
+
}
|
|
491
|
+
const contract = createCliContract(identity);
|
|
492
|
+
cliContractCache.set(cacheKey, contract);
|
|
493
|
+
return contract;
|
|
494
|
+
}
|
|
495
|
+
/** Returns the process status after all output has been written. */
|
|
496
|
+
export async function runCli(runtime) {
|
|
497
|
+
try {
|
|
498
|
+
const workingDirectory = runtime.cwd ?? process.cwd();
|
|
499
|
+
const contract = getCliContract(runtime.identity);
|
|
500
|
+
const rawArgs = runtime.argv.slice(2);
|
|
501
|
+
const effectiveArgs = rawArgs.length === 0 ? ["version"] : rawArgs;
|
|
502
|
+
const invocation = parseCliInvocation(contract, effectiveArgs);
|
|
503
|
+
const { exitCode } = await executeCli(contract, {
|
|
504
|
+
invocation,
|
|
505
|
+
dependencies: {
|
|
506
|
+
cwd: workingDirectory,
|
|
507
|
+
signal: runtime.signal,
|
|
508
|
+
},
|
|
509
|
+
write: runtime.write,
|
|
510
|
+
});
|
|
511
|
+
return exitCode;
|
|
512
|
+
}
|
|
513
|
+
catch (error) {
|
|
514
|
+
if (error instanceof CliWriteError) {
|
|
515
|
+
if (error.destination === "stderr")
|
|
516
|
+
return 2;
|
|
517
|
+
if (isNodeBrokenPipe(error))
|
|
518
|
+
return 0;
|
|
519
|
+
}
|
|
520
|
+
try {
|
|
521
|
+
await runtime.write({
|
|
522
|
+
destination: "stderr",
|
|
523
|
+
chunk: "Verule could not complete the command.\n",
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
catch {
|
|
527
|
+
// The command still failed if its diagnostic could not be written.
|
|
528
|
+
}
|
|
529
|
+
return 2;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type DiagnosticLocation, type EngineCallIoMetrics, type ExplainCleanupPath, type Gap, type NormalizedObserveConfig, type ObserveLimit, type ObserveLimitInput, type ObserveMode, type ObserveOptions, type ProjectSummary, type SourceTarget } from "tssae";
|
|
2
|
+
import { type CheckpointOutcome, type Diagnostic, type ExplainResult, type ObservationReport } from "./types.ts";
|
|
3
|
+
export { type EngineCallIoMetrics, type NormalizedObserveConfig };
|
|
4
|
+
export type QueryTraceRecord = {
|
|
5
|
+
readonly ruleId: string;
|
|
6
|
+
readonly queryIndex: number;
|
|
7
|
+
readonly op: string;
|
|
8
|
+
readonly projectId: string;
|
|
9
|
+
readonly provider: string;
|
|
10
|
+
readonly providers?: readonly string[];
|
|
11
|
+
readonly cacheStatus: "computed" | "reused";
|
|
12
|
+
readonly candidateCount: number;
|
|
13
|
+
readonly matchedTarget: boolean;
|
|
14
|
+
readonly durationMs: number;
|
|
15
|
+
readonly summary: string;
|
|
16
|
+
readonly detail?: unknown;
|
|
17
|
+
};
|
|
18
|
+
export type CheckpointTraceRecord = {
|
|
19
|
+
readonly ruleId: string;
|
|
20
|
+
readonly projectId: string;
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly outcome: CheckpointOutcome;
|
|
23
|
+
readonly at?: DiagnosticLocation;
|
|
24
|
+
readonly detail?: unknown;
|
|
25
|
+
};
|
|
26
|
+
/** Parses `<file[:line[:column]]>` into a normalized `SourceTarget` relative to `cwd`. */
|
|
27
|
+
export declare function parseTargetSpec(input: string, cwd: string): SourceTarget;
|
|
28
|
+
/** Normalizes a string or `SourceTarget` relative to `cwd`. */
|
|
29
|
+
export declare function normalizeTargetSpec(target: string | SourceTarget, cwd: string): SourceTarget;
|
|
30
|
+
/** Parses `--observe-limit` strings (e.g. `20`, `4096b`, `4kb`, `10items`) or SDK limit objects. */
|
|
31
|
+
export declare function parseObserveLimit(input: string | ObserveLimitInput): ObserveLimit;
|
|
32
|
+
/** Normalizes Verule `observe` and `observeLimit` inputs into validated `tssae` observe options. */
|
|
33
|
+
export declare function normalizeObserveConfig(observe: ObserveMode | ObserveOptions | undefined, observeLimit: ObserveLimitInput | undefined): NormalizedObserveConfig;
|
|
34
|
+
/** Collects observation metrics, bounded detail entries, and explain traces for a check run. */
|
|
35
|
+
export declare class ObservationCollector {
|
|
36
|
+
#private;
|
|
37
|
+
readonly mode: ObserveMode;
|
|
38
|
+
readonly limit: ObserveLimit | undefined;
|
|
39
|
+
readonly explainTarget: SourceTarget | undefined;
|
|
40
|
+
constructor(options: {
|
|
41
|
+
readonly mode: ObserveMode;
|
|
42
|
+
readonly limit?: ObserveLimit;
|
|
43
|
+
readonly explainTarget?: SourceTarget;
|
|
44
|
+
readonly configAndOpenMs?: number;
|
|
45
|
+
});
|
|
46
|
+
isObserveEnabled(): boolean;
|
|
47
|
+
isExplainEnabled(): boolean;
|
|
48
|
+
isActive(): boolean;
|
|
49
|
+
addConfigAndOpenMs(ms: number): void;
|
|
50
|
+
addEngineQueryMs(ms: number): void;
|
|
51
|
+
addRuleExecutionMs(ms: number): void;
|
|
52
|
+
recordRpcCall(io: EngineCallIoMetrics): void;
|
|
53
|
+
recordQuery(params: {
|
|
54
|
+
readonly ruleId: string;
|
|
55
|
+
readonly op: string;
|
|
56
|
+
readonly projectId: string;
|
|
57
|
+
readonly provider: string;
|
|
58
|
+
readonly providers?: readonly string[];
|
|
59
|
+
readonly cacheStatus: "computed" | "reused";
|
|
60
|
+
readonly candidateCount: number;
|
|
61
|
+
readonly durationMs: number;
|
|
62
|
+
readonly summary: string;
|
|
63
|
+
readonly matchedLocations?: readonly DiagnosticLocation[];
|
|
64
|
+
readonly detail?: unknown;
|
|
65
|
+
}): void;
|
|
66
|
+
recordCheckpoint(params: {
|
|
67
|
+
readonly ruleId: string;
|
|
68
|
+
readonly projectId: string;
|
|
69
|
+
readonly name: string;
|
|
70
|
+
readonly outcome: CheckpointOutcome;
|
|
71
|
+
readonly resolveLocation: () => DiagnosticLocation | undefined;
|
|
72
|
+
readonly detailFn?: () => unknown;
|
|
73
|
+
}): void;
|
|
74
|
+
hasMatchingCandidateOverlapping(ruleId: string, loc: DiagnosticLocation): boolean;
|
|
75
|
+
recordCleanupPaths(params: {
|
|
76
|
+
readonly ruleId: string;
|
|
77
|
+
readonly targetLocations: readonly DiagnosticLocation[];
|
|
78
|
+
readonly paths: readonly ExplainCleanupPath[];
|
|
79
|
+
}): void;
|
|
80
|
+
recordRuleGaps(ruleId: string, gaps: readonly Gap[], relatedLocation?: DiagnosticLocation): void;
|
|
81
|
+
buildObservationReport(): ObservationReport | undefined;
|
|
82
|
+
buildExplainResult(params: {
|
|
83
|
+
readonly ruleId: string;
|
|
84
|
+
readonly projects: readonly ProjectSummary[];
|
|
85
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
86
|
+
readonly suppressions?: readonly {
|
|
87
|
+
readonly ruleId: string;
|
|
88
|
+
readonly at: DiagnosticLocation;
|
|
89
|
+
readonly directiveAt: DiagnosticLocation;
|
|
90
|
+
readonly reason: string;
|
|
91
|
+
}[];
|
|
92
|
+
}): ExplainResult;
|
|
93
|
+
}
|