@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
mjs 116 lines 4.26 KB
Raw
1 import { spawnSync } from 'node:child_process';
2 import { readFileSync, writeFileSync } from 'node:fs';
3 import { createRequire } from 'node:module';
4 import { dirname, resolve } from 'node:path';
5 import { fileURLToPath } from 'node:url';
6
7 const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
8 const require = createRequire(import.meta.url);
9 const binary = resolve(dirname(require.resolve('@ast-grep/cli/package.json')), 'ast-grep');
10
11 export function scan(cwd = root) {
12 // The package keeps a Node shim on Windows; Unix installs may replace it
13 // with the native executable. Invoke each in its supported form.
14 const command = process.platform === 'win32' ? process.execPath : binary;
15 const args = [
16 'scan',
17 '--config',
18 'sgconfig.yml',
19 '--json=compact',
20 '--error=no-suppress-all',
21 '--error=unused-suppression',
22 ];
23 const result = spawnSync(command, process.platform === 'win32' ? [binary, ...args] : args, {
24 cwd,
25 encoding: 'utf8',
26 maxBuffer: 32 * 1024 * 1024,
27 });
28 // ast-grep exits 1 for error diagnostics and uses other codes for scan failures.
29 if (result.error || (result.status !== 0 && result.status !== 1)) {
30 throw new Error(`ast-grep failed: ${result.error?.message ?? result.stderr}`);
31 }
32 const findings = JSON.parse(result.stdout);
33 if (!Array.isArray(findings) || (result.status === 1 && findings.length === 0)) {
34 throw new Error(`ast-grep did not return valid diagnostics: ${result.stderr}`);
35 }
36 return findings;
37 }
38
39 function key(entry) {
40 return JSON.stringify([entry.ruleId, entry.file, entry.source]);
41 }
42
43 export function inventory(findings) {
44 const entries = new Map();
45 for (const finding of findings) {
46 const entry = {
47 ruleId: finding.ruleId,
48 file: finding.file.replaceAll('\\', '/'),
49 // Include the whole source line for comment-only matches. This binds a
50 // Python type-ignore to its statement, rather than to a reusable comment.
51 source: finding.lines.replaceAll('\r\n', '\n'),
52 count: 1,
53 };
54 const existing = entries.get(key(entry));
55 if (existing) existing.count += 1;
56 else entries.set(key(entry), entry);
57 }
58 return [...entries.values()].sort((a, b) => key(a).localeCompare(key(b), 'en'));
59 }
60
61 export function compare(current, baseline) {
62 const allowed = new Map();
63 for (const entry of baseline) {
64 if (
65 typeof entry.ruleId !== 'string' ||
66 typeof entry.file !== 'string' ||
67 typeof entry.source !== 'string' ||
68 !Number.isSafeInteger(entry.count) ||
69 entry.count < 1 ||
70 allowed.has(key(entry))
71 ) {
72 throw new Error('Invalid or duplicate anti-slop baseline entry');
73 }
74 allowed.set(key(entry), entry.count);
75 }
76 const observed = new Map(current.map(entry => [key(entry), entry.count]));
77 return {
78 added: current.filter(entry => entry.count > (allowed.get(key(entry)) ?? 0)),
79 stale: baseline.filter(entry => entry.count > (observed.get(key(entry)) ?? 0)),
80 };
81 }
82
83 function main() {
84 const args = process.argv.slice(2);
85 if (args.length > 1 || (args.length === 1 && args[0] !== '--prune')) {
86 throw new Error('Usage: node scripts/check-antislop.mjs [--prune]');
87 }
88 const findings = scan();
89 const current = inventory(findings);
90 const baselinePath = resolve(root, '.ast-grep/baseline.json');
91 const baseline = JSON.parse(readFileSync(baselinePath, 'utf8'));
92 const { added, stale } = compare(current, baseline);
93 for (const entry of added) {
94 const finding = findings.find(item => key(inventory([item])[0]) === key(entry));
95 console.error(
96 `${entry.file}:${finding.range.start.line + 1}: ${entry.ruleId}: ${finding.message}`
97 );
98 console.error(entry.source);
99 }
100 if (added.length) {
101 console.error('New anti-slop findings. Fix them or justify a rule-specific suppression.');
102 process.exitCode = 1;
103 } else if (args[0] === '--prune') {
104 writeFileSync(baselinePath, `${JSON.stringify(current, null, 2)}\n`);
105 console.log('Pruned resolved anti-slop baseline entries. No allowances added.');
106 } else if (stale.length) {
107 console.error(
108 'Resolved anti-slop findings remain in the baseline. Run pnpm lint:antislop:prune.'
109 );
110 process.exitCode = 1;
111 } else {
112 console.log(`Anti-slop passed (${findings.length} existing findings; no new findings).`);
113 }
114 }
115
116 if (import.meta.main) main();