Skip to content

chore(sync): strict common-v8 parity pass on Bun-first fork - #39

Merged
miccy merged 62 commits into
mainfrom
miccy-dev
Feb 16, 2026
Merged

chore(sync): strict common-v8 parity pass on Bun-first fork#39
miccy merged 62 commits into
mainfrom
miccy-dev

Conversation

@miccy

@miccy miccy commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • align packages/common/src/local-first/Db.ts and packages/common/src/local-first/Shared.ts with upstream common-v8 worker orchestration flow
  • keep Bun-first runtime/tooling (bun, biome) and avoid reintroducing pnpm/eslint/prettier
  • keep fork-only protocol surface behind explicit Experimental* aliases while preserving upstream Db exports
  • add upstream ancestry marker commit (merge -s ours upstream/common-v8) so next sync wave has explicit merge base
  • update parity tracking doc (.ai/knowledge/common-v8-parity-plan-2026-02-16.md)

Validation

  • bun run verify:fast
  • bunx vitest run packages/common/test/local-first/Shared.test.ts packages/common/test/local-first/Worker.test.ts packages/common/test/local-first/Evolu.test.ts
  • bunx vitest run packages/web/test/DbWorker.test.ts
  • bun test ./packages/bun/test/BunDbWorker.test.ts

Notes

  • intentional divergence remains in packages/common/src/local-first/Evolu.ts runtime queue path (Bun-first direct DbWorkerClient queue), API-compatible with upstream common-v8.

Summary by CodeRabbit

  • New Features

    • Added platform-agnostic shared worker implementation for coordinating multi-tab communication.
    • Introduced enhanced worker and message port testing utilities.
    • Added console-level tracking and forwarding across worker boundaries.
  • API Changes

    • Simplified SQLite execution API to return results directly; errors now surface via exceptions.
    • Refactored storage operations to return concrete values instead of wrapped results.
    • Updated mutation queue processing with improved serialization and retry logic.
  • Bug Fixes

    • Improved error handling with explicit console logging at protocol and storage boundaries.
    • Enhanced database schema validation and quarantine message handling.
  • Tests

    • Added comprehensive worker lifecycle and message channel testing.
    • Expanded SQLite and storage integration test coverage.

Introduce a tab message channel (InitTab) and typed EvoluTabOutput to carry ConsoleEntry and EvoluError messages between the worker and the UI. createEvoluDeps now creates and exposes a read-only evoluError store, wires tab channel outputs into console logging and the evoluError store (including fallback wrapping for untyped console.error entries), and returns a disposable deps object that disposes used resources. The worker init was updated to queue and broadcast EvoluTabOutput values, and EvoluWorker/Worker input types were adjusted accordingly. Tests and a minimal playground example were updated to reflect the new InitTab flow and to assert evoluError behavior. Also removed the now-unused DisposableStackDep/AsyncDisposableStackDep interfaces from Types.ts.
Added a reusable microtask batching helper for coalescing queued items per tick.

Added focused tests for coalescing, flushNow behavior, and reentrant pushes.

Exported the new utility from common index.
Added a reusable reference counting helper for arbitrary keys.

Added focused tests for increment/decrement behavior, key snapshots, clear, and object identity semantics.

Exported the new utility from common index.
Added Evolu-to-worker mutation input contract and worker-side message handling scaffold.

Implemented mutation dispatch flow in createEvolu with microtask batching and serialized mutate payloads.

Expanded local-first mutation tests for insert/update/upsert dispatch, coalescing, ownerId, and onComplete callback IDs.

