@h1r-ai/croche-react-native 0.5.5 → 0.7.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 (248) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +90 -451
  3. package/THIRD-PARTY-NOTICES +906 -14
  4. package/android/build.gradle +6 -1
  5. package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +126 -0
  6. package/android/src/main/AndroidManifest.xml +0 -5
  7. package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +106 -0
  8. package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +217 -0
  9. package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +48 -824
  10. package/android/src/main/java/dev/croche/reactnative/NativePal.kt +68 -0
  11. package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
  12. package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +64 -41
  13. package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +64 -0
  14. package/android/src/test/java/dev/croche/reactnative/BlobFilesTest.kt +107 -0
  15. package/android/src/test/java/dev/croche/reactnative/ImageAdapterWireTest.kt +70 -0
  16. package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +44 -0
  17. package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
  18. package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
  19. package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
  20. package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
  21. package/android/src/test/java/dev/croche/reactnative/StorageVersionWireTest.kt +37 -0
  22. package/android/src/test/java/dev/croche/reactnative/VerificationDispatchTest.kt +24 -0
  23. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
  24. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +10 -8
  25. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
  26. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +55 -0
  27. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
  28. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageCodec.kt +1 -0
  29. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
  30. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
  31. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
  32. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
  33. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
  34. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +116 -0
  35. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
  36. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
  37. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
  38. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStateStore.kt +216 -0
  39. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +108 -0
  40. package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +8 -2
  41. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Adapters.kt +75 -0
  42. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
  43. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +31 -0
  44. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +25 -1
  45. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +121 -103
  46. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +89 -103
  47. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +20 -360
  48. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +136 -0
  49. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Decision.kt +170 -0
  50. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +23 -0
  51. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +26 -21
  52. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/EncodedImageCodec.kt +51 -0
  53. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +715 -0
  54. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +9 -322
  55. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
  56. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +16 -28
  57. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeEvent.kt +59 -0
  58. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeHandleOwner.kt +118 -0
  59. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +10 -74
  60. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +294 -37
  61. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +127 -0
  62. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
  63. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +73 -0
  64. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
  65. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StateStore.kt +90 -0
  66. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +88 -24
  67. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +5723 -3088
  68. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
  69. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +96 -0
  70. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
  71. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
  72. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
  73. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +30 -0
  74. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
  75. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
  76. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +127 -0
  77. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ImageAdapterHost.kt +28 -0
  78. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +70 -0
  79. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStateStore.kt +125 -0
  80. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +64 -0
  81. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
  82. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativeCoreInitialization.kt +21 -0
  83. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +60 -0
  84. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
  85. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
  86. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +43 -0
  87. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
  88. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StateStoreContract.kt +103 -0
  89. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StorageVersionContract.kt +40 -0
  90. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
  91. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +100 -0
  92. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/BlobFiles.kt +157 -0
  93. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
  94. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/InstancePayloadPurge.kt +79 -0
  95. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/VersionedKeyFiles.kt +71 -0
  96. package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
  97. package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
  98. package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
  99. package/croche-react-native.podspec +1 -0
  100. package/dist/package.json +1 -0
  101. package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
  102. package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
  103. package/dist/react-native/src/core-native.d.ts +6 -0
  104. package/dist/react-native/src/core-native.js +316 -0
  105. package/dist/react-native/src/core-wire.d.ts +55 -0
  106. package/dist/react-native/src/core-wire.js +1 -0
  107. package/dist/react-native/src/native-module.d.ts +8 -0
  108. package/dist/react-native/src/native-module.js +53 -0
  109. package/dist/rn-shared-build.json +16 -0
  110. package/dist/runtime/src/adapters.d.ts +8 -0
  111. package/dist/runtime/src/adapters.js +29 -0
  112. package/dist/runtime/src/agent.d.ts +91 -0
  113. package/dist/runtime/src/agent.js +137 -0
  114. package/dist/runtime/src/app-api.d.ts +572 -0
  115. package/dist/runtime/src/app-api.js +1 -0
  116. package/dist/runtime/src/cancel.d.ts +9 -0
  117. package/dist/runtime/src/cancel.js +27 -0
  118. package/dist/runtime/src/core/foreground.d.ts +3 -0
  119. package/dist/runtime/src/core/foreground.js +40 -0
  120. package/dist/runtime/src/core/port.d.ts +247 -0
  121. package/dist/runtime/src/core/port.js +1 -0
  122. package/dist/runtime/src/core/wasm-owner.d.ts +32 -0
  123. package/dist/runtime/src/core/wasm-owner.js +237 -0
  124. package/dist/runtime/src/core/wasm.d.ts +1 -0
  125. package/dist/runtime/src/core/wasm.js +2 -0
  126. package/dist/runtime/src/croche.d.ts +97 -0
  127. package/dist/runtime/src/croche.js +172 -0
  128. package/dist/runtime/src/errors.d.ts +33 -0
  129. package/dist/runtime/src/errors.js +63 -0
  130. package/dist/runtime/src/events.d.ts +5 -0
  131. package/dist/runtime/src/events.js +31 -0
  132. package/dist/runtime/src/generated/app-stream.d.ts +328 -0
  133. package/dist/runtime/src/generated/app-stream.js +127 -0
  134. package/dist/runtime/src/image-input.d.ts +5 -0
  135. package/dist/runtime/src/image-input.js +60 -0
  136. package/dist/runtime/src/index.d.ts +6 -0
  137. package/dist/runtime/src/index.js +5 -0
  138. package/dist/runtime/src/index.native.d.ts +1 -0
  139. package/dist/runtime/src/index.native.js +2 -0
  140. package/dist/runtime/src/json.d.ts +13 -0
  141. package/dist/runtime/src/json.js +120 -0
  142. package/dist/runtime/src/native-adapter.d.ts +3 -0
  143. package/dist/runtime/src/native-adapter.js +30 -0
  144. package/dist/runtime/src/pal/host.d.ts +5 -0
  145. package/dist/runtime/src/pal/host.js +60 -0
  146. package/dist/runtime/src/pal/platform.d.ts +21 -0
  147. package/dist/runtime/src/pal/platform.js +7 -0
  148. package/dist/runtime/src/pal/state-store-lock.d.ts +8 -0
  149. package/dist/runtime/src/pal/state-store-lock.js +2 -0
  150. package/dist/runtime/src/pal/state-store.d.ts +66 -0
  151. package/dist/runtime/src/pal/state-store.js +1 -0
  152. package/dist/runtime/src/pal/storage-version-contract.d.ts +19 -0
  153. package/dist/runtime/src/pal/storage-version-contract.js +68 -0
  154. package/dist/runtime/src/pal/transactional-storage.d.ts +3 -0
  155. package/dist/runtime/src/pal/transactional-storage.js +124 -0
  156. package/dist/runtime/src/rn-entry.d.ts +1 -0
  157. package/dist/runtime/src/rn-entry.js +4 -0
  158. package/dist/runtime/src/run-tool.d.ts +33 -0
  159. package/dist/runtime/src/run-tool.js +7 -0
  160. package/dist/runtime/src/tools.d.ts +35 -0
  161. package/dist/runtime/src/tools.js +94 -0
  162. package/ios/CoreAgentTransport.swift +14 -0
  163. package/ios/CoreDeliveryTransport.swift +58 -0
  164. package/ios/CoreNativePal.swift +98 -0
  165. package/ios/CorePlatformTransport.swift +44 -0
  166. package/ios/CoreWireDispatch.swift +122 -0
  167. package/ios/CoreWireSupport.swift +286 -0
  168. package/ios/CrocheRuntimeBridge.swift +34 -994
  169. package/ios/ProjectPal.swift +49 -0
  170. package/ios/RNCrocheRuntime.mm +18 -201
  171. package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
  172. package/ios/vendor/CrocheRuntimeSDK/BlobFiles.swift +124 -0
  173. package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +60 -0
  174. package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
  175. package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
  176. package/ios/vendor/CrocheRuntimeSDK/EncodedImages.swift +85 -0
  177. package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +26 -157
  178. package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +672 -0
  179. package/ios/vendor/CrocheRuntimeSDK/Host.swift +34 -371
  180. package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
  181. package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
  182. package/ios/vendor/CrocheRuntimeSDK/InstancePayloadPurge.swift +88 -0
  183. package/ios/vendor/CrocheRuntimeSDK/JSON.swift +30 -14
  184. package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
  185. package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +1 -155
  186. package/ios/vendor/CrocheRuntimeSDK/Platform.swift +33 -59
  187. package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +289 -0
  188. package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +302 -0
  189. package/ios/vendor/CrocheRuntimeSDK/PlatformStateStore.swift +253 -0
  190. package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +281 -0
  191. package/ios/vendor/CrocheRuntimeSDK/Project.swift +4 -269
  192. package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +24 -461
  193. package/ios/vendor/CrocheRuntimeSDK/RuntimeHTTPAdapter.swift +42 -0
  194. package/ios/vendor/CrocheRuntimeSDK/S1/S1Adapters.swift +110 -0
  195. package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +248 -0
  196. package/ios/vendor/CrocheRuntimeSDK/S1/S1Allowance.swift +20 -0
  197. package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
  198. package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +170 -0
  199. package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +313 -0
  200. package/ios/vendor/CrocheRuntimeSDK/S1/S1Decision.swift +146 -0
  201. package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +37 -0
  202. package/ios/vendor/CrocheRuntimeSDK/S1/S1HandleOwner.swift +58 -0
  203. package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +346 -0
  204. package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +89 -0
  205. package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +165 -0
  206. package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +100 -0
  207. package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +278 -0
  208. package/ios/vendor/CrocheRuntimeSDK/S1/S1Verification.swift +27 -0
  209. package/ios/vendor/CrocheRuntimeSDK/S1/StateStoreContract.swift +189 -0
  210. package/ios/vendor/CrocheRuntimeSDK/S1/StorageVersionContract.swift +68 -0
  211. package/ios/vendor/CrocheRuntimeSDK/Storage.swift +23 -269
  212. package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +163 -116
  213. package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
  214. package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +4712 -2469
  215. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +899 -608
  216. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
  217. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +899 -608
  218. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
  219. package/package.json +18 -8
  220. package/scripts/README.md +11 -0
  221. package/scripts/build-shared.mjs +46 -0
  222. package/scripts/build.mjs +11 -0
  223. package/scripts/check-swift-vendor-modules.sh +15 -0
  224. package/scripts/vendor-native.sh +8 -6
  225. package/src/NativeCrocheRuntime.ts +12 -159
  226. package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
  227. package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
  228. package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
  229. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/CrocheProjectRecordProvider.kt +0 -32
  230. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +0 -146
  231. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +0 -110
  232. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
  233. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +0 -173
  234. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
  235. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +0 -25
  236. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
  237. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +0 -87
  238. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +0 -181
  239. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +0 -227
  240. package/ios/vendor/CrocheRuntimeSDK/Background.swift +0 -59
  241. package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
  242. package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +0 -453
  243. package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +0 -38
  244. package/src/index.ts +0 -664
  245. package/src/project-types.ts +0 -35
  246. package/src/project.ts +0 -434
  247. package/src/protocol.ts +0 -28
  248. package/src/types.ts +0 -9
