fix(search): honor in-memory cache on unchanged setSearch and full view

Repeated setSearch with the same query now marks searchResultsCached when
the resolve cache is still valid, and cache hits apply to full view() polls
so agentctl and headless sessions do not re-run guild.search every tick.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Raven Scott
2026-05-23 08:10:46 -04:00
co-authored by Cursor
parent 06db01f632
commit 756b789ec5
+19 -1
View File
@@ -331,6 +331,7 @@ class PearcordPlatform extends EventEmitter {
this._searchQuery = '' this._searchQuery = ''
this._searchScope = 'channel' this._searchScope = 'channel'
this._searchIncludeMesh = true this._searchIncludeMesh = true
this._searchUnchanged = false
this._lastSearchMeshMeta = null this._lastSearchMeshMeta = null
this._guildSearchIndexes = new Map() this._guildSearchIndexes = new Map()
this._searchIndexStore = null this._searchIndexStore = null
@@ -1170,6 +1171,21 @@ class PearcordPlatform extends EventEmitter {
nextScope === this._searchScope && nextScope === this._searchScope &&
(includeMesh === undefined || nextMesh === this._searchIncludeMesh) (includeMesh === undefined || nextMesh === this._searchIncludeMesh)
) { ) {
this._searchUnchanged = true
const cacheKey = this._searchResolveCacheKeyNow()
if (
this._cachedSearchResults &&
cacheKey === this._searchResolveCacheKey &&
this._searchCacheGuildId === (this.guild?.guild?.id || '')
) {
this._lastSearchFromCache = true
if (this._lastSearchMeshMeta) {
this._lastSearchMeshMeta = {
...this._lastSearchMeshMeta,
searchCached: true
}
}
}
return { return {
query: this._searchQuery, query: this._searchQuery,
scope: this._searchScope, scope: this._searchScope,
@@ -1177,6 +1193,7 @@ class PearcordPlatform extends EventEmitter {
unchanged: true unchanged: true
} }
} }
this._searchUnchanged = false
this._searchQuery = nextQuery this._searchQuery = nextQuery
this._searchScope = nextScope this._searchScope = nextScope
if (includeMesh !== undefined) this._searchIncludeMesh = nextMesh if (includeMesh !== undefined) this._searchIncludeMesh = nextMesh
@@ -1335,7 +1352,7 @@ class PearcordPlatform extends EventEmitter {
const light = !!opts.light const light = !!opts.light
const cacheKey = this._searchResolveCacheKeyNow() const cacheKey = this._searchResolveCacheKeyNow()
const guildId = this.guild.guild.id const guildId = this.guild.guild.id
if (light && cacheKey === this._searchResolveCacheKey && this._cachedSearchResults) { if (cacheKey === this._searchResolveCacheKey && this._cachedSearchResults) {
const channelCacheStale = const channelCacheStale =
this._searchScope === 'channel' && this._searchScope === 'channel' &&
this._cachedSearchResults.some( this._cachedSearchResults.some(
@@ -10628,6 +10645,7 @@ class PearcordPlatform extends EventEmitter {
) )
: [], : [],
searchScope: this._searchScope, searchScope: this._searchScope,
searchUnchanged: !!this._searchUnchanged,
forumIncludeArchived: this._forumIncludeArchived, forumIncludeArchived: this._forumIncludeArchived,
searchResults: await this._resolveSearchResults({ light }), searchResults: await this._resolveSearchResults({ light }),
searchResultsCached: !!this._lastSearchFromCache, searchResultsCached: !!this._lastSearchFromCache,