hosted_room_policy_checkpoint.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. """Durable bounded policy projection for hosted Group Chat preparation.
  2. The append-only room log remains the user-visible source of truth. This module materializes only the state
  3. needed to choose and reconstruct the next active discussion, so a busy room does not replay its complete
  4. history every poll.
  5. """
  6. from __future__ import annotations
  7. import json
  8. import sqlite3
  9. from dataclasses import dataclass
  10. from pathlib import Path
  11. from typing import Any, Callable, Mapping
  12. from gateway import hosted_rooms
  13. from gateway.hosted_room_task_input import validate_task_input
  14. from gateway.hosted_rooms_common import DbPath, compact_json, fenced_update
  15. MAX_ACTIVE_POLICY_EVENTS = 64
  16. # ``turn.deferred`` receipts come from explicit user retries, not from the configured reply
  17. # budget, so they are bounded on their own: sizing the reply budget exactly must leave room.
  18. MAX_ACTIVE_RETRY_RECEIPTS = 64
  19. MAX_THREAD_TRANSCRIPT_EVENTS = 24
  20. _TRANSCRIPT_SCHEMA_VERSION = 4
  21. MAX_TRANSCRIPT_POLICY_EVENTS = MAX_THREAD_TRANSCRIPT_EVENTS * (MAX_ACTIVE_POLICY_EVENTS + 2)
  22. _TERMINAL_KINDS = frozenset({"turn.settled", "turn.failed", "turn.cancelled", "turn.deferred"})
  23. # What a Continue restores into a reopened discussion's active projection (the cap's activity is not one).
  24. _REOPENED_KINDS = (*sorted(_TERMINAL_KINDS), "message.member", "room.assignment", "room.routed", "room.continued")
  25. _SCHEMA_DDL = (
  26. """CREATE TABLE IF NOT EXISTS hosted_room_policy_capacity (
  27. singleton INTEGER PRIMARY KEY CHECK(singleton=1), max_active_events INTEGER NOT NULL)""",
  28. """CREATE TABLE IF NOT EXISTS hosted_room_policy_cursors (
  29. room_id TEXT PRIMARY KEY, through_seq INTEGER NOT NULL DEFAULT 0,
  30. stopped_through_seq INTEGER NOT NULL DEFAULT 0, updated_at REAL NOT NULL DEFAULT 0)""",
  31. """CREATE TABLE IF NOT EXISTS hosted_room_policy_threads (
  32. room_id TEXT NOT NULL, thread_id TEXT NOT NULL, discussion_event_id TEXT NOT NULL,
  33. latest_user_seq INTEGER NOT NULL, completed INTEGER NOT NULL DEFAULT 0,
  34. PRIMARY KEY(room_id, thread_id))""",
  35. """CREATE INDEX IF NOT EXISTS idx_hosted_room_policy_pending
  36. ON hosted_room_policy_threads(room_id, completed, latest_user_seq, thread_id)""",
  37. """CREATE TABLE IF NOT EXISTS hosted_room_policy_events (
  38. room_id TEXT NOT NULL, thread_id TEXT NOT NULL, discussion_event_id TEXT NOT NULL,
  39. seq INTEGER NOT NULL, event_json TEXT NOT NULL, PRIMARY KEY(room_id, seq))""",
  40. """CREATE INDEX IF NOT EXISTS idx_hosted_room_policy_events_active
  41. ON hosted_room_policy_events(room_id, discussion_event_id, seq)""",
  42. """CREATE TABLE IF NOT EXISTS hosted_room_policy_watermarks (
  43. room_id TEXT NOT NULL, thread_id TEXT NOT NULL, member_id TEXT NOT NULL,
  44. seen_through_seq INTEGER NOT NULL, PRIMARY KEY(room_id, thread_id, member_id))""",
  45. """CREATE TABLE IF NOT EXISTS hosted_room_policy_publications (
  46. room_id TEXT NOT NULL, task_id TEXT NOT NULL, kind TEXT NOT NULL,
  47. execution_generation INTEGER NOT NULL DEFAULT 0, seq INTEGER NOT NULL,
  48. PRIMARY KEY(room_id, task_id, kind, execution_generation))""",
  49. # Transcript stores only references into the already bounded room log, so
  50. # prompt payloads are never duplicated outside room byte limits.
  51. """CREATE TABLE IF NOT EXISTS hosted_room_policy_transcript (
  52. room_id TEXT NOT NULL, thread_id TEXT NOT NULL, seq INTEGER NOT NULL,
  53. kind TEXT NOT NULL, settled_seq INTEGER, PRIMARY KEY(room_id, thread_id, seq))""",
  54. """CREATE TABLE IF NOT EXISTS hosted_room_policy_transcript_state (
  55. room_id TEXT PRIMARY KEY, schema_version INTEGER NOT NULL)""",
  56. """CREATE INDEX IF NOT EXISTS idx_hosted_room_user_thread
  57. ON hosted_room_events(room_id, json_extract(payload_json, '$.thread_id'), seq)
  58. WHERE kind='message.user'""",
  59. )
  60. _ROOM_EVENT_COLUMNS = hosted_rooms._EVENT_COLUMNS
  61. _DELETE_ACTIVE_EVENTS_SQL = ("DELETE FROM hosted_room_policy_events WHERE room_id=? AND discussion_event_id=?")
  62. _TRANSCRIPT_EVENTS_SQL = f"""WITH transcript_events(seq) AS (
  63. SELECT seq FROM hosted_room_policy_transcript WHERE room_id=? AND thread_id=?
  64. UNION ALL
  65. SELECT settled_seq FROM hosted_room_policy_transcript
  66. WHERE room_id=? AND thread_id=? AND settled_seq IS NOT NULL)
  67. SELECT {", ".join("events." + column for column in _ROOM_EVENT_COLUMNS.split(", "))}
  68. FROM transcript_events
  69. JOIN hosted_room_events AS events ON events.room_id=? AND events.seq=transcript_events.seq
  70. ORDER BY events.seq LIMIT ?"""
  71. @dataclass(frozen=True)
  72. class PolicySnapshot:
  73. """Bounded active policy input at one durable room-log cursor."""
  74. through_seq: int
  75. stopped_through_seq: int
  76. events: tuple[dict[str, Any], ...]
  77. watermarks: Mapping[tuple[str, str], int]
  78. _event_from_room_row = hosted_rooms._event_from_row
  79. def _text(mapping: Mapping[str, Any], key: str) -> str:
  80. return str(mapping.get(key) or "")
  81. def _require_room(conn: sqlite3.Connection, room_id: str) -> None:
  82. if conn.execute("SELECT 1 FROM hosted_rooms WHERE room_id=?", (room_id,)).fetchone() is None:
  83. raise hosted_rooms.RoomNotFoundError("hosted room not found")
  84. def _settled_message(
  85. conn: sqlite3.Connection, room_id: str, discussion_event_id: str, message_event_id: Any) -> dict[str, Any] | None:
  86. """Read the committed message even after its active discussion was compacted."""
  87. row = conn.execute(
  88. f"SELECT {_ROOM_EVENT_COLUMNS} FROM hosted_room_events WHERE room_id=? AND event_id=?",
  89. (room_id, message_event_id)).fetchone()
  90. if row is None:
  91. return None
  92. event = _event_from_room_row(row)
  93. if event["kind"] != "message.member" or _text(event["payload"], "discussion_event_id") != discussion_event_id:
  94. return None
  95. return event
  96. class HostedRoomPolicyCheckpoint:
  97. """Incrementally index room policy without compacting visible history."""
  98. def __init__(self, db_path: DbPath, *, max_active_events: int = MAX_ACTIVE_POLICY_EVENTS) -> None:
  99. if isinstance(max_active_events, bool) or not isinstance(max_active_events, int) or max_active_events < 1:
  100. raise ValueError("max_active_events must be a positive integer")
  101. self.db_path = Path(db_path)
  102. # LIMIT also reads one overflow sentinel; keep its parameter inside SQLite's integer range.
  103. capacity = min(2**63 - 2, max(MAX_ACTIVE_POLICY_EVENTS, max_active_events))
  104. with self._transaction() as conn:
  105. for ddl in _SCHEMA_DDL:
  106. conn.execute(ddl)
  107. # Preserve replay capacity across restarts with a lower scheduling budget.
  108. conn.execute("""INSERT INTO hosted_room_policy_capacity(singleton, max_active_events)
  109. VALUES (1, ?) ON CONFLICT(singleton) DO UPDATE SET
  110. max_active_events=MAX(max_active_events, excluded.max_active_events)""", (capacity,))
  111. self.max_active_events = int(conn.execute(
  112. "SELECT max_active_events FROM hosted_room_policy_capacity WHERE singleton=1").fetchone()[0])
  113. self.max_transcript_policy_events = min(
  114. 2**63 - 2, MAX_THREAD_TRANSCRIPT_EVENTS * (self.max_active_events + MAX_ACTIVE_RETRY_RECEIPTS + 2))
  115. def _connect(self) -> sqlite3.Connection:
  116. # Late import: a gateway that outlives an on-disk upgrade has the OLD sqlite_util cached.
  117. from hermes_cli.sqlite_util import open_db
  118. return open_db(self.db_path, db_label="shared-state.db (room policy checkpoint)", busy_timeout_ms=10_000)
  119. def _transaction(self):
  120. from hermes_cli.sqlite_util import transaction
  121. return transaction(self._connect())
  122. @staticmethod
  123. def _store_active_event(
  124. conn: sqlite3.Connection, *, event: Mapping[str, Any], thread_id: str, discussion_event_id: str) -> None:
  125. conn.execute("""INSERT OR IGNORE INTO hosted_room_policy_events(
  126. room_id, thread_id, discussion_event_id, seq, event_json
  127. ) VALUES (?, ?, ?, ?, ?)""",
  128. (event["room_id"], thread_id, discussion_event_id, int(event["seq"]), compact_json(dict(event))))
  129. @staticmethod
  130. def _store_transcript_event(
  131. conn: sqlite3.Connection, *, event: Mapping[str, Any], thread_id: str, settled_seq: int | None = None) -> None:
  132. conn.execute("""INSERT INTO hosted_room_policy_transcript(
  133. room_id, thread_id, seq, kind, settled_seq
  134. ) VALUES (?, ?, ?, ?, ?)
  135. ON CONFLICT(room_id, thread_id, seq) DO UPDATE SET
  136. settled_seq=COALESCE(excluded.settled_seq, hosted_room_policy_transcript.settled_seq)""",
  137. (event["room_id"], thread_id, int(event["seq"]), str(event["kind"]), settled_seq))
  138. if event["kind"] in {"message.user", "message.member"}:
  139. cutoff = conn.execute("""SELECT seq FROM hosted_room_policy_transcript
  140. WHERE room_id=? AND thread_id=? AND kind IN ('message.user', 'message.member')
  141. ORDER BY seq DESC LIMIT 1 OFFSET ?""",
  142. (event["room_id"], thread_id, MAX_THREAD_TRANSCRIPT_EVENTS - 1)).fetchone()
  143. if cutoff is not None:
  144. conn.execute(
  145. "DELETE FROM hosted_room_policy_transcript WHERE room_id=? AND thread_id=? AND seq<?",
  146. (event["room_id"], thread_id, int(cutoff["seq"])))
  147. def _discussion_events(
  148. self, conn: sqlite3.Connection, *, room_id: str, thread_id: str, discussion_event_id: str, bound_error: str
  149. ) -> list[dict[str, Any]]:
  150. """Merge the thread transcript with the active projection, ordered by seq."""
  151. active_rows = []
  152. for receipts, bound in ((0, self.max_active_events), (1, MAX_ACTIVE_RETRY_RECEIPTS)):
  153. rows = conn.execute("""SELECT event_json FROM hosted_room_policy_events
  154. WHERE room_id=? AND discussion_event_id=?
  155. AND (json_extract(event_json, '$.kind') = 'turn.deferred') = ?
  156. ORDER BY seq LIMIT ?""",
  157. (room_id, discussion_event_id, receipts, bound + 1)).fetchall()
  158. if len(rows) > bound:
  159. raise RuntimeError(bound_error)
  160. active_rows.extend(rows)
  161. rows = conn.execute(_TRANSCRIPT_EVENTS_SQL,
  162. (room_id, thread_id, room_id, thread_id, room_id,
  163. self.max_transcript_policy_events + 1)).fetchall()
  164. if len(rows) > self.max_transcript_policy_events:
  165. raise RuntimeError("thread policy transcript exceeded its bound")
  166. events_by_seq = {
  167. int(event["seq"]): event
  168. for event in (*map(_event_from_room_row, rows), *(json.loads(row["event_json"]) for row in active_rows))}
  169. return [events_by_seq[seq] for seq in sorted(events_by_seq)]
  170. # -- per-kind projection handlers (dispatched by _apply_event) -----------
  171. def _apply_user_message(
  172. self, conn: sqlite3.Connection, event: Mapping[str, Any], payload: Mapping[str, Any]) -> None:
  173. room_id = str(event["room_id"])
  174. thread_id, event_id = _text(payload, "thread_id"), _text(event, "event_id")
  175. if not thread_id or not event_id:
  176. return
  177. conn.execute("""INSERT INTO hosted_room_policy_threads(
  178. room_id, thread_id, discussion_event_id, latest_user_seq, completed
  179. ) VALUES (?, ?, ?, ?, 0)
  180. ON CONFLICT(room_id, thread_id) DO UPDATE SET
  181. discussion_event_id=excluded.discussion_event_id,
  182. latest_user_seq=excluded.latest_user_seq, completed=0""",
  183. (room_id, thread_id, event_id, int(event["seq"])))
  184. self._store_active_event(conn, event=event, thread_id=thread_id, discussion_event_id=event_id)
  185. self._store_transcript_event(conn, event=event, thread_id=thread_id)
  186. def _apply_discussion_event(
  187. self, conn: sqlite3.Connection, event: Mapping[str, Any], payload: Mapping[str, Any]) -> None:
  188. """Index member messages and terminal turn outcomes of a known discussion."""
  189. room_id, seq, kind = str(event["room_id"]), int(event["seq"]), _text(event, "kind")
  190. thread_id, discussion_event_id = _text(payload, "thread_id"), _text(payload, "discussion_event_id")
  191. if conn.execute(
  192. "SELECT 1 FROM hosted_room_policy_events WHERE room_id=? AND discussion_event_id=? LIMIT 1",
  193. (room_id, discussion_event_id)).fetchone() is not None:
  194. self._store_active_event(conn, event=event, thread_id=thread_id, discussion_event_id=discussion_event_id)
  195. # Late outcomes still need publication receipts and transcript commits,
  196. # but must not resurrect a completed discussion's active projection.
  197. if kind not in _TERMINAL_KINDS:
  198. return
  199. task_id = _text(payload, "task_id")
  200. execution_generation = int(payload.get("execution_generation") or 0) if kind == "turn.deferred" else 0
  201. if task_id:
  202. conn.execute("""INSERT OR IGNORE INTO hosted_room_policy_publications(
  203. room_id, task_id, kind, execution_generation, seq
  204. ) VALUES (?, ?, ?, ?, ?)""",
  205. (room_id, task_id, kind, execution_generation, seq))
  206. member_id = _text(payload, "member_id")
  207. seen_through_seq = int(payload.get("seen_through_seq") or 0)
  208. if kind == "turn.settled" and payload.get("message_event_id"):
  209. committed = _settled_message(conn, room_id, discussion_event_id, payload["message_event_id"])
  210. if committed is not None:
  211. seen_through_seq = max(seen_through_seq, int(committed["seq"]))
  212. self._store_transcript_event(conn, event=committed, thread_id=thread_id, settled_seq=seq)
  213. else:
  214. # Non-visible receipts still supply historical reconstruction watermarks.
  215. self._store_transcript_event(conn, event=event, thread_id=thread_id)
  216. if member_id and seen_through_seq > 0:
  217. conn.execute("""INSERT INTO hosted_room_policy_watermarks(
  218. room_id, thread_id, member_id, seen_through_seq
  219. ) VALUES (?, ?, ?, ?)
  220. ON CONFLICT(room_id, thread_id, member_id) DO UPDATE SET
  221. seen_through_seq=MAX(hosted_room_policy_watermarks.seen_through_seq, excluded.seen_through_seq)""",
  222. (room_id, thread_id, member_id, seen_through_seq))
  223. def _apply_room_activity(
  224. self, conn: sqlite3.Connection, event: Mapping[str, Any], payload: Mapping[str, Any]) -> None:
  225. room_id, thread_id = str(event["room_id"]), _text(payload, "thread_id")
  226. conn.execute(_DELETE_ACTIVE_EVENTS_SQL, (room_id, _text(payload, "discussion_event_id")))
  227. conn.execute("DELETE FROM hosted_room_policy_threads WHERE room_id=? AND thread_id=?", (room_id, thread_id))
  228. def _apply_continued(
  229. self, conn: sqlite3.Connection, event: Mapping[str, Any], payload: Mapping[str, Any]) -> None:
  230. """Continue reopens a discussion its cap completed: restore its thread row and active projection
  231. from the room log (the cap's ``room.activity`` dropped both)."""
  232. room_id, discussion_event_id = str(event["room_id"]), _text(payload, "discussion_event_id")
  233. source = conn.execute(f"SELECT {_ROOM_EVENT_COLUMNS} FROM hosted_room_events WHERE room_id=? AND event_id=?",
  234. (room_id, discussion_event_id)).fetchone()
  235. if source is None or source["kind"] != "message.user":
  236. return
  237. source_event = _event_from_room_row(source)
  238. thread_id = _text(source_event["payload"], "thread_id")
  239. conn.execute("""INSERT INTO hosted_room_policy_threads(
  240. room_id, thread_id, discussion_event_id, latest_user_seq, completed
  241. ) VALUES (?, ?, ?, ?, 0)
  242. ON CONFLICT(room_id, thread_id) DO UPDATE SET
  243. discussion_event_id=excluded.discussion_event_id,
  244. latest_user_seq=excluded.latest_user_seq, completed=0""",
  245. (room_id, thread_id, discussion_event_id, int(source_event["seq"])))
  246. self._store_active_event(conn, event=source_event, thread_id=thread_id, discussion_event_id=discussion_event_id)
  247. for row in conn.execute(f"""SELECT {_ROOM_EVENT_COLUMNS} FROM hosted_room_events
  248. WHERE room_id=? AND seq>? AND seq<=? AND kind IN ({",".join("?" * len(_REOPENED_KINDS))})
  249. AND json_extract(payload_json, '$.discussion_event_id')=? ORDER BY seq""",
  250. (room_id, int(source_event["seq"]), int(event["seq"]), *_REOPENED_KINDS,
  251. discussion_event_id)).fetchall():
  252. self._store_active_event(conn, event=_event_from_room_row(row), thread_id=thread_id,
  253. discussion_event_id=discussion_event_id)
  254. def _apply_stop_requested(
  255. self, conn: sqlite3.Connection, event: Mapping[str, Any], payload: Mapping[str, Any]) -> None:
  256. conn.execute("""UPDATE hosted_room_policy_cursors
  257. SET stopped_through_seq=MAX(stopped_through_seq, ?) WHERE room_id=?""",
  258. (int(event["seq"]), str(event["room_id"])))
  259. _APPLY_BY_KIND: dict[str, Callable[..., None]] = {
  260. "message.user": _apply_user_message, "message.member": _apply_discussion_event,
  261. **dict.fromkeys(_TERMINAL_KINDS, _apply_discussion_event), "room.activity": _apply_room_activity,
  262. # The lead's assignment joins its discussion's projection so the planner sees who takes round 0.
  263. "room.assignment": _apply_discussion_event,
  264. "room.routed": _apply_discussion_event, "room.continued": _apply_continued,
  265. "room.stop_requested": _apply_stop_requested}
  266. def _apply_event(self, conn: sqlite3.Connection, event: Mapping[str, Any]) -> None:
  267. handler = self._APPLY_BY_KIND.get(_text(event, "kind"))
  268. if handler is not None:
  269. payload = event.get("payload")
  270. handler(self, conn, event, payload if isinstance(payload, Mapping) else {})
  271. def _ensure_cursor_and_transcript(self, conn: sqlite3.Connection, room_id: str) -> int:
  272. """Create the cursor and migrate derived policy caches by bounded replay."""
  273. _require_room(conn, room_id)
  274. conn.execute("""INSERT OR IGNORE INTO hosted_room_policy_cursors(
  275. room_id, through_seq, stopped_through_seq, updated_at
  276. ) VALUES (?, 0, 0, 0)""", (room_id,))
  277. cursor = int(
  278. conn.execute("SELECT through_seq FROM hosted_room_policy_cursors WHERE room_id=?", (room_id,)).fetchone()[
  279. "through_seq"])
  280. transcript_state = conn.execute(
  281. "SELECT schema_version FROM hosted_room_policy_transcript_state WHERE room_id=?", (room_id,)).fetchone()
  282. if transcript_state is None or int(transcript_state["schema_version"]) < _TRANSCRIPT_SCHEMA_VERSION:
  283. # Rebuild derived history without rewriting admissions or the room log.
  284. for table in ("hosted_room_policy_events", "hosted_room_policy_threads",
  285. "hosted_room_policy_watermarks", "hosted_room_policy_transcript"):
  286. conn.execute(f"DELETE FROM {table} WHERE room_id=?", (room_id,))
  287. conn.execute("UPDATE hosted_room_policy_cursors SET through_seq=0, stopped_through_seq=0 WHERE room_id=?",
  288. (room_id,))
  289. cursor = 0
  290. conn.execute("""INSERT INTO hosted_room_policy_transcript_state(room_id, schema_version)
  291. VALUES (?, ?)
  292. ON CONFLICT(room_id) DO UPDATE SET schema_version=excluded.schema_version""",
  293. (room_id, _TRANSCRIPT_SCHEMA_VERSION))
  294. return cursor
  295. def sync(self, *, room_id: str, latest_seq: int) -> int:
  296. """Materialize each unseen event exactly once by durable cursor."""
  297. with self._transaction() as conn:
  298. conn.execute("BEGIN IMMEDIATE")
  299. cursor = self._ensure_cursor_and_transcript(conn, room_id)
  300. if cursor > latest_seq:
  301. with self._connect() as conn:
  302. room = conn.execute("SELECT next_seq FROM hosted_rooms WHERE room_id=?", (room_id,)).fetchone()
  303. if room is None:
  304. raise hosted_rooms.RoomNotFoundError("hosted room not found")
  305. if cursor >= int(room["next_seq"]):
  306. raise RuntimeError("room policy cursor is ahead of the durable log")
  307. while cursor < latest_seq:
  308. page = hosted_rooms.read_events(
  309. self.db_path, room_id=room_id, since_seq=cursor, limit=hosted_rooms.MAX_LOG_LIMIT)
  310. rows = [event for event in page.get("events", []) if isinstance(event, Mapping)]
  311. next_cursor = int(page.get("cursor") or cursor)
  312. if not rows or next_cursor <= cursor:
  313. raise RuntimeError("hosted room policy cursor did not advance")
  314. with self._transaction() as conn:
  315. conn.execute("BEGIN IMMEDIATE")
  316. _require_room(conn, room_id)
  317. current = conn.execute("SELECT through_seq FROM hosted_room_policy_cursors WHERE room_id=?",
  318. (room_id,)).fetchone()
  319. if current is None:
  320. raise RuntimeError("room policy cursor disappeared during replay")
  321. if int(current["through_seq"]) != cursor:
  322. # Another worker applied the page: reload before any projection effects.
  323. cursor = int(current["through_seq"])
  324. continue
  325. for event in rows:
  326. self._apply_event(conn, event)
  327. fenced_update(
  328. conn, "UPDATE hosted_room_policy_cursors SET through_seq=?, updated_at=? WHERE room_id=?",
  329. (next_cursor, float(rows[-1].get("created_at") or 0), room_id),
  330. RuntimeError("room policy cursor disappeared during replay"))
  331. cursor = next_cursor
  332. return cursor
  333. def snapshot(self, *, room_id: str, latest_seq: int) -> PolicySnapshot:
  334. """Return only the oldest active discussion and its watermark set."""
  335. self.sync(room_id=room_id, latest_seq=latest_seq)
  336. with self._transaction() as conn:
  337. cursor = conn.execute(
  338. "SELECT through_seq, stopped_through_seq FROM hosted_room_policy_cursors WHERE room_id=?", (room_id,)).fetchone()
  339. if cursor is None:
  340. raise hosted_rooms.RoomNotFoundError("hosted room checkpoint not found")
  341. through_seq = int(cursor["through_seq"])
  342. stopped_through_seq = int(cursor["stopped_through_seq"])
  343. thread = conn.execute("""SELECT thread_id, discussion_event_id FROM hosted_room_policy_threads
  344. WHERE room_id=? AND completed=0 AND latest_user_seq>?
  345. ORDER BY latest_user_seq, thread_id LIMIT 1""", (room_id, stopped_through_seq)).fetchone()
  346. if thread is None:
  347. return PolicySnapshot(
  348. through_seq=through_seq, stopped_through_seq=stopped_through_seq, events=(), watermarks={})
  349. thread_id = str(thread["thread_id"])
  350. events = self._discussion_events(
  351. conn, room_id=room_id, thread_id=thread_id, discussion_event_id=str(thread["discussion_event_id"]),
  352. bound_error="active room policy projection exceeded its bound")
  353. watermark_rows = conn.execute("""SELECT member_id, seen_through_seq FROM hosted_room_policy_watermarks
  354. WHERE room_id=? AND thread_id=?""", (room_id, thread_id)).fetchall()
  355. return PolicySnapshot(
  356. through_seq=through_seq, stopped_through_seq=stopped_through_seq, events=tuple(events),
  357. watermarks={(thread_id, str(row["member_id"])): int(row["seen_through_seq"]) for row in watermark_rows})
  358. def publication_exists(self, *, room_id: str, task_id: str, status: str, execution_generation: int) -> bool:
  359. """Return whether one exact driver outcome is already in the room log."""
  360. sql, params = (
  361. ("""SELECT 1 FROM hosted_room_policy_publications
  362. WHERE room_id=? AND task_id=? AND kind=? AND execution_generation=?""",
  363. (room_id, task_id, f"turn.{status}", execution_generation))
  364. if status == "deferred" else
  365. ("""SELECT 1 FROM hosted_room_policy_publications
  366. WHERE room_id=? AND task_id=? AND kind IN ('turn.settled', 'turn.failed', 'turn.cancelled')""",
  367. (room_id, task_id)))
  368. with self._transaction() as conn:
  369. return conn.execute(sql, params).fetchone() is not None
  370. def events_for_task(self, *, room_id: str, source_event_seq: int,
  371. input_context: Mapping[str, Any] | None = None,
  372. task_id: str | None = None) -> list[dict[str, Any]]:
  373. """Load one bounded discussion projection for terminal reconstruction."""
  374. with self._transaction() as conn:
  375. published = self._published_task_events(conn, room_id=room_id, task_id=task_id)
  376. if input_context is not None:
  377. context = validate_task_input(input_context)
  378. seqs = sorted({source_event_seq, *context["event_seqs"]})
  379. placeholders = ",".join("?" for _ in seqs)
  380. rows = conn.execute(f"SELECT * FROM hosted_room_events WHERE room_id=? AND seq IN ({placeholders}) ORDER BY seq",
  381. (room_id, *seqs)).fetchall()
  382. if len(rows) != len(seqs):
  383. raise RuntimeError("admitted task input event is missing")
  384. events = {int(row["seq"]): _event_from_room_row(row) for row in rows}
  385. source_event = events[source_event_seq]
  386. thread_id = _text(source_event["payload"], "thread_id")
  387. if source_event["kind"] != "message.user" or not thread_id:
  388. raise RuntimeError("admitted task source is not a user discussion")
  389. # Supersession remains authoritative after the display projection compacts.
  390. latest = conn.execute("""SELECT * FROM hosted_room_events
  391. WHERE room_id=? AND kind='message.user' AND json_extract(payload_json, '$.thread_id')=?
  392. ORDER BY seq DESC LIMIT 1""", (room_id, thread_id)).fetchone()
  393. if latest is not None:
  394. events[int(latest["seq"])] = _event_from_room_row(latest)
  395. for event in published:
  396. events[int(event["seq"])] = event
  397. return [events[seq] for seq in sorted(events)]
  398. row = conn.execute(
  399. f"SELECT {_ROOM_EVENT_COLUMNS} FROM hosted_room_events WHERE room_id=? AND seq=?",
  400. (room_id, source_event_seq)).fetchone()
  401. if row is None or row["kind"] != "message.user":
  402. return published
  403. source = _event_from_room_row(row)
  404. events = self._discussion_events(
  405. conn, room_id=room_id, thread_id=_text(source["payload"], "thread_id"),
  406. discussion_event_id=str(source["event_id"]),
  407. bound_error="task policy projection exceeded its bound")
  408. # The source can age out of BOTH bounded projections while a
  409. # deferred task remains retryable. Its frozen prompt lives in the task.
  410. by_seq = {event["seq"]: event for event in (*events, *published)}
  411. by_seq[source_event_seq] = source
  412. return [by_seq[seq] for seq in sorted(by_seq)]
  413. def compact_completed(self, *, room_id: str) -> None:
  414. """Drop any completed projections left by an interrupted sync."""
  415. with self._transaction() as conn:
  416. for row in conn.execute(
  417. "SELECT discussion_event_id FROM hosted_room_policy_threads WHERE room_id=? AND completed=1", (room_id,)
  418. ).fetchall():
  419. conn.execute(_DELETE_ACTIVE_EVENTS_SQL, (room_id, str(row["discussion_event_id"])))
  420. conn.execute("DELETE FROM hosted_room_policy_threads WHERE room_id=? AND completed=1", (room_id,))
  421. def _published_task_events(
  422. self,
  423. conn: sqlite3.Connection,
  424. *,
  425. room_id: str,
  426. task_id: str | None,
  427. ) -> list[dict[str, Any]]:
  428. if not task_id:
  429. return []
  430. digest = task_id.removeprefix("dtask:")
  431. rows = conn.execute(
  432. """SELECT * FROM hosted_room_events
  433. WHERE room_id=? AND event_id IN (?, ?) ORDER BY seq""",
  434. (room_id, f"dmessage:{digest}", f"dterminal:{digest}"),
  435. ).fetchall()
  436. events = [_event_from_room_row(row) for row in rows]
  437. if any(event["payload"].get("task_id") != task_id for event in events):
  438. raise RuntimeError("published task receipt identity changed")
  439. return events