|
3 | 3 | * windows; dashboard pool/service labels are deliberately not accepted as identity. |
4 | 4 | * Times are epoch milliseconds. Prior walls/stops must be carried by the caller; |
5 | 5 | * omission is NOT a release protocol. This module neither fetches nor persists. |
| 6 | + * |
| 7 | + * Stage-3 S4 (#12698, design D4): the caller may also carry forecast rows |
| 8 | + * (the S1 projection, per exact billing scope like observations). A fresh |
| 9 | + * forecast projected from the window's current reading that says projected |
| 10 | + * + wallsBeforeReset, with the projected wall at least the window's |
| 11 | + * configured forecastMarginMs before the reset, adds pace_hold to an |
| 12 | + * otherwise eligible window — and can do nothing else: it never overrides |
| 13 | + * a measured verdict, never manufactures evidence for an unknown window, |
| 14 | + * and a stale, superseded, conflicting or honesty-state forecast is |
| 15 | + * exactly current behaviour (#12698 criterion 3). Pool-level consumption |
| 16 | + * exclusions from S2's calibration (e.g. the bare opencode-go monthly |
| 17 | + * series) are the caller's duty: display pool identity is not accepted |
| 18 | + * here, so the caller assembles rows only for scopes it may consume. |
6 | 19 | */ |
7 | 20 | export type Verdict = "eligible" | "unknown" | "pace_hold" | "walled" | "stopped"; |
8 | 21 | export interface Scope { |
@@ -21,6 +34,10 @@ export interface Binding extends Scope { |
21 | 34 | collectionMs: number; |
22 | 35 | reservePct?: number; |
23 | 36 | pacePpHour?: number; |
| 37 | + /** S4: a projected wall at least this many ms before the reset |
| 38 | + * yields pace_hold. Absent = forecasts never affect this window |
| 39 | + * (per-window opt-in; the one live pool's windows carry it). */ |
| 40 | + forecastMarginMs?: number; |
24 | 41 | }[]; |
25 | 42 | } |
26 | 43 | export interface Observation extends Scope { |
@@ -49,12 +66,28 @@ export interface PriorWall extends Scope { |
49 | 66 | resetsAt: number; |
50 | 67 | evidenceId: string; |
51 | 68 | } |
| 69 | +export interface Forecast extends Scope { |
| 70 | + kind: string; |
| 71 | + period: string; |
| 72 | + /** Evidence id of the forecast row (the S1 product/ledger row). */ |
| 73 | + id: string; |
| 74 | + /** The underlying reading's stamp the projection was made from. */ |
| 75 | + measuredAt: number; |
| 76 | + resetsAt: number; |
| 77 | + /** The S1 projection state, verbatim (quota-analytics ProjectionState). */ |
| 78 | + state: "projected" | "walled" | "no-wall-before-reset" | "insufficient-history"; |
| 79 | + /** Projected instant the window reaches 100%, epoch ms. */ |
| 80 | + projectedWallAt: number | null; |
| 81 | + wallsBeforeReset: boolean | null; |
| 82 | +} |
52 | 83 | export interface AdmissionInput { |
53 | 84 | generation: string; |
54 | 85 | bindings: readonly Binding[]; |
55 | 86 | observations: readonly Observation[]; |
56 | 87 | stops: readonly Stop[]; |
57 | 88 | priorWalls: readonly PriorWall[]; |
| 89 | + /** S4 forecast rows, per exact scope; absent = pre-S4 behaviour. */ |
| 90 | + forecasts?: readonly Forecast[]; |
58 | 91 | } |
59 | 92 | export interface WindowDecision { |
60 | 93 | kind: string; |
@@ -93,6 +126,62 @@ const validScope = (s: Scope) => [s.provider, s.account, s.bucket].every(nonempt |
93 | 126 | const sorted = (s: readonly string[]) => [...new Set(s)].sort(); |
94 | 127 | const worst = (v: Verdict[]) => v.reduce((a, b) => (rank[a] >= rank[b] ? a : b), "eligible"); |
95 | 128 |
|
| 129 | +/** The one forecast row that may speak for this window, or null. A row |
| 130 | + * speaks only when it is the latest, internally consistent, projected |
| 131 | + * from the window's current reading, and still fresh by the same bound |
| 132 | + * the measured path applies to observations; every other row — stale, |
| 133 | + * superseded, conflicting, or an honesty state — is current behaviour |
| 134 | + * (#12698 criterion 3), never a reason and never a hold. */ |
| 135 | +function applicableForecast( |
| 136 | + input: AdmissionInput, |
| 137 | + b: Binding, |
| 138 | + w: Binding["windows"][number], |
| 139 | + now: number, |
| 140 | + latestMeasuredAt: number | null, |
| 141 | +): Forecast | null { |
| 142 | + const rows = (input.forecasts ?? []).filter( |
| 143 | + (x) => same(x, b) && x.kind === w.kind && x.period === w.period && x.resetsAt === w.resetsAt, |
| 144 | + ); |
| 145 | + if (!rows.length) return null; |
| 146 | + const ids = new Map<string, string>(); |
| 147 | + for (const x of rows) { |
| 148 | + if ( |
| 149 | + !nonempty(x.id) || |
| 150 | + !finite(x.measuredAt) || |
| 151 | + x.measuredAt > now || |
| 152 | + (x.projectedWallAt !== null && !finite(x.projectedWallAt)) || |
| 153 | + (x.wallsBeforeReset !== null && typeof x.wallsBeforeReset !== "boolean") || |
| 154 | + (x.state !== "projected" && |
| 155 | + x.state !== "walled" && |
| 156 | + x.state !== "no-wall-before-reset" && |
| 157 | + x.state !== "insufficient-history") |
| 158 | + ) |
| 159 | + return null; |
| 160 | + const signature = JSON.stringify([x.measuredAt, x.state, x.projectedWallAt, x.wallsBeforeReset]); |
| 161 | + if (ids.has(x.id) && ids.get(x.id) !== signature) return null; |
| 162 | + ids.set(x.id, signature); |
| 163 | + } |
| 164 | + const unique = [...new Map(rows.map((x) => [x.id, x])).values()].sort( |
| 165 | + (a, c) => c.measuredAt - a.measuredAt || ordinal(a.id, c.id), |
| 166 | + ); |
| 167 | + const latest = unique[0]; |
| 168 | + const tied = unique.filter((x) => x.measuredAt === latest.measuredAt); |
| 169 | + if ( |
| 170 | + tied.some( |
| 171 | + (x) => |
| 172 | + x.state !== latest.state || |
| 173 | + x.projectedWallAt !== latest.projectedWallAt || |
| 174 | + x.wallsBeforeReset !== latest.wallsBeforeReset, |
| 175 | + ) |
| 176 | + ) |
| 177 | + return null; |
| 178 | + if (latest.state !== "projected" || latest.wallsBeforeReset !== true || latest.projectedWallAt === null) return null; |
| 179 | + if (latest.projectedWallAt >= w.resetsAt) return null; |
| 180 | + if (now - latest.measuredAt > Math.min(w.freshnessMs, 60 * 60_000)) return null; |
| 181 | + if (latestMeasuredAt === null || latest.measuredAt < latestMeasuredAt) return null; |
| 182 | + return latest; |
| 183 | +} |
| 184 | + |
96 | 185 | function projectWindow(input: AdmissionInput, b: Binding, w: Binding["windows"][number], now: number): WindowDecision { |
97 | 186 | const out: WindowDecision = { |
98 | 187 | kind: w.kind, |
@@ -120,7 +209,8 @@ function projectWindow(input: AdmissionInput, b: Binding, w: Binding["windows"][ |
120 | 209 | !finite(w.collectionMs) || |
121 | 210 | w.collectionMs <= 0 || |
122 | 211 | (w.reservePct !== undefined && (!finite(w.reservePct) || w.reservePct < 0 || w.reservePct > 100)) || |
123 | | - (w.pacePpHour !== undefined && (!finite(w.pacePpHour) || w.pacePpHour < 0)) |
| 212 | + (w.pacePpHour !== undefined && (!finite(w.pacePpHour) || w.pacePpHour < 0)) || |
| 213 | + (w.forecastMarginMs !== undefined && (!finite(w.forecastMarginMs) || w.forecastMarginMs < 0)) |
124 | 214 | ) |
125 | 215 | return unknown("invalid_window_policy"); |
126 | 216 | if (w.resetsAt <= now) return unknown("period_expired"); |
@@ -234,6 +324,14 @@ function projectWindow(input: AdmissionInput, b: Binding, w: Binding["windows"][ |
234 | 324 | out.reasons.push("projected_exhaustion"); |
235 | 325 | if (previous) out.evidenceIds = sorted([...out.evidenceIds, previous.id]); |
236 | 326 | } |
| 327 | + if (out.verdict === "eligible" && w.forecastMarginMs !== undefined) { |
| 328 | + const f = applicableForecast(input, b, w, now, out.measuredAt); |
| 329 | + if (f && f.projectedWallAt !== null && w.resetsAt - f.projectedWallAt >= w.forecastMarginMs) { |
| 330 | + out.verdict = "pace_hold"; |
| 331 | + out.reasons.push("forecast_projected_wall"); |
| 332 | + out.evidenceIds = sorted([...out.evidenceIds, f.id]); |
| 333 | + } |
| 334 | + } |
237 | 335 | if (out.verdict === "eligible") out.reasons.push("fresh_window"); |
238 | 336 | return out; |
239 | 337 | } |
|