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,623 @@
1
+ require "active_support/inflector"
2
+ require "prism"
3
+ require "set"
4
+ require_relative "manifest"
5
+ require_relative "runner"
6
+
7
+ module InstantRecord
8
+ class Compiler
9
+ # Static Prism parser for config/routes.rb (and draw'n config/routes/*.rb).
10
+ # Extracts local_first route subtrees, server routes, inline include overrides,
11
+ # and emits a rewritten local-only config/routes.rb without executing app Ruby.
12
+ class RouteAnalyzer
13
+ HTTP_VERBS = %i[get post put patch delete].freeze
14
+ CONTAINER_CALLS = %i[namespace scope resources resource member collection constraints defaults].freeze
15
+ PLURAL_ACTIONS = %w[index new create show edit update destroy].freeze
16
+ SINGULAR_ACTIONS = %w[new create show edit update destroy].freeze
17
+
18
+ Result = Struct.new(
19
+ :has_local_first,
20
+ :island,
21
+ :declared_islands,
22
+ :local_routes,
23
+ :server_routes,
24
+ :overrides,
25
+ :local_routes_source,
26
+ keyword_init: true
27
+ )
28
+
29
+ Context = Struct.new(
30
+ :path_prefix,
31
+ :module_prefix,
32
+ :controller,
33
+ :in_local_first,
34
+ :island_name,
35
+ :resource_member_prefix,
36
+ :resource_collection_prefix,
37
+ :resource_controller,
38
+ keyword_init: true
39
+ )
40
+
41
+ def self.analyze(source_root:, island: nil)
42
+ new(source_root: source_root, island: island).analyze
43
+ end
44
+
45
+ def initialize(source_root:, island: nil)
46
+ @source_root = File.expand_path(source_root.to_s)
47
+ @island = island.nil? || island.to_s.empty? ? nil : island.to_s
48
+ @has_local_first = false
49
+ @declared_islands = []
50
+ @local_routes = []
51
+ @server_routes = []
52
+ @overrides = Manifest.normalize_overrides(nil)
53
+ @drawn_asts = {}
54
+ end
55
+
56
+ def analyze
57
+ routes_path = File.join(@source_root, "config/routes.rb")
58
+ unless File.file?(routes_path)
59
+ raise BuildError, "Missing config/routes.rb in #{@source_root}" if @island
60
+
61
+ return Result.new(
62
+ has_local_first: false,
63
+ declared_islands: [],
64
+ local_routes: [],
65
+ server_routes: [],
66
+ overrides: @overrides,
67
+ local_routes_source: nil
68
+ )
69
+ end
70
+
71
+ source = File.read(routes_path)
72
+ parsed = parse_ruby!(source, routes_path)
73
+ draw_call = find_routes_draw(parsed.value)
74
+ body = draw_call ? statements_of(draw_call.block&.body) : statements_of(parsed.value.statements)
75
+
76
+ root_ctx = Context.new(
77
+ path_prefix: "",
78
+ module_prefix: nil,
79
+ controller: nil,
80
+ in_local_first: false,
81
+ island_name: nil
82
+ )
83
+
84
+ walk_statements(body, root_ctx)
85
+
86
+ if @island && !@declared_islands.include?(@island)
87
+ available = @declared_islands.empty? ? "none declared" : @declared_islands.uniq.sort.join(", ")
88
+ raise BuildError, "Unknown local_first island #{@island.inspect} in config/routes.rb (declared islands: #{available})."
89
+ end
90
+
91
+ if @has_local_first
92
+ emitted_lines = emit_statements(body, root_ctx)
93
+ local_source = "Rails.application.routes.draw do\n#{indent_lines(emitted_lines, 2)}end\n"
94
+ local = Manifest.deduplicate_routes(@local_routes)
95
+ server = Manifest.deduplicate_routes(@server_routes + @overrides[:server_routes])
96
+ else
97
+ # Whole-app fallback mode: all non-internal routes are local routes.
98
+ local = Manifest.deduplicate_routes(@server_routes)
99
+ server = Manifest.deduplicate_routes(@overrides[:server_routes])
100
+ local_source = nil
101
+ end
102
+
103
+ Result.new(
104
+ has_local_first: @has_local_first,
105
+ island: @island,
106
+ declared_islands: @declared_islands.uniq.sort,
107
+ local_routes: local,
108
+ server_routes: server,
109
+ overrides: @overrides,
110
+ local_routes_source: local_source
111
+ )
112
+ end
113
+
114
+ private
115
+
116
+ def parse_ruby!(source, path)
117
+ parsed = Prism.parse(source)
118
+ unless parsed.success?
119
+ errors = parsed.errors.map { |e| "line #{e.location.start_line}: #{e.message}" }.join("; ")
120
+ raise BuildError, "Syntax error parsing #{path}: #{errors}"
121
+ end
122
+ parsed
123
+ end
124
+
125
+ def find_routes_draw(program)
126
+ statements_of(program.statements).each do |stmt|
127
+ next unless stmt.is_a?(Prism::CallNode) && stmt.name == :draw && stmt.block
128
+
129
+ return stmt
130
+ end
131
+ nil
132
+ end
133
+
134
+ def statements_of(node)
135
+ return [] unless node
136
+
137
+ node.is_a?(Prism::StatementsNode) ? node.body : [node]
138
+ end
139
+
140
+ def walk_statements(stmts, ctx, active_draws = Set.new)
141
+ stmts.each { |stmt| walk_node(stmt, ctx, active_draws) }
142
+ end
143
+
144
+ def walk_node(node, ctx, active_draws = Set.new)
145
+ return unless node.is_a?(Prism::CallNode) && node.receiver.nil?
146
+
147
+ case node.name
148
+ when :local_first
149
+ walk_local_first(node, ctx, active_draws)
150
+ when :draw
151
+ walk_draw(node, ctx, active_draws)
152
+ when :namespace
153
+ walk_namespace(node, ctx, active_draws)
154
+ when :scope
155
+ walk_scope(node, ctx, active_draws)
156
+ when :resources
157
+ walk_resources(node, ctx, singular: false, active_draws: active_draws)
158
+ when :resource
159
+ walk_resources(node, ctx, singular: true, active_draws: active_draws)
160
+ when :member
161
+ walk_member_or_collection(node, ctx, member: true, active_draws: active_draws)
162
+ when :collection
163
+ walk_member_or_collection(node, ctx, member: false, active_draws: active_draws)
164
+ when :constraints, :defaults
165
+ walk_statements(statements_of(node.block&.body), ctx, active_draws) if node.block
166
+ when :root
167
+ record_root(node, ctx)
168
+ when *HTTP_VERBS
169
+ record_http_verb(node, ctx, [node.name.to_s.upcase])
170
+ when :match
171
+ record_match(node, ctx)
172
+ when :mount
173
+ # Server-side engine mount (e.g. InstantRecord::Engine); not a local controller.
174
+ nil
175
+ end
176
+ end
177
+
178
+ def walk_local_first(node, ctx, active_draws = Set.new)
179
+ @has_local_first = true
180
+ pos, opts = extract_args(node)
181
+ declared_name = pos.first ? pos.first.to_s : nil
182
+ @declared_islands << declared_name if declared_name
183
+
184
+ active = @island.nil? || declared_name == @island
185
+ if active
186
+ inline_overrides = Manifest.normalize_overrides(opts)
187
+ @overrides = Manifest.merge_overrides(@overrides, inline_overrides)
188
+ end
189
+
190
+ child_ctx = ctx.dup
191
+ child_ctx.in_local_first = active
192
+ child_ctx.island_name = declared_name || ctx.island_name
193
+ walk_statements(statements_of(node.block&.body), child_ctx, active_draws) if node.block
194
+ end
195
+
196
+ def walk_draw(node, ctx, active_draws = Set.new)
197
+ pos, = extract_args(node)
198
+ name = pos.first&.to_s
199
+ return unless name
200
+
201
+ sub_path = resolve_draw_path!(name)
202
+ raise BuildError, "Cyclic route draw detected for config/routes/#{name}.rb" if active_draws.include?(sub_path)
203
+
204
+ parsed = load_drawn_ast!(sub_path)
205
+ walk_statements(statements_of(parsed.value.statements), ctx, active_draws + [sub_path])
206
+ end
207
+
208
+ def resolve_draw_path!(name)
209
+ routes_dir = File.expand_path(File.join(@source_root, "config/routes"))
210
+ sub_path = File.expand_path("#{name}.rb", routes_dir)
211
+ unless sub_path.start_with?("#{routes_dir}#{File::SEPARATOR}")
212
+ raise BuildError, "Invalid route draw name #{name.inspect}: escapes config/routes/"
213
+ end
214
+ unless File.file?(sub_path)
215
+ raise BuildError, "Route file config/routes/#{name}.rb referenced by draw(:#{name}) does not exist"
216
+ end
217
+ sub_path
218
+ end
219
+
220
+ def load_drawn_ast!(sub_path)
221
+ @drawn_asts[sub_path] ||= parse_ruby!(File.read(sub_path), sub_path)
222
+ end
223
+
224
+ def walk_namespace(node, ctx, active_draws = Set.new)
225
+ pos, opts = extract_args(node)
226
+ name = pos.first&.to_s
227
+ return unless name && node.block
228
+
229
+ path_seg = opts.key?(:path) ? opts[:path]&.to_s : name
230
+ mod_seg = opts.key?(:module) ? opts[:module]&.to_s : name
231
+
232
+ child_ctx = ctx.dup
233
+ child_ctx.path_prefix = join_path(ctx.path_prefix, path_seg) if path_seg && !path_seg.empty?
234
+ child_ctx.module_prefix = join_module(ctx.module_prefix, mod_seg) if mod_seg && !mod_seg.empty?
235
+ child_ctx.controller = qualify_controller(opts[:controller].to_s, child_ctx.module_prefix) if opts[:controller]
236
+ walk_statements(statements_of(node.block.body), child_ctx, active_draws)
237
+ end
238
+
239
+ def walk_scope(node, ctx, active_draws = Set.new)
240
+ return unless node.block
241
+
242
+ pos, opts = extract_args(node)
243
+ path_seg = opts.key?(:path) ? opts[:path]&.to_s : pos.first&.to_s
244
+ mod_seg = opts[:module]&.to_s
245
+
246
+ child_ctx = ctx.dup
247
+ child_ctx.path_prefix = join_path(ctx.path_prefix, path_seg) if path_seg && !path_seg.empty?
248
+ child_ctx.module_prefix = join_module(ctx.module_prefix, mod_seg) if mod_seg && !mod_seg.empty?
249
+ child_ctx.controller = qualify_controller(opts[:controller].to_s, child_ctx.module_prefix) if opts[:controller]
250
+ walk_statements(statements_of(node.block.body), child_ctx, active_draws)
251
+ end
252
+
253
+ def walk_resources(node, ctx, singular:, active_draws: Set.new)
254
+ pos, opts = extract_args(node)
255
+ names = pos.map(&:to_s)
256
+ return if names.empty?
257
+
258
+ default_actions = singular ? SINGULAR_ACTIONS : PLURAL_ACTIONS
259
+ actions = filter_actions(default_actions, only: opts[:only], except: opts[:except])
260
+
261
+ names.each do |name|
262
+ path_seg = opts.key?(:path) ? opts[:path]&.to_s : name
263
+ base_path = join_path(ctx.path_prefix, path_seg)
264
+ controller = resolve_resource_controller(name, opts, ctx, singular: singular)
265
+ param = (opts[:param] || "id").to_s
266
+
267
+ if singular
268
+ emit_singular_resource_routes(base_path, controller, actions, ctx)
269
+ member_prefix = base_path
270
+ nested_prefix = base_path
271
+ else
272
+ emit_plural_resource_routes(base_path, param, controller, actions, ctx)
273
+ member_prefix = "#{base_path}/:#{param}"
274
+ nested_prefix = "#{base_path}/:#{ActiveSupport::Inflector.singularize(name)}_#{param}"
275
+ end
276
+
277
+ next unless node.block
278
+
279
+ child_ctx = ctx.dup
280
+ child_ctx.path_prefix = nested_prefix
281
+ child_ctx.module_prefix = join_module(ctx.module_prefix, opts[:module].to_s) if opts[:module]
282
+ child_ctx.controller = controller
283
+ child_ctx.resource_member_prefix = member_prefix
284
+ child_ctx.resource_collection_prefix = base_path
285
+ child_ctx.resource_controller = controller
286
+ walk_statements(statements_of(node.block.body), child_ctx, active_draws)
287
+ end
288
+ end
289
+
290
+ def walk_member_or_collection(node, ctx, member:, active_draws: Set.new)
291
+ return unless node.block
292
+
293
+ child_ctx = ctx.dup
294
+ child_ctx.path_prefix = member ? (ctx.resource_member_prefix || ctx.path_prefix) : (ctx.resource_collection_prefix || ctx.path_prefix)
295
+ child_ctx.controller = ctx.resource_controller || ctx.controller
296
+ walk_statements(statements_of(node.block.body), child_ctx, active_draws)
297
+ end
298
+
299
+ def emit_plural_resource_routes(base, param, controller, actions, ctx)
300
+ member = "#{base}/:#{param}"
301
+ record_route("GET", base, controller, "index", ctx) if actions.include?("index")
302
+ record_route("GET", "#{base}/new", controller, "new", ctx) if actions.include?("new")
303
+ record_route("POST", base, controller, "create", ctx) if actions.include?("create")
304
+ record_route("GET", member, controller, "show", ctx) if actions.include?("show")
305
+ record_route("GET", "#{member}/edit", controller, "edit", ctx) if actions.include?("edit")
306
+ if actions.include?("update")
307
+ record_route("PATCH", member, controller, "update", ctx)
308
+ record_route("PUT", member, controller, "update", ctx)
309
+ end
310
+ record_route("DELETE", member, controller, "destroy", ctx) if actions.include?("destroy")
311
+ end
312
+
313
+ def emit_singular_resource_routes(base, controller, actions, ctx)
314
+ record_route("GET", "#{base}/new", controller, "new", ctx) if actions.include?("new")
315
+ record_route("POST", base, controller, "create", ctx) if actions.include?("create")
316
+ record_route("GET", base, controller, "show", ctx) if actions.include?("show")
317
+ record_route("GET", "#{base}/edit", controller, "edit", ctx) if actions.include?("edit")
318
+ if actions.include?("update")
319
+ record_route("PATCH", base, controller, "update", ctx)
320
+ record_route("PUT", base, controller, "update", ctx)
321
+ end
322
+ record_route("DELETE", base, controller, "destroy", ctx) if actions.include?("destroy")
323
+ end
324
+
325
+ def record_root(node, ctx)
326
+ pos, opts = extract_args(node)
327
+ target = (opts[:to] || pos.first)&.to_s
328
+ return unless target
329
+
330
+ controller, action = split_target(target, ctx)
331
+ path = ctx.path_prefix.empty? ? "/" : ctx.path_prefix
332
+ record_route("GET", path, controller, action, ctx)
333
+ end
334
+
335
+ def record_http_verb(node, ctx, verbs)
336
+ pos, opts, string_mapping = extract_args(node, include_string_mapping: true)
337
+ if string_mapping
338
+ raw_path, raw_target = string_mapping
339
+ else
340
+ raw_path = pos.first&.to_s
341
+ raw_target = opts[:to]&.to_s
342
+ end
343
+ return unless raw_path
344
+
345
+ effective_prefix =
346
+ case opts[:on]&.to_sym
347
+ when :member then ctx.resource_member_prefix || ctx.path_prefix
348
+ when :collection then ctx.resource_collection_prefix || ctx.path_prefix
349
+ else ctx.path_prefix
350
+ end
351
+ effective_controller =
352
+ %i[member collection].include?(opts[:on]&.to_sym) ? (ctx.resource_controller || ctx.controller) : ctx.controller
353
+
354
+ path = join_path(effective_prefix, raw_path)
355
+ controller, action =
356
+ if raw_target
357
+ split_target(raw_target, ctx)
358
+ elsif opts.key?(:to)
359
+ # Dynamic `to:` (e.g. redirect("/new") or Rack app): keep the route path without guessing a controller.
360
+ [nil, nil]
361
+ elsif opts[:controller] || opts[:action]
362
+ ctrl = opts[:controller] ? qualify_controller(opts[:controller].to_s, ctx.module_prefix) : effective_controller
363
+ act = (opts[:action] || raw_path.split("/").last).to_s
364
+ [ctrl, act]
365
+ elsif effective_controller
366
+ [effective_controller, raw_path.delete_prefix("/").tr("-", "_")]
367
+ elsif raw_path.include?("/")
368
+ parts = raw_path.delete_prefix("/").split("/")
369
+ act = parts.pop
370
+ [qualify_controller(parts.join("/"), ctx.module_prefix), act]
371
+ else
372
+ [nil, nil]
373
+ end
374
+
375
+ verbs.each do |verb|
376
+ record_route(verb, path, controller, action, ctx)
377
+ end
378
+ end
379
+
380
+ def record_match(node, ctx)
381
+ _, opts = extract_args(node)
382
+ via = opts[:via]
383
+ verbs =
384
+ case via
385
+ when Array then via.map { |v| v.to_s.upcase }
386
+ when Symbol, String
387
+ via.to_s == "all" ? %w[GET POST PATCH PUT DELETE OPTIONS] : [via.to_s.upcase]
388
+ else
389
+ %w[GET POST]
390
+ end
391
+ record_http_verb(node, ctx, verbs)
392
+ end
393
+
394
+ def record_route(method_name, path, controller, action, ctx)
395
+ # Skip Rails internal framework endpoints (e.g. rails/health, rails/pwa).
396
+ return if controller&.start_with?("rails/")
397
+
398
+ entry = {
399
+ "method" => method_name,
400
+ "path" => Manifest.normalize_route_path(path),
401
+ "controller" => controller,
402
+ "action" => action
403
+ }
404
+ entry["island"] = ctx.island_name if ctx.in_local_first && ctx.island_name
405
+
406
+ if ctx.in_local_first
407
+ @local_routes << entry
408
+ else
409
+ @server_routes << entry
410
+ end
411
+ end
412
+
413
+ # --- AST-preserving local config/routes.rb emitter ---
414
+
415
+ def emit_statements(stmts, ctx)
416
+ lines = []
417
+ stmts.each do |stmt|
418
+ lines.concat(emit_node(stmt, ctx))
419
+ end
420
+ lines
421
+ end
422
+
423
+ def emit_node(node, ctx)
424
+ return [] unless node.is_a?(Prism::CallNode) && node.receiver.nil?
425
+
426
+ case node.name
427
+ when :local_first
428
+ pos, = extract_args(node)
429
+ declared_name = pos.first ? pos.first.to_s : nil
430
+ active = @island.nil? || declared_name == @island
431
+ return [] unless active && node.block
432
+
433
+ child_ctx = ctx.dup
434
+ child_ctx.in_local_first = true
435
+ child_ctx.island_name = declared_name || ctx.island_name
436
+ emit_statements(statements_of(node.block.body), child_ctx)
437
+ when :draw
438
+ pos, = extract_args(node)
439
+ name = pos.first&.to_s
440
+ return [] unless name
441
+
442
+ sub_path = resolve_draw_path!(name)
443
+ parsed = load_drawn_ast!(sub_path)
444
+ emit_statements(statements_of(parsed.value.statements), ctx)
445
+ when *CONTAINER_CALLS
446
+ if node.block
447
+ inner = emit_statements(statements_of(node.block.body), ctx)
448
+ return [] if inner.empty? && !ctx.in_local_first
449
+
450
+ if ctx.in_local_first && !subtree_has_special_calls?(node.block.body)
451
+ reindent_slice(node.location.slice)
452
+ else
453
+ header = container_header(node)
454
+ closing = node.block.closing_loc.slice
455
+ [header, *inner.map { |line| " #{line}" }, closing]
456
+ end
457
+ else
458
+ ctx.in_local_first ? reindent_slice(node.location.slice) : []
459
+ end
460
+ when :root, *HTTP_VERBS, :match
461
+ ctx.in_local_first ? reindent_slice(node.location.slice) : []
462
+ else
463
+ []
464
+ end
465
+ end
466
+
467
+ def subtree_has_special_calls?(node)
468
+ return false unless node
469
+
470
+ found = false
471
+ queue = [node]
472
+ while (current = queue.shift)
473
+ if current.is_a?(Prism::CallNode) && current.receiver.nil? && %i[local_first draw].include?(current.name)
474
+ found = true
475
+ break
476
+ end
477
+ queue.concat(current.compact_child_nodes)
478
+ end
479
+ found
480
+ end
481
+
482
+ def container_header(node)
483
+ block = node.block
484
+ source_slice = node.location.slice
485
+ open_offset = block.opening_loc.end_offset - node.location.start_offset
486
+ if block.parameters
487
+ open_offset = block.parameters.location.end_offset - node.location.start_offset
488
+ end
489
+ source_slice[0...open_offset].strip
490
+ end
491
+
492
+ def reindent_slice(slice)
493
+ lines = slice.lines(chomp: true)
494
+ return [] if lines.empty?
495
+
496
+ # First line starts at the AST node column; trim common indent of subsequent lines.
497
+ first = lines.first.lstrip
498
+ rest = lines[1..]
499
+ return [first] if rest.empty?
500
+
501
+ non_empty = rest.reject { |l| l.strip.empty? }
502
+ min_indent = non_empty.map { |l| l[/\A */].length }.min || 0
503
+ [first, *rest.map { |l| l.strip.empty? ? "" : l[min_indent..] }]
504
+ end
505
+
506
+ def indent_lines(lines, spaces)
507
+ prefix = " " * spaces
508
+ lines.map { |line| line.empty? ? "\n" : "#{prefix}#{line}\n" }.join
509
+ end
510
+
511
+ # --- Helpers ---
512
+
513
+ def resolve_resource_controller(name, opts, ctx, singular:)
514
+ raw =
515
+ if opts[:controller]
516
+ opts[:controller].to_s
517
+ elsif singular
518
+ ActiveSupport::Inflector.pluralize(name)
519
+ else
520
+ name
521
+ end
522
+ mod = opts[:module] ? join_module(ctx.module_prefix, opts[:module].to_s) : ctx.module_prefix
523
+ qualify_controller(raw, mod)
524
+ end
525
+
526
+ def split_target(target, ctx)
527
+ if target.include?("#")
528
+ ctrl, action = target.split("#", 2)
529
+ [qualify_controller(ctrl, ctx.module_prefix), action]
530
+ else
531
+ [ctx.controller, target]
532
+ end
533
+ end
534
+
535
+ def qualify_controller(controller, module_prefix)
536
+ return controller.delete_prefix("/") if controller.start_with?("/")
537
+ return controller if module_prefix.nil? || module_prefix.empty?
538
+ return controller if controller.start_with?("#{module_prefix}/")
539
+
540
+ "#{module_prefix}/#{controller}"
541
+ end
542
+
543
+ def join_path(prefix, segment)
544
+ seg = segment.to_s.delete_prefix("/")
545
+ return prefix if seg.empty?
546
+
547
+ prefix.to_s.empty? ? "/#{seg}" : "#{prefix.delete_suffix("/")}/#{seg}"
548
+ end
549
+
550
+ def join_module(prefix, segment)
551
+ seg = segment.to_s.delete_prefix("/").delete_suffix("/")
552
+ return prefix if seg.empty?
553
+
554
+ prefix.to_s.empty? ? seg : "#{prefix}/#{seg}"
555
+ end
556
+
557
+ def filter_actions(defaults, only:, except:)
558
+ list = defaults.dup
559
+ if only
560
+ allowed = Array(only).map(&:to_s)
561
+ list &= allowed
562
+ end
563
+ if except
564
+ denied = Array(except).map(&:to_s)
565
+ list -= denied
566
+ end
567
+ list
568
+ end
569
+
570
+ ROUTE_OPTION_KEYS = %i[to controller action via as on only except path module param constraints defaults format].freeze
571
+
572
+ def extract_args(call, include_string_mapping: false)
573
+ pos = []
574
+ opts = {}
575
+ string_mapping = nil
576
+
577
+ Array(call.arguments&.arguments).each do |arg|
578
+ if arg.is_a?(Prism::KeywordHashNode) || arg.is_a?(Prism::HashNode)
579
+ arg.elements.each do |assoc|
580
+ next unless assoc.is_a?(Prism::AssocNode)
581
+
582
+ key = literal_value(assoc.key)
583
+ val = literal_value(assoc.value)
584
+ if include_string_mapping && val.is_a?(String) && val.include?("#") && string_mapping.nil? &&
585
+ (key.is_a?(String) || (key.is_a?(Symbol) && !ROUTE_OPTION_KEYS.include?(key)))
586
+ string_mapping = [key.to_s, val]
587
+ elsif key.is_a?(Symbol)
588
+ opts[key] = val
589
+ elsif key.is_a?(String)
590
+ opts[key.to_sym] = val
591
+ end
592
+ end
593
+ else
594
+ pos << literal_value(arg)
595
+ end
596
+ end
597
+
598
+ include_string_mapping ? [pos, opts, string_mapping] : [pos, opts]
599
+ end
600
+
601
+ def literal_value(node)
602
+ case node
603
+ when Prism::SymbolNode then node.unescaped.to_sym
604
+ when Prism::StringNode then node.unescaped
605
+ when Prism::IntegerNode, Prism::FloatNode then node.value
606
+ when Prism::TrueNode then true
607
+ when Prism::FalseNode then false
608
+ when Prism::NilNode then nil
609
+ when Prism::ArrayNode then node.elements.map { |el| literal_value(el) }
610
+ when Prism::HashNode, Prism::KeywordHashNode
611
+ node.elements.each_with_object({}) do |assoc, hash|
612
+ next unless assoc.is_a?(Prism::AssocNode)
613
+
614
+ k = literal_value(assoc.key)
615
+ hash[k] = literal_value(assoc.value) unless k.nil?
616
+ end
617
+ else
618
+ nil
619
+ end
620
+ end
621
+ end
622
+ end
623
+ end
@@ -0,0 +1,42 @@
1
+ require "fileutils"
2
+ require "open3"
3
+
4
+ module InstantRecord
5
+ class Compiler
6
+ class BuildError < StandardError; end
7
+
8
+ # Runs external toolchain binaries (Roundhouse, Spinel, clang, Vite) with
9
+ # captured logs and concise, readable error tails.
10
+ class Runner
11
+ attr_reader :build_dir
12
+
13
+ def initialize(build_dir:)
14
+ @build_dir = build_dir.to_s
15
+ end
16
+
17
+ def run(args, cwd: nil, env: {}, log: nil)
18
+ cmd = args.map(&:to_s)
19
+ merged_env = env.transform_keys(&:to_s).transform_values { |v| v.nil? ? nil : v.to_s }
20
+ opts = {}
21
+ opts[:chdir] = cwd.to_s if cwd
22
+
23
+ output, status = Open3.capture2e(merged_env, [cmd.first, cmd.first], *cmd[1..], **opts)
24
+ if log
25
+ FileUtils.mkdir_p(@build_dir)
26
+ File.write(File.join(@build_dir, log), output)
27
+ end
28
+
29
+ unless status.success?
30
+ filtered = output.scrub.lines.reject { |line| line.include?("--rbs") || line.include?("warning:") }.join
31
+ tail = filtered.length > 1500 ? filtered[-1500..] : filtered
32
+ location = log ? " (see #{File.join(@build_dir, log)})" : ""
33
+ raise BuildError, "build step failed: #{cmd.first(3).join(' ')}#{location}\n#{tail}"
34
+ end
35
+
36
+ output
37
+ rescue Errno::ENOENT => e
38
+ raise BuildError, "build step failed: executable #{cmd.first.inspect} not found (#{e.message})"
39
+ end
40
+ end
41
+ end
42
+ end