cpex-hosts-python 0.3.1

CPEX host — runs Python plugins out-of-process in isolated virtualenvs.
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
// Location: ./crates/cpex-hosts-python/src/legacy/payloads.rs
// Copyright 2026
// SPDX-License-Identifier: Apache-2.0
// Authors: Ted Habeck
//
// Typed payloads for the eight legacy (non-CMF) hooks.
//
// Each struct's serialized JSON must match the Pydantic model `worker.py`
// reconstructs via `json_to_payload`, field for field — a name mismatch
// surfaces as a Pydantic validation error inside the worker, at invoke time,
// far from its cause. The Python model each one mirrors is named in its doc
// comment; the field-shape tests in this module are the guard.
//
// # Redaction
//
// `IdentityResolvePayload` and `TokenDelegatePayload` carry token-adjacent
// fields, so both get a hand-written `Debug` that prints a placeholder instead
// of the value. A derive would put credential material into any `{:?}` — a
// tracing call, an `unwrap()` panic message, an assertion failure. The
// equivalent gap on the pre-existing praxis-policy-core types is deferred follow-up
// work; these new types do not reintroduce it.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// Placeholder printed in place of any token-adjacent value.
const REDACTED: &str = "<redacted>";

/// `tool_pre_invoke` — mirrors `ToolPreInvokePayload`.
///
/// `headers` mirrors `HttpHeaderPayload`, which serializes as a plain string
/// map.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ToolPreInvokePayload {
    pub name: String,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub args: Option<HashMap<String, serde_json::Value>>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub headers: Option<HashMap<String, String>>,
}
ppe::praxis_policy_core::impl_plugin_payload!(ToolPreInvokePayload);

/// `tool_post_invoke` — mirrors `ToolPostInvokePayload`.
///
/// `result` is `Any` on the Python side, so it stays an untyped JSON value.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ToolPostInvokePayload {
    pub name: String,

    #[serde(default)]
    pub result: serde_json::Value,
}
ppe::praxis_policy_core::impl_plugin_payload!(ToolPostInvokePayload);

/// `prompt_pre_fetch` — mirrors `PromptPrehookPayload`.
///
/// Note `args` is `dict[str, str]` here, not `dict[str, Any]` as on the tool
/// payload.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PromptPreFetchPayload {
    pub prompt_id: String,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub args: Option<HashMap<String, String>>,
}
ppe::praxis_policy_core::impl_plugin_payload!(PromptPreFetchPayload);

/// `prompt_post_fetch` — mirrors `PromptPosthookPayload`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PromptPostFetchPayload {
    pub prompt_id: String,

    #[serde(default)]
    pub result: serde_json::Value,
}
ppe::praxis_policy_core::impl_plugin_payload!(PromptPostFetchPayload);

/// `resource_pre_fetch` — mirrors `ResourcePreFetchPayload`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourcePreFetchPayload {
    pub uri: String,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, serde_json::Value>>,
}
ppe::praxis_policy_core::impl_plugin_payload!(ResourcePreFetchPayload);

/// `resource_post_fetch` — mirrors `ResourcePostFetchPayload`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourcePostFetchPayload {
    pub uri: String,

    #[serde(default)]
    pub content: serde_json::Value,
}
ppe::praxis_policy_core::impl_plugin_payload!(ResourcePostFetchPayload);

/// `identity.resolve` mirrors `IdentityPayload`. The worker knows the hook as
/// `identity_resolve`.
///
/// `raw_token` is `SecretStr` on the Python side and redacts on serialization,
/// so the value that travels in *this* field is a placeholder, never the real
/// token. The plaintext rides the separate capability-gated `credential`
/// object on the task (see the `credentials` module) and the worker folds it
/// back onto the payload before calling the plugin.
///
/// `Debug` is hand-written — see the module docs.
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct IdentityResolvePayload {
    /// Redacted on the wire. Populated from the `credential` object by the
    /// worker, not from this field.
    #[serde(default)]
    pub raw_token: String,

    /// How the credential was presented — `bearer`, `custom`, and so on.
    #[serde(default)]
    pub source: String,

    /// Headers the credential arrived in. Not a `SecretStr` on the Python
    /// side, so the worker scrubs the plaintext out of these values before a
    /// plugin can echo them back.
    #[serde(default)]
    pub headers: HashMap<String, String>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_host: Option<String>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_port: Option<u16>,
}
ppe::praxis_policy_core::impl_plugin_payload!(IdentityResolvePayload);

impl std::fmt::Debug for IdentityResolvePayload {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // `raw_token` and every header value are redacted: a header can carry
        // the credential ("Authorization: Bearer <token>"), so printing keys
        // alone is the safe maximum.
        f.debug_struct("IdentityResolvePayload")
            .field("raw_token", &REDACTED)
            .field("source", &self.source)
            .field("header_names", &self.headers.keys().collect::<Vec<_>>())
            .field("client_host", &self.client_host)
            .field("client_port", &self.client_port)
            .finish()
    }
}

