Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
One-time setup that gathers your project's design context and saves it to CLAUDE.md for future sessions.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/hook-before-edit.mjs
1#!/usr/bin/env node2/**3* Impeccable design hook — Cursor preToolUse write gate.4*5* Cursor's stop hook is not consistently dispatched by the headless agent, so6* this hook checks proposed Write/Edit content before it lands. It only denies7* writes when the real detector finds an issue in the proposed UI content.8*9* Contract: never break a turn accidentally. On malformed input or internal10* errors, allow the tool and exit 0.11*/1213import fs from 'node:fs';14import path from 'node:path';1516import {17ALLOWED_EXTS,18EDIT_COUNT_THRESHOLD,19GENERATED_PATH,20SENSITIVE_PATH,21appendDesignSystemNote,22designSystemOptions,23filterFindings,24loadDetector,25matchesAnyGlob,26persistCache,27readCache,28readConfig,29renderTemplate,30resolveProjectCwd,31truthy,32writeAuditLog,33} from './hook-lib.mjs';3435async function readStdin() {36if (process.stdin.isTTY) return '';37const chunks = [];38for await (const chunk of process.stdin) chunks.push(chunk);39return Buffer.concat(chunks).toString('utf-8');40}4142function done(payload = null) {43if (payload) process.stdout.write(JSON.stringify(payload));44process.exit(0);45}4647function allow(extra = {}, payload = {}) {48writeAuditLog(process.env, {49ts: new Date().toISOString(),50event: 'preToolUse',51...extra,52});53return done({ permission: 'allow', ...payload });54}5556function deny(message, audit) {57writeAuditLog(process.env, {58ts: new Date().toISOString(),59event: 'preToolUse',60blocked: true,61...audit,62});63return done({64permission: 'deny',65user_message: message,66agent_message: message,67});68}6970function toolInput(event) {71return event?.tool_input && typeof event.tool_input === 'object' ? event.tool_input : {};72}7374function proposedFilePath(event, cwd) {75const input = toolInput(event);76const raw = input.file_path || input.path || input.target_file || event?.file_path;77const candidate = typeof raw === 'string' && raw.trim()78? raw79: shellWriteDestination(shellCommand(input));80if (typeof candidate !== 'string' || !candidate.trim()) return '';81return path.isAbsolute(candidate) ? candidate : path.resolve(cwd, candidate);82}8384function proposedContent(event, cwd, filePath) {85const input = toolInput(event);86for (const key of ['content', 'streamContent', 'text']) {87if (typeof input[key] === 'string') return input[key];88}8990const editProjection = projectedEditContent(input, filePath, cwd);91if (editProjection !== undefined) return editProjection;9293if (hasFragmentEditContent(input)) {94return { skipped: 'fragment-only-edit' };95}9697const command = shellCommand(input);98const pythonContent = shellPythonWriteContent(command);99if (pythonContent) return pythonContent;100const shellContent = shellHereDocContent(command);101if (shellContent) return shellContent;102const copiedContent = shellCopiedFileContent(command, cwd);103if (copiedContent) return copiedContent;104return '';105}106107function hasFragmentEditContent(input) {108if (!input || typeof input !== 'object') return false;109if (typeof input.new_string === 'string' || typeof input.newString === 'string' || typeof input.new_str === 'string' || typeof input.replacement === 'string') {110return true;111}112return Array.isArray(input.edits) && input.edits.some((edit) => edit && typeof edit === 'object');113}114115function projectedEditContent(input, filePath, cwd) {116if (!filePath) return undefined;117const singleOld = firstString(input, ['old_string', 'oldString', 'old_str', 'target']);118const singleNew = firstString(input, ['new_string', 'newString', 'new_str', 'replacement']);119if (singleOld !== undefined || singleNew !== undefined) {120if (singleOld === undefined || singleNew === undefined) return { skipped: 'fragment-only-edit' };121const original = readExistingProjectFile(filePath, cwd);122if (original === null) return { skipped: 'edit-original-unreadable' };123const projected = replaceOnce(original, singleOld, singleNew);124return projected === null ? { skipped: 'edit-old-string-missing' } : projected;125}126127if (!Array.isArray(input.edits)) return undefined;128const original = readExistingProjectFile(filePath, cwd);129if (original === null) return { skipped: 'edit-original-unreadable' };130131let projected = original;132for (const edit of input.edits) {133if (!edit || typeof edit !== 'object') return { skipped: 'fragment-only-edit' };134const oldString = firstString(edit, ['old_string', 'oldString', 'old_str', 'target']);135const newString = firstString(edit, ['new_string', 'newString', 'new_str', 'replacement']);136if (oldString === undefined || newString === undefined) return { skipped: 'fragment-only-edit' };137const next = replaceOnce(projected, oldString, newString);138if (next === null) return { skipped: 'edit-old-string-missing' };139projected = next;140}141return projected;142}143144function firstString(obj, keys) {145for (const key of keys) {146if (typeof obj?.[key] === 'string') return obj[key];147}148return undefined;149}150151function replaceOnce(original, oldString, newString) {152if (oldString === '') return null;153const index = original.indexOf(oldString);154if (index === -1) return null;155return `${original.slice(0, index)}${newString}${original.slice(index + oldString.length)}`;156}157158function readExistingProjectFile(filePath, cwd) {159if (!isInsideProject(filePath, cwd)) return null;160if (SENSITIVE_PATH.test(filePath) || GENERATED_PATH.test(filePath)) return null;161try {162const stat = fs.statSync(filePath);163if (!stat.isFile() || stat.size > 1024 * 1024) return null;164return fs.readFileSync(filePath, 'utf-8');165} catch {166return null;167}168}169170function shellCommand(input) {171if (typeof input.command === 'string') return input.command;172if (input.args && typeof input.args.command === 'string') return input.args.command;173return '';174}175176function shellRedirectPath(command) {177if (!command || typeof command !== 'string') return '';178const match = command.match(/(?:^|[\s;&|])(?:>>?|1>>?)\s*(?:"([^"]+)"|'([^']+)'|([^<>\s]+))/);179return (match?.[1] || match?.[2] || match?.[3] || '').trim();180}181182function shellWriteDestination(command) {183return shellRedirectPath(command) || shellTeeDestination(command) || shellCopyPaths(command)?.dest || shellPythonWriteDestination(command) || '';184}185186function shellPythonWriteDestination(command) {187if (!/\bpython(?:3)?\b/.test(command || '')) return '';188const directPath = firstMatch(command, /(?:^|[^\w.])(?:pathlib\.)?Path\(\s*(["'])(.*?)\1\s*\)\s*\.write_text\s*\(/);189if (directPath) return directPath;190191const pathsByVar = new Map();192const assignmentRe = /\b([A-Za-z_]\w*)\s*=\s*(?:pathlib\.)?Path\(\s*(["'])(.*?)\2\s*\)/g;193let assignment;194while ((assignment = assignmentRe.exec(command))) {195pathsByVar.set(assignment[1], assignment[3]);196}197198const writeVarRe = /\b([A-Za-z_]\w*)\.write_text\s*\(/g;199let writeVar;200while ((writeVar = writeVarRe.exec(command))) {201const candidate = pathsByVar.get(writeVar[1]);202if (candidate) return candidate;203}204205return firstMatch(command, /\bopen\(\s*(["'])(.*?)\1\s*,\s*(["'])[wax](?:\+)?b?\3/);206}207208function firstMatch(value, re) {209const match = String(value || '').match(re);210return (match?.[2] || '').trim();211}212213function shellTeeDestination(command) {214const words = shellWords(command);215const teeIndex = words.findIndex((word) => path.basename(word) === 'tee');216if (teeIndex === -1) return '';217for (const word of words.slice(teeIndex + 1)) {218if (['&&', '||', ';', '|'].includes(word)) break;219if (word === '--') continue;220if (word.startsWith('-')) continue;221return word;222}223return '';224}225226function shellCopiedFileContent(command, cwd) {227const source = shellCopyPaths(command)?.source;228if (!source) return '';229const sourcePath = path.isAbsolute(source) ? source : path.resolve(cwd, source);230if (!isInsideProject(sourcePath, cwd)) return '';231if (SENSITIVE_PATH.test(sourcePath) || GENERATED_PATH.test(sourcePath)) return '';232try {233const stat = fs.statSync(sourcePath);234if (!stat.isFile() || stat.size > 1024 * 1024) return '';235return fs.readFileSync(sourcePath, 'utf-8');236} catch {237return '';238}239}240241function shellCopyPaths(command) {242const words = shellWords(command);243if (words.length < 3 || path.basename(words[0]) !== 'cp') return null;244const args = [];245for (const word of words.slice(1)) {246if (['&&', '||', ';', '|'].includes(word)) break;247if (word === '--') continue;248if (word.startsWith('-')) continue;249args.push(word);250}251if (args.length < 2) return null;252return { source: args[args.length - 2], dest: args[args.length - 1] };253}254255function shellWords(command) {256if (!command || typeof command !== 'string') return [];257const words = [];258const re = /"((?:\\"|[^"])*)"|'((?:\\'|[^'])*)'|([^\s]+)/g;259let match;260while ((match = re.exec(command))) {261words.push((match[1] ?? match[2] ?? match[3] ?? '').replace(/\\(["'])/g, '$1'));262}263return words;264}265266function shellHereDocContent(command) {267if (!command || typeof command !== 'string') return '';268const markerMatch = command.match(/<<-?\s*['"]?([A-Za-z0-9_.-]+)['"]?[^\r\n]*\r?\n/);269if (!markerMatch) return '';270const marker = markerMatch[1];271const start = (markerMatch.index || 0) + markerMatch[0].length;272const rest = command.slice(start);273const endRe = new RegExp(`\\r?\\n${escapeRegExp(marker)}(?:\\r?\\n|$)`);274const end = rest.search(endRe);275return end >= 0 ? rest.slice(0, end) : '';276}277278function shellPythonWriteContent(command) {279if (!/\bpython(?:3)?\b/.test(command || '')) return '';280const script = shellHereDocContent(command) || command;281return pythonStringArg(script, /\.write_text\s*\(\s*/g) || pythonStringArg(script, /\.write\s*\(\s*/g);282}283284function pythonStringArg(script, prefixRe) {285let prefix;286while ((prefix = prefixRe.exec(script))) {287const start = prefixRe.lastIndex;288const triple = script.slice(start, start + 3);289if (triple === "'''" || triple === '"""') {290const end = script.indexOf(triple, start + 3);291if (end !== -1) return script.slice(start + 3, end);292continue;293}294const quote = script[start];295if (quote !== '"' && quote !== "'") continue;296let out = '';297for (let i = start + 1; i < script.length; i++) {298const ch = script[i];299if (ch === '\\') {300out += script[i + 1] || '';301i += 1;302} else if (ch === quote) {303return out;304} else {305out += ch;306}307}308}309return '';310}311312function escapeRegExp(value) {313return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');314}315316function relativePath(filePath, cwd) {317try {318const rel = path.relative(cwd, filePath);319if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) return filePath;320return rel.split(path.sep).join('/');321} catch {322return filePath;323}324}325326function isInsideProject(filePath, cwd) {327try {328const rel = path.relative(cwd, filePath);329return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));330} catch {331return false;332}333}334335function cursorBlockMessage(findings, filePath, config, cwd) {336const rendered = renderTemplate(findings, filePath, config, { cwd });337const blocked = rendered.replace(338'[impeccable@1] Design hook findings requiring review',339'[impeccable@1] Impeccable design hook blocked this write before it landed. Design hook findings requiring review',340);341return blocked.length > 4000 ? `${blocked.slice(0, 3984)}\n...(truncated)` : blocked;342}343344function findingSignature(findings) {345return findings346.map((finding) => `${finding.antipattern || 'unknown'}:${finding.line || 0}`)347.sort()348.join('|');349}350351function bumpCursorDenial(cache, sessionId, filePath, findings) {352const session = cache.sessions[sessionId] || { updatedAt: Date.now(), files: {} };353cache.sessions[sessionId] = session;354session.updatedAt = Date.now();355const fileEntry = session.files[filePath] || { editCount: 0, findings: [] };356session.files[filePath] = fileEntry;357const key = findingSignature(findings);358fileEntry.cursorDenials = fileEntry.cursorDenials && typeof fileEntry.cursorDenials === 'object'359? fileEntry.cursorDenials360: {};361fileEntry.cursorDenials[key] = (fileEntry.cursorDenials[key] || 0) + 1;362return { key, count: fileEntry.cursorDenials[key] };363}364365async function main() {366if (truthy(process.env.IMPECCABLE_HOOK_DISABLED)) {367return allow({ skipped: 'env-disabled' });368}369370let event = null;371try {372const raw = await readStdin();373if (raw) event = JSON.parse(raw);374} catch {375return allow({ skipped: 'stdin-malformed' });376}377378if (!event || typeof event !== 'object') {379return allow({ skipped: 'stdin-empty' });380}381382const cwd = resolveProjectCwd(event);383const started = Date.now();384const filePath = proposedFilePath(event, cwd);385const audit = {386harness: 'cursor',387cwd,388tool: event.tool_name || null,389file: filePath || null,390};391392if (!filePath) return allow({ ...audit, skipped: 'no-file-path', durationMs: Date.now() - started });393if (!isInsideProject(filePath, cwd)) return allow({ ...audit, skipped: 'outside-project', durationMs: Date.now() - started });394if (SENSITIVE_PATH.test(filePath)) return allow({ ...audit, skipped: 'sensitive', durationMs: Date.now() - started });395if (GENERATED_PATH.test(filePath)) return allow({ ...audit, skipped: 'generated', durationMs: Date.now() - started });396397const ext = path.extname(filePath).toLowerCase();398audit.ext = ext;399if (!ALLOWED_EXTS.has(ext)) return allow({ ...audit, skipped: 'extension', durationMs: Date.now() - started });400401const contentResult = proposedContent(event, cwd, filePath);402if (contentResult && typeof contentResult === 'object' && contentResult.skipped) {403return allow({ ...audit, skipped: contentResult.skipped, durationMs: Date.now() - started });404}405const content = typeof contentResult === 'string' ? contentResult : '';406if (!content) return allow({ ...audit, skipped: 'no-proposed-content', durationMs: Date.now() - started });407408const config = readConfig(cwd);409if (config.enabled === false) return allow({ ...audit, skipped: 'config-disabled', durationMs: Date.now() - started });410411const rel = relativePath(filePath, cwd);412if (matchesAnyGlob(rel, config.ignoreFiles) || matchesAnyGlob(filePath, config.ignoreFiles)) {413return allow({ ...audit, skipped: 'config-ignore-file', durationMs: Date.now() - started });414}415416const detector = await loadDetector();417if (!detector || typeof detector.detectText !== 'function') {418return allow({ ...audit, skipped: 'detector-missing', durationMs: Date.now() - started });419}420const scanOptions = designSystemOptions(config, detector, cwd);421422let findings = [];423try {424findings = await detector.detectText(content, filePath, scanOptions);425} catch {426return allow({ ...audit, error: 'detector-threw', durationMs: Date.now() - started });427}428429const filtered = filterFindings(findings || [], content, ext, config);430if (filtered.length === 0) {431return allow({432...audit,433findings: (findings || []).length,434blockedFindings: 0,435durationMs: Date.now() - started,436});437}438439const message = appendDesignSystemNote(cursorBlockMessage(filtered, filePath, config, cwd), scanOptions);440const sessionId = event.session_id || event.conversation_id || 'unknown';441const cache = readCache(cwd);442const denial = bumpCursorDenial(cache, sessionId, filePath, filtered);443persistCache(cwd, cache);444if (denial.count > EDIT_COUNT_THRESHOLD) {445const warning = `${message}\n\nThis is the ${denial.count}th repeated denial for the same file and finding signature, so Impeccable is allowing this write to avoid a loop. Reconsider the issue immediately after the tool runs.`;446return allow({447...audit,448findings: (findings || []).length,449blockedFindings: filtered.length,450cursorDenialKey: denial.key,451cursorDenialCount: denial.count,452downgraded: true,453chars: warning.length,454durationMs: Date.now() - started,455}, {456user_message: warning,457agent_message: warning,458});459}460return deny(message, {461...audit,462findings: (findings || []).length,463blockedFindings: filtered.length,464cursorDenialKey: denial.key,465cursorDenialCount: denial.count,466chars: message.length,467durationMs: Date.now() - started,468});469}470471main().catch((err) => {472if (process.env.IMPECCABLE_HOOK_DEBUG) {473process.stderr.write(`[impeccable-hook-before-edit] ${err}\n`);474}475done({ permission: 'allow' });476});477