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
// SPDX-License-Identifier: Apache-2.0
//! `StewardFactory` trait — abstracts how a per-tenant `Arc<Steward>` is
//! built at registry-open time.
//!
//! Introduced in v0.9.0 P0c per the locked plan
//! (`docs/dev-log/0098-v0.9.0-implementation-plan.md` §6 "Steward
//! placement" / MAJOR 1 resolution). The trait shape was refined in the
//! v0.9.0 P2 revision (F2 cleanup) to drop the placeholder `MockPeer`
//! parameter that previously leaked into the public surface; see "Why
//! `build()` takes no peer" below.
//!
//! ## Why the trait exists
//!
//! Pre-v0.9.0, the daemon built one `Arc<Steward>` at startup and threaded
//! it through every `TenantHandle::open` via
//! `TenantOpenParams.steward`. That worked for static backends
//! (Anthropic / OpenAI / Ollama / `None`) where the `LlmClient` instance
//! is available before the first MCP session attaches.
//!
//! v0.9.0 adds an MCP-sampling backend whose `LlmClient` (the
//! `SamplingLlmClient`, P2) requires a `Peer<RoleServer>` only available
//! AFTER an MCP `initialize` handshake returns. We can't pre-build the
//! Steward at daemon startup because the peer doesn't exist yet.
//!
//! The factory trait expresses "how to build a Steward at registry-open
//! time, if at all". Two production variants:
//!
//! * **Static** — for Anthropic / OpenAI / Ollama / None. The
//! `LlmClient` is already known; the wrapped `Arc<Steward>` is
//! pre-built once at registry-open time and `build()` returns a
//! clone unconditionally. `requires_peer() = false`.
//!
//! * **MCP sampling** — for `LlmConfig::McpSampling`. `build()` is a
//! no-op that returns `Ok(None)` — the real sampling-backed Steward
//! is constructed by
//! [`crate::tenants::TenantHandle::steward_slot`] consumers (in
//! practice, `solo_api::mcp::SoloMcpServer::populate_sampling_steward`
//! at MCP `initialize` time) and written into the slot directly.
//! `requires_peer() = true`.
//!
//! ## Why `build()` takes no peer
//!
//! Earlier P0c drafts gave the trait a `peer: Option<MockPeer>`
//! parameter, intending v0.9.0 P2 to replace `MockPeer` with the real
//! `rmcp::Peer<RoleServer>` shape. That plan was abandoned in P2: the
//! real peer lives in `solo-api` (with the rmcp transitive), and
//! plumbing it across the `solo-api → solo-storage` boundary would
//! either leak rmcp into the storage layer or require a `Box<dyn Any>`
//! erased handle whose unsafe-by-construction down-cast wins nothing.
//!
//! The cleaner resolution — landed in P2 — is to bypass the factory on
//! the sampling path entirely. `solo_api::mcp::SoloMcpServer::
//! populate_sampling_steward` constructs the sampling-backed
//! `Arc<Steward>` directly from
//! `solo_api::llm::build_sampling_steward(peer, ..)` and writes it into
//! `TenantHandle::steward_slot()`. The factory's `build()` then never
//! needs a peer argument: it's only ever called at registry-open time
//! (before any MCP session can have attached), so for the sampling
//! backend it correctly returns `Ok(None)`.
//!
//! ## Slot semantics
//!
//! `TenantHandle` carries an `Arc<RwLock<Option<Arc<Steward>>>>`
//! "steward slot" that the factory populates at registry-open time. For
//! static backends, the slot is `Some(steward)` immediately — the hot
//! path's `slot.read()` always observes a populated value and pays no
//! lock-contention cost in practice (the `RwLock` is uncontested).
//! For the MCP-sampling backend, the slot stays `None` until
//! `SoloMcpServer::initialize` (P2) calls `populate_sampling_steward`
//! and writes the result in.
//!
//! Consumers should:
//!
//! 1. `slot.read().clone()` to get an `Option<Arc<Steward>>` snapshot.
//! 2. If `Some(steward)`, use it.
//! 3. If `None`, fall through to the "no LLM available" path
//! (cluster persists, abstraction skipped — same as v0.8.x when
//! `LlmConfig` was `None`).
//!
//! P0c provided the trait + impls + slot wiring. P2 ships the
//! late-population MCP-initialize hook. P4 will refactor the
//! writer-actor to read from the slot per command instead of capturing
//! `Arc<Steward>` at spawn time, which is what activates the
//! sampling-backed Steward end-to-end (until then it is plumbed but
//! inert; see dev log 0101's audit-response section for the full
//! context).
use Arc;
use Result;
/// Builds (or surfaces a pre-built) `Arc<Steward>` for a tenant at
/// registry-open time.
///
/// `Send + Sync` so the factory can live inside `Arc<dyn StewardFactory>`
/// in the registry; the trait object is cloned-by-Arc into every
/// `TenantHandle::open` call.
/// Static-backend factory: wraps a pre-built `Arc<Steward>` and surfaces
/// it from `build()` unconditionally.
///
/// Used by `LlmSettings::Anthropic`, `LlmSettings::Openai`,
/// `LlmSettings::Ollama`, and `LlmSettings::None` (the latter wrapping
/// a Steward backed by `NoopLlmClient`). Zero hot-path lock-contention
/// cost — the wrapped `Arc<Steward>` is cloned out of the factory in
/// `O(1)`.
/// MCP-sampling-backend factory: a no-op `build()` that always returns
/// `Ok(None)`.
///
/// The real sampling-backed Steward needs `rmcp::Peer<RoleServer>`,
/// which lives in `solo-api`. `solo-storage` doesn't depend on
/// `solo-api`, so the storage-layer factory cannot construct the
/// Steward. Instead,
/// [`solo_api::mcp::SoloMcpServer::populate_sampling_steward`] is the
/// canonical builder: at MCP `initialize` time it constructs the
/// `SamplingLlmClient`-backed `Arc<Steward>` via
/// `solo_api::llm::build_sampling_steward` and writes it directly into
/// `TenantHandle::steward_slot()`.
///
/// This factory exists only so the registry-open path has a uniform
/// `Arc<dyn StewardFactory>` API. `requires_peer() = true` short-
/// circuits the eager-populate code path; the slot stays `None` until
/// `initialize` writes the peer-bound Steward in.
;