|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Fails if a workspace route can reach the executable tool registry. |
| 4 | + * |
| 5 | + * `@/tools/registry` is a barrel over 4,300+ tools whose `ToolConfig`s hold |
| 6 | + * closures (`request.headers`, `transformResponse`, `directExecution`). Those |
| 7 | + * closures reach every integration's SDK client and parser, so reaching the |
| 8 | + * barrel costs ~4,700 modules — it was 71-82% of every workspace route's module |
| 9 | + * graph until those edges were cut. |
| 10 | + * |
| 11 | + * Client-reachable code reads `@/tools/metadata`, `@/tools/metadata-outputs` or |
| 12 | + * `@/tools/tool-ids` instead. See |
| 13 | + * `.agents/skills/tool-registry-boundary/SKILL.md`. |
| 14 | + * |
| 15 | + * This regresses silently and cheaply: any file under a route can import one |
| 16 | + * helper from a module that happens to import `getTool`, and the whole registry |
| 17 | + * comes back. That is exactly how it got there — `providers/utils.ts` pulled it |
| 18 | + * in through `mergeToolParameters`, and `mcp-dynamic-args.tsx` through |
| 19 | + * `formatParameterLabel`. Neither import looks remotely suspicious at the call |
| 20 | + * site, which is why this is a lint and not a convention. |
| 21 | + * |
| 22 | + * Usage: |
| 23 | + * bun run scripts/check-tool-registry-boundary.ts |
| 24 | + * bun run scripts/check-tool-registry-boundary.ts --verbose # print counts |
| 25 | + */ |
| 26 | +import { existsSync, readFileSync, statSync } from 'node:fs' |
| 27 | +import { dirname, join, relative, resolve } from 'node:path' |
| 28 | +import { fileURLToPath } from 'node:url' |
| 29 | + |
| 30 | +const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)) |
| 31 | +const ROOT = resolve(SCRIPT_DIR, '..') |
| 32 | +const APP = join(ROOT, 'apps/sim') |
| 33 | + |
| 34 | +/** Module no client-reachable entry may reach. */ |
| 35 | +const FORBIDDEN = join(APP, 'tools/registry.ts') |
| 36 | + |
| 37 | +/** |
| 38 | + * Entries guarded. These are the routes a developer works in daily and the |
| 39 | + * shared shell they all mount inside; the shell is the one that matters most, |
| 40 | + * since anything it reaches is paid for by every route. |
| 41 | + */ |
| 42 | +const ENTRIES = [ |
| 43 | + 'app/workspace/layout.tsx', |
| 44 | + 'app/workspace/[workspaceId]/w/page.tsx', |
| 45 | + 'app/workspace/[workspaceId]/logs/page.tsx', |
| 46 | + 'app/workspace/[workspaceId]/tables/page.tsx', |
| 47 | + 'app/workspace/[workspaceId]/files/page.tsx', |
| 48 | +] |
| 49 | + |
| 50 | +const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs'] |
| 51 | + |
| 52 | +/** |
| 53 | + * Matches value imports and re-exports, skipping `import type` — a type-only |
| 54 | + * edge is erased at compile time and costs nothing at runtime. |
| 55 | + */ |
| 56 | +const IMPORT_RE = /(?:^|\n)\s*import\s+(?!type\b)(?:[\s\S]*?from\s*)?['"]([^'"]+)['"]/g |
| 57 | +const REEXPORT_RE = /(?:^|\n)\s*export\s+(?!type\b)(?:\*|\{[\s\S]*?\})\s*from\s*['"]([^'"]+)['"]/g |
| 58 | + |
| 59 | +/** Resolves `@/` and relative specifiers. Bare package specifiers are ignored. */ |
| 60 | +function resolveSpecifier(specifier: string, importer: string): string | null { |
| 61 | + let base: string |
| 62 | + if (specifier.startsWith('@/')) base = join(APP, specifier.slice(2)) |
| 63 | + else if (specifier.startsWith('.')) base = resolve(dirname(importer), specifier) |
| 64 | + else return null |
| 65 | + |
| 66 | + for (const ext of EXTENSIONS) { |
| 67 | + if (existsSync(base + ext)) return base + ext |
| 68 | + } |
| 69 | + if (existsSync(base) && statSync(base).isDirectory()) { |
| 70 | + for (const ext of EXTENSIONS) { |
| 71 | + const indexPath = join(base, `index${ext}`) |
| 72 | + if (existsSync(indexPath)) return indexPath |
| 73 | + } |
| 74 | + } |
| 75 | + return null |
| 76 | +} |
| 77 | + |
| 78 | +interface Walk { |
| 79 | + reachable: Set<string> |
| 80 | + importedBy: Map<string, string> |
| 81 | +} |
| 82 | + |
| 83 | +function walk(entry: string): Walk { |
| 84 | + const reachable = new Set<string>() |
| 85 | + const importedBy = new Map<string, string>() |
| 86 | + const queue = [entry] |
| 87 | + reachable.add(entry) |
| 88 | + |
| 89 | + while (queue.length > 0) { |
| 90 | + const file = queue.pop() as string |
| 91 | + let source: string |
| 92 | + try { |
| 93 | + source = readFileSync(file, 'utf8') |
| 94 | + } catch { |
| 95 | + continue |
| 96 | + } |
| 97 | + for (const pattern of [IMPORT_RE, REEXPORT_RE]) { |
| 98 | + pattern.lastIndex = 0 |
| 99 | + let match = pattern.exec(source) |
| 100 | + while (match !== null) { |
| 101 | + const resolved = resolveSpecifier(match[1], file) |
| 102 | + if (resolved && !reachable.has(resolved)) { |
| 103 | + reachable.add(resolved) |
| 104 | + importedBy.set(resolved, file) |
| 105 | + queue.push(resolved) |
| 106 | + } |
| 107 | + match = pattern.exec(source) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return { reachable, importedBy } |
| 113 | +} |
| 114 | + |
| 115 | +/** Walks parent links back to the entry so the offending edge is obvious. */ |
| 116 | +function explainChain({ importedBy }: Walk, target: string): string[] { |
| 117 | + const chain: string[] = [] |
| 118 | + let current: string | undefined = target |
| 119 | + while (current) { |
| 120 | + chain.push(relative(ROOT, current)) |
| 121 | + current = importedBy.get(current) |
| 122 | + } |
| 123 | + return chain.reverse() |
| 124 | +} |
| 125 | + |
| 126 | +function main() { |
| 127 | + const verbose = process.argv.includes('--verbose') |
| 128 | + const failures: string[] = [] |
| 129 | + |
| 130 | + for (const entry of ENTRIES) { |
| 131 | + const entryPath = join(APP, entry) |
| 132 | + if (!existsSync(entryPath)) { |
| 133 | + console.error(`❌ Guarded entry no longer exists: ${entry}`) |
| 134 | + console.error(' Update ENTRIES in scripts/check-tool-registry-boundary.ts.') |
| 135 | + process.exit(1) |
| 136 | + } |
| 137 | + |
| 138 | + const result = walk(entryPath) |
| 139 | + if (result.reachable.has(FORBIDDEN)) { |
| 140 | + failures.push(entry) |
| 141 | + console.error(`\n❌ ${entry} can reach @/tools/registry via:`) |
| 142 | + for (const step of explainChain(result, FORBIDDEN)) { |
| 143 | + console.error(` ${step}`) |
| 144 | + } |
| 145 | + } else if (verbose) { |
| 146 | + console.log(`✓ ${entry} — ${result.reachable.size} modules, registry unreachable`) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (failures.length > 0) { |
| 151 | + console.error( |
| 152 | + `\n${failures.length} route(s) reach the executable tool registry, which adds ~4,700 modules to each.` |
| 153 | + ) |
| 154 | + console.error( |
| 155 | + 'Read the metadata instead: `@/tools/metadata` (params), `@/tools/metadata-outputs`' |
| 156 | + ) |
| 157 | + console.error( |
| 158 | + '(outputs), or `@/tools/tool-ids` (existence/resolution). Only code that executes a tool' |
| 159 | + ) |
| 160 | + console.error('may import `getTool`. See .agents/skills/tool-registry-boundary/SKILL.md.') |
| 161 | + process.exit(1) |
| 162 | + } |
| 163 | + |
| 164 | + console.log(`✓ tool registry stays out of ${ENTRIES.length} workspace route graphs`) |
| 165 | +} |
| 166 | + |
| 167 | +main() |
0 commit comments