ai_co_discuss 0.2.68 → 0.2.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ answers by itself always hosts, routes unnamed messages, names members in its an
9
9
  woken once when everything it dispatched has finished. No software is launched here.
10
10
  """
11
11
 
12
+ import hashlib
12
13
  import hmac
13
14
  import json
14
15
  import re
@@ -38,6 +39,8 @@ ASSIGN_HOST = """UPDATE room_members SET role='host' WHERE id=(SELECT id FROM ro
38
39
  AND access<>'online' ORDER BY access='runner' DESC, id LIMIT 1)
39
40
  AND NOT EXISTS (SELECT 1 FROM room_members WHERE room_id=? AND role='host' AND status='active')"""
40
41
  TERMINAL = ("completed", "cancelled", "failed")
42
+ # A queued member has been asked once its request for that message exists.
43
+ QUEUE_ASKED = "EXISTS(SELECT 1 FROM member_requests r WHERE r.message_id={row}.message_id AND r.member_id={row}.member_id)"
41
44
  SESSION_MODES = ("new", "borrowed")
42
45
  FREE_OPERATIONS = ("messages", "attachments", "move", "folder", "archive", "restore", "delete")
43
46
  MEMBER_FIELDS = ("id", "name", "adapter", "role", "status", "access", "session_mode", "read_until")
@@ -247,7 +250,8 @@ def free_view(connection, room_id):
247
250
  members.append(member)
248
251
  names = {row["id"]: row["name"] for row in connection.execute("SELECT id,name FROM room_members WHERE room_id=?", (room_id,))}
249
252
  requests = [{**dict(row), "member": names.get(row["member_id"], "")} for row in connection.execute(
250
- """SELECT id,member_id,message_id,status,execution_id,answer_id,chain_id,batch_id,purpose,created_by,created_at FROM member_requests
253
+ """SELECT id,member_id,message_id,status,execution_id,answer_id,chain_id,batch_id,purpose,created_by,created_at,
254
+ (SELECT reason FROM request_cancel_reasons c WHERE c.request_id=member_requests.id) AS cancel_reason FROM member_requests
251
255
  WHERE room_id=? ORDER BY id DESC LIMIT ?""", (room_id, RECENT_REQUESTS))]
252
256
  chains = [dict(row) for row in connection.execute(
253
257
  "SELECT chain_id,reason FROM free_chains WHERE room_id=? ORDER BY chain_id DESC LIMIT 10", (room_id,))]
@@ -344,6 +348,20 @@ def recipients(connection, room_id, payload):
344
348
  return [(host["id"], "route")] if host else []
345
349
 
346
350
 
351
+ def route_held_messages(connection, room_id, host):
352
+ """When a discussion ends, hand the user's messages its host never answered to the free room's recipients, as if just sent."""
353
+ held = connection.execute("SELECT held_since FROM turn_relay WHERE room_id=?", (room_id,)).fetchone()
354
+ if held and held[0]:
355
+ rows = connection.execute("""SELECT id FROM messages m WHERE room_id=? AND author='用户' AND id>=?
356
+ AND id>COALESCE((SELECT MAX(id) FROM messages WHERE room_id=? AND author=?),0)
357
+ AND NOT EXISTS(SELECT 1 FROM member_requests r WHERE r.message_id=m.id) ORDER BY id""",
358
+ (room_id, held[0], room_id, host)).fetchall()
359
+ for row in rows:
360
+ for member_id, purpose in recipients(connection, room_id, {}):
361
+ insert_request(connection, room_id, member_id, row["id"], row["id"], purpose, "user")
362
+ connection.execute("UPDATE turn_relay SET held_since=0 WHERE room_id=?", (room_id,))
363
+
364
+
347
365
  def proxy_member(connection, room_id, author):
348
366
  """Return the active online member the owner may forward an answer for."""
