entelix_core/llm_facing.rs
1//! LLM-facing channel — type-level separation of operator-facing
2//! diagnostics from the value the model actually sees (invariant #16).
3//!
4//! Two surfaces, both narrowly defined:
5//!
6//! - [`LlmRenderable`] — `render_for_llm()` returns the raw model-facing
7//! value; `for_llm()` wraps it in a sealed [`RenderedForLlm`] carrier
8//! so emit sites cannot fabricate model-facing content without
9//! passing through a registered impl. Implementors keep prose brief,
10//! omit operator-only context (status codes, type-system
11//! identifiers, source chains), and never echo input payloads —
12//! those are prompt-injection vectors.
13//! - [`LlmFacingSchema`] — `strip(&Value) -> Value` reduces a JSON
14//! Schema to the keys vendor APIs actually consume (`type`,
15//! `properties`, `required`, `items`, `enum`, `description`,
16//! bounds…). Schemars-generated knobs (`$schema`, `title`,
17//! `$defs`, `$ref`, format specifiers like `int64`) ride out.
18//! Saves 30–120 tokens per tool per request × every turn.
19//!
20//! ## Why the sealed carrier
21//!
22//! Errors, future sub-agent results, approval decisions, and
23//! memory-recall summaries all flow through the same funnel toward
24//! the model's context window. Without a sealed carrier any
25//! `String`-typed field can be fabricated by external code — a
26//! reviewer reading an emit site cannot distinguish "this string
27//! went through the LLM-facing rendering" from "this string was
28//! built directly from operator content". Wrapping the value in
29//! `RenderedForLlm<T>` whose constructor is private to this
30//! module makes the boundary structural: the only path from value
31//! to carrier is the trait's default `for_llm` impl, which wraps
32//! the implementer's `render_for_llm` output. A subtype that
33//! tries to override `for_llm` cannot reach `RenderedForLlm::new`,
34//! so the sealing holds across crate boundaries.
35//!
36//! ## Why a separate trait rather than a method on `Error`
37//!
38//! The split lets non-`Error` types (custom tool error wrappers, MCP
39//! server errors lifted into IR, future sub-agent result types) opt
40//! into the same contract without coupling to `entelix_core::Error`.
41//! Default impls on `Error` and `String`/`&str` cover the common
42//! cases; bespoke implementors override `render_for_llm` only.
43//!
44//! ## Enforcement
45//!
46//! `crates/entelix-tools/tests/llm_context_economy.rs` regression-checks
47//! that built-in tool outputs and tool-spec schemas never leak the
48//! forbidden patterns. CI rejects new sites silently re-introducing
49//! operator-channel content into the model's view.
50
51use std::collections::BTreeMap;
52
53use serde_json::{Map, Value};
54
55use crate::error::Error;
56
57/// Sealed carrier for a model-facing value of type `T`. Constructed
58/// only by [`LlmRenderable::for_llm`]'s default impl — the
59/// constructor is `pub(crate)`, so an external crate that
60/// implements [`LlmRenderable<T>`] for its own type can override
61/// `render_for_llm` (the raw producer) but cannot override
62/// `for_llm` (the carrier-producing wrapper) because it has no way
63/// to reach `RenderedForLlm::new`. Emit sites that accept
64/// `RenderedForLlm<T>` therefore receive a value that
65/// structurally must have come through the trait funnel.
66///
67/// `RenderedForLlm` is intentionally minimal — it exposes
68/// [`Self::into_inner`] for consumers that need to forward the
69/// underlying value (the audit-log projection of
70/// `AgentEvent::ToolError` does exactly this when emitting the
71/// model-safe rendering as `GraphEvent::ToolResult` content). The
72/// carrier carries no metadata because the boundary it enforces is
73/// authorship, not provenance.
74#[derive(Clone, Debug, Eq, Hash, PartialEq)]
75pub struct RenderedForLlm<T>(T);
76
77impl<T> RenderedForLlm<T> {
78 /// Sealed constructor — only [`LlmRenderable::for_llm`]'s
79 /// default impl reaches this. `pub(crate)` is the entire seal.
80 pub(crate) const fn new(inner: T) -> Self {
81 Self(inner)
82 }
83
84 /// Borrow the inner model-facing value.
85 #[must_use]
86 pub const fn as_inner(&self) -> &T {
87 &self.0
88 }
89
90 /// Consume the carrier and return the inner value.
91 #[must_use]
92 pub fn into_inner(self) -> T {
93 self.0
94 }
95}
96
97impl<T: AsRef<str>> AsRef<str> for RenderedForLlm<T> {
98 fn as_ref(&self) -> &str {
99 self.0.as_ref()
100 }
101}
102
103impl<T: std::fmt::Display> std::fmt::Display for RenderedForLlm<T> {
104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 self.0.fmt(f)
106 }
107}
108
109impl<T> serde::Serialize for RenderedForLlm<T>
110where
111 T: serde::Serialize,
112{
113 fn serialize<S: serde::Serializer>(&self, ser: S) -> std::result::Result<S::Ok, S::Error> {
114 self.0.serialize(ser)
115 }
116}
117
118impl<'de, T> serde::Deserialize<'de> for RenderedForLlm<T>
119where
120 T: serde::Deserialize<'de>,
121{
122 fn deserialize<D: serde::Deserializer<'de>>(de: D) -> std::result::Result<Self, D::Error> {
123 // Audit-log replay paths (re-load `AgentEvent::ToolError`
124 // events from a `SessionLog`) must reconstruct the carrier
125 // around its persisted inner value. The persisted value
126 // already passed `for_llm` on first emit (invariant 18 —
127 // events are the SSoT), so deserialising into the carrier
128 // is the inverse, not a fresh fabrication.
129 T::deserialize(de).map(Self::new)
130 }
131}
132
133/// Render a value (typically an error, sub-agent result, or
134/// memory-recall summary) into the short, actionable form the
135/// model is allowed to see. Implementors define
136/// [`Self::render_for_llm`] (the raw producer); the default
137/// [`Self::for_llm`] wraps the result in a sealed
138/// [`RenderedForLlm`] carrier whose constructor is private to this
139/// crate, so emit sites that accept the carrier receive a value
140/// that structurally went through the trait.
141///
142/// Implementations keep prose brief, omit operator-only context
143/// (status codes, type-system identifiers, source chains), and
144/// never echo input payloads — those are prompt-injection vectors.
145/// The full operator-facing form continues to flow through
146/// `Display` / `Error::source` / event sinks / OTel.
147pub trait LlmRenderable<T> {
148 /// The raw model-facing rendering. Must not include vendor
149 /// status codes, `provider returned …` framing, source chains,
150 /// RFC3339 timestamps, or internal type names — operator
151 /// channels carry those.
152 fn render_for_llm(&self) -> T;
153
154 /// Sealed carrier wrapping [`Self::render_for_llm`]'s output.
155 /// External crates that implement this trait cannot override
156 /// this method without access to `RenderedForLlm::new`, which
157 /// is `pub(crate)` to `entelix-core`. The boundary therefore
158 /// holds across crate boundaries: only `entelix-core`'s default
159 /// impl can produce a `RenderedForLlm<T>`.
160 fn for_llm(&self) -> RenderedForLlm<T> {
161 RenderedForLlm::new(self.render_for_llm())
162 }
163}
164
165// `use_self` would prefer `Self` in place of `String` here, but the
166// trait param `String` and the receiver type `String` are
167// fundamentally the same in this two-parameter `LlmRenderable<T>`
168// shape — substituting `Self` reads worse than the explicit form.
169#[allow(clippy::use_self)]
170impl LlmRenderable<String> for String {
171 /// Identity rendering. The seal still holds — `for_llm()`'s
172 /// default impl (the only path to `RenderedForLlm::new`) routes
173 /// every emit through this trait, even when the operator's hint
174 /// is already a plain string. Validators raising
175 /// `Error::ModelRetry` thus write
176 /// `"corrective text".to_owned().for_llm()` and the type system
177 /// confirms the rendering boundary was crossed.
178 fn render_for_llm(&self) -> String {
179 self.clone()
180 }
181}
182
183impl LlmRenderable<String> for &str {
184 fn render_for_llm(&self) -> String {
185 (*self).to_owned()
186 }
187}
188
189impl LlmRenderable<String> for Error {
190 /// Short, model-actionable rendering. Mapping:
191 ///
192 /// - `InvalidRequest(msg)` → `"invalid input: {msg}"` — the
193 /// message is already caller-supplied and free of vendor
194 /// identifiers.
195 /// - `Provider { .. }` → `"upstream model error"` — vendor
196 /// status is operator-only.
197 /// - `Auth(_)` → `"authentication failed"` — never echo the
198 /// underlying provider's auth diagnostic.
199 /// - `Config(_)` → `"tool misconfigured"` — operator must fix.
200 /// - `Cancelled` → `"cancelled"`.
201 /// - `DeadlineExceeded` → `"timed out"`.
202 /// - `Interrupted { .. }` → `"awaiting human review"`.
203 /// - `Serde(_)` → `"output could not be serialised"` — the
204 /// inner serde error names internal types.
205 fn render_for_llm(&self) -> String {
206 match self {
207 Self::InvalidRequest(msg) => format!("invalid input: {msg}"),
208 Self::Provider { .. } => "upstream model error".to_owned(),
209 Self::Auth(_) => "authentication failed".to_owned(),
210 Self::Config(_) => "tool misconfigured".to_owned(),
211 Self::Cancelled => "cancelled".to_owned(),
212 Self::DeadlineExceeded => "timed out".to_owned(),
213 Self::Interrupted { .. } => "awaiting human review".to_owned(),
214 Self::Serde(_) => "output could not be serialised".to_owned(),
215 // Usage-limit breaches are operational signals — the
216 // model does not need budget visibility (and exposing
217 // it would invite the model to plan around limits).
218 Self::UsageLimitExceeded(_) => "request quota reached".to_owned(),
219 // `ModelRetry` carries an already-rendered hint by
220 // construction — surface that text verbatim. The retry
221 // loop catches the variant before LLM emission in normal
222 // flow; this branch covers leaks past the loop boundary.
223 Self::ModelRetry { hint, .. } => hint.as_inner().clone(),
224 }
225 }
226}
227
228/// JSON-Schema sanitiser — strips schemars / draft-meta keys that
229/// vendor APIs ignore but that still cost tokens to ship.
230pub struct LlmFacingSchema;
231
232/// JSON-Schema key classification — drives the schema-aware walk.
233///
234/// Different keys hold different *kinds* of value: some carry literal
235/// data (`type: "string"`, `description: "..."`), some carry a single
236/// nested schema (`items`, `additionalProperties` when an object),
237/// some carry an array of schemas (`anyOf`, `oneOf`, `allOf`), some
238/// carry a `map<user-name, schema>` (`properties`), and some carry
239/// user data that must not be schema-walked (`enum`, `default`,
240/// `const`, `required`). The classifier picks the right walk for
241/// each key so user-named properties survive the strip and user
242/// values are not accidentally pruned to empty objects.
243enum AllowedKey {
244 /// Literal value — `type`, `description`, bounds, `format`, …
245 /// Cloned through (with the `format` noise filter applied).
246 Literal,
247 /// Single nested schema — `items` (single-schema form),
248 /// `additionalProperties` (when an object), `not`.
249 Schema,
250 /// Array of nested schemas — `anyOf`, `oneOf`, `allOf`.
251 /// `items` (array form) also flows through here at runtime.
252 SchemaArray,
253 /// Map of user-named entries to schemas — `properties`. Keys
254 /// are preserved verbatim; values are schema-walked.
255 SchemaMap,
256 /// User data — `enum`, `default`, `const`, `required`. Cloned
257 /// verbatim; never schema-walked.
258 UserData,
259}
260
261fn classify(key: &str) -> Option<AllowedKey> {
262 Some(match key {
263 "type" | "description" | "minimum" | "maximum" | "exclusiveMinimum"
264 | "exclusiveMaximum" | "minLength" | "maxLength" | "minItems" | "maxItems"
265 | "uniqueItems" | "minProperties" | "maxProperties" | "pattern" | "format" => {
266 AllowedKey::Literal
267 }
268 "items" | "additionalProperties" | "not" => AllowedKey::Schema,
269 "anyOf" | "oneOf" | "allOf" => AllowedKey::SchemaArray,
270 "properties" => AllowedKey::SchemaMap,
271 "enum" | "default" | "const" | "required" => AllowedKey::UserData,
272 _ => return None,
273 })
274}
275
276/// `format` values that read as noise to the vendor — the
277/// JSON-Schema-encoded width hint is already implied by
278/// `type: "integer"`/`"number"` and the model gains nothing from
279/// seeing it. Removing them shrinks the wire without losing meaning.
280const NOISY_FORMATS: &[&str] = &[
281 "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double",
282];
283
284impl LlmFacingSchema {
285 /// Walk `schema` and return a copy containing only
286 /// vendor-relevant keys. The walk inlines `$ref`/`$defs`
287 /// indirection so the resulting schema is self-contained — no
288 /// dangling references, no draft-meta envelope.
289 ///
290 /// Self-referential `$ref` chains (`Inner` → `Inner`, or
291 /// `A` → `B` → `A`) cannot be expanded into a finite tree.
292 /// On cycle the recursion breaks at the offending node by
293 /// substituting an empty `{}` (accept-any) schema and
294 /// `tracing::warn!`s the cycle's def-name chain so operators
295 /// see the truncation. The accept-any substitute keeps the
296 /// surrounding shape valid for vendor consumption — the model
297 /// loses the inner recursion's structural detail (necessarily,
298 /// since cyclic types have no finite JSON Schema form), but
299 /// the schema as a whole stays well-formed.
300 #[must_use]
301 pub fn strip(schema: &Value) -> Value {
302 let defs = collect_defs(schema);
303 let mut visited: Vec<String> = Vec::new();
304 strip_schema(schema, &defs, &mut visited)
305 }
306}
307
308fn collect_defs(schema: &Value) -> BTreeMap<String, Value> {
309 let mut out = BTreeMap::new();
310 if let Some(obj) = schema.as_object() {
311 // Merge `$defs` (2020-12) and the legacy `definitions` key.
312 for key in ["$defs", "definitions"] {
313 if let Some(Value::Object(defs)) = obj.get(key) {
314 for (name, body) in defs {
315 out.insert(name.clone(), body.clone());
316 }
317 }
318 }
319 }
320 out
321}
322
323/// Strip one schema node. Resolves `$ref` indirection up front, then
324/// dispatches each surviving key according to its [`AllowedKey`]
325/// classification.
326///
327/// `visited` is the stack of `$defs` / `definitions` names currently
328/// being expanded along this branch of the recursion. A `$ref` whose
329/// target name already sits on the stack is a cycle — without
330/// breaking the recursion the call stack-overflows on any
331/// self-referential type (`Box<Self>` in a Rust struct → schemars
332/// emits a `$ref` back into its own `$defs` body).
333fn strip_schema(node: &Value, defs: &BTreeMap<String, Value>, visited: &mut Vec<String>) -> Value {
334 let Some(obj) = node.as_object() else {
335 // Not an object (likely a boolean schema like
336 // `additionalProperties: false` or an `items: true` shorthand)
337 // — clone through unchanged.
338 return node.clone();
339 };
340
341 // `$ref` short-circuits — replace the whole node with the
342 // stripped definition body. Eliminates `$defs` indirection.
343 // Cyclic chains break to an accept-any `{}` substitute so
344 // self-referential types do not stack-overflow the encoder.
345 if let Some(Value::String(reference)) = obj.get("$ref")
346 && let Some(name) = reference
347 .strip_prefix("#/$defs/")
348 .or_else(|| reference.strip_prefix("#/definitions/"))
349 && let Some(target) = defs.get(name)
350 {
351 if visited.iter().any(|seen| seen == name) {
352 let cycle_chain: Vec<&str> = visited
353 .iter()
354 .map(String::as_str)
355 .chain(std::iter::once(name))
356 .collect();
357 tracing::warn!(
358 cycle = ?cycle_chain,
359 "LlmFacingSchema::strip broke a $ref cycle — emitting accept-any substitute"
360 );
361 return Value::Object(Map::new());
362 }
363 visited.push(name.to_owned());
364 let stripped = strip_schema(target, defs, visited);
365 visited.pop();
366 return stripped;
367 }
368
369 let mut out = Map::new();
370 for (key, value) in obj {
371 let Some(kind) = classify(key) else {
372 continue;
373 };
374 match kind {
375 AllowedKey::Literal => {
376 if key == "format"
377 && let Some(format) = value.as_str()
378 && NOISY_FORMATS.contains(&format)
379 {
380 continue;
381 }
382 out.insert(key.clone(), value.clone());
383 }
384 AllowedKey::Schema => {
385 // `items` may be a single schema or an array of
386 // schemas (tuple-style validation); `additionalProperties`
387 // may be a boolean. Dispatch per shape.
388 let stripped = match value {
389 Value::Array(arr) => {
390 Value::Array(arr.iter().map(|v| strip_schema(v, defs, visited)).collect())
391 }
392 other => strip_schema(other, defs, visited),
393 };
394 out.insert(key.clone(), stripped);
395 }
396 AllowedKey::SchemaArray => {
397 if let Value::Array(arr) = value {
398 let stripped: Vec<Value> =
399 arr.iter().map(|v| strip_schema(v, defs, visited)).collect();
400 out.insert(key.clone(), Value::Array(stripped));
401 } else {
402 // Malformed — keep the original; the vendor will
403 // reject it with a clearer error than we can
404 // synthesize here.
405 out.insert(key.clone(), value.clone());
406 }
407 }
408 AllowedKey::SchemaMap => {
409 // User-named keys → preserve verbatim, values → walk.
410 if let Value::Object(map) = value {
411 let stripped: Map<String, Value> = map
412 .iter()
413 .map(|(k, v)| (k.clone(), strip_schema(v, defs, visited)))
414 .collect();
415 out.insert(key.clone(), Value::Object(stripped));
416 } else {
417 out.insert(key.clone(), value.clone());
418 }
419 }
420 AllowedKey::UserData => {
421 out.insert(key.clone(), value.clone());
422 }
423 }
424 }
425 Value::Object(out)
426}
427
428#[cfg(test)]
429#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
430mod tests {
431 use super::*;
432 use serde_json::json;
433
434 #[test]
435 fn render_for_llm_omits_provider_status() {
436 let err = Error::provider_http(503, "vendor down".to_owned());
437 let rendered = err.render_for_llm();
438 assert!(!rendered.contains("503"), "{rendered}");
439 assert!(!rendered.contains("vendor down"), "{rendered}");
440 assert!(!rendered.contains("provider returned"), "{rendered}");
441 }
442
443 #[test]
444 fn render_for_llm_invalid_request_carries_caller_message() {
445 let err = Error::invalid_request("missing 'task' field");
446 assert_eq!(err.render_for_llm(), "invalid input: missing 'task' field");
447 }
448
449 #[test]
450 fn strip_removes_schema_envelope() {
451 let raw = json!({
452 "$schema": "https://json-schema.org/draft/2020-12/schema",
453 "title": "DoubleInput",
454 "type": "object",
455 "properties": {"n": {"type": "integer", "format": "int64"}},
456 "required": ["n"]
457 });
458 let stripped = LlmFacingSchema::strip(&raw);
459 assert!(stripped.get("$schema").is_none());
460 assert!(stripped.get("title").is_none());
461 assert_eq!(stripped["type"], "object");
462 assert_eq!(stripped["properties"]["n"]["type"], "integer");
463 // int64 is the noisy width hint — dropped.
464 assert!(stripped["properties"]["n"].get("format").is_none());
465 assert_eq!(stripped["required"], json!(["n"]));
466 }
467
468 #[test]
469 fn strip_inlines_refs_and_drops_defs_envelope() {
470 let raw = json!({
471 "$schema": "https://json-schema.org/draft/2020-12/schema",
472 "title": "Outer",
473 "type": "object",
474 "properties": {"inner": {"$ref": "#/$defs/Inner"}},
475 "$defs": {
476 "Inner": {
477 "title": "Inner",
478 "type": "object",
479 "properties": {"x": {"type": "string"}},
480 "required": ["x"]
481 }
482 }
483 });
484 let stripped = LlmFacingSchema::strip(&raw);
485 assert!(stripped.get("$defs").is_none());
486 let inner = &stripped["properties"]["inner"];
487 // $ref resolved → inlined object, title gone.
488 assert_eq!(inner["type"], "object");
489 assert_eq!(inner["properties"]["x"]["type"], "string");
490 assert!(inner.get("title").is_none());
491 }
492
493 #[test]
494 fn strip_keeps_meaningful_format_specifiers() {
495 // `date-time`, `email`, `uri` are real vendor-honored
496 // formats — the noise list only targets width hints.
497 let raw = json!({
498 "type": "string",
499 "format": "date-time"
500 });
501 let stripped = LlmFacingSchema::strip(&raw);
502 assert_eq!(stripped["format"], "date-time");
503 }
504
505 #[test]
506 fn strip_breaks_self_referential_ref_cycle() {
507 // `Tree { children: Vec<Tree> }` — schemars emits a `$ref`
508 // pointing back into `Tree`'s own `$defs` body. Without
509 // cycle detection the inliner stack-overflows on this
510 // shape, which would be a DoS surface for operator-supplied
511 // schemas.
512 let raw = json!({
513 "$schema": "https://json-schema.org/draft/2020-12/schema",
514 "title": "Tree",
515 "type": "object",
516 "properties": {
517 "value": {"type": "string"},
518 "children": {
519 "type": "array",
520 "items": {"$ref": "#/$defs/Tree"}
521 }
522 },
523 "$defs": {
524 "Tree": {
525 "type": "object",
526 "properties": {
527 "value": {"type": "string"},
528 "children": {
529 "type": "array",
530 "items": {"$ref": "#/$defs/Tree"}
531 }
532 }
533 }
534 }
535 });
536 let stripped = LlmFacingSchema::strip(&raw);
537 // Convention (matches ajv, json-ref-resolver, etc.): the
538 // first expansion of a `$ref` along a recursion branch is
539 // performed; the *second* hit on the same name breaks the
540 // cycle. So `Tree.children.items` expands once into the
541 // Tree body, and the next `Tree.children.items` inside
542 // that expansion is the accept-any substitute.
543 assert_eq!(stripped["type"], "object");
544 assert_eq!(stripped["properties"]["value"]["type"], "string");
545 let one_level_deep = &stripped["properties"]["children"]["items"];
546 assert_eq!(one_level_deep["type"], "object");
547 assert_eq!(one_level_deep["properties"]["value"]["type"], "string");
548 let cycle_break = &one_level_deep["properties"]["children"]["items"];
549 assert_eq!(cycle_break, &json!({}));
550 }
551
552 #[test]
553 fn strip_breaks_mutually_recursive_ref_cycle() {
554 // `A { b: B }` and `B { a: A }` — alternating `$ref`s. The
555 // visited stack must catch the cycle at the second hop back
556 // to A, not just direct self-references.
557 let raw = json!({
558 "type": "object",
559 "properties": {"root": {"$ref": "#/$defs/A"}},
560 "$defs": {
561 "A": {
562 "type": "object",
563 "properties": {"b": {"$ref": "#/$defs/B"}}
564 },
565 "B": {
566 "type": "object",
567 "properties": {"a": {"$ref": "#/$defs/A"}}
568 }
569 }
570 });
571 let stripped = LlmFacingSchema::strip(&raw);
572 let root = &stripped["properties"]["root"];
573 assert_eq!(root["type"], "object");
574 // root.b expanded to B; B.a hits the cycle → accept-any.
575 let cycle_break = &root["properties"]["b"]["properties"]["a"];
576 assert_eq!(cycle_break, &json!({}));
577 }
578
579 #[test]
580 fn strip_inlines_shared_non_cyclic_ref_at_every_usage() {
581 // `Outer { x: Shared, y: Shared }` — `Shared` referenced
582 // twice but never recursively. Both usages must inline
583 // independently; the visited stack must `pop` after `x` so
584 // `y` can re-enter `Shared`.
585 let raw = json!({
586 "type": "object",
587 "properties": {
588 "x": {"$ref": "#/$defs/Shared"},
589 "y": {"$ref": "#/$defs/Shared"}
590 },
591 "$defs": {
592 "Shared": {"type": "string", "description": "shared scalar"}
593 }
594 });
595 let stripped = LlmFacingSchema::strip(&raw);
596 assert_eq!(stripped["properties"]["x"]["type"], "string");
597 assert_eq!(stripped["properties"]["x"]["description"], "shared scalar");
598 assert_eq!(stripped["properties"]["y"]["type"], "string");
599 assert_eq!(stripped["properties"]["y"]["description"], "shared scalar");
600 }
601}