feat(base,learn,todo): the operator's assistants, and the instance's privacy page (ADR 0021, step 2)

The server half of the assistants (points 4 to 8, D27 to D46): the
operator sets language models in deploy/assistants.env, grants them to
people with limits, and the module forwards their questions with the
operator's key, counting what each used, never what was asked.

- assistantsFrom: ASSISTANTS, ASSISTANT_ID_KEY, ASSISTANT_<ID>_NAME,
  _PROTOCOL (anthropic, openai, openai-responses), _BASE_URL, _MODEL,
  _KEY, _IMAGES, _PRICE_INPUT and _PRICE_OUTPUT (per million tokens),
  ASSISTANT_CURRENCY, ASSISTANT_OWN_KEYS; anything unusable stops the
  server as it starts, naming the variable.
- The assistant endpoint (identified): offered, agree (D40), ask as a
  Serverpod stream (D35): the text as the model writes it, then the
  whole turn, sent once the request is counted; figures, the caller's
  own. Checked in order: at most 2 questions on their way per person
  (tooManyAtOnce), the grant, the agreement, limits in the pricing's
  unit now (limitOutdated), the month's and the day's limits (with when
  they reset, UTC), the question (malformed, imagesNotRead, tooLarge
  past 400,000 characters, an image over 3,932,160 bytes or more than
  20 images); output capped at 16,384 tokens. Each request carries the
  person's opaque ID (HMAC-SHA256 under ASSISTANT_ID_KEY, D30). Once
  sent, the provider's reply is read to its end even if the app
  cancels, and counted on a session of its own; a provider's failure is
  logged by kind and status, told as providerKey, providerBusy (with
  its wait), providerRefused or providerFailed.
- The assistantOperator endpoint (identified and operator, on the
  register): setUp, grants, grant (assistantNotSet, invalidLimit,
  notFound), revoke, figures of a UTC month.
- Tables assistant_grant and assistant_figures (per person, assistant
  and UTC day), in the module's, Learn's and Todo's migrations. The
  daily pruning keeps this month and the two before
  (forgetOldAssistantFigures, D41); deletePerson deletes both.
  A member offers no assistant (D43).
- instance.basics() gains assistantOwnKeys.
- /privacy (D39, addPrivacyPageRoute, on the register's web server,
  before the profile pages' route): made from the instance's settings,
  English or German, no JavaScript: who may sign up, where the data and
  backups are, what the module keeps (the account, sessions, IP
  addresses, the server's log), the host's privacyPagePoints (Learn's:
  studysets synced, private ones encrypted on the devices with what
  stays readable, what is published, dictionary lookups, the learner
  count), each assistant with its model and host, what goes and what is
  counted, and whether personal keys are allowed. Never a key.

Tests: 70 on the assistants (grants, limits, refusals in order,
streaming, cancelling, counting, concurrency, the operator's denials,
deletion, pruning), 19 on the settings, 8 on the privacy page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-25 18:07:01 +01:00
commit dbae802915
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -0,0 +1,135 @@
import 'package:halkyon_base_server/base.dart';
import 'package:serverpod/serverpod.dart';
import 'package:test/test.dart';
import '../support/fixtures.dart';
import '../test_tools/serverpod_test_tools.dart';
// The instance's `/privacy` (Halkyon Learn's ADR 0021, D27 and D39): made
// from its settings, in English or German, readable before signing up: who
// may sign up, where the data and backups are, what is kept, the host's own
// words of its app, each assistant with what goes where, and personal keys.
final _claude = {
'ASSISTANTS': 'claude',
'ASSISTANT_ID_KEY': 'k' * assistantIdKeyMinLength,
'ASSISTANT_CLAUDE_NAME': 'Claude Sonnet',
'ASSISTANT_CLAUDE_PROTOCOL': 'anthropic',
'ASSISTANT_CLAUDE_MODEL': 'claude-sonnet-5-5',
'ASSISTANT_CLAUDE_KEY': 'sk-ant-secret',
'ASSISTANT_CLAUDE_IMAGES': 'on',
};
void main() {
withServerpod('The privacy page', (sessionBuilder, endpoints) {
late Serverpod pod;
setUp(() {
useBase(
sessionBuilder,
privacyPagePoints: [
(
english: 'Your studysets are kept to sync them.',
german: 'Deine Lernsets werden zum Abgleich aufbewahrt.',
),
],
);
pod = sessionBuilder.build().serverpod;
});
tearDown(() {
useSignUpMode(pod, null);
useAssistants(pod, null);
});
String page({
String? acceptLanguage,
({String? data, String? backups}) locations = (data: null, backups: null),
}) =>
privacyPage(pod, acceptLanguage: acceptLanguage, locations: locations);
test('says who may sign up, as the operator set it', () {
useSignUpMode(pod, SignUpMode.open);
expect(page(), contains('Anyone may make an account here.'));
useSignUpMode(pod, SignUpMode.approval);
expect(page(), contains('An operator approves each new account'));
useSignUpMode(pod, SignUpMode.closed);
expect(page(), contains('Nobody can make an account here now.'));
});
test('says where the data and the backups are, or that the operator '
"doesn't say", () {
expect(
page(locations: (data: 'DE', backups: 'EU')),
allOf(contains('<b>DE</b>'), contains('<b>EU</b>')),
);
expect(
page(locations: (data: 'DE', backups: noBackups)),
contains('keeps no backups'),
);
expect(page(), contains("The operator doesn't say where"));
});
test('says what the instance keeps of a person, and the host its own '
"words of its app's data", () {
final text = page();
expect(text, contains('never your address'));
expect(text, contains('no IP address'));
expect(text, contains('Your studysets are kept to sync them.'));
});
test('names each assistant with its provider and model, what goes, and '
'what is counted, never its key (D27)', () {
useAssistants(pod, assistantsFrom(_claude.get));
final text = page();
expect(text, contains('Claude Sonnet'));
expect(text, contains('claude-sonnet-5-5'));
expect(text, contains('api.anthropic.com'));
expect(text, contains('reads images'));
expect(text, contains('keeps none of it'));
expect(text, contains('never what was asked'));
expect(text, isNot(contains('sk-ant-secret')));
expect(text, contains('Assistants with your own key are not offered'));
});
test('says when there is no assistant, and when personal keys are '
'allowed (D5, D6)', () {
expect(page(), contains('This instance offers no assistant.'));
useAssistants(pod, assistantsFrom({'ASSISTANT_OWN_KEYS': 'on'}.get));
expect(page(), contains('you may add assistants with your own key'));
});
test('in German, where the visitor reads German first', () {
useAssistants(pod, assistantsFrom(_claude.get));
final text = page(acceptLanguage: 'de-DE,de;q=0.9,en;q=0.8');
expect(text, contains('<html lang="de">'));
expect(text, contains('Datenschutz'));
expect(text, contains('Deine Lernsets werden zum Abgleich'));
expect(text, contains('Claude Sonnet'));
expect(page(), contains('<html lang="en">'));
});
test("escapes what the operator's settings hold", () {
useAssistants(
pod,
assistantsFrom(
{..._claude, 'ASSISTANT_CLAUDE_NAME': '<script>x</script>'}.get,
),
);
expect(page(), isNot(contains('<script>')));
});
test('on a member, names no assistant (D43)', () {
useBase(
sessionBuilder,
app: 'todo',
accounts: MemberOfRegister(Uri.parse('http://learn.test:8082')),
);
useAssistants(pod, assistantsFrom(_claude.get));
expect(page(), contains('This instance offers no assistant.'));
});
});
}
extension on Map<String, String> {
String? get(String name) => this[name];
}