@boringparts/verule 0.0.0-stage → 0.1.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.
@@ -0,0 +1,104 @@
1
+ import { dirname, resolve } from "node:path";
2
+ import { text } from "@ykdz/cli-contract";
3
+ import { z } from "zod";
4
+ import { loadProjectConfigWithDependencies } from "./config-loader.js";
5
+ import { REPORT_SCHEMA_VERSION, watchProject } from "./index.js";
6
+ import { appendGapLines, checkReportSchema, factConflictDetailsSchema, formatDiagnosticsLines, toWireFactConflict, toWireReport, } from "./report-wire.js";
7
+ export const recomputationScopeSchema = z.object({
8
+ recomputedProjects: z.array(z.string()),
9
+ reusedProjects: z.array(z.string()),
10
+ recomputedFiles: z.array(z.string()),
11
+ reusedFiles: z.array(z.string()),
12
+ removedFiles: z.array(z.string()),
13
+ invalidatedBy: z.array(z.string()),
14
+ });
15
+ export const memoryStatsSchema = z.object({
16
+ hostRssBytes: z.number().int().nonnegative(),
17
+ engineRssBytes: z.number().int().nonnegative().optional(),
18
+ });
19
+ export const watchReportRecordSchema = checkReportSchema.extend({
20
+ sequence: z.number().int().positive(),
21
+ trigger: z.enum(["initial", "change"]),
22
+ snapshotId: z.number().int().positive(),
23
+ recomputation: recomputationScopeSchema,
24
+ memory: memoryStatsSchema,
25
+ });
26
+ export const watchFailureRecordSchema = z.object({
27
+ reportVersion: z.literal(REPORT_SCHEMA_VERSION),
28
+ sequence: z.number().int().positive(),
29
+ trigger: z.enum(["initial", "change"]),
30
+ code: z.string(),
31
+ message: z.string(),
32
+ conflict: factConflictDetailsSchema.optional(),
33
+ });
34
+ export function formatWatchReportText(record) {
35
+ const lines = formatDiagnosticsLines(record);
36
+ appendGapLines(lines, record.gaps);
37
+ const gapSuffix = record.gaps.length > 0 ? ` (${record.gaps.length} gap(s))` : "";
38
+ const summary = record.diagnostics.length === 0
39
+ ? `[watch #${record.sequence}] No violations found across ${record.projects.length} project(s)${gapSuffix}.`
40
+ : `[watch #${record.sequence}] Found ${record.diagnostics.length} violation(s)${gapSuffix}.`;
41
+ if (lines.length === 0) {
42
+ return text.line(summary);
43
+ }
44
+ return text.fragment(`${[...lines, summary].join("\n")}\n`);
45
+ }
46
+ /**
47
+ * Streams complete analysis reports and single-round failures for `verule watch`,
48
+ * preserving the long-lived analysis session across edits until aborted.
49
+ */
50
+ export async function* streamWatchEvents(workingDirectory, configOption, signal, outcome) {
51
+ const events = watchProject({
52
+ cwd: workingDirectory,
53
+ ...(signal !== undefined ? { signal } : {}),
54
+ ...(configOption !== undefined
55
+ ? { watchFiles: [resolve(workingDirectory, configOption)] }
56
+ : {}),
57
+ loadConfig: async () => {
58
+ const loaded = await loadProjectConfigWithDependencies(workingDirectory, configOption);
59
+ return {
60
+ config: loaded.config,
61
+ watchFiles: loaded.watchFiles,
62
+ adapterResolutionRoot: dirname(loaded.configPath),
63
+ };
64
+ },
65
+ });
66
+ for await (const event of events) {
67
+ if (event.kind === "report") {
68
+ const wire = toWireReport(event.report);
69
+ yield outcome.record.report({
70
+ ...wire,
71
+ sequence: event.sequence,
72
+ trigger: event.trigger,
73
+ snapshotId: event.snapshotId,
74
+ recomputation: {
75
+ recomputedProjects: [...event.recomputation.recomputedProjects],
76
+ reusedProjects: [...event.recomputation.reusedProjects],
77
+ recomputedFiles: [...event.recomputation.recomputedFiles],
78
+ reusedFiles: [...event.recomputation.reusedFiles],
79
+ removedFiles: [...event.recomputation.removedFiles],
80
+ invalidatedBy: [...event.recomputation.invalidatedBy],
81
+ },
82
+ memory: {
83
+ hostRssBytes: event.memory.hostRssBytes,
84
+ ...(event.memory.engineRssBytes !== undefined
85
+ ? { engineRssBytes: event.memory.engineRssBytes }
86
+ : {}),
87
+ },
88
+ });
89
+ }
90
+ else {
91
+ yield outcome.record.failed({
92
+ reportVersion: REPORT_SCHEMA_VERSION,
93
+ sequence: event.sequence,
94
+ trigger: event.trigger,
95
+ code: event.code,
96
+ message: event.message,
97
+ ...(event.conflict !== undefined
98
+ ? { conflict: toWireFactConflict(event.conflict) }
99
+ : {}),
100
+ });
101
+ }
102
+ }
103
+ return outcome.streamSuccess();
104
+ }
@@ -0,0 +1,59 @@
1
+ import { type AnalysisSession, type CheckReport, type FactConflictDetails, type MemoryStats, type ObserveLimitInput, type ObserveMode, type ObserveOptions, type RecomputationScope, type SourceTarget, type VeruleConfig, type VeruleErrorCode, type WatchConfigLoadResult, type WatchEvent, type WatchProjectOptions } from "./types.ts";
2
+ export type WorkspaceFailureInfo = {
3
+ readonly code: VeruleErrorCode;
4
+ readonly message: string;
5
+ readonly conflict?: FactConflictDetails;
6
+ };
7
+ export type WorkspaceControllerOptions = {
8
+ readonly cwd: string;
9
+ readonly config?: VeruleConfig;
10
+ readonly adapterResolutionRoot?: string;
11
+ readonly loadConfig?: () => Promise<WatchConfigLoadResult>;
12
+ readonly watchFiles?: readonly string[];
13
+ readonly observe?: ObserveMode;
14
+ readonly observeLimit?: ObserveLimitInput;
15
+ readonly debounceMs?: number;
16
+ readonly onBatchReady?: () => void;
17
+ };
18
+ export type WorkspaceCheckParams = {
19
+ readonly ruleIds?: readonly string[];
20
+ readonly at?: string | SourceTarget;
21
+ readonly observe?: ObserveMode | ObserveOptions;
22
+ readonly observeLimit?: ObserveLimitInput;
23
+ };
24
+ export declare function toExecutionErrorInfo(err: unknown): WorkspaceFailureInfo;
25
+ export declare function initialRecomputationScope(session: AnalysisSession): RecomputationScope;
26
+ export type WorkspaceApplyResult = {
27
+ readonly applied: false;
28
+ } | {
29
+ readonly applied: true;
30
+ readonly trigger: "initial" | "change";
31
+ readonly session: AnalysisSession;
32
+ readonly recomputation: RecomputationScope;
33
+ readonly memory: MemoryStats;
34
+ };
35
+ /**
36
+ * Shared incremental workspace controller used by both `watchProject` and `startSharedSession`.
37
+ * Serializes filesystem watcher updates, synchronous disk checks, config reloads, and checks
38
+ * on a single underlying `AnalysisSession`.
39
+ */
40
+ export declare function createWorkspaceController(options: WorkspaceControllerOptions): {
41
+ absCwd: string;
42
+ readonly session: AnalysisSession | undefined;
43
+ syncWatchers: () => void;
44
+ syncAdditionalInputs: () => void;
45
+ openInitial: () => Promise<{
46
+ session: AnalysisSession;
47
+ recomputation: RecomputationScope;
48
+ memory: MemoryStats;
49
+ }>;
50
+ applyPendingBatch: () => Promise<WorkspaceApplyResult>;
51
+ checkLatest: (params?: WorkspaceCheckParams) => Promise<CheckReport>;
52
+ close: () => Promise<void>;
53
+ };
54
+ /**
55
+ * Watches a project directory (and any external config dependency files), updating an
56
+ * {@link AnalysisSession} incrementally and yielding a complete report or failure record
57
+ * after each initial check and subsequent save.
58
+ */
59
+ export declare function watchProject(options: WatchProjectOptions): AsyncGenerator<WatchEvent, void, void>;