349
367
  row = connection.execute("SELECT * FROM room_members WHERE room_id=? AND name=? AND status='active' AND access='online'",
@@ -532,6 +550,120 @@ def settle_batches(connection, room_id):
532
550
  halt_chain(connection, room_id, batch["chain_id"], "budget")
533
551
  else:
534
552
  insert_request(connection, room_id, host["id"], batch["batch_id"], batch["chain_id"], "handoff", "host")
553
+ advance_queues(connection, room_id)
554
+
555
+
556
+ def queue_in_turn(connection, room_id, message_id, member_ids):
557
+ """Line the named members up for one owner message in the room's speaking order; only the first is asked now."""
558
+ chosen = set(member_ids)
559
+ members = [dict(row) for row in connection.execute("SELECT id,name,role FROM room_members WHERE room_id=?", (room_id,))]
560
+ for member in default_order(connection, room_id, [member for member in members if member["id"] in chosen]):
561
+ connection.execute("INSERT INTO free_queue (room_id,message_id,member_id) VALUES (?,?,?)", (room_id, message_id, member["id"]))
562
+ advance_queues(connection, room_id)
563
+
564
+
565
+ def advance_queues(connection, room_id):
566
+ """Ask the next queued member of each message whose previous request ended; members no longer present are skipped.
567
+
568
+ Asked rows stay, so the speaking order can still show who already answered.
569
+ """
570
+ connection.execute(f"""DELETE FROM free_queue WHERE room_id=? AND NOT {QUEUE_ASKED.format(row="free_queue")}
571
+ AND NOT EXISTS(SELECT 1 FROM room_members m WHERE m.id=free_queue.member_id AND m.status='active')""", (room_id,))
572
+ connection.execute(f"""INSERT OR IGNORE INTO member_requests (room_id,member_id,message_id,chain_id,batch_id,purpose,created_by,created_at)
573
+ SELECT q.room_id,q.member_id,q.message_id,q.message_id,0,'ask','user',? FROM free_queue q
574
+ WHERE q.room_id=? AND q.id=(SELECT MIN(x.id) FROM free_queue x WHERE x.message_id=q.message_id AND NOT {QUEUE_ASKED.format(row="x")})
575
+ AND NOT EXISTS(SELECT 1 FROM member_requests r WHERE r.message_id=q.message_id AND r.status IN ('pending','delivering'))""",
576
+ (int(time.time()), room_id))
577
+
578
+
579
+ def default_order(connection, room_id, members):
580
+ """Members in the room's saved speaking order; without one (or for members it lacks), the host first, then by joining order."""
581
+ row = connection.execute("SELECT member_ids FROM speaking_orders WHERE room_id=?", (room_id,)).fetchone()
582
+ rank = {member_id: index for index, member_id in enumerate(json.loads(row[0]) if row else [])}
583
+ return sorted(members, key=lambda member: (member["id"] not in rank, rank.get(member["id"], 0), member["role"] != "host", member["id"]))
584
+
585
+
586
+ def speaking_order(connection, room):
587
+ """Who spoke, who speaks now and who is next, shared by free rooms and discussions; only the not-yet-spoken part may be reordered.
588
+
589
+ A free room shows its latest unfinished one-after-another queue, else every member in the room's default order;
590
+ a discussion shows the open round: speakers so far, the current turn, then queued turns. The revision changes with any of it.
591
+ """
592
+ active = [member for member in room["members"] if member["status"] == "active"]
593
+ by_id = {member["id"]: member for member in active}
594
+ ordered = [member["name"] for member in default_order(connection, room["id"], active)]
595
+ items, kind, target = [], "default", 0
596
+ if room["mode"] == "free":
597
+ target = connection.execute("""SELECT MAX(q.message_id) FROM free_queue q LEFT JOIN member_requests r ON r.message_id=q.message_id
598
+ AND r.member_id=q.member_id WHERE q.room_id=? AND (r.id IS NULL OR r.status IN ('pending','delivering'))""", (room["id"],)).fetchone()[0] or 0
599
+ if target:
600
+ kind = "queue"
601
+ for row in connection.execute("""SELECT q.member_id,r.status FROM free_queue q LEFT JOIN member_requests r
602
+ ON r.message_id=q.message_id AND r.member_id=q.member_id WHERE q.message_id=? ORDER BY q.id""", (target,)):
603
+ if row["member_id"] in by_id:
604
+ state = "queued" if row["status"] is None else "speaking" if row["status"] in ("pending", "delivering") else "done"
605
+ items.append({"name": by_id[row["member_id"]]["name"], "state": state})
606
+ else:
607
+ kind = "turns"
608
+ last = room["rounds"][-1] if room["rounds"] else None
609
+ items = [{"name": name, "state": "done"} for name in (last["speakers"] if last and not last["completed"] else [])]
610
+ turn = room["current_turn"]
611
+ if turn and turn["status"] in ("pending", "notified"):
612
+ items.append({"name": turn["expert"], "state": "speaking"})
613
+ items += [{"name": item["expert"], "state": "queued"} for item in room["queued_turns"]]
614
+ # The user may still hold the host seat of an old discussion; only AI participants have a place.
615
+ ordered = [name for name in [room["host"], *ordered, *room["experts"]] if (name in room["experts"] or name == room["host"]) and name != "用户"]
616
+ listed = {item["name"] for item in items}
617
+ items += [{"name": name, "state": "idle"} for name in dict.fromkeys(ordered) if name not in listed]
618
+ movable = "queued" if kind != "default" else "idle"
619
+ state = json.dumps([kind, target, [[item["name"], item["state"]] for item in items]], ensure_ascii=False)
620
+ return {"kind": kind, "message_id": target, "items": items, "movable": [item["name"] for item in items if item["state"] == movable],
621
+ "default": [member["name"] for member in default_order(connection, room["id"], active)],
622
+ "revision": hashlib.sha256(state.encode("utf-8")).hexdigest()[:12]}
623
+
624
+
625
+ def reorder_speakers(connection, room, token, payload):
626
+ """Owner reorders the not-yet-spoken part of the speaking order; who already spoke or speaks now stays put.
627
+
628
+ Read the speaking order first and send its revision: a missing revision is refused, a changed one conflicts,
629
+ and the new order must list exactly the movable members.
630
+ """
631
+ require_owner(connection, room["id"], token)
632
+ current = room["speaking_order"]
633
+ if payload.get("revision") is None:
634
+ raise ValueError("调整发言顺序前须先读取最新顺序(speaking-order),并带上其 revision")
635
+ if payload["revision"] != current["revision"]:
636
+ raise FileExistsError("发言顺序已变化,请重新读取后再调整")
637
+ order = payload.get("order")
638
+ movable = current["movable"]
639
+ if not isinstance(order, list) or sorted(map(str, order)) != sorted(movable):
640
+ given = [str(name) for name in order] if isinstance(order, list) else []
641
+ missing = [name for name in dict.fromkeys(movable) if given.count(name) < movable.count(name)]
642
+ extra = [name for name in dict.fromkeys(given) if movable.count(name) < given.count(name)]
643
+ raise ValueError("新顺序须恰好包含尚未发言的成员" + (f";缺少:{'、'.join(missing)}" if missing else "") + (f";多出:{'、'.join(extra)}" if extra else ""))
644
+ names = {member["name"]: member["id"] for member in room["members"] if member["status"] == "active"}
645
+ if current["kind"] == "queue":
646
+ connection.execute(f"DELETE FROM free_queue WHERE message_id=? AND NOT {QUEUE_ASKED.format(row='free_queue')}", (current["message_id"],))
647
+ for name in order:
648
+ connection.execute("INSERT INTO free_queue (room_id,message_id,member_id) VALUES (?,?,?)", (room["id"], current["message_id"], names[name]))
649
+ elif current["kind"] == "turns":
650
+ reorder_turns(connection, room["id"], order)
651
+ else:
652
+ connection.execute("INSERT OR REPLACE INTO speaking_orders (room_id,member_ids) VALUES (?,?)",
653
+ (room["id"], json.dumps([names[name] for name in order])))
654
+
655
+
656
+ def reorder_turns(connection, room_id, order):
657
+ """Move queued turns into the new order; ids keep the relay order, so each turn's content moves to the id at its new place."""
658
+ rows = [dict(row) for row in connection.execute("SELECT * FROM discussion_turns WHERE room_id=? AND status='queued' ORDER BY id", (room_id,))]
659
+ remaining, moved = list(rows), []
660
+ for name in order:
661
+ moved.append(remaining.pop(next(index for index, row in enumerate(remaining) if row["expert"] == name)))
662
+ # Submission ids are unique, so free them before writing them to their new rows.
663
+ connection.execute("UPDATE discussion_turns SET submission_id=submission_id||':moving:'||id WHERE room_id=? AND status='queued'", (room_id,))
664
+ for slot, content in zip(rows, moved):
665
+ connection.execute("UPDATE discussion_turns SET expert=?,question=?,submission_id=?,created_at=? WHERE id=?",
666
+ (content["expert"], content["question"], content["submission_id"], content["created_at"], slot["id"]))
535
667
 
536
668
 
537
669
  def answered_chain(connection, payload, actor):
@@ -549,7 +681,11 @@ def after_free_message(connection, room_id, message_id, actor, payload):
549
681
  """Create requests for the owner's message, or close what the member's answer covers."""
550
682
  now = int(time.time())
551
683
  if actor == "用户":
552
- for member_id, purpose in recipients(connection, room_id, payload):
684
+ pairs = recipients(connection, room_id, payload)
685
+ if payload.get("sequential") is True and payload.get("to") is not None:
686
+ queue_in_turn(connection, room_id, message_id, [member_id for member_id, _ in pairs])
687
+ return
688
+ for member_id, purpose in pairs:
553
689
  insert_request(connection, room_id, member_id, message_id, message_id, purpose, "user")
554
690
  return
555
691
  if actor["access"] == "online":
@@ -601,7 +737,8 @@ def asks_view(connection, room_id):
601
737
  """Direct questions of a discussion room: recent requests with their member, for status badges beside messages."""
602
738
  names = {row["id"]: row["name"] for row in connection.execute("SELECT id,name FROM room_members WHERE room_id=?", (room_id,))}
603
739
  requests = [{**dict(row), "member": names.get(row["member_id"], "")} for row in connection.execute(
604
- """SELECT id,member_id,message_id,status,execution_id,answer_id,chain_id,batch_id,purpose,created_by,created_at FROM member_requests
740
+ """SELECT id,member_id,message_id,status,execution_id,answer_id,chain_id,batch_id,purpose,created_by,created_at,
741
+ (SELECT reason FROM request_cancel_reasons c WHERE c.request_id=member_requests.id) AS cancel_reason FROM member_requests
605
742
  WHERE room_id=? ORDER BY id DESC LIMIT ?""", (room_id, RECENT_REQUESTS))]
606
743
  return {"requests": requests[::-1], "executions": recent_executions(connection, room_id), "execution_minutes": execution_minutes(connection, room_id)}
607
744
 
@@ -619,22 +756,56 @@ def expire_executions(connection, room_id):
619
756
  settle_batches(connection, room_id)
620
757
 
621
758
 
622
- def finish_execution(connection, execution_id, status, result, request_status):
759
+ def record_cancel_reasons(connection, where, params, reason):
760
+ """Remember why requests are about to be cancelled; call before the status change selects them."""
761
+ connection.execute(f"INSERT OR REPLACE INTO request_cancel_reasons (request_id,reason) SELECT id,? FROM member_requests WHERE {where}", (reason, *params))
762
+
763
+
764
+ def finish_execution(connection, execution_id, status, result, request_status, cancel_reason=""):
623
765
  """End one execution and move its delivered requests in the same transaction."""
766
+ if request_status == "cancelled" and cancel_reason:
767
+ record_cancel_reasons(connection, "execution_id=? AND status='delivering'", (execution_id,), cancel_reason)
624
768
  connection.execute("UPDATE member_executions SET status=?,finished_at=?,result=? WHERE id=? AND status IN ('dispatched','started')",
625
769
  (status, int(time.time()), result[:500], execution_id))
626
770
  connection.execute("UPDATE member_requests SET status=?,execution_id=CASE WHEN ?='pending' THEN NULL ELSE execution_id END WHERE execution_id=? AND status='delivering'",
627
771
  (request_status, request_status, execution_id))
628
772
 
629
773
 
630
- def stop_member_work(connection, member_id, reason):
774
+ def open_route_messages(connection, member_id, pending_only=False):
775
+ """Messages whose host request is still open, in order; they need a new host if this one leaves."""
776
+ states = "'pending'" if pending_only else "'pending','delivering'"
777
+ return [row[0] for row in connection.execute(
778
+ f"SELECT message_id FROM member_requests WHERE member_id=? AND purpose='route' AND status IN ({states}) ORDER BY message_id", (member_id,))]
779
+
780
+
781
+ def hand_over_routes(connection, room_id, message_ids, departed_id):
782
+ """Ask the current host to route messages left behind by the previous host."""
783
+ host = connection.execute("SELECT id FROM room_members WHERE room_id=? AND role='host' AND status='active' AND id<>?",
784
+ (room_id, departed_id or 0)).fetchone()
785
+ for message_id in message_ids if host else []:
786
+ connection.execute("""INSERT OR IGNORE INTO member_requests (room_id,member_id,message_id,chain_id,purpose,created_by,created_at)
787
+ VALUES (?,?,?,?,'route','user',?)""", (room_id, host["id"], message_id, message_id, int(time.time())))
788
+
789
+
790
+ def stop_member_work(connection, member_id, reason, cancel_reason):
631
791
  """Void a member's live execution and every open request, so nothing restarts by itself."""
632
792
  for row in connection.execute("SELECT id FROM member_executions WHERE member_id=? AND status IN ('dispatched','started')",
633
793
  (member_id,)).fetchall():
634
- finish_execution(connection, row["id"], "cancelled", reason, "cancelled")
794
+ finish_execution(connection, row["id"], "cancelled", reason, "cancelled", cancel_reason)
795
+ record_cancel_reasons(connection, "member_id=? AND status='pending'", (member_id,), cancel_reason)
635
796
  connection.execute("UPDATE member_requests SET status='cancelled' WHERE member_id=? AND status='pending'", (member_id,))
636
797
 
637
798
 
799
+ def stop_room_work(connection, room_id, reason, cancel_reason):
800
+ """Void every live execution and open request of a room, e.g. when it is archived."""
801
+ for row in connection.execute("SELECT id FROM member_executions WHERE room_id=? AND status IN ('dispatched','started')",
802
+ (room_id,)).fetchall():
803
+ finish_execution(connection, row["id"], "cancelled", reason, "cancelled", cancel_reason)
804
+ record_cancel_reasons(connection, "room_id=? AND status='pending'", (room_id,), cancel_reason)
805
+ connection.execute("UPDATE member_requests SET status='cancelled' WHERE room_id=? AND status='pending'", (room_id,))
806
+ settle_batches(connection, room_id)
807
+
808
+
638
809
  def inbox(connection, room_id, token):
639
810
  """List what the runner should deliver or reconcile; owner credential only."""
640
811
  require_owner(connection, room_id, token)
@@ -702,6 +873,7 @@ def route_wide(connection, room_id, message_id, action):
702
873
  if route["status"] != "pending":
703
874
  raise FileExistsError("主持人已开始处理这条消息,不再改派")
704
875
  if action == "none":
876
+ record_cancel_reasons(connection, "id=?", (route["id"],), "no_reply")
705
877
  connection.execute("UPDATE member_requests SET status='cancelled' WHERE id=?", (route["id"],))
706
878
  else:
707
879
  # The host keeps its row (one request per message and member) and now answers its own share, without dispatching.
@@ -737,8 +909,9 @@ def adopt_recommendation(connection, room_id, token, message_id, payload):
737
909
  mode = payload["mode"]
738
910
  if mode == "interrupt" and route["status"] == "delivering":
739
911
  # The host's execution may carry other messages too; stopping it voids that whole execution.
740
- finish_execution(connection, route["execution_id"], "cancelled", "智能调度改派,改由其他成员回答", "cancelled")
912
+ finish_execution(connection, route["execution_id"], "cancelled", "智能调度改派,改由其他成员回答", "cancelled", "rerouted")
741
913
  elif mode == "interrupt" and route["status"] == "pending":
914
+ record_cancel_reasons(connection, "id=?", (route["id"],), "rerouted")
742
915
  connection.execute("UPDATE member_requests SET status='cancelled' WHERE id=?", (route["id"],))
743
916
  connection.execute("UPDATE member_recommendations SET status='waiting',mode=? WHERE message_id=?", (mode, message_id))
744
917
  release_recommendations(connection, room_id)
@@ -863,7 +1036,7 @@ def owned_member(connection, room_id, member_id, token):
863
1036
  def interrupt_member(connection, room_id, member_id, token):
864
1037
  """Owner stop: void the live execution and every open request."""
865
1038
  owned_member(connection, room_id, member_id, token)
866
- stop_member_work(connection, member_id, "用户打断")
1039
+ stop_member_work(connection, member_id, "用户打断", "user_stop")
867
1040
  settle_batches(connection, room_id)
868
1041
  return free_view(connection, room_id)
869
1042
 
@@ -1017,8 +1190,12 @@ def member_action(connection, room_id, member_id, token, payload):
1017
1190
  """
1018
1191
  member = owned_member(connection, room_id, member_id, token)
1019
1192
  action = payload.get("action")
1193
+ routed = []
1020
1194
  if action in ("away", "remove"):
1021
- stop_member_work(connection, member_id, "成员已离开" if action == "away" else "成员已移除")
1195
+ # Unnamed messages still waiting on a departing host move to its successor instead of being dropped.
1196
+ routed = open_route_messages(connection, member_id)
1197
+ stop_member_work(connection, member_id, "成员已离开" if action == "away" else "成员已移除",
1198
+ "member_away" if action == "away" else "member_removed")
1022
1199
  connection.execute("UPDATE room_members SET status=? WHERE id=?", ("away" if action == "away" else "removed", member_id))
1023
1200
  elif action == "active":
1024
1201
  hashed = token_hash(payload.get("token"))
@@ -1037,10 +1214,17 @@ def member_action(connection, room_id, member_id, token, payload):
1037
1214
  if role == "member" and member["role"] == "host":
1038
1215
  raise ValueError("自由房间须由一位 AI 主持;请指定另一位成员担任主持人")
1039
1216
  if role == "host":
1217
+ # Unnamed messages the old host has not started move to the new host, which carries the host rules.
1218
+ old_host = connection.execute("SELECT id FROM room_members WHERE room_id=? AND role='host' AND id<>?", (room_id, member_id)).fetchone()
1219
+ if old_host:
1220
+ routed = open_route_messages(connection, old_host["id"], pending_only=True)
1221
+ record_cancel_reasons(connection, "member_id=? AND purpose='route' AND status='pending'", (old_host["id"],), "rerouted")
1222
+ connection.execute("UPDATE member_requests SET status='cancelled' WHERE member_id=? AND purpose='route' AND status='pending'", (old_host["id"],))
1040
1223
  connection.execute("UPDATE room_members SET role='member' WHERE room_id=? AND role='host'", (room_id,))
1041
1224
  connection.execute("UPDATE room_members SET role=? WHERE id=?", (role, member_id))
1042
1225
  else:
1043
1226
  raise ValueError("action 须为 away、active、remove、role 或 adopt")
1044
1227
  settle_batches(connection, room_id)
1045
1228
  sync_participants(connection, room_id)
1229
+ hand_over_routes(connection, room_id, routed, member_id if action in ("away", "remove") else None)
1046
1230
  return free_view(connection, room_id)
@@ -84,7 +84,7 @@ def limits_view(row, room):
84
84
  def require_within_limits(room):
85
85
  """Stop new expert work at the configured boundary, preserving user notes."""
86
86
  if room["limits"]["reached"]:
87
- raise FileExistsError("已达到讨论上限;请检查上限并暂停,由用户决定追加轮次或结束")
87
+ raise FileExistsError("已达到讨论上限;请检查上限,讨论随之结束并回到自由协作")
88
88
 
89
89
 
90
90
  def execution_seconds(connection, room_id):
@@ -595,6 +595,18 @@ CREATE TABLE IF NOT EXISTS scheduler_feedback (
595
595
  note TEXT NOT NULL DEFAULT '',
596
596
  created_at TEXT NOT NULL
597
597
  );
598
+ -- Messages the local service is still asking the decision model about; a transient marker, not part of backups.
599
+ CREATE TABLE IF NOT EXISTS scheduler_routing (
600
+ room_id TEXT NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,
601
+ message_id INTEGER NOT NULL,
602
+ created_at INTEGER NOT NULL,
603
+ PRIMARY KEY (room_id, message_id)
604
+ );
605
+ -- Why a request was cancelled; a side table so older databases need no column change. Requests cancelled before it existed have no row.
606
+ CREATE TABLE IF NOT EXISTS request_cancel_reasons (
607
+ request_id INTEGER PRIMARY KEY REFERENCES member_requests(id) ON DELETE CASCADE,
608
+ reason TEXT NOT NULL CHECK(reason IN ('user_stop','member_away','member_removed','rerouted','no_reply','archived'))
609
+ );
598
610
  -- What produced a decision (model, threshold, prompt and package versions), so ratings stay comparable across versions.
599
611
  CREATE TABLE IF NOT EXISTS scheduler_decision_context (
600
612
  decision_id INTEGER PRIMARY KEY REFERENCES scheduler_decisions(id) ON DELETE CASCADE,
@@ -628,6 +640,19 @@ CREATE TABLE IF NOT EXISTS free_batches (
628
640
  settled INTEGER NOT NULL DEFAULT 0,
629
641
  PRIMARY KEY (room_id,batch_id)
630
642
  );
643
+ -- Members the user named to answer one after another, in the room's speaking order: each gets its request once the one before
644
+ -- it ends. Asked rows stay so the speaking order can show who already answered; the owner may reorder rows not yet asked.
645
+ CREATE TABLE IF NOT EXISTS free_queue (
646
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
647
+ room_id TEXT NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,
648
+ message_id INTEGER NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
649
+ member_id INTEGER NOT NULL REFERENCES room_members(id) ON DELETE CASCADE
650
+ );
651
+ -- The owner's default speaking order (JSON list of member ids); members it lacks follow, the host first, then by joining order.
652
+ CREATE TABLE IF NOT EXISTS speaking_orders (
653
+ room_id TEXT PRIMARY KEY REFERENCES rooms(id) ON DELETE CASCADE,
654
+ member_ids TEXT NOT NULL
655
+ );
631
656
  -- Chains whose automatic progress stopped (budget reached or every dispatched member failed).
632
657
  CREATE TABLE IF NOT EXISTS free_chains (
633
658
  room_id TEXT NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,