specialist-agent 1.6.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +77 -0
- package/.claude-plugin/plugin.json +43 -0
- package/.codex/INSTALL.md +113 -0
- package/.cursor-plugin/marketplace.json +9 -0
- package/.cursor-plugin/plugin.json +35 -0
- package/.opencode/INSTALL.md +123 -0
- package/README.md +204 -263
- package/agents/analyst-lite.md +48 -0
- package/agents/analyst.md +391 -0
- package/agents/api-lite.md +43 -0
- package/agents/api.md +145 -0
- package/agents/cloud-lite.md +1 -1
- package/agents/cloud.md +1 -1
- package/agents/data-lite.md +1 -1
- package/agents/data.md +1 -1
- package/agents/debugger-lite.md +46 -0
- package/agents/debugger.md +406 -0
- package/agents/deps-lite.md +41 -0
- package/agents/deps.md +206 -0
- package/agents/designer-lite.md +1 -1
- package/agents/designer.md +1 -1
- package/agents/devops-lite.md +1 -1
- package/agents/devops.md +1 -1
- package/agents/docs-lite.md +42 -0
- package/agents/docs.md +197 -0
- package/agents/executor-lite.md +61 -0
- package/agents/executor.md +456 -0
- package/agents/explorer-lite.md +1 -1
- package/agents/explorer.md +1 -1
- package/agents/finance-lite.md +1 -1
- package/agents/finance.md +1 -1
- package/agents/i18n-lite.md +44 -0
- package/agents/i18n.md +198 -0
- package/agents/legal-lite.md +1 -1
- package/agents/legal.md +1 -1
- package/agents/memory.md +240 -0
- package/agents/orchestrator-lite.md +40 -0
- package/agents/orchestrator.md +387 -0
- package/agents/pair-lite.md +38 -0
- package/agents/pair.md +308 -0
- package/agents/perf-lite.md +42 -0
- package/agents/perf.md +141 -0
- package/agents/planner-lite.md +59 -0
- package/agents/planner.md +265 -0
- package/agents/refactor-lite.md +42 -0
- package/agents/refactor.md +178 -0
- package/agents/scout-lite.md +37 -0
- package/agents/scout.md +222 -0
- package/agents/security-lite.md +1 -1
- package/agents/security.md +1 -1
- package/agents/starter-lite.md +1 -1
- package/agents/starter.md +1 -1
- package/agents/tdd-lite.md +43 -0
- package/agents/tdd.md +317 -0
- package/agents/tester-lite.md +1 -1
- package/agents/tester.md +1 -1
- package/cli/index.mjs +636 -15
- package/hooks/after-review.mjs +130 -0
- package/hooks/after-task.mjs +117 -0
- package/hooks/before-plan.mjs +155 -0
- package/hooks/before-review.mjs +139 -0
- package/hooks/hooks.json +81 -0
- package/hooks/native/auto-dispatch.mjs +197 -0
- package/hooks/native/auto-format.mjs +193 -0
- package/hooks/native/security-config.json +31 -0
- package/hooks/native/security-guard.mjs +267 -0
- package/hooks/native/session-context.mjs +136 -0
- package/hooks/native/settings-template.json +50 -0
- package/hooks/native/utils.mjs +62 -0
- package/hooks/on-error.mjs +129 -0
- package/hooks/session-end.mjs +185 -0
- package/hooks/session-start.mjs +154 -0
- package/package.json +16 -3
- package/packs/nextjs/agents/builder.md +24 -1
- package/packs/nextjs/agents/doctor.md +70 -4
- package/packs/nextjs/agents/migrator.md +24 -1
- package/packs/nextjs/agents/reviewer.md +93 -3
- package/packs/nextjs/agents-lite/builder.md +1 -1
- package/packs/nextjs/agents-lite/doctor.md +1 -1
- package/packs/nextjs/agents-lite/migrator.md +1 -1
- package/packs/nextjs/agents-lite/reviewer.md +1 -1
- package/packs/nextjs/skills/dev-create-component/SKILL.md +1 -1
- package/packs/nextjs/skills/dev-create-hook/SKILL.md +1 -1
- package/packs/nextjs/skills/dev-create-module/SKILL.md +1 -1
- package/packs/nextjs/skills/dev-create-service/SKILL.md +1 -1
- package/packs/nextjs/skills/dev-create-test/SKILL.md +1 -1
- package/packs/nextjs/skills/dev-generate-types/SKILL.md +1 -1
- package/packs/nextjs/skills/docs-onboard/SKILL.md +1 -1
- package/packs/nextjs/skills/migration-migrate-component/SKILL.md +1 -1
- package/packs/nextjs/skills/migration-migrate-module/SKILL.md +1 -1
- package/packs/nextjs/skills/review-check-architecture/SKILL.md +1 -1
- package/packs/nextjs/skills/review-fix-violations/SKILL.md +1 -1
- package/packs/nextjs/skills/review-review/SKILL.md +1 -1
- package/packs/react/agents/builder.md +24 -1
- package/packs/react/agents/doctor.md +107 -13
- package/packs/react/agents/migrator.md +24 -1
- package/packs/react/agents/reviewer.md +113 -9
- package/packs/react/agents-lite/builder.md +1 -1
- package/packs/react/agents-lite/doctor.md +1 -1
- package/packs/react/agents-lite/migrator.md +1 -1
- package/packs/react/agents-lite/reviewer.md +1 -1
- package/packs/react/skills/dev-create-component/SKILL.md +1 -1
- package/packs/react/skills/dev-create-hook/SKILL.md +1 -1
- package/packs/react/skills/dev-create-module/SKILL.md +1 -1
- package/packs/react/skills/dev-create-service/SKILL.md +1 -1
- package/packs/react/skills/dev-create-test/SKILL.md +1 -1
- package/packs/react/skills/dev-generate-types/SKILL.md +1 -1
- package/packs/react/skills/docs-onboard/SKILL.md +1 -1
- package/packs/react/skills/migration-migrate-component/SKILL.md +1 -1
- package/packs/react/skills/migration-migrate-module/SKILL.md +1 -1
- package/packs/react/skills/review-check-architecture/SKILL.md +1 -1
- package/packs/react/skills/review-fix-violations/SKILL.md +1 -1
- package/packs/react/skills/review-review/SKILL.md +1 -1
- package/packs/svelte/agents/builder.md +24 -1
- package/packs/svelte/agents/doctor.md +70 -4
- package/packs/svelte/agents/migrator.md +24 -1
- package/packs/svelte/agents/reviewer.md +93 -3
- package/packs/svelte/agents-lite/builder.md +1 -1
- package/packs/svelte/agents-lite/doctor.md +1 -1
- package/packs/svelte/agents-lite/migrator.md +1 -1
- package/packs/svelte/agents-lite/reviewer.md +1 -1
- package/packs/svelte/skills/dev-create-component/SKILL.md +1 -1
- package/packs/svelte/skills/dev-create-module/SKILL.md +1 -1
- package/packs/svelte/skills/dev-create-service/SKILL.md +1 -1
- package/packs/svelte/skills/dev-create-store/SKILL.md +1 -1
- package/packs/svelte/skills/dev-create-test/SKILL.md +1 -1
- package/packs/svelte/skills/dev-generate-types/SKILL.md +1 -1
- package/packs/svelte/skills/docs-onboard/SKILL.md +1 -1
- package/packs/svelte/skills/migration-migrate-component/SKILL.md +1 -1
- package/packs/svelte/skills/migration-migrate-module/SKILL.md +1 -1
- package/packs/svelte/skills/review-check-architecture/SKILL.md +1 -1
- package/packs/svelte/skills/review-fix-violations/SKILL.md +1 -1
- package/packs/svelte/skills/review-review/SKILL.md +1 -1
- package/packs/vue/agents/builder.md +24 -1
- package/packs/vue/agents/doctor.md +107 -13
- package/packs/vue/agents/migrator.md +24 -1
- package/packs/vue/agents/reviewer.md +113 -9
- package/packs/vue/agents-lite/builder.md +1 -1
- package/packs/vue/agents-lite/doctor.md +1 -1
- package/packs/vue/agents-lite/migrator.md +1 -1
- package/packs/vue/agents-lite/reviewer.md +1 -1
- package/packs/vue/skills/dev-create-component/SKILL.md +1 -1
- package/packs/vue/skills/dev-create-composable/SKILL.md +1 -1
- package/packs/vue/skills/dev-create-module/SKILL.md +1 -1
- package/packs/vue/skills/dev-create-service/SKILL.md +1 -1
- package/packs/vue/skills/dev-create-test/SKILL.md +1 -1
- package/packs/vue/skills/dev-generate-types/SKILL.md +1 -1
- package/packs/vue/skills/docs-onboard/SKILL.md +1 -1
- package/packs/vue/skills/migration-migrate-component/SKILL.md +1 -1
- package/packs/vue/skills/migration-migrate-module/SKILL.md +1 -1
- package/packs/vue/skills/review-check-architecture/SKILL.md +1 -1
- package/packs/vue/skills/review-fix-violations/SKILL.md +1 -1
- package/packs/vue/skills/review-review/SKILL.md +1 -1
- package/skills/audit/SKILL.md +169 -0
- package/skills/brainstorm/SKILL.md +245 -0
- package/skills/checkpoint/SKILL.md +209 -0
- package/skills/debug/SKILL.md +210 -0
- package/skills/estimate/SKILL.md +145 -0
- package/skills/finish/SKILL.md +211 -0
- package/skills/health/SKILL.md +242 -0
- package/skills/learn/SKILL.md +260 -0
- package/skills/migrate-framework/SKILL.md +172 -0
- package/skills/onboard/SKILL.md +168 -0
- package/skills/plan/SKILL.md +132 -0
- package/skills/recall/SKILL.md +150 -0
- package/skills/remember/SKILL.md +158 -0
- package/skills/tdd/SKILL.md +156 -0
- package/skills/tutorial/SKILL.md +346 -0
- package/skills/verify/SKILL.md +168 -0
- package/skills/worktree/SKILL.md +168 -0
- package/skills/write-skill/SKILL.md +213 -0
- package/templates/industry/ecommerce.md +127 -0
- package/templates/industry/fintech.md +161 -0
- package/templates/industry/saas.md +134 -0
- package/templates/session-memory.json +48 -0
- package/templates/skill-template/SKILL.md +90 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Security Guard — PreToolUse Hook (Bash)
|
|
5
|
+
*
|
|
6
|
+
* Blocks dangerous shell commands BEFORE they execute.
|
|
7
|
+
* Design: fail-closed (if this script crashes, exit 2 = block).
|
|
8
|
+
*
|
|
9
|
+
* Claude Code event: PreToolUse (matcher: "Bash")
|
|
10
|
+
* Stdin: { tool_input: { command: "..." } }
|
|
11
|
+
* Block: exit 2 + stderr reason
|
|
12
|
+
* Allow: exit 0 (silent)
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { readFileSync } from 'fs';
|
|
16
|
+
import { join, dirname, resolve, normalize } from 'path';
|
|
17
|
+
import { fileURLToPath } from 'url';
|
|
18
|
+
|
|
19
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
20
|
+
const __dirname = dirname(__filename);
|
|
21
|
+
|
|
22
|
+
// ── Security Rules ──────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
const RULES = [
|
|
25
|
+
// CRITICAL — always block, no exceptions
|
|
26
|
+
{
|
|
27
|
+
id: 'catastrophic-rm',
|
|
28
|
+
severity: 'CRITICAL',
|
|
29
|
+
test: (cmd) => {
|
|
30
|
+
const normalized = cmd.replace(/\s+/g, ' ').trim();
|
|
31
|
+
// Match rm with -r and -f flags targeting / or ~ or bare .
|
|
32
|
+
if (!/\brm\b/.test(normalized)) return false;
|
|
33
|
+
if (!/-[a-zA-Z]*r/.test(normalized) && !/-[a-zA-Z]*R/.test(normalized)) return false;
|
|
34
|
+
// Check target paths
|
|
35
|
+
const dangerousTargets = [
|
|
36
|
+
/\s\/\s*$/, /\s\/[^a-zA-Z]/, // rm -rf /
|
|
37
|
+
/\s~\s*$/, /\s~\/\s*$/, // rm -rf ~ or ~/
|
|
38
|
+
/\s\.\s*$/, /\s\.\/\s*$/, // rm -rf . or ./
|
|
39
|
+
];
|
|
40
|
+
for (const pattern of dangerousTargets) {
|
|
41
|
+
if (pattern.test(' ' + normalized)) {
|
|
42
|
+
// Check allowlist — safe directories are OK
|
|
43
|
+
const safeTargets = [
|
|
44
|
+
/node_modules/, /dist[\/\\]?/, /build[\/\\]?/, /\.next[\/\\]?/,
|
|
45
|
+
/\.nuxt[\/\\]?/, /\.svelte-kit[\/\\]?/, /coverage[\/\\]?/,
|
|
46
|
+
/\.turbo[\/\\]?/, /\.cache[\/\\]?/, /__pycache__[\/\\]?/,
|
|
47
|
+
];
|
|
48
|
+
if (safeTargets.some(safe => safe.test(normalized))) return false;
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
},
|
|
54
|
+
message: 'BLOCKED [CRITICAL]: Recursive delete of system/home/project root detected.\nThis command could destroy your entire filesystem.\nIf you need to clean a specific directory, target it explicitly (e.g., rm -rf dist/).'
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: 'fork-bomb',
|
|
58
|
+
severity: 'CRITICAL',
|
|
59
|
+
test: (cmd) => /:\(\)\s*\{.*\|.*&\s*\}\s*;?\s*:/.test(cmd) || /\.\/bomb/.test(cmd),
|
|
60
|
+
message: 'BLOCKED [CRITICAL]: Fork bomb detected. This would crash the system by spawning infinite processes.'
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: 'disk-wipe',
|
|
64
|
+
severity: 'CRITICAL',
|
|
65
|
+
test: (cmd) => {
|
|
66
|
+
return /\bmkfs\b/.test(cmd) ||
|
|
67
|
+
/\bdd\b.*\bif=.*\/dev\//.test(cmd) && /\bof=.*\/dev\//.test(cmd) ||
|
|
68
|
+
/>\s*\/dev\/[hs]d[a-z]/.test(cmd);
|
|
69
|
+
},
|
|
70
|
+
message: 'BLOCKED [CRITICAL]: Disk wipe command detected. This would destroy disk data irreversibly.'
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
// HIGH — block with descriptive message
|
|
74
|
+
{
|
|
75
|
+
id: 'force-push-protected',
|
|
76
|
+
severity: 'HIGH',
|
|
77
|
+
test: (cmd, config) => {
|
|
78
|
+
if (!/\bgit\s+push\b/.test(cmd)) return false;
|
|
79
|
+
if (!/(-f\b|--force\b)/.test(cmd)) return false;
|
|
80
|
+
// force-with-lease is safer, allow it
|
|
81
|
+
if (/--force-with-lease/.test(cmd)) return false;
|
|
82
|
+
// Check if targeting a protected branch
|
|
83
|
+
const branches = config?.protectedBranches || ['main', 'master', 'production', 'release'];
|
|
84
|
+
return branches.some(branch => new RegExp(`\\b${branch}\\b`).test(cmd));
|
|
85
|
+
},
|
|
86
|
+
message: 'BLOCKED [HIGH]: Force push to protected branch.\nUse a feature branch and create a PR instead.\nIf you need to force push, use --force-with-lease for safety, or target a feature branch.'
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
id: 'hard-reset',
|
|
90
|
+
severity: 'HIGH',
|
|
91
|
+
test: (cmd) => {
|
|
92
|
+
if (!/\bgit\s+reset\b/.test(cmd)) return false;
|
|
93
|
+
if (/--hard/.test(cmd)) return true;
|
|
94
|
+
return false;
|
|
95
|
+
},
|
|
96
|
+
message: 'BLOCKED [HIGH]: git reset --hard discards all uncommitted changes permanently.\nConsider using: git stash (to save changes), git reset --soft (to unstage), or git checkout -- <file> (for specific files).'
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: 'sql-destructive',
|
|
100
|
+
severity: 'HIGH',
|
|
101
|
+
test: (cmd) => {
|
|
102
|
+
const upper = cmd.toUpperCase();
|
|
103
|
+
return /\bDROP\s+(TABLE|DATABASE|SCHEMA)\b/.test(upper) ||
|
|
104
|
+
/\bTRUNCATE\s+TABLE\b/.test(upper) ||
|
|
105
|
+
/\bDELETE\s+FROM\s+\w+\s*;?\s*$/.test(upper); // DELETE without WHERE
|
|
106
|
+
},
|
|
107
|
+
message: 'BLOCKED [HIGH]: Destructive SQL command detected.\nDROP/TRUNCATE/DELETE-without-WHERE can cause irreversible data loss.\nUse a transaction, add a WHERE clause, or test on a staging database first.'
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: 'pipe-to-shell',
|
|
111
|
+
severity: 'HIGH',
|
|
112
|
+
test: (cmd) => {
|
|
113
|
+
return /\bcurl\b.*\|\s*(bash|sh|zsh)\b/.test(cmd) ||
|
|
114
|
+
/\bwget\b.*\|\s*(bash|sh|zsh)\b/.test(cmd) ||
|
|
115
|
+
/\bcurl\b.*-[a-zA-Z]*o\s*-\s*\|/.test(cmd);
|
|
116
|
+
},
|
|
117
|
+
message: 'BLOCKED [HIGH]: Pipe-to-shell pattern detected (curl/wget | bash).\nThis downloads and executes remote code without inspection.\nDownload the script first, review it, then execute: curl -o script.sh <url> && cat script.sh && bash script.sh'
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
id: 'chmod-777',
|
|
121
|
+
severity: 'HIGH',
|
|
122
|
+
test: (cmd) => /\bchmod\b.*\b777\b/.test(cmd),
|
|
123
|
+
message: 'BLOCKED [HIGH]: chmod 777 makes files world-readable/writable/executable.\nUse specific permissions instead: chmod 755 (directories), chmod 644 (files), chmod 600 (secrets).'
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
// MEDIUM — block access to sensitive files
|
|
127
|
+
{
|
|
128
|
+
id: 'env-file-read',
|
|
129
|
+
severity: 'MEDIUM',
|
|
130
|
+
test: (cmd) => {
|
|
131
|
+
// Block reading .env files (but allow .env.example, .env.sample, .env.template)
|
|
132
|
+
if (/\bcat\b.*\.env\b/.test(cmd) || /\bless\b.*\.env\b/.test(cmd) || /\bmore\b.*\.env\b/.test(cmd)) {
|
|
133
|
+
if (/\.env\.(example|sample|template|test|development)/.test(cmd)) return false;
|
|
134
|
+
if (/\.env\.local/.test(cmd)) return true; // .env.local has secrets
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
// Block reading private keys
|
|
138
|
+
if (/\bcat\b.*\.(pem|key|p12|pfx)\b/.test(cmd)) return true;
|
|
139
|
+
return false;
|
|
140
|
+
},
|
|
141
|
+
message: 'BLOCKED [MEDIUM]: Reading sensitive file (.env or private key) via shell.\nUse the Read tool instead, which provides better access control.\nAllowed: .env.example, .env.sample, .env.template'
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
id: 'env-file-write',
|
|
145
|
+
severity: 'MEDIUM',
|
|
146
|
+
test: (cmd) => {
|
|
147
|
+
// Block writing to .env files
|
|
148
|
+
return />\s*\.env\b/.test(cmd) && !/\.env\.(example|sample|template)/.test(cmd);
|
|
149
|
+
},
|
|
150
|
+
message: 'BLOCKED [MEDIUM]: Writing to .env file via shell.\nManage secrets through environment variables or a secrets manager, not by writing them to files via commands.'
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
id: 'inline-secrets',
|
|
154
|
+
severity: 'MEDIUM',
|
|
155
|
+
test: (cmd) => {
|
|
156
|
+
// Block patterns like: export SECRET_KEY=actual_value, TOKEN=xxx command
|
|
157
|
+
const secretPatterns = [
|
|
158
|
+
/\b\w*(API_KEY|SECRET|TOKEN|PASSWORD|CREDENTIALS|PRIVATE_KEY)\w*\s*=\s*['"][^'"]{8,}['"]/i,
|
|
159
|
+
/\bexport\s+\w*(API_KEY|SECRET|TOKEN|PASSWORD|CREDENTIALS|PRIVATE_KEY)\w*\s*=\s*\S{8,}/i,
|
|
160
|
+
];
|
|
161
|
+
return secretPatterns.some(p => p.test(cmd));
|
|
162
|
+
},
|
|
163
|
+
message: 'BLOCKED [MEDIUM]: Inline secret detected in command.\nNever put secrets directly in shell commands. Use environment variables already set in the system:\n export MY_SECRET=... (in your shell profile)\n Then reference as $MY_SECRET in commands.'
|
|
164
|
+
},
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
// ── Core Evaluation Function (exported for testing) ─────────
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Evaluate a command against security rules.
|
|
171
|
+
* @param {string} command - The shell command to evaluate
|
|
172
|
+
* @param {object} config - Security config (from security-config.json)
|
|
173
|
+
* @returns {{ blocked: boolean, rule?: string, severity?: string, message?: string }}
|
|
174
|
+
*/
|
|
175
|
+
export function evaluateCommand(command, config = {}) {
|
|
176
|
+
if (!command || typeof command !== 'string') {
|
|
177
|
+
return { blocked: false };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const enabledRules = config?.rules || {};
|
|
181
|
+
|
|
182
|
+
for (const rule of RULES) {
|
|
183
|
+
// Check if rule is disabled in config
|
|
184
|
+
if (enabledRules[rule.id]?.enabled === false) continue;
|
|
185
|
+
|
|
186
|
+
// Check allowlist
|
|
187
|
+
const allowlist = config?.allowlist || [];
|
|
188
|
+
const isAllowed = allowlist.some(pattern => command.includes(pattern));
|
|
189
|
+
if (isAllowed) continue;
|
|
190
|
+
|
|
191
|
+
// Test the rule
|
|
192
|
+
if (rule.test(command, config)) {
|
|
193
|
+
return {
|
|
194
|
+
blocked: true,
|
|
195
|
+
rule: rule.id,
|
|
196
|
+
severity: rule.severity,
|
|
197
|
+
message: rule.message,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return { blocked: false };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ── Main Execution ──────────────────────────────────────────
|
|
206
|
+
|
|
207
|
+
async function main() {
|
|
208
|
+
let input = null;
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
// Read stdin
|
|
212
|
+
const data = await new Promise((resolve) => {
|
|
213
|
+
let buf = '';
|
|
214
|
+
const timeout = setTimeout(() => resolve(buf), 3000);
|
|
215
|
+
process.stdin.setEncoding('utf-8');
|
|
216
|
+
process.stdin.on('data', (chunk) => { buf += chunk; });
|
|
217
|
+
process.stdin.on('end', () => { clearTimeout(timeout); resolve(buf); });
|
|
218
|
+
process.stdin.on('error', () => { clearTimeout(timeout); resolve(''); });
|
|
219
|
+
process.stdin.resume();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
input = data ? JSON.parse(data) : null;
|
|
223
|
+
} catch {
|
|
224
|
+
// Fail-closed: if we can't parse input, block
|
|
225
|
+
process.stderr.write('Security Guard: Failed to parse input. Blocking as precaution.');
|
|
226
|
+
process.exit(2);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!input?.tool_input?.command) {
|
|
230
|
+
// No command to evaluate — allow
|
|
231
|
+
process.exit(0);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const command = input.tool_input.command;
|
|
235
|
+
|
|
236
|
+
// Load config
|
|
237
|
+
let config = {};
|
|
238
|
+
try {
|
|
239
|
+
const configPath = join(__dirname, 'security-config.json');
|
|
240
|
+
config = JSON.parse(readFileSync(configPath, 'utf-8'));
|
|
241
|
+
} catch {
|
|
242
|
+
// No config = use defaults (all rules enabled)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (config.enabled === false) {
|
|
246
|
+
process.exit(0);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const result = evaluateCommand(command, config);
|
|
250
|
+
|
|
251
|
+
if (result.blocked) {
|
|
252
|
+
process.stderr.write(`\n🛡️ Security Guard\n${result.message}\n\nRule: ${result.rule} (${result.severity})\nTo customize: edit hooks/native/security-config.json\n`);
|
|
253
|
+
process.exit(2);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Allow — silent exit
|
|
257
|
+
process.exit(0);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Only run main when executed directly
|
|
261
|
+
if (process.argv[1] && resolve(process.argv[1]) === resolve(__filename)) {
|
|
262
|
+
main().catch(() => {
|
|
263
|
+
// Fail-closed: any unhandled error = block
|
|
264
|
+
process.stderr.write('Security Guard: Unexpected error. Blocking as precaution.');
|
|
265
|
+
process.exit(2);
|
|
266
|
+
});
|
|
267
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Session Context — SessionStart Hook
|
|
5
|
+
*
|
|
6
|
+
* Injects project state as context when a Claude Code session begins.
|
|
7
|
+
* All operations are read-only. Errors are silently ignored.
|
|
8
|
+
*
|
|
9
|
+
* Claude Code event: SessionStart
|
|
10
|
+
* Output: { "additionalContext": "Project state summary..." }
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { execSync } from 'child_process';
|
|
14
|
+
import { readFileSync, readdirSync, existsSync } from 'fs';
|
|
15
|
+
import { join, resolve } from 'path';
|
|
16
|
+
import { fileURLToPath } from 'url';
|
|
17
|
+
|
|
18
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
19
|
+
|
|
20
|
+
// ── Data Collectors ─────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
function getGitInfo() {
|
|
23
|
+
try {
|
|
24
|
+
const branch = execSync('git branch --show-current', { encoding: 'utf-8', timeout: 5000 }).trim();
|
|
25
|
+
const statusOutput = execSync('git status --porcelain', { encoding: 'utf-8', timeout: 5000 });
|
|
26
|
+
const dirtyCount = statusOutput ? statusOutput.trim().split('\n').filter(Boolean).length : 0;
|
|
27
|
+
const lastCommit = execSync('git log -1 --oneline', { encoding: 'utf-8', timeout: 5000 }).trim();
|
|
28
|
+
return { branch, dirtyCount, lastCommit };
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getAgentCount(cwd) {
|
|
35
|
+
try {
|
|
36
|
+
const agentsDir = join(cwd, '.claude', 'agents');
|
|
37
|
+
if (!existsSync(agentsDir)) return 0;
|
|
38
|
+
return readdirSync(agentsDir).filter(f => f.endsWith('.md')).length;
|
|
39
|
+
} catch {
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getSkillCount(cwd) {
|
|
45
|
+
try {
|
|
46
|
+
const skillsDir = join(cwd, '.claude', 'skills');
|
|
47
|
+
if (!existsSync(skillsDir)) return 0;
|
|
48
|
+
return readdirSync(skillsDir, { withFileTypes: true }).filter(d => d.isDirectory()).length;
|
|
49
|
+
} catch {
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getMemoryDecisions(cwd) {
|
|
55
|
+
try {
|
|
56
|
+
const memoryPath = join(cwd, '.claude', 'session-memory.json');
|
|
57
|
+
if (!existsSync(memoryPath)) return 0;
|
|
58
|
+
const memory = JSON.parse(readFileSync(memoryPath, 'utf-8'));
|
|
59
|
+
return Array.isArray(memory.decisions) ? memory.decisions.length : 0;
|
|
60
|
+
} catch {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getActiveProfile(cwd) {
|
|
66
|
+
try {
|
|
67
|
+
const configPath = join(cwd, '.claude', 'config.json');
|
|
68
|
+
if (!existsSync(configPath)) return null;
|
|
69
|
+
const config = JSON.parse(readFileSync(configPath, 'utf-8'));
|
|
70
|
+
return config.profile || null;
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ── Context Builder (exported for testing) ──────────────────
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Build project context string from current state.
|
|
80
|
+
* @param {string} cwd - Project root directory
|
|
81
|
+
* @returns {string} Context summary or empty string
|
|
82
|
+
*/
|
|
83
|
+
export function buildContext(cwd) {
|
|
84
|
+
const parts = [];
|
|
85
|
+
|
|
86
|
+
// Git info
|
|
87
|
+
const git = getGitInfo();
|
|
88
|
+
if (git) {
|
|
89
|
+
parts.push(`Branch: ${git.branch}`);
|
|
90
|
+
if (git.dirtyCount > 0) {
|
|
91
|
+
parts.push(`Uncommitted files: ${git.dirtyCount}`);
|
|
92
|
+
}
|
|
93
|
+
parts.push(`Last commit: ${git.lastCommit}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Agents & skills
|
|
97
|
+
const agents = getAgentCount(cwd);
|
|
98
|
+
const skills = getSkillCount(cwd);
|
|
99
|
+
if (agents > 0 || skills > 0) {
|
|
100
|
+
parts.push(`Installed: ${agents} agents, ${skills} skills`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Memory
|
|
104
|
+
const decisions = getMemoryDecisions(cwd);
|
|
105
|
+
if (decisions > 0) {
|
|
106
|
+
parts.push(`Session memory: ${decisions} saved decisions (use /recall to query)`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Profile
|
|
110
|
+
const profile = getActiveProfile(cwd);
|
|
111
|
+
if (profile) {
|
|
112
|
+
parts.push(`Active profile: ${profile}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return parts.length > 0 ? `[Specialist Agent] ${parts.join(' | ')}` : '';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ── Main Execution ──────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
async function main() {
|
|
121
|
+
const cwd = process.cwd();
|
|
122
|
+
const context = buildContext(cwd);
|
|
123
|
+
|
|
124
|
+
if (context) {
|
|
125
|
+
process.stdout.write(JSON.stringify({ additionalContext: context }));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
process.exit(0);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (process.argv[1] && resolve(process.argv[1]) === resolve(__filename)) {
|
|
132
|
+
main().catch(() => {
|
|
133
|
+
// Advisory hook — never block on error
|
|
134
|
+
process.exit(0);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"hooks": {
|
|
3
|
+
"PreToolUse": [
|
|
4
|
+
{
|
|
5
|
+
"matcher": "Bash",
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "node hooks/native/security-guard.mjs",
|
|
10
|
+
"timeout": 5
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"UserPromptSubmit": [
|
|
16
|
+
{
|
|
17
|
+
"hooks": [
|
|
18
|
+
{
|
|
19
|
+
"type": "command",
|
|
20
|
+
"command": "node hooks/native/auto-dispatch.mjs",
|
|
21
|
+
"timeout": 5
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"SessionStart": [
|
|
27
|
+
{
|
|
28
|
+
"hooks": [
|
|
29
|
+
{
|
|
30
|
+
"type": "command",
|
|
31
|
+
"command": "node hooks/native/session-context.mjs",
|
|
32
|
+
"timeout": 10
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"PostToolUse": [
|
|
38
|
+
{
|
|
39
|
+
"matcher": "Write|Edit",
|
|
40
|
+
"hooks": [
|
|
41
|
+
{
|
|
42
|
+
"type": "command",
|
|
43
|
+
"command": "node hooks/native/auto-format.mjs",
|
|
44
|
+
"timeout": 15
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Native Hooks — Shared Utilities
|
|
5
|
+
* Common functions for all Claude Code native hooks.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { readFileSync } from 'fs';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Read JSON from stdin (Claude Code passes event data this way).
|
|
12
|
+
* Returns parsed object or null on failure.
|
|
13
|
+
*/
|
|
14
|
+
export async function readStdin() {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
let data = '';
|
|
17
|
+
const timeout = setTimeout(() => resolve(null), 5000);
|
|
18
|
+
|
|
19
|
+
process.stdin.setEncoding('utf-8');
|
|
20
|
+
process.stdin.on('data', (chunk) => { data += chunk; });
|
|
21
|
+
process.stdin.on('end', () => {
|
|
22
|
+
clearTimeout(timeout);
|
|
23
|
+
try {
|
|
24
|
+
resolve(JSON.parse(data));
|
|
25
|
+
} catch {
|
|
26
|
+
resolve(null);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
process.stdin.on('error', () => {
|
|
30
|
+
clearTimeout(timeout);
|
|
31
|
+
resolve(null);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
process.stdin.resume();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Output JSON to stdout for Claude Code to consume.
|
|
40
|
+
*/
|
|
41
|
+
export function outputJson(obj) {
|
|
42
|
+
process.stdout.write(JSON.stringify(obj));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Output error to stderr and exit with code 2 (block action).
|
|
47
|
+
*/
|
|
48
|
+
export function blockWithReason(reason) {
|
|
49
|
+
process.stderr.write(reason);
|
|
50
|
+
process.exit(2);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Safe JSON file reader. Returns null on any error.
|
|
55
|
+
*/
|
|
56
|
+
export function readJsonFile(filePath) {
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(readFileSync(filePath, 'utf-8'));
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* On Error Hook
|
|
5
|
+
*
|
|
6
|
+
* Runs when an agent encounters an error.
|
|
7
|
+
*
|
|
8
|
+
* Actions:
|
|
9
|
+
* - Logs error to metrics
|
|
10
|
+
* - Suggests recovery options
|
|
11
|
+
* - Optionally notifies external services
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
15
|
+
import { execSync } from 'child_process';
|
|
16
|
+
|
|
17
|
+
const SESSION_FILE = '.claude/metrics/current-session.json';
|
|
18
|
+
|
|
19
|
+
// Error data passed via environment or args
|
|
20
|
+
const errorData = {
|
|
21
|
+
agent: process.env.ERROR_AGENT || process.argv[2] || 'unknown',
|
|
22
|
+
message: process.env.ERROR_MESSAGE || process.argv[3] || 'Unknown error',
|
|
23
|
+
file: process.env.ERROR_FILE || process.argv[4] || '',
|
|
24
|
+
line: process.env.ERROR_LINE || process.argv[5] || '',
|
|
25
|
+
task: process.env.ERROR_TASK || process.argv[6] || '',
|
|
26
|
+
stack: process.env.ERROR_STACK || ''
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
async function main() {
|
|
30
|
+
console.log('\n──── Error Detected ────');
|
|
31
|
+
console.log(`Agent: ${errorData.agent}`);
|
|
32
|
+
console.log(`Message: ${errorData.message}`);
|
|
33
|
+
|
|
34
|
+
if (errorData.file) {
|
|
35
|
+
console.log(`Location: ${errorData.file}${errorData.line ? ':' + errorData.line : ''}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 1. Log to metrics
|
|
39
|
+
logErrorToMetrics(errorData);
|
|
40
|
+
|
|
41
|
+
// 2. Check for checkpoints
|
|
42
|
+
suggestRecovery();
|
|
43
|
+
|
|
44
|
+
// 3. Provide guidance
|
|
45
|
+
provideGuidance(errorData);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function logErrorToMetrics(error) {
|
|
49
|
+
if (!existsSync(SESSION_FILE)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const session = JSON.parse(readFileSync(SESSION_FILE, 'utf-8'));
|
|
55
|
+
|
|
56
|
+
session.errors.push({
|
|
57
|
+
agent: error.agent,
|
|
58
|
+
message: error.message,
|
|
59
|
+
file: error.file,
|
|
60
|
+
line: error.line,
|
|
61
|
+
task: error.task,
|
|
62
|
+
timestamp: new Date().toISOString()
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
writeFileSync(SESSION_FILE, JSON.stringify(session, null, 2));
|
|
66
|
+
console.log('\n✓ Error logged to session metrics');
|
|
67
|
+
} catch {
|
|
68
|
+
// Ignore logging errors
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function suggestRecovery() {
|
|
73
|
+
try {
|
|
74
|
+
// Check for checkpoints
|
|
75
|
+
const checkpoints = execSync('git tag -l "checkpoint/*" 2>/dev/null || true', {
|
|
76
|
+
encoding: 'utf-8'
|
|
77
|
+
}).trim().split('\n').filter(Boolean);
|
|
78
|
+
|
|
79
|
+
if (checkpoints.length > 0) {
|
|
80
|
+
console.log('\n──── Recovery Options ────');
|
|
81
|
+
console.log('Available checkpoints:');
|
|
82
|
+
checkpoints.slice(-3).forEach(cp => {
|
|
83
|
+
console.log(` • ${cp}`);
|
|
84
|
+
});
|
|
85
|
+
console.log('\nTo rollback:');
|
|
86
|
+
console.log(` git reset --hard ${checkpoints[checkpoints.length - 1]}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Check for restore points
|
|
90
|
+
const restorePoints = execSync('git tag -l "restore-point/*" 2>/dev/null || true', {
|
|
91
|
+
encoding: 'utf-8'
|
|
92
|
+
}).trim().split('\n').filter(Boolean);
|
|
93
|
+
|
|
94
|
+
if (restorePoints.length > 0) {
|
|
95
|
+
console.log('\nFull session rollback:');
|
|
96
|
+
console.log(` git reset --hard ${restorePoints[restorePoints.length - 1]}`);
|
|
97
|
+
}
|
|
98
|
+
} catch {
|
|
99
|
+
console.log('\nℹ Git checkpoints not available');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function provideGuidance(error) {
|
|
104
|
+
console.log('\n──── Suggestions ────');
|
|
105
|
+
|
|
106
|
+
// Error-specific guidance
|
|
107
|
+
if (error.message.includes('TypeScript') || error.message.includes('tsc')) {
|
|
108
|
+
console.log('• Run "npx tsc --noEmit" to see all type errors');
|
|
109
|
+
console.log('• Check imports and type definitions');
|
|
110
|
+
} else if (error.message.includes('test') || error.message.includes('jest')) {
|
|
111
|
+
console.log('• Run "npm test" to see full test output');
|
|
112
|
+
console.log('• Check test assertions and mocks');
|
|
113
|
+
} else if (error.message.includes('syntax') || error.message.includes('parse')) {
|
|
114
|
+
console.log('• Check for missing brackets or semicolons');
|
|
115
|
+
console.log('• Validate JSON/YAML files');
|
|
116
|
+
} else if (error.message.includes('module') || error.message.includes('import')) {
|
|
117
|
+
console.log('• Run "npm install" to ensure dependencies');
|
|
118
|
+
console.log('• Check import paths and exports');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// General suggestions
|
|
122
|
+
console.log('\nGeneral:');
|
|
123
|
+
console.log('• Use @debugger for systematic investigation');
|
|
124
|
+
console.log('• Use /checkpoint restore [name] to rollback');
|
|
125
|
+
console.log('• Check recent changes: git diff HEAD~1');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Run
|
|
129
|
+
main().catch(console.error);
|