session_operator.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Private provenance for already-authorized operator input.
  2. Operator access is profile-local, not account linking or a change of authorship.
  3. It never claims unowned legacy history: that still requires the native migration
  4. path. Only SessionAuthority.submit writes this envelope after authorization;
  5. public submission parameters cannot supply it.
  6. """
  7. from gateway.owner_identity import same_owner
  8. from hermes_state_runtime import RuntimeStoreError
  9. # Fields ``SessionAuthority.submit`` commits for a local session's input; producers commit their own envelope.
  10. LOCAL_INPUT_FIELDS = frozenset({'text', 'attachments_v1', 'finite', 'unattended', 'surface_v1', 'display_v1',
  11. 'cli_turn_v1', 'kanban_card_v1'})
  12. def is_viewer_input(row):
  13. """A row a session viewer submitted through ``session.submit`` (a client's own prompt), not a producer
  14. admission: ``local_automation_v1`` (plugin injection, delegation completions, process and watch notices,
  15. /loop, heartbeat), a hosted-room task (``hosted:`` request id) or any other envelope."""
  16. return (not str(row['request_id']).startswith('hosted:')
  17. and {'text'} <= set(row['payload']) <= LOCAL_INPUT_FIELDS | {'local_operator_v1'})
  18. def check_local_input(authority, ref, row, *, source=None):
  19. payload = row['payload']
  20. allowed = set(LOCAL_INPUT_FIELDS)
  21. if 'local_operator_v1' in payload:
  22. allowed.add('local_operator_v1')
  23. if payload['local_operator_v1'] != {
  24. 'profile_id': authority.profile_id, 'session_id': ref.session_id,
  25. 'principal_id': row['principal_id']}:
  26. raise RuntimeStoreError('permission_denied')
  27. else:
  28. source = source or authority.sessions[ref.session_id].source
  29. if not same_owner(source.user_id, row['principal_id']): # the rule submit() stamped by
  30. raise RuntimeStoreError('permission_denied')
  31. if not {'text'} <= set(payload) <= allowed:
  32. raise RuntimeStoreError('permission_denied')