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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
import type { PatrolAutonomyLevel, PatrolRunRecord, PatrolStatus } from '@/api/patrol';
import type { MetadataBadgeTone } from '@/components/shared/MetadataBadge';
import type { EvidenceCompleteness, EvidenceFreshness } from '@/types/operationalTrust';
import type {
ProtectionHistoryCompleteness,
ProtectionState,
RecoveryOutcome,
} from '@/types/recovery';
import {
getPatrolFindingIssueCountLabel,
getPatrolWorkTypeCompositionClause,
} from '@/utils/aiFindingPresentation';
import type { PatrolWorkTypeComposition } from '@/utils/aiFindingPresentation';
import { getPatrolRunCoverageSummary } from '@/utils/patrolRunPresentation';
import {
isPatrolConfigLevelCause,
PATROL_PROVIDER_AUTH_CAUSE,
PATROL_PROVIDER_CONNECTION_CAUSE,
} from '@/utils/patrolRuntimeActions';
import type { UpgradeDestination } from '@/utils/upgradeNavigation';
export const PATROL_AUTONOMY_POLICY_PRESENTATION: Record<
PatrolAutonomyLevel,
{ label: string; detail: string; compactLabel?: string }
> = {
monitor: {
label: 'Watch only',
detail: 'Patrol checks infrastructure and reports issues only. It does not start fixes.',
compactLabel: 'Watch only',
},
approval: {
label: 'Ask first',
detail: 'Patrol investigates and prepares fixes, but every change waits for your approval.',
compactLabel: 'Ask first',
},
assisted: {
label: 'Safe auto-fix',
detail:
'Patrol can run low- or medium-risk fixes allowed by policy. Higher-risk work still asks first.',
compactLabel: 'Safe auto-fix',
},
full: {
label: 'Autopilot',
detail: 'Patrol can act automatically within policy and still asks when approval is required.',
compactLabel: 'Autopilot',
},
};
export const PATROL_WORKSPACE_HISTORY_DESCRIPTION = 'Past checks and what Patrol recorded.';
export const PATROL_WORKSPACE_RUN_RECORD_DESCRIPTION = 'What Patrol found during this run.';
export const PATROL_WORKSPACE_SETUP_TITLE = 'Patrol needs setup';
export const PATROL_WORKSPACE_SETUP_DESCRIPTION =
'Patrol cannot check infrastructure until its selected model passes the Patrol tool check.';
export const PATROL_WORKSPACE_BUDGET_PAUSED_DESCRIPTION =
'Patrol is paused until the 30-day AI cost budget is raised or the 30-day window rolls on.';
/**
* The setup workspace subtitle follows the block cause: a used-up cost budget
* is a spending decision, not a model that failed the tool check (#1789).
*/
export const PATROL_WORKSPACE_CONNECT_DESCRIPTION =
'Patrol needs an AI model before it can check your infrastructure.';
export const PATROL_WORKSPACE_RECONNECT_DESCRIPTION =
'Your AI provider refused Patrol’s connection. Reconnect it so Patrol can run.';
export const PATROL_WORKSPACE_UNREACHABLE_DESCRIPTION =
'Pulse cannot reach your AI provider. Check its address and status on Provider & Models.';
export const PATROL_WORKSPACE_SETUP_ACTIVE_DESCRIPTION =
'Connect a model and check it here. Patrol starts once the check passes.';
export function getPatrolWorkspaceSetupDescription(cause?: string | null): string {
if (cause === 'budget_exhausted') return PATROL_WORKSPACE_BUDGET_PAUSED_DESCRIPTION;
// No provider or AI off: the model check is not the next step yet.
if (isPatrolConfigLevelCause(cause ?? undefined)) return PATROL_WORKSPACE_CONNECT_DESCRIPTION;
if (cause === PATROL_PROVIDER_AUTH_CAUSE) return PATROL_WORKSPACE_RECONNECT_DESCRIPTION;
if (cause === PATROL_PROVIDER_CONNECTION_CAUSE) return PATROL_WORKSPACE_UNREACHABLE_DESCRIPTION;
return PATROL_WORKSPACE_SETUP_DESCRIPTION;
}
export const PATROL_WORKSPACE_QUEUE_TITLE = 'Open work';
export const PATROL_WORKSPACE_RUN_RECORD_TITLE = 'Check details';
export interface PatrolAttentionEvidencePresentation {
detailLabel: string;
rowLabel?: string;
tone: MetadataBadgeTone;
}
export interface PatrolAttentionProtectionPresentation {
detailLabel: string;
rowLabel?: string;
tone: MetadataBadgeTone;
}
// Unknown freshness means the evidence source publishes no validity window
// (only availability probes do today), not that something is wrong — so it
// never warns and never claims "timing unavailable" while the envelope's
// observation time is displayed right next to it. Row labels surface only
// states worth a glance; the expected complete-evidence state stays quiet.
export function getPatrolAttentionEvidencePresentation(
freshness: EvidenceFreshness,
completeness: EvidenceCompleteness,
): PatrolAttentionEvidencePresentation {
if (completeness === 'unavailable') {
return { detailLabel: 'Evidence unavailable', rowLabel: 'Evidence unavailable', tone: 'muted' };
}
if (completeness === 'complete') {
if (freshness === 'fresh') {
return { detailLabel: 'Evidence current', rowLabel: 'Evidence current', tone: 'success' };
}
if (freshness === 'stale') {
return {
detailLabel: 'Evidence out of date',
rowLabel: 'Evidence out of date',
tone: 'warning',
};
}
return { detailLabel: 'Evidence recorded', tone: 'muted' };
}
if (freshness === 'stale') {
return {
detailLabel: 'Evidence incomplete and out of date',
rowLabel: 'Evidence incomplete and out of date',
tone: 'warning',
};
}
return { detailLabel: 'Evidence incomplete', rowLabel: 'Evidence incomplete', tone: 'warning' };
}
export function getPatrolAttentionProtectionPresentation(
state?: ProtectionState,
): PatrolAttentionProtectionPresentation {
switch (state) {
case 'protected':
return { detailLabel: 'Protected', rowLabel: 'Protected', tone: 'success' };
case 'attention':
return {
detailLabel: 'Protection needs attention',
rowLabel: 'Protection needs attention',
tone: 'warning',
};
case 'unprotected':
return { detailLabel: 'Not protected', rowLabel: 'Not protected', tone: 'danger' };
default:
return { detailLabel: 'Protection status unavailable', tone: 'muted' };
}
}
function formatAttentionMetadataToken(value: string): string {
const normalized = value.trim().replace(/[_-]+/g, ' ');
return normalized ? normalized.charAt(0).toUpperCase() + normalized.slice(1) : 'Unavailable';
}
export function getPatrolProtectionProviderLabels(input: {
jobState: RecoveryOutcome;
historyCompleteness: ProtectionHistoryCompleteness;
}): { job: string; history: string } {
return {
job:
input.jobState === 'unknown'
? 'Job status unavailable'
: `Job: ${formatAttentionMetadataToken(input.jobState)}`,
history:
input.historyCompleteness === 'unknown' || input.historyCompleteness === 'unavailable'
? 'History unavailable'
: `History: ${formatAttentionMetadataToken(input.historyCompleteness)}`,
};
}
interface PatrolControlCopyInput {
autonomyLevel?: PatrolAutonomyLevel;
autonomyLocked?: boolean;
}
interface PatrolQueueCountInput {
affectedResourceCount?: number;
findingCount?: number;
// Set when the queue lists one resource's findings: the summary names it
// instead of counting affected resources.
resourceName?: string;
workTypeComposition?: PatrolWorkTypeComposition;
}
export interface PatrolWorkspaceWorkGroupSummary {
detail: string;
id: 'approvals' | 'failed-actions' | 'failed-check' | 'recurring' | 'stale-protection';
label: string;
tone: MetadataBadgeTone;
}
export interface MonitorContextPatrolPostureSummary {
detail: string;
id: 'coverage' | 'open-work' | 'schedule';
label: string;
tone: MetadataBadgeTone;
}
interface PatrolWorkspaceWorkGroupsInput {
latestRun?:
| (Pick<PatrolRunRecord, 'error_count' | 'resources_checked' | 'status'> &
Partial<Pick<PatrolRunRecord, 'completed_at'>>)
| null;
nowMs?: number;
patrolStatus?:
| (Pick<PatrolStatus, 'error_count' | 'next_patrol_at' | 'running'> &
Partial<Pick<PatrolStatus, 'interval_ms' | 'last_patrol_at'>>)
| null;
pendingApprovalCount?: number;
// One resource's findings. The latest check's health and coverage describe
// every resource, so their groups stay in the unscoped queue.
resourceScoped?: boolean;
workTypeComposition?: PatrolWorkTypeComposition;
}
interface MonitorContextPatrolPostureInput {
findingCount?: number;
latestRun?: Pick<
PatrolRunRecord,
| 'effective_scope_resource_ids'
| 'error_count'
| 'resources_checked'
| 'scope_resource_ids'
| 'status'
> | null;
nowMs?: number;
patrolStatus?: Pick<
PatrolStatus,
| 'enabled'
| 'error_count'
| 'findings_count'
| 'healthy'
| 'next_patrol_at'
| 'resources_checked'
| 'running'
| 'runtime_state'
> | null;
pendingApprovalCount?: number;
workTypeComposition?: PatrolWorkTypeComposition;
monitoredResourceCount?: number;
}
interface PatrolSetupIssueReasonInput {
setupFindingTitle?: string;
readinessSummary?: string;
triggerDisabledReason?: string;
blockedReason?: string;
}
function normalizeCount(value: number | undefined): number {
if (typeof value !== 'number' || !Number.isFinite(value)) {
return 0;
}
return Math.max(0, Math.trunc(value));
}
function normalizeText(value: string | undefined): string {
return value?.trim() ?? '';
}
function formatCount(count: number, singular: string, plural = `${singular}s`): string {
return `${count} ${count === 1 ? singular : plural}`;
}
function normalizeStatus(value: string | undefined): string {
return String(value || '')
.trim()
.toLowerCase()
.replace(/\s+/g, '_');
}
function hasFailedPatrolCheck(
run: Pick<PatrolRunRecord, 'error_count' | 'status'> | null | undefined,
): boolean {
if (!run) return false;
return normalizeCount(run.error_count) > 0 || normalizeStatus(run.status) === 'error';
}
function isScheduledPatrolOverdue(
status: Pick<PatrolStatus, 'next_patrol_at' | 'running'> | null | undefined,
nowMs: number,
): boolean {
if (!status || status.running || !status.next_patrol_at) return false;
const nextPatrolAt = Date.parse(status.next_patrol_at);
return Number.isFinite(nextPatrolAt) && nextPatrolAt < nowMs;
}
const PATROL_MINIMUM_FRESHNESS_WINDOW_MS = 24 * 60 * 60 * 1000;
export function isPatrolCoverageStale(input: {
latestRun?: Partial<Pick<PatrolRunRecord, 'completed_at'>> | null;
nowMs?: number;
patrolStatus?:
| (Pick<PatrolStatus, 'running'> &
Partial<Pick<PatrolStatus, 'interval_ms' | 'last_patrol_at' | 'next_patrol_at'>>)
| null;
}): boolean {
const nowMs = input.nowMs ?? Date.now();
if (input.patrolStatus?.running) return false;
if (isScheduledPatrolOverdue(input.patrolStatus, nowMs)) return true;
const lastCheckAt = input.latestRun?.completed_at || input.patrolStatus?.last_patrol_at;
if (!lastCheckAt) return false;
const lastCheckMs = Date.parse(lastCheckAt);
if (!Number.isFinite(lastCheckMs) || lastCheckMs > nowMs) return false;
const configuredIntervalMs = normalizeCount(input.patrolStatus?.interval_ms);
const freshnessWindowMs = Math.max(
PATROL_MINIMUM_FRESHNESS_WINDOW_MS,
configuredIntervalMs > 0 ? configuredIntervalMs * 2 : 0,
);
return nowMs - lastCheckMs > freshnessWindowMs;
}
function isActivePatrolRuntime(
status: Pick<PatrolStatus, 'runtime_state'> | null | undefined,
): boolean {
return !status?.runtime_state || status.runtime_state === 'active';
}
function getPatrolCoverageLabel(
latestRun:
| Pick<
PatrolRunRecord,
'effective_scope_resource_ids' | 'resources_checked' | 'scope_resource_ids'
>
| null
| undefined,
status: Pick<PatrolStatus, 'resources_checked'> | null | undefined,
): string | undefined {
const latestRunCoverage = latestRun ? getPatrolRunCoverageSummary(latestRun) : '';
if (latestRunCoverage) return latestRunCoverage;
const statusResourcesChecked = normalizeCount(status?.resources_checked);
if (statusResourcesChecked > 0) {
return `Checked ${formatCount(statusResourcesChecked, 'resource')}`;
}
return undefined;
}
function formatMonitorCoverageLabel(coverageLabel: string | undefined): string {
if (!coverageLabel) return 'Patrol coverage needs refresh';
return `Patrol ${coverageLabel.charAt(0).toLowerCase()}${coverageLabel.slice(1)}`;
}
function getPatrolQueueActionDetail(input: PatrolControlCopyInput): string {
if (input.autonomyLocked) {
return 'Open a row to review evidence and record the outcome.';
}
switch (input.autonomyLevel) {
case 'approval':
return 'Open a row to review evidence, approve any change, and verify the outcome.';
case 'assisted':
return 'Open a row to see safe fixes, approval requests, and verification.';
case 'full':
return 'Open a row to see automatic actions, policy approvals, and verification.';
case 'monitor':
default:
return 'Open a row to review evidence and record the outcome.';
}
}
export function getPatrolSetupIssueReason(input: PatrolSetupIssueReasonInput): string {
const setupFindingTitle = normalizeText(input.setupFindingTitle);
if (setupFindingTitle) return setupFindingTitle;
const readinessSummary = normalizeText(input.readinessSummary);
if (readinessSummary) {
if (
/\btool(?:_| )?calls?\b/i.test(readinessSummary) ||
/\brun tools?\b/i.test(readinessSummary)
) {
return 'Selected model cannot run Patrol tools.';
}
return readinessSummary;
}
return (
normalizeText(input.triggerDisabledReason) ||
normalizeText(input.blockedReason) ||
'Provider settings need attention'
);
}
export function getPatrolReadyWorkDetail(input: PatrolControlCopyInput): string {
if (input.autonomyLocked) {
return 'Patrol is ready to check infrastructure and list current issues.';
}
switch (input.autonomyLevel) {
case 'approval':
return 'Patrol is ready to check, investigate, and ask before any change.';
case 'assisted':
return 'Patrol is ready to check, investigate, and fix safe issues when policy allows it.';
case 'full':
return 'Patrol is ready to check, investigate, and act automatically within your policy.';
case 'monitor':
default:
return 'Patrol is ready to check infrastructure and list current issues.';
}
}
export function getPatrolQueueWorkspaceDescription(
input: PatrolControlCopyInput & PatrolQueueCountInput,
): string {
const findingCount = normalizeCount(input.findingCount);
const affectedResourceCount = normalizeCount(input.affectedResourceCount);
const resourceName = normalizeText(input.resourceName);
if (findingCount > 0 && (resourceName || affectedResourceCount > 0)) {
const compositionClause = input.workTypeComposition
? getPatrolWorkTypeCompositionClause(input.workTypeComposition)
: '';
const subject = resourceName || formatCount(affectedResourceCount, 'affected resource');
return `Patrol found ${getPatrolFindingIssueCountLabel(findingCount)} on ${subject}${compositionClause}. ${getPatrolQueueActionDetail(input)}`;
}
if (input.autonomyLocked) {
return 'Current Patrol issues appear here.';
}
switch (input.autonomyLevel) {
case 'approval':
return 'Current issues, investigations, and approvals appear here.';
case 'assisted':
return 'Current issues, fixes, and approvals appear here.';
case 'full':
return 'Current issues, automatic work, and approvals appear here.';
case 'monitor':
default:
return 'Current Patrol issues appear here.';
}
}
export function getPatrolWorkspaceWorkGroups(
input: PatrolWorkspaceWorkGroupsInput,
): PatrolWorkspaceWorkGroupSummary[] {
const groups: PatrolWorkspaceWorkGroupSummary[] = [];
const composition = input.workTypeComposition;
const pendingApprovalCount = normalizeCount(input.pendingApprovalCount);
const failedActionCount = normalizeCount(composition?.failed);
const recurringIssueCount = normalizeCount(composition?.recurring);
const latestRun = input.latestRun ?? null;
const latestRunFailed = hasFailedPatrolCheck(latestRun);
const statusErrorCount = normalizeCount(input.patrolStatus?.error_count);
if (pendingApprovalCount > 0) {
groups.push({
id: 'approvals',
label: `${formatCount(pendingApprovalCount, 'approval')} waiting`,
detail: 'Approve or reject requested fixes from the issue rows.',
tone: 'warning',
});
}
if (failedActionCount > 0) {
groups.push({
id: 'failed-actions',
label: `${formatCount(failedActionCount, 'failed action')}`,
detail: 'Review the failed action and verification state in the affected issue row.',
tone: 'danger',
});
}
if (!input.resourceScoped && (latestRunFailed || statusErrorCount > 0)) {
const checkedResources = normalizeCount(latestRun?.resources_checked);
groups.push({
id: 'failed-check',
label: 'Latest check needs review',
detail:
checkedResources > 0
? `Patrol checked ${formatCount(checkedResources, 'resource')} but ended with runtime issues.`
: 'The last Patrol check ended with runtime issues.',
tone: 'danger',
});
}
if (recurringIssueCount > 0) {
groups.push({
id: 'recurring',
label: `${formatCount(recurringIssueCount, 'recurring issue')}`,
detail: 'Current work includes issues that reappeared after earlier resolution.',
tone: 'warning',
});
}
if (
!input.resourceScoped &&
isPatrolCoverageStale({
latestRun,
nowMs: input.nowMs,
patrolStatus: input.patrolStatus,
})
) {
groups.push({
id: 'stale-protection',
label: 'Coverage stale',
detail:
'Patrol has not completed a fresh full check. Run Patrol to refresh current coverage.',
tone: 'warning',
});
}
return groups;
}
function shouldSuppressMonitorContextPatrolPosture(
input: MonitorContextPatrolPostureInput,
): boolean {
const composition = input.workTypeComposition;
const pendingApprovalCount = normalizeCount(input.pendingApprovalCount);
const findingCount = normalizeCount(input.findingCount);
const failedActionCount = normalizeCount(composition?.failed);
const approvalWorkCount = normalizeCount(composition?.approval);
const inProgressWorkCount = normalizeCount(composition?.inProgress);
const recurringIssueCount = normalizeCount(composition?.recurring);
const latestRun = input.latestRun ?? null;
const patrolStatus = input.patrolStatus ?? null;
const statusErrorCount = normalizeCount(patrolStatus?.error_count);
const statusFindingCount = normalizeCount(patrolStatus?.findings_count);
return (
findingCount > 0 ||
pendingApprovalCount > 0 ||
failedActionCount > 0 ||
approvalWorkCount > 0 ||
inProgressWorkCount > 0 ||
recurringIssueCount > 0 ||
statusFindingCount > 0 ||
patrolStatus?.running ||
statusErrorCount > 0 ||
hasFailedPatrolCheck(latestRun) ||
!isActivePatrolRuntime(patrolStatus) ||
isScheduledPatrolOverdue(patrolStatus, input.nowMs ?? Date.now())
);
}
export function getMonitorContextPatrolProtectionPosture(
input: MonitorContextPatrolPostureInput,
): MonitorContextPatrolPostureSummary[] {
if (normalizeCount(input.monitoredResourceCount) <= 0) {
return [];
}
const patrolStatus = input.patrolStatus ?? null;
if ((!input.latestRun && !patrolStatus) || shouldSuppressMonitorContextPatrolPosture(input)) {
return [];
}
const healthyTone: MetadataBadgeTone = patrolStatus?.healthy === false ? 'warning' : 'success';
const coverageLabel = getPatrolCoverageLabel(input.latestRun ?? null, patrolStatus);
const summaries: MonitorContextPatrolPostureSummary[] = [
{
id: 'coverage',
label: formatMonitorCoverageLabel(coverageLabel),
detail: coverageLabel
? 'Latest Patrol evidence is available while you review this monitor view.'
: 'Run Patrol to refresh current coverage for monitored resources.',
tone: coverageLabel ? healthyTone : 'warning',
},
{
id: 'open-work',
label: 'No Patrol work waiting',
detail: 'Current Patrol findings and approvals stay in Patrol. None are waiting now.',
tone: healthyTone,
},
];
if (patrolStatus?.enabled === false) {
summaries.push({
id: 'schedule',
label: 'Scheduled checks paused',
detail: 'Run Patrol manually or enable scheduled checks to keep coverage fresh.',
tone: 'warning',
});
} else if (patrolStatus?.next_patrol_at) {
summaries.push({
id: 'schedule',
label: 'Next check scheduled',
detail: 'Patrol is scheduled to check monitored resources again.',
tone: 'info',
});
} else {
summaries.push({
id: 'schedule',
label: 'Ready to run Patrol',
detail: 'Run Patrol from the Patrol page any time to refresh coverage.',
tone: 'info',
});
}
return summaries;
}
const PATROL_PRO_HANDOFF_ACTIONABLE_SEVERITIES = new Set(['critical', 'warning']);
export interface PatrolProInvestigationHandoff {
detail: string;
actionLabel?: string;
destination?: UpgradeDestination;
}
export interface PatrolProInvestigationHandoffInput {
autoFixLocked: boolean;
commercialSurfacesHidden: boolean;
upgradePromptsHidden: boolean;
upgradeDestination: UpgradeDestination;
severity?: string;
status?: string;
}
export function getPatrolProInvestigationHandoff(
input: PatrolProInvestigationHandoffInput,
): PatrolProInvestigationHandoff | undefined {
if (!input.autoFixLocked) return undefined;
if (input.commercialSurfacesHidden) return undefined;
if (input.status && input.status !== 'active') return undefined;
if (!input.severity || !PATROL_PRO_HANDOFF_ACTIONABLE_SEVERITIES.has(input.severity)) {
return undefined;
}
const handoff: PatrolProInvestigationHandoff = {
detail: 'Pulse Pro can investigate and fix issues like this.',
};
if (!input.upgradePromptsHidden) {
handoff.actionLabel = 'Learn about Pulse Pro';
handoff.destination = input.upgradeDestination;
}
return handoff;
}
export interface PatrolWatchOnlyInvestigationNudge {
detail: string;
actionLabel: string;
}
export interface PatrolWatchOnlyInvestigationNudgeInput {
autoFixLocked: boolean;
autonomyLevel?: PatrolAutonomyLevel;
severity?: string;
status?: string;
}
// Counterpart to the Pro handoff for installs where Patrol modes are already
// unlocked but the install still runs Watch only: without this, an actionable
// finding offers no forward path at all (the Investigate button requires a
// non-monitor mode), so the detect → investigate → propose → approve loop can
// never start.
export function getPatrolWatchOnlyInvestigationNudge(
input: PatrolWatchOnlyInvestigationNudgeInput,
): PatrolWatchOnlyInvestigationNudge | undefined {
if (input.autoFixLocked) return undefined;
if (input.autonomyLevel !== 'monitor') return undefined;
if (input.status && input.status !== 'active') return undefined;
if (!input.severity || !PATROL_PRO_HANDOFF_ACTIONABLE_SEVERITIES.has(input.severity)) {
return undefined;
}
return {
detail:
'Watch only reports issues without preparing fixes. In Ask first mode, Patrol investigates and queues each fix for your approval.',
actionLabel: 'Switch to Ask first',
};
}