Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 1x 1x 1x 1x 1x 1x 19x 19x 19x 4x 4x 15x 19x 1x 1x 14x 19x 1x 1x 13x 19x 2x 2x 11x 11x 19x 1x 1x 1x 1x 1x 33x 33x 8x 8x 8x 8x 8x 8x 8x 19x 6x 6x 23x 2x 2x 2x 33x 1x 1x 1x 1x 1x 27x 2x 2x 2x 2x 2x 2x 25x 25x 25x 25x 25x 25x 27x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 3x 3x 3x 3x 1x 1x 3x 3x 4x 1x 1x 1x 1x 1x 1x 2x 2x 4x 1x 1x 1x 16x 16x 16x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x | /**
* Resource Governor
*
* Enforces resource limits and quiet hours policies for the agent.
* Determines whether the agent should accept new jobs based on
* current resource usage and configured governance rules.
*/
import { ResourceGovernance, QuietHoursConfig } from './config/types.js';
export interface ResourceSnapshot {
cpuPercent: number;
memoryPercent: number;
diskFreeGB: number;
activeJobs: number;
}
export interface JobRequirements {
cpuCores?: number;
memoryGB?: number;
}
export interface AcceptanceDecision {
allowed: boolean;
reason?: string;
}
export interface EffectiveLimits {
maxCpu: number;
maxMemory: number;
maxJobs: number;
}
export class ResourceGovernor {
constructor(
private config: ResourceGovernance,
private maxConcurrentJobs: number = 1,
private nowFn: () => Date = () => new Date()
) {}
/**
* Check if the agent should accept a new job based on current resources.
* @param snapshot Current resource usage snapshot
*/
canAcceptJob(snapshot: ResourceSnapshot): AcceptanceDecision {
const limits = this.getEffectiveLimits();
if (snapshot.activeJobs >= limits.maxJobs) {
return { allowed: false, reason: `Active jobs (${snapshot.activeJobs}) at or above limit (${limits.maxJobs})` };
}
if (snapshot.cpuPercent >= limits.maxCpu) {
return { allowed: false, reason: `CPU usage (${snapshot.cpuPercent}%) exceeds limit (${limits.maxCpu}%)` };
}
if (snapshot.memoryPercent >= limits.maxMemory) {
return { allowed: false, reason: `Memory usage (${snapshot.memoryPercent}%) exceeds limit (${limits.maxMemory}%)` };
}
if (snapshot.diskFreeGB < this.config.reservedDiskGB) {
return { allowed: false, reason: `Free disk (${snapshot.diskFreeGB}GB) below reserve (${this.config.reservedDiskGB}GB)` };
}
return { allowed: true };
}
/**
* Check if the agent is currently in quiet hours.
*/
isQuietHours(): boolean {
const qh = this.config.quietHours;
if (!qh || !qh.schedule) return false;
const now = this.getCurrentTimeInTimezone(qh);
const [startStr, endStr] = qh.schedule.split('-');
const startMinutes = this.parseTimeToMinutes(startStr);
const endMinutes = this.parseTimeToMinutes(endStr);
const nowMinutes = now.hours * 60 + now.minutes;
if (startMinutes <= endMinutes) {
// Same-day range (e.g., 22:00-23:00 or 09:00-17:00)
return nowMinutes >= startMinutes && nowMinutes < endMinutes;
} else {
// Overnight range (e.g., 22:00-06:00)
return nowMinutes >= startMinutes || nowMinutes < endMinutes;
}
}
/**
* Get effective resource limits, considering quiet hours.
*/
getEffectiveLimits(): EffectiveLimits {
if (this.isQuietHours() && this.config.quietHours) {
return {
maxCpu: this.config.quietHours.maxCpuPercent,
maxMemory: Math.min(this.config.maxMemoryPercent, this.config.quietHours.maxCpuPercent),
maxJobs: this.config.quietHours.maxConcurrentJobs
};
}
return {
maxCpu: this.config.maxCpuPercent,
maxMemory: this.config.maxMemoryPercent,
maxJobs: this.maxConcurrentJobs
};
}
/**
* Check if accepting a job with given requirements would exceed limits.
* @param jobRequirements CPU and memory requirements for the job
* @param currentSnapshot Current resource usage
*/
wouldExceedLimits(jobRequirements: JobRequirements, currentSnapshot?: ResourceSnapshot): boolean {
const limits = this.getEffectiveLimits();
if (jobRequirements.cpuCores !== undefined) {
const currentCpu = currentSnapshot?.cpuPercent ?? 0;
// Rough estimate: each core ~= some percent of total
// We just check if adding estimated usage would exceed limit
if (currentCpu + (jobRequirements.cpuCores * 10) > limits.maxCpu) {
return true;
}
}
if (jobRequirements.memoryGB !== undefined) {
const currentMem = currentSnapshot?.memoryPercent ?? 0;
// Rough estimate: each GB ~= some percent
if (currentMem + (jobRequirements.memoryGB * 5) > limits.maxMemory) {
return true;
}
}
return false;
}
/** Parse "HH:MM" to total minutes */
private parseTimeToMinutes(time: string): number {
const [hours, minutes] = time.split(':').map(Number);
return hours * 60 + minutes;
}
/** Get current time in the configured timezone */
private getCurrentTimeInTimezone(qh: QuietHoursConfig): { hours: number; minutes: number } {
const now = this.nowFn();
if (qh.timezone) {
try {
const formatted = now.toLocaleTimeString('en-US', {
timeZone: qh.timezone,
hour12: false,
hour: '2-digit',
minute: '2-digit'
});
const [hours, minutes] = formatted.split(':').map(Number);
return { hours, minutes };
} catch {
// Fall back to local time if timezone is invalid
}
}
return { hours: now.getHours(), minutes: now.getMinutes() };
}
}
|