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.
Files changed (198) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +58 -0
  3. data/README.md +267 -61
  4. data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
  5. data/app/controllers/instant_record/events_controller.rb +58 -6
  6. data/app/controllers/instant_record/files_controller.rb +116 -0
  7. data/app/controllers/instant_record/mutations_controller.rb +84 -5
  8. data/app/controllers/instant_record/native_screens_controller.rb +60 -0
  9. data/app/controllers/instant_record/records_controller.rb +86 -8
  10. data/app/controllers/instant_record/shells_controller.rb +64 -0
  11. data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
  12. data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
  13. data/app/jobs/instant_record/shell_build_job.rb +16 -0
  14. data/app/models/instant_record/change.rb +36 -3
  15. data/app/models/instant_record/mutation_applier.rb +633 -50
  16. data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
  17. data/compiled/README.md +272 -0
  18. data/compiled/pipeline/browser_migrations.rb +18 -0
  19. data/compiled/pipeline/build.rb +13 -0
  20. data/compiled/pipeline/build_native.rb +14 -0
  21. data/compiled/pipeline/native/host.c +78 -0
  22. data/compiled/pipeline/native/instant_record_native.h +57 -0
  23. data/compiled/pipeline/native_shim.c +149 -0
  24. data/compiled/pipeline/reactor_shim.c +66 -0
  25. data/compiled/pipeline/sqlite/rh_shim.c +5 -0
  26. data/compiled/pipeline/sqlite_release.rb +43 -0
  27. data/compiled/toolchain.env.example +22 -0
  28. data/compiled/web/index.html +20 -0
  29. data/compiled/web/package-lock.json +884 -0
  30. data/compiled/web/package.json +17 -0
  31. data/compiled/web/src/app-host-core.js +556 -0
  32. data/compiled/web/src/app-host.js +16 -0
  33. data/compiled/web/src/branch-manager.js +140 -0
  34. data/compiled/web/src/branch-preview.js +397 -0
  35. data/compiled/web/src/changes.js +191 -0
  36. data/compiled/web/src/faults.js +172 -0
  37. data/compiled/web/src/flight-recorder.js +347 -0
  38. data/compiled/web/src/history-fill.js +68 -0
  39. data/compiled/web/src/http-headers.js +100 -0
  40. data/compiled/web/src/leader-election.js +705 -0
  41. data/compiled/web/src/main.js +608 -0
  42. data/compiled/web/src/manifest.js +112 -0
  43. data/compiled/web/src/opfs-wasi-core.js +109 -0
  44. data/compiled/web/src/opfs-wasi-file.js +8 -0
  45. data/compiled/web/src/reactor-runtime.js +1692 -0
  46. data/compiled/web/src/redactor.js +518 -0
  47. data/compiled/web/src/release-updates.js +48 -0
  48. data/compiled/web/src/replay.js +338 -0
  49. data/compiled/web/src/schema-recovery.js +204 -0
  50. data/compiled/web/src/server-request.js +68 -0
  51. data/compiled/web/src/sha256.js +98 -0
  52. data/compiled/web/src/shell.js +52 -0
  53. data/compiled/web/src/snapshot-store.js +38 -0
  54. data/compiled/web/src/sqlite-redactor.js +111 -0
  55. data/compiled/web/src/storage-backend.js +159 -0
  56. data/compiled/web/src/stream-leader.js +675 -0
  57. data/compiled/web/src/sync-context.js +91 -0
  58. data/compiled/web/src/sync-drain.js +104 -0
  59. data/compiled/web/src/sync-schedule.js +9 -0
  60. data/compiled/web/src/wasi-minimal.js +275 -0
  61. data/compiled/web/src/worker.js +39 -0
  62. data/compiled/web/vite.config.js +27 -0
  63. data/config/routes.rb +25 -0
  64. data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
  65. data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
  66. data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
  67. data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
  68. data/exe/instant_record_mcp +6 -0
  69. data/frontend/LiveList.js +67 -0
  70. data/frontend/chord.js +79 -0
  71. data/frontend/file.js +65 -0
  72. data/frontend/files.js +738 -0
  73. data/frontend/focus.js +10 -0
  74. data/frontend/held.js +192 -0
  75. data/frontend/http.js +76 -0
  76. data/frontend/index.js +53 -0
  77. data/frontend/keyboard.js +56 -0
  78. data/frontend/live.js +298 -0
  79. data/frontend/notices.js +118 -0
  80. data/frontend/package.json +15 -0
  81. data/frontend/range.js +10 -0
  82. data/frontend/selection.js +47 -0
  83. data/frontend/shortcuts.js +25 -0
  84. data/frontend/sync-notices.js +130 -0
  85. data/frontend/undo.js +48 -0
  86. data/frontend/window.js +181 -0
  87. data/lib/generators/instant_record/install/install_generator.rb +18 -30
  88. data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
  89. data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
  90. data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
  91. data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
  92. data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
  93. data/lib/instant_record/branch/sqlite_store.rb +142 -0
  94. data/lib/instant_record/branch.rb +388 -0
  95. data/lib/instant_record/client/notifier.rb +8 -12
  96. data/lib/instant_record/client/rebaser.rb +565 -0
  97. data/lib/instant_record/client/transport.rb +64 -42
  98. data/lib/instant_record/client.rb +1600 -169
  99. data/lib/instant_record/compiled_site.rb +85 -0
  100. data/lib/instant_record/compiler/app_stage.rb +212 -0
  101. data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
  102. data/lib/instant_record/compiler/browser_migrations.rb +161 -0
  103. data/lib/instant_record/compiler/entry_builder.rb +832 -0
  104. data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
  105. data/lib/instant_record/compiler/gem_vendor.rb +194 -0
  106. data/lib/instant_record/compiler/host_adapters.rb +506 -0
  107. data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
  108. data/lib/instant_record/compiler/manifest.rb +178 -0
  109. data/lib/instant_record/compiler/native_build.rb +586 -0
  110. data/lib/instant_record/compiler/native_contracts.rb +338 -0
  111. data/lib/instant_record/compiler/path_scrubber.rb +31 -0
  112. data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
  113. data/lib/instant_record/compiler/route_analyzer.rb +623 -0
  114. data/lib/instant_record/compiler/runner.rb +42 -0
  115. data/lib/instant_record/compiler/schema_lock.rb +606 -0
  116. data/lib/instant_record/compiler/source_pages.rb +77 -0
  117. data/lib/instant_record/compiler/static_linter.rb +770 -0
  118. data/lib/instant_record/compiler/swift_codegen.rb +341 -0
  119. data/lib/instant_record/compiler/toolchain.rb +219 -0
  120. data/lib/instant_record/compiler/wasm_build.rb +704 -0
  121. data/lib/instant_record/compiler/wasm_tree.rb +518 -0
  122. data/lib/instant_record/compiler.rb +277 -0
  123. data/lib/instant_record/configuration.rb +104 -0
  124. data/lib/instant_record/engine.rb +68 -18
  125. data/lib/instant_record/file_store.rb +162 -0
  126. data/lib/instant_record/files.rb +92 -0
  127. data/lib/instant_record/hlc.rb +387 -0
  128. data/lib/instant_record/host.rb +66 -0
  129. data/lib/instant_record/inertia/renderer.rb +52 -0
  130. data/lib/instant_record/inertia.rb +222 -0
  131. data/lib/instant_record/mcp/cli.rb +319 -0
  132. data/lib/instant_record/mcp/host_process.rb +229 -0
  133. data/lib/instant_record/mcp/http_exchange.rb +233 -0
  134. data/lib/instant_record/mcp/replica.rb +556 -0
  135. data/lib/instant_record/mcp/runtime.rb +406 -0
  136. data/lib/instant_record/mcp/server.rb +429 -0
  137. data/lib/instant_record/mcp.rb +47 -0
  138. data/lib/instant_record/native/contract.rb +367 -0
  139. data/lib/instant_record/native/node.rb +125 -0
  140. data/lib/instant_record/native/screen.rb +366 -0
  141. data/lib/instant_record/native/xcode_project.rb +344 -0
  142. data/lib/instant_record/native.rb +41 -0
  143. data/lib/instant_record/outbox_mutation.rb +81 -9
  144. data/lib/instant_record/outbox_upcaster.rb +161 -0
  145. data/lib/instant_record/read_tracker.rb +610 -0
  146. data/lib/instant_record/routing.rb +33 -0
  147. data/lib/instant_record/runtime_scoped.rb +4 -0
  148. data/lib/instant_record/shell.rb +486 -0
  149. data/lib/instant_record/sync_actions.rb +800 -0
  150. data/lib/instant_record/sync_metadata.rb +17 -2
  151. data/lib/instant_record/sync_notice.rb +234 -0
  152. data/lib/instant_record/sync_partition.rb +63 -0
  153. data/lib/instant_record/sync_scope.rb +300 -0
  154. data/lib/instant_record/sync_shape.rb +102 -0
  155. data/lib/instant_record/sync_window.rb +92 -15
  156. data/lib/instant_record/syncable.rb +313 -17
  157. data/lib/instant_record/testing/authoritative_server.rb +547 -0
  158. data/lib/instant_record/testing/chaos_simulator.rb +429 -0
  159. data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
  160. data/lib/instant_record/testing/fault_network.rb +140 -0
  161. data/lib/instant_record/testing/parity_suite.rb +435 -0
  162. data/lib/instant_record/testing/runtimes/base.rb +184 -0
  163. data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
  164. data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
  165. data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
  166. data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
  167. data/lib/instant_record/testing.rb +333 -0
  168. data/lib/instant_record/trace/redactor.rb +278 -0
  169. data/lib/instant_record/trace/replayer.rb +126 -0
  170. data/lib/instant_record/trace/schema_extractor.rb +37 -0
  171. data/lib/instant_record/trace.rb +22 -0
  172. data/lib/instant_record/version.rb +1 -1
  173. data/lib/instant_record.rb +107 -37
  174. data/lib/tasks/instant_record.rake +299 -47
  175. data/native/ios/InstantRecordNative/Package.swift +12 -0
  176. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
  177. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
  178. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
  179. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
  180. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
  181. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
  182. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
  183. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
  184. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
  185. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
  186. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
  187. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
  188. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
  189. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
  190. data/native/ios/README.md +341 -0
  191. data/sig/instant_record/inertia.rbs +47 -0
  192. data/sig/instant_record/native.rbs +38 -0
  193. metadata +194 -18
  194. data/lib/generators/instant_record/install/templates/database.js +0 -32
  195. data/lib/generators/instant_record/install/templates/index.html +0 -69
  196. data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
  197. data/lib/instant_record/local_schema.rb +0 -75
  198. data/lib/instant_record/pglite_compat.rb +0 -40
