@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.
- package/.env.example +171 -0
- package/LICENSE +202 -0
- package/README.md +169 -2
- package/agent-version.json +1 -0
- package/dist/ai/HEARTBEAT.md +56 -0
- package/dist/ai/IDENTITY.md +54 -0
- package/dist/ai/MEMORY.md +40 -0
- package/dist/ai/TOOLS.md +538 -0
- package/dist/ai/USER.md +32 -0
- package/dist/cli/args.js +62 -0
- package/dist/cli/env-catalog.js +131 -0
- package/dist/cli/preflight.js +89 -0
- package/dist/cli.js +175 -0
- package/dist/config/cors.js +16 -0
- package/dist/config/env.js +392 -0
- package/dist/config/features.js +16 -0
- package/dist/config/index.js +3 -0
- package/dist/config/listener-security.js +38 -0
- package/dist/config/version.js +31 -0
- package/dist/db/client.js +44 -0
- package/dist/db/generated/prisma/browser.js +18 -0
- package/dist/db/generated/prisma/client.js +35 -0
- package/dist/db/generated/prisma/commonInputTypes.js +11 -0
- package/dist/db/generated/prisma/enums.js +42 -0
- package/dist/db/generated/prisma/internal/class.js +50 -0
- package/dist/db/generated/prisma/internal/prismaNamespace.js +1598 -0
- package/dist/db/generated/prisma/internal/prismaNamespaceBrowser.js +1569 -0
- package/dist/db/generated/prisma/models/A2ATask.js +2 -0
- package/dist/db/generated/prisma/models/AdmissionFence.js +2 -0
- package/dist/db/generated/prisma/models/Agent.js +2 -0
- package/dist/db/generated/prisma/models/AiConversation.js +2 -0
- package/dist/db/generated/prisma/models/AiDashboard.js +2 -0
- package/dist/db/generated/prisma/models/AiMemory.js +2 -0
- package/dist/db/generated/prisma/models/AiMessage.js +2 -0
- package/dist/db/generated/prisma/models/AiScheduledOp.js +2 -0
- package/dist/db/generated/prisma/models/AlertConditionState.js +2 -0
- package/dist/db/generated/prisma/models/AlertRule.js +2 -0
- package/dist/db/generated/prisma/models/AlertViolation.js +2 -0
- package/dist/db/generated/prisma/models/AnomalyEvent.js +2 -0
- package/dist/db/generated/prisma/models/ApplicationObservation.js +2 -0
- package/dist/db/generated/prisma/models/ArdentInstallation.js +2 -0
- package/dist/db/generated/prisma/models/ArdentProfile.js +2 -0
- package/dist/db/generated/prisma/models/ArdentScopeEnrollment.js +2 -0
- package/dist/db/generated/prisma/models/CommandResourceFence.js +2 -0
- package/dist/db/generated/prisma/models/CommandResourceLease.js +2 -0
- package/dist/db/generated/prisma/models/CommandResult.js +2 -0
- package/dist/db/generated/prisma/models/CronJob.js +2 -0
- package/dist/db/generated/prisma/models/CronJobExecution.js +2 -0
- package/dist/db/generated/prisma/models/DeployJob.js +2 -0
- package/dist/db/generated/prisma/models/DriftScan.js +2 -0
- package/dist/db/generated/prisma/models/EngineeringMetricSample.js +2 -0
- package/dist/db/generated/prisma/models/EngineeringRecoveryExercise.js +2 -0
- package/dist/db/generated/prisma/models/ExecutionRun.js +2 -0
- package/dist/db/generated/prisma/models/FilePatchTask.js +2 -0
- package/dist/db/generated/prisma/models/GpuSnapshot.js +2 -0
- package/dist/db/generated/prisma/models/HealthSnapshot.js +2 -0
- package/dist/db/generated/prisma/models/HermesCoreRollout.js +2 -0
- package/dist/db/generated/prisma/models/HermesCoreRolloutHost.js +2 -0
- package/dist/db/generated/prisma/models/HermesCredentialPool.js +2 -0
- package/dist/db/generated/prisma/models/HermesCredentialPoolAttachment.js +2 -0
- package/dist/db/generated/prisma/models/HermesCronJob.js +2 -0
- package/dist/db/generated/prisma/models/HermesHook.js +2 -0
- package/dist/db/generated/prisma/models/HermesInstallation.js +2 -0
- package/dist/db/generated/prisma/models/HermesMcpServer.js +2 -0
- package/dist/db/generated/prisma/models/HermesMcpServerAttachment.js +2 -0
- package/dist/db/generated/prisma/models/HermesMessagingBinding.js +2 -0
- package/dist/db/generated/prisma/models/HermesOperationJob.js +2 -0
- package/dist/db/generated/prisma/models/HermesPluginInstall.js +2 -0
- package/dist/db/generated/prisma/models/HermesProfile.js +2 -0
- package/dist/db/generated/prisma/models/HermesProviderConfig.js +2 -0
- package/dist/db/generated/prisma/models/HermesRegistryClient.js +2 -0
- package/dist/db/generated/prisma/models/HermesRegistryInventory.js +2 -0
- package/dist/db/generated/prisma/models/HermesRegistryReceipt.js +2 -0
- package/dist/db/generated/prisma/models/HermesRegistryRevision.js +2 -0
- package/dist/db/generated/prisma/models/HermesRegistryTarget.js +2 -0
- package/dist/db/generated/prisma/models/HermesRole.js +2 -0
- package/dist/db/generated/prisma/models/HermesRoleApplication.js +2 -0
- package/dist/db/generated/prisma/models/HermesSkillAssignment.js +2 -0
- package/dist/db/generated/prisma/models/HermesToolsetPolicy.js +2 -0
- package/dist/db/generated/prisma/models/IncidentLesson.js +2 -0
- package/dist/db/generated/prisma/models/LibraryCategory.js +2 -0
- package/dist/db/generated/prisma/models/LibraryFile.js +2 -0
- package/dist/db/generated/prisma/models/LogEntry.js +2 -0
- package/dist/db/generated/prisma/models/MagnusReleaseCanary.js +2 -0
- package/dist/db/generated/prisma/models/MaintenanceWindow.js +2 -0
- package/dist/db/generated/prisma/models/ManagedCronDefinition.js +2 -0
- package/dist/db/generated/prisma/models/ManagedCronOccurrence.js +2 -0
- package/dist/db/generated/prisma/models/Node.js +2 -0
- package/dist/db/generated/prisma/models/Notification.js +2 -0
- package/dist/db/generated/prisma/models/NotificationPreference.js +2 -0
- package/dist/db/generated/prisma/models/RemediationExecution.js +2 -0
- package/dist/db/generated/prisma/models/RemediationRule.js +2 -0
- package/dist/db/generated/prisma/models/RemoteCommand.js +2 -0
- package/dist/db/generated/prisma/models/ResourceDrainParticipant.js +2 -0
- package/dist/db/generated/prisma/models/ResourceEnrollment.js +2 -0
- package/dist/db/generated/prisma/models/ResourceParticipantWork.js +2 -0
- package/dist/db/generated/prisma/models/Role.js +2 -0
- package/dist/db/generated/prisma/models/RoleApplication.js +2 -0
- package/dist/db/generated/prisma/models/RolloutTarget.js +2 -0
- package/dist/db/generated/prisma/models/SecurityExportOutbox.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionAction.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionActionApproval.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionCase.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionCaseActivity.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionCaseEntity.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionCaseEvent.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionCaseRelationship.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionCollector.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionCommandOutbox.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionDeletionReceipt.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionDelivery.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionEntity.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionEntityAlias.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionEvent.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionEventEntity.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionEvidence.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionInvestigation.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionNonce.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionReceiptChainHead.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionRuleVersion.js +2 -0
- package/dist/db/generated/prisma/models/SecurityFusionSourceState.js +2 -0
- package/dist/db/generated/prisma/models/SecurityServiceState.js +2 -0
- package/dist/db/generated/prisma/models/SharedResourceFence.js +2 -0
- package/dist/db/generated/prisma/models/SharedResourceLease.js +2 -0
- package/dist/db/generated/prisma/models/SharedResourceLeaseResource.js +2 -0
- package/dist/db/generated/prisma/models/SkillInstallation.js +2 -0
- package/dist/db/generated/prisma/models/SkillRegistryEntry.js +2 -0
- package/dist/db/generated/prisma/models/SkillTag.js +2 -0
- package/dist/db/generated/prisma/models/SkillVersion.js +2 -0
- package/dist/db/generated/prisma/models/TeamApprovalDecision.js +2 -0
- package/dist/db/generated/prisma/models/User.js +2 -0
- package/dist/db/generated/prisma/models/WebhookDelivery.js +2 -0
- package/dist/db/generated/prisma/models/WebhookEndpoint.js +2 -0
- package/dist/db/generated/prisma/models.js +2 -0
- package/dist/db/index.js +3 -0
- package/dist/db/read-retry.js +35 -0
- package/dist/db/seed.js +348 -0
- package/dist/index.js +162 -0
- package/dist/middleware/auth.js +361 -0
- package/dist/middleware/body-limit.js +14 -0
- package/dist/middleware/correlation-id.js +13 -0
- package/dist/middleware/error-handler.js +28 -0
- package/dist/middleware/iam-token.js +21 -0
- package/dist/middleware/rate-limit.js +78 -0
- package/dist/middleware/request-logger.js +85 -0
- package/dist/middleware/security-headers.js +23 -0
- package/dist/middleware/tracing.js +120 -0
- package/dist/middleware/user-status.js +33 -0
- package/dist/openapi/spec.js +2656 -0
- package/dist/queue/redis.js +20 -0
- package/dist/queue/requeue.js +27 -0
- package/dist/queue/workers.js +342 -0
- package/dist/routes/ai-schedule.js +73 -0
- package/dist/routes/alerts.js +79 -0
- package/dist/routes/anomalies.js +101 -0
- package/dist/routes/ardent.js +82 -0
- package/dist/routes/assistant.js +417 -0
- package/dist/routes/audit.js +138 -0
- package/dist/routes/codex.js +65 -0
- package/dist/routes/commands.js +226 -0
- package/dist/routes/credentials.js +58 -0
- package/dist/routes/cron.js +292 -0
- package/dist/routes/dependencies.js +49 -0
- package/dist/routes/deploy.js +18 -0
- package/dist/routes/discovery.js +168 -0
- package/dist/routes/docs.js +24 -0
- package/dist/routes/drift.js +126 -0
- package/dist/routes/events-stream.js +87 -0
- package/dist/routes/files.js +214 -0
- package/dist/routes/fleet-checks.js +21 -0
- package/dist/routes/health.js +133 -0
- package/dist/routes/heartbeat.js +545 -0
- package/dist/routes/hermes-registry.js +278 -0
- package/dist/routes/hermes.js +61 -0
- package/dist/routes/library.js +402 -0
- package/dist/routes/logs.js +172 -0
- package/dist/routes/managed-cron.js +23 -0
- package/dist/routes/mcp.js +39 -0
- package/dist/routes/metrics.js +187 -0
- package/dist/routes/nodes.js +951 -0
- package/dist/routes/notifications.js +130 -0
- package/dist/routes/ops.js +258 -0
- package/dist/routes/orchestra.js +430 -0
- package/dist/routes/predictions.js +23 -0
- package/dist/routes/profiling.js +68 -0
- package/dist/routes/quick-command.js +72 -0
- package/dist/routes/remediation.js +111 -0
- package/dist/routes/resource-leases.js +297 -0
- package/dist/routes/roles.js +370 -0
- package/dist/routes/rollout.js +124 -0
- package/dist/routes/scan.js +116 -0
- package/dist/routes/security-backup.js +58 -0
- package/dist/routes/security-fusion.js +605 -0
- package/dist/routes/security-integration.js +71 -0
- package/dist/routes/self-update.js +129 -0
- package/dist/routes/shared-resource-leases.js +78 -0
- package/dist/routes/skills.js +485 -0
- package/dist/routes/stats.js +96 -0
- package/dist/routes/team-approvals.js +23 -0
- package/dist/routes/terminal-ticket.js +15 -0
- package/dist/routes/terminal.js +17 -0
- package/dist/routes/topology.js +37 -0
- package/dist/routes/users.js +219 -0
- package/dist/routes/webhooks.js +137 -0
- package/dist/schemas/command-reconciliation.js +14 -0
- package/dist/schemas/commands.js +51 -0
- package/dist/schemas/cron.js +33 -0
- package/dist/schemas/deploy.js +20 -0
- package/dist/schemas/files.js +14 -0
- package/dist/schemas/hermes.js +69 -0
- package/dist/schemas/index.js +11 -0
- package/dist/schemas/library.js +48 -0
- package/dist/schemas/logs.js +35 -0
- package/dist/schemas/managed-cron.js +127 -0
- package/dist/schemas/node-responses.js +87 -0
- package/dist/schemas/node-session-receipt.js +84 -0
- package/dist/schemas/nodes.js +36 -0
- package/dist/schemas/orchestra.js +118 -0
- package/dist/schemas/resource-participant-reconciliation.js +99 -0
- package/dist/schemas/roles.js +54 -0
- package/dist/schemas/shared-resource-lease.js +132 -0
- package/dist/schemas/skills.js +44 -0
- package/dist/schemas/users.js +28 -0
- package/dist/scripts/run-disposable-recovery-exercise.js +39 -0
- package/dist/sdk/index.js +2 -0
- package/dist/sdk/node-api-client.js +474 -0
- package/dist/security/approval-assertion.js +66 -0
- package/dist/security/command-completion-registry.js +35 -0
- package/dist/security/command-request.js +12 -0
- package/dist/security/command-result-signature.js +34 -0
- package/dist/security/managed-http.js +45 -0
- package/dist/security/native-completion-registry.js +44 -0
- package/dist/server.js +178 -0
- package/dist/services/admission-fence.js +71 -0
- package/dist/services/ai-assistant-service.js +2523 -0
- package/dist/services/ai-correlation-service.js +225 -0
- package/dist/services/ai-export-service.js +141 -0
- package/dist/services/ai-insights-service.js +219 -0
- package/dist/services/ai-schedule-service.js +145 -0
- package/dist/services/ai-scratchpad-service.js +222 -0
- package/dist/services/ai-tools.js +1343 -0
- package/dist/services/alert-service.js +313 -0
- package/dist/services/anomaly-detector.js +419 -0
- package/dist/services/api-local-work.js +79 -0
- package/dist/services/application-probes.js +309 -0
- package/dist/services/ardent-scope-enrollment.js +123 -0
- package/dist/services/ardent-service.js +86 -0
- package/dist/services/audit-service.js +308 -0
- package/dist/services/codex-service.js +209 -0
- package/dist/services/command-lifecycle.js +60 -0
- package/dist/services/command-resource-leases.js +305 -0
- package/dist/services/control-host-node.js +242 -0
- package/dist/services/credential-rotation-service.js +294 -0
- package/dist/services/dependency-service.js +145 -0
- package/dist/services/disposable-recovery-runner.js +595 -0
- package/dist/services/docker-group-health.js +121 -0
- package/dist/services/engineering-assurance.js +492 -0
- package/dist/services/engineering-evidence-ledger.js +94 -0
- package/dist/services/engineering-metric-evidence.js +26 -0
- package/dist/services/event-bus.js +227 -0
- package/dist/services/fleet-check-log.js +102 -0
- package/dist/services/forecast-service.js +154 -0
- package/dist/services/health-retention.js +175 -0
- package/dist/services/heartbeat-stream.js +63 -0
- package/dist/services/hermes-catalog-import.js +62 -0
- package/dist/services/hermes-cli-parser.js +671 -0
- package/dist/services/hermes-core-rollout.js +90 -0
- package/dist/services/hermes-dashboard-endpoint.js +13 -0
- package/dist/services/hermes-plugin-source.js +149 -0
- package/dist/services/hermes-registry-service.js +258 -0
- package/dist/services/hermes-service.js +566 -0
- package/dist/services/hivemem-client.js +85 -0
- package/dist/services/host-local-writer.js +82 -0
- package/dist/services/incident-evidence.js +110 -0
- package/dist/services/incident-lessons.js +186 -0
- package/dist/services/llm-providers/anthropic.js +143 -0
- package/dist/services/llm-providers/index.js +7 -0
- package/dist/services/llm-providers/llama-cpp.js +237 -0
- package/dist/services/llm-providers/ollama.js +292 -0
- package/dist/services/llm-providers/openai-compatible.js +200 -0
- package/dist/services/llm-providers/provider-error.js +36 -0
- package/dist/services/llm-providers/registry.js +45 -0
- package/dist/services/llm-providers/types.js +2 -0
- package/dist/services/log-emitter.js +19 -0
- package/dist/services/log-retention.js +66 -0
- package/dist/services/maintenance-service.js +29 -0
- package/dist/services/managed-cron.js +493 -0
- package/dist/services/managed-ssh-admission.js +68 -0
- package/dist/services/monitor-pulse.js +10 -0
- package/dist/services/node-issue-monitor.js +71 -0
- package/dist/services/node-service.js +35 -0
- package/dist/services/notification-retention.js +51 -0
- package/dist/services/notification-service.js +94 -0
- package/dist/services/operational-observations.js +112 -0
- package/dist/services/operations-config.js +67 -0
- package/dist/services/ops-service.js +491 -0
- package/dist/services/orchestra-extras-service.js +370 -0
- package/dist/services/orchestra-resources-service.js +530 -0
- package/dist/services/os-scheduler-writer.js +73 -0
- package/dist/services/password-service.js +22 -0
- package/dist/services/prediction-service.js +284 -0
- package/dist/services/recovery-exercises.js +682 -0
- package/dist/services/remediation-service.js +325 -0
- package/dist/services/resource-enrollment.js +433 -0
- package/dist/services/resource-node-lock.js +18 -0
- package/dist/services/resource-participant-work.js +302 -0
- package/dist/services/role-service.js +388 -0
- package/dist/services/rollout-service.js +440 -0
- package/dist/services/security-export.js +185 -0
- package/dist/services/security-fusion-actions.js +22 -0
- package/dist/services/security-fusion-approvals.js +83 -0
- package/dist/services/security-fusion-evidence-integrity.js +144 -0
- package/dist/services/security-fusion-ingest.js +101 -0
- package/dist/services/security-fusion-outbox.js +185 -0
- package/dist/services/security-fusion-receipt-chain.js +134 -0
- package/dist/services/security-fusion-service.js +751 -0
- package/dist/services/security-fusion-verification.js +50 -0
- package/dist/services/self-update-service.js +200 -0
- package/dist/services/shared-resource-leases.js +413 -0
- package/dist/services/skill-publication-bundle.js +90 -0
- package/dist/services/skill-publication-settlement.js +36 -0
- package/dist/services/skill-publication-state.js +40 -0
- package/dist/services/skill-registry-service.js +327 -0
- package/dist/services/skill-service.js +148 -0
- package/dist/services/ssh-exec-service.js +139 -0
- package/dist/services/ssh-host-verification.js +27 -0
- package/dist/services/tailscale-node-key-service.js +75 -0
- package/dist/services/team-approvals.js +118 -0
- package/dist/services/topology-service.js +138 -0
- package/dist/services/transfer-authority-snapshot.js +151 -0
- package/dist/services/webhook-credential-migration.js +97 -0
- package/dist/services/webhook-security.js +242 -0
- package/dist/services/webhook-service.js +494 -0
- package/dist/types/auth.js +3 -0
- package/dist/types/index.js +3 -0
- package/dist/types/library.js +3 -0
- package/dist/types/notifications.js +3 -0
- package/dist/types/roles.js +3 -0
- package/dist/types/shared-contracts.js +3 -0
- package/dist/utils/env.js +141 -0
- package/dist/utils/heartbeat-peer.js +19 -0
- package/dist/utils/idempotency.js +45 -0
- package/dist/utils/ip.js +25 -0
- package/dist/utils/json-fields.js +94 -0
- package/dist/utils/local-tls.js +39 -0
- package/dist/utils/magnus-home.js +15 -0
- package/dist/utils/node-url.js +59 -0
- package/dist/utils/redact.js +26 -0
- package/dist/utils/safe-equal.js +13 -0
- package/dist/utils/sentinel-nodes.js +16 -0
- package/dist/utils/validate.js +41 -0
- package/package.json +119 -4
- package/prisma/migrations/20261031000000_baseline/migration.sql +2935 -0
- package/prisma/migrations/migration_lock.toml +3 -0
- package/prisma/schema.prisma +2185 -0
- package/prisma.config.ts +14 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node cascade-delete helper.
|
|
3
|
+
*
|
|
4
|
+
* Deletes all related records in dependency order before removing the
|
|
5
|
+
* Node row itself. Used by both the hard-delete route and the migrate
|
|
6
|
+
* route in `routes/nodes.ts`.
|
|
7
|
+
*/
|
|
8
|
+
import { getDb } from '../db/index.js';
|
|
9
|
+
/** Remove a node and all child records in the correct dependency order. */
|
|
10
|
+
export async function deleteNodeCascade(nodeId) {
|
|
11
|
+
const db = getDb();
|
|
12
|
+
await db.$transaction(async (tx) => {
|
|
13
|
+
// Hermes posture is node-owned snapshot state. The schema cascade is the
|
|
14
|
+
// final invariant; this explicit delete also supports databases created
|
|
15
|
+
// before the foreign-key migration is deployed.
|
|
16
|
+
await tx.hermesInstallation.deleteMany({ where: { nodeId } });
|
|
17
|
+
await tx.rolloutTarget.deleteMany({ where: { nodeId } });
|
|
18
|
+
await tx.skillInstallation.deleteMany({ where: { nodeId } });
|
|
19
|
+
await tx.cronJobExecution.deleteMany({ where: { nodeId } });
|
|
20
|
+
await tx.cronJob.deleteMany({ where: { nodeId } });
|
|
21
|
+
await tx.filePatchTask.deleteMany({ where: { nodeId } });
|
|
22
|
+
// Anomaly records protect their source snapshots from retention. Explicit
|
|
23
|
+
// node deletion removes that evidence relationship before its snapshots.
|
|
24
|
+
await tx.anomalyEvent.deleteMany({ where: { nodeId } });
|
|
25
|
+
// The schema FK cascades this after migration; keep the explicit cleanup so
|
|
26
|
+
// node deletion is deterministic across rolling upgrades.
|
|
27
|
+
await tx.engineeringMetricSample.deleteMany({ where: { nodeId } });
|
|
28
|
+
await tx.healthSnapshot.deleteMany({ where: { nodeId } });
|
|
29
|
+
await tx.logEntry.deleteMany({ where: { nodeId } });
|
|
30
|
+
await tx.remoteCommand.deleteMany({ where: { nodeId } });
|
|
31
|
+
await tx.agent.deleteMany({ where: { nodeId } });
|
|
32
|
+
await tx.node.delete({ where: { id: nodeId } });
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=node-service.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getDb } from '../db/index.js';
|
|
2
|
+
import { getValidatedEnv } from '../utils/env.js';
|
|
3
|
+
const CHECK_INTERVAL_MS = 3_600_000;
|
|
4
|
+
let timer = null;
|
|
5
|
+
let lastPrunedDate = null;
|
|
6
|
+
export function getNotificationRetentionPolicy() {
|
|
7
|
+
const env = getValidatedEnv();
|
|
8
|
+
return {
|
|
9
|
+
readDays: env.NOTIFICATION_READ_RETENTION_DAYS,
|
|
10
|
+
maximumDays: env.NOTIFICATION_MAX_RETENTION_DAYS,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export async function pruneOldNotifications() {
|
|
14
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
15
|
+
if (lastPrunedDate === today)
|
|
16
|
+
return;
|
|
17
|
+
const policy = getNotificationRetentionPolicy();
|
|
18
|
+
const readCutoff = new Date(Date.now() - policy.readDays * 24 * 60 * 60 * 1000);
|
|
19
|
+
const maximumCutoff = new Date(Date.now() - policy.maximumDays * 24 * 60 * 60 * 1000);
|
|
20
|
+
try {
|
|
21
|
+
const result = await getDb().notification.deleteMany({
|
|
22
|
+
where: {
|
|
23
|
+
OR: [
|
|
24
|
+
{ isRead: true, createdAt: { lt: readCutoff } },
|
|
25
|
+
{ createdAt: { lt: maximumCutoff } },
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
lastPrunedDate = today;
|
|
30
|
+
if (result.count > 0) {
|
|
31
|
+
console.log(`🗑️ Notification retention: pruned ${result.count} old notifications`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.error(`Notification retention error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function startNotificationRetention() {
|
|
39
|
+
if (timer)
|
|
40
|
+
return;
|
|
41
|
+
const policy = getNotificationRetentionPolicy();
|
|
42
|
+
console.log(`🗑️ Notification retention started (read ${policy.readDays}d, maximum ${policy.maximumDays}d)`);
|
|
43
|
+
void pruneOldNotifications();
|
|
44
|
+
timer = setInterval(() => void pruneOldNotifications(), CHECK_INTERVAL_MS);
|
|
45
|
+
}
|
|
46
|
+
export function stopNotificationRetention() {
|
|
47
|
+
if (timer)
|
|
48
|
+
clearInterval(timer);
|
|
49
|
+
timer = null;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=notification-retention.js.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { matchingMaintenance } from './maintenance-service.js';
|
|
2
|
+
import { getDb } from '../db/index.js';
|
|
3
|
+
import { createWebhookDeliveries } from './webhook-service.js';
|
|
4
|
+
// ─── Notification Service — create, list, read, dedupe ───
|
|
5
|
+
/** Cooldown window to prevent duplicate notifications for the same event. */
|
|
6
|
+
const COOLDOWN_MS = 5 * 60 * 1000; // 5 minutes
|
|
7
|
+
/**
|
|
8
|
+
* Create a notification with built-in dedupe.
|
|
9
|
+
* If an identical (type + nodeId + sourceRef) notification was created within the cooldown window,
|
|
10
|
+
* we skip creation to avoid alert spam from transient flapping.
|
|
11
|
+
*/
|
|
12
|
+
export async function createNotification(input) {
|
|
13
|
+
const db = getDb();
|
|
14
|
+
const maintenance = await matchingMaintenance(input.nodeId, typeof input.metadata?.['serviceId'] === 'string' ? input.metadata['serviceId'] : null);
|
|
15
|
+
return db.$transaction(tx => createNotificationInTransaction(tx, input, maintenance?.id ?? null));
|
|
16
|
+
}
|
|
17
|
+
/** The notification and its pending webhook rows are one durable outbox write. */
|
|
18
|
+
export async function createNotificationInTransaction(tx, input, maintenanceId) {
|
|
19
|
+
const identity = JSON.stringify([input.type, input.nodeId ?? null, input.sourceRef ?? null]);
|
|
20
|
+
await tx.$executeRaw `SELECT pg_advisory_xact_lock(hashtextextended(${`notification:${identity}`}, 0))`;
|
|
21
|
+
const existing = await tx.notification.findFirst({ where: {
|
|
22
|
+
type: input.type, nodeId: input.nodeId ?? null, sourceRef: input.sourceRef ?? null,
|
|
23
|
+
createdAt: { gte: new Date(Date.now() - COOLDOWN_MS) },
|
|
24
|
+
}, select: { id: true } });
|
|
25
|
+
if (existing)
|
|
26
|
+
return null;
|
|
27
|
+
const notification = await tx.notification.create({ data: {
|
|
28
|
+
type: input.type, severity: input.severity, title: input.title, message: input.message,
|
|
29
|
+
nodeId: input.nodeId ?? null, nodeName: input.nodeName ?? null, sourceRef: input.sourceRef ?? null,
|
|
30
|
+
metadata: JSON.stringify(input.metadata ?? {}), suppressedBy: maintenanceId,
|
|
31
|
+
} });
|
|
32
|
+
if (!maintenanceId)
|
|
33
|
+
await createWebhookDeliveries(tx, input.type, {
|
|
34
|
+
notificationId: notification.id, ...input, metadata: input.metadata ?? {},
|
|
35
|
+
createdAt: notification.createdAt.toISOString(),
|
|
36
|
+
});
|
|
37
|
+
return notification.id;
|
|
38
|
+
}
|
|
39
|
+
export async function listNotifications(params) {
|
|
40
|
+
const db = getDb();
|
|
41
|
+
const page = params.page ?? 1;
|
|
42
|
+
const pageSize = Math.min(params.pageSize ?? 50, 100);
|
|
43
|
+
const skip = (page - 1) * pageSize;
|
|
44
|
+
const where = {};
|
|
45
|
+
if (params.isRead !== undefined)
|
|
46
|
+
where['isRead'] = params.isRead;
|
|
47
|
+
if (params.severity)
|
|
48
|
+
where['severity'] = params.severity;
|
|
49
|
+
if (params.type)
|
|
50
|
+
where['type'] = params.type;
|
|
51
|
+
if (params.nodeId)
|
|
52
|
+
where['nodeId'] = params.nodeId;
|
|
53
|
+
const [notifications, total] = await Promise.all([
|
|
54
|
+
db.notification.findMany({
|
|
55
|
+
where,
|
|
56
|
+
orderBy: { createdAt: 'desc' },
|
|
57
|
+
skip,
|
|
58
|
+
take: pageSize,
|
|
59
|
+
}),
|
|
60
|
+
db.notification.count({ where }),
|
|
61
|
+
]);
|
|
62
|
+
return {
|
|
63
|
+
notifications: notifications.map((n) => ({
|
|
64
|
+
...n,
|
|
65
|
+
metadata: JSON.parse(n.metadata),
|
|
66
|
+
createdAt: n.createdAt.toISOString(),
|
|
67
|
+
readAt: n.readAt?.toISOString() ?? null,
|
|
68
|
+
})),
|
|
69
|
+
total,
|
|
70
|
+
page,
|
|
71
|
+
pageSize,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export async function getUnreadCount() {
|
|
75
|
+
const db = getDb();
|
|
76
|
+
return db.notification.count({ where: { isRead: false } });
|
|
77
|
+
}
|
|
78
|
+
export async function markRead(ids) {
|
|
79
|
+
const db = getDb();
|
|
80
|
+
const result = await db.notification.updateMany({
|
|
81
|
+
where: { id: { in: ids }, isRead: false },
|
|
82
|
+
data: { isRead: true, readAt: new Date() },
|
|
83
|
+
});
|
|
84
|
+
return result.count;
|
|
85
|
+
}
|
|
86
|
+
export async function markAllRead() {
|
|
87
|
+
const db = getDb();
|
|
88
|
+
const result = await db.notification.updateMany({
|
|
89
|
+
where: { isRead: false },
|
|
90
|
+
data: { isRead: true, readAt: new Date() },
|
|
91
|
+
});
|
|
92
|
+
return result.count;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=notification-service.js.map
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { SENTINEL_NODE_IDS } from '../utils/sentinel-nodes.js';
|
|
2
|
+
import { magnusObservationSchema } from '@hivemagnus/magnus-contracts';
|
|
3
|
+
import { getDb } from '../db/index.js';
|
|
4
|
+
import { getApplicationConfigs } from './operations-config.js';
|
|
5
|
+
export async function getOperationalObservations() {
|
|
6
|
+
const db = getDb();
|
|
7
|
+
const [nodes, count, applications] = await Promise.all([
|
|
8
|
+
db.node.findMany({
|
|
9
|
+
where: { id: { notIn: SENTINEL_NODE_IDS } },
|
|
10
|
+
take: 10000,
|
|
11
|
+
orderBy: { id: 'asc' },
|
|
12
|
+
select: {
|
|
13
|
+
id: true,
|
|
14
|
+
hostname: true,
|
|
15
|
+
status: true,
|
|
16
|
+
lastHeartbeatAt: true,
|
|
17
|
+
agent: { select: { version: true } },
|
|
18
|
+
},
|
|
19
|
+
}),
|
|
20
|
+
db.node.count({ where: { id: { notIn: SENTINEL_NODE_IDS } } }),
|
|
21
|
+
db.applicationObservation.findMany({ take: 10000, orderBy: { id: 'asc' } }),
|
|
22
|
+
]);
|
|
23
|
+
const configs = getApplicationConfigs();
|
|
24
|
+
return magnusObservationSchema.parse({
|
|
25
|
+
schemaVersion: 1,
|
|
26
|
+
collectedAt: new Date().toISOString(),
|
|
27
|
+
complete: count <= 10000,
|
|
28
|
+
nodes: nodes.map((n) => ({
|
|
29
|
+
id: n.id,
|
|
30
|
+
hostname: n.hostname,
|
|
31
|
+
status: n.status,
|
|
32
|
+
lastHeartbeatAt: n.lastHeartbeatAt?.toISOString() ?? null,
|
|
33
|
+
agentVersion: n.agent?.version ?? null,
|
|
34
|
+
})),
|
|
35
|
+
deployments: configs.map((config) => {
|
|
36
|
+
const current = applications.find((a) => a.id === config.id);
|
|
37
|
+
let revision = null;
|
|
38
|
+
try {
|
|
39
|
+
revision = JSON.parse(current?.resultJson ?? '{}').releaseRevision;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
/* unknown */
|
|
43
|
+
}
|
|
44
|
+
const fresh = current?.observedAt && Date.now() - current.observedAt.getTime() < 180000;
|
|
45
|
+
return {
|
|
46
|
+
id: config.id,
|
|
47
|
+
projectId: config.projectId,
|
|
48
|
+
serviceId: config.serviceId,
|
|
49
|
+
nodeId: config.nodeId,
|
|
50
|
+
status: fresh ? current.status : 'unknown',
|
|
51
|
+
observedAt: current?.observedAt?.toISOString() ?? null,
|
|
52
|
+
releaseRevision: typeof revision === 'string' ? revision : null,
|
|
53
|
+
dependsOn: config.dependsOn,
|
|
54
|
+
};
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
export async function getCapacityAdvice() {
|
|
59
|
+
const nodes = await getDb().node.findMany({
|
|
60
|
+
where: { id: { notIn: SENTINEL_NODE_IDS } },
|
|
61
|
+
take: 1000,
|
|
62
|
+
orderBy: { id: 'asc' },
|
|
63
|
+
select: {
|
|
64
|
+
id: true,
|
|
65
|
+
hostname: true,
|
|
66
|
+
gpuSnapshots: {
|
|
67
|
+
take: 1,
|
|
68
|
+
orderBy: { sampledAt: 'desc' },
|
|
69
|
+
select: { sampledAt: true, devices: true },
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
advisoryOnly: true,
|
|
75
|
+
generatedAt: new Date().toISOString(),
|
|
76
|
+
nodes: nodes.map((node) => {
|
|
77
|
+
const sample = node.gpuSnapshots[0];
|
|
78
|
+
let devices = [];
|
|
79
|
+
try {
|
|
80
|
+
const parsed = JSON.parse(sample?.devices ?? '[]');
|
|
81
|
+
if (Array.isArray(parsed))
|
|
82
|
+
devices = parsed;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
/* unknown */
|
|
86
|
+
}
|
|
87
|
+
const fresh = sample && Date.now() - sample.sampledAt.getTime() < 120000;
|
|
88
|
+
const known = fresh &&
|
|
89
|
+
devices.length > 0 &&
|
|
90
|
+
devices.every((d) => d &&
|
|
91
|
+
typeof d === 'object' &&
|
|
92
|
+
Number.isFinite(d.utilizationPercent) &&
|
|
93
|
+
Number.isFinite(d.temperatureCelsius));
|
|
94
|
+
const pressure = known &&
|
|
95
|
+
devices.some((d) => (d.utilizationPercent ?? 0) >= 90 ||
|
|
96
|
+
(d.temperatureCelsius ?? 0) >= 85 ||
|
|
97
|
+
(d.fanPercent ?? 0) >= 95);
|
|
98
|
+
return {
|
|
99
|
+
nodeId: node.id,
|
|
100
|
+
hostname: node.hostname,
|
|
101
|
+
observedAt: sample?.sampledAt.toISOString() ?? null,
|
|
102
|
+
capacity: !known ? 'unknown' : pressure ? 'constrained' : 'available',
|
|
103
|
+
recommendation: !known
|
|
104
|
+
? 'Collect fresh supported GPU sensors before scheduling'
|
|
105
|
+
: pressure
|
|
106
|
+
? 'Consider another endpoint or wait; workload owner decides'
|
|
107
|
+
: 'No observed GPU pressure; workload owner must still check memory and model requirements',
|
|
108
|
+
};
|
|
109
|
+
}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=operational-observations.js.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { readFileSync, lstatSync } from 'node:fs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
const safeId = z.string().regex(/^[a-zA-Z0-9._/-]{1,160}$/);
|
|
4
|
+
const probePath = z
|
|
5
|
+
.string()
|
|
6
|
+
.regex(/^\/[a-zA-Z0-9/_.-]*$/)
|
|
7
|
+
.refine((v) => !v.includes('..') && !v.startsWith('//'));
|
|
8
|
+
export const applicationConfigSchema = z
|
|
9
|
+
.object({
|
|
10
|
+
id: safeId,
|
|
11
|
+
projectId: safeId,
|
|
12
|
+
serviceId: safeId,
|
|
13
|
+
nodeId: safeId,
|
|
14
|
+
baseUrl: z
|
|
15
|
+
.string()
|
|
16
|
+
.url()
|
|
17
|
+
.refine((value) => {
|
|
18
|
+
const u = new URL(value);
|
|
19
|
+
return (u.protocol === 'https:' &&
|
|
20
|
+
!u.username &&
|
|
21
|
+
!u.password &&
|
|
22
|
+
!u.search &&
|
|
23
|
+
!u.hash &&
|
|
24
|
+
u.pathname === '/');
|
|
25
|
+
}, 'Use an HTTPS origin without credentials'),
|
|
26
|
+
healthPath: probePath,
|
|
27
|
+
readinessPath: probePath.optional(),
|
|
28
|
+
queuePath: probePath.optional(),
|
|
29
|
+
canaryModel: z
|
|
30
|
+
.string()
|
|
31
|
+
.regex(/^[a-zA-Z0-9._:/-]{1,160}$/)
|
|
32
|
+
.optional(),
|
|
33
|
+
maxQueueAgeMs: z.number().int().min(1000).max(86400000).default(300000),
|
|
34
|
+
tokenEnv: z
|
|
35
|
+
.string()
|
|
36
|
+
.regex(/^[A-Z][A-Z0-9_]+$/)
|
|
37
|
+
.optional(),
|
|
38
|
+
kind: z
|
|
39
|
+
.enum(['http', 'workflow-queue', 'backend-pool', 'hive-router', 'hivemem'])
|
|
40
|
+
.default('http'),
|
|
41
|
+
restartService: z
|
|
42
|
+
.string()
|
|
43
|
+
.regex(/^[a-zA-Z0-9][a-zA-Z0-9_.@-]{0,100}\.service$/)
|
|
44
|
+
.refine((v) => !/ssh|tailscale|fail2ban|firewall/i.test(v))
|
|
45
|
+
.optional(),
|
|
46
|
+
dependsOn: z.array(safeId).max(100).default([]),
|
|
47
|
+
})
|
|
48
|
+
.strict();
|
|
49
|
+
export function getApplicationConfigs() {
|
|
50
|
+
const file = process.env['MAGNUS_APPLICATION_PROBES_FILE'];
|
|
51
|
+
if (!file)
|
|
52
|
+
return [];
|
|
53
|
+
const stat = lstatSync(file);
|
|
54
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 128000 || stat.mode & 0o022)
|
|
55
|
+
throw new Error('Unsafe application probe configuration');
|
|
56
|
+
const parsed = z
|
|
57
|
+
.object({
|
|
58
|
+
schemaVersion: z.literal(1),
|
|
59
|
+
applications: z.array(applicationConfigSchema).max(100),
|
|
60
|
+
})
|
|
61
|
+
.strict()
|
|
62
|
+
.parse(JSON.parse(readFileSync(file, 'utf8')));
|
|
63
|
+
if (new Set(parsed.applications.map((a) => a.id)).size !== parsed.applications.length)
|
|
64
|
+
throw new Error('Duplicate application probe ID');
|
|
65
|
+
return parsed.applications;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=operations-config.js.map
|