dyolo-kya 2.0.0

Know Your Agent (KYA): cryptographic chain-of-custody for recursive AI delegation with provable scope narrowing, namespace isolation, and enterprise-grade storage health
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
//! C ABI exports for dyolo-kya.
//!
//! Enable with `features = ["ffi"]`.
//!
//! This module exposes a stable C ABI so that any language capable of calling
//! a shared library can integrate dyolo-kya without a Rust toolchain:
//!
//! - **Python** — `ctypes` or `cffi`
//! - **Go** — `cgo` (`#include "dyolo_kya.h"`)
//! - **Java / JVM** — JNA or JNI
//! - **Node.js** — `node-ffi-napi` or a native addon
//! - **C / C++** — link against the `.so` / `.dll` / `.dylib` directly
//!
//! # Generated header
//!
//! Run `cbindgen --config cbindgen.toml --output dyolo_kya.h` from the
//! workspace root to regenerate the C header from this source file.
//! The header is published to `include/dyolo_kya.h` on every release.
//!
//! # Thread safety
//!
//! All exported functions are thread-safe. The chain, identity, and store
//! handles are heap-allocated Rust values wrapped in opaque pointers; the
//! caller must not alias or free them outside the provided `_free` functions.
//!
//! # Error handling
//!
//! Every function that can fail returns a `KyaStatus` integer:
//! - `0` (`KYA_OK`) — success
//! - Any other value — a `KyaError` variant (see `KyaStatus` enum)
//!
//! On failure, `dyolo_last_error()` returns a nul-terminated UTF-8 string
//! describing the error; the string is valid until the next FFI call on
//! the same thread.
//!
//! # Memory model
//!
//! - Objects returned as `*mut OpaqueType` are heap-allocated by Rust.
//!   The caller MUST free them with the corresponding `_free` function.
//! - Byte buffers (`*mut u8`) written by Rust are caller-allocated; the
//!   length is always passed in and the function writes at most that many bytes.
//! - String pointers returned by `dyolo_last_error()` are thread-local and
//!   must NOT be freed by the caller.

use std::cell::RefCell;
use std::ffi::{c_char, c_int, CStr, CString};
use std::panic;

use crate::chain::{DyoloChain, SystemClock};
use crate::error::KyaError;
use crate::identity::DyoloIdentity;
use crate::intent::{Intent, MerkleProof};
use crate::registry::{MemoryNonceStore, MemoryRevocationStore};

// Thread-local error string (avoids the need for a global mutex).
thread_local! {
    static LAST_ERROR: RefCell<Option<CString>> = const { RefCell::new(None) };
}

fn set_last_error(msg: impl Into<Vec<u8>>) {
    LAST_ERROR.with(|e| {
        let s =
            CString::new(msg).unwrap_or_else(|_| CString::new("error contains nul byte").unwrap());
        *e.borrow_mut() = Some(s);
    });
}

#[allow(dead_code)]
fn kya_error_to_status(e: &KyaError) -> c_int {
    // Map KyaError variants to integer codes.
    match e {
        KyaError::EmptyChain => 1,
        KyaError::StorageFailure(_) => 2,
        KyaError::RootMismatch => 3,
        KyaError::BrokenLinkage(_) => 4,
        KyaError::InvalidSignature(_) => 5,
        KyaError::NotYetValid(..) => 6,
        KyaError::Expired(..) => 7,
        KyaError::TemporalViolation(..) => 8,
        KyaError::MaxDepthExceeded(..) => 9,
        KyaError::InvalidSubScopeProof => 10,
        KyaError::ScopeEscalation(_) => 11,
        KyaError::UnauthorizedLeaf => 12,
        KyaError::ScopeViolation => 13,
        KyaError::NonceReplay => 14,
        KyaError::Revoked => 15,
        KyaError::IntentNotFound => 16,
        KyaError::EmptyTree => 17,
        KyaError::WireFormatError(_) => 18,
        KyaError::UnsupportedVersion { .. } => 19,
        _ => 99,
    }
}