Updated React web createEvoluDeps doc wording to match shared dependency semantics.
Added a reusable prompt that guides commit batching workflow with explicit approval gates for batch selection and commit message confirmation.
Added testCreateWorker in common worker abstractions and added coverage in the worker test suite for dedicated worker communication, message queueing, and disposal behavior.
Added DB worker dependency wiring to local-first core by introducing CreateDbWorker/DbWorkerInput types, invoking createDbWorker() in createEvolu, and sending { type: "init", name } to the DB worker. Updated local-first tests to provide a test DB worker and assert init messaging.
Updated web and React Native platform deps to provide createDbWorker, switched web shared-worker entry path to Shared.worker.js, renamed Worker.worker.ts to Shared.worker.ts, and added Db.worker.ts with TODO notes for DB-worker heartbeat based stale-leader detection and failover via SharedWorker.
Added cross-platform LeaderLock abstraction and dependency in common platform types.
Introduced shared worker init deps (WorkerInitDep, WorkerDeps) and reused them in worker init tasks.
Wired DB worker initialization to acquire leader lock before activation.
Implemented web leaderLock and switched web worker entrypoints to createRun(createWorkerDeps()).
Added React Native leaderLock, exported it, and wired RN shared deps to run initDbWorker/initSharedWorker with worker-side deps.
Added testName next to Name in common type definitions.
Added testAppName next to AppName in local-first Evolu module.
Established canonical source exports for shared test identifiers.
Renamed test helper module from _deps.nodejs.ts to _deps.ts.
Updated common test imports to use the renamed module path.
Switched shared test name/app name usage to canonical source exports.
Removed stale _deps references from local-first test comments.
Added leaderLock tests for React Native platform behavior.
Verified lock acquisition waits for prior lease disposal for same name.
Verified independent lock acquisition for different names.
Reused shared testName constant from @evolu/common.
Added browser-mode integration tests for web leaderLock behavior.
Covers same-name serialization and different-name independent acquisition.
Uses shared testName constant for stable lock naming.
Removed native console output from web createWorkerDeps.
Kept console output routed through consoleStoreOutput only.
Avoids duplicate logs when worker output is forwarded via shared worker.
Change the Run interface so the daemon property is typed as Run<D> rather than a function signature, and update createRunInternal to assign run.daemon = daemon ?? self instead of wrapping it in a lambda. This aligns the runtime assignment with the new type, removes an unnecessary closure, and resolves related typing mismatches.
Added deterministic test value exports to Test.ts (testEntropy32, testOwnerSecret, testAppOwner).

Kept eager test values in the test utility module to avoid affecting runtime tree-shaking paths.

Documented the rationale near the exports and placed them at the end of the file.
Configured the minimal playground to pass appOwner: Evolu.testAppOwner to createEvolu.

Keeps owner identity stable across reloads for deterministic local behavior.

Reuses shared test utility owner instead of ad-hoc inline owner creation.
Updated initDbWorker to return and manage AsyncDisposableStack directly without await using/move.

Switched leader lock acquisition to run.daemon(...) and reused leaderLock from deps.

Extracted initialization work into initializeDb(name) task and moved logging/TODO heartbeat note there.
Typed NativeMessagePort with input and output phantom types to make port transfer wiring type-safe across platforms. Updated platform worker adapters to preserve these types at boundaries.
Renamed init ports to port1 and port2, replaced broker naming with leader semantics, and scoped Evolu channel wiring for readability. Updated Db/Shared contracts and tests to match the clearer message flow.
Documented that Evolu instance communication is routed through SharedWorker as the central point for observability and resilience mechanisms such as retry and timeout.
Removed WorkerInitDep in favor of a single WorkerDeps contract, wired DbWorker console store output through the leader channel, and updated SharedWorker handling/tests to forward DbWorker console entries to tab outputs.
Added full DbWorker init inputs (consoleLevel, dbSchema, encryptionKey) and wired them from createEvolu through worker channels.
Unified leader output typing with EvoluTabOutput, added typed EvoluError forwarding, and updated SharedWorker InitTab to receive/apply consoleLevel.
Updated schema conversion typing to avoid external casts, wired sqlite driver in web Db worker run deps, and aligned SharedWorker tests with the new InitTab contract.
Updated React Native shared dependency creation to require and pass CreateSqliteDriverDep into worker runtime deps.
Updated the Expo SQLite export to provide createExpoSqliteDriver when creating Evolu deps.
steida and others added 15 commits February 16, 2026 02:08
…storage boundaries

- Removed SqliteError from SQLite public APIs and switched infra failure handling to throws
- Kept Result for domain/protocol outcomes and mapped boundary failures to protocol-level errors
- Added Sqlite.transaction overload for callbacks returning Result<T, E> or void
- Simplified storage/query/schema/sync call sites and updated relay/client boundary behavior
- Updated docs/changeset and tests across common, nodejs, and web to match new semantics
- Follow-up cleanup removed redundant Result<void> leftovers in storage helpers

(cherry picked from commit f94006d)
Add getOk to Result utilities to safely extract values from Result<T, never> with an assertion guard.

Add tests covering getOk success path, type-level constraints, and runtime invariant violation.

Include getOrNull tests to keep Result extraction helpers consistently covered.

