nemo-flow-adaptive 0.2.0

Adaptive runtime primitives and Redis-backed learning components for NeMo Flow.
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
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Redis-backed [`StorageBackend`] implementation.
//!
//! Provides [`RedisBackend`] which persists runs, plans, trie envelopes, and
//! accumulator state in Redis using atomic JSON blob operations.
//!
//! # Key layout
//!
//! All keys are prefixed with the configurable `key_prefix`:
//!
//! | Kind           | Key pattern                             | Value           |
//! |----------------|-----------------------------------------|-----------------|
//! | Run record     | `{prefix}runs:{agent_id}:{run_id}`     | JSON RunRecord  |
//! | Run index      | `{prefix}runs_index:{agent_id}`         | LIST of run UUIDs |
//! | Execution plan | `{prefix}plan:{agent_id}`               | JSON ExecutionPlan |
//! | Trie envelope  | `{prefix}trie:{agent_id}`               | JSON TrieEnvelope |
//! | Accumulators   | `{prefix}accumulators:{agent_id}`       | JSON AccumulatorState |

use std::future::Future;
use std::pin::Pin;

use redis::Client;
use redis::aio::ConnectionManager;

use crate::error::{AdaptiveError, Result};
use crate::storage::traits::{StorageBackend, StorageBackendDyn};
use crate::trie::accumulator::AccumulatorState;
use crate::trie::serialization::TrieEnvelope;
use crate::types::plan::ExecutionPlan;
use crate::types::records::RunRecord;

/// A Redis-backed storage backend for cross-process shared state.
///
/// Uses [`ConnectionManager`] which is `Clone` (internally `Arc`-based) and
/// automatically reconnects on transient failures. Trie persistence uses an
/// atomic single JSON blob `SET` — no partial update is possible.
pub struct RedisBackend {
    client: Client,
    conn: ConnectionManager,
    key_prefix: String,
}

impl RedisBackend {
    /// Connect to Redis and return a new `RedisBackend`.
    ///
    /// # Arguments
    ///
    /// * `url` — Redis connection URL (e.g. `redis://127.0.0.1:6379`).
    /// * `key_prefix` — String prepended to every Redis key (e.g. `"nemo_flow:"`).
    ///
    /// # Errors
    ///
    /// Returns [`AdaptiveError::Storage`] if the client cannot be created or the
    /// connection cannot be established.
    pub async fn new(url: &str, key_prefix: impl Into<String>) -> Result<Self> {
        let client = redis::Client::open(url)
            .map_err(|e| AdaptiveError::Storage(format!("redis client: {e}")))?;
        let conn = client
            .get_connection_manager()
            .await
            .map_err(|e| AdaptiveError::Storage(format!("redis connection: {e}")))?;
        Ok(Self {
            client,
            conn,
            key_prefix: key_prefix.into(),
        })
    }

    /// Build the key for a kind + agent_id pair.
    fn key(&self, kind: &str, agent_id: &str) -> String {
        format!("{}{}:{}", self.key_prefix, kind, agent_id)
    }

    /// Build the key for a specific run record.
    fn run_key(&self, agent_id: &str, run_id: &uuid::Uuid) -> String {
        format!("{}runs:{}:{}", self.key_prefix, agent_id, run_id)
    }

    async fn store_run_impl(&self, record: &RunRecord) -> Result<()> {
        let mut conn = self.conn.clone();
        let run_key = self.run_key(&record.agent_id, &record.id);
        let index_key = self.key("runs_index", &record.agent_id);
        let json = serde_json::to_string(record).map_err(AdaptiveError::Serialization)?;
        let run_id_str = record.id.to_string();

        redis::pipe()
            .atomic()
            .cmd("SET")
            .arg(&run_key)
            .arg(&json)
            .cmd("RPUSH")
            .arg(&index_key)
            .arg(&run_id_str)
            .exec_async(&mut conn)
            .await
            .map_err(|e| AdaptiveError::Storage(format!("redis store_run pipeline: {e}")))?;
        Ok(())
    }

    async fn load_plan_impl(&self, agent_id: &str) -> Result<Option<ExecutionPlan>> {
        let mut conn = self.conn.clone();
        let key = self.key("plan", agent_id);
        let maybe_json: Option<String> =
            redis::cmd("GET")
                .arg(&key)
                .query_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis GET plan: {e}")))?;

        match maybe_json {
            Some(json) => {
                let plan = serde_json::from_str(&json).map_err(AdaptiveError::Serialization)?;
                Ok(Some(plan))
            }
            None => Ok(None),
        }
    }

    async fn list_runs_impl(&self, agent_id: &str) -> Result<Vec<RunRecord>> {
        let mut conn = self.conn.clone();
        let index_key = self.key("runs_index", agent_id);
        let prefix = self.key_prefix.clone();
        let agent_id_owned = agent_id.to_string();
        let ids: Vec<String> = redis::cmd("LRANGE")
            .arg(&index_key)
            .arg(0i64)
            .arg(-1i64)
            .query_async(&mut conn)
            .await
            .map_err(|e| AdaptiveError::Storage(format!("redis LRANGE runs: {e}")))?;

        let mut records = Vec::with_capacity(ids.len());
        for id_str in &ids {
            let key = format!("{}runs:{}:{}", prefix, agent_id_owned, id_str);
            let maybe_json: Option<String> = redis::cmd("GET")
                .arg(&key)
                .query_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis GET run: {e}")))?;
            if let Some(json) = maybe_json {
                let record: RunRecord =
                    serde_json::from_str(&json).map_err(AdaptiveError::Serialization)?;
                records.push(record);
            }
        }

        Ok(records)
    }

    async fn store_trie_impl(&self, agent_id: &str, envelope: &TrieEnvelope) -> Result<()> {
        let mut conn = self.conn.clone();
        let key = self.key("trie", agent_id);
        let json = serde_json::to_string(envelope).map_err(AdaptiveError::Serialization)?;

        redis::cmd("SET")
            .arg(&key)
            .arg(&json)
            .exec_async(&mut conn)
            .await
            .map_err(|e| AdaptiveError::Storage(format!("redis SET trie: {e}")))?;
        Ok(())
    }

    async fn load_trie_impl(&self, agent_id: &str) -> Result<Option<TrieEnvelope>> {
        let mut conn = self.conn.clone();
        let key = self.key("trie", agent_id);
        let maybe_json: Option<String> =
            redis::cmd("GET")
                .arg(&key)
                .query_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis GET trie: {e}")))?;

        match maybe_json {
            Some(json) => {
                let envelope = serde_json::from_str(&json).map_err(AdaptiveError::Serialization)?;
                Ok(Some(envelope))
            }
            None => Ok(None),
        }
    }

    async fn store_accumulators_impl(
        &self,
        agent_id: &str,
        state: &AccumulatorState,
    ) -> Result<()> {
        let mut conn = self.conn.clone();
        let key = self.key("accumulators", agent_id);
        let json = serde_json::to_string(state).map_err(AdaptiveError::Serialization)?;

        redis::cmd("SET")
            .arg(&key)
            .arg(&json)
            .exec_async(&mut conn)
            .await
            .map_err(|e| AdaptiveError::Storage(format!("redis SET accumulators: {e}")))?;
        Ok(())
    }

    async fn load_accumulators_impl(&self, agent_id: &str) -> Result<Option<AccumulatorState>> {
        let mut conn = self.conn.clone();
        let key = self.key("accumulators", agent_id);
        let maybe_json: Option<String> =
            redis::cmd("GET")
                .arg(&key)
                .query_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis GET accumulators: {e}")))?;

        match maybe_json {
            Some(json) => {
                let state = serde_json::from_str(&json).map_err(AdaptiveError::Serialization)?;
                Ok(Some(state))
            }
            None => Ok(None),
        }
    }
}

impl StorageBackend for RedisBackend {
    fn store_run(&self, record: &RunRecord) -> impl Future<Output = Result<()>> + Send {
        self.store_run_impl(record)
    }

    fn load_plan(
        &self,
        agent_id: &str,
    ) -> impl Future<Output = Result<Option<ExecutionPlan>>> + Send {
        self.load_plan_impl(agent_id)
    }

    fn list_runs(&self, agent_id: &str) -> impl Future<Output = Result<Vec<RunRecord>>> + Send {
        self.list_runs_impl(agent_id)
    }
}

impl StorageBackendDyn for RedisBackend {
    fn store_run_dyn<'a>(
        &'a self,
        record: &'a RunRecord,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
        Box::pin(self.store_run_impl(record))
    }

    fn load_plan_dyn<'a>(
        &'a self,
        agent_id: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<ExecutionPlan>>> + Send + 'a>> {
        Box::pin(self.load_plan_impl(agent_id))
    }

    fn list_runs_dyn<'a>(
        &'a self,
        agent_id: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<RunRecord>>> + Send + 'a>> {
        Box::pin(self.list_runs_impl(agent_id))
    }

    fn store_trie<'a>(
        &'a self,
        agent_id: &'a str,
        envelope: &'a TrieEnvelope,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
        Box::pin(self.store_trie_impl(agent_id, envelope))
    }

    fn load_trie<'a>(
        &'a self,
        agent_id: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<TrieEnvelope>>> + Send + 'a>> {
        Box::pin(self.load_trie_impl(agent_id))
    }

    fn store_accumulators<'a>(
        &'a self,
        agent_id: &'a str,
        state: &'a AccumulatorState,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
        Box::pin(self.store_accumulators_impl(agent_id, state))
    }

    fn load_accumulators<'a>(
        &'a self,
        agent_id: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<AccumulatorState>>> + Send + 'a>> {
        Box::pin(self.load_accumulators_impl(agent_id))
    }

    fn store_plan(&self, plan: &ExecutionPlan) -> Result<()> {
        let mut conn = self
            .client
            .get_connection()
            .map_err(|e| AdaptiveError::Storage(format!("redis connection: {e}")))?;
        let key = self.key("plan", &plan.agent_id);
        let json = serde_json::to_string(plan).map_err(AdaptiveError::Serialization)?;

        redis::cmd("SET")
            .arg(&key)
            .arg(&json)
            .exec(&mut conn)
            .map_err(|e| AdaptiveError::Storage(format!("redis SET plan: {e}")))?;
        Ok(())
    }

    fn store_observations<'a>(
        &'a self,
        agent_id: &'a str,
        observations: &'a [crate::acg::prompt_ir::PromptIR],
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
        let mut conn = self.conn.clone();
        let key = self.key("acg_observations", agent_id);
        let json = serde_json::to_string(observations);

        Box::pin(async move {
            let json = json.map_err(AdaptiveError::Serialization)?;
            redis::cmd("SET")
                .arg(&key)
                .arg(&json)
                .exec_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis SET acg_observations: {e}")))?;
            Ok(())
        })
    }

    fn load_observations<'a>(
        &'a self,
        agent_id: &'a str,
    ) -> Pin<
        Box<dyn Future<Output = Result<Option<Vec<crate::acg::prompt_ir::PromptIR>>>> + Send + 'a>,
    > {
        let mut conn = self.conn.clone();
        let key = self.key("acg_observations", agent_id);

        Box::pin(async move {
            let maybe_json: Option<String> = redis::cmd("GET")
                .arg(&key)
                .query_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis GET acg_observations: {e}")))?;
            match maybe_json {
                Some(json) => {
                    let obs = serde_json::from_str(&json).map_err(AdaptiveError::Serialization)?;
                    Ok(Some(obs))
                }
                None => Ok(None),
            }
        })
    }

    fn store_stability<'a>(
        &'a self,
        agent_id: &'a str,
        result: &'a crate::acg::stability::StabilityAnalysisResult,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
        let mut conn = self.conn.clone();
        let key = self.key("acg_stability", agent_id);
        let json = serde_json::to_string(result);

        Box::pin(async move {
            let json = json.map_err(AdaptiveError::Serialization)?;
            redis::cmd("SET")
                .arg(&key)
                .arg(&json)
                .exec_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis SET acg_stability: {e}")))?;
            Ok(())
        })
    }

    fn load_stability<'a>(
        &'a self,
        agent_id: &'a str,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<Option<crate::acg::stability::StabilityAnalysisResult>>>
                + Send
                + 'a,
        >,
    > {
        let mut conn = self.conn.clone();
        let key = self.key("acg_stability", agent_id);

        Box::pin(async move {
            let maybe_json: Option<String> = redis::cmd("GET")
                .arg(&key)
                .query_async(&mut conn)
                .await
                .map_err(|e| AdaptiveError::Storage(format!("redis GET acg_stability: {e}")))?;
            match maybe_json {
                Some(json) => {
                    let result =
                        serde_json::from_str(&json).map_err(AdaptiveError::Serialization)?;
                    Ok(Some(result))
                }
                None => Ok(None),
            }
        })
    }
}

#[cfg(test)]
#[path = "../tests/unit/redis_tests.rs"]
mod tests;