Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cut-discovery-fs-probes.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 6 additions & 1 deletion packages/intent/src/discovery/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export interface CreatePackageRegistrarOptions {

export function createPackageRegistrar(opts: CreatePackageRegistrarOptions) {
const attemptedPackageRoots = new Set<string>()
const attemptedRawDirs = new Set<string>()
const scannedNodeModulesDirs = new Set<string>()

function shouldAttemptPackageRoot(dirPath: string): boolean {
const key = opts.getFsIdentity(dirPath)
attemptedRawDirs.add(key)
if (attemptedPackageRoots.has(key)) return false
attemptedPackageRoots.add(key)
return true
Expand Down Expand Up @@ -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}`)
Expand Down
4 changes: 4 additions & 0 deletions packages/intent/src/discovery/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ function createPackageKindResolver(
workspacePackageKeys: Set<string>,
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'
Expand Down
6 changes: 4 additions & 2 deletions packages/intent/src/skills/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ export function rewriteSkillLoadPaths({
projectRoot: string
skills: Array<SkillEntry>
}): 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) {
Expand Down
31 changes: 31 additions & 0 deletions packages/intent/tests/repeated-work.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { execFileSync } from 'node:child_process'
import {
existsSync,
lstatSync,
mkdirSync,
mkdtempSync,
Expand Down Expand Up @@ -30,6 +31,7 @@ vi.mock('node:fs', async (importOriginal) => {
const fs = await importOriginal<typeof NodeFs>()
return {
...fs,
existsSync: vi.fn(fs.existsSync),
readFileSync: vi.fn(fs.readFileSync),
readdirSync: vi.fn(fs.readdirSync),
lstatSync: vi.fn(fs.lstatSync),
Expand Down Expand Up @@ -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'),
Expand Down
Loading