jacs 0.10.0

JACS JSON AI Communication Standard
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
//! Type definitions for the simplified JACS API.
//!
//! Contains all public types returned by or passed to [`super::SimpleAgent`] methods:
//! agent info, signed documents, verification results, agreement status,
//! creation parameters, key rotation results, and migration results.

use crate::agent::document::JACSDocument;
use crate::error::JacsError;
use crate::schema::utils::ValueExt;
use crate::storage::MultiStorage;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

// =============================================================================
// Types
// =============================================================================

/// Information about a created or loaded agent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentInfo {
    /// Unique identifier for the agent (UUID).
    pub agent_id: String,
    /// Human-readable name of the agent.
    pub name: String,
    /// Path to the public key file.
    pub public_key_path: String,
    /// Path to the configuration file.
    pub config_path: String,
    /// Agent version string.
    #[serde(default)]
    pub version: String,
    /// Signing algorithm used.
    #[serde(default)]
    pub algorithm: String,
    /// Path to the private key file.
    #[serde(default)]
    pub private_key_path: String,
    /// Data directory path.
    #[serde(default)]
    pub data_directory: String,
    /// Key directory path.
    #[serde(default)]
    pub key_directory: String,
    /// Agent domain (if configured).
    #[serde(default)]
    pub domain: String,
    /// DNS TXT record to publish (if domain was configured).
    #[serde(default)]
    pub dns_record: String,
}

/// A signed JACS document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedDocument {
    /// The full JSON string of the signed JACS document.
    pub raw: String,
    /// Unique identifier for this document (UUID).
    pub document_id: String,
    /// ID of the agent that signed this document.
    pub agent_id: String,
    /// ISO 8601 timestamp of when the document was signed.
    pub timestamp: String,
}

impl SignedDocument {
    pub(crate) fn from_jacs_document(
        jacs_doc: JACSDocument,
        serialized_kind: &str,
    ) -> Result<Self, JacsError> {
        let raw = serde_json::to_string(&jacs_doc.value).map_err(|e| JacsError::Internal {
            message: format!("Failed to serialize {}: {}", serialized_kind, e),
        })?;

        let timestamp = jacs_doc
            .value
            .get_path_str_or(&["jacsSignature", "date"], "");
        let agent_id = jacs_doc
            .value
            .get_path_str_or(&["jacsSignature", "agentID"], "");

        Ok(Self {
            raw,
            document_id: jacs_doc.id,
            agent_id,
            timestamp,
        })
    }
}

/// Result of verifying a signed document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationResult {
    /// Whether the signature is valid.
    pub valid: bool,
    /// The original data that was signed (extracted from the document).
    pub data: Value,
    /// ID of the agent that signed the document.
    pub signer_id: String,
    /// Name of the signer (if available in trust store).
    pub signer_name: Option<String>,
    /// ISO 8601 timestamp of when the document was signed.
    pub timestamp: String,
    /// Any file attachments included in the document.
    pub attachments: Vec<Attachment>,
    /// Error messages if verification failed.
    pub errors: Vec<String>,
}

impl VerificationResult {
    /// Creates a failed verification result with the given error message.
    ///
    /// This is a convenience constructor for creating a `VerificationResult`
    /// that represents a failed verification.
    ///
    /// # Arguments
    ///
    /// * `error` - The error message describing why verification failed
    ///
    /// # Returns
    ///
    /// A `VerificationResult` with `valid: false` and the error in the `errors` field.
    #[must_use]
    pub fn failure(error: String) -> Self {
        Self {
            valid: false,
            data: json!(null),
            signer_id: String::new(),
            signer_name: None,
            timestamp: String::new(),
            attachments: vec![],
            errors: vec![error],
        }
    }

