Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(command): prioritize internal people over CRM contacts (#8012)
gbirman1724919360fa
diff --git a/apps/web/src/features/command/rank-command-search-items.test.ts b/apps/web/src/features/command/rank-command-search-items.test.tsindex e372fd4df1..ad04ab74d0 100644--- a/apps/web/src/features/command/rank-command-search-items.test.ts+++ b/apps/web/src/features/command/rank-command-search-items.test.ts@@ -57,6 +57,42 @@ function project(id: string, name: string, updatedAt = NOW): CommandMenuItem {};}+function contact(+ id: string,+ name: string,+ lastInteraction = NOW+): CommandMenuItem {+ return {+ id,+ kind: 'entity',+ bucket: 'crm_contact',+ searchText: name,+ sortTimestamp: lastInteraction,+ timestamps: { lastInteraction: new Date(lastInteraction) },+ data: {+ id,+ type: 'crm_contact',+ ownerId: 'team',+ companyId: 'company',+ name,+ email: `${id}@example.com`,+ hidden: false,+ },+ };+}++function person(id: string, name: string): CommandMenuItem {+ return {+ id,+ kind: 'user',+ bucket: 'person',+ searchText: name,+ sortTimestamp: 0,+ timestamps: {},+ data: { id, name, email: `${id}@macro.com` },+ };+}+function ids(items: CommandMenuItem[], query: string, cacheEnabled = true) {return rankCommandSearchItems(items, query, {preserveAdditionalEntityMatches: cacheEnabled,@@ -157,4 +193,68 @@ describe('command menu search ranking', () => {}).map((item) => item.id)).toEqual([person.id]);});++ it.each([false, true])(+ 'keeps old DMs and people without activity ahead of recent CRM contacts with GraphQL %s',+ (cacheEnabled) => {+ const items = [+ contact('crm', 'Jack Healey'),+ person('jack', 'Jack Smith'),+ channel('dm', 'Jackson Kustec', NOW - 60 * DAY, 'dm'),+ ];++ expect(ids(items, 'jack', cacheEnabled)).toEqual(['dm', 'jack', 'crm']);+ expect(ids([...items].reverse(), 'jack', cacheEnabled)).toEqual([+ 'dm',+ 'jack',+ 'crm',+ ]);+ }+ );++ it('preserves relevance and recency within People groups and other result positions', () => {+ const items = [+ contact('recent-crm', 'Jack CRM', NOW),+ contact('older-crm', 'Jack CRM', NOW - 10 * DAY),+ command('command', 'Jack command', NOW - 5 * DAY),+ channel('document-slot', 'Jack channel', NOW - 15 * DAY),+ person('jack', 'Jack Smith'),+ channel('old-dm', 'Jack old', NOW - 60 * DAY, 'dm'),+ channel('recent-dm', 'Jack recent', NOW - DAY, 'dm'),+ ];++ expect(ids(items, 'jack')).toEqual([+ 'recent-dm',+ 'old-dm',+ 'command',+ 'jack',+ 'recent-crm',+ 'document-slot',+ 'older-crm',+ ]);+ });++ it('prioritizes a broader cached DM match over a directly matched CRM contact', () => {+ const items = [+ contact('crm', 'Qtr plan'),+ channel('dm', 'Quarterly plan', NOW - 60 * DAY, 'dm'),+ ];++ expect(ids(items, 'qtr')).toEqual(['dm', 'crm']);+ expect(ids(items, 'qtr', false)).toEqual(['crm']);+ });++ it('prioritizes a preserved server-matched person over a directly matched CRM contact', () => {+ const items = [+ contact('crm', 'Qtr plan'),+ person('jack', 'Quarterly plan'),+ ];++ expect(+ rankCommandSearchItems(items, 'qtr', {+ preserveAdditionalEntityMatches: false,+ preservedIds: new Set(['jack']),+ }).map((item) => item.id)+ ).toEqual(['jack', 'crm']);+ });});diff --git a/apps/web/src/features/command/rank-command-search-items.ts b/apps/web/src/features/command/rank-command-search-items.tsindex 59af0ca5f6..bdb16a4093 100644--- a/apps/web/src/features/command/rank-command-search-items.ts+++ b/apps/web/src/features/command/rank-command-search-items.ts@@ -21,6 +21,29 @@ function compareRecency(left: CommandMenuItem, right: CommandMenuItem) {);}+function isPeopleItem(item: CommandMenuItem) {+ return (+ item.bucket === 'dm' ||+ item.bucket === 'person' ||+ item.bucket === 'crm_contact'+ );+}++/** Keep the existing order within each group and the positions of other results. */+function prioritizeInternalPeople(items: CommandMenuItem[]): CommandMenuItem[] {+ const people = items+ .filter(isPeopleItem)+ .sort(+ (left, right) =>+ Number(left.bucket === 'crm_contact') -+ Number(right.bucket === 'crm_contact')+ );+ let nextPerson = 0;+ return items.map((item) =>+ isPeopleItem(item) ? people[nextPerson++] : item+ );+}+/** Rank cached and local candidates together using the command menu's rules. */export function rankCommandSearchItems(items: CommandMenuItem[],@@ -44,7 +67,7 @@ export function rankCommandSearchItems((options.preserveAdditionalEntityMatches &&(item.kind === 'entity' || item.kind === 'initiative'));if (!options.preserveAdditionalEntityMatches && !options.preservedIds?.size)- return ranked;+ return prioritizeInternalPeople(ranked);// Cache search accepts broader subsequences than the menu's fuzzy matcher.// Keep these additional matches reachable after the regular ranked results.@@ -52,5 +75,5 @@ export function rankCommandSearchItems(const additional = items.filter((item) => preserved(item) && !matchedIds.has(item.id)).sort(compareRecency);- return [...ranked, ...additional];+ return prioritizeInternalPeople([...ranked, ...additional]);}diff --git a/apps/web/src/features/command/useCommandItems.contacts.test.ts b/apps/web/src/features/command/useCommandItems.contacts.test.tsindex c0356c31f7..d90ab2fff6 100644--- a/apps/web/src/features/command/useCommandItems.contacts.test.ts+++ b/apps/web/src/features/command/useCommandItems.contacts.test.ts@@ -130,7 +130,7 @@ function setup(initialCategory: CategoryFilter = 'dms') {const [dms, setDms] = createSignal<EntityItem[]>([]);const [hasMore, setHasMore] = createSignal(false);const [loading, setLoading] = createSignal(false);- mocks.lists = { person: users, dm: dms };+ mocks.lists = { person: users, dm: dms, category: dms };const discovery = {search: undefined as Accessor<string> | undefined,active: undefined as Accessor<boolean> | undefined,@@ -189,6 +189,32 @@ describe('command menu contact discovery', () => {expect(t.ids()).toContain('crm-pat');});+ it.each(['all', 'dms'] as const)(+ 'keeps existing DMs ahead of recent CRM contacts in %s as server results arrive',+ (category) => {+ const t = setup(category);+ const dm = directMessage(+ 'dm-jack',+ 'Jackson Kustec',+ 'macro|[email protected]'+ );+ t.setDms([dm]);+ t.setQuery('jack');+ expect(t.ids()).toEqual([`search:${category}:jack`, dm.id]);++ t.setContacts([+ {+ ...contact('crm-jack', 'Jack Healey', '[email protected]'),+ lastInteraction: new Date().toISOString(),+ },+ ]);+ expect(t.ids()).toEqual([`search:${category}:jack`, dm.id, 'crm-jack']);++ t.setQuery('');+ expect(t.ids()).toEqual([dm.id]);+ }+ );+it('keeps contacts out of unrelated categories', () => {const t = setup('documents');t.setQuery('asher');diff --git a/docs/AGENT_GUIDE/navigation.md b/docs/AGENT_GUIDE/navigation.mdindex 0625bb7748..74783231e1 100644--- a/docs/AGENT_GUIDE/navigation.md+++ b/docs/AGENT_GUIDE/navigation.md@@ -752,7 +752,12 @@ after typing stops and pages 100 contacts at a time, so contacts beyond the firsusual recency lists without contacts. A contact whose email belongs to a Macro userappears as that person instead, even when only the CRM name matched: their existingdirect message when you have one, otherwise the person, and selecting them opens a-direct message. Other contacts show their email and open the contact page; Enter and+direct message. Matching direct messages and Macro users rank ahead of CRM contacts,+even when the conversation is old or has no recorded activity. Relevance and recency+still order results within each group; other result types keep their positions.+Verify this in both People and All with a recent CRM contact and a matching DM last+viewed more than 30 days ago, including when server contacts arrive after the DM.+Other contacts show their email and open the contact page; Enter andclick open in place, Shift+Enter in a new split. A server match that arrives aftertyping takes the default selection from the Search for… row. Global Search does notlist CRM contacts; Search for… from People still opens the Channels search. Verify