instant_record 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 235808a74ddecfa3ffeb6037903c827033b3ee17a9b575bb3d916ab459d462f2
|
|
4
|
+
data.tar.gz: aa6c571364d5459823d756a26da2de06cd7c6d62461bb75d1a84b8acf5f0f6c1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e4f6a5392bd539299a9669452bc5a7912a44499fc271708f1aa407a41204111e0b2f736cba1520ee0ec004b01b1f86cc70e34121bdc13aba2792112df2b6fa8d
|
|
7
|
+
data.tar.gz: 442f87b9448fab8167c15f94965dccdbd780e145e97ed3bf43cf79b8461cbe3d1fe6fd45016d81b81a54778fdb79a09457eee02113e33c49be137efbf8e16fd3
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,63 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0 (2026-11-23)
|
|
4
|
+
|
|
5
|
+
### Sync Engine, Conflict Resolution, and Local Data
|
|
6
|
+
|
|
7
|
+
- **String HLCs and 3-way column-level merging.** Replaced raw wall-clock LWW with server-anchored string Hybrid Logical Clocks (`InstantRecord::HLC`) and column-level 3-way merging so concurrent edits to different columns on the same record merge cleanly without clobbering.
|
|
8
|
+
- **GGPO dirty-row optimistic rebasing and bounded outbox draining.** Local dirty rows rebase optimistically on top of incoming server state, and outbox drains are bounded to prevent livelock under continuous local writes. Pure self-acks stay quiet on `records_changed`.
|
|
9
|
+
- **Declarative dual-runtime custom mutators (`sync_action`).** Define transactional custom mutators (`InstantRecord.mutate`) that run optimistically inside a local SQLite savepoint on the client and authoritatively in one transaction on the server.
|
|
10
|
+
- **Partitions, shapes, background history-fill, and client headers.** Promoted `sync_partition`, `sync_shape`, background history-fill, and custom client headers into the core engine. Added a `partition` column and index on `instant_record_changes` (run `bin/rails db:migrate`). Ranked `sync_window` within visible scope before windowing, and unified the durable SQLite history ledger so eviction invalidates stale history marks.
|
|
11
|
+
- **Conflict and rejection quarantine ledger (`InstantRecord::SyncNotice`).** Captures rejected or conflicted mutations in `instant_record_sync_notices` (run `bin/rails db:migrate`) with `InstantRecord.sync_notices`, `resolve_sync_notice`, and React helpers (`useSyncNotices`, `createSyncNoticesStore`, `diffNotice`).
|
|
12
|
+
- **Durable SQLite delay-line undo (`hold_for:`).** `InstantRecord.hold_for(seconds)` and `cancel_pending(mutation_id)` hold outbox mutations locally with pre-mutation snapshots before releasing them to sync (`release_at` and `rollback_payload` columns on `instant_record_outbox`; run `bin/rails db:migrate`), backed by `durableHeldActions` in `frontend/`.
|
|
13
|
+
- **Copy-on-write local SQLite staging branches (`InstantRecord.branch!`).** Fork the local SQLite database into an isolated offline copy for bulk edits and visual UI preview, then approve (replaying queued mutations onto `main`) or discard with zero server impact.
|
|
14
|
+
- **Record file attachments (`sync_file`).** Declare `sync_file max_size:` on a `Syncable` model so each record carries an immutable file addressed by `(type, id)`, with configurable proxy-safe chunk sizes and Native iOS `FileStore` + SQLite FTS5 support. The `frontend/` package (0.3.0) adds `fileStore()` and `useFile`, writing files to the browser's Origin Private File System (OPFS) first (online or offline), uploading in resumable chunks in the background, and fetching via `Range` requests on other devices. Authorization follows the record's own `sync_scope` / `syncable_to` / `writable_by` rules, and destroying a record deletes its server file. Files are stored under `storage/instant_record` (`config.file_store`, `config.max_file_size`), and `bin/rails instant_record:files:sweep` removes abandoned partial uploads. Keep model-level `max_size:` under 2 GiB (compiled 32-bit integers); larger limits belong in `config.max_file_size`.
|
|
15
|
+
- **Per-client scoping (`sync_scope`) and rule-based authorization (`syncable_to`, `writable_by`).** `sync_scope :column` scopes bootstrap, change stream, and history pages to `InstantRecord.config.scope_from` (plus `NULL` shared rows) and stamps creates server-side. `syncable_to` and `writable_by` accept server-only blocks returning ActiveRecord relations; changes to newly hidden rows stream out as empty destroys so revoked records leave browsers. Bootstrap returns an identity key; when the session identity changes, polls return a `reset` event that drops scoped rows and queued writes and re-bootstraps. Adds a nullable `scope` column to `instant_record_changes` (run `bin/rails db:migrate`). **Changed:** `POST /instant_record/mutations` now returns `415` for non-JSON requests, `GET /bootstrap` is `no-store`, and `InstantRecord.request_scope` is replaced by `InstantRecord::SyncScope.identity`.
|
|
16
|
+
- **Typed row-delta live bus.** Upgraded `instant-record:records-changed`, `ir_notice`, and `BroadcastChannel` to a typed row-delta bus with read tracking (`InstantRecord::ReadTracker`) and surgical `liveProps` patching (`affectsReads`, `applyRowDeltas`, `patchProp`).
|
|
17
|
+
- **Instrumentation (`ActiveSupport::Notifications`) and WASM trap telemetry.** `InstantRecord::Client` instruments `bootstrap`, `drain` (with rejection counts and reasons), `poll`, `stream`, `fetch_history`, `reset`, and `offline` as `<event>.instant_record`. Added WASM trap telemetry with a circuit breaker, Ruby notification bridge, and `.irtrace` flight recorder (with redaction of sensitive government/tax identifiers).
|
|
18
|
+
- **SSE idle heartbeats and leader-elected adaptive streaming.** `GET /events` emits `: hb` comments after `config.instant_record.sse_heartbeat_seconds` (default 15s) to keep reverse proxies from closing idle streams and detect dead peers cleanly without backtrace spam. Replaced per-tab polling in the browser worker with a leader-elected, visibility-aware SSE sync stream. Documented `config.instant_record.sse_window_seconds` sizing for Puma (25s default) vs Falcon (fiber-per-stream, e.g. 600s).
|
|
19
|
+
|
|
20
|
+
### Compiler, Browser Runtime, and Tooling
|
|
21
|
+
|
|
22
|
+
- **Pure-Ruby build pipeline with Prism AST lowering and static linter.** Replaced all Python compiler scripts with in-process Ruby classes under `InstantRecord::Compiler` (`WasmBuild`, `NativeBuild`, `GemVendor`, `EntryBuilder`, `WasmTree`, `BrowserMigrations`, `Toolchain`, `PathScrubber`, `SourcePages`, `Runner`), swappable `InstantRecord::Host` adapters, Prism-based AST lowering, a static AOT/32-bit linter, and content-addressed C object caching in `NativeBuild`. Added `bin/rails instant_record:doctor` (and `bundle exec rake compile:doctor`) to validate toolchain paths before building.
|
|
23
|
+
- **OPFS page-level WASI storage, transactional browser migrations, and self-healing.** Added OPFS page-level WASI storage with a leader-elected worker reactor, transactional browser migrations with schema lockfile replay, outbox upcasters, and automatic rehydration on unrecoverable local state.
|
|
24
|
+
- **Prebuilt SQLite shells (`InstantRecord::Shell`).** Extracted `InstantRecord::Shell` as a core engine primitive for browser and iOS cold-start hydration.
|
|
25
|
+
- **Incremental local-first islands.** Added an incremental local-first islands compilation and manifest pipeline.
|
|
26
|
+
- **Browser UX and compatibility.** Forwarded HTTP headers through the WASI bridge with a compiled Inertia responder; kept plain pages and submitted forms morphing in place while preserving focused inputs; loaded the compiled shell as a deferred classic script so Firefox accepts app import maps; and added Brotli-11 precompressed static assets plus Wave-1 Wasm/worker preload priority alongside `earlyBootstrap` and stylesheet/script prewarming.
|
|
27
|
+
- **Local MCP server, offline CLI, and parity/chaos testing.** Packaged the native host binary behind `exe/instant_record_mcp` as a local MCP stdio server and offline CLI. Added `bundle exec rake instant_record:verify_parity` and `instant_record:chaos` for headless dual-runtime parity and seeded multi-client chaos verification across CRuby, compiled `app.wasm`, and the Native C ABI.
|
|
28
|
+
- **Consumer-app compiler parity and onboarding.**
|
|
29
|
+
- `GemVendor` strips `server_only` and unwraps `browser_only` / `client_only` blocks across `app/models`, `app/controllers`, `app/helpers`, and `app/screens`.
|
|
30
|
+
- `EntryBuilder` and `instant_record:compile` / `instant_record:native` preserve every consumer model's declared `sync_window` and initialize `InstantRecord.config.endpoint` and `mount_path` at boot.
|
|
31
|
+
- `InstantRecord.server_url(path)` works in compiled consumer apps with default or custom `mount_path` and preserves existing query strings (`?` vs `&`).
|
|
32
|
+
- `compiled/web/src/worker.js` forwards any safe same-origin `instant_record_network=1` path for consumer apps (preserving request and response `Content-Type` and honoring custom `mount_path`), and `compiled/web/src/main.js` passes `/service-worker.js`, `/instant-record-release.json`, and same-origin sync/file paths straight to the network.
|
|
33
|
+
- `InstantRecord::CompiledSite` serves `/service-worker.js` with `cache-control: no-cache` when present in the published build.
|
|
34
|
+
- `bin/rails generate instant_record:install` generates both `config/instant_record_toolchain.env` and a commented `config/initializers/instant_record.rb` starter, with clearer next steps.
|
|
35
|
+
- `bin/rails instant_record:compile` checks toolchain presence up front and aborts cleanly on build errors; missing `WEB_NODE_MODULES` reports actionable setup instructions.
|
|
36
|
+
- Cleaned up `instant_record.gemspec` (zero RubyGems warnings, excluded demo overlays and JS tests, aligned gem version `0.3.0` with `frontend/package.json`).
|
|
37
|
+
|
|
38
|
+
### Inertia & React (`frontend/` 0.3.0)
|
|
39
|
+
|
|
40
|
+
- Added `InstantRecord::Inertia` (`render inertia: "Page", live: { mail: MailEntry }`) for both server and compiled runtimes.
|
|
41
|
+
- Expanded the `frontend/` npm package (`npm install "$(bundle show instant_record)/frontend"`) with `instantRecord()`, `localHttpClient`, `liveTransport`, `createLiveTransport`, `affectsReads`, `applyRowDeltas`, `patchProp`, `LiveList`, `useLiveWindow`, `useLiveParam`, `useLiveSearch`, `useHistory`, `useStableRows`, `useKeyboardNavigation`, `useShortcuts` (`chord`, `match`, `keycaps`), `useSelection`, `useUndoable`, `durableHeldActions`, `useSyncNotices`, `createSyncNoticesStore`, `write`, `fileStore`, and `useFile`.
|
|
42
|
+
|
|
43
|
+
### Native iOS & Inertia Native Contracts (Experimental)
|
|
44
|
+
|
|
45
|
+
- Added `InstantRecord::Native::Screen` DSL (`navigation`, `list`, `section`, `row`, `stack`, `spacer`, `text`, `date`, `image`, `button`, `swipe`, `toolbar`, `bottom`, `field`, `more`, `swift` custom views, and menu `link:` / `replace:` / `sheet:`), shipped RBS types (`sig/instant_record/native.rbs`), and added `client_only` as an alias for `browser_only`.
|
|
46
|
+
- Elevated `Screen#swift` into Inertia Native typed prop contracts with Prism-powered Swift and Kotlin codegen (`InstantRecordContracts.swift` / `InstantRecordContracts.kt`).
|
|
47
|
+
- Added `bin/rails "instant_record:native[iphonesimulator|iphoneos]"` to build static libraries, `bin/rails generate instant_record:ios Name --screen first` to scaffold a thin Xcode project (`InstantRecord::Native::XcodeProject` with `packages:`), and `config.instant_record.native_dev = true` for rebuild-free local screen iteration over HTTP.
|
|
48
|
+
- Added the `native/ios/InstantRecordNative` SwiftUI package (zero external dependencies) with off-main-thread sync, SSE event streaming, `FileStore`, `Theme` styling, and instant multi-database switching via `ir_open`.
|
|
49
|
+
|
|
50
|
+
### Demo
|
|
51
|
+
|
|
52
|
+
- Added the Auth demo (`/auth`) exercising real Rails sessions, `syncable_to` / `writable_by`, and identity reset; the Assistant demo (`/assistant`) with `sync_scope :visitor_id`, server RubyLLM replies, and on-device Chrome Prompt API replies; the Four Todos comparison (`/compare`) comparing plain Rails, Turbo Streams, Inertia optimistic updates, and InstantRecord side by side; and the Inertia React Inbox (`/email`) with background history fill, local + server-widened search, bulk selection, undoable archive, optional shell snapshot (`?shell=1`), and PWA app-shell service worker caching.
|
|
53
|
+
- Built the experimental iPhone Inbox app (`native/ios/Inbox`) with Cora theming, instant inbox switching, and a feedback sheet.
|
|
54
|
+
|
|
55
|
+
## 0.2.0
|
|
56
|
+
|
|
57
|
+
Roundhouse and Spinel now provide the gem’s sole browser runtime. The generated app runs against SQLite. The install generator writes the compiler configuration, and both `instant_record:compile` and `instant_record:build` build the compiled site.
|
|
58
|
+
|
|
59
|
+
For apps upgrading from the former ruby.wasm/PGlite backend, sync pending writes before switching. The compiled runtime bootstraps SQLite from Rails. It does not import an old PGlite database. Existing compiled SQLite profiles continue to use their saved rows and outbox.
|
|
60
|
+
|
|
3
61
|
## 0.1.0
|
|
4
62
|
|
|
5
63
|
First release. Real Active Record models running in the browser on ruby.wasm
|
data/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Rails models that run in the browser, sync to your server, and keep working offline.
|
|
4
4
|
|
|
5
|
+
**[Try the live demo →](https://instant-record.kieranklaassen.com)** — a Rails app compiled with Spinel, running against SQLite in your tab. Open Todo in two windows to see the same records, inspect local queries in `/console`, or visit `/receipts` to measure boot and query time on your device.
|
|
6
|
+
|
|
5
7
|
## The Problem
|
|
6
8
|
|
|
7
9
|
Apps like Linear feel instant because they never make you wait on the network: every read and write hits a **local** database, the UI renders from local data, and syncing with the server happens in the background. No spinners, no loading states, and offline works for free.
|
|
@@ -10,7 +12,7 @@ Building that today means a JavaScript sync engine, a client-side store, an API
|
|
|
10
12
|
|
|
11
13
|
InstantRecord is a bet that Rails developers can have local-first without leaving Ruby:
|
|
12
14
|
|
|
13
|
-
- **Instant** — `Issue.create!` commits to
|
|
15
|
+
- **Instant** — `Issue.create!` commits to SQLite in the browser and the UI re-renders immediately. The network is never in the hot path.
|
|
14
16
|
- **Durable** — every write records a mutation in the same local transaction, so nothing is lost to a crash, a reload, or a dead connection.
|
|
15
17
|
- **Convergent** — a background loop drains mutations to your Rails server (the source of truth) and streams everyone else's changes back over SSE.
|
|
16
18
|
- **Still just Rails** — the same `ApplicationRecord` model file runs on both sides. Validations, scopes, callbacks — no API client, no duplicate schema, no JavaScript data layer.
|
|
@@ -19,20 +21,20 @@ InstantRecord is a bet that Rails developers can have local-first without leavin
|
|
|
19
21
|
|
|
20
22
|
## The Two Runtimes
|
|
21
23
|
|
|
22
|
-
The same model file loads into two Ruby runtimes, each
|
|
24
|
+
The same model file loads into two Ruby runtimes, each with its own database:
|
|
23
25
|
|
|
24
26
|
| | In the browser | On the server |
|
|
25
27
|
|---|---|---|
|
|
26
|
-
| Ruby |
|
|
27
|
-
| Database |
|
|
28
|
+
| Ruby | Rails code compiled by Spinel to WebAssembly in a dedicated worker | CRuby + Puma |
|
|
29
|
+
| Database | SQLite, persisted to IndexedDB | PostgreSQL |
|
|
28
30
|
| Writes | **Optimistic** — instant local commit, queued for sync | **Authoritative** — validated, versioned, logged |
|
|
29
|
-
| Your controllers & views | Run here when the page is served by the
|
|
31
|
+
| Your controllers & views | Run here when the page is served by the dedicated worker | Run here at `localhost:3000` |
|
|
30
32
|
|
|
31
33
|
```mermaid
|
|
32
34
|
flowchart LR
|
|
33
35
|
subgraph tab [Your browser tab]
|
|
34
|
-
UI[Your views] --> VM["Your Rails app
|
|
35
|
-
VM -->
|
|
36
|
+
UI[Your views] --> VM["Your compiled Rails app"]
|
|
37
|
+
VM --> DB[(SQLite)]
|
|
36
38
|
end
|
|
37
39
|
VM -->|mutations up| S[Your Rails app on a server]
|
|
38
40
|
S --> P[(Postgres)]
|
|
@@ -48,7 +50,7 @@ flowchart LR
|
|
|
48
50
|
gem "instant_record"
|
|
49
51
|
```
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
The browser build uses the [Roundhouse and Spinel toolchain](compiled/README.md#install-the-toolchain).
|
|
52
54
|
|
|
53
55
|
### 2. Make a model syncable
|
|
54
56
|
|
|
@@ -62,7 +64,18 @@ class Issue < ApplicationRecord
|
|
|
62
64
|
end
|
|
63
65
|
```
|
|
64
66
|
|
|
65
|
-
Syncable models need three columns — a string/
|
|
67
|
+
Syncable models need three columns — a string/UUID primary key, `server_version`, and `sync_state`:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
create_table :issues, id: :string do |t|
|
|
71
|
+
t.string :title, null: false
|
|
72
|
+
t.integer :server_version, default: 0, null: false
|
|
73
|
+
t.string :sync_state, default: "synced", null: false
|
|
74
|
+
t.timestamps
|
|
75
|
+
end
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The gem's own tables (outbox, sync metadata, change log, idempotency ledger) install automatically as engine migrations — just run `bin/rails db:migrate`.
|
|
66
79
|
|
|
67
80
|
Rules that should only run on the server (authorization, quotas, anything the client shouldn't decide) stay in the same file, scoped with a block:
|
|
68
81
|
|
|
@@ -94,30 +107,25 @@ InstantRecord.sync(Issue, Todo)
|
|
|
94
107
|
|
|
95
108
|
### 3. Build the browser bundle
|
|
96
109
|
|
|
97
|
-
|
|
110
|
+
Generate the toolchain config and starter initializer, fill in the tool paths in `config/instant_record_toolchain.env`, verify them with `instant_record:doctor`, and compile:
|
|
98
111
|
|
|
99
112
|
```sh
|
|
100
113
|
bin/rails generate instant_record:install
|
|
101
|
-
bin/rails
|
|
114
|
+
bin/rails db:migrate
|
|
115
|
+
bin/rails instant_record:doctor
|
|
116
|
+
bin/rails instant_record:compile
|
|
102
117
|
```
|
|
103
118
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
```ruby
|
|
107
|
-
# config/application.rb
|
|
108
|
-
config.instant_record.build_on_precompile = true
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
(Off by default — the build needs the wasm toolchain and network access for ruby.wasm artifacts.)
|
|
119
|
+
Serve the generated site from `tmp/instant_record/compiled/web/dist`, or publish it directly from Rails with `config.instant_record.compiled_host` and `config.instant_record.build_on_precompile = :compiled`. See [building and deploying](compiled/README.md) for the toolchain and hosting settings.
|
|
112
120
|
|
|
113
121
|
### 4. Write your app like it's normal Rails
|
|
114
122
|
|
|
115
|
-
No special APIs in your controllers or views. When the page is served by the
|
|
123
|
+
No special APIs in your controllers or views. When the page is served by the dedicated worker, this code executes **in the browser**, reading and writing the local database:
|
|
116
124
|
|
|
117
125
|
```ruby
|
|
118
126
|
class IssuesController < ApplicationController
|
|
119
127
|
def index
|
|
120
|
-
@issues = Issue.order(created_at: :desc) # instant: local
|
|
128
|
+
@issues = Issue.order(created_at: :desc) # instant: local SQLite read
|
|
121
129
|
end
|
|
122
130
|
|
|
123
131
|
def create
|
|
@@ -145,14 +153,18 @@ Useful helpers:
|
|
|
145
153
|
- `InstantRecord.browser?` — the underlying runtime check, for anywhere else
|
|
146
154
|
- `InstantRecord::Client.applying_remote?` — whether the write running right now arrived from the server, rather than being made here
|
|
147
155
|
- `InstantRecord.server_url(path)` — the URL that reaches the authoritative server from either runtime, honouring a customised `mount_path`
|
|
156
|
+
- `sync_file` — a record that carries a file, kept in the browser and sent to the server in the background (below)
|
|
148
157
|
|
|
149
|
-
Convergence
|
|
158
|
+
Convergence uses server-anchored string-encoded Hybrid Logical Clocks (HLCs) and 3-way column-level merging via `base_version`: disjoint concurrent column edits merge cleanly, while same-column collisions honor declarative `sync_merge` policies (`:lww` default, `:union`, or `:server_wins`). A model can also observe remote values dropped on the client:
|
|
150
159
|
|
|
151
160
|
```ruby
|
|
152
161
|
class Issue < ApplicationRecord
|
|
153
162
|
include InstantRecord::Syncable
|
|
154
163
|
|
|
155
|
-
|
|
164
|
+
sync_merge :state, :server_wins
|
|
165
|
+
sync_merge :tags, :union
|
|
166
|
+
|
|
167
|
+
# A value from the server that HLC ordering threw away. The block runs with
|
|
156
168
|
# the losing attribute hash at the only moment it exists here — nothing is
|
|
157
169
|
# assigned, no callback fires, and the gem keeps no copy.
|
|
158
170
|
on_discarded_change { |attributes| Rails.logger.info("dropped #{attributes["title"].inspect}") }
|
|
@@ -193,23 +205,62 @@ end
|
|
|
193
205
|
**Where authorization actually lives:** browser-side checks are UX, never enforcement — the user owns the client and everything in it. Real enforcement has exactly two places, both on the server:
|
|
194
206
|
|
|
195
207
|
1. **The mutations endpoint** — server-side validations and `server_only` model rules reject writes the client wasn't allowed to make; the client rolls back.
|
|
196
|
-
2. **What you sync down** — a client can only render what the change stream delivered to it.
|
|
208
|
+
2. **What you sync down** — a client can only render what the change stream delivered to it. A model that declares `sync_scope`, or `syncable_to`, is sent only to the clients that may hold its rows (both below); every other model syncs to every client.
|
|
197
209
|
|
|
198
|
-
|
|
210
|
+
#### Rows that belong to one client: `sync_scope`
|
|
199
211
|
|
|
200
|
-
|
|
212
|
+
```ruby
|
|
213
|
+
class Note < ApplicationRecord
|
|
214
|
+
include InstantRecord::Syncable
|
|
215
|
+
sync_scope :user_id # the column that says whose row this is
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# config/initializers/instant_record.rb
|
|
219
|
+
InstantRecord.config.scope_from = ->(request) { request.session[:user_id] }
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`scope_from` reads a sync request's scope. With it and `sync_scope`, the sync endpoints keep a model's rows to their owner: the bootstrap, the change stream and history pages carry the rows whose column equals the request's scope, plus the rows where it is `NULL`, which belong to everyone. A create is stamped with the sender's scope whatever it claims, and a row of another scope cannot be changed or removed (the mutation is rejected, and the client rolls back). A request with no scope gets only the `NULL` rows and can write nothing to the model.
|
|
201
223
|
|
|
202
|
-
The
|
|
224
|
+
The default `scope_from` trusts a `scope` parameter, and the compiled site sends one: a random token it keeps in `localStorage`. That makes a browser its own scope with no sign-in, which is what the demo's Assistant uses. It is a capability, not authentication: anyone holding the token reads those rows. An app with accounts should read the scope from its session, as above.
|
|
225
|
+
|
|
226
|
+
Limits, as built: a row's scope is fixed at create, a scoped model with a `sync_window` ranks its window before the scope is applied, and the Ruby client (`InstantRecord::Client`, which the native build uses) sends no scope yet.
|
|
227
|
+
|
|
228
|
+
#### Rules a column cannot say: `syncable_to` and `writable_by`
|
|
203
229
|
|
|
204
230
|
```ruby
|
|
205
|
-
|
|
206
|
-
InstantRecord
|
|
207
|
-
|
|
208
|
-
|
|
231
|
+
class Note < ApplicationRecord
|
|
232
|
+
include InstantRecord::Syncable
|
|
233
|
+
|
|
234
|
+
server_only do
|
|
235
|
+
# Which notes the asker may be sent.
|
|
236
|
+
syncable_to { |user| user ? where(team_id: user.team_ids).or(where(public: true)) : where(public: true) }
|
|
237
|
+
# Which they may create, change and delete. Without it, what is seen may be written.
|
|
238
|
+
writable_by { |user| user ? where(author_id: user.id) : none }
|
|
239
|
+
end
|
|
209
240
|
end
|
|
241
|
+
|
|
242
|
+
# config/initializers/instant_record.rb
|
|
243
|
+
InstantRecord.config.scope_from = ->(request) { User.find_by(id: request.session[:user_id]) }
|
|
210
244
|
```
|
|
211
245
|
|
|
212
|
-
|
|
246
|
+
Each block runs on the model, on the server, with whatever `scope_from` returned, and returns a relation. They are the same engine as `sync_scope`, and a model may declare both: `scope_from` is the one place an app says who is asking, and its answer's `to_s` is what a `sync_scope` column is compared with.
|
|
247
|
+
|
|
248
|
+
- **Bootstrap and history** carry the rows in the relation.
|
|
249
|
+
- **The change stream** asks the rule again when a change is sent, since it can depend on anything. A row the asker may see is sent as it stands now. A row they may not see is sent as an empty destroy: nothing to a client that never held it, and a removal from one that did, which is how a row that stops being shared leaves a browser.
|
|
250
|
+
- **A write** must leave the row inside `writable_by`, checked after it is applied and rolled back if not (`not permitted`). A row the asker cannot see answers as a missing one does: an update is `record not found`, a destroy is accepted and does nothing, and no rejection carries its attributes.
|
|
251
|
+
|
|
252
|
+
**When the person behind a browser changes.** The bootstrap carries a key for whoever asked: a digest of the identity's `sync_identity_key` if it answers one, else of its class and id, else of the identity as a string, and nobody at all has a key too. The client sends it back with every poll and write. A request under another key is a browser that synced as someone else: its poll is answered with one `reset` event, the client drops every row of the scoped models along with the writes it had queued to them, and it bootstraps again as whoever is there now. Writes that arrive under the old key are refused (`identity changed`). Models every client may see are left alone, and the poll after the new bootstrap starts from the old cursor so they miss nothing. An identity may answer `sync_reset_types` to name the models its key decides, when the rest follow something that did not change. The key is a name, not a credential: the server scopes every request by its own session whatever key it carries.
|
|
253
|
+
|
|
254
|
+
A session is a cookie, so the page and the sync endpoints must be one origin for it to arrive: serve the compiled site from the Rails app (`config.instant_record.compiled_host`) or put both behind one proxy. The mutations endpoint takes JSON only, so a cross-site form cannot write through someone's cookie. The demo's Auth page (`/auth`) is this section running: three people, a real session, and a panel that asks the sync server for everything.
|
|
255
|
+
|
|
256
|
+
Limits, as built: record ids are not secret (a change to a hidden row reaches every client as an empty destroy naming its id, and a create that reuses a hidden row's id is refused); a validation that looks at other rows, such as uniqueness, answers a refused write with a message about rows the sender cannot see; a stream that is open when the session changes keeps its identity until it closes (a poll lasts at most the SSE window); a rule is one query per model per batch of changes; and a `syncable_to` model with a `sync_window` ranks its window before the rule is applied.
|
|
257
|
+
|
|
258
|
+
A `server_only` controller filter protects the server-rendered surface; it cannot and need not protect the browser's local rendering.
|
|
259
|
+
|
|
260
|
+
### 5. Sync just happens
|
|
261
|
+
|
|
262
|
+
The dedicated worker drains queued writes and polls the Rails changes endpoint.
|
|
263
|
+
To host the sync server on a different origin, set `SYNC_ENDPOINT` when compiling.
|
|
213
264
|
|
|
214
265
|
Behind the scenes:
|
|
215
266
|
|
|
@@ -246,60 +297,216 @@ InstantRecord.fetch_history(Message,
|
|
|
246
297
|
# => { ok: true, applied: 50, has_more: true } — or :busy while a sync pass runs
|
|
247
298
|
```
|
|
248
299
|
|
|
249
|
-
In the browser this serves repeat scrolls and offline scrollback from the local database and only hits `GET /instant_record/records` for pages it doesn't hold. Page JavaScript reaches it through
|
|
300
|
+
In the browser this serves repeat scrolls and offline scrollback from the local database and only hits `GET /instant_record/records` for pages it doesn't hold. Page JavaScript reaches it through the dedicated worker (`widenWindow` in the `frontend/` package, or directly via `window.__spinel.worker` — never from inside a local request, since the Wasm VM cannot re-enter itself):
|
|
250
301
|
|
|
251
302
|
```js
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
303
|
+
// With the gem's frontend/ package:
|
|
304
|
+
import { widenWindow } from "instant_record"
|
|
305
|
+
const reply = await widenWindow({
|
|
306
|
+
type: "Message",
|
|
307
|
+
partition: channelId,
|
|
308
|
+
before: { created_at: oldestAt, id: oldestId },
|
|
309
|
+
limit: 50,
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
// Or directly against the compiled worker:
|
|
313
|
+
const channel = new MessageChannel()
|
|
314
|
+
channel.port1.onmessage = (event) => { /* { ok, applied, has_more, error } */ }
|
|
315
|
+
window.__spinel.worker.postMessage(
|
|
316
|
+
{
|
|
317
|
+
type: "instant_record.fetch_history",
|
|
318
|
+
request: {
|
|
319
|
+
type: "Message",
|
|
320
|
+
partition: channelId,
|
|
321
|
+
before: { created_at: oldestAt, id: oldestId },
|
|
322
|
+
limit: 50,
|
|
323
|
+
},
|
|
324
|
+
},
|
|
258
325
|
[channel.port2],
|
|
259
|
-
)
|
|
326
|
+
)
|
|
260
327
|
```
|
|
261
328
|
|
|
262
329
|
**Render the top of the scrollback server-side.** A windowed view needs a slot above its oldest row — a scroll-up sentinel, or a "beginning of history" marker once there's nothing left. Render that slot in the initial HTML even when you don't yet know which it is, and let JavaScript fill it in place. Injecting it after a probe shifts every row below it by its height on each load.
|
|
263
330
|
|
|
264
331
|
The Slack demo's infinite scroll is the reference wiring — IntersectionObserver sentinel, scroll anchoring, in-place DOM updates — in the Stimulus controller `demo/app/javascript/controllers/conversation_controller.js`.
|
|
265
332
|
|
|
333
|
+
### 7. Carry a file with a record
|
|
334
|
+
|
|
335
|
+
Some things a record is about are too large for a column: a recording, a render, a scan. A model says its records carry one, and that is the whole opt-in:
|
|
336
|
+
|
|
337
|
+
```ruby
|
|
338
|
+
class Recording < ApplicationRecord
|
|
339
|
+
include InstantRecord::Syncable
|
|
340
|
+
sync_file max_size: 500 * 1024 * 1024 # the most bytes the server takes for one
|
|
341
|
+
end
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
The model's file is compiled for the browser, where an integer has 32 bits: a `max_size` there stays under two gigabytes, and a larger limit goes in `config.max_file_size`, which only the server reads.
|
|
345
|
+
|
|
346
|
+
The record is still what syncs, and nothing about that changes. The file is known by the record's type and id, in the browser and on the server, and it never changes: new bytes are a new record. Where the model has the columns an Active Storage blob has, they are used: the server serves the file as its `content_type` and refuses an upload that is not `byte_size` bytes. None is required.
|
|
347
|
+
|
|
348
|
+
The bytes are never in the local database and never pass through the compiled app. Install the gem's `frontend/` package (`npm install "$(bundle show instant_record)/frontend"`) to keep them in the origin's private file system (OPFS), from the page:
|
|
349
|
+
|
|
350
|
+
```js
|
|
351
|
+
import { fileStore, useFile } from "instant_record"
|
|
352
|
+
|
|
353
|
+
const files = fileStore() // one for the page; beside the compiled runtime it knows the sync server and the browser's scope
|
|
354
|
+
|
|
355
|
+
// Written to this browser first, with or without a connection.
|
|
356
|
+
const id = crypto.randomUUID()
|
|
357
|
+
const about = await files.attach("Recording", id, blob) // { filename, content_type, byte_size }
|
|
358
|
+
router.post("/recordings", { recording: { id, title, ...about } }) // the record, as any other
|
|
359
|
+
|
|
360
|
+
// Here, or fetched from the server and kept.
|
|
361
|
+
const file = await files.open("Recording", id)
|
|
362
|
+
|
|
363
|
+
// In React: status is loading, ready, missing, offline or error.
|
|
364
|
+
const { url, status, progress } = useFile(files, "Recording", recording.id)
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
- **Local first.** `attach` returns when the file is on this device. It is there after a reload and with no connection, and `files.get` reads it without asking the server anything.
|
|
368
|
+
- **Sent in the background.** The file goes to the server in configurable pieces (512 KB by default, safely below common 1–2 MB reverse-proxy ceilings, and automatically capped to whatever `chunk_size` the server advertises), oldest first, one tab at a time. An upload that is cut off goes on from the piece it stopped at, after a reload too. A file whose record has not reached the server yet waits for it: the server answers 404 until the record's create has synced.
|
|
369
|
+
- **Fetched when asked for.** On another device `open` fetches the file a piece at a time and keeps it, and a fetch that is cut off keeps its pieces. A file the server does not hold (its upload has not finished where it was made) is `FileMissing`.
|
|
370
|
+
- **The record's rules are the file's.** Whoever may be sent the record (`sync_scope`, `syncable_to`) may read its bytes, and whoever may write it (`writable_by`) may send them. Anyone else is answered as for a file that does not exist. A model with no rule shares its files as it shares its rows.
|
|
371
|
+
- **A destroyed record takes its file** from the server. In a browser, `files.prune("Recording", ids)` drops the files of records that are gone, given the ids that are left, and `files.clear()` drops everything, for a sign-out. `fileStore({ name: userId })` keeps the files of two accounts in one browser apart.
|
|
372
|
+
|
|
373
|
+
**Room.** A browser gives an origin a share of the disk and may clear all of it when the disk fills. `attach` asks it not to (`navigator.storage.persist()`; `files.usage()` reports whether it agreed, with how much is held and how much is waiting to be sent). Past `fileStore({ budget: bytes })`, near the browser's own limit, or when a write fails for lack of room, the store drops files the server holds, least recently used first: those can be fetched again. A file the server does not hold yet is never dropped, and neither is one marked with `files.pin`. When nothing more may go, `attach` fails with a `QuotaExceededError` and leaves nothing behind, and `open` reads the file from the server without keeping it. Chromium reports a quota that is not the limit it enforces, so there the write that fails is what makes room.
|
|
374
|
+
|
|
375
|
+
**On the server** the files are kept under `storage/instant_record`, one per record, and served with ranges:
|
|
376
|
+
|
|
377
|
+
```ruby
|
|
378
|
+
# config/initializers/instant_record.rb
|
|
379
|
+
InstantRecord.configure do |config|
|
|
380
|
+
config.file_store = InstantRecord::FileStore.new("/var/lib/my_app/files") # or anything that answers what it does
|
|
381
|
+
config.max_file_size = 1024 * 1024 * 1024 # for models that name no max_size (two gigabytes when left alone)
|
|
382
|
+
config.file_chunk_size = 512 * 1024 # max bytes per upload piece (512 KB when left alone)
|
|
383
|
+
end
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
`GET <mount>/files/:type/:id` is the file (with `Range` and `X-Instant-Record-Chunk-Size`), `GET .../upload` says where an upload stands (and advertises `chunk_size`), and `PUT` takes one piece. A file is served as a download that may run nothing, whatever type its record claims. `bin/rails instant_record:files:sweep` removes uploads that were given up.
|
|
387
|
+
|
|
388
|
+
On Native iOS, `InstantRecordNative` provides a matching disk-backed chunked `FileStore` actor with resumable uploads/downloads, server `chunk_size` negotiation, and LRU cache eviction.
|
|
389
|
+
|
|
390
|
+
Limits, as built: one file to a record; no checksum, so a file is trusted to be what its size and its pieces' order say; a browser must be able to write its private file system from a page (`createWritable`, which Chromium and Firefox have and Safari names from 26), and without it `open` reads from the server and keeps nothing; a file that waits for a record that never arrives is asked about once a minute while the page is open, until it is pruned; the Ruby `InstantRecord::Client` has no file store. What has run: headless Chromium against a Rails server, in `test/browser/files.mjs` (`node test/browser/files.mjs --big` adds a 200 MB file), once beside a compiled build of the demo with a file on a todo, and `swift test` in `native/ios/InstantRecordNative`.
|
|
391
|
+
|
|
392
|
+
### 8. Using with Inertia & React
|
|
393
|
+
|
|
394
|
+
If your app uses `inertia_rails`, pass `live:` to `render inertia:` to bind props to synced models:
|
|
395
|
+
|
|
396
|
+
```ruby
|
|
397
|
+
render inertia: "Email/Inbox",
|
|
398
|
+
live: { mail: MailEntry, thread: MailEntry },
|
|
399
|
+
props: { mail: -> { ... }, thread: -> { ... } }
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
Install the gem's `frontend/` package (`npm install "$(bundle show instant_record)/frontend"`) and spread `instantRecord()` into `createInertiaApp`:
|
|
403
|
+
|
|
404
|
+
```js
|
|
405
|
+
import { createInertiaApp } from "@inertiajs/react"
|
|
406
|
+
import { instantRecord } from "instant_record"
|
|
407
|
+
|
|
408
|
+
createInertiaApp({ ...instantRecord(), resolve, setup })
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
In the compiled runtime, Inertia visits execute against the local SQLite database and reload only the props whose synced models changed. The package also exports `LiveList`, `useLiveWindow`, `useLiveParam`, `useLiveSearch`, `useHistory`, `useStableRows`, `useKeyboardNavigation`, `useShortcuts`, `useSelection`, `useUndoable`, and `write`.
|
|
412
|
+
|
|
413
|
+
### 9. Native iOS screens (experimental)
|
|
414
|
+
|
|
415
|
+
Write iOS screens in Ruby under `app/screens` subclassing `InstantRecord::Native::Screen`, compile your app's models and screens to static libraries, and generate a thin SwiftUI Xcode project:
|
|
416
|
+
|
|
417
|
+
```sh
|
|
418
|
+
bin/rails generate instant_record:ios Shop --screen catalog
|
|
419
|
+
bin/rails "instant_record:native[iphonesimulator]"
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
Enable `config.instant_record.native_dev = true` in development to iterate on Ruby screens over HTTP without rebuilding the native library. See [native/ios/README.md](native/ios/README.md) for the screen DSL, custom SwiftUI views, theming, and simulator tests.
|
|
423
|
+
|
|
424
|
+
## Compile a Rails app for the browser
|
|
425
|
+
|
|
426
|
+
The gem includes an experimental ahead-of-time compiler. Roundhouse and Spinel
|
|
427
|
+
compile the app’s routes, controllers, views, and syncable models into a WebAssembly
|
|
428
|
+
reactor. A dedicated worker runs it against local SQLite and syncs with your Rails server.
|
|
429
|
+
|
|
430
|
+
After installing the toolchain and filling in `config/instant_record_toolchain.env`, run:
|
|
431
|
+
|
|
432
|
+
```sh
|
|
433
|
+
bin/rails db:migrate
|
|
434
|
+
bin/rails instant_record:doctor
|
|
435
|
+
bin/rails instant_record:compile
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
The static site is written to `tmp/instant_record/compiled/web/dist`. The default
|
|
439
|
+
sync endpoint is `/instant_record`. Use `SYNC_ENDPOINT` for a separately hosted
|
|
440
|
+
Rails server. The compiled demo includes Todo, Chat, Shared inbox, Ship v2, local database
|
|
441
|
+
inspection and Receipts. [Toolchain setup and supported features](compiled/README.md).
|
|
442
|
+
|
|
443
|
+
To ship the optimized browser app with a Rails deploy, set
|
|
444
|
+
`config.instant_record.build_on_precompile = :compiled` and configure its browser
|
|
445
|
+
hostname. Rails asset precompilation publishes the build. Open tabs detect the
|
|
446
|
+
release, and returning profiles migrate their local database before rendering.
|
|
447
|
+
[Deployment and browser migration setup](compiled/README.md#ship-on-a-rails-deploy).
|
|
448
|
+
|
|
449
|
+
## Local MCP server and offline CLI
|
|
450
|
+
|
|
451
|
+
`bin/rails instant_record:native` compiles your Rails app (models, validations, screens, and SQLite) into a screenless native `host` binary. `bin/rails instant_record:mcp` packages that binary as a local MCP JSON-RPC 2.0 stdio server and offline CLI:
|
|
452
|
+
|
|
453
|
+
```sh
|
|
454
|
+
# Build the native host once (or pass --build / INSTANT_RECORD_BUILD=1)
|
|
455
|
+
bin/rails instant_record:native
|
|
456
|
+
|
|
457
|
+
# Start the drop-in MCP stdio server against a scoped local SQLite replica
|
|
458
|
+
INSTANT_RECORD_ENDPOINT=http://127.0.0.1:3000/instant_record \
|
|
459
|
+
INSTANT_RECORD_TOKEN="$MY_TOKEN" \
|
|
460
|
+
INSTANT_RECORD_SCOPE="visitor-1" \
|
|
461
|
+
bin/rails instant_record:mcp
|
|
462
|
+
|
|
463
|
+
# Or use the offline CLI subcommands (pass CLI arguments after `--`)
|
|
464
|
+
bin/rails instant_record:mcp -- sync
|
|
465
|
+
bin/rails instant_record:mcp -- --offline query Issue --where state=open
|
|
466
|
+
bin/rails instant_record:mcp -- --offline search "invoice"
|
|
467
|
+
bin/rails instant_record:mcp -- --offline action mailbox archive --param member=maya --param inbox=support --arg id=<id>
|
|
468
|
+
bin/rails instant_record:mcp -- --offline request POST /todo/issues --data 'issue[title]=Offline task'
|
|
469
|
+
|
|
470
|
+
# Standalone executable (skips Rails boot once the native host is compiled)
|
|
471
|
+
bundle exec instant_record_mcp serve \
|
|
472
|
+
--host tmp/instant_record/compiled/.build-native/host \
|
|
473
|
+
--db tmp/instant_record/mcp/local.sqlite3 \
|
|
474
|
+
--endpoint http://127.0.0.1:3000/instant_record \
|
|
475
|
+
--cookie "_session=..."
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
Exposed MCP tools (`<1ms` offline against local SQLite + persistent compiled `host`): `instant_record_status`, `instant_record_sync`, `instant_record_models`, `instant_record_query`, `instant_record_get`, `instant_record_search`, `instant_record_outbox`, `instant_record_render_screen`, `instant_record_action`, `instant_record_request`, and `instant_record_history`.
|
|
479
|
+
|
|
266
480
|
## Running the demo
|
|
267
481
|
|
|
268
|
-
|
|
482
|
+
Install Ruby 3.3.3, PostgreSQL and the [compiler toolchain](compiled/README.md#install-the-toolchain).
|
|
483
|
+
Copy `compiled/toolchain.env.example` to `compiled/toolchain.env` and fill in the tool paths.
|
|
269
484
|
|
|
270
485
|
```sh
|
|
271
|
-
# Gem tests
|
|
272
486
|
bundle install && bundle exec rake test
|
|
273
|
-
|
|
274
|
-
# One-time setup
|
|
275
487
|
cd demo
|
|
276
488
|
bundle install
|
|
277
489
|
bin/rails db:prepare
|
|
278
490
|
bin/rails test
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
# One-time browser build (~5 min)
|
|
282
|
-
bin/rails instant_record:build # writes pwa/public/app.wasm (~62 MB)
|
|
283
|
-
|
|
284
|
-
# Run it — sync server on :3000, PWA on :5173
|
|
491
|
+
SYNC_ENDPOINT=http://localhost:3000/instant_record ruby ../compiled/pipeline/build.rb
|
|
285
492
|
bin/dev
|
|
286
493
|
```
|
|
287
494
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
Open http://localhost:5173/boot.html, wait for **Service Worker Ready**, then open http://localhost:5173/ — that page is rendered by Rails in your browser.
|
|
495
|
+
Open http://localhost:5173/ for the compiled demo. Rails serves sync on port 3000.
|
|
291
496
|
|
|
292
497
|
Things to try:
|
|
293
498
|
|
|
294
499
|
- **Instant writes** — create an issue; it appears immediately with a `pending` badge that flips to `synced`.
|
|
295
|
-
- **Two clients** — open http://127.0.0.1:5173/ too (different origin = separate
|
|
500
|
+
- **Two clients** — open http://127.0.0.1:5173/ too (different origin = separate local database). Changes propagate between windows through the server. A fresh client catches up automatically.
|
|
501
|
+
- **Shared inbox** — Open `/email` as Maya, who shares Support with Theo and Billing with Jo. Reply offline in Support and reconnect to see it arrive for Theo. Each persona and inbox has a separate SQLite snapshot, outbox and sync cursor. The public persona picker is for fictional mail.
|
|
296
502
|
- **Infinite scroll** — `/slack` seeds thousands of messages in `#general`, but a fresh client only syncs the newest 50 per conversation. Scroll to the top and older pages stream in; messages arriving mid-read update the page without a reload or a scroll jump.
|
|
297
503
|
- **Offline** — stop the Rails server, create issues (they stay `pending`), reload the tab (still there), restart the server and watch them drain.
|
|
504
|
+
- **Sessions and permissions** — open `/auth`, sign in as Ada, Ben or Cleo, and watch which notes this browser holds. Sign out and they leave it. "Ask the sync server for everything" shows what a browser with that cookie can get by asking directly.
|
|
298
505
|
- **Rejection** — create an issue titled `reject me`. It appears optimistically, the server refuses it, and it disappears on reconcile.
|
|
299
506
|
|
|
300
507
|
## Scaling the SSE stream
|
|
301
508
|
|
|
302
|
-
|
|
509
|
+
The compiled demo checks for new changes once a second using short HTTP requests. Clients that choose to hold an SSE stream open need a server sized for their concurrent streams. The engine's stream endpoint is a plain Rack streaming body (no `ActionController::Live`, no thread per stream) that releases its database connection between polls, so it runs well on both servers; the ceiling is the server's concurrency model. Measured on the demo (M-series MacBook, dev mode):
|
|
303
510
|
|
|
304
511
|
| Server | Concurrent SSE clients | Result |
|
|
305
512
|
|---|---|---|
|
|
@@ -325,21 +532,20 @@ config.active_support.isolation_level = :fiber if defined?(Falcon)
|
|
|
325
532
|
bundle exec falcon serve --bind http://localhost:3000
|
|
326
533
|
```
|
|
327
534
|
|
|
328
|
-
|
|
535
|
+
Clients that hold an open SSE stream (such as the native iOS `StreamClient` or custom `EventSource` consumers) receive changes as they are logged — measured at **130ms** from a server-side write, bounded by the server's 0.5s change-log poll. Reopening with `?after=<cursor>` (or `Last-Event-ID`) replays anything a dropped connection missed. The tail window is configurable via `config.instant_record.sse_window_seconds` (25s default for Puma; raise under Falcon) and `config.instant_record.sse_heartbeat_seconds` (15s default) emits `: hb` keepalives so proxies do not reap idle streams.
|
|
329
536
|
|
|
330
|
-
|
|
537
|
+
By contrast, the compiled browser worker uses non-blocking `window=0` catch-up polls every second (returning immediately without holding a server thread, and skipping Wasm completely when empty), while draining the outbox on local writes, at boot, and on reconnect with exponential backoff. Reproduce the SSE load numbers with `demo/script/sse_load_spike.rb`.
|
|
331
538
|
|
|
332
539
|
## What's proven
|
|
333
540
|
|
|
334
541
|
Measured on the demo (Chrome, M-series MacBook):
|
|
335
542
|
|
|
336
|
-
-
|
|
337
|
-
- Warm boot — VM init plus PGlite schema prepare — in **~1.8 seconds**; the `app.wasm` module is **61.9 MB** raw, **17.1 MB** gzipped over the wire.
|
|
543
|
+
- The browser compiles and loads the Spinel WebAssembly module, opens SQLite and handles requests locally. Visit `/receipts` for measurements on your own device.
|
|
338
544
|
- Instant optimistic create with local persistence across reloads.
|
|
339
545
|
- Two independent clients converging through POST + SSE, including fresh-client catch-up.
|
|
340
546
|
- Server rejection reconciled by rolling the local record back.
|
|
341
547
|
- ~5,000 seeded messages in one channel: a fresh client bootstraps only the newest window per conversation, older pages stream in on scroll, and a cold boot evicts the local database back to the window.
|
|
342
|
-
-
|
|
548
|
+
- Browser tests exercise multi-tab offline writes, release upgrades, local migrations, retries and server reconciliation.
|
|
343
549
|
|
|
344
550
|
## Roadmap ideas
|
|
345
551
|
|
|
@@ -353,7 +559,7 @@ class Issue < ApplicationRecord
|
|
|
353
559
|
end
|
|
354
560
|
```
|
|
355
561
|
|
|
356
|
-
Also out of scope for the PoC, on purpose: CRDTs, Postgres logical replication, multi-tab leader election,
|
|
562
|
+
Also out of scope for the PoC, on purpose: CRDTs, Postgres logical replication, multi-tab leader election, several files to a record or a file that changes (a record carries one file: `sync_file`), a sign-in of the gem's own (it scopes the sync endpoints by whatever your app's session says: `sync_scope`, `syncable_to`), and migration generators. The Inbox demo runs as an Inertia page in React, with its props kept live from the local database: `docs/solutions/2026-11-19-inbox-on-inertia.md` says what held and what the gem would need to offer it properly.
|
|
357
563
|
|
|
358
564
|
## History
|
|
359
565
|
|