Skip to content

fix(sqlite-native): delete metadata before chunk range#4638

Open
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-native_keep_truncate_cache_coherentfrom
04-12-fix_sqlite-native_delete_metadata_before_chunk_range
Open

fix(sqlite-native): delete metadata before chunk range#4638
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-native_keep_truncate_cache_coherentfrom
04-12-fix_sqlite-native_delete_metadata_before_chunk_range

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): delete metadata before chunk range

The fix is directionally correct. Deleting metadata before chunks is the crash-safe ordering: if the process is killed between the two operations, you end up with orphaned chunks (wasted space) rather than live metadata pointing at missing chunks (potential read-time corruption).

Inconsistency with the WASM VFS

The CLAUDE.md requirement says: "The native Rust VFS and the WASM TypeScript VFS must match 1:1." This PR only updates the native Rust VFS, leaving the WASM VFS out of sync.

In rivetkit-typescript/packages/sqlite-vfs/src/vfs.ts (lines 1402-1407), the #delete method still deletes chunks before metadata:

// Delete all chunks via range delete and the metadata key.
await options.deleteRange(
    getChunkKey(fileTag, 0),
    getChunkKeyRangeEnd(fileTag),
);
await options.deleteBatch([metaKey]);   // metadata last — still wrong

The WASM VFS is also internally inconsistent: xTruncate (~line 1293) carries a comment explicitly motivating metadata-first order ("Update metadata first so a crash leaves orphaned chunks rather than metadata pointing at missing chunks (corruption)") but #delete does the opposite.

Suggested fix for the WASM VFS #delete method

// Delete metadata first so a crash leaves orphaned chunks (wasted
// space) rather than metadata pointing at missing chunks (corruption).
await options.deleteBatch([metaKey]);
await options.deleteRange(
    getChunkKey(fileTag, 0),
    getChunkKeyRangeEnd(fileTag),
);

Summary

Native Rust VFS (after this PR) WASM VFS (unchanged)
xDelete order metadata then chunks ✅ chunks then metadata ❌
xTruncate order metadata then chunks ✅ metadata then chunks ✅

The native fix is correct — please also update packages/sqlite-vfs/src/vfs.ts in the same change to keep both implementations in sync and fix the internal inconsistency in the WASM VFS.

@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_delete_metadata_before_chunk_range branch from 12c0baa to 61b5457 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_keep_truncate_cache_coherent branch 2 times, most recently from 459dd12 to d7b2c43 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_delete_metadata_before_chunk_range branch from 61b5457 to bc7e3d7 Compare April 13, 2026 05:50
@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-native_delete_metadata_before_chunk_range branch from bc7e3d7 to 49fae2b Compare April 13, 2026 07:03
@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
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_delete_metadata_before_chunk_range branch from 49fae2b to acf0da5 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