Skip to content

feat(tree): budget the whole fold tick, publication included - #438

Merged
baer merged 8 commits into
mainfrom
tree/fold-tick-budget
Nov 25, 2026
Merged

baer merged 8 commits into
mainfrom
tree/fold-tick-budget

Conversation

@baer

@baer baer commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What & why

Budgets the whole tree fold tick, publication included, and bounds the fold's read-ahead by that budget. A fold that reads past its tick budget is now reported and counted. The tree has no production consumers yet, so nothing outside packages/server/src/tree/ changes behavior.

Key points

  • Bench folds run at their host's width and arrival time, so bench numbers use the same concurrency the fold plans for.

Verification

| pnpm verify:agent | clean |
| pnpm build:agent | clean |
| pnpm test:agent | two 5 s timeouts in tree/fold.test.ts under load (load average ~36); file passed 78/78 on rerun |
| pnpm bundle-sizes | 14/14 |

baer added 8 commits November 25, 2026 11:54
Replace the planning-only deadline with a total tick budget
(TREE_FOLD_TICK_BUDGET_MS, 20 s). Before each log batch and entry the
fold adds the time elapsed since `startedAt` (the request's arrival,
defaulting to the fold's start) to an estimate of publishing the plan
so far, and stops admitting entries once that reaches the budget. A
planning-only deadline let an append-heavy batch, which writes many
fresh leaves from few reads, plan until the deadline and then overrun
the 30 s post-response window while publishing.

The estimate is the publication's rounds of calls (publicationStages:
the fold record create, each level of fresh nodes `parallelCalls` at a
time, then the head CAS and its re-read) times the slowest call the tick has timed: the
head GET, the root GET and the node GETs. Log reads are not timed, as
they may queue behind a host's concurrency cap. Liveness is kept: a
fold never stops before its publishable prefix is non-empty.

The fold's width is fixed per host in the maintenance profile
(`foldParallelCalls`): 6 on Workers (CF_MAX_CONCURRENT_STORAGE_CALLS,
where a seventh call queues) and 16 on Node (MAX_PARALLEL_LOG_READS).
Publication now honours the same width for each level's creates, so
the estimate holds; write order and adoption are unchanged.
The stop reason a fold reports when its tick budget would run out was
still called "deadline", from when the clock bounded planning only.
Rename it "tick-budget" to match `tickBudgetMs` and
TREE_FOLD_TICK_BUDGET_MS, in the type, the fold tests and the op-model
bench comment.
Cover what the tick-budget tests left open, each with a test that
fails when that piece of the estimate or the publication changes:

- exact-boundary budgets on a single-leaf plan (four rounds: record,
  leaf, head CAS, fate re-read) and on a branch plan (its sealed
  levels' rounds), so dropping the record, the re-read, the root level
  or the single leaf from the estimate stops the fold one entry late;
- a slow root GET under a fast head, and slow node GETs under a fast
  head and root, so the estimate must time both and take the slowest
  call rather than the first;
- a tick past its budget reads no further log batch;
- the literal fold width of each maintenance profile (6, 6, 16) and of
  CF_MAX_CONCURRENT_STORAGE_CALLS;
- the planner's per-level fresh-node counts against the sealed
  publication after every change, and while pinned the larger of the
  pinned and current plans;
- a level whose creates fail several times throws the failure of the
  first node in write order, even when it completes last.
TREE_FOLD_DEFAULT_CALL_MS was the per-call latency the publication
estimate assumed before any call was timed, but the fold times its head
GET before it plans anything and throws if that GET fails, so every
estimate already has a measurement. Setting the constant to 0 changed
no outcome. Start the slowest-call measurement at 0 and remove the
constant.
After the budget check at the first entry of a log batch, the fold read
ahead the nodes of the whole batch, up to 16 entries, before the next
check could run. At depth 2 and width 6 that is several rounds of the
slowest call at each level, all of it past a check that may have
passed just under the budget.

The read-ahead now counts its own rounds (one per `parallelCalls`
entries at each level below the root, `FoldPlanner.prefetchLevels`, at
the slowest call timed) with the elapsed time and the publication
estimate, and reads ahead the most entries that still fit, at least
one. The rest of the batch is read ahead the same way when the fold
reaches it, so a tick overshoots its last passing check by at most one
entry. While nothing is publishable it still reads ahead one entry at
a time.
A fold pinned at its floor, behind an invalid survivor or a depth-0
byte overrun, has nothing publishable, so the tick budget does not stop
it: it reads on until the pin releases or the window or call budget
ends. That keeps folding live, but the tick can overrun its budget, and
its result hid it: a released pin reports the later stop, and a pin
that never releases reports the invalid entry.

The result (published, cas-lost, refused, and the prepared fold) now
carries `pinnedPastTickBudget: true` when that happens, and the tick
is counted once as `db.compact.pinned_past_tick_budget_total`, beside
the fold's `db.compact.stuck_entry_total`. A tick past its budget with
nothing pinned, or pinned past its floor, stops as before and carries
neither.
The tree fold CPU bench called foldTree with neither a width nor a
start, so every tick ran at the default width and counted its tick
budget from the fold's start rather than the request's arrival.

runFoldStep now takes the host: the Worker passes the Workers Paid
maintenance profile (width 6) and the time its fetch handler was
entered, and the Node baseline passes the Node profile (width 16) and
the time each sample began, before its bucket is loaded. The op-model
hosts carry their profile's `foldParallelCalls`, which measureFold
passes on; its tick budget stays off, so its counts do not depend on
how fast the host plans.
FoldPlanner.publicationNodes is not empty before any change is
accepted: it counts a fresh root over the captured tree. Say so, and
rewrap publicationStages' JSDoc.
@baer
baer added this pull request to stack #440 November 25, 2026 20:21
@baer baer changed the title tree/fold tick budget feat(tree): budget the whole fold tick, publication included Nov 25, 2026
@baer
baer merged commit 80d54c9 into main Nov 25, 2026
7 checks passed
@baer
baer deleted the tree/fold-tick-budget branch November 25, 2026 20:56
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