cc_lb_plugin_wire/v1/mod.rs
1//! Wire schema version 1 (baseline).
2//!
3//! Shared types between host (`cc-lb-runtime-wasmtime`) and guest
4//! (`cc-lb-pdk-wasmtime`) compiled in lockstep.
5//!
6//! Wire types for the three hooks the wasmtime runtime ships:
7//! filter (Phase 1), shape (Phase 2), observe
8//! (Phase 2). Signer extension is intentionally not exposed across
9//! the plugin boundary — host-side built-in `AnthropicKeySigner` /
10//! `AnthropicOAuthSigner` handle credential signing in-process.
11//!
12//! rkyv derives `Archive` + `Serialize` + `Deserialize` for every wire type.
13//! The host calls `rkyv::access::<ArchivedFilterRequest, rkyv::rancor::Error>`
14//! to get a zero-copy `&ArchivedFilterRequest` view straight out of guest
15//! linear memory; the guest mirrors that pattern in reverse for the response.
16//!
17//! ## ABI invariants (review consensus)
18//!
19//! * Every guest-allocated buffer is aligned to `align_of::<Archived<T>>()`
20//! for its root type. rkyv 0.8's default relative pointers require this;
21//! the alignment is propagated through `cc_lb_alloc(size, align)`.
22//! * `Arc`/`Rc` MUST NOT appear in any wire type (rkyv 0.8 Issue #670 —
23//! `ArchivedRc::verify` can be bypassed for DST shared pointers).
24//! * The wire schema is fingerprinted via BLAKE3 of `cc_lb_schema_hash`
25//! custom section content. Host and guest must agree byte-for-byte.
26//!
27//! ## Borrowed-wire optimisation (RFC-0001 #9)
28//!
29//! Host-to-guest request types (FilterRequest, ShapeRequest) come in
30//! two flavours that produce IDENTICAL archived bytes:
31//! * **Owned** (`FilterRequest`, `ShapeRequest`, ...) — owned `Box<str>`
32//! / `Box<[u8]>` fields. Used by the guest for `rkyv::deserialize`
33//! round-trips and for round-trip test fixtures.
34//! * **Borrowed ref** (`FilterRequestRef<'a>`, `ShapeRequestRef<'a>`,
35//! ...) — `&'a str` / `&'a [u8]` fields with `#[rkyv(with =
36//! InlineAsBox)]`. Used by the host in `wire_to_host_wire_request`
37//! so the request body (up to 100 MiB on `/v1/files`) is serialised
38//! IN PLACE from the request pipeline's `bytes::Bytes` without any
39//! `.to_vec()` copy.
40//!
41//! Both variants archive to the same `ArchivedBox<ArchivedSlice<u8>>`
42//! / `ArchivedBox<ArchivedStr>` byte layouts — verified by the wire
43//! round-trip tests in `crates/cc-lb-plugin-wire/tests/borrowed_wire_roundtrip.rs`.
44//! When either variant is written to guest memory, the guest reads it
45//! via `rkyv::access::<ArchivedFilterRequest, _>` — the archived
46//! type name is identical because the owned type is the sole `Archive`
47//! source of truth; the ref type carries `#[rkyv(archived = ...)]` to
48//! reuse the owned type's archived form.
49//!
50//! See `docs/rfc/0001-plugin-runtime-vnext.md`.
51
52use alloc::boxed::Box;
53
54use rkyv::{Archive, Deserialize, Serialize, with::InlineAsBox};
55
56/// Principal context as seen by the filter plugin.
57///
58/// Mirrors `cc_lb_plugin_api::Principal` semantically but the rkyv-derived
59/// wire form is the source of truth on the host↔guest boundary.
60#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
61#[rkyv(derive(Debug))]
62pub struct Principal {
63 pub id: Box<str>,
64 pub kind: Box<str>,
65 /// Free-form key/value claims. Bytes intentionally, not JSON.
66 /// Modelled as a named `Claim` struct (not a tuple) so the
67 /// borrowed [`ClaimRef`] and this owned form archive to the same
68 /// byte layout for host/guest wire compatibility.
69 pub claims: Box<[Claim]>,
70}
71
72/// One claim key/value pair — see [`Principal::claims`]. Named
73/// struct (not tuple) so that `ClaimRef<'a>` matches the archived
74/// byte layout without an extra Ref wrapper.
75#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
76#[rkyv(derive(Debug))]
77pub struct Claim {
78 pub key: Box<str>,
79 pub value: Box<[u8]>,
80}
81
82/// Borrowed mirror of [`Principal`] used by the host encode path. Every
83/// reference field carries `#[rkyv(with = InlineAsBox)]` so serialising
84/// this struct emits the same archived byte layout as [`Principal`].
85#[derive(Archive, Serialize)]
86pub struct PrincipalRef<'a> {
87 #[rkyv(with = InlineAsBox)]
88 pub id: &'a str,
89 #[rkyv(with = InlineAsBox)]
90 pub kind: &'a str,
91 #[rkyv(with = InlineAsBox)]
92 pub claims: &'a [ClaimRef<'a>],
93}
94
95/// One claim entry inside [`PrincipalRef`]. The tuple form used by the
96/// owned type does not have a straight borrowed equivalent, so we
97/// promote it to a named struct with two borrowed fields.
98#[derive(Archive, Serialize)]
99pub struct ClaimRef<'a> {
100 #[rkyv(with = InlineAsBox)]
101 pub key: &'a str,
102 #[rkyv(with = InlineAsBox)]
103 pub value: &'a [u8],
104}
105
106/// One upstream candidate the filter plugin can choose to keep or drop.
107#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
108#[rkyv(derive(Debug))]
109pub struct UpstreamCandidate {
110 pub upstream_id: Box<str>,
111 pub name: Box<str>,
112 pub kind: Box<str>,
113 pub observed_at_unix_secs: u64,
114 pub predicted_cache_read_tokens: u32,
115}
116
117/// Borrowed mirror of [`UpstreamCandidate`].
118#[derive(Archive, Serialize)]
119pub struct UpstreamCandidateRef<'a> {
120 #[rkyv(with = InlineAsBox)]
121 pub upstream_id: &'a str,
122 #[rkyv(with = InlineAsBox)]
123 pub name: &'a str,
124 #[rkyv(with = InlineAsBox)]
125 pub kind: &'a str,
126 pub observed_at_unix_secs: u64,
127 pub predicted_cache_read_tokens: u32,
128}
129
130/// One header on the inbound request.
131#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
132#[rkyv(derive(Debug))]
133pub struct Header {
134 pub name: Box<str>,
135 /// Raw header bytes — never base64'd. The whole point of the rkyv wire is
136 /// to remove that encode/decode hop.
137 pub value: Box<[u8]>,
138}
139
140/// Borrowed mirror of [`Header`].
141#[derive(Archive, Serialize)]
142pub struct HeaderRef<'a> {
143 #[rkyv(with = InlineAsBox)]
144 pub name: &'a str,
145 #[rkyv(with = InlineAsBox)]
146 pub value: &'a [u8],
147}
148
149/// Filter hook input (owned form used by the guest).
150#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
151#[rkyv(derive(Debug))]
152pub struct FilterRequest {
153 pub request_id: Box<str>,
154 pub method: Box<str>,
155 pub path: Box<str>,
156 pub query: Option<Box<str>>,
157 pub headers: Box<[Header]>,
158 pub body: Box<[u8]>,
159 pub principal: Principal,
160 pub candidates: Box<[UpstreamCandidate]>,
161}
162
163/// Borrowed mirror of [`FilterRequest`] used by the host encode path.
164/// Serialising this emits the same archived byte layout as
165/// [`FilterRequest`] so the guest reads it via
166/// `rkyv::access::<ArchivedFilterRequest, _>` unchanged.
167#[derive(Archive, Serialize)]
168pub struct FilterRequestRef<'a> {
169 #[rkyv(with = InlineAsBox)]
170 pub request_id: &'a str,
171 #[rkyv(with = InlineAsBox)]
172 pub method: &'a str,
173 #[rkyv(with = InlineAsBox)]
174 pub path: &'a str,
175 pub query: Option<QueryRef<'a>>,
176 #[rkyv(with = InlineAsBox)]
177 pub headers: &'a [HeaderRef<'a>],
178 #[rkyv(with = InlineAsBox)]
179 pub body: &'a [u8],
180 pub principal: PrincipalRef<'a>,
181 #[rkyv(with = InlineAsBox)]
182 pub candidates: &'a [UpstreamCandidateRef<'a>],
183}
184
185/// Borrowed wrapper for the optional `query` field. Necessary because
186/// `Option<InlineAsBox<&'a str>>` does not compose directly at the
187/// derive layer; a named struct pushes the `#[rkyv(with = ...)]`
188/// attribute onto the inner reference.
189#[derive(Archive, Serialize)]
190pub struct QueryRef<'a> {
191 #[rkyv(with = InlineAsBox)]
192 pub value: &'a str,
193}
194
195/// Decision the plugin made for one candidate.
196#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
197#[rkyv(derive(Debug))]
198pub struct PerCandidateReason {
199 pub upstream_id: Box<str>,
200 /// `"accept"` or `"reject"` — kept as string so plugins remain forward-
201 /// compatible with future decision variants without a host re-bump.
202 pub decision: Box<str>,
203 pub reason: Box<str>,
204}
205
206/// Filter hook output.
207///
208/// Guest packs `(out_ptr, out_len)` into a single `u64` (`(ptr << 32) | len`)
209/// for the return value of `cc_lb_filter`. The host reads the bytes via
210/// `Memory::data(&store)` and runs `rkyv::access::<ArchivedFilterResponse, _>`.
211#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
212#[rkyv(derive(Debug))]
213pub struct FilterResponse {
214 pub results: Box<[PerCandidateReason]>,
215}
216
217/// Upstream backend exposed to plugins. Mirrors
218/// `cc_lb_plugin_api::Upstream` (currently a single variant —
219/// extending the host enum requires extending this one in lockstep
220/// and bumping a wire schema tag).
221#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
222#[rkyv(derive(Debug))]
223pub enum Upstream {
224 AnthropicDirect {
225 /// Operator-configured base URL override, if any.
226 base_url: Option<Box<str>>,
227 },
228}
229
230/// Borrowed mirror of [`Upstream`].
231#[derive(Archive, Serialize)]
232pub enum UpstreamRef<'a> {
233 AnthropicDirect { base_url: Option<QueryRef<'a>> },
234}
235
236/// Shape hook input (owned form).
237#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
238#[rkyv(derive(Debug))]
239pub struct ShapeRequest {
240 pub request_id: Box<str>,
241 pub method: Box<str>,
242 pub path: Box<str>,
243 pub query: Option<Box<str>>,
244 pub headers: Box<[Header]>,
245 pub body: Box<[u8]>,
246 pub principal: Principal,
247 pub upstream: Upstream,
248}
249
250/// Borrowed mirror of [`ShapeRequest`] used by the host encode path.
251#[derive(Archive, Serialize)]
252pub struct ShapeRequestRef<'a> {
253 #[rkyv(with = InlineAsBox)]
254 pub request_id: &'a str,
255 #[rkyv(with = InlineAsBox)]
256 pub method: &'a str,
257 #[rkyv(with = InlineAsBox)]
258 pub path: &'a str,
259 pub query: Option<QueryRef<'a>>,
260 #[rkyv(with = InlineAsBox)]
261 pub headers: &'a [HeaderRef<'a>],
262 #[rkyv(with = InlineAsBox)]
263 pub body: &'a [u8],
264 pub principal: PrincipalRef<'a>,
265 pub upstream: UpstreamRef<'a>,
266}
267
268/// Shape hook output — the upstream-bound request the plugin produced.
269#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
270#[rkyv(derive(Debug))]
271pub struct ShapeResponse {
272 pub url: Box<str>,
273 pub method: Box<str>,
274 pub headers: Box<[Header]>,
275 pub body: Box<[u8]>,
276}
277
278/// Lifecycle event delivered to the observe hook. rkyv mirror of
279/// `cc_lb_plugin_api::ObserveEvent`.
280#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
281#[rkyv(derive(Debug))]
282pub enum ObserveEvent {
283 RequestStarted {
284 request_id: Box<str>,
285 downstream_user_agent: Option<Box<str>>,
286 },
287 AuthnComplete {
288 principal_id: Box<str>,
289 principal_kind: Box<str>,
290 },
291 UpstreamChosen {
292 upstream: Upstream,
293 },
294 Chunk {
295 batch_index: u64,
296 event_count: u64,
297 total_bytes: u64,
298 },
299 RequestFinished {
300 status: u16,
301 input_tokens: Option<u64>,
302 output_tokens: Option<u64>,
303 cache_creation_input_tokens: Option<u64>,
304 cache_read_input_tokens: Option<u64>,
305 duration_ms: u64,
306 },
307 Error {
308 code: Box<str>,
309 message: Box<str>,
310 source: Box<str>,
311 },
312}
313
314include!(concat!(env!("OUT_DIR"), "/wire_schema_impls.rs"));