6.6 KiB
AGENTS.md: Contributions & XP
Parent:
../../../AGENTS.md(root guide: commands, Server Action convention, domain guide index).
Crowdsourced contribution XP: users earn XP for helping build the app (wiki pages, mission
planning markers, surveys, journal entries, manual awards). The XP feeds the existing virtual
profile total
(Profiles.progression.xp), so it shows up in the sidebar XPDisplay, the profile page, and the
level ladder with no extra wiring.
Data model
contribution-entries(src/collections/users/ContributionEntries.ts) — append-only ledger, one row per contribution. Fields:user,type,status,points(snapshot),sourceCollectionsourceId(polymorphic ref), optionalmission,summary, uniqueidempotencyKey,queuedAt/grantedAt/grantedBy/revokedAt/revokedBy/revokedReason.
- Status lifecycle:
queued→granted(counts toward XP) |revoked(pulled back) |expired(never granted, e.g. mission cancelled). - Access: REST read = own rows (any status) plus everyone's
grantedrows; create/update/delete arefalse(service-only,overrideAccess). The admin panel is a read-only review surface.
Profiles.progression.contributions.xpEarned— denormalized granted total, maintained only byaddContributionXp. Folded into the virtual XPafterReadsum inProfiles.ts. Any profile read that displays XP must selectprogression.contributions(layout, home dashboard, profile page).
Rules
src/lib/contributions/rules.ts (pure, client-safe) is the single source of tunables. Points are
snapshotted onto each entry, so editing a value rebalances future earnings only.
| Type | Points | Mode | Guards |
|---|---|---|---|
wiki-page-create |
10 | instant | 50 XP/day cap; Documentation category excluded |
wiki-page-edit |
5 | instant | 30 XP/day cap; min 200-char normalized content delta |
mission-map-marker |
2 | queued | first 10 per user per mission |
survey-completion |
5 | instant | one per submission (idempotency) |
journal-publish |
5 | instant | 25 XP/day cap; min 200-char body; one per entry |
manual |
grantor-set | instant | reason required, no self-grants below superuser |
Service (src/lib/contributions/)
Single write path (the applyTransaction() principle): every entry creation, status flip, bucket
update, event, and notification flows through these functions.
recordContribution(payload, input)— applies rules, idempotency (type:sourceCollection:sourceId:dedupeSuffix), daily and per-mission caps. Instant types grant immediately (bucket + event + notification); queued types just create the row.grantQueuedEntriesForMission/expireQueuedEntriesForMission— lifecycle sweeps called from the MissionsafterChangehook on Completed / Cancelled.revokeEntry/revokeEntriesForSource/revokeQueuedForSource— moderation and source-deletion cleanup. Revoking agrantedentry subtracts from the bucket.getContributionSummary(profile/history) /getContributionLeaderboard(the/contributionspage).buckets.ts—addContributionXp(the only bucket mutator; broadcastsprofile-updatewithxpGainedso the sidebar animates).index.ts—contentHashfor wiki edit dedupe.
Lifecycle (map markers)
- Placement (
saveMissionMapMarker, create branch) →recordContributionqueues 2 XP. - Marker deleted / map cleared →
revokeQueuedForSourcemarks the queued rowrevoked. - Mission → Completed (auto-complete tick or manual status change, both via
payload.update) → MissionsafterChangecallsgrantQueuedEntriesForMission: markers that still exist grant, the rest revoke. - Mission → Cancelled / deleted →
expireQueuedEntriesForMission.
Integration points
- Wiki:
createWikiPage/updateWikiPageaward on success (Documentation excluded; edits need a substantive delta, deduped by content hash);deleteWikiPagerevokes the page's entries. - Journals:
createJournal/updateJournalawardjournal-publishwhen an entry is published with a body of at least 200 chars;deleteJournalrevokes the entry's XP. - Surveys:
formBuilderConfig.tsform-submissionsafterChangeawardssurvey-completion. - Missions:
classification.planningMapDelegates+afterChangesweep +beforeDeleteexpire. - Moderation:
src/app/(frontend)/contributions/actions.ts(grantManualContribution,revokeContribution) gated oncontribution-entries:moderate. - Events:
contribution:queue/grant/revoke/expire(public) +mission:planning-delegates;contribution-entries,form-submissions,mission-map-markersare valid event targets. - Notifications: muteable
contribution:xp-granted/contribution:xp-revoked. - Permissions: group "Contributions" —
contribution-entries:read/create/update/delete,contribution-entries:moderate,admin:contribution-entries:manage. - UI: profile "Contributions" section (
ContributionHistory),/contributionsleaderboard (ContributionLeaderboard, sidebar "Leaderboard" entry), mission planning-map delegation control (PlanningAccessControl) and read-only lock banner.
Planning-map access (two tiers)
- Curators (
missions:updateor the mission'screatedBy) — toggle the map, place/edit/delete any marker, clear all, manage delegates. - Delegates (per-mission
classification.planningMapDelegates, set bymissions:updateholders viasetMissionPlanningDelegates) — place markers and edit/delete only their own. - The map locks for everyone (curators included) once status is Completed/Cancelled
(
isMissionPlanLocked). The delegate list stays on the doc as an audit record.
Anti-gaming
Idempotency key (unique index) prevents double-minting; daily/per-mission caps; wiki edit substance threshold; queued-by-default for curatable work (planner deletion revokes); source deletion pulls XP back; manual grants require a reason and block self-grants below superuser; everything is auditable in the ledger plus the public event log.
Gotchas
- The bucket is read-modify-write; concurrent grants to the same user can race (acceptable at unit
scale). Run
bun run src/tools/seed/reconcileContributionXp.tsto rebuild it from the ledger. missions_rels.pathis a plain varchar, so adding a hasMany relationship to Missions needs no schema migration; thecontribution-entriestable and profile column do (migration20261009_201920_add_contribution_entries).- The Missions
afterChangesweep must stay loop-safe: it writes entries/profiles, never missions. - Tests:
tests/int/contributions.int.spec.ts,tests/int/journal-contributions.int.spec.ts.