@rigour-labs/core 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context.test.d.ts +1 -0
- package/dist/context.test.js +61 -0
- package/dist/discovery.js +12 -7
- package/dist/discovery.test.d.ts +1 -0
- package/dist/discovery.test.js +57 -0
- package/dist/environment.test.d.ts +1 -0
- package/dist/environment.test.js +97 -0
- package/dist/gates/ast-handlers/python.js +15 -1
- package/dist/gates/ast.js +6 -3
- package/dist/gates/base.d.ts +5 -1
- package/dist/gates/base.js +2 -2
- package/dist/gates/content.js +5 -1
- package/dist/gates/context.d.ts +8 -0
- package/dist/gates/context.js +43 -0
- package/dist/gates/coverage.js +6 -1
- package/dist/gates/dependency.js +40 -1
- package/dist/gates/environment.d.ts +8 -0
- package/dist/gates/environment.js +73 -0
- package/dist/gates/file.js +5 -1
- package/dist/gates/runner.d.ts +1 -1
- package/dist/gates/runner.js +19 -2
- package/dist/gates/safety.js +9 -3
- package/dist/safety.test.d.ts +1 -0
- package/dist/safety.test.js +42 -0
- package/dist/services/context-engine.d.ts +22 -0
- package/dist/services/context-engine.js +78 -0
- package/dist/templates/index.js +13 -0
- package/dist/types/index.d.ts +147 -5
- package/dist/types/index.js +13 -0
- package/dist/utils/scanner.js +9 -4
- package/dist/utils/scanner.test.d.ts +1 -0
- package/dist/utils/scanner.test.js +29 -0
- package/package.json +4 -2
- package/src/context.test.ts +73 -0
- package/src/discovery.test.ts +61 -0
- package/src/discovery.ts +11 -6
- package/src/environment.test.ts +115 -0
- package/src/gates/ast-handlers/python.ts +14 -1
- package/src/gates/ast.ts +7 -3
- package/src/gates/base.ts +6 -2
- package/src/gates/content.ts +5 -1
- package/src/gates/context.ts +55 -0
- package/src/gates/coverage.ts +5 -1
- package/src/gates/dependency.ts +65 -1
- package/src/gates/environment.ts +94 -0
- package/src/gates/file.ts +5 -1
- package/src/gates/runner.ts +23 -2
- package/src/gates/safety.ts +11 -4
- package/src/safety.test.ts +53 -0
- package/src/services/context-engine.ts +104 -0
- package/src/templates/index.ts +13 -0
- package/src/types/index.ts +17 -0
- package/src/utils/scanner.test.ts +37 -0
- package/src/utils/scanner.ts +10 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
2
|
+
import { GateRunner } from '../src/gates/runner.js';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
const TEST_CWD = path.join(__dirname, '../temp-test-context');
|
|
8
|
+
describe('Context Awareness Engine', () => {
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
await fs.ensureDir(TEST_CWD);
|
|
11
|
+
});
|
|
12
|
+
afterAll(async () => {
|
|
13
|
+
await fs.remove(TEST_CWD);
|
|
14
|
+
});
|
|
15
|
+
it('should detect context drift for redundant env suffixes (Golden Example)', async () => {
|
|
16
|
+
// Setup: Define standard GCP_PROJECT_ID
|
|
17
|
+
await fs.writeFile(path.join(TEST_CWD, '.env.example'), 'GCP_PROJECT_ID=my-project\n');
|
|
18
|
+
// Setup: Use drifted GCP_PROJECT_ID_PRODUCTION
|
|
19
|
+
await fs.writeFile(path.join(TEST_CWD, 'feature.js'), `
|
|
20
|
+
const id = process.env.GCP_PROJECT_ID_PRODUCTION;
|
|
21
|
+
console.log(id);
|
|
22
|
+
`);
|
|
23
|
+
const config = {
|
|
24
|
+
version: 1,
|
|
25
|
+
commands: {},
|
|
26
|
+
gates: {
|
|
27
|
+
context: {
|
|
28
|
+
enabled: true,
|
|
29
|
+
sensitivity: 0.8,
|
|
30
|
+
mining_depth: 10,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
output: { report_path: 'rigour-report.json' }
|
|
34
|
+
};
|
|
35
|
+
const runner = new GateRunner(config);
|
|
36
|
+
const report = await runner.run(TEST_CWD);
|
|
37
|
+
const driftFailures = report.failures.filter(f => f.id === 'context-drift');
|
|
38
|
+
expect(driftFailures.length).toBeGreaterThan(0);
|
|
39
|
+
expect(driftFailures[0].details).toContain('GCP_PROJECT_ID_PRODUCTION');
|
|
40
|
+
expect(driftFailures[0].hint).toContain('GCP_PROJECT_ID');
|
|
41
|
+
});
|
|
42
|
+
it('should not flag valid environment variables', async () => {
|
|
43
|
+
await fs.writeFile(path.join(TEST_CWD, 'valid.js'), `
|
|
44
|
+
const id = process.env.GCP_PROJECT_ID;
|
|
45
|
+
`);
|
|
46
|
+
const config = {
|
|
47
|
+
version: 1,
|
|
48
|
+
commands: {},
|
|
49
|
+
gates: {
|
|
50
|
+
context: { enabled: true },
|
|
51
|
+
},
|
|
52
|
+
output: { report_path: 'rigour-report.json' }
|
|
53
|
+
};
|
|
54
|
+
const runner = new GateRunner(config);
|
|
55
|
+
const report = await runner.run(TEST_CWD);
|
|
56
|
+
const driftFailures = report.failures.filter(f => f.id === 'context-drift');
|
|
57
|
+
// Filter out failures from other files if they still exist in TEST_CWD
|
|
58
|
+
const specificFailures = driftFailures.filter(f => f.files?.includes('valid.js'));
|
|
59
|
+
expect(specificFailures.length).toBe(0);
|
|
60
|
+
});
|
|
61
|
+
});
|
package/dist/discovery.js
CHANGED
|
@@ -61,15 +61,20 @@ export class DiscoveryService {
|
|
|
61
61
|
return false;
|
|
62
62
|
}
|
|
63
63
|
async findSourceFiles(cwd) {
|
|
64
|
-
// Find a few files to sample
|
|
65
64
|
const extensions = ['.ts', '.js', '.py', '.go', '.java', '.tf', 'package.json'];
|
|
66
65
|
const samples = [];
|
|
67
|
-
const
|
|
68
|
-
for (const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
66
|
+
const commonDirs = ['.', 'src', 'app', 'lib', 'api', 'pkg'];
|
|
67
|
+
for (const dir of commonDirs) {
|
|
68
|
+
const fullDir = path.join(cwd, dir);
|
|
69
|
+
if (!(await fs.pathExists(fullDir)))
|
|
70
|
+
continue;
|
|
71
|
+
const files = await fs.readdir(fullDir);
|
|
72
|
+
for (const file of files) {
|
|
73
|
+
if (extensions.some(ext => file.endsWith(ext))) {
|
|
74
|
+
samples.push(path.join(fullDir, file));
|
|
75
|
+
if (samples.length >= 5)
|
|
76
|
+
return samples;
|
|
77
|
+
}
|
|
73
78
|
}
|
|
74
79
|
}
|
|
75
80
|
return samples;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { DiscoveryService } from './discovery.js';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
vi.mock('fs-extra');
|
|
5
|
+
describe('DiscoveryService', () => {
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
vi.resetAllMocks();
|
|
8
|
+
});
|
|
9
|
+
it('should discover project marker in root directory', async () => {
|
|
10
|
+
const service = new DiscoveryService();
|
|
11
|
+
vi.mocked(fs.pathExists).mockImplementation(async (p) => p.includes('package.json'));
|
|
12
|
+
vi.mocked(fs.readdir).mockResolvedValue(['package.json']);
|
|
13
|
+
vi.mocked(fs.readFile).mockResolvedValue('{}');
|
|
14
|
+
const result = await service.discover('/test');
|
|
15
|
+
// If package.json doesn't match a specific role marker, it stays Universal.
|
|
16
|
+
// Let's mock a specific one like 'express'
|
|
17
|
+
vi.mocked(fs.pathExists).mockImplementation(async (p) => p.includes('express'));
|
|
18
|
+
const result2 = await service.discover('/test');
|
|
19
|
+
expect(result2.matches.preset?.name).toBe('api');
|
|
20
|
+
});
|
|
21
|
+
it('should discover project marker in src/ directory (Deep Detection)', async () => {
|
|
22
|
+
const service = new DiscoveryService();
|
|
23
|
+
vi.mocked(fs.pathExists).mockImplementation((async (p) => {
|
|
24
|
+
if (p.endsWith('src'))
|
|
25
|
+
return true;
|
|
26
|
+
if (p.includes('src/index.ts'))
|
|
27
|
+
return true;
|
|
28
|
+
return false;
|
|
29
|
+
}));
|
|
30
|
+
vi.mocked(fs.readdir).mockImplementation((async (p) => {
|
|
31
|
+
if (p.toString().endsWith('/test'))
|
|
32
|
+
return ['src'];
|
|
33
|
+
if (p.toString().endsWith('src'))
|
|
34
|
+
return ['index.ts'];
|
|
35
|
+
return [];
|
|
36
|
+
}));
|
|
37
|
+
vi.mocked(fs.readFile).mockResolvedValue('export const x = 1;');
|
|
38
|
+
const result = await service.discover('/test');
|
|
39
|
+
// Since UNIVERSAL_CONFIG has a default, we check if it found something extra or matches expectation
|
|
40
|
+
// Default is universal, but detecting .ts should tilt it towards node or similar if configured
|
|
41
|
+
// In our current templates, package.json is the node marker.
|
|
42
|
+
// Let's check for paradigm detection which uses content
|
|
43
|
+
expect(result.config).toBeDefined();
|
|
44
|
+
});
|
|
45
|
+
it('should identify OOP paradigm from content in subfolder', async () => {
|
|
46
|
+
const service = new DiscoveryService();
|
|
47
|
+
vi.mocked(fs.pathExists).mockImplementation((async (p) => p.endsWith('src') || p.endsWith('src/Service.ts')));
|
|
48
|
+
vi.mocked(fs.readdir).mockImplementation((async (p) => {
|
|
49
|
+
if (p.toString().endsWith('src'))
|
|
50
|
+
return ['Service.ts'];
|
|
51
|
+
return ['src'];
|
|
52
|
+
}));
|
|
53
|
+
vi.mocked(fs.readFile).mockResolvedValue('class MyService {}');
|
|
54
|
+
const result = await service.discover('/test');
|
|
55
|
+
expect(result.matches.paradigm?.name).toBe('oop');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { GateRunner } from './gates/runner.js';
|
|
3
|
+
import { ConfigSchema } from './types/index.js';
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
describe('Environment Alignment Gate', () => {
|
|
7
|
+
const testDir = path.join(process.cwd(), 'temp-test-env');
|
|
8
|
+
beforeEach(async () => {
|
|
9
|
+
await fs.ensureDir(testDir);
|
|
10
|
+
});
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
await fs.remove(testDir);
|
|
13
|
+
});
|
|
14
|
+
it('should detect tool version mismatch (Explicit)', async () => {
|
|
15
|
+
const rawConfig = {
|
|
16
|
+
version: 1,
|
|
17
|
+
gates: {
|
|
18
|
+
environment: {
|
|
19
|
+
enabled: true,
|
|
20
|
+
enforce_contracts: false,
|
|
21
|
+
tools: {
|
|
22
|
+
node: ">=99.0.0" // Guaranteed to fail
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const config = ConfigSchema.parse(rawConfig);
|
|
28
|
+
const runner = new GateRunner(config);
|
|
29
|
+
const report = await runner.run(testDir);
|
|
30
|
+
expect(report.status).toBe('FAIL');
|
|
31
|
+
const envFailure = report.failures.find(f => f.id === 'environment-alignment');
|
|
32
|
+
expect(envFailure).toBeDefined();
|
|
33
|
+
expect(envFailure?.details).toContain('node');
|
|
34
|
+
expect(envFailure?.details).toContain('version mismatch');
|
|
35
|
+
});
|
|
36
|
+
it('should detect missing environment variables', async () => {
|
|
37
|
+
const rawConfig = {
|
|
38
|
+
version: 1,
|
|
39
|
+
gates: {
|
|
40
|
+
environment: {
|
|
41
|
+
enabled: true,
|
|
42
|
+
required_env: ["RIGOUR_TEST_VAR_MISSING"]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const config = ConfigSchema.parse(rawConfig);
|
|
47
|
+
const runner = new GateRunner(config);
|
|
48
|
+
const report = await runner.run(testDir);
|
|
49
|
+
expect(report.status).toBe('FAIL');
|
|
50
|
+
expect(report.failures[0].details).toContain('RIGOUR_TEST_VAR_MISSING');
|
|
51
|
+
});
|
|
52
|
+
it('should discover contracts from pyproject.toml', async () => {
|
|
53
|
+
// Create mock pyproject.toml with a version that will surely fail
|
|
54
|
+
await fs.writeFile(path.join(testDir, 'pyproject.toml'), `
|
|
55
|
+
[tool.ruff]
|
|
56
|
+
ruff = ">=99.14.0"
|
|
57
|
+
`);
|
|
58
|
+
const rawConfig = {
|
|
59
|
+
version: 1,
|
|
60
|
+
gates: {
|
|
61
|
+
environment: {
|
|
62
|
+
enabled: true,
|
|
63
|
+
enforce_contracts: true,
|
|
64
|
+
tools: {} // Should discover ruff from file
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const config = ConfigSchema.parse(rawConfig);
|
|
69
|
+
const runner = new GateRunner(config);
|
|
70
|
+
const report = await runner.run(testDir);
|
|
71
|
+
// This might pass or fail depending on the local ruff version,
|
|
72
|
+
// but we want to check if the gate attempted to check ruff.
|
|
73
|
+
// If ruff is missing, it will fail with "is missing".
|
|
74
|
+
const ruffFailure = report.failures.find(f => f.details.includes('ruff'));
|
|
75
|
+
expect(ruffFailure).toBeDefined();
|
|
76
|
+
});
|
|
77
|
+
it('should prioritize environment gate and run it first', async () => {
|
|
78
|
+
const rawConfig = {
|
|
79
|
+
version: 1,
|
|
80
|
+
gates: {
|
|
81
|
+
max_file_lines: 1,
|
|
82
|
+
environment: {
|
|
83
|
+
enabled: true,
|
|
84
|
+
required_env: ["MANDATORY_SECRET_MISSING"]
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const config = ConfigSchema.parse(rawConfig);
|
|
89
|
+
// Create a file that would fail max_file_lines
|
|
90
|
+
await fs.writeFile(path.join(testDir, 'large.js'), 'line1\nline2\nline3');
|
|
91
|
+
const runner = new GateRunner(config);
|
|
92
|
+
const report = await runner.run(testDir);
|
|
93
|
+
// In a real priority system, we might want to stop after environment failure.
|
|
94
|
+
// Currently GateRunner runs all, but environment-alignment has been unshifted.
|
|
95
|
+
expect(Object.keys(report.summary)[0]).toBe('environment-alignment');
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -10,8 +10,22 @@ export class PythonHandler extends ASTHandler {
|
|
|
10
10
|
async run(context) {
|
|
11
11
|
const failures = [];
|
|
12
12
|
const scriptPath = path.join(__dirname, 'python_parser.py');
|
|
13
|
+
// Dynamic command detection for cross-platform support (Mac/Linux usually python3, Windows usually python)
|
|
14
|
+
let pythonCmd = 'python3';
|
|
13
15
|
try {
|
|
14
|
-
|
|
16
|
+
await execa('python3', ['--version']);
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
try {
|
|
20
|
+
await execa('python', ['--version']);
|
|
21
|
+
pythonCmd = 'python';
|
|
22
|
+
}
|
|
23
|
+
catch (e2) {
|
|
24
|
+
// Both missing - handled by main catch
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const { stdout } = await execa(pythonCmd, [scriptPath], {
|
|
15
29
|
input: context.content,
|
|
16
30
|
cwd: context.cwd
|
|
17
31
|
});
|
package/dist/gates/ast.js
CHANGED
|
@@ -17,10 +17,13 @@ export class ASTGate extends Gate {
|
|
|
17
17
|
}
|
|
18
18
|
async run(context) {
|
|
19
19
|
const failures = [];
|
|
20
|
+
const patterns = (context.patterns || ['**/*.{ts,js,tsx,jsx,py,go,rs,cs,java,rb,c,cpp,php,swift,kt}']).map(p => p.replace(/\\/g, '/'));
|
|
21
|
+
const ignore = (context.ignore || ['node_modules/**', 'dist/**', 'build/**', '**/*.test.*', '**/*.spec.*', '**/__pycache__/**']).map(p => p.replace(/\\/g, '/'));
|
|
22
|
+
const normalizedCwd = context.cwd.replace(/\\/g, '/');
|
|
20
23
|
// Find all supported files
|
|
21
|
-
const files = await globby(
|
|
22
|
-
cwd:
|
|
23
|
-
ignore:
|
|
24
|
+
const files = await globby(patterns, {
|
|
25
|
+
cwd: normalizedCwd,
|
|
26
|
+
ignore: ignore,
|
|
24
27
|
});
|
|
25
28
|
for (const file of files) {
|
|
26
29
|
const handler = this.handlers.find(h => h.supports(file));
|
package/dist/gates/base.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
import { GoldenRecord } from '../services/context-engine.js';
|
|
1
2
|
import { Failure } from '../types/index.js';
|
|
2
3
|
export interface GateContext {
|
|
3
4
|
cwd: string;
|
|
5
|
+
record?: GoldenRecord;
|
|
6
|
+
ignore?: string[];
|
|
7
|
+
patterns?: string[];
|
|
4
8
|
}
|
|
5
9
|
export declare abstract class Gate {
|
|
6
10
|
readonly id: string;
|
|
7
11
|
readonly title: string;
|
|
8
12
|
constructor(id: string, title: string);
|
|
9
13
|
abstract run(context: GateContext): Promise<Failure[]>;
|
|
10
|
-
protected createFailure(details: string, files?: string[], hint?: string): Failure;
|
|
14
|
+
protected createFailure(details: string, files?: string[], hint?: string, title?: string): Failure;
|
|
11
15
|
}
|
package/dist/gates/base.js
CHANGED
|
@@ -5,10 +5,10 @@ export class Gate {
|
|
|
5
5
|
this.id = id;
|
|
6
6
|
this.title = title;
|
|
7
7
|
}
|
|
8
|
-
createFailure(details, files, hint) {
|
|
8
|
+
createFailure(details, files, hint, title) {
|
|
9
9
|
return {
|
|
10
10
|
id: this.id,
|
|
11
|
-
title: this.title,
|
|
11
|
+
title: title || this.title,
|
|
12
12
|
details,
|
|
13
13
|
files,
|
|
14
14
|
hint,
|
package/dist/gates/content.js
CHANGED
|
@@ -14,7 +14,11 @@ export class ContentGate extends Gate {
|
|
|
14
14
|
patterns.push(/FIXME/i);
|
|
15
15
|
if (patterns.length === 0)
|
|
16
16
|
return [];
|
|
17
|
-
const files = await FileScanner.findFiles({
|
|
17
|
+
const files = await FileScanner.findFiles({
|
|
18
|
+
cwd: context.cwd,
|
|
19
|
+
ignore: context.ignore,
|
|
20
|
+
patterns: context.patterns
|
|
21
|
+
});
|
|
18
22
|
const contents = await FileScanner.readFiles(context.cwd, files);
|
|
19
23
|
const violations = [];
|
|
20
24
|
for (const [file, content] of contents) {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Gate, GateContext } from './base.js';
|
|
2
|
+
import { Failure, Gates } from '../types/index.js';
|
|
3
|
+
export declare class ContextGate extends Gate {
|
|
4
|
+
private config;
|
|
5
|
+
constructor(config: Gates);
|
|
6
|
+
run(context: GateContext): Promise<Failure[]>;
|
|
7
|
+
private checkEnvDrift;
|
|
8
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Gate } from './base.js';
|
|
2
|
+
import { FileScanner } from '../utils/scanner.js';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
export class ContextGate extends Gate {
|
|
6
|
+
config;
|
|
7
|
+
constructor(config) {
|
|
8
|
+
super('context-drift', 'Context Awareness & Drift Detection');
|
|
9
|
+
this.config = config;
|
|
10
|
+
}
|
|
11
|
+
async run(context) {
|
|
12
|
+
const failures = [];
|
|
13
|
+
const record = context.record;
|
|
14
|
+
if (!record || !this.config.context?.enabled)
|
|
15
|
+
return [];
|
|
16
|
+
const files = await FileScanner.findFiles({ cwd: context.cwd });
|
|
17
|
+
const envAnchors = record.anchors.filter(a => a.type === 'env' && a.confidence >= 1);
|
|
18
|
+
for (const file of files) {
|
|
19
|
+
try {
|
|
20
|
+
const content = await fs.readFile(path.join(context.cwd, file), 'utf-8');
|
|
21
|
+
// 1. Detect Redundant Suffixes (The Golden Example)
|
|
22
|
+
this.checkEnvDrift(content, file, envAnchors, failures);
|
|
23
|
+
}
|
|
24
|
+
catch (e) { }
|
|
25
|
+
}
|
|
26
|
+
return failures;
|
|
27
|
+
}
|
|
28
|
+
checkEnvDrift(content, file, anchors, failures) {
|
|
29
|
+
// Find all environment variable accesses in the content
|
|
30
|
+
const matches = content.matchAll(/process\.env(?:\.([A-Z0-9_]+)|\[['"]([A-Z0-9_]+)['"]\])/g);
|
|
31
|
+
for (const match of matches) {
|
|
32
|
+
const accessedVar = match[1] || match[2];
|
|
33
|
+
for (const anchor of anchors) {
|
|
34
|
+
// If the accessed variable contains the anchor but is not equal to it,
|
|
35
|
+
// it's a potential "invented" redundancy (e.g. CORE_URL vs CORE_URL_PROD)
|
|
36
|
+
if (accessedVar !== anchor.id && accessedVar.includes(anchor.id)) {
|
|
37
|
+
const deviation = accessedVar.replace(anchor.id, '').replace(/^_|_$/, '');
|
|
38
|
+
failures.push(this.createFailure(`Context Drift: Redundant variation '${accessedVar}' detected in ${file}.`, [file], `The project already uses '${anchor.id}' as a standard anchor. Avoid inventing variations like '${deviation}'. Reuse the existing anchor or align with established project patterns.`));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
package/dist/gates/coverage.js
CHANGED
|
@@ -51,7 +51,12 @@ export class CoverageGate extends Gate {
|
|
|
51
51
|
results[currentFile] = { found: 0, hit: 0, isComplex: false };
|
|
52
52
|
}
|
|
53
53
|
else if (line.startsWith('LF:')) {
|
|
54
|
-
|
|
54
|
+
const found = parseInt(line.substring(3));
|
|
55
|
+
results[currentFile].found = found;
|
|
56
|
+
// SME Logic: If a file has > 100 logical lines, it's considered "Complex"
|
|
57
|
+
// and triggers the higher (80%) coverage requirement.
|
|
58
|
+
if (found > 100)
|
|
59
|
+
results[currentFile].isComplex = true;
|
|
55
60
|
}
|
|
56
61
|
else if (line.startsWith('LH:')) {
|
|
57
62
|
results[currentFile].hit = parseInt(line.substring(3));
|
package/dist/gates/dependency.js
CHANGED
|
@@ -25,12 +25,51 @@ export class DependencyGate extends Gate {
|
|
|
25
25
|
};
|
|
26
26
|
for (const dep of forbidden) {
|
|
27
27
|
if (allDeps[dep]) {
|
|
28
|
-
failures.push(this.createFailure(`The package '${dep}' is forbidden by project standards.`, ['package.json'], `Remove '${dep}' from package.json and use approved alternatives
|
|
28
|
+
failures.push(this.createFailure(`The package '${dep}' is forbidden by project standards.`, ['package.json'], `Remove '${dep}' from package.json and use approved alternatives.`, 'Forbidden Dependency'));
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
catch (e) { }
|
|
33
33
|
}
|
|
34
|
+
// 2. Scan Python (requirements.txt, pyproject.toml)
|
|
35
|
+
const reqPath = path.join(cwd, 'requirements.txt');
|
|
36
|
+
if (await fs.pathExists(reqPath)) {
|
|
37
|
+
const content = await fs.readFile(reqPath, 'utf-8');
|
|
38
|
+
for (const dep of forbidden) {
|
|
39
|
+
if (new RegExp(`^${dep}([=<>! ]|$)`, 'm').test(content)) {
|
|
40
|
+
failures.push(this.createFailure(`The Python package '${dep}' is forbidden.`, ['requirements.txt'], `Remove '${dep}' from requirements.txt.`, 'Forbidden Dependency'));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const pyprojPath = path.join(cwd, 'pyproject.toml');
|
|
45
|
+
if (await fs.pathExists(pyprojPath)) {
|
|
46
|
+
const content = await fs.readFile(pyprojPath, 'utf-8');
|
|
47
|
+
for (const dep of forbidden) {
|
|
48
|
+
if (new RegExp(`^${dep}\\s*=`, 'm').test(content)) {
|
|
49
|
+
failures.push(this.createFailure(`The Python package '${dep}' is forbidden in pyproject.toml.`, ['pyproject.toml'], `Remove '${dep}' from pyproject.toml dependencies.`, 'Forbidden Dependency'));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// 3. Scan Go (go.mod)
|
|
54
|
+
const goModPath = path.join(cwd, 'go.mod');
|
|
55
|
+
if (await fs.pathExists(goModPath)) {
|
|
56
|
+
const content = await fs.readFile(goModPath, 'utf-8');
|
|
57
|
+
for (const dep of forbidden) {
|
|
58
|
+
if (content.includes(dep)) {
|
|
59
|
+
failures.push(this.createFailure(`The Go module '${dep}' is forbidden.`, ['go.mod'], `Remove '${dep}' from go.mod.`, 'Forbidden Dependency'));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// 4. Scan Java (pom.xml)
|
|
64
|
+
const pomPath = path.join(cwd, 'pom.xml');
|
|
65
|
+
if (await fs.pathExists(pomPath)) {
|
|
66
|
+
const content = await fs.readFile(pomPath, 'utf-8');
|
|
67
|
+
for (const dep of forbidden) {
|
|
68
|
+
if (content.includes(`<artifactId>${dep}</artifactId>`)) {
|
|
69
|
+
failures.push(this.createFailure(`The Java artifact '${dep}' is forbidden.`, ['pom.xml'], `Remove '${dep}' from pom.xml.`, 'Forbidden Dependency'));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
34
73
|
return failures;
|
|
35
74
|
}
|
|
36
75
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Gate, GateContext } from './base.js';
|
|
2
|
+
import { Failure, Gates } from '../types/index.js';
|
|
3
|
+
export declare class EnvironmentGate extends Gate {
|
|
4
|
+
private config;
|
|
5
|
+
constructor(config: Gates);
|
|
6
|
+
run(context: GateContext): Promise<Failure[]>;
|
|
7
|
+
private discoverContracts;
|
|
8
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Gate } from './base.js';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
import semver from 'semver';
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
export class EnvironmentGate extends Gate {
|
|
7
|
+
config;
|
|
8
|
+
constructor(config) {
|
|
9
|
+
super('environment-alignment', 'Environment & Tooling Alignment');
|
|
10
|
+
this.config = config;
|
|
11
|
+
}
|
|
12
|
+
async run(context) {
|
|
13
|
+
const failures = [];
|
|
14
|
+
const envConfig = this.config.environment;
|
|
15
|
+
if (!envConfig || !envConfig.enabled)
|
|
16
|
+
return [];
|
|
17
|
+
const contracts = envConfig.enforce_contracts ? await this.discoverContracts(context.cwd) : {};
|
|
18
|
+
const toolsToCheck = { ...contracts, ...(envConfig.tools || {}) };
|
|
19
|
+
// 1. Verify Tool Versions
|
|
20
|
+
for (const [tool, range] of Object.entries(toolsToCheck)) {
|
|
21
|
+
// Ensure range is a string
|
|
22
|
+
const semverRange = String(range);
|
|
23
|
+
try {
|
|
24
|
+
const { stdout } = await execa(tool, ['--version'], { shell: true });
|
|
25
|
+
const versionMatch = stdout.match(/(\d+\.\d+\.\d+)/);
|
|
26
|
+
if (versionMatch) {
|
|
27
|
+
const version = versionMatch[1];
|
|
28
|
+
if (!semver.satisfies(version, semverRange)) {
|
|
29
|
+
failures.push(this.createFailure(`Environment Alignment: Tool '${tool}' version mismatch.`, [], `Project requires '${tool} ${semverRange}' (discovered from contract), but found version '${version}'. Please align your local environment to prevent drift.`));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
failures.push(this.createFailure(`Environment Alignment: Could not determine version for '${tool}'.`, [], `Ensure '${tool} --version' returns a standard SemVer string.`));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
failures.push(this.createFailure(`Environment Alignment: Required tool '${tool}' is missing.`, [], `Install '${tool}' and ensure it is in your $PATH.`));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// 2. Verify Required Env Vars
|
|
41
|
+
const requiredEnv = envConfig.required_env || [];
|
|
42
|
+
for (const envVar of requiredEnv) {
|
|
43
|
+
if (!process.env[envVar]) {
|
|
44
|
+
failures.push(this.createFailure(`Environment Alignment: Missing required environment variable '${envVar}'.`, [], `Ensure '${envVar}' is defined in your environment or .env file.`));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return failures;
|
|
48
|
+
}
|
|
49
|
+
async discoverContracts(cwd) {
|
|
50
|
+
const contracts = {};
|
|
51
|
+
// 1. Scan pyproject.toml (for ruff, mypy)
|
|
52
|
+
const pyprojectPath = path.join(cwd, 'pyproject.toml');
|
|
53
|
+
if (await fs.pathExists(pyprojectPath)) {
|
|
54
|
+
const content = await fs.readFile(pyprojectPath, 'utf-8');
|
|
55
|
+
// SME Logic: Look for ruff and mypy version constraints
|
|
56
|
+
// Handle both ruff = "^0.14.0" and ruff = { version = "^0.14.0" }
|
|
57
|
+
const ruffMatch = content.match(/ruff\s*=\s*(?:['"]([^'"]+)['"]|\{\s*version\s*=\s*['"]([^'"]+)['"]\s*\})/);
|
|
58
|
+
if (ruffMatch)
|
|
59
|
+
contracts['ruff'] = ruffMatch[1] || ruffMatch[2];
|
|
60
|
+
const mypyMatch = content.match(/mypy\s*=\s*(?:['"]([^'"]+)['"]|\{\s*version\s*=\s*['"]([^'"]+)['"]\s*\})/);
|
|
61
|
+
if (mypyMatch)
|
|
62
|
+
contracts['mypy'] = mypyMatch[1] || mypyMatch[2];
|
|
63
|
+
}
|
|
64
|
+
// 2. Scan package.json (for node/npm/pnpm)
|
|
65
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
66
|
+
if (await fs.pathExists(pkgPath)) {
|
|
67
|
+
const pkg = await fs.readJson(pkgPath);
|
|
68
|
+
if (pkg.engines?.node)
|
|
69
|
+
contracts['node'] = pkg.engines.node;
|
|
70
|
+
}
|
|
71
|
+
return contracts;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/dist/gates/file.js
CHANGED
|
@@ -7,7 +7,11 @@ export class FileGate extends Gate {
|
|
|
7
7
|
this.config = config;
|
|
8
8
|
}
|
|
9
9
|
async run(context) {
|
|
10
|
-
const files = await FileScanner.findFiles({
|
|
10
|
+
const files = await FileScanner.findFiles({
|
|
11
|
+
cwd: context.cwd,
|
|
12
|
+
ignore: context.ignore,
|
|
13
|
+
patterns: context.patterns
|
|
14
|
+
});
|
|
11
15
|
const contents = await FileScanner.readFiles(context.cwd, files);
|
|
12
16
|
const violations = [];
|
|
13
17
|
for (const [file, content] of contents) {
|
package/dist/gates/runner.d.ts
CHANGED
package/dist/gates/runner.js
CHANGED
|
@@ -5,6 +5,9 @@ import { ASTGate } from './ast.js';
|
|
|
5
5
|
import { SafetyGate } from './safety.js';
|
|
6
6
|
import { DependencyGate } from './dependency.js';
|
|
7
7
|
import { CoverageGate } from './coverage.js';
|
|
8
|
+
import { ContextGate } from './context.js';
|
|
9
|
+
import { ContextEngine } from '../services/context-engine.js';
|
|
10
|
+
import { EnvironmentGate } from './environment.js'; // [NEW]
|
|
8
11
|
import { execa } from 'execa';
|
|
9
12
|
import { Logger } from '../utils/logger.js';
|
|
10
13
|
export class GateRunner {
|
|
@@ -29,6 +32,13 @@ export class GateRunner {
|
|
|
29
32
|
this.gates.push(new DependencyGate(this.config));
|
|
30
33
|
this.gates.push(new SafetyGate(this.config.gates));
|
|
31
34
|
this.gates.push(new CoverageGate(this.config.gates));
|
|
35
|
+
if (this.config.gates.context?.enabled) {
|
|
36
|
+
this.gates.push(new ContextGate(this.config.gates));
|
|
37
|
+
}
|
|
38
|
+
// Environment Alignment Gate (Should be prioritized)
|
|
39
|
+
if (this.config.gates.environment?.enabled) {
|
|
40
|
+
this.gates.unshift(new EnvironmentGate(this.config.gates));
|
|
41
|
+
}
|
|
32
42
|
}
|
|
33
43
|
/**
|
|
34
44
|
* Allows adding custom gates dynamically (SOLID - Open/Closed Principle)
|
|
@@ -36,14 +46,21 @@ export class GateRunner {
|
|
|
36
46
|
addGate(gate) {
|
|
37
47
|
this.gates.push(gate);
|
|
38
48
|
}
|
|
39
|
-
async run(cwd) {
|
|
49
|
+
async run(cwd, patterns) {
|
|
40
50
|
const start = Date.now();
|
|
41
51
|
const failures = [];
|
|
42
52
|
const summary = {};
|
|
53
|
+
const ignore = this.config.ignore;
|
|
54
|
+
// 0. Run Context Discovery
|
|
55
|
+
let record;
|
|
56
|
+
if (this.config.gates.context?.enabled) {
|
|
57
|
+
const engine = new ContextEngine(this.config);
|
|
58
|
+
record = await engine.discover(cwd);
|
|
59
|
+
}
|
|
43
60
|
// 1. Run internal gates
|
|
44
61
|
for (const gate of this.gates) {
|
|
45
62
|
try {
|
|
46
|
-
const gateFailures = await gate.run({ cwd });
|
|
63
|
+
const gateFailures = await gate.run({ cwd, record, ignore, patterns });
|
|
47
64
|
if (gateFailures.length > 0) {
|
|
48
65
|
failures.push(...gateFailures);
|
|
49
66
|
summary[gate.id] = 'FAIL';
|
package/dist/gates/safety.js
CHANGED
|
@@ -17,11 +17,17 @@ export class SafetyGate extends Gate {
|
|
|
17
17
|
// This is a "Safety Rail" - if an agent touched these, we fail.
|
|
18
18
|
const { stdout } = await execa('git', ['status', '--porcelain'], { cwd: context.cwd });
|
|
19
19
|
const modifiedFiles = stdout.split('\n')
|
|
20
|
-
.filter(line =>
|
|
21
|
-
|
|
20
|
+
.filter(line => {
|
|
21
|
+
const status = line.slice(0, 2);
|
|
22
|
+
// M: Modified, A: Added (staged), D: Deleted, R: Renamed
|
|
23
|
+
// We ignore ?? (Untracked) to allow rigour init and new doc creation
|
|
24
|
+
return /M|A|D|R/.test(status);
|
|
25
|
+
})
|
|
26
|
+
.map(line => line.slice(3).trim());
|
|
22
27
|
for (const file of modifiedFiles) {
|
|
23
28
|
if (this.isProtected(file, protectedPaths)) {
|
|
24
|
-
|
|
29
|
+
const message = `Protected file '${file}' was modified.`;
|
|
30
|
+
failures.push(this.createFailure(message, [file], `Agents are forbidden from modifying files in ${protectedPaths.join(', ')}.`, message));
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|