(cherry picked from commit b9fcb47)
Replace manual Result unwrapping with getOk in DB and relay SQLite initialization paths.

Simplify dependent code by working with unwrapped sqlite values directly.

Update shared test deps helper to align with the same unwrapping pattern.

(cherry picked from commit af2e3f6)
…er and sync operations, and update dependencies.
Removed WorkerInitDep in favor of a single WorkerDeps contract, wired DbWorker console store output through the leader channel, and updated SharedWorker handling/tests to forward DbWorker console entries to tab outputs.

(cherry picked from commit 0bbef0a)
Added full DbWorker init inputs (consoleLevel, dbSchema, encryptionKey) and wired them from createEvolu through worker channels.
Unified leader output typing with EvoluTabOutput, added typed EvoluError forwarding, and updated SharedWorker InitTab to receive/apply consoleLevel.
Updated schema conversion typing to avoid external casts, wired sqlite driver in web Db worker run deps, and aligned SharedWorker tests with the new InitTab contract.

(cherry picked from commit 57d2390)
@coderabbitai

coderabbitai Bot commented Feb 16, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This pull request implements a comprehensive refactoring to remove SqliteError from public APIs and transition to throw-first error semantics. It introduces generic type parameters to message ports for better type safety, establishes a platform-agnostic Shared Worker implementation for coordinating worker communication, and significantly simplifies storage and database interfaces by eliminating Result-wrapped returns in favor of direct values. The changes span configuration, type definitions, test infrastructure, and implementation across multiple packages.

Changes

