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
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "time"
|
|
3
|
+
|
|
4
|
+
module InstantRecord
|
|
5
|
+
module Client
|
|
6
|
+
# GGPO-style dirty-row state manager.
|
|
7
|
+
#
|
|
8
|
+
# Persists a lightweight server-confirmed snapshot in SyncMetadata STRICTLY
|
|
9
|
+
# for rows that currently have unacked outbox mutations. Clean rows carry
|
|
10
|
+
# zero snapshot rows. When server truth arrives (via SSE/poll delta, ack,
|
|
11
|
+
# LWW skip, 3-way column merge, rejection, or poison-pill quarantine), a
|
|
12
|
+
# dirty row is rewound in memory to its confirmed server state, updated with
|
|
13
|
+
# server truth, and deterministically replayed through its remaining unacked
|
|
14
|
+
# mutations (or custom replay mutators). Once a row's last outbox mutation
|
|
15
|
+
# clears, its confirmed snapshot is immediately deleted.
|
|
16
|
+
module Rebaser
|
|
17
|
+
CONFIRMED_PREFIX = "confirmed_row:".freeze
|
|
18
|
+
SKIP_REPLAY_COLUMNS = %w[id server_version sync_state created_at updated_at].freeze
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
def snapshot_key(record_type, record_id)
|
|
22
|
+
"#{CONFIRMED_PREFIX}#{record_type}:#{record_id}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def confirmed_snapshot(record_type, record_id)
|
|
26
|
+
raw = SyncMetadata.get(snapshot_key(record_type, record_id))
|
|
27
|
+
return nil if raw.nil? || raw.empty?
|
|
28
|
+
|
|
29
|
+
JSON.parse(raw)
|
|
30
|
+
rescue JSON::ParserError
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def write_confirmed_snapshot(record_type, record_id, snapshot)
|
|
35
|
+
SyncMetadata.set(snapshot_key(record_type, record_id), JSON.generate(snapshot))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def clear_confirmed_snapshot(record_type, record_id)
|
|
39
|
+
SyncMetadata.delete(snapshot_key(record_type, record_id))
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def confirmed_snapshot_count
|
|
43
|
+
count = 0
|
|
44
|
+
SyncMetadata.all.each do |entry|
|
|
45
|
+
count += 1 if entry.key.start_with?(CONFIRMED_PREFIX)
|
|
46
|
+
end
|
|
47
|
+
count
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def pending_for(record_type, record_id)
|
|
51
|
+
OutboxMutation.where(record_type: record_type, record_id: record_id).ordered.to_a
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def dirty?(record_type, record_id)
|
|
55
|
+
OutboxMutation.where(record_type: record_type, record_id: record_id).limit(1).to_a.any?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Capture the server-confirmed row state right before a clean row
|
|
59
|
+
# transitions to dirty. Called inside the same transaction as the local
|
|
60
|
+
# record write and outbox insert, so rollbacks undo it atomically.
|
|
61
|
+
# +creating+ is positional here and in Syncable's callbacks: as a keyword on a
|
|
62
|
+
# concern's method, the browser's compiler types it as a Hash and every local
|
|
63
|
+
# write raises TypeError.
|
|
64
|
+
def capture_before_local_write(model, record_id, creating)
|
|
65
|
+
record_type = model.name
|
|
66
|
+
return if dirty?(record_type, record_id) && confirmed_snapshot(record_type, record_id)
|
|
67
|
+
|
|
68
|
+
snapshot =
|
|
69
|
+
if creating
|
|
70
|
+
{ "exists" => false, "version" => 0, "hlc" => nil, "attributes" => nil, "initial_attributes" => nil }
|
|
71
|
+
else
|
|
72
|
+
existing = model.find_by(id: record_id)
|
|
73
|
+
if existing
|
|
74
|
+
wire = InstantRecord.wire_attributes(existing.attributes)
|
|
75
|
+
{
|
|
76
|
+
"exists" => true,
|
|
77
|
+
"version" => existing[:server_version] || 0,
|
|
78
|
+
"hlc" => HLC.row_hlc(record_type, record_id),
|
|
79
|
+
"attributes" => wire,
|
|
80
|
+
"initial_attributes" => wire
|
|
81
|
+
}
|
|
82
|
+
else
|
|
83
|
+
{ "exists" => false, "version" => 0, "hlc" => nil, "attributes" => nil, "initial_attributes" => nil }
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
write_confirmed_snapshot(record_type, record_id, snapshot)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Reconcile one resolved outbox mutation against its row without
|
|
91
|
+
# clobbering newer unacked local edits on the same row.
|
|
92
|
+
def reconcile_mutation_result(mutation, result, record_delta: false)
|
|
93
|
+
record_type = mutation.record_type
|
|
94
|
+
record_id = mutation.record_id
|
|
95
|
+
model = InstantRecord.synced_model(record_type)
|
|
96
|
+
snapshot = confirmed_snapshot(record_type, record_id)
|
|
97
|
+
|
|
98
|
+
HLC.receive(result["hlc"]) if HLC.valid?(result["hlc"])
|
|
99
|
+
authoritative = result["skipped"] || result["merged"] || result["status"] == "rejected" || result["server_attributes"].present?
|
|
100
|
+
|
|
101
|
+
updated_snapshot =
|
|
102
|
+
case result["status"]
|
|
103
|
+
when "applied"
|
|
104
|
+
if authoritative
|
|
105
|
+
authoritative_snapshot(snapshot, result)
|
|
106
|
+
else
|
|
107
|
+
advance_confirmed_snapshot(snapshot, mutation, result["version"], model, hlc: result["hlc"])
|
|
108
|
+
end
|
|
109
|
+
when "rejected"
|
|
110
|
+
authoritative_snapshot(snapshot, result, fallback_missing: true)
|
|
111
|
+
else
|
|
112
|
+
snapshot
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
mutation.destroy!
|
|
116
|
+
remaining = pending_for(record_type, record_id)
|
|
117
|
+
|
|
118
|
+
if remaining.empty?
|
|
119
|
+
clear_confirmed_snapshot(record_type, record_id)
|
|
120
|
+
return false unless model
|
|
121
|
+
|
|
122
|
+
if result["status"] == "applied" && !authoritative
|
|
123
|
+
if mutation.operation == "destroy"
|
|
124
|
+
HLC.delete_row_hlc(record_type, record_id)
|
|
125
|
+
else
|
|
126
|
+
record = model.find_by(id: record_id)
|
|
127
|
+
record&.update_columns(sync_state: "synced", server_version: result["version"] || record.server_version)
|
|
128
|
+
HLC.set_row_hlc(record_type, record_id, result["hlc"]) if HLC.valid?(result["hlc"])
|
|
129
|
+
end
|
|
130
|
+
false
|
|
131
|
+
else
|
|
132
|
+
return false unless updated_snapshot
|
|
133
|
+
|
|
134
|
+
!!materialize_state(model, record_id, updated_snapshot, remaining: [], force_hlc: true, record_delta: record_delta)
|
|
135
|
+
end
|
|
136
|
+
else
|
|
137
|
+
write_confirmed_snapshot(record_type, record_id, updated_snapshot) if updated_snapshot
|
|
138
|
+
return false unless model && updated_snapshot
|
|
139
|
+
|
|
140
|
+
rebased = replay_onto_confirmed(model, record_id, updated_snapshot, remaining)
|
|
141
|
+
!!materialize_state(model, record_id, rebased, remaining: remaining, force_hlc: authoritative, record_delta: record_delta)
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Reconcile a poison-pilled or cancelled mutation removed from the
|
|
146
|
+
# outbox. Rewinds to the last confirmed snapshot and replays any
|
|
147
|
+
# remaining unacked mutations on that row. Returns whether visible
|
|
148
|
+
# local record data changed.
|
|
149
|
+
def reconcile_quarantined_mutation(mutation, record_delta: false)
|
|
150
|
+
record_type = mutation.record_type
|
|
151
|
+
record_id = mutation.record_id
|
|
152
|
+
model = InstantRecord.synced_model(record_type)
|
|
153
|
+
snapshot = confirmed_snapshot(record_type, record_id)
|
|
154
|
+
|
|
155
|
+
mutation.destroy!
|
|
156
|
+
remaining = pending_for(record_type, record_id)
|
|
157
|
+
|
|
158
|
+
if remaining.empty?
|
|
159
|
+
clear_confirmed_snapshot(record_type, record_id)
|
|
160
|
+
return false unless model && snapshot
|
|
161
|
+
|
|
162
|
+
!!materialize_state(model, record_id, snapshot, remaining: [], force_hlc: true, record_delta: record_delta)
|
|
163
|
+
else
|
|
164
|
+
return false unless model && snapshot
|
|
165
|
+
|
|
166
|
+
rebased = replay_onto_confirmed(model, record_id, snapshot, remaining)
|
|
167
|
+
!!materialize_state(model, record_id, rebased, remaining: remaining, force_hlc: true, record_delta: record_delta)
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Apply an incoming server delta (from SSE, poll, bootstrap, or
|
|
172
|
+
# history) to a currently dirty row: update its confirmed snapshot with
|
|
173
|
+
# server truth, rewind in memory, and deterministically replay all
|
|
174
|
+
# remaining unacked local mutations. Returns whether the visible local
|
|
175
|
+
# row changed.
|
|
176
|
+
def apply_server_delta(model, record_id, operation:, attributes:, version:, hlc: nil, record_delta: false)
|
|
177
|
+
record_type = model.name
|
|
178
|
+
HLC.receive(hlc) if HLC.valid?(hlc)
|
|
179
|
+
incoming_hlc = HLC.valid?(hlc) ? hlc : nil
|
|
180
|
+
|
|
181
|
+
remaining = pending_for(record_type, record_id)
|
|
182
|
+
snapshot = confirmed_snapshot(record_type, record_id) ||
|
|
183
|
+
{ "exists" => false, "version" => 0, "hlc" => nil, "attributes" => nil, "initial_attributes" => nil }
|
|
184
|
+
|
|
185
|
+
if operation == "destroy"
|
|
186
|
+
updated_snapshot = {
|
|
187
|
+
"exists" => false,
|
|
188
|
+
"version" => 0,
|
|
189
|
+
"hlc" => nil,
|
|
190
|
+
"attributes" => nil,
|
|
191
|
+
"initial_attributes" => snapshot["initial_attributes"]
|
|
192
|
+
}
|
|
193
|
+
write_confirmed_snapshot(record_type, record_id, updated_snapshot)
|
|
194
|
+
rebased = replay_onto_confirmed(model, record_id, updated_snapshot, remaining)
|
|
195
|
+
return materialize_state(model, record_id, rebased, remaining: remaining, force_hlc: true, record_delta: record_delta)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
return false unless attributes
|
|
199
|
+
|
|
200
|
+
sliced = attributes.deep_stringify_keys.slice(*model.column_names)
|
|
201
|
+
incoming_at = parse_timestamp(sliced["updated_at"])
|
|
202
|
+
confirmed_at = parse_timestamp(snapshot["attributes"] && snapshot["attributes"]["updated_at"])
|
|
203
|
+
confirmed_hlc = snapshot["hlc"]
|
|
204
|
+
|
|
205
|
+
if snapshot["exists"] && stale_against?(incoming_hlc, confirmed_hlc, incoming_at, confirmed_at)
|
|
206
|
+
notify_discarded_if_different(model, record_id, sliced)
|
|
207
|
+
return false
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
updated_snapshot = {
|
|
211
|
+
"exists" => true,
|
|
212
|
+
"version" => version || sliced["server_version"] || snapshot["version"] || 0,
|
|
213
|
+
"hlc" => incoming_hlc || confirmed_hlc,
|
|
214
|
+
"attributes" => sliced,
|
|
215
|
+
"initial_attributes" => snapshot["initial_attributes"]
|
|
216
|
+
}
|
|
217
|
+
write_confirmed_snapshot(record_type, record_id, updated_snapshot)
|
|
218
|
+
|
|
219
|
+
rebased = replay_onto_confirmed(model, record_id, updated_snapshot, remaining)
|
|
220
|
+
changed = materialize_state(model, record_id, rebased, remaining: remaining, force_hlc: true, record_delta: record_delta)
|
|
221
|
+
|
|
222
|
+
# Preserve on_discarded_change semantics when a newer local unacked
|
|
223
|
+
# edit shadowed incoming remote values.
|
|
224
|
+
final_at = parse_timestamp(rebased["attributes"] && rebased["attributes"]["updated_at"])
|
|
225
|
+
final_hlc = rebased["hlc"]
|
|
226
|
+
if stale_against?(incoming_hlc, final_hlc, incoming_at, final_at) && shadowed_by_local?(sliced, rebased["attributes"])
|
|
227
|
+
model.instant_record_discarded_change_handler&.call(sliced)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
changed
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def reset_types(types)
|
|
234
|
+
return if types.empty?
|
|
235
|
+
|
|
236
|
+
SyncMetadata.all.each do |entry|
|
|
237
|
+
next unless entry.key.start_with?(CONFIRMED_PREFIX)
|
|
238
|
+
|
|
239
|
+
rest = entry.key[CONFIRMED_PREFIX.length..]
|
|
240
|
+
record_type = rest.split(":", 2).first
|
|
241
|
+
entry.destroy if types.include?(record_type)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
private
|
|
246
|
+
|
|
247
|
+
def advance_confirmed_snapshot(snapshot, mutation, version, model, hlc: nil)
|
|
248
|
+
next_hlc = max_valid_hlc(snapshot && snapshot["hlc"], hlc)
|
|
249
|
+
case mutation.operation
|
|
250
|
+
when "destroy"
|
|
251
|
+
{ "exists" => false, "version" => 0, "hlc" => nil, "attributes" => nil, "initial_attributes" => nil }
|
|
252
|
+
when "create"
|
|
253
|
+
attrs = (mutation.changes_payload || {}).deep_stringify_keys.merge("id" => mutation.record_id)
|
|
254
|
+
attrs = attrs.slice(*model.column_names) if model
|
|
255
|
+
existing_initial = snapshot && snapshot["initial_attributes"]
|
|
256
|
+
{
|
|
257
|
+
"exists" => true,
|
|
258
|
+
"version" => version || 1,
|
|
259
|
+
"hlc" => next_hlc,
|
|
260
|
+
"attributes" => attrs,
|
|
261
|
+
"initial_attributes" => existing_initial || attrs
|
|
262
|
+
}
|
|
263
|
+
when "update"
|
|
264
|
+
base =
|
|
265
|
+
if snapshot && snapshot["exists"] && snapshot["attributes"]
|
|
266
|
+
snapshot["attributes"].deep_stringify_keys
|
|
267
|
+
elsif model && (existing = model.find_by(id: mutation.record_id))
|
|
268
|
+
InstantRecord.wire_attributes(existing.attributes)
|
|
269
|
+
else
|
|
270
|
+
{ "id" => mutation.record_id }
|
|
271
|
+
end
|
|
272
|
+
changes = (mutation.changes_payload || {}).deep_stringify_keys.except("id", "server_version", "sync_state")
|
|
273
|
+
base_at = parse_timestamp(base["updated_at"])
|
|
274
|
+
mut_at = parse_timestamp(changes["updated_at"])
|
|
275
|
+
merged = base.merge(changes)
|
|
276
|
+
# Never rewind confirmed updated_at backwards below a newer concurrent server delta.
|
|
277
|
+
merged["updated_at"] = base["updated_at"] if base_at && mut_at && mut_at < base_at
|
|
278
|
+
merged = merged.slice(*model.column_names) if model
|
|
279
|
+
# Preserve initial_attributes on untouched columns so a concurrent server delta
|
|
280
|
+
# on another column stays recognized while remaining local mutations replay.
|
|
281
|
+
initial =
|
|
282
|
+
if snapshot && snapshot["initial_attributes"]
|
|
283
|
+
updated_initial = snapshot["initial_attributes"].deep_stringify_keys.merge(changes)
|
|
284
|
+
model ? updated_initial.slice(*model.column_names) : updated_initial
|
|
285
|
+
else
|
|
286
|
+
merged
|
|
287
|
+
end
|
|
288
|
+
{
|
|
289
|
+
"exists" => true,
|
|
290
|
+
"version" => version || (snapshot && snapshot["version"]) || 0,
|
|
291
|
+
"hlc" => next_hlc,
|
|
292
|
+
"attributes" => merged,
|
|
293
|
+
"initial_attributes" => initial
|
|
294
|
+
}
|
|
295
|
+
else
|
|
296
|
+
snapshot
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def authoritative_snapshot(snapshot, result, fallback_missing: false)
|
|
301
|
+
if result["server_attributes"].present?
|
|
302
|
+
attrs = result["server_attributes"].deep_stringify_keys
|
|
303
|
+
hlc = HLC.valid?(result["hlc"]) ? result["hlc"] : (snapshot && snapshot["hlc"])
|
|
304
|
+
{
|
|
305
|
+
"exists" => true,
|
|
306
|
+
"version" => result["version"] || attrs["server_version"] || (snapshot && snapshot["version"]) || 0,
|
|
307
|
+
"hlc" => hlc,
|
|
308
|
+
"attributes" => attrs,
|
|
309
|
+
"initial_attributes" => snapshot ? snapshot["initial_attributes"] : nil
|
|
310
|
+
}
|
|
311
|
+
elsif fallback_missing
|
|
312
|
+
{
|
|
313
|
+
"exists" => false,
|
|
314
|
+
"version" => 0,
|
|
315
|
+
"hlc" => nil,
|
|
316
|
+
"attributes" => nil,
|
|
317
|
+
"initial_attributes" => snapshot ? snapshot["initial_attributes"] : nil
|
|
318
|
+
}
|
|
319
|
+
else
|
|
320
|
+
snapshot
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# Pure in-memory GGPO replay: start from server-confirmed state and
|
|
325
|
+
# deterministically apply each remaining unacked mutation in order.
|
|
326
|
+
def replay_onto_confirmed(model, record_id, confirmed, remaining)
|
|
327
|
+
exists = confirmed["exists"] == true
|
|
328
|
+
confirmed_attrs = exists ? (confirmed["attributes"] || {}).deep_stringify_keys.slice(*model.column_names) : nil
|
|
329
|
+
state_attrs = confirmed_attrs ? confirmed_attrs.dup : nil
|
|
330
|
+
initial_attrs = confirmed["initial_attributes"] ? confirmed["initial_attributes"].deep_stringify_keys.slice(*model.column_names) : nil
|
|
331
|
+
confirmed_version = confirmed["version"].to_i
|
|
332
|
+
confirmed_hlc = HLC.valid?(confirmed["hlc"]) ? confirmed["hlc"] : nil
|
|
333
|
+
state_hlc = confirmed_hlc
|
|
334
|
+
confirmed_at = parse_timestamp(confirmed_attrs && confirmed_attrs["updated_at"])
|
|
335
|
+
replay_handler = model.instant_record_replay_mutation_handler
|
|
336
|
+
|
|
337
|
+
remaining.each do |mutation|
|
|
338
|
+
mut_hlc = mutation.hlc
|
|
339
|
+
if replay_handler
|
|
340
|
+
record = build_in_memory_record(model, record_id, exists ? state_attrs : nil, confirmed_version)
|
|
341
|
+
handled = replay_handler.call(record, mutation)
|
|
342
|
+
if handled == :destroy
|
|
343
|
+
exists = false
|
|
344
|
+
state_attrs = nil
|
|
345
|
+
state_hlc = nil
|
|
346
|
+
next
|
|
347
|
+
elsif handled && handled != :default
|
|
348
|
+
exists = true
|
|
349
|
+
updated = InstantRecord.wire_attributes(record.attributes).slice(*model.column_names)
|
|
350
|
+
updated.delete("created_at") if updated["created_at"].nil?
|
|
351
|
+
state_attrs = (state_attrs || {}).merge(updated)
|
|
352
|
+
state_hlc = max_valid_hlc(state_hlc, mut_hlc)
|
|
353
|
+
next
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
changes = (mutation.changes_payload || {}).deep_stringify_keys.slice(*model.column_names)
|
|
358
|
+
mut_at = parse_timestamp(changes["updated_at"])
|
|
359
|
+
|
|
360
|
+
case mutation.operation
|
|
361
|
+
when "destroy"
|
|
362
|
+
exists = false
|
|
363
|
+
state_attrs = nil
|
|
364
|
+
state_hlc = nil
|
|
365
|
+
when "create"
|
|
366
|
+
if !exists
|
|
367
|
+
exists = true
|
|
368
|
+
state_attrs = changes.merge("id" => record_id)
|
|
369
|
+
state_hlc = max_valid_hlc(state_hlc, mut_hlc)
|
|
370
|
+
else
|
|
371
|
+
replayed = apply_replayed_columns!(state_attrs, changes,
|
|
372
|
+
mut_hlc: mut_hlc, confirmed_hlc: confirmed_hlc,
|
|
373
|
+
mut_at: mut_at, confirmed_at: confirmed_at,
|
|
374
|
+
confirmed_attrs: confirmed_attrs, initial_attrs: initial_attrs)
|
|
375
|
+
state_hlc = max_valid_hlc(state_hlc, mut_hlc) if replayed
|
|
376
|
+
end
|
|
377
|
+
when "update"
|
|
378
|
+
if exists
|
|
379
|
+
replayed = apply_replayed_columns!(state_attrs, changes,
|
|
380
|
+
mut_hlc: mut_hlc, confirmed_hlc: confirmed_hlc,
|
|
381
|
+
mut_at: mut_at, confirmed_at: confirmed_at,
|
|
382
|
+
confirmed_attrs: confirmed_attrs, initial_attrs: initial_attrs)
|
|
383
|
+
state_hlc = max_valid_hlc(state_hlc, mut_hlc) if replayed
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
{ "exists" => exists, "version" => confirmed_version, "hlc" => state_hlc, "attributes" => state_attrs }
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def apply_replayed_columns!(state_attrs, changes, mut_hlc:, confirmed_hlc:, mut_at:, confirmed_at:, confirmed_attrs:, initial_attrs:)
|
|
392
|
+
server_newer = strictly_newer?(confirmed_hlc, mut_hlc, confirmed_at, mut_at)
|
|
393
|
+
applied_any = false
|
|
394
|
+
|
|
395
|
+
changes.each do |col, val|
|
|
396
|
+
next if SKIP_REPLAY_COLUMNS.include?(col)
|
|
397
|
+
|
|
398
|
+
server_changed_col = initial_attrs.nil? || confirmed_attrs.nil? ||
|
|
399
|
+
normalized_value(confirmed_attrs[col]) != normalized_value(initial_attrs[col])
|
|
400
|
+
next if server_newer && server_changed_col
|
|
401
|
+
|
|
402
|
+
state_attrs[col] = val
|
|
403
|
+
applied_any = true
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
if strictly_newer?(mut_hlc, confirmed_hlc, mut_at, confirmed_at)
|
|
407
|
+
state_attrs["updated_at"] = changes["updated_at"] if changes["updated_at"]
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
applied_any
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def build_in_memory_record(model, record_id, attrs, version)
|
|
414
|
+
record = model.new
|
|
415
|
+
record.id = record_id
|
|
416
|
+
record.assign_attributes(attrs.except("id", "server_version", "sync_state")) if attrs
|
|
417
|
+
record.server_version = version
|
|
418
|
+
record
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# Materialize the computed state onto SQLite in a single write (only if
|
|
422
|
+
# needed). Returns true when visible user attributes (or row existence)
|
|
423
|
+
# actually changed.
|
|
424
|
+
def materialize_state(model, record_id, state, remaining:, force_hlc: false, record_delta: false)
|
|
425
|
+
existing = model.find_by(id: record_id)
|
|
426
|
+
before_visible = existing ? visible_attributes(existing) : nil
|
|
427
|
+
|
|
428
|
+
unless state["exists"]
|
|
429
|
+
HLC.delete_row_hlc(model.name, record_id)
|
|
430
|
+
return false unless existing
|
|
431
|
+
|
|
432
|
+
Client.note_record_change(existing, "destroy") if record_delta
|
|
433
|
+
existing.destroy!
|
|
434
|
+
return true
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
attrs = (state["attributes"] || {}).deep_stringify_keys.slice(*model.column_names)
|
|
438
|
+
record = existing || model.new(id: record_id)
|
|
439
|
+
assignable = attrs.except("id", "server_version", "sync_state")
|
|
440
|
+
assignable.delete("created_at") if existing && assignable["created_at"].nil?
|
|
441
|
+
record.assign_attributes(assignable)
|
|
442
|
+
record.server_version = state["version"].to_i
|
|
443
|
+
record.sync_state = remaining.empty? ? "synced" : "pending"
|
|
444
|
+
|
|
445
|
+
after_visible = visible_attributes(record)
|
|
446
|
+
visible_changed = (before_visible != after_visible)
|
|
447
|
+
was_new = record.new_record?
|
|
448
|
+
changed_cols = record.changed.map(&:to_s)
|
|
449
|
+
|
|
450
|
+
if was_new || visible_changed
|
|
451
|
+
preserve_timestamps = record.respond_to?(:record_timestamps=) &&
|
|
452
|
+
assignable["updated_at"].present? &&
|
|
453
|
+
(!was_new || !model.column_names.include?("created_at") || assignable["created_at"].present?)
|
|
454
|
+
if preserve_timestamps
|
|
455
|
+
previous = record.record_timestamps
|
|
456
|
+
record.record_timestamps = false
|
|
457
|
+
begin
|
|
458
|
+
record.save!(validate: false)
|
|
459
|
+
ensure
|
|
460
|
+
record.record_timestamps = previous
|
|
461
|
+
end
|
|
462
|
+
else
|
|
463
|
+
record.save!(validate: false)
|
|
464
|
+
end
|
|
465
|
+
Client.note_record_change(record, was_new ? "create" : "update", changed_columns: changed_cols) if record_delta
|
|
466
|
+
elsif record.changed?
|
|
467
|
+
record.update_columns(server_version: record.server_version, sync_state: record.sync_state)
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
if HLC.valid?(state["hlc"])
|
|
471
|
+
HLC.set_row_hlc(model.name, record_id, state["hlc"], force: force_hlc)
|
|
472
|
+
end
|
|
473
|
+
visible_changed
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
def visible_attributes(record)
|
|
477
|
+
InstantRecord.wire_attributes(record.attributes)
|
|
478
|
+
.except(*Client::BOOKKEEPING_COLUMNS)
|
|
479
|
+
.transform_values { |v| normalized_value(v) }
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
def shadowed_by_local?(incoming_attrs, final_attrs)
|
|
483
|
+
return false unless final_attrs
|
|
484
|
+
|
|
485
|
+
incoming_attrs.each do |col, val|
|
|
486
|
+
next if SKIP_REPLAY_COLUMNS.include?(col)
|
|
487
|
+
return true if normalized_value(final_attrs[col]) != normalized_value(val)
|
|
488
|
+
end
|
|
489
|
+
false
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
def notify_discarded_if_different(model, record_id, incoming_attrs)
|
|
493
|
+
handler = model.instant_record_discarded_change_handler
|
|
494
|
+
return unless handler
|
|
495
|
+
|
|
496
|
+
existing = model.find_by(id: record_id)
|
|
497
|
+
return unless existing
|
|
498
|
+
|
|
499
|
+
current = visible_attributes(existing)
|
|
500
|
+
differs = incoming_attrs.any? do |col, val|
|
|
501
|
+
next false if SKIP_REPLAY_COLUMNS.include?(col)
|
|
502
|
+
|
|
503
|
+
current[col] != normalized_value(val)
|
|
504
|
+
end
|
|
505
|
+
handler.call(incoming_attrs) if differs
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
def stale_against?(incoming_hlc, reference_hlc, incoming_at, reference_at)
|
|
509
|
+
if HLC.valid?(incoming_hlc) && HLC.valid?(reference_hlc)
|
|
510
|
+
HLC.compare(incoming_hlc, reference_hlc) <= 0
|
|
511
|
+
elsif HLC.valid?(incoming_hlc) && reference_at
|
|
512
|
+
pt = HLC.physical_time(incoming_hlc)
|
|
513
|
+
pt ? pt <= reference_at : false
|
|
514
|
+
elsif incoming_at && reference_at
|
|
515
|
+
incoming_at <= reference_at
|
|
516
|
+
else
|
|
517
|
+
false
|
|
518
|
+
end
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
def strictly_newer?(candidate_hlc, baseline_hlc, candidate_at, baseline_at)
|
|
522
|
+
if HLC.valid?(candidate_hlc) && HLC.valid?(baseline_hlc)
|
|
523
|
+
HLC.compare(candidate_hlc, baseline_hlc) > 0
|
|
524
|
+
elsif candidate_at && baseline_at
|
|
525
|
+
candidate_at > baseline_at
|
|
526
|
+
elsif HLC.valid?(candidate_hlc) && baseline_at
|
|
527
|
+
pt = HLC.physical_time(candidate_hlc)
|
|
528
|
+
pt ? pt > baseline_at : false
|
|
529
|
+
elsif candidate_at && HLC.valid?(baseline_hlc)
|
|
530
|
+
pt = HLC.physical_time(baseline_hlc)
|
|
531
|
+
pt ? candidate_at > pt : false
|
|
532
|
+
else
|
|
533
|
+
false
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
def max_valid_hlc(a, b)
|
|
538
|
+
a_valid = HLC.valid?(a)
|
|
539
|
+
b_valid = HLC.valid?(b)
|
|
540
|
+
return b unless a_valid
|
|
541
|
+
return a unless b_valid
|
|
542
|
+
|
|
543
|
+
HLC.compare(a, b) >= 0 ? a : b
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
def normalized_value(val)
|
|
547
|
+
if val.respond_to?(:usec)
|
|
548
|
+
val.utc.iso8601(InstantRecord::TIME_PRECISION)
|
|
549
|
+
else
|
|
550
|
+
val
|
|
551
|
+
end
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
def parse_timestamp(raw)
|
|
555
|
+
return nil if raw.nil? || raw.to_s.empty?
|
|
556
|
+
return raw.to_time.utc if raw.respond_to?(:to_time) && !raw.is_a?(String)
|
|
557
|
+
|
|
558
|
+
Time.parse(raw.to_s).utc
|
|
559
|
+
rescue ArgumentError
|
|
560
|
+
nil
|
|
561
|
+
end
|
|
562
|
+
end
|
|
563
|
+
end
|
|
564
|
+
end
|
|
565
|
+
end
|