Repository navigation
Fix SessionStart hook fallback and add adapter drift guards #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * CI guard: fail when adapter files drift from canonical sources. | ||
| */ | ||
|
|
||
| const { syncCommandAdapters } = require('../sync/sync-commands'); | ||
| const { syncRulesAndSkills } = require('../sync/sync-rules-skills'); | ||
|
|
||
| try { | ||
| const commandResult = syncCommandAdapters({ checkOnly: true }); | ||
| const rulesSkillsResult = syncRulesAndSkills({ checkOnly: true }); | ||
| const issueCount = commandResult.issues.length + rulesSkillsResult.issues.length; | ||
|
|
||
| if (issueCount > 0) { | ||
| process.exit(1); | ||
| } | ||
| console.log('Adapter drift check passed'); | ||
| process.exit(0); | ||
| } catch (err) { | ||
| console.error(`ERROR: adapter drift check failed - ${err.message}`); | ||
| process.exit(1); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,124 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * PostToolUse Hook: Auto-format JS/TS files with Prettier after edits | ||
| * PostToolUse Hook: Auto-format JS/TS files after edits | ||
| * | ||
| * Cross-platform (Windows, macOS, Linux) | ||
| * | ||
| * Runs after Edit tool use. If the edited file is a JS/TS file, | ||
| * formats it with Prettier. Fails silently if Prettier isn't installed. | ||
| * auto-detects the project formatter (Biome or Prettier) by looking | ||
| * for config files, then formats accordingly. | ||
| * Fails silently if no formatter is found or installed. | ||
| */ | ||
|
|
||
| const { execFileSync } = require('child_process'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const MAX_STDIN = 1024 * 1024; // 1MB limit | ||
| const JS_TS_FILE_REGEX = /\.(ts|tsx|js|jsx)$/; | ||
| let data = ''; | ||
| process.stdin.setEncoding('utf8'); | ||
|
|
||
| process.stdin.on('data', chunk => { | ||
| if (data.length < MAX_STDIN) { | ||
| data += chunk; | ||
| const remaining = MAX_STDIN - data.length; | ||
| data += chunk.substring(0, remaining); | ||
| } | ||
| }); | ||
|
|
||
| function findProjectRoot(startDir) { | ||
| let dir = startDir; | ||
|
|
||
| while (dir !== path.dirname(dir)) { | ||
| if (fs.existsSync(path.join(dir, 'package.json'))) return dir; | ||
| dir = path.dirname(dir); | ||
| } | ||
|
|
||
| return startDir; | ||
| } | ||
|
|
||
| function hasPrettierInPackageJson(dir) { | ||
| const pkgPath = path.join(dir, 'package.json'); | ||
| if (!fs.existsSync(pkgPath)) return false; | ||
|
|
||
| try { | ||
| const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); | ||
| return Object.prototype.hasOwnProperty.call(pkg, 'prettier'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function detectFormatter(startDir) { | ||
| const biomeConfigs = ['biome.json', 'biome.jsonc']; | ||
| const prettierConfigs = [ | ||
| '.prettierrc', | ||
| '.prettierrc.json', | ||
| '.prettierrc.js', | ||
| '.prettierrc.cjs', | ||
| '.prettierrc.mjs', | ||
| '.prettierrc.yml', | ||
| '.prettierrc.yaml', | ||
| '.prettierrc.toml', | ||
| 'prettier.config.js', | ||
| 'prettier.config.cjs', | ||
| 'prettier.config.mjs' | ||
| ]; | ||
|
|
||
| let dir = startDir; | ||
| while (true) { | ||
| for (const cfg of biomeConfigs) { | ||
| if (fs.existsSync(path.join(dir, cfg))) return 'biome'; | ||
| } | ||
|
|
||
| for (const cfg of prettierConfigs) { | ||
| if (fs.existsSync(path.join(dir, cfg))) return 'prettier'; | ||
| } | ||
|
|
||
| if (hasPrettierInPackageJson(dir)) return 'prettier'; | ||
|
|
||
| const parent = path.dirname(dir); | ||
| if (parent === dir) break; | ||
| dir = parent; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function getFormatterCommand(formatter, filePath) { | ||
| const npxBin = process.platform === 'win32' ? 'npx.cmd' : 'npx'; | ||
|
|
||
| if (formatter === 'biome') { | ||
| return { bin: npxBin, args: ['@biomejs/biome', 'format', '--write', filePath] }; | ||
| } | ||
| if (formatter === 'prettier') { | ||
| return { bin: npxBin, args: ['prettier', '--write', filePath] }; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| process.stdin.on('end', () => { | ||
| try { | ||
| const input = JSON.parse(data); | ||
| const filePath = input.tool_input?.file_path; | ||
|
|
||
| if (filePath && /\.(ts|tsx|js|jsx)$/.test(filePath)) { | ||
| if (filePath && JS_TS_FILE_REGEX.test(filePath)) { | ||
| try { | ||
| // Use npx.cmd on Windows to avoid shell: true which enables command injection | ||
| const npxBin = process.platform === 'win32' ? 'npx.cmd' : 'npx'; | ||
| execFileSync(npxBin, ['prettier', '--write', filePath], { | ||
| cwd: path.dirname(path.resolve(filePath)), | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| timeout: 15000 | ||
| }); | ||
| const absFilePath = path.resolve(filePath); | ||
| const projectRoot = findProjectRoot(path.dirname(absFilePath)); | ||
| const formatter = detectFormatter(projectRoot); | ||
| const cmd = getFormatterCommand(formatter, absFilePath); | ||
|
|
||
| if (cmd) { | ||
| execFileSync(cmd.bin, cmd.args, { | ||
| cwd: projectRoot, | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| timeout: 15000 | ||
| }); | ||
| } | ||
| } catch { | ||
| // Prettier not installed, file missing, or failed — non-blocking | ||
| // Formatter not installed, file missing, or failed — non-blocking | ||
| } | ||
| } | ||
| } catch { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Sync command adapters from canonical root commands/. | ||
| * | ||
| * By default, writes updates into adapter targets. | ||
| * Use --check to report drift without writing. | ||
| */ | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const ROOT_DIR = path.join(__dirname, '../..'); | ||
| const SOURCE_DIR = path.join(ROOT_DIR, 'commands'); | ||
|
|
||
| const TARGETS = [ | ||
| { | ||
| name: 'cursor', | ||
| dir: path.join(ROOT_DIR, '.cursor/commands'), | ||
| excluded: new Set(), | ||
| overrides: new Set([ | ||
| 'build-fix.md', | ||
| 'evolve.md', | ||
| 'refactor-clean.md', | ||
| 'test-coverage.md', | ||
| 'update-codemaps.md', | ||
| 'update-docs.md' | ||
| ]), | ||
| extraAllowed: new Set() | ||
| }, | ||
| { | ||
| name: 'opencode', | ||
| dir: path.join(ROOT_DIR, '.opencode/commands'), | ||
| excluded: new Set([ | ||
| 'multi-backend.md', | ||
| 'multi-execute.md', | ||
| 'multi-frontend.md', | ||
| 'multi-plan.md', | ||
| 'multi-workflow.md', | ||
| 'pm2.md', | ||
| 'python-review.md', | ||
| 'sessions.md' | ||
| ]), | ||
| overrides: new Set([ | ||
| 'build-fix.md', | ||
| 'checkpoint.md', | ||
| 'code-review.md', | ||
| 'e2e.md', | ||
| 'eval.md', | ||
| 'evolve.md', | ||
| 'go-build.md', | ||
| 'go-review.md', | ||
| 'go-test.md', | ||
| 'instinct-export.md', | ||
| 'instinct-import.md', | ||
| 'instinct-status.md', | ||
| 'learn.md', | ||
| 'orchestrate.md', | ||
| 'plan.md', | ||
| 'refactor-clean.md', | ||
| 'setup-pm.md', | ||
| 'skill-create.md', | ||
| 'tdd.md', | ||
| 'test-coverage.md', | ||
| 'update-codemaps.md', | ||
| 'update-docs.md', | ||
| 'verify.md' | ||
| ]), | ||
| extraAllowed: new Set(['security.md']) | ||
| } | ||
| ]; | ||
|
|
||
| function readNormalized(filePath) { | ||
| return fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n'); | ||
| } | ||
|
|
||
| function listMarkdownFiles(dir) { | ||
| if (!fs.existsSync(dir)) return []; | ||
| return fs.readdirSync(dir).filter(name => name.endsWith('.md')).sort(); | ||
| } | ||
|
|
||
| function ensureDir(dir) { | ||
| if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); | ||
| } | ||
|
|
||
| function copyFile(sourcePath, targetPath) { | ||
| ensureDir(path.dirname(targetPath)); | ||
| fs.copyFileSync(sourcePath, targetPath); | ||
| } | ||
|
|
||
| function syncTarget(target, sourceFiles, checkOnly) { | ||
| const issues = []; | ||
| const updates = []; | ||
| const expectedFiles = sourceFiles.filter(file => !target.excluded.has(file)); | ||
| const expectedSet = new Set(expectedFiles); | ||
|
|
||
| for (const file of expectedFiles) { | ||
| const sourcePath = path.join(SOURCE_DIR, file); | ||
| const targetPath = path.join(target.dir, file); | ||
| const isOverride = target.overrides.has(file); | ||
|
|
||
| if (!fs.existsSync(targetPath)) { | ||
| if (isOverride || checkOnly) { | ||
| issues.push(`[${target.name}] missing command: ${file}`); | ||
| } else { | ||
| copyFile(sourcePath, targetPath); | ||
| updates.push(`[${target.name}] created ${file}`); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (isOverride) continue; | ||
|
|
||
| const sourceContent = readNormalized(sourcePath); | ||
| const targetContent = readNormalized(targetPath); | ||
| if (sourceContent !== targetContent) { | ||
| if (checkOnly) { | ||
| issues.push(`[${target.name}] drift detected: ${file}`); | ||
| } else { | ||
| copyFile(sourcePath, targetPath); | ||
| updates.push(`[${target.name}] updated ${file}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const targetFiles = listMarkdownFiles(target.dir); | ||
| for (const file of targetFiles) { | ||
| const isExpected = expectedSet.has(file); | ||
| const isAllowedExtra = target.extraAllowed.has(file); | ||
| if (!isExpected && !isAllowedExtra) { | ||
| issues.push(`[${target.name}] unexpected adapter command: ${file}`); | ||
| } | ||
| } | ||
|
|
||
| return { issues, updates }; | ||
| } | ||
|
|
||
| function syncCommandAdapters(options = {}) { | ||
| const { checkOnly = false } = options; | ||
|
|
||
| if (!fs.existsSync(SOURCE_DIR)) { | ||
| throw new Error(`commands directory not found: ${SOURCE_DIR}`); | ||
| } | ||
|
|
||
| const sourceFiles = listMarkdownFiles(SOURCE_DIR); | ||
| let allIssues = []; | ||
| let allUpdates = []; | ||
|
|
||
| for (const target of TARGETS) { | ||
| ensureDir(target.dir); | ||
| const result = syncTarget(target, sourceFiles, checkOnly); | ||
| allIssues = allIssues.concat(result.issues); | ||
| allUpdates = allUpdates.concat(result.updates); | ||
| } | ||
|
|
||
| if (allIssues.length > 0) { | ||
| for (const issue of allIssues) console.error(issue); | ||
| } | ||
|
|
||
| if (!checkOnly && allUpdates.length > 0) { | ||
| for (const update of allUpdates) console.log(update); | ||
| } | ||
|
|
||
| if (!checkOnly && allUpdates.length === 0 && allIssues.length === 0) { | ||
| console.log('Command adapters are already in sync.'); | ||
| } | ||
|
|
||
| return { issues: allIssues, updates: allUpdates }; | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| const checkOnly = process.argv.includes('--check'); | ||
| const { issues } = syncCommandAdapters({ checkOnly }); | ||
|
|
||
| if (issues.length > 0) { | ||
| process.exit(1); | ||
| } | ||
| process.exit(0); | ||
| } | ||
|
|
||
| module.exports = { | ||
| syncCommandAdapters | ||
| }; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new resolver now searches
CLAUDE_PROJECT_DIRandprocess.cwd()before the known plugin directories, so whenCLAUDE_PLUGIN_ROOTis unset it will execute any project-localscripts/hooks/session-start.jsit finds first. In normal usagecwdis the user’s repo, so this can run unrelated or untrusted repository code at session start instead of the bundled hook, changing behavior and creating a code-execution risk tied to repository contents.Useful? React with 👍 / 👎.