Skip to content

feat(server): calibrate the stats policy thresholds from the phase-0 measurements (rows-first p31b) - #1023

Draft
paddymul wants to merge 6 commits into
adr-003-stats-tiers-and-size-policyfrom
feat/rowsfirst-p31b-policy-calibration
Draft

paddymul wants to merge 6 commits into
adr-003-stats-tiers-and-size-policyfrom
feat/rowsfirst-p31b-policy-calibration

Conversation

@paddymul

@paddymul paddymul commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The thresholds in buckaroo/server/stats_policy.py (PR #1019) are placeholders taken from repo constants: full stats up to 10M rows, scalar stats up to 500M cells, a 50M-row ceiling on full, no ceiling on scalar, and polars routed to xorq above 10M rows. The phase-0 measurement run has since produced proposed values from 115 tallyman loads, xorq stats timings on parquet and CSV, and the RSS of eager polars frames. Against those measurements the placeholders are off in four ways: five 10.8-11.8M-row entries that took 2.7-6.5 s would go to the scalar tier, the 50M-row ceiling sits inside a gap where the telemetry has no entries (11.8M to 42.3M rows) and lets a forced full run on the 42.3M-row entry that took 75 s, rows alone cannot see a wide entry, and 10M rows of eager polars does not fit the memory budget once windows copy the frame.

This PR replaces the placeholders with the proposals. It changes constants and adds two cell bounds, nothing else.

Phase and plan references

Follow-up to plan 3 (03-no-summary-stats-for-large-files.md) section 6 "Phase 1" and section 3.1, using measurements-phase0.md ("Proposed values", the telemetry join, the eager polars RSS section and the "not measured" list). Plan 1 sections 3 and 4.0 and plan 2 sections 3 and 4.1 are the vocabulary. The plans and the measurements are in buckaroo2-reports/plans/, outside this repo.

Approach

Each DEFAULT_* constant takes the proposed value and carries a comment with the measurement behind it. Two new StatsLimits fields and constants add the cell bounds the proposals need, and each has an environment override with the same parsing rules as the others (read on every call; a value that is not a non-negative integer is ignored with a warning).

Limit Environment variable Was Now
full_auto_rows BUCKAROO_STATS_FULL_AUTO_ROWS 10,000,000 12,000,000
full_auto_cells (new) BUCKAROO_STATS_FULL_AUTO_CELLS none 520,000,000
scalar_auto_cells BUCKAROO_STATS_SCALAR_AUTO_CELLS 500,000,000 1,000,000,000
ceiling_full_rows BUCKAROO_STATS_CEILING_FULL_ROWS 50,000,000 25,000,000
ceiling_full_cells (new) BUCKAROO_STATS_CEILING_FULL_CELLS none 1,000,000,000
ceiling_scalar_cells BUCKAROO_STATS_CEILING_SCALAR_CELLS unset (no ceiling) 4,000,000,000
polars_route_rows BUCKAROO_POLARS_ROUTE_ROWS 10,000,000 8,000,000

The two rules that read the new fields:

  • Size tier: full when rows <= full_auto_rows and cells <= full_auto_cells; otherwise scalar when cells <= scalar_auto_cells; otherwise schema. A 12M-row entry is full at 43 columns (516M cells) and scalar at 44 (528M).
  • Ceiling tier: schema when cells > ceiling_scalar_cells; otherwise full when rows <= ceiling_full_rows and cells <= ceiling_full_cells; otherwise scalar. This refuses full above 25M rows or 1.0B cells, whichever is lower, for every caller including a forced request.

Where the numbers come from:

  • 12M rows: 105 of 115 telemetry loads are at or below it, with stats p90 of 4.4 s. Every load above it is one of three CSV unions (42.3M, 54.1M, 78.0M rows) at 75, 114 and 215 s.
  • 520M cells: parquet 12M x 43 (516M cells) full stats measured 2.9 s over 8 repetitions. The bound only matters for entries wider than 43 columns.
  • 1.0B scalar cells: the scalar class costs 1.1-1.3 ms per Mcell on a parquet scan and 3-6 ms on a CSV union. At the worst telemetry batch rates that is about 20-34 s at 1.0B cells.
  • 25M rows and 1.0B cells (full ceiling): the geometric midpoint of the gap between the slowest in-limit load (11.8M rows, 22 s) and the fastest one above it (42.3M rows, 75 s).
  • 8M rows for polars: eager frame RSS is 0.28 GB + 0.543 GB per million rows, and a sorted window peaks at 0.23 + 1.62 GB per million rows (3.0x the loaded frame) when pre_limit is False. With a 32 GB budget, four files resident and one window in flight, 8M rows is 27 GB and 10M is 33.5 GB. The figure assumes pre_limit False, as the docstring says. With main's 1M pre_limit, windows do not copy the frame and the resident-only bound is about 14M rows for four files.

What changes

  • buckaroo/server/stats_policy.py: the DEFAULT_* constants and their comments, DEFAULT_FULL_AUTO_CELLS and DEFAULT_CEILING_FULL_CELLS, the two StatsLimits fields (appended after polars_route_rows, so positional construction is unchanged), two entries in _ENV_FIELDS, one added condition each in _size_tier and _ceiling_tier, and the module and route_polars_entry docstrings.
  • tests/unit/server/test_stats_policy.py.

Function signatures and the result shape are unchanged.

Tests

All in tests/unit/server/test_stats_policy.py.

  • RESOLVE_TABLE is rewritten to the new values, with an equal, a one-below and a one-above row for every threshold, in rows and in cells (a row count or a column count moves the product). It also covers the 11.7M-row CSV entries, the three slow telemetry entries and the new ceilings with and without a host tier.
  • TestCalibratedDefaults pins StatsLimits(), StatsLimits.from_env() on a clean environment and the module constants to literal values, and asserts every limit has an environment variable.
  • TestCeiling gains three property checks over all host tiers: full never resolves above 1.0B cells, above 25M rows with few cells, or past schema above 4.0B cells.
  • TestLimitsFromEnv runs its two parametrized checks over the new variables, and checks that each new bound is read from the environment on each call.
  • TestRoutePolarsEntry checks 7,999,999, 8,000,000 and 8,000,001 rows on the defaults.

The lim fixture now returns the module defaults (it used to pin the plan's starting values explicitly). This is what makes the tables fail on the old constants, and it means a change to a DEFAULT_* constant fails them until the table is updated with it.

The tests are committed alone first (3fd6b5fa). CI on that commit failed: the nine Python / Test matrix jobs (3.11 to 3.14, Max Versions 3.11 to 3.14, Windows) were red, and every other check completed (deploy skipped). Locally, 115 of the 321 tests in the file fail on the old constants, mostly on assertions; the rest fail with AttributeError for full_auto_cells and ceiling_full_cells, which the old StatsLimits does not have. The implementation is 3eef7097, and every check on it completed green: 26 check runs succeeded (all nine Python / Test jobs, lint, JS, wheel, docs and the Playwright jobs), deploy was skipped, and the Read the Docs status passed.

Three existing tests changed, because they hard-coded the old boundaries and fail on the new constants:

  • test_numpy_integer_counts_are_accepted used 11M rows and expected scalar; 11M rows x 43 columns is now full, so it uses 13M rows.
  • test_reload_re_resolves_against_the_new_row_count used a 40M-row entry that kept full under the old 50M-row ceiling; that entry is now refused, so the "before" entry is 20M rows x 44 columns (880M cells, inside both ceilings).
  • test_empty_value_means_unset asserted that an empty BUCKAROO_STATS_CEILING_SCALAR_CELLS gives None. The scalar ceiling now defaults to 4.0B cells, so an empty value means the default.

Two tests were added in the implementation commit because they also pass on the old constants: the scalar ceiling's environment override, and a check that requestable matches what a force request is granted at the new row and cell boundaries.

The local unit suite (pytest ./tests/unit -m "not slow") passes: 1519 passed, 5 skipped.

Stack

This PR's base is main, and it is cumulative: it contains the four commits of draft PR #1019 (feat/rowsfirst-p31-stats-policy: c1391701, 8895d26c, f03532ca, a2cc7ce5) until that merges. This phase's commits are 3fd6b5fa (failing tests) and 3eef7097 (implementation). git diff origin/feat/rowsfirst-p31-stats-policy...HEAD shows only these.

Why default behaviour is unchanged

No handler, session or dataflow imports stats_policy, so no request, load or stats run reads these values. The module is still not wired in.

Deviations from the plan

  • The plan's section 3.1 lists thresholds in rows and one cell budget. The proposals add two cell bounds (full_auto_cells, ceiling_full_cells), so StatsLimits has two fields and there are two environment variables beyond the p31 set.
  • The scalar ceiling has a default (4.0B cells) where p31 left it unset. It is an extrapolation: the only evidence above 1.8B cells is the 3.36B-cell entry whose batch took 162.5 s, and there is no scalar measurement at that size. The docstring says so.
  • The full ceiling (25M rows, 1.0B cells) is the midpoint of a gap in the telemetry and could sit anywhere between 11.8M and 42.3M rows.
  • The new fields are appended at the end of StatsLimits, not placed next to the field they extend.

Not in this PR

  • Any wiring, new tiers or new functions.
  • Any change to the rules other than the two new bounds.
  • The lazy (L-track) path.
  • The measurements the report lists as not taken: sorted-window memory at 10M and 12M rows (15.9 GB is a lower bound, the 16.4 and 19.7 GB figures are extrapolated), cold-disk timings, xorq sizes above 12M rows, a numeric-only eager frame, and the cause of the 22.1 s load of the 11.7M-row entry. The new defaults are as good as those gaps allow.

🤖 Generated with Claude Code

paddymul and others added 5 commits October 3, 2026 22:53
…(rows-first p31)

New tests/unit/server/test_stats_policy.py for buckaroo/server/stats_policy.py,
which does not exist yet:

- resolve_stats_policy: a table of (backend, source_kind, rows, cols, host_tier)
  against (tier_target, auto_request, requestable, reason), including the
  boundaries of each threshold and the three tallyman entries over 70 s.
- The ceiling holds for host_tier="full", for a reload that re-resolves
  against a larger row count, and for a force request, and every tier a result
  lists as requestable is one a force would be granted.
- Probes add no data query: the parquet footer probe reads a small share of a
  counting file object and decodes no row group, the dtype probe never collects
  a LazyFrame or executes a xorq expression, and a known xorq count is an input.
- route_polars_entry returns "xorq" above R and "eager" at or below it.
- Threshold overrides from BUCKAROO_* environment variables.

Nothing imports the module yet. The tests error at fixture setup on the missing
module until the implementation lands.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… p31)

New buckaroo/server/stats_policy.py, pure and imported by nothing yet:

- resolve_stats_policy(backend, source_kind, rows, cols, bytes, host_tier,
  limits) returns tier_target, auto_request, requestable, reason and estimate
  over the tiers schema < scalar < full. A ceiling is computed inside the
  function, so every caller gets the lower of the requested and ceiling tiers
  with reason "ceiling". A host tier lowers freely and raises only to the
  ceiling. Eager pandas and polars resolve to full.
- route_polars_entry(rows, cols) returns "xorq" above R rows and "eager" at or
  below it.
- probe_dtypes reads a schema without collecting or executing anything, and
  probe_parquet_rows reads a parquet footer's num_rows without decoding data.
  A xorq count is an input to the policy, never computed by it.
- The thresholds are provisional constants gathered in StatsLimits, each with
  a BUCKAROO_* environment override read on every call.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…esolve_stats_policy (rows-first p31)

bytes and source_kind were documented as validated but are not: bytes=-1,
'lots', 1.5 and object() are accepted, a np.int64 is echoed unchanged so
json.dumps of the result fails, a host tier passed positionally lands in the
bytes slot, and source_kind=None or 5 is accepted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…rows-first p31)

