@hivemagnus/magnus-server 0.0.0-stage → 1.0.2

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 (356) hide show
  1. package/.env.example +171 -0
  2. package/LICENSE +202 -0
  3. package/README.md +169 -2
  4. package/agent-version.json +1 -0
  5. package/dist/ai/HEARTBEAT.md +56 -0
  6. package/dist/ai/IDENTITY.md +54 -0
  7. package/dist/ai/MEMORY.md +40 -0
  8. package/dist/ai/TOOLS.md +538 -0
  9. package/dist/ai/USER.md +32 -0
  10. package/dist/cli/args.js +62 -0
  11. package/dist/cli/env-catalog.js +131 -0
  12. package/dist/cli/preflight.js +89 -0
  13. package/dist/cli.js +175 -0
  14. package/dist/config/cors.js +16 -0
  15. package/dist/config/env.js +392 -0
  16. package/dist/config/features.js +16 -0
  17. package/dist/config/index.js +3 -0
  18. package/dist/config/listener-security.js +38 -0
  19. package/dist/config/version.js +31 -0
  20. package/dist/db/client.js +44 -0
  21. package/dist/db/generated/prisma/browser.js +18 -0
  22. package/dist/db/generated/prisma/client.js +35 -0
  23. package/dist/db/generated/prisma/commonInputTypes.js +11 -0
  24. package/dist/db/generated/prisma/enums.js +42 -0
  25. package/dist/db/generated/prisma/internal/class.js +50 -0
  26. package/dist/db/generated/prisma/internal/prismaNamespace.js +1598 -0
  27. package/dist/db/generated/prisma/internal/prismaNamespaceBrowser.js +1569 -0
  28. package/dist/db/generated/prisma/models/A2ATask.js +2 -0
  29. package/dist/db/generated/prisma/models/AdmissionFence.js +2 -0
  30. package/dist/db/generated/prisma/models/Agent.js +2 -0
  31. package/dist/db/generated/prisma/models/AiConversation.js +2 -0
  32. package/dist/db/generated/prisma/models/AiDashboard.js +2 -0
  33. package/dist/db/generated/prisma/models/AiMemory.js +2 -0
  34. package/dist/db/generated/prisma/models/AiMessage.js +2 -0
  35. package/dist/db/generated/prisma/models/AiScheduledOp.js +2 -0
  36. package/dist/db/generated/prisma/models/AlertConditionState.js +2 -0
  37. package/dist/db/generated/prisma/models/AlertRule.js +2 -0
  38. package/dist/db/generated/prisma/models/AlertViolation.js +2 -0
  39. package/dist/db/generated/prisma/models/AnomalyEvent.js +2 -0
  40. package/dist/db/generated/prisma/models/ApplicationObservation.js +2 -0
  41. package/dist/db/generated/prisma/models/ArdentInstallation.js +2 -0
  42. package/dist/db/generated/prisma/models/ArdentProfile.js +2 -0
  43. package/dist/db/generated/prisma/models/ArdentScopeEnrollment.js +2 -0
  44. package/dist/db/generated/prisma/models/CommandResourceFence.js +2 -0
  45. package/dist/db/generated/prisma/models/CommandResourceLease.js +2 -0
  46. package/dist/db/generated/prisma/models/CommandResult.js +2 -0
  47. package/dist/db/generated/prisma/models/CronJob.js +2 -0
  48. package/dist/db/generated/prisma/models/CronJobExecution.js +2 -0
  49. package/dist/db/generated/prisma/models/DeployJob.js +2 -0
  50. package/dist/db/generated/prisma/models/DriftScan.js +2 -0
  51. package/dist/db/generated/prisma/models/EngineeringMetricSample.js +2 -0
  52. package/dist/db/generated/prisma/models/EngineeringRecoveryExercise.js +2 -0
  53. package/dist/db/generated/prisma/models/ExecutionRun.js +2 -0
  54. package/dist/db/generated/prisma/models/FilePatchTask.js +2 -0
  55. package/dist/db/generated/prisma/models/GpuSnapshot.js +2 -0
  56. package/dist/db/generated/prisma/models/HealthSnapshot.js +2 -0
  57. package/dist/db/generated/prisma/models/HermesCoreRollout.js +2 -0
  58. package/dist/db/generated/prisma/models/HermesCoreRolloutHost.js +2 -0
  59. package/dist/db/generated/prisma/models/HermesCredentialPool.js +2 -0
  60. package/dist/db/generated/prisma/models/HermesCredentialPoolAttachment.js +2 -0
  61. package/dist/db/generated/prisma/models/HermesCronJob.js +2 -0
  62. package/dist/db/generated/prisma/models/HermesHook.js +2 -0
  63. package/dist/db/generated/prisma/models/HermesInstallation.js +2 -0
  64. package/dist/db/generated/prisma/models/HermesMcpServer.js +2 -0
  65. package/dist/db/generated/prisma/models/HermesMcpServerAttachment.js +2 -0
  66. package/dist/db/generated/prisma/models/HermesMessagingBinding.js +2 -0
  67. package/dist/db/generated/prisma/models/HermesOperationJob.js +2 -0
  68. package/dist/db/generated/prisma/models/HermesPluginInstall.js +2 -0
  69. package/dist/db/generated/prisma/models/HermesProfile.js +2 -0
  70. package/dist/db/generated/prisma/models/HermesProviderConfig.js +2 -0
  71. package/dist/db/generated/prisma/models/HermesRegistryClient.js +2 -0
  72. package/dist/db/generated/prisma/models/HermesRegistryInventory.js +2 -0
  73. package/dist/db/generated/prisma/models/HermesRegistryReceipt.js +2 -0
  74. package/dist/db/generated/prisma/models/HermesRegistryRevision.js +2 -0
  75. package/dist/db/generated/prisma/models/HermesRegistryTarget.js +2 -0
  76. package/dist/db/generated/prisma/models/HermesRole.js +2 -0
  77. package/dist/db/generated/prisma/models/HermesRoleApplication.js +2 -0
  78. package/dist/db/generated/prisma/models/HermesSkillAssignment.js +2 -0
  79. package/dist/db/generated/prisma/models/HermesToolsetPolicy.js +2 -0
  80. package/dist/db/generated/prisma/models/IncidentLesson.js +2 -0
  81. package/dist/db/generated/prisma/models/LibraryCategory.js +2 -0
  82. package/dist/db/generated/prisma/models/LibraryFile.js +2 -0
  83. package/dist/db/generated/prisma/models/LogEntry.js +2 -0
  84. package/dist/db/generated/prisma/models/MagnusReleaseCanary.js +2 -0
  85. package/dist/db/generated/prisma/models/MaintenanceWindow.js +2 -0
  86. package/dist/db/generated/prisma/models/ManagedCronDefinition.js +2 -0
  87. package/dist/db/generated/prisma/models/ManagedCronOccurrence.js +2 -0
  88. package/dist/db/generated/prisma/models/Node.js +2 -0
  89. package/dist/db/generated/prisma/models/Notification.js +2 -0
  90. package/dist/db/generated/prisma/models/NotificationPreference.js +2 -0
  91. package/dist/db/generated/prisma/models/RemediationExecution.js +2 -0
  92. package/dist/db/generated/prisma/models/RemediationRule.js +2 -0
  93. package/dist/db/generated/prisma/models/RemoteCommand.js +2 -0
  94. package/dist/db/generated/prisma/models/ResourceDrainParticipant.js +2 -0
  95. package/dist/db/generated/prisma/models/ResourceEnrollment.js +2 -0
  96. package/dist/db/generated/prisma/models/ResourceParticipantWork.js +2 -0
  97. package/dist/db/generated/prisma/models/Role.js +2 -0
  98. package/dist/db/generated/prisma/models/RoleApplication.js +2 -0
  99. package/dist/db/generated/prisma/models/RolloutTarget.js +2 -0
  100. package/dist/db/generated/prisma/models/SecurityExportOutbox.js +2 -0
  101. package/dist/db/generated/prisma/models/SecurityFusionAction.js +2 -0
  102. package/dist/db/generated/prisma/models/SecurityFusionActionApproval.js +2 -0
  103. package/dist/db/generated/prisma/models/SecurityFusionCase.js +2 -0
  104. package/dist/db/generated/prisma/models/SecurityFusionCaseActivity.js +2 -0
  105. package/dist/db/generated/prisma/models/SecurityFusionCaseEntity.js +2 -0
  106. package/dist/db/generated/prisma/models/SecurityFusionCaseEvent.js +2 -0
  107. package/dist/db/generated/prisma/models/SecurityFusionCaseRelationship.js +2 -0
  108. package/dist/db/generated/prisma/models/SecurityFusionCollector.js +2 -0
  109. package/dist/db/generated/prisma/models/SecurityFusionCommandOutbox.js +2 -0
  110. package/dist/db/generated/prisma/models/SecurityFusionDeletionReceipt.js +2 -0
  111. package/dist/db/generated/prisma/models/SecurityFusionDelivery.js +2 -0
  112. package/dist/db/generated/prisma/models/SecurityFusionEntity.js +2 -0
  113. package/dist/db/generated/prisma/models/SecurityFusionEntityAlias.js +2 -0
  114. package/dist/db/generated/prisma/models/SecurityFusionEvent.js +2 -0
  115. package/dist/db/generated/prisma/models/SecurityFusionEventEntity.js +2 -0
  116. package/dist/db/generated/prisma/models/SecurityFusionEvidence.js +2 -0
  117. package/dist/db/generated/prisma/models/SecurityFusionInvestigation.js +2 -0
  118. package/dist/db/generated/prisma/models/SecurityFusionNonce.js +2 -0
  119. package/dist/db/generated/prisma/models/SecurityFusionReceiptChainHead.js +2 -0
  120. package/dist/db/generated/prisma/models/SecurityFusionRuleVersion.js +2 -0
  121. package/dist/db/generated/prisma/models/SecurityFusionSourceState.js +2 -0
  122. package/dist/db/generated/prisma/models/SecurityServiceState.js +2 -0
  123. package/dist/db/generated/prisma/models/SharedResourceFence.js +2 -0
  124. package/dist/db/generated/prisma/models/SharedResourceLease.js +2 -0
  125. package/dist/db/generated/prisma/models/SharedResourceLeaseResource.js +2 -0
  126. package/dist/db/generated/prisma/models/SkillInstallation.js +2 -0
  127. package/dist/db/generated/prisma/models/SkillRegistryEntry.js +2 -0
  128. package/dist/db/generated/prisma/models/SkillTag.js +2 -0
  129. package/dist/db/generated/prisma/models/SkillVersion.js +2 -0
  130. package/dist/db/generated/prisma/models/TeamApprovalDecision.js +2 -0
  131. package/dist/db/generated/prisma/models/User.js +2 -0
  132. package/dist/db/generated/prisma/models/WebhookDelivery.js +2 -0
  133. package/dist/db/generated/prisma/models/WebhookEndpoint.js +2 -0
  134. package/dist/db/generated/prisma/models.js +2 -0
  135. package/dist/db/index.js +3 -0
  136. package/dist/db/read-retry.js +35 -0
  137. package/dist/db/seed.js +348 -0
  138. package/dist/index.js +162 -0
  139. package/dist/middleware/auth.js +361 -0
  140. package/dist/middleware/body-limit.js +14 -0
  141. package/dist/middleware/correlation-id.js +13 -0
  142. package/dist/middleware/error-handler.js +28 -0
  143. package/dist/middleware/iam-token.js +21 -0
  144. package/dist/middleware/rate-limit.js +78 -0
  145. package/dist/middleware/request-logger.js +85 -0
  146. package/dist/middleware/security-headers.js +23 -0
  147. package/dist/middleware/tracing.js +120 -0
  148. package/dist/middleware/user-status.js +33 -0
  149. package/dist/openapi/spec.js +2656 -0
  150. package/dist/queue/redis.js +20 -0
  151. package/dist/queue/requeue.js +27 -0
  152. package/dist/queue/workers.js +342 -0
  153. package/dist/routes/ai-schedule.js +73 -0
  154. package/dist/routes/alerts.js +79 -0
  155. package/dist/routes/anomalies.js +101 -0
  156. package/dist/routes/ardent.js +82 -0
  157. package/dist/routes/assistant.js +417 -0
  158. package/dist/routes/audit.js +138 -0
  159. package/dist/routes/codex.js +65 -0
  160. package/dist/routes/commands.js +226 -0
  161. package/dist/routes/credentials.js +58 -0
  162. package/dist/routes/cron.js +292 -0
  163. package/dist/routes/dependencies.js +49 -0
  164. package/dist/routes/deploy.js +18 -0
  165. package/dist/routes/discovery.js +168 -0
  166. package/dist/routes/docs.js +24 -0
  167. package/dist/routes/drift.js +126 -0
  168. package/dist/routes/events-stream.js +87 -0
  169. package/dist/routes/files.js +214 -0
  170. package/dist/routes/fleet-checks.js +21 -0
  171. package/dist/routes/health.js +133 -0
  172. package/dist/routes/heartbeat.js +545 -0
  173. package/dist/routes/hermes-registry.js +278 -0
  174. package/dist/routes/hermes.js +61 -0
  175. package/dist/routes/library.js +402 -0
  176. package/dist/routes/logs.js +172 -0
  177. package/dist/routes/managed-cron.js +23 -0
  178. package/dist/routes/mcp.js +39 -0
  179. package/dist/routes/metrics.js +187 -0
  180. package/dist/routes/nodes.js +951 -0
  181. package/dist/routes/notifications.js +130 -0
  182. package/dist/routes/ops.js +258 -0
  183. package/dist/routes/orchestra.js +430 -0
  184. package/dist/routes/predictions.js +23 -0
  185. package/dist/routes/profiling.js +68 -0
  186. package/dist/routes/quick-command.js +72 -0
  187. package/dist/routes/remediation.js +111 -0
  188. package/dist/routes/resource-leases.js +297 -0
  189. package/dist/routes/roles.js +370 -0
  190. package/dist/routes/rollout.js +124 -0
  191. package/dist/routes/scan.js +116 -0
  192. package/dist/routes/security-backup.js +58 -0
  193. package/dist/routes/security-fusion.js +605 -0
  194. package/dist/routes/security-integration.js +71 -0
  195. package/dist/routes/self-update.js +129 -0
  196. package/dist/routes/shared-resource-leases.js +78 -0
  197. package/dist/routes/skills.js +485 -0
  198. package/dist/routes/stats.js +96 -0
  199. package/dist/routes/team-approvals.js +23 -0
  200. package/dist/routes/terminal-ticket.js +15 -0
  201. package/dist/routes/terminal.js +17 -0
  202. package/dist/routes/topology.js +37 -0
  203. package/dist/routes/users.js +219 -0
  204. package/dist/routes/webhooks.js +137 -0
  205. package/dist/schemas/command-reconciliation.js +14 -0
  206. package/dist/schemas/commands.js +51 -0
  207. package/dist/schemas/cron.js +33 -0
  208. package/dist/schemas/deploy.js +20 -0
  209. package/dist/schemas/files.js +14 -0
  210. package/dist/schemas/hermes.js +69 -0
  211. package/dist/schemas/index.js +11 -0
  212. package/dist/schemas/library.js +48 -0
  213. package/dist/schemas/logs.js +35 -0
  214. package/dist/schemas/managed-cron.js +127 -0
  215. package/dist/schemas/node-responses.js +87 -0
  216. package/dist/schemas/node-session-receipt.js +84 -0
  217. package/dist/schemas/nodes.js +36 -0
  218. package/dist/schemas/orchestra.js +118 -0
  219. package/dist/schemas/resource-participant-reconciliation.js +99 -0
  220. package/dist/schemas/roles.js +54 -0
  221. package/dist/schemas/shared-resource-lease.js +132 -0
  222. package/dist/schemas/skills.js +44 -0
  223. package/dist/schemas/users.js +28 -0
  224. package/dist/scripts/run-disposable-recovery-exercise.js +39 -0
  225. package/dist/sdk/index.js +2 -0
  226. package/dist/sdk/node-api-client.js +474 -0
  227. package/dist/security/approval-assertion.js +66 -0
  228. package/dist/security/command-completion-registry.js +35 -0
  229. package/dist/security/command-request.js +12 -0
  230. package/dist/security/command-result-signature.js +34 -0
  231. package/dist/security/managed-http.js +45 -0
  232. package/dist/security/native-completion-registry.js +44 -0
  233. package/dist/server.js +178 -0
  234. package/dist/services/admission-fence.js +71 -0
  235. package/dist/services/ai-assistant-service.js +2523 -0
  236. package/dist/services/ai-correlation-service.js +225 -0
  237. package/dist/services/ai-export-service.js +141 -0
  238. package/dist/services/ai-insights-service.js +219 -0
  239. package/dist/services/ai-schedule-service.js +145 -0
  240. package/dist/services/ai-scratchpad-service.js +222 -0
  241. package/dist/services/ai-tools.js +1343 -0
  242. package/dist/services/alert-service.js +313 -0
  243. package/dist/services/anomaly-detector.js +419 -0
  244. package/dist/services/api-local-work.js +79 -0
  245. package/dist/services/application-probes.js +309 -0
  246. package/dist/services/ardent-scope-enrollment.js +123 -0
  247. package/dist/services/ardent-service.js +86 -0
  248. package/dist/services/audit-service.js +308 -0
  249. package/dist/services/codex-service.js +209 -0
  250. package/dist/services/command-lifecycle.js +60 -0
  251. package/dist/services/command-resource-leases.js +305 -0
  252. package/dist/services/control-host-node.js +242 -0
  253. package/dist/services/credential-rotation-service.js +294 -0
  254. package/dist/services/dependency-service.js +145 -0
  255. package/dist/services/disposable-recovery-runner.js +595 -0
  256. package/dist/services/docker-group-health.js +121 -0
  257. package/dist/services/engineering-assurance.js +492 -0
  258. package/dist/services/engineering-evidence-ledger.js +94 -0
  259. package/dist/services/engineering-metric-evidence.js +26 -0
  260. package/dist/services/event-bus.js +227 -0
  261. package/dist/services/fleet-check-log.js +102 -0
  262. package/dist/services/forecast-service.js +154 -0
  263. package/dist/services/health-retention.js +175 -0
  264. package/dist/services/heartbeat-stream.js +63 -0
  265. package/dist/services/hermes-catalog-import.js +62 -0
  266. package/dist/services/hermes-cli-parser.js +671 -0
  267. package/dist/services/hermes-core-rollout.js +90 -0
  268. package/dist/services/hermes-dashboard-endpoint.js +13 -0
  269. package/dist/services/hermes-plugin-source.js +149 -0
  270. package/dist/services/hermes-registry-service.js +258 -0
  271. package/dist/services/hermes-service.js +566 -0
  272. package/dist/services/hivemem-client.js +85 -0
  273. package/dist/services/host-local-writer.js +82 -0
  274. package/dist/services/incident-evidence.js +110 -0
  275. package/dist/services/incident-lessons.js +186 -0
  276. package/dist/services/llm-providers/anthropic.js +143 -0
  277. package/dist/services/llm-providers/index.js +7 -0
  278. package/dist/services/llm-providers/llama-cpp.js +237 -0
  279. package/dist/services/llm-providers/ollama.js +292 -0
  280. package/dist/services/llm-providers/openai-compatible.js +200 -0
  281. package/dist/services/llm-providers/provider-error.js +36 -0
  282. package/dist/services/llm-providers/registry.js +45 -0
  283. package/dist/services/llm-providers/types.js +2 -0
  284. package/dist/services/log-emitter.js +19 -0
  285. package/dist/services/log-retention.js +66 -0
  286. package/dist/services/maintenance-service.js +29 -0
  287. package/dist/services/managed-cron.js +493 -0
  288. package/dist/services/managed-ssh-admission.js +68 -0
  289. package/dist/services/monitor-pulse.js +10 -0
  290. package/dist/services/node-issue-monitor.js +71 -0
  291. package/dist/services/node-service.js +35 -0
  292. package/dist/services/notification-retention.js +51 -0
  293. package/dist/services/notification-service.js +94 -0
  294. package/dist/services/operational-observations.js +112 -0
  295. package/dist/services/operations-config.js +67 -0
  296. package/dist/services/ops-service.js +491 -0
  297. package/dist/services/orchestra-extras-service.js +370 -0
  298. package/dist/services/orchestra-resources-service.js +530 -0
  299. package/dist/services/os-scheduler-writer.js +73 -0
  300. package/dist/services/password-service.js +22 -0
  301. package/dist/services/prediction-service.js +284 -0
  302. package/dist/services/recovery-exercises.js +682 -0
  303. package/dist/services/remediation-service.js +325 -0
  304. package/dist/services/resource-enrollment.js +433 -0
  305. package/dist/services/resource-node-lock.js +18 -0
  306. package/dist/services/resource-participant-work.js +302 -0
  307. package/dist/services/role-service.js +388 -0
  308. package/dist/services/rollout-service.js +440 -0
  309. package/dist/services/security-export.js +185 -0
  310. package/dist/services/security-fusion-actions.js +22 -0
  311. package/dist/services/security-fusion-approvals.js +83 -0
  312. package/dist/services/security-fusion-evidence-integrity.js +144 -0
  313. package/dist/services/security-fusion-ingest.js +101 -0
  314. package/dist/services/security-fusion-outbox.js +185 -0
  315. package/dist/services/security-fusion-receipt-chain.js +134 -0
  316. package/dist/services/security-fusion-service.js +751 -0
  317. package/dist/services/security-fusion-verification.js +50 -0
  318. package/dist/services/self-update-service.js +200 -0
  319. package/dist/services/shared-resource-leases.js +413 -0
  320. package/dist/services/skill-publication-bundle.js +90 -0
  321. package/dist/services/skill-publication-settlement.js +36 -0
  322. package/dist/services/skill-publication-state.js +40 -0
  323. package/dist/services/skill-registry-service.js +327 -0
  324. package/dist/services/skill-service.js +148 -0
  325. package/dist/services/ssh-exec-service.js +139 -0
  326. package/dist/services/ssh-host-verification.js +27 -0
  327. package/dist/services/tailscale-node-key-service.js +75 -0
  328. package/dist/services/team-approvals.js +118 -0
  329. package/dist/services/topology-service.js +138 -0
  330. package/dist/services/transfer-authority-snapshot.js +151 -0
  331. package/dist/services/webhook-credential-migration.js +97 -0
  332. package/dist/services/webhook-security.js +242 -0
  333. package/dist/services/webhook-service.js +494 -0
  334. package/dist/types/auth.js +3 -0
  335. package/dist/types/index.js +3 -0
  336. package/dist/types/library.js +3 -0
  337. package/dist/types/notifications.js +3 -0
  338. package/dist/types/roles.js +3 -0
  339. package/dist/types/shared-contracts.js +3 -0
  340. package/dist/utils/env.js +141 -0
  341. package/dist/utils/heartbeat-peer.js +19 -0
  342. package/dist/utils/idempotency.js +45 -0
  343. package/dist/utils/ip.js +25 -0
  344. package/dist/utils/json-fields.js +94 -0
  345. package/dist/utils/local-tls.js +39 -0
  346. package/dist/utils/magnus-home.js +15 -0
  347. package/dist/utils/node-url.js +59 -0
  348. package/dist/utils/redact.js +26 -0
  349. package/dist/utils/safe-equal.js +13 -0
  350. package/dist/utils/sentinel-nodes.js +16 -0
  351. package/dist/utils/validate.js +41 -0
  352. package/package.json +119 -4
  353. package/prisma/migrations/20261031000000_baseline/migration.sql +2935 -0
  354. package/prisma/migrations/migration_lock.toml +3 -0
  355. package/prisma/schema.prisma +2185 -0
  356. package/prisma.config.ts +14 -0