/// Scope-attenuation config — mirrors `AttenuationConfig`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AttenuationConfig {
    #[serde(default)]
    pub capabilities: Vec<String>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resource_template: Option<String>,

    #[serde(default)]
    pub actions: Vec<String>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ttl_seconds: Option<i64>,
}

/// `token.delegate` mirrors `DelegationPayload`. The worker knows the hook as
/// `token_delegate`.
///
/// `bearer_token` is `SecretStr | None` on the Python side and redacts on
/// serialization, so this field carries a placeholder. The plaintext travels
/// via the `credential` object.
///
/// `Debug` is hand-written — see the module docs.
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct TokenDelegatePayload {
    pub target_name: String,

    #[serde(default = "default_target_type")]
    pub target_type: String,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_audience: Option<String>,

    #[serde(default)]
    pub required_permissions: Vec<String>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trust_domain: Option<String>,

    #[serde(default = "default_auth_enforced_by")]
    pub auth_enforced_by: String,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub route_attenuation: Option<AttenuationConfig>,

    /// Redacted on the wire; the plaintext comes from the `credential` object.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bearer_token: Option<String>,
}
ppe::praxis_policy_core::impl_plugin_payload!(TokenDelegatePayload);

fn default_target_type() -> String {
    "tool".to_string()
}

fn default_auth_enforced_by() -> String {
    "caller".to_string()
}

impl std::fmt::Debug for TokenDelegatePayload {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TokenDelegatePayload")
            .field("target_name", &self.target_name)
            .field("target_type", &self.target_type)
            .field("target_audience", &self.target_audience)
            .field("required_permissions", &self.required_permissions)
            .field("trust_domain", &self.trust_domain)
            .field("auth_enforced_by", &self.auth_enforced_by)
            .field("route_attenuation", &self.route_attenuation)
            // Presence is useful for diagnosis; the value never is.
            .field(
                "bearer_token",
                &self.bearer_token.as_ref().map(|_| REDACTED),
            )
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Serialize and return the JSON object, so field names can be asserted
    /// against the Pydantic models `worker.py` reconstructs.
    fn json_of<T: Serialize>(value: &T) -> serde_json::Value {
        serde_json::to_value(value).expect("payload serializes")
    }

    #[test]
    fn tool_pre_invoke_matches_the_python_field_shape() {
        let payload = ToolPreInvokePayload {
            name: "search".into(),
            args: Some(HashMap::from([("q".into(), serde_json::json!("rust"))])),
            headers: Some(HashMap::from([("X-Tenant".into(), "acme".into())])),
        };
        let json = json_of(&payload);

        assert_eq!(json["name"], "search");
        assert_eq!(json["args"]["q"], "rust");
        assert_eq!(json["headers"]["X-Tenant"], "acme");
    }

    #[test]
    fn tool_pre_invoke_omits_absent_optionals() {
        // Pydantic defaults `args` to {} and `headers` to None. Emitting
        // explicit nulls would override those defaults on the Python side, so
        // absent fields must stay absent.
        let json = json_of(&ToolPreInvokePayload {
            name: "search".into(),
            ..Default::default()
        });
        assert_eq!(
            json.as_object().unwrap().len(),
            1,
            "only `name` should be present: {json}"
        );
    }

    #[test]
    fn tool_pre_invoke_round_trips() {
        let payload = ToolPreInvokePayload {
            name: "search".into(),
            args: Some(HashMap::from([("q".into(), serde_json::json!(7))])),
            headers: None,
        };
        let restored: ToolPreInvokePayload = serde_json::from_value(json_of(&payload)).unwrap();
        assert_eq!(restored.name, "search");
        assert_eq!(restored.args.unwrap()["q"], 7);
    }

    #[test]
    fn tool_post_invoke_matches_the_python_field_shape() {
        let json = json_of(&ToolPostInvokePayload {
            name: "search".into(),
            result: serde_json::json!({"hits": 3}),
        });
        assert_eq!(json["name"], "search");
        assert_eq!(json["result"]["hits"], 3);
    }

    #[test]
    fn prompt_payloads_use_prompt_id_not_name() {
        // The prompt hooks key on `prompt_id`; using `name` here would fail
        // Pydantic validation inside the worker.
        let pre = json_of(&PromptPreFetchPayload {
            prompt_id: "greeting".into(),
            args: Some(HashMap::from([("lang".into(), "en".into())])),
        });
        assert_eq!(pre["prompt_id"], "greeting");
        assert_eq!(pre["args"]["lang"], "en");
        assert!(pre.get("name").is_none());

        let post = json_of(&PromptPostFetchPayload {
            prompt_id: "greeting".into(),
            result: serde_json::json!({"messages": []}),
        });
        assert_eq!(post["prompt_id"], "greeting");
        assert!(post["result"]["messages"].is_array());
    }

