File size: 1,076 Bytes
39b3fb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { EntropyInput } from "./protocol.js";

export type EntropyWeights = {
  dependencies: number;
  failRate: number;
  latency: number;
};

export const DEFAULT_WEIGHTS: EntropyWeights = {
  dependencies: 0.3,
  failRate: 0.5,
  latency: 0.2,
};

export const DEFAULT_THRESHOLD = 0.21;

function clamp01(value: number): number {
  if (Number.isNaN(value) || !Number.isFinite(value)) {
    return 1;
  }
  return Math.min(1, Math.max(0, value));
}

export function entropyScore(task: EntropyInput, weights = DEFAULT_WEIGHTS): number {
  const dependencyPressure = clamp01(task.dependencies.length / 10);
  const failPressure = clamp01(task.failRate);
  const latencyPressure = clamp01(task.latencyMs / 10_000);

  return Number(
    (
      dependencyPressure * weights.dependencies +
      failPressure * weights.failRate +
      latencyPressure * weights.latency
    ).toFixed(4),
  );
}

export function isExecutable(task: EntropyInput, threshold = DEFAULT_THRESHOLD): boolean {
  return entropyScore(task) < threshold;
}