converge-core 3.2.0

Converge Agent OS - correctness-first, context-driven multi-agent runtime
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
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT

//! PromotionGate - The concrete gate enforcing "agents suggest, engine decides".
//!
//! PromotionGate validates Draft proposals and promotes Validated proposals to Facts.
//! It requires a ValidationReport for promotion - there is no bypass path.
//!
//! # Key Invariants
//!
//! - **No bypass path**: `promote_to_fact()` requires `ValidatedProposal` (which contains report)
//! - **No forgery**: `ValidationReport` has private token field, can only be created by gate
//! - **Complete audit**: `PromotionRecord` captures full provenance on every Fact
//!
//! # Usage
//!
//! ```ignore
//! let gate = PromotionGate::new(GateId::new("main"), ValidationPolicy::new());
//!
//! // Validate a draft proposal
//! let validated = gate.validate_proposal(draft_proposal, &context)?;
//!
//! // Promote to fact (requires the validated proof)
//! let fact = gate.promote_to_fact(validated, approver, evidence, trace)?;
//! ```

use crate::types::{
    Actor, ContentHash, Draft, EvidenceRef, Fact, FactContent, FactContentKind, FactId, GateId,
    LocalTrace, PromotionError, PromotionRecord, Proposal, ProposalId, Timestamp, TraceLink,
    Validated, ValidationSummary,
};

use super::lifecycle::ProposalLifecycle;
use super::validation::{
    CheckResult, ValidationContext, ValidationError, ValidationPolicy, ValidationReport,
};

// ============================================================================
// ValidatedProposal - Proof bundle for promotion
// ============================================================================

/// Validated proposal bundled with its validation proof.
///
/// This type ensures the report and proposal cannot be separated
/// between validation and promotion. The only way to create this
/// is through `PromotionGate::validate_proposal()`.
pub struct ValidatedProposal {
    /// The validated proposal.
    proposal: Proposal<Validated>,
    /// The validation report (proof of validation).
    report: ValidationReport,
}

impl ValidatedProposal {
    /// Get the validated proposal.
    pub fn proposal(&self) -> &Proposal<Validated> {
        &self.proposal
    }

    /// Get the validation report.
    pub fn report(&self) -> &ValidationReport {
        &self.report
    }

    /// Get the proposal ID.
    pub fn id(&self) -> &ProposalId {
        self.proposal.id()
    }
}

// Implement Debug manually to provide useful output
impl std::fmt::Debug for ValidatedProposal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ValidatedProposal")
            .field("proposal_id", &self.proposal.id())
            .field("report", &self.report)
            .finish()
    }
}

// ============================================================================
// PromotionGate - The gate enforcing "agents suggest, engine decides"
// ============================================================================

/// The promotion gate - enforces "agents suggest, engine decides".
///
/// PromotionGate is the ONLY path to create Facts from Proposals.
/// It requires:
/// 1. A Draft proposal to validate
/// 2. A ValidationReport proving validation occurred
/// 3. Actor and evidence for the promotion record
///
/// # Invariants
///
/// - No bypass path: `promote_to_fact()` requires `ValidatedProposal`
/// - No forgery: `ValidationReport` has private token field
/// - Complete audit: `PromotionRecord` captures full provenance
///
/// # Example
///
/// ```ignore
/// let gate = PromotionGate::new(
///     GateId::new("production-gate"),
///     ValidationPolicy::new()
///         .with_required_check("schema_valid")
///         .with_required_check("confidence_threshold"),
/// );
///
/// // Step 1: Validate
/// let validated = gate.validate_proposal(draft, &context)?;
///
/// // Step 2: Promote (can't skip validation - no other constructor)
/// let fact = gate.promote_to_fact(
///     validated,
///     Actor::system("engine"),
///     vec![EvidenceRef::observation(obs_id)],
///     TraceLink::local(LocalTrace::new("trace-1", "span-1")),
/// )?;
/// ```
#[derive(Debug, Clone)]
pub struct PromotionGate {
    /// Unique identifier for this gate.
    gate_id: GateId,
    /// Validation policy.
    policy: ValidationPolicy,
}

impl PromotionGate {
    /// Create a new promotion gate.
    pub fn new(gate_id: GateId, policy: ValidationPolicy) -> Self {
        Self { gate_id, policy }
    }

    /// Get the gate ID.
    pub fn gate_id(&self) -> &GateId {
        &self.gate_id
    }

    /// Get the policy version hash.
    pub fn policy_version(&self) -> &ContentHash {
        self.policy.version_hash()
    }

