Skip to content

Commit 3bbc43b

Browse files
baxendrone02
andcommitted
fix(unread): preserve protected floors and tied covering receipts
Co-authored-by: drone02 <b742e3775b7520952397ec6abedada58f426dc3cb5c3912414e5d66201ba21bc@buzz.block.builderlab.xyz> Signed-off-by: Bradley Axen <[email protected]>
1 parent 1833d19 commit 3bbc43b

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎src/features/relay/read-state-retention.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,74 @@ it("bounds the local reserve by entries and bytes, dropping oldest receipts dete
298298
).toEqual(kept);
299299
}
300300
});
301+
it("keeps a tied covering receipt in the reserve's last slot without raising the floor", () => {
302+
const reserve = {
303+
...Object.fromEntries(
304+
Array.from({ length: READ_RESERVE_KEYS - 1 }, (_, n) => [
305+
`msg:${n}`,
306+
200,
307+
]),
308+
),
309+
"thread-activity:root": 100,
310+
"thread:root": 100,
311+
};
312+
const kept = retainLocalRead([], {}, "fixture", reserve, (key, frontier) =>
313+
key === "thread-activity:root" &&
314+
(frontier("thread:root") ?? -1) >= (frontier(key) ?? 0)
315+
? "thread:root"
316+
: undefined,
317+
);
318+
expect(Object.keys(kept.reserve)).toHaveLength(READ_RESERVE_KEYS);
319+
expect(kept.reserve["thread:root"]).toBe(100);
320+
expect(kept.reserve["thread-activity:root"]).toBeUndefined();
321+
expect(kept.floor).toBe(0);
322+
});
323+
it("never advances the floor when protected ancestry fills the reserve ahead of newer messages", () => {
324+
const reserve = Object.fromEntries(
325+
Array.from({ length: READ_RESERVE_KEYS }, (_, n) => [
326+
`thread:${n}`,
327+
1_000_000 + n,
328+
]),
329+
);
330+
const kept = retainLocalRead(
331+
[
332+
{
333+
frontiers: Object.fromEntries(
334+
Array.from({ length: 8000 }, (_, n) => [`msg:${n}`, 2_000_000 + n]),
335+
),
336+
overrides: { child: { set: 1, clear: 0, baseline: 0 } },
337+
},
338+
],
339+
{},
340+
"fixture",
341+
reserve,
342+
undefined,
343+
10,
344+
);
345+
expect(kept.reserve).toEqual(reserve);
346+
expect(kept.floor).toBe(10);
347+
});
348+
it("reports protected ancestry overflow rather than dropping inherited floors", () => {
349+
const reserve = Object.fromEntries(
350+
Array.from({ length: READ_RESERVE_KEYS + 10 }, (_, n) => [
351+
`thread:${n}`,
352+
1_000_000 + n,
353+
]),
354+
);
355+
expect(() =>
356+
retainLocalRead(
357+
[
358+
{
359+
frontiers: {},
360+
overrides: { child: { set: 1, clear: 0, baseline: 0 } },
361+
},
362+
],
363+
{},
364+
"fixture",
365+
reserve,
366+
),
367+
).toThrow("Read override ancestry capacity reached; saved floors retained");
368+
});
301369
it("returning journal keys keep their highest archived frontier", () => {
302370
const kept = retainLocalRead(
303371
[{ frontiers: { "msg:old": 5 }, overrides: {} }],

‎src/features/relay/read-state-retention.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ export function retainLocalRead(
298298
const accepted = new Map<string, number>();
299299
// A mark that a kept broader mark already reads is redundant here too, as
300300
// in the journal: keep it and it only takes room from real reads. A cover
301-
// reads through at least the time of what it covers, so newest first
302-
// accepts it before what it covers.
301+
// reads through at least the time of what it covers, so newest first,
302+
// then broader scope at equal times, accepts it before what it covers.
303303
// Overrides make inherited ancestry load-bearing; keep everything then.
304304
const coveredBy = overrides.size ? undefined : covered;
305305
const redundant = (key: string, value: number) => {
@@ -325,6 +325,7 @@ export function retainLocalRead(
325325
([a, av], [b, bv]) =>
326326
Number(protectedKey(b)) - Number(protectedKey(a)) ||
327327
bv - av ||
328+
scope(a) - scope(b) ||
328329
a.localeCompare(b),
329330
)) {
330331
// Once one mark does not fit, every later one is dropped too.
@@ -342,6 +343,15 @@ export function retainLocalRead(
342343
entries.length >= READ_RESERVE_KEYS ||
343344
bytes + cost > READ_RESERVE_BYTES
344345
) {
346+
// Protected-first ordering is not newest-first: a dropped message can
347+
// be newer than every retained ancestor. Never raise the floor then.
348+
if (overrides.size) {
349+
if (protectedKey(key))
350+
throw new Error(
351+
"Read override ancestry capacity reached; saved floors retained",
352+
);
353+
break;
354+
}
345355
full = true;
346356
raised = Math.max(raised, value);
347357
continue;

0 commit comments

Comments
 (0)