pumuki-ast-hooks 5.3.1

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.
Files changed (567) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1105 -0
  3. package/bin/__tests__/auto-fix-violations.spec.js +132 -0
  4. package/bin/__tests__/auto-restart-guards.spec.js +11 -0
  5. package/bin/__tests__/check-doc-drift.spec.js +11 -0
  6. package/bin/__tests__/check-version.spec.js +240 -0
  7. package/bin/__tests__/cli.spec.js +11 -0
  8. package/bin/__tests__/guard-auto-manager.spec.js +11 -0
  9. package/bin/__tests__/guard-supervisor.spec.js +11 -0
  10. package/bin/__tests__/hook-status.spec.js +11 -0
  11. package/bin/__tests__/install.spec.js +11 -0
  12. package/bin/__tests__/nightly-metrics-report.spec.js +94 -0
  13. package/bin/__tests__/plan-review.spec.js +11 -0
  14. package/bin/__tests__/predictive-hooks.spec.js +11 -0
  15. package/bin/__tests__/run-ast-adapter.spec.js +11 -0
  16. package/bin/__tests__/run-orchestrator.spec.js +11 -0
  17. package/bin/__tests__/run-playbook.spec.js +11 -0
  18. package/bin/__tests__/setup-eslint.spec.js +11 -0
  19. package/bin/__tests__/violations-api.spec.js +11 -0
  20. package/bin/__tests__/watch-hooks.spec.js +11 -0
  21. package/bin/ai-commit.sh +5 -0
  22. package/bin/audit +5 -0
  23. package/bin/audit-library.js +6 -0
  24. package/bin/auto-fix-violations.js +19 -0
  25. package/bin/auto-restart-guards.js +6 -0
  26. package/bin/check-doc-drift.js +6 -0
  27. package/bin/check-version.js +19 -0
  28. package/bin/cleanup-branches.sh +5 -0
  29. package/bin/cli.js +6 -0
  30. package/bin/demo-recording.sh +5 -0
  31. package/bin/demo-violations +5 -0
  32. package/bin/fix-enforcer +5 -0
  33. package/bin/fix-gitflow-enforcement.sh +5 -0
  34. package/bin/generate-progress-report.sh +5 -0
  35. package/bin/git-analyze-pairs.sh +5 -0
  36. package/bin/git-leave-branch-check.sh +5 -0
  37. package/bin/gitflow +5 -0
  38. package/bin/gitflow-shell-integration.sh +5 -0
  39. package/bin/guard-auto-manager.js +6 -0
  40. package/bin/guard-autostart.sh +5 -0
  41. package/bin/guard-env.sh +5 -0
  42. package/bin/guard-supervisor.js +6 -0
  43. package/bin/hook-status.js +6 -0
  44. package/bin/install-git-wrapper.sh +5 -0
  45. package/bin/install.js +6 -0
  46. package/bin/kill-mcp-zombies.sh +5 -0
  47. package/bin/nightly-metrics-report.js +8 -0
  48. package/bin/plan-review.js +6 -0
  49. package/bin/predictive-hooks.js +6 -0
  50. package/bin/pumuki-audit.js +6 -0
  51. package/bin/pumuki-init.js +19 -0
  52. package/bin/pumuki-mcp-server.js +13 -0
  53. package/bin/pumuki-mcp.js +6 -0
  54. package/bin/pumuki-rules.js +6 -0
  55. package/bin/request-no-verify-approval.sh +5 -0
  56. package/bin/run-ast-adapter.js +6 -0
  57. package/bin/run-intelligent-audit.sh +5 -0
  58. package/bin/run-orchestrator.js +6 -0
  59. package/bin/run-playbook.js +6 -0
  60. package/bin/session-loader.sh +5 -0
  61. package/bin/setup-eslint.js +6 -0
  62. package/bin/start-guards.sh +5 -0
  63. package/bin/sync-autonomous-orchestrator.sh +5 -0
  64. package/bin/sync-to-library.sh +5 -0
  65. package/bin/update-evidence.sh +5 -0
  66. package/bin/update-session-context.sh +5 -0
  67. package/bin/verify-no-verify.sh +5 -0
  68. package/bin/violations +5 -0
  69. package/bin/violations-api.js +6 -0
  70. package/bin/watch-hooks.js +6 -0
  71. package/docs/API_REFERENCE.md +161 -0
  72. package/docs/ARCHITECTURE.md +236 -0
  73. package/docs/ARCHITECTURE_DETAILED.md +499 -0
  74. package/docs/BRANCH_PROTECTION_GUIDE.md +236 -0
  75. package/docs/CODE_STANDARDS.md +440 -0
  76. package/docs/CONTRIBUTING.md +246 -0
  77. package/docs/DEPENDENCIES.md +541 -0
  78. package/docs/HOW_IT_WORKS.md +716 -0
  79. package/docs/INSTALLATION.md +784 -0
  80. package/docs/MCP_SERVERS.md +786 -0
  81. package/docs/TESTING.md +423 -0
  82. package/docs/USAGE.md +856 -0
  83. package/docs/images/ast_intelligence_01.png +0 -0
  84. package/docs/images/ast_intelligence_02.png +0 -0
  85. package/docs/images/ast_intelligence_03.png +0 -0
  86. package/docs/images/ast_intelligence_04.png +0 -0
  87. package/docs/images/ast_intelligence_05.png +0 -0
  88. package/hooks/getSkillRulesPath.ts +52 -0
  89. package/hooks/git-status-monitor.ts +160 -0
  90. package/hooks/index.js +5 -0
  91. package/hooks/notify-macos.ts +42 -0
  92. package/hooks/package.json +16 -0
  93. package/hooks/post-tool-use-tracker.sh +89 -0
  94. package/hooks/pre-tool-use-evidence-validator.ts +252 -0
  95. package/hooks/pre-tool-use-guard.ts +151 -0
  96. package/hooks/skill-activation-prompt.sh +8 -0
  97. package/hooks/skill-activation-prompt.ts +307 -0
  98. package/index.js +49 -0
  99. package/package.json +117 -0
  100. package/presentation/cli/audit.sh +24 -0
  101. package/presentation/cli/autonomous-status.sh +92 -0
  102. package/presentation/cli/categorize-violations.sh +179 -0
  103. package/presentation/cli/direct-audit-option2.sh +23 -0
  104. package/presentation/cli/direct-audit.sh +33 -0
  105. package/scripts/hooks-system/.AI_TOKEN_STATUS.txt +16 -0
  106. package/scripts/hooks-system/.audit-reports/auto-recovery.log +1 -0
  107. package/scripts/hooks-system/.audit-reports/install-wizard.log +4 -0
  108. package/scripts/hooks-system/.audit-reports/notifications.log +425 -0
  109. package/scripts/hooks-system/.audit-reports/token-monitor.log +1275 -0
  110. package/scripts/hooks-system/.audit_tmp/intelligent-report.json +44953 -0
  111. package/scripts/hooks-system/.audit_tmp/intelligent-report.txt +1338 -0
  112. package/scripts/hooks-system/.audit_tmp/severity-history.jsonl +1 -0
  113. package/scripts/hooks-system/.audit_tmp/token-usage.jsonl +1 -0
  114. package/scripts/hooks-system/.hook-system/config.json +8 -0
  115. package/scripts/hooks-system/application/CompositionRoot.js +325 -0
  116. package/scripts/hooks-system/application/__tests__/CompositionRoot.spec.js +84 -0
  117. package/scripts/hooks-system/application/commands/index.js +64 -0
  118. package/scripts/hooks-system/application/queries/index.js +60 -0
  119. package/scripts/hooks-system/application/services/AutonomousOrchestrator.js +130 -0
  120. package/scripts/hooks-system/application/services/ContextDetectionEngine.js +181 -0
  121. package/scripts/hooks-system/application/services/DynamicRulesLoader.js +182 -0
  122. package/scripts/hooks-system/application/services/GitFlowService.js +156 -0
  123. package/scripts/hooks-system/application/services/GitTreeState.js +140 -0
  124. package/scripts/hooks-system/application/services/HookSystemScheduler.js +77 -0
  125. package/scripts/hooks-system/application/services/IntelligentCommitAnalyzer.js +151 -0
  126. package/scripts/hooks-system/application/services/IntelligentGitTreeMonitor.js +118 -0
  127. package/scripts/hooks-system/application/services/PlatformAnalysisService.js +173 -0
  128. package/scripts/hooks-system/application/services/PlatformDetectionService.js +168 -0
  129. package/scripts/hooks-system/application/services/PlaybookRunner.js +39 -0
  130. package/scripts/hooks-system/application/services/PredictiveHookAdvisor.js +56 -0
  131. package/scripts/hooks-system/application/services/RealtimeGuardPlugin.js +62 -0
  132. package/scripts/hooks-system/application/services/RealtimeGuardService.js +374 -0
  133. package/scripts/hooks-system/application/services/SmartDirtyTreeAnalyzer.js +63 -0
  134. package/scripts/hooks-system/application/services/__tests__/AutonomousOrchestrator.spec.js +36 -0
  135. package/scripts/hooks-system/application/services/__tests__/ContextDetectionEngine.spec.js +33 -0
  136. package/scripts/hooks-system/application/services/__tests__/DynamicRulesLoader.spec.js +43 -0
  137. package/scripts/hooks-system/application/services/__tests__/GitTreeState.spec.js +163 -0
  138. package/scripts/hooks-system/application/services/__tests__/HookSystemScheduler.spec.js +207 -0
  139. package/scripts/hooks-system/application/services/__tests__/IntelligentCommitAnalyzer.spec.js +365 -0
  140. package/scripts/hooks-system/application/services/__tests__/IntelligentGitTreeMonitor.spec.js +188 -0
  141. package/scripts/hooks-system/application/services/__tests__/PlatformDetectionService.spec.js +28 -0
  142. package/scripts/hooks-system/application/services/__tests__/PlaybookRunner.spec.js +143 -0
  143. package/scripts/hooks-system/application/services/__tests__/PredictiveHookAdvisor.spec.js +181 -0
  144. package/scripts/hooks-system/application/services/__tests__/RealtimeGuardPlugin.spec.js +45 -0
  145. package/scripts/hooks-system/application/services/__tests__/RealtimeGuardService.critical.spec.js +401 -0
  146. package/scripts/hooks-system/application/services/commit/CommitMessageGenerator.js +34 -0
  147. package/scripts/hooks-system/application/services/commit/FeatureDetector.js +101 -0
  148. package/scripts/hooks-system/application/services/evidence/EvidenceContextManager.js +163 -0
  149. package/scripts/hooks-system/application/services/evidence/__tests__/EvidenceContextManager.spec.js +98 -0
  150. package/scripts/hooks-system/application/services/guard/GuardAutoManagerService.js +169 -0
  151. package/scripts/hooks-system/application/services/guard/GuardConfig.js +15 -0
  152. package/scripts/hooks-system/application/services/guard/GuardEventLogger.js +70 -0
  153. package/scripts/hooks-system/application/services/guard/GuardHealthReminder.js +54 -0
  154. package/scripts/hooks-system/application/services/guard/GuardHeartbeatMonitor.js +94 -0
  155. package/scripts/hooks-system/application/services/guard/GuardLockManager.js +72 -0
  156. package/scripts/hooks-system/application/services/guard/GuardMonitorLoop.js +29 -0
  157. package/scripts/hooks-system/application/services/guard/GuardNotificationHandler.js +36 -0
  158. package/scripts/hooks-system/application/services/guard/GuardProcessManager.js +113 -0
  159. package/scripts/hooks-system/application/services/guard/GuardRecoveryService.js +90 -0
  160. package/scripts/hooks-system/application/services/guard/__tests__/GuardAutoManagerService.spec.js +77 -0
  161. package/scripts/hooks-system/application/services/installation/ConfigurationGeneratorService.js +123 -0
  162. package/scripts/hooks-system/application/services/installation/FileSystemInstallerService.js +112 -0
  163. package/scripts/hooks-system/application/services/installation/GitEnvironmentService.js +166 -0
  164. package/scripts/hooks-system/application/services/installation/HookInstaller.js +197 -0
  165. package/scripts/hooks-system/application/services/installation/IdeIntegrationService.js +37 -0
  166. package/scripts/hooks-system/application/services/installation/InstallService.js +130 -0
  167. package/scripts/hooks-system/application/services/installation/McpConfigurator.js +172 -0
  168. package/scripts/hooks-system/application/services/installation/PlatformDetectorService.js +36 -0
  169. package/scripts/hooks-system/application/services/installation/VSCodeTaskConfigurator.js +97 -0
  170. package/scripts/hooks-system/application/services/logging/UnifiedLogger.js +142 -0
  171. package/scripts/hooks-system/application/services/logging/__tests__/UnifiedLogger.spec.js +66 -0
  172. package/scripts/hooks-system/application/services/monitoring/ActivityMonitor.js +80 -0
  173. package/scripts/hooks-system/application/services/monitoring/AstMonitor.js +140 -0
  174. package/scripts/hooks-system/application/services/monitoring/DevDocsMonitor.js +85 -0
  175. package/scripts/hooks-system/application/services/monitoring/EvidenceMonitor.js +103 -0
  176. package/scripts/hooks-system/application/services/monitoring/EvidenceMonitorService.js +162 -0
  177. package/scripts/hooks-system/application/services/monitoring/GitTreeMonitor.js +123 -0
  178. package/scripts/hooks-system/application/services/monitoring/GitTreeMonitorService.js +114 -0
  179. package/scripts/hooks-system/application/services/monitoring/HealthCheckProviders.js +153 -0
  180. package/scripts/hooks-system/application/services/monitoring/HealthCheckService.js +118 -0
  181. package/scripts/hooks-system/application/services/monitoring/HeartbeatMonitorService.js +61 -0
  182. package/scripts/hooks-system/application/services/monitoring/TokenMonitor.js +60 -0
  183. package/scripts/hooks-system/application/services/monitoring/__tests__/EvidenceMonitorService.spec.js +107 -0
  184. package/scripts/hooks-system/application/services/monitoring/__tests__/GitTreeMonitorService.spec.js +27 -0
  185. package/scripts/hooks-system/application/services/monitoring/__tests__/HealthCheckProviders.spec.js +68 -0
  186. package/scripts/hooks-system/application/services/monitoring/__tests__/HealthCheckService.spec.js +69 -0
  187. package/scripts/hooks-system/application/services/monitoring/__tests__/HeartbeatMonitorService.spec.js +35 -0
  188. package/scripts/hooks-system/application/services/notification/MacNotificationSender.js +106 -0
  189. package/scripts/hooks-system/application/services/notification/NotificationCenterService.js +221 -0
  190. package/scripts/hooks-system/application/services/notification/NotificationDispatcher.js +42 -0
  191. package/scripts/hooks-system/application/services/notification/__tests__/NotificationCenterService.spec.js +40 -0
  192. package/scripts/hooks-system/application/services/notification/components/NotificationCooldownManager.js +62 -0
  193. package/scripts/hooks-system/application/services/notification/components/NotificationDeduplicator.js +67 -0
  194. package/scripts/hooks-system/application/services/notification/components/NotificationQueue.js +36 -0
  195. package/scripts/hooks-system/application/services/notification/components/NotificationRetryExecutor.js +58 -0
  196. package/scripts/hooks-system/application/services/platform/PlatformHeuristics.js +144 -0
  197. package/scripts/hooks-system/application/services/recovery/AutoRecoveryManager.js +137 -0
  198. package/scripts/hooks-system/application/services/recovery/__tests__/AutoRecoveryManager.spec.js +62 -0
  199. package/scripts/hooks-system/application/services/smart-commit/CommitMessageSuggester.js +97 -0
  200. package/scripts/hooks-system/application/services/smart-commit/FileContextGrouper.js +114 -0
  201. package/scripts/hooks-system/application/services/smart-commit/SmartCommitSummaryBuilder.js +53 -0
  202. package/scripts/hooks-system/application/services/token/CursorTokenService.js +44 -0
  203. package/scripts/hooks-system/application/services/token/TokenMetricsService.js +109 -0
  204. package/scripts/hooks-system/application/services/token/TokenMonitorService.js +160 -0
  205. package/scripts/hooks-system/application/services/token/TokenStatusReporter.js +56 -0
  206. package/scripts/hooks-system/application/services/token/__tests__/CursorTokenService.spec.js +69 -0
  207. package/scripts/hooks-system/application/services/token/__tests__/TokenMonitorService.spec.js +185 -0
  208. package/scripts/hooks-system/application/state/HookSystemStateMachine.js +59 -0
  209. package/scripts/hooks-system/application/state/__tests__/HookSystemStateMachine.spec.js +115 -0
  210. package/scripts/hooks-system/application/use-cases/AnalyzeCodebaseUseCase.js +54 -0
  211. package/scripts/hooks-system/application/use-cases/AnalyzeStagedFilesUseCase.js +61 -0
  212. package/scripts/hooks-system/application/use-cases/AutoExecuteAIStartUseCase.js +123 -0
  213. package/scripts/hooks-system/application/use-cases/BlockCommitUseCase.js +90 -0
  214. package/scripts/hooks-system/application/use-cases/GenerateAuditReportUseCase.js +184 -0
  215. package/scripts/hooks-system/application/use-cases/__tests__/AnalyzeCodebaseUseCase.spec.js +156 -0
  216. package/scripts/hooks-system/application/use-cases/__tests__/AnalyzeStagedFilesUseCase.spec.js +146 -0
  217. package/scripts/hooks-system/application/use-cases/__tests__/AutoExecuteAIStartUseCase.spec.js +89 -0
  218. package/scripts/hooks-system/application/use-cases/__tests__/BlockCommitUseCase.spec.js +171 -0
  219. package/scripts/hooks-system/application/use-cases/__tests__/GenerateAuditReportUseCase.spec.js +207 -0
  220. package/scripts/hooks-system/bin/__tests__/auto-fix-violations.spec.js +132 -0
  221. package/scripts/hooks-system/bin/__tests__/auto-restart-guards.spec.js +11 -0
  222. package/scripts/hooks-system/bin/__tests__/check-doc-drift.spec.js +11 -0
  223. package/scripts/hooks-system/bin/__tests__/check-version.spec.js +240 -0
  224. package/scripts/hooks-system/bin/__tests__/cli.spec.js +11 -0
  225. package/scripts/hooks-system/bin/__tests__/guard-auto-manager.spec.js +11 -0
  226. package/scripts/hooks-system/bin/__tests__/guard-supervisor.spec.js +11 -0
  227. package/scripts/hooks-system/bin/__tests__/hook-status.spec.js +11 -0
  228. package/scripts/hooks-system/bin/__tests__/install.spec.js +11 -0
  229. package/scripts/hooks-system/bin/__tests__/nightly-metrics-report.spec.js +94 -0
  230. package/scripts/hooks-system/bin/__tests__/plan-review.spec.js +11 -0
  231. package/scripts/hooks-system/bin/__tests__/predictive-hooks.spec.js +11 -0
  232. package/scripts/hooks-system/bin/__tests__/run-ast-adapter.spec.js +11 -0
  233. package/scripts/hooks-system/bin/__tests__/run-orchestrator.spec.js +11 -0
  234. package/scripts/hooks-system/bin/__tests__/run-playbook.spec.js +11 -0
  235. package/scripts/hooks-system/bin/__tests__/setup-eslint.spec.js +11 -0
  236. package/scripts/hooks-system/bin/__tests__/violations-api.spec.js +11 -0
  237. package/scripts/hooks-system/bin/__tests__/watch-hooks.spec.js +11 -0
  238. package/scripts/hooks-system/bin/ai-commit.sh +63 -0
  239. package/scripts/hooks-system/bin/audit +463 -0
  240. package/scripts/hooks-system/bin/audit-library.js +54 -0
  241. package/scripts/hooks-system/bin/auto-fix-violations.js +130 -0
  242. package/scripts/hooks-system/bin/auto-restart-guards.js +93 -0
  243. package/scripts/hooks-system/bin/check-doc-drift.js +35 -0
  244. package/scripts/hooks-system/bin/check-version.js +201 -0
  245. package/scripts/hooks-system/bin/cleanup-branches.sh +106 -0
  246. package/scripts/hooks-system/bin/cli.js +208 -0
  247. package/scripts/hooks-system/bin/demo-recording.sh +57 -0
  248. package/scripts/hooks-system/bin/demo-violations +44 -0
  249. package/scripts/hooks-system/bin/fix-enforcer +27 -0
  250. package/scripts/hooks-system/bin/fix-gitflow-enforcement.sh +68 -0
  251. package/scripts/hooks-system/bin/generate-progress-report.sh +129 -0
  252. package/scripts/hooks-system/bin/git-analyze-pairs.sh +0 -0
  253. package/scripts/hooks-system/bin/git-leave-branch-check.sh +73 -0
  254. package/scripts/hooks-system/bin/gitflow +17 -0
  255. package/scripts/hooks-system/bin/gitflow-shell-integration.sh +64 -0
  256. package/scripts/hooks-system/bin/guard-auto-manager.js +44 -0
  257. package/scripts/hooks-system/bin/guard-autostart.sh +158 -0
  258. package/scripts/hooks-system/bin/guard-env.sh +40 -0
  259. package/scripts/hooks-system/bin/guard-supervisor.js +516 -0
  260. package/scripts/hooks-system/bin/hook-status.js +41 -0
  261. package/scripts/hooks-system/bin/install-git-wrapper.sh +53 -0
  262. package/scripts/hooks-system/bin/install.js +10 -0
  263. package/scripts/hooks-system/bin/kill-mcp-zombies.sh +48 -0
  264. package/scripts/hooks-system/bin/nightly-metrics-report.js +138 -0
  265. package/scripts/hooks-system/bin/plan-review.js +31 -0
  266. package/scripts/hooks-system/bin/predictive-hooks.js +18 -0
  267. package/scripts/hooks-system/bin/pumuki-audit.js +113 -0
  268. package/scripts/hooks-system/bin/pumuki-init.js +104 -0
  269. package/scripts/hooks-system/bin/pumuki-mcp.js +74 -0
  270. package/scripts/hooks-system/bin/pumuki-rules.js +74 -0
  271. package/scripts/hooks-system/bin/request-no-verify-approval.sh +116 -0
  272. package/scripts/hooks-system/bin/run-ast-adapter.js +86 -0
  273. package/scripts/hooks-system/bin/run-intelligent-audit.sh +67 -0
  274. package/scripts/hooks-system/bin/run-orchestrator.js +27 -0
  275. package/scripts/hooks-system/bin/run-playbook.js +23 -0
  276. package/scripts/hooks-system/bin/session-loader.sh +264 -0
  277. package/scripts/hooks-system/bin/setup-eslint.js +110 -0
  278. package/scripts/hooks-system/bin/start-guards.sh +190 -0
  279. package/scripts/hooks-system/bin/sync-autonomous-orchestrator.sh +32 -0
  280. package/scripts/hooks-system/bin/sync-to-library.sh +46 -0
  281. package/scripts/hooks-system/bin/update-evidence.sh +1167 -0
  282. package/scripts/hooks-system/bin/update-session-context.sh +261 -0
  283. package/scripts/hooks-system/bin/verify-no-verify.sh +68 -0
  284. package/scripts/hooks-system/bin/violations +20 -0
  285. package/scripts/hooks-system/bin/violations-api.js +345 -0
  286. package/scripts/hooks-system/bin/watch-hooks.js +20 -0
  287. package/scripts/hooks-system/config/project.config.json +36 -0
  288. package/scripts/hooks-system/config/state-map.json +12 -0
  289. package/scripts/hooks-system/domain/entities/AuditResult.js +139 -0
  290. package/scripts/hooks-system/domain/entities/Finding.js +116 -0
  291. package/scripts/hooks-system/domain/entities/SeverityConfig.js +73 -0
  292. package/scripts/hooks-system/domain/entities/SeverityConfig.ts +90 -0
  293. package/scripts/hooks-system/domain/entities/__tests__/AuditResult.spec.js +450 -0
  294. package/scripts/hooks-system/domain/entities/__tests__/Finding.spec.js +335 -0
  295. package/scripts/hooks-system/domain/entities/__tests__/SeverityConfig.spec.js +240 -0
  296. package/scripts/hooks-system/domain/entities/__tests__/entities.spec.js +29 -0
  297. package/scripts/hooks-system/domain/errors/__tests__/DomainErrors.spec.js +59 -0
  298. package/scripts/hooks-system/domain/errors/index.js +169 -0
  299. package/scripts/hooks-system/domain/events/__tests__/DomainEvents.spec.js +60 -0
  300. package/scripts/hooks-system/domain/events/index.js +121 -0
  301. package/scripts/hooks-system/domain/ports/IAstPort.js +67 -0
  302. package/scripts/hooks-system/domain/ports/IEvidencePort.js +86 -0
  303. package/scripts/hooks-system/domain/ports/IGitCommandPort.js +110 -0
  304. package/scripts/hooks-system/domain/ports/IGitPort.js +114 -0
  305. package/scripts/hooks-system/domain/ports/IGitQueryPort.js +93 -0
  306. package/scripts/hooks-system/domain/ports/INotificationPort.js +35 -0
  307. package/scripts/hooks-system/domain/ports/__tests__/ports.spec.js +36 -0
  308. package/scripts/hooks-system/domain/ports/index.js +14 -0
  309. package/scripts/hooks-system/domain/repositories/ICursorTokenRepository.js +13 -0
  310. package/scripts/hooks-system/domain/repositories/IFindingsRepository.js +30 -0
  311. package/scripts/hooks-system/domain/repositories/__tests__/IFindingsRepository.spec.js +18 -0
  312. package/scripts/hooks-system/domain/rules/CommitBlockingRules.js +142 -0
  313. package/scripts/hooks-system/domain/rules/__tests__/CommitBlockingRules.spec.js +18 -0
  314. package/scripts/hooks-system/domain/services/AuditAnalyzer.js +103 -0
  315. package/scripts/hooks-system/domain/services/AuditFilter.js +26 -0
  316. package/scripts/hooks-system/domain/services/AuditResultSerializer.js +35 -0
  317. package/scripts/hooks-system/domain/services/AuditScorer.js +38 -0
  318. package/scripts/hooks-system/domain/values/Severity.js +93 -0
  319. package/scripts/hooks-system/index.js +49 -0
  320. package/scripts/hooks-system/infrastructure/adapters/AstAnalyzerAdapter.js +150 -0
  321. package/scripts/hooks-system/infrastructure/adapters/FileEvidenceAdapter.js +140 -0
  322. package/scripts/hooks-system/infrastructure/adapters/GitCliAdapter.js +16 -0
  323. package/scripts/hooks-system/infrastructure/adapters/GitCommandAdapter.js +68 -0
  324. package/scripts/hooks-system/infrastructure/adapters/GitHubCliAdapter.js +85 -0
  325. package/scripts/hooks-system/infrastructure/adapters/GitQueryAdapter.js +58 -0
  326. package/scripts/hooks-system/infrastructure/adapters/LegacyAnalyzerAdapter.js +61 -0
  327. package/scripts/hooks-system/infrastructure/adapters/MacOSNotificationAdapter.js +99 -0
  328. package/scripts/hooks-system/infrastructure/adapters/__tests__/AstAnalyzerAdapter.spec.js +32 -0
  329. package/scripts/hooks-system/infrastructure/adapters/__tests__/FileEvidenceAdapter.spec.js +31 -0
  330. package/scripts/hooks-system/infrastructure/adapters/__tests__/GitCliAdapter.spec.js +39 -0
  331. package/scripts/hooks-system/infrastructure/adapters/__tests__/MacOSNotificationAdapter.spec.js +33 -0
  332. package/scripts/hooks-system/infrastructure/adapters/git/GitCommandRunner.js +78 -0
  333. package/scripts/hooks-system/infrastructure/adapters/git/GitCommandService.js +67 -0
  334. package/scripts/hooks-system/infrastructure/adapters/git/GitQueryService.js +50 -0
  335. package/scripts/hooks-system/infrastructure/adapters/index.js +14 -0
  336. package/scripts/hooks-system/infrastructure/ast/README.md +198 -0
  337. package/scripts/hooks-system/infrastructure/ast/__tests__/ast-core.spec.js +160 -0
  338. package/scripts/hooks-system/infrastructure/ast/__tests__/ast-intelligence.spec.js +20 -0
  339. package/scripts/hooks-system/infrastructure/ast/android/__tests__/ast-android.spec.js +33 -0
  340. package/scripts/hooks-system/infrastructure/ast/android/__tests__/clean-architecture-analyzer.spec.js +96 -0
  341. package/scripts/hooks-system/infrastructure/ast/android/__tests__/ddd-analyzer.spec.js +113 -0
  342. package/scripts/hooks-system/infrastructure/ast/android/__tests__/detekt-runner.spec.js +36 -0
  343. package/scripts/hooks-system/infrastructure/ast/android/__tests__/feature-first-analyzer.spec.js +80 -0
  344. package/scripts/hooks-system/infrastructure/ast/android/__tests__/native-bridge.spec.js +31 -0
  345. package/scripts/hooks-system/infrastructure/ast/android/analyzers/AndroidASTIntelligentAnalyzer.js +15 -0
  346. package/scripts/hooks-system/infrastructure/ast/android/analyzers/AndroidASTParser.js +157 -0
  347. package/scripts/hooks-system/infrastructure/ast/android/analyzers/AndroidAnalysisOrchestrator.js +164 -0
  348. package/scripts/hooks-system/infrastructure/ast/android/analyzers/AndroidArchitectureDetector.js +334 -0
  349. package/scripts/hooks-system/infrastructure/ast/android/analyzers/AndroidClassAnalyzer.js +162 -0
  350. package/scripts/hooks-system/infrastructure/ast/android/analyzers/AndroidForbiddenLiteralsAnalyzer.js +261 -0
  351. package/scripts/hooks-system/infrastructure/ast/android/analyzers/AndroidSOLIDAnalyzer.js +287 -0
  352. package/scripts/hooks-system/infrastructure/ast/android/analyzers/__tests__/AndroidForbiddenLiteralsAnalyzer.spec.js +58 -0
  353. package/scripts/hooks-system/infrastructure/ast/android/analyzers/__tests__/AndroidSOLIDAnalyzer.spec.js +84 -0
  354. package/scripts/hooks-system/infrastructure/ast/android/ast-android.js +1785 -0
  355. package/scripts/hooks-system/infrastructure/ast/android/clean-architecture-analyzer.js +115 -0
  356. package/scripts/hooks-system/infrastructure/ast/android/ddd-analyzer.js +70 -0
  357. package/scripts/hooks-system/infrastructure/ast/android/detekt-runner.js +81 -0
  358. package/scripts/hooks-system/infrastructure/ast/android/feature-first-analyzer.js +53 -0
  359. package/scripts/hooks-system/infrastructure/ast/android/native-bridge.js +119 -0
  360. package/scripts/hooks-system/infrastructure/ast/archive/README.md +18 -0
  361. package/scripts/hooks-system/infrastructure/ast/archive/ast-intelligence.ts +276 -0
  362. package/scripts/hooks-system/infrastructure/ast/archive/ios-rules.js +329 -0
  363. package/scripts/hooks-system/infrastructure/ast/archive/kotlin-analyzer.js +332 -0
  364. package/scripts/hooks-system/infrastructure/ast/archive/kotlin-parser.js +303 -0
  365. package/scripts/hooks-system/infrastructure/ast/archive/swift-analyzer.js +390 -0
  366. package/scripts/hooks-system/infrastructure/ast/ast-core.js +594 -0
  367. package/scripts/hooks-system/infrastructure/ast/ast-intelligence.js +617 -0
  368. package/scripts/hooks-system/infrastructure/ast/backend/__tests__/ast-backend.spec.js +20 -0
  369. package/scripts/hooks-system/infrastructure/ast/backend/__tests__/clean-architecture-analyzer.spec.js +151 -0
  370. package/scripts/hooks-system/infrastructure/ast/backend/__tests__/ddd-analyzer.spec.js +124 -0
  371. package/scripts/hooks-system/infrastructure/ast/backend/__tests__/feature-first-analyzer.spec.js +128 -0
  372. package/scripts/hooks-system/infrastructure/ast/backend/__tests__/forbidden-literals-analyzer.spec.js +95 -0
  373. package/scripts/hooks-system/infrastructure/ast/backend/__tests__/nestjs-patterns-analyzer.spec.js +59 -0
  374. package/scripts/hooks-system/infrastructure/ast/backend/__tests__/solid-analyzer.spec.js +114 -0
  375. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/BackendArchitectureDetector.js +141 -0
  376. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/BackendPatternDetector.js +23 -0
  377. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/__tests__/BackendArchitectureDetector.spec.js +239 -0
  378. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/__tests__/BackendPatternDetector.spec.js +58 -0
  379. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/detectors/CQRSDetector.js +41 -0
  380. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/detectors/CleanArchitectureDetector.js +52 -0
  381. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/detectors/FeatureFirstCleanDetector.js +74 -0
  382. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/detectors/LayeredArchitectureDetector.js +25 -0
  383. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/detectors/MVCDetector.js +32 -0
  384. package/scripts/hooks-system/infrastructure/ast/backend/analyzers/detectors/OnionArchitectureDetector.js +32 -0
  385. package/scripts/hooks-system/infrastructure/ast/backend/ast-backend-clean.js +44 -0
  386. package/scripts/hooks-system/infrastructure/ast/backend/ast-backend.js +2048 -0
  387. package/scripts/hooks-system/infrastructure/ast/backend/clean-architecture-analyzer.js +142 -0
  388. package/scripts/hooks-system/infrastructure/ast/backend/ddd-analyzer.js +256 -0
  389. package/scripts/hooks-system/infrastructure/ast/backend/feature-first-analyzer.js +70 -0
  390. package/scripts/hooks-system/infrastructure/ast/backend/forbidden-literals-analyzer.js +236 -0
  391. package/scripts/hooks-system/infrastructure/ast/backend/nestjs-patterns-analyzer.js +11 -0
  392. package/scripts/hooks-system/infrastructure/ast/backend/solid-analyzer.js +392 -0
  393. package/scripts/hooks-system/infrastructure/ast/common/BDDTDDWorkflowRules.js +52 -0
  394. package/scripts/hooks-system/infrastructure/ast/common/__tests__/BDDTDDWorkflowRules.spec.js +133 -0
  395. package/scripts/hooks-system/infrastructure/ast/common/__tests__/ast-common.spec.js +20 -0
  396. package/scripts/hooks-system/infrastructure/ast/common/__tests__/documentation-analyzer.spec.js +120 -0
  397. package/scripts/hooks-system/infrastructure/ast/common/__tests__/images-backend-analyzer.spec.js +123 -0
  398. package/scripts/hooks-system/infrastructure/ast/common/__tests__/monorepo-health-analyzer.spec.js +118 -0
  399. package/scripts/hooks-system/infrastructure/ast/common/__tests__/network-resilience-analyzer.spec.js +180 -0
  400. package/scripts/hooks-system/infrastructure/ast/common/__tests__/offline-backend-analyzer.spec.js +111 -0
  401. package/scripts/hooks-system/infrastructure/ast/common/__tests__/push-backend-analyzer.spec.js +124 -0
  402. package/scripts/hooks-system/infrastructure/ast/common/ast-common.js +345 -0
  403. package/scripts/hooks-system/infrastructure/ast/common/documentation-analyzer.js +217 -0
  404. package/scripts/hooks-system/infrastructure/ast/common/images-backend-analyzer.js +36 -0
  405. package/scripts/hooks-system/infrastructure/ast/common/monorepo-health-analyzer.js +452 -0
  406. package/scripts/hooks-system/infrastructure/ast/common/network-resilience-analyzer.js +178 -0
  407. package/scripts/hooks-system/infrastructure/ast/common/offline-backend-analyzer.js +53 -0
  408. package/scripts/hooks-system/infrastructure/ast/common/push-backend-analyzer.js +42 -0
  409. package/scripts/hooks-system/infrastructure/ast/common/rules/BDDRules.js +87 -0
  410. package/scripts/hooks-system/infrastructure/ast/common/rules/ImplementationRules.js +83 -0
  411. package/scripts/hooks-system/infrastructure/ast/common/rules/TDDRules.js +109 -0
  412. package/scripts/hooks-system/infrastructure/ast/common/rules/WorkflowRules.js +137 -0
  413. package/scripts/hooks-system/infrastructure/ast/frontend/__tests__/ast-frontend.spec.js +20 -0
  414. package/scripts/hooks-system/infrastructure/ast/frontend/analyzers/FrontendArchitectureDetector.js +289 -0
  415. package/scripts/hooks-system/infrastructure/ast/frontend/analyzers/FrontendForbiddenLiteralsAnalyzer.js +257 -0
  416. package/scripts/hooks-system/infrastructure/ast/frontend/analyzers/FrontendSOLIDAnalyzer.js +274 -0
  417. package/scripts/hooks-system/infrastructure/ast/frontend/analyzers/__tests__/FrontendArchitectureDetector.spec.js +151 -0
  418. package/scripts/hooks-system/infrastructure/ast/frontend/analyzers/__tests__/FrontendForbiddenLiteralsAnalyzer.spec.js +20 -0
  419. package/scripts/hooks-system/infrastructure/ast/frontend/analyzers/__tests__/FrontendSOLIDAnalyzer.spec.js +108 -0
  420. package/scripts/hooks-system/infrastructure/ast/frontend/ast-frontend-clean.js +42 -0
  421. package/scripts/hooks-system/infrastructure/ast/frontend/ast-frontend.js +2094 -0
  422. package/scripts/hooks-system/infrastructure/ast/frontend/clean-architecture-analyzer.js +88 -0
  423. package/scripts/hooks-system/infrastructure/ast/frontend/ddd-analyzer.js +94 -0
  424. package/scripts/hooks-system/infrastructure/ast/frontend/feature-first-analyzer.js +51 -0
  425. package/scripts/hooks-system/infrastructure/ast/ios/__tests__/ast-ios.spec.js +40 -0
  426. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSArchitectureDetector.spec.js +20 -0
  427. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSArchitectureRules.spec.js +61 -0
  428. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSCICDRules.spec.js +10 -0
  429. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSEnterpriseAnalyzer.spec.js +36 -0
  430. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSForbiddenLiteralsAnalyzer.spec.js +64 -0
  431. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSNetworkingAdvancedRules.spec.js +10 -0
  432. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSPerformanceRules.spec.js +34 -0
  433. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSSPMRules.spec.js +10 -0
  434. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/__tests__/iOSSwiftUIAdvancedRules.spec.js +10 -0
  435. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSASTIntelligentAnalyzer.js +894 -0
  436. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSArchitectureDetector.js +445 -0
  437. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSArchitectureRules.js +700 -0
  438. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSCICDRules.js +431 -0
  439. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSEnterpriseAnalyzer.js +580 -0
  440. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSForbiddenLiteralsAnalyzer.js +261 -0
  441. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSNetworkingAdvancedRules.js +177 -0
  442. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSPerformanceRules.js +11 -0
  443. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSSPMRules.js +496 -0
  444. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSSwiftUIAdvancedRules.js +333 -0
  445. package/scripts/hooks-system/infrastructure/ast/ios/analyzers/iOSTestingAdvancedRules.js +225 -0
  446. package/scripts/hooks-system/infrastructure/ast/ios/ast-ios.js +2176 -0
  447. package/scripts/hooks-system/infrastructure/ast/ios/native-bridge.js +92 -0
  448. package/scripts/hooks-system/infrastructure/ast/ios/parsers/SourceKittenParser.js +471 -0
  449. package/scripts/hooks-system/infrastructure/ast/ios/parsers/__tests__/SourceKittenParser.spec.js +41 -0
  450. package/scripts/hooks-system/infrastructure/ast/text/__tests__/text-scanner.spec.js +20 -0
  451. package/scripts/hooks-system/infrastructure/ast/text/text-scanner.js +1120 -0
  452. package/scripts/hooks-system/infrastructure/cache/CacheService.js +160 -0
  453. package/scripts/hooks-system/infrastructure/cli/__tests__/install-wizard.spec.js +16 -0
  454. package/scripts/hooks-system/infrastructure/cli/install-wizard.js +74 -0
  455. package/scripts/hooks-system/infrastructure/core/GitOperations.js +50 -0
  456. package/scripts/hooks-system/infrastructure/core/GitOperations.ts +112 -0
  457. package/scripts/hooks-system/infrastructure/core/__tests__/GitOperations.spec.js +146 -0
  458. package/scripts/hooks-system/infrastructure/eslint/eslint-integration.sh +75 -0
  459. package/scripts/hooks-system/infrastructure/events/EventListeners.js +143 -0
  460. package/scripts/hooks-system/infrastructure/events/__tests__/events.spec.js +14 -0
  461. package/scripts/hooks-system/infrastructure/external-tools/GitOperations.js +54 -0
  462. package/scripts/hooks-system/infrastructure/external-tools/eslint/backend.config.template.mjs +58 -0
  463. package/scripts/hooks-system/infrastructure/git-hooks/pre-push +35 -0
  464. package/scripts/hooks-system/infrastructure/git-server/pre-receive-hook +253 -0
  465. package/scripts/hooks-system/infrastructure/guards/git-wrapper.sh +32 -0
  466. package/scripts/hooks-system/infrastructure/guards/master-validator.sh +247 -0
  467. package/scripts/hooks-system/infrastructure/guards/prevent-no-verify.sh +34 -0
  468. package/scripts/hooks-system/infrastructure/hooks/__tests__/skill-activation-prompt.spec.js +11 -0
  469. package/scripts/hooks-system/infrastructure/hooks/pre-tool-use-intelligent-enforcer.sh +489 -0
  470. package/scripts/hooks-system/infrastructure/hooks/skill-activation-prompt.js +244 -0
  471. package/scripts/hooks-system/infrastructure/logging/UnifiedLoggerFactory.js +40 -0
  472. package/scripts/hooks-system/infrastructure/logging/__tests__/logging.spec.js +9 -0
  473. package/scripts/hooks-system/infrastructure/mcp/README.md +116 -0
  474. package/scripts/hooks-system/infrastructure/mcp/__tests__/ast-intelligence-automation.spec.js +38 -0
  475. package/scripts/hooks-system/infrastructure/mcp/__tests__/evidence-watcher.spec.js +38 -0
  476. package/scripts/hooks-system/infrastructure/mcp/ast-intelligence-automation.js +1097 -0
  477. package/scripts/hooks-system/infrastructure/mcp/evidence-watcher.js +128 -0
  478. package/scripts/hooks-system/infrastructure/mcp/package.json +17 -0
  479. package/scripts/hooks-system/infrastructure/mcp/services/EvidenceService.js +87 -0
  480. package/scripts/hooks-system/infrastructure/mcp/services/McpProtocolHandler.js +166 -0
  481. package/scripts/hooks-system/infrastructure/orchestration/__tests__/intelligent-audit.spec.js +11 -0
  482. package/scripts/hooks-system/infrastructure/orchestration/intelligent-audit.js +353 -0
  483. package/scripts/hooks-system/infrastructure/patterns/pattern-checks.sh +98 -0
  484. package/scripts/hooks-system/infrastructure/reporting/ReportImpactAnalyzer.js +109 -0
  485. package/scripts/hooks-system/infrastructure/reporting/ReportMetricsCalculator.js +114 -0
  486. package/scripts/hooks-system/infrastructure/reporting/ReportPresenter.js +86 -0
  487. package/scripts/hooks-system/infrastructure/reporting/__tests__/reporting.spec.js +15 -0
  488. package/scripts/hooks-system/infrastructure/reporting/report-generator.js +130 -0
  489. package/scripts/hooks-system/infrastructure/reporting/severity-tracker.js +105 -0
  490. package/scripts/hooks-system/infrastructure/repositories/CursorTokenRepository.js +76 -0
  491. package/scripts/hooks-system/infrastructure/repositories/FileFindingsRepository.js +88 -0
  492. package/scripts/hooks-system/infrastructure/repositories/__tests__/repositories.spec.js +20 -0
  493. package/scripts/hooks-system/infrastructure/repositories/datasources/CursorApiDataSource.js +73 -0
  494. package/scripts/hooks-system/infrastructure/repositories/datasources/CursorFileDataSource.js +55 -0
  495. package/scripts/hooks-system/infrastructure/severity/__tests__/severity-evaluator.spec.js +18 -0
  496. package/scripts/hooks-system/infrastructure/severity/analyzers/__tests__/maintainability-analyzer.spec.js +170 -0
  497. package/scripts/hooks-system/infrastructure/severity/analyzers/__tests__/performance-analyzer.spec.js +186 -0
  498. package/scripts/hooks-system/infrastructure/severity/analyzers/__tests__/security-analyzer.spec.js +151 -0
  499. package/scripts/hooks-system/infrastructure/severity/analyzers/__tests__/stability-analyzer.spec.js +143 -0
  500. package/scripts/hooks-system/infrastructure/severity/analyzers/maintainability-analyzer.js +100 -0
  501. package/scripts/hooks-system/infrastructure/severity/analyzers/performance-analyzer.js +109 -0
  502. package/scripts/hooks-system/infrastructure/severity/analyzers/security-analyzer.js +104 -0
  503. package/scripts/hooks-system/infrastructure/severity/analyzers/stability-analyzer.js +85 -0
  504. package/scripts/hooks-system/infrastructure/severity/context/analyzers/CodeClassificationAnalyzer.js +71 -0
  505. package/scripts/hooks-system/infrastructure/severity/context/analyzers/DataAnalyzer.js +64 -0
  506. package/scripts/hooks-system/infrastructure/severity/context/analyzers/ImpactAnalyzer.js +68 -0
  507. package/scripts/hooks-system/infrastructure/severity/context/analyzers/SafetyAnalyzer.js +82 -0
  508. package/scripts/hooks-system/infrastructure/severity/context/context-builder.js +88 -0
  509. package/scripts/hooks-system/infrastructure/severity/generators/RecommendationGenerator.js +153 -0
  510. package/scripts/hooks-system/infrastructure/severity/mappers/SeverityMapper.js +10 -0
  511. package/scripts/hooks-system/infrastructure/severity/policies/gate-policies.js +136 -0
  512. package/scripts/hooks-system/infrastructure/severity/policies/severity-policies.json +206 -0
  513. package/scripts/hooks-system/infrastructure/severity/scorers/ContextMultiplier.js +49 -0
  514. package/scripts/hooks-system/infrastructure/severity/severity-evaluator.js +117 -0
  515. package/scripts/hooks-system/infrastructure/shell/core/constants.sh +26 -0
  516. package/scripts/hooks-system/infrastructure/shell/core/utils.sh +45 -0
  517. package/scripts/hooks-system/infrastructure/shell/gitflow/git-wrapper.sh +646 -0
  518. package/scripts/hooks-system/infrastructure/shell/gitflow/gitflow-enforcer.sh +620 -0
  519. package/scripts/hooks-system/infrastructure/shell/gitflow/gitflow-state-manager.sh +235 -0
  520. package/scripts/hooks-system/infrastructure/shell/gitflow-state-manager.sh +225 -0
  521. package/scripts/hooks-system/infrastructure/shell/orchestrators/audit-orchestrator.sh +1106 -0
  522. package/scripts/hooks-system/infrastructure/shell/security/detect-secrets.sh +26 -0
  523. package/scripts/hooks-system/infrastructure/shell/security/detect_secrets.py +182 -0
  524. package/scripts/hooks-system/infrastructure/shell/validate-clean-architecture.sh +254 -0
  525. package/scripts/hooks-system/infrastructure/shell/validators/check-doc-structure.sh +62 -0
  526. package/scripts/hooks-system/infrastructure/shell/validators/ensure-critical-docs.sh +26 -0
  527. package/scripts/hooks-system/infrastructure/shell/validators/validate-ai-protocol.sh +474 -0
  528. package/scripts/hooks-system/infrastructure/shell/validators/validate-clean-architecture.sh +303 -0
  529. package/scripts/hooks-system/infrastructure/shell/validators/validate-conventional-commit.sh +42 -0
  530. package/scripts/hooks-system/infrastructure/storage/file-operations.sh +31 -0
  531. package/scripts/hooks-system/infrastructure/telemetry/TelemetryService.js +165 -0
  532. package/scripts/hooks-system/infrastructure/telemetry/__tests__/telemetry.spec.js +15 -0
  533. package/scripts/hooks-system/infrastructure/telemetry/metrics-logger.js +66 -0
  534. package/scripts/hooks-system/infrastructure/telemetry/metrics-server.js +61 -0
  535. package/scripts/hooks-system/infrastructure/utils/__tests__/utils.spec.js +8 -0
  536. package/scripts/hooks-system/infrastructure/utils/error-utils.js +28 -0
  537. package/scripts/hooks-system/infrastructure/utils/timestamp-helper.sh +106 -0
  538. package/scripts/hooks-system/infrastructure/utils/token-manager.js +121 -0
  539. package/scripts/hooks-system/infrastructure/validators/__tests__/detect-commit-language.spec.js +16 -0
  540. package/scripts/hooks-system/infrastructure/validators/__tests__/enforce-english-literals.spec.js +67 -0
  541. package/scripts/hooks-system/infrastructure/validators/detect-commit-language.js +145 -0
  542. package/scripts/hooks-system/infrastructure/validators/enforce-english-literals.js +202 -0
  543. package/scripts/hooks-system/infrastructure/watchdog/__tests__/.audit-reports/token-monitor.log +18 -0
  544. package/scripts/hooks-system/infrastructure/watchdog/__tests__/auto-recovery.spec.js +14 -0
  545. package/scripts/hooks-system/infrastructure/watchdog/__tests__/token-monitor.spec.js +67 -0
  546. package/scripts/hooks-system/infrastructure/watchdog/__tests__/watchdog.spec.js +22 -0
  547. package/scripts/hooks-system/infrastructure/watchdog/ai-watchdog.sh +278 -0
  548. package/scripts/hooks-system/infrastructure/watchdog/auto-recovery.js +32 -0
  549. package/scripts/hooks-system/infrastructure/watchdog/health-check.js +58 -0
  550. package/scripts/hooks-system/infrastructure/watchdog/token-monitor-loop.sh +20 -0
  551. package/scripts/hooks-system/infrastructure/watchdog/token-monitor.js +69 -0
  552. package/scripts/hooks-system/infrastructure/watchdog/token-tracker.sh +208 -0
  553. package/scripts/hooks-system/presentation/cli/audit.sh +32 -0
  554. package/scripts/hooks-system/presentation/cli/autonomous-status.sh +92 -0
  555. package/scripts/hooks-system/presentation/cli/categorize-violations.sh +179 -0
  556. package/scripts/hooks-system/presentation/cli/direct-audit-option2.sh +23 -0
  557. package/scripts/hooks-system/presentation/cli/direct-audit.sh +33 -0
  558. package/skills/android-guidelines/SKILL.md +475 -0
  559. package/skills/android-guidelines/resources/advanced-topics.md +44 -0
  560. package/skills/android-guidelines/resources/architecture-overview.md +44 -0
  561. package/skills/backend-guidelines/SKILL.md +335 -0
  562. package/skills/backend-guidelines/resources/architecture-overview.md +48 -0
  563. package/skills/frontend-guidelines/SKILL.md +367 -0
  564. package/skills/frontend-guidelines/resources/architecture-overview.md +44 -0
  565. package/skills/ios-guidelines/SKILL.md +406 -0
  566. package/skills/ios-guidelines/resources/architecture-overview.md +47 -0
  567. package/skills/skill-rules.json +334 -0
