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
search_service
What it is
Part of the Search feature. OpenSearch-backed search API —
per-entity-type search plus a unified search across them — mounted into
document_storage_service, not run standalone.
Start here
src/api.rs+src/api/search.rs: the router; one file per entity type undersrc/api/search/(document.rs,email.rs,chat.rs,channel.rs,calendar_event.rs,call_record.rs,crm_company.rs,project.rs,agent_session.rs), plusunified.rsfor cross-type search.src/api/search/simple/: a second, simpler request/response shape per type used by AI tools — kept in sync with the full one by hand.- Name mode sources: chats (
simple/simple_chat.rs) and channels (simple/simple_channel.rs) name-search in Postgres throughname_search; every other type name-searches in OpenSearch.simple_unified.rsmerges them and keeps one cursor slot per source. src/api/context.rs:SearchHandlerState, the shared handler state.src/outbound.rs+src/outbound/agent_session.rs: the OpenSearch query builders.
Scope
- Owns: query construction against the
models_opensearchindex and the HTTP surface for searching each entity type. - Not here: indexing/backfill (the writer side) or the service binary that
hosts this router →
document_storage_service.
Boundaries
- Depends on:
opensearch_client,models_opensearch,macro_db_client,entity_access,properties,crm,item_filters,calendar_events(domain only, no features, for its Macro call rule and label port). - Used by:
document_storage_service(mountssearch_router); called throughsearch_service_client.
Recipes
- Add search for a new entity type:
src/api/search/<type>.rs(handler) →src/outbound.rs(OpenSearch query) →src/api/search.rs(route) →src/openapi.rs→ a matchingsrc/api/search/simple/simple_<type>.rsif AI tools need it. - Change the unified search ranking/shape:
src/api/search/unified.rsand itssrc/api/search/simple/simple_unified.rscounterpart together.
Checks
cargo test -p search_service(SQLX_OFFLINEunset).SQLX_OFFLINE=true cargo check(just checkrecipe injustfile).
Gotchas
- Every whitespace-split term is ANDed, so a term the standard tokenizer
analyzes to nothing (
-,@) matches no document and empties the search.terms::drop_wordless_termsremoves such terms for exact and partial queries; run new term handling through it. - Calendar events have two scopes (
calendar_event_scopeon the unified request):ownmatches the caller's own and delegated calendars by owner and inbox link;shared_with_youresolves the events shared with the caller's channels in Postgres and searches by id, leaving out meetings the caller holds a live copy of, paired bycalendar_meeting_uid. A snapshot counts there, since theownscope lists it, though it never counts as holding the meeting for the conference link. Enrichment re-checks every hit against the event's access rule. A caller who does not hold the meeting gets no conference link, andcalendar_events'content_without_macro_calltakes the Macro call out of the description; a provider's own join text in the body stays, as it does in a mention preview. Enrichment is the only gate: an id named in theownscope reaches a share as well. The index stores titles, never the description or location, so hits and highlights cannot carry the link. - Agents answer calendar questions from search as often as from
occurrences(), and theownscope matches by inbox link, so an event on a colleague's calendar comes back under the user's own address. Enrichment therefore labels each row with what an occurrence says (calendarName,contributesToAvailability,inbox,role) throughcalendar_events'CalendarEventLabelReader: one read per search, never per row, and a failed read leaves the labels off rather than failing the search, and missing labels mean unknown, never "not theirs".organizerstays the provider's raw value, which web rows show even where a read-only calendar names the user's own mailbox:NameSearch's description is what tells a model that onlyrolesays they organize it. Fields alone did not stop models calling a colleague's event "your appointment";ai_toolsaddscalendar_events'off_schedule_summaryto such a result, and that sentence did. Measure a change to any of these with the default model on rows that are really in the index, and a user whose memory does not describe the behavior under test.
Related
- Rules:
.agents/rules/rust/architecture.md