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
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
//! Knowledge / RAG (document-corpus) wiring for the agentic-worker runtime.
//!
//! Defines a local [`Knowledge`] seam — ingest pre-chunked document text,
//! hybrid-retrieve ranked chunks — kept deliberately distinct from
//! [`crate::long_term`] (D4): a read-mostly document corpus with auto
//! pre-retrieval, not evolving conversational memory, and from the key-value
//! [`crate::memory::MemoryProvider`] short-term tier.
//!
//! The trait + DTOs mirror the W3 `greentic-dw-knowledge` contract shape exactly,
//! but are defined locally rather than re-exported: the W3 trait crate pulls
//! `greentic-dw-providers-common` (catalog/pack machinery), which would drag a
//! conflicting `greentic-types` and the whole provider stack into this runtime
//! crate. Keeping a thin local seam lets the runtime stay free of graph/provider
//! weight; the concrete Chronicle-backed provider is adapted to this trait at the
//! runner-host edge (W4 4d). When W3 ships a lightweight trait-only crate (as the
//! memory tier does via `greentic-dw-memory-long-term`), this can re-export it.

use std::sync::Arc;

use crate::AgentConfig;
use crate::tenant::TenantContext;
use greentic_types::{EnvId, TenantCtx, TenantId};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// A pre-chunked unit of document text to ingest into the knowledge corpus.
/// Mirrors `greentic_dw_knowledge::KnowledgeChunk`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct KnowledgeChunk {
    pub doc_id: String,
    pub chunk_index: usize,
    pub text: String,
    #[serde(default)]
    pub metadata: serde_json::Map<String, serde_json::Value>,
    /// Optional pre-computed embedding vector for this chunk. When present,
    /// backends should use this vector directly instead of embedding `text`;
    /// `None` preserves the existing backend-computed-embedding behavior.
    #[serde(default)]
    pub embedding: Option<Vec<f32>>,
}

/// Outcome of an ingest: the backend-assigned id for each stored chunk.
/// Mirrors `greentic_dw_knowledge::IngestOutcome`.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct IngestOutcome {
    pub chunk_ids: Vec<String>,
}

/// A retrieval query over the tenant's knowledge corpus.
/// Mirrors `greentic_dw_knowledge::KnowledgeQuery`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct KnowledgeQuery {
    pub query: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<usize>,
}

/// A ranked chunk returned from retrieval.
/// Mirrors `greentic_dw_knowledge::RetrievedChunk`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RetrievedChunk {
    pub text: String,
    pub score: f64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub chunk_index: Option<usize>,
    #[serde(default)]
    pub metadata: serde_json::Map<String, serde_json::Value>,
}

/// Errors returned by knowledge operations.
/// Mirrors `greentic_dw_knowledge::KnowledgeError`.
#[derive(Debug, Error)]
pub enum KnowledgeError {
    /// The underlying storage or RAG backend returned an error.
    #[error("knowledge backend error: {0}")]
    Backend(String),
    /// The tenant identifier is invalid or unknown.
    #[error("invalid tenant: {0}")]
    InvalidTenant(String),
    /// No knowledge backend has been configured.
    #[error("knowledge provider not configured")]
    NotConfigured,
}

/// Convenience result alias for knowledge operations.
pub type KnowledgeResult<T> = Result<T, KnowledgeError>;

/// Contract implemented by knowledge (document-RAG) backends. Object-safe so the
/// concrete backend can be injected as `Arc<dyn Knowledge>` at the runner-host
/// edge. Mirrors `greentic_dw_knowledge::Knowledge`.
#[async_trait::async_trait]
pub trait Knowledge: Send + Sync {
    /// Ingest a batch of pre-chunked document text into the tenant's corpus.
    async fn ingest(
        &self,
        tenant: &TenantCtx,
        chunks: Vec<KnowledgeChunk>,
    ) -> KnowledgeResult<IngestOutcome>;

    /// Retrieve chunks relevant to `query`, ranked by relevance descending and
    /// bounded by `query.limit`.
    async fn search(
        &self,
        tenant: &TenantCtx,
        query: KnowledgeQuery,
    ) -> KnowledgeResult<Vec<RetrievedChunk>>;

