Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
1.9 KB
Raw Blame
import type { RemediationStep } from '../core/remediation-step.ts';
import { isManualOnlyStep } from '../core/remediation/manual-only.ts';

export { isManualOnlyStep };

export const AUTOPILOT_FULL_CYCLE_FLOOR_MINUTES = 60;

export interface AutopilotRemediationPlanShape {
  /** null: the tick's health read was degraded (#7236), so the score is unknown. */
  score: number | null;
  planLength: number;
  estimatedSeconds: number;
  minutesSinceLastFull: number;
}

/**
 * Keep recommendation keys stable for doctor/remediate checkpoints while
 * giving Autopilot a fresh single-flight slot on every dispatch interval.
 */
export function autopilotRemediationIdempotencyKey(
  recommendationKey: string,
  dispatchSlot: string,
): string {
  return `${recommendationKey}:autopilot:${dispatchSlot}`;
}

/**
 * A full cycle is a freshness invariant, independent of the current score or
 * targeted plan. Large/slow/severely degraded plans retain the existing
 * hammer behavior before the freshness floor is reached.
 */
export function shouldRunAutopilotFullCycle({
  score,
  planLength,
  estimatedSeconds,
  minutesSinceLastFull,
}: AutopilotRemediationPlanShape): boolean {
  return minutesSinceLastFull >= AUTOPILOT_FULL_CYCLE_FLOOR_MINUTES
    || planLength > 3
    || estimatedSeconds >= 300
    || (score !== null && score < 70);
}

/** An unknown (null) score never sleeps as healthy. */
export function shouldSleepHealthyAutopilot(
  score: number | null,
  planLength: number,
  minutesSinceLastFull: number,
): boolean {
  return score !== null
    && score >= 95
    && planLength === 0
    && minutesSinceLastFull < AUTOPILOT_FULL_CYCLE_FLOOR_MINUTES;
}

/** The steps a targeted dispatch submits: never a manual-only step. */
export function autopilotTargetedSteps<T extends Pick<RemediationStep, 'job'>>(plan: readonly T[]): T[] {
  return plan.filter((step) => !isManualOnlyStep(step));
}