/// Return value of every fallible FFI function.
///
/// `KYA_OK = 0` is the only success value; all other values are errors.
/// Call `dyolo_last_error()` immediately after a non-zero return to read
/// a human-readable description of the failure.
/// Returns a `KyaStatus` code detailing the failure reason.
/// Documented for `cbindgen` export.
#[repr(C)]
pub enum KyaStatus {
    /// Operation succeeded.
    KyaOk = 0,
    /// The delegation chain is empty.
    KyaErrEmptyChain = 1,
    /// The storage backend failed.
    KyaErrStorageFailure = 2,
    /// Chain root does not match the expected principal.
    KyaErrRootMismatch = 3,
    /// Delegation link broken at hop N.
    KyaErrBrokenLinkage = 4,
    /// Invalid cryptographic signature at hop N.
    KyaErrInvalidSig = 5,
    /// Certificate is not yet valid (clock drift or future issuance).
    KyaErrNotYetValid = 6,
    /// Certificate has expired.
    KyaErrExpired = 7,
    /// Temporal violation: child outlives parent.
    KyaErrTemporalViol = 8,
    /// Delegation depth exceeds policy or cert maximum.
    KyaErrMaxDepth = 9,
    /// The sub-scope or merkle proof is invalid.
    KyaErrInvalidProof = 10,
    /// Scope escalation: child attempts to delegate scope it does not have.
    KyaErrScopeEscal = 11,
    /// The executing agent is not the terminal delegate.
    KyaErrUnauthorized = 12,
    /// The requested intent is not permitted by the terminal scope.
    KyaErrScopeViol = 13,
    /// Nonce replay detected.
    KyaErrNonceReplay = 14,
    /// A certificate in the chain has been revoked.
    KyaErrRevoked = 15,
    /// Intent not found in the scope tree.
    KyaErrIntentNotFound = 16,
    /// Attempted to build an empty scope tree.
    KyaErrEmptyTree = 17,
    /// Invalid wire format (JSON/CBOR parse error).
    KyaErrWireFormat = 18,
    /// Unsupported certificate version.
    KyaErrUnsupportedVer = 19,
    /// A Rust panic occurred.
    KyaErrPanic = 98,
    /// Unknown or unmapped error.
    KyaErrUnknown = 99,
}

/// Opaque handle to a [`DyoloIdentity`].
pub struct OpaqueIdentity(DyoloIdentity);

/// Opaque handle to a persistent [`MemoryRevocationStore`].
pub struct OpaqueRevocationStore(MemoryRevocationStore);

/// Opaque handle to a persistent [`MemoryNonceStore`].
pub struct OpaqueNonceStore(MemoryNonceStore);

/// Opaque handle to a [`DyoloChain`] plus its in-process stores.
#[allow(dead_code)]
pub struct OpaqueChain {
    chain: DyoloChain,
    rev: MemoryRevocationStore,
    nonces: MemoryNonceStore,
}

// ── Error reporting ───────────────────────────────────────────────────────────

/// Returns a nul-terminated UTF-8 string describing the last error that
/// occurred on this thread, or a null pointer if no error has been set.
///
/// The returned pointer is valid until the next FFI call on this thread.
/// Do NOT free it.
///
/// # Safety
///
/// `LAST_ERROR` is thread-local; this function is safe to call from any thread
/// that previously called a `dyolo_*` function.
///
/// # Example (Python cffi)
/// ```python
/// err = lib.dyolo_last_error()
/// if err:
///     print(ffi.string(err).decode())
/// ```
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_last_error() -> *const c_char {
    LAST_ERROR.with(|e| e.borrow().as_ref().map_or(std::ptr::null(), |s| s.as_ptr()))
}

// ── Identity ──────────────────────────────────────────────────────────────────

/// Generate a new random `DyoloIdentity` and return an opaque handle.
///
/// The caller MUST free the returned handle with `dyolo_identity_free()`.
///
/// Returns `NULL` on allocation failure (extremely unlikely).
#[unsafe(no_mangle)]
pub extern "C" fn dyolo_identity_generate() -> *mut OpaqueIdentity {
    Box::into_raw(Box::new(OpaqueIdentity(DyoloIdentity::generate())))
}

/// Restore a `DyoloIdentity` from a 32-byte signing key seed.
///
/// `seed` must point to exactly 32 bytes of key material. The caller retains
/// ownership of `seed` and must zeroize it after calling this function.
///
/// Returns `NULL` if `seed` is null.
///
/// # Safety
/// `seed` must be a valid pointer to at least 32 bytes.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_identity_from_seed(seed: *const u8) -> *mut OpaqueIdentity {
    if seed.is_null() {
        set_last_error("dyolo_identity_from_seed: seed pointer is null");
        return std::ptr::null_mut();
    }
    let bytes: [u8; 32] = unsafe { std::slice::from_raw_parts(seed, 32) }
        .try_into()
        .expect("seed is always 32 bytes");
    Box::into_raw(Box::new(OpaqueIdentity(DyoloIdentity::from_signing_bytes(
        &bytes,
    ))))
}

