I believe prosperity should decrease when the food is decreasing. Right now the death waves happen because:
1.
Prosperity continues to increase even while food amount in storage decreases, making it decrease even faster. So even if you will just increase food storage amount - it will not help right away. Prosperity should be increased only with positive food balance. Or maybe even food shortage should be added to calculation like now, but not the food shortage below 0 (when starving), but food shortage even when town still has something in storage.
2.
Food storage is small and lasts a couple of days. It does not bring much stability. I guess it should be around 10 times larger.
3.
Prices are jumping faster when difference between current and stable price is higher (maybe that change follows some exponential equation too, see the
topic about price formulation), so if some trader brings huge amounts of food - price drops faster than it will rise back after the food is eaten.
4. Food is
eaten at much faster pace when it is available in larger amounts (overconsumption). And it even brings 10 times less prosperity.
With 1 and 2 it should already shift the food-prosperity waves equilibrium towards full granary, without garrison dying.
More than just -1 food = -1 prosperity. It works like that for food shortage. For food surplus +10 food converts to +1 prosperity. So there is an overconsumption of one order of magnitude.
And citizens eat all the food much faster to start starving again.
And when they do start starving, as you said, it takes time to bring back the demand, to decrease prosperity again.
You can see how prices do work now here:
Supply and Demand self-balancing economy and how it does work now
There are big price shifts when stocks are huge, but close to linear (and much smaller) when stocks are 0.
I do not have a knowledge of code, but I
did some tests to find out how things work.
Prices are based on prosperity and stock amount. Generally.
And food consumed is increased when the stock is higher, so it depends not solely on prosperity. There is some cap for food consumption though.