Skip to content

fix(sqlite-native): keep truncate cache coherent#4637

Open
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanupfrom
04-12-fix_sqlite-native_keep_truncate_cache_coherent
Open

fix(sqlite-native): keep truncate cache coherent#4637
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanupfrom
04-12-fix_sqlite-native_keep_truncate_cache_coherent

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

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

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

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

PR Review: fix(sqlite-native): keep truncate cache coherent

Both bugs fixed here are real and the fixes are correct.

Fix 1 — off-by-one in cache eviction boundary

The old formula:

let truncate_from_chunk = if size == 0 { 0u32 } else { (size as u32 / kv::CHUNK_SIZE as u32) + 1 };
// kept chunks with: chunk_idx < truncate_from_chunk

was off-by-one on chunk-aligned truncations. Truncating to exactly CHUNK_SIZE bytes gave truncate_from_chunk = 2, retaining chunk 1 in the cache even though chunk 1 now lies beyond the new file end.

The new formula uses (size - 1) / CHUNK_SIZE with chunk_idx as i64 <= last_chunk_to_keep, which correctly matches the KV-level delete boundary.

Fix 2 — stale cache after partial-chunk truncation

The old code wrote the shortened slice to KV via kv_put but never updated the read cache, causing subsequent reads to return stale (longer) cached data for the last chunk. The new code inserts the truncated chunk into the read cache after a successful kv_put, keeping both stores coherent.

Variable reordering

Moving last_chunk_to_keep / last_existing_chunk before the read_cache.retain block is necessary to use the correct boundary in Fix 1 and is a clean structural improvement.


Concerns

WASM VFS parity (required per CLAUDE.md)

The repo requires the native Rust VFS and the WASM TypeScript VFS to match 1:1. The WASM truncate path in rivetkit-typescript/packages/sqlite-wasm/src/vfs.ts should be audited for the same two bugs (off-by-one cache eviction and stale cache after partial-chunk write). No corresponding WASM change is included here.

Test coverage

No tests were added. Suggested cases to cover:

  • Truncate to a chunk-aligned boundary (exercises Fix 1)
  • Truncate to a non-aligned size (exercises Fix 2)
  • Truncate to zero

Minor: cache eviction before metadata write

The read cache is evicted before the metadata is written. If the metadata write fails, evicted entries are gone from the cache but the KV store is unchanged, forcing unnecessary round-trips on subsequent reads. This is a performance issue on error paths only, not a correctness issue.


Summary

The two correctness fixes are well-reasoned and the math is sound. The main outstanding item is WASM VFS parity, which is a hard requirement per the project's conventions.

@NathanFlurry NathanFlurry marked this pull request as ready for review April 13, 2026 05:18
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_keep_truncate_cache_coherent branch from 4079bac to 459dd12 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_keep_truncate_cache_coherent branch from 459dd12 to d7b2c43 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch from 5f174d0 to c75356d Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_keep_truncate_cache_coherent branch from d7b2c43 to 6c17fae Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch from c75356d to dfaae41 Compare April 13, 2026 21:07
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_keep_truncate_cache_coherent branch from 6c17fae to a070720 Compare April 13, 2026 21:07
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