    /// Retrieval with the agent's per-turn knowledge binding in hand.
    ///
    /// [`Self::search`] mirrors the `greentic-dw-knowledge` contract and must
    /// keep mirroring it, so the provider binding cannot be carried inside
    /// [`KnowledgeQuery`]. But a backend whose target is chosen PER WORKER —
    /// "delegate retrieval to extension X's tool Y" — cannot read its own ids
    /// from a runtime-level field: [`crate::AgentRuntime::knowledge`] is set
    /// once when the runtime is built, while the binding arrives per turn in
    /// [`AgentConfig`]. The out-of-process serve path serves many agents from
    /// one runtime, so mounting per-runtime from config would hand one worker
    /// another worker's provider.
    ///
    /// The default delegates to [`Self::search`], so a backend that ignores the
    /// binding (an env-configured Chronicle corpus, say) needs no change.
    async fn search_bound(
        &self,
        tenant: &TenantCtx,
        query: KnowledgeQuery,
        _binding: Option<&crate::config::MemoryProviderRef>,
    ) -> KnowledgeResult<Vec<RetrievedChunk>> {
        self.search(tenant, query).await
    }

    /// The backend this one delegates to, when it is a WRAPPER rather than a
    /// leaf. `None` for every backend that retrieves by itself.
    ///
    /// [`crate::AgentRuntime::with_knowledge`] replaces the mounted backend, so
    /// a host that mounts two must have the second wrap the first. Once one of
    /// those mounts is unconditional — as the runner-host's extension adapter
    /// is, because which extension to invoke is a per-turn decision it cannot
    /// make at build time — [`crate::AgentRuntime::has_knowledge`] is true on
    /// that path whether or not a corpus backend was ever mounted. It therefore
    /// stops being able to answer "did the corpus mount run", which is a
    /// question a host regression guard has to keep asking: a corpus mount that
    /// silently stopped running is what made the model hallucinate instead of
    /// retrieving. This answers it.
    fn wrapped_backend(&self) -> Option<Arc<dyn Knowledge>> {
        None
    }

    /// A stable identifier for this backend IMPLEMENTATION, so a host walking a
    /// wrapper chain can tell one layer from another.
    ///
    /// Never for dispatch — nothing in this crate reads it. It exists because
    /// [`Self::wrapped_backend`] returning `Some` says only "something is
    /// underneath", which a second delegating wrapper satisfies exactly as well
    /// as a corpus backend does; a host asking "did my corpus mount actually
    /// run" needs to tell those apart.
    fn backend_id(&self) -> &'static str {
        "unidentified"
    }
}

/// Convert the runtime's [`TenantContext`] into the `greentic-types`
/// [`TenantCtx`] expected by [`Knowledge`]. Validation failures (an id that
/// doesn't satisfy the shared tenant/env format) map to
/// [`KnowledgeError::InvalidTenant`] rather than panicking.
pub(crate) fn to_types_tenant(ctx: &TenantContext) -> Result<TenantCtx, KnowledgeError> {
    let env = EnvId::try_from(ctx.env_id.as_str())
        .map_err(|e| KnowledgeError::InvalidTenant(format!("env_id '{}': {e}", ctx.env_id)))?;
    let tenant = TenantId::try_from(ctx.tenant_id.as_str()).map_err(|e| {
        KnowledgeError::InvalidTenant(format!("tenant_id '{}': {e}", ctx.tenant_id))
    })?;
    Ok(TenantCtx::new(env, tenant))
}

/// Number of chunks auto-retrieved and injected each turn, read from the agent's
/// knowledge binding (falls back to the default, clamped to at least 1 so a stray
/// `0` never disables retrieval silently).
pub(crate) fn auto_top_k(config: &AgentConfig) -> usize {
    config
        .knowledge
        .as_ref()
        .map(|k| k.top_k)
        .unwrap_or_else(crate::config::default_knowledge_top_k)
        .max(1)
}