    /// Creates a successful verification result.
    ///
    /// # Arguments
    ///
    /// * `data` - The verified data/content
    /// * `signer_id` - The ID of the agent that signed the document
    /// * `timestamp` - The timestamp when the document was signed
    ///
    /// # Returns
    ///
    /// A `VerificationResult` with `valid: true` and no errors.
    #[must_use]
    pub fn success(data: Value, signer_id: String, timestamp: String) -> Self {
        Self {
            valid: true,
            data,
            signer_id,
            signer_name: None,
            timestamp,
            attachments: vec![],
            errors: vec![],
        }
    }
}

/// A file attachment in a signed document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Attachment {
    /// Original filename.
    pub filename: String,
    /// MIME type of the file.
    pub mime_type: String,
    /// File content (decoded if it was embedded).
    #[serde(with = "base64_bytes")]
    pub content: Vec<u8>,
    /// SHA-256 hash of the original file.
    pub hash: String,
    /// Whether the file was embedded (true) or referenced (false).
    pub embedded: bool,
}

// Custom serialization for Vec<u8> as base64
mod base64_bytes {
    use base64::{Engine as _, engine::general_purpose::STANDARD};
    use serde::{Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&STANDARD.encode(bytes))
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        STANDARD.decode(&s).map_err(serde::de::Error::custom)
    }
}

/// Setup instructions for publishing a JACS agent's DNS record and enabling DNSSEC.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SetupInstructions {
    /// BIND-format DNS record line (e.g. `_v1.agent.jacs.example.com. 3600 IN TXT "..."`)
    pub dns_record_bind: String,
    /// The raw TXT record value (without the owner/TTL/class prefix).
    pub dns_record_value: String,
    /// The DNS owner name (e.g. `_v1.agent.jacs.example.com.`)
    pub dns_owner: String,
    /// Provider-specific CLI/API commands keyed by provider name.
    pub provider_commands: std::collections::HashMap<String, String>,
    /// Provider-specific DNSSEC guidance keyed by provider name.
    pub dnssec_instructions: std::collections::HashMap<String, String>,
    /// Guidance about domain ownership requirements.
    pub tld_requirement: String,
    /// JSON payload for `/.well-known/jacs-agent.json`.
    pub well_known_json: String,
    /// Human-readable summary of all setup steps.
    pub summary: String,
}

// =============================================================================
// Programmatic Creation Parameters
// =============================================================================

/// Parameters for programmatic agent creation.
///
/// Provides full control over agent creation without interactive prompts.
/// Use `CreateAgentParams::builder()` for a fluent API, or construct directly.
///
/// # Example
///
/// ```rust,ignore
/// use jacs::simple::CreateAgentParams;
///
/// let params = CreateAgentParams::builder()
///     .name("my-agent")
///     .password("MyStr0ng!Pass#2024")
///     .algorithm("pq2025")
///     .build();
///
/// let (agent, info) = SimpleAgent::create_with_params(params)?;
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateAgentParams {
    /// Human-readable name for the agent (required).
    pub name: String,
    /// Password for encrypting the private key.
    /// If empty, falls back to JACS_PRIVATE_KEY_PASSWORD env var.
    #[serde(default)]
    pub password: String,
    /// Signing algorithm. Default: "pq2025".
    #[serde(default = "default_algorithm")]
    pub algorithm: String,
    /// Directory for data storage. Default: "./jacs_data".
    #[serde(default = "default_data_directory")]
    pub data_directory: String,
    /// Directory for keys. Default: "./jacs_keys".
    #[serde(default = "default_key_directory")]
    pub key_directory: String,
    /// Path to the config file. Default: "./jacs.config.json".
    #[serde(default = "default_config_path")]
    pub config_path: String,
    /// Agent type (e.g., "ai", "human", "hybrid"). Default: "ai".
    #[serde(default = "default_agent_type")]
    pub agent_type: String,
    /// Description of the agent's purpose.
    #[serde(default)]
    pub description: String,
    /// Agent domain for DNSSEC fingerprint (optional).
    #[serde(default)]
    pub domain: String,
    /// Default storage backend. Default: "fs".
    #[serde(default = "default_storage")]
    pub default_storage: String,
    /// Optional pre-configured storage backend.
    ///
    /// When `Some`, the agent will use this storage backend instead of
    /// creating one from `default_storage` and `data_directory`. This is
    /// useful for testing (in-memory backends) or when the caller has
    /// already configured storage with specific options.
    ///
    /// When `None` (the default), the agent creates its own storage from
    /// the `default_storage` type and `data_directory`.
    ///
    /// # Design Note
    ///
    /// The PRD (Section 4.2.4) specifies `Option<Box<dyn StorageDocumentTraits>>`.
    /// The current implementation uses `Option<MultiStorage>` (concrete type)
    /// because `Agent::set_storage()` accepts `MultiStorage`. `MultiStorage`
    /// already supports `fs`, `memory`, and `aws` backends internally, which
    /// covers all current use cases. Migrating to a trait object would require
    /// refactoring `Agent::set_storage()` and is tracked as a future improvement.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use jacs::storage::MultiStorage;
    /// use jacs::simple::CreateAgentParams;
    ///
    /// let memory_storage = MultiStorage::new("memory".to_string())?;
    /// let params = CreateAgentParams::builder()
    ///     .name("test-agent")
    ///     .password("secret")
    ///     .storage(memory_storage)
    ///     .build();
    /// ```
    #[serde(skip)]
    pub storage: Option<MultiStorage>,
}

