Skip to content

fix(sqlite-vfs): use delete range for truncate cleanup#4636

Merged
NathanFlurry merged 1 commit intomainfrom
04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup
Apr 24, 2026
Merged

fix(sqlite-vfs): use delete range for truncate cleanup#4636
NathanFlurry merged 1 commit intomainfrom
04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 13, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude Bot commented Apr 13, 2026

Review: fix(sqlite-vfs): use delete range for truncate cleanup

The changes look correct and consistent. Both the Rust and TypeScript VFS truncate implementations are now in sync, matching the CLAUDE.md parity requirement.

What this does

The truncate path previously iterated chunk indices from lastChunkToKeep + 1 to lastExistingChunk, batching individual deleteBatch/kv_delete calls in groups of up to KV_MAX_BATCH_KEYS. The PR replaces this with a single deleteRange call from lastChunkToKeep + 1 to getChunkKeyRangeEnd(fileTag).

Correctness

Parity with CLAUDE.md requirements: the spec explicitly says both implementations must use deleteRange for the truncate strategy. This PR brings both into compliance.

Edge case size == 0: when truncating to zero, last_chunk_to_keep = -1 (Rust) / lastChunkToKeep = -1 (TypeScript). The start key becomes getChunkKey(fileTag, 0), correctly deleting all chunks. The (last_chunk_to_keep + 1) as u32 Rust cast is safe because -1i64 + 1 = 0i64, which casts cleanly to 0u32.

Strictly more thorough: the old code deleted chunks only in [lastChunkToKeep+1, lastExistingChunk]. The new range end getChunkKeyRangeEnd(fileTag) covers all chunks for the file tag including any orphaned chunks beyond lastExistingChunk. This matches how delete_file already works.

Atomic: the old batch-delete loop was not atomic across batches when the chunk count exceeded KV_MAX_BATCH_KEYS. A single deleteRange is atomic.

KV_MAX_BATCH_KEYS not orphaned: the constant is still used on the write path (lines 933/935 in vfs.rs) so removing it from the truncate path does not leave dead code.

Minor observations

  • No tests were added. The existing suite likely covers truncate behavior indirectly, but an explicit test for the size == 0 and size > 0 && size % CHUNK_SIZE == 0 truncate paths on both VFS implementations would add confidence.
  • The PR description template was left blank. Describing the behavioral difference (range vs. batch enumerate) and the CLAUDE.md compliance angle would help with future archaeology.
  • docs-internal/engine/NATIVE_SQLITE_DATA_CHANNEL.md referenced in CLAUDE.md does not exist in the repo yet. When it is added, the truncate-via-deleteRange strategy should be documented there.

Summary

The change is correct, efficient, and brings both VFS implementations in line with documented requirements. Approving with the minor note about test coverage.

@NathanFlurry NathanFlurry marked this pull request as ready for review April 13, 2026 05:16
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 6eac78f to 4e380c8 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch from 4303e83 to 60882a2 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 4e380c8 to 7fbbf37 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch 2 times, most recently from 5f174d0 to c75356d Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 7fbbf37 to fe8cf4f Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch from 93f25d2 to e993f2c Compare April 24, 2026 07:33
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 455165f to b488e7c Compare April 24, 2026 07:33
Base automatically changed from 04-12-fix_sqlite-native_restore_kv_error_hook to main April 24, 2026 07:38
@NathanFlurry NathanFlurry merged commit e993f2c into main Apr 24, 2026
9 of 46 checks passed
@NathanFlurry NathanFlurry deleted the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch April 24, 2026 07:39
This was referenced Apr 24, 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