|
| 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, readdirSync, 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 | + * Root the guard walks: every `page.tsx` and `layout.tsx` under the workspace app. |
| 39 | + * |
| 40 | + * Discovered rather than listed. A hardcoded list goes stale silently — the |
| 41 | + * first version of this guard named `app/workspace/layout.tsx` as "the shared |
| 42 | + * shell", but that file only wraps `SocketProvider`; the real shell is |
| 43 | + * `app/workspace/[workspaceId]/layout.tsx`, which was never checked. |
| 44 | + * |
| 45 | + * Layouts must be enumerated separately because Next.js composes them by |
| 46 | + * convention — a page does not `import` its layout, so walking pages alone never |
| 47 | + * reaches layout modules even though every route pays for them. |
| 48 | + */ |
| 49 | +const ENTRY_ROOT = 'app/workspace' |
| 50 | +const ENTRY_FILENAMES = new Set(['page.tsx', 'layout.tsx']) |
| 51 | + |
| 52 | +function collectEntries(dir: string, found: string[] = []): string[] { |
| 53 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 54 | + const full = join(dir, entry.name) |
| 55 | + if (entry.isDirectory()) collectEntries(full, found) |
| 56 | + else if (ENTRY_FILENAMES.has(entry.name)) found.push(relative(APP, full)) |
| 57 | + } |
| 58 | + return found |
| 59 | +} |
| 60 | + |
| 61 | +const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs'] |
| 62 | + |
| 63 | +/** |
| 64 | + * Matches value imports and re-exports, skipping `import type` and |
| 65 | + * `export type` — a type-only edge is erased at compile time and costs nothing. |
| 66 | + * |
| 67 | + * `REEXPORT_RE` allows an alias after the star so `export * as ns from` is not |
| 68 | + * missed, and `DYNAMIC_IMPORT_RE` covers `import('…')`. A dynamic import splits |
| 69 | + * the registry into its own chunk rather than the route's initial one, but it |
| 70 | + * still puts 4,300 tools' worth of executable config on a client path, so it |
| 71 | + * counts as reaching it — and the settings route's registry edge hid behind |
| 72 | + * exactly such an import. |
| 73 | + * |
| 74 | + * `REQUIRE_RE` matters for the same reason: this codebase uses lazy |
| 75 | + * `require('@/…')` to break import cycles (`tools/params.ts` reaches `@/blocks` |
| 76 | + * that way), and those edges are as real as static ones. |
| 77 | + */ |
| 78 | +const IMPORT_RE = /(?:^|\n)\s*import\s+(?!type\b)(?:[\s\S]*?from\s*)?['"]([^'"]+)['"]/g |
| 79 | +const REEXPORT_RE = |
| 80 | + /(?:^|\n)\s*export\s+(?!type\b)(?:\*(?:\s+as\s+[\w$]+)?|\{[\s\S]*?\})\s*from\s*['"]([^'"]+)['"]/g |
| 81 | +const DYNAMIC_IMPORT_RE = /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g |
| 82 | +const REQUIRE_RE = /\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g |
| 83 | + |
| 84 | +/** Resolves `@/` and relative specifiers. Bare package specifiers are ignored. */ |
| 85 | +function resolveSpecifier(specifier: string, importer: string): string | null { |
| 86 | + let base: string |
| 87 | + if (specifier.startsWith('@/')) base = join(APP, specifier.slice(2)) |
| 88 | + else if (specifier.startsWith('.')) base = resolve(dirname(importer), specifier) |
| 89 | + else return null |
| 90 | + |
| 91 | + // An already-extensioned specifier (`@/tools/registry.ts`) resolves as-is. |
| 92 | + // Probing only `base + ext` would miss it and silently drop the edge — and |
| 93 | + // extensionful `@/` imports do exist in this repo. |
| 94 | + if (existsSync(base) && statSync(base).isFile()) return base |
| 95 | + |
| 96 | + for (const ext of EXTENSIONS) { |
| 97 | + if (existsSync(base + ext)) return base + ext |
| 98 | + } |
| 99 | + if (existsSync(base) && statSync(base).isDirectory()) { |
| 100 | + for (const ext of EXTENSIONS) { |
| 101 | + const indexPath = join(base, `index${ext}`) |
| 102 | + if (existsSync(indexPath)) return indexPath |
| 103 | + } |
| 104 | + } |
| 105 | + return null |
| 106 | +} |
| 107 | + |
| 108 | +interface Walk { |
| 109 | + reachable: Set<string> |
| 110 | + importedBy: Map<string, string> |
| 111 | +} |
| 112 | + |
| 113 | +function walk(entry: string): Walk { |
| 114 | + const reachable = new Set<string>() |
| 115 | + const importedBy = new Map<string, string>() |
| 116 | + const queue = [entry] |
| 117 | + reachable.add(entry) |
| 118 | + |
| 119 | + while (queue.length > 0) { |
| 120 | + const file = queue.pop() as string |
| 121 | + let source: string |
| 122 | + try { |
| 123 | + source = readFileSync(file, 'utf8') |
| 124 | + } catch { |
| 125 | + continue |
| 126 | + } |
| 127 | + for (const pattern of [IMPORT_RE, REEXPORT_RE, DYNAMIC_IMPORT_RE, REQUIRE_RE]) { |
| 128 | + pattern.lastIndex = 0 |
| 129 | + let match = pattern.exec(source) |
| 130 | + while (match !== null) { |
| 131 | + const resolved = resolveSpecifier(match[1], file) |
| 132 | + if (resolved && !reachable.has(resolved)) { |
| 133 | + reachable.add(resolved) |
| 134 | + importedBy.set(resolved, file) |
| 135 | + queue.push(resolved) |
| 136 | + } |
| 137 | + match = pattern.exec(source) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return { reachable, importedBy } |
| 143 | +} |
| 144 | + |
| 145 | +/** Walks parent links back to the entry so the offending edge is obvious. */ |
| 146 | +function explainChain({ importedBy }: Walk, target: string): string[] { |
| 147 | + const chain: string[] = [] |
| 148 | + let current: string | undefined = target |
| 149 | + while (current) { |
| 150 | + chain.push(relative(ROOT, current)) |
| 151 | + current = importedBy.get(current) |
| 152 | + } |
| 153 | + return chain.reverse() |
| 154 | +} |
| 155 | + |
| 156 | +function main() { |
| 157 | + const verbose = process.argv.includes('--verbose') |
| 158 | + const failures: string[] = [] |
| 159 | + |
| 160 | + const entryRoot = join(APP, ENTRY_ROOT) |
| 161 | + if (!existsSync(entryRoot)) { |
| 162 | + console.error(`❌ ${ENTRY_ROOT} no longer exists — update ENTRY_ROOT in this script.`) |
| 163 | + process.exit(1) |
| 164 | + } |
| 165 | + const entries = collectEntries(entryRoot).sort() |
| 166 | + if (entries.length === 0) { |
| 167 | + console.error( |
| 168 | + `❌ No page/layout entries found under ${ENTRY_ROOT}. Refusing to pass vacuously.` |
| 169 | + ) |
| 170 | + process.exit(1) |
| 171 | + } |
| 172 | + |
| 173 | + for (const entry of entries) { |
| 174 | + const entryPath = join(APP, entry) |
| 175 | + const result = walk(entryPath) |
| 176 | + if (result.reachable.has(FORBIDDEN)) { |
| 177 | + failures.push(entry) |
| 178 | + console.error(`\n❌ ${entry} can reach @/tools/registry via:`) |
| 179 | + for (const step of explainChain(result, FORBIDDEN)) { |
| 180 | + console.error(` ${step}`) |
| 181 | + } |
| 182 | + } else if (verbose) { |
| 183 | + console.log(`✓ ${entry} — ${result.reachable.size} modules, registry unreachable`) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if (failures.length > 0) { |
| 188 | + console.error( |
| 189 | + `\n${failures.length} route(s) reach the executable tool registry, which adds ~4,700 modules to each.` |
| 190 | + ) |
| 191 | + console.error( |
| 192 | + 'Read the metadata instead: `@/tools/metadata` (params), `@/tools/metadata-outputs`' |
| 193 | + ) |
| 194 | + console.error( |
| 195 | + '(outputs), or `@/tools/tool-ids` (existence/resolution). Only code that executes a tool' |
| 196 | + ) |
| 197 | + console.error('may import `getTool`. See .agents/skills/tool-registry-boundary/SKILL.md.') |
| 198 | + process.exit(1) |
| 199 | + } |
| 200 | + |
| 201 | + console.log(`✓ tool registry stays out of ${entries.length} workspace page/layout graphs`) |
| 202 | +} |
| 203 | + |
| 204 | +main() |
0 commit comments