pi-lens 3.6.2 → 3.6.4

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.
Files changed (207) hide show
  1. package/CHANGELOG.md +10 -2
  2. package/package.json +4 -4
  3. package/tsconfig.json +1 -1
  4. package/clients/__tests__/file-time.test.js +0 -216
  5. package/clients/__tests__/file-time.test.ts +0 -276
  6. package/clients/__tests__/format-service.test.js +0 -245
  7. package/clients/__tests__/format-service.test.ts +0 -339
  8. package/clients/__tests__/formatters.test.js +0 -271
  9. package/clients/__tests__/formatters.test.ts +0 -401
  10. package/clients/agent-behavior-client.js +0 -110
  11. package/clients/agent-behavior-client.test.js +0 -94
  12. package/clients/agent-behavior-client.test.ts +0 -116
  13. package/clients/amain-types.js +0 -164
  14. package/clients/architect-client.js +0 -291
  15. package/clients/ast-grep-client.js +0 -253
  16. package/clients/ast-grep-parser.js +0 -84
  17. package/clients/ast-grep-rule-manager.js +0 -89
  18. package/clients/ast-grep-types.js +0 -9
  19. package/clients/auto-loop.js +0 -131
  20. package/clients/biome-client.js +0 -420
  21. package/clients/biome-client.test.js +0 -144
  22. package/clients/biome-client.test.ts +0 -163
  23. package/clients/cache/rule-cache.js +0 -72
  24. package/clients/cache-manager.js +0 -245
  25. package/clients/cache-manager.test.js +0 -197
  26. package/clients/cache-manager.test.ts +0 -299
  27. package/clients/complexity-client.js +0 -675
  28. package/clients/complexity-client.test.js +0 -234
  29. package/clients/complexity-client.test.ts +0 -255
  30. package/clients/config-validator.js +0 -465
  31. package/clients/dependency-checker.js +0 -325
  32. package/clients/dependency-checker.test.js +0 -60
  33. package/clients/dependency-checker.test.ts +0 -71
  34. package/clients/dispatch/__tests__/autofix-integration.test.js +0 -245
  35. package/clients/dispatch/__tests__/autofix-integration.test.ts +0 -300
  36. package/clients/dispatch/__tests__/runner-registration.test.js +0 -234
  37. package/clients/dispatch/__tests__/runner-registration.test.ts +0 -286
  38. package/clients/dispatch/debug.log +0 -1
  39. package/clients/dispatch/dispatcher.edge.test.js +0 -82
  40. package/clients/dispatch/dispatcher.edge.test.ts +0 -100
  41. package/clients/dispatch/dispatcher.format.test.js +0 -46
  42. package/clients/dispatch/dispatcher.format.test.ts +0 -58
  43. package/clients/dispatch/dispatcher.inline.test.js +0 -74
  44. package/clients/dispatch/dispatcher.inline.test.ts +0 -93
  45. package/clients/dispatch/dispatcher.js +0 -381
  46. package/clients/dispatch/dispatcher.test.js +0 -116
  47. package/clients/dispatch/dispatcher.test.ts +0 -149
  48. package/clients/dispatch/integration.js +0 -108
  49. package/clients/dispatch/plan.js +0 -183
  50. package/clients/dispatch/runners/architect.js +0 -83
  51. package/clients/dispatch/runners/architect.test.js +0 -138
  52. package/clients/dispatch/runners/architect.test.ts +0 -162
  53. package/clients/dispatch/runners/ast-grep-napi.js +0 -405
  54. package/clients/dispatch/runners/ast-grep-napi.test.js +0 -107
  55. package/clients/dispatch/runners/ast-grep-napi.test.ts +0 -129
  56. package/clients/dispatch/runners/ast-grep.js +0 -157
  57. package/clients/dispatch/runners/biome.js +0 -55
  58. package/clients/dispatch/runners/config-validation.js +0 -67
  59. package/clients/dispatch/runners/go-vet.js +0 -48
  60. package/clients/dispatch/runners/index.js +0 -47
  61. package/clients/dispatch/runners/lsp.js +0 -102
  62. package/clients/dispatch/runners/oxlint.js +0 -67
  63. package/clients/dispatch/runners/oxlint.test.js +0 -230
  64. package/clients/dispatch/runners/oxlint.test.ts +0 -303
  65. package/clients/dispatch/runners/pyright.js +0 -100
  66. package/clients/dispatch/runners/pyright.test.js +0 -98
  67. package/clients/dispatch/runners/pyright.test.ts +0 -121
  68. package/clients/dispatch/runners/python-slop.js +0 -97
  69. package/clients/dispatch/runners/python-slop.test.js +0 -203
  70. package/clients/dispatch/runners/python-slop.test.ts +0 -298
  71. package/clients/dispatch/runners/ruff.js +0 -48
  72. package/clients/dispatch/runners/rust-clippy.js +0 -102
  73. package/clients/dispatch/runners/scan_codebase.test.js +0 -89
  74. package/clients/dispatch/runners/scan_codebase.test.ts +0 -105
  75. package/clients/dispatch/runners/shellcheck.js +0 -147
  76. package/clients/dispatch/runners/shellcheck.test.js +0 -98
  77. package/clients/dispatch/runners/shellcheck.test.ts +0 -129
  78. package/clients/dispatch/runners/similarity.js +0 -230
  79. package/clients/dispatch/runners/spellcheck.js +0 -106
  80. package/clients/dispatch/runners/spellcheck.test.js +0 -158
  81. package/clients/dispatch/runners/spellcheck.test.ts +0 -214
  82. package/clients/dispatch/runners/tree-sitter.js +0 -246
  83. package/clients/dispatch/runners/ts-lsp.js +0 -125
  84. package/clients/dispatch/runners/ts-slop.js +0 -113
  85. package/clients/dispatch/runners/type-safety.js +0 -142
  86. package/clients/dispatch/runners/utils/diagnostic-parsers.js +0 -134
  87. package/clients/dispatch/runners/utils/runner-helpers.js +0 -115
  88. package/clients/dispatch/runners/utils.js +0 -51
  89. package/clients/dispatch/runners/yaml-rule-parser.js +0 -360
  90. package/clients/dispatch/types.js +0 -16
  91. package/clients/dispatch/utils/format-utils.js +0 -44
  92. package/clients/dogfood.test.js +0 -201
  93. package/clients/dogfood.test.ts +0 -269
  94. package/clients/file-kinds.js +0 -177
  95. package/clients/file-kinds.test.js +0 -169
  96. package/clients/file-kinds.test.ts +0 -210
  97. package/clients/file-time.js +0 -152
  98. package/clients/file-utils.js +0 -40
  99. package/clients/fix-scanners.js +0 -204
  100. package/clients/format-service.js +0 -184
  101. package/clients/formatters.js +0 -488
  102. package/clients/go-client.js +0 -203
  103. package/clients/go-client.test.js +0 -127
  104. package/clients/go-client.test.ts +0 -143
  105. package/clients/installer/index.js +0 -403
  106. package/clients/interviewer-templates.js +0 -75
  107. package/clients/interviewer.js +0 -173
  108. package/clients/jscpd-client.js +0 -196
  109. package/clients/jscpd-client.test.js +0 -127
  110. package/clients/jscpd-client.test.ts +0 -145
  111. package/clients/knip-client.js +0 -239
  112. package/clients/knip-client.test.js +0 -112
  113. package/clients/knip-client.test.ts +0 -128
  114. package/clients/latency-logger.js +0 -40
  115. package/clients/lsp/__tests__/client.test.js +0 -310
  116. package/clients/lsp/__tests__/client.test.ts +0 -412
  117. package/clients/lsp/__tests__/config.test.js +0 -167
  118. package/clients/lsp/__tests__/config.test.ts +0 -217
  119. package/clients/lsp/__tests__/error-recovery.test.js +0 -213
  120. package/clients/lsp/__tests__/error-recovery.test.ts +0 -279
  121. package/clients/lsp/__tests__/integration.test.js +0 -127
  122. package/clients/lsp/__tests__/integration.test.ts +0 -160
  123. package/clients/lsp/__tests__/launch.test.js +0 -313
  124. package/clients/lsp/__tests__/launch.test.ts +0 -394
  125. package/clients/lsp/__tests__/server.test.js +0 -259
  126. package/clients/lsp/__tests__/server.test.ts +0 -332
  127. package/clients/lsp/__tests__/service.test.js +0 -438
  128. package/clients/lsp/__tests__/service.test.ts +0 -530
  129. package/clients/lsp/client.js +0 -350
  130. package/clients/lsp/config.js +0 -112
  131. package/clients/lsp/index.js +0 -318
  132. package/clients/lsp/installer/index.js +0 -391
  133. package/clients/lsp/interactive-install.js +0 -221
  134. package/clients/lsp/language.js +0 -170
  135. package/clients/lsp/launch.js +0 -329
  136. package/clients/lsp/lsp/launch.js +0 -116
  137. package/clients/lsp/lsp/server.js +0 -532
  138. package/clients/lsp/lsp-index.js +0 -10
  139. package/clients/lsp/path-utils.js +0 -5
  140. package/clients/lsp/server.js +0 -725
  141. package/clients/lsp/test-py-spawn/requirements.txt +0 -1
  142. package/clients/lsp/test-py-spawn/test.py +0 -3
  143. package/clients/lsp/test-py-svc/requirements.txt +0 -1
  144. package/clients/lsp/test-py-svc/test.py +0 -3
  145. package/clients/lsp/test-python-project/requirements.txt +0 -1
  146. package/clients/lsp/test-python-project/test.py +0 -5
  147. package/clients/metrics-client.js +0 -107
  148. package/clients/metrics-client.test.js +0 -128
  149. package/clients/metrics-client.test.ts +0 -163
  150. package/clients/metrics-history.js +0 -367
  151. package/clients/path-utils.js +0 -142
  152. package/clients/pipeline.js +0 -272
  153. package/clients/production-readiness.js +0 -522
  154. package/clients/project-index.js +0 -255
  155. package/clients/project-metadata.js +0 -531
  156. package/clients/ruff-client.js +0 -325
  157. package/clients/ruff-client.test.js +0 -132
  158. package/clients/ruff-client.test.ts +0 -153
  159. package/clients/rules-scanner.js +0 -97
  160. package/clients/runner-tracker.js +0 -152
  161. package/clients/rust-client.js +0 -205
  162. package/clients/rust-client.test.js +0 -108
  163. package/clients/rust-client.test.ts +0 -130
  164. package/clients/safe-spawn-async.js +0 -163
  165. package/clients/safe-spawn.js +0 -241
  166. package/clients/sanitize.js +0 -291
  167. package/clients/sanitize.test.js +0 -177
  168. package/clients/sanitize.test.ts +0 -223
  169. package/clients/scan-architectural-debt.js +0 -167
  170. package/clients/scan-utils.js +0 -83
  171. package/clients/secrets-scanner.js +0 -119
  172. package/clients/secrets-scanner.test.js +0 -100
  173. package/clients/secrets-scanner.test.ts +0 -113
  174. package/clients/sg-runner.js +0 -292
  175. package/clients/state-matrix.js +0 -160
  176. package/clients/subprocess-client.js +0 -65
  177. package/clients/symbol-types.js +0 -5
  178. package/clients/test-runner-client.js +0 -523
  179. package/clients/test-runner-client.test.js +0 -192
  180. package/clients/test-runner-client.test.ts +0 -253
  181. package/clients/test-utils.js +0 -27
  182. package/clients/test-utils.ts +0 -36
  183. package/clients/todo-scanner.js +0 -200
  184. package/clients/todo-scanner.test.js +0 -301
  185. package/clients/todo-scanner.test.ts +0 -352
  186. package/clients/tool-availability.js +0 -207
  187. package/clients/tree-sitter-client.js +0 -601
  188. package/clients/tree-sitter-query-loader.js +0 -355
  189. package/clients/tree-sitter-symbol-extractor.js +0 -289
  190. package/clients/ts-service.js +0 -129
  191. package/clients/type-coverage-client.js +0 -127
  192. package/clients/type-coverage-client.test.js +0 -105
  193. package/clients/type-coverage-client.test.ts +0 -125
  194. package/clients/type-safety-client.js +0 -138
  195. package/clients/types.js +0 -11
  196. package/clients/typescript-client.codefix.test.js +0 -157
  197. package/clients/typescript-client.codefix.test.ts +0 -186
  198. package/clients/typescript-client.js +0 -509
  199. package/clients/typescript-client.test.js +0 -105
  200. package/clients/typescript-client.test.ts +0 -126
  201. package/commands/booboo.js +0 -1007
  202. package/commands/fix-from-booboo.js +0 -398
  203. package/commands/fix-simplified.js +0 -618
  204. package/commands/rate.js +0 -281
  205. package/commands/rate.test.js +0 -119
  206. package/commands/rate.test.ts +0 -131
  207. package/commands/refactor.js +0 -130
