@boringparts/verule 0.1.0 → 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.
@@ -1,10 +1,6 @@
1
- import { type DiagnosticLocation, type EngineCallIoMetrics, type ExplainCleanupPath, type ExplainLocalGap, type ExplainProviderEvaluation, type Gap, type NativeAdapterObservation, type ObserveLimit, type ObserveLimitInput, type ObserveMode, type ObserveOptions, type ProjectSummary, type SourceTarget } from "tssae";
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
2
  import { type CheckpointOutcome, type Diagnostic, type ExplainResult, type ObservationReport } from "./types.ts";
3
- export { type EngineCallIoMetrics };
4
- export type NormalizedObserveConfig = {
5
- readonly mode: ObserveMode;
6
- readonly limit?: ObserveLimit;
7
- };
3
+ export { type EngineCallIoMetrics, type NormalizedObserveConfig };
8
4
  export type QueryTraceRecord = {
9
5
  readonly ruleId: string;
10
6
  readonly queryIndex: number;
@@ -33,6 +29,7 @@ export declare function parseTargetSpec(input: string, cwd: string): SourceTarge
33
29
  export declare function normalizeTargetSpec(target: string | SourceTarget, cwd: string): SourceTarget;
34
30
  /** Parses `--observe-limit` strings (e.g. `20`, `4096b`, `4kb`, `10items`) or SDK limit objects. */
35
31
  export declare function parseObserveLimit(input: string | ObserveLimitInput): ObserveLimit;
32
+ /** Normalizes Verule `observe` and `observeLimit` inputs into validated `tssae` observe options. */
36
33
  export declare function normalizeObserveConfig(observe: ObserveMode | ObserveOptions | undefined, observeLimit: ObserveLimitInput | undefined): NormalizedObserveConfig;
37
34
  /** Collects observation metrics, bounded detail entries, and explain traces for a check run. */
38
35
  export declare class ObservationCollector {
@@ -45,11 +42,6 @@ export declare class ObservationCollector {
45
42
  readonly limit?: ObserveLimit;
46
43
  readonly explainTarget?: SourceTarget;
47
44
  readonly configAndOpenMs?: number;
48
- readonly nativeAdapters?: readonly NativeAdapterObservation[];
49
- readonly configuredAdapters?: readonly {
50
- readonly id: string;
51
- readonly factTypeIds: readonly string[];
52
- }[];
53
45
  });
54
46
  isObserveEnabled(): boolean;
55
47
  isExplainEnabled(): boolean;
@@ -85,17 +77,7 @@ export declare class ObservationCollector {
85
77
  readonly targetLocations: readonly DiagnosticLocation[];
86
78
  readonly paths: readonly ExplainCleanupPath[];
87
79
  }): void;
88
- recordRuleGaps(ruleId: string, gaps: readonly Gap[]): void;
89
- recordFactExplain(params: {
90
- readonly ruleId: string;
91
- readonly subjectLocation?: DiagnosticLocation;
92
- readonly gaps: readonly Gap[];
93
- readonly localGaps: readonly ExplainLocalGap[];
94
- }): void;
95
- recordProviderEvaluation(params: {
96
- readonly ruleId: string;
97
- readonly evaluation: ExplainProviderEvaluation;
98
- }): void;
80
+ recordRuleGaps(ruleId: string, gaps: readonly Gap[], relatedLocation?: DiagnosticLocation): void;
99
81
  buildObservationReport(): ObservationReport | undefined;
