@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
|
@@ -7,18 +7,36 @@ import FoundationNetworking
|
|
|
7
7
|
// everything else, including SSE framing, response bounds, timeouts' meaning, persistence
|
|
8
8
|
// order, upkeep and cancellation rules. Its calls into this host block a background thread.
|
|
9
9
|
|
|
10
|
-
extension
|
|
10
|
+
extension LegacyCrocheError {
|
|
11
11
|
init(_ error: Error) {
|
|
12
|
-
if let e = error as?
|
|
12
|
+
if let e = error as? LegacyCrocheError { self = e }
|
|
13
13
|
else if case let .Failure(code, message)? = error as? FfiError { self.init(code, message) }
|
|
14
|
+
// Core's App API carrier (truncate since #830 2eb98fbc): its code and public detail, unchanged.
|
|
15
|
+
else if case let .Failure(code, detail, _, _, _)? = error as? CoreApiError { self.init(code, detail) }
|
|
14
16
|
else { self.init("host_error", "The host operation failed") }
|
|
15
17
|
}
|
|
16
18
|
var ffi: FfiError { .Failure(code: code, message: message) }
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
///
|
|
21
|
+
/// An effect failure is a platform FACT (Keel 18:44Z): a code from Core's closed per-effect list
|
|
22
|
+
/// and a detail. Core validates the code. An unexpected error with no fact code IS `internal`, sent
|
|
23
|
+
/// literally (Atlas's PAL error contract, Keel 21:00Z), with this detail kept for debugging.
|
|
24
|
+
/// Storage never comes through here: it reports no code.
|
|
25
|
+
func effectFailure(_ error: Error) -> CorePalError {
|
|
26
|
+
if let error = error as? CorePalError { return error }
|
|
27
|
+
if let error = error as? LegacyCrocheError { return .Failure(code: error.code, detail: error.message) }
|
|
28
|
+
if case let .Failure(code, message)? = error as? FfiError { return .Failure(code: code, detail: message) }
|
|
29
|
+
return .Failure(code: "internal", detail: "\(error)")
|
|
30
|
+
}
|
|
31
|
+
func effect<T>(_ body: () throws -> T) throws -> T {
|
|
32
|
+
do { return try body() } catch { throw effectFailure(error) }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// The binding's `identity`, named so the SDK's `storageIdentity` reads as its own. Unqualified:
|
|
20
36
|
/// the React Native pod compiles the SDK and its bindings into one module.
|
|
21
37
|
func ffiIdentity(_ config: String) throws -> String { try identity(config: config) }
|
|
38
|
+
/// The binding's `truncate`, named so `Croche.truncate` does not shadow it.
|
|
39
|
+
func ffiTruncate(_ text: String, _ max: Double) throws -> String { try truncate(text: text, max: max) }
|
|
22
40
|
|
|
23
41
|
/// Runs `body` on the Swift concurrency pool and blocks this (Rust-owned, background) thread
|
|
24
42
|
/// until it finishes, `cancel` fires or `milliseconds` pass.
|
|
@@ -34,7 +52,7 @@ func blockOn<T: Sendable>(milliseconds: Double?, cancel: CancelToken?,
|
|
|
34
52
|
done.signal()
|
|
35
53
|
}
|
|
36
54
|
done.wait()
|
|
37
|
-
do { return try box.get() } catch { throw
|
|
55
|
+
do { return try box.get() } catch { throw LegacyCrocheError(error).ffi }
|
|
38
56
|
}
|
|
39
57
|
|
|
40
58
|
/// An app command (H1): runs `body` until it returns, however long after a cancel. The cancel
|
|
@@ -65,7 +83,7 @@ func blockUntilSettled<T: Sendable>(cancel: CancelToken,
|
|
|
65
83
|
}
|
|
66
84
|
cancel.onCancel(callback: CancelHook(cancellation))
|
|
67
85
|
done.wait()
|
|
68
|
-
if cancellation.isCancelled { throw
|
|
86
|
+
if cancellation.isCancelled { throw LegacyCrocheError("cancelled", "The operation was cancelled").ffi }
|
|
69
87
|
do { return try box.get() } catch { throw handlerError(error).ffi }
|
|
70
88
|
}
|
|
71
89
|
|
|
@@ -79,23 +97,10 @@ struct ToolNotDispatched: Error {}
|
|
|
79
97
|
/// An error from a tool handler. "Nothing ran" is the host's to say, never a handler's: a
|
|
80
98
|
/// handler that throws `tool_not_dispatched` or `host_unresponsive` (perhaps passing on another
|
|
81
99
|
/// Runtime's error) may already have had its effect, so the call fails as `tool_failed`.
|
|
82
|
-
func handlerError(_ error: Error) ->
|
|
83
|
-
if error is ToolNotDispatched { return
|
|
84
|
-
let e =
|
|
85
|
-
return ["tool_not_dispatched", "host_unresponsive"].contains(e.code) ?
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/// Where app command handlers run (H1), where the OS has task executors: GCD threads, apart from
|
|
89
|
-
/// Swift's cooperative pool, which also runs the Runtime's own requests. This does not make a
|
|
90
|
-
/// blocking async handler safe (measured on Linux, #342); blocking work uses `blockingTool`.
|
|
91
|
-
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
|
|
92
|
-
final class ToolThreads: TaskExecutor, @unchecked Sendable {
|
|
93
|
-
static let shared = ToolThreads()
|
|
94
|
-
private let queue = DispatchQueue(label: "dev.croche.tools", attributes: .concurrent)
|
|
95
|
-
func enqueue(_ job: consuming ExecutorJob) {
|
|
96
|
-
let job = UnownedJob(job), executor = asUnownedTaskExecutor()
|
|
97
|
-
queue.async { job.runSynchronously(on: executor) }
|
|
98
|
-
}
|
|
100
|
+
func handlerError(_ error: Error) -> LegacyCrocheError {
|
|
101
|
+
if error is ToolNotDispatched { return LegacyCrocheError("tool_not_dispatched", "No handler ran for this tool") }
|
|
102
|
+
let e = LegacyCrocheError(error)
|
|
103
|
+
return ["tool_not_dispatched", "host_unresponsive"].contains(e.code) ? LegacyCrocheError("tool_failed", e.message) : e
|
|
99
104
|
}
|
|
100
105
|
|
|
101
106
|
final class CancelHook: CancelCallback, @unchecked Sendable {
|
|
@@ -108,7 +113,7 @@ final class EventHook: EventListener, @unchecked Sendable {
|
|
|
108
113
|
init(_ callback: @escaping @Sendable (JSON) -> Void) { self.callback = callback }
|
|
109
114
|
func onEvent(event: String) { if let value = try? JSON(event) { callback(value) } }
|
|
110
115
|
}
|
|
111
|
-
|
|
116
|
+
final class ResultBox<T>: @unchecked Sendable {
|
|
112
117
|
private let lock = NSLock()
|
|
113
118
|
private var value: Result<T, Error>?
|
|
114
119
|
func set(_ result: Result<T, Error>) { lock.withLock { value = result } }
|
|
@@ -130,6 +135,13 @@ func retryAt(_ value: JSON) -> Double? { value.number ?? value.string.flatMap(ep
|
|
|
130
135
|
|
|
131
136
|
/// The Swift PAL handed to the Rust Runtime.
|
|
132
137
|
final class SwiftHost: HostPal, @unchecked Sendable {
|
|
138
|
+
// This legacy host has no process-wide OS registrar. Never report success
|
|
139
|
+
// through its per-Runtime scheduler; Core owns replacement and retry policy.
|
|
140
|
+
func registerBackground(identifier: String, wake: CoreBackgroundWake) throws {
|
|
141
|
+
throw CorePalError.Failure(code: "capability_unsupported", detail: "This host has no OS background registrar.")
|
|
142
|
+
}
|
|
143
|
+
/// Where this host runs (a fact Core reads); a React Native host sets `.reactNative`.
|
|
144
|
+
var streamPlatformFact = HostStreamPlatform.other
|
|
133
145
|
private let storage: any RuntimeStorage
|
|
134
146
|
private let projectRecords: ProjectRecordFiles
|
|
135
147
|
// Internal test trace, configured before Core starts; records only delegated store calls.
|
|
@@ -181,27 +193,53 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
181
193
|
}
|
|
182
194
|
|
|
183
195
|
private func host<T>(_ body: () throws -> T) throws -> T {
|
|
184
|
-
do { return try body() } catch { throw
|
|
196
|
+
do { return try body() } catch { throw LegacyCrocheError(error).ffi }
|
|
185
197
|
}
|
|
186
198
|
|
|
199
|
+
// MARK: Images (private Rev 4a PAL). Core owns plans, limits and every URL; ImageIO decodes.
|
|
200
|
+
let images = NativeImagePal()
|
|
201
|
+
func imageCodecAvailable() -> Bool { images.codecAvailable }
|
|
202
|
+
func readImage(request: PalImageReadRequest, cancel: CancelToken) throws -> Data {
|
|
203
|
+
try effect { try images.read(request, cancel) }
|
|
204
|
+
}
|
|
205
|
+
func prepareImage(source: PalImageBytes, plan: PalImagePreparePlan, cancel: CancelToken) throws -> PalImageBytes {
|
|
206
|
+
try effect { try images.prepare(source, plan, cancel) }
|
|
207
|
+
}
|
|
208
|
+
func releaseImage(capture: String) { images.release(capture) }
|
|
209
|
+
func decodeImage(bytes: Data, options: PalImageDecodeOptions, cancel: CancelToken) throws -> PalDecodedImage {
|
|
210
|
+
try effect { try images.decode(bytes, options, cancel) }
|
|
211
|
+
}
|
|
212
|
+
func renderImage(request: PalImageRenderRequest, cancel: CancelToken) throws -> Data {
|
|
213
|
+
try effect { try images.render(request, cancel) }
|
|
214
|
+
}
|
|
215
|
+
func releaseDecodedImage(handle: String) { images.releaseDecoded(handle) }
|
|
216
|
+
/// A fact about the host, never a batching choice. A React Native host reports .reactNative.
|
|
217
|
+
func streamPlatform() -> HostStreamPlatform { streamPlatformFact }
|
|
218
|
+
|
|
187
219
|
// MARK: HTTP
|
|
188
220
|
func http(request: PalHttpRequest, sink: HttpSink, cancel: CancelToken) throws {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
return
|
|
221
|
+
try effect {
|
|
222
|
+
guard request.responseMode == "standard" else {
|
|
223
|
+
throw LegacyCrocheError("capability_unsupported", "Native image inputs are not supported yet")
|
|
224
|
+
}
|
|
225
|
+
if try PlatformHTTP.direct(request, sink: sink, cancel: cancel) { return }
|
|
226
|
+
let transport = http, session = session
|
|
227
|
+
_ = try blockOn(milliseconds: Double(request.timeoutMs), cancel: cancel) { signal -> Bool in
|
|
228
|
+
if let transport { try await Self.adapt(transport, request, sink, signal) }
|
|
229
|
+
else { try await session.stream(request, into: sink, cancellation: signal) }
|
|
230
|
+
return true
|
|
231
|
+
}
|
|
194
232
|
}
|
|
195
233
|
}
|
|
196
234
|
|
|
197
235
|
/// An app transport speaks whole effects (`RuntimeHTTP`): its answer goes to the Runtime as
|
|
198
236
|
/// a response, and its stream deltas as server-sent events.
|
|
199
|
-
|
|
237
|
+
static func adapt(_ transport: RuntimeHTTP, _ request: PalHttpRequest, _ sink: HttpSink,
|
|
200
238
|
_ signal: RuntimeCancellation) async throws {
|
|
201
239
|
var headers: [String: JSON] = [:]
|
|
202
240
|
for header in request.headers { headers[header.name] = .string(header.value) }
|
|
203
241
|
let effect = JSON.object(["url": .string(request.url), "method": .string(request.method), "headers": .object(headers),
|
|
204
|
-
"body": (try? JSON(
|
|
242
|
+
"body": request.body.map { (try? JSON($0)) ?? .string($0) } ?? .null,
|
|
205
243
|
"timeoutMs": .number(Double(request.timeoutMs))])
|
|
206
244
|
let streaming = StreamStart()
|
|
207
245
|
let reply = try await transport(effect, signal) { data in
|
|
@@ -224,6 +262,15 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
224
262
|
if !body.isEmpty { _ = sink.chunk(bytes: body) }
|
|
225
263
|
}
|
|
226
264
|
|
|
265
|
+
// MARK: #820 calls: the same assets, resolver and facts as `PlatformPal`. This per-agent
|
|
266
|
+
// store has no keyed listing; the keyed `PlatformPal` replaces it at Core's cutover.
|
|
267
|
+
func readAsset(path: String) throws -> Data? { try effect { try PlatformAssets.read(path, bundle: .main) } }
|
|
268
|
+
func resolve(host name: String, cancel: CancelToken) throws -> [String] { try effect { try PlatformResolver.resolve(name) } }
|
|
269
|
+
func platformFacts() -> PalPlatformFacts { PalPlatformFacts(targetKind: AppBuildFacts.targetKind, debuggable: AppBuildFacts.debuggable, locale: nil, attestationPlatform: nil, browserFetch: nil) }
|
|
270
|
+
func list(prefix: String) throws -> [String] {
|
|
271
|
+
throw FfiError.Failure(code: "storage_error", message: "This per-agent store keeps no keyed listing.")
|
|
272
|
+
}
|
|
273
|
+
|
|
227
274
|
// MARK: Storage
|
|
228
275
|
/// The Core names its snapshot by the key it locks, and other records `<key>.<entry>` (F19's
|
|
229
276
|
/// held sends: `<key>.held`). Each goes to its own place: an entry never replaces the snapshot.
|
|
@@ -231,7 +278,7 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
231
278
|
private var lockedKey: String?
|
|
232
279
|
private func entry(_ key: String) throws -> String? {
|
|
233
280
|
guard let main = snapshotKey, key != main else { return nil }
|
|
234
|
-
guard key.hasPrefix(main + "."), key.count > main.count + 1 else { throw
|
|
281
|
+
guard key.hasPrefix(main + "."), key.count > main.count + 1 else { throw LegacyCrocheError("storage_error", "Unknown storage key") }
|
|
235
282
|
return String(key.dropFirst(main.count + 1))
|
|
236
283
|
}
|
|
237
284
|
func read(key: String) throws -> String? {
|
|
@@ -253,7 +300,7 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
253
300
|
beforeStoreAccess?(key)
|
|
254
301
|
guard let name = try entry(key) else { return try storage.save(try JSON(value)) }
|
|
255
302
|
guard let keyed = storage as? any KeyedRuntimeStorage else {
|
|
256
|
-
throw
|
|
303
|
+
throw LegacyCrocheError("storage_error", "This RuntimeStorage keeps only its snapshot")
|
|
257
304
|
}
|
|
258
305
|
try keyed.save(entry: name, try JSON(value))
|
|
259
306
|
}
|
|
@@ -264,7 +311,7 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
264
311
|
func writeSecret(key: String, value: String) throws { try host { try secretStore.save(try JSON(value)) } }
|
|
265
312
|
func remove(key: String) throws {
|
|
266
313
|
try host {
|
|
267
|
-
guard !ProjectRecordFiles.owns(key) else { throw
|
|
314
|
+
guard !ProjectRecordFiles.owns(key) else { throw LegacyCrocheError("storage_error", "The project record is kept when agent data is removed") }
|
|
268
315
|
beforeStoreAccess?(key)
|
|
269
316
|
if let name = try entry(key) {
|
|
270
317
|
guard let keyed = storage as? any KeyedRuntimeStorage else { return }
|
|
@@ -273,13 +320,18 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
273
320
|
}
|
|
274
321
|
}
|
|
275
322
|
func removeSecret(key: String) throws { try host { try secretStore.remove() } }
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
323
|
+
/// Contention is the fact `.held` (ARCHITECTURE §2a): the legacy storages' own `store_busy`
|
|
324
|
+
/// becomes that fact here, and every other throw stays a failure.
|
|
325
|
+
@discardableResult func lock(key: String) throws -> PalLockOutcome {
|
|
326
|
+
do {
|
|
327
|
+
try host {
|
|
328
|
+
if ProjectRecordFiles.owns(key) { return try projectRecords.acquire(key) }
|
|
329
|
+
guard !projectRecords.held else { throw LegacyCrocheError("storage_error", "Release the project record lock before taking a store lock") }
|
|
330
|
+
beforeStoreAccess?(key)
|
|
331
|
+
try storage.acquire(); snapshotKey = key
|
|
332
|
+
}
|
|
333
|
+
return .acquired
|
|
334
|
+
} catch FfiError.Failure("store_busy", _) { return .held }
|
|
283
335
|
}
|
|
284
336
|
func unlock(key: String) {
|
|
285
337
|
if ProjectRecordFiles.owns(key) { projectRecords.release(key); return }
|
|
@@ -293,7 +345,7 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
293
345
|
/// turn, clearHistory and memory work as before.
|
|
294
346
|
private func archive() throws -> any ArchiveRuntimeStorage {
|
|
295
347
|
guard let archive = storage as? any ArchiveRuntimeStorage else {
|
|
296
|
-
throw
|
|
348
|
+
throw LegacyCrocheError("archive_unavailable", "This host keeps no turn archive.")
|
|
297
349
|
}
|
|
298
350
|
return archive
|
|
299
351
|
}
|
|
@@ -331,6 +383,11 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
331
383
|
func acceptTools(_ tools: [String: ToolHandler]) {
|
|
332
384
|
lock.withLock { for (name, handler) in tools { handlers[name] = handler; staged[name] = nil } }
|
|
333
385
|
}
|
|
386
|
+
/// A project instance's latest successful patched open: its table replaces the old one for future
|
|
387
|
+
/// invocations, as in TypeScript. A call already running keeps the handler it started with.
|
|
388
|
+
func replaceTools(_ tools: [String: ToolHandler]) {
|
|
389
|
+
lock.withLock { handlers = tools; staged.removeAll() }
|
|
390
|
+
}
|
|
334
391
|
|
|
335
392
|
// MARK: Tools and attestation
|
|
336
393
|
func tool(invocation: String, cancel: CancelToken) throws -> ToolOutput {
|
|
@@ -345,7 +402,7 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
345
402
|
// No deadline here: the Runtime times the call and cancels it (H4). This returns when
|
|
346
403
|
// the handler does, so a stuck one keeps its slot (H1).
|
|
347
404
|
let value = try blockUntilSettled(cancel: cancel) { signal in
|
|
348
|
-
try await handler(request,
|
|
405
|
+
try await handler(request, LegacyToolContext(data: context, cancellation: signal, images: sink))
|
|
349
406
|
}
|
|
350
407
|
// A plain handler's JSON is the value as it was; a `withResult` one's images ride beside
|
|
351
408
|
// it, one sink per call (never shared between concurrent calls).
|
|
@@ -353,17 +410,20 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
353
410
|
images: try sink.take().map(Images.record))
|
|
354
411
|
}
|
|
355
412
|
func attest(request: String, timeoutMs: UInt64) throws -> String {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
413
|
+
try effect {
|
|
414
|
+
guard let attestor else { throw LegacyCrocheError("attestation_unsupported", "This host has no attestor") }
|
|
415
|
+
let call = try JSON(request)
|
|
416
|
+
// A malformed call is Core's bug, not an attestation fact: unlisted, so Core reports internal.
|
|
417
|
+
guard let clientData = call["clientData"].string, let hash = base64URLDecode(call["clientDataHash"].string ?? "") else {
|
|
418
|
+
throw LegacyCrocheError("invalid_abi", "Malformed attest effect")
|
|
419
|
+
}
|
|
420
|
+
let proof = try blockOn(milliseconds: Double(timeoutMs), cancel: nil) { _ -> JSON in
|
|
421
|
+
let proof = try await attestor.attest(clientData: clientData, clientDataHash: hash)
|
|
422
|
+
return .object(["platform": .string(attestor.platform), "token": .string(proof.token),
|
|
423
|
+
"keyId": proof.keyId.map { .string($0) } ?? .null])
|
|
424
|
+
}
|
|
425
|
+
return String(decoding: try proof.encoded(), as: UTF8.self)
|
|
365
426
|
}
|
|
366
|
-
return String(decoding: try host { try proof.encoded() }, as: UTF8.self)
|
|
367
427
|
}
|
|
368
428
|
|
|
369
429
|
// MARK: Clock, entropy, timer
|
|
@@ -380,7 +440,7 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
380
440
|
/// open: a timer while running, and the OS's deferred work (`scheduler`) for after. A refused
|
|
381
441
|
/// registration fails this acknowledgement, which the Core reports as `schedule.degraded`
|
|
382
442
|
/// with its code.
|
|
383
|
-
func schedule(entries: String) throws { try
|
|
443
|
+
func schedule(entries: String) throws { try effect { try applySchedule(target(times(try JSON(entries)))) } }
|
|
384
444
|
private var retryFloor: Double?
|
|
385
445
|
/// A failed delivery answered `retryAt`: a note already due waits until then.
|
|
386
446
|
func retryAfter(_ at: Double) { lock.withLock { retryFloor = at } }
|
|
@@ -398,8 +458,8 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
398
458
|
guard let os = osScheduler else { return }
|
|
399
459
|
do {
|
|
400
460
|
if let at { try os.schedule(earliest: Date(timeIntervalSince1970: at / 1000)) } else { os.cancel() }
|
|
401
|
-
} catch let error as
|
|
402
|
-
catch { throw
|
|
461
|
+
} catch let error as LegacyCrocheError { throw error }
|
|
462
|
+
catch { throw LegacyCrocheError("schedule_failed", "The OS scheduler refused the request") }
|
|
403
463
|
}
|
|
404
464
|
/// A delivery is starting: it covers every note due now, so the timer never retries one.
|
|
405
465
|
func attempted() { let now = clock(); lock.withLock { fired = max(fired ?? -.infinity, now) } }
|
|
@@ -419,6 +479,7 @@ final class SwiftHost: HostPal, @unchecked Sendable {
|
|
|
419
479
|
return osAttached
|
|
420
480
|
}
|
|
421
481
|
if release { osScheduler?.detach() }
|
|
482
|
+
images.close()
|
|
422
483
|
}
|
|
423
484
|
/// One timer at the earliest note, never firing a delivery before its time (a timer that ends
|
|
424
485
|
/// early re-arms), and at most once per time: a delivery that was cut short leaves its notes
|
|
@@ -492,7 +553,7 @@ final class RuntimeSession: @unchecked Sendable {
|
|
|
492
553
|
private func start(_ request: URLRequest) throws -> (URLSessionDataTask, AsyncThrowingStream<HTTPPart, Error>) {
|
|
493
554
|
let (parts, continuation) = AsyncThrowingStream<HTTPPart, Error>.makeStream()
|
|
494
555
|
return try lock.withLock {
|
|
495
|
-
guard let session else { throw
|
|
556
|
+
guard let session else { throw LegacyCrocheError("network_error", "The Runtime is closed") }
|
|
496
557
|
let task = session.dataTask(with: request)
|
|
497
558
|
router.register(task, continuation)
|
|
498
559
|
return (task, parts)
|
|
@@ -501,10 +562,10 @@ final class RuntimeSession: @unchecked Sendable {
|
|
|
501
562
|
/// The response goes to the Runtime as it arrives. The Runtime frames and bounds it.
|
|
502
563
|
func stream(_ request: PalHttpRequest, into sink: any HttpSinkProtocol, cancellation: RuntimeCancellation) async throws {
|
|
503
564
|
try cancellation.check()
|
|
504
|
-
guard let url = URL(string: request.url) else { throw
|
|
565
|
+
guard let url = URL(string: request.url) else { throw LegacyCrocheError("network_error", "Invalid HTTP effect URL") }
|
|
505
566
|
var urlRequest = URLRequest(url: url)
|
|
506
567
|
urlRequest.httpMethod = request.method
|
|
507
|
-
if !["GET", "HEAD"].contains(request.method) { urlRequest.httpBody = Data(
|
|
568
|
+
if !["GET", "HEAD"].contains(request.method), let body = request.body { urlRequest.httpBody = Data(body.utf8) }
|
|
508
569
|
urlRequest.timeoutInterval = Double(request.timeoutMs) / 1000
|
|
509
570
|
urlRequest.httpShouldHandleCookies = false
|
|
510
571
|
for header in request.headers { urlRequest.setValue(header.value, forHTTPHeaderField: header.name) }
|
|
@@ -547,7 +608,7 @@ private final class HTTPStreamDelegate: NSObject, URLSessionDataDelegate, @unche
|
|
|
547
608
|
didReceive response: URLResponse, completionHandler: @escaping @Sendable (URLSession.ResponseDisposition) -> Void) {
|
|
548
609
|
guard let sink = sink(dataTask) else { completionHandler(.cancel); return }
|
|
549
610
|
guard let http = response as? HTTPURLResponse else {
|
|
550
|
-
sink.finish(throwing:
|
|
611
|
+
sink.finish(throwing: LegacyCrocheError("network_error", "No HTTP response")); completionHandler(.cancel); return
|
|
551
612
|
}
|
|
552
613
|
var headers: [String: JSON] = [:]
|
|
553
614
|
for (key, value) in http.allHeaderFields { headers[String(describing: key)] = .string(String(describing: value)) }
|
|
@@ -557,6 +618,6 @@ private final class HTTPStreamDelegate: NSObject, URLSessionDataDelegate, @unche
|
|
|
557
618
|
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive chunk: Data) { sink(dataTask)?.yield(.body(chunk)) }
|
|
558
619
|
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
|
|
559
620
|
let sink = lock.withLock { sinks.removeValue(forKey: task.taskIdentifier) }
|
|
560
|
-
if error != nil { sink?.finish(throwing:
|
|
621
|
+
if error != nil { sink?.finish(throwing: LegacyCrocheError("network_error", "HTTP transport failed")) } else { sink?.finish() }
|
|
561
622
|
}
|
|
562
623
|
}
|