GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #13271 from developing-today/lane-ur-12698 · developing-today/code@3d73cbb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pm-stack/aggregator
    • src
      • quota-admission-forecast-config.ts
      • quota-admission.ts
    • test
      • quota-admission-forecast-config.test.ts
      • quota-admission-forecast.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pm-stack/aggregator/src/quota-admission-forecast-config.ts‎

Copy file name to clipboard
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Live forecast-admission switch for Stage-3 S4 (#12698, epic
2+
// #12694; design doc/2026-11-22T22-35-00Z_design_quota-plane-stage-3,
3+
// decision D4), in the PM_AGG_STORE_BACKEND posture (store-backend.ts):
4+
//
5+
// PM_AGG_FORECAST_ADMISSION "off" (default) | "on"
6+
// PM_AGG_FORECAST_ADMISSION_POOL canonical provider id of the ONE
7+
// pool forecast admission is live
8+
// for (required when on)
9+
// PM_AGG_FORECAST_ADMISSION_MARGIN_MS
10+
// a projected wall at least this
11+
// many ms before the window reset
12+
// yields pace_hold (required when
13+
// on; positive integer)
14+
//
15+
// The default is today's behaviour: no forecast input reaches the
16+
// admission projection. Flipping the flag back to "off" is the whole
17+
// rollback — no deploy, no data change. Pool and margin are
18+
// configuration set from the S2 calibration (#12696); once live they
19+
// change only via the design's proposal mechanism (D5). An assembler
20+
// that supplies forecast rows to buildQuotaAdmission reads this
21+
// config; the projection itself stays pure and env-free. Any invalid
22+
// value fails loudly, never silently defaults.
23+
24+
export interface ForecastAdmissionConfig {
25+
enabled: boolean;
26+
/** Canonical provider id of the one live pool; null while off. */
27+
providerId: string | null;
28+
/** Configured margin in ms; null while off. */
29+
marginMs: number | null;
30+
}
31+
32+
export function parseForecastAdmissionConfig(
33+
env: Record<string, string | undefined> = process.env,
34+
): ForecastAdmissionConfig {
35+
const raw = (env.PM_AGG_FORECAST_ADMISSION ?? "").trim();
36+
if (raw === "" || raw === "off") return { enabled: false, providerId: null, marginMs: null };
37+
if (raw !== "on") {
38+
throw new Error(
39+
`Invalid PM_AGG_FORECAST_ADMISSION=${JSON.stringify(env.PM_AGG_FORECAST_ADMISSION)}; expected "off" or "on"`,
40+
);
41+
}
42+
const providerId = (env.PM_AGG_FORECAST_ADMISSION_POOL ?? "").trim();
43+
if (!providerId) {
44+
throw new Error("PM_AGG_FORECAST_ADMISSION=on requires PM_AGG_FORECAST_ADMISSION_POOL (the one live pool)");
45+
}
46+
const marginRaw = (env.PM_AGG_FORECAST_ADMISSION_MARGIN_MS ?? "").trim();
47+
const marginMs = Number(marginRaw);
48+
if (!marginRaw || !Number.isInteger(marginMs) || marginMs <= 0) {
49+
throw new Error(
50+
`Invalid PM_AGG_FORECAST_ADMISSION_MARGIN_MS=${JSON.stringify(env.PM_AGG_FORECAST_ADMISSION_MARGIN_MS)}; expected a positive integer (ms)`,
51+
);
52+
}
53+
return { enabled: true, providerId, marginMs };
54+
}
Collapse file

‎pm-stack/aggregator/src/quota-admission.ts‎

Copy file name to clipboardExpand all lines: pm-stack/aggregator/src/quota-admission.ts
+99-1Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
* windows; dashboard pool/service labels are deliberately not accepted as identity.
44
* Times are epoch milliseconds. Prior walls/stops must be carried by the caller;
55
* 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.
619
*/
720
export type Verdict = "eligible" | "unknown" | "pace_hold" | "walled" | "stopped";
821
export interface Scope {
@@ -21,6 +34,10 @@ export interface Binding extends Scope {
2134
collectionMs: number;
2235
reservePct?: number;
2336
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;
2441
}[];
2542
}
2643
export interface Observation extends Scope {
@@ -49,12 +66,28 @@ export interface PriorWall extends Scope {
4966
resetsAt: number;
5067
evidenceId: string;
5168
}
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+
}
5283
export interface AdmissionInput {
5384
generation: string;
5485
bindings: readonly Binding[];
5586
observations: readonly Observation[];
5687
stops: readonly Stop[];
5788
priorWalls: readonly PriorWall[];
89+
/** S4 forecast rows, per exact scope; absent = pre-S4 behaviour. */
90+
forecasts?: readonly Forecast[];
5891
}
5992
export interface WindowDecision {
6093
kind: string;
@@ -93,6 +126,62 @@ const validScope = (s: Scope) => [s.provider, s.account, s.bucket].every(nonempt
93126
const sorted = (s: readonly string[]) => [...new Set(s)].sort();
94127
const worst = (v: Verdict[]) => v.reduce((a, b) => (rank[a] >= rank[b] ? a : b), "eligible");
95128

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+
96185
function projectWindow(input: AdmissionInput, b: Binding, w: Binding["windows"][number], now: number): WindowDecision {
97186
const out: WindowDecision = {
98187
kind: w.kind,
@@ -120,7 +209,8 @@ function projectWindow(input: AdmissionInput, b: Binding, w: Binding["windows"][
120209
!finite(w.collectionMs) ||
121210
w.collectionMs <= 0 ||
122211
(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))
124214
)
125215
return unknown("invalid_window_policy");
126216
if (w.resetsAt <= now) return unknown("period_expired");
@@ -234,6 +324,14 @@ function projectWindow(input: AdmissionInput, b: Binding, w: Binding["windows"][
234324
out.reasons.push("projected_exhaustion");
235325
if (previous) out.evidenceIds = sorted([...out.evidenceIds, previous.id]);
236326
}
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+
}
237335
if (out.verdict === "eligible") out.reasons.push("fresh_window");
238336
return out;
239337
}
Collapse file

‎pm-stack/aggregator/test/quota-admission-forecast-config.test.ts‎

Copy file name to clipboard
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { parseForecastAdmissionConfig } from "../src/quota-admission-forecast-config.ts";
3+
4+
// Oracle: Stage-3 design D4 (#12698) — live forecast admission enters
5+
// behind a backend-style flag in the PM_AGG_STORE_BACKEND posture:
6+
// default is today's behaviour, the flag names ONE pool and the
7+
// configured margin, and flipping the flag back is the whole rollback.
8+
// Invalid configuration fails loudly, never silently defaults.
9+
describe("forecast admission flag: oracle = design D4 flag posture", () => {
10+
test("unset flag is off — today's behaviour is the default", () => {
11+
expect(parseForecastAdmissionConfig({})).toEqual({ enabled: false, providerId: null, marginMs: null });
12+
});
13+
14+
test("explicit off is off even with pool and margin present", () => {
15+
const c = parseForecastAdmissionConfig({
16+
PM_AGG_FORECAST_ADMISSION: "off",
17+
PM_AGG_FORECAST_ADMISSION_POOL: "opendesign",
18+
PM_AGG_FORECAST_ADMISSION_MARGIN_MS: "3600000",
19+
});
20+
expect(c.enabled).toBe(false);
21+
});
22+
23+
test("on requires a named pool and a positive margin", () => {
24+
expect(() => parseForecastAdmissionConfig({ PM_AGG_FORECAST_ADMISSION: "on" })).toThrow();
25+
expect(() =>
26+
parseForecastAdmissionConfig({ PM_AGG_FORECAST_ADMISSION: "on", PM_AGG_FORECAST_ADMISSION_POOL: "opendesign" }),
27+
).toThrow();
28+
expect(() =>
29+
parseForecastAdmissionConfig({
30+
PM_AGG_FORECAST_ADMISSION: "on",
31+
PM_AGG_FORECAST_ADMISSION_POOL: "opendesign",
32+
PM_AGG_FORECAST_ADMISSION_MARGIN_MS: "0",
33+
}),
34+
).toThrow();
35+
expect(() =>
36+
parseForecastAdmissionConfig({
37+
PM_AGG_FORECAST_ADMISSION: "on",
38+
PM_AGG_FORECAST_ADMISSION_POOL: "opendesign",
39+
PM_AGG_FORECAST_ADMISSION_MARGIN_MS: "abc",
40+
}),
41+
).toThrow();
42+
});
43+
44+
test("on with pool and margin parses", () => {
45+
expect(
46+
parseForecastAdmissionConfig({
47+
PM_AGG_FORECAST_ADMISSION: "on",
48+
PM_AGG_FORECAST_ADMISSION_POOL: "opendesign",
49+
PM_AGG_FORECAST_ADMISSION_MARGIN_MS: "3600000",
50+
}),
51+
).toEqual({ enabled: true, providerId: "opendesign", marginMs: 3_600_000 });
52+
});
53+
54+
test("an unrecognized flag value fails loudly", () => {
55+
expect(() => parseForecastAdmissionConfig({ PM_AGG_FORECAST_ADMISSION: "yes" })).toThrow(
56+
/PM_AGG_FORECAST_ADMISSION/,
57+
);
58+
});
59+
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.