All files / services/validation shacl-batch-runner.ts

87.77% Statements 79/90
76.08% Branches 35/46
91.66% Functions 11/12
87.77% Lines 79/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456                                                                                                                                                                                                                                                                                                                                                                61x             61x             1x             1x               34x                 1x               20x   20x               22x                                           21x 21x   21x 21x   21x 21x   21x         21x       21x 21x     26x     21x   21x               21x           21x   21x   21x   21x 21x     21x   21x       21x 21x             21x                 21x       21x         21x                             21x 21x 21x 21x 21x 21x 21x 21x   21x         21x   21x 39x   39x 6x     33x   33x 27x       6x       21x           21x 21x   21x 27x 1x 1x     26x   26x               26x   27x 4x 4x 4x 4x 4x 4x     22x   22x 22x   22x         22x                               22x 22x     21x      
import * as vscode from 'vscode';
import { SH, Store } from '@faubulous/mentor-rdf';
import { IDocumentContextService } from '@src/services/document';
import { IDocumentContext } from '@src/services/document/document-context.interface';
import { getConfig } from '@src/utilities/vscode/config';
import { createEventLoopYielder } from '@src/utilities/scheduling';
import { ShaclValidationResult } from './shacl-validator-engine';
import { ShaclValidationPresenter } from './shacl-validation-presenter';
 
/**
 * Summary of a batch validation run over a set of workspace files.
 */
export interface ProfileValidationSummary {
	/**
	 * Number of files matched by the validated profile(s).
	 */
	matched: number;
	/**
	 * Number of matched files that were actually validated (i.e. had a loaded context).
	 */
	validated: number;
	/**
	 * Total number of validation issues across all validated files.
	 */
	issues: number;
	/**
	 * Number of error-severity results (`sh:Violation`) across all validated files.
	 */
	errors: number;
	/**
	 * Number of warning-severity results (`sh:Warning`) across all validated files.
	 */
	warnings: number;
	/**
	 * The validated files that had at least one issue.
	 */
	issueFiles: vscode.Uri[];
	/**
	 * Whether any shape graphs were available to validate against.
	 */
	hasShapes: boolean;
	/**
	 * Number of matched files skipped because their data graph exceeded the
	 * `mentor.shacl.maxGraphSize` limit.
	 */
	skipped: number;
	/**
	 * Whether the run stopped early because its cancellation token was triggered.
	 */
	cancelled?: boolean;
}
 
/**
 * Summary statistics of the most recent batch validation run, surfaced on the
 * validation dashboard in the settings panel.
 */
export interface ValidationRunStatistics {
	/**
	 * The number of files that were validated.
	 */
	validatedFiles: number;
 
	/**
	 * The number of matched files skipped because their data graph exceeded
	 * `mentor.shacl.maxGraphSize`.
	 */
	skippedFiles: number;
 
	/**
	 * The number of error-severity results (`sh:Violation`) summed across all
	 * validated files.
	 */
	errorCount: number;
 
	/**
	 * The number of warning-severity results (`sh:Warning`) summed across all
	 * validated files.
	 */
	warningCount: number;
 
	/**
	 * The wall-clock duration of the run in milliseconds.
	 */
	durationMs: number;
}
 
/**
 * Optional controls for a batch validation run. Progress and cancellation are owned
 * by the runner itself (surfaced on the validation status bar item), so callers only
 * choose whether the large-graph guard applies.
 */
export interface BatchValidationOptions {
	/**
	 * Whether to skip data graphs larger than `mentor.shacl.maxGraphSize`. Defaults to
	 * `true` for automatic runs (startup batch); explicit user-invoked commands pass
	 * `false` to validate every matched file regardless of size.
	 */
	skipLargeGraphs?: boolean;
}
 
/**
 * Internal controls threaded into the per-file batch loop.
 */
