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
+53
View File
@@ -32,6 +32,7 @@ AGENTS = "agents"
IMAGES = "images"
SCHEDULES = "schedules"
SUBAGENTS = "subagents"
CROWD = "crowd"
BRANDING = "branding"
EXTRACTION = "extraction"
@@ -343,6 +344,41 @@ def _schedules_defaults() -> dict[str, Any]:
}
def _crowd_defaults() -> dict[str, Any]:
"""Several models answering one turn, in order, then again in reverse.
Off until an administrator turns it on, and the reason is arithmetic: one
turn costs **models x rounds x 2 - 1** replies, so four models over two
rounds is fifteen. On a single local endpoint every change of speaker is also
a model load, because llama-swap holds one at a time.
The owner's own warning, recorded because it is the failure this feature
actually has: *larger crowds of smaller models -- and sometimes of bigger
ones -- start cycling, or never stop.* So the numbers below are a ceiling
reached by ordinary work, not a runaway backstop, which is the opposite of
how `subagents.max_rounds` is set and is deliberate: a round of a crowd is a
visible, expensive thing somebody is waiting through.
"""
return {
"enabled": False,
# Besides the chat's own model. Four speakers is already eight replies a
# turn at one round each.
"max_models": 4,
# One round is out-and-back: everyone answers, then everyone is asked
# whether they disagree, ending at the main model. Two is one chance to
# change its mind after hearing the objections, which is the whole point;
# three is where cycling starts.
"max_rounds": 2,
# The whole turn, across every speaker, so a member whose endpoint has
# stalled cannot hold a round open all afternoon.
"wall_seconds": 900,
# Whether a short "I agree" on the way back is collapsed in the
# transcript. On by default: N-1 bubbles saying nothing is what makes
# somebody switch the feature off, and the disagreements are the point.
"collapse_agreement": True,
}
def _subagents_defaults() -> dict[str, Any]:
"""Delegating a piece of a reply to a second, unattended model.
@@ -389,6 +425,7 @@ _DEFAULTS: dict[str, Any] = {
IMAGES: _images_defaults,
SCHEDULES: _schedules_defaults,
SUBAGENTS: _subagents_defaults,
CROWD: _crowd_defaults,
# Whose instance this is. The defaults live in `services/branding.py`
# beside the code that reads them, because every one of them is paired with
# a label and a hint for the admin page and splitting the three across two
@@ -668,6 +705,22 @@ def subagents(db: DBSession) -> dict[str, Any]:
return values
def crowd(db: DBSession) -> dict[str, Any]:
"""Crowd settings, clamped on read for the reason `agents` gives.
Every bound has a floor of one: a `max_models` of zero is the feature
switched off wearing the switch's clothes, and that is a thing to answer in
one place rather than two.
"""
values = get_group(db, CROWD)
values["max_models"] = min(max(int(values.get("max_models") or 1), 1), 8)
values["max_rounds"] = min(max(int(values.get("max_rounds") or 1), 1), 5)
values["wall_seconds"] = min(max(int(values.get("wall_seconds") or 1), 60), 7200)
values["enabled"] = bool(values.get("enabled"))
values["collapse_agreement"] = bool(values.get("collapse_agreement"))
return values
def images_ready(db: DBSession) -> bool:
"""Whether image generation can actually happen.