hermes_state_mutation_chat_pick.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Exact-retry receipts for a viewer's pick on a messaging chat (gateway/session_messaging_picks.py).
  2. A messaging chat has no frozen policy for ``apply_action`` to write: its pick is the chat's own
  3. ``/model`` / ``/reasoning`` override. It still answers ``session.mutate`` like a LOCAL pick: the
  4. same ``gateway.mutation.v1.*`` receipt key and digest (``mutation_receipt_key``), checked before the
  5. override is applied so an exact retry never re-applies it, and committed with the revision bump in
  6. one fenced transaction.
  7. """
  8. import json
  9. from hermes_state_runtime import (
  10. RuntimeStoreError, _epoch, _json, _text, mutation_receipt_key, replay_mutation_receipt,
  11. )
  12. def chat_pick_key(principal_id, session_id, params):
  13. """``(key, digest)`` of the pick *params* on *session_id* (the mount the fences name)."""
  14. for value in (principal_id, session_id, params['request_id']):
  15. _text(value)
  16. for fence in (params['expected_revision'], params.get('expected_generation')):
  17. if type(fence) is not int or fence < 0:
  18. raise RuntimeStoreError('invalid_params')
  19. payload = json.loads(_json(params['payload']))
  20. key, digest = mutation_receipt_key(
  21. principal_id, session_id, params['request_id'], operation=params['operation'], payload=payload,
  22. expected_revision=params['expected_revision'], expected_generation=params['expected_generation'])
  23. return key, digest
  24. def _fenced_row(conn, session_id, params):
  25. row = conn.execute('SELECT * FROM sessions WHERE id=?', (session_id,)).fetchone()
  26. if row is None:
  27. raise RuntimeStoreError('not_found')
  28. if row['runtime_revision'] != params['expected_revision']:
  29. raise RuntimeStoreError('revision_conflict')
  30. if row['runtime_generation'] != params['expected_generation']:
  31. raise RuntimeStoreError('stale_generation')
  32. return row
  33. def prepare_chat_pick(db, *, epoch, key, digest, session_id, params):
  34. """The stored receipt of an exact retry (``{'receipt': ...}``), else the fenced mount row
  35. (``{'row': ...}``); a reused request id with another payload refuses ``admission_conflict``."""
  36. with db._read_ctx() as conn:
  37. _epoch(conn, epoch)
  38. replay = replay_mutation_receipt(conn, key, digest)
  39. if replay is not None:
  40. return {'receipt': replay}
  41. return {'row': dict(_fenced_row(conn, session_id, params))}
  42. def commit_chat_pick(db, *, epoch, key, digest, session_id, params, result):
  43. """Bump the mount's revision and store *result* (plus the new revision) as the receipt, in one
  44. write that re-checks the fences. Returns the committed receipt result."""
  45. def write(conn):
  46. _epoch(conn, epoch)
  47. replay = replay_mutation_receipt(conn, key, digest)
  48. if replay is not None:
  49. return replay
  50. _fenced_row(conn, session_id, params)
  51. conn.execute('UPDATE sessions SET runtime_revision=runtime_revision+1 WHERE id=?', (session_id,))
  52. revision = conn.execute('SELECT runtime_revision FROM sessions WHERE id=?', (session_id,)).fetchone()[0]
  53. committed = {**result, 'revision': revision}
  54. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?)',
  55. (key, _json({'digest': digest, 'result': committed})))
  56. return committed
  57. return db._execute_write(write)