agent-framework-purview 0.1.1

Microsoft Purview compliance middleware
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
//! [`PurviewAgentMiddleware`] and [`PurviewChatMiddleware`]: enforce Purview
//! policy on both the outgoing prompt and the model/agent response. Mirrors
//! Python's `PurviewPolicyMiddleware` (agent-level) and
//! `PurviewChatPolicyMiddleware` (chat-client-level) — see the crate docs
//! for which hook points these attach to and why both directions evaluate
//! with [`Activity::UploadText`](crate::models::Activity::UploadText).

use async_trait::async_trait;

use agent_framework_core::error::Result;
use agent_framework_core::middleware::{AgentContext, ChatContext, Middleware, Next};
use agent_framework_core::types::{AgentResponse, ChatResponse, Message, Role};

use crate::auth::TokenProvider;
use crate::client::PurviewClient;
use crate::processor::ContentProcessor;
use crate::settings::PurviewSettings;

/// Shared evaluation core for [`PurviewAgentMiddleware`] and
/// [`PurviewChatMiddleware`]: identical policy logic, only the surrounding
/// context type differs — matching how the two Python middleware classes
/// each build their own `PurviewClient`/`ScopedContentProcessor` but run the
/// exact same `process_messages` calls.
struct PurviewPolicyCore {
    processor: ContentProcessor,
    settings: PurviewSettings,
}

impl PurviewPolicyCore {
    fn new(token_provider: impl TokenProvider + 'static, settings: PurviewSettings) -> Self {
        let client = PurviewClient::new(token_provider, &settings);
        Self {
            processor: ContentProcessor::new(client),
            settings,
        }
    }

    /// Evaluate `messages`, folding a suppressed error into an "allow, carry
    /// the previously-known user id forward" outcome.
    ///
    /// Mirrors the two-tier `except PurviewPaymentRequiredError: ... except
    /// Exception: ...` structure in both Python middleware classes: a 402
    /// is checked against [`PurviewSettings::ignore_payment_required`]
    /// *independently of* [`PurviewSettings::ignore_exceptions`], which
    /// governs every other error. An unsuppressed error propagates (`?` at
    /// the call site), same as Python re-raising.
    async fn check(
        &self,
        messages: &[Message],
        provided_user_id: Option<&str>,
        phase: &'static str,
    ) -> Result<(bool, Option<String>)> {
        match self
            .processor
            .evaluate(messages, &self.settings, provided_user_id)
            .await
        {
            Ok(outcome) => Ok(outcome),
            Err(e) => {
                let suppress = if e.status() == Some(402) {
                    self.settings.ignore_payment_required
                } else {
                    self.settings.ignore_exceptions
                };
                if suppress {
                    tracing::error!(error = %e, phase, "Purview policy check failed; continuing (ignored per settings)");
                    Ok((false, provided_user_id.map(str::to_string)))
                } else {
                    Err(e)
                }
            }
        }
    }

    fn blocked_prompt_message(&self) -> Message {
        Message::new(Role::system(), self.settings.blocked_prompt_message.clone())
    }

    fn blocked_response_message(&self) -> Message {
        Message::new(
            Role::system(),
            self.settings.blocked_response_message.clone(),
        )
    }
}

/// SupportsAgentRun middleware enforcing Purview policy on both the outgoing prompt and
/// the agent's response. Mirrors Python's `PurviewPolicyMiddleware`.
///
/// - **Prompt (pre) check**: evaluates `ctx.messages`. If blocked,
///   short-circuits with a single `system`-role message
///   ([`PurviewSettings::blocked_prompt_message`]) and `ctx.terminate =
///   true` — the wrapped agent/chat-client is never called, matching this
///   crate's `ShortCircuitChat`-style test pattern in
///   `agent-framework-core`.
/// - **Response (post) check**: only runs when `!ctx.is_streaming` and a
///   result was produced (mirrors Python: "Streaming responses are not
///   supported for post-checks"). If blocked, `ctx.result` is *replaced*
///   with a single `system`-role message
///   ([`PurviewSettings::blocked_response_message`]) — unlike the prompt
///   check, `ctx.terminate` is not set here (there's nothing left to
///   terminate; the underlying call already happened), matching Python.
///
/// ```no_run
/// use agent_framework_core::prelude::*;
/// use agent_framework_purview::{PurviewAgentMiddleware, PurviewSettings, PurviewAppLocation, PurviewLocationType, StaticTokenProvider};
/// use std::sync::Arc;
///
/// # fn demo(client: impl ChatClient + 'static) {
/// let settings = PurviewSettings::new("My App")
///     .with_tenant_id("00000000-0000-0000-0000-000000000000")
///     .with_purview_app_location(PurviewAppLocation::new(
///         PurviewLocationType::Application,
///         "00000000-0000-0000-0000-000000000001",
///     ));
/// let middleware = PurviewAgentMiddleware::new(StaticTokenProvider::new("<graph-bearer-token>"), settings);
///
/// let agent = Agent::builder(client)
///     .instructions("You are a helpful assistant.")
///     .middleware(Arc::new(middleware))
///     .build();
/// # let _ = agent;
/// # }
/// ```
pub struct PurviewAgentMiddleware(PurviewPolicyCore);

