oracle_omen_core 0.1.0

Core types and abstractions for oracle.omen deterministic agent framework
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
//! State machine types for agents.
//!
//! Agents are pure state machines:
//! Input: prior state, observation, tool responses, injected deterministic context
//! Output: next state, planned actions, patch proposals

use std::collections::BTreeMap;
use std::fmt;
use std::string::String;
use std::vec::Vec;

use crate::hash::Hash;

/// Agent state - typed container for agent data
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AgentState {
    /// State version - increments on each transition
    pub version: u64,

    /// State data keyed by domain
    pub data: BTreeMap<String, StateData>,

    /// Hash of this state
    pub state_hash: Hash,
}

impl AgentState {
    /// Create initial empty state
    #[must_use]
    pub fn initial() -> Self {
        Self {
            version: 0,
            data: BTreeMap::new(),
            state_hash: Hash::zero(),
        }
    }

    /// Create state with run ID
    #[must_use]
    pub fn with_run_id(run_id: u64) -> Self {
        let mut state = Self::initial();
        state.data.insert(
            "system".to_string(),
            StateData::Value(StateValue::U64(run_id)),
        );
        state.rehash();
        state
    }

    /// Get state data for a domain
    #[must_use]
    pub fn get(&self, domain: &str) -> Option<&StateData> {
        self.data.get(domain)
    }

    /// Set state data for a domain
    pub fn set(&mut self, domain: impl Into<String>, data: StateData) {
        self.data.insert(domain.into(), data);
        self.version += 1;
        self.rehash();
    }

    /// Remove state data for a domain
    pub fn remove(&mut self, domain: &str) -> Option<StateData> {
        let result = self.data.remove(domain);
        if result.is_some() {
            self.version += 1;
            self.rehash();
        }
        result
    }

    /// Compute and store the state hash
    fn rehash(&mut self) {
        self.state_hash = Hash::from_canonical(&self.data);
    }

    /// Check if this state matches another by hash
    #[must_use]
    pub fn matches(&self, other: &Self) -> bool {
        self.state_hash == other.state_hash
    }

    /// Get the state hash
    #[must_use]
    pub const fn hash(&self) -> Hash {
        self.state_hash
    }

    /// Get the version
    #[must_use]
    pub const fn version(&self) -> u64 {
        self.version
    }
}

impl Default for AgentState {
    fn default() -> Self {
        Self::initial()
    }
}

/// State data - typed values for agent state
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum StateData {
    /// Single value
    Value(StateValue),

    /// Map of values
    Map(BTreeMap<String, StateValue>),

    /// List of values
    Vec(Vec<StateValue>),
}

/// Typed state value
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum StateValue {
    /// String value
    String(String),

    /// Boolean value
    Bool(bool),

    /// Unsigned 64-bit integer
    U64(u64),

    /// Signed 64-bit integer
    I64(i64),

    /// Bytes
    Bytes(Vec<u8>),

    /// Hash reference
    Hash(Hash),

    /// Null/none
    None,
}

impl StateValue {
    /// Get type name
    #[must_use]
    pub fn type_name(&self) -> &str {
        match self {
            StateValue::String(_) => "string",
            StateValue::Bool(_) => "bool",
            StateValue::U64(_) => "u64",
            StateValue::I64(_) => "i64",
            StateValue::Bytes(_) => "bytes",
            StateValue::Hash(_) => "hash",
            StateValue::None => "none",
        }
    }
}

impl From<String> for StateValue {
    fn from(s: String) -> Self {
        Self::String(s)
    }
}

impl From<&str> for StateValue {
    fn from(s: &str) -> Self {
        Self::String(s.to_string())
    }
}

impl From<bool> for StateValue {
    fn from(b: bool) -> Self {
        Self::Bool(b)
    }
}

impl From<u64> for StateValue {
    fn from(n: u64) -> Self {
        Self::U64(n)
    }
}

impl From<i64> for StateValue {
    fn from(n: i64) -> Self {
        Self::I64(n)
    }
}

impl From<Hash> for StateValue {
    fn from(h: Hash) -> Self {
        Self::Hash(h)
    }
}