@@ -1,134 +0,0 @@
1
- /**
2
- * Shared diagnostic output parsers for pi-lens runners
3
- *
4
- * Common patterns for parsing tool output into standardized diagnostics.
5
- * Supports the common `file:line:col: message` format used by most linters.
6
- */
7
- /**
8
- * Create a parser for line-based tool output.
9
- * Common format: file:line:col: message (with variations)
10
- */
11
- export function createLineParser(config) {
12
- return (raw, filePath) => {
13
- const diagnostics = [];
14
- // Optionally strip ANSI codes (for tools that output colored text)
15
- const clean = config.stripAnsi !== false
16
- ? raw.replace(/\x1b\[[0-9;]*m/g, "")
17
- : raw;
18
- const lines = clean.split("\n").filter((l) => l.trim());
19
- for (const line of lines) {
20
- const match = line.match(config.regex);
21
- if (!match)
22
- continue;
23
- const lineNum = parseInt(match[2], 10);
24
- const colNum = parseInt(match[3], 10);
25
- const severity = config.getSeverity
26
- ? config.getSeverity(line, match)
27
- : "warning";
28
- const fixable = typeof config.fixable === "function"
29
- ? config.fixable(match)
30
- : config.fixable ?? false;
31
- diagnostics.push({
32
- id: config.generateId(match),
33
- message: config.extractMessage(match),
34
- filePath,
35
- line: lineNum,
36
- column: colNum,
37
- severity,
38
- semantic: severity === "error" ? "blocking" : "warning",
39
- tool: config.tool,
40
- rule: config.extractRule?.(match),
41
- fixable,
42
- });
43
- }
44
- return diagnostics;
45
- };
46
- }
47
- // =============================================================================
48
- // PRE-BUILT PARSERS FOR COMMON TOOLS
49
- // =============================================================================
50
- /**
51
- * Parse Ruff output: file:line:col: CODE message
52
- */
53
- export const parseRuffOutput = createLineParser({
54
- tool: "ruff",
55
- regex: /^(.+?):(\d+):(\d+):\s*(\w+)\s*(.+)/,
56
- extractMessage: (m) => `${m[4]}: ${m[5]}`, // CODE: message
57
- extractRule: (m) => m[4],
58
- generateId: (m) => `ruff-${m[4]}`,
59
- fixable: true, // Ruff can fix most issues
60
- });
61
- /**
62
- * Parse Go vet output: file:line:col: message
63
- */
64
- export const parseGoVetOutput = createLineParser({
65
- tool: "go-vet",
66
- regex: /^(.+?):(\d+):(\d+):\s*(.+)/,
67
- extractMessage: (m) => m[4],
68
- generateId: (m) => `go-vet-${m[2]}`,
69
- });
70
- /**
71
- * Parse Biome output: file:line:col message (category)
72
- * With autofix support for fix suggestions
73
- */
74
- export function createBiomeParser(autofix = false) {
75
- return createLineParser({
76
- tool: "biome",
77
- regex: /^(.+?):(\d+):(\d+)\s+(.+?)\s*\((.+?)\)/,
78
- extractMessage: (m) => `${m[5]}: ${m[4]}`, // category: message
79
- extractRule: (m) => m[5],
80
- generateId: (m) => `biome-${m[2]}-${m[5]}`,
81
- getSeverity: (line) => (line.includes("error") ? "error" : "warning"),
82
- fixable: true,
83
- });
84
- }
85
- // Backward-compatible default biome parser
86
- export const parseBiomeOutput = createBiomeParser(false);
87
- // =============================================================================
88
- // GENERIC PARSER FACTORY
89
- // =============================================================================
90
- /**
91
- * Create a simple parser for tools using standard file:line:col format.
92
- * Format variations: :line:col:, line:col, (line,col), etc.
93
- */
94
- export function createSimpleParser(tool, options = {}) {
95
- const sep = options.separator ?? ":";
96
- const severity = options.severity ?? "warning";
97
- const fixable = options.fixable ?? false;
98
- // Build regex based on separator type
99
- const escapedSep = sep === " " ? "\\s+" : escapeRegExp(sep);
100
- const regex = options.includesFileName
101
- ? new RegExp(`^(.+?)${escapedSep}(\\d+)${escapedSep}(\\d+)${escapedSep}(.+)`)
102
- : new RegExp(`^(\\d+)${escapedSep}(\\d+)${escapedSep}(.+)`);
103
- return (raw, filePath) => {
104
- const diagnostics = [];
105
- const lines = raw.split("\n").filter((l) => l.trim());
106
- for (const line of lines) {
107
- const match = line.match(regex);
108
- if (!match)
109
- continue;
110
- const lineNum = options.includesFileName
111
- ? parseInt(match[2], 10)
112
- : parseInt(match[1], 10);
113
- const colNum = options.includesFileName
114
- ? parseInt(match[3], 10)
115
- : parseInt(match[2], 10);
116
- const message = options.includesFileName ? match[4] : match[3];
117
- diagnostics.push({
118
- id: `${tool}-${lineNum}`,
119
- message: message.trim(),
120
- filePath,
121
- line: lineNum,
122
- column: colNum,
123
- severity,
124
- semantic: severity === "error" ? "blocking" : "warning",
125
- tool,
126
- fixable,
127
- });
128
- }
129
- return diagnostics;
130
- };
131
- }
132
- function escapeRegExp(string) {
133
- return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
134
- }
@@ -1,115 +0,0 @@
1
- /**
2
- * Shared runner utilities for pi-lens dispatch system
3
- *
4
- * Extracted common patterns from multiple runners to reduce duplication:
5
- * - Venv-aware command finders
6
- * - Availability checkers with caching
7
- * - Config file finders
8
- */
9
- import * as fs from "node:fs";
10
- import * as path from "node:path";
11
- import { safeSpawn } from "../../../safe-spawn.js";
12
- /**
13
- * Find a command in venv first, then fall back to global.
14
- * Checks common venv locations (.venv, venv) before trying global.
15
- */
16
- export function createVenvFinder(command, windowsExt = "", quoteWindows = false) {
17
- return (cwd) => {
18
- const venvPaths = [
19
- `.venv/bin/${command}`,
20
- `venv/bin/${command}`,
21
- `.venv/Scripts/${command}${windowsExt}`,
22
- `venv/Scripts/${command}${windowsExt}`,
23
- ];
24
- for (const venvPath of venvPaths) {
25
- const fullPath = path.join(cwd, venvPath);
26
- if (fs.existsSync(fullPath)) {
27
- return quoteWindows && windowsExt
28
- ? `"${fullPath}"`
29
- : fullPath;
30
- }
31
- }
32
- // Fall back to global
33
- return command;
34
- };
35
- }
36
- /**
37
- * Create a cached availability checker for a command.
38
- * The checker will look for the command in venv first, then global.
39
- */
40
- export function createAvailabilityChecker(command, windowsExt = "") {
41
- const cache = {
42
- available: null,
43
- command: null,
44
- };
45
- const findCommand = createVenvFinder(command, windowsExt, true);
46
- function isAvailable(cwd) {
47
- if (cache.available !== null)
48
- return cache.available;
49
- const cmd = findCommand(cwd || process.cwd());
50
- const result = safeSpawn(cmd, ["--version"], {
51
- timeout: 5000,
52
- });
53
- cache.available = !result.error && result.status === 0;
54
- if (cache.available) {
55
- cache.command = cmd;
56
- }
57
- return cache.available;
58
- }
59
- function getCommand() {
60
- return cache.command;
61
- }
62
- return { isAvailable, getCommand };
63
- }
64
- // =============================================================================
65
- // CONFIG FILE FINDER FACTORY
66
- // =============================================================================
67
- /**
68
- * Create a config file finder for rule directories.
69
- * Common pattern used by slop runners and similar tools.
70
- */
71
- export function createConfigFinder(ruleDirName) {
72
- return (cwd) => {
73
- // Check for local config first
74
- const localPath = path.join(cwd, "rules", ruleDirName, ".sgconfig.yml");
75
- if (fs.existsSync(localPath)) {
76
- return localPath;
77
- }
78
- // Fall back to extension rules
79
- const extensionPaths = [
80
- `rules/${ruleDirName}/.sgconfig.yml`,
81
- `../rules/${ruleDirName}/.sgconfig.yml`,
82
- ];
83
- for (const candidate of extensionPaths) {
84
- const fullPath = path.resolve(cwd, candidate);
85
- if (fs.existsSync(fullPath)) {
86
- return fullPath;
87
- }
88
- }
89
- return undefined;
90
- };
91
- }
92
- // =============================================================================
93
- // SHARED AST-GREP AVAILABILITY
94
- // =============================================================================
95
- // Shared sg availability cache across all slop runners
96
- let sgAvailable = null;
97
- /**
98
- * Check if ast-grep CLI is available (shared cache)
99
- */
100
- export function isSgAvailable() {
101
- if (sgAvailable !== null)
102
- return sgAvailable;
103
- const check = safeSpawn("npx", ["sg", "--version"], {
104
- timeout: 5000,
105
- });
106
- sgAvailable = !check.error && check.status === 0;
107
- return sgAvailable;
108
- }
109
- // =============================================================================
110
- // PRE-BUILT CHECKERS FOR COMMON TOOLS
111
- // =============================================================================
112
- export const pyright = createAvailabilityChecker("pyright", ".exe");
113
- export const ruff = createAvailabilityChecker("ruff", ".exe");
114
- export const biome = createAvailabilityChecker("biome");
115
- export const sg = { isAvailable: isSgAvailable, getCommand: () => "npx" };
@@ -1,51 +0,0 @@
1
- /**
2
- * Shared utilities for runners
3
- */
4
- import * as fs from "node:fs";
5
- import { safeSpawn } from "../../safe-spawn.js";
6
- /**
7
- * Read file content, returning undefined if it can't be read
8
- */
9
- export function readFileContent(filePath) {
10
- try {
11
- return fs.readFileSync(filePath, "utf-8");
12
- }
13
- catch {
14
- return undefined;
15
- }
16
- }
17
- /**
18
- * Check if a command is available
19
- */
20
- export function isCommandAvailable(command) {
21
- try {
22
- const result = safeSpawn(command, ["--version"], {
23
- timeout: 5000,
24
- });
25
- return result.status === 0;
26
- }
27
- catch {
28
- return false;
29
- }
30
- }
31
- /**
32
- * Parse common tool output line into a diagnostic
33
- * Format: file:line:col message (category)
34
- */
35
- export function parseToolLine(line, toolName, filePath) {
36
- const match = line.match(/^(.+?):(\d+):(\d+)\s+(.+?)\s*\((.+?)\)/);
37
- if (!match)
38
- return null;
39
- return {
40
- id: `${toolName}-${match[2]}-${match[5]}`,
41
- message: `${match[5]}: ${match[4]}`,
42
- filePath,
43
- line: parseInt(match[2], 10),
44
- column: parseInt(match[3], 10),
45
- severity: line.includes("error") ? "error" : "warning",
46
- semantic: "warning",
47
- tool: toolName,
48
- rule: match[5],
49
- fixable: false,
50
- };
51
- }
@@ -1,360 +0,0 @@
1
- /**
2
- * YAML Rule Parser for ast-grep
3
- *
4
- * Parses simplified YAML rule files for structural code analysis.
5
- * Supports pattern matching, kind matching, and structured conditions
6
- * (has/any/all/not/regex).
7
- *
8
- * Features:
9
- * - Caching with mtime-based invalidation
10
- * - Severity filtering (error-only for blocking mode)
11
- * - Complexity scoring for performance optimization
12
- * - Overly broad pattern detection
13
- */
14
- import * as fs from "node:fs";
15
- import * as path from "node:path";
16
- // --- Constants ---
17
- /** Overly broad patterns that match everything (cause false positive explosions) */
18
- export const OVERLY_BROAD_PATTERNS = [
19
- "$NAME",
20
- "$FIELD",
21
- "$_",
22
- "$X",
23
- "$VAR",
24
- "$EXPR",
25
- ];
26
- /** Maximum complexity score for rules in blockingOnly mode */
27
- export const MAX_BLOCKING_RULE_COMPLEXITY = 8;
28
- // --- Caches ---
29
- const rulesCache = new Map();
30
- const blockingRulesCache = new Map();
31
- // --- Public API ---
32
- export function clearRulesCache() {
33
- rulesCache.clear();
34
- blockingRulesCache.clear();
35
- }
36
- export function loadYamlRules(ruleDir, severityFilter) {
37
- return getCachedRules(ruleDir, severityFilter);
38
- }
39
- export function loadYamlRulesUncached(ruleDir, severityFilter) {
40
- const rules = [];
41
- if (!fs.existsSync(ruleDir))
42
- return rules;
43
- const files = fs.readdirSync(ruleDir).filter((f) => f.endsWith(".yml"));
44
- for (const file of files) {
45
- try {
46
- const content = fs.readFileSync(path.join(ruleDir, file), "utf-8");
47
- const documents = content.split(/^---$/m).filter((d) => d.trim());
48
- for (const doc of documents) {
49
- const rule = parseSimpleYaml(doc.trim());
50
- if (rule?.id) {
51
- if (severityFilter && rule.severity !== severityFilter) {
52
- continue;
53
- }
54
- rules.push(rule);
55
- }
56
- }
57
- }
58
- catch {
59
- // Skip invalid files
60
- }
61
- }
62
- return rules;
63
- }
64
- export function getCachedRules(ruleDir, severityFilter) {
65
- if (!fs.existsSync(ruleDir)) {
66
- return [];
67
- }
68
- let currentMtime = 0;
69
- try {
70
- currentMtime = fs.statSync(ruleDir).mtimeMs;
71
- }
72
- catch {
73
- return [];
74
- }
75
- const cache = severityFilter === "error" ? blockingRulesCache : rulesCache;
76
- const cached = cache.get(ruleDir);
77
- if (cached && cached.mtime === currentMtime) {
78
- return cached.rules;
79
- }
80
- const rules = loadYamlRulesUncached(ruleDir, severityFilter);
81
- cache.set(ruleDir, { rules, mtime: currentMtime });
82
- return rules;
83
- }
84
- export function isOverlyBroadPattern(pattern) {
85
- if (!pattern)
86
- return false;
87
- if (OVERLY_BROAD_PATTERNS.includes(pattern.trim()))
88
- return true;
89
- return /^\$[A-Z_]+$/i.test(pattern.trim());
90
- }
91
- export function isValidCondition(condition) {
92
- if (!condition)
93
- return false;
94
- if (condition.all !== undefined && condition.all.length === 0)
95
- return false;
96
- if (condition.any !== undefined && condition.any.length === 0)
97
- return false;
98
- if (isOverlyBroadPattern(condition.pattern))
99
- return false;
100
- return true;
101
- }
102
- export function isStructuredRule(rule) {
103
- if (!rule.rule)
104
- return false;
105
- return !!(rule.rule.has ||
106
- rule.rule.any ||
107
- rule.rule.all ||
108
- rule.rule.not ||
109
- rule.rule.regex);
110
- }
111
- /**
112
- * Check if a rule or any of its nested conditions use features
113
- * not supported by the NAPI runner (inside, follows, precedes,
114
- * stopBy, field, nthChild). Rules using these must be skipped
115
- * to prevent false positives from incomplete condition evaluation.
116
- */
117
- export function hasUnsupportedConditions(rule) {
118
- if (rule.constraints)
119
- return true;
120
- if (!rule.rule)
121
- return false;
122
- return conditionHasUnsupported(rule.rule);
123
- }
124
- function conditionHasUnsupported(c) {
125
- if (c.inside ||
126
- c.follows ||
127
- c.precedes ||
128
- c.stopBy ||
129
- c.field ||
130
- c.nthChild) {
131
- return true;
132
- }
133
- if (c.has && conditionHasUnsupported(c.has))
134
- return true;
135
- if (c.not && conditionHasUnsupported(c.not))
136
- return true;
137
- if (c.any) {
138
- for (const sub of c.any) {
139
- if (conditionHasUnsupported(sub))
140
- return true;
141
- }
142
- }
143
- if (c.all) {
144
- for (const sub of c.all) {
145
- if (conditionHasUnsupported(sub))
146
- return true;
147
- }
148
- }
149
- return false;
150
- }
151
- export function calculateRuleComplexity(condition) {
152
- if (!condition)
153
- return 0;
154
- let score = 0;
155
- if (condition.has)
156
- score += 3;
157
- if (condition.not)
158
- score += 2;
159
- if (condition.regex)
160
- score += 2;
161
- if (condition.any)
162
- score += condition.any.length * 2;
163
- if (condition.all)
164
- score += condition.all.length * 3;
165
- if (condition.has)
166
- score += calculateRuleComplexity(condition.has);
167
- if (condition.not)
168
- score += calculateRuleComplexity(condition.not);
169
- if (condition.any) {
170
- for (const sub of condition.any)
171
- score += calculateRuleComplexity(sub);
172
- }
173
- if (condition.all) {
174
- for (const sub of condition.all)
175
- score += calculateRuleComplexity(sub);
176
- }
177
- return score;
178
- }
179
- // --- YAML Parser ---
180
- function getIndent(line) {
181
- let count = 0;
182
- for (const char of line) {
183
- if (char === " ")
184
- count++;
185
- else if (char === "\t")
186
- count += 2;
187
- else
188
- break;
189
- }
190
- return count;
191
- }
192
- function stripQuotes(value) {
193
- let s = value;
194
- while (s.startsWith('"') && s.endsWith('"') && s.length > 1)
195
- s = s.slice(1, -1);
196
- while (s.startsWith("'") && s.endsWith("'") && s.length > 1)
197
- s = s.slice(1, -1);
198
- return s;
199
- }
200
- export function parseSimpleYaml(content) {
201
- const lines = content.split("\n");
202
- const rule = { id: "", metadata: {} };
203
- const stack = [];
204
- let multilineBuffer = [];
205
- let multilineKey = "";
206
- const currentObj = () => stack.length === 0
207
- ? rule
208
- : stack[stack.length - 1].obj;
209
- const flushMultiline = () => {
210
- if (!multilineKey || multilineBuffer.length === 0)
211
- return;
212
- const value = multilineBuffer.join("\n");
213
- const obj = currentObj();
214
- if (multilineKey === "pattern")
215
- obj.pattern = value;
216
- else if (multilineKey === "message")
217
- rule.message = value;
218
- multilineKey = "";
219
- multilineBuffer = [];
220
- };
221
- for (let i = 0; i < lines.length; i++) {
222
- const line = lines[i];
223
- const trimmed = line.trim();
224
- if (!trimmed || trimmed.startsWith("#") || trimmed === "---")
225
- continue;
226
- const indent = getIndent(line);
227
- while (stack.length > 0 && indent <= stack[stack.length - 1].indent) {
228
- stack.pop();
229
- }
230
- if (line.startsWith(" ") && !trimmed.includes(":") && multilineKey) {
231
- multilineBuffer.push(trimmed);
232
- continue;
233
- }
234
- flushMultiline();
235
- const colonIdx = trimmed.indexOf(":");
236
- const key = colonIdx > 0 ? trimmed.substring(0, colonIdx).trim() : trimmed;
237
- const value = colonIdx > 0 ? trimmed.substring(colonIdx + 1).trim() : "";
238
- if (key === "id") {
239
- rule.id = stripQuotes(value);
240
- }
241
- else if (key === "language") {
242
- rule.language = value;
243
- }
244
- else if (key === "severity") {
245
- rule.severity = value;
246
- }
247
- else if (key === "message") {
248
- value === "|"
249
- ? (multilineKey = "message")
250
- : (rule.message = stripQuotes(value));
251
- }
252
- else if (key === "constraints") {
253
- rule.constraints = {};
254
- stack.push({
255
- name: "constraints",
256
- indent,
257
- obj: rule.constraints,
258
- });
259
- }
260
- else if (key === "metadata") {
261
- rule.metadata = {};
262
- stack.push({ name: "metadata", indent, obj: rule.metadata });
263
- }
264
- else if (key === "rule") {
265
- rule.rule = {};
266
- stack.push({ name: "rule", indent, obj: rule.rule });
267
- }
268
- else if (stack.length > 0) {
269
- const obj = currentObj();
270
- const section = stack[stack.length - 1].name;
271
- if (key === "weight" && section === "metadata") {
272
- if (!rule.metadata)
273
- rule.metadata = {};
274
- rule.metadata.weight = parseInt(value, 10) || 3;
275
- }
276
- else if (key === "category" && section === "metadata") {
277
- if (!rule.metadata)
278
- rule.metadata = {};
279
- rule.metadata.category = stripQuotes(value);
280
- }
281
- else if (key === "pattern") {
282
- value === "|"
283
- ? (multilineKey = "pattern")
284
- : (obj.pattern = stripQuotes(value));
285
- }
286
- else if (key === "kind") {
287
- obj.kind = value;
288
- }
289
- else if (key === "regex") {
290
- obj.regex = stripQuotes(value);
291
- }
292
- else if (key === "inside" || key === "follows" || key === "precedes") {
293
- // Mark as present for unsupported-condition detection
294
- obj[key] = {};
295
- stack.push({ name: key, indent, obj: obj[key] });
296
- }
297
- else if (key === "stopBy") {
298
- obj.stopBy = stripQuotes(value) || "end";
299
- }
300
- else if (key === "field") {
301
- obj.field = stripQuotes(value);
302
- }
303
- else if (key === "nthChild") {
304
- obj.nthChild = value || true;
305
- stack.push({ name: "nthChild", indent, obj: {} });
306
- }
307
- else if (key === "has" || key === "not") {
308
- obj[key] = {};
309
- stack.push({ name: key, indent, obj: obj[key] });
310
- }
311
- else if (key === "any" || key === "all") {
312
- if (!obj[key])
313
- obj[key] = [];
314
- const list = obj[key];
315
- let j = i + 1;
316
- while (j < lines.length) {
317
- const nextLine = lines[j];
318
- const nextTrimmed = nextLine.trim();
319
- if (!nextTrimmed || nextTrimmed.startsWith("#")) {
320
- j++;
321
- continue;
322
- }
323
- const nextIndent = getIndent(nextLine);
324
- if (nextIndent <= indent)
325
- break;
326
- if (nextTrimmed.startsWith("- ")) {
327
- const item = {};
328
- list.push(item);
329
- stack.push({
330
- name: key,
331
- indent: nextIndent,
332
- obj: item,
333
- });
334
- const itemContent = nextTrimmed.substring(2);
335
- const colonPos = itemContent.indexOf(":");
336
- if (colonPos !== -1) {
337
- const itemKey = itemContent.substring(0, colonPos);
338
- const itemVal = itemContent.substring(colonPos + 1);
339
- if (itemKey.trim() === "pattern") {
340
- item.pattern = stripQuotes(itemVal.trim());
341
- }
342
- else if (itemKey.trim() === "kind") {
343
- item.kind = itemVal.trim();
344
- }
345
- else if (itemKey.trim() === "regex") {
346
- item.regex = stripQuotes(itemVal.trim());
347
- }
348
- }
349
- else if (itemContent) {
350
- item.pattern = stripQuotes(itemContent);
351
- }
352
- }
353
- j++;
354
- }
355
- }
356
- }
357
- }
358
- flushMultiline();
359
- return rule.id ? rule : null;
360
- }
@@ -1,16 +0,0 @@
1
- /**
2
- * Redesigned Dispatch Types for pi-lens
3
- *
4
- * Key insight: Different clients have different OUTPUT SEMANTICS:
5
- * - BLOCKING: Errors that stop the agent (architect, ts-lsp errors)
6
- * - WARNING: Non-blocking issues (biome warnings, type-safety)
7
- * - FIXABLE: Issues with auto-fix available
8
- * - SILENT: Metrics tracked but not shown (complexity)
9
- * - INFORMATIONAL: Shown in session summary only
10
- *
11
- * The dispatcher must handle these semantics consistently.
12
- */
13
- // --- Registry ---
14
- // Test edit - adding unused variable to check inline diagnostics flow
15
- const _unusedTestVariable = "checking pre-write and post-write flow";
16
- export {};