Repository navigation
aggregator(forecast): design §1 resampling estimator in deriveEpicEta (#5274) #6169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,80 @@ function history(total: number, closesAtWeek: (number | null)[]): ItemHistoryCut | |
| ); | ||
| } | ||
|
|
||
|
|
||
| // --- Generated model-based coverage for the design \u00a71 resampling | ||
| // estimator (Codex P1 on PR #6169; testing doctrine: PBT/MBT with a | ||
| // named oracle). The fixed examples alone cannot detect a wrong | ||
| // resampling or percentile calculation (any deterministic ordered | ||
| // pair of dates passes them), so the estimator is also checked | ||
| // against an exact reference model over generated inputs. | ||
| // | ||
| // ORACLE (named): the exact completion-time distribution of the | ||
| // design \u00a71 model, computed by dynamic programming \u2014 each week | ||
| // independently draws one observed bucket uniformly with replacement, | ||
| // so the distribution of cumulative completions after w weeks is an | ||
| // exact convolution, and the completion-time quantile is the | ||
| // smallest w whose exact CDF reaches q. The oracle shares no code | ||
| // with the estimator's PRNG, sampling, or percentile code, so it | ||
| // decides the expected quantiles independently. | ||
| // | ||
| // SAMPLING TOLERANCE (explicit): the estimator reports nearest-rank | ||
| // quantiles of 4096 resampled trials, so its weeks can differ from | ||
| // the exact oracle by sampling error. Calibration over the generated | ||
| // range below (buckets 0..4, remaining 1..10) showed a maximum | ||
| // deviation of 1 week across 294 forecast cases; the tolerance is 2 | ||
| // weeks \u2014 twice the observed maximum, and far smaller than the | ||
| // error a swapped percentile, a wrong bucket series, or a constant | ||
| // date would produce (the constant-bucket cases below are exact, | ||
| // tolerance zero). | ||
| const SAMPLING_TOLERANCE_WEEKS = 2; | ||
|
|
||
| function exactWeeksQuantile(buckets: number[], remaining: number, q: number): number { | ||
| let dist = new Map<number, number>([[0, 1]]); | ||
| for (let w = 1; w <= 100_000; w++) { | ||
| const next = new Map<number, number>(); | ||
| for (const [sum, p] of dist) { | ||
| for (const b of buckets) { | ||
| const s = Math.min(remaining, sum + b); | ||
| next.set(s, (next.get(s) ?? 0) + p / buckets.length); | ||
| } | ||
| } | ||
| dist = next; | ||
| if ((dist.get(remaining) ?? 0) >= q) return w; | ||
| } | ||
| throw new Error("oracle did not converge"); | ||
| } | ||
|
|
||
| /** Realize primary buckets b[0..3] as closures at weeks 1..4 plus | ||
| * `remaining` children that never close, over the first 5 cutoffs. */ | ||
| function historyForBuckets(buckets: number[], remaining: number): ItemHistoryCutoff[] { | ||
| const closes: (number | null)[] = []; | ||
| buckets.forEach((count, i) => { | ||
| for (let k = 0; k < count; k++) closes.push(i + 1); | ||
| }); | ||
| for (let k = 0; k < remaining; k++) closes.push(null); | ||
| return history(closes.length, closes).slice(0, 5); | ||
| } | ||
|
|
||
| function etaWeeks(eta: { p50: string | null; p80: string | null; asOf: string }): { p50: number; p80: number } { | ||
| const toWeeks = (d: string | null) => Math.round((Date.parse(d!) - Date.parse(eta.asOf)) / (7 * 24 * 3600 * 1000)); | ||
| return { p50: toWeeks(eta.p50), p80: toWeeks(eta.p80) }; | ||
| } | ||
|
|
||
| /** Deterministic case generator (fixed seed: the suite is a | ||
| * regression pin, not a fuzz run). */ | ||
| function generatedEstimatorCases(count: number): { buckets: number[]; remaining: number }[] { | ||
| let s = 0x5eed_5274; | ||
| const rnd = () => { | ||
| s = (Math.imul(s, 1664525) + 1013904223) >>> 0; | ||
| return s / 4294967296; | ||
| }; | ||
| return Array.from({ length: count }, () => ({ | ||
| buckets: Array.from({ length: 4 }, () => Math.floor(rnd() * 5)), | ||
| remaining: 1 + Math.floor(rnd() * 10), | ||
| })); | ||
| } | ||
|
|
||
| describe("deriveEpicEta", () => { | ||
| test("missing history: not_enough_history, no dates, unavailable confidence", () => { | ||
| const eta = deriveEpicEta({ subjectRef: EPIC, cutoffs: [] }); | ||
|
|
@@ -168,17 +242,127 @@ describe("deriveEpicEta", () => { | |
| expect(eta.remainingUnits).toBe(2); | ||
| }); | ||
|
|
||
| test("zero lower-quartile pace: P50 present, P80 explicitly unmeasurable, no crash", () => { | ||
| // Primary weeks [0, 1, 1, 1]: median pace 1, lower quartile 0. | ||
| test("zero lower-quartile pace, re-pinned for the resampling estimator (#5274): P50 and P80 both present", () => { | ||
| // Primary weeks [0, 1, 1, 1]. Under forecast/v1 the median pace | ||
| // was 1 and the lower-quartile pace 0, so P80 was explicitly | ||
| // unmeasurable (the previous pin). The design \u00a71 resampling | ||
| // estimator (forecast/v2) draws the observed buckets themselves: | ||
| // a zero week is a draw, not a denominator, so the | ||
| // weeks-to-complete distribution has a finite conservative tail | ||
| // and P80 is defensible. This pin replaces the v1 pin with that | ||
| // rationale, per #5274's acceptance criteria. | ||
| const eta = deriveEpicEta({ | ||
| subjectRef: EPIC, | ||
| cutoffs: history(5, [2, 3, 4, null, null]).slice(0, 5), | ||
| }); | ||
| expect(eta.status).toBe("forecast"); | ||
| expect(eta.sample.completionsPrimary).toBe(3); | ||
| expect(eta.p50).not.toBeNull(); | ||
| expect(eta.p80).not.toBeNull(); | ||
| expect(eta.p80! >= eta.p50!).toBe(true); | ||
| expect(eta.limitations.join(" ")).not.toContain("lower-quartile"); | ||
| }); | ||
|
|
||
| test("bursty zero-median buckets [0, 0, 0, 4]: forecast with finite P50 <= P80, not no_measurable_pace (#5274)", () => { | ||
| // 4 children close in the last observed week; 4 remain. The | ||
| // median bucket is 0, so forecast/v1 returned | ||
| // no_measurable_pace despite 4 completions of evidence; the | ||
| // resampling estimator forecasts from the bucket distribution. | ||
| const closes: (number | null)[] = [4, 4, 4, 4, null, null, null, null]; | ||
| const eta = deriveEpicEta({ subjectRef: EPIC, cutoffs: history(8, closes).slice(0, 5) }); | ||
| expect(eta.status).toBe("forecast"); | ||
| expect(eta.sample.completionsPrimary).toBe(4); | ||
| expect(eta.remainingUnits).toBe(4); | ||
| expect(eta.p50).not.toBeNull(); | ||
| expect(eta.p80).not.toBeNull(); | ||
| expect(Number.isFinite(Date.parse(eta.p50!))).toBe(true); | ||
| expect(Number.isFinite(Date.parse(eta.p80!))).toBe(true); | ||
| expect(eta.p80! >= eta.p50!).toBe(true); | ||
|
Comment on lines
+276
to
+280
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new estimator is covered only by fixed examples asserting finite, ordered dates and repeatability; these tests would also pass if both percentiles were replaced with the same arbitrary deterministic date. The repository's testing doctrine requires generated property/model-based coverage where invariants and a design model exist, rather than example-only coverage. Add generated bucket/remaining-work cases with a named reference oracle for completion-time quantiles (and an explicit sampling tolerance), alongside determinism and P50 ≤ P80 properties, so incorrect resampling or percentile calculations are detected. AGENTS.md reference: AGENTS.md:L41-L45 Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 2c2a5e9 (this PR). Added generated, model-backed coverage to forecast-3982.test.ts:
Tests: forecast-3982.test.ts 31 pass on laptop (bun test), 1883 expect() calls. |
||
| }); | ||
|
|
||
| test("resampling estimator is deterministic: identical inputs produce identical outputs (#5274)", () => { | ||
| const closes: (number | null)[] = [4, 4, 4, 4, null, null, null, null]; | ||
| const cutoffs = history(8, closes).slice(0, 5); | ||
| const a = deriveEpicEta({ subjectRef: EPIC, cutoffs }); | ||
| const b = deriveEpicEta({ subjectRef: EPIC, cutoffs }); | ||
| expect(b).toEqual(a); | ||
| }); | ||
|
|
||
|
|
||
| test("generated cases match the exact DP oracle within the stated sampling tolerance (MBT, #5274 / PR #6169 Codex P1)", () => { | ||
| let forecastCases = 0; | ||
| for (const { buckets, remaining } of generatedEstimatorCases(240)) { | ||
| const eta = deriveEpicEta({ subjectRef: EPIC, cutoffs: historyForBuckets(buckets, remaining) }); | ||
| const total = buckets.reduce((a, b) => a + b, 0); | ||
| if (total === 0) { | ||
| expect(eta.status).toBe("no_measurable_pace"); | ||
| expect(eta.p50).toBeNull(); | ||
| expect(eta.p80).toBeNull(); | ||
| continue; | ||
| } | ||
| if (total < 3) { | ||
| expect(eta.status).toBe("not_enough_history"); | ||
| expect(eta.p50).toBeNull(); | ||
| expect(eta.p80).toBeNull(); | ||
| continue; | ||
| } | ||
| forecastCases++; | ||
| expect(eta.status).toBe("forecast"); | ||
| expect(eta.p50).not.toBeNull(); | ||
| expect(eta.p80).not.toBeNull(); | ||
| const w = etaWeeks(eta); | ||
| expect(w.p50).toBeGreaterThanOrEqual(1); | ||
| expect(w.p80).toBeGreaterThanOrEqual(w.p50); | ||
| expect(Math.abs(w.p50 - exactWeeksQuantile(buckets, remaining, 0.5))).toBeLessThanOrEqual( | ||
| SAMPLING_TOLERANCE_WEEKS, | ||
| ); | ||
| expect(Math.abs(w.p80 - exactWeeksQuantile(buckets, remaining, 0.8))).toBeLessThanOrEqual( | ||
| SAMPLING_TOLERANCE_WEEKS, | ||
| ); | ||
| } | ||
| // Guard the generator itself: most generated cases must exercise | ||
| // the forecast path, or the oracle comparison above is vacuous. | ||
| expect(forecastCases).toBeGreaterThan(150); | ||
| }); | ||
|
|
||
| test("generated cases are deterministic: identical generated inputs produce identical outputs (PBT, #5274)", () => { | ||
| generatedEstimatorCases(240).forEach(({ buckets, remaining }, i) => { | ||
| if (i % 10 !== 0) return; | ||
| const cutoffs = historyForBuckets(buckets, remaining); | ||
| expect(deriveEpicEta({ subjectRef: EPIC, cutoffs })).toEqual(deriveEpicEta({ subjectRef: EPIC, cutoffs })); | ||
| }); | ||
| }); | ||
|
|
||
| test("constant buckets are exact (tolerance zero): every resample draws the same pace (MBT, #5274)", () => { | ||
| for (const c of [1, 2, 3, 4]) { | ||
| for (const remaining of [1, 2, 5, 9, 12]) { | ||
| const buckets = [c, c, c, c]; | ||
| const eta = deriveEpicEta({ subjectRef: EPIC, cutoffs: historyForBuckets(buckets, remaining) }); | ||
| expect(eta.status).toBe("forecast"); | ||
| const exact = Math.ceil(remaining / c); | ||
| expect(exactWeeksQuantile(buckets, remaining, 0.5)).toBe(exact); | ||
| expect(exactWeeksQuantile(buckets, remaining, 0.8)).toBe(exact); | ||
| const w = etaWeeks(eta); | ||
| expect(w.p50).toBe(exact); | ||
| expect(w.p80).toBe(exact); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| 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 | ||
| // total observed pace is zero, and resampling zero buckets can | ||
| // only draw zero \u2014 no date is defensible. | ||
| const closes: (number | null)[] = [3, null, null, null, null, null]; | ||
| const eta = deriveEpicEta({ | ||
| subjectRef: EPIC, | ||
| cutoffs: history(6, closes).slice(0, 6), | ||
| borrowedBaseline: { scopeRef: `${REPO}#200`, weeklyCompletions: [0, 0, 0, 0] }, | ||
| }); | ||
| expect(eta.status).toBe("no_measurable_pace"); | ||
| expect(eta.p50).toBeNull(); | ||
| expect(eta.p80).toBeNull(); | ||
| expect(eta.limitations.join(" ")).toContain("lower-quartile"); | ||
| }); | ||
|
|
||
| test("inferred scope counts direct children only, not grandchildren via epicRef", () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When remaining work cannot finish within the 100,000-week cap, this records unfinished trials as actual completions, making the forecast optimistic rather than conservative. For example, buckets
[1, 1, 1, 1]with 100,001 remaining units yield P50 and P80 at 100,000 weeks even though completion takes exactly 100,001 weeks. Preserve capped trials as censored observations and return an unavailable date with a limitation when the requested percentile exceeds the simulation horizon, rather than publishing a completion date before the work finishes.Useful? React with 👍 / 👎.