Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

6.1 KB
Raw Blame
package aicontracts

import (
	"context"
	"time"
)

// InvestigationOrchestrator defines the interface for autonomous investigation
// of patrol findings. The OSS binary never creates a concrete implementation;
// enterprise registers one via BusinessHooks.
type InvestigationOrchestrator interface {
	// InvestigateFinding starts an investigation for a finding.
	InvestigateFinding(ctx context.Context, finding *Finding, autonomyLevel string) error
	// GetInvestigationByFinding returns the latest investigation for a finding.
	GetInvestigationByFinding(findingID string) *InvestigationSession
	// GetRunningCount returns the number of running investigations.
	GetRunningCount() int
	// GetFixedCount returns the number of issues auto-fixed by Patrol.
	GetFixedCount() int
	// CanStartInvestigation returns true if a new investigation can be started.
	CanStartInvestigation() bool
	// ReinvestigateFinding triggers a re-investigation of a finding.
	ReinvestigateFinding(ctx context.Context, findingID, autonomyLevel string) error
	// Shutdown signals all running investigations to stop, persists state,
	// and waits for them to finish (up to the context deadline).
	Shutdown(ctx context.Context) error
}

// InvestigationExecutionLimitUpdater is the optional live-configuration
// contract implemented by investigation orchestrators whose turn and timeout
// limits can change without rebuilding the orchestrator. Autonomy settings
// persistence must publish these limits before a newly triggered
// investigation is admitted.
type InvestigationExecutionLimitUpdater interface {
	SetInvestigationExecutionLimits(maxTurns int, timeout time.Duration)
}

// InvestigationConcurrencyLimitUpdater is the optional live-configuration
// contract for orchestrators whose concurrent-investigation limit can change
// without rebuilding the orchestrator. A Patrol provider that serves one call
// at a time needs one investigation at a time.
type InvestigationConcurrencyLimitUpdater interface {
	SetInvestigationConcurrencyLimit(maxConcurrent int)
}

// InvestigationStoreMaintainer is an optional interface for orchestrators that
// expose their investigation store for periodic maintenance.
type InvestigationStoreMaintainer interface {
	CleanupInvestigationStore(maxAge time.Duration, maxSessions int)
}

// RemediationEngine manages remediation plan lifecycle.
type RemediationEngine interface {
	// CreatePlan creates and stores a new remediation plan.
	CreatePlan(plan *RemediationPlan) error
	// GetPlan returns a plan by ID.
	GetPlan(planID string) *RemediationPlan
	// GetPlanForFinding returns the active plan for a finding.
	GetPlanForFinding(findingID string) *RemediationPlan
	// ListPlans returns remediation plans, ordered by creation time (newest first).
	ListPlans(limit int) []*RemediationPlan
	// GetLatestExecutionForPlan returns the most recent execution for a plan.
	GetLatestExecutionForPlan(planID string) *RemediationExecution
	// ApprovePlan approves a plan for execution.
	ApprovePlan(planID, approvedBy string) (*RemediationExecution, error)
	// Execute runs an approved execution.
	Execute(ctx context.Context, executionID string) error
	// Rollback attempts to rollback a failed execution.
	Rollback(ctx context.Context, executionID string) error
	// GetExecution returns an execution by ID.
	GetExecution(executionID string) *RemediationExecution
	// SetExecutionVerification updates the verification status of an execution.
	SetExecutionVerification(executionID string, verified bool, note string)
	// ListExecutions returns recent executions.
	ListExecutions(limit int) []*RemediationExecution
	// AddApprovalRule adds a pre-approval rule.
	AddApprovalRule(rule *ApprovalRule)
	// IsAutoApproved checks if a plan can be auto-executed.
	IsAutoApproved(plan *RemediationPlan) bool
	// FormatPlanForContext formats a plan for AI context.
	FormatPlanForContext(plan *RemediationPlan) string
	// SetCommandExecutor sets the command executor.
	SetCommandExecutor(executor CommandExecutor)
}

// CommandExecutor executes commands on target systems.
type CommandExecutor interface {
	Execute(ctx context.Context, target, command string) (output string, err error)
}

// AlertAnalyzer handles alert-triggered AI analysis.
// The OSS binary never creates a concrete implementation; enterprise registers
// one via BusinessHooks.
type AlertAnalyzer interface {
	OnAlertFired(alert AlertPayload)
	SetEnabled(enabled bool)
	IsEnabled() bool
	Start()
	Stop()
}

// AlertPayload is the minimal alert shape needed by the analyzer.
// This avoids importing internal/alerts in pkg/.
type AlertPayload interface {
	GetID() string
	GetType() string
	GetResourceID() string
	GetResourceName() string
	GetNode() string
	GetInstance() string
	GetMessage() string
	GetValue() float64
	GetThreshold() float64
	GetMetadata() map[string]interface{}
}

// InvestigationStore manages investigation session persistence.
// The concrete implementation lives in the enterprise repo.
type InvestigationStore interface {
	LoadFromDisk() error
	ForceSave() error
	Create(findingID, chatSessionID string) *InvestigationSession
	Get(id string) *InvestigationSession
	GetByFinding(findingID string) []*InvestigationSession
	GetLatestByFinding(findingID string) *InvestigationSession
	GetRunning() []*InvestigationSession
	CountRunning() int
	Update(session *InvestigationSession) bool
	UpdateStatus(id string, status InvestigationStatus) bool
	Complete(id string, outcome InvestigationOutcome, summary string, proposedFix *Fix) bool
	Fail(id string, errorMsg string) bool
	SetOutcome(id string, outcome InvestigationOutcome) bool
	IncrementTurnCount(id string) int
	SetApprovalID(id, approvalID string) bool
	GetAll() []*InvestigationSession
	CountFixed() int
	Cleanup(maxAge time.Duration) int
	EnforceSizeLimit(maxSessions int) int
}

// InvestigationActionStore is implemented by investigation stores that record
// an investigation's action without replacing the rest of the session. Pulse
// uses it, when the store offers it, to record the action lifecycle's
// transitions without undoing progress the orchestrator saved meanwhile.
type InvestigationActionStore interface {
	SetAction(id string, action *ActionReference) bool
}