interface MatchingFilesOptions {
	/**
	 * Cancellation token checked between files; when triggered the batch stops early.
	 */
	token?: vscode.CancellationToken;
	/**
	 * Whether to skip data graphs larger than `mentor.shacl.maxGraphSize`.
	 */
	skipLargeGraphs?: boolean;
	/**
	 * Called after each processed file with the running completed count and the total
	 * number of files that will be processed, for status bar progress.
	 */
	onProgress?: (completed: number, total: number) => void;
}
 
/**
 * The collaborators a {@link ShaclBatchRunner} drives a batch run through.
 */
export interface ShaclBatchRunnerDependencies {
	/**
	 * Provides the loaded document contexts of the workspace files.
	 */
	contextService: IDocumentContextService;
	/**
	 * The store holding the combined data graphs (for the size guard).
	 */
	store: Store;
	/**
	 * The "Mentor Validation" log channel for per-file and summary messages.
	 */
	log: vscode.LogOutputChannel;
	/**
	 * Drives the validation status bar item (progress + spinner).
	 */
	presenter: ShaclValidationPresenter;
	/**
	 * Validates one document and publishes its diagnostics; supplied by the
	 * validation service.
	 */
	validateAndPublish: (uri: vscode.Uri, context: IDocumentContext, shapeGraphUris: string[]) => Promise<ShaclValidationResult>;
	/**
	 * Called once when the outermost batch finishes, so the service can fire a
	 * single coalesced `onDidValidate` event instead of one per file.
	 */
	onBatchEnd: () => void;
	/**
	 * Called when a matched file is skipped because its data graph exceeds
	 * `mentor.shacl.maxGraphSize`, so the service can record the skip for UI
	 * surfaces such as the validation code lens.
	 */
	onFileSkipped: (uri: vscode.Uri, triples: number, maxGraphSize: number) => void;
}
 
/**
 * Runs batch validations over sets of workspace files: drives the validation
 * status bar item (progress + summary), the "Mentor Validation" log, the
 * cancellation token that the status bar item / cancel command trigger and the
 * per-batch coalescing of `onDidValidate` events.
 */
export class ShaclBatchRunner {
	/**
	 * The cancellation source of the batch validation run currently in flight, or
	 * `undefined` when no batch is running. Triggered by {@link cancel}.
	 */
	private _activeCancellation?: vscode.CancellationTokenSource;
 
	/**
	 * Depth of nested batch runs. While greater than zero, per-file `onDidValidate`
	 * events are suppressed and coalesced into a single event fired when the outermost
	 * batch finishes — validating every file individually would otherwise trigger the
	 * definition tree / decoration / code-lens listeners once per file.
	 */
	private _batchDepth = 0;
 
	/**
	 * Summary statistics of the most recent finished batch run.
	 */
	private _statistics?: ValidationRunStatistics;
 
	constructor(private readonly _deps: ShaclBatchRunnerDependencies) { }
 
	/**
	 * Summary statistics of the most recent finished batch run, or `undefined`
	 * when no batch has run yet in this session.
	 */
	get statistics(): ValidationRunStatistics | undefined {
		return this._statistics;
	}
 
	/**
	 * Whether a batch validation run is currently in flight (and therefore cancellable).
	 */
	get isValidating(): boolean {
		return this._activeCancellation !== undefined;
	}
 
	/**
	 * Whether a batch is currently running, in which case per-file `onDidValidate`
	 * events are suppressed in favor of the coalesced end-of-batch event.
	 */
	get isBatchActive(): boolean {
		return this._batchDepth > 0;
	}
 
	/**
	 * Cancels the batch validation run currently in flight, if any. Invoked (after an
	 * explicit confirmation) by the `mentor.command.cancelValidation` command that the
	 * status bar item triggers.
	 */
	cancel(): void {
		this._activeCancellation?.cancel();
	}
 
	/**
	 * The configured maximum data-graph size (in triples) for automatic validation,
	 * or `0` when the guard is disabled. See `mentor.shacl.maxGraphSize`.
	 */
	getMaxGraphSize(): number {
		const value = getConfig('shacl').get<number>('maxGraphSize', 50000);
 
		return Number.isFinite(value) && value > 0 ? value : 0;
	}
 
