claude-flow-guidance-implementation 0.1.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/.agents/README.md +38 -0
- package/.agents/config.toml +298 -0
- package/.agents/skills/memory-management/SKILL.md +126 -0
- package/.agents/skills/memory-management/scripts/memory-backup.sh +16 -0
- package/.agents/skills/memory-management/scripts/memory-consolidate.sh +11 -0
- package/.agents/skills/security-audit/SKILL.md +135 -0
- package/.agents/skills/security-audit/scripts/cve-remediate.sh +16 -0
- package/.agents/skills/security-audit/scripts/security-scan.sh +33 -0
- package/.agents/skills/sparc-methodology/SKILL.md +118 -0
- package/.agents/skills/sparc-methodology/scripts/sparc-init.sh +21 -0
- package/.agents/skills/sparc-methodology/scripts/sparc-review.sh +18 -0
- package/.agents/skills/swarm-orchestration/SKILL.md +114 -0
- package/.agents/skills/swarm-orchestration/scripts/swarm-monitor.sh +8 -0
- package/.agents/skills/swarm-orchestration/scripts/swarm-start.sh +14 -0
- package/.claude/agents/analysis/analyze-code-quality.md +179 -0
- package/.claude/agents/analysis/code-analyzer.md +210 -0
- package/.claude/agents/analysis/code-review/analyze-code-quality.md +179 -0
- package/.claude/agents/architecture/system-design/arch-system-design.md +155 -0
- package/.claude/agents/consensus/README.md +253 -0
- package/.claude/agents/consensus/byzantine-coordinator.md +63 -0
- package/.claude/agents/consensus/crdt-synchronizer.md +997 -0
- package/.claude/agents/consensus/gossip-coordinator.md +63 -0
- package/.claude/agents/consensus/performance-benchmarker.md +851 -0
- package/.claude/agents/consensus/quorum-manager.md +823 -0
- package/.claude/agents/consensus/raft-manager.md +63 -0
- package/.claude/agents/consensus/security-manager.md +622 -0
- package/.claude/agents/core/coder.md +266 -0
- package/.claude/agents/core/planner.md +168 -0
- package/.claude/agents/core/researcher.md +190 -0
- package/.claude/agents/core/reviewer.md +326 -0
- package/.claude/agents/core/tester.md +319 -0
- package/.claude/agents/custom/test-long-runner.md +44 -0
- package/.claude/agents/data/ml/data-ml-model.md +193 -0
- package/.claude/agents/development/backend/dev-backend-api.md +142 -0
- package/.claude/agents/development/dev-backend-api.md +345 -0
- package/.claude/agents/devops/ci-cd/ops-cicd-github.md +164 -0
- package/.claude/agents/documentation/api-docs/docs-api-openapi.md +174 -0
- package/.claude/agents/dual-mode/README.md +94 -0
- package/.claude/agents/dual-mode/codex-coordinator.md +224 -0
- package/.claude/agents/dual-mode/codex-worker.md +211 -0
- package/.claude/agents/dual-mode/dual-orchestrator.md +291 -0
- package/.claude/agents/flow-nexus/app-store.md +88 -0
- package/.claude/agents/flow-nexus/authentication.md +69 -0
- package/.claude/agents/flow-nexus/challenges.md +81 -0
- package/.claude/agents/flow-nexus/neural-network.md +88 -0
- package/.claude/agents/flow-nexus/payments.md +83 -0
- package/.claude/agents/flow-nexus/sandbox.md +76 -0
- package/.claude/agents/flow-nexus/swarm.md +76 -0
- package/.claude/agents/flow-nexus/user-tools.md +96 -0
- package/.claude/agents/flow-nexus/workflow.md +84 -0
- package/.claude/agents/github/code-review-swarm.md +538 -0
- package/.claude/agents/github/github-modes.md +173 -0
- package/.claude/agents/github/issue-tracker.md +319 -0
- package/.claude/agents/github/multi-repo-swarm.md +553 -0
- package/.claude/agents/github/pr-manager.md +191 -0
- package/.claude/agents/github/project-board-sync.md +509 -0
- package/.claude/agents/github/release-manager.md +367 -0
- package/.claude/agents/github/release-swarm.md +583 -0
- package/.claude/agents/github/repo-architect.md +398 -0
- package/.claude/agents/github/swarm-issue.md +573 -0
- package/.claude/agents/github/swarm-pr.md +428 -0
- package/.claude/agents/github/sync-coordinator.md +452 -0
- package/.claude/agents/github/workflow-automation.md +635 -0
- package/.claude/agents/goal/agent.md +816 -0
- package/.claude/agents/goal/code-goal-planner.md +446 -0
- package/.claude/agents/goal/goal-planner.md +168 -0
- package/.claude/agents/hive-mind/collective-intelligence-coordinator.md +130 -0
- package/.claude/agents/hive-mind/queen-coordinator.md +203 -0
- package/.claude/agents/hive-mind/scout-explorer.md +242 -0
- package/.claude/agents/hive-mind/swarm-memory-manager.md +193 -0
- package/.claude/agents/hive-mind/worker-specialist.md +217 -0
- package/.claude/agents/optimization/README.md +250 -0
- package/.claude/agents/optimization/benchmark-suite.md +665 -0
- package/.claude/agents/optimization/load-balancer.md +431 -0
- package/.claude/agents/optimization/performance-monitor.md +672 -0
- package/.claude/agents/optimization/resource-allocator.md +674 -0
- package/.claude/agents/optimization/topology-optimizer.md +808 -0
- package/.claude/agents/payments/agentic-payments.md +126 -0
- package/.claude/agents/sona/sona-learning-optimizer.md +74 -0
- package/.claude/agents/sparc/architecture.md +472 -0
- package/.claude/agents/sparc/pseudocode.md +318 -0
- package/.claude/agents/sparc/refinement.md +525 -0
- package/.claude/agents/sparc/specification.md +276 -0
- package/.claude/agents/specialized/mobile/spec-mobile-react-native.md +225 -0
- package/.claude/agents/sublinear/consensus-coordinator.md +338 -0
- package/.claude/agents/sublinear/matrix-optimizer.md +185 -0
- package/.claude/agents/sublinear/pagerank-analyzer.md +299 -0
- package/.claude/agents/sublinear/performance-optimizer.md +368 -0
- package/.claude/agents/sublinear/trading-predictor.md +246 -0
- package/.claude/agents/swarm/README.md +190 -0
- package/.claude/agents/swarm/adaptive-coordinator.md +396 -0
- package/.claude/agents/swarm/hierarchical-coordinator.md +327 -0
- package/.claude/agents/swarm/mesh-coordinator.md +392 -0
- package/.claude/agents/templates/automation-smart-agent.md +205 -0
- package/.claude/agents/templates/coordinator-swarm-init.md +105 -0
- package/.claude/agents/templates/github-pr-manager.md +177 -0
- package/.claude/agents/templates/implementer-sparc-coder.md +259 -0
- package/.claude/agents/templates/memory-coordinator.md +187 -0
- package/.claude/agents/templates/migration-plan.md +746 -0
- package/.claude/agents/templates/orchestrator-task.md +139 -0
- package/.claude/agents/templates/performance-analyzer.md +199 -0
- package/.claude/agents/templates/sparc-coordinator.md +183 -0
- package/.claude/agents/testing/production-validator.md +395 -0
- package/.claude/agents/testing/tdd-london-swarm.md +244 -0
- package/.claude/agents/testing/unit/tdd-london-swarm.md +244 -0
- package/.claude/agents/testing/validation/production-validator.md +395 -0
- package/.claude/agents/v3/database-specialist.yaml +21 -0
- package/.claude/agents/v3/index.yaml +17 -0
- package/.claude/agents/v3/project-coordinator.yaml +15 -0
- package/.claude/agents/v3/python-specialist.yaml +21 -0
- package/.claude/agents/v3/test-architect.yaml +20 -0
- package/.claude/agents/v3/typescript-specialist.yaml +21 -0
- package/.claude/agents/v3/v3-integration-architect.md +346 -0
- package/.claude/agents/v3/v3-memory-specialist.md +318 -0
- package/.claude/agents/v3/v3-performance-engineer.md +397 -0
- package/.claude/agents/v3/v3-queen-coordinator.md +98 -0
- package/.claude/agents/v3/v3-security-architect.md +174 -0
- package/.claude/commands/analysis/COMMAND_COMPLIANCE_REPORT.md +54 -0
- package/.claude/commands/analysis/README.md +9 -0
- package/.claude/commands/analysis/bottleneck-detect.md +162 -0
- package/.claude/commands/analysis/performance-bottlenecks.md +59 -0
- package/.claude/commands/analysis/performance-report.md +25 -0
- package/.claude/commands/analysis/token-efficiency.md +45 -0
- package/.claude/commands/analysis/token-usage.md +25 -0
- package/.claude/commands/automation/README.md +9 -0
- package/.claude/commands/automation/auto-agent.md +122 -0
- package/.claude/commands/automation/self-healing.md +106 -0
- package/.claude/commands/automation/session-memory.md +90 -0
- package/.claude/commands/automation/smart-agents.md +73 -0
- package/.claude/commands/automation/smart-spawn.md +25 -0
- package/.claude/commands/automation/workflow-select.md +25 -0
- package/.claude/commands/claude-flow-help.md +103 -0
- package/.claude/commands/claude-flow-memory.md +107 -0
- package/.claude/commands/claude-flow-swarm.md +205 -0
- package/.claude/commands/github/README.md +11 -0
- package/.claude/commands/github/code-review-swarm.md +514 -0
- package/.claude/commands/github/code-review.md +25 -0
- package/.claude/commands/github/github-modes.md +147 -0
- package/.claude/commands/github/github-swarm.md +121 -0
- package/.claude/commands/github/issue-tracker.md +292 -0
- package/.claude/commands/github/issue-triage.md +25 -0
- package/.claude/commands/github/multi-repo-swarm.md +519 -0
- package/.claude/commands/github/pr-enhance.md +26 -0
- package/.claude/commands/github/pr-manager.md +170 -0
- package/.claude/commands/github/project-board-sync.md +471 -0
- package/.claude/commands/github/release-manager.md +338 -0
- package/.claude/commands/github/release-swarm.md +544 -0
- package/.claude/commands/github/repo-analyze.md +25 -0
- package/.claude/commands/github/repo-architect.md +367 -0
- package/.claude/commands/github/swarm-issue.md +482 -0
- package/.claude/commands/github/swarm-pr.md +285 -0
- package/.claude/commands/github/sync-coordinator.md +301 -0
- package/.claude/commands/github/workflow-automation.md +442 -0
- package/.claude/commands/hooks/README.md +11 -0
- package/.claude/commands/hooks/overview.md +58 -0
- package/.claude/commands/hooks/post-edit.md +117 -0
- package/.claude/commands/hooks/post-task.md +112 -0
- package/.claude/commands/hooks/pre-edit.md +113 -0
- package/.claude/commands/hooks/pre-task.md +111 -0
- package/.claude/commands/hooks/session-end.md +118 -0
- package/.claude/commands/hooks/setup.md +103 -0
- package/.claude/commands/monitoring/README.md +9 -0
- package/.claude/commands/monitoring/agent-metrics.md +25 -0
- package/.claude/commands/monitoring/agents.md +44 -0
- package/.claude/commands/monitoring/real-time-view.md +25 -0
- package/.claude/commands/monitoring/status.md +46 -0
- package/.claude/commands/monitoring/swarm-monitor.md +25 -0
- package/.claude/commands/optimization/README.md +9 -0
- package/.claude/commands/optimization/auto-topology.md +62 -0
- package/.claude/commands/optimization/cache-manage.md +25 -0
- package/.claude/commands/optimization/parallel-execute.md +25 -0
- package/.claude/commands/optimization/parallel-execution.md +50 -0
- package/.claude/commands/optimization/topology-optimize.md +25 -0
- package/.claude/commands/sparc/analyzer.md +52 -0
- package/.claude/commands/sparc/architect.md +53 -0
- package/.claude/commands/sparc/ask.md +97 -0
- package/.claude/commands/sparc/batch-executor.md +54 -0
- package/.claude/commands/sparc/code.md +89 -0
- package/.claude/commands/sparc/coder.md +54 -0
- package/.claude/commands/sparc/debug.md +83 -0
- package/.claude/commands/sparc/debugger.md +54 -0
- package/.claude/commands/sparc/designer.md +53 -0
- package/.claude/commands/sparc/devops.md +109 -0
- package/.claude/commands/sparc/docs-writer.md +80 -0
- package/.claude/commands/sparc/documenter.md +54 -0
- package/.claude/commands/sparc/innovator.md +54 -0
- package/.claude/commands/sparc/integration.md +83 -0
- package/.claude/commands/sparc/mcp.md +117 -0
- package/.claude/commands/sparc/memory-manager.md +54 -0
- package/.claude/commands/sparc/optimizer.md +54 -0
- package/.claude/commands/sparc/orchestrator.md +132 -0
- package/.claude/commands/sparc/post-deployment-monitoring-mode.md +83 -0
- package/.claude/commands/sparc/refinement-optimization-mode.md +83 -0
- package/.claude/commands/sparc/researcher.md +54 -0
- package/.claude/commands/sparc/reviewer.md +54 -0
- package/.claude/commands/sparc/security-review.md +80 -0
- package/.claude/commands/sparc/sparc-modes.md +174 -0
- package/.claude/commands/sparc/sparc.md +111 -0
- package/.claude/commands/sparc/spec-pseudocode.md +80 -0
- package/.claude/commands/sparc/supabase-admin.md +348 -0
- package/.claude/commands/sparc/swarm-coordinator.md +54 -0
- package/.claude/commands/sparc/tdd.md +54 -0
- package/.claude/commands/sparc/tester.md +54 -0
- package/.claude/commands/sparc/tutorial.md +79 -0
- package/.claude/commands/sparc/workflow-manager.md +54 -0
- package/.claude/helpers/hook-handler.cjs +168 -0
- package/.claude/helpers/intelligence.cjs +197 -0
- package/.claude/helpers/memory.js +83 -0
- package/.claude/helpers/post-commit +16 -0
- package/.claude/helpers/pre-commit +26 -0
- package/.claude/helpers/router.js +66 -0
- package/.claude/helpers/session.js +127 -0
- package/.claude/settings.json +250 -0
- package/.claude/skills/agentdb-advanced/SKILL.md +550 -0
- package/.claude/skills/agentdb-learning/SKILL.md +545 -0
- package/.claude/skills/agentdb-memory-patterns/SKILL.md +339 -0
- package/.claude/skills/agentdb-optimization/SKILL.md +509 -0
- package/.claude/skills/agentdb-vector-search/SKILL.md +339 -0
- package/.claude/skills/github-code-review/SKILL.md +1140 -0
- package/.claude/skills/github-multi-repo/SKILL.md +874 -0
- package/.claude/skills/github-project-management/SKILL.md +1277 -0
- package/.claude/skills/github-release-management/SKILL.md +1081 -0
- package/.claude/skills/github-workflow-automation/SKILL.md +1065 -0
- package/.claude/skills/hooks-automation/SKILL.md +1201 -0
- package/.claude/skills/pair-programming/SKILL.md +1202 -0
- package/.claude/skills/reasoningbank-agentdb/SKILL.md +446 -0
- package/.claude/skills/reasoningbank-intelligence/SKILL.md +201 -0
- package/.claude/skills/skill-builder/.claude-flow/metrics/agent-metrics.json +1 -0
- package/.claude/skills/skill-builder/.claude-flow/metrics/performance.json +87 -0
- package/.claude/skills/skill-builder/.claude-flow/metrics/task-metrics.json +10 -0
- package/.claude/skills/skill-builder/SKILL.md +910 -0
- package/.claude/skills/sparc-methodology/SKILL.md +1115 -0
- package/.claude/skills/stream-chain/SKILL.md +563 -0
- package/.claude/skills/swarm-advanced/SKILL.md +973 -0
- package/.claude/skills/swarm-orchestration/SKILL.md +179 -0
- package/.claude/skills/v3-cli-modernization/SKILL.md +872 -0
- package/.claude/skills/v3-core-implementation/SKILL.md +797 -0
- package/.claude/skills/v3-ddd-architecture/SKILL.md +442 -0
- package/.claude/skills/v3-integration-deep/SKILL.md +241 -0
- package/.claude/skills/v3-mcp-optimization/SKILL.md +777 -0
- package/.claude/skills/v3-memory-unification/SKILL.md +174 -0
- package/.claude/skills/v3-performance-optimization/SKILL.md +390 -0
- package/.claude/skills/v3-security-overhaul/SKILL.md +82 -0
- package/.claude/skills/v3-swarm-coordination/SKILL.md +340 -0
- package/.claude/skills/verification-quality/SKILL.md +649 -0
- package/.claude/statusline.mjs +109 -0
- package/.claude/statusline.sh +375 -0
- package/.claude-flow/CAPABILITIES.md +403 -0
- package/.claude-flow/config.yaml +43 -0
- package/.claude-flow/metrics/learning.json +17 -0
- package/.claude-flow/metrics/swarm-activity.json +18 -0
- package/.claude-flow/metrics/v3-progress.json +26 -0
- package/.claude-flow/security/audit-status.json +8 -0
- package/.mcp.json +20 -0
- package/AGENTS.md +145 -0
- package/CLAUDE.md +188 -0
- package/LICENSE +21 -0
- package/README.md +115 -0
- package/bin/cf-guidance-impl.mjs +74 -0
- package/package.json +23 -0
- package/scaffold/.claude/helpers/hook-handler.cjs +576 -0
- package/scaffold/docs/guidance-control-plane.md +441 -0
- package/scaffold/docs/guidance-implementation-guide.md +424 -0
- package/scaffold/scripts/analyze-guidance.js +176 -0
- package/scaffold/scripts/guidance-ab-benchmark.js +40 -0
- package/scaffold/scripts/guidance-autopilot.js +473 -0
- package/scaffold/scripts/guidance-codex-bridge.js +337 -0
- package/scaffold/scripts/guidance-integrations.js +587 -0
- package/scaffold/scripts/guidance-runtime.js +133 -0
- package/scaffold/scripts/scaffold-guidance.js +122 -0
- package/scaffold/src/guidance/advanced-runtime.js +658 -0
- package/scaffold/src/guidance/content-aware-executor.js +70 -0
- package/scaffold/src/guidance/phase1-runtime.js +151 -0
- package/src/default-settings.mjs +111 -0
- package/src/installer.mjs +415 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Claude Flow Hook Handler (Cross-Platform)
|
|
4
|
+
* Dispatches hook events to the appropriate helper modules.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const { createHash } = require('crypto');
|
|
10
|
+
const { spawn, spawnSync } = require('child_process');
|
|
11
|
+
|
|
12
|
+
const helpersDir = __dirname;
|
|
13
|
+
|
|
14
|
+
function safeRequire(modulePath) {
|
|
15
|
+
try {
|
|
16
|
+
if (fs.existsSync(modulePath)) {
|
|
17
|
+
const origLog = console.log;
|
|
18
|
+
const origError = console.error;
|
|
19
|
+
console.log = () => {};
|
|
20
|
+
console.error = () => {};
|
|
21
|
+
try {
|
|
22
|
+
const mod = require(modulePath);
|
|
23
|
+
return mod;
|
|
24
|
+
} finally {
|
|
25
|
+
console.log = origLog;
|
|
26
|
+
console.error = origError;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
} catch (e) {
|
|
30
|
+
// silently fail
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const router = safeRequire(path.join(helpersDir, 'router.cjs'));
|
|
36
|
+
const session = safeRequire(path.join(helpersDir, 'session.cjs'));
|
|
37
|
+
const memory = safeRequire(path.join(helpersDir, 'memory.cjs'));
|
|
38
|
+
const intelligence = safeRequire(path.join(helpersDir, 'intelligence.cjs'));
|
|
39
|
+
|
|
40
|
+
const [,, command, ...args] = process.argv;
|
|
41
|
+
|
|
42
|
+
// Read stdin JSON from Claude Code hooks (provides tool_input, tool_name, etc.)
|
|
43
|
+
let stdinData = {};
|
|
44
|
+
try {
|
|
45
|
+
const raw = fs.readFileSync(0, 'utf-8').trim();
|
|
46
|
+
if (raw) stdinData = JSON.parse(raw);
|
|
47
|
+
} catch (e) { /* stdin may be empty or non-JSON */ }
|
|
48
|
+
|
|
49
|
+
const prompt = process.env.PROMPT || (stdinData.tool_input && stdinData.tool_input.command) || args.join(' ') || '';
|
|
50
|
+
|
|
51
|
+
function launchGuidanceAutopilot(source) {
|
|
52
|
+
if (process.env.GUIDANCE_AUTOPILOT_ENABLED === '0') return;
|
|
53
|
+
const projectDir = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
54
|
+
const scriptPath = path.join(projectDir, 'scripts', 'guidance-autopilot.js');
|
|
55
|
+
if (!fs.existsSync(scriptPath)) return;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const autopilotArgs = [
|
|
59
|
+
scriptPath,
|
|
60
|
+
'--once',
|
|
61
|
+
'--apply',
|
|
62
|
+
'--source',
|
|
63
|
+
source || 'hook',
|
|
64
|
+
'--min-delta',
|
|
65
|
+
process.env.GUIDANCE_AUTOPILOT_MIN_DELTA || '0.5',
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
if (process.env.GUIDANCE_AUTOPILOT_AB === '1') {
|
|
69
|
+
autopilotArgs.push('--ab');
|
|
70
|
+
autopilotArgs.push('--min-ab-gain');
|
|
71
|
+
autopilotArgs.push(process.env.GUIDANCE_AUTOPILOT_MIN_AB_GAIN || '0.05');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const child = spawn(process.execPath, autopilotArgs, {
|
|
75
|
+
cwd: projectDir,
|
|
76
|
+
env: process.env,
|
|
77
|
+
detached: true,
|
|
78
|
+
stdio: 'ignore',
|
|
79
|
+
});
|
|
80
|
+
child.unref();
|
|
81
|
+
} catch (e) {
|
|
82
|
+
// non-fatal
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function guidanceWiringEnabled() {
|
|
87
|
+
return process.env.GUIDANCE_EVENT_WIRING_ENABLED !== '0';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getProjectDir() {
|
|
91
|
+
return process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function getGuidanceScriptPath() {
|
|
95
|
+
return path.join(getProjectDir(), 'scripts', 'guidance-integrations.js');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function safeString(value, fallback) {
|
|
99
|
+
if (value == null) return fallback || '';
|
|
100
|
+
return String(value);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function toPositiveInteger(value, fallback) {
|
|
104
|
+
const parsed = Number(value);
|
|
105
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
|
|
106
|
+
return Math.round(parsed);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function parseJsonOutput(stdout) {
|
|
110
|
+
const text = safeString(stdout, '').trim();
|
|
111
|
+
if (!text) return null;
|
|
112
|
+
try {
|
|
113
|
+
return JSON.parse(text);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
const start = text.lastIndexOf('{');
|
|
116
|
+
if (start >= 0) {
|
|
117
|
+
const candidate = text.slice(start);
|
|
118
|
+
try {
|
|
119
|
+
return JSON.parse(candidate);
|
|
120
|
+
} catch (nestedError) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function stableId(prefix, seed) {
|
|
129
|
+
const input = safeString(seed, '').trim() || `${prefix}-${Date.now()}`;
|
|
130
|
+
const digest = createHash('sha1').update(input).digest('hex').slice(0, 12);
|
|
131
|
+
return `${prefix}-${digest}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getToolInput() {
|
|
135
|
+
return (stdinData && typeof stdinData.tool_input === 'object' && stdinData.tool_input) || {};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function getTaskDescription() {
|
|
139
|
+
const toolInput = getToolInput();
|
|
140
|
+
const candidate = toolInput.description || toolInput.prompt || toolInput.task || prompt;
|
|
141
|
+
return safeString(candidate, '').trim();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function getExplicitTaskId() {
|
|
145
|
+
const toolInput = getToolInput();
|
|
146
|
+
return safeString(stdinData.task_id || toolInput.task_id || toolInput.id || stdinData.id, '').trim();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getTaskCachePath() {
|
|
150
|
+
return path.join(getProjectDir(), '.claude-flow', 'guidance', 'hook-task-cache.json');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function readTaskCache() {
|
|
154
|
+
const cachePath = getTaskCachePath();
|
|
155
|
+
if (!fs.existsSync(cachePath)) return {};
|
|
156
|
+
try {
|
|
157
|
+
return JSON.parse(fs.readFileSync(cachePath, 'utf-8'));
|
|
158
|
+
} catch (error) {
|
|
159
|
+
return {};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function writeTaskCache(cache) {
|
|
164
|
+
const cachePath = getTaskCachePath();
|
|
165
|
+
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
166
|
+
fs.writeFileSync(cachePath, JSON.stringify(cache, null, 2));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function rememberTaskContext(taskId, taskDescription) {
|
|
170
|
+
const cache = readTaskCache();
|
|
171
|
+
cache.last = {
|
|
172
|
+
taskId: safeString(taskId, ''),
|
|
173
|
+
taskDescription: safeString(taskDescription, ''),
|
|
174
|
+
updatedAt: Date.now(),
|
|
175
|
+
};
|
|
176
|
+
writeTaskCache(cache);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function getRememberedTaskContext() {
|
|
180
|
+
const cache = readTaskCache();
|
|
181
|
+
const last = cache.last;
|
|
182
|
+
if (!last || typeof last !== 'object') return null;
|
|
183
|
+
const taskId = safeString(last.taskId, '').trim();
|
|
184
|
+
const taskDescription = safeString(last.taskDescription, '').trim();
|
|
185
|
+
if (!taskId && !taskDescription) return null;
|
|
186
|
+
return { taskId, taskDescription };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function getTaskId(prefix) {
|
|
190
|
+
const explicitId = getExplicitTaskId();
|
|
191
|
+
if (explicitId) return safeString(explicitId, `${prefix}-${Date.now()}`);
|
|
192
|
+
const remembered = getRememberedTaskContext();
|
|
193
|
+
if (prefix === 'post-task' && remembered && remembered.taskId) {
|
|
194
|
+
return remembered.taskId;
|
|
195
|
+
}
|
|
196
|
+
const toolInput = getToolInput();
|
|
197
|
+
return stableId(prefix, getTaskDescription() || safeString(toolInput.command, '') || prefix);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function getSessionId() {
|
|
201
|
+
return safeString(
|
|
202
|
+
stdinData.session_id || process.env.CLAUDE_SESSION_ID,
|
|
203
|
+
`session-${Date.now()}`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function getAgentId() {
|
|
208
|
+
return safeString(process.env.CLAUDE_AGENT_ID || stdinData.agent_id, 'claude-main');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function getFilePath() {
|
|
212
|
+
const toolInput = getToolInput();
|
|
213
|
+
return safeString(toolInput.file_path || toolInput.path || args[0], '').trim();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function getEditContent() {
|
|
217
|
+
const toolInput = getToolInput();
|
|
218
|
+
return safeString(toolInput.content || toolInput.new_string || toolInput.new_content || '', '');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function getDiffLines() {
|
|
222
|
+
const toolInput = getToolInput();
|
|
223
|
+
if (toolInput.diff_lines != null) return toPositiveInteger(toolInput.diff_lines, 0);
|
|
224
|
+
if (toolInput.diffLines != null) return toPositiveInteger(toolInput.diffLines, 0);
|
|
225
|
+
const content = getEditContent();
|
|
226
|
+
if (!content) return 0;
|
|
227
|
+
return content.split('\n').length;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function buildGuidancePayload(overrides) {
|
|
231
|
+
return Object.assign(
|
|
232
|
+
{
|
|
233
|
+
taskId: getTaskId('hook-task'),
|
|
234
|
+
sessionId: getSessionId(),
|
|
235
|
+
agentId: getAgentId(),
|
|
236
|
+
},
|
|
237
|
+
overrides || {}
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function runGuidanceEventSync(eventName, payload) {
|
|
242
|
+
if (!guidanceWiringEnabled()) return null;
|
|
243
|
+
const scriptPath = getGuidanceScriptPath();
|
|
244
|
+
if (!fs.existsSync(scriptPath)) return null;
|
|
245
|
+
|
|
246
|
+
const timeout = toPositiveInteger(process.env.GUIDANCE_EVENT_SYNC_TIMEOUT_MS, 8000);
|
|
247
|
+
const result = spawnSync(
|
|
248
|
+
process.execPath,
|
|
249
|
+
[scriptPath, 'event', eventName, JSON.stringify(payload || {})],
|
|
250
|
+
{
|
|
251
|
+
cwd: getProjectDir(),
|
|
252
|
+
env: process.env,
|
|
253
|
+
encoding: 'utf-8',
|
|
254
|
+
timeout,
|
|
255
|
+
}
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
if (result.error) {
|
|
259
|
+
return {
|
|
260
|
+
event: eventName,
|
|
261
|
+
success: true,
|
|
262
|
+
blocked: false,
|
|
263
|
+
skipped: true,
|
|
264
|
+
error: result.error.message,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (result.status !== 0) {
|
|
269
|
+
const failClosed = process.env.GUIDANCE_EVENT_FAIL_CLOSED === '1';
|
|
270
|
+
return {
|
|
271
|
+
event: eventName,
|
|
272
|
+
success: !failClosed,
|
|
273
|
+
blocked: failClosed,
|
|
274
|
+
skipped: false,
|
|
275
|
+
error: safeString(result.stderr, '').trim() || safeString(result.stdout, '').trim(),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const parsed = parseJsonOutput(result.stdout);
|
|
280
|
+
if (parsed) return parsed;
|
|
281
|
+
|
|
282
|
+
return {
|
|
283
|
+
event: eventName,
|
|
284
|
+
success: true,
|
|
285
|
+
blocked: false,
|
|
286
|
+
skipped: true,
|
|
287
|
+
error: 'Unable to parse guidance event output',
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function launchGuidanceEventAsync(eventName, payload) {
|
|
292
|
+
if (!guidanceWiringEnabled()) return;
|
|
293
|
+
const scriptPath = getGuidanceScriptPath();
|
|
294
|
+
if (!fs.existsSync(scriptPath)) return;
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
const child = spawn(
|
|
298
|
+
process.execPath,
|
|
299
|
+
[scriptPath, 'event', eventName, JSON.stringify(payload || {})],
|
|
300
|
+
{
|
|
301
|
+
cwd: getProjectDir(),
|
|
302
|
+
env: process.env,
|
|
303
|
+
detached: true,
|
|
304
|
+
stdio: 'ignore',
|
|
305
|
+
}
|
|
306
|
+
);
|
|
307
|
+
child.unref();
|
|
308
|
+
} catch (error) {
|
|
309
|
+
// non-fatal
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function guidanceBlockMessage(result, fallback) {
|
|
314
|
+
if (!result) return fallback;
|
|
315
|
+
const messages = Array.isArray(result.messages) ? result.messages : [];
|
|
316
|
+
const warnings = Array.isArray(result.warnings) ? result.warnings : [];
|
|
317
|
+
const fragments = [];
|
|
318
|
+
if (result.blockedByThreat) fragments.push('Adversarial threat detected in command input');
|
|
319
|
+
if (messages.length > 0) fragments.push(messages.join(' | '));
|
|
320
|
+
if (warnings.length > 0) fragments.push(warnings.join(' | '));
|
|
321
|
+
if (result.error) fragments.push(String(result.error));
|
|
322
|
+
if (fragments.length > 0) return fragments.join(' | ');
|
|
323
|
+
return fallback;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const handlers = {
|
|
327
|
+
'route': () => {
|
|
328
|
+
if (intelligence && intelligence.getContext) {
|
|
329
|
+
try {
|
|
330
|
+
const ctx = intelligence.getContext(prompt);
|
|
331
|
+
if (ctx) console.log(ctx);
|
|
332
|
+
} catch (e) { /* non-fatal */ }
|
|
333
|
+
}
|
|
334
|
+
if (router && router.routeTask) {
|
|
335
|
+
const result = router.routeTask(prompt);
|
|
336
|
+
var output = [];
|
|
337
|
+
output.push('[INFO] Routing task: ' + (prompt.substring(0, 80) || '(no prompt)'));
|
|
338
|
+
output.push('');
|
|
339
|
+
output.push('+------------------- Primary Recommendation -------------------+');
|
|
340
|
+
output.push('| Agent: ' + result.agent.padEnd(53) + '|');
|
|
341
|
+
output.push('| Confidence: ' + (result.confidence * 100).toFixed(1) + '%' + ' '.repeat(44) + '|');
|
|
342
|
+
output.push('| Reason: ' + result.reason.substring(0, 53).padEnd(53) + '|');
|
|
343
|
+
output.push('+--------------------------------------------------------------+');
|
|
344
|
+
console.log(output.join('\n'));
|
|
345
|
+
} else {
|
|
346
|
+
console.log('[INFO] Router not available, using default routing');
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
'pre-bash': () => {
|
|
351
|
+
const commandText = safeString(getToolInput().command || prompt, '').trim();
|
|
352
|
+
const guidance = runGuidanceEventSync(
|
|
353
|
+
'pre-command',
|
|
354
|
+
buildGuidancePayload({
|
|
355
|
+
taskId: getTaskId('pre-command'),
|
|
356
|
+
command: commandText,
|
|
357
|
+
})
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
if (guidance && guidance.blocked) {
|
|
361
|
+
console.error('[BLOCKED] ' + guidanceBlockMessage(guidance, 'Command blocked by guidance'));
|
|
362
|
+
process.exit(1);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
var cmd = commandText.toLowerCase();
|
|
366
|
+
var dangerous = ['rm -rf /', 'format c:', 'del /s /q c:\\', ':(){:|:&};:'];
|
|
367
|
+
for (var i = 0; i < dangerous.length; i++) {
|
|
368
|
+
if (cmd.includes(dangerous[i])) {
|
|
369
|
+
console.error('[BLOCKED] Dangerous command detected: ' + dangerous[i]);
|
|
370
|
+
process.exit(1);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
console.log('[OK] Command validated');
|
|
374
|
+
},
|
|
375
|
+
|
|
376
|
+
'pre-edit': () => {
|
|
377
|
+
const filePath = getFilePath();
|
|
378
|
+
if (!filePath) {
|
|
379
|
+
console.log('[OK] Edit validation skipped (missing file path)');
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const guidance = runGuidanceEventSync(
|
|
384
|
+
'pre-edit',
|
|
385
|
+
buildGuidancePayload({
|
|
386
|
+
taskId: getTaskId('pre-edit'),
|
|
387
|
+
filePath,
|
|
388
|
+
content: getEditContent(),
|
|
389
|
+
diffLines: getDiffLines(),
|
|
390
|
+
operation: safeString(getToolInput().operation, 'modify'),
|
|
391
|
+
})
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
if (guidance && guidance.blocked) {
|
|
395
|
+
console.error('[BLOCKED] ' + guidanceBlockMessage(guidance, 'Edit blocked by guidance'));
|
|
396
|
+
process.exit(1);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
console.log('[OK] Edit validated');
|
|
400
|
+
},
|
|
401
|
+
|
|
402
|
+
'post-edit': () => {
|
|
403
|
+
if (session && session.metric) {
|
|
404
|
+
try { session.metric('edits'); } catch (e) { /* no active session */ }
|
|
405
|
+
}
|
|
406
|
+
if (intelligence && intelligence.recordEdit) {
|
|
407
|
+
try {
|
|
408
|
+
var file = (stdinData.tool_input && stdinData.tool_input.file_path) || args[0] || '';
|
|
409
|
+
intelligence.recordEdit(file);
|
|
410
|
+
} catch (e) { /* non-fatal */ }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
launchGuidanceEventAsync(
|
|
414
|
+
'post-edit',
|
|
415
|
+
buildGuidancePayload({
|
|
416
|
+
taskId: getTaskId('post-edit'),
|
|
417
|
+
filePath: getFilePath(),
|
|
418
|
+
})
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
console.log('[OK] Edit recorded');
|
|
422
|
+
},
|
|
423
|
+
|
|
424
|
+
'session-restore': () => {
|
|
425
|
+
if (session) {
|
|
426
|
+
var existing = session.restore && session.restore();
|
|
427
|
+
if (!existing) {
|
|
428
|
+
session.start && session.start();
|
|
429
|
+
}
|
|
430
|
+
} else {
|
|
431
|
+
console.log('[OK] Session restored: session-' + Date.now());
|
|
432
|
+
}
|
|
433
|
+
if (intelligence && intelligence.init) {
|
|
434
|
+
try {
|
|
435
|
+
var result = intelligence.init();
|
|
436
|
+
if (result && result.nodes > 0) {
|
|
437
|
+
console.log('[INTELLIGENCE] Loaded ' + result.nodes + ' patterns, ' + result.edges + ' edges');
|
|
438
|
+
}
|
|
439
|
+
} catch (e) { /* non-fatal */ }
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
|
|
443
|
+
'session-end': () => {
|
|
444
|
+
if (intelligence && intelligence.consolidate) {
|
|
445
|
+
try {
|
|
446
|
+
var result = intelligence.consolidate();
|
|
447
|
+
if (result && result.entries > 0) {
|
|
448
|
+
var msg = '[INTELLIGENCE] Consolidated: ' + result.entries + ' entries, ' + result.edges + ' edges';
|
|
449
|
+
if (result.newEntries > 0) msg += ', ' + result.newEntries + ' new';
|
|
450
|
+
msg += ', PageRank recomputed';
|
|
451
|
+
console.log(msg);
|
|
452
|
+
}
|
|
453
|
+
} catch (e) { /* non-fatal */ }
|
|
454
|
+
}
|
|
455
|
+
if (session && session.end) {
|
|
456
|
+
session.end();
|
|
457
|
+
} else {
|
|
458
|
+
console.log('[OK] Session ended');
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
launchGuidanceEventAsync(
|
|
462
|
+
'session-end',
|
|
463
|
+
buildGuidancePayload({
|
|
464
|
+
taskId: getTaskId('session-end'),
|
|
465
|
+
})
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
// Non-blocking background guidance optimization run.
|
|
469
|
+
launchGuidanceAutopilot('session-end');
|
|
470
|
+
},
|
|
471
|
+
|
|
472
|
+
'pre-task': () => {
|
|
473
|
+
const taskDescription = getTaskDescription();
|
|
474
|
+
const taskId = getTaskId('pre-task');
|
|
475
|
+
rememberTaskContext(taskId, taskDescription);
|
|
476
|
+
const guidance = runGuidanceEventSync(
|
|
477
|
+
'pre-task',
|
|
478
|
+
buildGuidancePayload({
|
|
479
|
+
taskId,
|
|
480
|
+
taskDescription,
|
|
481
|
+
})
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
if (guidance && guidance.blocked) {
|
|
485
|
+
console.error('[BLOCKED] ' + guidanceBlockMessage(guidance, 'Task blocked by guidance'));
|
|
486
|
+
process.exit(1);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
if (session && session.metric) {
|
|
490
|
+
try { session.metric('tasks'); } catch (e) { /* no active session */ }
|
|
491
|
+
}
|
|
492
|
+
const routePrompt = taskDescription || prompt;
|
|
493
|
+
if (router && router.routeTask && routePrompt) {
|
|
494
|
+
var result = router.routeTask(routePrompt);
|
|
495
|
+
console.log('[INFO] Task routed to: ' + result.agent + ' (confidence: ' + result.confidence + ')');
|
|
496
|
+
} else {
|
|
497
|
+
console.log('[OK] Task started');
|
|
498
|
+
}
|
|
499
|
+
},
|
|
500
|
+
|
|
501
|
+
'post-task': () => {
|
|
502
|
+
const remembered = getRememberedTaskContext();
|
|
503
|
+
const taskId = getExplicitTaskId() || (remembered && remembered.taskId) || getTaskId('post-task');
|
|
504
|
+
const taskDescription = getTaskDescription() || (remembered && remembered.taskDescription) || '';
|
|
505
|
+
|
|
506
|
+
if (intelligence && intelligence.feedback) {
|
|
507
|
+
try {
|
|
508
|
+
intelligence.feedback(true);
|
|
509
|
+
} catch (e) { /* non-fatal */ }
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
launchGuidanceEventAsync(
|
|
513
|
+
'post-task',
|
|
514
|
+
buildGuidancePayload({
|
|
515
|
+
taskId,
|
|
516
|
+
taskDescription,
|
|
517
|
+
status: safeString(getToolInput().status, 'completed'),
|
|
518
|
+
toolsUsed: [],
|
|
519
|
+
filesTouched: [],
|
|
520
|
+
})
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
console.log('[OK] Task completed');
|
|
524
|
+
},
|
|
525
|
+
|
|
526
|
+
'compact-manual': () => {
|
|
527
|
+
console.log('PreCompact Guidance:');
|
|
528
|
+
console.log('IMPORTANT: Review CLAUDE.md in project root for:');
|
|
529
|
+
console.log(' - Available agents and concurrent usage patterns');
|
|
530
|
+
console.log(' - Swarm coordination strategies (hierarchical, mesh, adaptive)');
|
|
531
|
+
console.log(' - Critical concurrent execution rules (1 MESSAGE = ALL OPERATIONS)');
|
|
532
|
+
console.log('Ready for compact operation');
|
|
533
|
+
},
|
|
534
|
+
|
|
535
|
+
'compact-auto': () => {
|
|
536
|
+
console.log('Auto-Compact Guidance (Context Window Full):');
|
|
537
|
+
console.log('CRITICAL: Before compacting, ensure you understand:');
|
|
538
|
+
console.log(' - All agents available in .claude/agents/ directory');
|
|
539
|
+
console.log(' - Concurrent execution patterns from CLAUDE.md');
|
|
540
|
+
console.log(' - Swarm coordination strategies for complex tasks');
|
|
541
|
+
console.log('Apply GOLDEN RULE: Always batch operations in single messages');
|
|
542
|
+
console.log('Auto-compact proceeding with full agent context');
|
|
543
|
+
},
|
|
544
|
+
|
|
545
|
+
'status': () => {
|
|
546
|
+
console.log('[OK] Status check');
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
'stats': () => {
|
|
550
|
+
if (intelligence && intelligence.stats) {
|
|
551
|
+
intelligence.stats(args.includes('--json'));
|
|
552
|
+
} else {
|
|
553
|
+
console.log('[WARN] Intelligence module not available. Run session-restore first.');
|
|
554
|
+
}
|
|
555
|
+
},
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
async function main() {
|
|
559
|
+
if (command && handlers[command]) {
|
|
560
|
+
try {
|
|
561
|
+
await Promise.resolve(handlers[command]());
|
|
562
|
+
} catch (e) {
|
|
563
|
+
console.log('[WARN] Hook ' + command + ' encountered an error: ' + e.message);
|
|
564
|
+
}
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (command) {
|
|
569
|
+
console.log('[OK] Hook: ' + command);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
console.log('Usage: hook-handler.cjs <route|pre-bash|pre-edit|post-edit|session-restore|session-end|pre-task|post-task|compact-manual|compact-auto|status|stats>');
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
main();
|