/// Write the 32-byte Ed25519 verifying key of `identity` into `out`.
///
/// `out` must point to a caller-allocated buffer of at least 32 bytes.
///
/// Returns `KYA_OK` (0) on success.
///
/// # Safety
/// `identity` and `out` must be valid, non-null pointers.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_identity_verifying_key(
    identity: *const OpaqueIdentity,
    out: *mut u8,
) -> c_int {
    if identity.is_null() || out.is_null() {
        set_last_error("null pointer argument");
        return KyaStatus::KyaErrUnknown as c_int;
    }
    let vk = unsafe { (*identity).0.verifying_key() };
    unsafe { std::ptr::copy_nonoverlapping(vk.as_bytes().as_ptr(), out, 32) };
    KyaStatus::KyaOk as c_int
}

/// Free a `DyoloIdentity` handle previously returned by `dyolo_identity_generate`
/// or `dyolo_identity_from_seed`.
///
/// # Safety
/// `identity` must be a valid, non-null pointer returned by this library.
/// Calling this twice on the same pointer is undefined behavior.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_identity_free(identity: *mut OpaqueIdentity) {
    if !identity.is_null() {
        let _ = unsafe { Box::from_raw(identity) };
    }
}

// ── Stores ────────────────────────────────────────────────────────────────────

/// Allocate a new persistent in-memory revocation store.
#[unsafe(no_mangle)]
pub extern "C" fn dyolo_revocation_store_new() -> *mut OpaqueRevocationStore {
    Box::into_raw(Box::new(
        OpaqueRevocationStore(MemoryRevocationStore::new()),
    ))
}

/// Free a revocation store handle.
///
/// # Safety
///
/// `store` must be a valid pointer obtained from `dyolo_revocation_store_new()`,
/// or `NULL`. Must not be freed more than once.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_revocation_store_free(store: *mut OpaqueRevocationStore) {
    if !store.is_null() {
        let _ = unsafe { Box::from_raw(store) };
    }
}

/// Allocate a new persistent in-memory nonce store.
#[unsafe(no_mangle)]
pub extern "C" fn dyolo_nonce_store_new() -> *mut OpaqueNonceStore {
    Box::into_raw(Box::new(OpaqueNonceStore(MemoryNonceStore::new())))
}

/// Free a nonce store handle.
///
/// # Safety
///
/// `store` must be a valid pointer obtained from `dyolo_nonce_store_new()`,
/// or `NULL`. Must not be freed more than once.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_nonce_store_free(store: *mut OpaqueNonceStore) {
    if !store.is_null() {
        let _ = unsafe { Box::from_raw(store) };
    }
}

// ── Cert Operations ───────────────────────────────────────────────────────────

/// Revoke a certificate by fingerprint in the provided revocation store.
/// `fingerprint_hex` must be a 64-character null-terminated hex string.
///
/// # Safety
///
/// `store` must be a valid non-null pointer from `dyolo_revocation_store_new()`.
/// `fingerprint_hex` must be a valid null-terminated C string of exactly 64 hex characters.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_cert_revoke(
    store: *mut OpaqueRevocationStore,
    fingerprint_hex: *const c_char,
) -> c_int {
    let result = panic::catch_unwind(|| {
        if store.is_null() || fingerprint_hex.is_null() {
            return Err("null pointer argument".to_string());
        }
        let fp_str = unsafe { CStr::from_ptr(fingerprint_hex) }
            .to_str()
            .map_err(|e| format!("invalid utf-8: {e}"))?;
        let fp_bytes: [u8; 32] = hex::decode(fp_str)
            .map_err(|e| format!("invalid hex: {e}"))?
            .try_into()
            .map_err(|_| "fingerprint must be 32 bytes".to_string())?;

        use crate::registry::RevocationStore;
        unsafe { &(*store).0 }
            .revoke(&fp_bytes)
            .map_err(|e| e.to_string())
    });

    match result {
        Ok(Ok(())) => KyaStatus::KyaOk as c_int,
        Ok(Err(msg)) => {
            set_last_error(msg);
            KyaStatus::KyaErrUnknown as c_int
        }
        Err(_) => {
            set_last_error("internal panic");
            KyaStatus::KyaErrPanic as c_int
        }
    }
}

// ── JSON authorize ────────────────────────────────────────────────────────────

/// Verify a JSON-encoded `SignedChain` for a given intent and write the
/// JSON-encoded `VerifiedToken` (HMAC-authenticated receipt) into `out_buf`.
///
/// Parameters:
/// - `rev_store`       — pointer to a persistent `OpaqueRevocationStore`
/// - `nonce_store`     — pointer to a persistent `OpaqueNonceStore`
/// - `chain_json`      — nul-terminated UTF-8 JSON from `SignedChain::to_json()`
/// - `agent_pk_hex`    — nul-terminated hex-encoded 32-byte Ed25519 verifying key
/// - `intent_action`   — nul-terminated UTF-8 action name (e.g. `"trade.equity"`)
/// - `mac_key`         — pointer to 32 bytes of MAC key shared with the executor
/// - `out_buf`         — caller-allocated buffer for the JSON output
/// - `out_buf_len`     — length of `out_buf` in bytes
///
/// Returns `KYA_OK` (0) on success, a non-zero `KyaStatus` on failure.
/// On failure, `dyolo_last_error()` contains the error description.
/// On success, `out_buf` contains a nul-terminated JSON `VerifiedToken`.
///
/// # Safety
/// All pointer arguments must be valid and non-null.
#[cfg(feature = "wire")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_authorize_json(
    rev_store: *mut OpaqueRevocationStore,
    nonce_store: *mut OpaqueNonceStore,
    chain_json: *const c_char,
    agent_pk_hex: *const c_char,
    intent_action: *const c_char,
    mac_key: *const u8,
    out_buf: *mut c_char,
    out_buf_len: usize,
) -> c_int {
    let result = panic::catch_unwind(|| {
        // Safety: all pointers are checked non-null by the guard below.
        if chain_json.is_null()
            || agent_pk_hex.is_null()
            || intent_action.is_null()
            || mac_key.is_null()
            || out_buf.is_null()
            || out_buf_len == 0
        {
            return Err("null or zero-length argument".to_string());
        }

        if rev_store.is_null() || nonce_store.is_null() {
            return Err("null store pointer argument".to_string());
        }

        let chain_str = unsafe { CStr::from_ptr(chain_json) }
            .to_str()
            .map_err(|e| format!("chain_json is not valid UTF-8: {e}"))?;

        let pk_hex = unsafe { CStr::from_ptr(agent_pk_hex) }
            .to_str()
            .map_err(|e| format!("agent_pk_hex is not valid UTF-8: {e}"))?;

        let action = unsafe { CStr::from_ptr(intent_action) }
            .to_str()
            .map_err(|e| format!("intent_action is not valid UTF-8: {e}"))?;

        let mac: [u8; 32] = unsafe { std::slice::from_raw_parts(mac_key, 32) }
            .try_into()
            .map_err(|_| "mac_key must be 32 bytes".to_string())?;

        // Parse agent public key
        let pk_bytes: [u8; 32] = hex::decode(pk_hex)
            .map_err(|e| format!("invalid agent_pk_hex: {e}"))?
            .try_into()
            .map_err(|_| "agent_pk must be 32 bytes".to_string())?;
        let agent_pk = ed25519_dalek::VerifyingKey::from_bytes(&pk_bytes)
            .map_err(|e| format!("invalid agent public key: {e}"))?;

        // Deserialize the chain
        let signed: crate::wire::SignedChain =
            serde_json::from_str(chain_str).map_err(|e| format!("chain_json parse error: {e}"))?;

        #[allow(deprecated)]
        let chain = signed
            .into_chain()
            .map_err(|e| format!("chain conversion error: {e}"))?;

        // Full intent path - use Intent::new to build the structural hash safely
        let intent = Intent::new(action).map_err(|e| format!("intent error: {e}"))?;
        let intent_hash = intent.hash();

        let action_result = chain
            .authorize(
                &agent_pk,
                &intent_hash,
                &MerkleProof::default(), // Pass-through root match only via this basic FFI endpoint
                &SystemClock,
                unsafe { &(*rev_store).0 },
                unsafe { &(*nonce_store).0 },
            )
            .map_err(|e| format!("{e}"))?;

        let token = crate::wire::VerifiedToken::sign(&action_result.receipt, &mac);
        serde_json::to_string(&token).map_err(|e| format!("token serialization: {e}"))
    });

    match result {
        Ok(Ok(json)) => {
            let cstr = CString::new(json).unwrap_or_default();
            let bytes = cstr.as_bytes_with_nul();
            if bytes.len() > out_buf_len {
                set_last_error(format!(
                    "output buffer too small: need {}, got {out_buf_len}",
                    bytes.len()
                ));
                return KyaStatus::KyaErrUnknown as c_int;
            }
            unsafe {
                std::ptr::copy_nonoverlapping(bytes.as_ptr(), out_buf as *mut u8, bytes.len())
            };
            KyaStatus::KyaOk as c_int
        }
        Ok(Err(msg)) => {
            set_last_error(msg);
            KyaStatus::KyaErrUnknown as c_int
        }
        Err(_panic) => {
            set_last_error("internal panic in dyolo_authorize_json");
            KyaStatus::KyaErrPanic as c_int
        }
    }
}

