beforeSwap.
Block-open reference price
On the first swap of every block,beforeSwap reads slot0 before that swap executes and stores it as refSqrtPriceStored for the block.
refSqrtPrice() is used for live accounting: deposits, withdrawals, range changes and views. A swap later in the same block cannot move it, which defeats in-block flash manipulation.
Epoch TWAP settle price
The same hook call extends a tick accumulator: each block-open tick is weighted by how many seconds it held.settleEpoch() marks every portfolio at settleSqrtPrice(). A price pushed at the end of block N and “settled” first thing in block N+1 only contributes the few seconds it actually held, so it barely moves a 15-minute average.
Which price is used where
The deviation guard
maxTickDeviation ticks (300 on testnet, about 3%) from the block-open price within the current block, someone is pushing the price, and every action that moves real vault liquidity refuses to run (PriceDeviation), except settlement, which defers the move.