fix(#4294): reserve a full progress bar for 100% and give the render half one owner (#4473)

* fix(#4294): reserve a full progress bar for 100% and give the render half one owner

Six call sites each carried `Math.round((percent / 100) * width)` inline, and
every copy rounded to a full bar before the percent reached 100: from 95 up at
width 10, from 98 up at width 20. A project at 19/20 plans drew the same bar as
a shipped one beside a number that said otherwise, and an out-of-range percent
threw `RangeError` from the unguarded `'░'.repeat`.

ADR-3180 Decision 7 gave the completion-RATIO derivation one owner
(`clampPercentFromFraction`). This gives the RENDER half the same:
`progressBarFilledCells` / `renderProgressBar` in phase-lifecycle.cts, with the
`progress` table and bar renderers, the stats renderer, the gsd2 import writer,
and #4231's `formatProgressMachineSegment` (which now serves both STATE.md
writers) all drawing through it.

Contract: below 100 the fill is held one cell short of the width, so only the
saturating percents move (95-99 at width 10, 98-99 at width 20) and every other
value in 0-100 renders as before — pinned by an exhaustive comparison against
the legacy formula at both widths. Null / non-finite renders an empty bar;
out-of-range is clamped, never thrown.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hbFn24VWaxJBw8DUWjAmU

* chore(#4294): set changeset fragment pr to 4473

* docs(#4294): correct the pre-fix inline call-site count to five

The kernel's doc comment said SIX call sites carried their own
`Math.round((percent / 100) * width)`. The base tree has five: three in
`commands.cts` plus one each in `gsd2-import.cts` and
`formatProgressMachineSegment`, the latter two using `/ 10` with the width
already substituted (`pct` and `clamped` respectively).

The six is #4294's count of consumers -- it counts `cmdStateUpdateProgress`
and `syncCore` separately, but #4231 had already routed both through
`formatProgressMachineSegment` (as it does `applyPostSyncPreservation`), so by
this branch's base they share one copy. The comment now states the tree's count
and records where the six comes from, so neither number reads as an error
later.

Comment-only; no behaviour change, and no change to compiled output.

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Tom Boucher <trekkie@nomorestars.com>
This commit is contained in:
0xdhx
2026-09-10 23:42:55 -05:00
committed by GitHub
parent 523be34133
commit 7249bdddac
7 changed files with 180 additions and 21 deletions

View File

@@ -151,3 +151,61 @@ export function clampPercent(completed: number, total: number): number {
if (!total || total <= 0) return 0;
return clampPercentFromFraction(completed / total);
}
/**
* How many cells of a `width`-cell progress bar are filled for `percent`.
*
* #4294: the RENDER half's kernel — the counterpart of `clampPercentFromFraction`
* one layer down. That function owns `fraction -> integer percent`; this one
* owns `integer percent -> filled cells`, and is the single place that rounding
* is expressed. Five inline copies of the rounding carried it before this
* change — three in `commands.cts`, and one each in `gsd2-import.cts` and
* `formatProgressMachineSegment` here; the latter two used `/ 10` with the
* width already substituted (`pct` in `gsd2-import.cts`, `clamped` in the
* formatter). (#4294 counts SIX call sites because it counts
* `cmdStateUpdateProgress` and `syncCore` separately; #4231 had already routed
* both through `formatProgressMachineSegment` — as it does
* `applyPostSyncPreservation` — so by this branch's base they share one copy.)
* Every copy saturated: at width 10 that rounds to a full bar from 95 up, at
* width 20 from 98 up, so a project at 19/20 plans drew the same bar as a
* shipped one beside a number that said otherwise.
*
* Contract:
* - A FULL bar is reserved for an actual 100. Below 100 the fill is held one
* cell short of the width. This is the only departure from the old formula:
* at width 10 exactly 95-99 move (10 -> 9), at width 20 exactly 98-99
* (20 -> 19); every other percent in 0-100 rounds as before.
* - `null` / `undefined` / non-finite renders an empty bar, matching the
* `percent === null ? 0 : ...` guard the `progress` renderers already carried.
* - Out-of-range input is clamped to 0-100 before rounding, so the count is
* always within `[0, width]` and a `'░'.repeat(width - filled)` can never
* be handed a negative count (the old inline form threw `RangeError` at
* 120%). A non-positive width yields 0.
*
* Callers wanting the glyph run call `renderProgressBar`; this is exported so
* the rounding rule can be pinned directly against the legacy curve.
*/
export function progressBarFilledCells(percent: number | null | undefined, width: number): number {
const cells = Number.isFinite(width) && width > 0 ? Math.floor(width) : 0;
if (cells === 0) return 0;
if (typeof percent !== 'number' || !Number.isFinite(percent)) return 0;
const clamped = Math.max(0, Math.min(100, percent));
if (clamped >= 100) return cells;
// Scale by the WIDTH, not by 100 — this is cells-from-percent, not the
// completion-ratio derivation lint-completion-ratio-drift.cjs guards.
const rounded = Math.round((clamped / 100) * cells);
return Math.min(rounded, cells - 1);
}
/**
* Render the glyph run of a `width`-cell progress bar for `percent` —
* `'█'` for each filled cell, `'░'` for the rest, always exactly `width`
* glyphs (an empty string for a non-positive width). Brackets, the printed
* percent and any suffix stay with the caller; the fill rule is
* `progressBarFilledCells` (#4294).
*/
export function renderProgressBar(percent: number | null | undefined, width: number): string {
const filled = progressBarFilledCells(percent, width);
const cells = Number.isFinite(width) && width > 0 ? Math.floor(width) : 0;
return '█'.repeat(filled) + '░'.repeat(cells - filled);
}