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,770 @@
1
+ require "pathname"
2
+ require "prism"
3
+ require "set"
4
+ require_relative "ast_lowerer"
5
+ require_relative "runner"
6
+
7
+ module InstantRecord
8
+ class Compiler
9
+ # Fast pre-Spinel Prism static AOT / 32-bit linter.
10
+ #
11
+ # Catches the three silent/slow AOT failure classes before Roundhouse or
12
+ # Spinel run (and in `instant_record:doctor`):
13
+ #
14
+ # 1. `integer_overflow`: integer literals and constant expressions exceeding
15
+ # signed 32-bit / 31-bit-magnitude range (-2_147_483_648..2_147_483_647),
16
+ # which raise RangeError under `-DSP_INT_OVERFLOW_MODE_RAISE`.
17
+ # 2. `untyped_screen_hash`: untyped empty `{}` (or `Hash.new`) passed to
18
+ # `params:`, `args:`, or `props:` (or defaulted/assigned there) inside
19
+ # `InstantRecord::Native::Screen` subclasses / `app/screens/`.
20
+ # 3. `server_only_constant`: references from shared / `browser_only` /
21
+ # `client_only` code to constants defined only inside `server_only`
22
+ # blocks (or server-only gem constants).
23
+ class StaticLinter
24
+ INT32_MAX = (1 << 31) - 1 # 2_147_483_647 (31 magnitude bits)
25
+ INT32_MIN = -(1 << 31) # -2_147_483_648
26
+ INT32_RANGE = (INT32_MIN..INT32_MAX)
27
+
28
+ SCREEN_HASH_KEYS = %i[params args props].freeze
29
+ SCREEN_HASH_SETTERS = %i[link_params= args= props=].freeze
30
+ SCREEN_HASH_IVARS = %i[@link_params @args @props].freeze
31
+
32
+ BYTE_MULTIPLIERS = {
33
+ kilobyte: 1024, kilobytes: 1024, kb: 1024,
34
+ megabyte: 1024**2, megabytes: 1024**2, mb: 1024**2,
35
+ gigabyte: 1024**3, gigabytes: 1024**3, gb: 1024**3,
36
+ terabyte: 1024**4, terabytes: 1024**4, tb: 1024**4,
37
+ petabyte: 1024**5, petabytes: 1024**5, pb: 1024**5
38
+ }.freeze
39
+
40
+ SERVER_ONLY_GEM_CONSTANTS = Set.new(%w[
41
+ InstantRecord::Engine
42
+ InstantRecord::Files
43
+ InstantRecord::FileStore
44
+ InstantRecord::MutationApplier
45
+ InstantRecord::AppliedMutation
46
+ InstantRecord::SyncScope
47
+ InstantRecord::Inertia::Renderer
48
+ InstantRecord::CompiledSite
49
+ InstantRecord::Compiler
50
+ ]).freeze
51
+
52
+ NON_COMPILED_PREFIXES = %w[
53
+ db/
54
+ test/
55
+ spec/
56
+ tmp/
57
+ vendor/
58
+ node_modules/
59
+ lib/tasks/
60
+ lib/generators/
61
+ ].freeze
62
+
63
+ NON_COMPILED_FILES = Set.new(%w[
64
+ Gemfile
65
+ Rakefile
66
+ ]).freeze
67
+
68
+ Diagnostic = Struct.new(:rule, :file, :line, :column, :message, keyword_init: true) do
69
+ def to_s
70
+ "#{file}:#{line}:#{column} [#{rule}] #{message}"
71
+ end
72
+ end
73
+
74
+ Result = Struct.new(:files_checked, :diagnostics, keyword_init: true) do
75
+ def ok? = diagnostics.empty?
76
+
77
+ def to_s
78
+ return "Static AOT lint passed (#{files_checked} files checked)." if ok?
79
+
80
+ lines = ["Static AOT lint failed with #{diagnostics.size} issue(s) across #{files_checked} files:"]
81
+ diagnostics.each { |d| lines << " #{d}" }
82
+ lines.join("\n")
83
+ end
84
+ end
85
+
86
+ def self.lint(root, staged: false, server_only_constants: nil, exclude: [], only_files: nil)
87
+ new(root: root, staged: staged, server_only_constants: server_only_constants, exclude: exclude, only_files: only_files).run
88
+ end
89
+
90
+ def self.check!(root, staged: true, server_only_constants: nil, exclude: [], only_files: nil)
91
+ result = lint(root, staged: staged, server_only_constants: server_only_constants, exclude: exclude, only_files: only_files)
92
+ raise BuildError, result.to_s unless result.ok?
93
+
94
+ result
95
+ end
96
+
97
+ # Collects constants defined strictly inside `server_only` blocks across a
98
+ # directory tree before AstLowerer strips those blocks.
99
+ def self.collect_server_only_constants(root, staged: true, exclude: [], only_files: nil)
100
+ linter = new(root: root, staged: staged, exclude: exclude, only_files: only_files)
101
+ files = linter.discover_files
102
+ linter.scan_constant_definitions(files)
103
+ end
104
+
105
+ def initialize(root:, staged: false, server_only_constants: nil, exclude: [], only_files: nil)
106
+ @root = File.expand_path(root.to_s)
107
+ @staged = staged
108
+ @precollected_server_only = server_only_constants
109
+ @exclude = Array(exclude).map { |entry| entry.to_s.sub(%r{\A/+}, "").sub(%r{/+\z}, "") }
110
+ @only_files = only_files ? Set.new(Array(only_files).map { |entry| entry.to_s.sub(%r{\A/+}, "") }) : nil
111
+ end
112
+
113
+ def run
114
+ files = discover_files
115
+ parsed_files = files.map { |path| [path, relative_path(path), Prism.parse_file(path)] }
116
+
117
+ server_only_defs, client_defs =
118
+ if @precollected_server_only
119
+ [@precollected_server_only, collect_defs_from_parsed(parsed_files).last]
120
+ else
121
+ collect_defs_from_parsed(parsed_files)
122
+ end
123
+
124
+ effective_server_only = {}
125
+ server_only_defs.each do |name, loc|
126
+ effective_server_only[name] = loc unless client_defs.include?(name)
127
+ end
128
+ SERVER_ONLY_GEM_CONSTANTS.each do |name|
129
+ effective_server_only[name] ||= nil unless client_defs.include?(name)
130
+ end
131
+
132
+ diagnostics = []
133
+ parsed_files.each do |_abs, rel, parsed|
134
+ next unless parsed.success?
135
+
136
+ lint_file(rel, parsed.value, effective_server_only, diagnostics)
137
+ end
138
+
139
+ diagnostics.sort_by! { |d| [d.file, d.line, d.column, d.rule.to_s] }
140
+ Result.new(files_checked: parsed_files.size, diagnostics: diagnostics)
141
+ end
142
+
143
+ def discover_files
144
+ root_path = Pathname(@root)
145
+ return [] unless root_path.directory?
146
+
147
+ if @only_files
148
+ return @only_files.to_a.sort
149
+ .reject { |rel| excluded_custom?(rel) }
150
+ .map { |rel| File.join(@root, rel) }
151
+ .select { |path| File.file?(path) }
152
+ end
153
+
154
+ if @staged
155
+ candidates = Dir.glob("**/*.rb", base: @root).sort
156
+ return candidates.reject { |rel| excluded_staged?(rel) || excluded_custom?(rel) }.map { |rel| File.join(@root, rel) }
157
+ end
158
+
159
+ # Raw Rails app root (used by `instant_record:doctor`): scan the folders
160
+ # that actually enter compilation, or fall back to flat `**/*.rb` for
161
+ # standalone fixture directories.
162
+ app_subfolders = %w[app/models app/controllers app/helpers app/screens config/routes]
163
+ app_files = %w[config/application.rb config/routes.rb config/importmap.rb]
164
+ has_rails_layout = app_subfolders.any? { |dir| File.directory?(File.join(@root, dir)) } ||
165
+ app_files.any? { |file| File.file?(File.join(@root, file)) }
166
+
167
+ if has_rails_layout
168
+ found = []
169
+ app_subfolders.each do |folder|
170
+ dir = File.join(@root, folder)
171
+ next unless File.directory?(dir)
172
+
173
+ Dir.glob("**/*.rb", base: dir).sort.each { |rel| found << File.join(dir, rel) }
174
+ end
175
+ app_files.each do |file|
176
+ full = File.join(@root, file)
177
+ found << full if File.file?(full)
178
+ end
179
+ found.uniq.reject { |path| excluded_custom?(relative_path(path)) }.sort
180
+ else
181
+ Dir.glob("**/*.rb", base: @root).sort
182
+ .reject { |rel| excluded_staged?(rel) || excluded_custom?(rel) }
183
+ .map { |rel| File.join(@root, rel) }
184
+ end
185
+ end
186
+
187
+ def scan_constant_definitions(files)
188
+ parsed_files = files.map { |path| [path, relative_path(path), Prism.parse_file(path)] }
189
+ server_only_defs, client_defs = collect_defs_from_parsed(parsed_files)
190
+ server_only_defs.reject { |name, _| client_defs.include?(name) }
191
+ end
192
+
193
+ private
194
+
195
+ def relative_path(path)
196
+ Pathname(path).relative_path_from(Pathname(@root)).to_s
197
+ end
198
+
199
+ def excluded_staged?(rel)
200
+ return true if NON_COMPILED_FILES.include?(rel)
201
+
202
+ NON_COMPILED_PREFIXES.any? { |prefix| rel.start_with?(prefix) }
203
+ end
204
+
205
+ def excluded_custom?(rel)
206
+ @exclude.any? { |entry| rel == entry || rel.start_with?("#{entry}/") }
207
+ end
208
+
209
+ def collect_defs_from_parsed(parsed_files)
210
+ server_only_defs = {}
211
+ client_defs = Set.new
212
+
213
+ parsed_files.each do |_abs, rel, parsed|
214
+ next unless parsed.success?
215
+
216
+ walk_defs(parsed.value, rel, [], in_server_only: false, server_only_defs: server_only_defs, client_defs: client_defs)
217
+ end
218
+
219
+ [server_only_defs, client_defs]
220
+ end
221
+
222
+ def walk_defs(node, rel, namespace, in_server_only:, server_only_defs:, client_defs:)
223
+ return if node.nil?
224
+
225
+ case node
226
+ when Prism::CallNode
227
+ if server_only_call?(node)
228
+ walk_defs(node.block&.body, rel, namespace, in_server_only: true, server_only_defs: server_only_defs, client_defs: client_defs)
229
+ return
230
+ end
231
+ when Prism::ClassNode, Prism::ModuleNode
232
+ full_path = qualify_definition(node.constant_path, namespace)
233
+ if full_path
234
+ record_const_def(full_path, rel, node.location.start_line, in_server_only, server_only_defs, client_defs)
235
+ next_ns = full_path.split("::")
236
+ else
237
+ next_ns = namespace
238
+ end
239
+ walk_defs(node.body, rel, next_ns, in_server_only: in_server_only, server_only_defs: server_only_defs, client_defs: client_defs)
240
+ return
241
+ when Prism::ConstantWriteNode, Prism::ConstantOrWriteNode, Prism::ConstantAndWriteNode, Prism::ConstantOperatorWriteNode
242
+ full_path = (namespace + [node.name.to_s]).join("::")
243
+ record_const_def(full_path, rel, node.location.start_line, in_server_only, server_only_defs, client_defs)
244
+ when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathAndWriteNode, Prism::ConstantPathOperatorWriteNode
245
+ full_path = qualify_definition(node.target, namespace)
246
+ record_const_def(full_path, rel, node.location.start_line, in_server_only, server_only_defs, client_defs) if full_path
247
+ end
248
+
249
+ node.compact_child_nodes.each do |child|
250
+ walk_defs(child, rel, namespace, in_server_only: in_server_only, server_only_defs: server_only_defs, client_defs: client_defs)
251
+ end
252
+ end
253
+
254
+ def record_const_def(full_path, rel, line, in_server_only, server_only_defs, client_defs)
255
+ if in_server_only
256
+ server_only_defs[full_path] ||= "#{rel}:#{line}"
257
+ else
258
+ client_defs.add(full_path)
259
+ end
260
+ end
261
+
262
+ def qualify_definition(path_node, namespace)
263
+ name = AstLowerer.constant_name(path_node)
264
+ return nil unless name
265
+
266
+ if name.start_with?("::")
267
+ name.delete_prefix("::")
268
+ elsif namespace.empty?
269
+ name
270
+ else
271
+ (namespace + [name]).join("::")
272
+ end
273
+ end
274
+
275
+ def server_only_call?(node)
276
+ node.is_a?(Prism::CallNode) &&
277
+ node.name == :server_only &&
278
+ (node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode)) &&
279
+ node.arguments.nil? &&
280
+ node.block.is_a?(Prism::BlockNode)
281
+ end
282
+
283
+ def lint_file(rel, root, effective_server_only, diagnostics)
284
+ in_screen_file = rel.start_with?("app/screens/") ||
285
+ rel == "lib/instant_record/native/screen.rb" ||
286
+ rel == "lib/instant_record/native/node.rb"
287
+ state = {
288
+ rel: rel,
289
+ namespace: [],
290
+ in_screen: in_screen_file,
291
+ const_ints: {},
292
+ empty_hash_helpers: Set.new,
293
+ empty_hash_locals: Set.new,
294
+ reported_int_nodes: Set.new,
295
+ server_only: effective_server_only,
296
+ diagnostics: diagnostics
297
+ }
298
+ walk_lint(root, state)
299
+ end
300
+
301
+ def walk_lint(node, state)
302
+ return if node.nil?
303
+
304
+ case node
305
+ when Prism::DefinedNode
306
+ # `defined?(ServerOnlyConst)` is safe and never raises NameError.
307
+ return
308
+ when Prism::CallNode
309
+ return if server_only_call?(node)
310
+
311
+ # Check constant arithmetic expression overflow first (before recursing)
312
+ if constant_int_expr?(node)
313
+ check_int_expression(node, state)
314
+ end
315
+
316
+ check_screen_call(node, state) if state[:in_screen]
317
+
318
+ # Recurse into receiver, arguments, block
319
+ walk_lint(node.receiver, state)
320
+ walk_lint(node.arguments, state)
321
+ walk_lint(node.block, state)
322
+ return
323
+ when Prism::IntegerNode
324
+ check_int_expression(node, state)
325
+ return
326
+ when Prism::IfNode
327
+ pred = browser_predicate(node.predicate)
328
+ if pred == :true
329
+ walk_lint(node.statements, state)
330
+ return
331
+ elsif pred == :false
332
+ walk_lint(node.subsequent, state)
333
+ return
334
+ end
335
+ when Prism::UnlessNode
336
+ pred = browser_predicate(node.predicate)
337
+ if pred == :true
338
+ walk_lint(node.else_clause, state)
339
+ return
340
+ elsif pred == :false
341
+ walk_lint(node.statements, state)
342
+ return
343
+ end
344
+ when Prism::ClassNode
345
+ walk_lint(node.superclass, state)
346
+ full_path = qualify_definition(node.constant_path, state[:namespace])
347
+ prev_ns = state[:namespace]
348
+ prev_screen = state[:in_screen]
349
+ prev_helpers = state[:empty_hash_helpers]
350
+
351
+ state[:namespace] = full_path ? full_path.split("::") : prev_ns
352
+ state[:in_screen] = prev_screen || screen_superclass?(node.superclass)
353
+ state[:empty_hash_helpers] = state[:in_screen] ? collect_empty_hash_helpers(node.body) : Set.new
354
+
355
+ walk_lint(node.body, state)
356
+ state[:namespace] = prev_ns
357
+ state[:in_screen] = prev_screen
358
+ state[:empty_hash_helpers] = prev_helpers
359
+ return
360
+ when Prism::ModuleNode
361
+ full_path = qualify_definition(node.constant_path, state[:namespace])
362
+ prev_ns = state[:namespace]
363
+ state[:namespace] = full_path ? full_path.split("::") : prev_ns
364
+ walk_lint(node.body, state)
365
+ state[:namespace] = prev_ns
366
+ return
367
+ when Prism::DefNode
368
+ prev_locals = state[:empty_hash_locals]
369
+ state[:empty_hash_locals] = Set.new
370
+ check_screen_def_params(node, state) if state[:in_screen]
371
+ walk_lint(node.parameters, state)
372
+ walk_lint(node.body, state)
373
+ state[:empty_hash_locals] = prev_locals
374
+ return
375
+ when Prism::LocalVariableWriteNode
376
+ if state[:in_screen]
377
+ if empty_hash_literal?(node.value)
378
+ state[:empty_hash_locals].add(node.name)
379
+ else
380
+ state[:empty_hash_locals].delete(node.name)
381
+ end
382
+ end
383
+ when Prism::InstanceVariableWriteNode
384
+ if state[:in_screen] && SCREEN_HASH_IVARS.include?(node.name) && empty_hash_literal?(node.value)
385
+ report_untyped_hash(node, "#{node.name} = {}", state)
386
+ end
387
+ when Prism::ConstantWriteNode
388
+ val = eval_const_int(node.value, state[:const_ints], state[:namespace])
389
+ if val.is_a?(Integer) && INT32_RANGE.cover?(val)
390
+ full_path = (state[:namespace] + [node.name.to_s]).join("::")
391
+ state[:const_ints][full_path] = val
392
+ end
393
+ walk_lint(node.value, state)
394
+ return
395
+ when Prism::ConstantOrWriteNode, Prism::ConstantAndWriteNode, Prism::ConstantOperatorWriteNode
396
+ walk_lint(node.value, state)
397
+ return
398
+ when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathAndWriteNode, Prism::ConstantPathOperatorWriteNode
399
+ # Check the parent namespace of `Foo::BAR = 1` in case `Foo` is server_only,
400
+ # without flagging `BAR` itself as a read.
401
+ walk_lint(node.target.parent, state) if node.target.is_a?(Prism::ConstantPathNode) && node.target.parent
402
+ walk_lint(node.value, state)
403
+ return
404
+ when Prism::ConstantPathNode
405
+ check_server_only_const_ref(node, state)
406
+ # If the path is dynamic (e.g. `obj::FOO`), still lint the receiver expression.
407
+ if AstLowerer.constant_name(node).nil?
408
+ walk_lint(node.parent, state)
409
+ end
410
+ return
411
+ when Prism::ConstantReadNode
412
+ check_server_only_const_ref(node, state)
413
+ return
414
+ end
415
+
416
+ node.compact_child_nodes.each { |child| walk_lint(child, state) }
417
+ end
418
+
419
+ # --- Rule 1: >31-bit integer literals & constant expressions ---
420
+
421
+ def constant_int_expr?(node)
422
+ case node
423
+ when Prism::IntegerNode
424
+ true
425
+ when Prism::ParenthesesNode
426
+ body = node.body&.body
427
+ body&.length == 1 && constant_int_expr?(body.first)
428
+ when Prism::CallNode
429
+ if %i[-@ +@ ~].include?(node.name) && node.arguments.nil? && node.block.nil?
430
+ constant_int_expr?(node.receiver)
431
+ elsif BYTE_MULTIPLIERS.key?(node.name) && node.arguments.nil? && node.block.nil?
432
+ constant_int_expr?(node.receiver)
433
+ elsif %i[+ - * / % ** << >> & | ^].include?(node.name) && node.block.nil?
434
+ args = node.arguments&.arguments
435
+ args&.length == 1 && constant_int_expr?(node.receiver) && constant_int_expr?(args.first)
436
+ else
437
+ false
438
+ end
439
+ when Prism::ConstantReadNode, Prism::ConstantPathNode
440
+ true
441
+ else
442
+ false
443
+ end
444
+ end
445
+
446
+ def check_int_expression(node, state)
447
+ return if state[:reported_int_nodes].include?(node.object_id)
448
+
449
+ val = eval_const_int(node, state[:const_ints], state[:namespace])
450
+ return if val.nil?
451
+
452
+ if val == :overflow || (val.is_a?(Integer) && !INT32_RANGE.cover?(val))
453
+ # If an inner child already overflows, let recursion report the tightest offending node
454
+ # unless the current node itself is an IntegerNode or the overflow happens at this op.
455
+ if !node.is_a?(Prism::IntegerNode) && any_child_overflows?(node, state[:const_ints], state[:namespace])
456
+ return
457
+ end
458
+
459
+ mark_subtree_reported(node, state[:reported_int_nodes])
460
+ detail = val == :overflow ? "exceeds 32-bit signed integer range" : "evaluates to #{val}, outside 32-bit signed integer range (#{INT32_MIN}..#{INT32_MAX})"
461
+ add_diagnostic(
462
+ state,
463
+ rule: :integer_overflow,
464
+ node: node,
465
+ message: "Integer #{node.slice.inspect} #{detail}; compiled wasm32/Spinel builds raise RangeError on >31-bit values."
466
+ )
467
+ end
468
+ end
469
+
470
+ def any_child_overflows?(node, const_ints, namespace)
471
+ children =
472
+ case node
473
+ when Prism::CallNode
474
+ [node.receiver, *Array(node.arguments&.arguments)].compact
475
+ when Prism::ParenthesesNode
476
+ Array(node.body&.body)
477
+ else
478
+ []
479
+ end
480
+ children.any? do |child|
481
+ val = eval_const_int(child, const_ints, namespace)
482
+ val == :overflow || (val.is_a?(Integer) && !INT32_RANGE.cover?(val))
483
+ end
484
+ end
485
+
486
+ def mark_subtree_reported(node, set)
487
+ return if node.nil?
488
+
489
+ set.add(node.object_id)
490
+ node.compact_child_nodes.each { |child| mark_subtree_reported(child, set) }
491
+ end
492
+
493
+ def eval_const_int(node, const_ints, namespace)
494
+ case node
495
+ when Prism::IntegerNode
496
+ node.value
497
+ when Prism::ParenthesesNode
498
+ body = node.body&.body
499
+ body&.length == 1 ? eval_const_int(body.first, const_ints, namespace) : nil
500
+ when Prism::ConstantReadNode, Prism::ConstantPathNode
501
+ resolve_const_int(node, const_ints, namespace)
502
+ when Prism::CallNode
503
+ return nil if node.block
504
+
505
+ if node.arguments.nil?
506
+ lhs = eval_const_int(node.receiver, const_ints, namespace)
507
+ return :overflow if lhs == :overflow
508
+ return nil unless lhs.is_a?(Integer)
509
+
510
+ case node.name
511
+ when :-@ then -lhs
512
+ when :+@ then +lhs
513
+ when :~ then ~lhs
514
+ else
515
+ mult = BYTE_MULTIPLIERS[node.name]
516
+ mult ? lhs * mult : nil
517
+ end
518
+ elsif node.arguments.arguments.length == 1
519
+ lhs = eval_const_int(node.receiver, const_ints, namespace)
520
+ rhs = eval_const_int(node.arguments.arguments.first, const_ints, namespace)
521
+ return :overflow if lhs == :overflow || rhs == :overflow
522
+ return nil unless lhs.is_a?(Integer) && rhs.is_a?(Integer)
523
+
524
+ case node.name
525
+ when :+ then lhs + rhs
526
+ when :- then lhs - rhs
527
+ when :* then lhs * rhs
528
+ when :/ then rhs == 0 ? nil : lhs / rhs
529
+ when :% then rhs == 0 ? nil : lhs % rhs
530
+ when :& then lhs & rhs
531
+ when :| then lhs | rhs
532
+ when :^ then lhs ^ rhs
533
+ when :**
534
+ return nil if rhs < 0
535
+ return :overflow if lhs.abs >= 2 && rhs >= 32
536
+
537
+ lhs**rhs
538
+ when :<<
539
+ return nil if rhs < 0
540
+ return :overflow if lhs != 0 && rhs >= 32
541
+
542
+ lhs << rhs
543
+ when :>>
544
+ return nil if rhs < 0
545
+
546
+ lhs >> rhs
547
+ end
548
+ end
549
+ end
550
+ end
551
+
552
+ def resolve_const_int(node, const_ints, namespace)
553
+ raw = AstLowerer.constant_name(node)
554
+ return nil unless raw
555
+
556
+ candidates_for(raw, namespace).each do |full|
557
+ return const_ints[full] if const_ints.key?(full)
558
+ end
559
+ nil
560
+ end
561
+
562
+ # --- Rule 2: Untyped `{}` hashes in Native::Screen params:/args:/props: ---
563
+
564
+ def screen_superclass?(superclass)
565
+ name = AstLowerer.constant_name(superclass)
566
+ return false unless name
567
+
568
+ clean = name.delete_prefix("::")
569
+ clean == "InstantRecord::Native::Screen" || clean == "Native::Screen" || clean == "Screen"
570
+ end
571
+
572
+ def collect_empty_hash_helpers(body)
573
+ helpers = Set.new
574
+ return helpers unless body
575
+
576
+ Array(body.body).each do |stmt|
577
+ next unless stmt.is_a?(Prism::DefNode) && stmt.receiver.nil?
578
+ next if stmt.parameters
579
+
580
+ def_stmts = stmt.body&.body
581
+ if def_stmts&.length == 1 && empty_hash_literal?(def_stmts.first)
582
+ helpers.add(stmt.name)
583
+ end
584
+ end
585
+ helpers
586
+ end
587
+
588
+ def check_screen_def_params(def_node, state)
589
+ params = def_node.parameters
590
+ return unless params
591
+
592
+ Array(params.optionals).each do |opt|
593
+ if SCREEN_HASH_KEYS.include?(opt.name) && empty_hash_literal?(opt.value)
594
+ report_untyped_hash(opt, "#{opt.name} = {}", state)
595
+ end
596
+ end
597
+ Array(params.keywords).each do |kw|
598
+ next unless kw.is_a?(Prism::OptionalKeywordParameterNode)
599
+
600
+ if SCREEN_HASH_KEYS.include?(kw.name) && empty_hash_literal?(kw.value)
601
+ report_untyped_hash(kw, "#{kw.name}: {}", state)
602
+ end
603
+ end
604
+ end
605
+
606
+ def check_screen_call(call_node, state)
607
+ # Track mutations on local hash variables (e.g. `h["k"] = "v"` makes it typed)
608
+ if %i[[]= store merge! update].include?(call_node.name) && call_node.receiver.is_a?(Prism::LocalVariableReadNode)
609
+ state[:empty_hash_locals].delete(call_node.receiver.name)
610
+ end
611
+
612
+ # `node.link_params = {}` / `node.args = {}` / `node.props = {}`
613
+ if SCREEN_HASH_SETTERS.include?(call_node.name)
614
+ val = call_node.arguments&.arguments&.first
615
+ if val && untyped_hash_expr?(val, state)
616
+ report_untyped_hash(val, "#{call_node.name} {}", state)
617
+ end
618
+ return
619
+ end
620
+
621
+ # Keyword arguments `params: {}`, `args: {}`, `props: {}`
622
+ Array(call_node.arguments&.arguments).each do |arg|
623
+ next unless arg.is_a?(Prism::KeywordHashNode)
624
+
625
+ arg.elements.each do |assoc|
626
+ next unless assoc.is_a?(Prism::AssocNode) && assoc.key.is_a?(Prism::SymbolNode)
627
+
628
+ key = assoc.key.unescaped.to_sym
629
+ next unless SCREEN_HASH_KEYS.include?(key)
630
+
631
+ if untyped_hash_expr?(assoc.value, state)
632
+ report_untyped_hash(assoc.value, "#{key}: #{assoc.value.slice}", state)
633
+ end
634
+ end
635
+ end
636
+ end
637
+
638
+ def empty_hash_literal?(node)
639
+ case node
640
+ when Prism::HashNode
641
+ node.elements.empty?
642
+ when Prism::CallNode
643
+ node.name == :new &&
644
+ %w[Hash ::Hash].include?(AstLowerer.constant_name(node.receiver)) &&
645
+ node.arguments.nil? &&
646
+ node.block.nil?
647
+ when Prism::ParenthesesNode
648
+ body = node.body&.body
649
+ body&.length == 1 && empty_hash_literal?(body.first)
650
+ else
651
+ false
652
+ end
653
+ end
654
+
655
+ def untyped_hash_expr?(node, state)
656
+ return true if empty_hash_literal?(node)
657
+
658
+ case node
659
+ when Prism::LocalVariableReadNode
660
+ state[:empty_hash_locals].include?(node.name)
661
+ when Prism::CallNode
662
+ node.receiver.nil? && node.arguments.nil? && node.block.nil? &&
663
+ state[:empty_hash_helpers].include?(node.name)
664
+ when Prism::IfNode
665
+ branch_has_empty_hash?(node.statements, state) ||
666
+ (node.subsequent.is_a?(Prism::ElseNode) && branch_has_empty_hash?(node.subsequent.statements, state)) ||
667
+ (node.subsequent.is_a?(Prism::IfNode) && untyped_hash_expr?(node.subsequent, state))
668
+ when Prism::UnlessNode
669
+ branch_has_empty_hash?(node.statements, state) ||
670
+ (node.else_clause && branch_has_empty_hash?(node.else_clause.statements, state))
671
+ when Prism::ParenthesesNode
672
+ body = node.body&.body
673
+ body&.length == 1 && untyped_hash_expr?(body.first, state)
674
+ else
675
+ false
676
+ end
677
+ end
678
+
679
+ def branch_has_empty_hash?(stmts, state)
680
+ return false unless stmts && stmts.body.any?
681
+
682
+ untyped_hash_expr?(stmts.body.last, state)
683
+ end
684
+
685
+ def report_untyped_hash(node, snippet, state)
686
+ add_diagnostic(
687
+ state,
688
+ rule: :untyped_screen_hash,
689
+ node: node,
690
+ message: "Untyped empty hash in Native::Screen (#{snippet}); use InstantRecord::Native::Node.strings so Spinel types it as Hash[String, String]."
691
+ )
692
+ end
693
+
694
+ # --- Rule 3: server_only constant references from shared code ---
695
+
696
+ def check_server_only_const_ref(node, state)
697
+ raw = AstLowerer.constant_name(node)
698
+ return unless raw
699
+
700
+ candidates_for(raw, state[:namespace]).each do |full|
701
+ matched = matching_server_only_constant(full, state[:server_only])
702
+ next unless matched
703
+
704
+ defined_at = state[:server_only][matched]
705
+ origin = defined_at ? " (defined inside server_only at #{defined_at})" : " (server-only gem constant)"
706
+ add_diagnostic(
707
+ state,
708
+ rule: :server_only_constant,
709
+ node: node,
710
+ message: "Shared/client code references server_only constant `#{matched}`#{origin}; wrap in `server_only` or define a client counterpart."
711
+ )
712
+ return
713
+ end
714
+ end
715
+
716
+ def matching_server_only_constant(full_name, server_only)
717
+ return full_name if server_only.key?(full_name)
718
+
719
+ # Also catch `ServerOnlyClass::SUB` when `ServerOnlyClass` itself is server_only.
720
+ parts = full_name.split("::")
721
+ (parts.length - 1).downto(1) do |len|
722
+ prefix = parts[0, len].join("::")
723
+ return prefix if server_only.key?(prefix)
724
+ end
725
+ nil
726
+ end
727
+
728
+ def candidates_for(raw, namespace)
729
+ if raw.start_with?("::")
730
+ [raw.delete_prefix("::")]
731
+ else
732
+ list = []
733
+ namespace.length.downto(0) do |depth|
734
+ prefix = namespace[0, depth]
735
+ list << (prefix.empty? ? raw : (prefix + [raw]).join("::"))
736
+ end
737
+ list
738
+ end
739
+ end
740
+
741
+ def browser_predicate(node)
742
+ case node
743
+ when Prism::ParenthesesNode
744
+ body = node.body&.body
745
+ body&.length == 1 ? browser_predicate(body.first) : nil
746
+ when Prism::CallNode
747
+ if node.name == :browser? && node.arguments.nil? && node.block.nil?
748
+ recv = AstLowerer.constant_name(node.receiver)
749
+ return :true if recv == "InstantRecord" || recv == "::InstantRecord"
750
+ elsif node.name == :! && node.arguments.nil? && node.block.nil?
751
+ inner = browser_predicate(node.receiver)
752
+ return inner == :true ? :false : :true if inner
753
+ end
754
+ nil
755
+ end
756
+ end
757
+
758
+ def add_diagnostic(state, rule:, node:, message:)
759
+ loc = node.location
760
+ state[:diagnostics] << Diagnostic.new(
761
+ rule: rule,
762
+ file: state[:rel],
763
+ line: loc.start_line,
764
+ column: loc.start_column + 1,
765
+ message: message
766
+ )
767
+ end
768
+ end
769
+ end
770
+ end