irongraph-sdk 0.1.14

Embed a CPU graph database in Rust with asynchronous GPU text embeddings, Cypher, vector search, streaming, and queues.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! IronGraph SDK for embedded databases and remote API/Bolt connections.
//!
//! Documents, vector search, graph algorithms, projects, and administration use the same
//! [`Query`] interface. Synchronous methods block the caller; asynchronous embedded methods
//! run native work on Tokio's blocking pool and cancel active operations when dropped.

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::{collections::BTreeMap, fmt, path::PathBuf};

#[repr(C)]
struct Buffer {
    data: *mut u8,
    len: usize,
}
unsafe extern "C" {
    fn irongraph_abi_version() -> u32;
    fn irongraph_package_version_v1() -> *const std::ffi::c_char;
    fn irongraph_call_v1(request: *const u8, len: usize, out: *mut Buffer) -> i32;
    fn irongraph_buffer_free_v1(buffer: Buffer);
}

/// Structured native or SDK error. Database error codes and retry information are preserved.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Error {
    pub code: String,
    pub message: String,
    pub retryable: bool,
    pub retry_after_ms: Option<u64>,
}
impl Error {
    fn sdk(message: impl Into<String>) -> Self {
        Self {
            code: "SDK_PROTOCOL".into(),
            message: message.into(),
            retryable: false,
            retry_after_ms: None,
        }
    }
}
impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}: {}", self.code, self.message)
    }
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;

struct OwnedBuffer(Buffer);
impl Drop for OwnedBuffer {
    fn drop(&mut self) {
        unsafe {
            irongraph_buffer_free_v1(Buffer {
                data: self.0.data,
                len: self.0.len,
            });
        }
    }
}

fn call(request: Value) -> Result<Value> {
    call_typed(request)
}

fn call_typed<T: serde::de::DeserializeOwned>(request: Value) -> Result<T> {
    if unsafe { irongraph_abi_version() } != 1 {
        return Err(Error::sdk("native library ABI version mismatch"));
    }
    let version = unsafe { std::ffi::CStr::from_ptr(irongraph_package_version_v1()) };
    if version.to_bytes() != env!("CARGO_PKG_VERSION").as_bytes() {
        return Err(Error::sdk("native library package version mismatch"));
    }
    let bytes = serde_json::to_vec(&request).map_err(|error| Error::sdk(error.to_string()))?;
    let mut buffer = Buffer {
        data: std::ptr::null_mut(),
        len: 0,
    };
    let status = unsafe { irongraph_call_v1(bytes.as_ptr(), bytes.len(), &mut buffer) };
    if status == 2 {
        return Err(Error::sdk("native library rejected request buffer"));
    }
    let buffer = OwnedBuffer(buffer);
    if buffer.0.data.is_null() || buffer.0.len > isize::MAX as usize {
        return Err(Error::sdk("native library returned an invalid buffer"));
    }
    let bytes = unsafe { std::slice::from_raw_parts(buffer.0.data, buffer.0.len) };
    let response: NativeResponse<'_> =
        serde_json::from_slice(bytes).map_err(|error| Error::sdk(error.to_string()))?;
    if response.version != env!("CARGO_PKG_VERSION") {
        return Err(Error::sdk("native library package version mismatch"));
    }
    match (status, response.ok) {
        (0, true) => serde_json::from_str(
            response
                .result
                .ok_or_else(|| Error::sdk("native result is missing"))?
                .get(),
        )
        .map_err(|error| Error::sdk(error.to_string())),
        (1, false) => Err(response
            .error
            .ok_or_else(|| Error::sdk("native error is missing"))?),
        _ => Err(Error::sdk("native response status is inconsistent")),
    }
}

#[derive(Deserialize)]
struct NativeResponse<'a> {
    ok: bool,
    version: &'a str,
    #[serde(default, borrow, deserialize_with = "present_native_result")]
    result: Option<&'a serde_json::value::RawValue>,
    error: Option<Error>,
}

fn present_native_result<'de, D: serde::Deserializer<'de>>(
    deserializer: D,
) -> std::result::Result<Option<&'de serde_json::value::RawValue>, D::Error> {
    // Preserve an explicitly returned null, and distinguish it from a missing result field.
    <&serde_json::value::RawValue>::deserialize(deserializer).map(Some)
}