/// Authorize without a `VerifiedToken` — write a JSON `VerificationReceipt`
/// to `out_buf`. Use this when no cross-service MAC transport is needed
/// (e.g. the caller is logging the receipt for audit purposes only).
///
/// Parameters match `dyolo_authorize_json` except there is no `mac_key`.
///
/// Returns `KYA_OK` (0) on success.
///
/// # Safety
/// All pointer arguments must be valid and non-null.
#[cfg(feature = "wire")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_authorize_receipt_json(
    rev_store: *mut OpaqueRevocationStore,
    nonce_store: *mut OpaqueNonceStore,
    chain_json: *const c_char,
    agent_pk_hex: *const c_char,
    intent_action: *const c_char,
    out_buf: *mut c_char,
    out_buf_len: usize,
) -> c_int {
    let result = panic::catch_unwind(|| {
        if chain_json.is_null()
            || agent_pk_hex.is_null()
            || intent_action.is_null()
            || out_buf.is_null()
            || out_buf_len == 0
            || rev_store.is_null()
            || nonce_store.is_null()
        {
            return Err("null or zero-length argument".to_string());
        }

        let chain_str = unsafe { CStr::from_ptr(chain_json) }
            .to_str()
            .map_err(|e| format!("chain_json: {e}"))?;
        let pk_hex = unsafe { CStr::from_ptr(agent_pk_hex) }
            .to_str()
            .map_err(|e| format!("agent_pk_hex: {e}"))?;
        let action = unsafe { CStr::from_ptr(intent_action) }
            .to_str()
            .map_err(|e| format!("intent_action: {e}"))?;

        let pk_bytes: [u8; 32] = hex::decode(pk_hex)
            .map_err(|e| format!("invalid agent_pk_hex: {e}"))?
            .try_into()
            .map_err(|_| "agent_pk must be 32 bytes".to_string())?;
        let agent_pk = ed25519_dalek::VerifyingKey::from_bytes(&pk_bytes)
            .map_err(|e| format!("invalid agent public key: {e}"))?;

        let signed: crate::wire::SignedChain =
            serde_json::from_str(chain_str).map_err(|e| format!("chain_json parse error: {e}"))?;

        #[allow(deprecated)]
        let chain = signed.into_chain().map_err(|e| format!("{e}"))?;

        let intent = Intent::new(action).map_err(|e| format!("intent error: {e}"))?;
        let intent_hash = intent.hash();

        let authorized = chain
            .authorize(
                &agent_pk,
                &intent_hash,
                &MerkleProof::default(),
                &SystemClock,
                unsafe { &(*rev_store).0 },
                unsafe { &(*nonce_store).0 },
            )
            .map_err(|e| format!("{e}"))?;

        serde_json::to_string(&authorized.receipt)
            .map_err(|e| format!("receipt serialization: {e}"))
    });

    match result {
        Ok(Ok(json)) => {
            let cstr = CString::new(json).unwrap_or_default();
            let bytes = cstr.as_bytes_with_nul();
            if bytes.len() > out_buf_len {
                set_last_error(format!(
                    "output buffer too small: need {}, got {out_buf_len}",
                    bytes.len()
                ));
                return KyaStatus::KyaErrUnknown as c_int;
            }
            unsafe {
                std::ptr::copy_nonoverlapping(bytes.as_ptr(), out_buf as *mut u8, bytes.len())
            };
            KyaStatus::KyaOk as c_int
        }
        Ok(Err(msg)) => {
            set_last_error(msg);
            KyaStatus::KyaErrUnknown as c_int
        }
        Err(_) => {
            set_last_error("internal panic");
            KyaStatus::KyaErrPanic as c_int
        }
    }
}