/// State transition result
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct StateTransition {
    /// Previous state hash
    pub from_hash: Hash,

    /// New state
    pub to_state: AgentState,

    /// Event that caused the transition
    pub event_hash: Hash,

    /// Transition hash
    pub transition_hash: Hash,
}

impl StateTransition {
    /// Create a new state transition
    #[must_use]
    pub fn new(from_hash: Hash, to_state: AgentState, event_hash: Hash) -> Self {
        let transition_hash = crate::hash::transition_hash(from_hash, event_hash, to_state.hash());
        Self {
            from_hash,
            to_state,
            event_hash,
            transition_hash,
        }
    }

    /// Get the transition hash
    #[must_use]
    pub const fn hash(&self) -> Hash {
        self.transition_hash
    }
}

/// Observation - input from environment
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Observation {
    /// Observation kind/type
    pub kind: String,

    /// Observation data
    pub data: BTreeMap<String, StateValue>,

    /// Observation timestamp (logical)
    pub logical_time: u64,
}

impl Observation {
    /// Create a new observation
    #[must_use]
    pub fn new(kind: impl Into<String>, logical_time: u64) -> Self {
        Self {
            kind: kind.into(),
            data: BTreeMap::new(),
            logical_time,
        }
    }

    /// Add data field
    pub fn with_field(mut self, key: impl Into<String>, value: StateValue) -> Self {
        self.data.insert(key.into(), value);
        self
    }
}

/// Decision - output from agent
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Decision {
    /// No action - observe only
    None,

    /// Request tool execution
    ToolCall(ToolCall),

    /// Propose a self-patch
    PatchProposal(PatchProposal),

    /// Multiple actions
    Multiple(Vec<Decision>),
}

/// Tool call decision
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ToolCall {
    /// Tool to call
    pub tool_name: String,

    /// Tool version
    pub tool_version: String,

    /// Input payload (JSON)
    pub input: String,

    /// Required capabilities
    pub capabilities: Vec<String>,
}

impl ToolCall {
    /// Create a new tool call
    #[must_use]
    pub fn new(
        tool_name: impl Into<String>,
        tool_version: impl Into<String>,
        input: impl Into<String>,
    ) -> Self {
        Self {
            tool_name: tool_name.into(),
            tool_version: tool_version.into(),
            input: input.into(),
            capabilities: Vec::new(),
        }
    }

    /// Add required capability
    pub fn with_capability(mut self, capability: impl Into<String>) -> Self {
        self.capabilities.push(capability.into());
        self
    }
}

/// Patch proposal for self-evolution
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct PatchProposal {
    /// Patch type
    pub patch_type: PatchType,

    /// Target (what to patch)
    pub target: String,

    /// Patch data
    pub patch: String,

    /// Reasoning
    pub reasoning: String,

    /// Test requirements
    pub test_requirements: Vec<String>,
}

impl PatchProposal {
    /// Create a new patch proposal
    #[must_use]
    pub fn new(
        patch_type: PatchType,
        target: impl Into<String>,
        patch: impl Into<String>,
        reasoning: impl Into<String>,
    ) -> Self {
        Self {
            patch_type,
            target: target.into(),
            patch: patch.into(),
            reasoning: reasoning.into(),
            test_requirements: Vec::new(),
        }
    }
}

/// Types of patches
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum PatchType {
    /// Prompt update
    Prompt,

    /// Policy update
    Policy,

    /// Routing heuristic update
    Routing,

    /// Configuration update
    Config,

    /// Tool addition/removal
    Tools,

    /// Other
    Other(String),
}

/// Deterministic state machine trait
///
/// Agents must implement this trait for deterministic behavior.
pub trait StateMachine: Send + Sync {
    /// Process input and produce output
    ///
    /// # Arguments
    /// - `state`: Current agent state
    /// - `observation`: Environment observation
    /// - `tool_responses`: Responses from previous tool calls
    /// - `context`: Deterministic execution context
    ///
    /// # Returns
    /// Next state and decision
    fn transition(
        &self,
        state: &AgentState,
        observation: &Observation,
        tool_responses: &[ToolResponse],
        context: &ExecutionContext,
    ) -> StateResult<Transition>;