Cohort / File(s) Summary
SQLite API Refactoring
packages/common/src/Sqlite.ts, packages/nodejs/src/local-first/Relay.ts
Removes SqliteError from public exports and changes method signatures: exec, export return direct values instead of Result wrappers; transaction introduces new SqliteTransaction interface supporting both generic and void callback forms; createSqlite removes SqliteError from Task error channel. Simplifies driver initialization by using getOk() helper instead of explicit result checks.
Storage Interface Simplification
packages/common/src/local-first/Storage.ts, packages/common/src/local-first/Relay.ts
Eliminates nullable returns and Result wrappers: getSize, fingerprint, fingerprintRanges, findLowerBound return non-nullable values; setWriteKey, deleteOwner return void; writeMessages removes StorageWriteError; readDbChange returns non-nullable EncryptedDbChange. Removes StorageWriteError type entirely. Simplifies base storage factory from curried config pattern to no-config initialization.
Error Handling Consolidation
packages/common/src/local-first/Error.ts, packages/common/src/local-first/Sync.ts, packages/common/src/local-first/Query.ts, packages/common/src/local-first/Schema.ts
Removes SqliteError from EvoluError union and related imports. Updates function returns from Result<T, SqliteError> to direct values or void (e.g., loadQueries, getDbSchema, ensureDbSchema, clock/sync functions). Consolidates error paths to rely on exceptions rather than error channel propagation.
Message Port Type Generics
packages/common/src/Worker.ts, packages/nodejs/src/Worker.ts, packages/react-native/src/Worker.ts, packages/web/src/Worker.ts
Introduces generic Input/Output type parameters to NativeMessagePort, MessagePort, and related abstractions. Updates CreateMessagePort signature to accept typed ports. Adds public testing utilities: testCreateWorker, testCreateSharedWorker, testCreateMessageChannel, testCreateMessagePort with in-memory implementations.
Worker Communication and Console Handling
packages/common/src/local-first/Worker.ts, packages/common/src/local-first/Evolu.ts, packages/common/src/local-first/Protocol.ts
Integrates ConsoleLevel and console store output routing into worker initialization. Updates EvoluWorkerInput message shape to use port1/port2 instead of port/brokerPort; includes consoleLevel in InitTab. Refactors mutation queue processing with re-entrancy guards and consolidates error handling to include ConsoleDep in protocol paths.
Shared Worker Implementation
packages/common/src/local-first/Shared.ts
Introduces platform-agnostic initSharedWorker function with types for SharedWorkerInput, EvoluInput, EvoluTabOutput. Coordinates tab-level console ports, per-Evolu mutation queues, leader election, and console store subscriptions. Implements retry/backoff for mutation processing and console output queuing until tab connection.
DB Worker Initialization
packages/common/src/local-first/Db.ts, packages/common/src/local-first/index.ts, packages/bun/src/BunDbWorker.ts, packages/web/src/local-first/DbWorker.ts
Adds worker-based DB initialization with initDbWorker, DbWorkerInput, and related types. Introduces schema bootstrapping, quarantine handling, and column change validation. Aliases DbWorkerInput/Output types to ExperimentalDbWorkerInput/Output in public exports; updates web worker to accept optional consoleLevel in port config.
Result Helper Utilities
packages/common/src/Result.ts, packages/common/test/Result.test.ts
Adds public getOk(result) helper that extracts Ok values from Result<T, never> with assertion. Includes documentation updates reflecting error type changes. Tests verify value extraction and type-level constraints.
Type Aliases and Backward Compatibility
packages/common/src/Type.ts, packages/react-native/src/exports/*.ts, packages/react-native/src/shared.ts
Adds deprecated aliases: Name and NameError for SimpleName and SimpleNameError. Introduces CreateSqliteDriverDep to React Native shared dependencies; wires createOpSqliteDriver and createExpoSqliteDriver into platform-specific initializers.
Test Infrastructure Refactor
packages/common/test/_deps.ts, packages/common/test/_deps.nodejs.ts, packages/common/test/Worker.test.ts, packages/common/test/local-first/*.test.ts, packages/web/test/*.ts
Consolidates test helpers into _deps.ts with testCreateRunWithSqlite and testCreateRunWithSqliteAndRelayStorage factories. Removes intermediate Result unwrapping (.ok / .value) throughout test assertions; accesses rows/changes directly. Adds comprehensive Worker/SharedWorker/MessageChannel tests. Updates test imports from _deps.nodejs.js to _deps.js.
Configuration and Policy
.ai/knowledge/common-v8-parity-plan-2026-02-16.md, .ai/memory/user-preferences.md, biome.json, .changeset/sqlite-refactor.md
Adds upstream parity plan and sync rules documentation. Moves noImportCycles from nursery to suspicious rules in Biome. Introduces changeset describing SQLite refactoring rationale and API surface changes.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 With throws where errors once were tied,
And ports now typed both far and wide,
The workers dance through channels new,
While SQLite sheds its wrapper too.
Shared workers bring the harmony,
Console logs flow wild and free! 🎪


Note

🎁 Summarized by CodeRabbit Free

The PR author is not assigned a seat. To perform a comprehensive line-by-line review, please assign a seat to the pull request author through the subscription management page by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands and usage tips.

@miccy miccy self-assigned this Feb 16, 2026
@miccy miccy added upstream Needs cherry-pick or merge from upstream feat New feature or request labels Feb 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR performs a strict parity alignment pass against upstream/common-v8 for the local-first worker/DB orchestration flow, while preserving the fork’s Bun-first + Biome-first constraints and keeping fork-only protocol surface behind Experimental* aliases.

Changes:

  • Refactors SQLite + storage APIs to “throw-first” semantics (removing SqliteError from most result channels) and updates call sites/tests accordingly.
  • Introduces/aligns worker orchestration pieces (Shared.ts, worker console forwarding, typed NativeMessagePort generics) and expands worker-related test utilities/coverage.
  • Adjusts public exports to preserve upstream Db exports while moving DbWorkerProtocol surface behind Experimental* aliases; updates dependents and lock/config/docs.

Reviewed changes

Copilot reviewed 47 out of 48 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/web/test/Sqlite.test.ts Updates tests for new throw-first SQLite exec/export return shapes.
packages/web/test/DbWorker.test.ts Switches to ExperimentalDbWorker* protocol type aliases.
packages/web/src/local-first/Worker.worker.ts Updates web worker bootstrap to use createWorkerRun + injected runDbWorkerPort.
packages/web/src/local-first/DbWorker.ts Aligns web DB worker wiring with experimental protocol + console level plumb-through.
packages/web/src/Worker.ts Tightens NativeMessagePort generic typing in wrapper.
packages/react-native/src/shared.ts Requires createSqliteDriver dep for RN createEvoluDeps.
packages/react-native/src/exports/expo-sqlite.ts Wires Expo sqlite driver into deps creation.
packages/react-native/src/exports/bare-op-sqlite.ts Wires op-sqlite driver into deps creation.
packages/react-native/src/Worker.ts Tightens NativeMessagePort generic typing in RN worker shims.
packages/nodejs/test/Sqlite.test.ts Updates tests for new throw-first SQLite exec/export return shapes.
packages/nodejs/src/local-first/Relay.ts Updates relay startup/storage init for throw-first SQLite and getOk usage.
packages/nodejs/src/Worker.ts Tightens NativeMessagePort generic typing in wrapper.
packages/common/test/local-first/_utils.ts Updates schema + exec handling for throw-first SQLite results.
packages/common/test/local-first/Worker.test.ts Expands worker tests for console forwarding + updated InitTab/InitEvolu wiring.
packages/common/test/local-first/Storage.test.ts Updates storage tests for new return types and simplified storage config.
packages/common/test/local-first/Shared.test.ts Adds comprehensive tests for new initSharedWorker behavior.
packages/common/test/local-first/Schema.test.ts Updates schema tests for new return types (getDbSchema, ensureDbSchema).
packages/common/test/local-first/Relay.test.ts Updates relay storage tests for throw-first SQLite results and void-return APIs.
packages/common/test/local-first/Protocol.test.ts Updates protocol tests for console dep and non-nullable joins.
packages/common/test/local-first/Evolu.test.ts Adds mutation queue serialization test + updates InitEvolu channel payload.
packages/common/test/_deps.ts Consolidates common test deps helpers (sqlite + relay storage) into one module.
packages/common/test/_deps.nodejs.ts Keeps backward-compatible re-export shim to _deps.ts.
packages/common/test/Worker.test.ts Adds tests for new in-memory worker/message channel test utilities.
packages/common/test/Sqlite.test.ts Refactors transaction/export tests for throw-first semantics.
packages/common/test/Result.test.ts Adds tests for new getOk and getOrNull.
packages/common/src/local-first/index.ts Re-exports shared worker module and introduces ExperimentalDbWorker* aliases.
packages/common/src/local-first/Worker.ts Adds console level plumbing + console store forwarding to tabs.
packages/common/src/local-first/Sync.ts Refactors sync/storage flow to throw-first SQLite with explicit boundary handling.
packages/common/src/local-first/Storage.ts Converts many storage ops from Result/null to throw/void and adjusts API contracts.
packages/common/src/local-first/Shared.ts Adds platform-agnostic SharedWorker orchestration with mutation queue + retry/ack.
packages/common/src/local-first/Schema.ts Converts schema helpers to throw-first, improves typing for schema validation.
packages/common/src/local-first/Relay.ts Refactors relay sqlite storage to throw-first base storage and simplified error surface.
packages/common/src/local-first/Query.ts Refactors query loading to return values directly (no Result).
packages/common/src/local-first/Protocol.ts Adds console dep and guards around throw-capable storage operations.
packages/common/src/local-first/Evolu.ts Updates worker init payload (console level, ports) + mutation queue serialization.
packages/common/src/local-first/Error.ts Removes SqliteError from EvoluError union.
packages/common/src/local-first/Db.ts Adds upstream-parity DB worker definitions and bootstrap/init flow.
packages/common/src/Worker.ts Adds typed NativeMessagePort generics + in-memory worker/message test utilities.
packages/common/src/Type.ts Adds deprecated Name alias for upstream parity.
packages/common/src/Sqlite.ts Implements throw-first exec/export and overload-based transaction API.
packages/common/src/Result.ts Adds getOk helper and updates docs/examples accordingly.
packages/bun/test/BunDbWorker.test.ts Switches Bun DB worker tests to experimental protocol type aliases.
packages/bun/src/BunDbWorker.ts Switches Bun DB worker implementation to experimental protocol type aliases.
bun.lock Updates pinned tooling versions (Biome, Turbo, etc.).
biome.json Updates schema version and promotes noImportCycles rule configuration.
.changeset/sqlite-refactor.md Documents major SQLite API refactor and boundary handling rationale.
.ai/memory/user-preferences.md Updates parity sync rules documentation.
.ai/knowledge/common-v8-parity-plan-2026-02-16.md Adds parity tracking plan and status matrix snapshot.

Comment thread packages/common/src/Worker.ts
Comment thread packages/common/src/local-first/Shared.ts

Copilot AI commented Feb 16, 2026

Copy link
Copy Markdown

@miccy I've opened a new pull request, #40, to work on those changes. Once the pull request is ready, I'll request review from you.

miccy and others added 2 commits February 16, 2026 09:30
@miccy
miccy merged commit 5c0361b into main Feb 16, 2026
4 checks passed
@miccy
miccy deleted the miccy-dev branch February 22, 2026 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat New feature or request upstream Needs cherry-pick or merge from upstream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants