Models that know about each other, and have a self

Three features sharing one idea: a model here started from nothing every
conversation and had no notion that anything else existed.

THE ROSTER. `chat.roster_block` builds one line per model this *person* can
reach -- through `permissions.models_visible_to`, never the table -- and
`{{model_roster}}` carries it, gated on the `friend` family for the reason the
memories block is gated on `memory`: a list of peers a model cannot talk to is
context spent on nothing, and one checkbox is then the whole switch. New
`Model.notes` column, a column and not a `capabilities_json` key for the reason
`context_length` and `reasoning_efforts` both carry.

ASKING A FRIEND. A second entry point in `services/subagent.py` rather than a
second module, so one place still owns the bounds and the lifecycle. `_create_
child` takes the friend's (model_id, connection_id) *pair*, because Model is
unique on both and an id alone does not say which endpoint. Three things differ
from a helper: the effort is the friend's own default and never the parent's (the
1.3.0 bug by another door -- the vocabularies differ and a level a model does not
take raises inside its chat template), the chat is ordinary even when the asker's
is an agent chat, and `scope_json["role"]` marks it so `core.friend` speaks
instead of `core.subagent`. `friend` joins the unattended withdrawal set: a
friend that could ask a friend is the same unbounded fan-out in politer clothes.
Budget, concurrency and quota are shared with helpers, so one reply cannot spend
the allowance twice.

PERSONALITY. One table, two roles, `owner_id IS NULL` the discriminator: the
model's own persona, and its read of one person. Keyed on the model's *text* id
with no foreign key, because "Test & refresh" deletes a model the endpoint has
stopped listing and a personality must not be collateral. `PersonaRevision`
copies SkillRevision, and so does the argument: the safety story for a model
rewriting itself is a record and a way back, not a gate. The reflection is shown
to the person it is about, in their own settings, which is the whole of why
keeping one is acceptable. `persona` is withdrawn from any unattended chat --
a helper's task, a friend's question and a schedule's instruction are all words
nobody watched being written.

Two bugs found while reading for this, both silent:

`review_model_id` stored a `Model` primary key, so a refresh taken while an
endpoint was not listing that model unset the administrator's choice -- and
`_reviewer` then fell back to the chat's own model, so pictures were judged by
a model nobody chose. Now the text id, with the primary key still accepted.

`_messages_after` used a bare `>` on `created_at`, so a row sharing the edited
turn's microsecond survived a rewind -- and `_send` writes a user turn and its
placeholder back to back, which is exactly that tie. Deliberately NOT
`thread_tail`'s `(created_at, id)` tiebreak: ids are random UUIDs, so that
settles a tie by coin toss. A tie now reads as "later", which is the safe
direction for an operation whose purpose is to discard what follows.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-26 02:04:55 +00:00
co-authored by Claude Opus 5
parent 54fee49810
commit df52ec9d96
30 changed files with 2710 additions and 32 deletions
+3
View File
@@ -62,6 +62,7 @@ from lembas.db.models.library import (
SkillRevision,
chat_knowledge_bases,
)
from lembas.db.models.persona import Persona, PersonaRevision
from lembas.db.models.report import (
SOURCE_CHAT,
SOURCE_MANUAL,
@@ -178,6 +179,8 @@ __all__ = [
"KnowledgeBase",
"McpServer",
"Memory",
"Persona",
"PersonaRevision",
"Message",
"Model",
"Note",
+10
View File
@@ -101,6 +101,16 @@ class Model(UUIDPrimaryKey, Timestamps, Base):
model_id: Mapped[str] = mapped_column(String(300), nullable=False)
display_name: Mapped[str] = mapped_column(String(300), default="")
description: Mapped[str] = mapped_column(Text, default="")
# What the *other* models are told about this one, when the roster is in
# front of them. Separate from `description`, which is written for people
# and reads like marketing; this is meant to be facts -- parameters,
# quantisation, a benchmark figure, what it is bad at.
#
# A column and not a key in `capabilities_json`, for the reason
# `context_length` and `reasoning_efforts` both carry: that dict is rebuilt
# wholesale from the submitted checkboxes on every save.
notes: Mapped[str] = mapped_column(Text, default="")
enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
# Sort order in every picker. Ties fall back to model_id so the order is
+96
View File
@@ -0,0 +1,96 @@
"""Who a model is, and what it has made of the person it is talking to.
Two different things, one table, and the discriminator is a column:
* ``owner_id IS NULL`` -- the model's **persona**. Instance-wide, seeded by an
administrator, and rewritten by the model itself when it is allowed to.
* ``owner_id`` set -- that model's **read of that person**, kept as it goes.
Per (model, person) rather than per model, because two models may honestly
arrive at different views of the same somebody, and on an instance with more
than one account nobody should inherit another person's reflection.
Why not a fourth prompt layer: because *"system prompts replace, never stack"*
is a decision this project has already taken. Both of these reach the model as
``{{persona}}`` and ``{{person_view}}``, through ordinary fragments, exactly the
way the memories block does.
⚠ **``model_key`` is the model's text id, not the ``Model`` row's primary key**,
and there is deliberately no foreign key to ``models``. "Test & refresh" on the
connection screen deletes any model the endpoint no longer lists and recreates
it when it comes back -- so a row keyed on the primary key would lose a model's
whole personality to a refresh taken while its endpoint happened to be loading
something else. This is the reasoning ``Chat.model_id`` already carries: the
text id survives, and a row naming a model that no longer exists is invisible
rather than broken.
"""
from __future__ import annotations
from sqlalchemy import Boolean, ForeignKey, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from lembas.db.base import Base, Timestamps, UUIDPrimaryKey
from lembas.db.models.library import AUTHOR_MODEL, AUTHOR_USER
class Persona(UUIDPrimaryKey, Timestamps, Base):
"""One model's personality, or one model's read of one person."""
__tablename__ = "personas"
__table_args__ = (UniqueConstraint("model_key", "owner_id"),)
# The model's `model_id`, not a `models.id`. See the module docstring.
model_key: Mapped[str] = mapped_column(String(300), nullable=False, index=True)
# NULL means "this is the model's own persona". Set means "this is what that
# model makes of this person".
owner_id: Mapped[str | None] = mapped_column(
String(32), ForeignKey("users.id", ondelete="CASCADE"), nullable=True, index=True
)
content: Mapped[str] = mapped_column(Text, default="")
# Who wrote what is in `content` now. A person reading their own reflection
# is entitled to know which of the two put each version there.
author: Mapped[str] = mapped_column(String(16), default=AUTHOR_MODEL, nullable=False)
# Switched off rather than deleted, so turning it off does not throw the text
# away and turning it back on does not need it retyped.
enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
revisions: Mapped[list[PersonaRevision]] = relationship(
back_populates="persona",
cascade="all, delete-orphan",
order_by="PersonaRevision.created_at.desc()",
)
@property
def is_reflection(self) -> bool:
return self.owner_id is not None
def __repr__(self) -> str:
kind = "reflection" if self.is_reflection else "persona"
return f"<Persona {kind} {self.model_key} {self.content[:30]!r}>"
class PersonaRevision(UUIDPrimaryKey, Timestamps, Base):
"""The state of a persona before a change.
The same safety story as `SkillRevision`, for the same reason and with the
same limit stated plainly: a model that has just read a hostile page can
rewrite its own personality, and what stops that being permanent is a record
and a way back rather than a gate.
"""
__tablename__ = "persona_revisions"
persona_id: Mapped[str] = mapped_column(
String(32), ForeignKey("personas.id", ondelete="CASCADE"), nullable=False, index=True
)
content: Mapped[str] = mapped_column(Text, default="")
# Who made the change this revision is the "before" of.
author: Mapped[str] = mapped_column(String(16), default=AUTHOR_USER, nullable=False)
note: Mapped[str] = mapped_column(String(200), default="")
persona: Mapped[Persona] = relationship(back_populates="revisions")
__all__ = ["AUTHOR_MODEL", "AUTHOR_USER", "Persona", "PersonaRevision"]