Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 < flushedLength) {
// Truncating the dependency to the start of the cleared range makes the
// 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; 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 + i, blocks[i])
Comment on lines +629 to +636

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Small clears use huge memory

Clearing a small range near the start of a large named session now reads every later index and holds all returned blocks in memory at once. Sparse sessions also create a read and promise for every missing index.

Use createBlockStream() and bounded batches to stage the copies before applying the clear and shortening the dependency.

Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/session-state.js
Line: 629-636

Comment:
**Small clears use huge memory**

Clearing a small range near the start of a large named session now reads every later index and holds all returned blocks in memory at once. Sparse sessions also create a read and promise for every missing index.

Use `createBlockStream()` and bounded batches to stage the copies before applying the clear and shortening the dependency.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code

}
}

const flushed = await this.flush()

Expand Down
23 changes: 23 additions & 0 deletions test/clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,26 @@ 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.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')
Comment on lines +306 to +307

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing blocks stall the test

The new test uses waiting get() calls to check local blocks. If the regression returns, a missing block makes get() wait for replication with no request timeout, and this test has no peers. Pass { wait: false } to these reads so missing blocks produce assertion failures instead of unfinished checks.

Suggested change
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.alike(await s.get(2, { wait: false }), b4a.from('3'), 'block at the end boundary still resolves')
t.alike(await s.get(4, { wait: false }), b4a.from('5'), 'last block still resolves')
Prompt To Fix With AI
This is a comment left during a code review.
Path: test/clear.js
Line: 306-307

Comment:
**Missing blocks stall the test**

The new test uses waiting `get()` calls to check local blocks. If the regression returns, a missing block makes `get()` wait for replication with no request timeout, and this test has no peers. Pass `{ wait: false }` to these reads so missing blocks produce assertion failures instead of unfinished checks.

```suggestion
  t.alike(await s.get(2, { wait: false }), b4a.from('3'), 'block at the end boundary still resolves')
  t.alike(await s.get(4, { wait: false }), b4a.from('5'), 'last block still resolves')
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code


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()
})