/// Position of an applied database write.
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct Bookmark {
    pub term: u64,
    pub index: u64,
}

/// Acknowledgement for a write applied by this single-node database.
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum CommitAcknowledgement {
    #[default]
    Published,
}

/// One Cypher request covering the complete database query and administration surface.
#[derive(Clone, Debug, Serialize)]
pub struct Query {
    pub cypher: String,
    pub parameters: BTreeMap<String, Value>,
    /// Immutable project UUID, returned by `SHOW PROJECTS`; no default project is assumed.
    pub project_id: Option<String>,
    pub bookmark: Option<Bookmark>,
    pub consistency: CommitAcknowledgement,
}
impl Query {
    pub fn new(cypher: impl Into<String>) -> Self {
        Self {
            cypher: cypher.into(),
            parameters: BTreeMap::new(),
            project_id: None,
            bookmark: None,
            consistency: CommitAcknowledgement::Published,
        }
    }
    pub fn with_project(mut self, project_id: impl Into<String>) -> Self {
        self.project_id = Some(project_id.into());
        self
    }
    pub fn with_parameter(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
        self.parameters.insert(name.into(), value.into());
        self
    }
    pub fn with_bookmark(mut self, bookmark: Bookmark) -> Self {
        self.bookmark = Some(bookmark);
        self
    }
}

/// Lossless Query API result. Values retain their protocol type tags; 64-bit graph integers
/// remain decimal strings, and nodes, relationships, paths, vectors, and temporal values retain
/// their complete structure. Inspect `rows[row][column]` with `serde_json`.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct QueryResult {
    pub catalog: Option<Value>,
    pub columns: Vec<Value>,
    pub rows: Vec<Vec<Value>>,
    pub summary: Value,
}

#[derive(Clone, Copy, Debug, Default)]
pub enum ExecutionDevice {
    #[default]
    Auto,
    Cpu,
    Metal(u32),
    Cuda(u32),
}
/// Independently selected text inference device.
#[derive(Clone, Copy, Debug, Default)]
pub enum EmbeddingDevice {
    #[default]
    Auto,
    Cpu,
    Metal(u32),
    Cuda(u32),
}
#[derive(Clone, Copy, Debug, Default, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EmbeddingPolicy {
    #[default]
    Automatic,
    Disabled,
}

/// Options for one database process. Automatic embeddings install and warm the local model.
#[derive(Clone, Debug)]
pub struct EmbeddedOptions {
    pub data_dir: PathBuf,
    pub model_dir: Option<PathBuf>,
    pub embedding_cache_dir: Option<PathBuf>,
    pub execution_device: ExecutionDevice,
    pub embedding_device: EmbeddingDevice,
    pub embedding_policy: EmbeddingPolicy,
    pub worker_threads: Option<usize>,
}
impl EmbeddedOptions {
    pub fn new(data_dir: impl Into<PathBuf>) -> Self {
        Self {
            data_dir: data_dir.into(),
            model_dir: None,
            embedding_cache_dir: None,
            execution_device: ExecutionDevice::Auto,
            embedding_device: EmbeddingDevice::Auto,
            embedding_policy: EmbeddingPolicy::Automatic,
            worker_threads: None,
        }
    }
    pub fn with_execution_device(mut self, device: ExecutionDevice) -> Self {
        self.execution_device = device;
        self
    }
    pub fn with_embedding_policy(mut self, policy: EmbeddingPolicy) -> Self {
        self.embedding_policy = policy;
        self
    }
    pub fn with_embedding_device(mut self, device: EmbeddingDevice) -> Self {
        self.embedding_device = device;
        self
    }
    pub fn with_model_dir(mut self, directory: impl Into<PathBuf>) -> Self {
        self.model_dir = Some(directory.into());
        self
    }
    pub fn with_embedding_cache_dir(mut self, directory: impl Into<PathBuf>) -> Self {
        self.embedding_cache_dir = Some(directory.into());
        self
    }
    fn json(self) -> Value {
        let (device, ordinal) = match self.execution_device {
            ExecutionDevice::Auto => ("auto", 0),
            ExecutionDevice::Cpu => ("cpu", 0),
            ExecutionDevice::Metal(ordinal) => ("metal", ordinal),
            ExecutionDevice::Cuda(ordinal) => ("cuda", ordinal),
        };
        let (embedding_device, embedding_ordinal) = match self.embedding_device {
            EmbeddingDevice::Auto => ("auto", 0),
            EmbeddingDevice::Cpu => ("cpu", 0),
            EmbeddingDevice::Metal(ordinal) => ("metal", ordinal),
            EmbeddingDevice::Cuda(ordinal) => ("cuda", ordinal),
        };
        json!({"data_dir":self.data_dir,"model_dir":self.model_dir,"embedding_cache_dir":self.embedding_cache_dir,"execution_device":device,"device_ordinal":ordinal,"embedding_policy":self.embedding_policy,
            "embedding_device":embedding_device,"embedding_device_ordinal":embedding_ordinal,
            "worker_threads":self.worker_threads})
    }
}

