Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(bot-wake): owner presence no longer silences a message that names the bot (#21679)

Arvin Xu711f676f4bd9
diff --git a/apps/server/src/services/agentSignal/scenarios/__tests__/botWakePolicies.test.ts b/apps/server/src/services/agentSignal/scenarios/__tests__/botWakePolicies.test.ts
index 530cce6a4fe..42240d4df9e 100644
--- a/apps/server/src/services/agentSignal/scenarios/__tests__/botWakePolicies.test.ts
+++ b/apps/server/src/services/agentSignal/scenarios/__tests__/botWakePolicies.test.ts
@@ -328,6 +328,46 @@ describe('owner rules policy', () => {
});
});
+describe('owner rules policy: naming the bot', () => {
+ const rules = rulesFor({ rules: { ownerPresenceSeconds: 120 } });
+ const conversation = { botNames: ['Nasa'], recent: [] };
+ const run = (
+ s: Partial<WakeSignal>,
+ c: Partial<WakeChannelState>,
+ botWake: Record<string, unknown> = { rules: { ownerPresenceSeconds: 120 } },
+ ) =>
+ createOwnerRulesPolicy('enforce', rulesFor(botWake)).evaluate({
+ signal: signal(s),
+ state: { ...snapshot(c), conversation },
+ });
+
+ it('does not let owner presence silence someone else calling the bot by name', () => {
+ const result = run({ message: 'Nasa ?' }, { ownerLastSpokeAt: NOW - 10_000 });
+
+ expect([result.verdict, result.reason]).toEqual(['abstain', 'no-owner-rule-opinion']);
+ });
+
+ it('still suppresses unnamed chatter while the owner is around', () => {
+ const result = createOwnerRulesPolicy('enforce', rules).evaluate({
+ signal: signal({ message: 'anyone know why the deploy is slow?' }),
+ state: { ...snapshot({ ownerLastSpokeAt: NOW - 10_000 }), conversation },
+ });
+
+ expect([result.verdict, result.reason]).toEqual(['deny', 'owner-present']);
+ });
+
+ it('keeps scope and the channel lists in force when the bot is named', () => {
+ const named = { message: 'Nasa hi' };
+ const recent = { ownerLastSpokeAt: NOW - 10_000 };
+
+ expect(run(named, recent, { rules: { scope: 'none' } }).reason).toBe('scope-none');
+ expect(run(named, recent, { rules: { denyChannels: ['c1'] } }).reason).toBe('channel-denied');
+ expect(run(named, recent, { rules: { allowChannels: ['c2'] } }).reason).toBe(
+ 'channel-not-allowed',
+ );
+ });
+});
+
describe('classifier policy', () => {
const rules = rulesFor({
classifier: { mode: 'enforce', threshold: 0.5 },
diff --git a/apps/server/src/services/agentSignal/scenarios/botWake/policies/ownerRules.ts b/apps/server/src/services/agentSignal/scenarios/botWake/policies/ownerRules.ts
index baaa601afed..b97aa9e474c 100644
--- a/apps/server/src/services/agentSignal/scenarios/botWake/policies/ownerRules.ts
+++ b/apps/server/src/services/agentSignal/scenarios/botWake/policies/ownerRules.ts
@@ -1,3 +1,4 @@
+import { matchesBotName } from '../conversation';
import type { WakePolicy, WakePolicyMode, WakePolicyResult, WakeRules } from '../types';
export const OWNER_RULES_POLICY_ID = 'bot-wake:owner-rules';
@@ -13,6 +14,10 @@ const deny = (reason: string): WakePolicyResult => ({
* The bot owner's structured rules: scope, channel allow/deny lists, follow-up window, the
* silent-wake budget and owner-presence suppression.
*
+ * Owner presence keeps the bot from joining chatter on its own while the owner is around to
+ * answer. A message that calls the bot by name asked for the bot, not the owner, so presence does
+ * not silence it. Scope and the channel lists still do: those say where the bot may speak at all.
+ *
* Free-text instructions are deliberately not read here. They are context for the classifier;
* compiling prose into patterns would make the rule set say something the owner never wrote.
*
@@ -21,7 +26,7 @@ const deny = (reason: string): WakePolicyResult => ({
*
* Returns:
* - `deny` with the first rule that matched (checked in a fixed order: scope, deny list, allow
- * list, window, budget, owner presence)
+ * list, window, budget, owner presence; owner presence is skipped when the text names the bot)
* - `allow` when the bot spoke within the follow-up window and nothing denied
* - `abstain` otherwise: outside the window the owner's rules have no opinion
*/
@@ -55,7 +60,8 @@ export const createOwnerRulesPolicy = (mode: WakePolicyMode, rules: WakeRules):
rules.ownerPresenceSeconds > 0 &&
channel?.ownerLastSpokeAt !== undefined &&
!signal.author?.isOwner &&
- now - channel.ownerLastSpokeAt < rules.ownerPresenceSeconds * 1000
+ now - channel.ownerLastSpokeAt < rules.ownerPresenceSeconds * 1000 &&
+ !matchesBotName(signal.message, state.conversation?.botNames ?? [])
) {
return deny('owner-present');
}
@@ -78,6 +84,6 @@ export const createOwnerRulesPolicy = (mode: WakePolicyMode, rules: WakeRules):
},
id: OWNER_RULES_POLICY_ID,
mode,
- requires: ['channel'],
- version: 1,
+ requires: ['channel', 'conversation'],
+ version: 2,
});
diff --git a/apps/server/src/services/bot/BotMessageRouter/wakeDecision.initiate.test.ts b/apps/server/src/services/bot/BotMessageRouter/wakeDecision.initiate.test.ts
index 3d897f3228b..83ed5812ddd 100644
--- a/apps/server/src/services/bot/BotMessageRouter/wakeDecision.initiate.test.ts
+++ b/apps/server/src/services/bot/BotMessageRouter/wakeDecision.initiate.test.ts
@@ -151,7 +151,6 @@ describe('bot-wake recorder: initiating a wake nobody addressed', () => {
it.each([
{ reason: 'scope none', rules: { scope: 'none' } },
{ reason: 'channel denied', rules: { denyChannels: ['c1'] } },
- { reason: 'owner present', rules: { ownerPresenceSeconds: 600 } },
])('an owner rule still vetoes a message that names the bot ($reason)', async ({ rules }) => {
const state = fakeState({ ...spokeRecently(), ownerLastSpokeAt: Date.now() - 1000 });
const recorder = make({ botWake: { enabled: true, mode: 'enforce', rules } }, state);
@@ -163,6 +162,20 @@ describe('bot-wake recorder: initiating a wake nobody addressed', () => {
).resolves.toBeUndefined();
});
+ it('wakes when someone else names the bot while the owner is around', async () => {
+ const state = fakeState({ ownerLastSpokeAt: Date.now() - 10_000, silentWakes: 0 });
+ const recorder = make(
+ { botWake: { enabled: true, mode: 'enforce', rules: { ownerPresenceSeconds: 120 } } },
+ state,
+ );
+
+ await expect(
+ recorder.initiate(
+ observation({}, { author: { isBot: false, isMe: false, userId: 'someone-else' } }),
+ ),
+ ).resolves.toEqual({ reason: 'named-in-text' });
+ });
+
it('never wakes on its own message or another bot, even naming itself', async () => {
const recorder = make(ENFORCE, fakeState());
@@ -355,6 +368,26 @@ describe('bot-wake recorder: initiating a wake nobody addressed', () => {
expect(decision.metadata.stateSnapshot.conversation.botNames).toEqual([BOT_NAME]);
});
+ it('records the enforcing rule that stopped a named wake, not a shadow replay of it', async () => {
+ const recorder = make(
+ { botWake: { enabled: true, mode: 'enforce', rules: { denyChannels: ['c1'] } } },
+ fakeState(),
+ );
+
+ expect(await recorder.initiate(observation())).toBeUndefined();
+ recorder.record(observation());
+ await flush();
+
+ const verdicts = recorded()[0].verdicts.map((verdict: any) => [
+ verdict.policyId,
+ verdict.mode,
+ verdict.verdict,
+ verdict.reason,
+ ]);
+ expect(verdicts).toContainEqual(['bot-wake:owner-rules', 'enforce', 'deny', 'channel-denied']);
+ expect(verdicts.some(([, mode]: string[]) => mode === 'shadow')).toBe(false);
+ });
+
it('a message that did not wake the bot is still recorded as it was judged', async () => {
const state = fakeState();
const classify = classifierAnswer(0.3);
diff --git a/apps/server/src/services/bot/BotMessageRouter/wakeDecision.ts b/apps/server/src/services/bot/BotMessageRouter/wakeDecision.ts
index cb49ff70916..7f922c32d91 100644
--- a/apps/server/src/services/bot/BotMessageRouter/wakeDecision.ts
+++ b/apps/server/src/services/bot/BotMessageRouter/wakeDecision.ts
@@ -662,7 +662,13 @@ export const createBotWakeRecorder = (options: BotWakeRecorderOptions): BotWakeR
const ruleDecision = await aggregateWakeVerdictsAsync(rulePolicies, signal, snapshot);
if (ruleDecision.verdict !== 'allow' || ruleDecision.fastPath) {
- keepEvaluation(observation, { channel, conversation });
+ // An enforcing deny is what kept the bot quiet: keep its verdicts, so the row names the
+ // rule that stopped the wake instead of replaying every policy as if it only watched.
+ keepEvaluation(observation, {
+ channel,
+ conversation,
+ verdicts: ruleDecision.verdict === 'deny' ? ruleVerdicts(ruleDecision) : undefined,
+ });
return;
}