greentic-aw-runtime 1.2.2

Enterprise Agentic Worker runtime — Plan-Act-Observe loop, Redis state, tool dispatch via greentic-ext-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
//! Test doubles. Compiled only when `--features test-mock`. The runner-
//! host and designer integration tests use these to avoid hitting Redis
//! or real LLM providers in CI.

#[allow(clippy::expect_used)]
mod inner {
    use std::collections::HashMap;
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    use crate::config::AgentConfig;
    use crate::config_provider::ConfigProvider;
    use crate::error::{ConfigError, LlmError, StateError, TerminationReason};
    use crate::knowledge::{
        IngestOutcome as KnowledgeIngestOutcome, Knowledge, KnowledgeChunk, KnowledgeQuery,
        KnowledgeResult, RetrievedChunk,
    };
    use crate::llm::{LlmBackend, LlmRequest, LlmResponse};
    use crate::long_term::{
        EpisodeIngest, IngestOutcome, LongTermMemory, LongTermMemoryError, RecallQuery,
        RecalledFact,
    };
    use crate::state::{AgentStateStore, ConversationState, SessionLock, SessionLockInner};
    use crate::telemetry::{StepTelemetryCtx, Telemetry};
    use crate::tenant::TenantContext;
    use greentic_types::TenantCtx;
    use tokio::sync::Notify;

    /// LLM mock with a scripted response queue. Records the `system_prompt` of
    /// every request it sees so tests can assert on prompt augmentation.
    pub struct MockLlmBackend {
        pub responses: Mutex<Vec<Result<LlmResponse, LlmError>>>,
        pub seen_system_prompts: Mutex<Vec<String>>,
        /// Tool names advertised in each request the backend saw.
        pub seen_tool_names: Mutex<Vec<Vec<String>>>,
    }

    impl MockLlmBackend {
        pub fn new(responses: Vec<Result<LlmResponse, LlmError>>) -> Self {
            Self {
                responses: Mutex::new(responses),
                seen_system_prompts: Mutex::new(Vec::new()),
                seen_tool_names: Mutex::new(Vec::new()),
            }
        }
    }

