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
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
750
751
752
753
//! Greentic Agentic Worker Runtime — library crate.
//!
//! See `docs/superpowers/specs/2026-05-22-enterprise-aw-runtime-design.md`
//! for the full design spec. This crate exposes the [`AgentRuntime`] entry
//! point and the trait surface (`AgentStateStore`, `ConfigProvider`,
//! `LlmBackend`, `Telemetry`) that the production runner-host and the
//! designer playground both consume.
//!
//! The [`graph`] module provides durable multi-agent graph execution
//! (`GraphExecutor`, `GraphConfig`, `CheckpointStore`); see
//! `docs/superpowers/specs/2026-06-06-runtime-agent-graph-execution-design.md`.

#![deny(unsafe_code)]
#![warn(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

pub mod a2a_source;
pub mod artifact_door;
pub mod artifact_reader;
pub mod attachment_guard;
pub mod attachments;
pub mod attachments_materialize;
mod background;
pub mod billing;
pub mod component_source;
pub mod config;
pub mod config_provider;
pub mod cost;
pub mod dispatch_ledger;
pub mod dw;
pub mod end_conversation;
pub mod error;
pub mod flow_source;
mod flow_suspend;
pub mod graph;
pub mod guardrail;
pub mod guardrail_provider;
pub mod http_provider;
pub mod knowledge;
pub mod kv;
pub mod layered_provider;
pub mod llm;
pub mod llm_credential;
pub mod llm_extension;
#[cfg(feature = "greentic-llm-backend")]
pub mod llm_greentic;
pub mod llm_openai;
mod lock_keepalive;
pub mod long_term;
pub mod r#loop;
pub mod manifest_provider;
pub mod manifest_tools;
pub mod mcp_local;
pub mod mcp_scope;
pub mod mcp_secrets;
pub mod mcp_source;
pub mod mcp_store_pull;
pub mod memory;
pub mod oauth;
pub mod outbound_guard;
pub mod playbook_source;
pub mod run_trace;
pub mod scoped_secrets;
pub mod share_policy;
pub mod short_term;
pub mod sorla_source;
pub mod state;
pub mod state_kv;
pub mod state_redis;
pub mod telemetry;
pub mod tenant;
pub mod tool_call_frame;
pub mod tool_session;
pub mod tool_wire_name;
pub mod tools;
pub mod url_redact;
pub mod user_ledger;

#[cfg(feature = "test-mock")]
pub mod mock;

#[cfg(feature = "serve")]
pub mod serve;

