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,67 @@
1
+ import { createElement, useEffect, useMemo, useRef, useState } from "react"
2
+ import { Virtuoso } from "react-virtuoso"
3
+
4
+ // Virtuoso numbers rows from `firstItemIndex`. Lowering it by the rows that
5
+ // arrived above the reader keeps the row they are on where it is.
6
+ const START = 1_000_000
7
+ // Rows in the first draw, before the list knows its height: a screenful.
8
+ const FIRST_DRAW = 20
9
+
10
+ function useSteadyStart(rows, anchor) {
11
+ const before = useRef({ start: START, at: new Map() })
12
+ return useMemo(() => {
13
+ const was = before.current
14
+ const at = new Map(rows.map((row, index) => [row.id, index]))
15
+ let start = was.start
16
+ const held = anchor.current
17
+ if (held && was.at.has(held) && at.has(held)) start -= at.get(held) - was.at.get(held)
18
+ before.current = { start, at }
19
+ return start
20
+ }, [rows, anchor])
21
+ }
22
+
23
+ // A virtual list for rows that change under the reader: sync adds rows above
24
+ // and removes rows anywhere, and the row being read stays where it is.
25
+ //
26
+ // rows objects with an `id`, in order
27
+ // children (row, index) => the row's element
28
+ // onEnd called as the reader nears the last row (useLiveWindow's `older`)
29
+ // register from useKeyboardNavigation, so keys can bring any row into view
30
+ // openAt the index to open on, read once (a reload returns to its row)
31
+ // onTop called with whether the list is scrolled to its top
32
+ export default function LiveList({ rows, children, onEnd, register, openAt = 0, onTop, className }) {
33
+ const list = useRef(null)
34
+ const anchor = useRef(null)
35
+ const start = useSteadyStart(rows, anchor)
36
+ const now = useRef({})
37
+ now.current = { rows, start }
38
+
39
+ // Opened at its top, the list's first draw holds rows. Left to itself it
40
+ // draws empty, measures its box and draws again, and the reader sees the
41
+ // empty one. Opened deeper it has to scroll before it can show a row, so
42
+ // there it keeps the empty draw.
43
+ const [opening] = useState(() => (openAt > 0
44
+ ? { initialTopMostItemIndex: { index: Math.min(openAt, rows.length - 1), align: "center" } }
45
+ : { initialItemCount: Math.min(rows.length, FIRST_DRAW) }))
46
+
47
+ useEffect(() => register?.((index) => list.current?.scrollIntoView({ index, behavior: "auto" })), [register])
48
+
49
+ // Plain createElement, so the package needs no JSX transform where it is installed.
50
+ return createElement(Virtuoso, {
51
+ ref: list,
52
+ className,
53
+ data: rows,
54
+ firstItemIndex: start,
55
+ ...opening,
56
+ computeItemKey: (_, row) => row.id,
57
+ increaseViewportBy: 600,
58
+ endReached: () => onEnd?.(),
59
+ atTopStateChange: onTop,
60
+ rangeChanged: ({ startIndex }) => {
61
+ const { rows, start } = now.current
62
+ // At the top a new row should show, so nothing is held there.
63
+ anchor.current = startIndex - start > 0 ? rows[startIndex - start]?.id : null
64
+ },
65
+ itemContent: (index, row) => children(row, index - now.current.start),
66
+ })
67
+ }
data/frontend/chord.js ADDED
@@ -0,0 +1,79 @@
1
+ // Naming a key press, and finding the binding for it. No React here, so the
2
+ // tests import this file alone.
3
+ //
4
+ // A chord is a string: "j", "Shift+J", "Mod+Enter", "Mod+Shift+Enter", "?",
5
+ // "Escape", "Space". `Mod` is ⌘ on Apple platforms and Ctrl everywhere else.
6
+ //
7
+ // A binding is { keys, run, label, group, when, typing, dialog, repeat }:
8
+ //
9
+ // keys the chords that run it
10
+ // when () => false where it does not apply just now
11
+ // typing true for a binding that also runs while a text field has focus
12
+ // dialog true for a binding that also runs inside an open dialog
13
+ // repeat true for a binding a held key repeats (movement)
14
+ //
15
+ // `label` and `group` are for whoever lists the keys: the same table drives
16
+ // the handlers and the help, so the two cannot disagree.
17
+
18
+ export const isApple = () =>
19
+ typeof navigator !== "undefined" && /Mac|iPhone|iPad|iPod/.test(navigator.platform || navigator.userAgent || "")
20
+
21
+ const isLetter = (key) => key.length === 1 && key.toLowerCase() !== key.toUpperCase()
22
+
23
+ // The chord an event is, or null for one no binding can name (Alt, or the
24
+ // other platform's modifier).
25
+ export function chord(event, { apple = isApple() } = {}) {
26
+ // Autofill sends a keydown with no key.
27
+ if (event.altKey || !event.key) return null
28
+ const mod = apple ? event.metaKey : event.ctrlKey
29
+ if (apple ? event.ctrlKey : event.metaKey) return null
30
+
31
+ const key = event.key === " " ? "Space" : event.key
32
+ const letter = isLetter(key)
33
+ // A shifted symbol is its own name: "?" and never "Shift+/".
34
+ const shift = event.shiftKey && (letter || key.length > 1)
35
+ const name = letter ? (shift ? key.toUpperCase() : key.toLowerCase()) : key
36
+ return [mod && "Mod", shift && "Shift", name].filter(Boolean).join("+")
37
+ }
38
+
39
+ // What kind of place a key press landed in.
40
+ export function place(target) {
41
+ if (!target?.tagName) return {}
42
+ const tag = target.tagName
43
+ const pressable = tag === "INPUT" && /^(checkbox|radio|button|submit|reset)$/.test(target.type || "")
44
+ return {
45
+ typing: !pressable && (!!target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(tag)),
46
+ control: pressable || /^(BUTTON|A|SUMMARY)$/.test(tag),
47
+ dialog: !!target.closest?.("[role=dialog]"),
48
+ }
49
+ }
50
+
51
+ const ACTIVATES = /(^|\+)(Enter|Space)$/
52
+
53
+ // The first binding that answers this event where it landed, or null.
54
+ export function match(bindings, event, target, options) {
55
+ const name = chord(event, options)
56
+ if (!name) return null
57
+ const at = place(target)
58
+ return bindings.find((binding) => {
59
+ if (!binding.keys.includes(name)) return false
60
+ if (event.repeat && !binding.repeat) return false
61
+ if (at.typing && !binding.typing) return false
62
+ if (at.dialog && !binding.dialog) return false
63
+ // A button, link or checkbox keeps the keys that press it.
64
+ if (at.control && ACTIVATES.test(name)) return false
65
+ return !binding.when || binding.when()
66
+ }) || null
67
+ }
68
+
69
+ const CAPS = { Escape: "Esc", Enter: "↵", ArrowDown: "↓", ArrowUp: "↑", ArrowLeft: "←", ArrowRight: "→" }
70
+
71
+ // The key caps to draw for a chord: "Mod+Shift+Enter" is ["⌘", "⇧", "↵"] on a
72
+ // Mac and ["Ctrl", "Shift", "↵"] elsewhere.
73
+ export function keycaps(name, { apple = isApple() } = {}) {
74
+ return name.split("+").map((part) => {
75
+ if (part === "Mod") return apple ? "⌘" : "Ctrl"
76
+ if (part === "Shift") return apple ? "⇧" : "Shift"
77
+ return CAPS[part] || (part.length === 1 ? part.toUpperCase() : part)
78
+ })
79
+ }
data/frontend/file.js ADDED
@@ -0,0 +1,65 @@
1
+ // A record's file, for a page that shows or plays it.
2
+ //
3
+ // const files = fileStore() // once, outside the component
4
+ // const { url, status, progress } = useFile(files, "Recording", recording.id)
5
+ // return status === "ready" ? <audio src={url} controls /> : <Waiting status={status} progress={progress} />
6
+ //
7
+ // The file is read from this browser when it is here, with or without a
8
+ // connection, and fetched from the server and kept when it is not. `status`
9
+ // is one of:
10
+ //
11
+ // loading reading it, or fetching it (`progress` goes from 0 to 1)
12
+ // ready `url` and `file` are set
13
+ // missing the server does not hold it: its upload has not finished on
14
+ // the device that made it
15
+ // offline it is not here and there is no connection to fetch it over
16
+ // error anything else, as `error`
17
+ //
18
+ // A file that could not be had is asked for again when the connection
19
+ // returns, and when `retry` is called.
20
+ import { useCallback, useEffect, useRef, useState } from "react"
21
+
22
+ const NONE = { status: "none", url: null, file: null, progress: 0, error: null }
23
+ const LOADING = { status: "loading", url: null, file: null, progress: 0, error: null }
24
+
25
+ // What a failed open is called. Out here so it can be tested without React.
26
+ export function failure(error, online = globalThis.navigator?.onLine !== false) {
27
+ if (error?.name === "FileMissing") return "missing"
28
+ return online ? "error" : "offline"
29
+ }
30
+
31
+ export function useFile(store, type, id) {
32
+ const [state, setState] = useState(id ? LOADING : NONE)
33
+ const [attempt, setAttempt] = useState(0)
34
+ const status = useRef(state.status)
35
+ status.current = state.status
36
+ const retry = useCallback(() => setAttempt((count) => count + 1), [])
37
+
38
+ useEffect(() => {
39
+ if (!id) return setState(NONE)
40
+ let live = true
41
+ let url = null
42
+ setState(LOADING)
43
+ store.open(type, id, {
44
+ onProgress: ({ received, size }) => live && setState((now) => ({ ...now, progress: received / size })),
45
+ }).then((file) => {
46
+ if (!live) return
47
+ url = URL.createObjectURL(file)
48
+ setState({ status: "ready", url, file, progress: 1, error: null })
49
+ }, (error) => {
50
+ if (live) setState({ ...NONE, status: failure(error), error })
51
+ })
52
+ return () => {
53
+ live = false
54
+ if (url) URL.revokeObjectURL(url)
55
+ }
56
+ }, [store, type, id, attempt])
57
+
58
+ useEffect(() => {
59
+ const online = () => ["offline", "error", "missing"].includes(status.current) && retry()
60
+ window.addEventListener("online", online)
61
+ return () => window.removeEventListener("online", online)
62
+ }, [retry])
63
+
64
+ return { ...state, retry }
65
+ }