Python: Address PR 5331 comments and track sesssion while calling Agent in email_security_example (#5446)

* Address PR review: fix paths and update FIDES implementation

* Address PR comments and add session tracking in email example in samples

* Fix session creation and resolve merge conflict in docstring example

* Resolve merge conflict in docstring example
This commit is contained in:
shrutitople
2026-05-04 10:00:41 +02:00
committed by eavanvalkenburg
parent 14d779c0fb
commit 9711562c9e
6 changed files with 37 additions and 195 deletions
+18 -17
View File
@@ -108,10 +108,24 @@ async def fetch_emails(count: int = 5) -> list[Content]:
}),
additional_properties={
"security_label": {
"integrity": "trusted" if email["is_internal"] else "untrusted",
"integrity": "trusted" if email["internal"] else "untrusted",
"confidentiality": "private",
}
},
),
)
for email in emails
]
```
These embedded labels are automatically consumed by `LabelTrackingFunctionMiddleware`, which:
- Extracts the `security_label` from `additional_properties`
- Uses the embedded label as the highest-priority source for that item
- Automatically hides UNTRUSTED items in the variable store
- Replaces hidden items with `VariableReferenceContent` in the LLM context
- Preserves TRUSTED items visible to the LLM without tainting the context label
This enables tools to return mixed-trust data where some items (internal emails) remain visible while untrusted items (external emails) are automatically hidden without manual intervention.
},
)
for email in emails
]
@@ -119,6 +133,8 @@ async def fetch_emails(count: int = 5) -> list[Content]:
### 3. Automatic Variable Hiding
This feature automatically hides any UNTRUSTED content returned by tools while keeping the hiding logic transparent to the developer. Developers do not need to manually call `store_untrusted_content()`. This allows the LLM /agent's context to remain clean and secure. Key aspects include:
- **Automatic Detection**: Middleware checks integrity label after each tool call
- **Automatic Storage**: UNTRUSTED results/items stored in variable store
- **Transparent Replacement**: LLM context receives `VariableReferenceContent`
@@ -168,16 +184,6 @@ agent = Agent(
)
```
### 7. Message-Level Label Tracking (Phase 1)
Track security labels at the message level:
```python
labeled_messages = middleware.label_messages(messages)
label = middleware.get_message_label(5)
all_labels = middleware.get_all_message_labels()
```
## Security Properties
### Deterministic Defense
@@ -323,11 +329,6 @@ cd python/packages/core && ../../.venv/bin/pytest tests/test_security.py -v
`quarantine_chat_client` support for real LLM calls
`SECURITY_TOOL_INSTRUCTIONS` constant
### Phase 1: Message-Level Tracking
`LabeledMessage` class with auto-inference from role
`label_message()`, `get_message_label()`, `label_messages()` methods
`get_all_message_labels()` method
### Documentation & Testing
✅ Complete FIDES Developer Guide (~1250 lines)
✅ Architecture Decision Record (ADR)