fn default_algorithm() -> String {
    "pq2025".to_string()
}
fn default_data_directory() -> String {
    "./jacs_data".to_string()
}
fn default_key_directory() -> String {
    "./jacs_keys".to_string()
}
fn default_config_path() -> String {
    "./jacs.config.json".to_string()
}
fn default_agent_type() -> String {
    "ai".to_string()
}
fn default_storage() -> String {
    "fs".to_string()
}

impl Default for CreateAgentParams {
    fn default() -> Self {
        Self {
            name: String::new(),
            password: String::new(),
            algorithm: default_algorithm(),
            data_directory: default_data_directory(),
            key_directory: default_key_directory(),
            config_path: default_config_path(),
            agent_type: default_agent_type(),
            description: String::new(),
            domain: String::new(),
            default_storage: default_storage(),
            storage: None,
        }
    }
}

impl CreateAgentParams {
    /// Returns a new builder for `CreateAgentParams`.
    pub fn builder() -> CreateAgentParamsBuilder {
        CreateAgentParamsBuilder::default()
    }
}

/// Fluent builder for `CreateAgentParams`.
#[derive(Debug, Clone, Default)]
pub struct CreateAgentParamsBuilder {
    params: CreateAgentParams,
}

impl CreateAgentParamsBuilder {
    pub fn name(mut self, name: &str) -> Self {
        self.params.name = name.to_string();
        self
    }
    pub fn password(mut self, password: &str) -> Self {
        self.params.password = password.to_string();
        self
    }
    pub fn algorithm(mut self, algorithm: &str) -> Self {
        self.params.algorithm = algorithm.to_string();
        self
    }
    pub fn data_directory(mut self, dir: &str) -> Self {
        self.params.data_directory = dir.to_string();
        self
    }
    pub fn key_directory(mut self, dir: &str) -> Self {
        self.params.key_directory = dir.to_string();
        self
    }
    pub fn config_path(mut self, path: &str) -> Self {
        self.params.config_path = path.to_string();
        self
    }
    pub fn agent_type(mut self, agent_type: &str) -> Self {
        self.params.agent_type = agent_type.to_string();
        self
    }
    pub fn description(mut self, desc: &str) -> Self {
        self.params.description = desc.to_string();
        self
    }
    pub fn domain(mut self, domain: &str) -> Self {
        self.params.domain = domain.to_string();
        self
    }
    pub fn default_storage(mut self, storage_type: &str) -> Self {
        self.params.default_storage = storage_type.to_string();
        self
    }
    /// Set a pre-configured storage backend.
    ///
    /// When set, the agent will use this storage instead of creating one
    /// from `default_storage` and `data_directory`. Useful for in-memory
    /// testing or custom storage configurations.
    pub fn storage(mut self, storage: MultiStorage) -> Self {
        self.params.storage = Some(storage);
        self
    }
    /// Build the `CreateAgentParams`. Name is required.
    pub fn build(self) -> CreateAgentParams {
        self.params
    }
}

