Repository navigation
feat(mentions): say once in the room when a never-connected agent is mentioned (TASK-655, supersedes #3087) #3101
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
+290
−0
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
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
backend/__tests__/unit/services/neverConnectedMentionService.test.js
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,107 @@ | ||
| // TASK-655 item 3: a person's @mention of a Bring your own agent that has never | ||
| // polled gets one system line in the room, at most one per agent per UTC day. | ||
|
|
||
| const mockFindUser = jest.fn(); | ||
| const mockFindInstall = jest.fn(); | ||
| const mockFindBot = jest.fn(); | ||
| const mockEmitNotConnected = jest.fn(); | ||
| const mockHasLineToday = jest.fn(); | ||
|
|
||
| jest.mock('../../../models/User', () => ({ | ||
| findById: (...args) => mockFindUser(...args), | ||
| findOne: (...args) => mockFindBot(...args), | ||
| })); | ||
| jest.mock('../../../models/AgentRegistry', () => ({ | ||
| AgentInstallation: { findOne: (...args) => mockFindInstall(...args) }, | ||
| })); | ||
| jest.mock('../../../services/systemActionLineService', () => ({ | ||
| emitNotConnectedLine: (...args) => mockEmitNotConnected(...args), | ||
| hasNotConnectedLineToday: (...args) => mockHasLineToday(...args), | ||
| })); | ||
|
|
||
| const chain = (value) => ({ select: () => ({ lean: async () => value }) }); | ||
| const leanOnly = (value) => ({ lean: async () => value }); | ||
|
|
||
| const { noteNeverConnectedMention } = require('../../../services/neverConnectedMentionService'); | ||
|
|
||
| const NOW = new Date('2026-10-31T20:00:00.000Z'); | ||
| const byoInstall = { | ||
| agentName: 'nova', | ||
| instanceId: 'default', | ||
| installedBy: 'owner-1', | ||
| status: 'active', | ||
| config: { runtime: { host: 'byo', runtimeType: 'openclaw' } }, | ||
| runtimeTokens: [], | ||
| }; | ||
| const botRow = (tokens = []) => ({ | ||
| _id: 'bot-1', | ||
| username: 'nova', | ||
| profilePicture: null, | ||
| botMetadata: { displayName: 'Nova', instanceId: 'default' }, | ||
| agentRuntimeTokens: tokens, | ||
| }); | ||
| const args = { | ||
| podId: 'pod-1', | ||
| agentName: 'nova', | ||
| instanceId: 'default', | ||
| senderId: 'human-1', | ||
| messageId: '555', | ||
| threadRootId: null, | ||
| now: NOW, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockFindUser.mockReturnValue(chain({ isBot: false })); | ||
| mockFindInstall.mockReturnValue(leanOnly(byoInstall)); | ||
| mockFindBot.mockReturnValue(chain(botRow([]))); | ||
| mockHasLineToday.mockResolvedValue(false); | ||
| mockEmitNotConnected.mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| it('posts one not-connected line when a person mentions a BYO agent that never polled', async () => { | ||
| const posted = await noteNeverConnectedMention(args); | ||
|
|
||
| expect(posted).toBe(true); | ||
| expect(mockHasLineToday).toHaveBeenCalledWith('pod-1', 'nova:default', NOW); | ||
| expect(mockEmitNotConnected).toHaveBeenCalledTimes(1); | ||
| expect(mockEmitNotConnected.mock.calls[0][0]).toMatchObject({ | ||
| podId: 'pod-1', | ||
| actorUserId: 'bot-1', | ||
| agentKey: 'nova:default', | ||
| messageId: '555', | ||
| }); | ||
| }); | ||
|
|
||
| it('stays silent for a bot sender', async () => { | ||
| mockFindUser.mockReturnValue(chain({ isBot: true })); | ||
| expect(await noteNeverConnectedMention(args)).toBe(false); | ||
| expect(mockEmitNotConnected).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('stays silent once the agent has a live credential use', async () => { | ||
| mockFindBot.mockReturnValue(chain(botRow([{ lastUsedAt: new Date(NOW.getTime() - 60 * 1000) }]))); | ||
| expect(await noteNeverConnectedMention(args)).toBe(false); | ||
| expect(mockEmitNotConnected).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('stays silent when a line for this agent already sits in the pod today', async () => { | ||
| mockHasLineToday.mockResolvedValue(true); | ||
| expect(await noteNeverConnectedMention(args)).toBe(false); | ||
| expect(mockEmitNotConnected).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('stays silent when there is no active install', async () => { | ||
| mockFindInstall.mockReturnValue(leanOnly(null)); | ||
| expect(await noteNeverConnectedMention(args)).toBe(false); | ||
| expect(mockEmitNotConnected).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('posts one line when the same agent is mentioned twice at once', async () => { | ||
| const [a, b] = await Promise.all([ | ||
| noteNeverConnectedMention(args), | ||
| noteNeverConnectedMention(args), | ||
| ]); | ||
| expect([a, b].filter(Boolean)).toHaveLength(1); | ||
| expect(mockEmitNotConnected).toHaveBeenCalledTimes(1); | ||
| }); |
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,108 @@ | ||
| import { emitNotConnectedLine, hasNotConnectedLineToday } from './systemActionLineService'; | ||
| import { deriveAgentState } from './agentStateService'; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const User = require('../models/User'); | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const { AgentInstallation } = require('../models/AgentRegistry'); | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const AgentIdentityService = require('./agentIdentityService'); | ||
|
|
||
| /** | ||
| * TASK-655 item 3. A person @mentions a Bring your own agent that has never | ||
| * polled. The mention still queues (it is delivered the moment the agent | ||
| * connects), but the room now says so once, as a reply to the mention, so the | ||
| * person is not left waiting on silence. | ||
| * | ||
| * ONE DERIVATION. "Never connected" is `deriveAgentState` returning | ||
| * `never-connected`, the same verdict the Your Team row and the stalled-connect | ||
| * nudge use. There is no second rule here. | ||
| * | ||
| * Silent when: the sender is a bot (agents do not get told they are unseen), | ||
| * the install is not active, the agent is not in the never-connected state, or | ||
| * a line for this agent already sits in the pod today. | ||
| */ | ||
| export async function noteNeverConnectedMention({ | ||
| podId, | ||
| agentName, | ||
| instanceId, | ||
| senderId, | ||
| messageId, | ||
| threadRootId, | ||
| now = new Date(), | ||
| }: { | ||
| podId: string; | ||
| agentName: string; | ||
| instanceId: string; | ||
| senderId: string; | ||
| messageId: string | number | null; | ||
| threadRootId?: string | number | null; | ||
| now?: Date; | ||
| }): Promise<boolean> { | ||
| const sender = await User.findById(senderId).select('isBot').lean() as { isBot?: boolean } | null; | ||
| if (!sender || sender.isBot) return false; | ||
|
|
||
| const install = await AgentInstallation.findOne({ | ||
| podId, agentName, instanceId, status: 'active', | ||
| }).lean() as Record<string, unknown> | null; | ||
| if (!install) return false; | ||
|
|
||
| const agent = await User.findOne({ | ||
| isBot: true, | ||
| 'botMetadata.agentName': agentName, | ||
| 'botMetadata.instanceId': instanceId, | ||
| }).select('username profilePicture botMetadata agentRuntimeTokens').lean() as { | ||
| _id: unknown; username?: string; profilePicture?: string | null; | ||
| botMetadata?: { displayName?: string; instanceId?: string }; | ||
| agentRuntimeTokens?: Array<{ lastUsedAt?: Date | string | null }>; | ||
| } | null; | ||
| if (!agent) return false; | ||
|
|
||
| const derived = deriveAgentState( | ||
| install as Parameters<typeof deriveAgentState>[0], | ||
| agent.agentRuntimeTokens || [], | ||
| '', | ||
| now.getTime(), | ||
| ); | ||
| if (derived.state !== 'never-connected') return false; | ||
|
|
||
| const agentKey = `${agentName}:${instanceId}`; | ||
| // The day check is read-then-insert, so two mentions racing in one process | ||
| // (`@nova @nova`) would both pass it. An in-flight key closes that window; | ||
| // across replicas the worst case stays one extra line, never a dropped mention. | ||
| const flightKey = `${podId}:${agentKey}`; | ||
| if (inFlight.has(flightKey)) return false; | ||
| inFlight.add(flightKey); | ||
| try { | ||
| return await postOnce(podId, agentKey, agent, agentName, messageId, threadRootId, now); | ||
| } finally { | ||
| inFlight.delete(flightKey); | ||
| } | ||
| } | ||
|
|
||
| const inFlight = new Set<string>(); | ||
|
|
||
| async function postOnce( | ||
| podId: string, | ||
| agentKey: string, | ||
| agent: { _id: unknown; username?: string; profilePicture?: string | null; botMetadata?: { displayName?: string; instanceId?: string } }, | ||
| agentName: string, | ||
| messageId: string | number | null, | ||
| threadRootId: string | number | null | undefined, | ||
| now: Date, | ||
| ): Promise<boolean> { | ||
| if (await hasNotConnectedLineToday(podId, agentKey, now)) return false; | ||
|
|
||
| await emitNotConnectedLine({ | ||
| podId, | ||
| actorUserId: String(agent._id), | ||
| actorUsername: agent.username || agentName, | ||
| actorAvatarUrl: agent.profilePicture || null, | ||
| actorIsBot: true, | ||
| agentLabel: AgentIdentityService.resolveAgentDisplayLabel(agent, agentName), | ||
| agentKey, | ||
| messageId, | ||
| threadRootId: threadRootId ?? null, | ||
| }); | ||
| return true; | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.