@@ -0,0 +1,344 @@
1
+ require "fileutils"
2
+ require "pathname"
3
+
4
+ module InstantRecord
5
+ module Native
6
+ # Writes the Xcode project for a native app whose screens are Ruby: one
7
+ # app target that links the compiled libraries and the renderer package,
8
+ # and a UI test target when the app has UI tests. The project file is
9
+ # this class's output and is never edited by hand: add a Swift file to
10
+ # the app's folder and write the project again.
11
+ #
12
+ # InstantRecord::Native::XcodeProject.new(
13
+ # folder: "native/ios/Inbox", name: "Inbox", bundle_id: "dev.instantrecord.Inbox",
14
+ # package: "../InstantRecordNative", headers: "../../../compiled/pipeline/native",
15
+ # libraries: "../../../compiled/.build-native"
16
+ # ).write
17
+ #
18
+ # `folder` holds `<name>/` with the app's Swift files, and `<name>UITests/`
19
+ # when it has any. `package`, `headers` and `libraries` are paths from
20
+ # `folder`: the InstantRecordNative Swift package, the folder with
21
+ # instant_record_native.h, and the folder whose `iphonesimulator`
22
+ # subfolder holds the built libraries. `packages` are other Swift
23
+ # packages the app links, each product's name with its path from
24
+ # `folder`: `packages: { "Feedback" => "../../feedback" }`.
25
+ class XcodeProject
26
+ def initialize(folder:, name:, bundle_id:, package:, headers:, libraries:, packages: {})
27
+ @folder = Pathname(folder)
28
+ @name = name
29
+ @bundle_id = bundle_id
30
+ @packages = [["InstantRecordNative", package]] + packages.to_a
31
+ @headers = headers
32
+ @libraries = libraries
33
+ end
34
+
35
+ def write
36
+ project = @folder.join("#{@name}.xcodeproj")
37
+ FileUtils.mkdir_p(project.join("xcshareddata/xcschemes"))
38
+ project.join("project.pbxproj").write(pbxproj)
39
+ project.join("xcshareddata/xcschemes/#{@name}.xcscheme").write(scheme)
40
+ project
41
+ end
42
+
43
+ def sources = swift_files(@name)
44
+ def ui_tests = swift_files("#{@name}UITests")
45
+
46
+ def pbxproj
47
+ app, tests = sources, ui_tests
48
+ others = ["#{@name}-Bridging-Header.h", "Info.plist"]
49
+ <<~PBXPROJ
50
+ // !$*UTF8*$!
51
+ {
52
+ archiveVersion = 1;
53
+ classes = {
54
+ };
55
+ objectVersion = 60;
56
+ objects = {
57
+
58
+ /* Begin PBXBuildFile section */
59
+ #{app.each_with_index.map { |file, n| "\t\t#{id(:build, n)} /* #{file} in Sources */ = {isa = PBXBuildFile; fileRef = #{id(:file, n)} /* #{file} */; };" }.join("\n")}
60
+ #{tests.each_with_index.map { |file, n| "\t\t#{id(:test_build, n)} /* #{file} in Sources */ = {isa = PBXBuildFile; fileRef = #{id(:test_file, n)} /* #{file} */; };" }.join("\n")}
61
+ #{each_package { |product, _, n| "\t\t#{id(:package_build, n)} /* #{product} in Frameworks */ = {isa = PBXBuildFile; productRef = #{id(:package_product, n)} /* #{product} */; };" }}
62
+ /* End PBXBuildFile section */
63
+
64
+ /* Begin PBXContainerItemProxy section */
65
+ #{id(:proxy)} /* PBXContainerItemProxy */ = {
66
+ isa = PBXContainerItemProxy;
67
+ containerPortal = #{id(:project)} /* Project object */;
68
+ proxyType = 1;
69
+ remoteGlobalIDString = #{id(:app_target)};
70
+ remoteInfo = #{@name};
71
+ };
72
+ /* End PBXContainerItemProxy section */
73
+
74
+ /* Begin PBXFileReference section */
75
+ #{app.each_with_index.map { |file, n| file_reference(id(:file, n), file, "sourcecode.swift") }.join("\n")}
76
+ #{others.each_with_index.map { |file, n| file_reference(id(:other, n), file, file.end_with?(".h") ? "sourcecode.c.h" : "text.plist.xml") }.join("\n")}
77
+ #{tests.each_with_index.map { |file, n| file_reference(id(:test_file, n), file, "sourcecode.swift") }.join("\n")}
78
+ #{id(:app_product)} /* #{@name}.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = #{@name}.app; sourceTree = BUILT_PRODUCTS_DIR; };
79
+ #{id(:test_product)} /* #{@name}UITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = #{@name}UITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
80
+ /* End PBXFileReference section */
81
+
82
+ /* Begin PBXFrameworksBuildPhase section */
83
+ #{id(:frameworks)} /* Frameworks */ = {
84
+ isa = PBXFrameworksBuildPhase;
85
+ buildActionMask = 2147483647;
86
+ files = (
87
+ #{each_package { |product, _, n| "\t\t\t\t#{id(:package_build, n)} /* #{product} in Frameworks */," }}
88
+ );
89
+ runOnlyForDeploymentPostprocessing = 0;
90
+ };
91
+ /* End PBXFrameworksBuildPhase section */
92
+
93
+ /* Begin PBXGroup section */
94
+ #{id(:root_group)} = {
95
+ isa = PBXGroup;
96
+ children = (
97
+ #{id(:app_group)} /* #{@name} */,
98
+ #{id(:test_group)} /* #{@name}UITests */,
99
+ #{id(:products_group)} /* Products */,
100
+ );
101
+ sourceTree = "<group>";
102
+ };
103
+ #{id(:app_group)} /* #{@name} */ = {
104
+ isa = PBXGroup;
105
+ children = (
106
+ #{(app.each_index.map { |n| "\t\t\t\t#{id(:file, n)} /* #{app[n]} */," } + others.each_index.map { |n| "\t\t\t\t#{id(:other, n)} /* #{others[n]} */," }).join("\n")}
107
+ );
108
+ path = #{@name};
109
+ sourceTree = "<group>";
110
+ };
111
+ #{id(:test_group)} /* #{@name}UITests */ = {
112
+ isa = PBXGroup;
113
+ children = (
114
+ #{tests.each_index.map { |n| "\t\t\t\t#{id(:test_file, n)} /* #{tests[n]} */," }.join("\n")}
115
+ );
116
+ path = #{@name}UITests;
117
+ sourceTree = "<group>";
118
+ };
119
+ #{id(:products_group)} /* Products */ = {
120
+ isa = PBXGroup;
121
+ children = (
122
+ #{id(:app_product)} /* #{@name}.app */,
123
+ #{id(:test_product)} /* #{@name}UITests.xctest */,
124
+ );
125
+ name = Products;
126
+ sourceTree = "<group>";
127
+ };
128
+ /* End PBXGroup section */
129
+
130
+ /* Begin PBXNativeTarget section */
131
+ #{id(:app_target)} /* #{@name} */ = {
132
+ isa = PBXNativeTarget;
133
+ buildConfigurationList = #{id(:app_configurations)} /* Build configuration list for PBXNativeTarget "#{@name}" */;
134
+ buildPhases = (
135
+ #{id(:app_sources)} /* Sources */,
136
+ #{id(:frameworks)} /* Frameworks */,
137
+ );
138
+ buildRules = (
139
+ );
140
+ dependencies = (
141
+ );
142
+ name = #{@name};
143
+ packageProductDependencies = (
144
+ #{each_package { |product, _, n| "\t\t\t\t#{id(:package_product, n)} /* #{product} */," }}
145
+ );
146
+ productName = #{@name};
147
+ productReference = #{id(:app_product)} /* #{@name}.app */;
148
+ productType = "com.apple.product-type.application";
149
+ };
150
+ #{id(:test_target)} /* #{@name}UITests */ = {
151
+ isa = PBXNativeTarget;
152
+ buildConfigurationList = #{id(:test_configurations)} /* Build configuration list for PBXNativeTarget "#{@name}UITests" */;
153
+ buildPhases = (
154
+ #{id(:test_sources)} /* Sources */,
155
+ );
156
+ buildRules = (
157
+ );
158
+ dependencies = (
159
+ #{id(:dependency)} /* PBXTargetDependency */,
160
+ );
161
+ name = #{@name}UITests;
162
+ productName = #{@name}UITests;
163
+ productReference = #{id(:test_product)} /* #{@name}UITests.xctest */;
164
+ productType = "com.apple.product-type.bundle.ui-testing";
165
+ };
166
+ /* End PBXNativeTarget section */
167
+
168
+ /* Begin PBXProject section */
169
+ #{id(:project)} /* Project object */ = {
170
+ isa = PBXProject;
171
+ attributes = {
172
+ BuildIndependentTargetsInParallel = 1;
173
+ LastUpgradeCheck = 2720;
174
+ };
175
+ buildConfigurationList = #{id(:project_configurations)} /* Build configuration list for PBXProject "#{@name}" */;
176
+ compatibilityVersion = "Xcode 15.0";
177
+ developmentRegion = en;
178
+ hasScannedForEncodings = 0;
179
+ knownRegions = (
180
+ en,
181
+ Base,
182
+ );
183
+ mainGroup = #{id(:root_group)};
184
+ packageReferences = (
185
+ #{each_package { |_, path, n| %(\t\t\t\t#{id(:package, n)} /* XCLocalSwiftPackageReference "#{path}" */,) }}
186
+ );
187
+ productRefGroup = #{id(:products_group)} /* Products */;
188
+ projectDirPath = "";
189
+ projectRoot = "";
190
+ targets = (
191
+ #{id(:app_target)} /* #{@name} */,
192
+ #{id(:test_target)} /* #{@name}UITests */,
193
+ );
194
+ };
195
+ /* End PBXProject section */
196
+
197
+ /* Begin PBXSourcesBuildPhase section */
198
+ #{id(:app_sources)} /* Sources */ = {
199
+ isa = PBXSourcesBuildPhase;
200
+ buildActionMask = 2147483647;
201
+ files = (
202
+ #{app.each_index.map { |n| "\t\t\t\t#{id(:build, n)} /* #{app[n]} in Sources */," }.join("\n")}
203
+ );
204
+ runOnlyForDeploymentPostprocessing = 0;
205
+ };
206
+ #{id(:test_sources)} /* Sources */ = {
207
+ isa = PBXSourcesBuildPhase;
208
+ buildActionMask = 2147483647;
209
+ files = (
210
+ #{tests.each_index.map { |n| "\t\t\t\t#{id(:test_build, n)} /* #{tests[n]} in Sources */," }.join("\n")}
211
+ );
212
+ runOnlyForDeploymentPostprocessing = 0;
213
+ };
214
+ /* End PBXSourcesBuildPhase section */
215
+
216
+ /* Begin PBXTargetDependency section */
217
+ #{id(:dependency)} /* PBXTargetDependency */ = {
218
+ isa = PBXTargetDependency;
219
+ target = #{id(:app_target)} /* #{@name} */;
220
+ targetProxy = #{id(:proxy)} /* PBXContainerItemProxy */;
221
+ };
222
+ /* End PBXTargetDependency section */
223
+
224
+ /* Begin XCBuildConfiguration section */
225
+ #{configuration(:project_debug, "Debug", project_settings + ['DEBUG_INFORMATION_FORMAT = dwarf', 'ENABLE_TESTABILITY = YES', 'GCC_OPTIMIZATION_LEVEL = 0', 'ONLY_ACTIVE_ARCH = YES', 'SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG', 'SWIFT_OPTIMIZATION_LEVEL = "-Onone"'])}
226
+ #{configuration(:project_release, "Release", project_settings + ['DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"', 'SWIFT_COMPILATION_MODE = wholemodule'])}
227
+ #{configuration(:app_debug, "Debug", app_settings)}
228
+ #{configuration(:app_release, "Release", app_settings)}
229
+ #{configuration(:test_debug, "Debug", test_settings)}
230
+ #{configuration(:test_release, "Release", test_settings)}
231
+ /* End XCBuildConfiguration section */
232
+
233
+ /* Begin XCConfigurationList section */
234
+ #{configuration_list(:project_configurations, %(PBXProject "#{@name}"), :project_debug, :project_release)}
235
+ #{configuration_list(:app_configurations, %(PBXNativeTarget "#{@name}"), :app_debug, :app_release)}
236
+ #{configuration_list(:test_configurations, %(PBXNativeTarget "#{@name}UITests"), :test_debug, :test_release)}
237
+ /* End XCConfigurationList section */
238
+
239
+ /* Begin XCLocalSwiftPackageReference section */
240
+ #{each_package { |_, path, n| %(\t\t#{id(:package, n)} /* XCLocalSwiftPackageReference "#{path}" */ = {\n\t\t\tisa = XCLocalSwiftPackageReference;\n\t\t\trelativePath = #{path};\n\t\t};) }}
241
+ /* End XCLocalSwiftPackageReference section */
242
+
243
+ /* Begin XCSwiftPackageProductDependency section */
244
+ #{each_package { |product, _, n| "\t\t#{id(:package_product, n)} /* #{product} */ = {\n\t\t\tisa = XCSwiftPackageProductDependency;\n\t\t\tproductName = #{product};\n\t\t};" }}
245
+ /* End XCSwiftPackageProductDependency section */
246
+ };
247
+ rootObject = #{id(:project)} /* Project object */;
248
+ }
249
+ PBXPROJ
250
+ end
251
+
252
+ def scheme
253
+ app = buildable(:app_target, "#{@name}.app", @name)
254
+ <<~SCHEME
255
+ <?xml version="1.0" encoding="UTF-8"?>
256
+ <Scheme LastUpgradeVersion = "2720" version = "1.7">
257
+ <BuildAction parallelizeBuildables = "YES" buildImplicitDependencies = "YES">
258
+ <BuildActionEntries>
259
+ <BuildActionEntry buildForTesting = "YES" buildForRunning = "YES" buildForProfiling = "YES" buildForArchiving = "YES" buildForAnalyzing = "YES">
260
+ #{app}
261
+ </BuildActionEntry>
262
+ </BuildActionEntries>
263
+ </BuildAction>
264
+ <TestAction buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES">
265
+ <Testables>
266
+ <TestableReference skipped = "NO">
267
+ #{buildable(:test_target, "#{@name}UITests.xctest", "#{@name}UITests")}
268
+ </TestableReference>
269
+ </Testables>
270
+ </TestAction>
271
+ <LaunchAction buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" launchStyle = "0" useCustomWorkingDirectory = "NO" ignoresPersistentStateOnLaunch = "NO" debugDocumentVersioning = "YES" debugServiceExtension = "internal" allowLocationSimulation = "YES">
272
+ <BuildableProductRunnable runnableDebuggingMode = "0">
273
+ #{app}
274
+ </BuildableProductRunnable>
275
+ </LaunchAction>
276
+ </Scheme>
277
+ SCHEME
278
+ end
279
+
280
+ private
281
+
282
+ KINDS = %i[project root_group app_group test_group products_group app_target test_target app_sources test_sources frameworks
283
+ app_product test_product proxy dependency package package_product package_build
284
+ project_configurations app_configurations test_configurations
285
+ project_debug project_release app_debug app_release test_debug test_release
286
+ file other build test_file test_build].freeze
287
+
288
+ # An object's id: 24 hex digits that say what it is and which one.
289
+ def id(kind, index = 0)
290
+ format("1D%02X%020X", KINDS.index(kind), index)
291
+ end
292
+
293
+ # A line, or a few, for every package the app links.
294
+ def each_package
295
+ @packages.each_with_index.map { |(product, path), n| yield product, path, n }.join("\n")
296
+ end
297
+
298
+ def swift_files(subfolder)
299
+ Dir.children(@folder.join(subfolder)).select { |file| file.end_with?(".swift") }.sort
300
+ rescue Errno::ENOENT
301
+ []
302
+ end
303
+
304
+ def file_reference(id, file, type)
305
+ quoted = file.match?(/\A[\w.]+\z/) ? file : %("#{file}")
306
+ "\t\t#{id} /* #{file} */ = {isa = PBXFileReference; lastKnownFileType = #{type}; path = #{quoted}; sourceTree = \"<group>\"; };"
307
+ end
308
+
309
+ def project_settings
310
+ ["ALWAYS_SEARCH_USER_PATHS = NO", "CLANG_ENABLE_MODULES = YES", "IPHONEOS_DEPLOYMENT_TARGET = 17.0", "SDKROOT = iphoneos", "SWIFT_VERSION = 5.0"]
311
+ end
312
+
313
+ def app_settings
314
+ ["CODE_SIGN_STYLE = Automatic", "CURRENT_PROJECT_VERSION = 1", "GENERATE_INFOPLIST_FILE = YES",
315
+ %(HEADER_SEARCH_PATHS = "$(SRCROOT)/#{@headers}"), "INFOPLIST_FILE = #{@name}/Info.plist",
316
+ "INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES", "INFOPLIST_KEY_UILaunchScreen_Generation = YES",
317
+ "INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait",
318
+ %(LIBRARY_SEARCH_PATHS = "$(SRCROOT)/#{@libraries}/$(PLATFORM_NAME)"), "MARKETING_VERSION = 0.1",
319
+ %(OTHER_LDFLAGS = (\n\t\t\t\t\t"-linstant_record_app",\n\t\t\t\t\t"-lspinel_rt",\n\t\t\t\t)),
320
+ %(PRODUCT_BUNDLE_IDENTIFIER = "#{@bundle_id}"), %(PRODUCT_NAME = "$(TARGET_NAME)"),
321
+ %(SWIFT_OBJC_BRIDGING_HEADER = "#{@name}/#{@name}-Bridging-Header.h"), "TARGETED_DEVICE_FAMILY = 1"]
322
+ end
323
+
324
+ def test_settings
325
+ ["CODE_SIGN_STYLE = Automatic", "GENERATE_INFOPLIST_FILE = YES", %(PRODUCT_BUNDLE_IDENTIFIER = "#{@bundle_id}UITests"),
326
+ %(PRODUCT_NAME = "$(TARGET_NAME)"), "TARGETED_DEVICE_FAMILY = 1", "TEST_TARGET_NAME = #{@name}"]
327
+ end
328
+
329
+ def configuration(kind, name, settings)
330
+ lines = settings.sort.map { |setting| "\t\t\t\t#{setting};" }.join("\n")
331
+ "\t\t#{id(kind)} /* #{name} */ = {\n\t\t\tisa = XCBuildConfiguration;\n\t\t\tbuildSettings = {\n#{lines}\n\t\t\t};\n\t\t\tname = #{name};\n\t\t};"
332
+ end
333
+
334
+ def configuration_list(kind, owner, debug, release)
335
+ "\t\t#{id(kind)} /* Build configuration list for #{owner} */ = {\n\t\t\tisa = XCConfigurationList;\n\t\t\tbuildConfigurations = (\n" \
336
+ "\t\t\t\t#{id(debug)} /* Debug */,\n\t\t\t\t#{id(release)} /* Release */,\n\t\t\t);\n\t\t\tdefaultConfigurationIsVisible = 0;\n\t\t\tdefaultConfigurationName = Debug;\n\t\t};"
337
+ end
338
+
339
+ def buildable(kind, product, target)
340
+ %(<BuildableReference BuildableIdentifier = "primary" BlueprintIdentifier = "#{id(kind)}" BuildableName = "#{product}" BlueprintName = "#{target}" ReferencedContainer = "container:#{@name}.xcodeproj">\n </BuildableReference>)
341
+ end
342
+ end
343
+ end
344
+ end
@@ -0,0 +1,41 @@
1
+ require "instant_record/native/node"
2
+ require "instant_record/native/screen"
3
+ require "instant_record/native/contract"
4
+
5
+ module InstantRecord
6
+ # Screens of a native app, written in Ruby (InstantRecord::Native::Screen).
7
+ module Native
8
+ # The screen a name stands for: "mailbox" is MailboxScreen. The compiled
9
+ # app has no constant lookup, so its build writes this out as a `case`
10
+ # from `class_name`.
11
+ def self.screen(name, params = {})
12
+ found = screens.find { |screen| screen_name(screen) == name }
13
+ raise ArgumentError, "No screen named #{name}" unless found
14
+ found.new(params)
15
+ end
16
+
17
+ def self.screens(base = Screen)
18
+ base.subclasses.flat_map { |screen| [screen] + screens(screen) }
19
+ end
20
+
21
+ def self.screen_name(screen)
22
+ underscore(screen.name.to_s.split("::").last.to_s.delete_suffix("Screen"))
23
+ end
24
+
25
+ # The class a screen's file holds: two_words_screen.rb holds
26
+ # TwoWordsScreen. The compiled build asks this for each file in
27
+ # app/screens (InstantRecord::Compiler::EntryBuilder), so the rule that
28
+ # names a screen is written in this file alone.
29
+ def self.class_name(name)
30
+ camel = name.split("_").map(&:capitalize).join
31
+ unless name.match?(/\A[a-z][a-z0-9_]*\z/) && underscore(camel) == name
32
+ raise ArgumentError, "Name each file in app/screens <name>_screen.rb, in lower case with single underscores between words that start with a letter: #{name}"
33
+ end
34
+ "#{camel}Screen"
35
+ end
36
+
37
+ def self.underscore(camel)
38
+ camel.gsub(/([a-z0-9])([A-Z])/, '\1_\2').downcase
39
+ end
40
+ end
41
+ end
@@ -5,20 +5,92 @@ module InstantRecord
5
5
  self.table_name = "instant_record_outbox"