    /// Validate a draft proposal.
    ///
    /// Runs validation checks and returns a `ValidatedProposal` if all pass.
    /// The returned type bundles the validated proposal with its report,
    /// ensuring they cannot be separated.
    ///
    /// # Arguments
    ///
    /// - `proposal`: The draft proposal to validate (consumed)
    /// - `context`: Validation context (tenant, session, metadata)
    ///
    /// # Returns
    ///
    /// - `Ok(ValidatedProposal)`: If validation passes
    /// - `Err(ValidationError)`: If any check fails
    pub fn validate_proposal(
        &self,
        proposal: Proposal<Draft>,
        _context: &ValidationContext,
    ) -> Result<ValidatedProposal, ValidationError> {
        // Run basic validation checks
        let mut checks = Vec::new();

        // Check: proposal has content
        if proposal.content().content.is_empty() {
            checks.push(CheckResult::failed(
                "content_not_empty",
                "Proposal content is empty",
            ));
        } else {
            checks.push(CheckResult::passed("content_not_empty"));
        }

        // Check: required checks from policy
        for required in &self.policy.required_checks {
            // For now, mark as passed - real validation would check actual conditions
            checks.push(CheckResult::passed(required.clone()));
        }

        // Check if any checks failed
        if !checks.iter().all(|c| c.passed) {
            let failed: Vec<_> = checks
                .iter()
                .filter(|c| !c.passed)
                .map(|c| c.name.clone())
                .collect();
            return Err(ValidationError::CheckFailed {
                name: failed.join(", "),
                reason: format!("Checks failed: {}", failed.join(", ")),
            });
        }

        // Create validated proposal (pub(crate) constructor)
        let validated = Proposal::<Validated>::from_validated(
            proposal.id().clone(),
            proposal.content().clone(),
            proposal.provenance().clone(),
        );

        // Create validation report (pub(crate) constructor)
        let report = ValidationReport::new(
            proposal.id().clone(),
            checks,
            self.policy.version_hash().clone(),
        );

        Ok(ValidatedProposal {
            proposal: validated,
            report,
        })
    }

    /// Promote a validated proposal to a Fact.
    ///
    /// Requires the `ValidatedProposal` (which contains the report),
    /// the approver, evidence references, and trace link.
    ///
    /// # Arguments
    ///
    /// - `validated`: The validated proposal bundle (consumed)
    /// - `approver`: Who approved this promotion
    /// - `evidence`: Evidence references supporting the promotion
    /// - `trace`: Trace link for audit/replay
    ///
    /// # Returns
    ///
    /// - `Ok(Fact)`: The promoted fact with complete audit trail
    /// - `Err(PromotionError)`: If promotion fails
    pub fn promote_to_fact(
        &self,
        validated: ValidatedProposal,
        approver: Actor,
        evidence: Vec<EvidenceRef>,
        trace: TraceLink,
    ) -> Result<Fact, PromotionError> {
        let ValidatedProposal { proposal, report } = validated;

        // Verify report matches proposal
        if report.proposal_id() != proposal.id() {
            return Err(PromotionError::report_mismatch(
                proposal.id(),
                report.proposal_id(),
            ));
        }

        // Build validation summary from report
        let mut summary = ValidationSummary::new();
        for check in report.checks() {
            if check.passed {
                summary = summary.with_passed(&check.name);
            }
        }

        // Build promotion record
        let record = PromotionRecord::new(
            self.gate_id.clone(),
            report.policy_version().clone(),
            approver,
            summary,
            evidence,
            trace,
            Timestamp::now(),
        );

        // Create fact content from proposal content
        let fact_content = FactContent::new(
            FactContentKind::from(proposal.content().kind),
            proposal.content().content.clone(),
        );

        // Create fact (pub(crate) constructor)
        let fact = Fact::new(
            FactId::new(format!("fact:{}", proposal.id())),
            fact_content,
            record,
            Timestamp::now(),
        );

        Ok(fact)
    }
}

// ============================================================================
// SimpleIntent - Basic intent for ProposalLifecycle implementation
// ============================================================================

/// Simple intent for basic validation.
///
/// This is a minimal intent type for demonstrating the ProposalLifecycle
/// trait implementation. Real intents would have more structure.
#[derive(Debug, Clone)]
pub struct SimpleIntent {
    /// Description of what we're trying to achieve.
    pub description: String,
}

impl SimpleIntent {
    /// Create a new simple intent.
    pub fn new(description: impl Into<String>) -> Self {
        Self {
            description: description.into(),
        }
    }
}

// ============================================================================
// ProposalLifecycle implementation for PromotionGate
// ============================================================================

impl ProposalLifecycle<SimpleIntent, Proposal<Draft>, ValidatedProposal, Fact> for PromotionGate {
    fn validate(
        &self,
        _intent: &SimpleIntent,
        proposal: Proposal<Draft>,
    ) -> Result<ValidatedProposal, ValidationError> {
        self.validate_proposal(proposal, &ValidationContext::default())
    }

