1
0
Fork 0

docs: document the unit poll system

Root domain index gains a row with the three invariants that matter: castPollVote
is the only vote path, poll-votes is superuser-read only (which is what makes an
anonymous poll anonymous), and a poll is created once per sourceKey.

The bot guide gains a feature-flow section covering why buttons beat a native
poll, how the hash sync works, why buttons stay neutral, and the tie ladder.
This commit is contained in:
Jason Fraley 2026-12-01 14:17:57 -05:00
parent 06dbc88a2e
commit 3291340b77
3 changed files with 16 additions and 1 deletions

View file

@ -139,6 +139,7 @@ lists the invariant that caused the most damage when ignored.
| Marketplace & NPC negotiation | `src/lib/market/AGENTS.md` | Only "clean" locker stock can be listed; `buyListing()` is the single purchase path |
| Superstructure interiors (rooms/jobs/stations) | `src/lib/interior/AGENTS.md` | Cancel never touches storage (refund flows through the job's collectable output); collect prechecks deposits and parks on full storage |
| Notifications | `src/lib/notifications/AGENTS.md` | `notifyUser()` is fire-and-forget via `overrideAccess` |
| Unit polls (NPC-triggered) | `src/lib/polls/` + `src/bot/AGENTS.md` ("Feature flow: unit polls") | `castPollVote()` is the only vote path; `poll-votes` is superuser-read only, which is what keeps anonymous polls anonymous; a poll is created once per `sourceKey` and never re-triggered |
| Announcements | `src/lib/announcements/AGENTS.md` | Client-side evaluation (Shims pattern); role targeting uses Roles docs, not the legacy enum |
| Narrative events editor | `src/components/admin/narrative-flow/AGENTS.md` | Flow data syncs to the Payload form via `useField().setValue()` with JSON diffing |
| Tech tree (admin) | `src/components/admin/tech-tree/AGENTS.md` | Positions go through one bulk POST; never PATCH per node (pool exhaustion) |

View file

@ -8,7 +8,7 @@ Standalone long-running process (`bun run bot`). Imports `@payload-config` direc
## Env
Required: `DISCORD_TOKEN`, `DISCORD_GUILD_ID` (fail-fast on missing vars in `config.ts`). Optional: `DISCORD_OPS_CHANNEL_ID`, `DISCORD_ANNOUNCE_CHANNEL_ID`, `DISCORD_CHANGELOG_CHANNEL_ID` (dedicated #releases channel for `/changelog`; falls back to the announce channel), `DISCORD_STAFF_ROLE_IDS`, `DISCORD_SIDE_OPS_CHANNEL_ID` (Friday open-slot notice channel), `DISCORD_COMMUNITY_ZEUS_ROLE_ID` (role ping for that notice), `DISCORD_NPC_EVENT_CHANNEL_ID` (NPC dispatch channel for dialogue events; unset disables posting), `DISCORD_DEBUG_CHANNEL_ID` (sandbox channel for `/debug run ... mode:sandbox`; unset disables sandbox), `APP_URL`, plus poll intervals `DISCORD_ATTENDANCE_POLL_MS` (default 60s), `DISCORD_NOTIFICATION_POLL_MS` (default 20s), `DISCORD_EVALUATION_POLL_MS` (default 60s), `DISCORD_IMMINENT_POLL_MS` (default 60s), `DISCORD_SIDE_OPS_POLL_MS` (default 30 min), `DISCORD_PRESENCE_POLL_MS` (default 30s), `DISCORD_NPC_EVENT_POLL_MS` (default 60s). Logs through `payload.logger`.
Required: `DISCORD_TOKEN`, `DISCORD_GUILD_ID` (fail-fast on missing vars in `config.ts`). Optional: `DISCORD_OPS_CHANNEL_ID`, `DISCORD_ANNOUNCE_CHANNEL_ID`, `DISCORD_CHANGELOG_CHANNEL_ID` (dedicated #releases channel for `/changelog`; falls back to the announce channel), `DISCORD_STAFF_ROLE_IDS`, `DISCORD_SIDE_OPS_CHANNEL_ID` (Friday open-slot notice channel), `DISCORD_COMMUNITY_ZEUS_ROLE_ID` (role ping for that notice), `DISCORD_NPC_EVENT_CHANNEL_ID` (NPC dispatch channel for dialogue events; unset disables posting), `DISCORD_DEBUG_CHANNEL_ID` (sandbox channel for `/debug run ... mode:sandbox`; unset disables sandbox), `APP_URL`, plus poll intervals `DISCORD_ATTENDANCE_POLL_MS` (default 60s), `DISCORD_NOTIFICATION_POLL_MS` (default 20s), `DISCORD_EVALUATION_POLL_MS` (default 60s), `DISCORD_IMMINENT_POLL_MS` (default 60s), `DISCORD_SIDE_OPS_POLL_MS` (default 30 min), `DISCORD_PRESENCE_POLL_MS` (default 30s), `DISCORD_NPC_EVENT_POLL_MS` (default 60s), `DISCORD_POLL_CHANNEL_ID` (unit poll channel; falls back to the ops channel), `DISCORD_POLL_POLL_MS` (default 60s). Logs through `payload.logger`.
## Structure
@ -36,6 +36,7 @@ bot/
services/
index.ts # Service registry
missionEmbeds.ts # Attendance embed lifecycle + reconcile loop (poll tick)
pollEmbeds.ts # Unit poll embed + buttons + reconcile loop + LISTEN ptf_poll_changed
notificationBridge.ts # Poll user-notifications → Discord DMs
evaluationReminders.ts # Post-mission evaluation reminder DMs (one-shot marker on Missions)
imminentReminders.ts # Pre-op reminder: thread on the roll-call message + attendee pings (one-shot marker on Missions)
@ -85,6 +86,18 @@ Actions live in `bot/lib/debugActions.ts`. Each `build` receives the client and
Bot posts RSVP embeds (Yes/Tentative/No) for future, Scheduled, visibility:"unit" missions into ops channel. Stores `discordMessageId` + `discordAttendanceHash` on mission. Reconciles hash changes every poll tick (web ↔ Discord two-way sync, loop-safe). The embed title links to the mission page, a mission summary sits under the title (truncated to 300 characters with an ellipsis), and an "Open the op" field carries the URL; `attendanceHash` includes an embed schema version so layout-only changes re-render live roll-calls once (idempotent). Web UI: `MissionAttendance` component. Weekly main missions are cloned by `bun run payload generate-mission` (a payload bin, see `src/scripts/AGENTS.md`). Staff can force one outside the lead-time window with `/rollcall [mission]` (ID, name, or code name; omit to target the next upcoming Scheduled unit op): `forceRollCall` in `missionEmbeds.ts` re-renders the live message or posts a fresh one (even after a manual deletion), bypassing only the 3-day gate; target lookup/validation lives in `src/lib/intelligence/rollCall.ts`.
## Feature flow: unit polls
An NPC `event` node with the `poll` effect raises a unit-wide poll: `launchDialoguePoll()` (`src/lib/polls/service.ts`) snapshots the node's authored config into a `polls` document keyed by `sourceKey` (`<dialogueId>:<nodeId>`), so the trigger is idempotent: a second player reaching the node joins the existing poll instead of opening a rival one, and later edits to the node never rewrite a poll people already voted in. The bot posts an embed with one button per option (`ptf-poll:<pollId>:<optionKey>`) into `DISCORD_POLL_CHANNEL_ID`, and both surfaces write through `castPollVote()`, the only vote path.
**Why buttons, not a native Discord poll**: native polls (message type 46) cannot weight votes, cannot be edited, and cannot be closed early. Buttons keep weighted tallies, the tie ladder and the two-way sync possible.
**Sync** mirrors the roll-call exactly: `discordPollHash` fingerprints everything the message shows (schema version, status, counts, winner, deadline, public voter names), the reconcile loop edits on drift and stamps the hash back, and a `LISTEN ptf_poll_changed` listener reacts to web-side votes in under a second. A deleted message is never reposted (one-shot rule); the web page stays the source of truth. `advancePoll()` runs inside the reconcile pass, so a poll closes and resolves even when nobody is looking at it.
**Buttons are deliberately neutral**: the message is shared, so highlighting one member's choice would show their ballot to the unit, and on an anonymous poll it would leak the thing the poll hides. The voter's own ballot comes back ephemerally.
**Ties**: `tallyPoll` weights votes by the highest matching role tier, then `resolvePollTally` gives a tie to the conversation initiator when exactly one tied option is on their ballot. Otherwise the poll parks in `pending_decision` with the tally frozen, the buttons disable, and a superuser settles it on `/polls/[id]`; `resolveDeadlineTie` picks a random tied option once `tieGraceHours` passes. Only a resolved poll writes its `resultFlag`.
## Feature flow: notifications
`notificationBridge` polls `user-notifications` (cursor = last seen id; cap 5 DMs/tick). DMs opted-in users (`preferences.discord.enabled`, not in `mutedTypes`). Remaining polish: the web preferences UI does not yet expose the `preferences.discord` toggles — they exist on the Users collection and are read by the bridge, but are currently only settable in the Payload admin panel.

View file

@ -33,6 +33,7 @@ lib/
logistics/ # Mission lifecycle + mission reminder logic
market/ # Buy/sell, NPC negotiation, dialogue, vendor resolution
notifications/ # User notification helper + muteable types
polls/ # Unit polls: tally.ts (pure weights/tie ladder), service.ts (single write path), surface.ts (read model + anonymity), realtime.ts (pg_notify)
projects/ # Project ticket-type metadata (projectMeta.ts)
realtime/ # In-process SSE bus + presence tracking (single-instance only)
session/ # Session token expiry helpers