pub use a2a_source::{A2aRoute, A2aToolCatalog, A2aToolEntry, A2aToolSource};
pub use artifact_door::{DOOR_ATTEMPTS, DOOR_CONCURRENCY, DoorReply, DoorRetry, DoorSendError};
pub use artifact_reader::{
    ArtifactBytes, ArtifactClientError, ArtifactError, ArtifactReader, HttpArtifactReader, door_url,
};
pub use attachments::{AttachmentKind, AttachmentRef, is_artifact_ref};
pub use component_source::{
    ComponentInvoker, ComponentOperation, ComponentToolCatalog, ComponentToolEntry,
    ComponentToolSource,
};
pub use config::{
    AgentConfig, AgentLimits, LlmProviderRef, MemoryProviderRef, MemorySettings, ParkedTextPolicy,
    ToolRef,
};
pub use config_provider::{CachingConfigProvider, ConfigProvider, InMemoryConfigProvider};
#[cfg(feature = "test-mock")]
pub use cost::MockTokenMeter;
pub use cost::{KvTokenMeter, RedisTokenMeter, TokenMeter};
pub use dispatch_ledger::{DispatchLedger, NoopDispatchLedger, RedisDispatchLedger};
pub use error::{AgentError, ConfigError, LlmError, MemoryError, StateError, TerminationReason};
pub use flow_source::{
    FlowInvokeOutcome, FlowInvoker, FlowOperation, FlowToolCatalog, FlowToolEntry, FlowToolSource,
};
pub use graph::http_provider::{CachingGraphProvider, HttpGraphProvider};
pub use http_provider::HttpConfigProvider;
#[cfg(feature = "state-disk")]
pub use kv::RedbKv;
pub use kv::{AwKv, MemoryKv};
pub use layered_provider::LayeredConfigProvider;
pub use llm::{LlmBackend, LlmRequest, LlmResponse, RetryingLlmBackend};
pub use llm_extension::{
    BridgeCredential, ExtensionLlmBackend, LlmExtensionInvoker, RuntimeInvoker,
};
#[cfg(feature = "greentic-llm-backend")]
pub use llm_greentic::GreenticLlmBackend;
pub use llm_openai::{OpenAiLlmBackend, encode_tool_name, split_tool_name};
pub use long_term::{
    EpisodeIngest, EpisodeSource, IngestOutcome, LongTermMemory, LongTermMemoryError, RecallQuery,
    RecalledFact,
};
pub use manifest_provider::ManifestToolOverlayProvider;
pub use mcp_source::{
    MCP_ROLE_AGENTIC_WORKER, MCP_ROLE_FLOW_EDITOR, McpCallerIdentity, McpPackRoute, McpRoute,
    McpToolCatalog, McpToolEntry, McpToolSource, dispatch_route,
};
pub use memory::{InMemoryMemoryProvider, MemoryProvider, MemoryQuery, MemoryRecord};
pub use playbook_source::{
    PlaybookLlmCapability, PlaybookLlmRequirement, PlaybookLlmTier, PlaybookOperation,
    PlaybookSource, PlaybookToolCatalog, PlaybookToolEntry, PlaybookToolSource, PlaybookTurnFn,
    PlaybookTurnRequest, PlaybookTurnResult,
};
/// The `reqwest` the door helpers ([`DoorRetry`]) speak. A door client in
/// another crate builds its `reqwest::Client` from this one, so the two never
/// disagree on the version.
pub use reqwest as door_reqwest;
pub use run_trace::{RunContext, RunTrace, StepId, ToolOutcome};
pub use share_policy::{BindingModes, ShareMode, SharePolicy};
pub use sorla_source::{
    SorlaToolCatalog, SorlaToolEntry, SorlaToolSource, SorxInvoker, SorxOperation,
};
pub use state::{AgentStateStore, ChatMessage, ConversationState, SessionLock};
pub use state_kv::KvAgentStateStore;
pub use state_redis::RedisAgentStateStore;
pub use telemetry::{OtelTelemetry, StepTelemetryCtx, Telemetry};
pub use tenant::{TenantContext, VerifiedCaller};
pub use tool_call_frame::{ToolCallFrame, current_tool_call};
pub use tool_session::{ToolSession, ToolSessionError, ToolSessionSchema};
pub use tool_wire_name::{ToolNameCodec, is_wire_safe, wire_tool_name};
pub use tools::{KvToolLedger, RedisToolLedger, ToolLedger};
pub use user_ledger::{UserLedgerBinding, UserLedgerTarget};

use std::sync::Arc;

/// Observer for incremental step progress: token deltas as the LLM streams
/// its reply, and tool-call activity as the loop dispatches tools.
///
/// All methods have no-op default bodies, so callers implement only the
/// hooks they consume and the non-streaming [`AgentRuntime::step`] path
/// (which uses [`NoopStepObserver`]) costs nothing.
///
/// **Extending this trait:** add new capabilities as NEW defaulted methods
/// (e.g. `fn on_iteration_started(&self, _iter: u32) {}`) rather than
/// changing an existing signature. The current `on_token_delta(&self,
/// chunk: &str)` is deliberately minimal; per-iteration context must arrive
/// through an additional hook so existing implementors keep compiling.
pub trait StepObserver: Send + Sync {
    /// Whether this observer wants token-level streaming. Defaults to
    /// `false` so the non-streaming [`AgentRuntime::step`] path calls
    /// [`LlmBackend::complete`] and preserves the exact request wire shape
    /// (no `stream: true`) every existing caller relied on before streaming
    /// existed. A streaming consumer overrides this to `true`, which makes
    /// [`r#loop::run_step`] use [`LlmBackend::complete_streaming`] and drive
    /// [`StepObserver::on_token_delta`].
    fn wants_streaming(&self) -> bool {
        false
    }
    /// Called with each incremental text chunk of the assistant reply.
    /// Only invoked when [`StepObserver::wants_streaming`] returns `true`.
    fn on_token_delta(&self, _chunk: &str) {}
    /// Called just before a tool is dispatched, with its arguments.
    fn on_tool_call(&self, _name: &str, _call_id: &str, _args: &serde_json::Value) {}
    /// Called after a tool dispatch succeeds, with the tool's result.
    fn on_tool_result(&self, _name: &str, _call_id: &str, _result: &serde_json::Value) {}
    /// Called after a tool dispatch fails or is blocked (e.g. not in the
    /// agent's allow-list), with the error/reason payload. Additive hook —
    /// see the "Extending this trait" note above.
    fn on_tool_failed(&self, _name: &str, _call_id: &str, _error: &serde_json::Value) {}
    /// Called after each LLM iteration completes, with that call's token usage.
    /// Additive hook — lets a streaming consumer surface a per-message LLM/token
    /// trace even for turns that call no tools (the token trail is otherwise
    /// only in the final `AgentOutput.trail`, which the SSE stream omits).
    fn on_llm_call(&self, _tokens_in: u64, _tokens_out: u64) {}
    /// Called for every guardrail denial — blocked (Enforce) or merely recorded
    /// (Monitor). Default no-op: only the audit observer forwards these.
    /// Additive hook — see the "Extending this trait" note above.
    fn on_guardrail(&self, _obs: &crate::guardrail::GuardrailObservation) {}
}