/// Whether the knowledge tier is active for this turn: a backend is wired AND the
/// agent's config carries an enabled knowledge provider binding.
pub(crate) fn knowledge_active(has_provider: bool, config: &AgentConfig) -> bool {
    has_provider
        && config
            .knowledge
            .as_ref()
            .and_then(|k| k.knowledge.as_ref())
            .is_some()
}

/// Build the system prompt for a turn: the base prompt followed by a delimited
/// `<knowledge>` block listing the retrieved chunks. Returns the base prompt
/// unchanged when there are no chunks (no empty block).
pub(crate) fn augment_system_prompt(base: &str, chunks: &[RetrievedChunk]) -> String {
    if chunks.is_empty() {
        return base.to_string();
    }
    let mut out = String::with_capacity(base.len() + 128 * chunks.len());
    out.push_str(base);
    out.push_str("\n\n<knowledge>\nRelevant passages retrieved from the agent's knowledge base:\n");
    for c in chunks {
        out.push_str("- ");
        out.push_str(c.text.trim());
        out.push('\n');
    }
    out.push_str("</knowledge>");
    out
}

/// Surface an auto knowledge retrieval as a trace step, so the UI can show which
/// corpus chunks were pulled into context — doc id, chunk index, score, and text.
///
/// This is the LIVE trace, emitted through the tool-call observer seam so the
/// test-chat UI can stream it. It is not how the retrieval is recorded: the
/// loop also pushes an `AgentStep::KnowledgeRetrieval` onto `AgentOutput.trail`
/// (#770), a distinct kind rather than a `ToolCall`, because retrieval is
/// automatic pre-context and must not be counted as a model-invoked tool. A
/// trail consumer that also listens to this observer sees the retrieval twice
/// and must pick one. A no-op when nothing was retrieved (no empty step).
///
/// The synthetic `call_id` is per-call so the UI pairs this result with its own
/// call and never a neighbouring tool's. `doc`/`index` ride through as JSON `null`
/// when the backend did not supply them (`RetrievedChunk`'s `Option` fields).
pub(crate) fn emit_retrieval_trace(
    observer: &dyn crate::StepObserver,
    query: &str,
    chunks: &[RetrievedChunk],
) {
    if chunks.is_empty() {
        return;
    }
    const NAME: &str = "search_knowledge";
    let call_id = uuid::Uuid::new_v4().to_string();
    observer.on_tool_call(NAME, &call_id, &serde_json::json!({ "query": query }));

    let hits: Vec<serde_json::Value> = chunks
        .iter()
        .map(|c| {
            serde_json::json!({
                "doc": c.doc_id,
                "index": c.chunk_index,
                "score": c.score,
                "text": c.text,
            })
        })
        .collect();
    observer.on_tool_result(
        NAME,
        &call_id,
        &serde_json::json!({ "chunks": hits, "count": chunks.len() }),
    );
}

