A crowd in one chat

The chat's own model answers, then each other member in order, then the order runs
backwards asking each whether it disagrees, ending at the main model, which either
closes or sends them round again. Design and reasoning: LLeMbas.wiki/Crowd-chats.

THE SPEAKER SEAM, WHICH IS ALSO A BUG FIX

`chat_service.speaker_for` makes the *message* name the answering model and the
chat only the default. That closes a live half-wired feature -- `wake_chat` takes a
model override and `schedule/runner` passes one, and it reached the row and never
the request, so a schedule naming another model got the chat's model wearing the
other one's name.

The seam is wider than `build_request`: `{{model_name}}`, the authored prompt's
model layer, `vision` (where a wrong answer makes the endpoint reject the whole
request), the effort vocabulary (which raises inside the model's own chat template,
and whose refusal narrows every Model row sharing the id), `resolve_tools`,
`context_length` -> `_too_big`, and `ToolContext.model_id`. `resolve_endpoint` may
now only write back `chat.connection_id` when the speaker *is* the chat's model.

WHY N CHAINED REPLIES

`Generation` is one reply's state and `_follow` streams per message, so one
generation cannot stream into nine bubbles and `ensure` would not know which of the
nine it was after a restart. A subagent per speaker cannot work either: its answer
comes back as a tool result and tool results are never replayed, so speaker 3 could
not see speaker 2 -- which is the whole point. Chained, exactly one incomplete row
exists at a time, and `tests/test_crowd_chain.py` asserts that at every
observation.

The round lives on `Message.crowd_json`, not on the chat: the row is the authority,
and chat-level state would describe turns a rewind or a restart had removed.
`crowd.next_turn` is pure, so all eight refusals are tested with no endpoint.

THREE RULES, EACH A BUG WRITTEN THE OTHER WAY ROUND

- `if not _advance_crowd(g): _drain(g)` -- advancing must *suppress* draining, or a
  queued human turn puts a second incomplete row beside the next speaker's.
- `_advance_crowd` refuses unless the finishing row is the newest, or regenerating
  member 2 creates a second member 3 and two chains race down one turn.
- an error skips one speaker and two in a row end the round: the usual failure is a
  small member's window overflowing, and `_drain`'s stop-on-error would kill every
  crowd at whichever member is smallest.

Each other speaker's turn is relabelled as attributed user content, which is both
how a model can disagree with words it did not write and how the history keeps
alternating. The per-speaker instruction is payload-only -- as a row it could be
dropped from the request by a `created_at` tie, and every later speaker would answer
it. Compaction, titling and the notification are gated to once per turn; `_inject`
is off during a round; the way back gets no tools and a member is treated as
unattended.

Membership stores the model as text with no foreign key: "Test & refresh" deletes
and recreates Model rows, and a cascade would empty the crowd out of every chat.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-26 13:38:52 +00:00
co-authored by Claude Opus 5
parent ac51dd46cc
commit da0797ccad
27 changed files with 3018 additions and 59 deletions
+20 -2
View File
@@ -637,6 +637,15 @@ def test_editing_rewinds_and_discards_later_messages(
first_user = db.scalars(
select(Message).where(Message.role == "user").order_by(Message.created_at)
).first()
# The ids as strings, taken now: these rows are about to be deleted, and an
# ORM instance read afterwards raises ObjectDeletedError.
discarded_ids = set(
db.scalars(
select(Message.id).where(
Message.chat_id == chat_id, Message.role == "assistant"
)
)
)
client.post(
f"/api/chats/{chat_id}/messages/{first_user.id}/edit",
data={"content": "first, revised"},
@@ -648,8 +657,17 @@ def test_editing_rewinds_and_discards_later_messages(
remaining = db.scalars(select(Message).order_by(Message.created_at)).all()
assert [m.role for m in remaining] == ["user", "assistant"]
assert remaining[0].content == "first, revised"
# The fresh assistant row is incomplete, which is what restarts the stream.
assert remaining[1].complete is False
# A *fresh* assistant row, which is what restarts the stream: a different row
# from the one that was discarded, with nothing written into it yet.
#
# ⚠ Deliberately not `complete is False`. A real generation is started here
# against the fixture's unreachable endpoint, and it does finish -- it errors
# with "could not reach" and `_persist` marks the row complete. Whether that
# has happened by the time this line runs is a race, and asserting on it made
# this test pass only while that failure stayed slower than the rest of the
# request. It began flaking the moment unrelated work shifted the timing.
assert remaining[1].id not in discarded_ids
assert remaining[1].content == ""
def test_a_rewind_takes_a_message_written_in_the_same_microsecond(