/// No-op observer used by the non-streaming [`AgentRuntime::step`].
pub struct NoopStepObserver;
impl StepObserver for NoopStepObserver {}

/// The main entry point for executing a single agentic step.
///
/// Construct via [`AgentRuntime::new`] with the trait objects (config,
/// state, LLM, telemetry, token_meter, ledger) plus a shared
/// `Arc<ExtensionRuntime>` for tool dispatch. Call [`AgentRuntime::step`]
/// per inbound user message.
pub struct AgentRuntime {
    pub(crate) config_provider: Arc<dyn ConfigProvider>,
    pub(crate) state_store: Arc<dyn AgentStateStore>,
    pub(crate) ext_runtime: Arc<greentic_ext_runtime::ExtensionRuntime>,
    pub(crate) llm: Arc<dyn LlmBackend>,
    pub(crate) telemetry: Arc<dyn Telemetry>,
    pub(crate) token_meter: Arc<dyn TokenMeter>,
    pub(crate) billing_meter: Arc<dyn crate::billing::BillingMeter>,
    pub(crate) ledger: Arc<dyn ToolLedger>,
    /// Per-tenant agentic-worker MCP tool source. `None` disables MCP tools
    /// entirely (`mcp:`-prefixed tool refs then resolve to nothing). The real
    /// per-operator wiring lives in the runner host; tests and non-MCP callers
    /// pass `None`.
    pub(crate) mcp: Option<Arc<crate::mcp_source::McpToolSource>>,
    /// Policy that supplies platform/tenant-wide mandatory guardrail refs.
    /// Defaults to [`crate::guardrail::NoMandatoryGuardrails`] (no platform
    /// enforcement). Replaced by a real policy during runner-host wiring.
    pub(crate) guardrail_policy: Arc<dyn crate::guardrail::GuardrailPolicy>,
    /// Evaluator that invokes guardrail WASM extensions.
    /// Defaults to [`crate::guardrail::AcceptAllEvaluator`] until Task 7
    /// wires in the real extension-backed evaluator.
    pub(crate) guardrail_evaluator: Arc<dyn crate::guardrail::GuardrailEvaluator>,
    /// Per-tenant agentic-worker component tool source. `None` disables
    /// component tools entirely (`component:`-prefixed tool refs then resolve to
    /// nothing). Set via [`AgentRuntime::with_component_source`]; the concrete
    /// invoker (over the runner-host `PackRuntime` component host) is injected
    /// at the runner-host edge, never compiled in.
    pub(crate) components: Option<Arc<crate::component_source::ComponentToolSource>>,
    /// Per-tenant agentic-worker flow tool source. `None` disables flow tools
    /// entirely (`flow:`-prefixed tool refs then resolve to nothing). Set via
    /// [`AgentRuntime::with_flow_source`]; the concrete invoker (over the
    /// runner-host pack flow runtime) is injected at the runner-host edge, never
    /// compiled in.
    pub(crate) flows: Option<Arc<crate::flow_source::FlowToolSource>>,
    /// Set by [`AgentRuntime::with_playbook_source`]; the pack reader and the
    /// turn effect both live in the host (see `playbook_source`'s header for
    /// why the turn cannot be resolved here).
    pub(crate) playbooks: Option<Arc<crate::playbook_source::PlaybookToolSource>>,
    /// Per-tenant agentic-worker SoRLa SoR tool source. `None` disables sorla
    /// tools entirely (`sorla:`-prefixed tool refs then resolve to nothing).
    /// Set via [`AgentRuntime::with_sorla_source`]; the concrete invoker (over
    /// the host SoRX interact client) is injected at the runner-host edge,
    /// never compiled in.
    pub(crate) sorla: Option<Arc<crate::sorla_source::SorlaToolSource>>,
    /// Agentic-worker A2A tool source. `None` disables A2A tools entirely
    /// (`a2a:`-prefixed tool refs are then dropped from the tool list and
    /// reported by the preflight check). Set via
    /// [`AgentRuntime::with_a2a_source`]. Unlike the flow and component
    /// sources this one owns its transport, since an A2A agent is plain
    /// HTTP(S) reachable from here.
    pub(crate) a2a: Option<Arc<crate::a2a_source::A2aToolSource>>,
    /// Episodic long-term memory backend (e.g. Chronicle). `None` disables the
    /// long-term tier. Set via [`AgentRuntime::with_long_term_memory`]; the
    /// concrete backend is injected at the runner-host edge, never compiled in.
    pub(crate) long_term_memory: Option<Arc<dyn long_term::LongTermMemory>>,
    /// Knowledge / RAG (document-corpus) backend (e.g. Chronicle doc-RAG).
    /// `None` disables the knowledge tier. Set via [`AgentRuntime::with_knowledge`];
    /// the concrete backend is injected at the runner-host edge, never compiled in.
    pub(crate) knowledge: Option<Arc<dyn knowledge::Knowledge>>,
    /// Short-term ("working") memory backend, scoped per `(tenant, session, key)`.
    /// `None` disables the short-term tier. Set via
    /// [`AgentRuntime::with_short_term_memory`]; the host attaches the always-
    /// available in-memory provider, and the tools are gated by
    /// `config.memory.short_term`.
    pub(crate) short_term_memory: Option<Arc<dyn crate::memory::MemoryProvider>>,
    /// Per-binding sharing modes from the pack's `assets/run-context.json`
    /// (shared context, Phase A2). `None` means every NESTED binding is `none`
    /// (a top-level turn under an open context still injects and records).
    pub(crate) share_policy: Option<Arc<crate::share_policy::SharePolicy>>,
    /// The user ledger (shared context, Phase C): a durable, per verified
    /// end-user history shared by the environment's units. `None` = off.
    pub(crate) user_ledger: Option<Arc<crate::user_ledger::UserLedgerBinding>>,
}

