symbi-runtime 1.7.0

Agent Runtime System for the Symbi platform
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
//! Cryptographic audit for director-critic exchanges
//!
//! Provides hash-chained, Ed25519-signed audit entries for every
//! director-critic interaction, enabling tamper-evident review trails.

use chrono::{DateTime, Utc};
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;

/// Identity of who produced an artifact in the audit chain.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AuditIdentity {
    /// An LLM model acted as critic.
    #[serde(rename = "llm")]
    Llm { model_id: String },
    /// A human acted as critic.
    #[serde(rename = "human")]
    Human { user_id: String, name: String },
}

/// Verdict of a critic evaluation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AuditVerdict {
    Approved,
    Rejected,
    NeedsRevision,
}

/// A single auditable director-critic exchange.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CriticAuditEntry {
    /// Unique ID for this entry.
    pub entry_id: String,
    /// SHA-256 hash of the director's output.
    pub director_output_hash: String,
    /// SHA-256 hash of the critic's assessment.
    pub critic_assessment_hash: String,
    /// The critic's verdict.
    pub verdict: AuditVerdict,
    /// Per-dimension scores (for rubric evaluations).
    pub dimension_scores: HashMap<String, f64>,
    /// Overall score (0.0 - 1.0).
    pub score: f64,
    /// Identity of the critic.
    pub critic_identity: AuditIdentity,
    /// Timestamp of the exchange.
    pub timestamp: DateTime<Utc>,
    /// Chain hash: SHA-256(previous_chain_hash || entry_data).
    pub chain_hash: String,
    /// Ed25519 signature over the chain hash (hex-encoded).
    pub signature: String,
    /// Iteration number in the director-critic loop.
    pub iteration: u32,
}

/// Parameters for recording a director-critic exchange.
pub struct RecordParams<'a> {
    /// The director's output text.
    pub director_output: &'a str,
    /// The critic's assessment text.
    pub critic_assessment: &'a str,
    /// The critic's verdict.
    pub verdict: AuditVerdict,
    /// Per-dimension scores (for rubric evaluations).
    pub dimension_scores: HashMap<String, f64>,
    /// Overall score (0.0 - 1.0).
    pub score: f64,
    /// Identity of the critic.
    pub critic_identity: AuditIdentity,
    /// Iteration number in the director-critic loop.
    pub iteration: u32,
}

/// Maintains a hash-chained, Ed25519-signed audit trail for director-critic exchanges.
pub struct AuditChain {
    entries: Vec<CriticAuditEntry>,
    signing_key: SigningKey,
    last_chain_hash: String,
}

impl AuditChain {
    /// Create a new audit chain with the given signing key.
    pub fn new(signing_key: SigningKey) -> Self {
        let genesis = sha256_hex(b"genesis");
        Self {
            entries: Vec::new(),
            signing_key,
            last_chain_hash: genesis,
        }
    }

    /// Record a director-critic exchange and append it to the chain.
    pub fn record(&mut self, params: RecordParams<'_>) -> CriticAuditEntry {
        let entry_id = uuid::Uuid::new_v4().to_string();
        let director_output_hash = sha256_hex(params.director_output.as_bytes());
        let critic_assessment_hash = sha256_hex(params.critic_assessment.as_bytes());
        let timestamp = Utc::now();

        // Compute chain hash: SHA-256(previous_chain_hash || entry_data)
        let entry_data = format!(
            "{}|{}|{}|{:?}|{}|{}|{}",
            entry_id,
            director_output_hash,
            critic_assessment_hash,
            params.verdict,
            params.score,
            timestamp.to_rfc3339(),
            params.iteration
        );
        let chain_input = format!("{}{}", self.last_chain_hash, entry_data);
        let chain_hash = sha256_hex(chain_input.as_bytes());

        // Sign the chain hash with Ed25519
        let signature_bytes = self.signing_key.sign(chain_hash.as_bytes());
        let signature = hex::encode(signature_bytes.to_bytes());

        let entry = CriticAuditEntry {
            entry_id,
            director_output_hash,
            critic_assessment_hash,
            verdict: params.verdict,
            dimension_scores: params.dimension_scores,
            score: params.score,
            critic_identity: params.critic_identity,
            timestamp,
            chain_hash: chain_hash.clone(),
            signature,
            iteration: params.iteration,
        };

        self.last_chain_hash = chain_hash;
        self.entries.push(entry.clone());
        entry
    }

    /// Get all entries in the chain.
    pub fn entries(&self) -> &[CriticAuditEntry] {
        &self.entries
    }

