peaks-loop 4.1.12 → 4.1.14
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/.claude-plugin/marketplace.json +2 -2
- package/CHANGELOG.md +118 -0
- package/dist/cli/commands/_register.js +2 -0
- package/dist/cli/commands/batch-commands.d.ts +6 -0
- package/dist/cli/commands/batch-commands.js +70 -0
- package/dist/cli/commands/batch-diagnose-command.d.ts +10 -0
- package/dist/cli/commands/batch-diagnose-command.js +196 -0
- package/dist/cli/commands/batch-diagnose-envelope.d.ts +25 -0
- package/dist/cli/commands/batch-diagnose-envelope.js +60 -0
- package/dist/cli/commands/batch-diagnose-record.d.ts +21 -0
- package/dist/cli/commands/batch-diagnose-record.js +51 -0
- package/dist/cli/commands/batch-item-input.d.ts +59 -0
- package/dist/cli/commands/batch-item-input.js +113 -0
- package/dist/cli/commands/batch-report-command.d.ts +22 -0
- package/dist/cli/commands/batch-report-command.js +199 -0
- package/dist/cli/commands/code-gate-command.d.ts +18 -0
- package/dist/cli/commands/code-gate-command.js +95 -32
- package/dist/cli/commands/core/skill-context-verdict.d.ts +16 -1
- package/dist/cli/commands/core/skill-context-verdict.js +38 -8
- package/dist/cli/commands/lint-commands.js +8 -0
- package/dist/cli/commands/lint-rules-command.d.ts +14 -0
- package/dist/cli/commands/lint-rules-command.js +44 -0
- package/dist/services/batch/batch-active-batch.d.ts +55 -0
- package/dist/services/batch/batch-active-batch.js +127 -0
- package/dist/services/batch/batch-context-reminder.d.ts +85 -0
- package/dist/services/batch/batch-context-reminder.js +201 -0
- package/dist/services/batch/batch-diagnosis-finding.d.ts +25 -0
- package/dist/services/batch/batch-diagnosis-finding.js +227 -0
- package/dist/services/batch/batch-diagnosis-report-store.d.ts +49 -0
- package/dist/services/batch/batch-diagnosis-report-store.js +133 -0
- package/dist/services/batch/batch-diagnosis-summary.d.ts +31 -0
- package/dist/services/batch/batch-diagnosis-summary.js +129 -0
- package/dist/services/batch/batch-diagnosis.d.ts +36 -0
- package/dist/services/batch/batch-diagnosis.js +107 -0
- package/dist/services/batch/batch-entry.d.ts +22 -0
- package/dist/services/batch/batch-entry.js +42 -0
- package/dist/services/batch/batch-fix-agent.d.ts +39 -0
- package/dist/services/batch/batch-fix-agent.js +126 -0
- package/dist/services/batch/batch-fix-types.d.ts +85 -0
- package/dist/services/batch/batch-fix-types.js +11 -0
- package/dist/services/batch/batch-fix.d.ts +15 -0
- package/dist/services/batch/batch-fix.js +229 -0
- package/dist/services/batch/batch-phase-advance.d.ts +45 -0
- package/dist/services/batch/batch-phase-advance.js +105 -0
- package/dist/services/batch/batch-progress-store.d.ts +52 -0
- package/dist/services/batch/batch-progress-store.js +219 -0
- package/dist/services/batch/batch-progress-types.d.ts +79 -0
- package/dist/services/batch/batch-progress-types.js +10 -0
- package/dist/services/batch/batch-recovery.d.ts +5 -0
- package/dist/services/batch/batch-recovery.js +24 -0
- package/dist/services/batch/batch-verify.d.ts +93 -0
- package/dist/services/batch/batch-verify.js +216 -0
- package/dist/services/code/auto-compact-decision.d.ts +17 -0
- package/dist/services/code/auto-compact-decision.js +6 -0
- package/dist/services/code/auto-compact-orchestrator.js +6 -0
- package/dist/services/code/batch-compaction-point.d.ts +45 -0
- package/dist/services/code/batch-compaction-point.js +99 -0
- package/dist/services/context/context-schema.d.ts +1 -1
- package/dist/services/hooks/pre-tool-code-gate.d.ts +33 -1
- package/dist/services/hooks/pre-tool-code-gate.js +67 -6
- package/dist/services/lint/curated-rule-reader.d.ts +37 -0
- package/dist/services/lint/curated-rule-reader.js +179 -0
- package/dist/services/lint/project-lint-declarations.d.ts +27 -0
- package/dist/services/lint/project-lint-declarations.js +217 -0
- package/dist/services/lint/project-lint-tool-detection.d.ts +14 -0
- package/dist/services/lint/project-lint-tool-detection.js +208 -0
- package/dist/services/lint/project-lint-tool-rules.d.ts +19 -0
- package/dist/services/lint/project-lint-tool-rules.js +199 -0
- package/dist/services/lint/rule-pre-read-types.d.ts +66 -0
- package/dist/services/lint/rule-pre-read-types.js +22 -0
- package/dist/services/lint/rule-pre-read.d.ts +20 -0
- package/dist/services/lint/rule-pre-read.js +145 -0
- package/dist/services/skill/skill-search-service.d.ts +3 -3
- package/package.json +5 -5
- package/skills/peaks-code/references/micro-cycle.md +13 -1
- package/skills/peaks-race-code/SKILL.md +40 -1
|
@@ -28,6 +28,10 @@ export interface GateInput {
|
|
|
28
28
|
export declare const HARD_BLOCKED_PATH_FAMILIES: readonly ["src/", "tests/unit/", "tests/integration/", "config/", "bin/", "scripts/"];
|
|
29
29
|
/** Allow-listed path prefixes / suffixes — orchestrator may freely Edit/Write these. */
|
|
30
30
|
export declare const ALLOW_LISTED_PATH_PATTERNS: readonly [".peaks/", ".peaks_", "skills/", "docs/", ".md", "CHANGELOG.md", "README.md"];
|
|
31
|
+
/** The tool names this gate judges. A call under any other name is allowed. */
|
|
32
|
+
export declare const GATED_TOOL_NAMES: readonly ["Edit", "Write", "MultiEdit"];
|
|
33
|
+
/** Is this tool one the gate judges at all? */
|
|
34
|
+
export declare function isGatedTool(tool: GateToolName): boolean;
|
|
31
35
|
export type GateVerdict = {
|
|
32
36
|
readonly action: 'allow';
|
|
33
37
|
} | {
|
|
@@ -38,6 +42,30 @@ export type GateVerdict = {
|
|
|
38
42
|
};
|
|
39
43
|
/** Pull the target file path from `input.file_path | input.path | input.notebook_path`. */
|
|
40
44
|
export declare function extractFilePath(input: Readonly<Record<string, unknown>>): string;
|
|
45
|
+
/**
|
|
46
|
+
* The ONE spelling-independent form of a target path.
|
|
47
|
+
*
|
|
48
|
+
* The deny families are project-relative prefixes (`src/`, `tests/unit/`, …),
|
|
49
|
+
* so how the incoming path happens to be SPELLED must not decide the verdict:
|
|
50
|
+
* `src/x.ts`, `./src/x.ts` and the absolute spelling of that same file are one
|
|
51
|
+
* file and must get one verdict. Without this, the same edit is denied in one
|
|
52
|
+
* spelling and allowed in another — and the allow is the one that happens.
|
|
53
|
+
*
|
|
54
|
+
* - separators fold to `/` (a Windows payload spells `src\x.ts`)
|
|
55
|
+
* - a leading `./` is dropped
|
|
56
|
+
* - an ABSOLUTE path inside `projectRoot` becomes project-relative
|
|
57
|
+
* - an absolute path OUTSIDE `projectRoot` stays absolute: the families name
|
|
58
|
+
* THIS project's directories, and a path in another repository is another
|
|
59
|
+
* project's business. That is also what the gate already did with every
|
|
60
|
+
* absolute path, so it is not a widening.
|
|
61
|
+
* - with no `projectRoot` an absolute path cannot be made relative and is
|
|
62
|
+
* left as it is
|
|
63
|
+
*
|
|
64
|
+
* A `..` segment is deliberately NOT resolved: a relative path is relative to
|
|
65
|
+
* the tool call's own working directory, which this module does not know, and
|
|
66
|
+
* resolving it against the project root would be a guess.
|
|
67
|
+
*/
|
|
68
|
+
export declare function normalizeGatePath(filePath: string, projectRoot?: string | null): string;
|
|
41
69
|
/**
|
|
42
70
|
* Pure decision function. Given the tool name + input object, return
|
|
43
71
|
* the gate verdict. Tolerates malformed payloads (allow).
|
|
@@ -47,5 +75,9 @@ export declare function extractFilePath(input: Readonly<Record<string, unknown>>
|
|
|
47
75
|
* `null` are the same verdict as before this parameter existed: the
|
|
48
76
|
* hard-blocked families deny. Resolution is the caller's job, so this
|
|
49
77
|
* module keeps no session, lease or filesystem state.
|
|
78
|
+
*
|
|
79
|
+
* `projectRoot` is the root the deny families are relative to. It is what
|
|
80
|
+
* lets an ABSOLUTE path be judged like its relative spelling; without it an
|
|
81
|
+
* absolute path cannot be related to the project and is left alone.
|
|
50
82
|
*/
|
|
51
|
-
export declare function decideGateAction(tool: GateToolName, input: Readonly<Record<string, unknown>>, driver?: string | null): GateVerdict;
|
|
83
|
+
export declare function decideGateAction(tool: GateToolName, input: Readonly<Record<string, unknown>>, driver?: string | null, projectRoot?: string | null): GateVerdict;
|
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
* logic, usable from any non-Node harness. Both share the same
|
|
19
19
|
* hard-blocked + allow-listed path families.
|
|
20
20
|
*/
|
|
21
|
+
import { isAbsolute, relative, resolve } from 'node:path';
|
|
22
|
+
import { normalizePath } from '../../shared/path-utils.js';
|
|
21
23
|
/** Hard-blocked path families — orchestrator MUST NOT Edit/Write these. */
|
|
22
24
|
export const HARD_BLOCKED_PATH_FAMILIES = [
|
|
23
25
|
'src/',
|
|
@@ -44,6 +46,12 @@ export const ALLOW_LISTED_PATH_PATTERNS = [
|
|
|
44
46
|
* this write, because the write it opens is source editing.
|
|
45
47
|
*/
|
|
46
48
|
const FAST_LANE_DRIVER = 'peaks-race-code';
|
|
49
|
+
/** The tool names this gate judges. A call under any other name is allowed. */
|
|
50
|
+
export const GATED_TOOL_NAMES = ['Edit', 'Write', 'MultiEdit'];
|
|
51
|
+
/** Is this tool one the gate judges at all? */
|
|
52
|
+
export function isGatedTool(tool) {
|
|
53
|
+
return GATED_TOOL_NAMES.includes(tool);
|
|
54
|
+
}
|
|
47
55
|
/** Pull the target file path from `input.file_path | input.path | input.notebook_path`. */
|
|
48
56
|
export function extractFilePath(input) {
|
|
49
57
|
for (const key of ['file_path', 'path', 'notebook_path']) {
|
|
@@ -63,6 +71,50 @@ function matchesAny(path, families) {
|
|
|
63
71
|
}
|
|
64
72
|
return null;
|
|
65
73
|
}
|
|
74
|
+
/** Drop every leading `./` segment: `./src/x.ts` and `src/x.ts` are one path. */
|
|
75
|
+
function stripLeadingDotSegments(path) {
|
|
76
|
+
let out = path;
|
|
77
|
+
while (out.startsWith('./'))
|
|
78
|
+
out = out.slice(2);
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The ONE spelling-independent form of a target path.
|
|
83
|
+
*
|
|
84
|
+
* The deny families are project-relative prefixes (`src/`, `tests/unit/`, …),
|
|
85
|
+
* so how the incoming path happens to be SPELLED must not decide the verdict:
|
|
86
|
+
* `src/x.ts`, `./src/x.ts` and the absolute spelling of that same file are one
|
|
87
|
+
* file and must get one verdict. Without this, the same edit is denied in one
|
|
88
|
+
* spelling and allowed in another — and the allow is the one that happens.
|
|
89
|
+
*
|
|
90
|
+
* - separators fold to `/` (a Windows payload spells `src\x.ts`)
|
|
91
|
+
* - a leading `./` is dropped
|
|
92
|
+
* - an ABSOLUTE path inside `projectRoot` becomes project-relative
|
|
93
|
+
* - an absolute path OUTSIDE `projectRoot` stays absolute: the families name
|
|
94
|
+
* THIS project's directories, and a path in another repository is another
|
|
95
|
+
* project's business. That is also what the gate already did with every
|
|
96
|
+
* absolute path, so it is not a widening.
|
|
97
|
+
* - with no `projectRoot` an absolute path cannot be made relative and is
|
|
98
|
+
* left as it is
|
|
99
|
+
*
|
|
100
|
+
* A `..` segment is deliberately NOT resolved: a relative path is relative to
|
|
101
|
+
* the tool call's own working directory, which this module does not know, and
|
|
102
|
+
* resolving it against the project root would be a guess.
|
|
103
|
+
*/
|
|
104
|
+
export function normalizeGatePath(filePath, projectRoot) {
|
|
105
|
+
if (filePath.length === 0)
|
|
106
|
+
return '';
|
|
107
|
+
const slashed = normalizePath(filePath);
|
|
108
|
+
if (!isAbsolute(slashed))
|
|
109
|
+
return stripLeadingDotSegments(slashed);
|
|
110
|
+
if (typeof projectRoot === 'string' && projectRoot.length > 0) {
|
|
111
|
+
const relativePath = normalizePath(relative(resolve(projectRoot), resolve(slashed)));
|
|
112
|
+
if (relativePath.length > 0 && !relativePath.startsWith('..') && !isAbsolute(relativePath)) {
|
|
113
|
+
return stripLeadingDotSegments(relativePath);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return slashed;
|
|
117
|
+
}
|
|
66
118
|
/**
|
|
67
119
|
* Pure decision function. Given the tool name + input object, return
|
|
68
120
|
* the gate verdict. Tolerates malformed payloads (allow).
|
|
@@ -72,19 +124,28 @@ function matchesAny(path, families) {
|
|
|
72
124
|
* `null` are the same verdict as before this parameter existed: the
|
|
73
125
|
* hard-blocked families deny. Resolution is the caller's job, so this
|
|
74
126
|
* module keeps no session, lease or filesystem state.
|
|
127
|
+
*
|
|
128
|
+
* `projectRoot` is the root the deny families are relative to. It is what
|
|
129
|
+
* lets an ABSOLUTE path be judged like its relative spelling; without it an
|
|
130
|
+
* absolute path cannot be related to the project and is left alone.
|
|
75
131
|
*/
|
|
76
|
-
export function decideGateAction(tool, input, driver) {
|
|
132
|
+
export function decideGateAction(tool, input, driver, projectRoot) {
|
|
77
133
|
// Only gate Edit / Write / MultiEdit.
|
|
78
|
-
if (tool
|
|
134
|
+
if (!isGatedTool(tool)) {
|
|
79
135
|
return { action: 'allow' };
|
|
80
136
|
}
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
137
|
+
const requestedPath = extractFilePath(input);
|
|
138
|
+
if (requestedPath.length === 0) {
|
|
83
139
|
// No path → cannot decide; fail-open (allow). The probe side
|
|
84
140
|
// (`orchestrator-can-do`) handles slice-spec content; this hook
|
|
85
141
|
// handles file paths only.
|
|
86
142
|
return { action: 'allow' };
|
|
87
143
|
}
|
|
144
|
+
// The verdict is about the FILE, not about the spelling of the path that
|
|
145
|
+
// names it, so the path is reduced to the project-relative form the
|
|
146
|
+
// families are written in before anything is matched. The verdict still
|
|
147
|
+
// echoes the path the tool actually asked for.
|
|
148
|
+
const filePath = normalizeGatePath(requestedPath, projectRoot);
|
|
88
149
|
// Allow-list check first — short-circuits any deny, for every driver.
|
|
89
150
|
const allowMatch = matchesAny(filePath, ALLOW_LISTED_PATH_PATTERNS);
|
|
90
151
|
if (allowMatch !== null) {
|
|
@@ -100,9 +161,9 @@ export function decideGateAction(tool, input, driver) {
|
|
|
100
161
|
return { action: 'allow' };
|
|
101
162
|
}
|
|
102
163
|
const reason = `${denyMatch} (orchestrator's hard-blocked path family)`;
|
|
103
|
-
const message = `PEAKS_CODE_PROHIBITED_DIRECT_EDIT: ${
|
|
164
|
+
const message = `PEAKS_CODE_PROHIBITED_DIRECT_EDIT: ${requestedPath} matches hard-blocked path family ${reason}; ` +
|
|
104
165
|
`orchestrator MUST NOT Edit/Write these directly. Use: peaks sub-agent dispatch rd --prompt '<your task>' --request-id <rid> --project . --batch-id <uuid>`;
|
|
105
|
-
return { action: 'deny', filePath, reason, message };
|
|
166
|
+
return { action: 'deny', filePath: requestedPath, reason, message };
|
|
106
167
|
}
|
|
107
168
|
// Anything not in the deny list → allow (e.g. CHANGELOG, README, top-level files).
|
|
108
169
|
return { action: 'allow' };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { EffectiveRule } from './rule-pre-read-types.js';
|
|
2
|
+
/** Where the curated rule sets live, or why they could not be found. */
|
|
3
|
+
export type CuratedRoot = {
|
|
4
|
+
readonly ok: true;
|
|
5
|
+
readonly path: string;
|
|
6
|
+
} | {
|
|
7
|
+
readonly ok: false;
|
|
8
|
+
readonly reason: string;
|
|
9
|
+
};
|
|
10
|
+
/** One rule file, parsed into its declared patterns and its body. */
|
|
11
|
+
type CuratedRuleFile = {
|
|
12
|
+
readonly globs: readonly string[];
|
|
13
|
+
readonly body: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The curated rule root, resolved through the bundled `ecc-universal`
|
|
17
|
+
* dependency. The dependency ships the rules, so there is nothing to fetch.
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveCuratedRulesRoot(): CuratedRoot;
|
|
20
|
+
/**
|
|
21
|
+
* Split a rule file into its declared patterns and its body.
|
|
22
|
+
*
|
|
23
|
+
* A file with no frontmatter has no declared patterns, which means it governs
|
|
24
|
+
* every file — that is how the `common/` layer is written.
|
|
25
|
+
*/
|
|
26
|
+
export declare function splitFrontmatter(text: string): CuratedRuleFile;
|
|
27
|
+
/**
|
|
28
|
+
* The curated rules that govern a file set.
|
|
29
|
+
*
|
|
30
|
+
* Returns the rules AND the reasons the read could not produce them, so a
|
|
31
|
+
* missing curated layer is never mistaken for "there are no curated rules".
|
|
32
|
+
*/
|
|
33
|
+
export declare function readCuratedRules(files: readonly string[], root: CuratedRoot): {
|
|
34
|
+
rules: readonly EffectiveRule[];
|
|
35
|
+
reasons: readonly string[];
|
|
36
|
+
};
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// src/services/lint/curated-rule-reader.ts
|
|
2
|
+
//
|
|
3
|
+
// L3 — the CURATED language rule sets (the ECC `rules/<lang>/` tree).
|
|
4
|
+
//
|
|
5
|
+
// The curated layer answers "what are the general rules for this language?".
|
|
6
|
+
// It is read the same way the other two layers are read: by asking the source
|
|
7
|
+
// what it declares. Each ECC rule file declares the file patterns it governs
|
|
8
|
+
// in its own frontmatter:
|
|
9
|
+
//
|
|
10
|
+
// ---
|
|
11
|
+
// paths:
|
|
12
|
+
// - "**/*.ts"
|
|
13
|
+
// - "**/*.tsx"
|
|
14
|
+
// ---
|
|
15
|
+
//
|
|
16
|
+
// So the file set is matched against those DECLARED patterns — there is no
|
|
17
|
+
// language table here, and no branch on the language of a file. A rule file
|
|
18
|
+
// with no `paths` (the `common/` layer) governs every file.
|
|
19
|
+
//
|
|
20
|
+
// ONE ENTRY PER RULE FILE, never one per file: the same rule text governs a
|
|
21
|
+
// whole family of files, and repeating it per file is what blows a fix prompt
|
|
22
|
+
// up.
|
|
23
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
24
|
+
import { join } from 'node:path';
|
|
25
|
+
import picomatch from 'picomatch';
|
|
26
|
+
import { resolveEccPackageRoot } from 'peaks-loop-mut';
|
|
27
|
+
/** The directory inside the ECC package that holds the curated rule sets. */
|
|
28
|
+
const CURATED_RULES_SUBDIR = 'rules';
|
|
29
|
+
/**
|
|
30
|
+
* The curated rule root, resolved through the bundled `ecc-universal`
|
|
31
|
+
* dependency. The dependency ships the rules, so there is nothing to fetch.
|
|
32
|
+
*/
|
|
33
|
+
export function resolveCuratedRulesRoot() {
|
|
34
|
+
try {
|
|
35
|
+
return { ok: true, path: join(resolveEccPackageRoot(), CURATED_RULES_SUBDIR) };
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
return {
|
|
39
|
+
ok: false,
|
|
40
|
+
reason: `the curated rule sets are not available: ${error instanceof Error ? error.message : String(error)}`
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** The rule files of one curated language directory. */
|
|
45
|
+
function curatedRuleFiles(rulesDir, language, problems) {
|
|
46
|
+
const dir = join(rulesDir, language);
|
|
47
|
+
let entries;
|
|
48
|
+
try {
|
|
49
|
+
entries = readdirSync(dir);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
problems.push(`${language}: ${error instanceof Error ? error.message : String(error)}`);
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const files = [];
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
if (!entry.endsWith('.md'))
|
|
58
|
+
continue;
|
|
59
|
+
const read = readRuleFile(join(dir, entry));
|
|
60
|
+
if (!read.ok) {
|
|
61
|
+
problems.push(`${language}/${entry}: ${read.reason}`);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
files.push(read.ruleFile);
|
|
65
|
+
}
|
|
66
|
+
return files;
|
|
67
|
+
}
|
|
68
|
+
/** One rule file, or the reason it could not be read. */
|
|
69
|
+
function readRuleFile(path) {
|
|
70
|
+
try {
|
|
71
|
+
return { ok: true, ruleFile: splitFrontmatter(readFileSync(path, 'utf8')) };
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
return {
|
|
75
|
+
ok: false,
|
|
76
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Split a rule file into its declared patterns and its body.
|
|
82
|
+
*
|
|
83
|
+
* A file with no frontmatter has no declared patterns, which means it governs
|
|
84
|
+
* every file — that is how the `common/` layer is written.
|
|
85
|
+
*/
|
|
86
|
+
export function splitFrontmatter(text) {
|
|
87
|
+
const lines = text.split(/\r?\n/);
|
|
88
|
+
if (lines[0]?.trim() !== '---')
|
|
89
|
+
return { globs: [], body: text };
|
|
90
|
+
const end = lines.findIndex((line, index) => index > 0 && line.trim() === '---');
|
|
91
|
+
if (end === -1)
|
|
92
|
+
return { globs: [], body: text };
|
|
93
|
+
const globs = frontmatterGlobs(lines.slice(1, end));
|
|
94
|
+
return {
|
|
95
|
+
globs,
|
|
96
|
+
body: lines
|
|
97
|
+
.slice(end + 1)
|
|
98
|
+
.join('\n')
|
|
99
|
+
.trim()
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** The `paths:` list a rule file declares. */
|
|
103
|
+
function frontmatterGlobs(lines) {
|
|
104
|
+
const globs = [];
|
|
105
|
+
let inPaths = false;
|
|
106
|
+
for (const line of lines) {
|
|
107
|
+
const item = /^\s*-\s+(.*)$/.exec(line);
|
|
108
|
+
if (item?.[1] !== undefined) {
|
|
109
|
+
if (inPaths)
|
|
110
|
+
globs.push(unquote(item[1]));
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const inline = /^\s*paths\s*:\s*\[(.*)\]\s*$/.exec(line);
|
|
114
|
+
if (inline?.[1] !== undefined) {
|
|
115
|
+
for (const part of inline[1].split(',')) {
|
|
116
|
+
const glob = unquote(part.trim());
|
|
117
|
+
if (glob.length > 0)
|
|
118
|
+
globs.push(glob);
|
|
119
|
+
}
|
|
120
|
+
inPaths = false;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
inPaths = /^\s*paths\s*:/.test(line);
|
|
124
|
+
}
|
|
125
|
+
return globs;
|
|
126
|
+
}
|
|
127
|
+
function unquote(value) {
|
|
128
|
+
return value.trim().replace(/^['"]|['"]$/g, '');
|
|
129
|
+
}
|
|
130
|
+
/** Whether a rule file's declared patterns cover a file. */
|
|
131
|
+
function covers(ruleFile, file) {
|
|
132
|
+
if (ruleFile.globs.length === 0)
|
|
133
|
+
return true;
|
|
134
|
+
return ruleFile.globs.some((glob) => picomatch.isMatch(file, glob, { dot: true }));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The curated rules that govern a file set.
|
|
138
|
+
*
|
|
139
|
+
* Returns the rules AND the reasons the read could not produce them, so a
|
|
140
|
+
* missing curated layer is never mistaken for "there are no curated rules".
|
|
141
|
+
*/
|
|
142
|
+
export function readCuratedRules(files, root) {
|
|
143
|
+
if (!root.ok) {
|
|
144
|
+
return { rules: [], reasons: [root.reason] };
|
|
145
|
+
}
|
|
146
|
+
const rulesDir = root.path;
|
|
147
|
+
if (!existsSync(rulesDir)) {
|
|
148
|
+
return { rules: [], reasons: [`the curated rule directory is missing: ${rulesDir}`] };
|
|
149
|
+
}
|
|
150
|
+
const reasons = [];
|
|
151
|
+
const rules = [];
|
|
152
|
+
for (const language of curatedLanguages(rulesDir, reasons)) {
|
|
153
|
+
for (const ruleFile of curatedRuleFiles(rulesDir, language, reasons)) {
|
|
154
|
+
const covered = files.filter((file) => covers(ruleFile, file));
|
|
155
|
+
if (covered.length === 0)
|
|
156
|
+
continue;
|
|
157
|
+
rules.push({
|
|
158
|
+
layer: 'curated',
|
|
159
|
+
source: `ecc:${language}`,
|
|
160
|
+
pattern: ruleFile.globs.length === 0 ? '*' : ruleFile.globs.join(', '),
|
|
161
|
+
rule: ruleFile.body
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return { rules, reasons };
|
|
166
|
+
}
|
|
167
|
+
/** The curated language directories, and the ones that could not be listed. */
|
|
168
|
+
function curatedLanguages(rulesDir, reasons) {
|
|
169
|
+
try {
|
|
170
|
+
return readdirSync(rulesDir, { withFileTypes: true })
|
|
171
|
+
.filter((entry) => entry.isDirectory())
|
|
172
|
+
.map((entry) => entry.name)
|
|
173
|
+
.sort();
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
reasons.push(`the curated rule directory could not be listed: ${error instanceof Error ? error.message : String(error)}`);
|
|
177
|
+
return [];
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** One declaration the project ships: a script, a target, a table, a CI step. */
|
|
2
|
+
export type DeclaredCommand = {
|
|
3
|
+
/**
|
|
4
|
+
* The declaration's own name — the script name, the make target, the tool
|
|
5
|
+
* table. Empty for a CI step, which has no name of its own.
|
|
6
|
+
*/
|
|
7
|
+
readonly label: string;
|
|
8
|
+
/** The command line the project declared. */
|
|
9
|
+
readonly command: string;
|
|
10
|
+
/** Where the declaration was found. */
|
|
11
|
+
readonly declaredBy: string;
|
|
12
|
+
};
|
|
13
|
+
/** What one declaration site yielded. */
|
|
14
|
+
export type DeclarationScan = {
|
|
15
|
+
readonly commands: readonly DeclaredCommand[];
|
|
16
|
+
/** Sites that could not be read — reported, never swallowed. */
|
|
17
|
+
readonly problems: readonly string[];
|
|
18
|
+
};
|
|
19
|
+
/** Every declaration the project ships, and the sites that could not be read. */
|
|
20
|
+
export type ProjectDeclarations = {
|
|
21
|
+
readonly commands: readonly DeclaredCommand[];
|
|
22
|
+
readonly problems: readonly string[];
|
|
23
|
+
/** The `package.json` scripts by name, so `pnpm lint` can be resolved. */
|
|
24
|
+
readonly scripts: ReadonlyMap<string, string>;
|
|
25
|
+
};
|
|
26
|
+
/** Every declaration the project ships. */
|
|
27
|
+
export declare function readProjectDeclarations(projectRoot: string): ProjectDeclarations;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// src/services/lint/project-lint-declarations.ts
|
|
2
|
+
//
|
|
3
|
+
// The declaration sites a project ships — the raw material for L1.
|
|
4
|
+
//
|
|
5
|
+
// A project says what its gate runs in the files it ships. This module reads
|
|
6
|
+
// those files and reports the commands they declare; it does not judge them.
|
|
7
|
+
// Judging (is this a lint tool?) belongs to the caller.
|
|
8
|
+
//
|
|
9
|
+
// THE SITES, and why each one is here:
|
|
10
|
+
// - `package.json` scripts — the most common declaration of all.
|
|
11
|
+
// - `Makefile` targets — the Unix convention.
|
|
12
|
+
// - `pyproject.toml` `[tool.*]` tables — the Python convention.
|
|
13
|
+
// - `go.mod` `tool` directives — the Go 1.24+ convention.
|
|
14
|
+
// - CI workflow steps that RUN something — the gate as it actually executes.
|
|
15
|
+
//
|
|
16
|
+
// NOTHING HERE BRANCHES ON A LANGUAGE. The sites are the same for every
|
|
17
|
+
// project; a site that a project does not ship simply yields nothing.
|
|
18
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
19
|
+
import { join } from 'node:path';
|
|
20
|
+
import { normalizePath } from '../../shared/path-utils.js';
|
|
21
|
+
const EMPTY_SCAN = { commands: [], problems: [] };
|
|
22
|
+
function readText(path) {
|
|
23
|
+
if (!existsSync(path))
|
|
24
|
+
return { ok: false, reason: 'not present' };
|
|
25
|
+
try {
|
|
26
|
+
return { ok: true, text: readFileSync(path, 'utf8') };
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
return { ok: false, reason: error instanceof Error ? error.message : String(error) };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function errorText(error) {
|
|
33
|
+
return error instanceof Error ? error.message : String(error);
|
|
34
|
+
}
|
|
35
|
+
/** The `package.json` scripts — the most common declaration site. */
|
|
36
|
+
function packageJsonSite(root) {
|
|
37
|
+
const read = readText(join(root, 'package.json'));
|
|
38
|
+
if (!read.ok)
|
|
39
|
+
return EMPTY_SCAN;
|
|
40
|
+
let parsed;
|
|
41
|
+
try {
|
|
42
|
+
parsed = JSON.parse(read.text);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return { commands: [], problems: [`package.json is not valid JSON: ${errorText(error)}`] };
|
|
46
|
+
}
|
|
47
|
+
if (parsed === null || typeof parsed !== 'object') {
|
|
48
|
+
return { commands: [], problems: ['package.json is not a JSON object'] };
|
|
49
|
+
}
|
|
50
|
+
const scripts = parsed.scripts;
|
|
51
|
+
if (scripts === null || typeof scripts !== 'object' || Array.isArray(scripts))
|
|
52
|
+
return EMPTY_SCAN;
|
|
53
|
+
const commands = [];
|
|
54
|
+
for (const [label, value] of Object.entries(scripts)) {
|
|
55
|
+
if (typeof value !== 'string')
|
|
56
|
+
continue;
|
|
57
|
+
commands.push({ label, command: value, declaredBy: `package.json scripts.${label}` });
|
|
58
|
+
}
|
|
59
|
+
return { commands, problems: [] };
|
|
60
|
+
}
|
|
61
|
+
/** The `Makefile` targets and their recipes. */
|
|
62
|
+
function makefileSite(root) {
|
|
63
|
+
const commands = [];
|
|
64
|
+
for (const fileName of ['Makefile', 'makefile']) {
|
|
65
|
+
const read = readText(join(root, fileName));
|
|
66
|
+
if (!read.ok)
|
|
67
|
+
continue;
|
|
68
|
+
readMakefileTargets(read.text, fileName, commands);
|
|
69
|
+
}
|
|
70
|
+
return { commands, problems: [] };
|
|
71
|
+
}
|
|
72
|
+
function readMakefileTargets(text, fileName, commands) {
|
|
73
|
+
let target = null;
|
|
74
|
+
let recipe = [];
|
|
75
|
+
const flush = () => {
|
|
76
|
+
if (target === null)
|
|
77
|
+
return;
|
|
78
|
+
commands.push({
|
|
79
|
+
label: target,
|
|
80
|
+
command: recipe.join(' '),
|
|
81
|
+
declaredBy: `${fileName} ${target}`
|
|
82
|
+
});
|
|
83
|
+
target = null;
|
|
84
|
+
recipe = [];
|
|
85
|
+
};
|
|
86
|
+
for (const line of text.split(/\r?\n/)) {
|
|
87
|
+
const header = /^([A-Za-z0-9_./-]+):(?!=)\s*$/.exec(line);
|
|
88
|
+
if (header?.[1] !== undefined) {
|
|
89
|
+
flush();
|
|
90
|
+
target = header[1];
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (target !== null && /^\s+\S/.test(line))
|
|
94
|
+
recipe.push(line.trim());
|
|
95
|
+
}
|
|
96
|
+
flush();
|
|
97
|
+
}
|
|
98
|
+
/** The `pyproject.toml` `[tool.*]` tables. */
|
|
99
|
+
function pyprojectSite(root) {
|
|
100
|
+
const read = readText(join(root, 'pyproject.toml'));
|
|
101
|
+
if (!read.ok)
|
|
102
|
+
return EMPTY_SCAN;
|
|
103
|
+
const commands = [];
|
|
104
|
+
for (const line of read.text.split(/\r?\n/)) {
|
|
105
|
+
const match = /^\[tool\.([A-Za-z0-9_.-]+)\]/.exec(line.trim());
|
|
106
|
+
const table = match?.[1];
|
|
107
|
+
if (table === undefined)
|
|
108
|
+
continue;
|
|
109
|
+
const label = table.split('.')[0] ?? table;
|
|
110
|
+
commands.push({ label, command: label, declaredBy: `pyproject.toml [tool.${table}]` });
|
|
111
|
+
}
|
|
112
|
+
return { commands, problems: [] };
|
|
113
|
+
}
|
|
114
|
+
/** The `go.mod` tool directives (Go 1.24+ `tool` blocks and single directives). */
|
|
115
|
+
function goModSite(root) {
|
|
116
|
+
const read = readText(join(root, 'go.mod'));
|
|
117
|
+
if (!read.ok)
|
|
118
|
+
return EMPTY_SCAN;
|
|
119
|
+
const commands = [];
|
|
120
|
+
let inToolBlock = false;
|
|
121
|
+
for (const raw of read.text.split(/\r?\n/)) {
|
|
122
|
+
const line = raw.trim();
|
|
123
|
+
if (line.startsWith('tool (')) {
|
|
124
|
+
inToolBlock = true;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (inToolBlock) {
|
|
128
|
+
if (line === ')') {
|
|
129
|
+
inToolBlock = false;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const label = line.split(/\s+/)[0];
|
|
133
|
+
if (label !== undefined && label.length > 0)
|
|
134
|
+
commands.push(goToolCommand(label));
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const single = /^tool\s+(\S+)/.exec(line);
|
|
138
|
+
if (single?.[1] !== undefined)
|
|
139
|
+
commands.push(goToolCommand(single[1]));
|
|
140
|
+
}
|
|
141
|
+
return { commands, problems: [] };
|
|
142
|
+
}
|
|
143
|
+
function goToolCommand(pkg) {
|
|
144
|
+
const label = pkg.split('/').pop() ?? pkg;
|
|
145
|
+
return { label, command: `go tool ${label}`, declaredBy: `go.mod tool ${pkg}` };
|
|
146
|
+
}
|
|
147
|
+
/** The CI workflow steps that RUN something. */
|
|
148
|
+
function ciSite(root) {
|
|
149
|
+
const commands = [];
|
|
150
|
+
const problems = [];
|
|
151
|
+
const found = ciFiles(root);
|
|
152
|
+
problems.push(...found.problems);
|
|
153
|
+
for (const file of found.files) {
|
|
154
|
+
const read = readText(file);
|
|
155
|
+
if (!read.ok) {
|
|
156
|
+
problems.push(`${normalizePath(file.slice(root.length + 1))}: ${read.reason}`);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const relative = normalizePath(file.slice(root.length + 1));
|
|
160
|
+
read.text.split(/\r?\n/).forEach((line, index) => {
|
|
161
|
+
const command = ciRunCommand(line);
|
|
162
|
+
if (command === null)
|
|
163
|
+
return;
|
|
164
|
+
commands.push({ label: '', command, declaredBy: `${relative}:${String(index + 1)}` });
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return { commands, problems };
|
|
168
|
+
}
|
|
169
|
+
/** The CI files a project ships, plus the sites that could not be listed. */
|
|
170
|
+
function ciFiles(root) {
|
|
171
|
+
const files = [];
|
|
172
|
+
const problems = [];
|
|
173
|
+
const workflowDir = join(root, '.github', 'workflows');
|
|
174
|
+
if (existsSync(workflowDir)) {
|
|
175
|
+
try {
|
|
176
|
+
for (const entry of readdirSync(workflowDir)) {
|
|
177
|
+
if (entry.endsWith('.yml') || entry.endsWith('.yaml'))
|
|
178
|
+
files.push(join(workflowDir, entry));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
problems.push(`.github/workflows: ${errorText(error)}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const gitlab = join(root, '.gitlab-ci.yml');
|
|
186
|
+
if (existsSync(gitlab))
|
|
187
|
+
files.push(gitlab);
|
|
188
|
+
return { files, problems };
|
|
189
|
+
}
|
|
190
|
+
/** The command a CI line runs, or `null` when the line runs nothing. */
|
|
191
|
+
function ciRunCommand(line) {
|
|
192
|
+
const match = /^\s*(?:-\s*)?(?:run|script):\s*(.+)$/.exec(line);
|
|
193
|
+
const raw = match?.[1];
|
|
194
|
+
if (raw === undefined)
|
|
195
|
+
return null;
|
|
196
|
+
const command = raw.trim().replace(/^['"]|['"]$/g, '');
|
|
197
|
+
if (command.length === 0 || command === '|' || command === '>-')
|
|
198
|
+
return null;
|
|
199
|
+
return command;
|
|
200
|
+
}
|
|
201
|
+
/** Every declaration the project ships. */
|
|
202
|
+
export function readProjectDeclarations(projectRoot) {
|
|
203
|
+
const packageScan = packageJsonSite(projectRoot);
|
|
204
|
+
const scripts = new Map(packageScan.commands.map((entry) => [entry.label, entry.command]));
|
|
205
|
+
const scans = [
|
|
206
|
+
packageScan,
|
|
207
|
+
makefileSite(projectRoot),
|
|
208
|
+
pyprojectSite(projectRoot),
|
|
209
|
+
goModSite(projectRoot),
|
|
210
|
+
ciSite(projectRoot)
|
|
211
|
+
];
|
|
212
|
+
return {
|
|
213
|
+
commands: scans.flatMap((scan) => scan.commands),
|
|
214
|
+
problems: scans.flatMap((scan) => scan.problems),
|
|
215
|
+
scripts
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ProjectLintToolDetection } from './rule-pre-read-types.js';
|
|
2
|
+
/** The reason a project with no lint tooling gets. Exact, and never empty. */
|
|
3
|
+
export declare const NO_LINT_TOOL_REASON = "no lint tool declared by this project";
|
|
4
|
+
/** The tool a command line invokes, or `null` when it invokes no known tool. */
|
|
5
|
+
export declare function toolNameFromCommand(command: string, scripts: ReadonlyMap<string, string>, depth?: number): string | null;
|
|
6
|
+
/** Whether a name is a lint tool's name. */
|
|
7
|
+
export declare function isLintToolName(name: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* The project's own lint tooling, discovered from its declaration sites.
|
|
10
|
+
*
|
|
11
|
+
* A project that declares no lint tool gets an EXPLICIT reason, never a
|
|
12
|
+
* silent empty list.
|
|
13
|
+
*/
|
|
14
|
+
export declare function detectProjectLintTools(projectRoot: string): ProjectLintToolDetection;
|