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,370 @@
|
|
|
1
|
+
# Persistent headless CRuby client bridge speaking synchronous NDJSON over
|
|
2
|
+
# stdin/stdout (identical wire protocol to wasm_host_bridge.mjs). Runs all
|
|
3
|
+
# simulated CRuby clients in an isolated SQLite-only ActiveRecord process so
|
|
4
|
+
# they never share StatementCache, InstantRecord.browser?, or Client state
|
|
5
|
+
# with a PostgreSQL-backed authoritative Rails server.
|
|
6
|
+
#
|
|
7
|
+
# Usage: ruby -Ilib cruby_host_bridge.rb <tmpdir>
|
|
8
|
+
require "active_record"
|
|
9
|
+
require "active_support/core_ext/time"
|
|
10
|
+
require "cgi"
|
|
11
|
+
require "json"
|
|
12
|
+
require "stringio"
|
|
13
|
+
require "uri"
|
|
14
|
+
require "instant_record"
|
|
15
|
+
|
|
16
|
+
Time.zone = "UTC"
|
|
17
|
+
|
|
18
|
+
# This process is exclusively a local-first client runtime.
|
|
19
|
+
InstantRecord.define_singleton_method(:browser?) { true }
|
|
20
|
+
|
|
21
|
+
tmpdir = ARGV[0] || raise("usage: cruby_host_bridge.rb <tmpdir>")
|
|
22
|
+
|
|
23
|
+
# Bootstrap default connection so ActiveRecord::Base has a live SQLite pool.
|
|
24
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: File.join(tmpdir, "_bootstrap.sqlite3"))
|
|
25
|
+
|
|
26
|
+
class Issue < ActiveRecord::Base
|
|
27
|
+
self.table_name = "issues"
|
|
28
|
+
include InstantRecord::Syncable
|
|
29
|
+
validates :title, presence: true, length: { maximum: 200 }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class AuthNote < ActiveRecord::Base
|
|
33
|
+
self.table_name = "auth_notes"
|
|
34
|
+
include InstantRecord::Syncable
|
|
35
|
+
AUDIENCES = %w[everyone members admins owner].freeze
|
|
36
|
+
validates :title, presence: true, length: { maximum: 120 }
|
|
37
|
+
validates :body, length: { maximum: 2_000 }
|
|
38
|
+
validates :audience, inclusion: { in: AUDIENCES }
|
|
39
|
+
validates :owner_id, presence: true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
InstantRecord.sync(Issue, AuthNote)
|
|
43
|
+
|
|
44
|
+
$stdout.sync = true
|
|
45
|
+
$stdin.sync = true
|
|
46
|
+
|
|
47
|
+
def write_msg(obj)
|
|
48
|
+
$stdout.puts(JSON.generate(obj))
|
|
49
|
+
$stdout.flush
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def read_msg
|
|
53
|
+
line = $stdin.gets
|
|
54
|
+
return nil unless line
|
|
55
|
+
|
|
56
|
+
JSON.parse(line)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class PipeTransport
|
|
60
|
+
attr_reader :exchanges
|
|
61
|
+
|
|
62
|
+
def initialize(client_id)
|
|
63
|
+
@client_id = client_id
|
|
64
|
+
@exchanges = []
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def get_json(path)
|
|
68
|
+
JSON.parse(exchange("GET", path, ""))
|
|
69
|
+
rescue JSON::ParserError => e
|
|
70
|
+
raise InstantRecord::Client::Transport::Error, "GET #{path} invalid JSON: #{e.message}"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def post_json(path, payload)
|
|
74
|
+
JSON.parse(exchange("POST", path, JSON.generate(payload)))
|
|
75
|
+
rescue JSON::ParserError => e
|
|
76
|
+
raise InstantRecord::Client::Transport::Error, "POST #{path} invalid JSON: #{e.message}"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def each_event(path, &block)
|
|
80
|
+
parser = InstantRecord::Client::Transport::SseParser.new(&block)
|
|
81
|
+
parser.feed(exchange("GET", path, ""))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def exchange(method, path, body)
|
|
87
|
+
url = "#{InstantRecord.config.endpoint}#{path}"
|
|
88
|
+
write_msg({ type: "exchange", client: @client_id, method: method, url: url, body: body })
|
|
89
|
+
reply = read_msg
|
|
90
|
+
raise InstantRecord::Client::Transport::Error, "EOF waiting for exchange reply" unless reply
|
|
91
|
+
|
|
92
|
+
status = reply["status"].to_i
|
|
93
|
+
@exchanges << "#{method} #{url} -> #{status}"
|
|
94
|
+
unless status >= 200 && status < 300
|
|
95
|
+
raise InstantRecord::Client::Transport::Error, "#{method} #{path} -> #{status}"
|
|
96
|
+
end
|
|
97
|
+
reply["body"].to_s
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
class RecordingNotifier
|
|
102
|
+
attr_reader :last_notice
|
|
103
|
+
|
|
104
|
+
def initialize
|
|
105
|
+
@last_notice = ""
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def records_changed
|
|
109
|
+
@last_notice = InstantRecord::Client.changes_notice
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
ClientState = Struct.new(:id, :db_path, :shard, :stream_parser, :stream_events, :history_marks, keyword_init: true)
|
|
114
|
+
|
|
115
|
+
clients = {}
|
|
116
|
+
counter = 0
|
|
117
|
+
|
|
118
|
+
MIGRATIONS_DIR = File.expand_path("../../../db/migrate", __dir__)
|
|
119
|
+
|
|
120
|
+
def init_schema!(shard)
|
|
121
|
+
ActiveRecord::Base.connected_to(role: :writing, shard: shard) do
|
|
122
|
+
ActiveRecord::Migration.suppress_messages do
|
|
123
|
+
ActiveRecord::MigrationContext.new(MIGRATIONS_DIR).migrate
|
|
124
|
+
ActiveRecord::Schema.define do
|
|
125
|
+
create_table :issues, id: :string, force: true do |t|
|
|
126
|
+
t.string :title, null: false
|
|
127
|
+
t.string :state, null: false, default: "open"
|
|
128
|
+
t.integer :server_version, null: false, default: 0
|
|
129
|
+
t.string :sync_state, null: false, default: "synced"
|
|
130
|
+
t.timestamps
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
create_table :auth_notes, id: :string, force: true do |t|
|
|
134
|
+
t.string :title, null: false
|
|
135
|
+
t.text :body, null: false, default: ""
|
|
136
|
+
t.string :audience, null: false, default: "owner"
|
|
137
|
+
t.string :owner_id, null: false
|
|
138
|
+
t.integer :server_version, null: false, default: 0
|
|
139
|
+
t.string :sync_state, null: false, default: "synced"
|
|
140
|
+
t.timestamps
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def with_client(client, transport: nil, notifier: nil, endpoint: nil)
|
|
148
|
+
InstantRecord::Client.transport = transport if transport
|
|
149
|
+
InstantRecord::Client.notifier = notifier if notifier
|
|
150
|
+
InstantRecord.config.endpoint = endpoint if endpoint && !endpoint.to_s.empty?
|
|
151
|
+
InstantRecord::Client.streaming = false
|
|
152
|
+
InstantRecord::Client.instance_variable_set(:@stream_parser, client.stream_parser)
|
|
153
|
+
InstantRecord::Client.instance_variable_set(:@stream_events, client.stream_events)
|
|
154
|
+
InstantRecord::Client.instance_variable_set(:@history_marks, client.history_marks)
|
|
155
|
+
InstantRecord::Client.instance_variable_set(:@changed_records, [])
|
|
156
|
+
InstantRecord::Client.instance_variable_set(:@unlisted_changes, false)
|
|
157
|
+
|
|
158
|
+
captured = StringIO.new
|
|
159
|
+
prev_stderr = $stderr
|
|
160
|
+
$stderr = captured
|
|
161
|
+
|
|
162
|
+
val = ActiveRecord::Base.connected_to(role: :writing, shard: client.shard) { yield }
|
|
163
|
+
|
|
164
|
+
client.stream_parser = InstantRecord::Client.instance_variable_get(:@stream_parser)
|
|
165
|
+
client.stream_events = InstantRecord::Client.instance_variable_get(:@stream_events)
|
|
166
|
+
client.history_marks = InstantRecord::Client.instance_variable_get(:@history_marks)
|
|
167
|
+
[val, captured.string.lines(chomp: true)]
|
|
168
|
+
ensure
|
|
169
|
+
$stderr = prev_stderr if prev_stderr
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def model_class(name)
|
|
173
|
+
case name.to_s
|
|
174
|
+
when "Issue" then Issue
|
|
175
|
+
when "AuthNote" then AuthNote
|
|
176
|
+
else raise ArgumentError, "Unknown model: #{name}"
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
write_msg({ type: "ready" })
|
|
181
|
+
|
|
182
|
+
while (cmd = read_msg)
|
|
183
|
+
begin
|
|
184
|
+
case cmd["op"]
|
|
185
|
+
when "create_client"
|
|
186
|
+
counter += 1
|
|
187
|
+
cid = cmd["client"].to_s
|
|
188
|
+
db_path = File.join(tmpdir, "#{cid.gsub(/[^a-zA-Z0-9_-]/, '_')}_#{counter}.sqlite3")
|
|
189
|
+
shard = :"client_#{counter}"
|
|
190
|
+
ActiveRecord::Base.connection_handler.establish_connection(
|
|
191
|
+
{ adapter: "sqlite3", database: db_path },
|
|
192
|
+
owner_name: ActiveRecord::Base.name,
|
|
193
|
+
role: :writing,
|
|
194
|
+
shard: shard
|
|
195
|
+
)
|
|
196
|
+
init_schema!(shard)
|
|
197
|
+
clients[cid] = ClientState.new(
|
|
198
|
+
id: cid,
|
|
199
|
+
db_path: db_path,
|
|
200
|
+
shard: shard,
|
|
201
|
+
stream_parser: nil,
|
|
202
|
+
stream_events: [],
|
|
203
|
+
history_marks: {}
|
|
204
|
+
)
|
|
205
|
+
write_msg({ type: "result", ok: true, db_path: db_path, errors: [] })
|
|
206
|
+
|
|
207
|
+
when "request"
|
|
208
|
+
client = clients.fetch(cmd["client"].to_s)
|
|
209
|
+
verb = cmd["method"].to_s.upcase
|
|
210
|
+
body_str = cmd["body"].to_s
|
|
211
|
+
content_type = cmd["contentType"].to_s
|
|
212
|
+
if verb == "POST" && content_type.start_with?("application/x-www-form-urlencoded")
|
|
213
|
+
override = URI.decode_www_form(body_str).to_h["_method"]
|
|
214
|
+
verb = override.upcase if override && override.match?(/\A(?:patch|put|delete)\z/i)
|
|
215
|
+
end
|
|
216
|
+
route = URI.parse(cmd["path"].to_s).path
|
|
217
|
+
params = URI.decode_www_form(body_str).to_h
|
|
218
|
+
|
|
219
|
+
res, errors = with_client(client) do
|
|
220
|
+
if verb == "GET" && route == "/todo"
|
|
221
|
+
issues = Issue.order(created_at: :desc)
|
|
222
|
+
html = issues.map { |i| %(<li data-id="#{CGI.escapeHTML(i.id.to_s)}" class="#{i.state}">#{CGI.escapeHTML(i.title.to_s)}</li>) }.join("\n")
|
|
223
|
+
{ status: 200, headers: [["Content-Type", "text/html"]], body: "<ul class=\"issues\">#{html}</ul>" }
|
|
224
|
+
elsif verb == "POST" && route == "/todo/issues"
|
|
225
|
+
Issue.create!(title: params["issue[title]"] || params["title"])
|
|
226
|
+
{ status: 302, headers: [["Location", "/todo"]], body: "" }
|
|
227
|
+
elsif verb == "PATCH" && route =~ %r{\A/todo/issues/([^/]+)\z}
|
|
228
|
+
Issue.find(Regexp.last_match(1)).update!(state: params["done"] == "1" ? "done" : "open")
|
|
229
|
+
{ status: 302, headers: [["Location", "/todo"]], body: "" }
|
|
230
|
+
elsif verb == "DELETE" && route =~ %r{\A/todo/issues/([^/]+)\z}
|
|
231
|
+
Issue.find(Regexp.last_match(1)).destroy!
|
|
232
|
+
{ status: 302, headers: [["Location", "/todo"]], body: "" }
|
|
233
|
+
elsif verb == "GET" && route == "/auth"
|
|
234
|
+
notes = AuthNote.order(created_at: :desc)
|
|
235
|
+
html = notes.map { |n| %(<article data-id="#{CGI.escapeHTML(n.id.to_s)}">#{CGI.escapeHTML(n.title.to_s)}</article>) }.join("\n")
|
|
236
|
+
{ status: 200, headers: [["Content-Type", "text/html"]], body: "<section class=\"auth-notes\">#{html}</section>" }
|
|
237
|
+
elsif verb == "POST" && route == "/auth/notes"
|
|
238
|
+
note = AuthNote.new(
|
|
239
|
+
title: params["note[title]"],
|
|
240
|
+
body: params["note[body]"].to_s,
|
|
241
|
+
audience: params["note[audience]"] || "owner",
|
|
242
|
+
owner_id: params["note[owner_id]"].to_s
|
|
243
|
+
)
|
|
244
|
+
note.save
|
|
245
|
+
{ status: 302, headers: [["Location", "/auth"]], body: "" }
|
|
246
|
+
elsif verb == "DELETE" && route =~ %r{\A/auth/notes/([^/]+)\z}
|
|
247
|
+
AuthNote.find_by(id: Regexp.last_match(1))&.destroy!
|
|
248
|
+
{ status: 302, headers: [["Location", "/auth"]], body: "" }
|
|
249
|
+
else
|
|
250
|
+
{ status: 404, headers: [], body: "Not Found" }
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
write_msg({ type: "result", **res, exit: 0, errors: errors })
|
|
254
|
+
|
|
255
|
+
when "sync"
|
|
256
|
+
client = clients.fetch(cmd["client"].to_s)
|
|
257
|
+
transport = PipeTransport.new(client.id)
|
|
258
|
+
notifier = RecordingNotifier.new
|
|
259
|
+
res, errors = with_client(client, transport: transport, notifier: notifier, endpoint: cmd["endpoint"]) do
|
|
260
|
+
changed = InstantRecord::Client.sync_pass
|
|
261
|
+
{
|
|
262
|
+
changed: changed,
|
|
263
|
+
pending: InstantRecord::Client.pending_count,
|
|
264
|
+
cursor: InstantRecord::Client.cursor,
|
|
265
|
+
notice: notifier.last_notice,
|
|
266
|
+
exchanges: transport.exchanges
|
|
267
|
+
}
|
|
268
|
+
end
|
|
269
|
+
write_msg({ type: "result", **res, exit: 0, errors: errors })
|
|
270
|
+
|
|
271
|
+
when "sync_plan"
|
|
272
|
+
client = clients.fetch(cmd["client"].to_s)
|
|
273
|
+
plan, errors = with_client(client) do
|
|
274
|
+
mutations = InstantRecord::Client.pending_mutations
|
|
275
|
+
push = mutations.empty? ? "" : JSON.generate(InstantRecord::Client.mutations_payload(mutations))
|
|
276
|
+
{ "push" => push, "bootstrap" => !InstantRecord::Client.bootstrapped? }
|
|
277
|
+
end
|
|
278
|
+
write_msg({ type: "result", plan: plan, errors: errors })
|
|
279
|
+
|
|
280
|
+
when "consume_stream"
|
|
281
|
+
client = clients.fetch(cmd["client"].to_s)
|
|
282
|
+
notifier = RecordingNotifier.new
|
|
283
|
+
res, errors = with_client(client, notifier: notifier) do
|
|
284
|
+
applied = InstantRecord::Client.consume_stream(cmd["chunk"].to_s)
|
|
285
|
+
{
|
|
286
|
+
applied: applied,
|
|
287
|
+
cursor: InstantRecord::Client.cursor,
|
|
288
|
+
bootstrapped: InstantRecord::Client.bootstrapped?,
|
|
289
|
+
notice: notifier.last_notice
|
|
290
|
+
}
|
|
291
|
+
end
|
|
292
|
+
write_msg({ type: "result", **res, exit: 0, errors: errors })
|
|
293
|
+
|
|
294
|
+
when "sync_state"
|
|
295
|
+
client = clients.fetch(cmd["client"].to_s)
|
|
296
|
+
res, errors = with_client(client) do
|
|
297
|
+
{
|
|
298
|
+
bootstrapped: InstantRecord::Client.bootstrapped?,
|
|
299
|
+
cursor: InstantRecord::Client.cursor,
|
|
300
|
+
pending: InstantRecord::Client.pending_count,
|
|
301
|
+
identity: InstantRecord::Client.identity_key.to_s
|
|
302
|
+
}
|
|
303
|
+
end
|
|
304
|
+
write_msg({ type: "result", **res, errors: errors })
|
|
305
|
+
|
|
306
|
+
when "history"
|
|
307
|
+
client = clients.fetch(cmd["client"].to_s)
|
|
308
|
+
res, errors = with_client(client) do
|
|
309
|
+
meta_key = "history:#{cmd['key']}"
|
|
310
|
+
response = cmd["response"].to_s
|
|
311
|
+
if response.empty?
|
|
312
|
+
mark = InstantRecord::SyncMetadata.get(meta_key)
|
|
313
|
+
{ "ok" => !mark.nil?, "applied" => 0, "has_more" => mark == "more" }
|
|
314
|
+
else
|
|
315
|
+
page = JSON.parse(response)
|
|
316
|
+
records = Array(page["records"])
|
|
317
|
+
more = page["has_more"] == true
|
|
318
|
+
InstantRecord::OutboxMutation.transaction do
|
|
319
|
+
records.each { |rec| InstantRecord::Client.apply_change(rec) }
|
|
320
|
+
InstantRecord::SyncMetadata.set(meta_key, more ? "more" : "end")
|
|
321
|
+
end
|
|
322
|
+
{ "ok" => true, "applied" => records.size, "has_more" => more }
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
write_msg({ type: "result", history: res, errors: errors })
|
|
326
|
+
|
|
327
|
+
when "local_write"
|
|
328
|
+
client = clients.fetch(cmd["client"].to_s)
|
|
329
|
+
skew = cmd["clockSkew"].to_f
|
|
330
|
+
stamp = Time.now.utc + skew
|
|
331
|
+
attrs = (cmd["attributes"] || {}).transform_keys(&:to_s)
|
|
332
|
+
record_id, errors = with_client(client) do
|
|
333
|
+
InstantRecord::HLC.with_clock(-> { Time.now.utc + skew }) do
|
|
334
|
+
klass = model_class(cmd["model"])
|
|
335
|
+
case cmd["operation"].to_s
|
|
336
|
+
when "create"
|
|
337
|
+
attrs["id"] = cmd["id"] if cmd["id"]
|
|
338
|
+
attrs["created_at"] ||= stamp if skew != 0.0
|
|
339
|
+
attrs["updated_at"] ||= stamp if skew != 0.0
|
|
340
|
+
klass.create!(attrs.slice(*klass.column_names)).id
|
|
341
|
+
when "update"
|
|
342
|
+
rec = klass.find_by(id: cmd["id"])
|
|
343
|
+
if rec
|
|
344
|
+
attrs["updated_at"] ||= stamp if skew != 0.0
|
|
345
|
+
rec.update!(attrs.slice(*klass.column_names))
|
|
346
|
+
rec.id
|
|
347
|
+
end
|
|
348
|
+
when "destroy"
|
|
349
|
+
rec = klass.find_by(id: cmd["id"])
|
|
350
|
+
if rec
|
|
351
|
+
rec.destroy!
|
|
352
|
+
rec.id
|
|
353
|
+
end
|
|
354
|
+
else
|
|
355
|
+
raise ArgumentError, "Unknown local_write operation: #{cmd['operation']}"
|
|
356
|
+
end
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
write_msg({ type: "result", record_id: record_id, errors: errors })
|
|
360
|
+
|
|
361
|
+
when "shutdown"
|
|
362
|
+
write_msg({ type: "result", ok: true })
|
|
363
|
+
break
|
|
364
|
+
else
|
|
365
|
+
write_msg({ type: "error", message: "Unknown op: #{cmd['op']}" })
|
|
366
|
+
end
|
|
367
|
+
rescue StandardError => e
|
|
368
|
+
write_msg({ type: "error", message: "#{e.class}: #{e.message}\n#{e.backtrace.first(5).join("\n")}" })
|
|
369
|
+
end
|
|
370
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module InstantRecord
|
|
4
|
+
module Testing
|
|
5
|
+
# Deterministic network fault injector wrapping an AuthoritativeServer.
|
|
6
|
+
# Controls per-client network partitions, asymmetric drops, post-commit ack
|
|
7
|
+
# drops (for idempotency verification), one-shot mid-bootstrap aborts,
|
|
8
|
+
# identity assignments, and per-client clock skew.
|
|
9
|
+
class FaultNetwork
|
|
10
|
+
PARTITION_MODES = %i[online offline drop_post drop_get server_503 drop_after_apply].freeze
|
|
11
|
+
|
|
12
|
+
ClientPolicy = Struct.new(:partition, :identity, :clock_skew, :bootstrap_abort, keyword_init: true)
|
|
13
|
+
|
|
14
|
+
ExchangeRecord = Struct.new(:client_id, :method, :url, :status, :fault, :identity, keyword_init: true)
|
|
15
|
+
|
|
16
|
+
attr_reader :server, :log
|
|
17
|
+
|
|
18
|
+
def initialize(server)
|
|
19
|
+
@server = server
|
|
20
|
+
@policies = {}
|
|
21
|
+
@log = []
|
|
22
|
+
@fault_fired_in_window = false
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def register_client(client_id, identity: nil, clock_skew: 0.0, partition: :online)
|
|
26
|
+
@policies[client_id.to_s] = ClientPolicy.new(
|
|
27
|
+
partition: partition,
|
|
28
|
+
identity: identity,
|
|
29
|
+
clock_skew: clock_skew.to_f,
|
|
30
|
+
bootstrap_abort: nil
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def policy(client_id)
|
|
35
|
+
@policies[client_id.to_s] ||= ClientPolicy.new(partition: :online, identity: nil, clock_skew: 0.0, bootstrap_abort: nil)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def set_partition(client_id, mode)
|
|
39
|
+
sym = mode.to_sym
|
|
40
|
+
raise ArgumentError, "Unknown partition mode: #{mode}" unless PARTITION_MODES.include?(sym)
|
|
41
|
+
|
|
42
|
+
policy(client_id).partition = sym
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def set_identity(client_id, identity)
|
|
46
|
+
policy(client_id).identity = identity
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def set_clock_skew(client_id, seconds)
|
|
50
|
+
policy(client_id).clock_skew = seconds.to_f
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# One-shot mid-bootstrap abort: :network_drop (status 0), :server_503 (503),
|
|
54
|
+
# or :truncated_body (200 with truncated invalid JSON).
|
|
55
|
+
def arm_bootstrap_abort(client_id, mode = :network_drop)
|
|
56
|
+
policy(client_id).bootstrap_abort = mode.to_sym
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def heal_all!
|
|
60
|
+
@policies.each_value do |pol|
|
|
61
|
+
pol.partition = :online
|
|
62
|
+
pol.bootstrap_abort = nil
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def track_faults
|
|
67
|
+
previous = @fault_fired_in_window
|
|
68
|
+
@fault_fired_in_window = false
|
|
69
|
+
result = yield
|
|
70
|
+
[result, @fault_fired_in_window]
|
|
71
|
+
ensure
|
|
72
|
+
@fault_fired_in_window = previous || @fault_fired_in_window
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Synchronous exchange invoked by CRuby, Wasm (Node AppHost), or Native (Fiddle).
|
|
76
|
+
def exchange(client_id, method, url, body = "")
|
|
77
|
+
pol = policy(client_id)
|
|
78
|
+
verb = method.to_s.upcase
|
|
79
|
+
path = url.to_s
|
|
80
|
+
|
|
81
|
+
# 1. One-shot mid-bootstrap abort takes precedence on /bootstrap.
|
|
82
|
+
if verb == "GET" && path.include?("/bootstrap") && pol.bootstrap_abort
|
|
83
|
+
abort_mode = pol.bootstrap_abort
|
|
84
|
+
pol.bootstrap_abort = nil
|
|
85
|
+
@fault_fired_in_window = true
|
|
86
|
+
return record_exchange(client_id, verb, path, pol.identity, fault: :"bootstrap_#{abort_mode}") do
|
|
87
|
+
case abort_mode
|
|
88
|
+
when :network_drop then { status: 0, body: "" }
|
|
89
|
+
when :server_503 then { status: 503, body: "Service Unavailable" }
|
|
90
|
+
when :truncated_body then { status: 200, body: '{"cursor":99,"records":[{"type":"Issue","id":"half' }
|
|
91
|
+
else { status: 0, body: "" }
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# 2. Partition rules.
|
|
97
|
+
case pol.partition
|
|
98
|
+
when :offline
|
|
99
|
+
@fault_fired_in_window = true
|
|
100
|
+
return record_exchange(client_id, verb, path, pol.identity, fault: :offline) { { status: 0, body: "" } }
|
|
101
|
+
when :drop_post
|
|
102
|
+
if verb == "POST"
|
|
103
|
+
@fault_fired_in_window = true
|
|
104
|
+
return record_exchange(client_id, verb, path, pol.identity, fault: :drop_post) { { status: 0, body: "" } }
|
|
105
|
+
end
|
|
106
|
+
when :drop_get
|
|
107
|
+
if verb == "GET"
|
|
108
|
+
@fault_fired_in_window = true
|
|
109
|
+
return record_exchange(client_id, verb, path, pol.identity, fault: :drop_get) { { status: 0, body: "" } }
|
|
110
|
+
end
|
|
111
|
+
when :server_503
|
|
112
|
+
@fault_fired_in_window = true
|
|
113
|
+
return record_exchange(client_id, verb, path, pol.identity, fault: :server_503) { { status: 503, body: "Service Unavailable" } }
|
|
114
|
+
when :drop_after_apply
|
|
115
|
+
if verb == "POST" && path.include?("/mutations")
|
|
116
|
+
# Server applies and commits the batch, then the response is dropped
|
|
117
|
+
# before reaching the client. The client must retry the same mutation_id
|
|
118
|
+
# and receive the idempotent ledger replay without duplicating state.
|
|
119
|
+
@server.exchange(verb, path, body, identity: pol.identity)
|
|
120
|
+
pol.partition = :online
|
|
121
|
+
@fault_fired_in_window = true
|
|
122
|
+
return record_exchange(client_id, verb, path, pol.identity, fault: :drop_after_apply) { { status: 0, body: "" } }
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
reply = @server.exchange(verb, path, body, identity: pol.identity)
|
|
127
|
+
@log << ExchangeRecord.new(client_id: client_id.to_s, method: verb, url: path, status: reply[:status], fault: nil, identity: pol.identity)
|
|
128
|
+
{ status: reply[:status], body: reply[:body].to_s }
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
private
|
|
132
|
+
|
|
133
|
+
def record_exchange(client_id, verb, path, identity, fault:)
|
|
134
|
+
reply = yield
|
|
135
|
+
@log << ExchangeRecord.new(client_id: client_id.to_s, method: verb, url: path, status: reply[:status], fault: fault, identity: identity)
|
|
136
|
+
reply
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|