Repository navigation
feat: enforce purpose-limited PII event protection #803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
ec3b3c8
5e06114
33f312c
5c51c3a
f13c224
606eb37
9f8b094
1f83652
35692d5
26d1d30
f646b04
3e682c4
7233f64
5c6670c
b399eee
ef375cc
641b2d1
519dd9d
6b6bd78
030b434
299e99a
68064ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| import math | ||
| import os | ||
| from pathlib import Path | ||
| import queue | ||
| import random | ||
| import re | ||
| import socket | ||
|
|
@@ -33,6 +34,14 @@ | |
|
|
||
| from .conventions import require_object_name | ||
| from .credentials import NotConfigured, get_credential | ||
| from .pii_protection import ( | ||
| DEFAULT_PII_KEY_NAME, | ||
| ENCRYPTED_FIELDS_KEY, | ||
| PiiFieldEncryptor, | ||
| PiiProtectionError, | ||
| is_encrypted_detail, | ||
| load_pii_encryptor, | ||
| ) | ||
|
|
||
|
|
||
| # content is usually str; multimodal vision messages use OpenAI content-parts lists. | ||
|
|
@@ -1542,20 +1551,25 @@ class _StateStore: | |
| """Minimal write-through sqlite persistence for orchestrator runtime state. | ||
|
|
||
| ponytail: one generic table, no ORM. Keyed kinds (workflow_run, evaluation_run) | ||
| upsert by key; stream kinds (analytics, audit) append. Stream rows grow unbounded | ||
| on disk while the in-memory deques stay capped — add pruning if db size matters. | ||
| upsert by key; stream kinds append. Durable audit streams use the same bounded | ||
| retention as their in-memory deques so request traffic cannot grow the DB forever. | ||
| Runtime values (kind, key, payload, limit) are always bound through SQLite | ||
| placeholders so persisted prompts and identifiers cannot become SQL syntax. | ||
| """ | ||
|
|
||
| _KEYED = {"workflow_run", "evaluation_run"} | ||
| _STREAM_LIMITS = {"audit": 256, "authorization": 256, "analytics": 256} | ||
| _CREATE_RECORDS_SQL = ( | ||
| "CREATE TABLE IF NOT EXISTS records (" | ||
| "seq INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, key TEXT, payload TEXT NOT NULL)" | ||
| ) | ||
| _CREATE_RECORDS_KIND_SEQ_INDEX_SQL = "CREATE INDEX IF NOT EXISTS records_kind_seq ON records(kind, seq)" | ||
| _DELETE_KEYED_SQL = "DELETE FROM records WHERE kind = ? AND key = ?" | ||
| _INSERT_SQL = "INSERT INTO records (kind, key, payload) VALUES (?, ?, ?)" | ||
| _PRUNE_STREAM_SQL = ( | ||
| "DELETE FROM records WHERE kind = ? AND seq NOT IN (" | ||
| "SELECT seq FROM records WHERE kind = ? ORDER BY seq DESC LIMIT ?)" | ||
| ) | ||
| _SELECT_ALL_SQL = "SELECT payload FROM records WHERE kind = ? ORDER BY seq" | ||
| _SELECT_LIMIT_SQL = "SELECT payload FROM records WHERE kind = ? ORDER BY seq DESC LIMIT ?" | ||
|
|
||
|
|
@@ -1565,16 +1579,50 @@ def __init__(self, path: str) -> None: | |
| self._conn.execute(self._CREATE_RECORDS_SQL) | ||
| self._conn.execute(self._CREATE_RECORDS_KIND_SEQ_INDEX_SQL) | ||
| self._conn.commit() | ||
| # Stream kinds (audit/authorization/analytics) are already bounded and | ||
| # best-effort by design (see class docstring). Route them through a | ||
| # background queue so an unauthenticated request flood — e.g. denied | ||
| # authorizations — cannot force synchronous, lock-serialized disk | ||
| # commits on the request-handling thread. Keyed kinds (workflow_run, | ||
| # evaluation_run) need durability and stay synchronous. | ||
| self._stream_queue: queue.Queue[tuple[str, str | None, dict[str, Any]]] = queue.Queue(maxsize=2048) | ||
| self._stream_worker = threading.Thread( | ||
| target=self._drain_stream_queue, | ||
| name="contextual-orchestrator-state-store", | ||
| daemon=True, | ||
| ) | ||
| self._stream_worker.start() | ||
|
|
||
| def save(self, kind: str, key: str | None, payload: dict[str, Any]) -> None: | ||
| if kind in self._STREAM_LIMITS: | ||
| try: | ||
| self._stream_queue.put_nowait((kind, key, payload)) | ||
| except queue.Full: | ||
| pass | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return | ||
| self._save_sync(kind, key, payload) | ||
|
seonghobae marked this conversation as resolved.
Resolved
Comment on lines
+1719
to
+1727
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Analytics and denials become best-effort async
Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| def _save_sync(self, kind: str, key: str | None, payload: dict[str, Any]) -> None: | ||
| blob = json.dumps(payload, ensure_ascii=False) | ||
| with self._lock: | ||
| if kind in self._KEYED: | ||
| self._conn.execute(self._DELETE_KEYED_SQL, (kind, key)) | ||
| self._conn.execute(self._INSERT_SQL, (kind, key, blob)) | ||
| if kind in self._STREAM_LIMITS: | ||
| limit = self._STREAM_LIMITS[kind] | ||
| self._conn.execute(self._PRUNE_STREAM_SQL, (kind, kind, limit)) | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
| self._conn.commit() | ||
|
|
||
| def _drain_stream_queue(self) -> None: | ||
| while True: | ||
| kind, key, payload = self._stream_queue.get() | ||
| try: | ||
| self._save_sync(kind, key, payload) | ||
| finally: | ||
| self._stream_queue.task_done() | ||
|
github-actions[bot] marked this conversation as resolved.
Resolved
github-actions[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| def load(self, kind: str, limit: int | None = None) -> list[dict[str, Any]]: | ||
| self._stream_queue.join() | ||
|
github-actions[bot] marked this conversation as resolved.
Resolved
|
||
| with self._lock: | ||
| if limit is None: | ||
| rows = self._conn.execute(self._SELECT_ALL_SQL, (kind,)).fetchall() | ||
|
|
@@ -1585,6 +1633,7 @@ def load(self, kind: str, limit: int | None = None) -> list[dict[str, Any]]: | |
|
|
||
| def close(self) -> None: | ||
| """Close the sqlite handle so Windows can release the database file.""" | ||
| self._stream_queue.join() | ||
| with self._lock: | ||
| self._conn.close() | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
@@ -1667,6 +1716,7 @@ def __init__( | |
| agents_db: str | None = None, | ||
| cache_ttl: float = 0.0, | ||
| cache_max_entries: int = 256, | ||
| pii_key_name: str = DEFAULT_PII_KEY_NAME, | ||
| ) -> None: | ||
| # Optional durable model-group management: stored operator changes overlay the | ||
| # seed agents file at startup (stored rows win by id; stored-new rows append). | ||
|
|
@@ -1687,8 +1737,9 @@ def __init__( | |
| self.budget_max_cost_usd = budget_max_cost_usd | ||
| self._workflow_runs: dict[str, dict[str, Any]] = {} | ||
| self._evaluation_runs: dict[str, dict[str, Any]] = {} | ||
| self._analytics_events: deque[dict[str, Any]] = deque(maxlen=512) | ||
| self._analytics_events: deque[dict[str, Any]] = deque(maxlen=256) | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
| self._audit_events: deque[dict[str, Any]] = deque(maxlen=256) | ||
| self._authorization_events: deque[dict[str, Any]] = deque(maxlen=256) | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
| self._run_order: deque[str] = deque(maxlen=128) | ||
| # Per-agent circuit breaker: consecutive failures trip an agent "open" | ||
| # so a persistently failing provider is skipped until it cools down. | ||
|
|
@@ -1702,6 +1753,10 @@ def __init__( | |
| # Optional durable persistence: default None keeps all state purely in-memory | ||
| # (zero behavior change). When set, runs/audit/analytics survive restart. | ||
| self._store = _StateStore(state_db) if state_db else None | ||
| if not isinstance(pii_key_name, str) or not pii_key_name: | ||
| raise ValueError("pii_key_name must be a non-empty string") | ||
|
Comment on lines
+1946
to
+1947
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 State store thread leaks when PII key name is rejected The durable state store starts its background thread and opens its sqlite connection ( Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| self._pii_key_name = pii_key_name | ||
| self._pii_encryptors: dict[str, PiiFieldEncryptor] = {} | ||
| self._commercial_report_cache_local = threading.local() | ||
| if self._store is not None: | ||
| self._reload_state() | ||
|
|
@@ -1770,6 +1825,8 @@ def _reload_state(self) -> None: | |
| self._analytics_events.append(event) | ||
| for event in self._store.load("audit", self._audit_events.maxlen): | ||
| self._audit_events.append(event) | ||
| for event in self._store.load("authorization", self._authorization_events.maxlen): | ||
| self._authorization_events.append(event) | ||
|
|
||
| # Orchestration-only body keys that must not be forwarded to the provider. | ||
| _ORCHESTRATION_ONLY_KEYS = frozenset( | ||
|
|
@@ -2708,15 +2765,55 @@ def _judge_verifier_output(self, verifier_output: str, thinker_output: str, work | |
| "verifier_output": verifier_output, | ||
| } | ||
|
|
||
| def _append_audit_event(self, event_type: str, detail: dict[str, Any]) -> None: | ||
| def _protected_event_detail(self, detail: dict[str, Any], pii_fields: Iterable[str]) -> dict[str, Any]: | ||
| """Encrypt explicitly declared PII fields before an event enters memory or storage.""" | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
| fields = tuple(pii_fields) | ||
| if not fields: | ||
| return detail | ||
| encryptor = self._pii_encryptors.get(self._pii_key_name) | ||
| if encryptor is None: | ||
| encryptor = load_pii_encryptor(self._pii_key_name) | ||
| self._pii_encryptors[self._pii_key_name] = encryptor | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
| return encryptor.encrypt_fields(detail, fields) | ||
|
|
||
| def _append_audit_event( | ||
| self, | ||
| event_type: str, | ||
| detail: dict[str, Any], | ||
| *, | ||
| pii_fields: Iterable[str] = (), | ||
| stream: str = "audit", | ||
| ) -> None: | ||
| """Append an event to a bounded audit stream.""" | ||
| event = { | ||
| "created_at": int(time.time()), | ||
| "event_type": event_type, | ||
| "event_detail": detail, | ||
| "event_detail": self._protected_event_detail(detail, pii_fields), | ||
| } | ||
| self._audit_events.append(event) | ||
| events = self._authorization_events if stream == "authorization" else self._audit_events | ||
| events.append(event) | ||
| if self._store is not None: | ||
| self._store.save("audit", None, event) | ||
| self._store.save(stream, None, event) | ||
|
|
||
| def record_authorization_decision( | ||
| self, | ||
| *, | ||
| scope: str, | ||
| purpose: str, | ||
| allowed: bool, | ||
| reason: str, | ||
| ) -> None: | ||
| """Record a secret-free role/purpose authorization decision.""" | ||
| self._append_audit_event( | ||
| "authorization_decision", | ||
| { | ||
| "scope": scope, | ||
| "purpose": purpose, | ||
| "allowed": bool(allowed), | ||
| "reason": reason, | ||
| }, | ||
| stream="authorization", | ||
| ) | ||
|
|
||
| def _infer_provider_name(self, base_url: str) -> str: | ||
| if base_url.startswith("mock://"): | ||
|
|
@@ -2805,8 +2902,15 @@ def list_recent_runs(self, page_number: int = 1, page_size: int = 10) -> list[di | |
| run_ids = list(self._run_order)[start:end] | ||
| return [self._workflow_runs[run_id] for run_id in run_ids] | ||
|
|
||
| def list_recent_audit_events(self, page_number: int = 1, page_size: int = 25) -> list[dict[str, Any]]: | ||
| """Return recent audit events in newest-first order.""" | ||
| def list_recent_audit_events( | ||
| self, | ||
| page_number: int = 1, | ||
| page_size: int = 25, | ||
| *, | ||
| role: str | None = None, | ||
| purpose: str | None = None, | ||
| ) -> list[dict[str, Any]]: | ||
| """Return recent audit events, decrypting PII only for authorized replay.""" | ||
| if page_number < 1 or page_size < 1: # pragma: no cover | ||
| raise ValueError("page_number/page_size must be >= 1") | ||
| events = list(self._audit_events) | ||
|
|
@@ -2815,15 +2919,60 @@ def list_recent_audit_events(self, page_number: int = 1, page_size: int = 25) -> | |
| total = len(events) | ||
| left = max(0, total - end) | ||
| right = max(0, total - start) | ||
| selected = list(reversed(events[left:right])) | ||
| if role != "admin" or purpose != "audit_replay": | ||
| return selected | ||
| restored: list[dict[str, Any]] = [] | ||
| encryptors: dict[str, Any] = {} | ||
| for event in selected: | ||
| detail = event.get("event_detail") | ||
| if not is_encrypted_detail(detail): | ||
| restored.append(event) | ||
| continue | ||
| restored_event = dict(event) | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
|
||
| try: | ||
| metadata = detail.get(ENCRYPTED_FIELDS_KEY) | ||
| key_name = metadata.get("key_name") if isinstance(metadata, dict) else self._pii_key_name | ||
| if not isinstance(key_name, str) or not key_name: | ||
| raise PiiProtectionError("encrypted field metadata has no valid key name") | ||
| encryptor = encryptors.get(key_name) | ||
| if encryptor is None: | ||
| encryptor = load_pii_encryptor(key_name) | ||
| encryptors[key_name] = encryptor | ||
| restored_event["event_detail"] = encryptor.decrypt_fields(detail) | ||
| except PiiProtectionError: | ||
| restored_event["event_detail"] = { | ||
| **detail, | ||
| "__pii_protection_error__": "unavailable", | ||
| } | ||
| restored.append(restored_event) | ||
| return restored | ||
|
Comment on lines
+3254
to
+3280
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: PII plaintext exposure limited to admin audit_replay Decryption in Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| def list_recent_authorization_decisions(self, page_number: int = 1, page_size: int = 25) -> list[dict[str, Any]]: | ||
| """Return recent secret-free authorization decisions in newest-first order.""" | ||
| if page_number < 1 or page_size < 1: # pragma: no cover | ||
| raise ValueError("page_number/page_size must be >= 1") | ||
| events = list(self._authorization_events) | ||
| start = (page_number - 1) * page_size | ||
| end = start + page_size | ||
| total = len(events) | ||
| left = max(0, total - end) | ||
| right = max(0, total - start) | ||
| return list(reversed(events[left:right])) | ||
|
|
||
| def record_analytics_event(self, event_name: str, detail: dict[str, Any]) -> None: | ||
| def record_analytics_event( | ||
| self, | ||
| event_name: str, | ||
| detail: dict[str, Any], | ||
| *, | ||
| pii_fields: Iterable[str] = (), | ||
| ) -> None: | ||
| """Record a compact in-memory analytics event without prompt or output text.""" | ||
| require_object_name(event_name, "analytics.event_name") | ||
| event = { | ||
| "event_time": int(time.time()), | ||
| "event_name": event_name, | ||
| "event_detail": redact_value(detail), | ||
| "event_detail": redact_value(self._protected_event_detail(detail, pii_fields)), | ||
| } | ||
| self._analytics_events.append(event) | ||
| if self._store is not None: | ||
|
seonghobae marked this conversation as resolved.
Resolved
seonghobae marked this conversation as resolved.
Resolved
Comment on lines
+3294
to
3309
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Analytics-encrypted PII is never decryptable
Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
@@ -9006,7 +9155,7 @@ def section( | |
| }, | ||
| } | ||
|
|
||
| def admin_state(self) -> dict[str, Any]: | ||
| def admin_state(self, *, role: str | None = None, purpose: str | None = None) -> dict[str, Any]: | ||
| """Build the admin console state payload from agents, policy, and audit data.""" | ||
| agent_page_size = max(1, len(self.candidates)) | ||
| return { | ||
|
|
@@ -9017,7 +9166,8 @@ def admin_state(self) -> dict[str, Any]: | |
| "complex_hints": list(self.COMPLEX_HINTS), | ||
| }, | ||
| "recent_workflow_runs": [self._shorten_run(run) for run in self.list_recent_runs(page_size=max(1, len(self._run_order)))], | ||
| "recent_audit_events": self.list_recent_audit_events(), | ||
| "recent_audit_events": self.list_recent_audit_events(role=role, purpose=purpose), | ||
| "recent_authorization_decisions": self.list_recent_authorization_decisions(), | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
| "spend": self.spend_analytics(), | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.