session_prompt_pin.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """SessionStore prompt pins: the durable snapshot of the system-prompt inputs internal turns reuse.
  2. Internal events (kanban wakes, delegation completions, startup resume) reuse the last human turn's
  3. session-context bytes and channel prompt so the system prompt stays byte-stable; the routing entry
  4. carries that snapshot so a gateway restart does not flip it (#125793).
  5. """
  6. from __future__ import annotations
  7. from dataclasses import replace
  8. from typing import Any, Dict, Optional
  9. PROMPT_PIN_VERSION = 1
  10. def sanitize_prompt_pin(pin: Any) -> Optional[dict[str, Any]]:
  11. """Validated durable snapshot of the exact ephemeral inputs reused by internal turns.
  12. ``redact_pii`` is the ``privacy.redact_pii`` the context bytes were rendered under; a pin
  13. without it cannot prove which privacy policy produced them and is refused."""
  14. if not isinstance(pin, dict) or pin.get("version") != PROMPT_PIN_VERSION:
  15. return None
  16. context_key = pin.get("context_key")
  17. context_prompt = pin.get("context_prompt")
  18. redact_pii = pin.get("redact_pii")
  19. channel_prompt = pin.get("channel_prompt")
  20. parent_chat_id = pin.get("parent_chat_id")
  21. if not isinstance(context_key, str) or not context_key or not isinstance(context_prompt, str):
  22. return None
  23. if not isinstance(redact_pii, bool):
  24. return None
  25. if channel_prompt is not None and not isinstance(channel_prompt, str):
  26. return None
  27. if parent_chat_id is not None and not isinstance(parent_chat_id, str):
  28. return None
  29. return {
  30. "version": PROMPT_PIN_VERSION,
  31. "context_key": context_key,
  32. "context_prompt": context_prompt,
  33. "redact_pii": redact_pii,
  34. "channel_prompt": channel_prompt,
  35. "parent_chat_id": parent_chat_id,
  36. }
  37. class SessionPromptPinMixin:
  38. """Fenced read/write of ``SessionEntry.prompt_pin`` on the routing index."""
  39. def set_prompt_pin(
  40. self, session_key: str, pin: dict[str, Any], *, expected_session_id: Optional[str] = None,
  41. ) -> bool:
  42. """Persist effective prompt inputs without letting a stale turn cross a boundary.
  43. The candidate is durable before the in-memory entry is published. The expected session id
  44. is the turn's launch identity: a concurrent reset or resume makes this a no-op instead of
  45. writing the old conversation's system bytes onto the new one.
  46. """
  47. cleaned = sanitize_prompt_pin(pin)
  48. if cleaned is None:
  49. return False
  50. with self._lock:
  51. entry = self._entry_locked(session_key)
  52. if entry is None or (expected_session_id is not None and entry.session_id != expected_session_id):
  53. return False
  54. if entry.prompt_pin != cleaned:
  55. self._save_entry(
  56. session_key, entry_data=replace(entry, prompt_pin=cleaned).to_dict(), lock_held=True)
  57. entry.prompt_pin = cleaned
  58. return True
  59. def get_prompt_pin(
  60. self, session_key: str, *, expected_session_id: Optional[str] = None,
  61. ) -> Optional[dict[str, Any]]:
  62. """Return the prompt pin only while the route still owns the caller's session."""
  63. with self._lock:
  64. entry = self._entry_locked(session_key)
  65. if entry is None:
  66. return None
  67. if expected_session_id is not None and entry.session_id != expected_session_id:
  68. return None
  69. return dict(entry.prompt_pin) if entry.prompt_pin else None