fix(auto-slash-command): expire duplicate suppression after 30s
Allow legitimate repeated slash commands in long sessions by replacing session-lifetime dedup with a short-lived TTL cache. Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,16 +1,25 @@
|
||||
const MAX_PROCESSED_ENTRY_COUNT = 10_000
|
||||
const PROCESSED_COMMAND_TTL_MS = 30_000
|
||||
|
||||
function trimProcessedEntries(entries: Set<string>): Set<string> {
|
||||
function pruneExpiredEntries(entries: Map<string, number>, now: number): Map<string, number> {
|
||||
return new Map(Array.from(entries.entries()).filter(([, expiresAt]) => expiresAt > now))
|
||||
}
|
||||
|
||||
function trimProcessedEntries(entries: Map<string, number>): Map<string, number> {
|
||||
if (entries.size <= MAX_PROCESSED_ENTRY_COUNT) {
|
||||
return entries
|
||||
}
|
||||
|
||||
return new Set(Array.from(entries).slice(Math.floor(entries.size / 2)))
|
||||
return new Map(
|
||||
Array.from(entries.entries())
|
||||
.sort((left, right) => left[1] - right[1])
|
||||
.slice(Math.floor(entries.size / 2))
|
||||
)
|
||||
}
|
||||
|
||||
function removeSessionEntries(entries: Set<string>, sessionID: string): Set<string> {
|
||||
function removeSessionEntries(entries: Map<string, number>, sessionID: string): Map<string, number> {
|
||||
const sessionPrefix = `${sessionID}:`
|
||||
return new Set(Array.from(entries).filter((entry) => !entry.startsWith(sessionPrefix)))
|
||||
return new Map(Array.from(entries.entries()).filter(([entry]) => !entry.startsWith(sessionPrefix)))
|
||||
}
|
||||
|
||||
export interface ProcessedCommandStore {
|
||||
@@ -21,14 +30,19 @@ export interface ProcessedCommandStore {
|
||||
}
|
||||
|
||||
export function createProcessedCommandStore(): ProcessedCommandStore {
|
||||
let entries = new Set<string>()
|
||||
let entries = new Map<string, number>()
|
||||
|
||||
return {
|
||||
has(commandKey: string): boolean {
|
||||
const now = Date.now()
|
||||
entries = pruneExpiredEntries(entries, now)
|
||||
return entries.has(commandKey)
|
||||
},
|
||||
add(commandKey: string): void {
|
||||
entries.add(commandKey)
|
||||
const now = Date.now()
|
||||
entries = pruneExpiredEntries(entries, now)
|
||||
entries.delete(commandKey)
|
||||
entries.set(commandKey, now + PROCESSED_COMMAND_TTL_MS)
|
||||
entries = trimProcessedEntries(entries)
|
||||
},
|
||||
cleanupSession(sessionID: string): void {
|
||||
|
||||
Reference in New Issue
Block a user