100
82
  buildExplainResult(params: {
101
83
  readonly ruleId: string;
@@ -1,7 +1,7 @@
1
1
  import { isAbsolute, relative } from "node:path";
2
2
  import { performance } from "node:perf_hooks";
3
- import { locationMatchesTarget, locationsOverlap, } from "tssae";
4
- import { VeruleExecutionError, } from "./types.js";
3
+ import { locationMatchesTarget, locationsOverlap, normalizeObserveConfig as normalizeEngineObserveConfig, } from "tssae";
4
+ import { toVeruleError, VeruleExecutionError, } from "./types.js";
5
5
  export {};
6
6
  function normalizeSlashes(value) {
7
7
  return value.replaceAll("\\", "/");
@@ -108,32 +108,20 @@ function parseNonNegativeInt(raw, source) {
108
108
  throw new VeruleExecutionError("INVALID_CONFIG", `Invalid observe limit "${source}": expected a non-negative integer.`);
109
109
  }
110
110
  const value = Number(raw);
111
- if (!Number.isSafeInteger(value) || value < 0) {
112
- throw new VeruleExecutionError("INVALID_CONFIG", `Invalid observe limit "${source}": must be a non-negative integer.`);
111
+ if (!Number.isSafeInteger(value)) {
112
+ throw new VeruleExecutionError("INVALID_CONFIG", `Invalid observe limit "${source}" ${String(value)}: must be a non-negative integer.`);
113
113
  }
114
114
  return value;
115
115
  }
116
116
  /** Parses `--observe-limit` strings (e.g. `20`, `4096b`, `4kb`, `10items`) or SDK limit objects. */
117
117
  export function parseObserveLimit(input) {
118
- if (typeof input === "number") {
119
- if (!Number.isSafeInteger(input) || input < 0) {
120
- throw new VeruleExecutionError("INVALID_CONFIG", `Invalid observe limit ${String(input)}: must be a non-negative integer.`);
118
+ if (typeof input !== "string") {
119
+ try {
120
+ return (normalizeEngineObserveConfig({ mode: "off", limit: input }).limit ?? {});
121
121
  }
122
- return { maxItems: input };
123
- }
124
- if (typeof input === "object") {
125
- if (input.maxItems !== undefined &&
126
- (!Number.isSafeInteger(input.maxItems) || input.maxItems < 0)) {
127
- throw new VeruleExecutionError("INVALID_CONFIG", `Invalid maxItems ${String(input.maxItems)}: must be a non-negative integer.`);
128
- }
129
- if (input.maxBytes !== undefined &&
130
- (!Number.isSafeInteger(input.maxBytes) || input.maxBytes < 0)) {
131
- throw new VeruleExecutionError("INVALID_CONFIG", `Invalid maxBytes ${String(input.maxBytes)}: must be a non-negative integer.`);
122
+ catch (err) {
123
+ throw toVeruleError(err, "INVALID_CONFIG");
132
124
  }
133
- return {
134
- ...(input.maxItems !== undefined ? { maxItems: input.maxItems } : {}),
135
- ...(input.maxBytes !== undefined ? { maxBytes: input.maxBytes } : {}),
136
- };
137
125
  }
138
126
  const normalized = input.trim().toLowerCase();
139
127
  if (normalized === "") {
@@ -161,6 +149,7 @@ export function parseObserveLimit(input) {
161
149
  }
162
150
  return { maxItems: parseNonNegativeInt(normalized, input) };
163
151
  }
152
+ /** Normalizes Verule `observe` and `observeLimit` inputs into validated `tssae` observe options. */
164
153
  export function normalizeObserveConfig(observe, observeLimit) {
165
154
  const mode = observe === undefined
166
155
  ? "off"
@@ -170,19 +159,22 @@ export function normalizeObserveConfig(observe, observeLimit) {
170
159
  const rawLimit = typeof observe === "object" && observe.limit !== undefined
171
160
  ? observe.limit
172
161
  : observeLimit;
173
- const limit = rawLimit !== undefined ? parseObserveLimit(rawLimit) : undefined;
174
- return {
175
- mode,
176
- ...(limit !== undefined ? { limit } : {}),
177
- };
162
+ const limit = typeof rawLimit === "string" ? parseObserveLimit(rawLimit) : rawLimit;
163
+ try {
164
+ return normalizeEngineObserveConfig({
165
+ mode,
166
+ ...(limit !== undefined ? { limit } : {}),
167
+ });
168
+ }
169
+ catch (err) {
170
+ throw toVeruleError(err, "INVALID_CONFIG");
171
+ }
178
172
  }
179
173
  /** Collects observation metrics, bounded detail entries, and explain traces for a check run. */
180
174
  export class ObservationCollector {
181
175
  mode;
182
176
  limit;
183
177
  explainTarget;
184
- #nativeAdapters;
185
- #configuredAdapters;
186
178
  #startedAt;
187
179
  #configAndOpenMs;
188
180
  #engineQueryMs = 0;
@@ -202,24 +194,31 @@ export class ObservationCollector {
202
194
  #truncated = false;
203
195
  #droppedCount = 0;
204
196
  #usedDetailBytes = 0;
197
+ #lowering = { functionsLowered: 0, verticesCreated: 0, durationMs: 0 };
198
+ #irCache = {
199
+ functionHits: 0,
200
+ functionMisses: 0,
201
+ filesReused: 0,
202
+ filesInvalidated: 0,
203
+ };
204
+ #summaryLookup = {
205
+ lookups: 0,
206
+ hits: 0,
207
+ misses: 0,
208
+ hitsBySource: { local: 0, package: 0, community: 0, builtin: 0 },
209
+ };
205
210
  #providers = new Set();
206
- #engineExtractions = new Map();
207
211
  #details = [];
208
212
  #queriesByRule = new Map();
209
213
  #checkpointsByRule = new Map();
210
214
  #matchingCandidatesByRule = new Map();
211
215
  #cleanupPathsByRule = new Map();
212
216
  #gapsByRule = new Map();
213
- #factGapsByRule = new Map();
214
- #localGapsByRule = new Map();
215
- #providerEvaluationsByRule = new Map();
216
217
  constructor(options) {
217
218
  this.mode = options.mode;
218
219
  this.limit = options.limit;
219
220
  this.explainTarget = options.explainTarget;
220
221
  this.#configAndOpenMs = options.configAndOpenMs ?? 0;
221
- this.#nativeAdapters = options.nativeAdapters ?? [];
222
- this.#configuredAdapters = options.configuredAdapters ?? [];
223
222
  this.#startedAt = this.isObserveEnabled() ? performance.now() : 0;
224
223
  }
225
224
  isObserveEnabled() {
@@ -290,23 +289,37 @@ export class ObservationCollector {
290
289
  }
291
290
  }
292
291
  }
292
+ if (engineMetrics.lowering) {
293
+ this.#lowering.functionsLowered +=
294
+ engineMetrics.lowering.functionsLowered;
295
+ this.#lowering.verticesCreated += engineMetrics.lowering.verticesCreated;
296
+ this.#lowering.durationMs += engineMetrics.lowering.durationMs;
297
+ }
298
+ if (engineMetrics.irCache) {
299
+ this.#irCache.functionHits += engineMetrics.irCache.functionHits;
300
+ this.#irCache.functionMisses += engineMetrics.irCache.functionMisses;
301
+ this.#irCache.filesReused += engineMetrics.irCache.filesReused;
302
+ this.#irCache.filesInvalidated += engineMetrics.irCache.filesInvalidated;
303
+ }
304
+ if (engineMetrics.summaryLookup) {
305
+ this.#summaryLookup.lookups += engineMetrics.summaryLookup.lookups;
306
+ this.#summaryLookup.hits += engineMetrics.summaryLookup.hits;
307
+ this.#summaryLookup.misses += engineMetrics.summaryLookup.misses;
308
+ this.#summaryLookup.hitsBySource.local +=
309
+ engineMetrics.summaryLookup.hitsBySource.local;
310
+ this.#summaryLookup.hitsBySource.package +=
311
+ engineMetrics.summaryLookup.hitsBySource.package;
312
+ this.#summaryLookup.hitsBySource.community +=
313
+ engineMetrics.summaryLookup.hitsBySource.community;
314
+ this.#summaryLookup.hitsBySource.builtin +=
315
+ engineMetrics.summaryLookup.hitsBySource.builtin;
316
+ }
293
317
  if (engineMetrics.truncated) {
294
318
  this.#truncated = true;
295
319
  }
296
320
  if (engineMetrics.droppedCount > 0) {
297
321
  this.#droppedCount += engineMetrics.droppedCount;
298
322
  }
299
- if (engineMetrics.providerExtractions) {
300
- for (const [providerId, stats] of Object.entries(engineMetrics.providerExtractions)) {
301
- const current = this.#engineExtractions.get(providerId) ?? {
302
- extractions: 0,
303
- candidates: 0,
304
- };
305
- current.extractions += stats.extractions;
306
- current.candidates += stats.candidates;
307
- this.#engineExtractions.set(providerId, current);
308
- }
309
- }
310
323
  }
311
324
  recordQuery(params) {
312
325
  if (!this.isActive()) {
@@ -437,50 +450,18 @@ export class ObservationCollector {
437
450
  }
438
451
  this.#cleanupPathsByRule.set(params.ruleId, list);
439
452
  }
440
- recordRuleGaps(ruleId, gaps) {
453
+ recordRuleGaps(ruleId, gaps, relatedLocation) {
441
454
  if (!this.isExplainEnabled() || gaps.length === 0) {
442
455
  return;
443
456
  }
444
457
  const list = this.#gapsByRule.get(ruleId) ?? [];
445
- list.push(...gaps);
446
- this.#gapsByRule.set(ruleId, list);
447
- }
448
- recordFactExplain(params) {
449
- if (!this.isExplainEnabled()) {
450
- return;
451
- }
452
- if (params.gaps.length > 0) {
453
- const list = this.#factGapsByRule.get(params.ruleId) ?? [];
454
- for (const gap of params.gaps) {
455
- list.push({
456
- gap,
457
- ...(params.subjectLocation !== undefined
458
- ? { subjectLocation: params.subjectLocation }
459
- : {}),
460
- });
461
- }
462
- this.#factGapsByRule.set(params.ruleId, list);
463
- }
464
- if (params.localGaps.length > 0) {
465
- const list = this.#localGapsByRule.get(params.ruleId) ?? [];
466
- for (const localGap of params.localGaps) {
467
- list.push({
468
- localGap,
469
- ...(params.subjectLocation !== undefined
470
- ? { consumerLocation: params.subjectLocation }
471
- : {}),
472
- });
473
- }
474
- this.#localGapsByRule.set(params.ruleId, list);
475
- }
476
- }
477
- recordProviderEvaluation(params) {
478
- if (!this.isExplainEnabled()) {
479
- return;
458
+ for (const gap of gaps) {
459
+ list.push({
460
+ gap,
461
+ ...(relatedLocation !== undefined ? { relatedLocation } : {}),
462
+ });
480
463
  }
481
- const list = this.#providerEvaluationsByRule.get(params.ruleId) ?? [];
482
- list.push(params.evaluation);
483
- this.#providerEvaluationsByRule.set(params.ruleId, list);
464
+ this.#gapsByRule.set(ruleId, list);
484
465
  }
485
466
  #isItemLimitReached() {
486
467
  return (this.#truncated ||
@@ -512,6 +493,18 @@ export class ObservationCollector {
512
493
  }
513
494
  const mode = this.mode === "detail" ? "detail" : "summary";
514
495
  const totalMs = Math.max(0, performance.now() - this.#startedAt);
496
+ const lowering = {
497
+ functionsLowered: this.#lowering.functionsLowered,
498
+ verticesCreated: this.#lowering.verticesCreated,
499
+ durationMs: roundMs(this.#lowering.durationMs),
500
+ };
501
+ const irCache = { ...this.#irCache };
502
+ const summaryLookup = {
503
+ lookups: this.#summaryLookup.lookups,
504
+ hits: this.#summaryLookup.hits,
505
+ misses: this.#summaryLookup.misses,
506
+ hitsBySource: { ...this.#summaryLookup.hitsBySource },
507
+ };
515
508
  return {
516
509
  mode,
517
510
  phases: {
@@ -537,13 +530,9 @@ export class ObservationCollector {
537
530
  candidateCount: this.#candidateCount,
538
531
  checkpointCount: this.#checkpointCount,
539
532
  providers: [...this.#providers].toSorted(),
540
- nativeAdapters: [...this.#nativeAdapters],
541
- engineExtractions: Object.fromEntries([...this.#engineExtractions.entries()]
542
- .toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
543
- .map(([k, v]) => [
544
- k,
545
- { extractions: v.extractions, candidates: v.candidates },
546
- ])),
533
+ lowering,
534
+ irCache,
535
+ summaryLookup,
547
536
  ...(this.limit !== undefined ? { limit: this.limit } : {}),
548
537
  truncated: this.#truncated,
549
538
  droppedCount: this.#droppedCount,
@@ -558,7 +547,8 @@ export class ObservationCollector {
558
547
  const queries = this.#queriesByRule.get(params.ruleId) ?? [];
559
548
  const checkpoints = this.#checkpointsByRule.get(params.ruleId) ?? [];
560
549
  const matchingCandidates = this.#matchingCandidatesByRule.get(params.ruleId) ?? [];
561
- const inCandidates = matchingCandidates.length > 0;
550
+ const hasTargetCheckpoint = checkpoints.some((cp) => cp.at !== undefined && locationMatchesTarget(cp.at, target));
551
+ const inCandidates = matchingCandidates.length > 0 || hasTargetCheckpoint;
562
552
  const relevantCheckpoints = checkpoints
563
553
  .filter((cp) => cp.at !== undefined &&
564
554
  (locationMatchesTarget(cp.at, target) ||
@@ -631,65 +621,13 @@ export class ObservationCollector {
631
621
  relevantGaps.push(gap);
632
622
  }
633
623
  };
634
- for (const gap of this.#gapsByRule.get(params.ruleId) ?? []) {
635
- if (matchesLoc(gap.at) || (gap.at === undefined && inCandidates)) {
636
- pushGap(gap);
637
- }
638
- }
639
- for (const entry of this.#factGapsByRule.get(params.ruleId) ?? []) {
640
- if (matchesLoc(entry.gap.at) || matchesLoc(entry.subjectLocation)) {
624
+ for (const entry of this.#gapsByRule.get(params.ruleId) ?? []) {
625
+ if (matchesLoc(entry.gap.at) ||
626
+ matchesLoc(entry.relatedLocation) ||
627
+ (entry.gap.at === undefined && inCandidates)) {
641
628
  pushGap(entry.gap);
642
629
  }
643
630
  }
644
- const relevantLocalGaps = [];
645
- const seenLocalGapKeys = new Set();
646
- for (const entry of this.#localGapsByRule.get(params.ruleId) ?? []) {
647
- const lg = entry.localGap;
648
- if (matchesLoc(lg.gap.at) ||
649
- matchesLoc(lg.subjectLocation) ||
650
- matchesLoc(entry.consumerLocation) ||
651
- matchesLoc(lg.resolvedBy?.at)) {
652
- const key = JSON.stringify(lg);
653
- if (!seenLocalGapKeys.has(key)) {
654
- seenLocalGapKeys.add(key);
655
- relevantLocalGaps.push(lg);
656
- }
657
- }
658
- }
659
- const providerEvaluations = [];
660
- const seenEvalKeys = new Set();
661
- const activeProviderIds = new Set();
662
- const skippedProviderIds = new Set();
663
- for (const evalEntry of this.#providerEvaluationsByRule.get(params.ruleId) ?? []) {
664
- if (evalEntry.status === "skipped") {
665
- skippedProviderIds.add(evalEntry.providerId);
666
- }
667
- else {
668
- activeProviderIds.add(evalEntry.providerId);
669
- }
670
- const key = JSON.stringify([
671
- evalEntry.providerId,
672
- evalEntry.factTypeId ?? null,
673
- evalEntry.status,
674
- evalEntry.reason ?? null,
675
- evalEntry.subjectLocation?.file ?? null,
676
- evalEntry.subjectLocation?.start ?? null,
677
- ]);
678
- if (!seenEvalKeys.has(key)) {
679
- seenEvalKeys.add(key);
680
- providerEvaluations.push(evalEntry);
681
- }
682
- }
683
- for (const adapter of this.#configuredAdapters) {
684
- if (!activeProviderIds.has(adapter.id) &&
685
- !skippedProviderIds.has(adapter.id)) {
686
- providerEvaluations.push({
687
- providerId: adapter.id,
688
- status: "skipped",
689
- reason: "Not queried during rule execution.",
690
- });
691
- }
692
- }
693
631
  if (matchingDiagnostic) {
694
632
  status = "reported";
695
633
  summary = `Rule "${params.ruleId}" reported a diagnostic at ${formatTarget(target)}.`;
@@ -748,8 +686,6 @@ export class ObservationCollector {
748
686
  queries: querySteps,
749
687
  checkpoints: relevantCheckpoints,
750
688
  gaps: relevantGaps,
751
- localGaps: relevantLocalGaps,
752
- providerEvaluations,
753
689
  providers,
754
690
  cleanupPaths,
755
691
  };
package/dist/projects.js CHANGED
@@ -16,7 +16,10 @@ function canonicalEvidenceKey(evidence) {
16
16
  kind: item.kind,
17
17
  message: item.message,
18
18
  ...(item.at !== undefined ? { at: canonicalLocationKey(item.at) } : {}),
19
- ...(item.modelId !== undefined ? { modelId: item.modelId } : {}),
19
+ ...(item.summaryId !== undefined ? { summaryId: item.summaryId } : {}),
20
+ ...(item.summarySource !== undefined
21
+ ? { summarySource: item.summarySource }
22
+ : {}),
20
23
  ...(item.declaration !== undefined
21
24
  ? { declaration: item.declaration }
22
25
  : {}),