-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(discord): add opt-in staleness check for slow-reasoning agents #7431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lalalune
merged 1 commit into
elizaOS:develop
from
0xSolace:feat/discord-staleness-check
May 6, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import type { Content } from "@elizaos/core"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| applyDiscordStalenessGuard, | ||
| getDiscordStalenessConfig, | ||
| recordDiscordChannelMessageSeen, | ||
| } from "../staleness"; | ||
|
|
||
| function mockMessage(channelId = "channel-1") { | ||
| return { | ||
| id: "message-1", | ||
| channel: { id: channelId }, | ||
| } as never; | ||
| } | ||
|
|
||
| describe("Discord staleness guard", () => { | ||
| it("is disabled by default and parses scoped settings", () => { | ||
| const settings = new Map<string, unknown>([ | ||
| ["DISCORD_STALENESS_BEHAVIOR", "skip"], | ||
| ["DISCORD_STALENESS_THRESHOLD", "4"], | ||
| ]); | ||
|
|
||
| expect(getDiscordStalenessConfig((key) => settings.get(key))).toEqual({ | ||
| enabled: false, | ||
| behavior: "skip", | ||
| threshold: 4, | ||
| }); | ||
|
|
||
| settings.set("DISCORD_STALENESS_ENABLED", "true"); | ||
| expect(getDiscordStalenessConfig((key) => settings.get(key))).toEqual({ | ||
| enabled: true, | ||
| behavior: "skip", | ||
| threshold: 4, | ||
| }); | ||
| }); | ||
|
|
||
| it("allows responses when the newer-message delta is within threshold", () => { | ||
| const owner = {}; | ||
| const start = recordDiscordChannelMessageSeen(owner, "channel-1", "a"); | ||
| recordDiscordChannelMessageSeen(owner, "channel-1", "b"); | ||
| const content: Content = { text: "hello" }; | ||
|
|
||
| expect( | ||
| applyDiscordStalenessGuard({ | ||
| config: { enabled: true, behavior: "skip", threshold: 1 }, | ||
| owner, | ||
| message: mockMessage(), | ||
| startSequence: start, | ||
| content, | ||
| }), | ||
| ).toMatchObject({ shouldSend: true, stale: false }); | ||
| expect(content.text).toBe("hello"); | ||
| }); | ||
|
|
||
| it("skips stale responses when configured to skip", () => { | ||
| const owner = {}; | ||
| const start = recordDiscordChannelMessageSeen(owner, "channel-1", "a"); | ||
| recordDiscordChannelMessageSeen(owner, "channel-1", "b"); | ||
| recordDiscordChannelMessageSeen(owner, "channel-1", "c"); | ||
| const content: Content = { text: "hello" }; | ||
|
|
||
| expect( | ||
| applyDiscordStalenessGuard({ | ||
| config: { enabled: true, behavior: "skip", threshold: 1 }, | ||
| owner, | ||
| message: mockMessage(), | ||
| startSequence: start, | ||
| content, | ||
| }), | ||
| ).toMatchObject({ | ||
| shouldSend: false, | ||
| stale: true, | ||
| messagesSinceTurnStart: 2, | ||
| }); | ||
| }); | ||
|
|
||
| it("tags stale responses once when configured to tag", () => { | ||
| const owner = {}; | ||
| const start = recordDiscordChannelMessageSeen(owner, "channel-1", "a"); | ||
| recordDiscordChannelMessageSeen(owner, "channel-1", "b"); | ||
| recordDiscordChannelMessageSeen(owner, "channel-1", "c"); | ||
| const content: Content = { text: "hello" }; | ||
|
|
||
| const first = applyDiscordStalenessGuard({ | ||
| config: { enabled: true, behavior: "tag", threshold: 1 }, | ||
| owner, | ||
| message: mockMessage(), | ||
| startSequence: start, | ||
| content, | ||
| }); | ||
| const second = applyDiscordStalenessGuard({ | ||
| config: { enabled: true, behavior: "tag", threshold: 1 }, | ||
| owner, | ||
| message: mockMessage(), | ||
| startSequence: start, | ||
| content, | ||
| }); | ||
|
|
||
| expect(first).toMatchObject({ shouldSend: true, stale: true }); | ||
| expect(second).toMatchObject({ shouldSend: true, stale: true }); | ||
| expect(content.text).toBe("(catching up:) hello"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import type { Content } from "@elizaos/core"; | ||
| import type { Message as DiscordMessage } from "discord.js"; | ||
|
|
||
| export type DiscordStalenessBehavior = "tag" | "skip" | "ignore"; | ||
|
|
||
| export interface DiscordStalenessConfig { | ||
| enabled: boolean; | ||
| behavior: DiscordStalenessBehavior; | ||
| threshold: number; | ||
| } | ||
|
|
||
| const DEFAULT_THRESHOLD = 2; | ||
| const channelSequences = new WeakMap<object, Map<string, number>>(); | ||
| const lastMessageIds = new WeakMap<object, Map<string, string | undefined>>(); | ||
|
|
||
| function parseBoolean(value: unknown, fallback: boolean): boolean { | ||
| if (value === undefined || value === null) { | ||
| return fallback; | ||
| } | ||
| return String(value).trim().toLowerCase() === "true"; | ||
| } | ||
|
|
||
| function parseNonNegativeInteger(value: unknown, fallback: number): number { | ||
| const parsed = Number.parseInt(String(value ?? ""), 10); | ||
| return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback; | ||
| } | ||
|
|
||
| function parseBehavior(value: unknown): DiscordStalenessBehavior { | ||
| const normalized = String(value ?? "tag") | ||
| .trim() | ||
| .toLowerCase(); | ||
| return normalized === "skip" || | ||
| normalized === "ignore" || | ||
| normalized === "tag" | ||
| ? normalized | ||
| : "tag"; | ||
| } | ||
|
|
||
| function ensureSequenceMap(owner: object): Map<string, number> { | ||
| let map = channelSequences.get(owner); | ||
| if (!map) { | ||
| map = new Map<string, number>(); | ||
| channelSequences.set(owner, map); | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| export function getDiscordStalenessConfig( | ||
| getSetting: (key: string) => unknown, | ||
| ): DiscordStalenessConfig { | ||
| return { | ||
| enabled: parseBoolean(getSetting("DISCORD_STALENESS_ENABLED"), false), | ||
| behavior: parseBehavior(getSetting("DISCORD_STALENESS_BEHAVIOR")), | ||
| threshold: parseNonNegativeInteger( | ||
| getSetting("DISCORD_STALENESS_THRESHOLD"), | ||
| DEFAULT_THRESHOLD, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| export function recordDiscordChannelMessageSeen( | ||
| owner: object | undefined, | ||
| channelId: string | undefined, | ||
| messageId?: string, | ||
| ): number { | ||
| if (!owner || !channelId) { | ||
| return 0; | ||
| } | ||
| const sequences = ensureSequenceMap(owner); | ||
| const next = (sequences.get(channelId) ?? 0) + 1; | ||
| sequences.set(channelId, next); | ||
|
|
||
| let lastIds = lastMessageIds.get(owner); | ||
| if (!lastIds) { | ||
| lastIds = new Map<string, string | undefined>(); | ||
| lastMessageIds.set(owner, lastIds); | ||
| } | ||
| lastIds.set(channelId, messageId); | ||
|
|
||
| return next; | ||
| } | ||
|
|
||
| export function getDiscordChannelMessageSequence( | ||
| owner: object | undefined, | ||
| channelId: string | undefined, | ||
| ): number { | ||
| if (!owner || !channelId) { | ||
| return 0; | ||
| } | ||
| return ensureSequenceMap(owner).get(channelId) ?? 0; | ||
| } | ||
|
|
||
| export interface DiscordStalenessDecision { | ||
| shouldSend: boolean; | ||
| stale: boolean; | ||
| messagesSinceTurnStart: number; | ||
| behavior: DiscordStalenessBehavior; | ||
| } | ||
|
|
||
| export function applyDiscordStalenessGuard(options: { | ||
| config: DiscordStalenessConfig; | ||
| owner: object | undefined; | ||
| message: DiscordMessage; | ||
| startSequence: number; | ||
| content: Content; | ||
| }): DiscordStalenessDecision { | ||
| const { config, owner, message, startSequence, content } = options; | ||
| if (!config.enabled || config.behavior === "ignore") { | ||
| return { | ||
| shouldSend: true, | ||
| stale: false, | ||
| messagesSinceTurnStart: 0, | ||
| behavior: config.behavior, | ||
| }; | ||
| } | ||
|
|
||
| const currentSequence = getDiscordChannelMessageSequence( | ||
| owner, | ||
| message.channel?.id, | ||
| ); | ||
| const messagesSinceTurnStart = Math.max(0, currentSequence - startSequence); | ||
| const stale = messagesSinceTurnStart > config.threshold; | ||
| if (!stale) { | ||
| return { | ||
| shouldSend: true, | ||
| stale: false, | ||
| messagesSinceTurnStart, | ||
| behavior: config.behavior, | ||
| }; | ||
| } | ||
|
|
||
| if (config.behavior === "skip") { | ||
| return { | ||
| shouldSend: false, | ||
| stale: true, | ||
| messagesSinceTurnStart, | ||
| behavior: config.behavior, | ||
| }; | ||
| } | ||
|
|
||
| if ( | ||
| config.behavior === "tag" && | ||
| typeof content.text === "string" && | ||
| content.text.trim().length > 0 && | ||
| !/^(\s*\(catching up:\))/i.test(content.text) | ||
| ) { | ||
| content.text = `(catching up:) ${content.text}`; | ||
| } | ||
|
|
||
| return { | ||
| shouldSend: true, | ||
| stale: true, | ||
| messagesSinceTurnStart, | ||
| behavior: config.behavior, | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stalenessStartSequenceis captured once before the callback is defined, andapplyDiscordStalenessGuardis evaluated on every individual callback invocation. For a slow-reasoning agent that fires the callback multiple times (multi-part response), if the channel moves from within-threshold to beyond-threshold between the first and second invocations, the first chunk is already sent to Discord and cannot be unsent, but subsequent chunks are suppressed. The user receives a visibly truncated message — exactly the worst-case outcome in a busy channel, which is precisely the scenario this feature targets.Since Discord messages cannot be retracted after sending, the only robust mitigation for
behavior=skipwould be to buffer all callback output until reasoning completes and gate the send on a final staleness evaluation, rather than checking per-chunk.