/// Surface a FAILED auto knowledge retrieval as a trace step.
///
/// The counterpart to [`emit_retrieval_trace`], and the reason it exists: a
/// retrieval that errors degrades to no injection, and until this existed that
/// degrade was indistinguishable from "the corpus held nothing relevant" —
/// same empty prompt, same confident answer, no trace step either way. The
/// operator watching test-chat could not tell a dead backend from an empty one.
///
/// Emitted through the same observer seam and with the same synthetic
/// `call_id` discipline. The result payload carries `"error"` rather than
/// `"chunks"`, which is what makes the two cases distinguishable downstream.
pub(crate) fn emit_retrieval_failure_trace(
    observer: &dyn crate::StepObserver,
    query: &str,
    error: &KnowledgeError,
) {
    const NAME: &str = "search_knowledge";
    let call_id = uuid::Uuid::new_v4().to_string();
    observer.on_tool_call(NAME, &call_id, &serde_json::json!({ "query": query }));
    observer.on_tool_result(
        NAME,
        &call_id,
        &serde_json::json!({ "error": error.to_string(), "count": 0 }),
    );
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use std::sync::Arc;

    #[test]
    fn to_types_tenant_maps_valid_ids() {
        let ctx = TenantContext::new("acme", "dev");
        let resolved = to_types_tenant(&ctx).expect("valid tenant converts");
        assert_eq!(resolved.tenant.as_str(), "acme");
        assert_eq!(resolved.env.as_str(), "dev");
    }

    #[test]
    fn to_types_tenant_rejects_empty_tenant() {
        let ctx = TenantContext::new("", "dev");
        let err = to_types_tenant(&ctx).expect_err("empty tenant id is invalid");
        assert!(matches!(err, KnowledgeError::InvalidTenant(_)));
    }

    fn cfg_with_knowledge(top_k: Option<usize>, with_binding: bool) -> AgentConfig {
        use crate::config::{KnowledgeSettings, MemoryProviderRef};
        let binding = with_binding.then(|| MemoryProviderRef {
            provider: "provider.knowledge.chronicle".into(),
            capability: "cap://dw.knowledge".into(),
            params: serde_json::Map::new(),
            credential_ref: None,
        });
        AgentConfig {
            agent_id: "a".into(),
            system_prompt: "s".into(),
            tools: vec![],
            llm: crate::LlmProviderRef {
                provider: "m".into(),
                model: "m".into(),
                credential_ref: None,
            },
            limits: crate::AgentLimits::default(),
            memory: None,
            knowledge: Some(KnowledgeSettings {
                knowledge: binding,
                embedding: None,
                top_k: top_k.unwrap_or_else(crate::config::default_knowledge_top_k),
            }),
            guardrails: vec![],
            conversational: false,
            opening_message: None,
            on_text_while_parked: Default::default(),
        }
    }

    #[test]
    fn knowledge_active_requires_provider_and_enabled_binding() {
        let cfg = cfg_with_knowledge(None, true);
        assert!(knowledge_active(true, &cfg));
        assert!(!knowledge_active(false, &cfg));

        // Binding present but no knowledge provider → inactive.
        let cfg_no_binding = cfg_with_knowledge(None, false);
        assert!(!knowledge_active(true, &cfg_no_binding));

        // No knowledge settings at all → inactive.
        let mut bare = cfg_with_knowledge(None, true);
        bare.knowledge = None;
        assert!(!knowledge_active(true, &bare));
    }

    #[test]
    fn auto_top_k_uses_config_then_default_clamped() {
        assert_eq!(auto_top_k(&cfg_with_knowledge(Some(3), true)), 3);
        assert_eq!(auto_top_k(&cfg_with_knowledge(None, true)), 5);
        // A stray 0 clamps up to 1 rather than disabling retrieval.
        assert_eq!(auto_top_k(&cfg_with_knowledge(Some(0), true)), 1);
        let mut bare = cfg_with_knowledge(None, true);
        bare.knowledge = None;
        assert_eq!(auto_top_k(&bare), 5);
    }

    fn chunk(text: &str, score: f64) -> RetrievedChunk {
        RetrievedChunk {
            text: text.into(),
            score,
            doc_id: None,
            chunk_index: None,
            metadata: serde_json::Map::new(),
        }
    }

    #[test]
    fn augment_with_chunks_wraps_a_block() {
        let chunks = vec![
            chunk("Refunds are processed within 5 business days.", 0.9),
            chunk("Premium plans include priority support.", 0.7),
        ];
        let out = augment_system_prompt("base prompt", &chunks);
        assert!(out.starts_with("base prompt"));
        assert!(out.contains("<knowledge>"));
        assert!(out.contains("</knowledge>"));
        assert!(out.contains("Refunds are processed within 5 business days."));
        assert!(out.contains("Premium plans include priority support."));
    }

    #[test]
    fn augment_with_no_chunks_returns_base_unchanged() {
        let out = augment_system_prompt("base prompt", &[]);
        assert_eq!(out, "base prompt");
    }

    // A minimal in-crate `Knowledge` to prove the trait + tenant conversion line
    // up end-to-end without a graph backend (the real backend is Chronicle doc-RAG,
    // injected at the runner-host edge and exercised in integration tests).
    struct StubKnowledge;

    #[async_trait::async_trait]
    impl Knowledge for StubKnowledge {
        async fn ingest(
            &self,
            _tenant: &TenantCtx,
            chunks: Vec<KnowledgeChunk>,
        ) -> KnowledgeResult<IngestOutcome> {
            Ok(IngestOutcome {
                chunk_ids: chunks
                    .iter()
                    .map(|c| format!("{}#{}", c.doc_id, c.chunk_index))
                    .collect(),
            })
        }

        async fn search(
            &self,
            _tenant: &TenantCtx,
            query: KnowledgeQuery,
        ) -> KnowledgeResult<Vec<RetrievedChunk>> {
            Ok(vec![chunk(&format!("retrieved for: {}", query.query), 1.0)])
        }
    }

    #[tokio::test]
    async fn trait_object_drives_ingest_and_search_through_converted_tenant() {
        let kb: Arc<dyn Knowledge> = Arc::new(StubKnowledge);
        let ctx = to_types_tenant(&TenantContext::new("acme", "dev")).unwrap();

        let outcome = kb
            .ingest(
                &ctx,
                vec![KnowledgeChunk {
                    doc_id: "faq".into(),
                    chunk_index: 0,
                    text: "Refunds within 5 days.".into(),
                    metadata: serde_json::Map::new(),
                    embedding: None,
                }],
            )
            .await
            .unwrap();
        assert_eq!(outcome.chunk_ids, vec!["faq#0".to_string()]);

        let hits = kb
            .search(
                &ctx,
                KnowledgeQuery {
                    query: "refund policy".into(),
                    limit: Some(3),
                },
            )
            .await
            .unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].text, "retrieved for: refund policy");
    }

    #[derive(Default)]
    struct RecordingObserver {
        // (event, tool_name, payload) per observer call.
        calls: std::sync::Mutex<Vec<(&'static str, String, serde_json::Value)>>,
    }
    impl crate::StepObserver for RecordingObserver {
        fn on_tool_call(&self, name: &str, _call_id: &str, args: &serde_json::Value) {
            self.calls
                .lock()
                .unwrap()
                .push(("call", name.to_string(), args.clone()));
        }
        fn on_tool_result(&self, name: &str, _call_id: &str, result: &serde_json::Value) {
            self.calls
                .lock()
                .unwrap()
                .push(("result", name.to_string(), result.clone()));
        }
    }

    #[test]
    fn emit_retrieval_trace_reports_chunk_fields_as_a_tool_step() {
        let rec = RecordingObserver::default();
        let chunks = vec![RetrievedChunk {
            text: "Agentic worker adalah komponen...".into(),
            score: 0.89,
            doc_id: Some("greentic.pdf".into()),
            chunk_index: Some(3),
            metadata: serde_json::Map::new(),
        }];
        emit_retrieval_trace(&rec, "Apa itu agentic?", &chunks);

        let calls = rec.calls.lock().unwrap();
        assert_eq!(calls.len(), 2, "one on_tool_call + one on_tool_result");
        assert_eq!(calls[0].0, "call");
        assert_eq!(calls[0].1, "search_knowledge");
        assert_eq!(calls[0].2["query"], "Apa itu agentic?");

        assert_eq!(calls[1].0, "result");
        assert_eq!(calls[1].2["count"], 1);
        let hit = &calls[1].2["chunks"][0];
        assert_eq!(hit["doc"], "greentic.pdf");
        assert_eq!(hit["index"], 3);
        assert_eq!(hit["score"], 0.89);
        assert_eq!(hit["text"], "Agentic worker adalah komponen...");
    }

    /// Backends that leave `doc_id`/`chunk_index` unset must not break the step —
    /// they ride through as JSON `null`, which the UI tolerates.
    #[test]
    fn emit_retrieval_trace_carries_null_for_missing_doc_and_index() {
        let rec = RecordingObserver::default();
        emit_retrieval_trace(&rec, "q", &[chunk("no metadata", 0.5)]);

        let calls = rec.calls.lock().unwrap();
        let hit = &calls[1].2["chunks"][0];
        assert!(hit["doc"].is_null());
        assert!(hit["index"].is_null());
        assert_eq!(hit["score"], 0.5);
    }

    #[test]
    fn emit_retrieval_trace_is_noop_without_chunks() {
        let rec = RecordingObserver::default();
        emit_retrieval_trace(&rec, "q", &[]);
        assert!(
            rec.calls.lock().unwrap().is_empty(),
            "no chunks retrieved => no trace step at all"
        );
    }

    /// The whole point of the failure trace: an operator watching the live
    /// trace must be able to tell "the corpus held nothing relevant" from "the
    /// backend did not answer". Those were the same empty prompt before.
    #[test]
    fn a_failed_retrieval_is_distinguishable_from_an_empty_one() {
        let empty = RecordingObserver::default();
        emit_retrieval_trace(&empty, "q", &[]);
        assert!(empty.calls.lock().unwrap().is_empty());

        let failed = RecordingObserver::default();
        emit_retrieval_failure_trace(
            &failed,
            "q",
            &KnowledgeError::Backend("service refused".into()),
        );
        let calls = failed.calls.lock().unwrap();
        assert_eq!(calls.len(), 2, "one on_tool_call + one on_tool_result");
        assert_eq!(calls[0].1, "search_knowledge");
        assert_eq!(calls[0].2["query"], "q");
        assert_eq!(calls[1].2["count"], 0);
        assert!(
            calls[1].2["error"]
                .as_str()
                .is_some_and(|e| e.contains("service refused")),
            "the result must name the failure, not merely report zero chunks: {:?}",
            calls[1].2
        );
        assert!(
            calls[1].2.get("chunks").is_none(),
            "a failure must not look like a successful retrieval of nothing"
        );
    }

    /// A backend that ignores the binding — every backend that existed before
    /// this seam — must keep working through the default method.
    #[tokio::test]
    async fn search_bound_defaults_to_search_for_a_binding_agnostic_backend() {
        let kb: Arc<dyn Knowledge> = Arc::new(StubKnowledge);
        let ctx = to_types_tenant(&TenantContext::new("acme", "dev")).unwrap();
        let binding = crate::config::MemoryProviderRef {
            provider: "provider.knowledge.chronicle".into(),
            capability: "cap://dw.knowledge".into(),
            params: serde_json::Map::new(),
            credential_ref: None,
        };
        let hits = kb
            .search_bound(
                &ctx,
                KnowledgeQuery {
                    query: "refund policy".into(),
                    limit: Some(3),
                },
                Some(&binding),
            )
            .await
            .unwrap();
        assert_eq!(hits[0].text, "retrieved for: refund policy");
    }

    /// #770: the serialised shape of `AgentStep::KnowledgeRetrieval` is a
    /// cross-repo contract — greentic-start's `agent_provenance` reads
    /// `kind == "knowledge_retrieval"` and each chunk's `text`/`score`/
    /// `doc_id`/`metadata`. Pin it, and pin that it round-trips.
    #[test]
    fn knowledge_retrieval_step_serialises_with_a_stable_shape() {
        let mut metadata = serde_json::Map::new();
        metadata.insert("title".into(), serde_json::json!("Refund policy"));
        let step = crate::AgentStep::KnowledgeRetrieval {
            chunks: vec![RetrievedChunk {
                text: "Refunds take 5 days.".into(),
                score: 0.5,
                doc_id: Some("doc-1".into()),
                chunk_index: Some(2),
                metadata,
            }],
        };
        let value = serde_json::to_value(&step).unwrap();
        assert_eq!(
            value,
            serde_json::json!({
                "kind": "knowledge_retrieval",
                "chunks": [{
                    "text": "Refunds take 5 days.",
                    "score": 0.5,
                    "doc_id": "doc-1",
                    "chunk_index": 2,
                    "metadata": {"title": "Refund policy"}
                }]
            })
        );
        let back: crate::AgentStep = serde_json::from_value(value).unwrap();
        assert!(matches!(
            back,
            crate::AgentStep::KnowledgeRetrieval { ref chunks } if chunks.len() == 1
        ));
    }
}