6
6
 
7
7
  serialize :changes_payload, coder: JSON
8
+ serialize :rollback_payload, coder: JSON
8
9
 
9
- before_create { self.id ||= SecureRandom.uuid }
10
+ before_create { self.id ||= self.class.next_monotonic_id }
11
+ after_destroy { InstantRecord::HLC.delete_mutation_hlc(id) }
10
12
 
11
13
  scope :ordered, -> { order(:created_at, :id) }
12
14
 
15
+ # Class methods rather than parameterized scopes so Roundhouse/Spinel AOT
16
+ # compilation handles them identically to CRuby ActiveRecord.
17
+ def self.matured(now = Time.now.utc)
18
+ where("release_at IS NULL OR release_at <= :now", now: now)
19
+ end
20
+
21
+ def self.held(now = Time.now.utc)
22
+ where("release_at IS NOT NULL AND release_at > :now", now: now)
23
+ end
24
+
25
+ # Lexicographically monotonic IDs using string timestamps + a per-process
26
+ # sequence counter (no 64-bit integer math for wasm32 Spinel), so multiple
27
+ # writes within the same millisecond preserve exact insertion order.
28
+ def self.next_monotonic_id
29
+ stamp = Time.now.utc.strftime("%Y%m%d%H%M%S%6N")
30
+ if @last_id_stamp == stamp
31
+ @last_id_seq = (@last_id_seq || 0) + 1
32
+ else
33
+ @last_id_stamp = stamp
34
+ @last_id_seq = 0
35
+ end
36
+ "#{stamp}-#{format('%04x', @last_id_seq)}-#{SecureRandom.hex(6)}"
37
+ end
38
+
39
+ def held?(now = Time.now.utc)
40
+ !release_at.nil? && release_at > now
41
+ end
42
+
43
+ def matured?(now = Time.now.utc)
44
+ release_at.nil? || release_at <= now
45
+ end
46
+
47
+ def hlc
48
+ InstantRecord::HLC.mutation_hlc(id)
49
+ end
50
+
51
+ def mutator?
52
+ operation == "mutator"
53
+ end
54
+
55
+ def mutator_name
56
+ changes_payload["mutator"] || record_id
57
+ end
58
+
59
+ def mutator_args
60
+ changes_payload["args"] || {}
61
+ end
62
+
63
+ def touched_records
64
+ Array(changes_payload["touched"])
65
+ end
66
+
13
67
  def as_mutation