struct Handle(Option<u64>, Option<tokio::sync::OwnedSemaphorePermit>);

#[cfg(test)]
mod application_paths_tests {
    use super::*;

    #[test]
    fn application_paths_serialize_independent_storage_options() {
        let options = EmbeddedOptions::new("app/data")
            .with_model_dir("app/models")
            .with_embedding_cache_dir("app/cache")
            .json();
        assert_eq!(options["data_dir"], "app/data");
        assert_eq!(options["model_dir"], "app/models");
        assert_eq!(options["embedding_cache_dir"], "app/cache");
        let defaults = EmbeddedOptions::new("app/data").json();
        assert!(defaults["model_dir"].is_null());
        assert!(defaults["embedding_cache_dir"].is_null());
    }
}

struct HandleTeardown {
    handle: u64,
    _permit: tokio::sync::OwnedSemaphorePermit,
}
struct HandleReaper {
    sender: std::sync::mpsc::SyncSender<HandleTeardown>,
    permits: std::sync::Arc<tokio::sync::Semaphore>,
    failed: std::sync::atomic::AtomicBool,
}
static HANDLE_REAPER: std::sync::OnceLock<std::result::Result<HandleReaper, String>> =
    std::sync::OnceLock::new();
#[cfg(test)]
type HandleTeardownGate = (std::sync::mpsc::Sender<()>, std::sync::mpsc::Receiver<()>);
#[cfg(test)]
static HANDLE_TEARDOWN_GATE: std::sync::Mutex<Option<HandleTeardownGate>> =
    std::sync::Mutex::new(None);

fn handle_reaper() -> Result<&'static HandleReaper> {
    let reaper = HANDLE_REAPER
        .get_or_init(|| {
            let (sender, receiver) = std::sync::mpsc::sync_channel::<HandleTeardown>(64);
            std::thread::Builder::new()
                .name("irongraph-sdk-teardown".into())
                .spawn(move || {
                    for teardown in receiver {
                        #[cfg(test)]
                        if let Some((entered, release)) =
                            HANDLE_TEARDOWN_GATE.lock().unwrap().take()
                        {
                            let _ = entered.send(());
                            let _ = release.recv();
                        }
                        let _ = std::panic::catch_unwind(|| {
                            let _ = call(json!({"action":"close","handle":teardown.handle}));
                        });
                        drop(teardown);
                    }
                })
                .map_err(|error| error.to_string())?;
            Ok(HandleReaper {
                sender,
                permits: std::sync::Arc::new(tokio::sync::Semaphore::new(64)),
                failed: std::sync::atomic::AtomicBool::new(false),
            })
        })
        .as_ref()
        .map_err(|error| Error::sdk(format!("SDK teardown worker failed: {error}")))?;
    if reaper.failed.load(std::sync::atomic::Ordering::Acquire) {
        return Err(Error::sdk("SDK teardown worker is unavailable"));
    }
    Ok(reaper)
}
#[cfg(test)]
mod embedding_configuration_tests {
    use super::*;

