Skip to content

Count the blocks actually cleared when clear() is called with diff - #945

Closed
hakarrr wants to merge 3 commits into
holepunchto:mainfrom
hakarrr:fix-795-clear-diff-blocks
Closed

hakarrr wants to merge 3 commits into
holepunchto:mainfrom
hakarrr:fix-795-clear-diff-blocks

Conversation

@hakarrr

@hakarrr hakarrr commented Nov 8, 2026

Copy link
Copy Markdown

Fixes #795.

Problem

core.clear(start, end, { diff: true }) returned a stub { blocks: 0 } - the counter was never implemented after the storage move, and the test covering it was commented out.

Change

SessionState.clear now counts the blocks that actually had data in the cleared range:

  • On the default session, from the core bitfield (cheap).
  • On dependency-backed sessions, from storage reads resolved through the session's dependencies.

The count is capped at the session's length so out-of-range clears report zero, and the commented-out diff test in test/clear.js is re-enabled: clearing a stored block reports { blocks: 1 }, clearing it again reports { blocks: 0 }. README updated to document the return value.

Per the issue, named and atomic sessions are not covered by new tests here.

Verification: test/clear.js 12/12, test/basic.js 658/658, prettier --check and lunte clean.

Co-authored-by: generaltony007 144252170+GeneralTony007@users.noreply.github.com

hakarrr and others added 3 commits November 9, 2026 02:00
The diff option returned a stub { blocks: 0 }. Count blocks that had data in the cleared range: from the bitfield on the default session, and from storage reads on dependency-backed sessions.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Uncomments the diff-option test, asserting the first clear counts the stored block and a repeat clear counts zero.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
The diff option now returns a `cleared` object counting how many blocks were actually cleared, e.g. { blocks: 1 }.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
@hakarrr
hakarrr requested a review from a team November 8, 2026 20:31
@greptile-apps

greptile-apps Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5 Tier: apex

[Medium impact] The change affects callers that ask `core.clear()` for a block count.

Fix atomic-session counts before merging; the large-range and test concerns are non-blocking.

Fix All in Claude CodeFindings

  1. P1 Atomic clears report zero ▶
  2. P2 Clear loads all blocks ▶
  3. P2 Sparse clears scan every index ▶
  4. P2 Range counts lack tests ▶
Fix with agent prompt
### Issue 1
lib/session-state.js:624-626
`isDefault()` also includes atomic sessions, but `core.bitfield` does not include their staged appends. After creating `core.session({ atom })` and appending a block without flushing the atom, `clear(0, { diff: true })` clears that block but still reports `{ blocks: 0 }`. Read the atomic session’s own stored blocks instead of the parent’s bitfield.

### Issue 2
lib/session-state.js:630-634
A named-session clear with `diff` now reads every block and retains all the results in `Promise.all()` before deleting anything. Peak memory grows with all the block bytes being cleared, plus one promise per index. Use bounded batches or `createBlockStream()`, as `count()` already does, so clearing a large range does not require loading the whole range into memory.

### Issue 3
lib/session-state.js:625-627
On a normal core, this synchronous loop checks every index, including every missing block in a sparse range. Large clears therefore block other JavaScript work for an avoidable per-block scan. Use `core.bitfield.countSet(start, clearEnd - start)` instead; it counts by page and already backs `count()`.

```suggestion
          cleared.blocks += this.core.bitfield.countSet(start, clearEnd - start)
```

### Issue 4
test/clear.js:184-188
The restored test checks only one stored block and its repeated clear. It does not check counting across holes or limiting the count at `length`. Add a default-session test that clears a partly stored multi-block range with an end beyond `length`, and an out-of-range call with `diff: true`. The existing `clear(1337)` call checks only the no-diff return.

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!

---

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

Summary

Adds stored-block counts to clear() when diff is enabled and documents the result.

  • core.clear() reports how many blocks held data in the cleared range.

Reviews (1) · Last reviewed commit: "Document the clear() diff return value" · Reviewed by Greptile