    /// Get the verifying (public) key for this chain.
    pub fn verifying_key(&self) -> VerifyingKey {
        self.signing_key.verifying_key()
    }

    /// Verify the integrity of the entire chain (hashes + signatures).
    pub fn verify(&self, verifying_key: &VerifyingKey) -> Result<(), AuditError> {
        verify_chain(&self.entries, verifying_key)
    }

    /// Get the number of entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if the chain is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

/// Verify a chain of audit entries against a verifying key.
///
/// Checks both hash chain integrity and Ed25519 signatures on every entry.
pub fn verify_chain(
    entries: &[CriticAuditEntry],
    verifying_key: &VerifyingKey,
) -> Result<(), AuditError> {
    let mut expected_prev_hash = sha256_hex(b"genesis");

    for (i, entry) in entries.iter().enumerate() {
        // Recompute chain hash from entry data
        let entry_data = format!(
            "{}|{}|{}|{:?}|{}|{}|{}",
            entry.entry_id,
            entry.director_output_hash,
            entry.critic_assessment_hash,
            entry.verdict,
            entry.score,
            entry.timestamp.to_rfc3339(),
            entry.iteration
        );
        let chain_input = format!("{}{}", expected_prev_hash, entry_data);
        let expected_chain_hash = sha256_hex(chain_input.as_bytes());

        if entry.chain_hash != expected_chain_hash {
            return Err(AuditError::ChainIntegrity {
                entry_index: i,
                expected: expected_chain_hash,
                found: entry.chain_hash.clone(),
            });
        }

        // Verify Ed25519 signature
        let sig_bytes =
            hex::decode(&entry.signature).map_err(|e| AuditError::InvalidSignature {
                entry_index: i,
                message: format!("hex decode failed: {}", e),
            })?;

        let sig_array: [u8; 64] =
            sig_bytes
                .as_slice()
                .try_into()
                .map_err(|_| AuditError::InvalidSignature {
                    entry_index: i,
                    message: "signature must be 64 bytes".into(),
                })?;

        let signature = Signature::from_bytes(&sig_array);

        verifying_key
            .verify(entry.chain_hash.as_bytes(), &signature)
            .map_err(|e| AuditError::InvalidSignature {
                entry_index: i,
                message: e.to_string(),
            })?;

        expected_prev_hash = entry.chain_hash.clone();
    }

    Ok(())
}

/// Compute SHA-256 and return hex-encoded string.
fn sha256_hex(data: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(data);
    hex::encode(hasher.finalize())
}

/// Errors from the audit system.
#[derive(Debug, thiserror::Error)]
pub enum AuditError {
    #[error(
        "Chain integrity violation at entry {entry_index}: expected {expected}, found {found}"
    )]
    ChainIntegrity {
        entry_index: usize,
        expected: String,
        found: String,
    },

    #[error("Invalid signature at entry {entry_index}: {message}")]
    InvalidSignature { entry_index: usize, message: String },
}

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

    fn test_signing_key() -> SigningKey {
        use rand::RngCore;
        let mut secret = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut secret);
        SigningKey::from_bytes(&secret)
    }

    #[test]
    fn test_record_and_verify() {
        let key = test_signing_key();
        let mut chain = AuditChain::new(key);

        chain.record(RecordParams {
            director_output: "The analysis shows...",
            critic_assessment: "Good analysis, approved.",
            verdict: AuditVerdict::Approved,
            dimension_scores: HashMap::new(),
            score: 0.9,
            critic_identity: AuditIdentity::Llm {
                model_id: "claude-sonnet".into(),
            },
            iteration: 1,
        });

        assert_eq!(chain.len(), 1);
        assert!(chain.verify(&chain.verifying_key()).is_ok());
    }

    #[test]
    fn test_multi_entry_chain() {
        let key = test_signing_key();
        let mut chain = AuditChain::new(key);

        for i in 0..5 {
            chain.record(RecordParams {
                director_output: &format!("Director output {}", i),
                critic_assessment: &format!("Critic review {}", i),
                verdict: if i < 4 {
                    AuditVerdict::NeedsRevision
                } else {
                    AuditVerdict::Approved
                },
                dimension_scores: {
                    let mut scores = HashMap::new();
                    scores.insert("accuracy".into(), 0.5 + (i as f64) * 0.1);
                    scores
                },
                score: 0.5 + (i as f64) * 0.1,
                critic_identity: AuditIdentity::Llm {
                    model_id: "claude-sonnet".into(),
                },
                iteration: i as u32 + 1,
            });
        }

        assert_eq!(chain.len(), 5);
        assert!(chain.verify(&chain.verifying_key()).is_ok());
    }

    #[test]
    fn test_tampered_chain_hash_detected() {
        let key = test_signing_key();
        let verifying_key = key.verifying_key();
        let mut chain = AuditChain::new(key);

        chain.record(RecordParams {
            director_output: "output 1",
            critic_assessment: "review 1",
            verdict: AuditVerdict::Approved,
            dimension_scores: HashMap::new(),
            score: 0.8,
            critic_identity: AuditIdentity::Llm {
                model_id: "test".into(),
            },
            iteration: 1,
        });
        chain.record(RecordParams {
            director_output: "output 2",
            critic_assessment: "review 2",
            verdict: AuditVerdict::Approved,
            dimension_scores: HashMap::new(),
            score: 0.9,
            critic_identity: AuditIdentity::Llm {
                model_id: "test".into(),
            },
            iteration: 2,
        });

        // Tamper with first entry's chain hash
        let mut tampered = chain.entries().to_vec();
        tampered[0].chain_hash = sha256_hex(b"tampered");

        let result = verify_chain(&tampered, &verifying_key);
        assert!(result.is_err());
        match result.unwrap_err() {
            AuditError::ChainIntegrity { entry_index, .. } => assert_eq!(entry_index, 0),
            other => panic!("Expected ChainIntegrity, got {:?}", other),
        }
    }

    #[test]
    fn test_wrong_key_rejected() {
        let key = test_signing_key();
        let wrong_key = test_signing_key();
        let mut chain = AuditChain::new(key);

        chain.record(RecordParams {
            director_output: "output",
            critic_assessment: "review",
            verdict: AuditVerdict::Approved,
            dimension_scores: HashMap::new(),
            score: 0.9,
            critic_identity: AuditIdentity::Human {
                user_id: "user-1".into(),
                name: "Alice".into(),
            },
            iteration: 1,
        });

        let result = verify_chain(chain.entries(), &wrong_key.verifying_key());
        assert!(result.is_err());
        match result.unwrap_err() {
            AuditError::InvalidSignature { entry_index, .. } => assert_eq!(entry_index, 0),
            other => panic!("Expected InvalidSignature, got {:?}", other),
        }
    }

    #[test]
    fn test_entry_serialization() {
        let key = test_signing_key();
        let mut chain = AuditChain::new(key);

        let entry = chain.record(RecordParams {
            director_output: "test output",
            critic_assessment: "test review",
            verdict: AuditVerdict::NeedsRevision,
            dimension_scores: {
                let mut m = HashMap::new();
                m.insert("accuracy".into(), 0.7);
                m.insert("completeness".into(), 0.8);
                m
            },
            score: 0.75,
            critic_identity: AuditIdentity::Llm {
                model_id: "claude-sonnet".into(),
            },
            iteration: 1,
        });

        let json = serde_json::to_string(&entry).unwrap();
        let restored: CriticAuditEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.entry_id, entry.entry_id);
        assert_eq!(restored.verdict, AuditVerdict::NeedsRevision);
        assert_eq!(restored.dimension_scores.len(), 2);
    }

    #[test]
    fn test_empty_chain_verifies() {
        let key = test_signing_key();
        let chain = AuditChain::new(key);
        assert!(chain.is_empty());
        assert!(chain.verify(&chain.verifying_key()).is_ok());
    }

    #[test]
    fn test_sha256_deterministic() {
        let hash1 = sha256_hex(b"hello world");
        let hash2 = sha256_hex(b"hello world");
        assert_eq!(hash1, hash2);
        assert_ne!(hash1, sha256_hex(b"different input"));
    }

    #[test]
    fn test_chain_order_matters() {
        let key = test_signing_key();
        let verifying_key = key.verifying_key();
        let mut chain = AuditChain::new(key);

        chain.record(RecordParams {
            director_output: "first",
            critic_assessment: "review first",
            verdict: AuditVerdict::NeedsRevision,
            dimension_scores: HashMap::new(),
            score: 0.5,
            critic_identity: AuditIdentity::Llm {
                model_id: "test".into(),
            },
            iteration: 1,
        });
        chain.record(RecordParams {
            director_output: "second",
            critic_assessment: "review second",
            verdict: AuditVerdict::Approved,
            dimension_scores: HashMap::new(),
            score: 0.9,
            critic_identity: AuditIdentity::Llm {
                model_id: "test".into(),
            },
            iteration: 2,
        });

        // Swap entries — should fail chain verification
        let mut swapped = chain.entries().to_vec();
        swapped.swap(0, 1);

        let result = verify_chain(&swapped, &verifying_key);
        assert!(result.is_err());
    }
}