fix: harden Cursor hooks

This commit is contained in:
ZacharyZhang-NY committed 2026-07-19 23:28:01 -04:00
1 parent d5a2dcbe53
commit 6d36f557b1
7 files changed
+1109 -126

No files matched your search

+43 -125
View File
@@ -1,151 +1,69 @@
import fsp from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import path from 'node:path';
import { resolvePrivateChildDirectory } from '../runtime-paths.js';
import type { AgentPlugin, InstallConfig, PluginStatus } from './base.js';
import {
AGENT_KEY,
AUDIT_SCRIPT,
GUARD_SCRIPT,
buildHandler,
removeManagedHooks,
renderDocument,
runtimeContracts,
samePath,
} from './cursor-contract.js';
import {
readDocument,
requireRuntime,
runtimeFilesExist,
writeDocument,
} from './cursor-io.js';
import { SUPPORTED_AGENTS } from './registry.js';
const AGENT_KEY = 'cursor';
const entry = SUPPORTED_AGENTS.get(AGENT_KEY)!;
function resolveConfigDir(): string {
// .cursor is project-relative, but for global install use home dir
return path.join(os.homedir(), entry.configDir);
}
function resolveConfigPath(): string {
return path.join(resolveConfigDir(), entry.configFile);
}
function buildHookCommand(scriptPath: string): string {
return `node "${scriptPath}"`;
}
function isElydoraCommand(cmd: string, agentId?: string): boolean {
if (!cmd.includes('elydora')) return false;
if (agentId) {
return cmd.includes(agentId);
}
return true;
}
function filterElydoraEntries(arr: Array<Record<string, unknown>>, agentId?: string): Array<Record<string, unknown>> {
return arr.filter(
(h) => typeof h.command === 'string' && !isElydoraCommand(h.command, agentId),
);
}
export const cursorPlugin: AgentPlugin = {
async install(config: InstallConfig): Promise<void> {
const configDir = resolveConfigDir();
await fsp.mkdir(configDir, { recursive: true });
const configPath = resolveConfigPath();
let settings: Record<string, unknown> = {};
try {
const raw = await fsp.readFile(configPath, 'utf-8');
settings = JSON.parse(raw);
} catch {
// Start fresh
if (!config.agentId) throw new Error('agentId is required');
const document = await readDocument();
const runtimeRoot = path.join(os.homedir(), '.elydora');
const agentDirectory = resolvePrivateChildDirectory(runtimeRoot, config.agentId);
const expectedGuard = path.join(agentDirectory, GUARD_SCRIPT);
const expectedAudit = path.join(agentDirectory, AUDIT_SCRIPT);
if (!samePath(config.guardScriptPath, expectedGuard)) {
throw new Error(`Elydora guard runtime must use the managed agent directory: ${expectedGuard}`);
}
if (!settings.hooks || typeof settings.hooks !== 'object') {
settings.hooks = {};
if (!samePath(config.hookScriptPath, expectedAudit)) {
throw new Error(`Elydora audit runtime must use the managed agent directory: ${expectedAudit}`);
}
const hooks = settings.hooks as Record<string, unknown>;
await requireRuntime(expectedGuard, 'Elydora guard runtime');
await requireRuntime(expectedAudit, 'Elydora audit runtime');
// --- preToolUse (guard — freeze enforcement) ---
if (!Array.isArray(hooks.preToolUse)) {
hooks.preToolUse = [];
}
const preFiltered = filterElydoraEntries(hooks.preToolUse as Array<Record<string, unknown>>);
preFiltered.push({ command: buildHookCommand(config.guardScriptPath) });
hooks.preToolUse = preFiltered;
// --- postToolUse (audit logging) ---
if (!Array.isArray(hooks.postToolUse)) {
hooks.postToolUse = [];
}
const postFiltered = filterElydoraEntries(hooks.postToolUse as Array<Record<string, unknown>>);
postFiltered.push({ command: buildHookCommand(config.hookScriptPath) });
hooks.postToolUse = postFiltered;
settings.hooks = hooks;
settings.version = 1;
await fsp.writeFile(configPath, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
const hooks = removeManagedHooks(document.hooks);
hooks.preToolUse = [...(hooks.preToolUse ?? []), buildHandler(expectedGuard)];
hooks.postToolUse = [...(hooks.postToolUse ?? []), buildHandler(expectedAudit)];
await writeDocument(renderDocument(document, hooks));
console.log(` Cursor hooks: ${document.filePath}`);
},
async uninstall(agentId?: string): Promise<void> {
const configPath = resolveConfigPath();
let settings: Record<string, unknown>;
try {
const raw = await fsp.readFile(configPath, 'utf-8');
settings = JSON.parse(raw);
} catch {
return;
}
const hooks = settings.hooks as Record<string, unknown> | undefined;
if (!hooks) return;
if (Array.isArray(hooks.preToolUse)) {
hooks.preToolUse = filterElydoraEntries(hooks.preToolUse as Array<Record<string, unknown>>, agentId);
}
if (Array.isArray(hooks.postToolUse)) {
hooks.postToolUse = filterElydoraEntries(hooks.postToolUse as Array<Record<string, unknown>>, agentId);
}
await fsp.writeFile(configPath, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
const document = await readDocument();
const hooks = removeManagedHooks(document.hooks, agentId);
await writeDocument(renderDocument(document, hooks));
},
async status(): Promise<PluginStatus> {
const configPath = resolveConfigPath();
let hookConfigured = false;
let hookScriptPath = '';
try {
const raw = await fsp.readFile(configPath, 'utf-8');
const settings = JSON.parse(raw);
const hooks = settings.hooks as Record<string, unknown> | undefined;
if (hooks) {
const check = (arr: unknown) =>
Array.isArray(arr) && (arr as Array<Record<string, unknown>>).some(
(h) => typeof h.command === 'string' && h.command.includes('elydora'),
);
hookConfigured = check(hooks.preToolUse) && check(hooks.postToolUse);
// Extract hook script path from postToolUse command
if (hookConfigured && Array.isArray(hooks.postToolUse)) {
for (const h of hooks.postToolUse as Array<Record<string, unknown>>) {
const cmd = h.command as string;
if (cmd && cmd.includes('elydora')) {
hookScriptPath = cmd.replace(/^node\s+"?/, '').replace(/"?\s*$/, '');
}
}
}
}
} catch {
// Config not readable
}
let hookScriptExists = false;
if (hookScriptPath) {
try {
await fsp.access(hookScriptPath);
hookScriptExists = true;
} catch {
// File doesn't exist
}
}
const document = await readDocument();
const contracts = runtimeContracts(document.hooks);
const hookConfigured = contracts.length > 0;
const hookScriptExists = hookConfigured && await runtimeFilesExist(contracts);
return {
installed: hookConfigured && hookScriptExists,
agentName: AGENT_KEY,
displayName: entry.name,
hookConfigured,
hookScriptExists,
configPath,
configPath: document.filePath,
};
},
};