@h1r-ai/croche-react-native 0.5.5 → 0.6.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.
- package/README.md +69 -458
- package/THIRD-PARTY-NOTICES +7 -8
- package/android/build.gradle +6 -1
- package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +125 -0
- package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +81 -0
- package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +83 -0
- package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +45 -824
- package/android/src/main/java/dev/croche/reactnative/NativePal.kt +51 -0
- package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
- package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +42 -41
- package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +71 -0
- package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +29 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
- package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +6 -6
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +51 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +8 -8
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +121 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +9 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +5 -2
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +45 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +16 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +60 -105
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +47 -107
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +41 -40
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +67 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +20 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +18 -20
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +322 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +71 -34
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +9 -7
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +47 -19
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +5 -5
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +1 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +9 -9
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +108 -37
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +44 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +40 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +8 -8
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StoragePal.kt +18 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +50 -25
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +22 -22
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +8285 -2159
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +25 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +31 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +128 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +72 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +45 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +79 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
- package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
- package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
- package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
- package/dist/package.json +1 -0
- package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
- package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
- package/dist/react-native/src/core-native.d.ts +3 -0
- package/dist/react-native/src/core-native.js +193 -0
- package/dist/react-native/src/core-wire.d.ts +38 -0
- package/dist/react-native/src/core-wire.js +1 -0
- package/dist/react-native/src/native-module.d.ts +8 -0
- package/dist/react-native/src/native-module.js +53 -0
- package/dist/rn-shared-build.json +16 -0
- package/dist/runtime/src/agent.d.ts +43 -0
- package/dist/runtime/src/agent.js +75 -0
- package/dist/runtime/src/app-api.d.ts +284 -0
- package/dist/runtime/src/app-api.js +1 -0
- package/dist/runtime/src/cancel.d.ts +9 -0
- package/dist/runtime/src/cancel.js +24 -0
- package/dist/runtime/src/core/port.d.ts +234 -0
- package/dist/runtime/src/core/port.js +1 -0
- package/dist/runtime/src/core/wasm.d.ts +1 -0
- package/dist/runtime/src/core/wasm.js +2 -0
- package/dist/runtime/src/croche.d.ts +42 -0
- package/dist/runtime/src/croche.js +111 -0
- package/dist/runtime/src/errors.d.ts +26 -0
- package/dist/runtime/src/errors.js +49 -0
- package/dist/runtime/src/events.d.ts +5 -0
- package/dist/runtime/src/events.js +31 -0
- package/dist/runtime/src/generated/app-stream.d.ts +174 -0
- package/dist/runtime/src/generated/app-stream.js +124 -0
- package/dist/runtime/src/index.native.d.ts +21 -0
- package/dist/runtime/src/index.native.js +8 -0
- package/dist/runtime/src/json.d.ts +13 -0
- package/dist/runtime/src/json.js +120 -0
- package/dist/runtime/src/native-adapter.d.ts +3 -0
- package/dist/runtime/src/native-adapter.js +24 -0
- package/dist/runtime/src/pal/codec.d.ts +15 -0
- package/dist/runtime/src/pal/codec.js +52 -0
- package/dist/runtime/src/pal/host.d.ts +5 -0
- package/dist/runtime/src/pal/host.js +35 -0
- package/dist/runtime/src/pal/platform.d.ts +16 -0
- package/dist/runtime/src/pal/platform.js +7 -0
- package/dist/runtime/src/rn-entry.d.ts +1 -0
- package/dist/runtime/src/rn-entry.js +4 -0
- package/dist/runtime/src/run-tool.d.ts +33 -0
- package/dist/runtime/src/run-tool.js +7 -0
- package/dist/runtime/src/tools.d.ts +19 -0
- package/dist/runtime/src/tools.js +70 -0
- package/ios/CoreAgentTransport.swift +14 -0
- package/ios/CoreDeliveryTransport.swift +58 -0
- package/ios/CoreNativePal.swift +93 -0
- package/ios/CorePlatformTransport.swift +44 -0
- package/ios/CoreWireDispatch.swift +92 -0
- package/ios/CoreWireSupport.swift +111 -0
- package/ios/CrocheRuntimeBridge.swift +34 -994
- package/ios/ProjectPal.swift +46 -0
- package/ios/RNCrocheRuntime.mm +18 -201
- package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
- package/ios/vendor/CrocheRuntimeSDK/Background.swift +11 -11
- package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +64 -0
- package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
- package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
- package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +85 -43
- package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +443 -0
- package/ios/vendor/CrocheRuntimeSDK/Host.swift +122 -61
- package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
- package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
- package/ios/vendor/CrocheRuntimeSDK/JSON.swift +4 -4
- package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
- package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +37 -36
- package/ios/vendor/CrocheRuntimeSDK/Platform.swift +35 -35
- package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +285 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +266 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +195 -0
- package/ios/vendor/CrocheRuntimeSDK/Project.swift +209 -95
- package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +97 -123
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +195 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +161 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +234 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +28 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +177 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +61 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +95 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +105 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +209 -0
- package/ios/vendor/CrocheRuntimeSDK/Storage.swift +52 -52
- package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +180 -137
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +16 -10
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +131 -116
- package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
- package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +7209 -2803
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +1342 -218
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +1342 -218
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
- package/package.json +18 -8
- package/scripts/README.md +11 -0
- package/scripts/build-shared.mjs +46 -0
- package/scripts/build.mjs +11 -0
- package/scripts/vendor-native.sh +5 -4
- package/src/NativeCrocheRuntime.ts +12 -159
- package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
- package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
- package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
- package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
- package/src/index.ts +0 -664
- package/src/project-types.ts +0 -35
- package/src/project.ts +0 -434
- package/src/protocol.ts +0 -28
- package/src/types.ts +0 -9
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonParser
|
|
4
|
+
import dev.croche.AppStreamContract
|
|
5
|
+
import dev.croche.ffi.CoreDelivery
|
|
6
|
+
import dev.croche.ffi.CoreEventSink
|
|
7
|
+
import kotlinx.coroutines.CoroutineDispatcher
|
|
8
|
+
import kotlinx.coroutines.runBlocking
|
|
9
|
+
|
|
10
|
+
/** A Core event the generated reader cannot read (e.g. an error category outside the closed set).
|
|
11
|
+
* It is an SDK/Core fault, not the app's: [appEventSink] throws it from `deliver` BEFORE entry, so
|
|
12
|
+
* the handler never runs and is never blamed (no `threw` / `handler_failed`). The sink has no error
|
|
13
|
+
* codes (Atlas's PAL error contract): UniFFI hands this untyped exception to Core as an unexpected
|
|
14
|
+
* callback error, and Core completes the delivery `unavailable`, ending only this subscription.
|
|
15
|
+
* Swift's `MalformedEvent` is the same rule. */
|
|
16
|
+
internal class MalformedEvent(cause: Throwable) : RuntimeException("Core's event is not an AgentEvent: ${cause.message}", cause)
|
|
17
|
+
|
|
18
|
+
/** Core owns the serial queue. Decode on Core's stack, dispatch to the platform UI thread, enter
|
|
19
|
+
* immediately before invoking the handler, and complete exactly once after the suspend handler settles. */
|
|
20
|
+
internal fun appEventSink(dispatcher: CoroutineDispatcher, handler: suspend (AppStreamContract.AgentEvent) -> Unit): CoreEventSink =
|
|
21
|
+
object : CoreEventSink {
|
|
22
|
+
override fun deliver(deliveryId: String, eventJson: String, delivery: CoreDelivery) {
|
|
23
|
+
val event = try { AppStreamContract.decode(JsonParser.parseString(eventJson).asJsonObject) }
|
|
24
|
+
catch (error: Exception) { throw MalformedEvent(error) }
|
|
25
|
+
runBlocking(dispatcher) {
|
|
26
|
+
if (!delivery.tryEnter()) return@runBlocking
|
|
27
|
+
val outcome = try {
|
|
28
|
+
handler(event)
|
|
29
|
+
"""{"outcome":"returned"}"""
|
|
30
|
+
} catch (_: Throwable) { """{"outcome":"threw"}""" }
|
|
31
|
+
delivery.complete(outcome)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.LockOutcome
|
|
4
|
+
import dev.croche.StoragePal
|
|
5
|
+
import dev.croche.ffi.*
|
|
6
|
+
import kotlinx.coroutines.Dispatchers
|
|
7
|
+
import kotlinx.coroutines.runBlocking
|
|
8
|
+
|
|
9
|
+
/** Core invokes these on its effect worker. The wrapper only bridges suspend app I/O. */
|
|
10
|
+
internal fun appStorage(storage: StoragePal): CoreStoragePal = object : CoreStoragePal {
|
|
11
|
+
private fun <T> call(block: suspend () -> T): T = storageCallback { runBlocking(Dispatchers.IO) { block() } }
|
|
12
|
+
override fun read(key: String) = call { storage.read(key) }
|
|
13
|
+
override fun write(key: String, value: String) {
|
|
14
|
+
call { storage.write(key, value) }
|
|
15
|
+
}
|
|
16
|
+
override fun remove(key: String) = call { storage.remove(key) }
|
|
17
|
+
override fun list(prefix: String) = call { storage.list(prefix) }
|
|
18
|
+
override fun lock(name: String) = call {
|
|
19
|
+
when (storage.lock(name)) { LockOutcome.ACQUIRED -> PalLockOutcome.ACQUIRED; LockOutcome.HELD -> PalLockOutcome.HELD }
|
|
20
|
+
}
|
|
21
|
+
override fun unlock(name: String) = call { storage.unlock(name) }
|
|
22
|
+
override fun readSecret(key: String) = call { storage.secrets.read(key) }
|
|
23
|
+
override fun writeSecret(key: String, value: String) = call { storage.secrets.write(key, value) }
|
|
24
|
+
override fun removeSecret(key: String) = call { storage.secrets.remove(key) }
|
|
25
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.CoreBackgroundWake
|
|
4
|
+
import dev.croche.ffi.CorePalException
|
|
5
|
+
|
|
6
|
+
/** Only the platform install/replace acknowledgement; Core owns retry and wake policy. */
|
|
7
|
+
internal class BackgroundOsRegistration(private val enqueue: (String, Boolean, Long) -> Unit) {
|
|
8
|
+
private var identifier: String? = null
|
|
9
|
+
@Volatile private var callback: CoreBackgroundWake? = null
|
|
10
|
+
|
|
11
|
+
@Synchronized fun install(name: String, wake: CoreBackgroundWake) {
|
|
12
|
+
val previous = callback
|
|
13
|
+
callback = wake // The Core object holds a WEAK service reference, not a Croche.
|
|
14
|
+
try {
|
|
15
|
+
enqueue(name, true, 0)
|
|
16
|
+
identifier = name
|
|
17
|
+
} catch (error: Exception) {
|
|
18
|
+
callback = previous
|
|
19
|
+
if (error is InterruptedException) Thread.currentThread().interrupt()
|
|
20
|
+
throw CorePalException.Failure("host_unavailable", "OS background registration failed")
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@Synchronized fun schedule(earliest: Long?, now: Long) {
|
|
25
|
+
val name = identifier ?: return
|
|
26
|
+
if (earliest == null) return
|
|
27
|
+
try { enqueue(name, false, if (earliest <= now) 0 else earliest - now) }
|
|
28
|
+
catch (error: Exception) {
|
|
29
|
+
if (error is InterruptedException) Thread.currentThread().interrupt()
|
|
30
|
+
throw CorePalException.Failure("host_unavailable", "OS background scheduling failed")
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fun wake(): CoreBackgroundWake? = callback
|
|
35
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonParser
|
|
4
|
+
import dev.croche.BackgroundEvent
|
|
5
|
+
import dev.croche.ffi.CoreEventSink
|
|
6
|
+
import dev.croche.ffi.CoreDelivery
|
|
7
|
+
import kotlinx.coroutines.CoroutineDispatcher
|
|
8
|
+
import kotlinx.coroutines.runBlocking
|
|
9
|
+
|
|
10
|
+
/** Core owns event policy. Unknown object fields/types survive; malformed Core data is dropped. */
|
|
11
|
+
internal fun backgroundEvent(json: String): BackgroundEvent? {
|
|
12
|
+
val value = try { JsonParser.parseString(json) } catch (_: RuntimeException) { return null }
|
|
13
|
+
if (!value.isJsonObject) return null
|
|
14
|
+
val fields = value.asJsonObject
|
|
15
|
+
fun string(key: String) = fields.get(key)?.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }?.asString
|
|
16
|
+
val type = string("type") ?: return null
|
|
17
|
+
return BackgroundEvent(type, string("at"), string("reason"), fields)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
internal fun backgroundSink(dispatcher: CoroutineDispatcher, handler: (BackgroundEvent) -> Unit): CoreEventSink =
|
|
21
|
+
object : CoreEventSink {
|
|
22
|
+
override fun deliver(deliveryId: String, eventJson: String, delivery: CoreDelivery) = runBlocking(dispatcher) {
|
|
23
|
+
val value = backgroundEvent(eventJson)
|
|
24
|
+
if (value == null) {
|
|
25
|
+
// A malformed Core event cannot enter app code. Report the binding fact so
|
|
26
|
+
// Core can retire this flight rather than leave its queue waiting forever.
|
|
27
|
+
delivery.complete("""{"outcome":"unavailable"}""")
|
|
28
|
+
return@runBlocking
|
|
29
|
+
}
|
|
30
|
+
if (!delivery.tryEnter()) return@runBlocking
|
|
31
|
+
val outcome = try {
|
|
32
|
+
handler(value)
|
|
33
|
+
"""{"outcome":"returned"}"""
|
|
34
|
+
} catch (_: Throwable) { """{"outcome":"threw"}""" }
|
|
35
|
+
delivery.complete(outcome)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonElement
|
|
4
|
+
import com.google.gson.JsonNull
|
|
5
|
+
import dev.croche.BindingFacts
|
|
6
|
+
import dev.croche.ffi.CoreStoragePal
|
|
7
|
+
import dev.croche.ffi.coreWebSearch
|
|
8
|
+
import dev.croche.ffi.CoreToolRegistration
|
|
9
|
+
import dev.croche.ffi.CoreToolDefinition
|
|
10
|
+
import dev.croche.ffi.CoreToolExecute
|
|
11
|
+
import dev.croche.ffi.CoreEventSink
|
|
12
|
+
import dev.croche.ffi.HostPal
|
|
13
|
+
import dev.croche.ffi.coreRegisterBackground
|
|
14
|
+
|
|
15
|
+
/** Direct generated constructor: Core validates the manifest and owns tool identity/metadata.
|
|
16
|
+
* No handwritten built-in definition or callback-result policy is supplied by this adapter. */
|
|
17
|
+
internal object ToolDefinitionCalls {
|
|
18
|
+
fun defineTool(manifest: JsonElement, execute: CoreToolExecute): CoreToolDefinition =
|
|
19
|
+
CoreToolDefinition(manifest.toString(), attached(execute))
|
|
20
|
+
|
|
21
|
+
val webSearch: CoreToolDefinition get() = coreWebSearch()
|
|
22
|
+
|
|
23
|
+
/** Replace only the opaque reference. Unknown/malformed registration options reach Core. */
|
|
24
|
+
fun registration(tool: CoreToolDefinition, wrapper: JsonElement? = null): CoreToolRegistration {
|
|
25
|
+
val options = wrapper?.deepCopy()?.also { if (it.isJsonObject) it.asJsonObject.add("tool", JsonNull.INSTANCE) }
|
|
26
|
+
return CoreToolRegistration(tool, options?.toString())
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Direct generated function; Core owns enrollment, wake policy, delivery and retries. */
|
|
31
|
+
internal object BackgroundCalls {
|
|
32
|
+
fun registerBackground(optionsJson: String, storage: CoreStoragePal, host: HostPal, sink: CoreEventSink? = null, platformStorage: CoreStoragePal? = null,
|
|
33
|
+
bindingFactsJson: String = BindingFacts.json()) =
|
|
34
|
+
coreRegisterBackground(optionsJson, attached(sink), attached(storage), attached(platformStorage), attached(host), bindingFactsJson)
|
|
35
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.BindingFacts
|
|
4
|
+
import dev.croche.ffi.*
|
|
5
|
+
import kotlinx.coroutines.Dispatchers
|
|
6
|
+
import kotlinx.coroutines.withContext
|
|
7
|
+
|
|
8
|
+
/** One native factory. No SDK project/agent registry, config resolver or fallback factory. */
|
|
9
|
+
internal object CoreFactoryCalls {
|
|
10
|
+
/** [bindingFactsJson]: the Kotlin SDK's own facts, or the RN Android bridge's ("react-native"). */
|
|
11
|
+
suspend fun open(openJson: String, registrations: List<CoreToolRegistration>, storage: CoreStoragePal, host: HostPal,
|
|
12
|
+
bindingFactsJson: String = BindingFacts.json(), recoveryJson: String? = null): CoreCroche =
|
|
13
|
+
withContext(Dispatchers.IO) { GeneratedLifecycleBinding.open(openJson, registrations, attached(storage), attached(host), bindingFactsJson, recoveryJson) }
|
|
14
|
+
|
|
15
|
+
suspend fun wakeTimes(openJson: String, queryJson: String?, storage: CoreStoragePal, host: HostPal): String =
|
|
16
|
+
withContext(Dispatchers.IO) { coreWakeTimes(openJson, queryJson, attached(storage), attached(host)) }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Facade groupings forward to the same Core object; they keep no state of their own. */
|
|
20
|
+
internal class CrocheCalls(private val core: CoreCrocheInterface) {
|
|
21
|
+
val agentIds: List<String> get() = core.agentIds()
|
|
22
|
+
fun agent(id: String, scopeJson: String?) = core.agent(id, scopeJson)
|
|
23
|
+
suspend fun deliverDue(optionsJson: String?, cancel: CancelToken?): String =
|
|
24
|
+
withContext(Dispatchers.IO) { core.deliverDue(optionsJson, cancel) }
|
|
25
|
+
fun wikiPut(docJson: String) = core.wikiPut(docJson)
|
|
26
|
+
fun wikiRemove(path: String) = core.wikiRemove(path)
|
|
27
|
+
fun wikiList(): String = core.wikiList()
|
|
28
|
+
fun debugAttach(optionsJson: String): String = core.debugAttach(optionsJson)
|
|
29
|
+
fun debugDetach() = core.debugDetach()
|
|
30
|
+
fun close() = core.shutdown()
|
|
31
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonNull
|
|
4
|
+
import com.google.gson.JsonObject
|
|
5
|
+
import com.google.gson.JsonParser
|
|
6
|
+
import dev.croche.ffi.CoreStoragePal
|
|
7
|
+
import dev.croche.ffi.CoreToolRegistration
|
|
8
|
+
import dev.croche.ffi.CoreEventSink
|
|
9
|
+
|
|
10
|
+
/** Internal marshalling only. Public typed options do not admit undeclared keys.
|
|
11
|
+
* Core receives every serializable value unchanged, with only opaque references replaced.
|
|
12
|
+
*/
|
|
13
|
+
internal object CoreOptionsEncoding {
|
|
14
|
+
data class Encoded(
|
|
15
|
+
val optionsJson: String,
|
|
16
|
+
val registrations: List<CoreToolRegistration>,
|
|
17
|
+
val storage: CoreStoragePal,
|
|
18
|
+
val sink: CoreEventSink?,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
fun encode(
|
|
22
|
+
options: JsonObject,
|
|
23
|
+
registrations: List<CoreToolRegistration>?,
|
|
24
|
+
platformStorage: CoreStoragePal,
|
|
25
|
+
customStorage: CoreStoragePal? = null,
|
|
26
|
+
sink: CoreEventSink? = null,
|
|
27
|
+
): Encoded {
|
|
28
|
+
val json = options.deepCopy()
|
|
29
|
+
if (registrations != null) {
|
|
30
|
+
val tools = com.google.gson.JsonArray()
|
|
31
|
+
registrations.forEach { registration ->
|
|
32
|
+
tools.add(registration.optionsJson?.let { JsonParser.parseString(it) } ?: JsonNull.INSTANCE)
|
|
33
|
+
}
|
|
34
|
+
json.add("tools", tools)
|
|
35
|
+
}
|
|
36
|
+
// The PAL argument is always real. Only an app-supplied PAL gets a JSON marker.
|
|
37
|
+
if (customStorage != null) json.add("storage", JsonNull.INSTANCE) else json.remove("storage")
|
|
38
|
+
if (sink != null) json.add("sink", JsonNull.INSTANCE)
|
|
39
|
+
return Encoded(json.toString(), registrations ?: emptyList(), customStorage ?: platformStorage, sink)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.CancelToken
|
|
4
|
+
import dev.croche.ffi.CoreImageInput
|
|
5
|
+
import dev.croche.ffi.CoreAgentInterface
|
|
6
|
+
import dev.croche.ffi.CoreEventSink
|
|
7
|
+
import dev.croche.ffi.CoreSubscriptionInterface
|
|
8
|
+
import kotlinx.coroutines.Dispatchers
|
|
9
|
+
import kotlinx.coroutines.withContext
|
|
10
|
+
|
|
11
|
+
/** Thin calls to S1/S3's generated agent, not a staging port or a second request state machine.
|
|
12
|
+
* JSON/options/cancellation are forwarded unchanged; Core owns all outcomes and queues. */
|
|
13
|
+
internal class AgentStreamCalls(private val agent: CoreAgentInterface) {
|
|
14
|
+
suspend fun send(inputJson: String, images: List<CoreImageInput> = emptyList(), optionsJson: String? = null, cancel: CancelToken? = null): String =
|
|
15
|
+
coreStreamIO { agent.send(inputJson, images, optionsJson, cancel) }
|
|
16
|
+
|
|
17
|
+
suspend fun request(inputJson: String, images: List<CoreImageInput> = emptyList(), optionsJson: String? = null, cancel: CancelToken? = null): String =
|
|
18
|
+
coreStreamIO { agent.request(inputJson, images, optionsJson, cancel) }
|
|
19
|
+
|
|
20
|
+
fun subscribe(selectionJson: String, eventSink: CoreEventSink): SubscriptionCalls =
|
|
21
|
+
SubscriptionCalls(agent.subscribe(selectionJson, attached(eventSink)))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** History uses the original subscription sink. No SDK queue, lifetime flag or callback cache. */
|
|
25
|
+
internal class SubscriptionCalls(private val subscription: CoreSubscriptionInterface) {
|
|
26
|
+
suspend fun history(optionsJson: String) = coreStreamIO { subscription.history(optionsJson) }
|
|
27
|
+
fun close() = subscription.shutdown()
|
|
28
|
+
val ended: Boolean get() = subscription.ended()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Carry failures as values across the coroutine dispatcher boundary, then throw the original.
|
|
32
|
+
* Otherwise coroutine stack-trace recovery may copy a Core/host exception on the caller's stack.
|
|
33
|
+
*/
|
|
34
|
+
private suspend fun <T> coreStreamIO(block: () -> T): T =
|
|
35
|
+
withContext(Dispatchers.IO) { runCatching(block) }.getOrThrow()
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.sun.jna.Native
|
|
4
|
+
import dev.croche.ffi.*
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Keeps a Core thread attached while a Kotlin callback runs.
|
|
8
|
+
*
|
|
9
|
+
* Core calls Kotlin from its own native threads, and a callback can call Core again on the same
|
|
10
|
+
* thread: `HostPal.http` hands each piece of the body to `HttpSink.chunk`, inside which Core delivers
|
|
11
|
+
* stream deltas and asks `nowMs`/`random`; an app listener or tool can call the runtime. JNA 5.17.0
|
|
12
|
+
* recomputes "detach on return" for every callback, a nested one too, from the thread state the
|
|
13
|
+
* outer attach left (native/callback.c:750), so the inner return detached the thread under the
|
|
14
|
+
* still-running outer frame. ART aborts on that ("attempting to detach while still running code");
|
|
15
|
+
* HotSpot refuses it and prints "JNA: could not detach thread".
|
|
16
|
+
*/
|
|
17
|
+
internal fun keepCallbackThreadAttached() = Native.detach(false)
|
|
18
|
+
|
|
19
|
+
/** Per thread: Kotlin callbacks running now, and whether the thread is the VM's own (JNA never detaches it). */
|
|
20
|
+
private class CallbackThread { var depth = 0; var vmThread = false }
|
|
21
|
+
private val callbackThread = ThreadLocal.withInitial { CallbackThread() }
|
|
22
|
+
|
|
23
|
+
/** Tests only: every JNA callback frame on the stack must be one of ours (one per depth). */
|
|
24
|
+
private val checkFrames = System.getProperty("croche.test.foreignCallbackFrames") == "true"
|
|
25
|
+
private fun checkFrames(depth: Int) {
|
|
26
|
+
val frames = Thread.currentThread().stackTrace
|
|
27
|
+
.count { it.methodName == "callback" && it.className == "com.sun.jna.CallbackReference\$DefaultCallbackProxy" }
|
|
28
|
+
if (frames != depth + 1) System.err.println("Croche: foreign callback outside hostCallback ($frames JNA frames, depth $depth)")
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Every Kotlin object Core can call enters here: each Core-facing interface is wrapped below, and an
|
|
33
|
+
* object is wrapped where it is handed to Core. With every entry counted, depth 0 is the outermost
|
|
34
|
+
* callback on the thread; it gives back JNA's usual detach on return, so a long-lived Core thread
|
|
35
|
+
* (croche-timer, croche-drive) is not left attached: as a non-daemon it would hold the JVM's exit.
|
|
36
|
+
* Core also calls back on the app's own thread during a call; JNA refuses to detach that one, which
|
|
37
|
+
* is learned once per thread.
|
|
38
|
+
*/
|
|
39
|
+
internal fun <T> hostCallback(block: () -> T): T {
|
|
40
|
+
val thread = callbackThread.get()
|
|
41
|
+
if (checkFrames) checkFrames(thread.depth)
|
|
42
|
+
val release = thread.depth == 0 && !thread.vmThread
|
|
43
|
+
thread.depth++
|
|
44
|
+
keepCallbackThreadAttached()
|
|
45
|
+
try { return block() } finally {
|
|
46
|
+
thread.depth--
|
|
47
|
+
if (release) try { Native.detach(true) } catch (_: IllegalStateException) { thread.vmThread = true }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A new member of a Core-facing interface must be added to its wrapper. */
|
|
52
|
+
internal class AttachedHostPal(private val host: HostPal) : HostPal {
|
|
53
|
+
private fun <T> enter(block: () -> T): T = hostCallback(block)
|
|
54
|
+
override fun streamPlatform() = enter { host.streamPlatform() }
|
|
55
|
+
override fun imageCodecAvailable() = enter { host.imageCodecAvailable() }
|
|
56
|
+
override fun readImage(request: PalImageReadRequest, cancel: CancelToken) = enter { host.readImage(request, cancel) }
|
|
57
|
+
override fun prepareImage(source: PalImageBytes, plan: PalImagePreparePlan, cancel: CancelToken) = enter { host.prepareImage(source, plan, cancel) }
|
|
58
|
+
override fun releaseImage(capture: String) = enter { host.releaseImage(capture) }
|
|
59
|
+
override fun decodeImage(bytes: ByteArray, options: PalImageDecodeOptions, cancel: CancelToken) = enter { host.decodeImage(bytes, options, cancel) }
|
|
60
|
+
override fun renderImage(request: PalImageRenderRequest, cancel: CancelToken) = enter { host.renderImage(request, cancel) }
|
|
61
|
+
override fun releaseDecodedImage(handle: String) = enter { host.releaseDecodedImage(handle) }
|
|
62
|
+
override fun http(request: PalHttpRequest, sink: HttpSink, cancel: CancelToken) = enter { host.http(request, sink, cancel) }
|
|
63
|
+
override fun resolve(host: String, cancel: CancelToken) = enter { this.host.resolve(host, cancel) }
|
|
64
|
+
override fun platformFacts() = enter { host.platformFacts() }
|
|
65
|
+
override fun readAsset(path: String) = enter { host.readAsset(path) }
|
|
66
|
+
override fun list(prefix: String) = enter { host.list(prefix) }
|
|
67
|
+
override fun read(key: String) = enter { host.read(key) }
|
|
68
|
+
override fun write(key: String, value: String) = enter { host.write(key, value) }
|
|
69
|
+
override fun readSecret(key: String) = enter { host.readSecret(key) }
|
|
70
|
+
override fun writeSecret(key: String, value: String) = enter { host.writeSecret(key, value) }
|
|
71
|
+
override fun remove(key: String) = enter { host.remove(key) }
|
|
72
|
+
override fun removeSecret(key: String) = enter { host.removeSecret(key) }
|
|
73
|
+
override fun lock(key: String) = enter { host.lock(key) }
|
|
74
|
+
override fun unlock(key: String) = enter { host.unlock(key) }
|
|
75
|
+
override fun storeIdentity() = enter { host.storeIdentity() }
|
|
76
|
+
override fun tool(invocation: String, cancel: CancelToken) = enter { host.tool(invocation, cancel) }
|
|
77
|
+
override fun attest(request: String, timeoutMs: ULong) = enter { host.attest(request, timeoutMs) }
|
|
78
|
+
override fun nowMs() = enter { host.nowMs() }
|
|
79
|
+
override fun utcOffsetMinutes(atMs: Double) = enter { host.utcOffsetMinutes(atMs) }
|
|
80
|
+
override fun random() = enter { host.random() }
|
|
81
|
+
override fun wake(atMs: Double?) = enter { host.wake(atMs) }
|
|
82
|
+
override fun schedule(entries: String) = enter { host.schedule(entries) }
|
|
83
|
+
override fun registerBackground(identifier: String, wake: CoreBackgroundWake) = enter { host.registerBackground(identifier, wake) }
|
|
84
|
+
override fun archiveAppendLine(entry: String, line: String) = enter { host.archiveAppendLine(entry, line) }
|
|
85
|
+
override fun archiveReadLines(entry: String) = enter { host.archiveReadLines(entry) }
|
|
86
|
+
override fun archiveReplaceLines(entry: String, lines: List<String>) = enter { host.archiveReplaceLines(entry, lines) }
|
|
87
|
+
override fun archiveRemove(entry: String) = enter { host.archiveRemove(entry) }
|
|
88
|
+
override fun archiveEntries(prefix: String) = enter { host.archiveEntries(prefix) }
|
|
89
|
+
override fun storageCapability() = enter { host.storageCapability() }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
internal class AttachedStoragePal(private val storage: CoreStoragePal) : CoreStoragePal {
|
|
93
|
+
private fun <T> enter(block: () -> T): T = hostCallback(block)
|
|
94
|
+
override fun read(key: String) = enter { storage.read(key) }
|
|
95
|
+
override fun write(key: String, value: String) = enter { storage.write(key, value) }
|
|
96
|
+
override fun remove(key: String) = enter { storage.remove(key) }
|
|
97
|
+
override fun list(prefix: String) = enter { storage.list(prefix) }
|
|
98
|
+
override fun lock(name: String) = enter { storage.lock(name) }
|
|
99
|
+
override fun unlock(name: String) = enter { storage.unlock(name) }
|
|
100
|
+
override fun readSecret(key: String) = enter { storage.readSecret(key) }
|
|
101
|
+
override fun writeSecret(key: String, value: String) = enter { storage.writeSecret(key, value) }
|
|
102
|
+
override fun removeSecret(key: String) = enter { storage.removeSecret(key) }
|
|
103
|
+
}
|
|
104
|
+
private class AttachedListener(val listener: EventListener) : EventListener {
|
|
105
|
+
override fun onEvent(event: String) = hostCallback { listener.onEvent(event) }
|
|
106
|
+
}
|
|
107
|
+
private class AttachedExecute(val execute: CoreToolExecute) : CoreToolExecute {
|
|
108
|
+
override fun execute(callJson: String, contextJson: String, cancel: CancelToken) =
|
|
109
|
+
hostCallback { execute.execute(callJson, contextJson, cancel) }
|
|
110
|
+
}
|
|
111
|
+
private class AttachedSink(val sink: CoreEventSink) : CoreEventSink {
|
|
112
|
+
override fun deliver(deliveryId: String, eventJson: String, delivery: CoreDelivery) =
|
|
113
|
+
hostCallback { sink.deliver(deliveryId, eventJson, delivery) }
|
|
114
|
+
}
|
|
115
|
+
private class AttachedCancel(val callback: CancelCallback) : CancelCallback {
|
|
116
|
+
override fun cancelled() = hostCallback { callback.cancelled() }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Wrap once, where the object is handed to Core; an already attached object is returned as it is. */
|
|
120
|
+
internal fun attached(host: HostPal): HostPal = host as? AttachedHostPal ?: AttachedHostPal(host)
|
|
121
|
+
internal fun attached(storage: CoreStoragePal): CoreStoragePal = storage as? AttachedStoragePal ?: AttachedStoragePal(storage)
|
|
122
|
+
@JvmName("attachedOrNull") internal fun attached(storage: CoreStoragePal?): CoreStoragePal? = storage?.let(::attached)
|
|
123
|
+
internal fun attached(listener: EventListener): EventListener = listener as? AttachedListener ?: AttachedListener(listener)
|
|
124
|
+
@JvmName("attachedListenerOrNull") internal fun attached(listener: EventListener?): EventListener? = listener?.let(::attached)
|
|
125
|
+
internal fun attached(execute: CoreToolExecute): CoreToolExecute = execute as? AttachedExecute ?: AttachedExecute(execute)
|
|
126
|
+
internal fun attached(sink: CoreEventSink): CoreEventSink = sink as? AttachedSink ?: AttachedSink(sink)
|
|
127
|
+
@JvmName("attachedSinkOrNull") internal fun attached(sink: CoreEventSink?): CoreEventSink? = sink?.let(::attached)
|
|
128
|
+
internal fun attached(callback: CancelCallback): CancelCallback = callback as? AttachedCancel ?: AttachedCancel(callback)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ResponseSink
|
|
4
|
+
import dev.croche.RuntimeCancellation
|
|
5
|
+
import dev.croche.ffi.*
|
|
6
|
+
import dev.croche.s7.pal.DirectHttpPal
|
|
7
|
+
import java.io.File
|
|
8
|
+
import java.io.FileNotFoundException
|
|
9
|
+
import java.io.IOException
|
|
10
|
+
import java.net.URI
|
|
11
|
+
import java.util.TimeZone
|
|
12
|
+
import java.util.UUID
|
|
13
|
+
|
|
14
|
+
/** Plain JVM effects only. No image codec and no acquisition before Core's codec preflight. */
|
|
15
|
+
internal class JvmCoreHost(private val storage: CoreStoragePal) : HostPal {
|
|
16
|
+
private val http = DirectHttpPal()
|
|
17
|
+
override fun streamPlatform() = HostStreamPlatform.OTHER
|
|
18
|
+
override fun platformFacts() = PalPlatformFacts(PalTargetKind.OTHER, null, java.util.Locale.getDefault().toLanguageTag(), null, false)
|
|
19
|
+
override fun imageCodecAvailable() = false
|
|
20
|
+
override fun readImage(request: PalImageReadRequest, cancel: CancelToken): ByteArray = throw CorePalException.Failure("image_fetch_failed", "No JVM image reader is attached")
|
|
21
|
+
override fun prepareImage(source: PalImageBytes, plan: PalImagePreparePlan, cancel: CancelToken): PalImageBytes = throw CorePalException.Failure("image_unsupported", "No JVM image codec is attached")
|
|
22
|
+
override fun decodeImage(bytes: ByteArray, options: PalImageDecodeOptions, cancel: CancelToken): PalDecodedImage = throw CorePalException.Failure("invalid_input", "No JVM image codec is attached")
|
|
23
|
+
override fun renderImage(request: PalImageRenderRequest, cancel: CancelToken): ByteArray = throw CorePalException.Failure("image_unsupported", "No JVM image codec is attached")
|
|
24
|
+
override fun releaseImage(capture: String) = Unit
|
|
25
|
+
override fun releaseDecodedImage(handle: String) = Unit
|
|
26
|
+
override fun resolve(host: String, cancel: CancelToken) = effectCallback { http.resolve(host, signal(cancel)) }
|
|
27
|
+
override fun http(request: PalHttpRequest, sink: HttpSink, cancel: CancelToken) = effectCallback {
|
|
28
|
+
http.http(request, object : ResponseSink {
|
|
29
|
+
override fun head(status: Int, headers: List<PalHeader>) = sink.head(status.toUShort(), headers)
|
|
30
|
+
override fun chunk(bytes: ByteArray) = sink.chunk(bytes)
|
|
31
|
+
}, signal(cancel))
|
|
32
|
+
}
|
|
33
|
+
override fun readAsset(path: String): ByteArray? = effectCallback {
|
|
34
|
+
val file = if (path.startsWith("file:")) File(URI(path)) else File(path)
|
|
35
|
+
try { file.readBytes() }
|
|
36
|
+
catch (error: FileNotFoundException) { if (file.exists()) throw CorePalException.Failure("asset_read_failed", error.message ?: "Asset unavailable") else null }
|
|
37
|
+
catch (error: IOException) { throw CorePalException.Failure("asset_read_failed", error.message ?: "Asset unavailable") }
|
|
38
|
+
catch (error: SecurityException) { throw CorePalException.Failure("asset_read_failed", error.message ?: "Asset unavailable") }
|
|
39
|
+
}
|
|
40
|
+
override fun read(key: String) = storage.read(key)
|
|
41
|
+
override fun write(key: String, value: String) = storage.write(key, value)
|
|
42
|
+
override fun remove(key: String) = storage.remove(key)
|
|
43
|
+
override fun list(prefix: String) = storage.list(prefix)
|
|
44
|
+
override fun lock(key: String) = storage.lock(key)
|
|
45
|
+
override fun unlock(key: String) = storage.unlock(key)
|
|
46
|
+
override fun readSecret(key: String) = storage.readSecret(key)
|
|
47
|
+
override fun writeSecret(key: String, value: String) = storage.writeSecret(key, value)
|
|
48
|
+
override fun removeSecret(key: String) = storage.removeSecret(key)
|
|
49
|
+
override fun storeIdentity() = "jvm-core-storage-pal"
|
|
50
|
+
override fun storageCapability() = StorageCapability.KEYED
|
|
51
|
+
override fun nowMs() = System.currentTimeMillis().toDouble()
|
|
52
|
+
override fun utcOffsetMinutes(atMs: Double) = TimeZone.getDefault().getOffset(atMs.toLong()).toLong() / 60000
|
|
53
|
+
override fun random() = UUID.randomUUID().toString()
|
|
54
|
+
// CORE-GAP: no factory tick callback at this pin. Core must own scheduled upkeep;
|
|
55
|
+
// a delivery call is not maintenance, so the SDK cannot invent that substitution.
|
|
56
|
+
override fun wake(atMs: Double?) = Unit
|
|
57
|
+
override fun registerBackground(identifier: String, wake: CoreBackgroundWake): Unit =
|
|
58
|
+
throw CorePalException.Failure("capability_unsupported", "Plain JVM has no OS background installer")
|
|
59
|
+
override fun schedule(entries: String) = Unit
|
|
60
|
+
override fun tool(invocation: String, cancel: CancelToken): ToolOutput = throw CorePalException.Failure("tool_not_dispatched", "S1 uses registered tool definitions")
|
|
61
|
+
override fun attest(request: String, timeoutMs: ULong): String = throw CorePalException.Failure("attestation_unsupported", "No JVM attestation provider")
|
|
62
|
+
override fun archiveAppendLine(entry: String, line: String): Unit = archive()
|
|
63
|
+
override fun archiveReadLines(entry: String): List<String> = archive()
|
|
64
|
+
override fun archiveReplaceLines(entry: String, lines: List<String>): Unit = archive()
|
|
65
|
+
override fun archiveRemove(entry: String): Unit = archive()
|
|
66
|
+
override fun archiveEntries(prefix: String): List<ArchiveEntry> = archive()
|
|
67
|
+
private fun archive(): Nothing = throw CoreStorageFailure.Failure("Core owns keyed archives")
|
|
68
|
+
private fun signal(cancel: CancelToken) = RuntimeCancellation().also { result ->
|
|
69
|
+
if (cancel.isCancelled()) result.cancel()
|
|
70
|
+
cancel.onCancel(attached(object : CancelCallback { override fun cancelled() = result.cancel() }))
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.*
|
|
4
|
+
import dev.croche.s7.pal.AtomicKeyFiles
|
|
5
|
+
import java.io.File
|
|
6
|
+
import java.nio.channels.FileChannel
|
|
7
|
+
import java.nio.file.StandardOpenOption
|
|
8
|
+
import java.nio.file.attribute.PosixFilePermissions
|
|
9
|
+
import java.nio.file.Files
|
|
10
|
+
|
|
11
|
+
/** Plain JVM file PAL; Android uses Keystore instead. The private root contains separate
|
|
12
|
+
* opaque state and secret areas. No project identity or store layout is resolved here. */
|
|
13
|
+
internal class JvmStoragePal(root: File = File(System.getProperty("user.home"), ".croche/pal-v1")) : CoreStoragePal {
|
|
14
|
+
private val root = root
|
|
15
|
+
private val values = AtomicKeyFiles(File(root, "state"), ::sync)
|
|
16
|
+
private val secrets = AtomicKeyFiles(File(root, "secrets"), ::sync)
|
|
17
|
+
private fun prepareRoot() {
|
|
18
|
+
Files.createDirectories(root.toPath())
|
|
19
|
+
try { Files.setPosixFilePermissions(root.toPath(), PosixFilePermissions.fromString("rwx------")) }
|
|
20
|
+
catch (_: UnsupportedOperationException) { /* Windows uses the user's profile ACL. */ }
|
|
21
|
+
}
|
|
22
|
+
override fun read(key: String) = storageCallback { values.read(key) }
|
|
23
|
+
override fun write(key: String, value: String) = storageCallback {
|
|
24
|
+
prepareRoot(); values.write(key, value)
|
|
25
|
+
}
|
|
26
|
+
override fun remove(key: String) = storageCallback { values.remove(key) }
|
|
27
|
+
override fun list(prefix: String) = storageCallback { values.list(prefix) }
|
|
28
|
+
override fun lock(name: String) = storageCallback { prepareRoot(); if (values.lock(name)) PalLockOutcome.ACQUIRED else PalLockOutcome.HELD }
|
|
29
|
+
override fun unlock(name: String) = storageCallback { values.unlock(name) }
|
|
30
|
+
override fun readSecret(key: String) = storageCallback { secrets.read(key) }
|
|
31
|
+
override fun writeSecret(key: String, value: String) = storageCallback { prepareRoot(); secrets.write(key, value) }
|
|
32
|
+
override fun removeSecret(key: String) = storageCallback { secrets.remove(key) }
|
|
33
|
+
private fun sync(directory: File) { FileChannel.open(directory.toPath(), StandardOpenOption.READ).use { it.force(true) } }
|
|
34
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.*
|
|
4
|
+
|
|
5
|
+
/** Hale's L1/R1 ABI. No reflection, static-deletion fallback, recovery policy or mutable global hook. */
|
|
6
|
+
internal interface LifecycleBinding {
|
|
7
|
+
fun open(openJson: String, registrations: List<CoreToolRegistration>, storage: CoreStoragePal?, host: HostPal,
|
|
8
|
+
bindingFactsJson: String, recoveryJson: String?): CoreCrocheInterface
|
|
9
|
+
fun deleteData(owner: CoreCrocheInterface, scopeJson: String?)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Compilation intentionally depends on #1144's regenerated bindings; never emulate recovery in the SDK. */
|
|
13
|
+
internal object GeneratedLifecycleBinding : LifecycleBinding {
|
|
14
|
+
override fun open(openJson: String, registrations: List<CoreToolRegistration>, storage: CoreStoragePal?, host: HostPal,
|
|
15
|
+
bindingFactsJson: String, recoveryJson: String?): CoreCroche =
|
|
16
|
+
CoreCroche.open(openJson, registrations, storage, host, bindingFactsJson, recoveryJson)
|
|
17
|
+
override fun deleteData(owner: CoreCrocheInterface, scopeJson: String?) = owner.deleteData(scopeJson)
|
|
18
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.*
|
|
4
|
+
import dev.croche.StoragePal
|
|
5
|
+
import kotlinx.coroutines.CoroutineDispatcher
|
|
6
|
+
import kotlinx.coroutines.Dispatchers
|
|
7
|
+
import java.lang.reflect.InvocationTargetException
|
|
8
|
+
|
|
9
|
+
internal data class NativePlatformPals(val storage: CoreStoragePal, val host: HostPal,
|
|
10
|
+
val eventDispatcher: CoroutineDispatcher, val platformStorage: CoreStoragePal? = null)
|
|
11
|
+
internal interface NativePlatformProvider {
|
|
12
|
+
fun create(platform: Any?, storage: CoreStoragePal?): NativePlatformPals
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The second carrier exists only for Core's private custom-backend enrollment marker. */
|
|
16
|
+
internal fun backgroundStorage(custom: CoreStoragePal?, platform: () -> CoreStoragePal): Pair<CoreStoragePal, CoreStoragePal?> {
|
|
17
|
+
val default = platform()
|
|
18
|
+
return Pair(custom ?: default, default.takeIf { custom != null })
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Android ships the provider in its AAR; plain JVM uses file effects. No Core-side default. */
|
|
22
|
+
internal fun nativePlatform(platform: Any?, customStorage: StoragePal?): NativePlatformPals {
|
|
23
|
+
val android = try { Class.forName("dev.croche.android.pal.AndroidPublicPlatform") } catch (_: ClassNotFoundException) { null }
|
|
24
|
+
if (android != null) {
|
|
25
|
+
try {
|
|
26
|
+
return (android.getDeclaredConstructor().newInstance() as NativePlatformProvider).create(platform, customStorage?.let(::appStorage))
|
|
27
|
+
} catch (error: InvocationTargetException) { throw error.targetException }
|
|
28
|
+
}
|
|
29
|
+
val (storage, platformStorage) = backgroundStorage(customStorage?.let(::appStorage)) { JvmStoragePal() }
|
|
30
|
+
val host = JvmCoreHost(storage)
|
|
31
|
+
return NativePlatformPals(storage, host, Dispatchers.Unconfined, platformStorage)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** App storage throws facts only, just like platform storage. A supplied code is not copied. */
|
|
35
|
+
internal fun storageFacts(storage: CoreStoragePal): CoreStoragePal = object : CoreStoragePal {
|
|
36
|
+
override fun read(key: String) = storageCallback { storage.read(key) }
|
|
37
|
+
override fun write(key: String, value: String) = storageCallback { storage.write(key, value) }
|
|
38
|
+
override fun remove(key: String) = storageCallback { storage.remove(key) }
|
|
39
|
+
override fun list(prefix: String) = storageCallback { storage.list(prefix) }
|
|
40
|
+
override fun lock(name: String) = storageCallback { storage.lock(name) }
|
|
41
|
+
override fun unlock(name: String) = storageCallback { storage.unlock(name) }
|
|
42
|
+
override fun readSecret(key: String) = storageCallback { storage.readSecret(key) }
|
|
43
|
+
override fun writeSecret(key: String, value: String) = storageCallback { storage.writeSecret(key, value) }
|
|
44
|
+
override fun removeSecret(key: String) = storageCallback { storage.removeSecret(key) }
|
|
45
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.CrocheError
|
|
4
|
+
import dev.croche.ffi.CorePalException
|
|
5
|
+
import dev.croche.ffi.CoreStorageFailure
|
|
6
|
+
|
|
7
|
+
/** Storage carries exception detail only, regardless of the exception's class or any code. */
|
|
8
|
+
internal inline fun <T> storageCallback(block: () -> T): T = try {
|
|
9
|
+
block()
|
|
10
|
+
} catch (error: CoreStorageFailure) {
|
|
11
|
+
throw error
|
|
12
|
+
} catch (error: Throwable) {
|
|
13
|
+
throw CoreStorageFailure.Failure(error.message ?: error.javaClass.simpleName)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A supplied effect fact is never reclassified here. Core validates it for that effect.
|
|
17
|
+
* An unexpected uncoded failure has the literal internal code, never a platform fallback.
|
|
18
|
+
*/
|
|
19
|
+
internal inline fun <T> effectCallback(block: () -> T): T = try {
|
|
20
|
+
block()
|
|
21
|
+
} catch (error: CorePalException) {
|
|
22
|
+
throw error
|
|
23
|
+
} catch (error: CrocheError) {
|
|
24
|
+
throw CorePalException.Failure(error.code, error.message)
|
|
25
|
+
} catch (error: Throwable) {
|
|
26
|
+
throw CorePalException.Failure("internal", error.message ?: error.javaClass.simpleName)
|
|
27
|
+
}
|