converge-core 2.1.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
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT

//! Converge Prompt DSL — Compact machine-to-machine contract format.
//!
//! This module provides EDN-like serialization for agent prompts,
//! optimized for token efficiency and deterministic parsing.
//!
//! # Philosophy
//!
//! Agent prompts are **machine-to-machine contracts**, not human UX.
//! They prioritize:
//! - Token efficiency (50-60% savings vs Markdown)
//! - Structural clarity
//! - Deterministic parsing
//! - Zero fluff
//!
//! Human explanations are generated downstream from provenance + context.

use crate::context::{Context, ContextKey, Fact};
use std::collections::HashSet;
use std::fmt::Write;

/// Prompt format for agent prompts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PromptFormat {
    /// Plain text (backward compatible, human-readable).
    Plain,
    /// EDN-like compact format (default, token-efficient).
    #[default]
    Edn,
}

/// Agent role in the prompt contract.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentRole {
    /// Proposes new facts (LLM agents).
    Proposer,
    /// Validates proposals (deterministic agents).
    Validator,
    /// Synthesizes existing facts.
    Synthesizer,
    /// Analyzes and evaluates.
    Analyzer,
}

impl AgentRole {
    /// Converts to compact keyword string.
    fn to_keyword(self) -> &'static str {
        match self {
            Self::Proposer => ":proposer",
            Self::Validator => ":validator",
            Self::Synthesizer => ":synthesizer",
            Self::Analyzer => ":analyzer",
        }
    }
}

/// Constraint keywords for prompts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Constraint {
    /// Do not invent facts not in context.
    NoInvent,
    /// Do not contradict existing facts.
    NoContradict,
    /// Do not hallucinate.
    NoHallucinate,
    /// Cite sources when possible.
    CiteSources,
}

impl Constraint {
    /// Converts to compact keyword string.
    fn to_keyword(self) -> &'static str {
        match self {
            Self::NoInvent => ":no-invent",
            Self::NoContradict => ":no-contradict",
            Self::NoHallucinate => ":no-hallucinate",
            Self::CiteSources => ":cite-sources",
        }
    }
}

/// Output contract for the prompt.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutputContract {
    /// What to emit (e.g., :proposed-fact, :fact, :analysis).
    pub emit: String,
    /// Target context key.
    pub key: ContextKey,
    /// Output format (e.g., :edn, :json, :xml).
    pub format: Option<String>,
}

impl OutputContract {
    /// Creates a new output contract.
    #[must_use]
    pub fn new(emit: impl Into<String>, key: ContextKey) -> Self {
        Self {
            emit: emit.into(),
            key,
            format: None,
        }
    }

    /// Sets the output format.
    #[must_use]
    pub fn with_format(mut self, format: impl Into<String>) -> Self {
        self.format = Some(format.into());
        self
    }
}

/// Compact agent prompt contract.
///
/// This is the canonical internal representation that gets serialized
/// to EDN-like format for LLM consumption.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AgentPrompt {
    /// Agent role.
    pub role: AgentRole,
    /// Objective (what the agent should do).
    pub objective: String,
    /// Context data (facts from dependencies).
    pub context: PromptContext,
    /// Constraints (keywords).
    pub constraints: HashSet<Constraint>,
    /// Output contract.
    pub output_contract: OutputContract,
}

/// Context data extracted from Context for the prompt.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PromptContext {
    /// Facts grouped by `ContextKey`.
    pub facts: Vec<(ContextKey, Vec<Fact>)>,
}

impl PromptContext {
    /// Creates an empty context.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds facts for a given key.
    pub fn add_facts(&mut self, key: ContextKey, facts: Vec<Fact>) {
        if !facts.is_empty() {
            self.facts.push((key, facts));
        }
    }

    /// Builds context from a Context and dependency keys.
    #[must_use]
    pub fn from_context(ctx: &Context, dependencies: &[ContextKey]) -> Self {
        let mut prompt_ctx = Self::new();
        for &key in dependencies {
            let facts = ctx.get(key).to_vec();
            prompt_ctx.add_facts(key, facts);
        }
        prompt_ctx
    }
}

