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.
This commit is contained in:
Teknium
2026-11-06 11:38:35 -08:00
parent dc29ecd4c9
commit caab69a83a
5 changed files with 41 additions and 4 deletions

View File

@@ -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()

View File

@@ -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<number, CanonicalPausedDiscussion>()
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 })
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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"}