/// Status of a single signer in a multi-party agreement.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignerStatus {
    /// Unique identifier of the signing agent.
    pub agent_id: String,
    /// Whether this agent has signed the agreement.
    pub signed: bool,
    /// ISO 8601 timestamp when the agent signed (if signed).
    pub signed_at: Option<String>,
}

/// Status of a multi-party agreement.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgreementStatus {
    /// Whether all required parties have signed.
    pub complete: bool,
    /// List of signers and their status.
    pub signers: Vec<SignerStatus>,
    /// List of agent IDs that haven't signed yet.
    pub pending: Vec<String>,
}

// =============================================================================
// Key Rotation
// =============================================================================

/// Result of a key rotation operation.
///
/// Returned by [`super::SimpleAgent::rotate()`] after successfully generating new keys,
/// archiving old keys, and producing a new self-signed agent document.
///
/// The `signed_agent_json` field contains the complete, self-signed agent document
/// with the new version and new public key. This is the document that should be
/// sent to HAI for re-registration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RotationResult {
    /// The agent's stable identity (unchanged across rotations).
    pub jacs_id: String,
    /// The version string of the agent before rotation.
    pub old_version: String,
    /// The new version string assigned during rotation.
    pub new_version: String,
    /// PEM-encoded public key for the new keypair.
    pub new_public_key_pem: String,
    /// SHA-256 hash of the new public key (hex-encoded).
    pub new_public_key_hash: String,
    /// The complete, self-signed agent JSON document with the new version.
    pub signed_agent_json: String,
    /// JSON string of the `jacsKeyRotationProof` object (transition signature
    /// proving the rotation was authorized by the old key holder).
    /// `None` if the rotation was performed without transition proof generation.
    pub transition_proof: Option<String>,
}

/// Result of a legacy agent migration operation.
///
/// Returned by [`super::SimpleAgent::migrate_agent()`] after successfully patching
/// a pre-schema-change agent document to include required `iat` and `jti`
/// fields in `jacsSignature`, then re-signing it as a new version.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrateResult {
    /// The agent's stable identity (unchanged across migrations).
    pub jacs_id: String,
    /// The version string of the agent before migration.
    pub old_version: String,
    /// The new version string assigned during migration (after re-signing).
    pub new_version: String,
    /// Fields that were patched in the raw JSON before loading (e.g. `["iat", "jti"]`).
    pub patched_fields: Vec<String>,
}

// =============================================================================
// Inline text signature options (Task 05, PRD §4.1)
// =============================================================================

/// Options for [`super::advanced::sign_text_file`].
#[derive(Debug, Clone)]
pub struct SignTextOptions {
    /// When true (default), copy the original file to `<path>.bak` before
    /// overwriting. Disable for a non-backed-up sign (footgun — original is
    /// gone if the write fails mid-flight).
    pub backup: bool,
    /// When true, allow re-signing with the same agent over unchanged content.
    /// Default false: duplicate-signer is a no-op (file unchanged, no second
    /// block written, no error).
    pub allow_duplicate: bool,
    /// PRD §4.2.4b: override the default 0o600 backup permission. None = 0o600.
    /// Mirrors [`SignImageOptions::unsafe_bak_mode`] so both surfaces share
    /// the [`super::advanced::write_backup_or_err`] helper.
    pub unsafe_bak_mode: Option<u32>,
}

impl Default for SignTextOptions {
    fn default() -> Self {
        Self {
            backup: true,
            allow_duplicate: false,
            unsafe_bak_mode: None,
        }
    }
}

