From ca8b06d6442af4128bf7c6b6d218433f019fec9f Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:51:02 -0600 Subject: [PATCH] aggregator(forecast): capped resampling trials are censored, not completions (#6386) resampledWeeksToComplete recorded a trial that hit the 100000-week cap as a completion at the cap, pulling P50/P80 earlier than the true completion (Codex P2 on PR #6169: buckets [1,1,1,1] with 100001 remaining published both dates at 100000 weeks, one week before the work can finish). Trials that do not finish within the cap are now censored observations (null, sorted last); a percentile whose rank falls on a censored trial returns no date, with a limitation naming the censored share, and when both percentiles are censored the confidence is unavailable. An exact early-censoring check (remaining units exceed what drawing the largest bucket every remaining week could cover) ends hopeless trials without changing their outcome. FORECAST_MODEL_VERSION bumps forecast/v2 -> forecast/v3: outputs change materially for horizon-exceeding scopes. Regression tests in forecast-3982.test.ts: the reviewer example verbatim (exact-arithmetic oracle: constant pace 1/week needs exactly 100001 weeks, so every trial is censored) and a partially censored distribution (buckets [0,0,0,4], 99600 remaining: P50 dated just inside the horizon, P80 censored, per the exact binomial CDF). --- pm-stack/aggregator/src/forecast.ts | 75 +++++++++++++++---- .../aggregator/test/forecast-3982.test.ts | 40 ++++++++++ 2 files changed, 99 insertions(+), 16 deletions(-) diff --git a/pm-stack/aggregator/src/forecast.ts b/pm-stack/aggregator/src/forecast.ts index 4a1e6a5dd5..5e314a1b56 100644 --- a/pm-stack/aggregator/src/forecast.ts +++ b/pm-stack/aggregator/src/forecast.ts @@ -24,7 +24,12 @@ // weeks-to-complete for the remaining units; P50 and the // conservative P80 are percentiles of that distribution. The seed // derives from the buckets and the remaining-unit count, so the -// same inputs always produce the same forecast. A scope whose +// same inputs always produce the same forecast. A trial that does +// not finish within the per-trial week cap is a CENSORED +// observation, never a completion at the cap: a percentile whose +// rank falls on a censored trial lies beyond the simulation +// horizon and returns no date, with a limitation saying so +// (#6386). A scope whose // total observed pace is zero still returns "no_measurable_pace": // resampling zero buckets can only ever draw zero. // - Effort stays explicit: remaining work is reported per effort @@ -46,7 +51,13 @@ import type { RefKey } from "./model.ts"; // Outputs for the same inputs change materially (bursty scopes with // a zero median bucket now forecast instead of returning // no_measurable_pace), so the model version bumps. -export const FORECAST_MODEL_VERSION = "forecast/v2"; +// forecast/v3 (#6386): capped resampling trials are censored +// observations, not completions at the cap. Outputs change +// materially for horizon-exceeding scopes (a percentile beyond the +// simulation horizon now returns no date instead of a date at the +// cap, which understated the true completion), so the version bumps +// again. +export const FORECAST_MODEL_VERSION = "forecast/v3"; export const STALL_MODEL_VERSION = "stall-risk/v1"; /** At/above this score an item is surfaced as "at risk" (§3). */ @@ -125,8 +136,13 @@ const RESAMPLE_TRIALS = 4096; /** Per-trial week cap: with any positive bucket a trial finishes * almost surely long before this; the cap only bounds pathological * inputs (a huge remaining count over near-zero pace) so the pure - * function always terminates. A capped trial reads as that many - * weeks, which is conservative for both percentiles. */ + * function always terminates. A trial that reaches the cap + * unfinished is CENSORED — it is not a completion at the cap. + * (Before #6386 a capped trial read as a completion at that many + * weeks; the old comment here called that "conservative", which is + * backwards for a trial whose true completion exceeds the cap: + * recording the cap pulls the percentile EARLIER than the true + * completion and publishes an optimistic date.) */ const RESAMPLE_MAX_WEEKS = 100_000; /** @@ -134,30 +150,41 @@ const RESAMPLE_MAX_WEEKS = 100_000; * completion buckets (zero weeks included — they are observations, * not gaps) with replacement until the drawn completions cover the * remaining units; the number of drawn weeks is one sample of the - * weeks-to-complete distribution. Returns the sorted samples. + * weeks-to-complete distribution. Returns the sorted samples, with + * a trial that does not finish within RESAMPLE_MAX_WEEKS as a + * censored observation (null, sorted last) — never as a completion + * at the cap (#6386). * Precondition: the buckets' total is positive (the caller returns * no_measurable_pace otherwise — resampling all-zero buckets can * only draw zero and no trial would ever finish). */ -function resampledWeeksToComplete(buckets: number[], remainingUnits: number): number[] { +function resampledWeeksToComplete(buckets: number[], remainingUnits: number): (number | null)[] { const rng = mulberry32(estimatorSeed(buckets, remainingUnits)); - const samples: number[] = []; + const maxBucket = Math.max(...buckets); + const samples: (number | null)[] = []; for (let trial = 0; trial < RESAMPLE_TRIALS; trial++) { let completed = 0; let weeks = 0; while (completed < remainingUnits && weeks < RESAMPLE_MAX_WEEKS) { + // Exact early censoring: when the units still remaining exceed + // what drawing the largest bucket every remaining week could + // cover, this trial cannot finish within the cap — it is + // censored now, and no further draws can change that. + if (remainingUnits - completed > (RESAMPLE_MAX_WEEKS - weeks) * maxBucket) break; completed += buckets[Math.floor(rng() * buckets.length)]!; weeks++; } - samples.push(weeks); + samples.push(completed >= remainingUnits ? weeks : null); } - return samples.sort((a, b) => a - b); + return samples.sort((a, b) => (a ?? Number.POSITIVE_INFINITY) - (b ?? Number.POSITIVE_INFINITY)); } -/** Nearest-rank percentile of an ascending-sorted sample. */ -function percentile(sortedXs: number[], q: number): number { +/** Nearest-rank percentile of an ascending-sorted sample, or null + * when the rank falls on a censored sample: that percentile lies + * beyond the simulation horizon, so no weeks value is defensible. */ +function percentile(sortedXs: (number | null)[], q: number): number | null { const idx = Math.min(sortedXs.length - 1, Math.max(0, Math.ceil(q * sortedXs.length) - 1)); - return sortedXs[idx]!; + return sortedXs[idx] ?? null; } export type ForecastConfidence = "high" | "medium" | "low" | "unavailable"; @@ -418,12 +445,23 @@ export function deriveEpicEta(input: EpicEtaInput): EpicEta { // the median weeks-to-complete; P80 is the conservative (slower) // tail of the same distribution, so P50 <= P80 by construction. const weeksToComplete = resampledWeeksToComplete(paceSeries, remainingUnits); - result.p50 = etaDateForWeeks(asOf, percentile(weeksToComplete, 0.5)); - if (result.p50 === null) { + const censoredTrials = weeksToComplete.filter((w) => w === null).length; + const p50Weeks = percentile(weeksToComplete, 0.5); + const p80Weeks = percentile(weeksToComplete, 0.8); + result.p50 = p50Weeks === null ? null : etaDateForWeeks(asOf, p50Weeks); + if (p50Weeks === null) { + limitations.push( + `P50 exceeds the simulation horizon: ${censoredTrials} of ${RESAMPLE_TRIALS} resampled trials did not complete within the ${RESAMPLE_MAX_WEEKS}-week cap (censored observations, not completions), so no P50 date is returned`, + ); + } else if (result.p50 === null) { limitations.push("P50 horizon exceeds the representable date range; no P50 date is returned"); } - result.p80 = etaDateForWeeks(asOf, percentile(weeksToComplete, 0.8)); - if (result.p80 === null) { + result.p80 = p80Weeks === null ? null : etaDateForWeeks(asOf, p80Weeks); + if (p80Weeks === null) { + limitations.push( + `P80 exceeds the simulation horizon: ${censoredTrials} of ${RESAMPLE_TRIALS} resampled trials did not complete within the ${RESAMPLE_MAX_WEEKS}-week cap (censored observations, not completions), so no P80 date is returned`, + ); + } else if (result.p80 === null) { limitations.push("P80 horizon exceeds the representable date range; no P80 date is returned"); } @@ -434,6 +472,11 @@ export function deriveEpicEta(input: EpicEtaInput): EpicEta { result.confidence = completionsComparison >= 12 && comparison.length >= 8 && stable ? "high" : "medium"; if (!stable) limitations.push("4-week and 8-week rates differ materially; confidence capped"); } + if (p50Weeks === null && p80Weeks === null) { + // Both percentiles lie beyond the simulation horizon: no date + // was returned at all, so no confidence attaches to one. + result.confidence = "unavailable"; + } return result; } diff --git a/pm-stack/aggregator/test/forecast-3982.test.ts b/pm-stack/aggregator/test/forecast-3982.test.ts index 78be03b5db..7fcbb44150 100644 --- a/pm-stack/aggregator/test/forecast-3982.test.ts +++ b/pm-stack/aggregator/test/forecast-3982.test.ts @@ -349,6 +349,46 @@ describe("deriveEpicEta", () => { } }); + test("capped trials are censored, not completions: buckets [1, 1, 1, 1] with 100001 remaining publish no date (#6386)", () => { + // The Codex P2 example from PR #6169, verbatim. ORACLE (exact + // arithmetic, no sampling): a constant pace of 1 unit/week needs + // exactly 100001 weeks for 100001 units, so EVERY resampled trial + // exceeds the 100000-week cap and is censored — neither + // percentile is inside the simulation horizon. Before the fix + // each capped trial was recorded as a completion at the cap and + // both dates were published at 100000 weeks, one week before + // the work can finish. + const eta = deriveEpicEta({ subjectRef: EPIC, cutoffs: historyForBuckets([1, 1, 1, 1], 100_001) }); + expect(eta.status).toBe("forecast"); + expect(eta.remainingUnits).toBe(100_001); + expect(eta.p50).toBeNull(); + expect(eta.p80).toBeNull(); + expect(eta.confidence).toBe("unavailable"); + expect(eta.limitations.some((l) => l.includes("P50") && l.includes("censored"))).toBe(true); + expect(eta.limitations.some((l) => l.includes("P80") && l.includes("censored"))).toBe(true); + }); + + test("partially censored distribution: P50 inside the horizon is dated, P80 beyond it is not (#6386)", () => { + // Buckets [0, 0, 0, 4] with 99600 remaining: a trial finishes + // within the cap iff 4 * Binomial(100000, 1/4) >= 99600, which + // the exact binomial CDF puts at ~77% of trials — so the median + // trial completes (just inside the horizon) while the 80th + // percentile lies beyond it and must return no date. (Sampling + // margin: the censored share sits ~5 sigma from the 20% that + // would move the P80 rank onto a finite sample.) + const eta = deriveEpicEta({ subjectRef: EPIC, cutoffs: historyForBuckets([0, 0, 0, 4], 99_600) }); + expect(eta.status).toBe("forecast"); + expect(eta.remainingUnits).toBe(99_600); + expect(eta.p50).not.toBeNull(); + const p50Weeks = Math.round((Date.parse(eta.p50!) - Date.parse(eta.asOf)) / (7 * 24 * 3600 * 1000)); + expect(p50Weeks).toBeGreaterThan(98_000); + expect(p50Weeks).toBeLessThanOrEqual(100_000); + expect(eta.p80).toBeNull(); + expect(eta.confidence).not.toBe("unavailable"); + expect(eta.limitations.some((l) => l.includes("P80") && l.includes("censored"))).toBe(true); + expect(eta.limitations.some((l) => l.includes("P50 exceeds"))).toBe(false); + }); + test("all-zero borrowed baseline buckets: still no_measurable_pace, no dates (#5274)", () => { // Local evidence is below the floor (1 completion in the primary // window) and the borrowed baseline's buckets are all zero: the