/// Authorize with a provided Merkle Proof.
/// `proof_json` must be a serialized `MerkleProof`.
///
/// # Safety
///
/// All pointer arguments must be valid, non-null, null-terminated C strings (except `mac_key`
/// which must point to at least 32 bytes, and `out_buf` which must point to `out_buf_len` bytes).
/// `rev_store` and `nonce_store` must be valid pointers from their respective `_new` functions.
#[cfg(feature = "wire")]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dyolo_authorize_with_proof_json(
    rev_store: *mut OpaqueRevocationStore,
    nonce_store: *mut OpaqueNonceStore,
    chain_json: *const c_char,
    agent_pk_hex: *const c_char,
    intent_action: *const c_char,
    proof_json: *const c_char,
    mac_key: *const u8,
    out_buf: *mut c_char,
    out_buf_len: usize,
) -> c_int {
    let result = panic::catch_unwind(|| {
        if chain_json.is_null()
            || agent_pk_hex.is_null()
            || intent_action.is_null()
            || mac_key.is_null()
            || out_buf.is_null()
            || out_buf_len == 0
            || proof_json.is_null()
            || rev_store.is_null()
            || nonce_store.is_null()
        {
            return Err("null argument".to_string());
        }

        let proof_str = unsafe { CStr::from_ptr(proof_json) }
            .to_str()
            .map_err(|e| e.to_string())?;
        let proof: MerkleProof = serde_json::from_str(proof_str).map_err(|e| e.to_string())?;

        let chain_str = unsafe { CStr::from_ptr(chain_json) }
            .to_str()
            .map_err(|e| e.to_string())?;
        let action = unsafe { CStr::from_ptr(intent_action) }
            .to_str()
            .map_err(|e| e.to_string())?;

        let pk_hex = unsafe { CStr::from_ptr(agent_pk_hex) }
            .to_str()
            .map_err(|e| e.to_string())?;
        let pk_bytes: [u8; 32] = hex::decode(pk_hex)
            .map_err(|e| e.to_string())?
            .try_into()
            .map_err(|_| "32 bytes".to_string())?;
        let agent_pk =
            ed25519_dalek::VerifyingKey::from_bytes(&pk_bytes).map_err(|e| e.to_string())?;

        let mac: [u8; 32] = unsafe { std::slice::from_raw_parts(mac_key, 32) }
            .try_into()
            .map_err(|_| "32 bytes".to_string())?;

        let signed: crate::wire::SignedChain =
            serde_json::from_str(chain_str).map_err(|e| e.to_string())?;

        #[allow(deprecated)]
        let chain = signed.into_chain().map_err(|e| e.to_string())?;

        let intent = Intent::new(action).map_err(|e| e.to_string())?;
        let intent_hash = intent.hash();

        let action_result = chain
            .authorize(
                &agent_pk,
                &intent_hash,
                &proof,
                &SystemClock,
                unsafe { &(*rev_store).0 },
                unsafe { &(*nonce_store).0 },
            )
            .map_err(|e| e.to_string())?;

        let token = crate::wire::VerifiedToken::sign(&action_result.receipt, &mac);
        serde_json::to_string(&token).map_err(|e| e.to_string())
    });

    match result {
        Ok(Ok(json)) => {
            let cstr = CString::new(json).unwrap_or_default();
            let bytes = cstr.as_bytes_with_nul();
            if bytes.len() > out_buf_len {
                return KyaStatus::KyaErrUnknown as c_int;
            }
            unsafe {
                std::ptr::copy_nonoverlapping(bytes.as_ptr(), out_buf as *mut u8, bytes.len())
            };
            KyaStatus::KyaOk as c_int
        }
        Ok(Err(msg)) => {
            set_last_error(msg);
            KyaStatus::KyaErrUnknown as c_int
        }
        Err(_) => KyaStatus::KyaErrPanic as c_int,
    }
}

// ── Version ───────────────────────────────────────────────────────────────────

/// Return the nul-terminated semantic version string of this build
/// (e.g. `"2.0.0"`).
///
/// The returned pointer is valid for the lifetime of the process.
/// Do NOT free it.
#[unsafe(no_mangle)]
pub extern "C" fn dyolo_version() -> *const c_char {
    // SAFETY: This is a static string literal, always valid UTF-8 and nul-terminated.
    concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr().cast()
}