impl AgentRuntime {
    // Each argument is a distinct injected dependency (config, state, ext,
    // llm, telemetry, token-meter, ledger, mcp); a builder would add ceremony
    // without removing the coupling, so the wide constructor is intentional.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        config_provider: Arc<dyn ConfigProvider>,
        state_store: Arc<dyn AgentStateStore>,
        ext_runtime: Arc<greentic_ext_runtime::ExtensionRuntime>,
        llm: Arc<dyn LlmBackend>,
        telemetry: Arc<dyn Telemetry>,
        token_meter: Arc<dyn TokenMeter>,
        ledger: Arc<dyn ToolLedger>,
        mcp: Option<Arc<crate::mcp_source::McpToolSource>>,
    ) -> Self {
        Self {
            config_provider,
            state_store,
            ext_runtime,
            llm,
            telemetry,
            token_meter,
            billing_meter: Arc::new(crate::billing::NoopBillingMeter),
            ledger,
            mcp,
            guardrail_policy: Arc::new(crate::guardrail::NoMandatoryGuardrails),
            guardrail_evaluator: Arc::new(crate::guardrail::AcceptAllEvaluator),
            components: None,
            flows: None,
            playbooks: None,
            sorla: None,
            a2a: None,
            long_term_memory: None,
            knowledge: None,
            short_term_memory: None,
            share_policy: None,
            user_ledger: None,
        }
    }

    /// Override the guardrail policy and evaluator after construction.
    ///
    /// Available in all builds (including production runner-host) so the
    /// runner can inject the real `ExtRuntimeGuardrailEvaluator` +
    /// `StaticGuardrailPolicy` without requiring `test-mock`.
    pub fn with_guardrails(
        mut self,
        policy: Arc<dyn crate::guardrail::GuardrailPolicy>,
        evaluator: Arc<dyn crate::guardrail::GuardrailEvaluator>,
    ) -> Self {
        self.guardrail_policy = policy;
        self.guardrail_evaluator = evaluator;
        self
    }

    /// Install a billing sink. Defaults to [`crate::billing::NoopBillingMeter`]
    /// (no-op, billing disabled); the host calls this when
    /// `GREENTIC_BILLING_BASE_URL` and `GREENTIC_BILLING_SERVICE_SECRET` are
    /// configured. The setter is intentionally separate from [`AgentRuntime::new`]
    /// so the constructor signature remains stable across all consumers.
    #[must_use]
    pub fn with_billing_meter(mut self, meter: Arc<dyn crate::billing::BillingMeter>) -> Self {
        self.billing_meter = meter;
        self
    }

    /// Wire the component tool source so `component:`-prefixed tool refs resolve
    /// to greentic `.gtpack` components invoked over the host component runtime.
    /// Coexists with the MCP/extension tool surfaces; defaults off when not set.
    #[must_use]
    pub fn with_component_source(
        mut self,
        components: Option<Arc<crate::component_source::ComponentToolSource>>,
    ) -> Self {
        self.components = components;
        self
    }

    /// Wire the flow tool source so `flow:`-prefixed tool refs resolve to pack
    /// flows invoked over the host flow runtime. Coexists with the
    /// MCP/extension/component tool surfaces; defaults off when not set.
    #[must_use]
    pub fn with_flow_source(
        mut self,
        flows: Option<Arc<crate::flow_source::FlowToolSource>>,
    ) -> Self {
        self.flows = flows;
        self
    }

    /// Wire the playbook tool source so `playbook:`-prefixed tool refs resolve
    /// to the playbooks the loaded packs carry. Defaults off when not set.
    ///
    /// Unlike every sibling source this one carries a TURN EFFECT as well as a
    /// reader, because running a playbook is an LLM turn rather than an outbound
    /// call — see `playbook_source`'s header.
    #[must_use]
    pub fn with_playbook_source(
        mut self,
        playbooks: Option<Arc<crate::playbook_source::PlaybookToolSource>>,
    ) -> Self {
        self.playbooks = playbooks;
        self
    }

    /// Install the pack-carried sharing policy (`assets/run-context.json`).
    /// Defaults off: with no policy every nested call shares nothing.
    #[must_use]
    pub fn with_share_policy(
        mut self,
        policy: Option<Arc<crate::share_policy::SharePolicy>>,
    ) -> Self {
        self.share_policy = policy;
        self
    }

    /// The installed sharing policy, if any.
    pub fn share_policy(&self) -> Option<&Arc<crate::share_policy::SharePolicy>> {
        self.share_policy.as_ref()
    }

    /// Install the user ledger. Defaults off; the runner host installs one
    /// only when the embedding host passed a door target AND a pack names the
    /// agent (`assets/user-ledger.json`).
    #[must_use]
    pub fn with_user_ledger(
        mut self,
        binding: Option<Arc<crate::user_ledger::UserLedgerBinding>>,
    ) -> Self {
        self.user_ledger = binding;
        self
    }

    /// The installed user ledger, if any.
    pub fn user_ledger(&self) -> Option<&Arc<crate::user_ledger::UserLedgerBinding>> {
        self.user_ledger.as_ref()
    }

    pub(crate) fn share_modes_for(
        &self,
        agent_id: &str,
    ) -> Option<Arc<crate::share_policy::BindingModes>> {
        self.share_policy
            .as_ref()
            .and_then(|p| p.for_agent(agent_id))
    }

    /// Attach a per-tenant SoRLa SoR tool source: `sorla:<pack>` tool refs
    /// resolve to a SoR BusinessAction invoked over the host SoRX interact
    /// client. Coexists with the mcp/component/flow sources.
    #[must_use]
    pub fn with_sorla_source(
        mut self,
        sorla: Option<Arc<crate::sorla_source::SorlaToolSource>>,
    ) -> Self {
        self.sorla = sorla;
        self
    }

    /// Wire the A2A tool source so `a2a:`-prefixed tool refs resolve to
    /// external A2A agents invoked over HTTP(S). Coexists with the
    /// mcp/component/flow/sorla sources; defaults off when not set.
    #[must_use]
    pub fn with_a2a_source(mut self, a2a: Option<Arc<crate::a2a_source::A2aToolSource>>) -> Self {
        self.a2a = a2a;
        self
    }

    /// Wire the episodic long-term memory backend (e.g. Chronicle). Coexists
    /// with the short-term/working memory; defaults off when not set.
    #[must_use]
    pub fn with_long_term_memory(mut self, memory: Arc<dyn long_term::LongTermMemory>) -> Self {
        self.long_term_memory = Some(memory);
        self
    }

    /// Wire the short-term ("working") memory backend. Coexists with the
    /// long-term tier; defaults off when not set. The MVP host attaches the
    /// in-memory provider unconditionally; the `remember`/`recall` tools are
    /// advertised only when `config.memory.short_term` is set.
    #[must_use]
    pub fn with_short_term_memory(
        mut self,
        memory: Arc<dyn crate::memory::MemoryProvider>,
    ) -> Self {
        self.short_term_memory = Some(memory);
        self
    }

    /// Ingest one episode (conversation turn, document, event) into long-term
    /// memory. Returns [`LongTermMemoryError::NotConfigured`] when no long-term
    /// backend is wired.
    pub async fn remember_episode(
        &self,
        tenant: &TenantContext,
        episode: long_term::EpisodeIngest,
    ) -> Result<long_term::IngestOutcome, long_term::LongTermMemoryError> {
        let memory = self.long_term_memory.as_ref().ok_or_else(|| {
            long_term::LongTermMemoryError::NotConfigured("long-term memory not wired".into())
        })?;
        let ctx = long_term::to_types_tenant(tenant)?;
        memory.ingest_episode(&ctx, episode).await
    }

    /// Semantic recall over long-term memory. Returns
    /// [`LongTermMemoryError::NotConfigured`] when no long-term backend is wired.
    pub async fn recall_long_term(
        &self,
        tenant: &TenantContext,
        query: long_term::RecallQuery,
    ) -> Result<Vec<long_term::RecalledFact>, long_term::LongTermMemoryError> {
        let memory = self.long_term_memory.as_ref().ok_or_else(|| {
            long_term::LongTermMemoryError::NotConfigured("long-term memory not wired".into())
        })?;
        let ctx = long_term::to_types_tenant(tenant)?;
        memory.recall(&ctx, query).await
    }

    /// Wire the knowledge / RAG backend (e.g. Chronicle doc-RAG). Coexists with
    /// the memory tiers (distinct `cap://dw.knowledge` capability); defaults off.
    #[must_use]
    pub fn with_knowledge(mut self, knowledge: Arc<dyn knowledge::Knowledge>) -> Self {
        self.knowledge = Some(knowledge);
        self
    }

    /// Whether a knowledge / RAG backend is mounted. Mirrors the check
    /// [`knowledge::knowledge_active`] applies against `runtime.knowledge` in the
    /// agentic loop, exposed so hosts and tests can confirm the seam is wired
    /// without issuing a (network-bound) [`Self::search_knowledge`] call.
    ///
    /// This answers "can retrieval be attempted at all", NOT "is a corpus
    /// mounted": a host may mount a backend that delegates elsewhere per turn,
    /// in which case this is true with no corpus behind it. Ask
    /// [`knowledge::Knowledge::wrapped_backend`] for that, via
    /// [`Self::knowledge_backend`].
    #[must_use]
    pub fn has_knowledge(&self) -> bool {
        self.knowledge.is_some()
    }

    /// Whether an A2A tool source is wired, so `a2a:` refs can resolve.
    ///
    /// Exposed for the host's wiring regression test. The runtime is built in
    /// runner-host, where this field is not visible, and a dropped
    /// `with_a2a_source` call otherwise fails only as "the tool vanished" at
    /// run time.
    #[must_use]
    pub fn has_a2a_source(&self) -> bool {
        self.a2a.is_some()
    }

    /// The mounted knowledge backend, if any.
    ///
    /// Exposed so a host that mounts a SECOND backend can wrap the first
    /// instead of replacing it: [`Self::with_knowledge`] overwrites the field,
    /// and two mounts that each assume they are the only one would silently
    /// disable whichever ran first.
    #[must_use]
    pub fn knowledge_backend(&self) -> Option<Arc<dyn knowledge::Knowledge>> {
        self.knowledge.clone()
    }

    /// Hybrid retrieval over the agent's knowledge corpus. Returns
    /// [`knowledge::KnowledgeError::NotConfigured`] when no backend is wired.
    ///
    /// `binding` is the agent's own `config.knowledge.knowledge` provider ref
    /// for this turn. It is threaded through because a backend may delegate to
    /// a target named in the binding's `params` — see
    /// [`knowledge::Knowledge::search_bound`] for why that cannot ride on the
    /// runtime-level field or inside [`knowledge::KnowledgeQuery`].
    pub async fn search_knowledge(
        &self,
        tenant: &TenantContext,
        query: knowledge::KnowledgeQuery,
        binding: Option<&config::MemoryProviderRef>,
    ) -> knowledge::KnowledgeResult<Vec<knowledge::RetrievedChunk>> {
        let kb = self
            .knowledge
            .as_ref()
            .ok_or(knowledge::KnowledgeError::NotConfigured)?;
        let ctx = knowledge::to_types_tenant(tenant)?;
        kb.search_bound(&ctx, query, binding).await
    }

    /// The card of the session's live parked `flow:` tool, if any. A host uses
    /// it to keep the card on offer when a turn fails after the park was kept
    /// (an LLM error or a lock timeout during a side turn), so the user's next
    /// submit is not lost. `None` when nothing is parked, the park expired, or
    /// the state cannot be read.
    pub async fn parked_card(
        &self,
        tenant: &TenantContext,
        session_id: &str,
    ) -> Option<serde_json::Value> {
        let state = self.state_store.load(tenant, session_id).await.ok()?;
        let pending = state.pending_tool?;
        if pending.is_expired(chrono::Utc::now()) {
            return None;
        }
        pending.presentation
    }

    /// Execute one agentic step against the given session.
    /// Implementation lives in [`r#loop::run_step`].
    pub async fn step(
        &self,
        tenant: TenantContext,
        session_id: &str,
        agent_id: &str,
        message: AgentInput,
    ) -> Result<AgentOutput, AgentError> {
        self.step_with_observer(
            tenant,
            session_id,
            agent_id,
            message,
            Arc::new(NoopStepObserver),
        )
        .await
    }

    /// Execute one agentic step while reporting incremental progress to
    /// `observer` (streamed token deltas + tool-call activity).
    /// [`AgentRuntime::step`] delegates here with a [`NoopStepObserver`].
    pub async fn step_with_observer(
        &self,
        tenant: TenantContext,
        session_id: &str,
        agent_id: &str,
        message: AgentInput,
        observer: Arc<dyn StepObserver>,
    ) -> Result<AgentOutput, AgentError> {
        r#loop::run_step(self, tenant, session_id, agent_id, message, observer).await
    }
}

