From caab69a83a4e7699725c38bcc35b443e5f3138bd Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:38:35 -0800 Subject: [PATCH] fix(groups): a replayed overtaken Continue never strands the newer message, and Desktop hides it Re-review of 4f84a6dbda3: the router now refuses a Continue for a discussion a newer message on its thread overtook, but a log written by an older build can already hold one. Replaying it (and the schema-5 rebuild replays every room) re-pointed the thread at the old discussion; the newer one's settle no longer matched, so the row leaked and the room re-planned it forever. _apply_continued now skips a Continue that a newer message on the thread preceded, the same fence the router applies before writing one. Desktop's pause rows used the old fence (only messages after the cap notice), so that case still drew a Continue button the gateway answers with "not paused". The row now retires on any newer user message in the thread after the discussion's own message. --- .../canonical-group-routed.test.tsx | 7 +++++++ .../hermes-bots/canonical-group-routed.tsx | 8 ++++++-- gateway/hosted_room_policy_checkpoint.py | 6 ++++++ gateway/hosted_room_router.py | 4 ++-- .../test_hosted_room_policy_settle_race.py | 20 +++++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.test.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.test.tsx index 9a91bab358..ac70e7fc97 100644 --- a/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.test.tsx +++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.test.tsx @@ -129,6 +129,13 @@ it('a Stop that landed before a late cap notice still retires the pause', () => expect(screen.queryByTestId('room-continue')).toBeNull() }) +it('a newer message in the thread that landed before a late cap notice retires the pause', () => { + const source = { ...userMsg(1), event_id: D } + renderHistory([source, memberMsg(2), memberMsg(3), userMsg(4), bounded(5)], { maxContinues: 5, onContinue: vi.fn(), onStop: vi.fn() }) + expect(screen.getByTestId('room-paused')).toBeTruthy() + expect(screen.queryByTestId('room-continue')).toBeNull() +}) + it('an unrelated thread\'s user message does not retire the pause', () => { renderHistory([...pausedLog, userMsg(6, 'thread-2')], { maxContinues: 5, onContinue: vi.fn(), onStop: vi.fn() }) expect(screen.getByTestId('room-continue')).toBeTruthy() diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.tsx index 807174e57e..6630c4b60f 100644 --- a/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.tsx +++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-routed.tsx @@ -108,16 +108,20 @@ export function pausedDiscussions(events: readonly CanonicalGroupEvent[], maxCon const ordered = [...events].sort((a, b) => a.seq - b.seq) const { counts, grants } = pauseCounts(ordered) const moved = movedOnPauses(ordered) - // A Stop after the discussion's own message fences it for good, even one older than the cap notice. + // A Stop, or a newer user message in its thread, after the discussion's own message fences it for + // good, even one older than the cap notice (the gateway refuses that Continue). const lastStop = ordered.reduce((seq, event) => event.kind === 'room.stop_requested' ? event.seq : seq, 0) const sourceSeq = new Map(ordered.flatMap(event => event.kind === 'message.user' && event.event_id ? [[event.event_id, event.seq] as const] : [])) + const lastUserInThread = new Map(ordered.flatMap(event => event.kind === 'message.user' && typeof event.payload.thread_id === 'string' ? [[event.payload.thread_id, event.seq] as const] : [])) const result = new Map() for (const event of ordered) { if (!isBoundedActivity(event)) {continue} const discussion = discussionOf(event) - const fenced = lastStop > (sourceSeq.get(discussion) ?? event.seq) + const source = sourceSeq.get(discussion) ?? event.seq + const thread = typeof event.payload.thread_id === 'string' ? event.payload.thread_id : '' + const fenced = lastStop > source || (!!thread && (lastUserInThread.get(thread) ?? 0) > source) result.set(event.seq, { count: counts.get(event.seq) ?? 0, actionable: !!discussion && !fenced && !moved.has(event.seq) && (grants.get(discussion) ?? 0) < maxContinues }) } diff --git a/gateway/hosted_room_policy_checkpoint.py b/gateway/hosted_room_policy_checkpoint.py index 850bfc04ec..824a74ef44 100644 --- a/gateway/hosted_room_policy_checkpoint.py +++ b/gateway/hosted_room_policy_checkpoint.py @@ -272,6 +272,12 @@ class HostedRoomPolicyCheckpoint: return source_event = _event_from_room_row(source) thread_id = _text(source_event["payload"], "thread_id") + # A Continue written by an older build after a newer message on the thread (the router now + # refuses those) must not hand the thread back: the newer discussion owns it. + if conn.execute("""SELECT 1 FROM hosted_room_events WHERE room_id=? AND seq>? AND seq<=? + AND kind='message.user' AND json_extract(payload_json, '$.thread_id')=? LIMIT 1""", + (room_id, int(source_event["seq"]), int(event["seq"]), thread_id)).fetchone(): + return conn.execute("""INSERT INTO hosted_room_policy_threads( room_id, thread_id, discussion_event_id, latest_user_seq, completed ) VALUES (?, ?, ?, ?, 0) diff --git a/gateway/hosted_room_router.py b/gateway/hosted_room_router.py index d301252c19..b6006194e5 100644 --- a/gateway/hosted_room_router.py +++ b/gateway/hosted_room_router.py @@ -117,8 +117,8 @@ def validate_continued_event(kind: str, payload: Mapping[str, Any], actor: Mappi def paused_discussion(events: Sequence[Mapping[str, Any]], discussion_event_id: str) -> dict[str, Any] | None: """``{"thread_id", "grant"}`` when the discussion's latest outcome is the cap (``room.activity`` - ``bounded`` after its last Continue), no Stop followed its message and no newer message in its thread - followed the cap; + ``bounded`` after its last Continue) and no Stop and no newer message in its thread followed its + message; else None. ``grant`` is the number the next Continue takes (1-based).""" bounded = None grants = 0 diff --git a/tests/gateway/test_hosted_room_policy_settle_race.py b/tests/gateway/test_hosted_room_policy_settle_race.py index 0040b01b31..2b847a1684 100644 --- a/tests/gateway/test_hosted_room_policy_settle_race.py +++ b/tests/gateway/test_hosted_room_policy_settle_race.py @@ -42,3 +42,23 @@ def test_a_cap_notice_landing_after_a_newer_message_cannot_be_continued(tmp_path "discussion_event_id": "first"}, "gateway") events = rooms.read_events(db, room_id="room")["events"] assert paused_discussion(events, "first") is None + + +def test_a_continue_an_older_build_wrote_after_a_newer_message_does_not_strand_it(tmp_path): + """Logs from before the router fence can hold that Continue; replaying it must not re-point the thread.""" + db = tmp_path / "shared-state.db" + rooms.create_room(db, room_id="room", name="Room", authority_gateway_id="home", + members=[{"member_id": "reviewer", "profile": "reviewer", "handle": "reviewer"}]) + _append(db, "first", "message.user", {"text": "@reviewer one", "thread_id": "thread"}) + _append(db, "second", "message.user", {"text": "@reviewer two", "thread_id": "thread"}) + _append(db, "dactivity:first:max_messages", "room.activity", { + "status": "bounded", "reason_code": "max_messages", "thread_id": "thread", + "discussion_event_id": "first"}, "gateway") + _append(db, "dcontinue:first:1", "room.continued", + {"discussion_event_id": "first", "thread_id": "thread", "grant": 1}, "gateway") + _append(db, "dactivity:second:silent_round", "room.activity", { + "status": "settled", "reason_code": "silent_round", "thread_id": "thread", + "discussion_event_id": "second"}, "gateway") + other = _append(db, "third", "message.user", {"text": "@reviewer other", "thread_id": "other"}) + snapshot = HostedRoomPolicyCheckpoint(db).snapshot(room_id="room", latest_seq=int(other["seq"])) + assert {(e.get("payload") or {}).get("thread_id") for e in snapshot.events} == {"other"}