From 3f8a840df42c2847b50ad8d844fcee86af2ddacb Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:55:13 +0530 Subject: [PATCH 1/4] Fix clear() on sessions with dependencies losing later blocks Clearing a range on a session whose storage sits on a dependency truncated the dependency to the start of the range, so every block after the range silently resolved to the session's own (empty) storage and was lost. Copy the blocks after the cleared range into the session's own storage before truncating the dependency. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- lib/session-state.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/session-state.js b/lib/session-state.js index f5bf31f1..53071829 100644 --- a/lib/session-state.js +++ b/lib/session-state.js @@ -619,7 +619,23 @@ class SessionState { if (end - start === 1) tx.deleteBlock(start) else tx.deleteBlockRange(start, end) - const dependency = start < this.flushedLength() ? updateDependency(this, start, true) : null + const flushedLength = this.flushedLength() + const dependency = start < flushedLength ? updateDependency(this, start, true) : null + + if (dependency !== null && !this.isDefault() && end + 1 < flushedLength) { + // Truncating the dependency to the start of the cleared range makes the + // blocks after the range resolve against this session's own storage, so + // copy them over first to keep them available. + const rx = this.storage.read() + const blockPromises = [] + for (let i = end + 1; i < flushedLength; i++) blockPromises.push(rx.getBlock(i)) + rx.tryFlush() + + const blocks = await Promise.all(blockPromises) + for (let i = 0; i < blocks.length; i++) { + if (blocks[i] !== null) tx.putBlock(end + 1 + i, blocks[i]) + } + } const flushed = await this.flush() From a90b88b5c6a66f80e0149b5f605efd1cfab3994b Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:55:43 +0530 Subject: [PATCH 2/4] Test clear() keeping blocks after the range on dependency sessions Reproduces #775: on a named session, clear(1, 2) also dropped every block after the range (has(s.length - 1) was false, get() never resolved). Asserts the cleared range is cleared, the surrounding blocks survive, and the default session is untouched. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- test/clear.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/clear.js b/test/clear.js index 4c1767ba..4ca43d3b 100644 --- a/test/clear.js +++ b/test/clear.js @@ -288,3 +288,25 @@ async function consumeStream(rx) { for await (const b of rx) res.push(b) return res } + +test('clear on a session with dependencies keeps the blocks after the range', async function (t) { + const a = await create(t) + await a.append(['1', '2', '3', '4', '5']) + + const s = a.session({ name: 'batch' }) + await s.ready() + + await s.clear(1, 2) + + t.ok(await s.has(0), 'block before the range is kept') + t.is(await s.has(1), false, 'block in the range is cleared') + t.is(await s.has(2), false, 'block in the range is cleared') + t.ok(await s.has(3), 'block after the range is kept') + t.ok(await s.has(s.length - 1), 'last block is kept') + t.alike(await s.get(4), b4a.from('5'), 'last block still resolves') + + t.ok(await a.has(3), 'default session is unaffected') + t.alike(await a.get(4), b4a.from('5'), 'default session block still resolves') + + await s.close() +}) From f2115d77dba11854e63605a2cea53e72f3773b73 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:57:40 +0530 Subject: [PATCH 3/4] Keep the end-boundary block when copying (end is exclusive) clear(start, end) treats end as exclusive (clear(4) clears only block 4), so the blocks after the range start at end, not end + 1. Copy from end so the block right after the range is kept too. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- lib/session-state.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/session-state.js b/lib/session-state.js index 53071829..233ddc67 100644 --- a/lib/session-state.js +++ b/lib/session-state.js @@ -622,18 +622,18 @@ class SessionState { const flushedLength = this.flushedLength() const dependency = start < flushedLength ? updateDependency(this, start, true) : null - if (dependency !== null && !this.isDefault() && end + 1 < flushedLength) { + if (dependency !== null && !this.isDefault() && end < flushedLength) { // Truncating the dependency to the start of the cleared range makes the - // blocks after the range resolve against this session's own storage, so - // copy them over first to keep them available. + // blocks after the range (end is exclusive) resolve against this + // session's own storage, so copy them over first to keep them available. const rx = this.storage.read() const blockPromises = [] - for (let i = end + 1; i < flushedLength; i++) blockPromises.push(rx.getBlock(i)) + for (let i = end; i < flushedLength; i++) blockPromises.push(rx.getBlock(i)) rx.tryFlush() const blocks = await Promise.all(blockPromises) for (let i = 0; i < blocks.length; i++) { - if (blocks[i] !== null) tx.putBlock(end + 1 + i, blocks[i]) + if (blocks[i] !== null) tx.putBlock(end + i, blocks[i]) } } From b85e8ec3dc0bc2bb7b2676eb52773e127fd8ec1c Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:58:01 +0530 Subject: [PATCH 4/4] Assert the end-boundary block is kept The range end is exclusive, so clear(1, 2) clears only block 1: block 2 must survive with its value intact. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- test/clear.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/clear.js b/test/clear.js index 4ca43d3b..2a54908b 100644 --- a/test/clear.js +++ b/test/clear.js @@ -300,9 +300,10 @@ test('clear on a session with dependencies keeps the blocks after the range', as t.ok(await s.has(0), 'block before the range is kept') t.is(await s.has(1), false, 'block in the range is cleared') - t.is(await s.has(2), false, 'block in the range is cleared') + t.ok(await s.has(2), 'block at the end of the range is kept (end is exclusive)') t.ok(await s.has(3), 'block after the range is kept') t.ok(await s.has(s.length - 1), 'last block is kept') + t.alike(await s.get(2), b4a.from('3'), 'block at the end boundary still resolves') t.alike(await s.get(4), b4a.from('5'), 'last block still resolves') t.ok(await a.has(3), 'default session is unaffected')