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.

11.2 KB
Raw Blame
package aicontracts

import (
	"context"
	"encoding/json"
	"time"

	"github.com/rcourtman/pulse-go-rewrite/internal/agentcapabilities"
)

// ---------------------------------------------------------------------------
// Orchestrator dependency interfaces
// ---------------------------------------------------------------------------
// These interfaces define what the investigation orchestrator needs from
// the outside world. They live in pkg/aicontracts so both the OSS repo
// (which assembles the deps) and the enterprise repo (which constructs the
// concrete Orchestrator) can reference them without hitting internal/ visibility.

// OrchestratorChatService provides AI chat session management for investigations.
type OrchestratorChatService interface {
	CreateSession(ctx context.Context) (*OrchestratorChatSession, error)
	GetMessages(ctx context.Context, sessionID string) ([]OrchestratorMessage, error)
	DeleteSession(ctx context.Context, sessionID string) error
	// ExecuteInvestigationStream runs one Patrol investigation under the
	// core-owned investigation execution profile and returns the
	// structured result. There is deliberately no generic execution
	// method, no tool listing outside the investigation projection, and
	// no autonomy field anywhere on this interface: non-interactive
	// operation grants no authority, and enterprise code can neither
	// select a profile nor relax one.
	ExecuteInvestigationStream(ctx context.Context, req OrchestratorInvestigationRequest, callback OrchestratorStreamCallback) (*OrchestratorInvestigationResult, error)
	// ListInvestigationTools names the tools an investigation run
	// offers, projected through the same profile path the run uses.
	ListInvestigationTools(ctx context.Context) []string
}

// OrchestratorInvestigationRequest is one investigation run. Correlation
// identity is injected here by the orchestrator from trusted context; the
// model's proposal tool schema never carries it.
type OrchestratorInvestigationRequest struct {
	SessionID        string `json:"session_id,omitempty"`
	Prompt           string `json:"prompt"`
	SystemPrompt     string `json:"system_prompt,omitempty"`
	MaxTurns         int    `json:"max_turns,omitempty"`
	MaxEvidenceCalls int    `json:"max_evidence_calls,omitempty"`
	ExecutionID      string `json:"execution_id,omitempty"`
	ProposalID       string `json:"proposal_id"`
	FindingID        string `json:"finding_id"`
	InvestigationID  string `json:"investigation_id"`
	// ResourceType is trusted finding metadata used by core only to reduce the
	// investigation tool manifest. It cannot add tools or mutation authority.
	ResourceType string   `json:"resource_type,omitempty"`
	EvidenceIDs  []string `json:"evidence_ids,omitempty"`
}

// OrchestratorInvestigationResult is the structured outcome of one
// investigation run. Proposal cardinality is first-class: consumers never
// reconstruct proposals from session messages. A non-nil Proposal is
// immutable, canonically valid, fully correlated, exposure-safe, and
// produced only by a completely successful run - ready for
// OrchestratorActionBroker.Submit.
type OrchestratorInvestigationResult struct {
	// Action survives provider failure. Its presence is not diagnosis proof or execution authority.
	Action        *ActionReference `json:"action,omitempty"`
	Content       string           `json:"content"`
	Proposal      *ActionProposal  `json:"proposal,omitempty"`
	InputTokens   int              `json:"input_tokens"`
	OutputTokens  int              `json:"output_tokens"`
	ModelTurns    int              `json:"model_turns"`
	EvidenceCalls int              `json:"evidence_calls"`
	ToolCalls     int              `json:"tool_calls"`
	// Conclusion is the model's structured zero-action conclusion, nil when
	// the run recorded none. It carries no remediation authority.
	Conclusion *InvestigationConclusion `json:"conclusion,omitempty"`
}

// OrchestratorFindingsStore provides access to patrol findings for the orchestrator.
type OrchestratorFindingsStore interface {
	Get(id string) *Finding
	Update(f *Finding) bool
}

// OrchestratorInfraContextProvider provides discovered infrastructure context.
type OrchestratorInfraContextProvider interface {
	GetInfrastructureContext() string
}

// OrchestratorMetricsCallback receives metrics events from the orchestrator.
type OrchestratorMetricsCallback interface {
	RecordInvestigationOutcome(outcome string)
	RecordFixVerification(result string)
}

// ---------------------------------------------------------------------------
// Supporting types for orchestrator interfaces
// ---------------------------------------------------------------------------

// OrchestratorChatSession represents a chat session.
type OrchestratorChatSession struct {
	ID string `json:"id"`
}

// OrchestratorStreamCallback is called for each streaming event.
type OrchestratorStreamCallback func(event OrchestratorStreamEvent)

// OrchestratorStreamEvent represents a streaming event.
type OrchestratorStreamEvent struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data,omitempty"`
}

// OrchestratorMessage represents a chat message.
type OrchestratorMessage struct {
	ID               string                      `json:"id"`
	Role             string                      `json:"role"`
	Content          string                      `json:"content"`
	ReasoningContent string                      `json:"reasoning_content,omitempty"`
	ToolCalls        []OrchestratorToolCallInfo  `json:"tool_calls"`
	ToolResult       *OrchestratorToolResultInfo `json:"tool_result,omitempty"`
	Timestamp        time.Time                   `json:"timestamp"`
}

func EmptyOrchestratorMessage() OrchestratorMessage {
	return OrchestratorMessage{}.NormalizeCollections()
}

func (m OrchestratorMessage) NormalizeCollections() OrchestratorMessage {
	if m.ToolCalls == nil {
		m.ToolCalls = []OrchestratorToolCallInfo{}
	}
	for i := range m.ToolCalls {
		m.ToolCalls[i] = m.ToolCalls[i].NormalizeCollections()
	}
	return m
}

