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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 23x 23x 1x 1x 1x 17x 17x 1x 1x 1x 16x 16x 17x 17x 17x 16x 16x 16x 1x 1x 1x 16x 17x 17x 15x 15x 15x 15x 15x 15x 15x 17x 1x 1x 15x 15x 1x 1x 1x 8x 8x 8x 8x 7x 7x 7x 7x 7x 8x 8x 8x 8x 1x 1x 1x 1x 1x 8x 8x 8x 8x 1x 1x 1x 3x 1x 1x 3x 3x 3x 2x 3x 2x 2x 1x 1x 1x 22x 22x 22x 22x | // lib/reflection/policy.ts
// ========================================
// Policy Manager with Adjustment Support
// ========================================
import { isDryRun, isVerbose } from './flags';
import {
POLICY_NAMES,
type PolicyName,
type Policy,
type PolicyState,
type PolicyAdjustment,
type FeedbackDecision,
} from './types';
// ========================================
// Default & Initial State
// ========================================
const DEFAULT_POLICIES: Record<string, Policy> = {
[POLICY_NAMES.THROTTLE_WINDOWS]: { name: POLICY_NAMES.THROTTLE_WINDOWS, value: 100 }, // ms between window opens
[POLICY_NAMES.DEFER_ANIMATIONS]: { name: POLICY_NAMES.DEFER_ANIMATIONS, value: false }, // Whether to defer non-critical animations
[POLICY_NAMES.ADJUST_PREFETCH]: { name: POLICY_NAMES.ADJUST_PREFETCH, value: 1.0 }, // Multiplier for prefetching resources (1.0 = normal)
[POLICY_NAMES.TUNE_FOCUS_TIMEOUT]: { name: POLICY_NAMES.TUNE_FOCUS_TIMEOUT, value: 150 }, // ms for focus-related timeouts
};
const createInitialPolicyState = (): PolicyState => ({
version: 1,
policies: { ...DEFAULT_POLICIES },
timestamp: performance.now(),
});
// ========================================
// Module State
// ========================================
let currentState: PolicyState = createInitialPolicyState();
const history: PolicyState[] = [currentState];
// ========================================
// Public API
// ========================================
/**
* Returns the current policy state.
*/
export function getPolicyState(): Readonly<PolicyState> {
return currentState;
}
/**
* Returns the value of a specific policy.
* @param name The name of the policy to retrieve.
* @returns The value of the policy.
*/
export function getPolicyValue(name: PolicyName): number | boolean {
return currentState.policies[name]?.value ?? DEFAULT_POLICIES[name].value;
}
/**
* Applies a set of adjustments to the current policy state.
* Creates a new, versioned policy state.
* Respects DRY_RUN flag.
*
* @param adjustments An array of policy adjustments to apply.
* @returns The new policy state, or the current state if in DRY_RUN mode.
*/
export function applyPolicyAdjustments(adjustments: PolicyAdjustment[]): PolicyState {
if (!adjustments?.length) return currentState;
if (isDryRun()) {
if (isVerbose()) {
console.log('[Reflection/Policy] DRY RUN: Would apply adjustments:', adjustments);
}
return currentState;
}
const newPolicies = { ...currentState.policies };
let hasChanges = false;
for (const adj of adjustments) {
const prevValue = newPolicies[adj.policyName]?.value;
if (prevValue !== adj.newValue) {
newPolicies[adj.policyName] = { name: adj.policyName, value: adj.newValue };
hasChanges = true;
if (isVerbose()) {
console.log(
`[Reflection/Policy] Adjusted ${adj.policyName}: ${prevValue} → ${adj.newValue}`
);
}
}
}
if (!hasChanges) return currentState;
const newState: PolicyState = {
version: currentState.version + 1,
policies: newPolicies,
timestamp: performance.now(),
};
currentState = newState;
history.push(newState);
if (isVerbose()) {
console.log(`[Reflection/Policy] Policy state updated to v${newState.version}`, newState);
}
return newState;
}
/**
* Applies a single policy change from a feedback decision.
* This is the bridge called by the Feedback system.
*/
export function applyPolicyChange(decision: FeedbackDecision): void {
if (isDryRun()) {
if (isVerbose()) {
console.log('[Reflection/Policy] DRY RUN (applyPolicyChange):', decision);
}
return;
}
const adjustments: PolicyAdjustment[] = [];
switch (decision.action) {
case 'THROTTLE_WINDOWS':
adjustments.push({
policyName: POLICY_NAMES.THROTTLE_WINDOWS,
newValue: 500, // ms delay between new windows
});
break;
case 'DEFER_ANIM':
case 'DEFER_ANIMATIONS':
adjustments.push({
policyName: POLICY_NAMES.DEFER_ANIMATIONS,
newValue: true,
});
break;
case 'ADJUST_PREFETCH':
adjustments.push({
policyName: POLICY_NAMES.ADJUST_PREFETCH,
newValue: 0.5, // reduce prefetch intensity
});
break;
case 'TUNE_FOCUS_TIMEOUT':
adjustments.push({
policyName: POLICY_NAMES.TUNE_FOCUS_TIMEOUT,
newValue: 200, // increase focus timeout
});
break;
default:
if (isVerbose()) {
console.warn('[Reflection/Policy] Unknown decision action:', decision.action);
}
return;
}
applyPolicyAdjustments(adjustments);
}
/**
* Reverts the policy state to a previous version.
* @param version The version number to revert to. If not provided, reverts to the previous version.
* @returns The policy state after rollback, or null if the version is invalid.
*/
export function rollbackPolicy(version?: number): PolicyState | null {
if (history.length < 2) {
return null; // Cannot rollback initial state
}
const targetVersion = version ?? currentState.version - 1;
const targetState = history.find((s) => s.version === targetVersion);
if (!targetState) {
console.error(
`[Reflection/Policy] Cannot find policy version ${targetVersion} to roll back to.`
);
return null;
}
currentState = targetState;
if (isVerbose()) {
console.warn(
`[Reflection/Policy] Rolled back to policy state v${targetState.version}`,
targetState
);
}
return currentState;
}
/**
* Resets the policy manager to its initial state.
* (For testing purposes)
*/
export function _resetPolicyManager(): void {
currentState = createInitialPolicyState();
history.length = 0;
history.push(currentState);
}
|