Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/flow/docs/design/DECOMPOSITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,11 @@ that:

- A pull request that shrinks a file lowers its budget in the same pull
request; the check prints the new number.
- A pull request that adds net lines to a listed file raises the budget in
its own diff, with a one-line reason in the pull request description. The
ratchet makes growth visible in review; it does not block feature work.
- Budgets never rise (founder decision, 2026-11-21). A pull request that
needs new code next to a listed file puts it in a new module and adds only
the call site, within the budget. The check fails a pull request that raises
a budget or drops the row of a file that still exists. The notes below about
earlier raises are history.

PR #2239 raised `__main__.py` and `terminal_verification_v2.py`: its review fixes added authority scoping and `authority_basis`.

Expand Down
53 changes: 48 additions & 5 deletions scripts/check_file_size_ratchet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
The budgets live in one table in packages/flow/docs/design/DECOMPOSITION.md,
between the ``size-ratchet:begin`` and ``size-ratchet:end`` markers. A file may
shrink; it may not grow. When a change shrinks a file, lower its budget in the
same change so the gain is kept. Raising a budget is a reviewed edit to that
table.
same change so the gain is kept. With ``--base``, a change that raises a
budget, or removes the row of a file that still exists, fails: budgets never
rise, so new code goes in a new module (founder decision, 2026-11-21).

With ``--base REF`` (a pull request or the merge queue compares against the
base it merges into), a file over budget fails only when this change grew it
Expand Down Expand Up @@ -107,6 +108,22 @@ def base_budgets(root: Path, base: str) -> dict[str, int]:
return {}


def renamed_from(root: Path, base: str) -> dict[str, str]:
"""New path -> old path for files git sees as renamed since ``base``."""
result = subprocess.run(
["git", "-C", str(root), "diff", "--name-status", "-M", base],
capture_output=True,
text=True,
check=False,
)
renames = {}
for line in result.stdout.splitlines():
fields = line.split("\t")
if len(fields) == 3 and fields[0].startswith("R"):
renames[fields[2]] = fields[1]
return renames


def policy(env: Mapping[str, str]) -> tuple[str | None, bool]:
"""(base, report_only) for a CI event; nightly and local runs are strict.

Expand All @@ -123,16 +140,42 @@ def check(root: Path, base: str | None = None) -> tuple[list[str], list[str]]:
"""Return (failures, notes) for the budgets in ``root``'s plan.

With ``base``, an over-budget or missing file fails only when the change
from ``base`` grew it, removed it or changed its budget row.
from ``base`` grew it, removed it or changed its budget row, and raising a
budget or dropping the row of a file that still exists always fails.
"""
budgets = parse_budgets((root / DOC).read_text(encoding="utf-8"))
before = base_budgets(root, base) if base else {}
failures: list[str] = []
notes: list[str] = []
listed = {budget.path for budget in budgets}
renames = renamed_from(root, base) if base else {}
for gone in sorted(set(before) - listed):
if (root / gone).is_file():
failures.append(
f"{gone}: its budget row was removed but the file still exists. "
"Budgets never rise; keep the row."
)
for budget in budgets:
path = root / budget.path
old = read_at(root, base, budget.path) if base else None
row_changed = base is not None and before.get(budget.path) != budget.lines
if budget.path in before and budget.lines > before[budget.path]:
failures.append(
f"{budget.path}: budget raised from {before[budget.path]} to "
f"{budget.lines}. Budgets never rise: put new code in a new "
"module and keep only the call site here."
)
origin = renames.get(budget.path)
if (
budget.path not in before
and origin in before
and budget.lines > before[origin]
):
failures.append(
f"{budget.path}: renamed from {origin}, budget raised from "
f"{before[origin]} to {budget.lines}. A renamed file keeps its "
"budget; budgets never rise."
)
if not path.is_file():
message = (
f"{budget.path}: listed in {DOC} but missing. If it was split or "
Expand All @@ -147,8 +190,8 @@ def check(root: Path, base: str | None = None) -> tuple[list[str], list[str]]:
if lines > budget.lines:
message = (
f"{budget.path}: {lines} lines, budget {budget.lines} "
f"(+{lines - budget.lines}). Move code out instead of adding it, "
f"or raise the budget in {DOC} with a reason in the pull request."
f"(+{lines - budget.lines}). Put new code in a new module and keep "
"only the call site here; budgets never rise."
)
grew = old is None or lines > old.count(b"\n")
if base is None or grew or row_changed:
Expand Down
58 changes: 58 additions & 0 deletions tests/test_file_size_ratchet.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,61 @@ def test_an_unreadable_base_is_refused(tmp_path: Path) -> None:
with pytest.raises(ValueError, match="not a commit"):
ratchet.check(root, "no-such-ref")
assert ratchet.main(["--root", str(root), "--base", "no-such-ref"]) == 2


def test_raising_a_budget_fails(tmp_path: Path) -> None:
root = _base(_repo(tmp_path, "| `a.py` | 10 |\n", {"a.py": 10}))
doc = root / ratchet.DOC
doc.write_text(doc.read_text().replace("| 10 |", "| 12 |"))
_write(root, "a.py", 12)
failures, _notes = ratchet.check(root, "HEAD")
assert failures == [
"a.py: budget raised from 10 to 12. Budgets never rise: put new code "
"in a new module and keep only the call site here."
]
assert ratchet.main(["--root", str(root), "--base", "HEAD"]) == 1


def test_lowering_a_budget_passes(tmp_path: Path) -> None:
root = _base(_repo(tmp_path, "| `a.py` | 10 |\n", {"a.py": 10}))
doc = root / ratchet.DOC
doc.write_text(doc.read_text().replace("| 10 |", "| 8 |"))
_write(root, "a.py", 8)
assert ratchet.check(root, "HEAD")[0] == []


def test_dropping_the_row_of_a_file_that_still_exists_fails(tmp_path: Path) -> None:
root = _base(
_repo(tmp_path, "| `a.py` | 10 |\n| `b.py` | 10 |\n", {"a.py": 10, "b.py": 9})
)
doc = root / ratchet.DOC
doc.write_text(doc.read_text().replace("| `b.py` | 10 |\n", ""))
failures, _notes = ratchet.check(root, "HEAD")
assert len(failures) == 1 and "b.py: its budget row was removed" in failures[0]


def test_dropping_the_row_of_a_removed_file_passes(tmp_path: Path) -> None:
root = _base(
_repo(tmp_path, "| `a.py` | 10 |\n| `b.py` | 10 |\n", {"a.py": 10, "b.py": 9})
)
doc = root / ratchet.DOC
doc.write_text(doc.read_text().replace("| `b.py` | 10 |\n", ""))
(root / "b.py").unlink()
assert ratchet.check(root, "HEAD")[0] == []


@pytest.mark.parametrize("new_budget, fails", [(12, True), (10, False), (9, False)])
def test_a_renamed_file_keeps_its_budget(
tmp_path: Path, new_budget: int, fails: bool
) -> None:
root = _base(_repo(tmp_path, "| `a.py` | 10 |\n", {"a.py": 9}))
_git(root, "mv", "a.py", "b.py")
_write(root, "b.py", min(new_budget, 12))
doc = root / ratchet.DOC
doc.write_text(
doc.read_text().replace("| `a.py` | 10 |", f"| `b.py` | {new_budget} |")
)
_git(root, "add", "-A")
failures, _notes = ratchet.check(root, "HEAD")
renamed = [f for f in failures if "renamed from a.py" in f]
assert bool(renamed) is fails