/// Converts `ContextKey` to compact keyword string.
fn context_key_to_keyword(key: ContextKey) -> &'static str {
    match key {
        ContextKey::Seeds => ":seeds",
        ContextKey::Hypotheses => ":hypotheses",
        ContextKey::Strategies => ":strategies",
        ContextKey::Constraints => ":constraints",
        ContextKey::Signals => ":signals",
        ContextKey::Competitors => ":competitors",
        ContextKey::Evaluations => ":evaluations",
        ContextKey::Proposals => ":proposals",
        ContextKey::Diagnostic => ":diagnostic",
    }
}

impl AgentPrompt {
    /// Creates a new agent prompt.
    #[must_use]
    pub fn new(
        role: AgentRole,
        objective: impl Into<String>,
        context: PromptContext,
        output_contract: OutputContract,
    ) -> Self {
        Self {
            role,
            objective: objective.into(),
            context,
            constraints: HashSet::new(),
            output_contract,
        }
    }

    /// Adds a constraint.
    #[must_use]
    pub fn with_constraint(mut self, constraint: Constraint) -> Self {
        self.constraints.insert(constraint);
        self
    }

    /// Adds multiple constraints.
    #[must_use]
    pub fn with_constraints(mut self, constraints: impl IntoIterator<Item = Constraint>) -> Self {
        self.constraints.extend(constraints);
        self
    }

    /// Serializes to EDN-like compact format.
    ///
    /// Format:
    /// ```edn
    /// {:r :proposer
    ///  :o :extract-competitors
    ///  :c {:signals [{:id "s1" :c "..."}]}
    ///  :k #{:no-invent :no-contradict}
    ///  :out {:emit :proposed-fact :key :competitors}}
    /// ```
    #[must_use]
    pub fn to_edn(&self) -> String {
        let mut s = String::new();
        s.push_str("{:r ");
        s.push_str(self.role.to_keyword());
        s.push_str("\n :o :");
        // Escape objective if needed (simplified: assume no special chars)
        s.push_str(&self.objective.replace(' ', "-"));
        s.push_str("\n :c {");

        // Serialize context facts
        let mut first_key = true;
        for (key, facts) in &self.context.facts {
            if !first_key {
                s.push(' ');
            }
            first_key = false;
            s.push_str(context_key_to_keyword(*key));
            s.push_str(" [{");
            for (i, fact) in facts.iter().enumerate() {
                if i > 0 {
                    s.push_str("} {");
                }
                s.push_str(":id \"");
                s.push_str(&escape_string(&fact.id));
                s.push_str("\" :c \"");
                s.push_str(&escape_string(&fact.content));
                s.push('"');
            }
            s.push_str("}]");
        }

        s.push_str("}\n :k #{");

        // Serialize constraints
        let mut constraints: Vec<_> = self.constraints.iter().collect();
        constraints.sort(); // Deterministic ordering
        for (i, constraint) in constraints.iter().enumerate() {
            if i > 0 {
                s.push(' ');
            }
            s.push_str(constraint.to_keyword());
        }

        s.push_str("}\n :out {:emit :");
        s.push_str(&self.output_contract.emit);
        s.push_str(" :key ");
        s.push_str(context_key_to_keyword(self.output_contract.key));
        if let Some(ref format) = self.output_contract.format {
            s.push_str(" :format :");
            s.push_str(format);
        }
        s.push_str("}}");

        s
    }

