1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Negotiation: deciding what the client already has.
//!
//! # Why this is the hard part
//!
//! A `have`/`want` loop that terminates on the happy path but never on a
//! divergent history is the classic git-server failure, and it does
//! not show up against a three-commit fixture: with a linear history the very
//! first `have` is common and the loop ends on round one. It only appears when
//! the client's branch has genuinely diverged, so that round after round of
//! `have`s are all unknown to the server.
//!
//! Two things make that safe here, and both are load-bearing:
//!
//! * **Termination does not depend on the server saying `ready`.** The client
//! stops when it runs out of its own commits and sends `done`; the server's
//! `ready` is only an early exit. So the worst case of a total divergence is
//! a longer negotiation, never a hang, provided the server keeps answering.
//! That is why an empty round answers `NAK` rather than nothing.
//! * **The walk is bounded twice**: by the oldest `have`'s commit date, and by
//! an object budget. Without the date bound, every round of a divergent fetch
//! walks to the root of the repository.
//!
//! The criterion for `ready` is git's own: *every want can reach at least one
//! commit the client claims to have*. Not "some want", which stops too early
//! and sends a pack the client cannot use, and not "the client has a want",
//! which is a different question entirely.
//!
//! # What a *round* costs, which is a separate question from what a walk costs
//!
//! Everything above is about one traversal. The thing that actually cost 154
//! seconds on the `negotiate_divergent` bench arm was none of it.
//!
//! A protocol-v2 client re-sends its **whole** `have` list on every
//! `command=fetch`. So if a round is answered purely from its own arguments,
//! round *n* redoes the work of rounds 1..n and a negotiation is `O(rounds²)`.
//! That is invisible in a three-round test and invisible on a wall clock at
//! fixture size, and it hid three separate redundancies at once: a linear
//! `Vec` scan to de-duplicate `common`, a reachability walk that read every
//! commit twice, and a date cutoff re-derived by re-reading every common commit
//! every round. MEASURED 2026-08-03: **197 s → 17.9 s**, and object reads
//! **18.9 M → 6.4 M**, with no change to what any round decides.
//!
//! [`HaveMemo`] is the answer to the third of those, and its doc explains why a
//! cache here is not negotiation state. The remaining `O(rounds²)` term is the
//! walk itself ([`Reachability`] is rebuilt per round), and the measurement says so
//! rather than pretending the shape is gone.
use ;
use Mutex;
use ObjectId;
use object;
use crate;
use crateResult;
/// What one round of negotiation concluded.
/// What one `have` turned out to be.
/// Everything this connection has already learned about the `have`s offered to
/// it.
///
/// # Why this exists, and why it is only a memo
///
/// A protocol-v2 `command=fetch` carries the client's **whole** `have` list
/// every round, and [`negotiate`] is a pure function of one round's arguments.
/// So without a memo, round *n* re-asks the object store the same two questions
/// about every `have` from rounds 1..n: *do you have this?* and *when was it
/// committed?* That is `O(rounds²)` store work over a negotiation, and it is
/// paid entirely on `have`s whose answer was settled long ago.
///
/// git does not do this. `upload-pack`'s `got_oid()` looks each `have` up once
/// as it arrives and folds its date into a running `oldest_have`; the value
/// this memo computes is the same `oldest_have`, kept the same way.
///
/// **It is a cache, not negotiation state.** Every entry is a fact about the
/// object store, which does not change under a connection, so the answers are
/// identical with the memo cold or warm; there is no round-to-round *decision*
/// remembered here, and `negotiate` still sees only the `have`s of the round it
/// was handed. The one race worth naming: if a concurrent push adds an object a
/// client already offered and was told nothing about, the memo keeps saying
/// `Absent`, and the server sends that object again. Sending more than
/// necessary is slow; sending less would be a bug. The failure direction is the
/// safe one.
///
/// **Memory** is one entry per *distinct* `have`, which is client-sized rather
/// than repository-sized — but it is client-sized **across the whole
/// connection**, not per round, and that is the part that used to be missing.
/// `crate::limits` bounds one round; a client that sends a fresh page of
/// invented `have`s every round stays under that ceiling every time and still
/// grows this map without end. So the map is capped as well, by
/// [`HaveMemo::with_capacity`].
///
/// A **full memo stops recording, and answers exactly the same**. Every entry
/// is a fact about the object store, so a miss is re-derived from the store
/// rather than guessed; the only thing lost past the cap is the speed-up. That
/// is what makes a cap safe here and would not make it safe on anything holding
/// a decision.
pub
/// The two things one lock guards.
///
/// One mutex rather than two, because [`HaveMemo::resolve`] needs both in the
/// same pass and two locks in one function is a lock order waiting to be got
/// wrong by the next person to add a third.
/// What one pass over a round's `have`s worked out.
/// Run one round.
///
/// `wait_for_done` suppresses `ready` entirely: the client has asked to drive
/// the negotiation to its own end, and a server that stops early anyway has
/// accepted a capability it does not honour.
///
/// `memo` lives for the connection; see [`HaveMemo`] for why, and for why it
/// cannot change what this function decides.
pub