/// Outcome of [`super::advanced::sign_text_file`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignTextOutcome {
    /// The path that was signed (echoed back).
    pub path: String,
    /// 1 if a new signature block was written; 0 if the call was a duplicate-
    /// signer no-op.
    pub signers_added: u32,
    /// Path to the backup file, if one was created (`<path>.bak`). `None` if
    /// `SignTextOptions { backup: false }`.
    pub backup_path: Option<String>,
}

// =============================================================================
// Image / media signature types (Task 06, PRD §4.2)
// =============================================================================

/// Options for [`super::advanced::sign_image`].
#[derive(Debug, Clone)]
pub struct SignImageOptions {
    /// When true, additionally embed the signature payload into the visual
    /// channel via LSB steganography (PNG + JPEG only; WebP returns
    /// `Unsupported` per PRD §9). Default false (Q4 resolution).
    pub robust: bool,
    /// Optional format hint (forces a specific decode path). None = auto-detect
    /// from the input bytes.
    pub format_hint: Option<String>,
    /// When true, refuse to overwrite an existing JACS signature in the input.
    /// Default false: re-signing overwrites the prior payload.
    pub refuse_overwrite: bool,
    /// PRD §4.2.4a: automatic `.bak` backup. Default true when in_path == out_path
    /// (caller is overwriting their only copy); false otherwise.
    pub backup: bool,
    /// PRD §4.2.4b: override the default 0o600 backup permission. None = 0o600.
    pub unsafe_bak_mode: Option<u32>,
}

impl Default for SignImageOptions {
    fn default() -> Self {
        Self {
            robust: false,
            format_hint: None,
            refuse_overwrite: false,
            backup: true,
            unsafe_bak_mode: None,
        }
    }
}

/// Outcome of [`super::advanced::sign_image`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedMedia {
    /// The output path that was written.
    pub out_path: String,
    /// Agent ID that signed.
    pub signer_id: String,
    /// Detected format: `"png"` | `"jpeg"` | `"webp"`.
    pub format: String,
    /// Whether robust LSB embedding was applied.
    pub robust: bool,
    /// Path to the backup file, if one was created. None if no backup taken.
    pub backup_path: Option<String>,
}

/// Per-image verification status. Maps roughly to
/// [`crate::inline::SignatureStatus`] but with image-specific additions
/// (`UnsupportedFormat`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MediaVerifyStatus {
    Valid,
    InvalidSignature,
    HashMismatch,
    MissingSignature,
    KeyNotFound,
    UnsupportedFormat,
    Malformed(String),
}

/// Result of [`super::advanced::verify_image`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MediaVerificationResult {
    pub status: MediaVerifyStatus,
    pub signer_id: Option<String>,
    pub algorithm: Option<String>,
    pub format: Option<String>,
    /// `"metadata"` | `"metadata+lsb"` — populated when the verifier knows
    /// which channel(s) carried the payload (Valid only).
    pub embedding_channels: Option<String>,
}

/// Options for [`super::advanced::verify_image`].
#[derive(Debug, Clone, Default)]
pub struct VerifyImageOptions {
    /// Strict and key_dir, reused from inline text path.
    pub base: crate::inline::VerifyOptions,
    /// When true, scan the LSB channel if the metadata channel is absent.
    /// Default false (cost: full pixel decode).
    pub scan_robust: bool,
}

/// Options for [`super::advanced::extract_media_signature_with_options`] and
/// [`super::advanced::extract_media_signature_raw_with_options`] (R-011).
///
/// PRD §3.2 documents `extract` as the user-facing inspection verb, and PRD
/// §4.2.4 documents the `--robust` LSB scan as a verify-time fallback. R-011
/// makes both verbs honour the same opt-in: callers can ask `extract` to fall
/// back to LSB recovery when the metadata channel is empty (e.g. an image
/// passed through a metadata-stripping pipeline).
#[derive(Debug, Clone, Copy, Default)]
pub struct ExtractMediaOptions {
    /// When true, fall back to LSB scan if the metadata channel has no
    /// payload. Default false (cost: full pixel decode).
    pub scan_robust: bool,
}