@@ -7,7 +7,7 @@ import Darwin
7
7
 
8
8
  /// acquire must provide exclusive ownership until release. save must atomically replace
9
9
  /// the whole JSON snapshot or throw; it must never be interrupted by cancellation.
10
- public protocol RuntimeStorage: Sendable {
10
+ protocol RuntimeStorage: Sendable {
11
11
  func acquire() throws
12
12
  func load() throws -> JSON
13
13
  func save(_ state: JSON) throws
@@ -21,325 +21,79 @@ public protocol RuntimeStorage: Sendable {
21
21
  /// snapshot, under the same ownership. `load(entry:)` returns `.null` for an entry never saved.
22
22
  /// A storage without entries still works; the Runtime then cannot keep sends that arrive while a
23
23
  /// turn runs, and refuses them (`storage_error`) rather than overwrite the snapshot.
24
- public protocol KeyedRuntimeStorage: RuntimeStorage {
24
+ protocol KeyedRuntimeStorage: RuntimeStorage {
25
25
  func load(entry: String) throws -> JSON
26
26
  func save(entry: String, _ value: JSON) throws
27
27
  /// Deletes an entry for real; absence succeeds.
28
28
  func remove(entry: String) throws
29
29
  }
30
30
 
31
- /// A RuntimeStorage that also keeps the turn archive (F20.6): every finished turn in full, as
32
- /// JSON lines in entries named `archive<NNNN>` (100 turns each) beside the snapshot. Opt-in and
33
- /// separate from KeyedRuntimeStorage: a storage without it keeps no archive, typed
34
- /// (`archive_unavailable`), and everything else works. The Runtime keeps the rules (segments,
35
- /// the byte bound, the last record per turn); the storage keeps lines:
36
- /// - `appendLine` writes a newline first when the entry does not end with one, and is durable
37
- /// when it returns;
38
- /// - `readLines` returns complete lines only: a torn last line (a crash mid-append) is not one;
39
- /// - `replaceLines` is atomic: after a crash the entry holds every old line or every new one,
40
- /// never a mix and never none; `[]` deletes the entry;
41
- /// - `remove` deletes for real; an absent entry is not an error;
42
- /// - `entries` lists the entries whose names start with `prefix`, with their sizes in bytes.
43
- public protocol ArchiveRuntimeStorage: RuntimeStorage {
44
- func appendLine(entry: String, _ line: String) throws
45
- func readLines(entry: String) throws -> [String]
46
- func replaceLines(entry: String, _ lines: [String]) throws
47
- func remove(entry: String) throws
48
- func entries(prefix: String) throws -> [String: Int]
49
- }
50
-
51
31
  /// Private local file storage with a process-safe advisory lock and atomic replacement.
52
- /// Use an application-private directory; snapshots include runtime credentials. Entries are
53
- /// sidecar files beside the snapshot (`<file>.held`), each replaced atomically. The turn archive
54
- /// is sidecars too (`<file>.archive0001`, JSON lines), under the same lock; a segment is rewritten
55
- /// to `<file>.archive0001.tmp-<nonce>` and renamed over it, and stale temporaries are deleted when
56
- /// the storage is acquired.
57
- public final class FileRuntimeStorage: KeyedRuntimeStorage, ArchiveRuntimeStorage, @unchecked Sendable {
32
+ /// Use an application-private directory; snapshots include runtime credentials.
33
+ final class FileRuntimeStorage: KeyedRuntimeStorage, @unchecked Sendable {
58
34
  private let url: URL
59
35
  private let mutex = NSLock()
60
36
  private var descriptor: Int32 = -1
61
- /// Runs after a segment's temporary is complete and before the rename (tests inject a crash).
62
- var beforeCommit: ((URL) throws -> Void)?
63
- public init(url: URL) { self.url = url }
64
- public func acquire() throws {
37
+ init(url: URL) { self.url = url }
38
+ func acquire() throws {
65
39
  mutex.lock(); defer { mutex.unlock() }
66
- guard descriptor == -1 else { throw CrocheError("store_busy", "Storage is already owned") }
40
+ guard descriptor == -1 else { throw LegacyCrocheError("store_busy", "Storage is already owned") }
67
41
  try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
68
42
  let fd = open(url.path + ".lock", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)
69
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not open storage lock") }
43
+ guard fd >= 0 else { throw LegacyCrocheError("storage_error", "Could not open storage lock") }
70
44
  guard flock(fd, LOCK_EX | LOCK_NB) == 0 else {
71
- _ = close(fd); throw CrocheError("store_busy", "Storage is already owned")
45
+ _ = close(fd); throw LegacyCrocheError("store_busy", "Storage is already owned")
72
46
  }
73
47
  descriptor = fd
74
- removeStaleTemporaries()
75
48
  }
76
- public func load() throws -> JSON {
49
+ func load() throws -> JSON {
77
50
  mutex.lock(); defer { mutex.unlock() }
78
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
51
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
79
52
  if !FileManager.default.fileExists(atPath: url.path) { return .null }
80
53
  let saved = try JSON(data: Data(contentsOf: url))
81
- guard saved.object != nil else { throw CrocheError("storage_error", "Existing Runtime snapshot must be an object") }
54
+ guard saved.object != nil else { throw LegacyCrocheError("storage_error", "Existing Runtime snapshot must be an object") }
82
55
  return saved
83
56
  }
84
- public func save(_ state: JSON) throws {
57
+ func save(_ state: JSON) throws {
85
58
  mutex.lock(); defer { mutex.unlock() }
86
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
59
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
87
60
  try state.encoded().write(to: url, options: .atomic)
88
61
  _ = chmod(url.path, S_IRUSR | S_IWUSR)
89
62
  }
90
- public func load(entry: String) throws -> JSON {
63
+ func load(entry: String) throws -> JSON {
91
64
  mutex.lock(); defer { mutex.unlock() }
92
65
  let file = try sidecar(entry)
93
66
  if !FileManager.default.fileExists(atPath: file.path) { return .null }
94
67
  return try JSON(data: Data(contentsOf: file))
95
68
  }
96
- public func save(entry: String, _ value: JSON) throws {
69
+ func save(entry: String, _ value: JSON) throws {
97
70
  mutex.lock(); defer { mutex.unlock() }
98
71
  let file = try sidecar(entry)
99
72
  try value.encoded().write(to: file, options: .atomic)
100
73
  _ = chmod(file.path, S_IRUSR | S_IWUSR)
101
74
  }
102
75
  private func sidecar(_ entry: String) throws -> URL {
103
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
104
- guard !entry.isEmpty, entry.allSatisfy({ $0.isASCII && ($0.isLowercase || $0.isNumber) }) else { throw CrocheError("storage_error", "Invalid storage entry") }
76
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
77
+ guard !entry.isEmpty, entry.allSatisfy({ $0.isASCII && ($0.isLowercase || $0.isNumber) }) else { throw LegacyCrocheError("storage_error", "Invalid storage entry") }
105
78
  return URL(fileURLWithPath: url.path + "." + entry)
106
79
  }
107
- // MARK: The turn archive (F20.6)
108
- public func appendLine(entry: String, _ line: String) throws {
109
- mutex.lock(); defer { mutex.unlock() }
110
- let file = try segment(entry)
111
- guard !line.contains("\n") else { throw CrocheError("storage_error", "An archive line holds no newline") }
112
- let fd = open(file.path, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)
113
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not open the archive") }
114
- defer { _ = close(fd) }
115
- var info = stat()
116
- guard fstat(fd, &info) == 0 else { throw CrocheError("storage_error", "Could not read the archive") }
117
- var bytes = [UInt8]()
118
- if info.st_size > 0 {
119
- var last: UInt8 = 0
120
- guard pread(fd, &last, 1, off_t(info.st_size - 1)) == 1 else { throw CrocheError("storage_error", "Could not read the archive") }
121
- if last != 0x0a { bytes.append(0x0a) } // a torn line never swallows this record
122
- }
123
- bytes += Array(line.utf8); bytes.append(0x0a)
124
- try Self.writeAll(fd, bytes)
125
- guard fsync(fd) == 0 else { throw CrocheError("storage_error", "Could not sync the archive") }
126
- }
127
- public func readLines(entry: String) throws -> [String] {
128
- mutex.lock(); defer { mutex.unlock() }
129
- let file = try segment(entry)
130
- guard FileManager.default.fileExists(atPath: file.path) else { return [] }
131
- var lines = String(decoding: try Data(contentsOf: file), as: UTF8.self)
132
- .split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
133
- lines.removeLast() // the piece after the last newline: a torn line, or "" when whole
134
- return lines.filter { !$0.isEmpty }
135
- }
136
- public func replaceLines(entry: String, _ lines: [String]) throws {
137
- mutex.lock(); defer { mutex.unlock() }
138
- let file = try segment(entry)
139
- guard !lines.isEmpty else { return try unlinkIfPresent(file) }
140
- guard !lines.contains(where: { $0.contains("\n") }) else { throw CrocheError("storage_error", "An archive line holds no newline") }
141
- // '.' and '-' are outside the entry rule: a temporary is never an entry, `held` or `lock`.
142
- let nonce = UUID().uuidString.lowercased()
143
- let temporary = URL(fileURLWithPath: file.path + ".tmp-" + nonce)
144
- let fd = open(temporary.path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)
145
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not write the archive") }
146
- do {
147
- defer { _ = close(fd) }
148
- try Self.writeAll(fd, Array(lines.map { $0 + "\n" }.joined().utf8))
149
- guard fsync(fd) == 0 else { throw CrocheError("storage_error", "Could not sync the archive") }
150
- } catch { _ = unlink(temporary.path); throw error }
151
- do {
152
- try beforeCommit?(temporary)
153
- guard rename(temporary.path, file.path) == 0 else { throw CrocheError("storage_error", "Could not replace the archive") }
154
- } catch { _ = unlink(temporary.path); throw error }
155
- }
156
- public func remove(entry: String) throws {
80
+ func remove(entry: String) throws {
157
81
  mutex.lock(); defer { mutex.unlock() }
158
82
  try unlinkIfPresent(try sidecar(entry))
159
83
  }
160
- public func entries(prefix: String) throws -> [String: Int] {
161
- mutex.lock(); defer { mutex.unlock() }
162
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
163
- var found: [String: Int] = [:]
164
- for (name, path) in try siblings() {
165
- guard Self.isEntry(name), name.hasPrefix(prefix) else { continue }
166
- if let size = try? FileManager.default.attributesOfItem(atPath: path)[.size] as? NSNumber { found[name] = size.intValue }
167
- }
168
- return found
169
- }
170
- /// Line entries are segments only: the archive's (`archive` and four digits) and the message
171
- /// log's (`log` and six digits, S1). Never the snapshot's own sidecars (`held`) or its lock
172
- /// file (`lock`).
173
- private func segment(_ entry: String) throws -> URL {
174
- guard Self.isSegment(entry) else { throw CrocheError("storage_error", "Invalid archive entry") }
175
- return try sidecar(entry)
176
- }
177
- private static func isSegment(_ entry: Substring) -> Bool {
178
- let digits = { (n: Int) in entry.dropFirst(n).allSatisfy { $0.isASCII && $0.isNumber } }
179
- return (entry.count == 11 && entry.hasPrefix("archive") && digits(7))
180
- || (entry.count == 9 && entry.hasPrefix("log") && digits(3))
181
- }
182
- private static func isSegment(_ entry: String) -> Bool { isSegment(Substring(entry)) }
183
- private static func isEntry(_ name: String) -> Bool {
184
- !name.isEmpty && name.allSatisfy { $0.isASCII && ($0.isLowercase || $0.isNumber) }
185
- }
186
- /// The files beside the snapshot: `<file>.<suffix>`, as (suffix, path).
187
- private func siblings() throws -> [(String, String)] {
188
- let directory = url.deletingLastPathComponent().path, base = url.lastPathComponent + "."
189
- guard let names = try? FileManager.default.contentsOfDirectory(atPath: directory) else { return [] }
190
- return names.filter { $0.hasPrefix(base) }.map { (String($0.dropFirst(base.count)), directory + "/" + $0) }
191
- }
192
- /// Segment rewrites a crash interrupted (`archive<NNNN>.tmp-<nonce>`, `log<NNNNNN>.tmp-<nonce>`):
193
- /// deleted by the owner.
194
- private func removeStaleTemporaries() {
195
- for (suffix, path) in (try? siblings()) ?? [] {
196
- let parts = suffix.split(separator: ".", maxSplits: 1).map(String.init)
197
- guard parts.count == 2, Self.isSegment(parts[0]), parts[1].hasPrefix("tmp-") else { continue }
198
- _ = unlink(path)
199
- }
200
- }
201
84
  private func unlinkIfPresent(_ file: URL) throws {
202
- if unlink(file.path) != 0 && errno != ENOENT { throw CrocheError("storage_error", "Could not delete the archive") }
203
- }
204
- private static func writeAll(_ fd: Int32, _ bytes: [UInt8]) throws {
205
- var offset = 0
206
- while offset < bytes.count {
207
- let written = bytes[offset...].withUnsafeBytes { write(fd, $0.baseAddress, $0.count) }
208
- guard written > 0 else { throw CrocheError("storage_error", "Could not write the archive") }
209
- offset += written
210
- }
85
+ if unlink(file.path) != 0 && errno != ENOENT { throw LegacyCrocheError("storage_error", "Could not delete the storage entry") }
211
86
  }
212
-
213
- public func remove() throws {
87
+ func remove() throws {
214
88
  try mutex.withLock {
215
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
89
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
216
90
  try unlinkIfPresent(url)
217
91
  }
218
92
  }
219
93
 
220
- public func release() {
94
+ func release() {
221
95
  mutex.lock(); defer { mutex.unlock() }
222
96
  if descriptor >= 0 { _ = flock(descriptor, LOCK_UN); _ = close(descriptor); descriptor = -1 }
223
97
  }
224
98
  deinit { release() }
225
99
  }
226
-
227
- // Project namespace, independent of every app-supplied RuntimeStorage. Core mints and
228
- // validates the record; this host only owns its file, atomic replacement and flock.
229
- final class ProjectRecordFiles: @unchecked Sendable {
230
- private let directory: @Sendable () -> URL?
231
- private let mutex = NSLock()
232
- private var descriptors: [String: Int32] = [:]
233
- // Internal host-test fences; configured before the host is handed to Core.
234
- var afterRead: (@Sendable (String, String?) -> Void)?
235
- var beforeLock: (@Sendable (String) -> Void)?
236
- var afterWrite: (@Sendable (String) -> Void)?
237
- init(directory: @escaping @Sendable () -> URL? = ProjectRecordFiles.defaultDirectory) { self.directory = directory }
238
- static var hasPrivateStorage: Bool {
239
- #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
240
- true
241
- #else
242
- false
243
- #endif
244
- }
245
- // Policy seam lets host tests exercise a platform without a default on Apple too.
246
- static func forOpen(projectDirectory: URL?, hasPrivateStorage: Bool = ProjectRecordFiles.hasPrivateStorage) throws -> ProjectRecordFiles {
247
- if hasPrivateStorage {
248
- guard projectDirectory == nil else {
249
- throw CrocheError("invalid_configuration", "this platform keeps the project record in the app's private storage")
250
- }
251
- return ProjectRecordFiles()
252
- }
253
- guard let projectDirectory else {
254
- throw CrocheError("storage_error", "This platform requires projectDirectory for the project record")
255
- }
256
- return ProjectRecordFiles(directory: { projectDirectory })
257
- }
258
- static func defaultDirectory() -> URL? {
259
- #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
260
- return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
261
- #else
262
- return nil
263
- #endif
264
- }
265
- static func owns(_ key: String) -> Bool { key.hasPrefix("croche.project.") }
266
- func file(_ key: String) throws -> URL {
267
- let prefix = "croche.project.v1."
268
- let hash = key.dropFirst(prefix.count)
269
- guard key.hasPrefix(prefix), hash.count == 64, hash.allSatisfy({ $0.isASCII && ($0.isNumber || ("a"..."f").contains(String($0))) }) else {
270
- throw CrocheError("storage_error", "Unsupported project record key")
271
- }
272
- guard let directory = directory() else {
273
- throw CrocheError("storage_error", "Project record needs app-private storage or an explicit projectDirectory")
274
- }
275
- return directory.appendingPathComponent("croche.project.v1.\(hash).json")
276
- }
277
- private func io<T>(_ body: () throws -> T) throws -> T {
278
- do { return try body() }
279
- catch let error as CrocheError { throw error }
280
- catch { throw CrocheError("storage_error", "Could not access the project record directory") }
281
- }
282
- func acquire(_ key: String) throws {
283
- beforeLock?(key)
284
- try io { try mutex.withLock {
285
- guard descriptors[key] == nil else { throw CrocheError("store_busy", "Project record is already owned") }
286
- let url = try file(key)
287
- try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
288
- let fd = open(url.path + ".lock", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)
289
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not open project record lock") }
290
- guard flock(fd, LOCK_EX | LOCK_NB) == 0 else {
291
- _ = close(fd); throw CrocheError("store_busy", "Project record is already owned")
292
- }
293
- descriptors[key] = fd
294
- } }
295
- }
296
- private func ownedFile(_ key: String) throws -> URL {
297
- guard descriptors[key] != nil else { throw CrocheError("storage_error", "Project record is not owned") }
298
- return try file(key)
299
- }
300
- func read(_ key: String) throws -> String? {
301
- let value = try io { try mutex.withLock {
302
- let url = try ownedFile(key)
303
- guard FileManager.default.fileExists(atPath: url.path) else { return nil as String? }
304
- return try String(contentsOf: url, encoding: .utf8)
305
- } }
306
- afterRead?(key, value)
307
- return value
308
- }
309
- func write(_ key: String, _ value: String) throws {
310
- try io { try mutex.withLock {
311
- let url = try ownedFile(key)
312
- let temporary = URL(fileURLWithPath: url.path + ".tmp-" + UUID().uuidString)
313
- let fd = open(temporary.path, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR)
314
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not write project record") }
315
- defer { _ = close(fd); _ = unlink(temporary.path) }
316
- let bytes = Array(value.utf8)
317
- var offset = 0
318
- while offset < bytes.count {
319
- let count = bytes[offset...].withUnsafeBytes {
320
- #if canImport(Glibc)
321
- return Glibc.write(fd, $0.baseAddress, $0.count)
322
- #else
323
- return Darwin.write(fd, $0.baseAddress, $0.count)
324
- #endif
325
- }
326
- if count < 0 && errno == EINTR { continue }
327
- guard count > 0 else { throw CrocheError("storage_error", "Could not write project record") }
328
- offset += count
329
- }
330
- guard fsync(fd) == 0, rename(temporary.path, url.path) == 0 else {
331
- throw CrocheError("storage_error", "Could not commit project record")
332
- }
333
- } }
334
- afterWrite?(key)
335
- }
336
- func release(_ key: String) {
337
- mutex.withLock {
338
- if let fd = descriptors.removeValue(forKey: key) { _ = flock(fd, LOCK_UN); _ = close(fd) }
339
- }
340
- }
341
- var held: Bool { mutex.withLock { !descriptors.isEmpty } }
342
- deinit {
343
- for fd in descriptors.values { _ = flock(fd, LOCK_UN); _ = close(fd) }
344
- }
345
- }