# Incremental SQLite engine rewrite ## Compatibility contract Retain SQLite's database, rollback journal and WAL formats, including the ability to read and write databases from the deployed generation. Preserve the public C ABI, SQL behavior, error codes and transaction semantics that existing callers use. Preserve lock acquisition and release behavior, reader snapshots, and the ordering of durable writes, syncs and checkpoints. Legacy data remains usable throughout the owner's transition window without a format conversion. SQLite's existing durability settings stay in effect. [SQLite format](https://sqlite.org/fileformat2.html) [SQLite WAL design](https://sqlite.org/wal.html) ## Stages 1. **File-format varints.** The standalone Ziran primitive decodes one to nine bytes. It accepts the same complete encodings as `sqlite3GetVarint`, including overlong encodings. The ninth byte contributes eight bits. Zero means a null argument or a truncated input and leaves the result untouched. A caller supplies a readable input range and an aligned, writable 64-bit result. Signed interpretation of the returned bits belongs to the caller. The tests compare width and value against the pinned C implementation and place every input prefix against an inaccessible page. The baseline still uses upstream C at every engine call site. 2. **Bounded record reads.** Map the original decoder's callers and their physical buffer limits. Pick a single read-only call site with a verified remaining length and existing corruption error path. Compare complete and malformed record headers with upstream over real database pages, including compatibility across integer size boundaries and overflow pages. Verify both code paths against upstream and collect representative latency and throughput measurements before enabling a replacement. Keep a build-time way to select upstream C and test both paths. 3. **Higher-level engine work.** Port larger components only after stable differential coverage establishes SQL results, errors and compatible on-disk effects. For B-tree, pager, VFS or WAL changes, require upstream regression and fault suites, parallel reader/writer tests, process and injected I/O crash tests, and recovery with both the old engine and the new one. Process kills cover process recovery. Loss of power and incomplete disk writes require fault injection or a test VFS. ## Binding and Mesh integration The binding continues to export `sqlite/SQLite`. For an isolated downstream trial, add the engine as a normal Ziran package at the verified exact Gitea commit: ```sh ziran add https://git.waozi.xyz/ziranlang/sqlite-engine.git --name sqlite_engine --ref "$engine_revision" ``` Build changes in the binding should resolve the fetched root using `ziran pkg path sqlite_engine --locked`, then configure its `upstream/` out of tree. Preserve the binding's native link names and its exported Ziran API. For consumers that use the archive, carry through the configured SQLite link libraries, including zlib if enabled. Verify the engine's `sqlite_source_id()` and effective compile options in the linked consumer before measuring it. Remove the original engine source dependency using the package command only after the build and consumer tests select the new dependency successfully. Test the source and saved-IR C and Go binding paths and C callbacks against the upstream baseline. Rehearse Mesh schema migrations, backup/restore and DB reopen with committed copies of synthetic or approved redacted data. For changes that will reach production, compare representative sync workload latency and resource use and verify rollback with the existing engine before changing a pin. Keep encryption, key management and authorization within their existing Mesh ownership. The Ziran varint code is suitable for the isolated component tests at this stage. Production continues using the current binding and SQLite dependency until a separate integration passes those gates.