    impl LlmBackend for MockLlmBackend {
        fn complete<'a>(
            &'a self,
            req: LlmRequest,
        ) -> Pin<Box<dyn Future<Output = Result<LlmResponse, LlmError>> + Send + 'a>> {
            self.seen_system_prompts
                .lock()
                .expect("mock llm prompt lock poisoned")
                .push(req.system_prompt.clone());
            self.seen_tool_names
                .lock()
                .expect("mock llm tools lock poisoned")
                .push(req.tools.iter().map(|t| t.tool_name.clone()).collect());
            // Eagerly extract the next scripted response so the future is Send + 'a.
            let next = {
                let mut queue = self.responses.lock().expect("mock LLM mutex poisoned");
                if queue.is_empty() {
                    Err(LlmError::Transport("mock queue exhausted".into()))
                } else {
                    queue.remove(0)
                }
            };
            Box::pin(async move { next })
        }
    }

    /// In-memory long-term backend for tests. Returns `scripted_facts` from
    /// `recall`, records every `ingest_episode` body, and notifies after each
    /// ingest so fire-and-forget callers can be awaited deterministically.
    pub struct MockLongTermMemory {
        scripted_facts: Vec<RecalledFact>,
        ingested: Mutex<Vec<EpisodeIngest>>,
        ingested_notify: Notify,
    }

    impl MockLongTermMemory {
        pub fn new(scripted_facts: Vec<RecalledFact>) -> Self {
            Self {
                scripted_facts,
                ingested: Mutex::new(Vec::new()),
                ingested_notify: Notify::new(),
            }
        }

        /// Snapshot of the episodes ingested so far.
        pub fn ingested(&self) -> Vec<EpisodeIngest> {
            self.ingested
                .lock()
                .expect("mock ingest lock poisoned")
                .clone()
        }

        /// Wait until at least `n` episodes have been ingested (deterministic
        /// alternative to sleeping for a fire-and-forget ingest).
        pub async fn wait_for_ingests(&self, n: usize) {
            loop {
                // Register interest BEFORE checking, so a notify that fires
                // between the check and the await is not missed.
                let notified = self.ingested_notify.notified();
                if self
                    .ingested
                    .lock()
                    .expect("mock ingest lock poisoned")
                    .len()
                    >= n
                {
                    return;
                }
                notified.await;
            }
        }
    }

    #[async_trait::async_trait]
    impl LongTermMemory for MockLongTermMemory {
        async fn ingest_episode(
            &self,
            _tenant: &TenantCtx,
            episode: EpisodeIngest,
        ) -> Result<IngestOutcome, LongTermMemoryError> {
            let id = format!("ep-{}", episode.name);
            self.ingested
                .lock()
                .expect("mock ingest lock poisoned")
                .push(episode);
            self.ingested_notify.notify_waiters();
            Ok(IngestOutcome {
                episode_id: id,
                fact_count: 0,
                entity_count: 0,
            })
        }

        async fn recall(
            &self,
            _tenant: &TenantCtx,
            _query: RecallQuery,
        ) -> Result<Vec<RecalledFact>, LongTermMemoryError> {
            Ok(self.scripted_facts.clone())
        }
    }

    /// Knowledge (doc-RAG) mock: returns a scripted set of chunks from `search`
    /// (bounded by `query.limit`) and records every ingested batch so tests can
    /// assert on first-boot ingestion.
    pub struct MockKnowledge {
        scripted_chunks: Vec<RetrievedChunk>,
        ingested: Mutex<Vec<KnowledgeChunk>>,
    }

    impl MockKnowledge {
        pub fn new(scripted_chunks: Vec<RetrievedChunk>) -> Self {
            Self {
                scripted_chunks,
                ingested: Mutex::new(Vec::new()),
            }
        }

        /// Snapshot of the chunks ingested so far.
        pub fn ingested(&self) -> Vec<KnowledgeChunk> {
            self.ingested
                .lock()
                .expect("mock knowledge ingest lock poisoned")
                .clone()
        }
    }

    #[async_trait::async_trait]
    impl Knowledge for MockKnowledge {
        async fn ingest(
            &self,
            _tenant: &TenantCtx,
            chunks: Vec<KnowledgeChunk>,
        ) -> KnowledgeResult<KnowledgeIngestOutcome> {
            let ids = chunks
                .iter()
                .map(|c| format!("{}#{}", c.doc_id, c.chunk_index))
                .collect();
            self.ingested
                .lock()
                .expect("mock knowledge ingest lock poisoned")
                .extend(chunks);
            Ok(KnowledgeIngestOutcome { chunk_ids: ids })
        }

        async fn search(
            &self,
            _tenant: &TenantCtx,
            query: KnowledgeQuery,
        ) -> KnowledgeResult<Vec<RetrievedChunk>> {
            let mut hits = self.scripted_chunks.clone();
            if let Some(limit) = query.limit {
                hits.truncate(limit);
            }
            Ok(hits)
        }
    }

    /// In-memory state store; lock is a no-op semaphore that counts its
    /// refreshes (`lock_refreshes`), so a test can see a held lock kept alive.
    pub struct MockAgentStateStore {
        entries: Mutex<HashMap<String, ConversationState>>,
        lock_refreshes: Arc<AtomicUsize>,
    }

    impl MockAgentStateStore {
        pub fn new() -> Self {
            Self {
                entries: Mutex::new(HashMap::new()),
                lock_refreshes: Arc::new(AtomicUsize::new(0)),
            }
        }

        /// How many times any lock this store handed out was refreshed.
        pub fn lock_refreshes(&self) -> usize {
            self.lock_refreshes.load(Ordering::SeqCst)
        }

        fn build_key(tenant: &TenantContext, session_id: &str) -> String {
            format!("{}:{}", tenant.key_prefix(), session_id)
        }
    }

    impl Default for MockAgentStateStore {
        fn default() -> Self {
            Self::new()
        }
    }

    impl AgentStateStore for MockAgentStateStore {
        fn load<'a>(
            &'a self,
            tenant: &'a TenantContext,
            session_id: &'a str,
        ) -> Pin<Box<dyn Future<Output = Result<ConversationState, StateError>> + Send + 'a>>
        {
            let key = Self::build_key(tenant, session_id);
            let state = self
                .entries
                .lock()
                .expect("mock state mutex poisoned")
                .get(&key)
                .cloned()
                .unwrap_or_else(|| ConversationState::empty(tenant, session_id));
            Box::pin(async move { Ok(state) })
        }

        fn save<'a>(
            &'a self,
            tenant: &'a TenantContext,
            session_id: &'a str,
            state: &'a ConversationState,
        ) -> Pin<Box<dyn Future<Output = Result<(), StateError>> + Send + 'a>> {
            let key = Self::build_key(tenant, session_id);
            let cloned = state.clone();
            Box::pin(async move {
                self.entries
                    .lock()
                    .expect("mock state mutex poisoned")
                    .insert(key, cloned);
                Ok(())
            })
        }

        fn acquire_lock<'a>(
            &'a self,
            _tenant: &'a TenantContext,
            _session_id: &'a str,
            _wait: Duration,
        ) -> Pin<Box<dyn Future<Output = Result<SessionLock, StateError>> + Send + 'a>> {
            let refreshes = self.lock_refreshes.clone();
            Box::pin(async move { Ok(SessionLock::new(Box::new(NoopLockInner { refreshes }))) })
        }
    }

    struct NoopLockInner {
        refreshes: Arc<AtomicUsize>,
    }

    impl SessionLockInner for NoopLockInner {
        fn refresh<'a>(
            &'a self,
        ) -> Pin<Box<dyn Future<Output = Result<(), StateError>> + Send + 'a>> {
            self.refreshes.fetch_add(1, Ordering::SeqCst);
            Box::pin(async { Ok(()) })
        }

        fn release(&self) {}
    }

    pub struct MockTelemetry {
        pub recorded: Mutex<Vec<StepTelemetryCtx>>,
    }

    impl MockTelemetry {
        pub fn new() -> Self {
            Self {
                recorded: Mutex::new(Vec::new()),
            }
        }
    }

    impl Default for MockTelemetry {
        fn default() -> Self {
            Self::new()
        }
    }

    impl Telemetry for MockTelemetry {
        fn record_step(&self, ctx: &StepTelemetryCtx) {
            self.recorded
                .lock()
                .expect("mock telemetry mutex poisoned")
                .push(ctx.clone());
        }
    }

    pub struct MockConfigProvider {
        pub configs: Mutex<HashMap<String, AgentConfig>>,
    }

    impl MockConfigProvider {
        pub fn new() -> Self {
            Self {
                configs: Mutex::new(HashMap::new()),
            }
        }

        pub fn insert(&self, tenant: &TenantContext, agent_id: &str, cfg: AgentConfig) {
            self.configs
                .lock()
                .expect("mock config mutex poisoned")
                .insert(format!("{}:{agent_id}", tenant.key_prefix()), cfg);
        }
    }

    impl Default for MockConfigProvider {
        fn default() -> Self {
            Self::new()
        }
    }

    impl ConfigProvider for MockConfigProvider {
        fn agent_config<'a>(
            &'a self,
            tenant: &'a TenantContext,
            agent_id: &'a str,
        ) -> Pin<Box<dyn Future<Output = Result<AgentConfig, ConfigError>> + Send + 'a>> {
            let key = format!("{}:{agent_id}", tenant.key_prefix());
            let agent_id_owned = agent_id.to_string();
            let entry = self
                .configs
                .lock()
                .expect("mock config mutex poisoned")
                .get(&key)
                .cloned();
            Box::pin(async move { entry.ok_or(ConfigError::AgentNotFound(agent_id_owned)) })
        }
    }

    /// Ledger that never records anything — every `get` returns `None`,
    /// so the loop always dispatches. Used by unit tests that don't want
    /// Redis-backed idempotency.
    pub struct NoopToolLedger;

    impl crate::tools::ToolLedger for NoopToolLedger {
        fn get<'a>(
            &'a self,
            _tenant: &'a TenantContext,
            _session_id: &'a str,
            _call_id: &'a str,
        ) -> Pin<Box<dyn Future<Output = Result<Option<serde_json::Value>, StateError>> + Send + 'a>>
        {
            Box::pin(async { Ok(None) })
        }

        fn record<'a>(
            &'a self,
            _tenant: &'a TenantContext,
            _session_id: &'a str,
            _call_id: &'a str,
            _result: serde_json::Value,
        ) -> Pin<Box<dyn Future<Output = Result<(), StateError>> + Send + 'a>> {
            Box::pin(async { Ok(()) })
        }
    }

    /// Billing meter with a fixed `over_budget` verdict, for exercising the credit
    /// gate without HTTP. `emit` records nothing — the gate is what is under test.
    pub struct MockBillingMeter {
        over: bool,
    }

    impl MockBillingMeter {
        #[must_use]
        pub fn new(over: bool) -> Self {
            Self { over }
        }
    }

    impl crate::billing::BillingMeter for MockBillingMeter {
        fn emit<'a>(
            &'a self,
            _tenant: &'a TenantContext,
            _input_tokens: u64,
            _output_tokens: u64,
            _agent_id: &'a str,
            _model: &'a str,
        ) -> Pin<Box<dyn Future<Output = Result<(), crate::billing::BillingError>> + Send + 'a>>
        {
            Box::pin(async { Ok(()) })
        }

        fn over_budget<'a>(
            &'a self,
            _tenant: &'a TenantContext,
        ) -> Pin<Box<dyn Future<Output = bool> + Send + 'a>> {
            Box::pin(std::future::ready(self.over))
        }
    }

    /// Convenience: assert a [`TerminationReason`] matches the expected value.
    pub fn assert_terminated_by(actual: &TerminationReason, expected: &TerminationReason) {
        assert_eq!(actual, expected, "expected {expected:?}, got {actual:?}");
    }
}

pub use inner::{
    MockAgentStateStore, MockBillingMeter, MockConfigProvider, MockKnowledge, MockLlmBackend,
    MockLongTermMemory, MockTelemetry, NoopToolLedger, assert_terminated_by,
};