14
- {
15
- id: id,
16
- record_type: record_type,
17
- record_id: record_id,
18
- operation: operation,
19
- changes: changes_payload,
20
- base_version: base_version
21
- }
68
+ if mutator?
69
+ {
70
+ id: id,
71
+ mutation_id: id,
72
+ operation: "mutator",
73
+ record_type: record_type,
74
+ mutator: mutator_name,
75
+ args: mutator_args
76
+ }
77
+ else
78
+ payload = {
79
+ id: id,
80
+ record_type: record_type,
81
+ record_id: record_id,
82
+ operation: operation,
83
+ changes: changes_payload,
84
+ base_version: base_version
85
+ }
86
+ if has_attribute?(:schema_version) && self[:schema_version].present?
87
+ payload[:schema_version] = self[:schema_version]
88
+ end
89
+ if (stamp = hlc)
90
+ payload[:hlc] = stamp
91
+ end
92
+ payload
93
+ end
22
94
  end
23
95
  end
24
96
  end
@@ -0,0 +1,161 @@
1
+ module InstantRecord
2
+ # Transforms queued client outbox mutations across schema versions during
3
+ # rolling deploys (both N-1 -> N upcasts and N+1 -> N downcasts).
4
+ #
5
+ # Models declare transforms via `upcast_outbox` (see InstantRecord::Syncable),
6
+ # and apps may also register global or type-level transforms via
7
+ # `InstantRecord.upcast_outbox` (useful for record_type renames).
8
+ module OutboxUpcaster
9
+ class << self
10
+ def registry
11
+ @registry ||= []
12
+ end
13
+
14
+ def register(record_type = nil, from: nil, to: nil, operation: nil, &block)
15
+ raise ArgumentError, "upcast_outbox requires a block" unless block
16
+
17
+ registry << {
18
+ record_type: record_type&.to_s,
19
+ from: normalize_version(from),
20
+ to: normalize_version(to),
21
+ operation: normalize_operations(operation),
22
+ transform: block
23
+ }
24
+ nil
25
+ end
26
+
27
+ def clear!
28
+ registry.clear
29
+ end
30
+
31
+ # Apply global upcasters (may rewrite record_type and/or changes).
32
+ # Returns [updated_mutation_hash, updated_changes_hash].
33
+ def apply_global(mutation, changes)
34
+ working_mutation = normalize_mutation(mutation)
35
+ working_changes = stringify_hash(changes)
36
+ version = normalize_version(working_mutation[:schema_version])
37
+
38
+ registry.each do |rule|
39
+ next if rule[:record_type] && rule[:record_type] != working_mutation[:record_type].to_s
40
+ next unless applicable?(rule, working_mutation[:operation], version)
41
+
42
+ working_changes = invoke(rule[:transform], working_changes, working_mutation)
43
+ version = advance_version(rule, version)
44
+ working_mutation[:schema_version] = version if version
45
+ end
46
+
47
+ [working_mutation, working_changes]
48
+ end
49
+
50
+ # Apply model-declared `upcast_outbox` transforms to a changes hash.
51
+ def apply_for_model(model, changes, mutation = {})
52
+ return stringify_hash(changes) unless model.respond_to?(:instant_record_outbox_upcasters)
53
+
54
+ rules = model.instant_record_outbox_upcasters
55
+ return stringify_hash(changes) if rules.empty?
56
+
57
+ working_mutation = normalize_mutation(mutation)
58
+ working_changes = stringify_hash(changes)
59
+ version = normalize_version(working_mutation[:schema_version])
60
+
61
+ rules.each do |raw_rule|
62
+ rule = normalize_rule(raw_rule)
63
+ next unless applicable?(rule, working_mutation[:operation], version)
64
+
65
+ working_changes = invoke(rule[:transform], working_changes, working_mutation)
66
+ version = advance_version(rule, version)
67
+ working_mutation[:schema_version] = version if version
68
+ end
69
+
70
+ working_changes
71
+ end
72
+
73
+ private
74
+
75
+ def normalize_mutation(mutation)
76
+ hash = mutation.respond_to?(:to_h) ? mutation.to_h : {}
77
+ hash.transform_keys(&:to_sym)
78
+ end
79
+
80
+ def stringify_hash(hash)
81
+ (hash || {}).to_h.transform_keys(&:to_s)
82
+ end
83
+
84
+ def normalize_version(value)
85
+ return nil if value.nil? || value.to_s.strip.empty?
86
+
87
+ Integer(value.to_s, 10)
88
+ end
89
+
90
+ def normalize_operations(operation)
91
+ return nil if operation.nil?
92
+
93
+ Array(operation).map(&:to_s)
94
+ end
95
+
96
+ def normalize_rule(rule)
97
+ {
98
+ from: normalize_version(rule[:from]),
99
+ to: normalize_version(rule[:to]),
100
+ operation: normalize_operations(rule[:operation]),
101
+ transform: rule[:transform]
102
+ }
103
+ end
104
+
105
+ def applicable?(rule, operation, version)
106
+ if rule[:operation] && !rule[:operation].include?(operation.to_s)
107
+ return false
108
+ end
109
+
110
+ from = rule[:from]
111
+ to = rule[:to]
112
+ return true if from.nil? && to.nil?
113
+ # Legacy/N-1 clients that predate schema_version tagging still run
114
+ # versioned upcasters so their queued mutations are rescued.
115
+ return true if version.nil?
116
+
117
+ if from && to
118
+ if from < to
119
+ version < to
120
+ elsif from > to
121
+ version > to
122
+ else
123
+ version == from
124
+ end
125
+ elsif to
126
+ version < to
127
+ else
128
+ version == from
129
+ end
130
+ end
131
+
132
+ def advance_version(rule, version)
133
+ return nil if version.nil?
134
+
135
+ rule[:to] || version
136
+ end
137
+
138
+ def invoke(transform, changes, mutation)
139
+ working = changes.dup
140
+ result =
141
+ case transform.arity
142
+ when 0 then transform.call
143
+ when 1 then transform.call(working)
144
+ else transform.call(working, mutation)
145
+ end
146
+ (result.is_a?(Hash) ? result : working).transform_keys(&:to_s)
147
+ end
148
+ end
149
+ end
150
+
151
+ class << self
152
+ # Register a global (or record_type-scoped) outbox upcaster.
153
+ def upcast_outbox(record_type = nil, from: nil, to: nil, operation: nil, &block)
154
+ OutboxUpcaster.register(record_type, from: from, to: to, operation: operation, &block)
155
+ end
156
+
157
+ def clear_outbox_upcasters!
158
+ OutboxUpcaster.clear!
159
+ end
160
+ end
161
+ end