1
0
Fork 0
polaris-task-force/src/lib/contributions/AGENTS.md

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), sourceCollection
    • sourceId (polymorphic ref), optional mission, summary, unique idempotencyKey, 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 granted rows; create/update/delete are false (service-only, overrideAccess). The admin panel is a read-only review surface.
  • Profiles.progression.contributions.xpEarned — denormalized granted total, maintained only by addContributionXp. Folded into the virtual XP afterRead sum in Profiles.ts. Any profile read that displays XP must select progression.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 Missions afterChange hook on Completed / Cancelled.
  • revokeEntry / revokeEntriesForSource / revokeQueuedForSource — moderation and source-deletion cleanup. Revoking a granted entry subtracts from the bucket.
  • getContributionSummary (profile/history) / getContributionLeaderboard (the /contributions page).
  • buckets.ts — addContributionXp (the only bucket mutator; broadcasts profile-update with xpGained so the sidebar animates). index.ts — contentHash for wiki edit dedupe.

Lifecycle (map markers)

  • Placement (saveMissionMapMarker, create branch) → recordContribution queues 2 XP.
  • Marker deleted / map cleared → revokeQueuedForSource marks the queued row revoked.
  • Mission → Completed (auto-complete tick or manual status change, both via payload.update) → Missions afterChange calls grantQueuedEntriesForMission: markers that still exist grant, the rest revoke.
  • Mission → Cancelled / deleted → expireQueuedEntriesForMission.

Integration points

  • Wiki: createWikiPage / updateWikiPage award on success (Documentation excluded; edits need a substantive delta, deduped by content hash); deleteWikiPage revokes the page's entries.
  • Journals: createJournal / updateJournal award journal-publish when an entry is published with a body of at least 200 chars; deleteJournal revokes the entry's XP.
  • Surveys: formBuilderConfig.ts form-submissions afterChange awards survey-completion.
  • Missions: classification.planningMapDelegates + afterChange sweep + beforeDelete expire.
  • Moderation: src/app/(frontend)/contributions/actions.ts (grantManualContribution, revokeContribution) gated on contribution-entries:moderate.
  • Events: contribution:queue/grant/revoke/expire (public) + mission:planning-delegates; contribution-entries, form-submissions, mission-map-markers are 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), /contributions leaderboard (ContributionLeaderboard, sidebar "Leaderboard" entry), mission planning-map delegation control (PlanningAccessControl) and read-only lock banner.

Planning-map access (two tiers)

  • Curators (missions:update or the mission's createdBy) — toggle the map, place/edit/delete any marker, clear all, manage delegates.
  • Delegates (per-mission classification.planningMapDelegates, set by missions:update holders via setMissionPlanningDelegates) — 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.ts to rebuild it from the ledger.
  • missions_rels.path is a plain varchar, so adding a hasMany relationship to Missions needs no schema migration; the contribution-entries table and profile column do (migration 20261009_201920_add_contribution_entries).
  • The Missions afterChange sweep must stay loop-safe: it writes entries/profiles, never missions.
  • Tests: tests/int/contributions.int.spec.ts, tests/int/journal-contributions.int.spec.ts.