/// Inbound user message handed to [`AgentRuntime::step`].
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct AgentInput {
    pub text: String,
    /// Whether THIS invocation is a conversational segment. Set from the flow
    /// node's `conversational` flag so a node marked conversational makes the
    /// agent's host `end_conversation` tool available (and the closing prompt
    /// note) even when the agent's own [`AgentConfig::conversational`] default
    /// is false — the node, not just the agent config, can opt in. OR-ed with
    /// the agent config in the loop.
    #[serde(default)]
    pub conversational: bool,
    /// The user's answer to a flow tool that parked this agent on a card
    /// ([`TerminationReason::AwaitingToolInput`]) — typically the card's
    /// submit. `Some` resumes the pending tool flow with it; `None` while a
    /// tool is pending cancels that tool and treats `text` as an ordinary
    /// message. Ignored when no tool is pending.
    ///
    /// [`TerminationReason::AwaitingToolInput`]: crate::error::TerminationReason::AwaitingToolInput
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resume_payload: Option<serde_json::Value>,
    /// Attachments of THIS message (references only; see [`attachments`]).
    ///
    /// Adding this field made `AgentInput { .. }` literals without a base fail
    /// to compile; downstream constructors use `..Default::default()` rather
    /// than the struct being `#[non_exhaustive]`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<AttachmentRef>,
}

/// Token + iteration accounting for one [`AgentRuntime::step`]. Surfaced on
/// [`AgentOutput`] so a caller (e.g. the designer's Run Demo trace) can show
/// per-turn LLM usage without a separate telemetry channel.
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct StepUsage {
    /// Prompt tokens summed across every LLM call in this step.
    pub tokens_in: u64,
    /// Completion tokens summed across every LLM call in this step.
    pub tokens_out: u64,
    /// Plan-Act-Observe iterations the loop ran this step.
    pub iterations: u32,
}

/// Outbound reply produced by [`AgentRuntime::step`].
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct AgentOutput {
    pub reply: String,
    pub trail: Vec<AgentStep>,
    pub terminated_by: TerminationReason,
    /// Per-step token + iteration usage (default zero for callers that don't
    /// track it, e.g. mocks).
    #[serde(default)]
    pub usage: StepUsage,
    /// What to show the user while the agent waits on a flow tool — set only
    /// when `terminated_by` is [`TerminationReason::AwaitingToolInput`]. It is
    /// the parked flow's own output at its park point (typically an Adaptive
    /// Card), rendered in place of `reply`.
    ///
    /// [`TerminationReason::AwaitingToolInput`]: crate::error::TerminationReason::AwaitingToolInput
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_presentation: Option<serde_json::Value>,
    /// True when this turn answered a typed message as a side turn while the
    /// flow tool stayed parked; `reply` is the answer and
    /// `pending_presentation` the card re-offered after it.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub side_turn: bool,
}