    /// Get initial state
    fn initial_state(&self) -> AgentState;
}

/// Result type for state transitions
pub type StateResult<T> = core::result::Result<T, StateError>;

/// State machine errors
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum StateError {
    /// Invalid state transition
    InvalidTransition { from: String, to: String },

    /// Missing required state
    MissingState(String),

    /// State corruption
    Corrupted(String),

    /// Invalid observation
    InvalidObservation(String),

    /// Transition failed
    TransitionFailed(String),
}

impl fmt::Display for StateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            StateError::InvalidTransition { from, to } => {
                write!(f, "Invalid transition: {} -> {}", from, to)
            }
            StateError::MissingState(s) => write!(f, "Missing state: {}", s),
            StateError::Corrupted(s) => write!(f, "State corrupted: {}", s),
            StateError::InvalidObservation(s) => write!(f, "Invalid observation: {}", s),
            StateError::TransitionFailed(s) => write!(f, "Transition failed: {}", s),
        }
    }
}

/// Transition output
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Transition {
    /// Next state
    pub state: AgentState,

    /// Decision
    pub decision: Decision,

    /// Transition hash
    pub transition_hash: Hash,
}

impl Transition {
    /// Create a new transition
    #[must_use]
    pub fn new(state: AgentState, decision: Decision) -> Self {
        let transition_hash = state.hash(); // Simplified for now
        Self {
            state,
            decision,
            transition_hash,
        }
    }
}

/// Execution context for state transitions
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExecutionContext {
    /// Logical timestamp
    pub logical_time: u64,

    /// Run ID
    pub run_id: u64,

    /// Random seed (if applicable)
    pub seed: Option<u64>,
}

impl ExecutionContext {
    /// Create a new context
    #[must_use]
    pub fn new(logical_time: u64, run_id: u64) -> Self {
        Self {
            logical_time,
            run_id,
            seed: None,
        }
    }

    /// Create with seed
    #[must_use]
    pub fn with_seed(logical_time: u64, run_id: u64, seed: u64) -> Self {
        Self {
            logical_time,
            run_id,
            seed: Some(seed),
        }
    }
}

/// Tool response for state machine input
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ToolResponse {
    /// Tool name
    pub tool_name: String,

    /// Response data
    pub data: StateData,

    /// Success status
    pub success: bool,

    /// Error message if failed
    pub error: Option<String>,
}

impl ToolResponse {
    /// Create a successful response
    #[must_use]
    pub fn success(tool_name: impl Into<String>, data: StateData) -> Self {
        Self {
            tool_name: tool_name.into(),
            data,
            success: true,
            error: None,
        }
    }

    /// Create an error response
    #[must_use]
    pub fn error(tool_name: impl Into<String>, error: impl Into<String>) -> Self {
        Self {
            tool_name: tool_name.into(),
            data: StateData::Value(StateValue::None),
            success: false,
            error: Some(error.into()),
        }
    }
}

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

    #[test]
    fn test_agent_state_initial() {
        let state = AgentState::initial();
        assert_eq!(state.version, 0);
        assert!(state.data.is_empty());
    }

    #[test]
    fn test_agent_state_set_get() {
        let mut state = AgentState::initial();
        state.set("test", StateData::Value(StateValue::U64(42)));

        assert!(state.get("test").is_some());
        assert_eq!(state.version, 1);
    }

    #[test]
    fn test_agent_state_rehash() {
        let mut state1 = AgentState::initial();
        state1.set("key", StateData::Value(StateValue::String("value".to_string())));

        let mut state2 = AgentState::initial();
        state2.set("key", StateData::Value(StateValue::String("value".to_string())));

        assert_eq!(state1.hash(), state2.hash());
    }

    #[test]
    fn test_observation() {
        let obs = Observation::new("test_observation", 100)
            .with_field("value", StateValue::U64(42));

        assert_eq!(obs.kind, "test_observation");
        assert_eq!(obs.logical_time, 100);
        assert_eq!(obs.data.len(), 1);
    }
}