@@ -0,0 +1,1167 @@
1
+ #!/bin/bash
2
+ # ═══════════════════════════════════════════════════════════════
3
+ # AI Evidence Auto-Updater (Proactive + Autonomous)
4
+ # ═══════════════════════════════════════════════════════════════
5
+ # Usage:
6
+ # ai-start [feature-name] # Interactive mode
7
+ # ai-start --auto --platforms backend # Autonomous mode
8
+ #
9
+ # This script MUST be run BEFORE editing any code.
10
+ # It updates .AI_EVIDENCE.json with current timestamp and
11
+ # generates the 3 questions protocol template.
12
+ #
13
+ # Add to .zshrc: alias ai-start="bash scripts/hooks-system/bin/update-evidence.sh"
14
+ # ═══════════════════════════════════════════════════════════════
15
+
16
+ set -euo pipefail
17
+
18
+ # Colors
19
+ RED='\033[0;31m'
20
+ GREEN='\033[0;32m'
21
+ YELLOW='\033[1;33m'
22
+ BLUE='\033[0;34m'
23
+ CYAN='\033[0;36m'
24
+ NC='\033[0m'
25
+
26
+ # Paths
27
+ REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
28
+ EVIDENCE_FILE="$REPO_ROOT/.AI_EVIDENCE.json"
29
+ SESSION_FILE="$REPO_ROOT/.AI_SESSION_START.md"
30
+
31
+ # Detect if running from node_modules (installed package) or from local dev
32
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
33
+ if [[ "$SCRIPT_DIR" == *"node_modules/@pumuki/ast-intelligence-hooks"* ]]; then
34
+ # Running from installed package in node_modules
35
+ HOOKS_SYSTEM_DIR="$SCRIPT_DIR/.."
36
+ elif [[ -d "$REPO_ROOT/infrastructure/ast" ]]; then
37
+ # Running from local development (ast-intelligence-hooks repo itself)
38
+ HOOKS_SYSTEM_DIR="$REPO_ROOT"
39
+ elif [[ -d "$REPO_ROOT/scripts/hooks-system" ]]; then
40
+ # Legacy: running from scripts/hooks-system
41
+ HOOKS_SYSTEM_DIR="$REPO_ROOT/scripts/hooks-system"
42
+ else
43
+ # Fallback: use script directory parent
44
+ HOOKS_SYSTEM_DIR="$SCRIPT_DIR/.."
45
+ fi
46
+
47
+ # Parse arguments for autonomous mode
48
+ AUTO_MODE=false
49
+ PLATFORMS=""
50
+ REFRESH_ONLY=false
51
+
52
+ while [[ $# -gt 0 ]]; do
53
+ case "$1" in
54
+ --auto)
55
+ AUTO_MODE=true
56
+ shift
57
+ ;;
58
+ --platforms)
59
+ PLATFORMS="$2"
60
+ shift 2
61
+ ;;
62
+ --refresh-only)
63
+ REFRESH_ONLY=true
64
+ shift
65
+ ;;
66
+ *)
67
+ FEATURE_NAME="$1"
68
+ shift
69
+ ;;
70
+ esac
71
+ done
72
+
73
+ # FEATURE_NAME may be provided as first argument; if not, it will default
74
+ # to a name derived from the current branch (configured after reading CURRENT_BRANCH).
75
+
76
+ # Banner (only in interactive mode)
77
+ if [[ "$AUTO_MODE" == "false" ]]; then
78
+ echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
79
+ echo -e "${BLUE}🤖 AI Evidence Auto-Updater${NC}"
80
+ echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
81
+ echo ""
82
+ fi
83
+
84
+ # Get current timestamp in local timezone with offset (ISO 8601)
85
+ TIMESTAMP=$(date +"%Y-%m-%dT%H:%M:%S%z" | sed 's/\([0-9][0-9]\)$/:\1/')
86
+
87
+ # Get current branch for context
88
+ CURRENT_BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown")
89
+
90
+ if [[ "$REFRESH_ONLY" == "false" && ("$CURRENT_BRANCH" == "develop" || "$CURRENT_BRANCH" == "main" || "$CURRENT_BRANCH" == "master") ]]; then
91
+ echo -e "${RED}❌ AI Gate: Protected branch '$CURRENT_BRANCH'.${NC}"
92
+ echo -e "${YELLOW}➡️ Create a feature branch (git checkout -b feature/xxx) before editing code.${NC}"
93
+ exit 1
94
+ fi
95
+
96
+ # Derive default feature name from current branch if not provided
97
+ DEFAULT_FEATURE_NAME="manual-update"
98
+ if [[ -n "$CURRENT_BRANCH" && "$CURRENT_BRANCH" != "unknown" ]]; then
99
+ DEFAULT_FEATURE_NAME=$(echo "$CURRENT_BRANCH" | sed 's#[/ ]#-#g')
100
+ fi
101
+ FEATURE_NAME="${FEATURE_NAME:-$DEFAULT_FEATURE_NAME}"
102
+
103
+ # Get last 3 commits for context
104
+ LAST_COMMITS=$(git log --oneline -3 2>/dev/null | head -3 | tr '\n' '; ' || echo "No recent commits")
105
+
106
+ # JSON-safe string (prevent invalid JSON when commit messages contain quotes)
107
+ LAST_COMMITS_JSON=$(printf '%s' "$LAST_COMMITS" | jq -Rs .)
108
+
109
+ # Detect which files will be modified (if in git staging)
110
+ STAGED_FILES=$(git diff --cached --name-only 2>/dev/null || echo "")
111
+
112
+ # Also check modified but not staged files for context
113
+ MODIFIED_FILES=$(git diff --name-only 2>/dev/null || echo "")
114
+ ALL_CHANGED_FILES="$STAGED_FILES $MODIFIED_FILES"
115
+
116
+ # Generate contextual answers based on actual work
117
+ generate_contextual_answers() {
118
+ local all_files="$1"
119
+ local branch="$2"
120
+ local commits="$3"
121
+
122
+ # Detect work type first
123
+ local work_type="code"
124
+ local is_docs_only=false
125
+ local is_config_only=false
126
+ local is_tests_only=false
127
+
128
+ # Check if only documentation
129
+ if [[ -n "$all_files" ]]; then
130
+ local non_doc_files=$(echo "$all_files" | tr ' ' '\n' | grep -v "\.md$" | grep -v "^$" | head -1)
131
+ if [[ -z "$non_doc_files" ]]; then
132
+ is_docs_only=true
133
+ work_type="documentation"
134
+ fi
135
+ fi
136
+
137
+ # Check if only config files
138
+ if [[ "$all_files" == *".json"* ]] || [[ "$all_files" == *".yaml"* ]] || [[ "$all_files" == *".yml"* ]]; then
139
+ if [[ "$all_files" != *".ts"* ]] && [[ "$all_files" != *".js"* ]] && [[ "$all_files" != *".swift"* ]]; then
140
+ is_config_only=true
141
+ work_type="configuration"
142
+ fi
143
+ fi
144
+
145
+ # Check if only tests
146
+ if [[ "$all_files" == *".spec.ts"* ]] || [[ "$all_files" == *".test.ts"* ]]; then
147
+ local non_test_files=$(echo "$all_files" | tr ' ' '\n' | grep -v "\.spec\." | grep -v "\.test\." | grep -v "^$" | head -1)
148
+ if [[ -z "$non_test_files" ]]; then
149
+ is_tests_only=true
150
+ work_type="testing"
151
+ fi
152
+ fi
153
+
154
+ # Detect modules being worked on
155
+ local modules=""
156
+ [[ "$all_files" == *"/admin/"* ]] && modules="$modules admin,"
157
+ [[ "$all_files" == *"/auth/"* ]] && modules="$modules auth,"
158
+ [[ "$all_files" == *"/orders/"* ]] && modules="$modules orders,"
159
+ [[ "$all_files" == *"/products/"* ]] && modules="$modules products,"
160
+ [[ "$all_files" == *"/stores/"* ]] && modules="$modules stores,"
161
+ [[ "$all_files" == *"/notifications/"* ]] && modules="$modules notifications,"
162
+ [[ "$all_files" == *"/hooks-system/"* ]] && modules="$modules hooks-system,"
163
+ [[ "$all_files" == *"/testing/"* ]] && modules="$modules testing,"
164
+ [[ "$all_files" == *"docs/"* ]] && modules="$modules documentation,"
165
+ [[ "$all_files" == *".windsurf/"* ]] && modules="$modules ide-config,"
166
+ [[ "$all_files" == *".cursor/"* ]] && modules="$modules ide-config,"
167
+ [[ "$all_files" == *".vscode/"* ]] && modules="$modules ide-config,"
168
+ modules="${modules%,}"
169
+ [[ -z "$modules" ]] && modules="general"
170
+
171
+ # Detect file types (all platforms)
172
+ local file_types=""
173
+ # Documentation
174
+ [[ "$all_files" == *".md"* ]] && file_types="$file_types markdown,"
175
+ # Backend/Frontend TypeScript
176
+ [[ "$all_files" == *".spec.ts"* ]] && file_types="$file_types tests,"
177
+ [[ "$all_files" == *".service.ts"* ]] && file_types="$file_types services,"
178
+ [[ "$all_files" == *".repository.ts"* ]] && file_types="$file_types repositories,"
179
+ [[ "$all_files" == *".controller.ts"* ]] && file_types="$file_types controllers,"
180
+ [[ "$all_files" == *".gateway.ts"* ]] && file_types="$file_types gateways,"
181
+ [[ "$all_files" == *".interface.ts"* ]] && file_types="$file_types interfaces,"
182
+ [[ "$all_files" == *".mock.ts"* ]] && file_types="$file_types mocks,"
183
+ [[ "$all_files" == *".helper.ts"* ]] && file_types="$file_types helpers,"
184
+ [[ "$all_files" == *".ts"* ]] && file_types="$file_types typescript,"
185
+ [[ "$all_files" == *".tsx"* ]] && file_types="$file_types react-components,"
186
+ [[ "$all_files" == *".jsx"* ]] && file_types="$file_types react-components,"
187
+ # iOS Swift
188
+ [[ "$all_files" == *".swift"* ]] && file_types="$file_types swift,"
189
+ [[ "$all_files" == *"Tests.swift"* ]] && file_types="$file_types swift-tests,"
190
+ [[ "$all_files" == *"ViewModel.swift"* ]] && file_types="$file_types viewmodels,"
191
+ [[ "$all_files" == *"View.swift"* ]] && file_types="$file_types swiftui-views,"
192
+ [[ "$all_files" == *"UseCase.swift"* ]] && file_types="$file_types use-cases,"
193
+ [[ "$all_files" == *"Repository.swift"* ]] && file_types="$file_types repositories,"
194
+ # Android Kotlin
195
+ [[ "$all_files" == *".kt"* ]] && file_types="$file_types kotlin,"
196
+ [[ "$all_files" == *".kts"* ]] && file_types="$file_types gradle-kotlin,"
197
+ [[ "$all_files" == *"Test.kt"* ]] && file_types="$file_types kotlin-tests,"
198
+ [[ "$all_files" == *"ViewModel.kt"* ]] && file_types="$file_types viewmodels,"
199
+ [[ "$all_files" == *"Activity.kt"* ]] && file_types="$file_types activities,"
200
+ [[ "$all_files" == *"Fragment.kt"* ]] && file_types="$file_types fragments,"
201
+ [[ "$all_files" == *"UseCase.kt"* ]] && file_types="$file_types use-cases,"
202
+ [[ "$all_files" == *"Repository.kt"* ]] && file_types="$file_types repositories,"
203
+ # Scripts and config
204
+ [[ "$all_files" == *".sh"* ]] && file_types="$file_types shell-scripts,"
205
+ [[ "$all_files" == *".js"* ]] && file_types="$file_types javascript,"
206
+ [[ "$all_files" == *".json"* ]] && file_types="$file_types json-config,"
207
+ [[ "$all_files" == *".yaml"* ]] && file_types="$file_types yaml-config,"
208
+ [[ "$all_files" == *".yml"* ]] && file_types="$file_types yaml-config,"
209
+ [[ "$all_files" == *".xml"* ]] && file_types="$file_types xml-config,"
210
+ [[ "$all_files" == *".plist"* ]] && file_types="$file_types plist-config,"
211
+ [[ "$all_files" == *".gradle"* ]] && file_types="$file_types gradle,"
212
+ file_types="${file_types%,}"
213
+ [[ -z "$file_types" ]] && file_types="mixed files"
214
+
215
+ # Detect layer (Clean Architecture) - only for code
216
+ local layer="N/A"
217
+ if [[ "$is_docs_only" == "false" ]] && [[ "$is_config_only" == "false" ]]; then
218
+ [[ "$all_files" == *"/domain/"* ]] && layer="Domain"
219
+ [[ "$all_files" == *"/application/"* ]] && layer="Application"
220
+ [[ "$all_files" == *"/infrastructure/"* ]] && layer="Infrastructure"
221
+ [[ "$all_files" == *"/presentation/"* ]] && layer="Presentation"
222
+ [[ "$layer" == "N/A" ]] && layer="multiple layers"
223
+ fi
224
+
225
+ # Extract feature from branch name
226
+ local action="working on"
227
+ if [[ "$branch" == feature/* ]]; then
228
+ action="implementing feature"
229
+ elif [[ "$branch" == fix/* ]]; then
230
+ action="fixing"
231
+ elif [[ "$branch" == refactor/* ]]; then
232
+ action="refactoring"
233
+ elif [[ "$branch" == chore/* ]]; then
234
+ action="maintenance task"
235
+ elif [[ "$branch" == docs/* ]]; then
236
+ action="documenting"
237
+ fi
238
+
239
+ # Generate contextual Q1 based on work type
240
+ if [[ "$is_docs_only" == "true" ]]; then
241
+ Q1="Documentation task on branch '$branch'. Modifying $file_types in: $modules. No code rules apply - focus on clarity and accuracy."
242
+ elif [[ "$is_config_only" == "true" ]]; then
243
+ Q1="Configuration task on branch '$branch'. Modifying $file_types. Ensure config changes are backward compatible."
244
+ elif [[ "$is_tests_only" == "true" ]]; then
245
+ Q1="Testing task on branch '$branch'. Modifying $file_types in: $modules. Follow AAA pattern (Arrange-Act-Assert)."
246
+ else
247
+ Q1="Code task on branch '$branch'. Modifying $file_types in: $modules. Target layer: $layer."
248
+ fi
249
+
250
+ # Generate contextual Q2 based on work type
251
+ if [[ "$is_docs_only" == "true" ]]; then
252
+ Q2="Documentation in: $modules. Recent commits: $commits. Check for duplicate docs before creating new ones."
253
+ elif [[ "$is_tests_only" == "true" ]]; then
254
+ Q2="Tests for: $modules. Recent commits: $commits. Check existing test helpers and mocks before creating new ones."
255
+ else
256
+ Q2="Modules affected: $modules. Recent commits: $commits. Check for existing patterns before adding new code."
257
+ fi
258
+
259
+ # Generate contextual Q3 based on work type
260
+ if [[ "$is_docs_only" == "true" ]]; then
261
+ Q3="Documentation changes in $modules. Keep docs in sync with code. Update related READMEs if needed."
262
+ elif [[ "$is_config_only" == "true" ]]; then
263
+ Q3="Config changes. Validate JSON/YAML syntax. Check for environment-specific values."
264
+ elif [[ "$is_tests_only" == "true" ]]; then
265
+ Q3="Test changes in $modules. Ensure tests are isolated, deterministic, and follow naming conventions."
266
+ else
267
+ Q3="Code changes in $layer layer affecting $modules. Ensure dependencies point inward (Domain <- App <- Infra)."
268
+ fi
269
+
270
+ # Export work type for rules selection
271
+ export WORK_TYPE="$work_type"
272
+ export CONTEXTUAL_Q1="$Q1"
273
+ export CONTEXTUAL_Q2="$Q2"
274
+ export CONTEXTUAL_Q3="$Q3"
275
+ }
276
+
277
+ # Generate contextual answers
278
+ generate_contextual_answers "$ALL_CHANGED_FILES" "$CURRENT_BRANCH" "$LAST_COMMITS"
279
+
280
+ # Extract key rules from IDE rules files (supports multiple IDEs)
281
+ # Searches: project-level first, then global Cursor config
282
+ extract_ide_rules() {
283
+ local rules_file="$1"
284
+ local rules_path=""
285
+ local home_dir="${HOME}"
286
+
287
+ # 1. Try project-level rules first (project-specific overrides)
288
+ for ide_dir in ".windsurf" ".cursor" ".vscode" ".kilo" ".cline"; do
289
+ local candidate="$REPO_ROOT/$ide_dir/rules/$rules_file"
290
+ if [[ -f "$candidate" ]]; then
291
+ rules_path="$candidate"
292
+ break
293
+ fi
294
+ # Also try without .mdc extension
295
+ candidate="$REPO_ROOT/$ide_dir/rules/${rules_file%.mdc}.md"
296
+ if [[ -f "$candidate" ]]; then
297
+ rules_path="$candidate"
298
+ break
299
+ fi
300
+ done
301
+
302
+ # 2. If not found in project, try Cursor project-specific cache location
303
+ # Cursor stores rules per project in: ~/.cursor/projects/[sanitized-path]/rules/
304
+ if [[ -z "$rules_path" ]] || [[ ! -f "$rules_path" ]]; then
305
+ # Try to find in Cursor's project cache (where Cursor actually stores project rules)
306
+ local sanitized_repo=$(echo "$REPO_ROOT" | sed 's/[^a-zA-Z0-9]/-/g' | tr '[:upper:]' '[:lower:]')
307
+ local cursor_project_cache="$home_dir/.cursor/projects/$sanitized_repo/rules/$rules_file"
308
+ if [[ -f "$cursor_project_cache" ]]; then
309
+ rules_path="$cursor_project_cache"
310
+ fi
311
+ fi
312
+
313
+ # 3. If still not found, try global Cursor locations and other projects as fallback
314
+ if [[ -z "$rules_path" ]] || [[ ! -f "$rules_path" ]]; then
315
+ # Try Cursor global config locations
316
+ local global_paths=(
317
+ "$home_dir/.cursor/rules/$rules_file"
318
+ "$home_dir/.cursor/rules/${rules_file%.mdc}.md"
319
+ "$home_dir/Library/Application Support/Cursor/User/rules/$rules_file"
320
+ "$home_dir/Library/Application Support/Cursor/User/rules/${rules_file%.mdc}.md"
321
+ "$home_dir/.config/cursor/rules/$rules_file"
322
+ "$home_dir/.config/cursor/rules/${rules_file%.mdc}.md"
323
+ )
324
+
325
+ for candidate in "${global_paths[@]}"; do
326
+ if [[ -f "$candidate" ]]; then
327
+ rules_path="$candidate"
328
+ break
329
+ fi
330
+ done
331
+ fi
332
+
333
+ # 4. Also check other Cursor project caches (look for rules in R_GO_local or other template projects)
334
+ if [[ -z "$rules_path" ]] || [[ ! -f "$rules_path" ]]; then
335
+ # Search in all Cursor project caches for this rules file
336
+ if [[ -d "$home_dir/.cursor/projects" ]]; then
337
+ local found_rule=$(find "$home_dir/.cursor/projects" -type f \( -name "$rules_file" -o -name "${rules_file%.mdc}.md" \) 2>/dev/null | head -1)
338
+ if [[ -n "$found_rule" ]] && [[ -f "$found_rule" ]]; then
339
+ rules_path="$found_rule"
340
+ fi
341
+ fi
342
+ fi
343
+
344
+ # 5. Also check for @rulesgold or goldrules (common naming)
345
+ if [[ -z "$rules_path" ]] || [[ ! -f "$rules_path" ]]; then
346
+ local gold_names=(
347
+ "rulesgold.mdc"
348
+ "goldrules.mdc"
349
+ "@rulesgold.mdc"
350
+ "rules-gold.mdc"
351
+ )
352
+
353
+ # Try project-level gold rules
354
+ for ide_dir in ".cursor" ".vscode"; do
355
+ for gold_name in "${gold_names[@]}"; do
356
+ local candidate="$REPO_ROOT/$ide_dir/rules/$gold_name"
357
+ if [[ -f "$candidate" ]]; then
358
+ rules_path="$candidate"
359
+ break 2
360
+ fi
361
+ done
362
+ done
363
+
364
+ # Try library-installed gold rules (from node_modules)
365
+ if [[ -z "$rules_path" ]] || [[ ! -f "$rules_path" ]]; then
366
+ for gold_name in "${gold_names[@]}"; do
367
+ local library_gold="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/.cursor/rules/$gold_name"
368
+ if [[ -f "$library_gold" ]]; then
369
+ rules_path="$library_gold"
370
+ break
371
+ fi
372
+ done
373
+ fi
374
+
375
+ # Try global gold rules
376
+ if [[ -z "$rules_path" ]] || [[ ! -f "$rules_path" ]]; then
377
+ for gold_name in "${gold_names[@]}"; do
378
+ local global_gold=(
379
+ "$home_dir/.cursor/rules/$gold_name"
380
+ "$home_dir/Library/Application Support/Cursor/User/rules/$gold_name"
381
+ )
382
+ for candidate in "${global_gold[@]}"; do
383
+ if [[ -f "$candidate" ]]; then
384
+ rules_path="$candidate"
385
+ break 2
386
+ fi
387
+ done
388
+ done
389
+ fi
390
+ fi
391
+
392
+ if [[ -n "$rules_path" ]] && [[ -f "$rules_path" ]]; then
393
+ grep -E "^##" "$rules_path" 2>/dev/null | head -10 | sed 's/^## //' | tr '\n' '; ' || echo "No sections found"
394
+ else
395
+ echo "No IDE rules found"
396
+ fi
397
+ }
398
+
399
+ # Extract AST Intelligence rules summary
400
+ extract_ast_rules() {
401
+ local platform="$1"
402
+ local ast_path="$HOOKS_SYSTEM_DIR/infrastructure/ast/$platform"
403
+
404
+ # If hooks-system doesn't exist in project, try node_modules
405
+ if [[ ! -d "$ast_path" ]] && [[ "$HOOKS_SYSTEM_DIR" != *"node_modules"* ]]; then
406
+ local nm_path="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/infrastructure/ast/$platform"
407
+ if [[ -d "$nm_path" ]]; then
408
+ ast_path="$nm_path"
409
+ fi
410
+ fi
411
+
412
+ if [[ -d "$ast_path" ]]; then
413
+ # Count rule files and extract rule IDs
414
+ local rule_count=$(find "$ast_path" -name "*.js" 2>/dev/null | wc -l | tr -d ' ')
415
+ local rule_ids=$(grep -rh "ruleId:" "$ast_path" 2>/dev/null | head -10 | sed "s/.*ruleId:[[:space:]]*['\"]\\([^'\"]*\\)['\"].*/\\1/" | tr '\n' ', ' || echo "")
416
+ echo "Files: $rule_count, Rules: ${rule_ids%,}"
417
+ else
418
+ echo "No AST rules for $platform"
419
+ fi
420
+ }
421
+
422
+ # Get rules summary based on detected platform
423
+ get_rules_summary() {
424
+ local rules_file="$1"
425
+ local platform=""
426
+
427
+ case "$rules_file" in
428
+ rulesbackend.mdc|rulesbackend.md) platform="backend" ;;
429
+ rulesfront.mdc|rulesfront.md) platform="frontend" ;;
430
+ rulesios.mdc|rulesios.md) platform="ios" ;;
431
+ rulesandroid.mdc|rulesandroid.md) platform="android" ;;
432
+ esac
433
+
434
+ local ide_sections=$(extract_ide_rules "${rules_file%.mdc}.md")
435
+ local ast_summary=$(extract_ast_rules "$platform")
436
+
437
+ echo "IDE Rules: $ide_sections | AST: $ast_summary"
438
+ }
439
+
440
+ run_ast_full_analysis() {
441
+ local ast_intelligence="$HOOKS_SYSTEM_DIR/infrastructure/ast/ast-intelligence.js"
442
+ if [[ ! -f "$ast_intelligence" ]]; then
443
+ ast_intelligence="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/infrastructure/ast/ast-intelligence.js"
444
+ if [[ ! -f "$ast_intelligence" ]]; then
445
+ ast_intelligence="$REPO_ROOT/infrastructure/ast/ast-intelligence.js"
446
+ if [[ ! -f "$ast_intelligence" ]]; then
447
+ return 0
448
+ fi
449
+ fi
450
+ fi
451
+
452
+ if [[ "$AUTO_MODE" == "true" ]]; then
453
+ node "$ast_intelligence" >/dev/null 2>&1 || true
454
+ else
455
+ echo -e "${BLUE}🧠 Running full AST analysis...${NC}"
456
+ node "$ast_intelligence" 2>&1 | tail -5 || true
457
+ fi
458
+
459
+ local summary_file="$REPO_ROOT/.audit_tmp/ast-summary.json"
460
+ if [[ -f "$summary_file" ]]; then
461
+ local total_findings
462
+ total_findings=$(jq '.findings | length' "$summary_file" 2>/dev/null || echo "0")
463
+ local critical=$(jq '.levels.CRITICAL // 0' "$summary_file" 2>/dev/null || echo "0")
464
+ local high=$(jq '.levels.HIGH // 0' "$summary_file" 2>/dev/null || echo "0")
465
+
466
+ if [[ "$AUTO_MODE" != "true" ]]; then
467
+ echo -e "${GREEN}🧠 AST Analysis: $total_findings findings (CRITICAL:$critical HIGH:$high)${NC}"
468
+ fi
469
+ fi
470
+ }
471
+
472
+ run_ast_early_check() {
473
+ if [[ "${AST_EARLY_CHECK:-1}" != "1" ]]; then
474
+ return 0
475
+ fi
476
+
477
+ run_ast_full_analysis
478
+
479
+ local ast_adapter="$HOOKS_SYSTEM_DIR/bin/run-ast-adapter.js"
480
+ if [[ ! -f "$ast_adapter" ]]; then
481
+ ast_adapter="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/infrastructure/ast/run-ast-adapter.js"
482
+ if [[ ! -f "$ast_adapter" ]]; then
483
+ return 0
484
+ fi
485
+ fi
486
+
487
+ if [[ "$AUTO_MODE" != "true" ]]; then
488
+ echo -e "${BLUE}🧠 Running AST early check on staged files...${NC}"
489
+ fi
490
+ local ast_output
491
+ ast_output=$(node "$ast_adapter" 2>/dev/null || echo "[]")
492
+
493
+ local findings_count
494
+ findings_count=$(echo "$ast_output" | jq 'length' 2>/dev/null || echo "0")
495
+
496
+ if [[ "$AUTO_MODE" != "true" ]]; then
497
+ if [[ "$findings_count" -gt 0 ]]; then
498
+ echo -e "${YELLOW}🧠 AST Early Check: $findings_count findings on staged files (see AST reports/pre-commit).${NC}"
499
+ else
500
+ echo -e "${GREEN}🧠 AST Early Check: no findings on staged files.${NC}"
501
+ fi
502
+ fi
503
+
504
+ if [[ "$(uname 2>/dev/null)" == "Darwin" ]] && [[ "$AUTO_MODE" != "true" ]]; then
505
+ local platforms_label
506
+ platforms_label=$(echo "$PLATFORMS_JSON" | jq -r 'join(", ")' 2>/dev/null || echo "")
507
+
508
+ local msg
509
+ if [[ "$findings_count" -gt 0 ]]; then
510
+ msg="AST early check: $findings_count findings"
511
+ else
512
+ msg="AST early check: no findings"
513
+ fi
514
+
515
+ if [[ -n "$platforms_label" ]]; then
516
+ msg+=" on $platforms_label"
517
+ fi
518
+
519
+ osascript -e "display notification \"$msg\" with title \"AST Hooks\" sound name \"default\"" >/dev/null 2>&1 || true
520
+ fi
521
+ }
522
+
523
+ start_ast_watch_if_needed() {
524
+ if [[ "$AUTO_MODE" == "true" ]]; then
525
+ return 0
526
+ fi
527
+
528
+ if [[ "${AST_WATCH_AUTO:-1}" != "1" ]]; then
529
+ return 0
530
+ fi
531
+
532
+ local watcher_script="$HOOKS_SYSTEM_DIR/bin/watch-hooks.js"
533
+ if [[ ! -f "$watcher_script" ]]; then
534
+ # Try node_modules path
535
+ watcher_script="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/bin/watch-hooks.js"
536
+ if [[ ! -f "$watcher_script" ]]; then
537
+ return 0
538
+ fi
539
+ fi
540
+
541
+ local pid_file="$REPO_ROOT/.ast_watch.pid"
542
+ if [[ -f "$pid_file" ]]; then
543
+ local existing_pid
544
+ existing_pid=$(cat "$pid_file" 2>/dev/null || echo "")
545
+ if [[ -n "$existing_pid" ]]; then
546
+ local ps_out
547
+ ps_out=$(ps -p "$existing_pid" -o args= 2>/dev/null || true)
548
+ if echo "$ps_out" | grep -q "watch-hooks.js"; then
549
+ echo -e "${CYAN}ℹ️ AST watch already running (PID $existing_pid).${NC}"
550
+ if [[ "$(uname 2>/dev/null)" == "Darwin" ]]; then
551
+ osascript -e "display notification \"AST watch already running (PID $existing_pid)\" with title \"AST Hooks\" sound name \"default\"" >/dev/null 2>&1 || true
552
+ fi
553
+ return 0
554
+ fi
555
+ fi
556
+ fi
557
+
558
+ local log_file="$REPO_ROOT/.ast_watch.log"
559
+ node "$watcher_script" >"$log_file" 2>&1 &
560
+ local new_pid=$!
561
+ echo "$new_pid" > "$pid_file"
562
+ echo -e "${GREEN}✅ AST watch started in background (PID $new_pid).${NC}"
563
+ if [[ "$(uname 2>/dev/null)" == "Darwin" ]]; then
564
+ osascript -e "display notification \"AST watch started (PID $new_pid)\" with title \"AST Hooks\" sound name \"default\"" >/dev/null 2>&1 || true
565
+ fi
566
+ }
567
+
568
+ if [[ -z "$STAGED_FILES" ]]; then
569
+ echo -e "${YELLOW}⚠️ No staged files detected.${NC}"
570
+ echo -e "${CYAN}📝 You can manually add files to evidence after editing.${NC}"
571
+ FILES_ARRAY="[]"
572
+ else
573
+ echo -e "${GREEN}✅ Staged files detected:${NC}"
574
+ echo "$STAGED_FILES" | while read -r file; do
575
+ echo " • $file"
576
+ done
577
+ FILES_ARRAY=$(echo "$STAGED_FILES" | jq -R . | jq -s .)
578
+ fi
579
+
580
+ echo ""
581
+
582
+ # Auto-detect rules file based on staged files
583
+ detect_rules_file() {
584
+ local file="$1"
585
+ local ext="${file##*.}"
586
+ # Documentation - no code rules
587
+ if [[ "$ext" == "md" ]]; then
588
+ echo "none"
589
+ return
590
+ fi
591
+
592
+ # Hook-system / tooling scripts (Multi-platform library) - NOT platform-specific
593
+ # These files support ALL 4 platforms (Backend, Frontend, iOS, Android)
594
+ if [[ "$file" == *"scripts/hooks-system/"* ]] || [[ "$file" == *"scripts/hook-system/"* ]]; then
595
+ echo "infrastructure" # Multi-platform library, not tied to single platform
596
+ return
597
+ fi
598
+
599
+ # Backend detection (NestJS apps) - relative paths like 'apps/backend/...'
600
+ if [[ "$file" == *"apps/backend/"* ]] || [[ "$file" == *"src/backend/"* ]]; then
601
+ echo "rulesbackend.mdc"
602
+ return
603
+ fi
604
+
605
+ # Frontend detection
606
+ if [[ "$file" == *"apps/frontend/"* ]] || [[ "$file" == *"apps/web/"* ]] || [[ "$file" == *"src/frontend/"* ]]; then
607
+ echo "rulesfront.mdc"
608
+ return
609
+ fi
610
+
611
+ # iOS detection
612
+ if [[ "$file" == *"ios/"* ]] || [[ "$file" == *"iOS/"* ]] || [[ "$file" == *"Apps/iOS/"* ]]; then
613
+ echo "rulesios.mdc"
614
+ return
615
+ fi
616
+
617
+ # Android detection
618
+ if [[ "$file" == *"android/"* ]] || [[ "$file" == *"Android/"* ]] || [[ "$file" == *"Apps/Android/"* ]]; then
619
+ echo "rulesandroid.mdc"
620
+ return
621
+ fi
622
+
623
+ # Extension-based detection (fallback)
624
+ case "$ext" in
625
+ ts)
626
+ echo "rulesbackend.mdc"
627
+ ;;
628
+ tsx|jsx)
629
+ echo "rulesfront.mdc"
630
+ ;;
631
+ js|mjs|cjs)
632
+ echo "rulesbackend.mdc"
633
+ ;;
634
+ swift)
635
+ echo "rulesios.mdc"
636
+ ;;
637
+ kt|kts)
638
+ echo "rulesandroid.mdc"
639
+ ;;
640
+ sh|json|yaml|yml|xml|plist|gradle)
641
+ echo "none"
642
+ ;;
643
+ *)
644
+ echo "none"
645
+ ;;
646
+ esac
647
+ }
648
+
649
+ # Detect appropriate rules file
650
+ RULES_FILES=()
651
+
652
+ # Always include rulesgold.mdc if available (generic rules that apply to all projects)
653
+ GOLD_RULES_PATH=""
654
+ for gold_name in "rulesgold.mdc" "goldrules.mdc" "@rulesgold.mdc" "rules-gold.mdc"; do
655
+ # Check library-installed rules first (highest priority for generic rules)
656
+ if [[ -f "$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/.cursor/rules/$gold_name" ]]; then
657
+ GOLD_RULES_PATH="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/.cursor/rules/$gold_name"
658
+ break
659
+ fi
660
+ # Check project-level
661
+ if [[ -f "$REPO_ROOT/.cursor/rules/$gold_name" ]]; then
662
+ GOLD_RULES_PATH="$REPO_ROOT/.cursor/rules/$gold_name"
663
+ break
664
+ fi
665
+ # Check global
666
+ if [[ -f "${HOME}/.cursor/rules/$gold_name" ]]; then
667
+ GOLD_RULES_PATH="${HOME}/.cursor/rules/$gold_name"
668
+ break
669
+ fi
670
+ done
671
+
672
+ if [[ -n "$GOLD_RULES_PATH" ]] && [[ -f "$GOLD_RULES_PATH" ]]; then
673
+ RULES_FILES+=("rulesgold.mdc")
674
+ fi
675
+
676
+ if [[ -n "$STAGED_FILES" ]]; then
677
+ # Infer platforms from ALL staged files (skip only evidence metadata files)
678
+ while IFS= read -r file; do
679
+ [[ -z "$file" ]] && continue
680
+
681
+ if [[ "$file" == ".AI_EVIDENCE.json" ]] || [[ "$file" == ".AI_SESSION_START.md" ]]; then
682
+ continue
683
+ fi
684
+
685
+ rules_for_file=$(detect_rules_file "$file")
686
+ if [[ "$rules_for_file" != "none" ]]; then
687
+ # Use default expansion to avoid unbound variable errors under set -u
688
+ if [[ ! " ${RULES_FILES[*]-} " =~ " $rules_for_file " ]]; then
689
+ RULES_FILES+=("$rules_for_file")
690
+ fi
691
+ fi
692
+ done <<< "$STAGED_FILES"
693
+
694
+ # Always prepend rulesgold.mdc if available (generic rules apply to all)
695
+ if [[ -n "$GOLD_RULES_PATH" ]] && [[ -f "$GOLD_RULES_PATH" ]]; then
696
+ if [[ ! " ${RULES_FILES[*]-} " =~ " rulesgold.mdc " ]]; then
697
+ RULES_FILES=("rulesgold.mdc" "${RULES_FILES[@]}")
698
+ fi
699
+ fi
700
+
701
+ # Fallback: if no platform-specific rules detected, use first staged file
702
+ if [[ ${#RULES_FILES[@]} -eq 0 ]]; then
703
+ PRIMARY_FILE=$(echo "$STAGED_FILES" | head -1)
704
+ RULES_FILE=$(detect_rules_file "$PRIMARY_FILE")
705
+ RULES_FILES+=("$RULES_FILE")
706
+ fi
707
+ else
708
+ if [[ "$AUTO_MODE" == "true" ]]; then
709
+ # Non-interactive mode: infer platforms from --platforms or use all by default
710
+ if [[ -n "$PLATFORMS" ]]; then
711
+ IFS=',' read -ra SELECTED_PLATFORMS <<< "$PLATFORMS"
712
+ else
713
+ SELECTED_PLATFORMS=("frontend" "backend" "ios" "android")
714
+ fi
715
+
716
+ for platform in "${SELECTED_PLATFORMS[@]}"; do
717
+ platform_normalized=$(echo "$platform" | tr '[:upper:]' '[:lower:]' | xargs)
718
+ case "$platform_normalized" in
719
+ 1|"frontend")
720
+ RULES_FILES+=("rulesfront.mdc")
721
+ ;;
722
+ 2|"backend")
723
+ RULES_FILES+=("rulesbackend.mdc")
724
+ ;;
725
+ 3|"ios")
726
+ RULES_FILES+=("rulesios.mdc")
727
+ ;;
728
+ 4|"android")
729
+ RULES_FILES+=("rulesandroid.mdc")
730
+ ;;
731
+ esac
732
+ done
733
+
734
+ # Fallback: si nada válido, usar las 4 plataformas por defecto
735
+ if [[ ${#RULES_FILES[@]} -eq 0 ]]; then
736
+ RULES_FILES=("rulesfront.mdc" "rulesbackend.mdc" "rulesios.mdc" "rulesandroid.mdc")
737
+ fi
738
+
739
+ # Compatibilidad con el caso de una sola plataforma
740
+ RULES_FILE="${RULES_FILES[0]}"
741
+ else
742
+ echo -e "${CYAN}ℹ️ Which platforms are you working on?${NC}"
743
+ echo " 1) Frontend (React/Next.js)"
744
+ echo " 2) Backend (NestJS)"
745
+ echo " 3) iOS (Swift)"
746
+ echo " 4) Android (Kotlin)"
747
+ echo ""
748
+ echo -e "${YELLOW}💡 Tip: You can select multiple platforms (e.g., '1,2' for Frontend+Backend)${NC}"
749
+ read -p "Select (e.g., 1 or 1,2 or 1,2,3): " platform_choice
750
+
751
+ # Parse multiple selections
752
+ RULES_FILES=()
753
+ IFS=',' read -ra PLATFORMS <<< "$platform_choice"
754
+ for platform in "${PLATFORMS[@]}"; do
755
+ # Trim whitespace
756
+ platform=$(echo "$platform" | xargs)
757
+ case $platform in
758
+ 1) RULES_FILES+=("rulesfront.mdc") ;;
759
+ 2) RULES_FILES+=("rulesbackend.mdc") ;;
760
+ 3) RULES_FILES+=("rulesios.mdc") ;;
761
+ 4) RULES_FILES+=("rulesandroid.mdc") ;;
762
+ esac
763
+ done
764
+
765
+ # Fallback if nothing selected
766
+ if [[ ${#RULES_FILES[@]} -eq 0 ]]; then
767
+ RULES_FILES=("rulesbackend.mdc")
768
+ fi
769
+
770
+ # Always prepend rulesgold.mdc if available (generic rules apply to all)
771
+ if [[ -n "$GOLD_RULES_PATH" ]] && [[ -f "$GOLD_RULES_PATH" ]]; then
772
+ if [[ ! " ${RULES_FILES[*]-} " =~ " rulesgold.mdc " ]]; then
773
+ RULES_FILES=("rulesgold.mdc" "${RULES_FILES[@]}")
774
+ fi
775
+ fi
776
+
777
+ # For single selection, maintain backward compatibility
778
+ RULES_FILE="${RULES_FILES[0]}"
779
+ fi
780
+ fi
781
+
782
+ # Default RULES_FILE to first detected rules file (if any)
783
+ RULES_FILE="${RULES_FILES[0]:-none}"
784
+
785
+ PLATFORMS_JSON="[]"
786
+ if [[ ${#RULES_FILES[@]} -gt 0 ]]; then
787
+ platforms_tmp=""
788
+ for rf in "${RULES_FILES[@]}"; do
789
+ platform=""
790
+ case "$rf" in
791
+ rulesbackend.mdc|rulesbackend.md) platform="backend" ;;
792
+ rulesfront.mdc|rulesfront.md) platform="frontend" ;;
793
+ rulesios.mdc|rulesios.md) platform="ios" ;;
794
+ rulesandroid.mdc|rulesandroid.md) platform="android" ;;
795
+ esac
796
+ if [[ -n "$platform" ]]; then
797
+ if [[ -n "$platforms_tmp" ]]; then
798
+ platforms_tmp+=","
799
+ fi
800
+ platforms_tmp+="\"$platform\""
801
+ fi
802
+ done
803
+ if [[ -n "$platforms_tmp" ]]; then
804
+ PLATFORMS_JSON="[$platforms_tmp]"
805
+ fi
806
+ fi
807
+
808
+ echo -e "${GREEN}✅ Rules selected:${NC}"
809
+ if [[ ${#RULES_FILES[@]} -le 1 ]]; then
810
+ echo " • ${RULES_FILES[0]:-none}"
811
+ else
812
+ for rule in "${RULES_FILES[@]}"; do
813
+ echo " • $rule"
814
+ done
815
+ fi
816
+ echo ""
817
+
818
+ # Prepare temp file for atomic write
819
+ TMP_FILE=$(mktemp "${EVIDENCE_FILE}.tmp.XXXXXX")
820
+
821
+ # Get all modified files (staged + unstaged) and split into code vs infra
822
+ CODE_MODIFIED_FOR_JSON="[]"
823
+ INFRA_MODIFIED_FOR_JSON="[]"
824
+ if [[ -n "$ALL_CHANGED_FILES" ]] && [[ "$ALL_CHANGED_FILES" != " " ]]; then
825
+ FILTERED_FILES=$(echo "$ALL_CHANGED_FILES" | tr ' ' '\n' | grep -v "^$" | sort -u | head -20 || true)
826
+ if [[ -n "$FILTERED_FILES" ]]; then
827
+ CODE_FILES=""
828
+ INFRA_FILES=""
829
+
830
+ while IFS= read -r f; do
831
+ [[ -z "$f" ]] && continue
832
+
833
+ # Classify by extension and purpose
834
+ case "$f" in
835
+ # Backend code (TypeScript/JavaScript in apps/backend)
836
+ apps/backend/src/*.ts|apps/backend/src/*.js)
837
+ CODE_FILES+="$f"$'\n'
838
+ ;;
839
+ # Frontend code (React/Next.js)
840
+ apps/admin-dashboard/src/*.tsx|apps/admin-dashboard/src/*.ts|apps/admin-dashboard/src/*.jsx|apps/admin-dashboard/src/*.js)
841
+ CODE_FILES+="$f"$'\n'
842
+ ;;
843
+ apps/web-app/src/*.tsx|apps/web-app/src/*.ts|apps/web-app/src/*.jsx|apps/web-app/src/*.js)
844
+ CODE_FILES+="$f"$'\n'
845
+ ;;
846
+ # iOS code (Swift)
847
+ apps/ios/*.swift|apps/ios/**/*.swift)
848
+ CODE_FILES+="$f"$'\n'
849
+ ;;
850
+ # Android code (Kotlin)
851
+ apps/android/*.kt|apps/android/**/*.kt)
852
+ CODE_FILES+="$f"$'\n'
853
+ ;;
854
+ # Hook-system code (JavaScript/Shell - FIRST CLASS CODE)
855
+ scripts/hooks-system/*.js|scripts/hooks-system/**/*.js|scripts/hooks-system/*.sh|scripts/hooks-system/**/*.sh)
856
+ CODE_FILES+="$f"$'\n'
857
+ ;;
858
+ # Infrastructure (docs, configs, metadata - NOT code)
859
+ *.md|*.json|*.yaml|*.yml|*.toml|.AI_*|docs/*|.cursor/*|.vscode/*|.windsurf/*|.pre-commit-config.yaml|package.json|tsconfig.json|*.baseline.json)
860
+ INFRA_FILES+="$f"$'\n'
861
+ ;;
862
+ # Default: if in src/ directory → CODE, else → INFRA
863
+ */src/*)
864
+ CODE_FILES+="$f"$'\n'
865
+ ;;
866
+ *)
867
+ INFRA_FILES+="$f"$'\n'
868
+ ;;
869
+ esac
870
+ done <<< "$FILTERED_FILES"
871
+
872
+ if [[ -n "$CODE_FILES" ]]; then
873
+ CODE_MODIFIED_FOR_JSON=$(echo "$CODE_FILES" | grep -v "^$" | jq -R . | jq -s . 2>/dev/null || echo "[]")
874
+ fi
875
+
876
+ if [[ -n "$INFRA_FILES" ]]; then
877
+ INFRA_MODIFIED_FOR_JSON=$(echo "$INFRA_FILES" | grep -v "^$" | jq -R . | jq -s . 2>/dev/null || echo "[]")
878
+ fi
879
+ fi
880
+ fi
881
+
882
+ # Get rules summary based on work type (single-platform case)
883
+ if [[ "$WORK_TYPE" == "documentation" ]]; then
884
+ RULES_SUMMARY="N/A - Documentation only (no code rules apply)"
885
+ RULES_FILE="none"
886
+ elif [[ "$WORK_TYPE" == "configuration" ]]; then
887
+ RULES_SUMMARY="N/A - Configuration only (validate syntax)"
888
+ RULES_FILE="none"
889
+ else
890
+ RULES_SUMMARY=$(get_rules_summary "$RULES_FILE")
891
+ fi
892
+
893
+ # Generate contextual answers
894
+ generate_contextual_answers "$ALL_CHANGED_FILES" "$CURRENT_BRANCH" "$LAST_COMMITS"
895
+
896
+ # JSON-safe protocol answers (prevent invalid JSON when answers contain quotes)
897
+ CONTEXTUAL_Q1_JSON=$(printf '%s' "${CONTEXTUAL_Q1:-}" | jq -Rs .)
898
+ CONTEXTUAL_Q2_JSON=$(printf '%s' "${CONTEXTUAL_Q2:-}" | jq -Rs .)
899
+ CONTEXTUAL_Q3_JSON=$(printf '%s' "${CONTEXTUAL_Q3:-}" | jq -Rs .)
900
+
901
+ # Generate evidence JSON with multi-platform support
902
+ if [[ ${#RULES_FILES[@]} -le 1 ]]; then
903
+ # Single platform (backward compatible)
904
+ cat > "$TMP_FILE" <<EOF
905
+ {
906
+ "timestamp": "$TIMESTAMP",
907
+ "session_id": "$FEATURE_NAME",
908
+ "action": "$(echo $FEATURE_NAME | sed 's/-/_/g')",
909
+ "work_type": "$WORK_TYPE",
910
+ "platforms": $PLATFORMS_JSON,
911
+ "files_modified": $CODE_MODIFIED_FOR_JSON,
912
+ "infra_modified": $INFRA_MODIFIED_FOR_JSON,
913
+ "rules_read": {
914
+ "file": "$RULES_FILE",
915
+ "verified": true,
916
+ "summary": "$RULES_SUMMARY"
917
+ },
918
+ "protocol_3_questions": {
919
+ "answered": true,
920
+ "question_1_file_type": $CONTEXTUAL_Q1_JSON,
921
+ "question_2_similar_exists": $CONTEXTUAL_Q2_JSON,
922
+ "question_3_clean_architecture": $CONTEXTUAL_Q3_JSON
923
+ },
924
+ "current_context": {
925
+ "branch": "$CURRENT_BRANCH",
926
+ "last_commits": $LAST_COMMITS_JSON
927
+ },
928
+ "justification": "Context-aware evidence for branch '$CURRENT_BRANCH'. Auto-detected modules and file types from current work.",
929
+ "approved_by": "Pumuki Team®"
930
+ }
931
+ EOF
932
+ else
933
+ # Multiple platforms
934
+ RULES_JSON="["
935
+ for i in "${!RULES_FILES[@]}"; do
936
+ if [[ $i -gt 0 ]]; then
937
+ RULES_JSON+=","
938
+ fi
939
+ rule_summary=$(get_rules_summary "${RULES_FILES[$i]}")
940
+ RULES_JSON+="
941
+ {
942
+ \"file\": \"${RULES_FILES[$i]}\",
943
+ \"verified\": true,
944
+ \"summary\": \"$rule_summary\"
945
+ }"
946
+ done
947
+ RULES_JSON+="
948
+ ]"
949
+
950
+ cat > "$TMP_FILE" <<EOF
951
+ {
952
+ "timestamp": "$TIMESTAMP",
953
+ "session_id": "$FEATURE_NAME",
954
+ "action": "$(echo $FEATURE_NAME | sed 's/-/_/g')",
955
+ "work_type": "$WORK_TYPE",
956
+ "platforms": $PLATFORMS_JSON,
957
+ "files_modified": $CODE_MODIFIED_FOR_JSON,
958
+ "infra_modified": $INFRA_MODIFIED_FOR_JSON,
959
+ "rules_read": $RULES_JSON,
960
+ "also_read": [".AI_SESSION_START.md"],
961
+ "protocol_3_questions": {
962
+ "answered": true,
963
+ "question_1_file_type": $CONTEXTUAL_Q1_JSON,
964
+ "question_2_similar_exists": $CONTEXTUAL_Q2_JSON,
965
+ "question_3_clean_architecture": $CONTEXTUAL_Q3_JSON
966
+ },
967
+ "current_context": {
968
+ "branch": "$CURRENT_BRANCH",
969
+ "last_commits": $LAST_COMMITS_JSON
970
+ },
971
+ "justification": "Context-aware evidence for branch '$CURRENT_BRANCH'. Auto-detected modules and file types from current work.",
972
+ "approved_by": "Pumuki Team®"
973
+ }
974
+ EOF
975
+ fi
976
+
977
+ mv "$TMP_FILE" "$EVIDENCE_FILE"
978
+
979
+ # Execute audit first to get violations, then call intelligent-audit to add ai_gate
980
+ # Always run audit if ai_gate is missing or if not in refresh-only mode
981
+ NEEDS_AUDIT=false
982
+ if [[ "$REFRESH_ONLY" == "false" ]]; then
983
+ NEEDS_AUDIT=true
984
+ elif [[ -f "$EVIDENCE_FILE" ]]; then
985
+ # Check if ai_gate section exists in evidence
986
+ if ! grep -q '"ai_gate"' "$EVIDENCE_FILE" 2>/dev/null; then
987
+ NEEDS_AUDIT=true
988
+ fi
989
+ fi
990
+
991
+ if [[ "$NEEDS_AUDIT" == "true" ]]; then
992
+ # Run AST audit to detect violations (only staged files for evidence)
993
+ # Clean previous ast-summary.json to avoid reading violations from full repo audits
994
+ rm -f "$REPO_ROOT/.audit_tmp/ast-summary.json" 2>/dev/null || true
995
+
996
+ export STAGING_ONLY_MODE=1
997
+ AST_SCRIPT="$HOOKS_SYSTEM_DIR/infrastructure/ast/ast-intelligence.js"
998
+ if [[ ! -f "$AST_SCRIPT" ]]; then
999
+ AST_SCRIPT="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/infrastructure/ast/ast-intelligence.js"
1000
+ fi
1001
+ if [[ -f "$AST_SCRIPT" ]]; then
1002
+ node "$AST_SCRIPT" >/dev/null 2>&1 || true
1003
+ fi
1004
+ unset STAGING_ONLY_MODE
1005
+ fi
1006
+
1007
+ # Call intelligent-audit to add ai_gate, watchers, git_flow sections
1008
+ INTELLIGENT_AUDIT="$HOOKS_SYSTEM_DIR/infrastructure/orchestration/intelligent-audit.js"
1009
+ if [[ ! -f "$INTELLIGENT_AUDIT" ]]; then
1010
+ # Try node_modules path
1011
+ INTELLIGENT_AUDIT="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/infrastructure/orchestration/intelligent-audit.js"
1012
+ fi
1013
+ if [[ -f "$INTELLIGENT_AUDIT" ]]; then
1014
+ node "$INTELLIGENT_AUDIT" >/dev/null 2>&1 || true
1015
+ fi
1016
+
1017
+ # Export non-gitignored public artifacts for AI + tooling (summary + public JSON)
1018
+ # Rationale: .AI_EVIDENCE.json is gitignored and may be blocked by some tooling.
1019
+ PUBLIC_EVIDENCE_FILE="$REPO_ROOT/.AI_EVIDENCE_PUBLIC.json"
1020
+ EVIDENCE_SUMMARY_FILE="$REPO_ROOT/.AI_EVIDENCE_SUMMARY.md"
1021
+ AST_SUMMARY_FILE="$REPO_ROOT/.audit_tmp/ast-summary.json"
1022
+ AST_SUMMARY_ENHANCED_FILE="$REPO_ROOT/.audit_tmp/ast-summary-enhanced.json"
1023
+
1024
+ if command -v jq >/dev/null 2>&1 && [[ -f "$EVIDENCE_FILE" ]]; then
1025
+ # Prefer enhanced AST summary (includes intelligent evaluation), fallback to raw.
1026
+ AST_SOURCE_FILE=""
1027
+ if [[ -f "$AST_SUMMARY_ENHANCED_FILE" ]]; then
1028
+ AST_SOURCE_FILE="$AST_SUMMARY_ENHANCED_FILE"
1029
+ elif [[ -f "$AST_SUMMARY_FILE" ]]; then
1030
+ AST_SOURCE_FILE="$AST_SUMMARY_FILE"
1031
+ fi
1032
+
1033
+ # Public JSON (sanitized subset) + embed ast_summary if available
1034
+ if [[ -n "$AST_SOURCE_FILE" ]]; then
1035
+ jq -S --argfile ast "$AST_SOURCE_FILE" '
1036
+ {
1037
+ timestamp: .timestamp,
1038
+ session: (.session // .session_id // null),
1039
+ action: (.action // null),
1040
+ platforms_detected: (.platforms_detected // null),
1041
+ severity_metrics: (.severity_metrics // null),
1042
+ token_usage: (.token_usage // null),
1043
+ git_flow: (.git_flow // null),
1044
+ ai_gate: (.ai_gate // null),
1045
+ watchers: (.watchers // null),
1046
+ ast_summary: $ast
1047
+ }
1048
+ ' "$EVIDENCE_FILE" > "$PUBLIC_EVIDENCE_FILE" 2>/dev/null || true
1049
+ else
1050
+ jq -S '
1051
+ {
1052
+ timestamp: .timestamp,
1053
+ session: (.session // .session_id // null),
1054
+ action: (.action // null),
1055
+ platforms_detected: (.platforms_detected // null),
1056
+ severity_metrics: (.severity_metrics // null),
1057
+ token_usage: (.token_usage // null),
1058
+ git_flow: (.git_flow // null),
1059
+ ai_gate: (.ai_gate // null),
1060
+ watchers: (.watchers // null)
1061
+ }
1062
+ ' "$EVIDENCE_FILE" > "$PUBLIC_EVIDENCE_FILE" 2>/dev/null || true
1063
+ fi
1064
+
1065
+ # Markdown summary (stable, human + AI friendly)
1066
+ {
1067
+ echo "# AI Evidence Summary"
1068
+ echo ""
1069
+ echo "- **timestamp**: $(jq -r '.timestamp // "unknown"' "$EVIDENCE_FILE" 2>/dev/null)"
1070
+ echo "- **session**: $(jq -r '.session // .session_id // "unknown"' "$EVIDENCE_FILE" 2>/dev/null)"
1071
+ echo "- **action**: $(jq -r '.action // "unknown"' "$EVIDENCE_FILE" 2>/dev/null)"
1072
+ echo ""
1073
+
1074
+ if [[ -n "$AST_SOURCE_FILE" ]]; then
1075
+ echo "## Levels"
1076
+ echo ""
1077
+ echo "- **CRITICAL**: $(jq -r '.levels.CRITICAL // .summary.CRITICAL // 0' "$AST_SOURCE_FILE" 2>/dev/null)"
1078
+ echo "- **HIGH**: $(jq -r '.levels.HIGH // .summary.HIGH // 0' "$AST_SOURCE_FILE" 2>/dev/null)"
1079
+ echo "- **MEDIUM**: $(jq -r '.levels.MEDIUM // .summary.MEDIUM // 0' "$AST_SOURCE_FILE" 2>/dev/null)"
1080
+ echo "- **LOW**: $(jq -r '.levels.LOW // .summary.LOW // 0' "$AST_SOURCE_FILE" 2>/dev/null)"
1081
+ echo ""
1082
+ echo "## Top Rules"
1083
+ echo ""
1084
+ jq -r '
1085
+ (.findings // [])
1086
+ | sort_by(-(.count // 1))
1087
+ | .[:10]
1088
+ | .[]
1089
+ | "- **\(.ruleId // .rule // \"unknown\")**: \(.count // 1) (\(.severity // .level // \"unknown\"), \(.platform // \"unknown\"))"
1090
+ ' "$AST_SOURCE_FILE" 2>/dev/null || true
1091
+ echo ""
1092
+ fi
1093
+
1094
+ if jq -e '.ai_gate.violations | length > 0' "$EVIDENCE_FILE" >/dev/null 2>&1; then
1095
+ echo "## Top Gate Violations"
1096
+ echo ""
1097
+ jq -r '
1098
+ (.ai_gate.violations // [])
1099
+ | .[:10]
1100
+ | .[]
1101
+ | "- **\(.rule // \"unknown\")**: \(.severity // \"unknown\") - \(.file // \"unknown\"):\(.line // 0)"
1102
+ ' "$EVIDENCE_FILE" 2>/dev/null || true
1103
+ echo ""
1104
+ fi
1105
+ } > "$EVIDENCE_SUMMARY_FILE" 2>/dev/null || true
1106
+ fi
1107
+
1108
+ if [[ "$AUTO_MODE" == "true" ]]; then
1109
+ if command -v osascript >/dev/null 2>&1; then
1110
+ osascript -e "display notification \"AI evidence updated at $TIMESTAMP\" with title \"✅ Evidence Updated\" sound name \"Glass\"" 2>/dev/null || true
1111
+ fi
1112
+ echo "{\"success\":true,\"timestamp\":\"$TIMESTAMP\",\"session\":\"$FEATURE_NAME\",\"platforms\":\"$PLATFORMS\",\"mode\":\"autonomous\"}"
1113
+ exit 0
1114
+ fi
1115
+
1116
+ run_ast_early_check
1117
+ start_ast_watch_if_needed
1118
+
1119
+ SYNC_SCRIPT="$HOOKS_SYSTEM_DIR/bin/sync-autonomous-orchestrator.sh"
1120
+ if [[ ! -x "$SYNC_SCRIPT" ]]; then
1121
+ # Try node_modules path
1122
+ SYNC_SCRIPT="$REPO_ROOT/node_modules/@pumuki/ast-intelligence-hooks/bin/sync-autonomous-orchestrator.sh"
1123
+ fi
1124
+ if [[ -x "$SYNC_SCRIPT" ]]; then
1125
+ if ! "$SYNC_SCRIPT" >/dev/null 2>&1; then
1126
+ echo -e "${YELLOW}⚠️ sync-autonomous-orchestrator.sh fallo, revisa la ruta de la librería.${NC}"
1127
+ fi
1128
+ fi
1129
+
1130
+ echo -e "${GREEN}✅ .AI_EVIDENCE.json updated${NC}"
1131
+ echo -e "${CYAN} Timestamp: $TIMESTAMP${NC}"
1132
+ echo -e "${CYAN} Session: $FEATURE_NAME${NC}"
1133
+ echo ""
1134
+
1135
+ if [[ "$AUTO_MODE" == "false" ]]; then
1136
+ # Show 3 questions as a reflection aid (already auto-filled in .AI_EVIDENCE.json)
1137
+ echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
1138
+ echo -e "${YELLOW}📋 PROTOCOL: 3 Questions (auto-filled in .AI_EVIDENCE.json)${NC}"
1139
+ echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
1140
+ echo ""
1141
+ echo -e "${CYAN}🤔 QUESTION 1: What type of file are you creating/modifying?${NC}"
1142
+ echo " → Describe: Config, Repository, Use Case, Controller, Component?"
1143
+ echo " → Where should it go per Clean Architecture?"
1144
+ echo ""
1145
+ echo -e "${CYAN}🤔 QUESTION 2: Does similar code already exist?${NC}"
1146
+ echo " → Search the codebase for similar patterns"
1147
+ echo " → If yes, where? Can you reuse it?"
1148
+ echo ""
1149
+ echo -e "${CYAN}🤔 QUESTION 3: Does this violate Clean Architecture or SOLID?${NC}"
1150
+ echo " → Check dependency direction (Domain ← Application ← Infrastructure)"
1151
+ echo " → Single Responsibility? Open/Closed? Dependency Inversion?"
1152
+ echo ""
1153
+ echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
1154
+ echo ""
1155
+ echo -e "${GREEN}✅ Ready to edit code!${NC}"
1156
+ echo -e "${YELLOW}ℹ️ The 3 questions have been pre-filled in .AI_EVIDENCE.json for this session.${NC}"
1157
+ echo ""
1158
+ fi
1159
+
1160
+ # Show token status if available
1161
+ TOKEN_STATUS="$REPO_ROOT/.AI_TOKEN_STATUS.txt"
1162
+ if [[ -f "$TOKEN_STATUS" ]]; then
1163
+ cat "$TOKEN_STATUS"
1164
+ echo ""
1165
+ fi
1166
+
1167
+ echo -e "${GREEN}🚀 You can now start editing code${NC}"