Users, groups, permissions, model settings and reasoning display

Four features, plus the schema machinery they needed.

**Schema sync.** The first live instance had data in it, and create_all
only creates missing *tables* -- a new column silently never appeared.
db/migrations.py now diffs the declared models against the database and
ALTER TABLE ... ADD COLUMN for what is missing, deriving a backfill
default from the column type (SQLite refuses a NOT NULL column without
one, and a Python-side `default=dict` cannot be expressed in DDL).
Verified against a copy of the live database: eight changes applied, all
rows preserved, second run a no-op. Renames, drops and retypes are still
manual and say so.

**Permissions.** A flat set of named booleans: an instance baseline
widened by each group the user belongs to. A group grants and never
denies -- with denies, "why can this user not do X" cannot be answered
without simulating every group. Admins bypass entirely, because an admin
can grant it back to themselves in two clicks and pretending otherwise
is theatre. Model *access* is separate: public, or granted to groups.
The picker is not the boundary -- switching a chat to a model you cannot
reach is a 403.

**Model settings.** Ordering, pinned-first, an instance default and a
per-user default, display names, descriptions, capability flags, and
uploaded images. Images are stored and served locally rather than by
URL: a remote URL makes every page render a request to a third party.
Uploads are validated by magic number, not the declared content type,
and stored under a random name. Models with no image get a generated
initial whose hue is derived from the model id, so it is stable.

**Reasoning display.** Streams into its own collapsible block above the
answer, labelled "Thought for 14 seconds", collapsed once finished, and
never replayed as context on the next turn. Two sources: the
reasoning_content delta field, and <think> tags inline in content -- the
latter needs a streaming splitter because the tags arrive split across
chunks. Models emitting no reasoning show nothing, via a :has() rule
rather than JavaScript. Verified against qwen35-9b on llama-swap: 694
reasoning events, 52 answer tokens, cleanly separated.

Two bugs found and fixed while testing:

- A bare `Mapped[list]` relationship is treated by SQLAlchemy as a scalar
  and returns None instead of []. It needs the element type.
- FastAPI substitutes the default for an empty form value, so with
  `x: str | None = Form(None)` a submitted `x=` is indistinguishable from
  an absent field. That silently broke clearing a system prompt or a
  temperature. update_chat now reads the raw form and checks key presence.

143 tests, ruff clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaroslav Beneš
2026-07-21 11:49:32 +02:00
parent 9179461bfe
commit 1d3f6c450b
36 changed files with 2834 additions and 134 deletions
+161
View File
@@ -0,0 +1,161 @@
{% extends "admin/_layout.html" %}
{% from "_macros.html" import icon %}
{% set section = "users" %}
{% block title %}Users - LLeMbas{% endblock %}
{% block heading %}Users{% endblock %}
{% block admin_content %}
<p class="admin-lede">
Everyone with an account on this instance. Administrators bypass every
permission; ordinary users get the baseline permissions plus whatever their
groups add.
</p>
{% if saved %}
<div class="alert alert--success">{{ icon("check", "icon--sm") }} <span>{{ saved }}</span></div>
{% endif %}
<form method="get" action="/admin/users" class="row" style="margin-bottom: var(--sp-5)">
<input class="input" type="search" name="q" value="{{ q }}"
placeholder="Search by name or email" aria-label="Search users">
<button class="btn" type="submit">{{ icon("search", "icon--sm") }} Search</button>
{% if q %}<a class="btn btn--ghost" href="/admin/users">Clear</a>{% endif %}
</form>
<details class="card">
<summary class="card__title" style="cursor: pointer">Add a user</summary>
<form method="post" action="/admin/users" style="margin-top: var(--sp-4)">
<div class="field">
<label class="field__label" for="nu-name">Name</label>
<input class="input" id="nu-name" name="name" required>
</div>
<div class="field">
<label class="field__label" for="nu-email">Email</label>
<input class="input" id="nu-email" name="email" type="email" required>
</div>
<div class="field">
<label class="field__label" for="nu-password">Password</label>
<input class="input" id="nu-password" name="password" type="password"
required minlength="8" autocomplete="new-password">
<p class="field__hint">
At least 8 characters. Tell them to change it — you will know it otherwise.
</p>
</div>
<div class="field">
<label class="field__label" for="nu-role">Role</label>
<select class="select" id="nu-role" name="role">
{% for role in roles %}
<option value="{{ role }}" {{ 'selected' if role == 'user' }}>{{ role }}</option>
{% endfor %}
</select>
</div>
<button class="btn btn--primary" type="submit">{{ icon("plus", "icon--sm") }} Create</button>
</form>
</details>
<h2 class="admin-section-title">
Accounts <span class="badge">{{ users|length }}</span>
</h2>
{% for account in users %}
<section class="card">
<form method="post" action="/admin/users/{{ account.id }}">
<div class="row row--between" style="margin-bottom: var(--sp-4); flex-wrap: wrap">
<div class="row" style="gap: var(--sp-2); min-width: 0">
<span class="status-dot {{ 'is-ok' if account.active else 'is-off' }}"></span>
<strong class="truncate">{{ account.name }}</strong>
<code class="text-xs faint">{{ account.email }}</code>
{% if account.is_admin %}<span class="badge badge--gold">admin</span>{% endif %}
{% if not account.active %}<span class="badge badge--danger">deactivated</span>{% endif %}
{% if account.id == user.id %}<span class="badge">you</span>{% endif %}
</div>
<span class="text-xs faint">
{% if account.last_login_at %}
last seen {{ account.last_login_at.strftime("%Y-%m-%d %H:%M") }}
{% else %}
never signed in
{% endif %}
</span>
</div>
<div class="field">
<label class="field__label" for="un-{{ account.id }}">Name</label>
<input class="input" id="un-{{ account.id }}" name="name" value="{{ account.name }}" required>
</div>
<div class="field">
<label class="field__label" for="ur-{{ account.id }}">Role</label>
<select class="select" id="ur-{{ account.id }}" name="role">
{% for role in roles %}
<option value="{{ role }}" {{ 'selected' if role == account.role }}>{{ role }}</option>
{% endfor %}
</select>
<p class="field__hint">
<strong>admin</strong> can do everything, including this page.
<strong>user</strong> is an ordinary account.
<strong>pending</strong> cannot sign in until promoted.
</p>
</div>
<div class="field">
<label class="checkbox">
<input type="checkbox" name="active" value="true" {{ 'checked' if account.active }}>
<span>Active — may sign in</span>
</label>
<p class="field__hint">
Deactivating signs them out everywhere immediately, rather than waiting
for their session to expire.
</p>
</div>
{% if groups %}
<div class="field">
<span class="field__label">Groups</span>
<div class="checkbox-row">
{% for group in groups %}
<label class="checkbox">
<input type="checkbox" name="group_ids" value="{{ group.id }}"
{{ 'checked' if group in account.groups }}>
<span>{{ group.name }}</span>
</label>
{% endfor %}
</div>
</div>
{% endif %}
{% if account.is_admin and admin_count <= 1 %}
<div class="alert alert--warning">
{{ icon("warning", "alert__icon") }}
<span>
The only administrator. Promote someone else before demoting or
deactivating this account — an instance with no admin can only be
recovered with <code>lembas create-admin</code>.
</span>
</div>
{% endif %}
<button class="btn btn--primary" type="submit">Save</button>
</form>
<div class="connection__footer">
<form method="post" action="/admin/users/{{ account.id }}/password" class="row"
style="gap: var(--sp-2)">
<input class="input" type="password" name="password" minlength="8"
placeholder="Set a new password" autocomplete="new-password" required
aria-label="New password for {{ account.email }}">
<button class="btn btn--sm" type="submit">{{ icon("key", "icon--sm") }} Reset</button>
</form>
{% if account.id != user.id %}
<form method="post" action="/admin/users/{{ account.id }}/delete"
onsubmit="return confirm('Delete {{ account.email }} and all their chats? This cannot be undone.')">
<button class="btn btn--sm btn--danger" type="submit">
{{ icon("trash", "icon--sm") }} Delete
</button>
</form>
{% endif %}
</div>
</section>
{% endfor %}
{% endblock %}