	/**
	 * The number of triples in a document's combined data graph, counted from the
	 * store's indexes (no re-parse), so this is cheap even for large graphs.
	 */
	getDataGraphSize(context: IDocumentContext): number {
		return this._deps.store.getDataset(context.graphs, false).size;
	}
 
	/**
	 * Whether a document's combined data graph exceeds the given size limit. A limit
	 * of `0` disables the guard.
	 */
	exceedsGraphSizeLimit(context: IDocumentContext, maxGraphSize: number): boolean {
		return maxGraphSize > 0 && this.getDataGraphSize(context) > maxGraphSize;
	}
 
	/**
	 * Runs a batch validation over the matched files. A newly started batch cancels
	 * any batch still in flight.
	 */
	async run(
		label: string,
		files: ReadonlyArray<vscode.Uri>,
		hasShapes: boolean,
		selectShapes: (uri: vscode.Uri) => string[] | undefined,
		options?: BatchValidationOptions
	): Promise<ProfileValidationSummary> {
		this._activeCancellation?.cancel();
		this._activeCancellation?.dispose();
 
		const cancellation = new vscode.CancellationTokenSource();
		this._activeCancellation = cancellation;
 
		this._deps.presenter.showRunning('$(sync~spin) Validating…');
		this._deps.log.info(`-- Started validating ${label}...`);
 
		const startTime = performance.now();
 
		// Suppress per-file onDidValidate events for the duration of the batch and fire a
		// single coalesced event at the end, so the tree/decoration/code-lens listeners
		// refresh once instead of once per file.
		this._batchDepth++;
 
		let summary: ProfileValidationSummary | undefined;
 
		try {
			summary = await this._validateMatchingFiles(files, hasShapes, selectShapes, {
				token: cancellation.token,
				skipLargeGraphs: options?.skipLargeGraphs,
				onProgress: (completed, total) => this._deps.presenter.showRunning(`$(sync~spin) Validating: ${completed} of ${total} files...`),
			});
 
			const duration = Math.round(performance.now() - startTime);
 
			this._statistics = {
				validatedFiles: summary.validated,
				skippedFiles: summary.skipped,
				errorCount: summary.errors,
				warningCount: summary.warnings,
				durationMs: duration,
			};
 
			this._deps.log.info(
				`Validated ${summary.validated} of ${summary.matched} files in ${duration} ms`
				+ (summary.skipped > 0 ? `; ${summary.skipped} skipped` : '')
				+ (summary.cancelled ? ' (cancelled)' : '')
			);
 
			this._deps.log.info(`-- Ended ${label} validation.`);
 
			return summary;
		} finally {
			cancellation.dispose();
 
			Eif (this._activeCancellation === cancellation) {
				this._activeCancellation = undefined;
			}
 
			this._batchDepth--;
 
			Eif (this._batchDepth === 0) {
				// Leave a persistent summary of the outcome on the status bar, mirroring
				// the indexer's "Indexed N files" summary. On a failure, just end the
				// running state.
				if (summary) {
					this._showSummary(summary);
				} else E{
					this._deps.presenter.clearRunning();
				}
 
				// Coalesced refresh: notify once so the tree, decorations and code lenses
				// rebuild a single time for the whole batch instead of per file.
				this._deps.onBatchEnd();
			}
		}
	}
 
	/**
	 * Shows the persistent status bar summary of a finished batch run.
	 */
	private _showSummary(summary: ProfileValidationSummary): void {
		const text = `$(checklist) Validated ${summary.validated} files`
			+ (summary.skipped > 0 ? `; ${summary.skipped} skipped` : '')
			+ (summary.cancelled ? ' (cancelled)' : '');
 
		const tooltip = `SHACL validation: ${summary.matched} files matched by profiles, `
			+ `${summary.validated} validated, ${summary.skipped} skipped`
			+ (summary.cancelled ? ' (run cancelled)' : '') + '.'
			+ '\nClick to open the validation dashboard.';
 
		this._deps.presenter.showSummary(text, tooltip);
	}
 
