Files
sim/scripts/sync-skills.ts
Waleed 94b879d224 docs(agents): accurate guidance, guardrail map, and check:guidance-refs (#8557)
* fix(audits): guard the route wrapper against lib/mothership, not the removed lib/copilot

The Copilot modules moved to lib/mothership in the v1.0.0 rename, so the
route-wrapper graph guard was banning a directory that no longer exists.

* improvement(audits): add check:guidance-refs and fix the stale references it found

Agent guidance (CLAUDE.md, every AGENTS.md, .claude/rules, .agents/skills)
names paths, scripts, skills, and import specifiers that agents follow
literally. The new audit resolves each one and fails on any that no longer
exists. Fixes the references it found: lib/copilot -> lib/mothership,
stores/workflows/store -> stores/workflows/workflow/store, a relative landing
path, deleted selector-provider and legacy landing mentions, and illustrative
example imports rewritten as placeholders.

* improvement(audits): check rule frontmatter globs in check:guidance-refs

A paths glob that matches nothing silently stops the rule from loading.
sim-api-contracts still targeted the removed apps/sim/hooks/selectors; drop
it, and name check:api-validation:strict as the gate the rule describes.

* docs(agents): consolidate guidance and add the local gate to CLAUDE.md

- CLAUDE.md: "How your work is checked" (the local gate and a rule-to-check
  map), sharper comment rules (no narration, restated names, or change
  history), drop filler, fix the type-check command description.
- Rules: one source for the text scale (sim-styling), use-client boundary
  (sim-queries), testing principles (CLAUDE.md); fix an in-place sort in a
  list-ordering example and an ESLint directive the repo does not use; drop
  change-history narration and rotting line-number references.

* docs(agents): fix stale facts in connector, model, column-type, and selector skills

Verified against the code: fetchWithRetry moved to secure-fetch.server,
deletion reconciliation is checkpoint.unsafe (shouldReconcileDeletions is
gone), the legacy Search toggle is removed, inline-content connectors may
hash content, reasoningEffort/thinking provider lists, column-type metadata
update path, check:api-validation:strict as the gate. CLAUDE.md now names
the coerceValue switch and relative barrel re-exports as the documented
exceptions the code relies on. Selector rules in connector skills point at
validate-selector instead of restating it.

* docs(agents): fix contradictions and stale facts in platform skills

tool-registry-boundary told tests to re-mock a global (check:test-patterns
fails that); db-migrate listed a nonexistent annotate rule and omitted the
pending-drop-tables contract; permission-group skills cited a removed
descriptions record, a nonexistent key-order test, and a single
principal-wide capability where there are two; v2-api-conventions cited a
removed helper; memory-load-check called a pattern the connectors use
cargo-culting. Gates now name check:api-validation:strict / check:audits,
ship runs CI's schema/migration sync step, test-audit points at CLAUDE.md
instead of copying it, and incident narration became current-state rules.

* docs(agents): align UI skills with the settings, emcn, and state rules

add-settings-page's audit greps used a pathspec that matches nothing, and
its registration steps named types that no longer exist
(SETTINGS_SECTION_REGISTRY and SECTION_MODULES are the mechanism). The
emcn review taught the legacy Button variants and a destructive Delete
chip that sim-settings-pages forbids. The design skills now carry repo
precedence notes (no global styles, emcn owns chrome, framer-motion,
hover-hover:, fixed fonts and weights). The you-might-not-need-* skills
recognize URL state, the list-preference exception, and the Map memo the
component rule prescribes.

* docs(agents): fix contradictions and stale facts in integration skills

Verified against the code: tags live on BlockMeta, not BlockConfig;
check-block-registry requires required user-only params to be filled by a
subBlock of the same id, so remapping them in tools.config.params fails CI;
subblock ids are unique per condition; matchEvent may return a
NextResponse; createHmacVerifier needs requireSecret to fail closed;
FileToolProcessor is executor-side; provider scopes live in
lib/auth/connectors/providers.ts; BYOK needs PROVIDER_SECTIONS; polling
crons need the matching docker/crontab line. Duplicated option-list and
regenerate sections now point at one copy.

* docs(agents): map the new ratchets and lint rules in the guardrail table

* docs(agents): correct audit findings in guidance and gate docs

Restore the connector byte-cap rule's skip list, list every CI gate step,
name the real baseline flags and generated-artifact checks, make
api-validation strict-only guidance explicit, pass a base ref to
check-block-registry, and teach check:guidance-refs about bun run --cwd.

* docs(agents): drop the hand-kept rule-to-check table from CLAUDE.md

Name the enforcing check on the rule's own bullet instead, and tighten the
Comments bullet.

* improvement(audits): share rule frontmatter parsing and fail on dead graph guards

check:guidance-refs reuses sync-skills' parseRule, reads workspace manifests
once from the root workspaces globs, and matches rule globs against one git
listing instead of a filesystem scan per glob (~2.5s to ~0.2s). Markdown
links now go through the shared path check. check:application-graph fails
when a forbidden prefix matches nothing under apps/sim.

* fix(audits): resolve wildcard and root package exports, ignore deleted files and test-only guards

check:guidance-refs now requires a bare @sim/<pkg> import to have a '.' export, checks that a wildcard export match maps to an existing file, and drops index entries missing from the working tree before matching rule path globs. check:application-graph no longer counts a leftover test file as keeping a non-directory forbidden prefix alive.

* docs(agents): correct review findings in skills and rules

Scope SSRF, client-boundary, forcedToolUse, canonicalParamId, integration metadata, and HEAD claims to what the code does; fix the ship migration pathspec, the babysit conflict path, enrichment folder placeholders, framer-motion samples, and stale connector and column-type references.

* fix(audits): resolve import specifiers to module files only; tighten review-flagged guidance

* docs(agents): keep the repo-wide ES2023 ban in sim-components after rebase

* fix(audits): require exact export targets to exist and resolve markdown links strictly

* fix(audits): check require() specifiers and keep path resolution inside the repo
2026-10-02 01:59:22 -07:00

338 lines
12 KiB
TypeScript

/**
* Generate the per-tool skill and rule projections from the canonical sources
* so every supported agent surface stays aligned.
*
* Skills — canonical source of truth: `.agents/skills/<name>/SKILL.md`
* frontmatter: `name`, `description`, optional `argument-hint`; then the body.
*
* Projection (generated — do not hand-edit):
* - `.claude/skills/<name>` — symlink to the complete canonical skill directory.
*
* Cursor discovers `.agents/skills` directly, so it needs no projection.
* Deprecated `.claude/commands/<name>.md` and `.cursor/commands/<name>.md`
* projections are removed when syncing and treated as stale in check mode.
*
* Claude receives the complete skill directory so bundled resources remain
* available.
*
* Rules — canonical source of truth: `.claude/rules/<name>.md`
* frontmatter: `description`, optional `paths` list; then the body. Claude
* reads these directly.
*
* Projection (generated — do not hand-edit):
* - `.cursor/rules/<name>.mdc` — same body under Cursor's frontmatter:
* `description`, and `globs` from `paths` or `alwaysApply: true` when the
* rule has none.
*
* Usage:
* bun run scripts/sync-skills.ts # write projections
* bun run scripts/sync-skills.ts --check # fail (exit 1) if any projection is stale
*/
import {
lstat,
mkdir,
readdir,
readFile,
readlink,
rm,
rmdir,
symlink,
writeFile,
} from 'node:fs/promises'
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path'
import { fileURLToPath } from 'node:url'
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url))
const ROOT = resolve(SCRIPT_DIR, '..')
const CANONICAL_DIR = resolve(ROOT, '.agents/skills')
const CLAUDE_SKILLS_DIR = resolve(ROOT, '.claude/skills')
const LEGACY_CLAUDE_COMMANDS_DIR = resolve(ROOT, '.claude/commands')
const LEGACY_CURSOR_COMMANDS_DIR = resolve(ROOT, '.cursor/commands')
const CANONICAL_RULES_DIR = resolve(ROOT, '.claude/rules')
const CURSOR_RULES_DIR = resolve(ROOT, '.cursor/rules')
interface Skill {
name: string
frontmatter: string[]
}
/** Read the ordered frontmatter lines from a SKILL.md. */
function parseSkill(name: string, raw: string): Skill {
if (!raw.startsWith('---\n')) {
throw new Error(`${name}: SKILL.md must start with a '---' frontmatter block`)
}
const end = raw.indexOf('\n---\n', 4)
if (end === -1) throw new Error(`${name}: unterminated frontmatter block`)
const frontmatter = raw.slice(4, end).split('\n')
return { name, frontmatter }
}
/** Return the first frontmatter line whose key matches, verbatim, or undefined. */
function line(skill: Skill, key: string): string | undefined {
return skill.frontmatter.find((l) => l.startsWith(`${key}:`))
}
async function loadCanonicalSkills(): Promise<Skill[]> {
const entries = await readdir(CANONICAL_DIR, { withFileTypes: true })
const names = entries
.filter((e) => e.isDirectory())
.map((e) => e.name)
.sort()
const skills: Skill[] = []
for (const name of names) {
const raw = await readFile(resolve(CANONICAL_DIR, name, 'SKILL.md'), 'utf8')
const skill = parseSkill(name, raw)
const nameLine = line(skill, 'name')
if (nameLine !== `name: ${name}`) {
throw new Error(`${name}: frontmatter 'name' must equal the directory name`)
}
if (!line(skill, 'description')) {
throw new Error(`${name}: missing 'description' in frontmatter`)
}
skills.push(skill)
}
return skills
}
interface Rule {
name: string
description: string
paths: string[]
body: string
}
/** Parse a canonical rule: `description`, optional `paths` list, then the body. */
export function parseRule(name: string, raw: string): Rule {
if (!raw.startsWith('---\n')) {
throw new Error(`${name}: rule must start with a '---' frontmatter block`)
}
const end = raw.indexOf('\n---\n', 4)
if (end === -1) throw new Error(`${name}: unterminated frontmatter block`)
const frontmatter = raw.slice(4, end).split('\n')
const body = raw.slice(end + '\n---\n'.length).replace(/^\n+/, '')
let description: string | undefined
const paths: string[] = []
let inPaths = false
for (const entry of frontmatter) {
if (entry.startsWith('description:')) {
description = entry.slice('description:'.length).trim()
inPaths = false
} else if (entry === 'paths:') {
inPaths = true
} else if (inPaths && /^\s+- /.test(entry)) {
paths.push(
entry
.replace(/^\s+- /, '')
.trim()
.replace(/^"(.*)"$/, '$1')
)
} else {
inPaths = false
}
}
if (!description) throw new Error(`${name}: missing 'description' in frontmatter`)
if (!isPlainYamlScalar(description)) {
throw new Error(
`${name}: 'description' must be a plain YAML string (no leading quote or indicator, no ': ' or ' #', and not a bare boolean, null, number, or date) so both Claude and Cursor parse it`
)
}
return { name, description, paths, body }
}
/**
* Whether a value survives as an unquoted YAML plain scalar. The canonical rule
* frontmatter is written by hand and read by Claude as-is, so a description that
* needs quoting would silently break its `paths` there too; refuse it at the
* source instead of only quoting the projection.
*/
function isPlainYamlScalar(value: string): boolean {
if (value !== value.trim()) return false
if (/^[-?:,[\]{}#&*!|>'"%@`]/.test(value)) return false
if (value.includes(': ') || value.includes(' #')) return false
if (value.endsWith(':')) return false
return !IMPLICITLY_TYPED_SCALAR.test(value)
}
/**
* Plain scalars a YAML 1.1 or 1.2 reader resolves to something other than a
* string: booleans, null, integers (decimal, hex, octal, binary, underscored),
* floats (including .inf/.nan), and dates or timestamps.
*/
const IMPLICITLY_TYPED_SCALAR = new RegExp(
[
'^(?:',
'~|null|true|false|yes|no|on|off|y|n',
'|[-+]?(?:0b[01_]+|0o?[0-7_]+|0x[0-9a-f_]+|[0-9][0-9_]*(?::[0-5]?[0-9])*)',
'|[-+]?(?:[0-9][0-9_]*)?\\.[0-9_]*(?:e[-+]?[0-9]+)?',
'|[-+]?[0-9][0-9_]*(?:\\.[0-9_]*)?e[-+]?[0-9]+',
'|[-+]?\\.(?:inf|nan)',
'|[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}(?:[tT ].*)?',
')$',
].join(''),
'i'
)
async function loadCanonicalRules(): Promise<Rule[]> {
const entries = await readdir(CANONICAL_RULES_DIR)
const rules: Rule[] = []
for (const file of entries.filter((f) => f.endsWith('.md')).sort()) {
const raw = await readFile(resolve(CANONICAL_RULES_DIR, file), 'utf8')
rules.push(parseRule(file.slice(0, -'.md'.length), raw))
}
return rules
}
/** Render the Cursor `.mdc` projection of a canonical rule. */
function renderCursorRule(rule: Rule): string {
const scope = rule.paths.length > 0 ? `globs: ${JSON.stringify(rule.paths)}` : 'alwaysApply: true'
return [
'---',
`description: ${JSON.stringify(rule.description)}`,
scope,
'---',
'',
`<!-- Generated from .claude/rules/${rule.name}.md by \`bun run skills:sync\`. Edit the source, not this file. -->`,
'',
rule.body,
].join('\n')
}
/** Find generated Claude links whose canonical skill no longer exists. */
async function findOrphanedClaudeLinks(expectedPaths: ReadonlySet<string>): Promise<string[]> {
const entries = await readdir(CLAUDE_SKILLS_DIR, { withFileTypes: true }).catch(
(error: unknown) => {
const code =
typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined
if (code === 'ENOENT') return []
throw error
}
)
const orphaned: string[] = []
for (const entry of entries) {
if (!entry.isSymbolicLink()) continue
const path = resolve(CLAUDE_SKILLS_DIR, entry.name)
if (expectedPaths.has(path)) continue
const target = resolve(dirname(path), await readlink(path))
const canonicalRelative = relative(CANONICAL_DIR, target)
const targetsCanonicalSkills =
canonicalRelative === '' ||
(!isAbsolute(canonicalRelative) &&
canonicalRelative !== '..' &&
!canonicalRelative.startsWith(`..${sep}`))
if (targetsCanonicalSkills) orphaned.push(path)
}
return orphaned
}
async function main() {
const check = process.argv.includes('--check')
const skills = await loadCanonicalSkills()
const claudeLinks = skills.map((skill) => {
const path = resolve(CLAUDE_SKILLS_DIR, skill.name)
return {
path,
target: relative(dirname(path), resolve(CANONICAL_DIR, skill.name)),
}
})
const deprecatedTargets = skills.flatMap((skill) => [
resolve(LEGACY_CLAUDE_COMMANDS_DIR, `${skill.name}.md`),
resolve(LEGACY_CURSOR_COMMANDS_DIR, `${skill.name}.md`),
])
const stale: string[] = []
const expectedClaudePaths = new Set(claudeLinks.map(({ path }) => path))
for (const { path, target } of claudeLinks) {
const current = await readlink(path).catch(() => null)
if (current === target) continue
const displayPath = path.replace(`${ROOT}/`, '')
stale.push(displayPath)
if (!check) {
const existing = await lstat(path).catch(() => null)
if (existing && !existing.isSymbolicLink()) {
throw new Error(`${displayPath}: expected a generated symlink, found a real directory`)
}
if (existing) await rm(path)
await mkdir(dirname(path), { recursive: true })
await symlink(target, path, 'dir')
}
}
const orphanedClaudeLinks = await findOrphanedClaudeLinks(expectedClaudePaths)
for (const path of orphanedClaudeLinks) {
stale.push(`${path.replace(`${ROOT}/`, '')} (orphaned)`)
if (!check) await rm(path)
}
for (const path of deprecatedTargets) {
const current = await readFile(path, 'utf8').catch(() => null)
if (current === null) continue
stale.push(`${path.replace(`${ROOT}/`, '')} (deprecated)`)
if (!check) await rm(path)
}
const rules = await loadCanonicalRules()
const expectedCursorRules = new Set<string>()
for (const rule of rules) {
const path = resolve(CURSOR_RULES_DIR, `${rule.name}.mdc`)
expectedCursorRules.add(path)
const rendered = renderCursorRule(rule)
const current = await readFile(path, 'utf8').catch(() => null)
if (current === rendered) continue
stale.push(path.replace(`${ROOT}/`, ''))
if (!check) {
await mkdir(dirname(path), { recursive: true })
await writeFile(path, rendered)
}
}
const cursorRuleFiles = await readdir(CURSOR_RULES_DIR).catch(() => [] as string[])
for (const file of cursorRuleFiles) {
if (!file.endsWith('.mdc')) continue
const path = resolve(CURSOR_RULES_DIR, file)
if (expectedCursorRules.has(path)) continue
stale.push(`${path.replace(`${ROOT}/`, '')} (orphaned)`)
if (!check) await rm(path)
}
if (!check) {
for (const path of [LEGACY_CLAUDE_COMMANDS_DIR, LEGACY_CURSOR_COMMANDS_DIR]) {
await rmdir(path).catch((error: unknown) => {
const code =
typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined
if (code !== 'ENOENT' && code !== 'ENOTEMPTY') throw error
})
}
}
if (check) {
if (stale.length > 0) {
console.error(
`✗ ${stale.length} skill/rule projection(s) are stale — run \`bun run skills:sync\`:\n` +
stale.map((p) => ` ${p}`).join('\n')
)
process.exit(1)
}
console.log(
`✓ ${skills.length} canonical skills and ${rules.length} canonical rules available to Claude and Cursor`
)
return
}
console.log(
stale.length === 0
? `✓ ${skills.length} skills and ${rules.length} rules already in sync — nothing to write`
: `✓ updated ${stale.length} surface(s) from ${skills.length} canonical skills and ${rules.length} canonical rules`
)
}
if (import.meta.main) {
main().catch((err) => {
console.error(err instanceof Error ? err.message : err)
process.exit(1)
})
}