    /// Serializes to plain text format (backward compatible).
    #[must_use]
    pub fn to_plain(&self) -> String {
        let mut s = String::new();
        writeln!(s, "Role: {:?}", self.role).unwrap();
        writeln!(s, "Objective: {}", self.objective).unwrap();
        writeln!(s, "\nContext:").unwrap();

        for (key, facts) in &self.context.facts {
            writeln!(s, "\n## {key:?}").unwrap();
            for fact in facts {
                writeln!(s, "- {}: {}", fact.id, fact.content).unwrap();
            }
        }

        if !self.constraints.is_empty() {
            writeln!(s, "\nConstraints:").unwrap();
            for constraint in &self.constraints {
                writeln!(s, "- {constraint:?}").unwrap();
            }
        }

        writeln!(
            s,
            "\nOutput: {:?} -> {:?}",
            self.output_contract.emit, self.output_contract.key
        )
        .unwrap();

        s
    }

    /// Serializes based on format.
    #[must_use]
    pub fn serialize(&self, format: PromptFormat) -> String {
        match format {
            PromptFormat::Edn => self.to_edn(),
            PromptFormat::Plain => self.to_plain(),
        }
    }
}

/// Escapes special characters in strings for EDN.
fn escape_string(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::{Context, Fact};

    #[test]
    fn test_edn_serialization() {
        let mut ctx = PromptContext::new();
        ctx.add_facts(
            ContextKey::Signals,
            vec![
                Fact {
                    key: ContextKey::Signals,
                    id: "s1".to_string(),
                    content: "Revenue +15% Q3".to_string(),
                },
                Fact {
                    key: ContextKey::Signals,
                    id: "s2".to_string(),
                    content: "Market $2.3B".to_string(),
                },
            ],
        );

        let prompt = AgentPrompt::new(
            AgentRole::Proposer,
            "extract-competitors",
            ctx,
            OutputContract::new("proposed-fact", ContextKey::Competitors),
        )
        .with_constraint(Constraint::NoInvent)
        .with_constraint(Constraint::NoContradict);

        let edn = prompt.to_edn();
        assert!(edn.contains(":r :proposer"));
        assert!(edn.contains(":o :extract-competitors"));
        assert!(edn.contains(":signals"));
        assert!(edn.contains(":no-invent"));
        assert!(edn.contains(":no-contradict"));
        assert!(edn.contains(":competitors"));
    }

    #[test]
    fn test_context_building() {
        let mut context = Context::new();
        context
            .add_fact(Fact {
                key: ContextKey::Seeds,
                id: "seed1".to_string(),
                content: "Test seed".to_string(),
            })
            .unwrap();

        let prompt_ctx = PromptContext::from_context(&context, &[ContextKey::Seeds]);
        assert_eq!(prompt_ctx.facts.len(), 1);
        assert_eq!(prompt_ctx.facts[0].0, ContextKey::Seeds);
        assert_eq!(prompt_ctx.facts[0].1.len(), 1);
    }

    #[test]
    fn test_escape_string() {
        assert_eq!(escape_string("hello"), "hello");
        assert_eq!(escape_string("hello\"world"), "hello\\\"world");
        assert_eq!(escape_string("hello\nworld"), "hello\\nworld");
    }

    #[test]
    fn test_token_efficiency() {
        let mut ctx = PromptContext::new();
        ctx.add_facts(
            ContextKey::Signals,
            vec![Fact {
                key: ContextKey::Signals,
                id: "s1".to_string(),
                content: "Revenue +15% Q3".to_string(),
            }],
        );

        let prompt = AgentPrompt::new(
            AgentRole::Proposer,
            "analyze",
            ctx,
            OutputContract::new("proposed-fact", ContextKey::Strategies),
        );

        let edn = prompt.to_edn();
        let plain = prompt.to_plain();

        println!("EDN length: {}", edn.len());
        println!("Plain length: {}", plain.len());
        println!("EDN:\n{edn}");
        println!("Plain:\n{plain}");

        // For small prompts, EDN overhead may exceed plain text.
        // The efficiency gain comes from larger contexts where structural
        // overhead is amortized. This test verifies the format works correctly.
        // Token efficiency is verified in integration tests with real contexts.
        assert!(!edn.is_empty());
        assert!(!plain.is_empty());
    }
}