From 597ff49b9181895e779b7c81bd484eb41b1c81bf Mon Sep 17 00:00:00 2001 From: ladybluenotes Date: Sun, 22 Nov 2026 11:49:52 -0700 Subject: [PATCH 1/2] chore(benchmarks): add review, typed-example validate, and validate --fix benchmarks Add a shared CLI fixture helper that runs the built CLI in process, checks each fixture once during setup, and works under both the tinybench runner (bench options) and the CodSpeed runner (suite hooks). New cases: intent review --json with 20 distinct source patterns and with one shared pattern, intent validate with TypeScript code blocks for one package and for a monorepo, and intent validate --fix with a pending frontmatter migration. --- benchmarks/intent/helpers.ts | 200 ++++++++++++++++++++++++++-- benchmarks/intent/review.bench.ts | 77 +++++++++++ benchmarks/intent/validate.bench.ts | 159 +++++++++++++++++++++- 3 files changed, 424 insertions(+), 12 deletions(-) create mode 100644 benchmarks/intent/review.bench.ts diff --git a/benchmarks/intent/helpers.ts b/benchmarks/intent/helpers.ts index 859e74f4..f5a1ba89 100644 --- a/benchmarks/intent/helpers.ts +++ b/benchmarks/intent/helpers.ts @@ -1,10 +1,11 @@ -import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs' +import { execFileSync } from 'node:child_process' +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { basename, dirname, join } from 'node:path' -let builtCliMainPromise: Promise< - (argv?: Array) => Promise -> | null = null +type CliMain = (argv?: Array, runtime?: object) => Promise + +let builtCliMainPromise: Promise | null = null type CliRunnerOptions = { cwd: string @@ -50,6 +51,18 @@ export function createBenchOptions( } } +export function createSlowBenchOptions( + setup: () => void | Promise, + teardown: () => void | Promise, +) { + return { + warmupIterations: 3, + time: 3_000, + setup, + teardown, + } +} + export function createConsoleSilencer() { let snapshot: ConsoleSnapshot | null = null @@ -82,7 +95,7 @@ export function createConsoleSilencer() { } export function createCliRunner(options: CliRunnerOptions) { - let main: ((argv?: Array) => Promise) | null = null + let main: CliMain | null = null let previousCwd = '' let previousGlobalNodeModules: string | undefined @@ -116,12 +129,12 @@ export function createCliRunner(options: CliRunnerOptions) { previousGlobalNodeModules = undefined main = null }, - async run(argv: Array) { + async run(argv: Array, runtime?: object) { if (!main) { throw new Error('CLI runner must be set up before running benchmarks') } - const exitCode = await main(argv) + const exitCode = await main(argv, runtime) if (exitCode !== 0) { throw new Error( `intent ${argv.join(' ')} failed with exit code ${exitCode}`, @@ -131,9 +144,7 @@ export function createCliRunner(options: CliRunnerOptions) { } } -async function loadBuiltCliMain(): Promise< - (argv?: Array) => Promise -> { +async function loadBuiltCliMain(): Promise { builtCliMainPromise ??= import('../../packages/intent/dist/cli.mjs').then( (module) => { if (typeof module.main !== 'function') { @@ -142,13 +153,102 @@ async function loadBuiltCliMain(): Promise< ) } - return module.main as (argv?: Array) => Promise + return module.main as CliMain }, ) return builtCliMainPromise } +export function createCliFixture( + name: string, + write: ( + root: string, + capture: (argv: Array, runtime?: object) => Promise, + ) => void | Promise, +) { + const consoleSilencer = createConsoleSilencer() + let fixture: { + root: string + runner: ReturnType + } | null = null + + function current() { + if (!fixture) { + throw new Error(`Fixture ${name} must be set up before use`) + } + + return fixture + } + + async function capture( + argv: Array, + runtime?: object, + ): Promise { + const silenced = console.log + const lines: Array = [] + console.log = (...values: Array) => { + lines.push(values.join(' ')) + } + try { + await current().runner.run(argv, runtime) + } finally { + console.log = silenced + } + return lines.join('\n') + } + + function teardown(): void { + if (fixture) { + fixture.runner.teardown() + rmSync(fixture.root, { recursive: true, force: true }) + fixture = null + } + + consoleSilencer.restore() + } + + return { + get root(): string { + return current().root + }, + async setup(): Promise { + if (fixture) return + + consoleSilencer.silence() + const root = createTempDir(name) + fixture = { root, runner: createCliRunner({ cwd: root }) } + try { + await fixture.runner.setup() + await write(root, capture) + } catch (err) { + teardown() + throw err + } + }, + teardown, + run(argv: Array, runtime?: object): Promise { + return current().runner.run(argv, runtime) + }, + } +} + +export function git(root: string, ...args: Array): void { + execFileSync( + 'git', + [ + '-c', + 'core.fsmonitor=false', + '-c', + 'user.name=Benchmark', + '-c', + 'user.email=benchmark@example.invalid', + ...args, + ], + { cwd: root, stdio: 'ignore' }, + ) +} + export function createTempDir(name: string): string { return mkdtempSync(join(tmpdir(), `intent-bench-${name}-`)) } @@ -252,3 +352,81 @@ export function writePackage( }) } } + +export function writeExampleLibrary( + packageRoot: string, + name: string, + dependency?: string, +): void { + writeJson(join(packageRoot, 'package.json'), { + name, + version: '1.0.0', + repository: `https://github.com/example/${basename(name)}`, + keywords: ['tanstack-intent'], + files: ['dist', 'skills'], + devDependencies: { + '@tanstack/intent': 'workspace:*', + }, + }) + writeFile( + join(packageRoot, 'src', 'index.ts'), + [ + ...(dependency + ? [ + `import type { RetryOptions } from '${dependency}'`, + `export type { RetryOptions } from '${dependency}'`, + ] + : [ + 'export interface RetryOptions {', + ' attempts: number', + ' delay?: number', + '}', + ]), + 'export interface ClientOptions {', + ' baseUrl: string', + ' retry?: RetryOptions', + '}', + 'export interface ClientResponse {', + ' status: number', + ' data: TData', + '}', + 'export interface Client {', + ' request: (path: string) => Promise>', + '}', + 'export declare function createClient(options: ClientOptions): Client', + '', + ].join('\n'), + ) +} + +export function writeExampleSkill( + packageRoot: string, + skillName: string, + library: string, + attempts: number | string, +): void { + writeFile( + join(packageRoot, 'skills', skillName, 'SKILL.md'), + [ + '---', + `name: ${JSON.stringify(skillName)}`, + `description: ${JSON.stringify(`Use when requesting ${skillName}.`)}`, + 'sources:', + ' - "src/index.ts"', + '---', + '', + `# ${skillName}`, + '', + '```ts', + `import { createClient } from '${library}'`, + `import type { RetryOptions } from '${library}'`, + '', + `const retry: RetryOptions = { attempts: ${attempts}, delay: 250 }`, + "const client = createClient({ baseUrl: 'https://example.invalid', retry })", + `const response = await client.request<{ id: number }>('/${skillName}')`, + 'response.data.id', + '```', + '', + ].join('\n'), + ) +} diff --git a/benchmarks/intent/review.bench.ts b/benchmarks/intent/review.bench.ts new file mode 100644 index 00000000..edaa1922 --- /dev/null +++ b/benchmarks/intent/review.bench.ts @@ -0,0 +1,77 @@ +import { join } from 'node:path' +import { afterAll, beforeAll, bench, describe } from 'vitest' +import { + createCliFixture, + createSlowBenchOptions, + git, + writeFile, + writeJson, + writeSkill, +} from './helpers.ts' + +const skillCount = 20 + +function createReviewFixture(sourcesFor: (index: number) => string) { + return createCliFixture('review', async (root, capture) => { + writeJson(join(root, 'package.json'), { + name: '@bench/library', + version: '1.0.0', + }) + for (let index = 0; index < skillCount; index++) { + for (const file of ['index', 'options', 'errors']) { + writeFile( + join(root, 'src', `feature-${index}`, `${file}.ts`), + `export const ${file}${index} = ${index}\n`, + ) + } + writeSkill(root, `feature-${index}`, { + description: `Use for feature ${index}.`, + sources: [sourcesFor(index)], + }) + } + git(root, 'init', '-q') + git(root, 'add', '.') + git(root, 'commit', '-qm', 'fixture') + + const report = JSON.parse(await capture(['review', '--json'])) as { + items: Array<{ problems: Array }> + } + if ( + report.items.length !== skillCount || + report.items.some((item) => item.problems.length > 0) + ) { + throw new Error(`Unexpected review report: ${JSON.stringify(report)}`) + } + }) +} + +const distinctSources = createReviewFixture( + (index) => `src/feature-${index}/**`, +) +const sharedSources = createReviewFixture(() => 'src/**/*.ts') + +describe('intent review with distinct sources per skill', () => { + beforeAll(distinctSources.setup) + afterAll(distinctSources.teardown) + + bench( + 'reports every pending skill as json', + async () => { + await distinctSources.run(['review', '--json']) + }, + createSlowBenchOptions(distinctSources.setup, distinctSources.teardown), + ) +}) + +describe('intent review with one source pattern shared by every skill', () => { + beforeAll(sharedSources.setup) + afterAll(sharedSources.teardown) + + bench( + 'reuses source matches and hashes across skills', + async () => { + await sharedSources.run(['review', '--json']) + }, + createSlowBenchOptions(sharedSources.setup, sharedSources.teardown), + ) +}) diff --git a/benchmarks/intent/validate.bench.ts b/benchmarks/intent/validate.bench.ts index e9b656d6..3f2e4ad7 100644 --- a/benchmarks/intent/validate.bench.ts +++ b/benchmarks/intent/validate.bench.ts @@ -1,11 +1,15 @@ import { rmSync } from 'node:fs' import { join } from 'node:path' -import { afterAll, beforeAll, bench, describe } from 'vitest' +import { afterAll, beforeAll, beforeEach, bench, describe } from 'vitest' import { createBenchOptions, + createCliFixture, createCliRunner, createConsoleSilencer, + createSlowBenchOptions, createTempDir, + writeExampleLibrary, + writeExampleSkill, writeFile, writeJson, writeSkill, @@ -131,3 +135,156 @@ describe('intent validate', () => { createBenchOptions(setup, teardown), ) }) + +async function expectFailure( + command: Promise, + message: string, +): Promise { + if ( + await command.then( + () => true, + () => false, + ) + ) { + throw new Error(message) + } +} + +const untypedExample = 'Expected validate to typecheck the skill code blocks' + +const typedExamples = createCliFixture( + 'validate-typed-examples', + async (root, capture) => { + writeExampleLibrary(root, '@bench/client') + for (let index = 1; index < 20; index++) { + writeExampleSkill(root, `resource-${index}`, '@bench/client', index) + } + + writeExampleSkill(root, 'resource-0', '@bench/client', "'many'") + await expectFailure(capture(['validate']), untypedExample) + writeExampleSkill(root, 'resource-0', '@bench/client', 20) + }, +) + +describe('intent validate with typed examples', () => { + beforeAll(typedExamples.setup) + afterAll(typedExamples.teardown) + + bench( + 'typechecks code blocks that import the package', + async () => { + await typedExamples.run(['validate']) + }, + createSlowBenchOptions(typedExamples.setup, typedExamples.teardown), + ) +}) + +const typedMonorepo = createCliFixture( + 'validate-typed-monorepo', + async (root, capture) => { + writeJson(join(root, 'package.json'), { + name: 'intent-validate-typed-monorepo-benchmark', + private: true, + workspaces: ['packages/*'], + devDependencies: { + '@tanstack/intent': 'workspace:*', + }, + }) + writeFile( + join(root, 'pnpm-workspace.yaml'), + "packages:\n - 'packages/*'\n", + ) + + for (const name of ['core', 'angular', 'react', 'solid', 'svelte', 'vue']) { + const packageRoot = join(root, 'packages', name) + writeExampleLibrary( + packageRoot, + `@bench/${name}`, + name === 'core' ? undefined : '@bench/core', + ) + for (let index = 1; index <= 4; index++) { + writeExampleSkill( + packageRoot, + `${name}-resource-${index}`, + `@bench/${name}`, + index, + ) + } + } + + const packageRoot = join(root, 'packages', 'vue') + writeExampleSkill(packageRoot, 'vue-resource-1', '@bench/vue', "'many'") + await expectFailure(capture(['validate']), untypedExample) + writeExampleSkill(packageRoot, 'vue-resource-1', '@bench/vue', 1) + }, +) + +describe('intent validate in a monorepo with typed examples', () => { + beforeAll(typedMonorepo.setup) + afterAll(typedMonorepo.teardown) + + bench( + 'typechecks code blocks for every workspace package', + async () => { + await typedMonorepo.run(['validate']) + }, + createSlowBenchOptions(typedMonorepo.setup, typedMonorepo.teardown), + ) +}) + +let fixed = false + +function writeLegacySkills(root: string): void { + for (let index = 0; index < 30; index++) { + const name = `task-${index}` + writeFile( + join(root, 'skills', name, 'SKILL.md'), + [ + '---', + `name: ${name}`, + `description: Use for ${name}.`, + 'type: core', + 'library_version: "1.0.0"', + '---', + '', + `Keep ${name} aligned with the documented workflow.`, + '', + ].join('\n'), + ) + } + fixed = false +} + +const pendingFix = createCliFixture('validate-fix', async (root, capture) => { + writeJson(join(root, 'package.json'), { + name: '@bench/validate-fix', + version: '1.0.0', + keywords: ['tanstack-intent'], + files: ['dist', 'skills'], + devDependencies: { + '@tanstack/intent': 'workspace:*', + }, + }) + writeLegacySkills(root) + await expectFailure( + capture(['validate', '--check']), + 'Expected a pending frontmatter migration', + ) +}) + +describe('intent validate --fix', () => { + beforeAll(pendingFix.setup) + afterAll(pendingFix.teardown) + // CodSpeed runs this hook outside its measurement; Vitest's tinybench runner never calls it. + beforeEach(() => writeLegacySkills(pendingFix.root)) + + bench( + 'migrates pending frontmatter in every skill', + async () => { + if (fixed) writeLegacySkills(pendingFix.root) + await pendingFix.run(['validate', '--fix']) + fixed = true + }, + createBenchOptions(pendingFix.setup, pendingFix.teardown), + ) +}) From 36cb00240c6e832b1eca7c963c2d581041863d90 Mon Sep 17 00:00:00 2001 From: ladybluenotes Date: Sun, 22 Nov 2026 11:49:52 -0700 Subject: [PATCH 2/2] chore(benchmarks): cover the paths guarded by call-count tests Add cases for list (allowlisted scopes among plain dependencies, nested symlinked node_modules), load (one skill from a 200-skill package, a workspace package in a 120-member workspace), stale (large workspace, targeted package, artifact entries matched by name, installed-dependency fallback), install --review, and maintainer check and sync on a reviewed repository. --- benchmarks/intent/install.bench.ts | 65 +++++++++ benchmarks/intent/list.bench.ts | 116 +++++++++++++++- benchmarks/intent/load.bench.ts | 59 ++++++++ benchmarks/intent/maintainer.bench.ts | 83 +++++++++++- benchmarks/intent/stale.bench.ts | 185 ++++++++++++++++++++++++++ 5 files changed, 505 insertions(+), 3 deletions(-) create mode 100644 benchmarks/intent/install.bench.ts diff --git a/benchmarks/intent/install.bench.ts b/benchmarks/intent/install.bench.ts new file mode 100644 index 00000000..fe86da18 --- /dev/null +++ b/benchmarks/intent/install.bench.ts @@ -0,0 +1,65 @@ +import { join } from 'node:path' +import { afterAll, beforeAll, bench, describe } from 'vitest' +import { + createBenchOptions, + createCliFixture, + writeJson, + writePackage, +} from './helpers.ts' + +const scopeCount = 40 +const skillsPerPackage = 5 + +const reviewRuntime = { + isTTY: true, + permissionPrompts: { + selectPermissions: () => Promise.resolve(null), + reviewPermissions: () => Promise.resolve(null), + editPermissions: (_packages: unknown, selection: unknown) => + Promise.resolve(selection), + confirmWrite: () => Promise.resolve(true), + }, +} + +const fixture = createCliFixture('install-review', async (root, capture) => { + const dependencies: Record = {} + for (let index = 0; index < scopeCount; index++) { + const name = `@bench-scope-${index}/library` + dependencies[name] = '1.0.0' + writePackage(join(root, 'node_modules'), name, '1.0.0', { + skills: Array.from( + { length: skillsPerPackage }, + (_, skill) => `topic-${skill}`, + ), + }) + } + writeJson(join(root, 'package.json'), { + name: 'intent-install-review-benchmark', + private: true, + dependencies, + intent: { + skills: Object.keys(dependencies).map((name) => + name.replace(/\/.*/, '/*'), + ), + }, + }) + + const output = await capture(['install', '--review'], reviewRuntime) + const expected = `Available: ${scopeCount * skillsPerPackage} skills from ${scopeCount} packages.` + if (!output.includes(expected)) { + throw new Error(`Unexpected install review output: ${output}`) + } +}) + +describe('intent install --review', () => { + beforeAll(fixture.setup) + afterAll(fixture.teardown) + + bench( + 'reviews unchanged permissions with one scan', + async () => { + await fixture.run(['install', '--review'], reviewRuntime) + }, + createBenchOptions(fixture.setup, fixture.teardown), + ) +}) diff --git a/benchmarks/intent/list.bench.ts b/benchmarks/intent/list.bench.ts index 815c8ada..00e0bd14 100644 --- a/benchmarks/intent/list.bench.ts +++ b/benchmarks/intent/list.bench.ts @@ -1,8 +1,9 @@ -import { rmSync } from 'node:fs' +import { mkdirSync, rmSync, symlinkSync } from 'node:fs' import { join } from 'node:path' import { afterAll, beforeAll, bench, describe } from 'vitest' import { createBenchOptions, + createCliFixture, createCliRunner, createConsoleSilencer, createTempDir, @@ -159,3 +160,116 @@ describe('intent list', () => { createBenchOptions(setup, teardown), ) }) + +type ListOutput = { + packages: Array<{ name: string }> +} + +const allowlistedScopes = 30 + +const manyDependencies = createCliFixture( + 'list-many-dependencies', + async (root, capture) => { + const dependencies: Record = {} + for (let index = 0; index < allowlistedScopes * 10; index++) { + const allowlisted = index % 10 === 0 + const name = allowlisted + ? `@bench-scope-${index / 10}/library` + : `plain-${index}` + dependencies[name] = '1.0.0' + writePackage(join(root, 'node_modules'), name, '1.0.0', { + skills: allowlisted ? ['core', 'core/setup'] : undefined, + }) + } + writeJson(join(root, 'package.json'), { + name: 'intent-list-many-dependencies-benchmark', + private: true, + dependencies, + intent: { + skills: Array.from( + { length: allowlistedScopes }, + (_, scope) => `@bench-scope-${scope}/*`, + ), + }, + }) + + const output = JSON.parse(await capture(['list', '--json'])) as ListOutput + if (output.packages.length !== allowlistedScopes) { + throw new Error(`Expected ${allowlistedScopes} listed packages`) + } + }, +) + +describe('intent list with many dependencies', () => { + beforeAll(manyDependencies.setup) + afterAll(manyDependencies.teardown) + + bench( + 'scans allowlisted scopes among dependencies without skills', + async () => { + await manyDependencies.run(['list', '--json']) + }, + createBenchOptions(manyDependencies.setup, manyDependencies.teardown), + ) +}) + +const nestedWrappers = 40 + +const nestedNodeModules = createCliFixture( + 'list-nested-node-modules', + async (root, capture) => { + writeJson(join(root, 'package.json'), { + name: 'intent-list-nested-node-modules-benchmark', + private: true, + }) + + for (let index = 0; index < nestedWrappers; index++) { + const wrapperRoot = join(root, 'node_modules', `wrapper-${index}`) + writePackage(join(root, 'node_modules'), `wrapper-${index}`, '1.0.0', {}) + writePackage(join(root, 'store'), `@bench/nested-${index}`, '1.0.0', { + skills: [`nested-${index}`], + }) + writePackage( + join(wrapperRoot, 'src', 'node_modules'), + `@bench/source-tree-${index}`, + '1.0.0', + { skills: [`source-tree-${index}`] }, + ) + + mkdirSync(join(wrapperRoot, 'node_modules', '@bench'), { + recursive: true, + }) + symlinkSync( + join(root, 'store', '@bench', `nested-${index}`), + join(wrapperRoot, 'node_modules', '@bench', `nested-${index}`), + 'dir', + ) + symlinkSync( + join(root, 'node_modules'), + join(wrapperRoot, 'node_modules', 'loop'), + 'dir', + ) + } + + const output = JSON.parse(await capture(['list', '--json'])) as ListOutput + if ( + output.packages.length !== nestedWrappers || + output.packages.some((pkg) => !pkg.name.startsWith('@bench/nested-')) + ) { + throw new Error(`Expected ${nestedWrappers} nested packages`) + } + }, +) + +describe('intent list through nested node_modules', () => { + beforeAll(nestedNodeModules.setup) + afterAll(nestedNodeModules.teardown) + + bench( + 'discovers symlinked nested packages without crawling loops or source trees', + async () => { + await nestedNodeModules.run(['list', '--json']) + }, + createBenchOptions(nestedNodeModules.setup, nestedNodeModules.teardown), + ) +}) diff --git a/benchmarks/intent/load.bench.ts b/benchmarks/intent/load.bench.ts index 4797469d..650f30f4 100644 --- a/benchmarks/intent/load.bench.ts +++ b/benchmarks/intent/load.bench.ts @@ -3,6 +3,7 @@ import { join } from 'node:path' import { afterAll, beforeAll, bench, describe } from 'vitest' import { createBenchOptions, + createCliFixture, createCliRunner, createConsoleSilencer, createTempDir, @@ -206,3 +207,61 @@ describe('intent load', () => { createBenchOptions(setup, teardown), ) }) + +const manySkills = createCliFixture('load-many-skills', (root) => { + writeJson(join(root, 'package.json'), { + name: 'intent-load-many-skills-benchmark', + private: true, + dependencies: { + '@bench/catalog': '1.0.0', + }, + }) + writePackage(join(root, 'node_modules'), '@bench/catalog', '1.0.0', { + skills: Array.from({ length: 200 }, (_, index) => `topic-${index}`), + }) +}) + +describe('intent load from a package with many skills', () => { + beforeAll(manySkills.setup) + afterAll(manySkills.teardown) + + bench( + 'loads one skill without reading its siblings', + async () => { + for (let index = 0; index < 10; index++) { + await manySkills.run(['load', '@bench/catalog#topic-150', '--path']) + } + }, + createBenchOptions(manySkills.setup, manySkills.teardown), + ) +}) + +const workspacePackage = createCliFixture('load-workspace-package', (root) => { + writeJson(join(root, 'package.json'), { + name: 'intent-workspace-package-load-benchmark', + private: true, + workspaces: ['packages/*'], + }) + writeFile(join(root, 'pnpm-workspace.yaml'), "packages:\n - 'packages/*'\n") + + for (let index = 0; index < 120; index++) { + writePackage(join(root, 'packages'), `workspace-pkg-${index}`, '1.0.0', { + skills: index === 60 ? ['guide'] : undefined, + }) + } +}) + +describe('intent load of a workspace package', () => { + beforeAll(workspacePackage.setup) + afterAll(workspacePackage.teardown) + + bench( + 'loads a workspace package skill from a large workspace', + async () => { + for (let index = 0; index < 10; index++) { + await workspacePackage.run(['load', 'workspace-pkg-60#guide', '--path']) + } + }, + createBenchOptions(workspacePackage.setup, workspacePackage.teardown), + ) +}) diff --git a/benchmarks/intent/maintainer.bench.ts b/benchmarks/intent/maintainer.bench.ts index 853353cf..8701a914 100644 --- a/benchmarks/intent/maintainer.bench.ts +++ b/benchmarks/intent/maintainer.bench.ts @@ -1,9 +1,19 @@ import { execFileSync, spawnSync } from 'node:child_process' -import { rmSync } from 'node:fs' +import { readFileSync, rmSync, writeFileSync } from 'node:fs' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { afterAll, beforeAll, bench, describe } from 'vitest' -import { createTempDir, writeFile, writeJson, writeSkill } from './helpers.ts' +import { + createCliFixture, + createSlowBenchOptions, + createTempDir, + git, + writeExampleLibrary, + writeExampleSkill, + writeFile, + writeJson, + writeSkill, +} from './helpers.ts' const cliPath = fileURLToPath( new URL('../../packages/intent/dist/cli.mjs', import.meta.url), @@ -70,3 +80,72 @@ for (const count of [20, 200]) { ) }) } + +const reviewedRepository = createCliFixture( + 'maintainer-reviewed', + async (root, capture) => { + writeExampleLibrary(root, '@bench/client') + writeFile( + join(root, '.github/workflows/check-skills.yml'), + '# Existing repository workflow\n', + ) + for (let index = 0; index < 20; index++) { + writeExampleSkill(root, `task-${index}`, '@bench/client', index + 1) + } + git(root, 'init', '-q') + await capture([ + 'maintainer', + 'setup', + '--distribution', + 'repo', + '--skill', + 'task-0', + ]) + for (const [record, placeholder, authored] of [ + ['domain_map.yaml', 'tasks: []', 'tasks: ["Request the resource"]'], + ['skill_spec.md', '', 'Authored.'], + ] as const) { + const path = join(root, 'skills', '_artifacts', record) + writeFileSync( + path, + readFileSync(path, 'utf8').replaceAll(placeholder, authored), + ) + } + await capture(['maintainer', 'sync']) + git(root, 'add', '.') + git(root, 'commit', '-qm', 'fixture') + await capture(['maintainer', 'review', '--unchanged', 'Matches source.']) + + const synchronized = await capture(['maintainer', 'sync']) + if (!synchronized.startsWith('Nothing to synchronize.')) { + throw new Error(`Unexpected maintainer sync output: ${synchronized}`) + } + }, +) + +describe('maintainer workflow on a reviewed repository', () => { + beforeAll(reviewedRepository.setup) + afterAll(reviewedRepository.teardown) + + bench( + 'checks skills, typed examples, and reviews', + async () => { + await reviewedRepository.run(['maintainer', 'check']) + }, + createSlowBenchOptions( + reviewedRepository.setup, + reviewedRepository.teardown, + ), + ) + + bench( + 'plans an unchanged repository distribution', + async () => { + await reviewedRepository.run(['maintainer', 'sync']) + }, + createSlowBenchOptions( + reviewedRepository.setup, + reviewedRepository.teardown, + ), + ) +}) diff --git a/benchmarks/intent/stale.bench.ts b/benchmarks/intent/stale.bench.ts index 76a88f56..e475c3c7 100644 --- a/benchmarks/intent/stale.bench.ts +++ b/benchmarks/intent/stale.bench.ts @@ -3,11 +3,13 @@ import { join } from 'node:path' import { afterAll, beforeAll, bench, describe } from 'vitest' import { createBenchOptions, + createCliFixture, createCliRunner, createConsoleSilencer, createTempDir, writeFile, writeJson, + writePackage, writeSkill, } from './helpers.ts' @@ -180,3 +182,186 @@ describe('intent stale', () => { createBenchOptions(setupWithArtifacts, teardown), ) }) + +type StaleReport = { + skills: Array + signals: Array +} + +async function expectReports( + output: Promise, + count: number, +): Promise> { + const reports = JSON.parse(await output) as Array + if ( + reports.length !== count || + reports.some((report) => report.signals.length > 0) + ) { + throw new Error(`Unexpected stale reports: ${JSON.stringify(reports)}`) + } + + return reports +} + +const workspacePackages = 40 + +const largeWorkspace = createCliFixture( + 'stale-large-workspace', + async (root, capture) => { + writeJson(join(root, 'package.json'), { + name: 'intent-stale-large-workspace-benchmark', + private: true, + workspaces: ['packages/*'], + }) + writeFile( + join(root, 'pnpm-workspace.yaml'), + "packages:\n - 'packages/*'\n", + ) + + const artifactSkills: Array<{ + package: string + slug: string + path: string + }> = [] + for (let index = 0; index < workspacePackages; index++) { + const packageDir = `pkg-${index}` + const skills = ['core', 'cache', 'streaming', 'runtime'].map( + (skill) => `${packageDir}/${skill}`, + ) + writeStalePackage(root, { + name: `@bench/${packageDir}`, + packageDir, + packageVersion: '2.1.0', + skillVersion: '2.0.0', + skills, + }) + for (const slug of skills) { + artifactSkills.push({ + package: `packages/${packageDir}`, + slug, + path: `packages/${packageDir}/skills/${slug}/SKILL.md`, + }) + } + } + writeJson(join(root, '_artifacts', 'skill_tree.yaml'), { + skills: artifactSkills, + }) + + await expectReports(capture(['stale', '--json']), workspacePackages) + await expectReports(capture(['stale', 'packages/pkg-20', '--json']), 1) + }, +) + +describe('intent stale in a large workspace', () => { + beforeAll(largeWorkspace.setup) + afterAll(largeWorkspace.teardown) + + bench( + 'reports drift for every package with shared artifacts', + async () => { + await largeWorkspace.run(['stale', '--json']) + }, + createBenchOptions(largeWorkspace.setup, largeWorkspace.teardown), + ) + + bench( + 'checks only the targeted package', + async () => { + for (let index = 0; index < 10; index++) { + await largeWorkspace.run(['stale', 'packages/pkg-20', '--json']) + } + }, + createBenchOptions(largeWorkspace.setup, largeWorkspace.teardown), + ) +}) + +const namedSkills = 200 + +const artifactNames = createCliFixture( + 'stale-artifact-names', + async (root, capture) => { + writeJson(join(root, 'package.json'), { + name: '@bench/artifact-names', + version: '1.0.0', + }) + + const slugs = Array.from( + { length: namedSkills }, + (_, index) => `skill-${index}`, + ) + for (const slug of slugs) { + writeSkill(root, slug, { + description: `${slug} maintenance guide`, + libraryVersion: '1.0.0', + }) + } + writeJson(join(root, '_artifacts', 'skill_tree.yaml'), { + skills: slugs.map((slug) => ({ slug })), + }) + + const [report] = await expectReports(capture(['stale', '--json']), 1) + if (report!.skills.length !== namedSkills) { + throw new Error(`Expected ${namedSkills} skills in the stale report`) + } + }, +) + +describe('intent stale with many artifact entries', () => { + beforeAll(artifactNames.setup) + afterAll(artifactNames.teardown) + + bench( + 'matches artifact entries to skills by name', + async () => { + await artifactNames.run(['stale', '--json']) + }, + createBenchOptions(artifactNames.setup, artifactNames.teardown), + ) +}) + +const installedPackages = 20 + +const installedDependencies = createCliFixture( + 'stale-installed-dependencies', + async (root, capture) => { + const dependencies: Record = {} + for (let index = 0; index < installedPackages; index++) { + const name = `@bench/installed-${index}` + dependencies[name] = '1.0.0' + writePackage(join(root, 'node_modules'), name, '1.0.0', { + skills: ['core', 'core/cache', 'core/streaming', 'core/runtime'], + }) + } + writeJson(join(root, 'package.json'), { + name: 'intent-stale-installed-dependencies-benchmark', + private: true, + dependencies, + }) + writeFile( + join(root, 'pnpm-workspace.yaml'), + "packages:\n - 'packages/*'\n", + ) + writeJson(join(root, 'packages', 'app', 'package.json'), { + name: '@consumer/app', + version: '1.0.0', + }) + + await expectReports(capture(['stale', '--json']), installedPackages) + }, +) + +describe('intent stale for installed dependencies', () => { + beforeAll(installedDependencies.setup) + afterAll(installedDependencies.teardown) + + bench( + 'reports installed packages through the fallback scan', + async () => { + await installedDependencies.run(['stale', '--json']) + }, + createBenchOptions( + installedDependencies.setup, + installedDependencies.teardown, + ), + ) +})