From f6b436e333aff8a06813fdd6e160600afa974e18 Mon Sep 17 00:00:00 2001 From: Sarah Gerrard Date: Sun, 18 Oct 2026 18:13:32 -0700 Subject: [PATCH] perf: skip redundant filesystem probes during discovery and load path rewriting --- .changeset/cut-discovery-fs-probes.md | 5 ++++ packages/intent/src/discovery/register.ts | 7 ++++- packages/intent/src/discovery/scanner.ts | 4 +++ packages/intent/src/skills/paths.ts | 6 ++-- packages/intent/tests/repeated-work.test.ts | 31 +++++++++++++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 .changeset/cut-discovery-fs-probes.md diff --git a/.changeset/cut-discovery-fs-probes.md b/.changeset/cut-discovery-fs-probes.md new file mode 100644 index 00000000..79dd8905 --- /dev/null +++ b/.changeset/cut-discovery-fs-probes.md @@ -0,0 +1,5 @@ +--- +'@tanstack/intent': patch +--- + +Skip redundant filesystem probes when scanning packages and rewriting skill load paths during `intent list` and `intent load`; behavior does not change. diff --git a/packages/intent/src/discovery/register.ts b/packages/intent/src/discovery/register.ts index 90516a07..14807664 100644 --- a/packages/intent/src/discovery/register.ts +++ b/packages/intent/src/discovery/register.ts @@ -42,10 +42,12 @@ export interface CreatePackageRegistrarOptions { export function createPackageRegistrar(opts: CreatePackageRegistrarOptions) { const attemptedPackageRoots = new Set() + const attemptedRawDirs = new Set() const scannedNodeModulesDirs = new Set() function shouldAttemptPackageRoot(dirPath: string): boolean { const key = opts.getFsIdentity(dirPath) + attemptedRawDirs.add(key) if (attemptedPackageRoots.has(key)) return false attemptedPackageRoots.add(key) return true @@ -80,11 +82,14 @@ export function createPackageRegistrar(opts: CreatePackageRegistrarOptions) { fallbackName: string, source: IntentPackage['source'] = 'local', ): boolean { - if (!shouldAttemptPackageRoot(dirPath)) return false + if (attemptedRawDirs.has(dirPath)) return false + attemptedRawDirs.add(dirPath) const skillsDir = join(dirPath, 'skills') if (!opts.exists(skillsDir)) return false + if (!shouldAttemptPackageRoot(dirPath)) return false + const pkgJson = opts.readPkgJson(dirPath) if (!pkgJson) { opts.warnings.push(`Could not read package.json for ${dirPath}`) diff --git a/packages/intent/src/discovery/scanner.ts b/packages/intent/src/discovery/scanner.ts index 0cd85218..93406be9 100644 --- a/packages/intent/src/discovery/scanner.ts +++ b/packages/intent/src/discovery/scanner.ts @@ -624,6 +624,10 @@ function createPackageKindResolver( workspacePackageKeys: Set, getFsIdentity: (path: string) => string, ): (packageRoot: string) => IntentPackage['kind'] { + if (workspacePackageKeys.size === 0) { + return () => 'npm' + } + return (packageRoot: string): IntentPackage['kind'] => { return workspacePackageKeys.has(getFsIdentity(packageRoot)) ? 'workspace' diff --git a/packages/intent/src/skills/paths.ts b/packages/intent/src/skills/paths.ts index 6980a02e..7092140a 100644 --- a/packages/intent/src/skills/paths.ts +++ b/packages/intent/src/skills/paths.ts @@ -50,9 +50,11 @@ export function rewriteSkillLoadPaths({ projectRoot: string skills: Array }): void { + const topLevelPackageRoot = + packageName !== '' ? join(projectRoot, 'node_modules', packageName) : '' const hasStableSymlink = - packageName !== '' && - existsSync(join(projectRoot, 'node_modules', packageName)) + topLevelPackageRoot !== '' && + (packageRoot === topLevelPackageRoot || existsSync(topLevelPackageRoot)) for (const skill of skills) { if (hasStableSymlink) { diff --git a/packages/intent/tests/repeated-work.test.ts b/packages/intent/tests/repeated-work.test.ts index 13eb4795..a9444971 100644 --- a/packages/intent/tests/repeated-work.test.ts +++ b/packages/intent/tests/repeated-work.test.ts @@ -1,5 +1,6 @@ import { execFileSync } from 'node:child_process' import { + existsSync, lstatSync, mkdirSync, mkdtempSync, @@ -30,6 +31,7 @@ vi.mock('node:fs', async (importOriginal) => { const fs = await importOriginal() return { ...fs, + existsSync: vi.fn(fs.existsSync), readFileSync: vi.fn(fs.readFileSync), readdirSync: vi.fn(fs.readdirSync), lstatSync: vi.fn(fs.lstatSync), @@ -91,12 +93,41 @@ describe('command work budgets', () => { expect(resolveIntentSkill('example#core', { cwd: root }).skillName).toBe( 'core', ) + expect( + vi + .mocked(existsSync) + .mock.calls.filter( + ([path]) => path === join(root, 'node_modules', 'example'), + ), + ).toHaveLength(0) expect(loadIntentSkill('example#core', { cwd: root }).content).toContain( 'Guide.', ) expect(metadataRead).not.toHaveBeenCalled() }) + it('skips lstat on node_modules packages without a skills directory during list', () => { + write( + join(root, 'package.json'), + JSON.stringify({ + name: 'consumer', + dependencies: { example: '1.0.0', plain: '1.0.0' }, + }), + ) + writePackage(join(root, 'node_modules', 'example'), 'example') + const plainDir = join(root, 'node_modules', 'plain') + write( + join(plainDir, 'package.json'), + JSON.stringify({ name: 'plain', version: '1.0.0' }), + ) + vi.clearAllMocks() + + expect(listIntentSkills({ cwd: root }).skills).toHaveLength(1) + expect( + vi.mocked(lstatSync).mock.calls.filter(([path]) => path === plainDir), + ).toHaveLength(0) + }) + it('indexes each skill path once when matching many artifact entries by name', async () => { write( join(root, 'package.json'),