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, 04:45 UTC
| 1 | import * as fs from 'node:fs'; |
| 2 | import * as path from 'node:path'; |
| 3 | |
| 4 | export interface BoundarySite { |
| 5 | readonly rule: string; |
| 6 | readonly reason: string; |
| 7 | readonly target: string; |
| 8 | } |
| 9 | |
| 10 | export type BoundaryManifest = Record<string, BoundarySite[]>; |
| 11 | |
| 12 | export interface CollectedBoundaries { |
| 13 | readonly manifest: BoundaryManifest; |
| 14 | readonly errors: readonly string[]; |
| 15 | } |
| 16 | |
| 17 | const NEXT_LINE_FORM = /\/\/ eslint-disable-next-line (?<rules>[\w@/,\- ]+?) -- (?<reason>.+?)\s*$/; |
| 18 | |
| 19 | const boundarySiteKey = (site: BoundarySite): string => |
| 20 | JSON.stringify([site.rule, site.reason, site.target]); |
| 21 | |
| 22 | const compareBoundarySites = (left: BoundarySite, right: BoundarySite): number => { |
| 23 | const leftKey = boundarySiteKey(left); |
| 24 | const rightKey = boundarySiteKey(right); |
| 25 | return leftKey < rightKey ? -1 : leftKey > rightKey ? 1 : 0; |
| 26 | }; |
| 27 | |
| 28 | const formatBoundarySite = (site: BoundarySite): string => |
| 29 | `"${site.rule}" -- ${site.reason} -> ${JSON.stringify(site.target)}`; |
| 30 | |
| 31 | export const listSourceFiles = (dir: string): string[] => |
| 32 | fs.readdirSync(dir, { withFileTypes: true }).flatMap(entry => { |
| 33 | const full = path.join(dir, entry.name); |
| 34 | if (entry.isDirectory()) return listSourceFiles(full); |
| 35 | return entry.isFile() && /\.tsx?$/.test(entry.name) ? [full] : []; |
| 36 | }); |
| 37 | |
| 38 | export const parseBoundarySites = ( |
| 39 | relativePath: string, |
| 40 | source: string |
| 41 | ): { readonly sites: readonly BoundarySite[]; readonly errors: readonly string[] } => { |
| 42 | const lines = source.split('\n'); |
| 43 | const sites: BoundarySite[] = []; |
| 44 | const errors: string[] = []; |
| 45 | |
| 46 | lines.forEach((line, index) => { |
| 47 | // oxlint honors both the eslint-disable and oxlint-disable spellings, so |
| 48 | // the scanner must see both; only the eslint-disable next-line form below |
| 49 | // is accepted into the manifest. |
| 50 | if (!/(?:es|ox)lint-disable/.test(line)) return; |
| 51 | |
| 52 | const location = `${relativePath}:${index + 1}`; |
| 53 | const match = NEXT_LINE_FORM.exec(line); |
| 54 | if (!match?.groups) { |
| 55 | errors.push( |
| 56 | `${location}: only "// eslint-disable-next-line <rules> -- <reason>" is allowed ` + |
| 57 | '(no file-wide disables, no bare disables without a justification, ' + |
| 58 | 'no oxlint-disable spellings).' |
| 59 | ); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const target = lines[index + 1]?.trim(); |
| 64 | if (!target) { |
| 65 | errors.push(`${location}: eslint-disable-next-line must govern a non-empty next line.`); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | const reason = match.groups.reason.trim(); |
| 70 | for (const rule of match.groups.rules.split(',').map(rule => rule.trim())) { |
| 71 | sites.push({ rule, reason, target }); |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | return { errors, sites }; |
| 76 | }; |
| 77 | |
| 78 | export const collectBoundaryManifest = ( |
| 79 | packageRoot: string, |
| 80 | srcRoot: string |
| 81 | ): CollectedBoundaries => { |
| 82 | const manifest: BoundaryManifest = {}; |
| 83 | const errors: string[] = []; |
| 84 | |
| 85 | for (const file of listSourceFiles(srcRoot).sort()) { |
| 86 | const relative = path.relative(packageRoot, file).split(path.sep).join('/'); |
| 87 | const parsed = parseBoundarySites(relative, fs.readFileSync(file, 'utf8')); |
| 88 | errors.push(...parsed.errors); |
| 89 | |
| 90 | if (parsed.sites.length > 0) { |
| 91 | manifest[relative] = [...parsed.sites].sort(compareBoundarySites); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return { errors, manifest }; |
| 96 | }; |
| 97 | |
| 98 | const countSites = (sites: readonly BoundarySite[]): Map<string, number> => { |
| 99 | const counts = new Map<string, number>(); |
| 100 | for (const site of sites) { |
| 101 | const key = boundarySiteKey(site); |
| 102 | counts.set(key, (counts.get(key) ?? 0) + 1); |
| 103 | } |
| 104 | return counts; |
| 105 | }; |
| 106 | |
| 107 | export const compareBoundaryManifests = ( |
| 108 | found: BoundaryManifest, |
| 109 | registered: BoundaryManifest |
| 110 | ): readonly string[] => { |
| 111 | const errors: string[] = []; |
| 112 | const files = [...new Set([...Object.keys(found), ...Object.keys(registered)])].sort(); |
| 113 | |
| 114 | for (const file of files) { |
| 115 | const foundSites = found[file] ?? []; |
| 116 | const registeredSites = registered[file] ?? []; |
| 117 | const foundCounts = countSites(foundSites); |
| 118 | const registeredCounts = countSites(registeredSites); |
| 119 | const sitesByKey = new Map( |
| 120 | [...foundSites, ...registeredSites].map(site => [boundarySiteKey(site), site]) |
| 121 | ); |
| 122 | |
| 123 | for (const key of [...sitesByKey.keys()].sort()) { |
| 124 | const site = sitesByKey.get(key)!; |
| 125 | const foundCount = foundCounts.get(key) ?? 0; |
| 126 | const registeredCount = registeredCounts.get(key) ?? 0; |
| 127 | |
| 128 | if (foundCount > registeredCount) { |
| 129 | errors.push( |
| 130 | `${file}: ${foundCount}x ${formatBoundarySite(site)} found, ${registeredCount} registered. ` + |
| 131 | 'New or relocated eslint-disables are not allowed in CLI src: thread the ' + |
| 132 | 'effect/Path or effect/FileSystem service, effect/Config, or Result.try instead — ' + |
| 133 | 'see "Effect Boundary Policy" in ts/packages/cli/AGENTS.md. If this is genuinely a new ' + |
| 134 | 'runtime boundary, regenerate the manifest with ' + |
| 135 | '`pnpm run validate:boundaries -- --update` and justify the boundary in your PR.' |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | if (foundCount < registeredCount) { |
| 140 | errors.push( |
| 141 | `${file}: manifest registers ${registeredCount}x ${formatBoundarySite(site)} but only ` + |
| 142 | `${foundCount} exist. Nice — a boundary went away or moved. Regenerate the manifest ` + |
| 143 | 'with `pnpm run validate:boundaries -- --update`.' |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return errors; |
| 150 | }; |
| 151 | |
| 152 | export const countBoundarySites = (manifest: BoundaryManifest): number => |
| 153 | Object.values(manifest).reduce((sum, sites) => sum + sites.length, 0); |