// OrchestratorToolCallInfo represents a tool invocation.
type OrchestratorToolCallInfo struct {
	ID               string                 `json:"id"`
	Name             string                 `json:"name"`
	Input            map[string]interface{} `json:"input"`
	ThoughtSignature json.RawMessage        `json:"thought_signature,omitempty"`
}

func EmptyOrchestratorToolCallInfo() OrchestratorToolCallInfo {
	return OrchestratorToolCallInfo{}.NormalizeCollections()
}

func (t OrchestratorToolCallInfo) NormalizeCollections() OrchestratorToolCallInfo {
	return OrchestratorToolCallInfoFromProvider(t.ProviderToolCall())
}

// OrchestratorToolCallInfoFromProvider projects the shared provider-facing
// tool-call shape into the public orchestrator contract without duplicating
// Pulse Intelligence normalization rules.
func OrchestratorToolCallInfoFromProvider(tc agentcapabilities.ProviderToolCall) OrchestratorToolCallInfo {
	tc = tc.NormalizeCollections()
	return OrchestratorToolCallInfo{
		ID:               tc.ID,
		Name:             tc.Name,
		Input:            tc.Input,
		ThoughtSignature: tc.ThoughtSignature,
	}
}

// ProviderToolCall returns the shared provider-facing shape used by Assistant
// and MCP-facing Pulse Intelligence tool projections.
func (t OrchestratorToolCallInfo) ProviderToolCall() agentcapabilities.ProviderToolCall {
	return agentcapabilities.ProviderToolCall{
		ID:               t.ID,
		Name:             t.Name,
		Input:            t.Input,
		ThoughtSignature: t.ThoughtSignature,
	}.NormalizeCollections()
}

// OrchestratorToolResultInfo represents the result of a tool invocation.
type OrchestratorToolResultInfo struct {
	ToolUseID string `json:"tool_use_id"`
	Content   string `json:"content"`
	IsError   bool   `json:"is_error,omitempty"`
}

// OrchestratorToolResultInfoFromProvider projects the shared provider-facing
// result shape into the public orchestrator contract.
func OrchestratorToolResultInfoFromProvider(result agentcapabilities.ProviderToolResult) OrchestratorToolResultInfo {
	return OrchestratorToolResultInfo{
		ToolUseID: result.ToolUseID,
		Content:   result.Content,
		IsError:   result.IsError,
	}
}

// ProviderToolResult returns the shared provider-facing result shape used by
// Assistant and MCP-facing Pulse Intelligence tool projections.
func (r OrchestratorToolResultInfo) ProviderToolResult() agentcapabilities.ProviderToolResult {
	return agentcapabilities.NewProviderToolResult(r.ToolUseID, r.Content, r.IsError)
}

// OrchestratorApproval represents a queued approval request.
type OrchestratorApproval struct {
	ID          string    `json:"id"`
	Type        string    `json:"type"`
	FindingID   string    `json:"finding_id"`
	SessionID   string    `json:"session_id"`
	Description string    `json:"description"`
	TargetHost  string    `json:"target_host,omitempty"`
	Command     string    `json:"command"`
	RiskLevel   string    `json:"risk_level"`
	CreatedAt   time.Time `json:"created_at"`
}

// ---------------------------------------------------------------------------
// OrchestratorDeps bundles all dependencies needed to construct an
// investigation orchestrator. The OSS side resolves each field from its
// singletons and passes this struct to the enterprise factory.
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// AIFinding / AIFindingsStore — used by FindingsStoreAdapter to bridge between
// the patrol FindingsStore (which uses getter/setter interfaces) and the
// orchestrator's FindingsStore (which uses *Finding structs).
// ---------------------------------------------------------------------------

// OrchestratorAIFinding represents the interface for an AI finding from the ai package.
type OrchestratorAIFinding interface {
	GetID() string
	GetSeverity() string
	GetCategory() string
	GetResourceID() string
	GetResourceName() string
	GetResourceType() string
	GetTitle() string
	GetDescription() string
	GetRecommendation() string
	GetEvidence() string
	GetInvestigationSessionID() string
	GetInvestigationStatus() string
	GetInvestigationOutcome() string
	GetLastInvestigatedAt() *time.Time
	GetInvestigationAttempts() int
	SetInvestigationSessionID(string)
	SetInvestigationStatus(string)
	SetInvestigationOutcome(string)
	SetLastInvestigatedAt(*time.Time)
	SetInvestigationAttempts(int)
}

// OrchestratorAIFindingsStore represents the interface for the AI findings store.
type OrchestratorAIFindingsStore interface {
	Get(id string) OrchestratorAIFinding
	UpdateInvestigation(id, sessionID, status, outcome string, lastInvestigatedAt *time.Time, attempts int) bool
}

// OrchestratorDeps contains all dependencies for constructing an investigation orchestrator.
type OrchestratorDeps struct {
	ChatService   OrchestratorChatService
	Store         InvestigationStore
	FindingsStore OrchestratorFindingsStore
	// ActionBroker is the typed, plan-only proposal seam into the core
	// action lifecycle - the ONLY route from an investigation to an
	// infrastructure mutation. Tenant-bound and actor-stamped by the
	// core adapter. REQUIRED: a missing broker disables the
	// orchestrator; there is no command-execution fallback.
	ActionBroker OrchestratorActionBroker
	Config       InvestigationConfig
	InfraContext OrchestratorInfraContextProvider // may be nil
	Metrics      OrchestratorMetricsCallback
}