    fn promote(&self, validated: ValidatedProposal) -> Result<Fact, PromotionError> {
        // Use system actor for trait-based promotion
        let approver = Actor::system("promotion-gate");
        let evidence = vec![];
        let trace = TraceLink::local(LocalTrace::new("auto-promote", "gate-promote"));
        self.promote_to_fact(validated, approver, evidence, trace)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{
        CaptureContext, ContentHash, ObservationId, ObservationProvenance, ProposedContent,
        ProposedContentKind,
    };

    fn make_draft_proposal(id: &str, content: &str) -> Proposal<Draft> {
        Proposal::new(
            ProposalId::new(id),
            ProposedContent::new(ProposedContentKind::Claim, content),
            ObservationProvenance::new(
                ObservationId::new("obs-001"),
                ContentHash::zero(),
                CaptureContext::default(),
            ),
        )
    }

    #[test]
    fn gate_creation() {
        let gate = PromotionGate::new(
            GateId::new("test-gate"),
            ValidationPolicy::new().with_required_check("schema_valid"),
        );

        assert_eq!(gate.gate_id().as_str(), "test-gate");
    }

    #[test]
    fn successful_validation() {
        let gate = PromotionGate::new(GateId::new("test-gate"), ValidationPolicy::new());
        let proposal = make_draft_proposal("prop-001", "This is a valid claim");
        let context = ValidationContext::new();

        let validated = gate.validate_proposal(proposal, &context).unwrap();

        assert_eq!(validated.id().as_str(), "prop-001");
        assert!(validated.report().all_passed());
    }

    #[test]
    fn failed_validation_empty_content() {
        let gate = PromotionGate::new(GateId::new("test-gate"), ValidationPolicy::new());
        let proposal = make_draft_proposal("prop-002", ""); // Empty content
        let context = ValidationContext::new();

        let result = gate.validate_proposal(proposal, &context);

        assert!(result.is_err());
        match result {
            Err(ValidationError::CheckFailed { name, .. }) => {
                assert!(name.contains("content_not_empty"));
            }
            _ => panic!("expected CheckFailed error"),
        }
    }

    #[test]
    fn successful_promotion() {
        let gate = PromotionGate::new(GateId::new("test-gate"), ValidationPolicy::new());
        let proposal = make_draft_proposal("prop-003", "Valid claim");
        let context = ValidationContext::new();

        let validated = gate.validate_proposal(proposal, &context).unwrap();

        let fact = gate
            .promote_to_fact(
                validated,
                Actor::system("test-engine"),
                vec![EvidenceRef::observation(ObservationId::new("obs-001"))],
                TraceLink::local(LocalTrace::new("trace-001", "span-001")),
            )
            .unwrap();

        assert_eq!(fact.id().as_str(), "fact:prop-003");
        assert_eq!(fact.content().content, "Valid claim");
        assert_eq!(fact.promotion_record().gate_id.as_str(), "test-gate");
        assert!(fact.is_replay_eligible());
    }

    #[test]
    fn proposal_lifecycle_trait() {
        let gate = PromotionGate::new(GateId::new("lifecycle-gate"), ValidationPolicy::new());
        let intent = SimpleIntent::new("test intent");
        let proposal = make_draft_proposal("prop-004", "Lifecycle test");

        // Use trait methods
        let validated = gate.validate(&intent, proposal).unwrap();
        let fact = gate.promote(validated).unwrap();

        assert_eq!(fact.id().as_str(), "fact:prop-004");
        assert_eq!(fact.content().content, "Lifecycle test");
    }

    #[test]
    fn policy_required_checks_run() {
        let gate = PromotionGate::new(
            GateId::new("policy-gate"),
            ValidationPolicy::new()
                .with_required_check("schema_valid")
                .with_required_check("confidence_threshold"),
        );
        let proposal = make_draft_proposal("prop-005", "Policy test");
        let context = ValidationContext::new();

        let validated = gate.validate_proposal(proposal, &context).unwrap();

        // All required checks should be in the report (passed for now)
        let check_names: Vec<_> = validated
            .report()
            .checks()
            .iter()
            .map(|c| c.name.as_str())
            .collect();
        assert!(check_names.contains(&"schema_valid"));
        assert!(check_names.contains(&"confidence_threshold"));
        assert!(check_names.contains(&"content_not_empty"));
    }

    #[test]
    fn validated_proposal_debug() {
        let gate = PromotionGate::new(GateId::new("debug-gate"), ValidationPolicy::new());
        let proposal = make_draft_proposal("prop-006", "Debug test");
        let context = ValidationContext::new();

        let validated = gate.validate_proposal(proposal, &context).unwrap();
        let debug = format!("{:?}", validated);

        assert!(debug.contains("ValidatedProposal"));
        assert!(debug.contains("prop-006"));
    }

    #[test]
    fn fact_content_kind_conversion() {
        // Verify the ProposedContentKind -> FactContentKind conversion
        use crate::types::{FactContentKind, ProposedContentKind};

        assert_eq!(
            FactContentKind::from(ProposedContentKind::Claim),
            FactContentKind::Claim
        );
        assert_eq!(
            FactContentKind::from(ProposedContentKind::Plan),
            FactContentKind::Plan
        );
        assert_eq!(
            FactContentKind::from(ProposedContentKind::Draft),
            FactContentKind::Document
        );
        assert_eq!(
            FactContentKind::from(ProposedContentKind::Reasoning),
            FactContentKind::Reasoning
        );
    }

    // Note: External code cannot construct ValidatedProposal directly.
    // The only way is through PromotionGate::validate_proposal().
    //
    // // In external crate:
    // let validated = ValidatedProposal { ... };
    // // ERROR: `proposal` and `report` fields are private
}