/// One iteration of the Plan-Act-Observe loop, surfaced in the audit
/// trail (`AgentOutput.trail`). Caller decides whether to persist or
/// display.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum AgentStep {
    /// One LLM iteration's assistant output + token cost, recorded before that
    /// iteration's tool calls. Lets a caller show a per-message breakdown (each
    /// model call), not just the aggregate `AgentOutput.usage`. `content` is the
    /// assistant text for the turn (empty when the model only emitted tool calls).
    LlmCall {
        content: String,
        tokens_in: u64,
        tokens_out: u64,
    },
    ToolCall {
        name: String,
        call_id: String,
        /// The arguments the model passed to the tool (its input). Lets a caller
        /// show what was actually requested, not just the tool name. Defaults to
        /// `null` for older trails that predate this field.
        #[serde(default)]
        args: serde_json::Value,
        result: serde_json::Value,
        /// Wall-clock time spent dispatching this tool call, in milliseconds.
        /// `0` for host built-ins that resolve instantly and for older trails.
        #[serde(default)]
        duration_ms: u64,
    },
    ToolCallReused {
        name: String,
        call_id: String,
    },
    ToolCallBlocked {
        name: String,
        reason: String,
    },
    Reply {
        text: String,
    },
    /// The worker's BUILT-IN knowledge base (`KnowledgeSettings`) was searched
    /// for this turn and returned `chunks`, which were injected into the system
    /// prompt before the first LLM call. Recorded once per step, ahead of every
    /// `LlmCall`, and only when retrieval succeeded with at least one chunk — a
    /// failed or empty retrieval injects nothing and so has nothing to cite.
    ///
    /// This is NOT a tool call: the model did not ask for it, the loop ran it.
    /// Recording it as a synthetic `ToolCall` would inflate tool-call counts and
    /// anything metering them. It exists so a trail consumer (greentic-start's
    /// `agent_provenance`) can cite what a knowledge-grounded answer drew on.
    ///
    /// The chunks are recorded faithfully, text included: the trail is
    /// server-side data. Whether a chunk's text may reach an end user's
    /// browser is the CONSUMER's disclosure decision, not the runtime's.
    ///
    /// Adding a variant to this `#[serde(tag = "kind")]` enum is a contract
    /// change: a strict `Vec<AgentStep>` deserialiser built against an older
    /// runtime rejects `"kind": "knowledge_retrieval"`. Readers must skip kinds
    /// they do not know.
    KnowledgeRetrieval {
        chunks: Vec<knowledge::RetrievedChunk>,
    },
}