Repository navigation
Tell the sender when a known mention is outside the pod #2552
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3539d07
fix(mentions): tell sender when known handle is outside pod
lilyshen0722 81a9967
fix(i18n): localize sender mention feedback
lilyshen0722 569e16f
fix(mentions): share routed alias map with sender feedback
lilyshen0722 f4a55eb
fix(mentions): omit empty human notice metadata
lilyshen0722 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
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
64 changes: 64 additions & 0 deletions
64
backend/__tests__/unit/services/mentionNonMemberNoticeService.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,64 @@ | ||
| jest.mock('../../../models/Pod', () => ({ findById: jest.fn() })); | ||
| jest.mock('../../../models/User', () => ({ find: jest.fn() })); | ||
| jest.mock('../../../models/AgentProfile', () => ({ find: jest.fn() })); | ||
| jest.mock('../../../models/AgentRegistry', () => ({ | ||
| AgentInstallation: { find: jest.fn() }, | ||
| })); | ||
|
|
||
| const Pod = require('../../../models/Pod'); | ||
| const User = require('../../../models/User'); | ||
| const AgentProfile = require('../../../models/AgentProfile'); | ||
| const { AgentInstallation } = require('../../../models/AgentRegistry'); | ||
| const { findNonMemberMentionNotices } = require('../../../services/mentionNonMemberNoticeService'); | ||
|
|
||
| const rows = (value) => ({ select: () => ({ lean: async () => value }) }); | ||
|
|
||
| describe('non-member mention feedback', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| Pod.findById.mockReturnValue(rows({ members: ['member-user'] })); | ||
| User.find.mockReturnValue(rows([ | ||
| { _id: 'member-user', username: 'sam' }, | ||
| { _id: 'other-user', username: 'Lily' }, | ||
| ])); | ||
| AgentInstallation.find.mockReturnValue(rows([ | ||
| { podId: 'pod-1', agentName: 'nova', instanceId: 'nova', displayName: 'Nova' }, | ||
| { podId: 'pod-2', agentName: 'rhea', instanceId: 'rhea', displayName: 'Rhea' }, | ||
| { podId: 'pod-2', agentName: 'scout', instanceId: 'scout', displayName: '' }, | ||
| ])); | ||
| AgentProfile.find.mockReturnValue(rows([ | ||
| { podId: 'pod-2', agentName: 'scout', instanceId: 'scout', name: 'Friendly Scout' }, | ||
| ])); | ||
| }); | ||
|
|
||
| it('notifies for known outsiders but not members or unknown handles', async () => { | ||
| const notices = await findNonMemberMentionNotices( | ||
| 'pod-1', '@rhea @lily @nova @sam @notarealhandle @rhea @guide @friendly-scout', | ||
| ); | ||
| expect(notices).toEqual([ | ||
| { code: 'MENTION_NOT_IN_POD', handle: 'rhea', message: "rhea isn't in this pod. Invite them, or ask in a pod they're in." }, | ||
| { code: 'MENTION_NOT_IN_POD', handle: 'lily', message: "lily isn't in this pod. Invite them, or ask in a pod they're in." }, | ||
| { code: 'MENTION_NOT_IN_POD', handle: 'guide', message: "guide isn't in this pod. Invite them, or ask in a pod they're in." }, | ||
| { code: 'MENTION_NOT_IN_POD', handle: 'friendly-scout', message: "friendly-scout isn't in this pod. Invite them, or ask in a pod they're in." }, | ||
| ]); | ||
| expect(User.find).toHaveBeenCalledWith({ | ||
| $or: expect.arrayContaining([{ username: /^lily$/i }]), | ||
| }); | ||
| }); | ||
|
|
||
| it('skips all lookups when there is no mention', async () => { | ||
| expect(await findNonMemberMentionNotices('pod-1', 'hello')).toEqual([]); | ||
| expect(Pod.findById).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('recognizes an in-pod legacy alias and profile display slug as delivered', async () => { | ||
| AgentInstallation.find.mockReturnValue(rows([ | ||
| { podId: 'pod-1', agentName: 'scout', instanceId: 'scout', displayName: '' }, | ||
| ])); | ||
| AgentProfile.find.mockReturnValue(rows([ | ||
| { podId: 'pod-1', agentName: 'scout', instanceId: 'scout', name: 'Friendly Scout' }, | ||
| ])); | ||
|
|
||
| expect(await findNonMemberMentionNotices('pod-1', '@guide @friendly-scout')).toEqual([]); | ||
| }); | ||
| }); |
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
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,86 @@ | ||
| // A sender can address only pod members. When a real handle names someone | ||
| // outside this pod, return sender-only feedback without exposing where they are. | ||
| // eslint-disable-next-line global-require | ||
| const Pod = require('../models/Pod'); | ||
| // eslint-disable-next-line global-require | ||
| const User = require('../models/User'); | ||
| // eslint-disable-next-line global-require | ||
| const { AgentInstallation } = require('../models/AgentRegistry'); | ||
| // eslint-disable-next-line global-require | ||
| const AgentProfile = require('../models/AgentProfile'); | ||
| // eslint-disable-next-line global-require | ||
| const { buildMentionMap } = require('./agentMentionService'); | ||
|
|
||
| export interface MentionNotice { | ||
| code: 'MENTION_NOT_IN_POD'; | ||
| handle: string; | ||
| message: string; | ||
| } | ||
|
|
||
| const mentionedHandles = (content: string): string[] => { | ||
| const handles = new Set<string>(); | ||
| const regex = /@([a-z0-9_-]{2,})/gi; | ||
| let match: RegExpExecArray | null; | ||
| while ((match = regex.exec(content)) !== null && handles.size < 20) { | ||
| handles.add(match[1].toLowerCase()); | ||
| } | ||
| return [...handles]; | ||
| }; | ||
|
|
||
| const exactHandle = (handle: string): RegExp => new RegExp(`^${handle}$`, 'i'); | ||
|
|
||
| export const findNonMemberMentionNotices = async ( | ||
| podId: string, | ||
| content: string, | ||
| ): Promise<MentionNotice[]> => { | ||
| const handles = mentionedHandles(content); | ||
| if (handles.length === 0) return []; | ||
|
|
||
| const [pod, users, installations, profiles] = await Promise.all([ | ||
| Pod.findById(podId).select('members').lean(), | ||
| // Usernames retain their original case at write time. Mention parsing | ||
| // lowercases them, so use the same anchored case-insensitive match as | ||
| // human mention routing. | ||
| User.find({ $or: handles.map((handle) => ({ username: exactHandle(handle) })) }) | ||
| .select('_id username').lean(), | ||
| // Display-name slugs and instance handles are routable in the pod, so | ||
| // inspect the same active installation records rather than only names. | ||
| AgentInstallation.find({ status: 'active' }) | ||
| .select('podId agentName instanceId displayName').lean(), | ||
| AgentProfile.find({}).select('podId agentName instanceId name').lean(), | ||
| ]); | ||
| if (!pod) return []; | ||
|
|
||
| const members = new Set((pod.members || []).map((id: unknown) => String(id))); | ||
| const known = new Set<string>(); | ||
| const inPod = new Set<string>(); | ||
| for (const user of users || []) { | ||
| const handle = String(user.username || '').toLowerCase(); | ||
| known.add(handle); | ||
| if (members.has(String(user._id))) inPod.add(handle); | ||
| } | ||
| const installationsByPod = new Map<string, Array<Record<string, unknown>>>(); | ||
| for (const installation of installations || []) { | ||
| const installationPodId = String(installation.podId); | ||
| const list = installationsByPod.get(installationPodId) || []; | ||
| list.push(installation); | ||
| installationsByPod.set(installationPodId, list); | ||
| } | ||
| for (const [installationPodId, podInstallations] of installationsByPod) { | ||
| const podProfiles = (profiles || []).filter( | ||
| (profile: { podId: unknown }) => String(profile.podId) === installationPodId, | ||
| ); | ||
| const { map } = buildMentionMap(podInstallations, podProfiles); | ||
| for (const handle of map.keys()) { | ||
| known.add(handle); | ||
| if (installationPodId === String(podId)) inPod.add(handle); | ||
| } | ||
| } | ||
|
|
||
| return handles.filter((handle) => known.has(handle) && !inPod.has(handle)) | ||
| .map((handle) => ({ | ||
| code: 'MENTION_NOT_IN_POD' as const, | ||
| handle, | ||
| message: `${handle} isn't in this pod. Invite them, or ask in a pod they're in.`, | ||
| })); | ||
| }; |
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.
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.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: Team-Commonly/commonly
Length of output: 7767
🏁 Script executed:
Repository: Team-Commonly/commonly
Length of output: 15755
🏁 Script executed:
Repository: Team-Commonly/commonly
Length of output: 6099
🌐 Web query:
Mongoose 7.0.3 official documentation bufferTimeoutMS serverSelectionTimeoutMS socketTimeoutMS connectTimeoutMS defaults💡 Result:
Do not block mention delivery on the advisory Mongo lookup.
deliverMessageToAgentsawaitssenderNoticesbeforeenqueueMentions. The lookup runs four Mongoose queries, and its rejection fallback does not handle a pending query. Production does not configure an operation or socket timeout. A stalled lookup can therefore leave the stored message without mention delivery and keep the POST pending until the caller times out.Start mention delivery before the advisory lookup, then apply a bounded wait to the advisory result.
agentsRuntime.tsalready starts enqueueing before awaiting its notice lookup, so this finding applies todeliverMessageToAgents.🤖 Prompt for AI Agents