    #[test]
    fn native_envelope_borrows_result_and_distinguishes_null_from_missing() {
        let null: NativeResponse<'_> =
            serde_json::from_str(r#"{"ok":true,"version":"0.1.6","result":null}"#).unwrap();
        assert_eq!(null.result.unwrap().get(), "null");
        let missing: NativeResponse<'_> =
            serde_json::from_str(r#"{"ok":true,"version":"0.1.6"}"#).unwrap();
        assert!(missing.result.is_none());
        let dirty = serde_json::json!({"rows":[[{"type":"string","value":"Unicode 🦀\\\"\0".repeat(16_384)}]],"columns":[],"catalog":null,"summary":{}});
        let encoded = serde_json::json!({"ok":true,"version":"0.1.6","result":dirty}).to_string();
        let response: NativeResponse<'_> = serde_json::from_str(&encoded).unwrap();
        let raw = response.result.unwrap().get();
        assert!(raw.as_ptr() >= encoded.as_ptr());
        assert!(raw.as_bytes().as_ptr_range().end <= encoded.as_bytes().as_ptr_range().end);
        let result: QueryResult = serde_json::from_str(raw).unwrap();
        assert_eq!(serde_json::to_value(result).unwrap(), dirty);
    }

    #[test]
    fn cpu_graph_serializes_independent_embedding_device() {
        let options = EmbeddedOptions::new("test-data")
            .with_execution_device(ExecutionDevice::Cpu)
            .with_embedding_device(EmbeddingDevice::Metal(2))
            .json();
        assert_eq!(options["execution_device"], "cpu");
        assert_eq!(options["embedding_device"], "metal");
        assert_eq!(options["embedding_device_ordinal"], 2);
        assert_eq!(
            EmbeddedOptions::new("test-data").json()["embedding_device"],
            "auto"
        );
    }
}
impl Handle {
    fn open(request: Value) -> Result<Self> {
        let permit = handle_reaper()?
            .permits
            .clone()
            .try_acquire_owned()
            .map_err(|_| Error {
                code: "BACKPRESSURE".into(),
                message: "SDK native handle capacity exhausted before open".into(),
                retryable: true,
                retry_after_ms: None,
            })?;
        call(request)?["handle"]
            .as_u64()
            .filter(|handle| *handle != 0)
            .map(|handle| Self(Some(handle), Some(permit)))
            .ok_or_else(|| Error::sdk("native open returned an invalid handle"))
    }
    fn query(&self, query: Query) -> Result<QueryResult> {
        call_typed(json!({"action":"query","handle":self.0,"query":query}))
    }
    fn close(&mut self) -> Result<()> {
        if let Some(handle) = self.0.take() {
            let result = call(json!({"action":"close","handle":handle}));
            self.1.take();
            result?;
        }
        Ok(())
    }
}
impl Drop for Handle {
    fn drop(&mut self) {
        if let Some(handle) = self.0.take() {
            let teardown = HandleTeardown {
                handle,
                _permit: self
                    .1
                    .take()
                    .expect("live handle retains lifecycle admission"),
            };
            let reaper = HANDLE_REAPER
                .get()
                .expect("open initialized reaper")
                .as_ref()
                .expect("open admitted reaper");
            if let Err(error) = reaper.sender.try_send(teardown) {
                reaper
                    .failed
                    .store(true, std::sync::atomic::Ordering::Release);
                let (std::sync::mpsc::TrySendError::Full(teardown)
                | std::sync::mpsc::TrySendError::Disconnected(teardown)) = error;
                std::mem::forget(teardown);
            }
        }
    }
}

#[cfg(test)]
mod bounded_teardown_tests {
    use super::*;

    #[test]
    fn paused_native_teardown_bounds_handles_and_keeps_current_thread_responsive() {
        let directory = tempfile::tempdir().unwrap();
        let options =
            EmbeddedOptions::new(directory.path()).with_embedding_policy(EmbeddingPolicy::Disabled);
        let database = EmbeddedDatabase::open(options.clone()).unwrap();
        let (entered, observed) = std::sync::mpsc::channel();
        let (release, blocked) = std::sync::mpsc::channel();
        *HANDLE_TEARDOWN_GATE.lock().unwrap() = Some((entered, blocked));
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        runtime.block_on(async {
            drop(database);
            tokio::time::sleep(std::time::Duration::from_millis(1)).await;
        });
        observed
            .recv_timeout(std::time::Duration::from_secs(5))
            .unwrap();
        let mut handles = Vec::new();
        for _ in 0..63 {
            handles.push(RemoteClient::api("http://127.0.0.1:1").unwrap());
        }
        let error = match RemoteClient::api("http://127.0.0.1:1") {
            Err(error) => error,
            Ok(_) => panic!("teardown did not retain handle admission"),
        };
        assert_eq!(error.code, "BACKPRESSURE");
        for handle in handles {
            handle.close().unwrap();
        }
        assert!(
            EmbeddedDatabase::open(options.clone()).is_err(),
            "instance guard released before actual teardown"
        );
        release.send(()).unwrap();
        runtime.block_on(async {
            tokio::time::timeout(std::time::Duration::from_secs(5), async {
                while handle_reaper().unwrap().permits.available_permits() != 64 {
                    tokio::time::sleep(std::time::Duration::from_millis(1)).await;
                }
            })
            .await
            .unwrap();
        });
        EmbeddedDatabase::open(options.clone())
            .unwrap()
            .close()
            .unwrap();
        EmbeddedDatabase::open(options).unwrap().close().unwrap();
    }
}

static ASYNC_JOBS: std::sync::LazyLock<std::sync::Arc<tokio::sync::Semaphore>> =
    std::sync::LazyLock::new(|| std::sync::Arc::new(tokio::sync::Semaphore::new(64)));
static ASYNC_CONTROLS: std::sync::LazyLock<std::sync::Arc<tokio::sync::Semaphore>> =
    std::sync::LazyLock::new(|| std::sync::Arc::new(tokio::sync::Semaphore::new(64)));
static ASYNC_CANCELLATIONS: std::sync::LazyLock<std::sync::Arc<tokio::sync::Semaphore>> =
    std::sync::LazyLock::new(|| std::sync::Arc::new(tokio::sync::Semaphore::new(64)));
static ASYNC_OPERATION_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

struct AsyncCancellation {
    operation: Option<(u64, String)>,
    cancelled: std::sync::Arc<std::sync::atomic::AtomicBool>,
    abort: tokio::task::AbortHandle,
    runtime: tokio::runtime::Handle,
    control: Option<tokio::sync::OwnedSemaphorePermit>,
    job: std::sync::Arc<tokio::sync::OwnedSemaphorePermit>,
}
impl Drop for AsyncCancellation {
    fn drop(&mut self) {
        let Some((handle, operation_id)) = self.operation.take() else {
            return;
        };
        if self.abort.is_finished() {
            return;
        }
        self.cancelled
            .store(true, std::sync::atomic::Ordering::Release);
        // Blocking tasks can only be aborted before they start. A running native request
        // may still be parsing its packet, so retry until registration or completion.
        self.abort.abort();
        let abort = self.abort.clone();
        let control = self.control.take();
        let job = self.job.clone();
        self.runtime.spawn_blocking(move || {
            let _control = control;
            let _job = job;
            while !abort.is_finished() {
                match call(json!({"action":"cancel","handle":handle,"operation_id":operation_id})) {
                    Ok(result) if result.as_bool() == Some(true) => break,
                    Err(_) => break,
                    _ => std::thread::sleep(std::time::Duration::from_millis(1)),
                }
            }
        });
    }
}
async fn blocking<T: Send + 'static>(
    operation: Option<(u64, String)>,
    bounded: bool,
    work: impl FnOnce() -> Result<T> + Send + 'static,
) -> Result<T> {
    let runtime = tokio::runtime::Handle::try_current()
        .map_err(|_| Error::sdk("asynchronous embedded calls require a Tokio runtime"))?;
    let admission = if bounded {
        &*ASYNC_JOBS
    } else {
        &*ASYNC_CONTROLS
    };
    let permit = admission
        .clone()
        .acquire_owned()
        .await
        .map_err(|_| Error::sdk("asynchronous SDK workers are closed"))?;
    let control = if operation.is_some() {
        Some(
            ASYNC_CANCELLATIONS
                .clone()
                .acquire_owned()
                .await
                .map_err(|_| Error::sdk("asynchronous SDK cancellation workers are closed"))?,
        )
    } else {
        None
    };
    let permit = std::sync::Arc::new(permit);
    let worker_permit = permit.clone();
    let cancelled = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let worker_cancelled = cancelled.clone();
    let task = runtime.spawn_blocking(move || {
        let _permit = worker_permit;
        if worker_cancelled.load(std::sync::atomic::Ordering::Acquire) {
            return Err(Error::sdk(
                "asynchronous operation cancelled before dispatch",
            ));
        }
        work()
    });
    let mut cancellation = AsyncCancellation {
        operation,
        control,
        job: permit,
        cancelled,
        abort: task.abort_handle(),
        runtime,
    };
    let result = task.await;
    cancellation.operation = None;
    result.map_err(|error| Error::sdk(format!("native worker failed: {error}")))?
}
fn async_options(mut options: OperationOptions) -> (OperationOptions, String) {
    let id = options
        .operation_id
        .get_or_insert_with(|| {
            format!(
                "sdk:{}:{}",
                std::process::id(),
                ASYNC_OPERATION_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            )
        })
        .clone();
    (options, id)
}
fn decode<T: serde::de::DeserializeOwned>(request: Value) -> Result<T> {
    call_typed(request)
}

/// Database with synchronous and Tokio asynchronous access to the same native owner.
/// Only one embedded instance can be active in a process. Close explicitly to observe shutdown errors.
pub struct EmbeddedDatabase(Handle);

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct OperationOptions {
    pub operation_id: Option<String>,
    pub timeout_ms: Option<u64>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StreamRecord {
    pub key: Option<Vec<u8>>,
    pub headers: BTreeMap<String, Vec<u8>>,
    pub value: Option<Vec<u8>>,
    pub create_time_ms: Option<i64>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StreamAppend {
    pub project_id: String,
    pub topic: String,
    pub partition: i32,
    pub records: Vec<StreamRecord>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StreamAcknowledgement {
    pub bookmark: Bookmark,
    pub first_offset: u64,
    pub record_count: u32,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StreamFetch {
    pub project_id: String,
    pub topic: String,
    pub partition: i32,
    pub offset: u64,
    pub max_records: usize,
    pub max_bytes: usize,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StreamPayload {
    pub id: u64,
    pub resolved_time_ms: i64,
    pub ingress: Value,
    pub payload: Vec<u8>,
    pub checksum: Vec<u8>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StreamPage {
    pub records: Vec<(u64, StreamPayload)>,
    pub high_watermark: u64,
    pub next_offset: u64,
    pub truncated: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RuntimeStatus {
    pub data_dir: PathBuf,
    pub ready: bool,
    pub active_operations: usize,
    pub worker_threads: usize,
}

impl EmbeddedDatabase {
    pub async fn open_async(options: EmbeddedOptions) -> Result<Self> {
        blocking(None, true, move || Self::open(options)).await
    }
    pub async fn query_async(&self, query: Query) -> Result<QueryResult> {
        self.query_with_options_async(query, OperationOptions::default())
            .await
    }
    pub async fn query_with_options_async(
        &self,
        query: Query,
        options: OperationOptions,
    ) -> Result<QueryResult> {
        let handle = self.0.0.ok_or_else(|| Error::sdk("database is closed"))?;
        let (options, id) = async_options(options);
        blocking(Some((handle, id)), true, move || {
            decode(json!({"action":"query","handle":handle,"query":query,"options":options}))
        })
        .await
    }
    pub async fn stream_append_async(
        &self,
        request: StreamAppend,
        options: OperationOptions,
    ) -> Result<StreamAcknowledgement> {
        let handle = self.0.0.ok_or_else(|| Error::sdk("database is closed"))?;
        let (options, id) = async_options(options);
        blocking(Some((handle, id)), true, move || decode(json!({"action":"stream_append","handle":handle,"request":request,"options":options}))).await
    }
    pub async fn stream_fetch_async(
        &self,
        request: StreamFetch,
        options: OperationOptions,
    ) -> Result<StreamPage> {
        let handle = self.0.0.ok_or_else(|| Error::sdk("database is closed"))?;
        let (options, id) = async_options(options);
        blocking(Some((handle, id)), true, move || decode(json!({"action":"stream_fetch","handle":handle,"request":request,"options":options}))).await
    }
    pub async fn status_async(&self) -> Result<RuntimeStatus> {
        let handle = self.0.0;
        blocking(None, false, move || {
            decode(json!({"action":"status","handle":handle}))
        })
        .await
    }
    pub async fn cancel_async(&self, operation_id: &str) -> Result<bool> {
        let handle = self.0.0;
        let operation_id = operation_id.to_owned();
        blocking(None, false, move || {
            decode(json!({"action":"cancel","handle":handle,"operation_id":operation_id}))
        })
        .await
    }
    pub async fn flush_async(&self) -> Result<()> {
        let handle = self.0.0;
        blocking(None, true, move || {
            call(json!({"action":"flush","handle":handle})).map(|_| ())
        })
        .await
    }
    pub async fn close_async(self) -> Result<()> {
        blocking(None, false, move || self.close()).await
    }
    pub fn open(options: EmbeddedOptions) -> Result<Self> {
        Handle::open(json!({"action":"open_embedded","options":options.json()})).map(Self)
    }
    pub fn query(&self, query: Query) -> Result<QueryResult> {
        self.0.query(query)
    }
    pub fn query_with_options(
        &self,
        query: Query,
        options: OperationOptions,
    ) -> Result<QueryResult> {
        call_typed(json!({"action":"query","handle":self.0.0,"query":query,"options":options}))
    }
    pub fn stream_append(
        &self,
        request: StreamAppend,
        options: OperationOptions,
    ) -> Result<StreamAcknowledgement> {
        call_typed(
            json!({"action":"stream_append","handle":self.0.0,"request":request,"options":options}),
        )
    }
    pub fn stream_fetch(
        &self,
        request: StreamFetch,
        options: OperationOptions,
    ) -> Result<StreamPage> {
        call_typed(
            json!({"action":"stream_fetch","handle":self.0.0,"request":request,"options":options}),
        )
    }
    pub fn status(&self) -> Result<RuntimeStatus> {
        call_typed(json!({"action":"status","handle":self.0.0}))
    }
    pub fn cancel(&self, operation_id: &str) -> Result<bool> {
        call_typed(json!({"action":"cancel","handle":self.0.0,"operation_id":operation_id}))
    }
    pub fn flush(&self) -> Result<()> {
        call(json!({"action":"flush","handle":self.0.0}))?;
        Ok(())
    }
    pub fn close(mut self) -> Result<()> {
        self.0.close()
    }
}

/// PEM paths for mutual TLS. Credentials are kept outside graph data.
#[derive(Clone, Debug, Serialize)]
pub struct MutualTls {
    pub certificate: PathBuf,
    pub private_key: PathBuf,
    pub certificate_authority: PathBuf,
}
impl MutualTls {
    pub fn new(
        certificate: impl Into<PathBuf>,
        private_key: impl Into<PathBuf>,
        certificate_authority: impl Into<PathBuf>,
    ) -> Self {
        Self {
            certificate: certificate.into(),
            private_key: private_key.into(),
            certificate_authority: certificate_authority.into(),
        }
    }
}

/// Remote Query API or Bolt connection. Plain connections are restricted to loopback addresses;
/// remote connections require mutual TLS. Bolt requests require `Query::with_project`.
pub struct RemoteClient(Handle);
impl RemoteClient {
    fn connect(transport: &str, endpoint: &str, tls: Option<&MutualTls>) -> Result<Self> {
        Handle::open(json!({"action":"open_remote","options":{"transport":transport,"endpoint":endpoint,"tls":tls}})).map(Self)
    }
    pub fn api(endpoint: &str) -> Result<Self> {
        Self::connect("api", endpoint, None)
    }
    pub fn bolt(endpoint: &str) -> Result<Self> {
        Self::connect("bolt", endpoint, None)
    }
    pub fn api_mtls(endpoint: &str, tls: &MutualTls) -> Result<Self> {
        Self::connect("api", endpoint, Some(tls))
    }
    pub fn bolt_mtls(endpoint: &str, tls: &MutualTls) -> Result<Self> {
        Self::connect("bolt", endpoint, Some(tls))
    }
    pub fn query(&self, query: Query) -> Result<QueryResult> {
        self.0.query(query)
    }
    pub fn close(mut self) -> Result<()> {
        self.0.close()
    }
}