	/**
	 * Validates the files for which `selectShapes` returns a non-empty shape graph
	 * list, aggregating the outcome. Matched files without a loaded context are
	 * counted but skipped (no diagnostics). The matched-and-loaded files are resolved
	 * up front so progress can report an accurate `n of total`.
	 */
	private async _validateMatchingFiles(
		files: ReadonlyArray<vscode.Uri>,
		hasShapes: boolean,
		selectShapes: (uri: vscode.Uri) => string[] | undefined,
		options?: MatchingFilesOptions
	): Promise<ProfileValidationSummary> {
		let matched = 0;
		let validated = 0;
		let issues = 0;
		let errors = 0;
		let warnings = 0;
		let skipped = 0;
		let cancelled = false;
		const issueFiles: vscode.Uri[] = [];
 
		const maxGraphSize = options?.skipLargeGraphs === false ? 0 : this.getMaxGraphSize();
 
		// Resolve the files that actually carry work (matched by a profile and with a loaded
		// context) so the progress denominator is exact. Matched-but-unloaded files are still
		// counted in `matched` but never processed, mirroring how the indexer skips files.
		const workItems: { uri: vscode.Uri; shapes: string[] }[] = [];
 
		for (const uri of files) {
			const shapes = selectShapes(uri);
 
			if (!shapes || shapes.length === 0) {
				continue;
			}
 
			matched++;
 
			if (this._deps.contextService.contexts[uri.toString()]) {
				workItems.push({ uri, shapes });
			} else {
				// Matched by a profile but never validated because it has no loaded context
				// (e.g. not indexed). Log it so the output accounts for every matched file.
				this._deps.log.info(`Skipped ${uri.toString()}: no document context (not indexed).`);
			}
		}
 
		const total = workItems.length;
 
		// `shacl-engine`'s validate() is a CPU-bound loop that only yields microtasks, so a
		// tight batch would starve the extension-host event loop (frozen status bar, webviews
		// and trees). The budget yields a macrotask between files whenever enough wall-clock
		// time has elapsed, letting VS Code paint and process queued work.
		const eventLoop = createEventLoopYielder();
		let completed = 0;
 
		for (const { uri, shapes } of workItems) {
			if (options?.token?.isCancellationRequested) {
				cancelled = true;
				break;
			}
 
			const context = this._deps.contextService.contexts[uri.toString()];
 
			Iif (!context) {
				continue;
			}
 
			// Skip pathologically large data graphs (e.g. imported vocabularies): validating
			// them can take many seconds each and is rarely intended automatically. Explicit
			// single-document validation (validateDocument) never reaches this loop, and the
			// user-invoked profile commands opt out via `skipLargeGraphs: false` (maxGraphSize 0).
			const triples = maxGraphSize > 0 ? this.getDataGraphSize(context) : 0;
 
			if (maxGraphSize > 0 && triples > maxGraphSize) {
				skipped++;
				completed++;
				options?.onProgress?.(completed, total);
				this._deps.log.info(`Skipped ${uri.toString()}: data graph exceeds mentor.shacl.maxGraphSize (${maxGraphSize}).`);
				this._deps.onFileSkipped(uri, triples, maxGraphSize);
				continue;
			}
 
			await eventLoop.yieldIfBusy();
 
			try {
				const result = await this._deps.validateAndPublish(uri, context, shapes);
 
				validated++;
 
				// Count every published result so the reported total matches the Problems
				// panel. A file can conform (no sh:Violation) yet still publish Warning/Info
				// diagnostics, which must be counted as issues too.
				Iif (result.results.length > 0) {
					issueFiles.push(uri);
					issues += result.results.length;
 
					for (const entry of result.results) {
						if (entry.severity === SH.Violation) {
							errors++;
						} else if (entry.severity === SH.Warning) {
							warnings++;
						}
					}
				}
			} catch (error) {
				this._deps.log.error(`SHACL validation failed for ${uri.toString()}: ${error}`);
			}
 
			completed++;
			options?.onProgress?.(completed, total);
		}
 
		return { matched, validated, issues, errors, warnings, issueFiles, hasShapes, skipped, cancelled };
	}
}