impl PurviewAgentMiddleware {
    pub fn new(token_provider: impl TokenProvider + 'static, settings: PurviewSettings) -> Self {
        Self(PurviewPolicyCore::new(token_provider, settings))
    }
}

#[async_trait]
impl Middleware<AgentContext> for PurviewAgentMiddleware {
    async fn process(
        &self,
        mut ctx: AgentContext,
        next: Next<AgentContext>,
    ) -> Result<AgentContext> {
        let (should_block, resolved_user_id) = self.0.check(&ctx.messages, None, "prompt").await?;
        if should_block {
            ctx.result = Some(AgentResponse {
                messages: vec![self.0.blocked_prompt_message()],
                ..Default::default()
            });
            ctx.terminate = true;
            return Ok(ctx);
        }

        let mut ctx = next.run(ctx).await?;

        if !ctx.is_streaming {
            let post_messages = ctx.result.as_ref().map(|r| r.messages.clone());
            if let Some(messages) = post_messages {
                let (should_block, _) = self
                    .0
                    .check(&messages, resolved_user_id.as_deref(), "response")
                    .await?;
                if should_block {
                    ctx.result = Some(AgentResponse {
                        messages: vec![self.0.blocked_response_message()],
                        ..Default::default()
                    });
                }
            }
        }
        Ok(ctx)
    }
}

/// Chat-client middleware variant of [`PurviewAgentMiddleware`], for
/// attaching Purview enforcement directly to a [`ChatClient`](agent_framework_core::client::ChatClient)
/// rather than an agent's middleware pipeline. Mirrors Python's
/// `PurviewChatPolicyMiddleware`; the policy logic is identical (see
/// the internal `PurviewPolicyCore`) — only the hook point (and result/message types)
/// differ.
pub struct PurviewChatMiddleware(PurviewPolicyCore);

impl PurviewChatMiddleware {
    pub fn new(token_provider: impl TokenProvider + 'static, settings: PurviewSettings) -> Self {
        Self(PurviewPolicyCore::new(token_provider, settings))
    }
}

