aingle_cortex 0.6.3

Córtex API - REST/GraphQL/SPARQL interface for AIngle semantic graphs
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! REST endpoints for the Ineru memory subsystem.
//!
//! These endpoints expose the STM/LTM dual-memory architecture through
//! the Cortex REST API, allowing agents to store, recall, consolidate,
//! and checkpoint their memory state.
//!
//! ## Endpoints
//!
//! - `POST   /api/v1/memory/remember`      - Store in STM
//! - `POST   /api/v1/memory/recall`         - Query STM + LTM
//! - `POST   /api/v1/memory/consolidate`    - Force STM → LTM
//! - `GET    /api/v1/memory/stats`          - Memory statistics
//! - `DELETE /api/v1/memory/:id`            - Forget
//! - `POST   /api/v1/memory/checkpoint`     - Snapshot state
//! - `GET    /api/v1/memory/checkpoints`    - List snapshots
//! - `POST   /api/v1/memory/restore/:id`    - Restore snapshot

use axum::{
    extract::{Path, State},
    http::StatusCode,
    Json,
};
use serde::{Deserialize, Serialize};
use ineru::{MemoryEntry, MemoryId, MemoryQuery};

use crate::error::{Error, Result};
use crate::state::AppState;

// ============================================================================
// DTOs
// ============================================================================

#[derive(Debug, Deserialize)]
pub struct RememberRequest {
    pub entry_type: String,
    pub data: serde_json::Value,
    #[serde(default)]
    pub tags: Vec<String>,
    #[serde(default = "default_importance")]
    pub importance: f32,
    pub embedding: Option<Vec<f32>>,
}

fn default_importance() -> f32 {
    0.7
}

#[derive(Debug, Serialize)]
pub struct RememberResponse {
    pub id: String,
}

#[derive(Debug, Deserialize)]
pub struct RecallRequest {
    pub text: Option<String>,
    #[serde(default)]
    pub tags: Vec<String>,
    pub entry_type: Option<String>,
    pub min_importance: Option<f32>,
    pub limit: Option<usize>,
}

#[derive(Debug, Serialize)]
pub struct MemoryResultDto {
    pub id: String,
    pub entry_type: String,
    pub data: serde_json::Value,
    pub tags: Vec<String>,
    pub importance: f32,
    pub relevance: f32,
    pub source: String,
    pub created_at: String,
    pub last_accessed: String,
    pub access_count: u32,
}

#[derive(Debug, Serialize)]
pub struct ConsolidateResponse {
    pub consolidated: usize,
}

#[derive(Debug, Serialize)]
pub struct MemoryStatsDto {
    pub stm_count: usize,
    pub stm_capacity: usize,
    pub ltm_entity_count: usize,
    pub ltm_link_count: usize,
    pub total_memory_bytes: usize,
}

#[derive(Debug, Deserialize)]
pub struct CheckpointRequest {
    pub label: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct CheckpointResponse {
    #[serde(rename = "checkpointId")]
    pub checkpoint_id: String,
}

#[derive(Debug, Serialize)]
pub struct CheckpointListDto {
    pub id: String,
    pub label: Option<String>,
    pub created_at: String,
    pub stm_count: usize,
    pub ltm_entity_count: usize,
}

// ============================================================================
// Handlers
// ============================================================================

/// Store a memory entry in Short-Term Memory.
pub async fn remember(
    State(state): State<AppState>,
    Json(req): Json<RememberRequest>,
) -> Result<(StatusCode, Json<RememberResponse>)> {
    // Cluster mode: route through Raft
    #[cfg(feature = "cluster")]
    if let Some(ref raft) = state.raft {
        let raft_req = aingle_raft::CortexRequest {
            kind: aingle_wal::WalEntryKind::MemoryStore {
                memory_id: String::new(), // assigned by state machine
                entry_type: req.entry_type.clone(),
                data: req.data.clone(),
                importance: req.importance,
            },
        };
        let resp = raft
            .client_write(raft_req)
            .await
            .map_err(|e| handle_raft_write_error(e, &state))?;

        if !resp.response().success {
            return Err(Error::Internal(
                resp.response()
                    .detail
                    .clone()
                    .unwrap_or_else(|| "Raft memory store failed".to_string()),
            ));
        }

        let id = resp
            .response()
            .id
            .clone()
            .unwrap_or_else(|| "raft".to_string());

        return Ok((
            StatusCode::CREATED,
            Json(RememberResponse { id }),
        ));
    }

    // Guard: if Raft is initialized, all writes MUST go through Raft (#2).
    #[cfg(feature = "cluster")]
    if state.raft.is_some() {
        return Err(Error::Internal("Raft initialized but write not routed through Raft".into()));
    }

    // Non-cluster mode: direct write
    #[cfg(feature = "cluster")]
    let wal_data = req.data.clone();
    let mut entry = MemoryEntry::new(&req.entry_type, req.data);

    if !req.tags.is_empty() {
        let tag_refs: Vec<&str> = req.tags.iter().map(|s| s.as_str()).collect();
        entry = entry.with_tags(&tag_refs);
    }

    entry = entry.with_importance(req.importance);

    if let Some(emb) = req.embedding {
        entry = entry.with_embedding(ineru::Embedding::new(emb));
    }

    let mut memory = state.memory.write().await;
    let id = memory
        .remember(entry)
        .map_err(|e| Error::Internal(format!("Memory store failed: {e}")))?;

    // Append to WAL (legacy cluster path)
    #[cfg(feature = "cluster")]
    if let Some(ref wal) = state.wal {
        wal.append(aingle_wal::WalEntryKind::MemoryStore {
            memory_id: id.to_hex(),
            entry_type: req.entry_type.clone(),
            data: wal_data.clone(),
            importance: req.importance,
        }).map_err(|e| Error::Internal(format!("WAL append failed: {e}")))?;
    }

    Ok((
        StatusCode::CREATED,
        Json(RememberResponse {
            id: id.to_hex(),
        }),
    ))
}

/// Query both STM and LTM for matching memories.
pub async fn recall(
    State(state): State<AppState>,
    Json(req): Json<RecallRequest>,
) -> Result<Json<Vec<MemoryResultDto>>> {
    let query = build_query(&req);
    let memory = state.memory.read().await;
    let results = memory
        .recall(&query)
        .map_err(|e| Error::Internal(format!("Memory recall failed: {e}")))?;

    let dtos: Vec<MemoryResultDto> = results
        .into_iter()
        .map(|r| MemoryResultDto {
            id: r.entry.id.to_hex(),
            entry_type: r.entry.entry_type.clone(),
            data: r.entry.data.clone(),
            tags: r.entry.tags.iter().map(|t| t.0.clone()).collect(),
            importance: r.entry.metadata.importance,
            relevance: r.relevance,
            source: match r.source {
                ineru::types::MemorySource::ShortTerm => "ShortTerm".to_string(),
                ineru::types::MemorySource::LongTerm => "LongTerm".to_string(),
            },
            created_at: r.entry.metadata.created_at.0.to_string(),
            last_accessed: r.entry.metadata.last_accessed.0.to_string(),
            access_count: r.entry.metadata.access_count,
        })
        .collect();

    Ok(Json(dtos))
}

/// Force consolidation of important STM entries into LTM.
pub async fn consolidate(
    State(state): State<AppState>,
) -> Result<Json<ConsolidateResponse>> {
    // Cluster mode: route through Raft so all nodes consolidate deterministically
    #[cfg(feature = "cluster")]
    if let Some(ref raft) = state.raft {
        let raft_req = aingle_raft::CortexRequest {
            kind: aingle_wal::WalEntryKind::MemoryConsolidate {
                consolidated_count: 0, // state machine will compute real count
            },
        };
        let resp = raft
            .client_write(raft_req)
            .await
            .map_err(|e| handle_raft_write_error(e, &state))?;

        if !resp.response().success {
            return Err(Error::Internal(
                resp.response()
                    .detail
                    .clone()
                    .unwrap_or_else(|| "Raft consolidate failed".to_string()),
            ));
        }

        // The detail field contains the consolidated count from state machine
        let count: usize = resp
            .response()
            .detail
            .as_ref()
            .and_then(|d| d.parse().ok())
            .unwrap_or(0);

        return Ok(Json(ConsolidateResponse {
            consolidated: count,
        }));
    }

    // Guard: if Raft is initialized, all writes MUST go through Raft (#2).
    #[cfg(feature = "cluster")]
    if state.raft.is_some() {
        return Err(Error::Internal("Raft initialized but write not routed through Raft".into()));
    }

    // Non-cluster mode: direct consolidation
    let mut memory = state.memory.write().await;
    let count = memory
        .consolidate()
        .map_err(|e| Error::Internal(format!("Consolidation failed: {e}")))?;

    // Append to WAL (legacy cluster path)
    #[cfg(feature = "cluster")]
    if let Some(ref wal) = state.wal {
        wal.append(aingle_wal::WalEntryKind::MemoryConsolidate {
            consolidated_count: count,
        }).map_err(|e| Error::Internal(format!("WAL append failed: {e}")))?;
    }

    Ok(Json(ConsolidateResponse {
        consolidated: count,
    }))
}

/// Get memory subsystem statistics.
pub async fn stats(State(state): State<AppState>) -> Result<Json<MemoryStatsDto>> {
    let memory = state.memory.read().await;
    let s = memory.stats();

    Ok(Json(MemoryStatsDto {
        stm_count: s.stm_count,
        stm_capacity: s.stm_capacity,
        ltm_entity_count: s.ltm_entity_count,
        ltm_link_count: s.ltm_link_count,
        total_memory_bytes: s.total_memory_bytes,
    }))
}

/// Forget (delete) a specific memory entry.
pub async fn forget(
    State(state): State<AppState>,
    Path(id): Path<String>,
) -> Result<StatusCode> {
    // Cluster mode: route through Raft
    #[cfg(feature = "cluster")]
    if let Some(ref raft) = state.raft {
        let raft_req = aingle_raft::CortexRequest {
            kind: aingle_wal::WalEntryKind::MemoryForget {
                memory_id: id.clone(),
            },
        };
        let resp = raft
            .client_write(raft_req)
            .await
            .map_err(|e| handle_raft_write_error(e, &state))?;

        if !resp.response().success {
            return Err(Error::Internal(
                resp.response()
                    .detail
                    .clone()
                    .unwrap_or_else(|| "Raft forget failed".to_string()),
            ));
        }

        return Ok(StatusCode::NO_CONTENT);
    }

    // Guard: if Raft is initialized, all writes MUST go through Raft (#2).
    #[cfg(feature = "cluster")]
    if state.raft.is_some() {
        return Err(Error::Internal("Raft initialized but write not routed through Raft".into()));
    }

    // Non-cluster mode: direct delete
    let memory_id = MemoryId::from_hex(&id)
        .ok_or_else(|| Error::InvalidInput(format!("Invalid memory ID: {id}")))?;

    let mut memory = state.memory.write().await;
    memory
        .forget(&memory_id)
        .map_err(|e| Error::NotFound(format!("Memory not found: {e}")))?;

    // Append to WAL (legacy cluster path)
    #[cfg(feature = "cluster")]
    if let Some(ref wal) = state.wal {
        wal.append(aingle_wal::WalEntryKind::MemoryForget {
            memory_id: id.clone(),
        }).map_err(|e| Error::Internal(format!("WAL append failed: {e}")))?;
    }

    Ok(StatusCode::NO_CONTENT)
}

/// Create a checkpoint (snapshot) of current memory state.
pub async fn checkpoint(
    State(state): State<AppState>,
    Json(req): Json<CheckpointRequest>,
) -> Result<(StatusCode, Json<CheckpointResponse>)> {
    // Checkpoint is a logical snapshot — we store STM+LTM stats as metadata
    // For now, create a proof-of-state in the proof store
    let memory = state.memory.read().await;
    let s = memory.stats();
    let label = req.label.unwrap_or_else(|| {
        format!("checkpoint-{}", chrono::Utc::now().timestamp())
    });

    let checkpoint_data = serde_json::json!({
        "label": label,
        "stm_count": s.stm_count,
        "ltm_entity_count": s.ltm_entity_count,
        "ltm_link_count": s.ltm_link_count,
        "total_memory_bytes": s.total_memory_bytes,
        "created_at": chrono::Utc::now().to_rfc3339(),
    });

    let proof_req = crate::proofs::SubmitProofRequest {
        proof_type: crate::proofs::ProofType::Knowledge,
        proof_data: checkpoint_data,
        metadata: Some(crate::proofs::ProofMetadata {
            submitter: Some("memory-system".to_string()),
            tags: vec!["checkpoint".to_string(), "memory".to_string()],
            extra: Default::default(),
        }),
    };

    let proof_id = state
        .proof_store
        .submit(proof_req)
        .await
        .map_err(|e| Error::Internal(format!("Checkpoint creation failed: {e}")))?;

    Ok((
        StatusCode::CREATED,
        Json(CheckpointResponse {
            checkpoint_id: proof_id,
        }),
    ))
}

/// List memory checkpoints.
pub async fn list_checkpoints(
    State(state): State<AppState>,
) -> Result<Json<Vec<CheckpointListDto>>> {
    let proofs = state
        .proof_store
        .list(Some(crate::proofs::ProofType::Knowledge))
        .await;

    let checkpoints: Vec<CheckpointListDto> = proofs
        .into_iter()
        .filter(|p| p.metadata.tags.contains(&"checkpoint".to_string()))
        .map(|p| {
            let data: serde_json::Value =
                serde_json::from_slice(&p.data).unwrap_or_default();
            CheckpointListDto {
                id: p.id.clone(),
                label: data.get("label").and_then(|v| v.as_str()).map(String::from),
                created_at: p.created_at.to_rfc3339(),
                stm_count: data
                    .get("stm_count")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0) as usize,
                ltm_entity_count: data
                    .get("ltm_entity_count")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0) as usize,
            }
        })
        .collect();

    Ok(Json(checkpoints))
}

/// Restore memory from a checkpoint.
///
/// Note: Full state restoration requires serialized memory dumps. For now,
/// this verifies the checkpoint exists and returns success.
pub async fn restore_checkpoint(
    State(state): State<AppState>,
    Path(id): Path<String>,
) -> Result<StatusCode> {
    let proof = state
        .proof_store
        .get(&id)
        .await
        .ok_or_else(|| Error::NotFound(format!("Checkpoint not found: {id}")))?;

    if !proof.metadata.tags.contains(&"checkpoint".to_string()) {
        return Err(Error::InvalidInput("Not a memory checkpoint".to_string()));
    }

    // TODO: Implement full state restoration from serialized memory dump
    // For now, this validates the checkpoint exists
    tracing::info!(checkpoint_id = %id, "Memory checkpoint acknowledged for restoration");

    Ok(StatusCode::OK)
}

// ============================================================================
// Vector Search (Motomeru)
// ============================================================================

#[derive(Debug, Deserialize)]
pub struct VectorSearchRequest {
    pub embedding: Vec<f32>,
    pub k: usize,
    #[serde(default = "default_min_similarity")]
    pub min_similarity: f32,
    pub entry_type: Option<String>,
    pub tags: Option<Vec<String>>,
}

fn default_min_similarity() -> f32 {
    0.0
}

#[derive(Debug, Serialize)]
pub struct VectorIndexStatsDto {
    pub point_count: usize,
    pub deleted_count: usize,
    pub dimensions: usize,
    pub memory_bytes: usize,
}

/// Vector search over memory entries using HNSW index.
pub async fn vector_search(
    State(state): State<AppState>,
    Json(req): Json<VectorSearchRequest>,
) -> Result<Json<Vec<MemoryResultDto>>> {
    let memory = state.memory.read().await;
    let results = memory.ltm.vector_search_memories(&req.embedding, req.k, req.min_similarity);

    let mut dtos: Vec<MemoryResultDto> = results
        .into_iter()
        .map(|(entry, similarity)| MemoryResultDto {
            id: entry.id.to_hex(),
            entry_type: entry.entry_type.clone(),
            data: entry.data.clone(),
            tags: entry.tags.iter().map(|t| t.0.clone()).collect(),
            importance: entry.metadata.importance,
            relevance: similarity,
            source: "LongTerm".to_string(),
            created_at: entry.metadata.created_at.0.to_string(),
            last_accessed: entry.metadata.last_accessed.0.to_string(),
            access_count: entry.metadata.access_count,
        })
        .collect();

    // Apply optional filters
    if let Some(ref entry_type) = req.entry_type {
        dtos.retain(|d| &d.entry_type == entry_type);
    }
    if let Some(ref tags) = req.tags {
        if !tags.is_empty() {
            dtos.retain(|d| tags.iter().any(|t| d.tags.contains(t)));
        }
    }

    Ok(Json(dtos))
}

/// Get HNSW vector index statistics.
pub async fn vector_index_stats(
    State(state): State<AppState>,
) -> Result<Json<VectorIndexStatsDto>> {
    let memory = state.memory.read().await;
    let stats = memory.ltm.hnsw_index()
        .map(|idx| idx.stats())
        .unwrap_or(ineru::hnsw::HnswStats {
            point_count: 0,
            deleted_count: 0,
            dimensions: 0,
            max_layer: 0,
            memory_bytes: 0,
        });

    Ok(Json(VectorIndexStatsDto {
        point_count: stats.point_count,
        deleted_count: stats.deleted_count,
        dimensions: stats.dimensions,
        memory_bytes: stats.memory_bytes,
    }))
}

/// Force rebuild of the HNSW vector index.
pub async fn rebuild_vector_index(
    State(state): State<AppState>,
) -> Result<StatusCode> {
    let mut memory = state.memory.write().await;
    if let Some(hnsw) = memory.ltm.hnsw_index_mut() {
        hnsw.rebuild();
        tracing::info!("HNSW index rebuilt, {} active points", hnsw.len());
    }
    Ok(StatusCode::OK)
}

// ============================================================================
// Helpers
// ============================================================================

/// Re-export shared Raft write error handler for this module.
#[cfg(feature = "cluster")]
use crate::rest::cluster_utils::handle_raft_write_error;

fn build_query(req: &RecallRequest) -> MemoryQuery {
    let mut query = if let Some(text) = &req.text {
        MemoryQuery::text(text)
    } else if !req.tags.is_empty() {
        let tag_refs: Vec<&str> = req.tags.iter().map(|s| s.as_str()).collect();
        MemoryQuery::tags(&tag_refs)
    } else {
        MemoryQuery::text("")
    };

    if let Some(limit) = req.limit {
        query = query.with_limit(limit);
    }

    if let Some(min_imp) = req.min_importance {
        query = query.with_min_importance(min_imp);
    }

    if let Some(entry_type) = &req.entry_type {
        query = MemoryQuery::entry_type(entry_type);
        if let Some(limit) = req.limit {
            query = query.with_limit(limit);
        }
        if let Some(min_imp) = req.min_importance {
            query = query.with_min_importance(min_imp);
        }
    }

    query
}

/// Create the memory sub-router.
pub fn memory_router() -> axum::Router<AppState> {
    use axum::routing::{delete, get, post};

    axum::Router::new()
        .route("/api/v1/memory/remember", post(remember))
        .route("/api/v1/memory/recall", post(recall))
        .route("/api/v1/memory/consolidate", post(consolidate))
        .route("/api/v1/memory/stats", get(stats))
        .route("/api/v1/memory/{id}", delete(forget))
        .route("/api/v1/memory/checkpoint", post(checkpoint))
        .route("/api/v1/memory/checkpoints", get(list_checkpoints))
        .route("/api/v1/memory/restore/{id}", post(restore_checkpoint))
        // Motomeru: HNSW vector search endpoints
        .route("/api/v1/memory/search", post(vector_search))
        .route("/api/v1/memory/index/stats", get(vector_index_stats))
        .route("/api/v1/memory/index/rebuild", post(rebuild_vector_index))
}