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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! What the commit feed tells the search index, and how little it is allowed
//! to do about it.
//!
//! A mark runs on whatever task is draining the feed, and that task's next
//! chunk must not wait on a replay: every replay this index performs therefore
//! happens on a background worker, and marking only records what the worker
//! still owes. The mark is a map lookup and an integer merge, nothing more.
//!
//! # Two inputs, because the feed has one blind spot
//!
//! [`CommitMarks::note_commit`] takes a chunk of the durable commit feed
//! (#1565, chunk B6), which is the steady state. A destroy, an excision, a
//! repair, or a migration changes a partition's durable content without
//! committing, so none of them appears on the feed at all —
//! [`CommitMarks::note_partition_change`] is how they arrive, from whoever
//! issued the command, after that command returned its receipt.
//!
//! # Why the mark must not panic
//!
//! A panic here would take out the task draining the feed, and that
//! partition's mark would be lost — with nothing on the read path ever
//! disagreeing with the stale watermark left behind. So the mark does only
//! infallible work (integer comparison, map insert), and a poisoned lock
//! escalates to `DirtySet::degraded` rather than unwinding: a mark that
//! cannot be recorded makes the whole index refuse, which is loud, instead of
//! silently dropping one conversation's updates, which is not.
//!
//! # A missed mark costs latency, not correctness
//!
//! Feed delivery is at-least-once and a subscription can stop without saying
//! so, so no mark may be the only thing standing between a conversation and
//! its coverage. Two properties close that. Marking is idempotent by
//! construction — `Pending::merge` keeps whichever side demands more work,
//! so a redelivered chunk merges to the same pending state it already had.
//! And [`crate::search_index::worker::SearchIndexWorker::reconcile`] sweeps
//! the whole deployment on its own schedule, re-establishing coverage for
//! every partition whether anything marked it or not; a consumer that knows
//! its own feed went dark says so with [`CommitMarks::note_coverage_doubt`]
//! and brings that sweep forward.
//!
//! # Why the queue is bounded, and what overflow means
//!
//! An unbounded dirty set is a memory leak with a busy fleet behind it. A
//! bounded one that silently forgets is worse: the forgotten partition keeps
//! serving a stale watermark forever. So overflow sets `DirtySet::degraded`,
//! and a degraded index refuses every search until a full reconcile
//! re-establishes coverage.
//!
//! An earlier draft of the design record asked overflow to enqueue the
//! partition for rebuild and apply backpressure, and named fleet-wide refusal
//! as the outcome to avoid. Neither half of that is available here: the set is
//! already full by definition, and an observer runs on the write path where it
//! cannot block. Global refusal is the only fail-closed option left with a
//! hard bound — a search quietly answering "not found" from a partition nobody
//! re-read is a correctness failure, where a loud refusal is an availability
//! one. The record now states this behavior
//! (`docs/proposals/participation-scoped-agent-search.md`, "No silent document
//! eviction").
//!
//! The forgotten partition is recovered by
//! [`crate::search_index::worker::SearchIndexWorker::reconcile`], which
//! enumerates every partition rather than draining this queue — so it does not
//! depend on the entry that was dropped, and it is the only caller of
//! [`DirtySet::clear_degraded`].
//!
//! With that reconcile in place the bound stays where it is. Raising it trades
//! a larger resident map for a later refusal without removing the refusal, and
//! the recovery no longer depends on the bound at all: overflow now costs one
//! full sweep, not a restart.
use BTreeMap;
use ;
use kinds;
use FeedRecord;
use cratePartitionChange;
/// Prefix every conversation partition carries — `partition_for` in the
/// control plane.
///
/// Duplicated here rather than depended on, exactly as
/// [`crate::dashboard`] duplicates it: a Component cannot depend on the
/// Container that composes it.
const CONVERSATION_PARTITION_PREFIX: &str = "conv-";
/// Whether `partition` is a conversation this index has any business tracking.
///
/// Marks arrive for every partition in the deployment — `persona-<id>-mem`,
/// `admin-audit`, `skill-share-ledger`, the enrollment and workqueue
/// partitions. Without this filter a memory erasure on `persona-abc-mem` would
/// insert a removal for a conversation the index has never held, consume the
/// tracking budget that overflow turns into a fleet-wide refusal, and hand the
/// worker a partition it cannot act on.
///
/// The sibling projection in this crate guards its own input the same way
/// (`crate::dashboard::conversation_id_from_partition`).
///
/// Shared with [`crate::search_index::worker`] rather than restated there: the
/// reconcile enumerates every partition in the deployment and must admit
/// exactly the set this observer marks, or a conversation is either swept by
/// one and not the other — a partition the reconcile establishes coverage for
/// but no append ever re-marks, or one marked dirty that no reconcile can
/// repair.
pub
/// The most partitions the dirty set tracks before it declares itself
/// degraded.
///
/// Sized well above any plausible burst of concurrently active conversations,
/// because crossing it is a correctness event rather than a load-shedding one:
/// past this point the index cannot promise it knows what changed.
pub const MAX_TRACKED_PARTITIONS: usize = 16_384;
/// What the worker still owes a partition.
pub
/// The set of partitions the index owes work on.
///
/// Shared between [`CommitMarks`] (which only ever adds) and the worker (which
/// drains). Deliberately holds no handle to the store, the event log, or
/// anything else that could block: the write path reaches this and nothing
/// beyond it.
pub
/// Marks partitions dirty as commits and mutations reach this index.
///
/// # Only ever driven alongside a running worker
///
/// Driving it by itself is safe from a crash standpoint — every method here is
/// infallible — but the set it fills would never be emptied. It grows by one
/// entry per newly-dirtied conversation until it crosses
/// `MAX_TRACKED_PARTITIONS`, at which point `DirtySet::degraded` flips and
/// stays flipped until something re-establishes coverage for every partition.
///
/// [`crate::search_index::SearchIndex`] is what makes the correct wiring the
/// easy one: it hands out this handle and owns
/// `SearchIndexWorker` over the SAME
/// `DirtySet`, so a Container cannot accidentally build a second set that
/// nothing drains, and `marks()` borrows where `run()` consumes, so the handle
/// cannot outlive its own loop by construction.
///
/// It does not make the mistake impossible. `open(...).marks()` followed by
/// dropping the [`crate::search_index::SearchIndex`] compiles, and driving that
/// handle would fill a set nothing empties. Closing that by construction would
/// mean the handle could not exist before the worker is supervised, which no
/// ownership arrangement here expresses — so it is stated rather than claimed
/// away, and the Container's own wiring (`crates/control-plane`) is where the
/// pairing is checked.
///
/// Holds nothing but the shared `DirtySet`.