#[async_trait]
impl Middleware<ChatContext> for PurviewChatMiddleware {
    async fn process(&self, mut ctx: ChatContext, next: Next<ChatContext>) -> Result<ChatContext> {
        let (should_block, resolved_user_id) = self.0.check(&ctx.messages, None, "prompt").await?;
        if should_block {
            ctx.result = Some(ChatResponse {
                messages: vec![self.0.blocked_prompt_message()],
                ..Default::default()
            });
            ctx.terminate = true;
            return Ok(ctx);
        }

        let mut ctx = next.run(ctx).await?;

        if !ctx.is_streaming {
            let post_messages = ctx.result.as_ref().map(|r| r.messages.clone());
            if let Some(messages) = post_messages {
                let (should_block, _) = self
                    .0
                    .check(&messages, resolved_user_id.as_deref(), "response")
                    .await?;
                if should_block {
                    ctx.result = Some(ChatResponse {
                        messages: vec![self.0.blocked_response_message()],
                        ..Default::default()
                    });
                }
            }
        }
        Ok(ctx)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::StaticTokenProvider;
    use crate::settings::{PurviewAppLocation, PurviewLocationType};
    use agent_framework_core::middleware::{MiddlewarePipeline, Terminal};
    use agent_framework_core::tools::BoxFuture;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    fn valid_settings() -> PurviewSettings {
        PurviewSettings::new("Test App")
            .with_tenant_id("12345678-1234-1234-1234-123456789012")
            .with_purview_app_location(PurviewAppLocation::new(
                PurviewLocationType::Application,
                "app-1",
            ))
    }

    // These tests never reach the network: a missing tenant id / app
    // location fails deterministically before any HTTP call is attempted,
    // and "no resolvable user id" short-circuits to an allow verdict before
    // one too (see `ContentProcessor::evaluate`). That's enough to exercise
    // `PurviewAgentMiddleware`/`PurviewChatMiddleware::process`'s
    // short-circuit and error-propagation logic hermetically, using a "mock
    // next" continuation built from `agent-framework-core`'s own
    // `MiddlewarePipeline`/`Terminal` — the same machinery `Agent` uses
    // internally — per this work package's "core test patterns" note. The
    // happy-path HTTP call itself (an actual `processContent` round trip) is
    // covered by `tests/loopback.rs`.

    fn agent_middleware(settings: PurviewSettings) -> PurviewAgentMiddleware {
        PurviewAgentMiddleware::new(StaticTokenProvider::new("token"), settings)
    }

    fn chat_middleware(settings: PurviewSettings) -> PurviewChatMiddleware {
        PurviewChatMiddleware::new(StaticTokenProvider::new("token"), settings)
    }

    fn agent_terminal(called: Arc<AtomicBool>, text: &'static str) -> Terminal<AgentContext> {
        Box::new(move |mut ctx: AgentContext| {
            called.store(true, Ordering::SeqCst);
            Box::pin(async move {
                ctx.result = Some(AgentResponse {
                    messages: vec![Message::assistant(text)],
                    ..Default::default()
                });
                Ok(ctx)
            }) as BoxFuture<Result<AgentContext>>
        })
    }

    fn chat_terminal(called: Arc<AtomicBool>, text: &'static str) -> Terminal<ChatContext> {
        Box::new(move |mut ctx: ChatContext| {
            called.store(true, Ordering::SeqCst);
            Box::pin(async move {
                ctx.result = Some(ChatResponse::from_text(text));
                Ok(ctx)
            }) as BoxFuture<Result<ChatContext>>
        })
    }

    /// `Result::unwrap_err` requires the `Ok` type to implement `Debug`,
    /// which `AgentContext`/`ChatContext` deliberately don't (they carry
    /// non-`Debug` middleware/result trait objects). Same shape, without
    /// that bound.
    fn expect_err<T>(result: Result<T>) -> agent_framework_core::error::Error {
        match result {
            Ok(_) => panic!("expected Err, got Ok"),
            Err(e) => e,
        }
    }

    // -- config-error propagation (no network: fails before any HTTP call) -

    #[tokio::test]
    async fn agent_middleware_propagates_config_error_without_ignore_exceptions() {
        let settings = PurviewSettings::new("Test App"); // no tenant_id/app_location
        let middleware = agent_middleware(settings);
        let pipeline = MiddlewarePipeline::new(vec![Arc::new(middleware)]);
        let called = Arc::new(AtomicBool::new(false));
        let ctx = AgentContext::new(vec![Message::user("hi")], false);

        let err = expect_err(
            pipeline
                .execute(ctx, agent_terminal(called.clone(), "should not be reached"))
                .await,
        );
        assert!(err.to_string().contains("tenant_id"));
        assert!(
            !called.load(Ordering::SeqCst),
            "next must not run on a pre-check error"
        );
    }

    #[tokio::test]
    async fn agent_middleware_ignore_exceptions_suppresses_config_error_and_allows() {
        let settings = PurviewSettings::new("Test App").with_ignore_exceptions(true);
        let middleware = agent_middleware(settings);
        let pipeline = MiddlewarePipeline::new(vec![Arc::new(middleware)]);
        let called = Arc::new(AtomicBool::new(false));
        let ctx = AgentContext::new(vec![Message::user("hi")], false);

        let result_ctx = pipeline
            .execute(ctx, agent_terminal(called.clone(), "real response"))
            .await
            .unwrap();
        assert!(
            called.load(Ordering::SeqCst),
            "next must run once the error is suppressed"
        );
        assert!(!result_ctx.terminate);
        assert_eq!(result_ctx.result.unwrap().text(), "real response");
    }

    // -- resolve_user_id short-circuits allow (no network) ------------------

    #[tokio::test]
    async fn agent_middleware_allows_when_no_resolvable_user_id() {
        // Valid settings, but the message carries no GUID-shaped user_id or
        // author_name -- resolve_user_id returns None, which the processor
        // treats as "cannot evaluate; allow" *without* an HTTP call. If it
        // did attempt one, this test would hang/fail trying to reach
        // graph.microsoft.com.
        let middleware = agent_middleware(valid_settings());
        let pipeline = MiddlewarePipeline::new(vec![Arc::new(middleware)]);
        let called = Arc::new(AtomicBool::new(false));
        let ctx = AgentContext::new(
            vec![Message::user("hello, nothing identifying here")],
            false,
        );

        let result_ctx = pipeline
            .execute(ctx, agent_terminal(called.clone(), "real response"))
            .await
            .unwrap();
        assert!(called.load(Ordering::SeqCst));
        assert!(!result_ctx.terminate);
        assert_eq!(result_ctx.result.unwrap().text(), "real response");
    }

    #[tokio::test]
    async fn agent_middleware_skips_post_check_when_streaming() {
        // is_streaming = true -> the response-phase check must never run,
        // so even a config-broken response phase can't surface an error.
        let middleware = agent_middleware(valid_settings());
        let pipeline = MiddlewarePipeline::new(vec![Arc::new(middleware)]);
        let called = Arc::new(AtomicBool::new(false));
        let ctx = AgentContext::new(vec![Message::user("hello, nothing identifying here")], true);

        let result_ctx = pipeline
            .execute(ctx, agent_terminal(called.clone(), "streamed response"))
            .await
            .unwrap();
        assert!(called.load(Ordering::SeqCst));
        assert_eq!(result_ctx.result.unwrap().text(), "streamed response");
    }

    #[tokio::test]
    async fn chat_middleware_allows_when_no_resolvable_user_id() {
        let middleware = chat_middleware(valid_settings());
        let pipeline = MiddlewarePipeline::new(vec![Arc::new(middleware)]);
        let called = Arc::new(AtomicBool::new(false));
        let ctx = ChatContext::new(
            vec![Message::user("hello, nothing identifying here")],
            agent_framework_core::types::ChatOptions::new(),
            false,
        );

        let result_ctx = pipeline
            .execute(ctx, chat_terminal(called.clone(), "real response"))
            .await
            .unwrap();
        assert!(called.load(Ordering::SeqCst));
        assert!(!result_ctx.terminate);
        assert_eq!(result_ctx.result.unwrap().text(), "real response");
    }

    #[tokio::test]
    async fn chat_middleware_propagates_config_error_without_ignore_exceptions() {
        let settings = PurviewSettings::new("Test App");
        let middleware = chat_middleware(settings);
        let pipeline = MiddlewarePipeline::new(vec![Arc::new(middleware)]);
        let called = Arc::new(AtomicBool::new(false));
        let ctx = ChatContext::new(
            vec![Message::user("hi")],
            agent_framework_core::types::ChatOptions::new(),
            false,
        );

        let err = expect_err(
            pipeline
                .execute(ctx, chat_terminal(called.clone(), "should not be reached"))
                .await,
        );
        assert!(err.to_string().contains("tenant_id"));
        assert!(!called.load(Ordering::SeqCst));
    }

    // -- blocked-message text/role ------------------------------------------

    #[test]
    fn blocked_messages_use_system_role_and_configured_text() {
        let core = PurviewPolicyCore::new(StaticTokenProvider::new("t"), valid_settings());
        let prompt_msg = core.blocked_prompt_message();
        let response_msg = core.blocked_response_message();
        assert_eq!(prompt_msg.role, Role::system());
        assert_eq!(prompt_msg.text(), "Prompt blocked by policy");
        assert_eq!(response_msg.role, Role::system());
        assert_eq!(response_msg.text(), "Response blocked by policy");
    }

    #[test]
    fn blocked_messages_honor_custom_text() {
        let settings = valid_settings()
            .with_blocked_prompt_message("custom prompt block")
            .with_blocked_response_message("custom response block");
        let core = PurviewPolicyCore::new(StaticTokenProvider::new("t"), settings);
        assert_eq!(core.blocked_prompt_message().text(), "custom prompt block");
        assert_eq!(
            core.blocked_response_message().text(),
            "custom response block"
        );
    }
}