ADR-0299:
derive StateView (@redact,
@window, @summary, State Schema &
Diffs)
- Status: Accepted
- Date: 2026-11-28
- Related: ADR-0291 (Freeze Rust, Grow in Tenon), ADR-0292
(Application Actions API & State Deltas), ADR-0293
(
app_actions_gen), ADR-0294 (Unifiedjson.lib.Value& Builders), ADR-0295 (Implicit CachedderiveMaterialization)
Context
Agentic and TUI applications exposing live state through
app_state / state_delta previously
hand-wrote three repetitive, error-prone projections for every
state struct:
- Manual
json.lib.Valueobject construction (project_state), risking accidentally serializing large binary buffers or leaking secrets in strings/logs/env maps. - Manual collection slicing + pagination metadata
(
total_*,*_truncated) so large vectors do not overflow LLM context windows. - Manual 1-line status summaries and JSON Schemas matching the projected state shape.
Per ADR-0291 ("Freeze Rust, Grow in Tenon"), this capability
belongs in a pure .tn GEN_ABI 2
generator plugin (capabilities = []) with zero Rust
compiler changes, leveraging Wave 1's unified
json.lib.Value + JsonEntry builders
(ADR-0294) and zero-step implicit cached derive materialization
(ADR-0295).
Decision
1. New
Pure Generator Plugin: plugins/state_view_gen/
We introduce state_view_gen
(kind = "generator", capabilities = [],
GEN_ABI 2) exporting
pub fn StateView(t: std.reflect.Type) -> result<str, str>.
For any annotated struct Foo (with
import json.lib { Value, JsonEntry } and
import state_view_gen.lib { StateView } in the host
module), derive StateView for Foo emits four
companion functions:
pub fn <snake>_project_state(state: Foo) -> Value— projects all non-omitted fields to a canonicaljson.lib.Value.Obj, applying redaction and windowing deterministically.pub fn <snake>_project_state_with(state: Foo, extra: vec<JsonEntry>) -> Value— projects state and overlays caller-supplied computedJsonEntryfields without duplicating projection logic.pub fn <snake>_state_summary(state: Foo) -> str— produces a single-line, newline-free summary (<= 256bytes, UTF-8 safe), honoring@summaryfield selection (or all projected fields if none are tagged) and@redactscrubbing.pub fn <snake>_state_schema_json() -> str— returns a pre-computed JSON Schema string verified at generation time againstmcp.wire.schema_supported(..., 0).
2. Field Annotations
Using Tenon's existing @key /
@key(literal) field attribute grammar (no Rust parser
changes):
@view/@view("alias")— include field in projection (default for non-omitted fields); optional string argument renames the projected JSON key;@view(false)omits the field.@omit— exclude heavy internal caches, buffers, or unsupported types before type checking.@redact— supported onstr,option<str>,vec<str>, andmap<str, str>; scrubs secrets viastate_delta.redactin bothproject_stateandstate_summary.@window(N)/@window("max = N, total_key = \"...\"")(or@window(N)+@total_key("...")) — supported onvec<T>; caps emitted elements toN, adds"maxItems": Nto the schema, and emits<total_key>(integer,minimum: 0, default<key>_total) plus<key>_truncated(boolean).@summary/@summary("label")— marks fields included in<snake>_state_summary(showingshown/totalfor truncated windowed vectors).
3. Supported Types & Fail-Closed Guarantees
Supported projected types: str, i64,
i32, u8, u32,
u64, f64, bool, unit
enum (snake_case strings), nested
struct (recursive up to depth 14 with cycle
detection, honoring nested
@omit/@redact/@window),
option<T>, vec<T>, and
map<str, str|i64|bool> (sorted keys for
deterministic diffs).
StateView fails closed (E0966) on
non-struct targets, conflicting annotations (e.g.
@omit with
@redact/@window/@summary),
@redact on non-string types, @window on
non-vec types or max <= 0, duplicate
projected keys (including
total_key/*_truncated collisions),
struct cycles, unsupported non-omitted types (bytes,
non-unit enums, option<option<T>>), or
any schema rejected by mcp.wire.schema_supported.
4.
Secret Assignment Scrubbing in
plugins/state_delta/redact.tn
We extend state_delta.redact with pure helpers
used by state_view_gen.rt and
redact_value:
redact_secret_assignments(s: str) -> str— scrubsKEY=VALUE,KEY="VALUE",KEY: VALUE,Authorization: Bearer <tok>,sk-<tok>, andghp_/xoxb-provider tokens while preserving key names.redact_str(s: str, policy: RedactPolicy) -> strredact_opt_str(v: option<str>, policy: RedactPolicy) -> option<str>redact_vec(items: vec<str>, policy: RedactPolicy) -> vec<str>
Consequences
- Positive: Any complex multi-collection state
struct gets redacted, context-windowed
json.lib.Valueprojection (directly diffable withstate_delta.diff), a 1-line summary, a custom-field hook, and anmcp.wire-compatible schema in a singlederive StateViewline, with identical behavior on native C andtenon interpand zero Rust changes. - Trade-off: Because
GEN_ABI 2sandboxes generated companion imports tostate_view_gen.rt, deriving modules must importValueandJsonEntryfromjson.lib(mirroringapp_actions_genrequiringActionSpecandcodec.JsonValuerequiringValue). - Coordinator Wiring Note:
state_view_genis ready to be added toMEMBERS_2026incrates/tenon-compiler/src/editions.rsandcrates/tenon-assets/build.rsat the Wave 2 integration barrier.