bytes goes through the same integer check as rows and cols, so a negative,
float or non-numeric value raises, a numpy integer is echoed as a plain int
and the result stays JSON-serialisable, and a host tier passed positionally
into the bytes slot raises instead of being echoed. source_kind must be a
str; its vocabulary stays open for the callers that will read it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…s (rows-first p31b)

The boundary tables now run on the module defaults and are written against
the phase-0 proposals: full auto up to 12M rows and 520M cells, scalar auto
up to 1.0B cells, full refused above 25M rows or 1.0B cells (force
included), a scalar ceiling of 4.0B cells, and polars routed to xorq above
8M rows. Each threshold has an equal, a one-below and a one-above row, in
rows and in cells. New tests pin each default to its literal value and
cover the two new environment overrides, BUCKAROO_STATS_FULL_AUTO_CELLS and
BUCKAROO_STATS_CEILING_FULL_CELLS.

They fail on the current constants (10M rows, 500M cells, 50M-row ceiling,
no scalar ceiling, R of 10M) and on the missing full_auto_cells and
ceiling_full_cells fields.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

📦 TestPyPI package published

pip install --index-strategy unsafe-best-match --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ buckaroo==0.15.9.dev37180060370

or with uv:

uv pip install --index-strategy unsafe-best-match --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ buckaroo==0.15.9.dev37180060370