Comment thread lib/session-state.js
Comment on lines +624 to +626
if (this.isDefault()) {
for (let i = start; i < clearEnd; i++) {
if (this.core.bitfield.get(i)) cleared.blocks++

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Atomic clears report zero

isDefault() also includes atomic sessions, but core.bitfield does not include their staged appends. After creating core.session({ atom }) and appending a block without flushing the atom, clear(0, { diff: true }) clears that block but still reports { blocks: 0 }. Read the atomic session’s own stored blocks instead of the parent’s bitfield.

Knowledge Base Used: Sessions, snapshots, and views

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

Comment:
**Atomic clears report zero**

`isDefault()` also includes atomic sessions, but `core.bitfield` does not include their staged appends. After creating `core.session({ atom })` and appending a block without flushing the atom, `clear(0, { diff: true })` clears that block but still reports `{ blocks: 0 }`. Read the atomic session’s own stored blocks instead of the parent’s bitfield.

**Knowledge Base Used:** [Sessions, snapshots, and views](https://app.greptile.com/-/switch-tenant?tenant=tetherio&next=%2Fcustom-context%2Fknowledge-base%2Fholepunchto%2Fhypercore%2F-%2Fdocs%2Fsessions-and-snapshots.md)

---

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

Fix in Claude Code

Comment thread lib/session-state.js
Comment on lines +630 to +634
const hasPromises = []
for (let i = start; i < clearEnd; i++) hasPromises.push(rx.getBlock(i))
rx.tryFlush()

const had = await Promise.all(hasPromises)

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 Clear loads all blocks

A named-session clear with diff now reads every block and retains all the results in Promise.all() before deleting anything. Peak memory grows with all the block bytes being cleared, plus one promise per index. Use bounded batches or createBlockStream(), as count() already does, so clearing a large range does not require loading the whole range into memory.

Knowledge Base Used: Core lifecycle and mutation API

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

Comment:
**Clear loads all blocks**

A named-session clear with `diff` now reads every block and retains all the results in `Promise.all()` before deleting anything. Peak memory grows with all the block bytes being cleared, plus one promise per index. Use bounded batches or `createBlockStream()`, as `count()` already does, so clearing a large range does not require loading the whole range into memory.

**Knowledge Base Used:** [Core lifecycle and mutation API](https://app.greptile.com/-/switch-tenant?tenant=tetherio&next=%2Fcustom-context%2Fknowledge-base%2Fholepunchto%2Fhypercore%2F-%2Fdocs%2Fcore-lifecycle.md)

---

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

Fix in Claude Code

Comment thread lib/session-state.js
Comment on lines +625 to +627
for (let i = start; i < clearEnd; i++) {
if (this.core.bitfield.get(i)) cleared.blocks++
}

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 Sparse clears scan every index

On a normal core, this synchronous loop checks every index, including every missing block in a sparse range. Large clears therefore block other JavaScript work for an avoidable per-block scan. Use core.bitfield.countSet(start, clearEnd - start) instead; it counts by page and already backs count().

Suggested change
for (let i = start; i < clearEnd; i++) {
if (this.core.bitfield.get(i)) cleared.blocks++
}
cleared.blocks += this.core.bitfield.countSet(start, clearEnd - start)

Knowledge Base Used: Sparse bitfields and availability state

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

Comment:
**Sparse clears scan every index**

On a normal core, this synchronous loop checks every index, including every missing block in a sparse range. Large clears therefore block other JavaScript work for an avoidable per-block scan. Use `core.bitfield.countSet(start, clearEnd - start)` instead; it counts by page and already backs `count()`.

```suggestion
          cleared.blocks += this.core.bitfield.countSet(start, clearEnd - start)
```

**Knowledge Base Used:** [Sparse bitfields and availability state](https://app.greptile.com/-/switch-tenant?tenant=tetherio&next=%2Fcustom-context%2Fknowledge-base%2Fholepunchto%2Fhypercore%2F-%2Fdocs%2Fsparse-bitfields.md)

---

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

Fix in Claude Code

Comment thread test/clear.js
Comment on lines +184 to +188
const cleared2 = await core.clear(0, { diff: true })
t.is(cleared2.blocks, 1, 'the stored block was cleared')

// const cleared2 = await core.clear(0, { diff: true })
// t.ok(cleared2.blocks > 0)

// const cleared3 = await core.clear(0, { diff: true })
// t.is(cleared3.blocks, 0)
const cleared3 = await core.clear(0, { diff: true })
t.is(cleared3.blocks, 0, 'the block was already cleared')

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 Range counts lack tests

The restored test checks only one stored block and its repeated clear. It does not check counting across holes or limiting the count at length. Add a default-session test that clears a partly stored multi-block range with an end beyond length, and an out-of-range call with diff: true. The existing clear(1337) call checks only the no-diff return.

Prompt To Fix With AI
This is a comment left during a code review.
Path: test/clear.js
Line: 184-188

Comment:
**Range counts lack tests**

The restored test checks only one stored block and its repeated clear. It does not check counting across holes or limiting the count at `length`. Add a default-session test that clears a partly stored multi-block range with an end beyond `length`, and an out-of-range call with `diff: true`. The existing `clear(1337)` call checks only the no-diff return.

---

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

@hakarrr

hakarrr commented Nov 8, 2026

Copy link
Copy Markdown
Author

Closing as duplicate of #795, which already implements diff counting for clear(). My apologies - I mistook that open PR for an issue when scanning the tracker.

@hakarrr hakarrr closed this Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant