feat(modules): event triggers, audiences and the ceiling lattice (engagement Phase 2)
The contract half of the engagement system: a module (and core) can DECLARE an
event with a payload contract and fire it. Nothing delivers yet — `emit`
validates, logs and stops, and Phase 4 replaces that log line with the engine.
`api.registerEventTriggers` and `api.registerAudiences` ride the existing
stage()/apply() validate-then-commit discipline, so a registrant that throws
halfway leaves nothing behind. `ctx.events.emit` is fire-and-forget and binds
the owner from the calling module — a module fires its own triggers and no one
else's. `ctx.inbox.push` is present and throws until Phase 7, the shape 1.6.0
settled on for a member that arrives a phase late.
MODULE_API_VERSION 1.7.0 on both halves. Additions only; module-uo's
`coreApi: "^1.3.0"` still resolves.
Three design decisions, approved by the org lead before any code:
ONE NAMESPACE for trigger ids and notification-stream ids (ENGAGEMENT.md §7.2,
against the recommendation in the text). A trigger is a payload contract
attached to an id that may also carry a subscription toggle, so an id has
exactly one owner across both facets, checked in both directions. Core's five
trigger ids ARE its five stream ids, so the same-owner upgrade case is
exercised on every boot rather than only by a module. It keeps
notification_channel_prefs single-keyed in Phase 3, where two namespaces would
have forced a `kind` discriminator into its primary key.
Two knock-on effects appeared only once it was implemented. The id grammar had
to be RELAXED to admit `_` inside a segment — §4.3's own worked example is
`uo.house.idoc_warning`, and two grammars over one namespace would mean an id
legal as a trigger and illegal as the stream it is the same event as. And the
seven grandfathered `uo.*` ids had to share their legacy allowlist with
triggers, because under one namespace `idoc.warning` is a single id. The push
catalog is untouched either way: allStreams() still serves the stream facet
only, so the shipped Android client sees exactly what it saw before.
THE CEILING LATTICE (G24), which the plan named everywhere and defined nowhere.
It is containment, not size: everyone ⊃ authenticated ⊃ {subscribers, members,
staff, owner}, with the four leaves mutually incomparable. The flat total order
the plan's wording invites would let a `staff`-ceilinged trigger be given an
`owner` audience — a rule that mails cheat detection to the player it detected.
Fewer people is not less exposure. Two incomparable ceilings have no meet at
all, so a composition is refused rather than guessed; union-widens is the
intuitive implementation and it is the wrong one.
`kind: 'event' | 'scheduled'` is declarable now and no evaluator exists (§7.1
Q6). Registration accepts `scheduled` and emit refuses to fire one, so `kind`
means something from the moment it can be written rather than from the moment
it is honoured.
Also: `GET /admin/engagement/{triggers,audiences}`, served from the registries
rather than a table so an uninstalled module simply stops appearing;
`npm run engagement:manifest` plus its CI `--check`, the twin of the route
manifest, because renaming a variable breaks stored templates silently, at send
time, in mail someone already received.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
86
server/test/engagementCeilings.test.js
Normal file
86
server/test/engagementCeilings.test.js
Normal file
@@ -0,0 +1,86 @@
|
||||
// ── The audience ceiling lattice ───────────────────────────────────────────
|
||||
//
|
||||
// ENGAGEMENT.md §5.1a / G24. These are the tests for the security property the
|
||||
// whole rule model rests on: **composition may narrow, never widen**, and a
|
||||
// ceiling is about WHICH people rather than how many.
|
||||
//
|
||||
// The case worth naming is `staff` vs `owner`. Under the flat total order the
|
||||
// plan's wording invites — self < owner < staff < members < authenticated <
|
||||
// everyone — a trigger ceilinged at `staff` also permits `owner`, so a rule
|
||||
// could mail `uo.cheat.detected` to the player it detected. That is the bug this
|
||||
// file exists to keep out, so it is asserted explicitly rather than left implied
|
||||
// by the shape of the table.
|
||||
|
||||
const { test } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
|
||||
const ceilings = require('../src/modules/ceilings')
|
||||
|
||||
test('the six ceilings are the vocabulary, and nothing else is', () => {
|
||||
assert.deepEqual(
|
||||
[...ceilings.CEILINGS].sort(),
|
||||
['authenticated', 'everyone', 'members', 'owner', 'staff', 'subscribers'],
|
||||
)
|
||||
for (const id of ceilings.CEILINGS) assert.ok(ceilings.LABELS[id], `${id} has an operator label`)
|
||||
assert.equal(ceilings.isCeiling('nobody'), false)
|
||||
assert.equal(ceilings.isCeiling(undefined), false)
|
||||
})
|
||||
|
||||
test('everyone permits every ceiling; every ceiling permits itself', () => {
|
||||
for (const id of ceilings.CEILINGS) {
|
||||
assert.equal(ceilings.permits('everyone', id), true, `everyone permits ${id}`)
|
||||
assert.equal(ceilings.permits(id, id), true, `${id} permits itself`)
|
||||
}
|
||||
})
|
||||
|
||||
test('authenticated permits the four leaves but not everyone', () => {
|
||||
for (const leaf of ['subscribers', 'members', 'staff', 'owner']) {
|
||||
assert.equal(ceilings.permits('authenticated', leaf), true)
|
||||
}
|
||||
assert.equal(ceilings.permits('authenticated', 'everyone'), false)
|
||||
})
|
||||
|
||||
// The one that a flat ordering gets wrong.
|
||||
test('a staff ceiling does NOT permit owner — fewer people is not less exposure', () => {
|
||||
assert.equal(ceilings.permits('staff', 'owner'), false)
|
||||
assert.equal(ceilings.permits('owner', 'staff'), false)
|
||||
// …and the same for every other pair of leaves, so the property is the tree's
|
||||
// and not a special case someone wrote for cheat detection.
|
||||
const leaves = ['subscribers', 'members', 'staff', 'owner']
|
||||
for (const a of leaves) {
|
||||
for (const b of leaves) {
|
||||
if (a === b) continue
|
||||
assert.equal(ceilings.permits(a, b), false, `${a} must not permit ${b}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
test('an unknown ceiling is permitted by nothing, on either side', () => {
|
||||
assert.equal(ceilings.permits('everyone', 'god'), false)
|
||||
assert.equal(ceilings.permits('god', 'owner'), false)
|
||||
assert.equal(ceilings.permits('everyone', undefined), false)
|
||||
})
|
||||
|
||||
test('A OR B takes the NARROWER of the two ceilings, not the wider', () => {
|
||||
assert.equal(ceilings.meet('everyone', 'staff'), 'staff')
|
||||
assert.equal(ceilings.meet('staff', 'everyone'), 'staff')
|
||||
assert.equal(ceilings.meet('authenticated', 'members'), 'members')
|
||||
assert.equal(ceilings.meet('members', 'members'), 'members')
|
||||
})
|
||||
|
||||
test('incomparable ceilings have no meet — the save is refused, not guessed', () => {
|
||||
assert.equal(ceilings.meet('staff', 'members'), null)
|
||||
assert.equal(ceilings.meet('owner', 'subscribers'), null)
|
||||
assert.equal(ceilings.meet('staff', 'nonsense'), null)
|
||||
})
|
||||
|
||||
test('meetAll folds, short-circuits to null, and has no opinion about an empty list', () => {
|
||||
assert.equal(ceilings.meetAll(['everyone', 'authenticated', 'members']), 'members')
|
||||
// members ∧ staff is undefined, so the whole composition is.
|
||||
assert.equal(ceilings.meetAll(['everyone', 'members', 'staff']), null)
|
||||
assert.equal(ceilings.meetAll(['owner']), 'owner')
|
||||
// Not 'everyone': an empty composition states no bound, and defaulting it to
|
||||
// the top would make "no audiences selected" the widest possible rule.
|
||||
assert.equal(ceilings.meetAll([]), null)
|
||||
assert.equal(ceilings.meetAll(null), null)
|
||||
})
|
||||
Reference in New Issue
Block a user