VersionControl:PRs/magenta/411

From PickiPedia: A knowledge base of bluegrass, old time psychedelic jams, and other public domain music
Jump to navigationJump to search
magenta#411: Lighter polls: three indexes, and four things a poll asked every 4 s asked once
State 🟣 merged 2026-11-06 18:57:04
Worked on by magent, justin
Moods #notifications
Deploy not said
Branch poll-light → main, by magent-cryptograss
Opened · last pushed 2026-11-06 18:15:47 · 2026-11-06 18:55:27
Justin, 6 Nov: "let's just go right into profiling it."

**What a poll costs now** (`Server-Timing` on `/api/moods/<slug>/turns/?after=`, live, 6 Nov, nine samples across #interfaces, #general and #emergence; total 1.4 to 5.4 s):

| lap | ms | what it is |
|---|---|---|
| pronouns | 106–997 | `mood.thinking_entities()`: a DISTINCT over every row of the Mood |
| zones | 92–1517 | the same DISTINCT again |
| agents | 210–1086 | per agent: settings rows fetched again, its latest row here (context, changes since), and with no model set, its latest row *anywhere*, sorted over everything it ever sent |
| activities | 121–691 | per agent, its latest rows here |
| pins / polls / reactions | 10–428 each | `filter(mood, source_file=…)`: no index on source, so the Mood's rows are walked |
| timeline, wakes | up to 830 / 280 | #emergence only; real new rows |

I couldn't profile against the live database (no credentials here, as it should be), so this is the server's own clocks plus reading the queries.

**Changes**
- **Migration 0046**: three indexes on messages: `(mood, sender, created_at)`, `(sender, created_at)`, `(mood, source_file)`. Built `CONCURRENTLY` on PostgreSQL in a non-atomic migration, so the poller and watcher keep writing while it builds; plain on SQLite. Checked on a PostgreSQL 16 container: applies, the three indexes exist as expected, unapplies.
- **`spoke_in(mood)`**: who has spoken here, kept 60 s. pronouns and zones both read it. A first word in a Mood shows that person's pronouns within the minute.
- **`agents_in`**: the settings rows it already fetched are passed to `resolve`, not fetched again per agent; the "model it last ran on anywhere" fallback is `last_model_of`, kept 10 min.
- activities is unchanged in logic; its per-agent scans use the new `(mood, sender, created_at)` index.

**Expected**: pronouns and zones drop to near zero; agents and activities to tens of ms; pins, polls and reactions to a few ms. I'll read the Server-Timing again after the redeploy and say what it actually did.

Tests: 1726 pass, including `test_poll_light` (the two caches). `makemigrations --check` is clean.

Redeploy: maybelle (the migration runs on deploy; the index build takes a little while on the live table but doesn't block writes)
Worked on by: magent and Justin, in #notifications