MCP server for Claude Code

claude mcp add buckaroo-table -- uvx --from "buckaroo[mcp]==0.15.9.dev37180060370" --index-strategy unsafe-best-match --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ buckaroo-table

📖 Docs preview

🎨 Storybook preview

…measurements (rows-first p31b)

Full auto now needs at most 12M rows and 520M cells (was 10M rows), scalar
auto goes to a 1.0B cell budget (was 500M), `full` is refused above 25M rows
or 1.0B cells including for a forced request (was 50M rows), scalar gets a
4.0B cell ceiling by default (was unset; an extrapolation with no
measurement behind it), and polars routes to xorq above 8M rows (was 10M;
the figure assumes pre_limit False). Each DEFAULT_* constant carries the
measurement it comes from.

The two new bounds are full_auto_cells and ceiling_full_cells, appended to
StatsLimits, with BUCKAROO_STATS_FULL_AUTO_CELLS and
BUCKAROO_STATS_CEILING_FULL_CELLS as overrides under the same parsing rules.
_size_tier and _ceiling_tier read them; signatures and the result shape are
unchanged.

Three existing tests that hard-coded the old boundaries are updated: the
numpy-integer case (11M rows is now full), the reload case (a 40M-row entry
is now refused full) and the empty-environment case for the scalar ceiling
(an empty value now means the 4.0B default, not no ceiling).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
testpypi — 3eef7097 Deployed Oct 4, 2026 by paddymul via Publish to TestPyPI #1679
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant