post-cortex-memory 0.3.0

Conversation memory orchestrator for post-cortex. Ties storage + embeddings + graph + session + summary into a single lock-free memory hierarchy with async pipelines and a canonical PostCortexService API.
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// Copyright (c) 2025, 2026 Julius ML
// Licensed under the MIT License. See LICENSE at the workspace root.

//! Canonical [`PostCortexService`] implementation for post-cortex-memory.
//!
//! [`MemoryServiceImpl`] is a thin adapter over
//! [`ConversationMemorySystem`] that fulfils the
//! [`post_cortex_core::services::PostCortexService`] trait. Phases 6 + 7
//! migrate the daemon's gRPC handlers and the MCP tool layer to call
//! this trait instead of `ConversationMemorySystem` directly — at that
//! point every read / write / search / manage operation flows through
//! one canonical function (TODO.md:106-117).
//!
//! Right now this impl is **incomplete on purpose**: most methods return
//! a `SystemError::Internal("not yet wired …")` while the legacy
//! transport handlers continue to call `ConversationMemorySystem` paths
//! directly. The trait surface is stable; the impl fills in
//! incrementally as Phases 6 + 7 migrate their callers. This avoids a
//! single massive commit that's impossible to bisect.

use std::collections::HashSet;
use std::sync::Arc;

use async_trait::async_trait;
use chrono::Utc;
use post_cortex_core::core::context_update::{ContextUpdate, EntityRelationship, TypedEntity};
use post_cortex_core::core::error::SystemError;
use post_cortex_core::services::{
    AdminRequest, AdminResponse, AssembleContextRequest, AssembleContextResponse,
    BulkUpdateContextRequest, BulkUpdateContextResponse, HealthReport, ManageEntityRequest,
    ManageEntityResponse, ManageSessionRequest, ManageSessionResponse, ManageWorkspaceRequest,
    ManageWorkspaceResponse, PostCortexService, QueryContextRequest, QueryContextResponse,
    SemanticSearchRequest, SemanticSearchResponse, StructuredSummaryRequest,
    StructuredSummaryResponse, UpdateContextRequest, UpdateContextResponse,
};
use tracing::warn;
use uuid::Uuid;

use crate::memory_system::ConversationMemorySystem;
use crate::pipeline::{
    EmbeddingWorkItem, GraphWorkItem, Pipeline, PipelineConfig, PipelineError, SummaryWorkItem,
};

/// Canonical [`PostCortexService`] implementation backed by
/// [`ConversationMemorySystem`].
///
/// Construct with [`Self::new`] passing an `Arc<ConversationMemorySystem>`
/// (typically the singleton owned by the daemon).
pub struct MemoryServiceImpl {
    system: Arc<ConversationMemorySystem>,
    pipeline: Arc<Pipeline>,
}

impl MemoryServiceImpl {
    /// Wrap an existing memory system in the canonical service trait,
    /// starting the non-blocking write pipeline with default capacities.
    /// Must be called from inside a Tokio runtime.
    #[must_use]
    pub fn new(system: Arc<ConversationMemorySystem>) -> Self {
        Self::with_pipeline_config(system, PipelineConfig::default())
    }

    /// Wrap an existing memory system in the service trait with an
    /// explicit pipeline configuration.
    #[must_use]
    pub fn with_pipeline_config(
        system: Arc<ConversationMemorySystem>,
        config: PipelineConfig,
    ) -> Self {
        let pipeline = Arc::new(Pipeline::start(config, Arc::clone(&system)));
        Self { system, pipeline }
    }

    /// Borrow the non-blocking pipeline. Exposed so transport adapters
    /// can submit derived-work items directly (e.g. enqueue an
    /// embedding compute after a manual storage write).
    #[must_use]
    pub fn pipeline(&self) -> &Arc<Pipeline> {
        &self.pipeline
    }

    /// Borrow the underlying memory system. Provided so the daemon's
    /// gRPC handlers can fall back to direct calls for methods that
    /// have not been migrated to the trait yet.
    #[must_use]
    pub fn inner(&self) -> &Arc<ConversationMemorySystem> {
        &self.system
    }

    fn not_yet_wired<T>(op: &'static str) -> Result<T, SystemError> {
        Err(SystemError::Internal(format!(
            "PostCortexService::{op} is not yet wired — migration lands in Phase 6 (MCP) / Phase 7 (daemon). \
             Use ConversationMemorySystem directly until then."
        )))
    }
}

#[async_trait]
impl PostCortexService for MemoryServiceImpl {
    #[tracing::instrument(skip(self), name = "post_cortex.health")]
    async fn health(&self) -> Result<HealthReport, SystemError> {
        let health = self.system.get_system_health();
        Ok(HealthReport {
            status: if health.circuit_breaker_open {
                "degraded".to_string()
            } else {
                "ok".to_string()
            },
            active_sessions: health.active_sessions,
            // SystemHealth does not yet expose memory bytes — wired in
            // Phase 11 (perf observability). For now report 0.
            memory_usage_bytes: 0,
            pipeline_backlog: self.pipeline.backlog(),
            uptime_seconds: health.uptime_seconds,
        })
    }

    #[tracing::instrument(
        skip(self, req),
        fields(
            session_id = %req.session_id,
            interaction_type = ?req.interaction_type,
            entities = req.entities.len(),
            relations = req.relations.len(),
        ),
        name = "post_cortex.update_context",
    )]
    async fn update_context(
        &self,
        req: UpdateContextRequest,
    ) -> Result<UpdateContextResponse, SystemError> {
        validate_update_request(&req)?;

        let description = build_description(&req);
        let context_update = build_context_update(&req);
        let metadata = serde_json::to_value(&context_update)
            .expect("ContextUpdate serialization cannot fail");
        let session_id = req.session_id;

        let entry_id_str = self
            .system
            .add_incremental_update(session_id, description.clone(), Some(metadata))
            .await
            .map_err(SystemError::Internal)?;

        let entry_id = Uuid::parse_str(&entry_id_str).map_err(|e| {
            SystemError::Internal(format!(
                "storage returned non-UUID entry id {entry_id_str:?}: {e}"
            ))
        })?;

        // Hand derived work to the bounded pipeline. Best-effort: a full
        // queue logs `Backpressure` but doesn't fail the durable write
        // — the legacy in-system `tokio::spawn`s in
        // `ConversationMemorySystem::add_incremental_update_internal`
        // remain as the safety net until they're fully retired in 0.4.0.
        submit_derived_work(
            &self.pipeline,
            session_id,
            entry_id,
            &description,
            context_update,
        );

        Ok(UpdateContextResponse {
            entry_id,
            session_id,
            persisted_at: Utc::now(),
            durable: true,
        })
    }

    #[tracing::instrument(
        skip(self, req),
        fields(session_id = %req.session_id, batch_size = req.updates.len()),
        name = "post_cortex.bulk_update_context",
    )]
    async fn bulk_update_context(
        &self,
        req: BulkUpdateContextRequest,
    ) -> Result<BulkUpdateContextResponse, SystemError> {
        // Pre-validate the whole batch first so a bad item doesn't leave us
        // with a half-applied write (storage-actor batching lands later;
        // until then we still fail-fast on validation but persist
        // sequentially).
        for (i, item) in req.updates.iter().enumerate() {
            if item.session_id != req.session_id {
                return Err(SystemError::InvalidArgument(format!(
                    "bulk_update_context: updates[{i}].session_id {} does not match request session_id {}",
                    item.session_id, req.session_id
                )));
            }
            validate_update_request(item).map_err(|e| match e {
                SystemError::InvalidArgument(msg) => {
                    SystemError::InvalidArgument(format!("updates[{i}]: {msg}"))
                }
                other => other,
            })?;
        }

        let mut entry_ids = Vec::with_capacity(req.updates.len());
        for (i, item) in req.updates.iter().enumerate() {
            let description = build_description(item);
            let context_update = build_context_update(item);
            let metadata = serde_json::to_value(&context_update)
                .expect("ContextUpdate serialization cannot fail");
            let entry_id_str = self
                .system
                .add_incremental_update(item.session_id, description.clone(), Some(metadata))
                .await
                .map_err(|e| SystemError::Internal(format!("updates[{i}]: {e}")))?;
            let entry_id = Uuid::parse_str(&entry_id_str).map_err(|e| {
                SystemError::Internal(format!(
                    "updates[{i}]: storage returned non-UUID entry id {entry_id_str:?}: {e}"
                ))
            })?;
            entry_ids.push(entry_id);
            submit_derived_work(
                &self.pipeline,
                item.session_id,
                entry_id,
                &description,
                context_update,
            );
        }

        Ok(BulkUpdateContextResponse {
            entry_ids,
            persisted_at: Utc::now(),
            durable: true,
        })
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.semantic_search")]
    async fn semantic_search(
        &self,
        _req: SemanticSearchRequest,
    ) -> Result<SemanticSearchResponse, SystemError> {
        Self::not_yet_wired("semantic_search")
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.query_context")]
    async fn query_context(
        &self,
        _req: QueryContextRequest,
    ) -> Result<QueryContextResponse, SystemError> {
        Self::not_yet_wired("query_context")
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.assemble_context")]
    async fn assemble_context(
        &self,
        _req: AssembleContextRequest,
    ) -> Result<AssembleContextResponse, SystemError> {
        Self::not_yet_wired("assemble_context")
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.manage_session")]
    async fn manage_session(
        &self,
        _req: ManageSessionRequest,
    ) -> Result<ManageSessionResponse, SystemError> {
        Self::not_yet_wired("manage_session")
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.manage_workspace")]
    async fn manage_workspace(
        &self,
        _req: ManageWorkspaceRequest,
    ) -> Result<ManageWorkspaceResponse, SystemError> {
        Self::not_yet_wired("manage_workspace")
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.manage_entity")]
    async fn manage_entity(
        &self,
        _req: ManageEntityRequest,
    ) -> Result<ManageEntityResponse, SystemError> {
        Self::not_yet_wired("manage_entity")
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.get_structured_summary")]
    async fn get_structured_summary(
        &self,
        _req: StructuredSummaryRequest,
    ) -> Result<StructuredSummaryResponse, SystemError> {
        Self::not_yet_wired("get_structured_summary")
    }

    #[tracing::instrument(skip(self, _req), name = "post_cortex.admin")]
    async fn admin(&self, _req: AdminRequest) -> Result<AdminResponse, SystemError> {
        Self::not_yet_wired("admin")
    }
}

// ---------------------------------------------------------------------------
// Canonical helpers — single source of truth for update_context validation
// and metadata shaping. Every transport (gRPC, MCP, REST) reaches the same
// behaviour by going through `update_context` / `bulk_update_context`; the
// helpers below are intentionally private so transports cannot bypass them.
// ---------------------------------------------------------------------------

/// Strict validation lifted from the legacy gRPC helper. Rejects any input
/// that would corrupt the entity graph (empty title+description, missing
/// entities/relations, dangling relation endpoints, self-relations, empty
/// relation context).
fn validate_update_request(req: &UpdateContextRequest) -> Result<(), SystemError> {
    if req.content.title.trim().is_empty() && req.content.description.trim().is_empty() {
        return Err(SystemError::InvalidArgument(
            "update_context: title and description are both empty — provide at least one".into(),
        ));
    }
    if req.entities.is_empty() {
        return Err(SystemError::InvalidArgument(
            "update_context: entities must not be empty".into(),
        ));
    }
    if req.relations.is_empty() {
        return Err(SystemError::InvalidArgument(
            "update_context: relations must not be empty".into(),
        ));
    }

    let entity_names: HashSet<&str> = req.entities.iter().map(|e| e.name.as_str()).collect();
    for (i, rel) in req.relations.iter().enumerate() {
        if rel.from_entity.is_empty() {
            return Err(SystemError::InvalidArgument(format!(
                "relation[{i}]: from_entity must not be empty"
            )));
        }
        if rel.to_entity.is_empty() {
            return Err(SystemError::InvalidArgument(format!(
                "relation[{i}]: to_entity must not be empty"
            )));
        }
        if rel.from_entity == rel.to_entity {
            return Err(SystemError::InvalidArgument(format!(
                "relation[{i}]: self-relations are not allowed (from_entity == to_entity == {:?})",
                rel.from_entity
            )));
        }
        if rel.context.trim().is_empty() {
            return Err(SystemError::InvalidArgument(format!(
                "relation[{i}]: context must not be empty — every relation requires an explanation"
            )));
        }
        if !entity_names.contains(rel.from_entity.as_str()) {
            return Err(SystemError::InvalidArgument(format!(
                "relation[{i}]: from_entity {:?} is not declared in the entities list",
                rel.from_entity
            )));
        }
        if !entity_names.contains(rel.to_entity.as_str()) {
            return Err(SystemError::InvalidArgument(format!(
                "relation[{i}]: to_entity {:?} is not declared in the entities list",
                rel.to_entity
            )));
        }
    }

    Ok(())
}

/// Build the text fed to the vectorizer — `title\n description` matches what
/// the MCP path settled on for cross-session search quality.
fn build_description(req: &UpdateContextRequest) -> String {
    if req.content.description.is_empty() {
        req.content.title.clone()
    } else if req.content.title.is_empty() {
        req.content.description.clone()
    } else {
        format!("{}\n{}", req.content.title, req.content.description)
    }
}

/// Build the `ContextUpdate` JSON blob stored as the storage entry's metadata.
/// Keeps `creates_entities` (names) and `typed_entities` (name + type) in sync
/// — both are consumed downstream by the entity graph builder.
fn build_context_update(req: &UpdateContextRequest) -> ContextUpdate {
    let typed_entities: Vec<TypedEntity> = req
        .entities
        .iter()
        .map(|e| TypedEntity {
            name: e.name.clone(),
            entity_type: e.entity_type.clone(),
        })
        .collect();
    let creates_entities: Vec<String> = req.entities.iter().map(|e| e.name.clone()).collect();
    let creates_relationships: Vec<EntityRelationship> = req.relations.clone();
    let related_code = req.code_reference.clone();

    ContextUpdate {
        id: Uuid::new_v4(),
        timestamp: Utc::now(),
        update_type: req.interaction_type.clone(),
        content: req.content.clone(),
        related_code,
        parent_update: None,
        user_marked_important: false,
        creates_entities,
        creates_relationships,
        references_entities: Vec::new(),
        typed_entities,
    }
}

/// Hand a freshly-persisted update off to the bounded background pipeline:
/// embedding compute, entity-graph merge, and summary refresh all run on
/// separate worker tasks so the write path returns as soon as the entry
/// is durably stored. Backpressure on any queue is logged but swallowed
/// — the legacy in-system `tokio::spawn`s in
/// [`ConversationMemorySystem::add_incremental_update_internal`] still
/// run as a safety net, so a full queue degrades to "do the work on a
/// raw spawn anyway" rather than dropping it.
fn submit_derived_work(
    pipeline: &Pipeline,
    session_id: Uuid,
    entry_id: Uuid,
    text: &str,
    update: ContextUpdate,
) {
    if let Err(e) = pipeline.submit_embedding(EmbeddingWorkItem {
        session_id,
        entry_id,
        text: text.to_string(),
    }) {
        log_pipeline_submit("embedding", session_id, entry_id, e);
    }
    if let Err(e) = pipeline.submit_graph(GraphWorkItem::ApplyUpdate {
        session_id,
        update,
    }) {
        log_pipeline_submit("graph", session_id, entry_id, e);
    }
    if let Err(e) = pipeline.submit_summary(SummaryWorkItem { session_id }) {
        log_pipeline_submit("summary", session_id, entry_id, e);
    }
}

fn log_pipeline_submit(queue: &str, session_id: Uuid, entry_id: Uuid, err: PipelineError) {
    warn!(
        queue,
        %session_id,
        %entry_id,
        error = %err,
        "pipeline submission failed (non-fatal — legacy in-system spawn covers the work)"
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory_system::SystemConfig;
    use chrono::Utc;
    use post_cortex_core::core::context_update::{
        EntityData, EntityType, RelationType, UpdateContent, UpdateType,
    };

    fn entity(name: &str) -> EntityData {
        EntityData {
            name: name.to_string(),
            entity_type: EntityType::Concept,
            first_mentioned: Utc::now(),
            last_mentioned: Utc::now(),
            mention_count: 1,
            importance_score: 1.0,
            description: None,
        }
    }

    fn relation(from: &str, to: &str) -> EntityRelationship {
        EntityRelationship {
            from_entity: from.to_string(),
            to_entity: to.to_string(),
            relation_type: RelationType::RelatedTo,
            context: "test relation".to_string(),
        }
    }

    fn good_request() -> UpdateContextRequest {
        UpdateContextRequest {
            session_id: Uuid::new_v4(),
            interaction_type: UpdateType::ConceptDefined,
            content: UpdateContent {
                title: "Some concept".into(),
                description: "A short definition".into(),
                details: vec![],
                examples: vec![],
                implications: vec![],
            },
            entities: vec![entity("Foo"), entity("Bar")],
            relations: vec![relation("Foo", "Bar")],
            code_reference: None,
        }
    }

    #[tokio::test]
    async fn trait_is_object_safe() {
        fn _accept_dyn(_svc: Arc<dyn PostCortexService>) {}
    }

    #[test]
    fn validation_rejects_empty_title_and_description() {
        let mut req = good_request();
        req.content.title = String::new();
        req.content.description = String::new();
        let err = validate_update_request(&req).unwrap_err();
        assert!(matches!(err, SystemError::InvalidArgument(ref m) if m.contains("both empty")));
    }

    #[test]
    fn validation_rejects_empty_entities() {
        let mut req = good_request();
        req.entities = vec![];
        assert!(matches!(
            validate_update_request(&req),
            Err(SystemError::InvalidArgument(_))
        ));
    }

    #[test]
    fn validation_rejects_empty_relations() {
        let mut req = good_request();
        req.relations = vec![];
        assert!(matches!(
            validate_update_request(&req),
            Err(SystemError::InvalidArgument(_))
        ));
    }

    #[test]
    fn validation_rejects_self_relation() {
        let mut req = good_request();
        req.relations = vec![relation("Foo", "Foo")];
        assert!(matches!(
            validate_update_request(&req),
            Err(SystemError::InvalidArgument(ref m)) if m.contains("self-relations")
        ));
    }

    #[test]
    fn validation_rejects_dangling_relation_endpoint() {
        let mut req = good_request();
        req.relations = vec![relation("Foo", "Ghost")];
        assert!(matches!(
            validate_update_request(&req),
            Err(SystemError::InvalidArgument(ref m)) if m.contains("Ghost")
        ));
    }

    #[test]
    fn validation_rejects_empty_relation_context() {
        let mut req = good_request();
        req.relations[0].context = "   ".into();
        assert!(matches!(
            validate_update_request(&req),
            Err(SystemError::InvalidArgument(ref m)) if m.contains("context must not be empty")
        ));
    }

    #[test]
    fn validation_accepts_good_request() {
        assert!(validate_update_request(&good_request()).is_ok());
    }

    #[test]
    fn description_joins_title_and_body() {
        let req = good_request();
        assert_eq!(build_description(&req), "Some concept\nA short definition");
    }

    #[test]
    fn description_falls_back_when_one_side_empty() {
        let mut req = good_request();
        req.content.description = String::new();
        assert_eq!(build_description(&req), "Some concept");

        req.content.title = String::new();
        req.content.description = "Body only".into();
        assert_eq!(build_description(&req), "Body only");
    }

    #[test]
    fn metadata_keeps_creates_entities_in_sync_with_typed_entities() {
        let req = good_request();
        let update = build_context_update(&req);
        let meta = serde_json::to_value(&update).unwrap();
        let names = meta["creates_entities"].as_array().unwrap();
        let typed = meta["typed_entities"].as_array().unwrap();
        assert_eq!(names.len(), typed.len());
        assert_eq!(names.len(), req.entities.len());
        for (i, e) in req.entities.iter().enumerate() {
            assert_eq!(names[i].as_str().unwrap(), e.name);
            assert_eq!(typed[i]["name"].as_str().unwrap(), e.name);
        }
    }

    async fn make_service(suffix: &str) -> (MemoryServiceImpl, String) {
        let test_dir = format!(
            "./test_data_memservice_{}_{}",
            suffix,
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        );
        std::fs::create_dir_all(&test_dir).unwrap();
        let config = SystemConfig {
            data_directory: test_dir.clone(),
            ..Default::default()
        };
        let system = Arc::new(ConversationMemorySystem::new(config).await.unwrap());
        (MemoryServiceImpl::new(system), test_dir)
    }

    #[tokio::test]
    async fn health_returns_ok_status() {
        let (svc, test_dir) = make_service("health").await;
        let report = svc.health().await.unwrap();
        assert!(report.status == "ok" || report.status == "degraded");
        std::fs::remove_dir_all(&test_dir).unwrap();
    }

    #[tokio::test]
    async fn update_context_persists_and_returns_entry_id() {
        let (svc, test_dir) = make_service("update").await;
        let session_id = svc.inner().create_session(None, None).await.unwrap();

        let mut req = good_request();
        req.session_id = session_id;
        let resp = svc.update_context(req).await.unwrap();

        assert_eq!(resp.session_id, session_id);
        assert!(resp.durable);
        std::fs::remove_dir_all(&test_dir).unwrap();
    }

    #[tokio::test]
    async fn update_context_rejects_invalid_input_with_invalid_argument() {
        let (svc, test_dir) = make_service("invalid").await;
        let session_id = svc.inner().create_session(None, None).await.unwrap();

        let mut req = good_request();
        req.session_id = session_id;
        req.entities = vec![]; // violates "entities must not be empty"
        let err = svc.update_context(req).await.unwrap_err();
        assert!(matches!(err, SystemError::InvalidArgument(_)));
        std::fs::remove_dir_all(&test_dir).unwrap();
    }

    #[tokio::test]
    async fn bulk_update_context_persists_every_item() {
        let (svc, test_dir) = make_service("bulk").await;
        let session_id = svc.inner().create_session(None, None).await.unwrap();

        let mut a = good_request();
        a.session_id = session_id;
        a.content.title = "First".into();
        let mut b = good_request();
        b.session_id = session_id;
        b.content.title = "Second".into();

        let resp = svc
            .bulk_update_context(BulkUpdateContextRequest {
                session_id,
                updates: vec![a, b],
            })
            .await
            .unwrap();
        assert_eq!(resp.entry_ids.len(), 2);
        assert!(resp.durable);
        std::fs::remove_dir_all(&test_dir).unwrap();
    }

    #[tokio::test]
    async fn update_context_returns_fast_then_pipeline_drains_in_background() {
        // The hot path persists durably; embedding + graph + summary land
        // on background workers. Verify that, on a warm path
        // (post-vectorizer-init), update_context returns quickly and the
        // pipeline backlog drains shortly after.
        //
        // The first call still pays the model-download cost via the
        // legacy in-system `spawn_background_vectorization` (kept as a
        // safety net while direct callers migrate to the pipeline path).
        // Phase 7+ removes that path entirely.
        let (svc, test_dir) = make_service("nonblocking").await;
        let session_id = svc.inner().create_session(None, None).await.unwrap();

        // Warm-up call — absorbs first-time model load.
        let mut warmup = good_request();
        warmup.session_id = session_id;
        warmup.content.title = "warmup".into();
        let _ = svc.update_context(warmup).await.unwrap();

        // Wait for any pending background work to settle.
        for _ in 0..100 {
            if svc.pipeline().backlog() == 0 {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }

        // Measured call — should be fast.
        let mut req = good_request();
        req.session_id = session_id;
        let start = std::time::Instant::now();
        let resp = svc.update_context(req).await.unwrap();
        let write_latency = start.elapsed();

        assert!(resp.durable);
        assert!(
            write_latency.as_millis() < 250,
            "update_context took {write_latency:?} on warm path — should be <250ms"
        );

        // Workers drain the queues asynchronously.
        for _ in 0..100 {
            if svc.pipeline().backlog() == 0 {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
        assert_eq!(
            svc.pipeline().backlog(),
            0,
            "pipeline backlog should drain within 2s"
        );

        std::fs::remove_dir_all(&test_dir).unwrap();
    }

    #[tokio::test]
    async fn bulk_update_context_rejects_mismatched_session() {
        let (svc, test_dir) = make_service("bulkmis").await;
        let session_id = svc.inner().create_session(None, None).await.unwrap();

        let mut item = good_request();
        item.session_id = Uuid::new_v4(); // intentionally different
        let err = svc
            .bulk_update_context(BulkUpdateContextRequest {
                session_id,
                updates: vec![item],
            })
            .await
            .unwrap_err();
        assert!(matches!(err, SystemError::InvalidArgument(ref m) if m.contains("does not match")));
        std::fs::remove_dir_all(&test_dir).unwrap();
    }
}