@@ -0,0 +1,545 @@
1
+ import { enqueueSecurityObservations, securityFusionLocal } from '../services/security-export.js';
2
+ import { Hono } from 'hono';
3
+ import { z } from 'zod';
4
+ import { nanoid } from 'nanoid';
5
+ import { getDb } from '../db/index.js';
6
+ import { promises as dns } from 'node:dns';
7
+ import { isIP } from 'node:net';
8
+ import { HttpError } from '../middleware/error-handler.js';
9
+ import { createNotificationInTransaction } from '../services/notification-service.js';
10
+ import { matchingMaintenance } from '../services/maintenance-service.js';
11
+ import { evaluateHealth } from '../services/alert-service.js';
12
+ import { evaluateAnomalies } from '../services/anomaly-detector.js';
13
+ import { evaluatePredictionsForHeartbeat } from '../services/prediction-service.js';
14
+ import { recordManagedHermesState } from '../services/hermes-service.js';
15
+ import { NODE_MANAGER_SECRET } from '../utils/env.js';
16
+ import { getCredentialStatus, secretFingerprint, verifyWithRotation, } from '../services/credential-rotation-service.js';
17
+ import { eventBus, EventChannels } from '../services/event-bus.js';
18
+ import { parseBody, safeParse } from '../utils/validate.js';
19
+ import { heartbeatHealthSchema } from '../schemas/node-responses.js';
20
+ import { normalizeIpAddress, isLoopbackOrUnroutableIp, isUsableNodeIp } from '../utils/ip.js';
21
+ import { AGENT_VERSION } from '../config/version.js';
22
+ import { getHeartbeatStreamStats } from '../services/heartbeat-stream.js';
23
+ import { ingestMagnusServiceHealth } from '../services/security-fusion-service.js';
24
+ import { deriveAgentCertificateHostname, selectAgentEndpointHostname, verifyAgentCertificateHostname, } from '../utils/node-url.js';
25
+ import { heartbeatPeerAddress } from '../utils/heartbeat-peer.js';
26
+ import { authMiddleware } from '../middleware/auth.js';
27
+ export const heartbeatRouter = new Hono();
28
+ const DEV_NODE_NAME = 'dev-local';
29
+ /**
30
+ * Envelope validation for agent heartbeats. Deliberately lenient: unknown
31
+ * fields pass through (agents send uptime/status and newer agents may add
32
+ * more), and only the types of fields this route reads are enforced. The
33
+ * health payload is validated in detail later by heartbeatHealthSchema.
34
+ */
35
+ export const heartbeatRequestSchema = z.object({
36
+ agentVersion: z.string().max(64).optional(),
37
+ fqdn: z.string().max(253).nullable().optional(),
38
+ platform: z.string().max(64).optional(),
39
+ arch: z.string().max(64).optional(),
40
+ health: z.object({
41
+ // No count cap: container hosts report one veth per container, and the
42
+ // request body limit already bounds the array.
43
+ network: z.array(z.object({ ipAddress: z.string().max(64).nullable().optional() }).passthrough())
44
+ .nullable().optional(),
45
+ }).passthrough().nullable().optional(),
46
+ hermes: z.unknown().optional(),
47
+ securityCapabilities: z.object({
48
+ isolation: z.boolean().optional(),
49
+ serviceRestart: z.boolean().optional(),
50
+ }).passthrough().nullable().optional(),
51
+ }).passthrough();
52
+ function isHermesNodeState(value) {
53
+ if (!value || typeof value !== 'object')
54
+ return false;
55
+ const state = value;
56
+ return typeof state['installed'] === 'boolean'
57
+ && typeof state['status'] === 'string'
58
+ && typeof state['hermesHome'] === 'string'
59
+ && typeof state['hermesBin'] === 'string'
60
+ && Boolean(state['configuration'] && typeof state['configuration'] === 'object');
61
+ }
62
+ function uniqueNonEmpty(values) {
63
+ return [...new Set(values.map((v) => normalizeIpAddress(v)).filter(Boolean))];
64
+ }
65
+ function getHealthNetworkIps(body) {
66
+ return uniqueNonEmpty((body.health?.network ?? [])
67
+ .map((n) => n.ipAddress ?? '')
68
+ .filter((ip) => isUsableNodeIp(ip)));
69
+ }
70
+ async function tryResolveFqdnToIp(fqdn) {
71
+ try {
72
+ const records = await dns.lookup(fqdn, { all: true, verbatim: true });
73
+ const ip = records.map((r) => normalizeIpAddress(r.address)).find((candidate) => isUsableNodeIp(candidate));
74
+ return ip ?? null;
75
+ }
76
+ catch {
77
+ return null;
78
+ }
79
+ }
80
+ async function tryResolveCertificateHostnameToIp(hostname) {
81
+ try {
82
+ const records = await dns.lookup(hostname, { all: true, verbatim: true });
83
+ return records
84
+ .map((record) => normalizeIpAddress(record.address))
85
+ .find((candidate) => isIP(candidate) !== 0) ?? null;
86
+ }
87
+ catch {
88
+ return null;
89
+ }
90
+ }
91
+ export function selectPreferredNodeIp(options) {
92
+ const candidates = uniqueNonEmpty([
93
+ options.fqdnResolvedIp ?? '',
94
+ options.callerIp,
95
+ ...options.healthNetworkIps,
96
+ ]);
97
+ const best = candidates.find((candidate) => isUsableNodeIp(candidate));
98
+ return best ?? null;
99
+ }
100
+ export function shouldRepairNodeIp(currentIp) {
101
+ return isLoopbackOrUnroutableIp(currentIp);
102
+ }
103
+ /**
104
+ * Try to resolve FQDN server-side via reverse DNS lookup.
105
+ * Returns the FQDN if found, otherwise null.
106
+ */
107
+ async function tryReverseDns(ipAddress) {
108
+ try {
109
+ // Skip loopback / non-routable addresses
110
+ if (!isUsableNodeIp(ipAddress))
111
+ return null;
112
+ const hostnames = await dns.reverse(ipAddress);
113
+ const fqdn = hostnames.find((h) => h.includes('.'));
114
+ return fqdn ?? null;
115
+ }
116
+ catch {
117
+ return null;
118
+ }
119
+ }
120
+ /**
121
+ * Validate the shared secret from the x-node-token header.
122
+ */
123
+ function validateSharedSecret(token) {
124
+ if (!NODE_MANAGER_SECRET) {
125
+ throw new HttpError(500, 'CONFIG_ERROR', 'NODE_MANAGER_SECRET not configured on control-api');
126
+ }
127
+ if (!verifyWithRotation('NODE_MANAGER_SECRET', NODE_MANAGER_SECRET, token)) {
128
+ throw new HttpError(401, 'INVALID_CREDENTIALS', 'Invalid node token');
129
+ }
130
+ return secretFingerprint(token);
131
+ }
132
+ function parseMetadata(raw) {
133
+ try {
134
+ const parsed = JSON.parse(raw);
135
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
136
+ ? parsed
137
+ : {};
138
+ }
139
+ catch {
140
+ return {};
141
+ }
142
+ }
143
+ async function recordNodeSecretFingerprint(nodeId, observedFingerprint) {
144
+ const db = getDb();
145
+ const row = await db.node.findUnique({ where: { id: nodeId }, select: { metadata: true } });
146
+ const metadata = parseMetadata(row?.metadata ?? '');
147
+ const status = getCredentialStatus('NODE_MANAGER_SECRET', NODE_MANAGER_SECRET);
148
+ const state = observedFingerprint === status.activeFingerprint
149
+ ? 'active'
150
+ : observedFingerprint === status.previousFingerprint
151
+ ? 'previous'
152
+ : 'unknown';
153
+ metadata['nodeManagerSecretFingerprint'] = observedFingerprint;
154
+ metadata['nodeManagerSecretState'] = state;
155
+ metadata['nodeManagerSecretObservedAt'] = new Date().toISOString();
156
+ await db.node.update({
157
+ where: { id: nodeId },
158
+ data: { metadata: JSON.stringify(metadata) },
159
+ });
160
+ }
161
+ /**
162
+ * Resolve the calling node by its FQDN / hostname.
163
+ *
164
+ * 1. Look up existing node by FQDN match on hostname or name
165
+ * 2. If not found and this is localhost, find/create the dev-local node
166
+ * 3. Otherwise return null — the heartbeat handler will still accept the
167
+ * heartbeat but won't be able to update a specific node.
168
+ */
169
+ async function resolveNodeByFqdn(fqdn, callerIp, candidateIps) {
170
+ const db = getDb();
171
+ // ── 1. Match by FQDN against hostname or name ──
172
+ if (fqdn) {
173
+ const shortName = fqdn.split('.')[0] || fqdn;
174
+ // Optional DNS suffix (for example a private overlay network domain) under
175
+ // which agents may report their FQDN while inventory stores a short name.
176
+ const dnsSuffix = process.env['MAGNUS_NODE_DNS_SUFFIX']?.trim().replace(/^\.+|\.+$/g, '');
177
+ const aliasHostnames = [
178
+ fqdn,
179
+ shortName,
180
+ ...(dnsSuffix ? [`${shortName}.${dnsSuffix}`] : []),
181
+ ];
182
+ const byHostname = await db.node.findFirst({
183
+ where: {
184
+ status: { not: 'DECOMMISSIONED' },
185
+ OR: [
186
+ { hostname: { in: aliasHostnames } },
187
+ { name: { in: aliasHostnames } },
188
+ { name: shortName },
189
+ ],
190
+ },
191
+ select: { id: true, name: true, ipAddress: true, hostname: true, status: true },
192
+ orderBy: { updatedAt: 'desc' },
193
+ });
194
+ if (byHostname) {
195
+ return { nodeId: byHostname.id, existing: byHostname };
196
+ }
197
+ }
198
+ // ── 2. Match by IP address ──
199
+ for (const ip of uniqueNonEmpty([callerIp, ...candidateIps])) {
200
+ if (!isUsableNodeIp(ip))
201
+ continue;
202
+ const byIp = await db.node.findFirst({
203
+ where: { ipAddress: ip },
204
+ select: { id: true, name: true, ipAddress: true, hostname: true, status: true },
205
+ });
206
+ if (byIp) {
207
+ return { nodeId: byIp.id, existing: byIp };
208
+ }
209
+ }
210
+ // ── 3. Dev-local fallback (only for true dev: local connection + no FQDN) ──
211
+ const isLocal = !callerIp || callerIp === '127.0.0.1' || callerIp === '::1' || callerIp === '0.0.0.0';
212
+ if (isLocal && !fqdn) {
213
+ let devNode = await db.node.findUnique({
214
+ where: { name: DEV_NODE_NAME },
215
+ select: { id: true, name: true, ipAddress: true, hostname: true, status: true },
216
+ });
217
+ if (!devNode) {
218
+ const id = nanoid();
219
+ devNode = await db.node.create({
220
+ data: {
221
+ id,
222
+ name: DEV_NODE_NAME,
223
+ hostname: 'localhost',
224
+ ipAddress: '127.0.0.1',
225
+ port: 4100,
226
+ os: process.platform,
227
+ arch: process.arch,
228
+ token: `local-${id}`,
229
+ status: 'ONLINE',
230
+ },
231
+ select: { id: true, name: true, ipAddress: true, hostname: true, status: true },
232
+ });
233
+ await db.agent.create({
234
+ data: { nodeId: id, version: 'dev', status: 'RUNNING', capabilities: '[]' },
235
+ });
236
+ console.log(`[Dev] Auto-registered local node: ${id}`);
237
+ }
238
+ return { nodeId: devNode.id, existing: devNode };
239
+ }
240
+ return null;
241
+ }
242
+ // ── Receive heartbeat from node agents ──
243
+ heartbeatRouter.post('/', async (c) => {
244
+ const db = getDb();
245
+ const token = c.req.header('x-node-token');
246
+ if (!token)
247
+ throw new HttpError(401, 'INVALID_CREDENTIALS', 'Missing x-node-token');
248
+ // Validate the shared secret
249
+ const observedNodeManagerSecretFingerprint = validateSharedSecret(token);
250
+ const body = await parseBody(c, heartbeatRequestSchema);
251
+ // Determine FQDN — prefer agent-reported, then try reverse DNS from caller IP
252
+ let fqdn = body.fqdn ?? null;
253
+ // The direct socket peer is authoritative. Proxy metadata is only a fallback
254
+ // when the Node adapter does not expose connection data.
255
+ const callerIp = heartbeatPeerAddress(c);
256
+ const healthNetworkIps = getHealthNetworkIps(body);
257
+ const fqdnResolvedIp = fqdn ? await tryResolveFqdnToIp(fqdn) : null;
258
+ const preferredNodeIp = selectPreferredNodeIp({ callerIp, healthNetworkIps, fqdnResolvedIp });
259
+ const ipCandidates = uniqueNonEmpty([callerIp, ...healthNetworkIps, fqdnResolvedIp ?? '']);
260
+ const agentCertificateHostname = deriveAgentCertificateHostname(fqdn, process.env['AGENT_TLS_DOMAIN_SUFFIX']);
261
+ const agentCertificateIp = agentCertificateHostname
262
+ ? await tryResolveCertificateHostnameToIp(agentCertificateHostname)
263
+ : null;
264
+ const verifiedAgentCertificateHostname = verifyAgentCertificateHostname(agentCertificateHostname, agentCertificateIp, ipCandidates);
265
+ let resolved = await resolveNodeByFqdn(fqdn, callerIp, ipCandidates);
266
+ // ── Auto-register unknown agents ──────────────────────────────────────────
267
+ // Token is valid but node isn't in our DB — create a record automatically.
268
+ // This handles migrated agents arriving at a new control node.
269
+ if (!resolved && (fqdn || callerIp)) {
270
+ const nodeName = fqdn || preferredNodeIp || callerIp;
271
+ const nodeHostname = fqdn || nodeName;
272
+ const nodeIp = verifiedAgentCertificateHostname ?? preferredNodeIp ?? '0.0.0.0';
273
+ try {
274
+ const id = nanoid();
275
+ const newNode = await db.node.create({
276
+ data: {
277
+ id,
278
+ name: nodeName,
279
+ hostname: nodeHostname,
280
+ ipAddress: nodeIp,
281
+ port: 4100,
282
+ os: typeof body.platform === 'string' ? body.platform.slice(0, 32) : 'linux',
283
+ arch: typeof body.arch === 'string' ? body.arch.slice(0, 32) : 'unknown',
284
+ token: `auto-${id}`,
285
+ status: 'ONLINE',
286
+ lastHeartbeatAt: new Date(),
287
+ },
288
+ select: { id: true, name: true, ipAddress: true, hostname: true, status: true },
289
+ });
290
+ await db.agent.create({
291
+ data: {
292
+ nodeId: id,
293
+ version: body.agentVersion ?? 'unknown',
294
+ status: 'RUNNING',
295
+ capabilities: '[]',
296
+ },
297
+ });
298
+ console.log(`[Heartbeat] Auto-registered node: ${nodeName} (${nodeIp})`);
299
+ resolved = { nodeId: id, existing: newNode };
300
+ }
301
+ catch {
302
+ // Name collision (unique constraint) — find the existing node
303
+ const existing = await db.node.findUnique({
304
+ where: { name: nodeName },
305
+ select: { id: true, name: true, ipAddress: true, hostname: true, status: true },
306
+ });
307
+ if (existing) {
308
+ resolved = { nodeId: existing.id, existing };
309
+ }
310
+ }
311
+ }
312
+ if (!resolved) {
313
+ // Secret is valid but we truly can't identify the node — accept silently
314
+ return c.json({ success: true, data: { accepted: true } });
315
+ }
316
+ const { nodeId, existing } = resolved;
317
+ if (body.securityCapabilities && existing) {
318
+ const currentNode = await db.node.findUnique({ where: { id: nodeId }, select: { metadata: true } });
319
+ let metadata = {};
320
+ try {
321
+ metadata = JSON.parse(currentNode?.metadata ?? '{}');
322
+ }
323
+ catch { /* preserve an empty metadata object */ }
324
+ await db.node.update({
325
+ where: { id: nodeId },
326
+ data: { metadata: JSON.stringify({
327
+ ...metadata,
328
+ securityIsolationCapable: body.securityCapabilities.isolation === true,
329
+ securityServiceRestartCapable: body.securityCapabilities.serviceRestart === true,
330
+ }) },
331
+ });
332
+ }
333
+ if (!fqdn && existing) {
334
+ // If the node's hostname already contains a dot it's likely an FQDN — skip lookup
335
+ if (!existing.hostname.includes('.')) {
336
+ fqdn = await tryReverseDns(existing.ipAddress);
337
+ }
338
+ }
339
+ const repairedAgentAddress = existing && existing.name !== DEV_NODE_NAME
340
+ ? verifiedAgentCertificateHostname ??
341
+ (shouldRepairNodeIp(existing.ipAddress) ? preferredNodeIp : null)
342
+ : null;
343
+ // Update node heartbeat
344
+ // When FQDN is available, update hostname and name.
345
+ // Prefer a DNS identity whose resolved IP was present in this authenticated
346
+ // heartbeat. This keeps hostname verification working even when a legacy
347
+ // node record stored a LAN IP that is not present in the agent certificate.
348
+ const recoveryMaintenance = existing ? await matchingMaintenance(existing.id) : null;
349
+ let recoveredFrom = null;
350
+ await db.$transaction(async (tx) => {
351
+ const current = existing ? await tx.node.findUnique({ where: { id: existing.id }, select: { status: true } }) : null;
352
+ if (fqdn && existing) {
353
+ const endpointHostname = selectAgentEndpointHostname(verifiedAgentCertificateHostname, fqdn, existing.hostname, existing.ipAddress);
354
+ const autoGenerated = existing.name === DEV_NODE_NAME ||
355
+ existing.name.startsWith('node-') ||
356
+ /^\d+\.\d+\.\d+\.\d+/.test(existing.name) ||
357
+ existing.name === fqdn;
358
+ await tx.node.update({
359
+ where: { id: existing.id },
360
+ data: {
361
+ lastHeartbeatAt: new Date(),
362
+ status: 'ONLINE',
363
+ hostname: endpointHostname,
364
+ ...(typeof body.platform === 'string' ? { os: body.platform.slice(0, 32) } : {}),
365
+ ...(typeof body.arch === 'string' ? { arch: body.arch.slice(0, 32) } : {}),
366
+ ...(repairedAgentAddress ? { ipAddress: repairedAgentAddress } : {}),
367
+ ...(autoGenerated ? { name: fqdn } : {}),
368
+ },
369
+ });
370
+ }
371
+ else if (existing) {
372
+ await tx.node.update({
373
+ where: { id: existing.id },
374
+ data: {
375
+ lastHeartbeatAt: new Date(),
376
+ status: 'ONLINE',
377
+ ...(repairedAgentAddress ? { ipAddress: repairedAgentAddress } : {}),
378
+ },
379
+ });
380
+ }
381
+ else {
382
+ // Fallback: node verified by JWT but not found by id (edge case)
383
+ await tx.node.updateMany({
384
+ where: { id: nodeId },
385
+ data: {
386
+ lastHeartbeatAt: new Date(),
387
+ status: 'ONLINE',
388
+ },
389
+ });
390
+ }
391
+ // ── Recovery notification: node was OFFLINE/DEGRADED and just came back ──
392
+ if (existing && (current?.status === 'OFFLINE' || current?.status === 'DEGRADED')) {
393
+ recoveredFrom = current.status;
394
+ await createNotificationInTransaction(tx, {
395
+ type: 'node.recovered',
396
+ severity: 'info',
397
+ title: `${existing.name} is back online`,
398
+ message: `Node ${existing.name} (${existing.hostname}) recovered from ${current.status} status.`,
399
+ nodeId: existing.id,
400
+ nodeName: existing.name,
401
+ metadata: { previousStatus: current.status },
402
+ }, recoveryMaintenance?.id ?? null);
403
+ }
404
+ });
405
+ if (existing && recoveredFrom) {
406
+ void eventBus.publish(EventChannels.NODE_STATUS, {
407
+ type: 'node.recovered', nodeId: existing.id, nodeName: existing.name,
408
+ previousStatus: recoveredFrom, currentStatus: 'ONLINE',
409
+ timestamp: new Date().toISOString(), source: 'control-api',
410
+ });
411
+ }
412
+ // Update agent check-in
413
+ if (body.agentVersion) {
414
+ await db.agent.updateMany({
415
+ where: { nodeId },
416
+ data: {
417
+ lastCheckIn: new Date(),
418
+ version: body.agentVersion,
419
+ },
420
+ });
421
+ }
422
+ await recordNodeSecretFingerprint(nodeId, observedNodeManagerSecretFingerprint);
423
+ if (isHermesNodeState(body.hermes)) {
424
+ await recordManagedHermesState(nodeId, body.hermes).catch((err) => {
425
+ console.warn(`[Heartbeat] Failed to record Hermes posture for ${nodeId}: ${err instanceof Error ? err.message : String(err)}`);
426
+ });
427
+ }
428
+ else if (body.hermes !== undefined) {
429
+ console.warn(`[Heartbeat] Invalid Hermes posture payload from ${nodeId}`);
430
+ }
431
+ // Store health snapshot if provided (validated against schema)
432
+ if (body.health) {
433
+ const parsed = safeParse(body.health, heartbeatHealthSchema);
434
+ if (parsed.success) {
435
+ const h = parsed.data;
436
+ const gpuSampledAt = h.gpu ? new Date(h.gpu.sampledAt) : null;
437
+ const gpuSampleIsSane = gpuSampledAt !== null
438
+ && gpuSampledAt.getTime() <= Date.now() + 5 * 60 * 1000;
439
+ const gpuUtilizationValues = h.gpu?.devices
440
+ .map((device) => device.utilizationPercent)
441
+ .filter((value) => value !== null) ?? [];
442
+ const gpuFanValues = h.gpu?.devices
443
+ .map((device) => device.fanPercent)
444
+ .filter((value) => value !== null) ?? [];
445
+ let gpuSampleIsNew = false;
446
+ if (h.gpu && gpuSampledAt && gpuSampleIsSane) {
447
+ const inserted = await db.gpuSnapshot.createMany({
448
+ data: [{
449
+ nodeId,
450
+ sampledAt: gpuSampledAt,
451
+ sampleIntervalSeconds: h.gpu.sampleIntervalSeconds,
452
+ status: h.gpu.status,
453
+ devices: JSON.stringify(h.gpu.devices),
454
+ maxUtilizationPercent: gpuUtilizationValues.length > 0 ? Math.max(...gpuUtilizationValues) : null,
455
+ maxFanPercent: gpuFanValues.length > 0 ? Math.max(...gpuFanValues) : null,
456
+ }],
457
+ skipDuplicates: true,
458
+ });
459
+ gpuSampleIsNew = inserted.count > 0;
460
+ }
461
+ const healthSnapshot = await db.$transaction(async (tx) => {
462
+ const snapshot = await tx.healthSnapshot.create({
463
+ data: {
464
+ nodeId,
465
+ cpuUsagePercent: h.cpu?.usagePercent ?? 0,
466
+ cpuCores: h.cpu?.cores ?? 0,
467
+ cpuLoadAverage: JSON.stringify(h.cpu?.loadAverage ?? [0, 0, 0]),
468
+ memoryTotalBytes: BigInt(h.memory?.totalBytes ?? 0),
469
+ memoryUsedBytes: BigInt(h.memory?.usedBytes ?? 0),
470
+ memoryPercent: h.memory?.usagePercent ?? 0,
471
+ swapTotalBytes: BigInt(h.memory?.swapTotalBytes ?? 0),
472
+ swapUsedBytes: BigInt(h.memory?.swapUsedBytes ?? 0),
473
+ disks: JSON.stringify(h.disks ?? []),
474
+ network: JSON.stringify(h.network ?? []),
475
+ services: JSON.stringify(h.services ?? []),
476
+ wazuh: JSON.stringify(h.wazuh ?? {}),
477
+ systemUpdates: JSON.stringify(h.systemUpdates ?? {}),
478
+ cockpitAvailable: h.cockpitAvailable ?? false,
479
+ source: h.source ?? 'system-probe',
480
+ },
481
+ });
482
+ await enqueueSecurityObservations(tx, nodeId, existing?.name ?? fqdn ?? nodeId, (h.services ?? []), snapshot.id, snapshot.timestamp);
483
+ return snapshot;
484
+ });
485
+ if (securityFusionLocal())
486
+ void ingestMagnusServiceHealth(nodeId, existing?.name ?? fqdn ?? nodeId, (h.services ?? [])).catch((err) => {
487
+ console.error(`[security-fusion] heartbeat conversion failed for ${nodeId}:`, err instanceof Error ? err.message : err);
488
+ });
489
+ // Evaluate health against alert rules (fire-and-forget)
490
+ const alertNodeName = existing?.name ?? fqdn ?? nodeId;
491
+ evaluateHealth(nodeId, alertNodeName, {
492
+ cpuUsagePercent: h.cpu?.usagePercent,
493
+ memoryPercent: h.memory?.usagePercent,
494
+ swapUsedBytes: h.memory?.swapUsedBytes,
495
+ swapTotalBytes: h.memory?.swapTotalBytes,
496
+ disks: h.disks,
497
+ gpuFanPercent: gpuFanValues.length > 0 ? Math.max(...gpuFanValues) : undefined,
498
+ gpuSampledAt: gpuSampleIsSane ? gpuSampledAt ?? undefined : undefined,
499
+ gpuSampleIsNew,
500
+ gpuDevices: h.gpu?.devices,
501
+ }).catch((err) => {
502
+ console.error(`[alert] evaluate error for ${nodeId}:`, err instanceof Error ? err.message : err);
503
+ });
504
+ // Evaluate anomaly detection (fire-and-forget)
505
+ evaluateAnomalies(nodeId, alertNodeName, {
506
+ cpuUsagePercent: h.cpu?.usagePercent,
507
+ memoryPercent: h.memory?.usagePercent,
508
+ swapUsedBytes: h.memory?.swapUsedBytes,
509
+ swapTotalBytes: h.memory?.swapTotalBytes,
510
+ disks: h.disks,
511
+ }, healthSnapshot.id).catch((err) => {
512
+ console.error(`[anomaly] evaluate error for ${nodeId}:`, err instanceof Error ? err.message : err);
513
+ });
514
+ // Evaluate predictive failure detection (fire-and-forget, throttled per node)
515
+ evaluatePredictionsForHeartbeat(nodeId, alertNodeName).catch((err) => {
516
+ console.error(`[prediction] evaluate error for ${nodeId}:`, err instanceof Error ? err.message : err);
517
+ });
518
+ }
519
+ else {
520
+ console.warn(`[Heartbeat] Invalid health payload from ${nodeId}: ${parsed.error}`);
521
+ }
522
+ }
523
+ const expectedVersion = AGENT_VERSION;
524
+ const updateAvailable = body.agentVersion && body.agentVersion !== expectedVersion;
525
+ return c.json({ success: true, data: { accepted: true, expectedVersion, updateAvailable } });
526
+ });
527
+ // ── Retired Redis Streams heartbeat ingress ──
528
+ // No agent or script used it. It stored the shared node secret in plaintext in
529
+ // a Redis stream and replayed it over loopback, losing the caller address.
530
+ // Agents POST /heartbeat directly; the read-only stats route below remains.
531
+ heartbeatRouter.post('/stream', (c) => c.json({
532
+ success: false,
533
+ error: {
534
+ code: 'HEARTBEAT_STREAM_RETIRED',
535
+ message: 'Redis stream heartbeat ingestion is retired; POST /api/v1/heartbeat instead.',
536
+ },
537
+ }, 410));
538
+ /**
539
+ * Stats endpoint for the dashboard.
540
+ */
541
+ heartbeatRouter.get('/stream/stats', authMiddleware, async (c) => {
542
+ const stats = await getHeartbeatStreamStats();
543
+ return c.json({ success: true, data: stats });
544
+ });
545
+ //# sourceMappingURL=heartbeat.js.map