    #[test]
    fn prompt_pre_fetch_args_are_strings() {
        // dict[str, str] on the Python side, so a non-string value must not
        // deserialize into it.
        let parsed: Result<PromptPreFetchPayload, _> =
            serde_json::from_value(serde_json::json!({ "prompt_id": "p", "args": {"n": 5} }));
        assert!(
            parsed.is_err(),
            "prompt args are dict[str, str], not dict[str, Any]"
        );
    }

    #[test]
    fn resource_payloads_key_on_uri() {
        let pre = json_of(&ResourcePreFetchPayload {
            uri: "file:///tmp/x".into(),
            metadata: Some(HashMap::from([("etag".into(), serde_json::json!("abc"))])),
        });
        assert_eq!(pre["uri"], "file:///tmp/x");
        assert_eq!(pre["metadata"]["etag"], "abc");

        let post = json_of(&ResourcePostFetchPayload {
            uri: "file:///tmp/x".into(),
            content: serde_json::json!("hello"),
        });
        assert_eq!(post["content"], "hello");
    }

    #[test]
    fn identity_resolve_matches_the_python_field_shape() {
        let json = json_of(&IdentityResolvePayload {
            raw_token: "**********".into(),
            source: "bearer".into(),
            headers: HashMap::from([("Authorization".into(), "**********".into())]),
            client_host: Some("10.0.0.1".into()),
            client_port: Some(443),
        });

        assert_eq!(json["source"], "bearer");
        assert_eq!(json["client_host"], "10.0.0.1");
        assert_eq!(json["client_port"], 443);
        assert!(
            json.get("raw_token").is_some(),
            "the field exists; its value is a placeholder"
        );
    }

    #[test]
    fn token_delegate_matches_the_python_field_shape_and_defaults() {
        let parsed: TokenDelegatePayload =
            serde_json::from_value(serde_json::json!({ "target_name": "billing-api" })).unwrap();

        // Pydantic defaults these two; the host must agree or a minimal
        // payload would deserialize with empty strings.
        assert_eq!(parsed.target_type, "tool");
        assert_eq!(parsed.auth_enforced_by, "caller");
        assert!(parsed.required_permissions.is_empty());
        assert!(parsed.bearer_token.is_none());
    }

    #[test]
    fn token_delegate_carries_attenuation_config() {
        let json = json_of(&TokenDelegatePayload {
            target_name: "billing-api".into(),
            route_attenuation: Some(AttenuationConfig {
                capabilities: vec!["read".into()],
                resource_template: Some("/v1/*".into()),
                actions: vec!["GET".into()],
                ttl_seconds: Some(300),
            }),
            ..Default::default()
        });

        assert_eq!(json["route_attenuation"]["capabilities"][0], "read");
        assert_eq!(json["route_attenuation"]["ttl_seconds"], 300);
    }

    // --- redaction ----------------------------------------------------------

    #[test]
    fn identity_resolve_debug_hides_token_and_header_values() {
        let payload = IdentityResolvePayload {
            raw_token: "eyJhbGciOiJSUzI1NiJ9.SECRET-TOKEN-BYTES".into(),
            source: "bearer".into(),
            headers: HashMap::from([("Authorization".into(), "Bearer SECRET-TOKEN-BYTES".into())]),
            client_host: None,
            client_port: None,
        };

        let debug = format!("{payload:?}");
        assert!(
            !debug.contains("SECRET-TOKEN-BYTES"),
            "token material leaked into Debug: {debug}"
        );
        // The header *name* is still useful for diagnosis.
        assert!(debug.contains("Authorization"));
        assert!(
            debug.contains("bearer"),
            "non-secret fields stay readable: {debug}"
        );
    }

    #[test]
    fn token_delegate_debug_hides_the_bearer_token() {
        let payload = TokenDelegatePayload {
            target_name: "billing-api".into(),
            bearer_token: Some("MINTED-SECRET-BYTES".into()),
            ..Default::default()
        };

        let debug = format!("{payload:?}");
        assert!(
            !debug.contains("MINTED-SECRET-BYTES"),
            "delegated token leaked into Debug: {debug}"
        );
        // Presence is diagnosable without exposing the value.
        assert!(debug.contains("billing-api"));
        assert!(debug.contains(REDACTED));
    }

    #[test]
    fn token_delegate_debug_distinguishes_absent_from_present() {
        let absent = format!(
            "{:?}",
            TokenDelegatePayload {
                target_name: "x".into(),
                ..Default::default()
            }
        );
        assert!(
            absent.contains("None"),
            "an absent token should read as None: {absent}"
        );
        assert!(!absent.contains(REDACTED));
    }
}