#22945: feat(discord): seed channel history from API and persist to disk
channel: discord
size: S
Cluster:
Signal and Discord Fixes
## Summary
- When `guildHistories` is empty for a channel (e.g. after gateway restart), automatically fetches the last N messages from the Discord API to seed conversation context. This ensures the bot has awareness of recent messages without requiring users to manually re-establish context.
- Persists `guildHistories` to disk (`discord-history-cache.json`) on provider shutdown and loads on startup, so most channels don't need the API call at all — only channels not in the disk cache fall back to API seeding.
### Details
**API seeding** (`message-handler.process.ts`):
- Triggers when `shouldIncludeChannelHistory` is true, `historyLimit > 0`, and the in-memory map has no entries for the channel
- Fetches `min(historyLimit, 50)` messages via `readMessagesDiscord` (existing function)
- Labels bot's own messages as `"assistant"`, uses nick/globalName/username fallback for others
- One-shot per channel — once seeded, the in-memory map takes over
**Disk persistence** (`provider.ts`):
- `loadGuildHistoriesFromDisk()` on startup, `saveGuildHistoriesToDisk()` in the `finally` block on shutdown
- Caps at 50 entries per channel on save
- Cache stored in the config dir alongside other state files
Closes #15807
## Test plan
- [ ] Restart gateway — verify disk cache loads (check verbose logs for `loaded N channel histories from disk cache`)
- [ ] Send message in a guild channel after restart — verify API seeding triggers for channels not in cache (`seeded N history entries for <channelId> from API`)
- [ ] Verify bot responses reference recent conversation context after restart
- [ ] Verify no duplicate history entries on subsequent messages (seeding only fires once per channel)
- [ ] Verify DMs are unaffected (seeding only applies to guild channels)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Added automatic seeding of Discord channel history from the API when the in-memory map is empty (e.g., after gateway restart), and implemented disk persistence for `guildHistories` to minimize API calls on subsequent startups.
Key changes:
- API seeding triggers when `shouldIncludeChannelHistory` is true, `historyLimit > 0`, and no entries exist for the channel in memory
- Fetches up to `min(historyLimit, 50)` messages using `readMessagesDiscord` with `before: message.id` to exclude the current message
- Bot messages labeled as `"assistant"`, others use nick/globalName/username fallback
- Disk cache stored at `discord-history-cache.json` in config directory, loaded on startup and saved on shutdown
- Cache capped at 50 entries per channel on save to prevent unbounded growth
<h3>Confidence Score: 4/5</h3>
- Safe to merge with minor considerations - the changes are well-contained, include proper error handling, and follow existing patterns
- The implementation is sound with good error handling and follows the repository's patterns. The seeding logic correctly checks for empty history before making API calls, properly handles the `botUserId` identification, and includes appropriate error logging. Disk persistence uses standard filesystem operations with try-catch blocks. The only minor consideration is ensuring the API seeding doesn't cause rate limiting issues in channels with high message volume, but the one-shot-per-channel design and `min(historyLimit, 50)` cap mitigate this risk.
- No files require special attention
<sub>Last reviewed commit: acd58af</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#13540: feat: persist channel histories to disk
by carrotRakko · 2026-02-10
75.0%
#16736: fix: stagger multi-account channel startup to avoid Discord rate li...
by rm289 · 2026-02-15
74.1%
#19615: fix(discord): include default account when sub-accounts are configured
by prue-starfield · 2026-02-18
72.2%
#16622: fix(mattermost): record pending history for messages dropped by gro...
by oskarmodig · 2026-02-14
71.2%
#23158: discord: harden preflight/reply path against slow lookup latency
by danielstarman · 2026-02-22
70.2%
#19362: feat(discord): clean up sessions when channel is deleted
by chubes4 · 2026-02-17
69.8%
#12204: fix(discord): resolve numeric guildId/channelId pairs in channel al...
by mcaxtr · 2026-02-09
69.1%
#14318: feat(discord): enforce outbound allowlist on send functions
by builtbyrobben · 2026-02-11
69.0%
#18718: matrix: add pending group history context for room messages
by pharasyte · 2026-02-17
69.0%
#10731: fix(discord): add outer retry loop for gateway reconnect exhaustion
by Milofax · 2026-02-06
68.9%