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
feat(slack): support importing Slack channel shape into Macro (#7485)
Will Hutchinson3794ae6af14f
diff --git a/.sqlx/query-43b292b87d42ce0041ca9b106d20dcf3be64b1bb5fbbf37854a0a96ff0b0ecf1.json b/.sqlx/query-43b292b87d42ce0041ca9b106d20dcf3be64b1bb5fbbf37854a0a96ff0b0ecf1.jsonnew file mode 100644index 0000000000..9db8d6c793--- /dev/null+++ b/.sqlx/query-43b292b87d42ce0041ca9b106d20dcf3be64b1bb5fbbf37854a0a96ff0b0ecf1.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT user_id FROM team_user WHERE team_id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "user_id",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "43b292b87d42ce0041ca9b106d20dcf3be64b1bb5fbbf37854a0a96ff0b0ecf1"+}diff --git a/.sqlx/query-84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f.json b/.sqlx/query-84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f.jsonnew file mode 100644index 0000000000..6f2dfb1b36--- /dev/null+++ b/.sqlx/query-84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO import_run (user_id, source, status, auto_import)\n VALUES ($1, $2, 'running', false)\n ON CONFLICT (user_id, source) DO UPDATE\n SET status = 'running', auto_import = false, error = NULL, updated_at = NOW()\n WHERE import_run.status::text = ANY($3::text[])\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f"+}diff --git a/Cargo.lock b/Cargo.lockindex 6eddecb5d6..c8363ca39d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -9846,6 +9846,7 @@ dependencies = ["macro_user_id","mcp_select","model-entity",+ "model_user","models_properties","properties","regex",@@ -9857,6 +9858,7 @@ dependencies = ["strum 0.27.2","thiserror 2.0.18","tokio",+ "tower 0.5.3","tracing","utoipa","uuid",@@ -11720,6 +11722,7 @@ dependencies = ["mcp_client","pipedream_mcp","serde_json",+ "tokio","tracing","workspace-hack",]diff --git a/apps/web/src/features/settings/connections/PipedreamAiProvider.tsx b/apps/web/src/features/settings/connections/PipedreamAiProvider.tsxindex 490386c21c..1165c1f89e 100644--- a/apps/web/src/features/settings/connections/PipedreamAiProvider.tsx+++ b/apps/web/src/features/settings/connections/PipedreamAiProvider.tsx@@ -1,10 +1,11 @@import { toast } from '@core/component/Toast/Toast';import { createPipedreamCatalogConnect } from '@core/pipedream/catalog';+import { connectSlugForPipedreamApp } from '@core/pipedream/slugs';import {useDeletePipedreamConnectionMutation,useUpdatePipedreamConnectionMutation,} from '@queries/pipedream-connectors';-import { createSignal, type JSX } from 'solid-js';+import { createSignal, type JSX, Show, Suspense } from 'solid-js';import { AiGrantActions } from './ai-grant-actions';import { capabilityFacts } from './capability-row';import {@@ -25,6 +26,7 @@ import {SettingsSection,} from './primitives';import { providerIcon } from './provider-meta';+import { SlackChannelImportCard } from './slack-channel-import/SlackChannelImportCard';import { useConnectionsView } from './view-state';const COPY: Record<@@ -72,7 +74,7 @@ export function PipedreamAiProvider(props: {const native = useNativeMcpActions();const { connect, busy } = createPipedreamCatalogConnect({entry: () => ({- app_slug: props.provider,+ app_slug: connectSlugForPipedreamApp(props.provider),display_name: copy.name,}),onConnected: () => toast.success(`${copy.name} connected`),@@ -95,7 +97,7 @@ export function PipedreamAiProvider(props: {return;}update.mutate(- { app_slug: props.provider, enabled },+ { app_slug: row()?.appSlug ?? props.provider, enabled },{ onError: () => toast.failure('Failed to update connector') });};@@ -126,7 +128,7 @@ export function PipedreamAiProvider(props: {body: `Disconnect ${copy.name}?`,onConfirm: () =>remove.mutate(- { app_slug: props.provider },+ { app_slug: row()?.appSlug ?? props.provider },{onSuccess: () =>toast.success(`Disconnected ${copy.name} from Macro`),@@ -181,6 +183,17 @@ export function PipedreamAiProvider(props: {</IntegrationRow></SettingsCard></SettingsSection>+ <Show+ when={+ props.provider === 'slack' &&+ row()?.mechanism === 'pipedream' &&+ row()?.status === 'connected'+ }+ >+ <Suspense>+ <SlackChannelImportCard />+ </Suspense>+ </Show><DisconnectConfirmDialogrequest={disconnect()}onClose={() => setDisconnect(null)}diff --git a/apps/web/src/features/settings/connections/model.test.ts b/apps/web/src/features/settings/connections/model.test.tsnew file mode 100644index 0000000000..d49c1baf51--- /dev/null+++ b/apps/web/src/features/settings/connections/model.test.ts@@ -0,0 +1,114 @@+import type { PipedreamConnectionResponse } from '@service-cognition/client';+import { describe, expect, it } from 'vitest';+import { capabilitiesFor, toConnectionsModel } from './model';++function connection(+ appSlug: string,+ enabled = true+): PipedreamConnectionResponse {+ return { app_slug: appSlug, server_name: 'Connected account', enabled };+}++describe('Pipedream connections model', () => {+ it.each(['slack', 'slack_v2'])(+ 'presents %s as Slack and preserves its stored slug',+ (appSlug) => {+ const model = toConnectionsModel({+ pipedream: [connection(appSlug)],+ nativeMcp: [],+ });++ expect(capabilitiesFor(model, 'slack')).toEqual([+ expect.objectContaining({+ id: 'slack-ai',+ provider: 'slack',+ title: 'Slack',+ account: 'Connected account',+ status: 'connected',+ mechanism: 'pipedream',+ appSlug,+ }),+ ]);+ expect(model.providers).toEqual([+ expect.objectContaining({ id: 'slack', name: 'Slack', ready: 1 }),+ ]);+ expect(model.leftovers).toEqual([]);+ }+ );++ it('preserves disabled Slack connections', () => {+ const model = toConnectionsModel({+ pipedream: [connection('slack_v2', false)],+ nativeMcp: [],+ });+ expect(capabilitiesFor(model, 'slack')[0]).toMatchObject({+ status: 'off',+ appSlug: 'slack_v2',+ });+ expect(model.providers[0].ready).toBe(0);+ });++ describe.each([+ ['slack', 'slack_v2'],+ ['slack_v2', 'slack'],+ ])('alias input order: %s, %s', (first, second) => {+ it.each([+ [true, true],+ [false, true],+ [true, false],+ [false, false],+ ])(+ 'prefers slack (enabled=%s) and keeps slack_v2 (enabled=%s) manageable',+ (slackEnabled, slackV2Enabled) => {+ const connections = [first, second].map((slug) => ({+ ...connection(slug, slug === 'slack' ? slackEnabled : slackV2Enabled),+ server_name: `${slug} account`,+ }));+ const model = toConnectionsModel({+ pipedream: connections,+ nativeMcp: [],+ });+ expect(capabilitiesFor(model, 'slack')).toEqual([+ expect.objectContaining({+ appSlug: 'slack',+ account: 'slack account',+ status: slackEnabled ? 'connected' : 'off',+ }),+ ]);+ expect(model.providers[0].ready).toBe(slackEnabled ? 1 : 0);+ expect(model.leftovers).toEqual([+ {+ kind: 'pipedream',+ id: 'pipedream:slack_v2',+ title: 'slack_v2 account',+ subtitle: 'slack_v2',+ appSlug: 'slack_v2',+ enabled: slackV2Enabled,+ },+ ]);+ }+ );+ });++ it('preserves other curated and non-curated Pipedream apps', () => {+ const model = toConnectionsModel({+ pipedream: [connection('github'), connection('posthog', false)],+ nativeMcp: [],+ });+ expect(capabilitiesFor(model, 'github')[0]).toMatchObject({+ provider: 'github',+ appSlug: 'github',+ status: 'connected',+ });+ expect(model.leftovers).toEqual([+ {+ kind: 'pipedream',+ id: 'pipedream:posthog',+ title: 'Connected account',+ subtitle: 'posthog',+ appSlug: 'posthog',+ enabled: false,+ },+ ]);+ });+});diff --git a/apps/web/src/features/settings/connections/model.ts b/apps/web/src/features/settings/connections/model.tsindex 75381ca0ed..9ae90ad692 100644--- a/apps/web/src/features/settings/connections/model.ts+++ b/apps/web/src/features/settings/connections/model.ts@@ -1,3 +1,4 @@+import { SLACK_PIPEDREAM_SLUGS } from '@core/pipedream/slugs';import type { PipedreamConnectionResponse } from '@service-cognition/client';import type { ServerResponse } from '@service-cognition/generated/schemas';@@ -27,6 +28,8 @@ export type Capability = {scope: CapabilityScope;status: CapabilityStatus;mechanism: CapabilityMechanism;+ /** Stored Pipedream slug used to update or remove this connection. */+ appSlug?: string;/** Native MCP URL, when this row is that server. */sourceUrl?: string;};@@ -155,18 +158,22 @@ function curatedAiAndLeftovers(input: ConnectionsInput): {leftovers: Leftover[];} {const pipedream = pipedreamBySlug(input.pipedream);+ const usedPipedream = new Set<string>();const usedNative = new Set<string>();const capabilities: Capability[] = [];const leftovers: Leftover[] = [];for (const provider of Object.keys(CURATED_AI) as CuratedAiProvider[]) {const copy = CURATED_AI[provider];- const pd = pipedream.get(provider);+ // Match backend alias priority before considering enabled state.+ const slugs = provider === 'slack' ? SLACK_PIPEDREAM_SLUGS : [provider];+ const pd = slugs.map((slug) => pipedream.get(slug)).find((row) => row);const native = input.nativeMcp.find((server) => nativeCuratedProvider(server) === provider);if (pd) {+ usedPipedream.add(pd.app_slug);capabilities.push({id: `${provider}-ai`,kind: 'ai',@@ -177,6 +184,7 @@ function curatedAiAndLeftovers(input: ConnectionsInput): {scope: 'personal',status: aiStatus(pd.enabled),mechanism: 'pipedream',+ appSlug: pd.app_slug,});if (native) {usedNative.add(native.url);@@ -210,7 +218,7 @@ function curatedAiAndLeftovers(input: ConnectionsInput): {}for (const row of input.pipedream) {- if (row.app_slug in CURATED_AI) continue;+ if (usedPipedream.has(row.app_slug)) continue;leftovers.push({kind: 'pipedream',id: `pipedream:${row.app_slug}`,diff --git a/apps/web/src/features/settings/connections/provider-meta.tsx b/apps/web/src/features/settings/connections/provider-meta.tsxindex 203398d530..5f17003d0a 100644--- a/apps/web/src/features/settings/connections/provider-meta.tsx+++ b/apps/web/src/features/settings/connections/provider-meta.tsx@@ -1,4 +1,5 @@import { pipedreamAppAvailableInEnv } from '@core/component/AI/constant/mcpServers';+import { SLACK_PIPEDREAM_SLUGS } from '@core/pipedream/slugs';import GithubIcon from '@icon/mcp-github.svg';import LinearIcon from '@icon/mcp-linear.svg';import NotionIcon from '@icon/mcp-notion.svg';@@ -38,6 +39,7 @@ export const EMPTY_STARTERS: FeaturedStarter[] = [export const PIPEDREAM_BROWSE_HIDDEN_SLUGS: ReadonlySet<string> = new Set([...EMPTY_STARTERS.map((item) => item.id),+ ...SLACK_PIPEDREAM_SLUGS,'cursor',]);diff --git a/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.test.tsx b/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.test.tsxnew file mode 100644index 0000000000..a523b27dd4--- /dev/null+++ b/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.test.tsx@@ -0,0 +1,231 @@+import type { ImportEntity, ImportState } from '@queries/import';+import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';+import { createSignal, type JSX } from 'solid-js';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { SlackChannelImportCard } from './SlackChannelImportCard';++const mocks = vi.hoisted(() => ({+ discover: vi.fn(),+ importChannels: vi.fn(),+ failure: vi.fn(),+}));+const [state, setState] = createSignal<ImportState>({ runs: [], entities: [] });++vi.mock('@core/context/user', () => ({ useUserId: () => () => 'me' }));+vi.mock('@core/component/Toast/Toast', () => ({+ toast: { failure: mocks.failure },+}));+vi.mock('@service-connection/websocket', () => ({+ createConnectionWebsocketEffect: vi.fn(),+}));+vi.mock('@queries/import', async (importOriginal) => ({+ ...(await importOriginal<typeof import('@queries/import')>()),+ useImportQuery: () => ({+ isSuccess: true,+ get data() {+ return state();+ },+ }),+ useDiscoverMutation: () => ({ mutate: mocks.discover, isPending: false }),+ useRunImportMutation: () => ({+ mutate: mocks.importChannels,+ isPending: false,+ }),+}));+// Test the card's controls without unrelated app theme/layout dependencies.+vi.mock('@ui', () => ({+ Button: (props: JSX.ButtonHTMLAttributes<HTMLButtonElement>) => (+ <button {...props} />+ ),+}));+vi.mock('../primitives', () => ({+ SettingsSection: (props: { title: string; children: JSX.Element }) => (+ <section aria-label={props.title}>{props.children}</section>+ ),+ SettingsCard: (props: { children: JSX.Element }) => (+ <div>{props.children}</div>+ ),+}));++function channel(+ id: string,+ overrides: Partial<ImportEntity> = {}+): ImportEntity {+ return {+ id,+ foreign_id: `C${id}`,+ source: 'slack',+ status: 'staged',+ initiator: 'manual',+ user_id: 'me',+ created_at: '',+ updated_at: '',+ metadata: { name: id, members_resolved: true, participants: [] },+ ...overrides,+ };+}++function checkbox(name: string): HTMLInputElement {+ return screen.getByRole('checkbox', { name }) as HTMLInputElement;+}++beforeEach(() => {+ vi.clearAllMocks();+ setState({ runs: [], entities: [] });+});+afterEach(cleanup);++describe('Slack channel import card', () => {+ it('discovers Slack and disables refresh during a running gather', () => {+ render(() => <SlackChannelImportCard />);+ fireEvent.click(screen.getByRole('button', { name: 'Find channels' }));+ expect(mocks.discover).toHaveBeenCalledWith('slack', expect.any(Object));+ setState({+ runs: [+ {+ source: 'slack',+ status: 'running',+ auto_import: false,+ updated_at: '',+ },+ ],+ entities: [],+ });+ expect(+ (screen.getByRole('button', { name: 'Refresh' }) as HTMLButtonElement)+ .disabled+ ).toBe(true);+ expect(+ screen.getByText('Finding channels and checking members…')+ ).toBeTruthy();+ });++ it('selects only visible staged channels, preserves hidden selections and clears on success', () => {+ setState({+ runs: [],+ entities: [+ channel('general'),+ channel('archive', { metadata: { name: 'archive', archived: true } }),+ channel('existing', { status: 'imported' }),+ ],+ });+ render(() => <SlackChannelImportCard />);+ expect(+ screen.queryByRole('checkbox', { name: 'Select archive' })+ ).toBeNull();+ expect(+ screen.queryByRole('checkbox', { name: 'Select existing' })+ ).toBeNull();+ fireEvent.click(checkbox('Select all visible'));+ expect(checkbox('Select general').checked).toBe(true);+ fireEvent.click(checkbox('Show archived'));+ expect(checkbox('Select all visible').indeterminate).toBe(true);+ fireEvent.click(checkbox('Select archive'));+ fireEvent.input(screen.getByRole('searchbox'), {+ target: { value: 'no match' },+ });+ expect(screen.getByText('No matching channels.')).toBeTruthy();+ fireEvent.click(screen.getByRole('button', { name: 'Import 2 channels' }));+ expect(mocks.importChannels).toHaveBeenCalledWith(+ { importIds: ['general', 'archive'], discardIds: [] },+ expect.any(Object)+ );+ mocks.importChannels.mock.calls[0][1].onSuccess();+ expect(+ (+ screen.getByRole('button', {+ name: 'Import 0 channels',+ }) as HTMLButtonElement+ ).disabled+ ).toBe(true);+ });++ it('updates selected rows to importing and then links to a teammate import', () => {+ setState({ runs: [], entities: [channel('general')] });+ render(() => <SlackChannelImportCard />);+ fireEvent.click(checkbox('Select general'));+ setState({+ runs: [],+ entities: [channel('general', { status: 'importing' })],+ });+ expect(screen.getByText('Importing…')).toBeTruthy();+ expect(+ screen.queryByRole('checkbox', { name: 'Select general' })+ ).toBeNull();+ expect(+ (+ screen.getByRole('button', {+ name: 'Import 0 channels',+ }) as HTMLButtonElement+ ).disabled+ ).toBe(true);+ setState({+ runs: [],+ entities: [+ channel('general', {+ status: 'imported',+ user_id: 'teammate',+ entity_id: 'macro-channel',+ entity_type: 'channel',+ }),+ ],+ });+ expect(+ screen+ .getByRole('link', { name: 'Open imported channel general' })+ .getAttribute('href')+ ).toContain('/app/channel/macro-channel');+ expect(screen.getByText('by a teammate', { exact: false })).toBeTruthy();+ });++ it('shows empty and failed discoveries with retry', () => {+ setState({+ runs: [+ {+ source: 'slack',+ status: 'ready',+ auto_import: false,+ updated_at: '',+ },+ ],+ entities: [],+ });+ render(() => <SlackChannelImportCard />);+ expect(screen.getByText('No public channels found')).toBeTruthy();+ setState({+ runs: [+ {+ source: 'slack',+ status: 'failed',+ error: 'Slack is not connected',+ auto_import: false,+ updated_at: '',+ },+ ],+ entities: [],+ });+ expect(screen.getByRole('alert').textContent).toContain(+ 'Slack is not connected'+ );+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));+ expect(mocks.discover).toHaveBeenCalledWith('slack', expect.any(Object));+ mocks.discover.mock.calls[0][1].onError();+ expect(mocks.failure).toHaveBeenCalledWith('Failed to find Slack channels');+ });++ it('retains the selection and shows a toast when importing fails', () => {+ setState({ runs: [], entities: [channel('general')] });+ render(() => <SlackChannelImportCard />);+ fireEvent.click(checkbox('Select general'));+ fireEvent.click(screen.getByRole('button', { name: 'Import 1 channel' }));+ expect(mocks.importChannels).toHaveBeenCalledWith(+ { importIds: ['general'], discardIds: [] },+ expect.any(Object)+ );+ mocks.importChannels.mock.calls[0][1].onError();+ expect(mocks.failure).toHaveBeenCalledWith(+ 'Failed to import Slack channels'+ );+ expect(checkbox('Select general').checked).toBe(true);+ });+});diff --git a/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.tsx b/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.tsxnew file mode 100644index 0000000000..99be928880--- /dev/null+++ b/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.tsx@@ -0,0 +1,296 @@+import { toast } from '@core/component/Toast/Toast';+import { useUserId } from '@core/context/user';+import { buildSimpleEntityUrl } from '@core/util/url';+import SpinnerIcon from '@phosphor/spinner-gap.svg';+import {+ type ImportState,+ useDiscoverMutation,+ useImportQuery,+ useRunImportMutation,+} from '@queries/import';+import { Button } from '@ui';+import {+ createEffect,+ createMemo,+ createSignal,+ For,+ type JSX,+ Show,+} from 'solid-js';+import { SettingsCard, SettingsSection } from '../primitives';+import {+ buildRows,+ filterRows,+ matchLabel,+ runLabel,+ type SlackChannelRow,+ selectableIds,+} from './model';++const EMPTY_STATE: ImportState = { runs: [], entities: [] };++type ChannelRowProps = {+ row: SlackChannelRow;+ selected: boolean;+ disabled: boolean;+ onSelect(selected: boolean): void;+};++function ChannelRow(props: ChannelRowProps): JSX.Element {+ const href = () => {+ const { entityId, entityType } = props.row;+ if (!entityId || !entityType) return undefined;+ return buildSimpleEntityUrl({ id: entityId, type: entityType });+ };++ return (+ <li class="flex items-start gap-3 py-3 text-sm">+ <Show when={props.row.status === 'staged'}>+ <input+ type="checkbox"+ class="mt-1 shrink-0"+ aria-label={`Select ${props.row.name}`}+ checked={props.selected}+ disabled={props.disabled}+ onChange={(event) => props.onSelect(event.currentTarget.checked)}+ />+ </Show>+ <div class="min-w-0 flex-1 break-words">+ <div class="flex flex-wrap items-center gap-2">+ <span class="font-medium">{props.row.name}</span>+ <Show when={props.row.archived}>+ <span class="rounded bg-ink/5 px-1.5 py-0.5 text-xs text-ink-muted">+ Archived+ </span>+ </Show>+ </div>+ <Show when={props.row.purpose}>+ <p class="text-ink-muted">{props.row.purpose}</p>+ </Show>+ <p class="text-xs text-ink-muted">{matchLabel(props.row)}</p>+ <Show when={props.row.status === 'importing'}>+ <span+ class="mt-1 inline-flex items-center gap-1 text-ink-muted"+ role="status"+ >+ <SpinnerIcon class="size-3 animate-spin" aria-hidden="true" />+ Importing…+ </span>+ </Show>+ <Show when={props.row.status === 'imported'}>+ <p class="mt-1 text-ink-muted">+ <Show when={href()} fallback="Imported">+ {(url) => (+ <a+ href={url()}+ class="text-accent underline"+ aria-label={`Open imported channel ${props.row.name}`}+ >+ Imported+ </a>+ )}+ </Show>+ {props.row.importedByTeammate ? ' by a teammate' : ''}+ </p>+ </Show>+ </div>+ </li>+ );+}++export function SlackChannelImportCard(): JSX.Element {+ const userId = useUserId();+ const query = useImportQuery();+ const discover = useDiscoverMutation();+ const importChannels = useRunImportMutation();+ const [search, setSearch] = createSignal('');+ const [showArchived, setShowArchived] = createSignal(false);+ const [selected, setSelected] = createSignal<ReadonlySet<string>>(new Set());+ // Guard resource reads so polling never suspends the surrounding settings page.+ const state = () => (query.isSuccess ? query.data : EMPTY_STATE);+ const run = () => state().runs.find((run) => run.source === 'slack');+ const rows = createMemo(() => buildRows(state(), userId()));+ const visible = createMemo(() =>+ filterRows(rows(), {+ search: search(),+ showArchived: showArchived(),+ })+ );+ const visibleIds = () => selectableIds(visible());+ // Progress updates can make a previously selected row no longer importable.+ const importIds = () =>+ selectableIds(rows()).filter((id) => selected().has(id));+ const allSelected = () =>+ visibleIds().length > 0 && visibleIds().every((id) => selected().has(id));+ const someSelected = () => visibleIds().some((id) => selected().has(id));+ const discovering = () => discover.isPending || run()?.status === 'running';+ let selectAll: HTMLInputElement | undefined;+ // Indeterminate is a DOM property, not an HTML checkbox attribute.+ createEffect(() => {+ if (selectAll) selectAll.indeterminate = someSelected() && !allSelected();+ });++ function select(ids: string[], checked: boolean): void {+ setSelected((previous) => {+ const next = new Set(previous);+ for (const id of ids) {+ if (checked) next.add(id);+ else next.delete(id);+ }+ return next;+ });+ }++ function findChannels(): void {+ discover.mutate('slack', {+ onError: () => toast.failure('Failed to find Slack channels'),+ });+ }++ function startImport(): void {+ const ids = importIds();+ if (!ids.length) return;+ importChannels.mutate(+ { importIds: ids, discardIds: [] },+ {+ onSuccess: () => setSelected(new Set<string>()),+ onError: () => toast.failure('Failed to import Slack channels'),+ }+ );+ }++ return (+ <SettingsSection+ title="Import channels"+ description="Recreate Slack channels in Macro with their name and the members who are on your team. Messages are not imported."+ >+ <SettingsCard>+ <div class="flex flex-col gap-4 p-5 touch:p-4">+ <div class="flex flex-wrap items-center justify-between gap-3">+ <p role="status" class="text-sm text-ink-muted">+ {runLabel(run())}+ </p>+ <Button+ variant="accent"+ size="sm"+ disabled={+ discovering() || !query.isSuccess || importChannels.isPending+ }+ onClick={findChannels}+ >+ <Show when={discovering()}>+ <SpinnerIcon class="size-4 animate-spin" aria-hidden="true" />+ </Show>+ {run() || rows().length > 0 ? 'Refresh' : 'Find channels'}+ </Button>+ </div>+ <Show when={query.isError}>+ <div role="alert" class="text-sm text-failure">+ Could not load Slack import progress.+ <Button+ variant="ghost"+ size="sm"+ onClick={() => void query.refetch()}+ >+ Retry+ </Button>+ </div>+ </Show>+ <Show when={run()?.status === 'failed'}>+ <div role="alert" class="text-sm text-failure break-words">+ {run()?.error || 'Slack channel discovery failed.'}+ <Button+ variant="ghost"+ size="sm"+ disabled={discovering()}+ onClick={findChannels}+ >+ Retry+ </Button>+ </div>+ </Show>+ <label class="flex flex-col gap-1 text-sm">+ Search channels+ <input+ type="search"+ class="rounded border border-edge-muted bg-input p-2 text-ink"+ value={search()}+ onInput={(event) => setSearch(event.currentTarget.value)}+ />+ </label>+ <div class="flex flex-wrap gap-x-5 gap-y-2 text-sm">+ <label class="flex items-center gap-2">+ <input+ type="checkbox"+ checked={showArchived()}+ onChange={(event) =>+ setShowArchived(event.currentTarget.checked)+ }+ />+ Show archived+ </label>+ <label class="flex items-center gap-2">+ <input+ type="checkbox"+ checked={allSelected()}+ ref={selectAll}+ disabled={!visibleIds().length || importChannels.isPending}+ onChange={(event) =>+ select(visibleIds(), event.currentTarget.checked)+ }+ />+ Select all visible+ </label>+ </div>+ <p class="text-sm text-ink-muted">+ {importIds().length} selected. Filtering does not clear selections.+ </p>+ <Show when={rows().length > 0}>+ <ul+ aria-label="Slack channels"+ class="max-h-72 overflow-y-auto rounded border border-edge-muted px-3 divide-y divide-edge-muted"+ >+ <For+ each={visible()}+ fallback={+ <li class="py-3 text-sm text-ink-muted">+ No matching channels.+ </li>+ }+ >+ {(row) => (+ <ChannelRow+ row={row}+ selected={selected().has(row.id)}+ disabled={importChannels.isPending}+ onSelect={(checked) => select([row.id], checked)}+ />+ )}+ </For>+ </ul>+ </Show>+ <Show when={run()?.status === 'ready' && rows().length === 0}>+ <p class="text-sm text-ink-muted">No public channels found</p>+ </Show>+ <div class="flex flex-col items-start gap-3">+ <Button+ variant="accent"+ size="sm"+ disabled={!importIds().length || importChannels.isPending}+ onClick={startImport}+ >+ <Show when={importChannels.isPending}>+ <SpinnerIcon class="size-4 animate-spin" aria-hidden="true" />+ </Show>+ Import {importIds().length}{' '}+ {importIds().length === 1 ? 'channel' : 'channels'}+ </Button>+ <p class="text-xs text-ink-muted">+ Private channels and direct messages are not imported yet.+ </p>+ </div>+ </div>+ </SettingsCard>+ </SettingsSection>+ );+}diff --git a/apps/web/src/features/settings/connections/slack-channel-import/model.test.ts b/apps/web/src/features/settings/connections/slack-channel-import/model.test.tsnew file mode 100644index 0000000000..00a5327300--- /dev/null+++ b/apps/web/src/features/settings/connections/slack-channel-import/model.test.ts@@ -0,0 +1,231 @@+import type { ImportEntity, ImportRun, ImportState } from '@queries/import';+import { describe, expect, it, vi } from 'vitest';+import {+ buildRows,+ filterRows,+ matchLabel,+ runLabel,+ selectableIds,+} from './model';++// Keep the real metadata helper, without starting the query module's websocket.+vi.mock('@service-connection/websocket', () => ({+ createConnectionWebsocketEffect: vi.fn(),+}));++function entity(overrides: Partial<ImportEntity> = {}): ImportEntity {+ return {+ id: 'ledger-1',+ user_id: 'me',+ source: 'slack',+ foreign_id: 'C123',+ status: 'staged',+ initiator: 'manual',+ metadata: {+ name: 'Engineering',+ channel_id: 'C123',+ purpose: 'Ship great software',+ archived: false,+ member_count: 10,+ members_resolved: true,+ participants: [{ name: 'Alice', email: '[email protected]' }],+ },+ created_at: '',+ updated_at: '',+ ...overrides,+ };+}++function run(status: ImportRun['status']): ImportRun {+ return { source: 'slack', status, auto_import: false, updated_at: '' };+}++function state(entities: ImportEntity[], runs: ImportRun[] = []): ImportState {+ return { entities, runs };+}++describe('Slack channel rows', () => {+ it('projects Slack metadata and ignores other sources and discarded rows', () => {+ const rows = buildRows(+ state(+ [+ entity(),+ entity({ source: 'linear' }),+ entity({ source: 'notion' }),+ entity({ status: 'discarded' }),+ ],+ [run('running')]+ ),+ 'me'+ );+ expect(rows).toEqual([+ {+ id: 'ledger-1',+ channelId: 'C123',+ name: 'Engineering',+ purpose: 'Ship great software',+ archived: false,+ memberCount: 10,+ matched: 1,+ membersResolved: true,+ runStatus: 'running',+ status: 'staged',+ entityId: null,+ entityType: null,+ importedByTeammate: false,+ },+ ]);+ });++ it('handles older metadata without treating unchecked members as zero matches', () => {+ const [row] = buildRows(state([entity({ metadata: {} })]), 'me');+ expect(row).toMatchObject({+ channelId: 'C123',+ name: 'C123',+ purpose: '',+ archived: false,+ memberCount: null,+ matched: 0,+ membersResolved: false,+ });+ expect(matchLabel(row)).toBe('Members not checked yet');+ });++ it('marks only imported rows owned by another known user as teammate imports', () => {+ const imported = entity({+ status: 'imported',+ user_id: 'teammate',+ entity_id: 'macro-channel',+ entity_type: 'channel',+ });+ const [row] = buildRows(state([imported]), 'me');+ expect(row).toMatchObject({+ importedByTeammate: true,+ entityId: 'macro-channel',+ entityType: 'channel',+ });+ expect(buildRows(state([imported]), 'teammate')[0].importedByTeammate).toBe(+ false+ );+ expect(buildRows(state([imported]), undefined)[0].importedByTeammate).toBe(+ false+ );+ expect(+ buildRows(state([entity({ user_id: 'teammate' })]), 'me')[0]+ .importedByTeammate+ ).toBe(false);+ });++ it('selects staged ledger ids only, including unresolved channels', () => {+ const rows = buildRows(+ state([+ entity({ metadata: {} }),+ entity({ id: 'importing', status: 'importing' }),+ entity({ id: 'imported', status: 'imported' }),+ ]),+ 'me'+ );+ expect(selectableIds(rows)).toEqual(['ledger-1']);+ expect(selectableIds([])).toEqual([]);+ });+});++describe('channel filtering', () => {+ const rows = buildRows(+ state([+ entity(),+ entity({+ id: 'archived',+ metadata: { name: 'Old Engineering', archived: true },+ }),+ ]),+ 'me'+ );++ it.each([+ 'engineering',+ 'ENGINEERING',+ 'software',+ 'c123',+ ' Engineering ',+ ])(+ 'searches name, purpose and channel id case-insensitively: %s',+ (search) => {+ expect(+ filterRows(rows, { search, showArchived: false }).map((row) => row.id)+ ).toEqual(['ledger-1']);+ }+ );++ it('hides archived channels unless requested without modifying the source rows', () => {+ expect(filterRows(rows, { search: '', showArchived: false })).toHaveLength(+ 1+ );+ expect(filterRows(rows, { search: '', showArchived: true })).toHaveLength(+ 2+ );+ expect(+ filterRows(rows, { search: 'old', showArchived: true }).map(+ (row) => row.id+ )+ ).toEqual(['archived']);+ expect(filterRows(rows, { search: 'missing', showArchived: true })).toEqual(+ []+ );+ expect(rows).toHaveLength(2);+ });+});++describe('progress labels', () => {+ it('distinguishes active checks, unresolved members and resolved match counts', () => {+ const [row] = buildRows(state([entity()]), 'me');+ expect(matchLabel(row)).toBe('1 of 10 members is on your team');+ expect(matchLabel({ ...row, memberCount: null })).toBe(+ '1 member on your team'+ );+ expect(matchLabel({ ...row, matched: 0, memberCount: 0 })).toBe(+ '0 of 0 members are on your team'+ );+ expect(+ matchLabel({ ...row, membersResolved: false, runStatus: 'running' })+ ).toBe('Checking members…');+ for (const runStatus of [+ 'ready',+ 'failed',+ 'completed',+ undefined,+ ] as const) {+ expect(matchLabel({ ...row, membersResolved: false, runStatus })).toBe(+ 'Members not checked yet'+ );+ }+ });++ it.each([+ [0, null, '0 members on your team'],+ [1, null, '1 member on your team'],+ [2, null, '2 members on your team'],+ [0, 1, '0 of 1 member are on your team'],+ [1, 1, '1 of 1 member is on your team'],+ [1, 10, '1 of 10 members is on your team'],+ [2, 10, '2 of 10 members are on your team'],+ ])('labels %s matched members out of %s', (matched, memberCount, label) => {+ const [row] = buildRows(state([entity()]), 'me');+ expect(matchLabel({ ...row, matched, memberCount })).toBe(label);+ });++ it.each([+ ['running', 'Finding channels and checking members…'],+ ['ready', 'Channels ready to import'],+ ['failed', 'Could not find channels'],+ ['importing', 'Importing channels…'],+ ['completed', 'Import complete'],+ ['dismissed', 'Find public Slack channels to import.'],+ ] as const)('labels a %s run', (status, label) => {+ expect(runLabel(run(status))).toBe(label);+ });++ it('prompts discovery when there is no run', () => {+ expect(runLabel(undefined)).toBe('Find public Slack channels to import.');+ });+});diff --git a/apps/web/src/features/settings/connections/slack-channel-import/model.ts b/apps/web/src/features/settings/connections/slack-channel-import/model.tsnew file mode 100644index 0000000000..a9c63e1637--- /dev/null+++ b/apps/web/src/features/settings/connections/slack-channel-import/model.ts@@ -0,0 +1,100 @@+import {+ type ImportEntityStatus,+ type ImportRun,+ type ImportRunStatus,+ type ImportState,+ slackChannelMeta,+} from '@queries/import';+import { match } from 'ts-pattern';++export type SlackChannelRow = {+ id: string;+ channelId: string;+ name: string;+ purpose: string;+ archived: boolean;+ memberCount: number | null;+ matched: number;+ membersResolved: boolean;+ runStatus: ImportRunStatus | undefined;+ status: Exclude<ImportEntityStatus, 'discarded'>;+ entityId: string | null;+ entityType: string | null;+ importedByTeammate: boolean;+};++export function buildRows(+ state: ImportState,+ currentUserId: string | undefined+): SlackChannelRow[] {+ const run = state.runs.find((run) => run.source === 'slack');+ return state.entities.flatMap((entity) => {+ if (entity.source !== 'slack' || entity.status === 'discarded') return [];+ const meta = slackChannelMeta(entity);+ const channelId = meta?.channel_id || entity.foreign_id;+ return [+ {+ id: entity.id,+ channelId,+ name: meta?.name || channelId,+ purpose: meta?.purpose ?? '',+ archived: meta?.archived ?? false,+ memberCount: meta?.member_count ?? null,+ matched: meta?.participants?.length ?? 0,+ membersResolved: meta?.members_resolved ?? false,+ runStatus: run?.status,+ status: entity.status,+ entityId: entity.entity_id ?? null,+ entityType: entity.entity_type ?? null,+ importedByTeammate:+ entity.status === 'imported' &&+ currentUserId !== undefined &&+ entity.user_id !== currentUserId,+ },+ ];+ });+}++export function filterRows(+ rows: readonly SlackChannelRow[],+ options: { search: string; showArchived: boolean }+): SlackChannelRow[] {+ const search = options.search.trim().toLowerCase();+ return rows.filter(+ (row) =>+ (options.showArchived || !row.archived) &&+ [row.name, row.purpose, row.channelId].some((value) =>+ value.toLowerCase().includes(search)+ )+ );+}++export function selectableIds(rows: readonly SlackChannelRow[]): string[] {+ return rows.filter((row) => row.status === 'staged').map((row) => row.id);+}++export function matchLabel(row: SlackChannelRow): string {+ if (!row.membersResolved) {+ return row.runStatus === 'running'+ ? 'Checking members…'+ : 'Members not checked yet';+ }+ if (row.memberCount === null) {+ return `${row.matched} ${row.matched === 1 ? 'member' : 'members'} on your team`;+ }+ const members = row.memberCount === 1 ? 'member' : 'members';+ const verb = row.matched === 1 ? 'is' : 'are';+ return `${row.matched} of ${row.memberCount} ${members} ${verb} on your team`;+}++export function runLabel(run: ImportRun | undefined): string {+ if (!run) return 'Find public Slack channels to import.';+ return match(run.status)+ .with('running', () => 'Finding channels and checking members…')+ .with('ready', () => 'Channels ready to import')+ .with('failed', () => 'Could not find channels')+ .with('importing', () => 'Importing channels…')+ .with('completed', () => 'Import complete')+ .with('dismissed', () => 'Find public Slack channels to import.')+ .exhaustive();+}diff --git a/apps/web/src/features/setup/flow/ConnectorStep.tsx b/apps/web/src/features/setup/flow/ConnectorStep.tsxindex 8ee68e8bee..b15cc58193 100644--- a/apps/web/src/features/setup/flow/ConnectorStep.tsx+++ b/apps/web/src/features/setup/flow/ConnectorStep.tsx@@ -1,6 +1,7 @@import { useAnalytics } from '@app/lib/analytics/analytics-context';import type { FeaturedMcpServer } from '@core/component/AI/constant/mcpServers';import { usePipedreamMcpFlag } from '@core/pipedream/flag';+import { pipedreamSlugsMatch } from '@core/pipedream/slugs';import SpinnerIcon from '@phosphor/spinner-gap.svg';import { useMcpServersQuery } from '@queries/mcp-servers';import { usePipedreamConnectionsQuery } from '@queries/pipedream-connectors';@@ -36,8 +37,8 @@ export function ConnectorStep(props: {serversQuery.data?.find((server) => server.url === props.server.url));const pipedreamRecord = createMemo(() =>- pipedreamQuery.data?.find(- (connection) => connection.app_slug === props.server.app_slug+ pipedreamQuery.data?.find((connection) =>+ pipedreamSlugsMatch(connection.app_slug, props.server.app_slug)));const authenticated = () =>diff --git a/apps/web/src/lib/core/component/AI/constant/mcpServers.ts b/apps/web/src/lib/core/component/AI/constant/mcpServers.tsindex f48f051def..fb565ef70c 100644--- a/apps/web/src/lib/core/component/AI/constant/mcpServers.ts+++ b/apps/web/src/lib/core/component/AI/constant/mcpServers.ts@@ -1,4 +1,9 @@import { DEV_MODE_ENV } from '@core/constant/featureFlags';+import {+ isSlackPipedreamSlug,+ SLACK_CONNECT_SLUG,+ SLACK_PIPEDREAM_SLUGS,+} from '@core/pipedream/slugs';import IconDatadog from '@icon/mcp-datadog.svg';import IconGithub from '@icon/mcp-github.svg';import IconGrafana from '@icon/mcp-grafana.svg';@@ -28,7 +33,7 @@ export const QUICK_CONNECT_SERVERS = [? ([{server_name: 'Slack',- app_slug: 'slack',+ app_slug: SLACK_CONNECT_SLUG,url: 'https://mcp.slack.com/mcp',icon: IconSlack as SvgIcon,},@@ -92,9 +97,13 @@ export const QUICK_CONNECT_ICON_MAP: Map<string, SvgIcon> = new Map();/** Bundled connector icons keyed by Pipedream app slug (Pipedream flow). */-export const PIPEDREAM_ICON_MAP: Map<string, SvgIcon> = new Map(- QUICK_CONNECT_SERVERS.map((s) => [s.app_slug, s.icon])-);+export const PIPEDREAM_ICON_MAP: Map<string, SvgIcon> = new Map([+ ...QUICK_CONNECT_SERVERS.map((s): [string, SvgIcon] => [s.app_slug, s.icon]),+ ...SLACK_PIPEDREAM_SLUGS.map((slug): [string, SvgIcon] => [+ slug,+ IconSlack as SvgIcon,+ ]),+]);/*** Whether a Pipedream catalog app should be offered in this environment.@@ -102,7 +111,7 @@ export const PIPEDREAM_ICON_MAP: Map<string, SvgIcon> = new Map(*/export function pipedreamAppAvailableInEnv(appSlug: string): boolean {if (DEV_MODE_ENV) return true;- return appSlug !== 'slack';+ return !isSlackPipedreamSlug(appSlug);}const SERVER_NAME_ICON_MAP: Map<string, SvgIcon> = new Map(diff --git a/apps/web/src/lib/core/pipedream/slugs.test.ts b/apps/web/src/lib/core/pipedream/slugs.test.tsnew file mode 100644index 0000000000..9a44e8d48c--- /dev/null+++ b/apps/web/src/lib/core/pipedream/slugs.test.ts@@ -0,0 +1,46 @@+import { describe, expect, it } from 'vitest';+import {+ canonicalPipedreamSlug,+ connectSlugForPipedreamApp,+ isSlackPipedreamSlug,+ pipedreamSlugsMatch,+ SLACK_CONNECT_SLUG,+ SLACK_PIPEDREAM_SLUGS,+} from './slugs';++describe('Pipedream slugs', () => {+ it('keeps the documented fallback connect slug first', () => {+ expect(SLACK_PIPEDREAM_SLUGS).toEqual(['slack', 'slack_v2']);+ expect(SLACK_CONNECT_SLUG).toBe(SLACK_PIPEDREAM_SLUGS[0]);+ });++ it.each(SLACK_PIPEDREAM_SLUGS)('recognizes %s as Slack', (slug) => {+ expect(isSlackPipedreamSlug(slug)).toBe(true);+ expect(canonicalPipedreamSlug(slug)).toBe('slack');+ for (const alias of SLACK_PIPEDREAM_SLUGS) {+ expect(pipedreamSlugsMatch(slug, alias)).toBe(true);+ }+ expect(pipedreamSlugsMatch(slug, 'github')).toBe(false);+ expect(pipedreamSlugsMatch('github', slug)).toBe(false);+ });++ it.each(['github', 'linear', '', 'Slack', 'slack_v3'])(+ 'preserves non-Slack slug %j',+ (slug) => {+ expect(isSlackPipedreamSlug(slug)).toBe(false);+ expect(canonicalPipedreamSlug(slug)).toBe(slug);+ expect(connectSlugForPipedreamApp(slug)).toBe(slug);+ expect(pipedreamSlugsMatch(slug, slug)).toBe(true);+ expect(pipedreamSlugsMatch(slug, 'slack')).toBe(false);+ }+ );++ it('does not match different non-Slack apps', () => {+ expect(pipedreamSlugsMatch('github', 'linear')).toBe(false);+ });++ it('connects the Slack provider using the documented slug', () => {+ expect(connectSlugForPipedreamApp('slack')).toBe(SLACK_CONNECT_SLUG);+ expect(connectSlugForPipedreamApp('slack_v2')).toBe('slack_v2');+ });+});diff --git a/apps/web/src/lib/core/pipedream/slugs.ts b/apps/web/src/lib/core/pipedream/slugs.tsnew file mode 100644index 0000000000..bf2089b363--- /dev/null+++ b/apps/web/src/lib/core/pipedream/slugs.ts@@ -0,0 +1,19 @@+// Keep the connect slug first, per docs/SLACK_LIVE_CHANNEL_IMPORT.md.+export const SLACK_PIPEDREAM_SLUGS = ['slack', 'slack_v2'] as const;+export const SLACK_CONNECT_SLUG = SLACK_PIPEDREAM_SLUGS[0];++export function isSlackPipedreamSlug(slug: string): boolean {+ return SLACK_PIPEDREAM_SLUGS.some((alias) => alias === slug);+}++export function canonicalPipedreamSlug(slug: string): string {+ return isSlackPipedreamSlug(slug) ? 'slack' : slug;+}++export function pipedreamSlugsMatch(a: string, b: string): boolean {+ return canonicalPipedreamSlug(a) === canonicalPipedreamSlug(b);+}++export function connectSlugForPipedreamApp(slug: string): string {+ return slug === 'slack' ? SLACK_CONNECT_SLUG : slug;+}diff --git a/apps/web/src/lib/queries/import.ts b/apps/web/src/lib/queries/import.tsindex f569245897..059324ce98 100644--- a/apps/web/src/lib/queries/import.ts+++ b/apps/web/src/lib/queries/import.ts@@ -14,9 +14,14 @@ import {type ImportSource,type ImportState,importClient,+ type SlackChannelMeta,} from '@service-cognition/import';import { createConnectionWebsocketEffect } from '@service-connection/websocket';-import { useMutation, useQuery } from '@tanstack/solid-query';+import {+ type UseMutationResult,+ useMutation,+ useQuery,+} from '@tanstack/solid-query';export type {ImportEntity,@@ -110,6 +115,19 @@ export function useRunImportMutation() {}));}+/** Discover candidates for manual selection. */+export function useDiscoverMutation(): UseMutationResult<+ void,+ Error,+ ImportSource+> {+ return useMutation(() => ({+ mutationFn: (source: ImportSource) =>+ throwOnErr(() => importClient.discover(source)),+ onSuccess: () => void invalidateImportState(),+ }));+}+/** Restart a failed gather run. */export function useRetryGatherMutation() {return useMutation(() => ({@@ -128,6 +146,14 @@ export function useDismissRunMutation() {}));}+/** Slack channel metadata, only for Slack ledger rows. */+export function slackChannelMeta(+ entity: ImportEntity+): SlackChannelMeta | null {+ if (entity.source !== 'slack') return null;+ return entity.metadata as SlackChannelMeta;+}+/** A human label for a ledger row, from its per-source metadata. */export function entityLabel(entity: ImportEntity): string {const field = entity.source === 'slack' ? 'name' : 'title';diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/client.ts b/apps/web/src/lib/service-clients/service-cognition/generated/client.tsindex 46c513e487..fe2c078c42 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/client.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/client.ts@@ -1269,6 +1269,61 @@ export const runImportHandler = async (} as runImportHandlerResponse;};+/**+ * @summary Discover candidates for user selection without auto-importing them.+ */+export type discoverHandlerResponse204 = {+ data: void;+ status: 204;+};++export type discoverHandlerResponse400 = {+ data: void;+ status: 400;+};++export type discoverHandlerResponse500 = {+ data: void;+ status: 500;+};++export type discoverHandlerResponseSuccess = discoverHandlerResponse204 & {+ headers: Headers;+};+export type discoverHandlerResponseError = (+ | discoverHandlerResponse400+ | discoverHandlerResponse500+) & {+ headers: Headers;+};++export type discoverHandlerResponse =+ | discoverHandlerResponseSuccess+ | discoverHandlerResponseError;++export const getDiscoverHandlerUrl = (source: string) => {+ return `/import/runs/${source}/discover`;+};++export const discoverHandler = async (+ source: string,+ options?: RequestInit+): Promise<discoverHandlerResponse> => {+ const res = await fetch(getDiscoverHandlerUrl(source), {+ ...options,+ method: 'POST',+ });++ const body = [204, 205, 304].includes(res.status) ? null : await res.text();++ const data: discoverHandlerResponse['data'] = body ? JSON.parse(body) : {};+ return {+ data,+ status: res.status,+ headers: res.headers,+ } as discoverHandlerResponse;+};+/*** @summary Dismiss one source's import section.*/diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.tsindex 960a2fa5aa..459d7f9228 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts@@ -216,6 +216,7 @@ export * from './sharePermissionV2LinkShareAccessLevel';export * from './sharePermissionV2TeamShareAccessLevel';export * from './slackChannelMeta';export * from './slackChannelMetaChannelId';+export * from './slackChannelMetaMemberCount';export * from './slackChannelMetaPurpose';export * from './slackParticipant';export * from './slackParticipantEmail';diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.tsindex 09577e3a99..1853cf1092 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts@@ -16,4 +16,5 @@ export const Initiator = {onboarding: 'onboarding',chat: 'chat',archive: 'archive',+ manual: 'manual',} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMeta.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMeta.tsindex 75d69a732e..f1034696a5 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMeta.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMeta.ts@@ -5,6 +5,7 @@* OpenAPI spec version: 1.0.0*/import type { SlackChannelMetaChannelId } from './slackChannelMetaChannelId';+import type { SlackChannelMetaMemberCount } from './slackChannelMetaMemberCount';import type { SlackChannelMetaPurpose } from './slackChannelMetaPurpose';import type { SlackParticipant } from './slackParticipant';@@ -12,11 +13,20 @@ import type { SlackParticipant } from './slackParticipant';* Metadata for one staged Slack channel.*/export interface SlackChannelMeta {+ /** Whether Slack has archived this channel. */+ archived?: boolean;/** Slack's channel id (e.g. `C0123456789`), stable across renames. */channel_id?: SlackChannelMetaChannelId;+ /**+ * Total human members in the Slack channel.+ * @minimum 0+ */+ member_count?: SlackChannelMetaMemberCount;+ /** Whether `participants` reflects a live membership read. */+ members_resolved?: boolean;/** Channel name without the leading `#`. */name: string;- /** The channel's most relevant members, when discoverable. */+ /** Slack members matched to the Macro team roster when discovery resolved membership. */participants?: SlackParticipant[];/** The channel's purpose/topic, when set. */purpose?: SlackChannelMetaPurpose;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaMemberCount.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaMemberCount.tsnew file mode 100644index 0000000000..fb85de4753--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaMemberCount.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Total human members in the Slack channel.+ * @minimum 0+ */+export type SlackChannelMetaMemberCount = number | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/import.ts b/apps/web/src/lib/service-clients/service-cognition/import.tsindex eceb9b1024..10e71634ee 100644--- a/apps/web/src/lib/service-clients/service-cognition/import.ts+++ b/apps/web/src/lib/service-clients/service-cognition/import.ts@@ -41,7 +41,7 @@ export type ImportEntityStatus =| 'imported'| 'discarded';-export type ImportInitiator = 'onboarding' | 'chat';+export type ImportInitiator = 'onboarding' | 'chat' | 'archive' | 'manual';export type ImportRunStatus =| 'running'@@ -81,6 +81,9 @@ export type SlackChannelMeta = {channel_id?: string | null;purpose?: string | null;participants?: SlackParticipant[];+ member_count?: number | null;+ archived?: boolean;+ members_resolved?: boolean;};/** One row of the import ledger. */@@ -135,6 +138,13 @@ export const importClient = {});},+ /** Discover candidates for manual selection. */+ async discover(source: ImportSource) {+ return await importFetch(`/import/runs/${source}/discover`, {+ method: 'POST',+ });+ },+/** Restart a failed gather run for one source. */async retryGather(source: ImportSource) {return await importFetch(`/import/runs/${source}/retry`, {diff --git a/apps/web/src/lib/service-clients/service-cognition/openapi.json b/apps/web/src/lib/service-clients/service-cognition/openapi.jsonindex 2b2f6d6fe8..404f2e14b7 100644--- a/apps/web/src/lib/service-clients/service-cognition/openapi.json+++ b/apps/web/src/lib/service-clients/service-cognition/openapi.json@@ -1097,6 +1097,35 @@}}},+ "/import/runs/{source}/discover": {+ "post": {+ "tags": ["import"],+ "summary": "Discover candidates for user selection without auto-importing them.",+ "operationId": "discover_handler",+ "parameters": [+ {+ "name": "source",+ "in": "path",+ "description": "Import source",+ "required": true,+ "schema": {+ "type": "string"+ }+ }+ ],+ "responses": {+ "204": {+ "description": "Discovery accepted (idempotent)"+ },+ "400": {+ "description": "Unknown import source or discovery not supported"+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },"/import/runs/{source}/dismiss": {"post": {"tags": ["import"],@@ -3866,7 +3895,7 @@"Initiator": {"type": "string","description": "Where an import entity was first staged from. Provenance only — never a\nvisibility filter.",- "enum": ["onboarding", "chat", "archive"]+ "enum": ["onboarding", "chat", "archive", "manual"]},"JwtPayload": {"type": "object",@@ -4555,10 +4584,24 @@"description": "Metadata for one staged Slack channel.","required": ["name"],"properties": {+ "archived": {+ "type": "boolean",+ "description": "Whether Slack has archived this channel."+ },"channel_id": {"type": ["string", "null"],"description": "Slack's channel id (e.g. `C0123456789`), stable across renames."},+ "member_count": {+ "type": ["integer", "null"],+ "format": "int64",+ "description": "Total human members in the Slack channel.",+ "minimum": 0+ },+ "members_resolved": {+ "type": "boolean",+ "description": "Whether `participants` reflects a live membership read."+ },"name": {"type": "string","description": "Channel name without the leading `#`."@@ -4568,7 +4611,7 @@"items": {"$ref": "#/components/schemas/SlackParticipant"},- "description": "The channel's most relevant members, when discoverable."+ "description": "Slack members matched to the Macro team roster when discovery resolved membership."},"purpose": {"type": ["string", "null"],diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rsindex 97b74f98ed..c2fe9614e7 100644--- a/crates/ai_tools/src/tool_context.rs+++ b/crates/ai_tools/src/tool_context.rs@@ -1532,6 +1532,7 @@ pub type ToolImportService = import::domain::service::ImportServiceImpl<import::outbound::pg_import_repo::PgImportRepo,ToolMcpSelector,ToolEntityCreator,+ import::outbound::mcp_slack_source::McpSlackSource<ToolMcpSelector>,>;/// Type alias for the import tool context. Built `unwired` by the shareddiff --git a/crates/import/Cargo.toml b/crates/import/Cargo.tomlindex 774801e2c3..45af7fae75 100644--- a/crates/import/Cargo.toml+++ b/crates/import/Cargo.toml@@ -42,3 +42,5 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]macro_db_migrator = { path = "../macro_db_migrator" }+model_user = { path = "../model_user" }+tower = { workspace = true, features = ["util"] }diff --git a/crates/import/src/domain/models.rs b/crates/import/src/domain/models.rsindex cd2d997aec..496bb2b8fe 100644--- a/crates/import/src/domain/models.rs+++ b/crates/import/src/domain/models.rs@@ -48,19 +48,21 @@ impl ImportSource {]}- /// The Pipedream app slug backing this source's connector.- pub fn pipedream_app_slug(self) -> &'static str {+ /// The Pipedream app slugs backing this source's connector, in preference order.+ pub fn pipedream_app_slugs(self) -> &'static [&'static str] {match self {- ImportSource::Linear => "linear",- ImportSource::Notion => "notion",- ImportSource::Slack => "slack",+ ImportSource::Linear => &["linear"],+ ImportSource::Notion => &["notion"],+ // Keep the canonical fallback from docs/SLACK_LIVE_CHANNEL_IMPORT.md+ // until live verification supports switching to `slack_v2`.+ ImportSource::Slack => &["slack", "slack_v2"],}}/// This source's connector on both MCP stacks.pub fn connector_ref(self) -> mcp_select::ConnectorRef<'static> {mcp_select::ConnectorRef {- pipedream_app_slug: self.pipedream_app_slug(),+ pipedream_app_slugs: self.pipedream_app_slugs(),native_server_url: self.mcp_server_url(),}}@@ -165,6 +167,8 @@ pub enum Initiator {Chat,/// Imported from an administrator-uploaded archive.Archive,+ /// Staged by the user from Settings (the pick-channels flow).+ Manual,}/// Lifecycle of a gather run (one per user × source).@@ -214,6 +218,94 @@ impl SlackConversationId {}}+/// A stable Slack user ID.+#[derive(Debug, Clone, PartialEq, Eq, Hash)]+pub struct SlackUserId(String);++impl SlackUserId {+ /// Validate the source ID without trimming or name normalization.+ pub fn new(value: &str) -> Option<Self> {+ valid_slack_id(value, b"UW").then(|| Self(value.to_owned()))+ }++ /// The exact source identifier.+ pub fn as_str(&self) -> &str {+ &self.0+ }+}++/// The kind of conversation reported by Slack.+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum SlackConversationKind {+ /// A public workspace channel.+ PublicChannel,+ /// An invite-only workspace channel.+ PrivateChannel,+ /// A one-to-one direct message.+ DirectMessage,+ /// A multi-person direct message.+ GroupDirectMessage,+}++/// A conversation read from a live Slack workspace.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SlackConversation {+ /// Stable source identity.+ pub id: SlackConversationId,+ /// Conversation name.+ pub name: String,+ /// Source conversation kind, independent of the Macro target kind.+ pub kind: SlackConversationKind,+ /// Whether Slack has archived this conversation.+ pub archived: bool,+ /// Member count reported by Slack, when available.+ pub member_count: Option<u64>,+ /// Conversation purpose, when set.+ pub purpose: Option<String>,+}++/// One page of live Slack conversations.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SlackConversationPage {+ /// Conversations on this page.+ pub conversations: Vec<SlackConversation>,+ /// Opaque continuation cursor; `None` means the final page.+ pub next_cursor: Option<String>,+}++/// One page of a live Slack conversation's members.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SlackMemberPage {+ /// Slack user IDs on this page.+ pub members: Vec<SlackUserId>,+ /// Opaque continuation cursor; `None` means the final page.+ pub next_cursor: Option<String>,+}++/// A user read from a live Slack workspace.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SlackUser {+ /// Stable source identity.+ pub id: SlackUserId,+ /// Display name reported by Slack.+ pub display_name: String,+ /// Email, when the workspace exposes it.+ pub email: Option<String>,+ /// Whether this user is a bot.+ pub is_bot: bool,+ /// Whether this user has been deactivated.+ pub deleted: bool,+}++/// One page of live Slack users.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SlackUserPage {+ /// Users on this page.+ pub users: Vec<SlackUser>,+ /// Opaque continuation cursor; `None` means the final page.+ pub next_cursor: Option<String>,+}+/// A stable Slack workspace ID.#[derive(Debug, Clone, PartialEq, Eq)]pub struct SlackWorkspaceId(String);@@ -313,8 +405,8 @@ const MAX_TEXT: usize = 300;const MAX_LONG_TEXT: usize = 4_000;/// Cap for summaries and purposes.const MAX_SUMMARY: usize = 600;-/// Cap on Slack participant lists.-const MAX_PARTICIPANTS: usize = 25;+/// Cap on matched Slack participants, bounded by Macro team size, not channel size.+const MAX_PARTICIPANTS: usize = 100;/// Truncate to a character boundary at most `max` bytes in.fn truncated(s: String, max: usize) -> String {@@ -415,9 +507,18 @@ pub struct SlackChannelMeta {pub channel_id: Option<String>,/// The channel's purpose/topic, when set.pub purpose: Option<String>,- /// The channel's most relevant members, when discoverable.+ /// Slack members matched to the Macro team roster when discovery resolved membership.#[serde(default)]pub participants: Vec<SlackParticipant>,+ /// Total human members in the Slack channel.+ #[serde(default)]+ pub member_count: Option<u64>,+ /// Whether Slack has archived this channel.+ #[serde(default)]+ pub archived: bool,+ /// Whether `participants` reflects a live membership read.+ #[serde(default)]+ pub members_resolved: bool,}impl SlackChannelMeta {@@ -435,6 +536,9 @@ impl SlackChannelMeta {email: truncate_opt(p.email, MAX_TEXT),}).collect(),+ member_count: self.member_count,+ archived: self.archived,+ members_resolved: self.members_resolved,}}}diff --git a/crates/import/src/domain/models/test.rs b/crates/import/src/domain/models/test.rsindex bf5c531b88..f7b23ea1a0 100644--- a/crates/import/src/domain/models/test.rs+++ b/crates/import/src/domain/models/test.rs@@ -24,7 +24,12 @@ fn enums_round_trip_through_strings() {status.as_ref());}- for initiator in [Initiator::Onboarding, Initiator::Chat, Initiator::Archive] {+ for initiator in [+ Initiator::Onboarding,+ Initiator::Chat,+ Initiator::Archive,+ Initiator::Manual,+ ] {assert_eq!(Initiator::from_str(initiator.as_ref()).unwrap(), initiator);}for run in [@@ -39,6 +44,39 @@ fn enums_round_trip_through_strings() {}}+#[test]+fn manual_initiator_uses_snake_case() {+ assert_eq!(Initiator::Manual.as_ref(), "manual");+ assert_eq!(Initiator::from_str("manual").unwrap(), Initiator::Manual);+ assert_eq!(serde_json::to_value(Initiator::Manual).unwrap(), "manual");+ assert_eq!(+ serde_json::from_value::<Initiator>(serde_json::json!("manual")).unwrap(),+ Initiator::Manual+ );+}++#[test]+fn slack_user_ids_are_validated() {+ for id in ["U0123ABCD", "W0123ABCD"] {+ assert_eq!(SlackUserId::new(id).unwrap().as_str(), id);+ }+ for id in [+ "",+ "U",+ "W",+ "C0123ABCD",+ "u0123abcd",+ "U0123abcd",+ " U0123ABCD",+ "U0123ABCD ",+ "U../123",+ "Ué",+ ] {+ assert!(SlackUserId::new(id).is_none(), "{id}");+ }+ assert!(SlackUserId::new(&format!("U{}", "A".repeat(64))).is_none());+}+#[test]fn canonical_slack_ids_reject_names_and_wrong_namespaces() {for id in ["C0123456789", "GABC123", "D123", "C1"] {@@ -114,6 +152,21 @@ fn normalize_foreign_id_per_source() {);}+#[test]+fn connector_slugs_preserve_single_apps_and_accept_both_slack_aliases() {+ assert_eq!(ImportSource::Linear.pipedream_app_slugs(), &["linear"]);+ assert_eq!(ImportSource::Notion.pipedream_app_slugs(), &["notion"]);+ assert_eq!(+ ImportSource::Slack.pipedream_app_slugs(),+ &["slack", "slack_v2"]+ );++ let slack = ImportSource::Slack.connector_ref();+ assert!(slack.matches_pipedream_slug("slack"));+ assert!(slack.matches_pipedream_slug("slack_v2"));+ assert!(!slack.matches_pipedream_slug("slack_bot"));+}+#[test]fn fixed_entity_type_mapping() {assert_eq!(ImportSource::Linear.entity_type(), "task");@@ -142,7 +195,7 @@ fn validate_metadata_caps_and_drops_unknown_fields() {assert!(validate_metadata(ImportSource::Slack, serde_json::json!({"purpose": "x"})).is_err());// Participant lists are capped.- let many: Vec<_> = (0..100)+ let many: Vec<_> = (0..150).map(|i| serde_json::json!({"name": format!("p{i}")})).collect();let validated = validate_metadata(@@ -150,7 +203,42 @@ fn validate_metadata_caps_and_drops_unknown_fields() {serde_json::json!({"name": "eng", "participants": many}),).unwrap();- assert_eq!(validated["participants"].as_array().unwrap().len(), 25);+ assert_eq!(validated["participants"].as_array().unwrap().len(), 100);+ assert_eq!(validated["participants"][99]["name"], "p99");+}++#[test]+fn slack_metadata_round_trips_live_membership_fields() {+ let raw = serde_json::json!({+ "name": "eng",+ "channel_id": "C0123ABCD",+ "purpose": "Engineering",+ "participants": [{"name": "Alice", "email": "[email protected]"}],+ "member_count": 250,+ "archived": true,+ "members_resolved": true,+ });+ let validated = validate_metadata(ImportSource::Slack, raw.clone()).unwrap();+ assert_eq!(validated, raw);+ let metadata: SlackChannelMeta = serde_json::from_value(validated).unwrap();+ assert_eq!(metadata.member_count, Some(250));+ assert!(metadata.archived);+ assert!(metadata.members_resolved);+}++#[test]+fn slack_metadata_defaults_live_membership_fields() {+ let validated =+ validate_metadata(ImportSource::Slack, serde_json::json!({"name": "eng"})).unwrap();+ assert_eq!(validated["member_count"], serde_json::Value::Null);+ assert_eq!(validated["archived"], false);+ assert_eq!(validated["members_resolved"], false);+ assert_eq!(validated["participants"], serde_json::json!([]));+ let metadata: SlackChannelMeta = serde_json::from_value(validated).unwrap();+ assert_eq!(metadata.member_count, None);+ assert!(!metadata.archived);+ assert!(!metadata.members_resolved);+ assert!(metadata.participants.is_empty());}#[test]diff --git a/crates/import/src/domain/ports.rs b/crates/import/src/domain/ports.rsindex 658d13c401..be6baa0f28 100644--- a/crates/import/src/domain/ports.rs+++ b/crates/import/src/domain/ports.rs@@ -2,9 +2,11 @@use super::models::{ImportEntity, ImportRun, ImportSource, ImportSourceBinding, ImportStatus, ImportTargetKey,- ImportTargetKind, ImportTargetReservation, Initiator, RunStatus, SlackWorkspaceId,+ ImportTargetKind, ImportTargetReservation, Initiator, RunStatus, SlackConversationId,+ SlackConversationPage, SlackMemberPage, SlackUserPage, SlackWorkspaceId,};use macro_user_id::user_id::MacroUserIdStr;+use std::time::Duration;use thiserror::Error;use uuid::Uuid;@@ -14,6 +16,9 @@ pub enum ImportError {/// AI work was refused or usage validation is temporarily unavailable.#[error(transparent)]Admission(#[from] ai_billing::AiAdmissionError),+ /// This source does not support user-triggered discovery.+ #[error("manual discovery is not supported for {source}", source = .0.as_ref())]+ UnsupportedDiscovery(ImportSource),/// Database failure.#[error("database error: {0}")]Db(rootcause::Report),@@ -40,6 +45,67 @@ pub enum ImportError {/// Result alias for import operations.pub type Result<T> = std::result::Result<T, ImportError>;+/// Errors reading a live Slack workspace.+#[derive(Debug, Error)]+pub enum SlackSourceError {+ /// The user has no Slack connector.+ #[error("Slack is not connected")]+ NotConnected,+ /// The connector lacks a required capability.+ #[error("Slack connector capability unavailable: {0}")]+ ToolsUnavailable(&'static str),+ /// Slack temporarily refused reads due to rate limiting.+ #[error("Slack rate limit exceeded")]+ RateLimited {+ /// Provider-supplied delay before retrying, when available.+ retry_after: Option<Duration>,+ },+ /// The connection lacks a required Slack permission scope.+ #[error("missing Slack scope: {0}")]+ MissingScope(String),+ /// Any other source failure.+ #[error(transparent)]+ Other(#[from] anyhow::Error),+}++/// Opens live Slack workspace sessions for import discovery and membership reads.+pub trait SlackWorkspaceSource: Send + Sync + 'static {+ /// Workspace reader scoped to one gather or one import batch.+ type Session: SlackWorkspaceSession;++ /// Open a session scoped to one gather or one import batch.+ /// Returns [`SlackSourceError::NotConnected`] when the user has no Slack+ /// connector, or [`SlackSourceError::ToolsUnavailable`] when the connector+ /// lacks a needed capability.+ fn open(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = std::result::Result<Self::Session, SlackSourceError>> + Send;+}++/// Paginated workspace reads scoped to one gather or one import batch.+/// Pass `None` to read the first page, then the returned continuation cursor.+pub trait SlackWorkspaceSession: Send + Sync {+ /// List conversations visible to the connected user.+ fn list_conversations(+ &self,+ cursor: Option<&str>,+ ) -> impl Future<Output = std::result::Result<SlackConversationPage, SlackSourceError>> + Send;++ /// List the members of one Slack conversation.+ fn conversation_members(+ &self,+ conversation: &SlackConversationId,+ cursor: Option<&str>,+ ) -> impl Future<Output = std::result::Result<SlackMemberPage, SlackSourceError>> + Send;++ /// List the workspace user directory.+ fn list_users(+ &self,+ cursor: Option<&str>,+ ) -> impl Future<Output = std::result::Result<SlackUserPage, SlackSourceError>> + Send;+}+/// Persistence for the import ledger and gather runs.////// Status writes are compare-and-swaps that report whether they happened, so@@ -188,6 +254,13 @@ pub trait ImportRepo: Send + Sync + 'static {user: &MacroUserIdStr<'static>,) -> impl Future<Output = Result<Option<Uuid>>> + Send;+ /// IDs of the team's members. Macro user IDs encode emails, so this is+ /// the roster the import domain matches Slack emails against.+ fn team_members(+ &self,+ team_id: Uuid,+ ) -> impl Future<Output = Result<Vec<MacroUserIdStr<'static>>>> + Send;+/// All gather runs for the user.fn list_runs(&self,@@ -206,6 +279,16 @@ pub trait ImportRepo: Send + Sync + 'static {auto_import: bool,) -> impl Future<Output = Result<bool>> + Send;+ /// CAS a run to `running` with `auto_import = false`. Wins when no run+ /// exists or the existing status is in `from`. Unlike `start_run`, a+ /// successful claim always clears `auto_import`.+ fn start_manual_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ ) -> impl Future<Output = Result<bool>> + Send;+/// CAS the `running` run for `source` to `to` (ready/failed), recording/// `error` when there is one. Returns whether it happened.fn finish_run(diff --git a/crates/import/src/domain/service.rs b/crates/import/src/domain/service.rsindex 2ddbc0056b..ba02f84120 100644--- a/crates/import/src/domain/service.rs+++ b/crates/import/src/domain/service.rs@@ -1,10 +1,11 @@//! The import orchestrator: staging (with team-wide dedup), gather jobs,//! and import jobs.//!-//! Gather jobs are short agent sessions over the user's connector MCP server-//! whose toolset includes an in-process `CreateImportEntity` locked to-//! `(user, source, initiator)` — the model stages candidates by calling the-//! tool, so there is no structured output to parse. Import jobs copy+//! Slack discovery uses the typed workspace source port to stage public+//! channels and enrich their membership. Manual discovery never uses an agent+//! or auto-imports; onboarding may fall back to an agent session. Other gather+//! jobs use connector tools with `CreateImportEntity` locked to+//! `(user, source, initiator)`. Import jobs copy//! accepted rows in: Linear tasks and Slack channels are composed//! deterministically from staged metadata; Notion pages are fetched//! directly through the connector's fetch tool (no model in the content@@ -15,6 +16,7 @@ use super::models::*;use super::ports::{CanonicalImportRepo, EntityCreator, ImportError, ImportRepo, ImportedDocumentProperties,ImportedDocumentProperty, ImportedDocumentPropertyValue, ImportedTaskProperties, Result,+ SlackSourceError, SlackWorkspaceSource,};use crate::inbound::toolset::{ImportToolContext, ToolPolicy, gather_toolset, notion_import_toolset,@@ -35,6 +37,10 @@ use uuid::Uuid;mod admission;mod prompts;mod slack;+mod slack_discovery;++pub use slack_discovery::NoSlackSource;+use slack_discovery::SlackBatch;#[cfg(test)]mod test;@@ -67,12 +73,22 @@ const NOTION_PAGE_IMPORT_TIMEOUT: Duration = Duration::from_secs(120);/// enough to stay polite to Notion's MCP.const NOTION_IMPORT_CONCURRENCY: usize = 4;-/// How many channels the deterministic Slack gather stages, mirroring the-/// "8-15 strong candidates" the agent prompt asks for.-const SLACK_GATHER_MAX_CHANNELS: usize = 15;-/// How many channel-search pages the deterministic Slack gather follows-/// before staging what it has.-const SLACK_GATHER_MAX_PAGES: usize = 5;+/// Discovery policy: bounded onboarding suggestions or a user-driven listing.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum GatherMode {+ /// Suggest the strongest active channels for onboarding.+ Onboarding,+ /// Stage all visible public channels for explicit selection.+ Manual,+}++fn gather_timeout(source: ImportSource, mode: GatherMode) -> Duration {+ match (source, mode) {+ (ImportSource::Slack, GatherMode::Onboarding) => Duration::from_secs(180),+ (ImportSource::Slack, GatherMode::Manual) => Duration::from_secs(360),+ _ => GATHER_TIMEOUT,+ }+}/// How often a running import batch touches its rows' `updated_at`. The/// heartbeat covers every row the batch owns — queued AND in-flight — so a@@ -165,6 +181,14 @@ pub trait ImportService: Send + Sync + 'static {auto_import: bool,) -> impl Future<Output = Result<bool>> + Send;+ /// User-triggered, non-auto-import gather that stages everything visible.+ /// Only Slack is supported in this slice. Returns whether a run started.+ fn start_discovery(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> impl Future<Output = Result<bool>> + Send;+/// Restart a failed (or dismissed) gather run. Returns whether a run/// started.fn retry_gather(@@ -302,8 +326,9 @@ pub trait NotionPageImporter: Send + Sync + 'static {/// Concrete orchestrator wiring the repo, the user's MCP servers, the/// entity creator, and usage recording together.-pub struct ImportServiceImpl<R, S, C> {+pub struct ImportServiceImpl<R, S, C, W = NoSlackSource> {repo: R,+ slack_source: Arc<W>,mcp_tools: Arc<S>,creator: Arc<C>,recorder: Arc<dyn ai_usage::UsageRecorder>,@@ -311,10 +336,11 @@ pub struct ImportServiceImpl<R, S, C> {notifier: Option<ImportNotify>,}-impl<R: Clone, S, C> Clone for ImportServiceImpl<R, S, C> {+impl<R: Clone, S, C, W: SlackWorkspaceSource> Clone for ImportServiceImpl<R, S, C, W> {fn clone(&self) -> Self {Self {repo: self.repo.clone(),+ slack_source: self.slack_source.clone(),mcp_tools: self.mcp_tools.clone(),creator: self.creator.clone(),recorder: self.recorder.clone(),@@ -334,6 +360,7 @@ impl<R, S, C> ImportServiceImpl<R, S, C> {) -> Self {Self {repo,+ slack_source: Arc::new(NoSlackSource),mcp_tools,creator,recorder,@@ -341,6 +368,24 @@ impl<R, S, C> ImportServiceImpl<R, S, C> {notifier: None,}}+}++impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {+ /// Attach a live Slack workspace reader, preserving the service configuration.+ pub fn with_slack_source<W2: SlackWorkspaceSource>(+ self,+ source: Arc<W2>,+ ) -> ImportServiceImpl<R, S, C, W2> {+ ImportServiceImpl {+ repo: self.repo,+ mcp_tools: self.mcp_tools,+ creator: self.creator,+ recorder: self.recorder,+ admission: self.admission,+ notifier: self.notifier,+ slack_source: source,+ }+ }/// Push state-change nudges through the given notifier.pub fn with_notifier(mut self, notifier: ImportNotify) -> Self {@@ -355,7 +400,7 @@ impl<R, S, C> ImportServiceImpl<R, S, C> {}}-impl<R, S, C> ImportServiceImpl<R, S, C>+impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -363,13 +408,15 @@ where{/// Spawn the gather session for one source; finishes the run row either/// way and nudges the client.- fn spawn_gather(&self, user: MacroUserIdStr<'static>, source: ImportSource) {+ fn spawn_gather(&self, user: MacroUserIdStr<'static>, source: ImportSource, mode: GatherMode) {let service = self.clone();tokio::spawn(async move {- let outcome =- tokio::time::timeout(GATHER_TIMEOUT, service.run_gather_session(&user, source))- .await- .unwrap_or_else(|_| Err(anyhow::anyhow!("gather session timed out")));+ let outcome = tokio::time::timeout(+ gather_timeout(source, mode),+ service.run_gather_session(&user, source, mode),+ )+ .await+ .unwrap_or_else(|_| Err(anyhow::anyhow!("gather session timed out")));let gather_succeeded = outcome.is_ok();let finished = match outcome {@@ -453,45 +500,44 @@ where}}- /// Run one gather session: connector MCP tools plus the locked- /// `CreateImportEntity`. Staged rows land as the session runs; the final- /// text is ignored.+ /// Run typed Slack discovery or an agent gather with locked staging tools.+ /// Candidates land incrementally; any final agent text is ignored.#[tracing::instrument(skip(self, user), err)]async fn run_gather_session(&self,user: &MacroUserIdStr<'static>,source: ImportSource,+ mode: GatherMode,) -> anyhow::Result<()> {- // The gather may have waited since it was accepted. Refuse before- // loading tools; Slack still gets its deterministic attempt first.+ // Recheck queued AI work before loading tools. Slack stays deterministic+ // until its onboarding fallback actually needs an agent.self.admit_gather(user, source).await?;- let mcp_tools = self.connector_tools(user, source).await?;-- self.gather_with_tools(user, source, mcp_tools).await+ self.gather_with_tools(user, source, mode).await}- async fn gather_with_tools<M>(+ async fn gather_with_tools(&self,user: &MacroUserIdStr<'static>,source: ImportSource,- mcp_tools: Arc<M>,- ) -> anyhow::Result<()>- where- M: ToolSet<()> + ToolSet<ImportToolContext<Self>> + 'static,- {- // Slack discovery is a listing problem, not a language problem:- // enumerate channels through the connector directly and stage the- // strongest. The agent session only runs as a fallback, when the- // connector's tool surface changed under us.+ mode: GatherMode,+ ) -> anyhow::Result<()> {+ // Typed Slack discovery runs before loading agent tools. Only+ // onboarding may fall back to an agent when workspace reads fail.if source == ImportSource::Slack {- match self.gather_slack_direct(user, &*mcp_tools).await {+ match slack_discovery::gather_slack(self, user, mode).await {Ok(_) => return Ok(()),+ Err(SlackSourceError::ToolsUnavailable(_)) if mode == GatherMode::Manual => {+ anyhow::bail!("Your Slack connection does not expose channel tools");+ }+ Err(e) if mode == GatherMode::Manual => return Err(e.into()),Err(e) => {tracing::warn!(error = ?e, "direct slack gather failed; trying the agent");}}}+ let mcp_tools = self.connector_tools(user, source).await?;+// Staging is idempotent (the ledger dedups already-staged rows), so// rerunning the whole session on the fallback model is safe even// when the primary died mid-way through staging.@@ -598,116 +644,6 @@ whereOk(())}- /// Call one connector tool and surface both dispatch and tool errors as- /// plain errors.- async fn connector_tool_call<M: ToolSet<()>>(- &self,- user: &MacroUserIdStr<'static>,- mcp_tools: &M,- tool_name: &str,- arguments: &serde_json::Value,- ) -> anyhow::Result<serde_json::Value> {- ToolSet::<()>::try_tool_call(- mcp_tools,- (),- RequestContext::new(user.clone()),- tool_name,- arguments,- )- .await- .map_err(|e| anyhow::anyhow!("{tool_name} dispatch failed: {e}"))?- .map_err(|e| anyhow::anyhow!("{tool_name} failed: {}", e.description))- }-- /// Stage the user's Slack channels WITHOUT a model: call the connector's- /// channel-search tool directly (an empty query lists every channel the- /// connected user can see), follow pagination, and stage the strongest- /// candidates. The agent-driven gather routinely finished "successfully"- /// having staged nothing; a direct call either produces channels or a- /// real error. Staged rows carry no participants — inviting teammates- /// stays best-effort and must never block discovery.- #[tracing::instrument(skip(self, user, mcp_tools), err)]- async fn gather_slack_direct<M: ToolSet<()>>(- &self,- user: &MacroUserIdStr<'static>,- mcp_tools: &M,- ) -> anyhow::Result<usize> {- let search_tool = slack_channel_search_tool_name(mcp_tools)- .ok_or_else(|| anyhow::anyhow!("connector exposes no channel-search tool"))?;-- let mut channels: Vec<SlackChannelCandidate> = Vec::new();- let mut cursor: Option<String> = None;- for page in 0..SLACK_GATHER_MAX_PAGES {- let mut arguments = serde_json::json!({ "query": "" });- if let Some(cursor) = &cursor {- arguments["cursor"] = serde_json::Value::String(cursor.clone());- }- let result = match self- .connector_tool_call(user, mcp_tools, &search_tool, &arguments)- .await- {- Ok(result) => Ok(result),- // Some servers reject an explicit empty query; try the first- // page again without one before giving up on the direct path.- Err(empty_query_error) if page == 0 => self- .connector_tool_call(user, mcp_tools, &search_tool, &serde_json::json!({}))- .await- .map_err(|no_query_error| {- anyhow::anyhow!(- "channel search failed with an empty query ({empty_query_error}) \- and without one ({no_query_error})"- )- }),- Err(e) => {- // Later pages are best-effort: keep what earlier pages- // already produced.- tracing::warn!(page, error = ?e, "channel search page failed");- break;- }- }?;-- let parsed = parse_slack_channel_page(result);- channels.extend(parsed.channels);- cursor = parsed.next_cursor;- if cursor.is_none() {- break;- }- }- anyhow::ensure!(!channels.is_empty(), "channel search returned no channels");-- let enumerated = channels.len();- let mut staged = 0usize;- for channel in select_slack_candidates(channels, SLACK_GATHER_MAX_CHANNELS) {- let foreign_id = channel.id.clone().unwrap_or_else(|| channel.name.clone());- let metadata = serde_json::json!({- "name": channel.name,- "channel_id": channel.id,- "purpose": channel.purpose,- "participants": [],- });- match self- .stage(- user,- Initiator::Onboarding,- ImportSource::Slack,- &foreign_id,- metadata,- )- .await- {- Ok(StageOutcome::Staged(_)) => staged += 1,- // Already imported (by the user or a teammate), declined, or- // mid-import — the ledger already covers it.- Ok(_) => {}- Err(e) => {- tracing::warn!(channel = %channel.name, error = ?e, "failed to stage slack channel");- }- }- }- tracing::info!(enumerated, staged, "direct slack gather finished");- Ok(staged)- }-/// Fallback: run a single-page Haiku session over the shared connector/// tools. Rows the agent fails to finalize are handled by the caller.#[tracing::instrument(skip(self, user, mcp_tools, rows), fields(pages = rows.len()), err)]@@ -795,9 +731,13 @@ whereOk(())}- /// Copy one accepted Linear/Slack row in, deterministically from its- /// staged metadata.- async fn import_deterministic(&self, user: &MacroUserIdStr<'static>, row: &ImportEntity) {+ /// Copy one accepted Linear/Slack row from staged metadata, refreshing Slack membership live.+ async fn import_deterministic(+ &self,+ user: &MacroUserIdStr<'static>,+ row: &ImportEntity,+ slack_batch: &mut SlackBatch<W::Session>,+ ) {let created: anyhow::Result<(String, Option<Uuid>)> = match row.source {ImportSource::Linear => {match serde_json::from_value::<LinearIssueMeta>(row.metadata.clone()) {@@ -819,12 +759,15 @@ where.user_team_id(user).await?.ok_or_else(|| anyhow::anyhow!("Slack import requires a team"))?;+ let meta = serde_json::from_value(row.metadata.clone())?;+ let emails = slack_batch.resolve_emails(self, user, team_id, &meta).await;let id = slack::ensure_channel(&self.repo,self.creator.as_ref(),user,row,team_id,+ &emails,).await?;Ok((id.to_string(), Some(team_id)))@@ -959,8 +902,11 @@ where}}));+ let mut slack_batch = SlackBatch::default();for row in &direct_rows {- service.import_deterministic(&user, row).await;+ service+ .import_deterministic(&user, row, &mut slack_batch)+ .await;}if !notion_rows.is_empty() {@@ -1007,7 +953,7 @@ where}}-impl<R, S, C> ImportService for ImportServiceImpl<R, S, C>+impl<R, S, C, W: SlackWorkspaceSource> ImportService for ImportServiceImpl<R, S, C, W>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -1081,7 +1027,33 @@ where// The CAS still decides the winner if another request raced admission.let won = self.repo.start_run(&user, source, &[], auto_import).await?;if won {- self.spawn_gather(user.clone(), source);+ self.spawn_gather(user.clone(), source, GatherMode::Onboarding);+ self.notify(&user).await;+ }+ Ok(won)+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn start_discovery(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> Result<bool> {+ if source != ImportSource::Slack {+ return Err(ImportError::UnsupportedDiscovery(source));+ }+ let from = [+ RunStatus::Ready,+ RunStatus::Completed,+ RunStatus::Failed,+ RunStatus::Dismissed,+ ];+ if !self.prepare_gather(&user, source, &from).await? {+ return Ok(false);+ }+ let won = self.repo.start_manual_run(&user, source, &from).await?;+ if won {+ self.spawn_gather(user.clone(), source, GatherMode::Manual);self.notify(&user).await;}Ok(won)@@ -1109,7 +1081,7 @@ where).await?;if won {- self.spawn_gather(user.clone(), source);+ self.spawn_gather(user.clone(), source, GatherMode::Onboarding);self.notify(&user).await;}Ok(won)@@ -1196,20 +1168,21 @@ where}}-impl<R, S, C> ImportStager for ImportServiceImpl<R, S, C>+impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,C: EntityCreator,{#[tracing::instrument(skip(self, user, metadata), err)]- async fn stage(+ async fn stage_inner(&self,user: &MacroUserIdStr<'static>,initiator: Initiator,source: ImportSource,foreign_id: &str,metadata: serde_json::Value,+ notify: bool,) -> Result<StageOutcome> {let foreign_id = source.normalize_foreign_id(foreign_id)@@ -1250,7 +1223,9 @@ where.await?{Some(row) => {- self.notify(user).await;+ if notify {+ self.notify(user).await;+ }Ok(StageOutcome::Staged(row))}// Raced into a non-staged status between the check and the@@ -1274,6 +1249,25 @@ where}}}+}++impl<R, S, C, W: SlackWorkspaceSource> ImportStager for ImportServiceImpl<R, S, C, W>+where+ R: ImportRepo + CanonicalImportRepo + Clone,+ S: ConnectorSelect,+ C: EntityCreator,+{+ async fn stage(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ source: ImportSource,+ foreign_id: &str,+ metadata: serde_json::Value,+ ) -> Result<StageOutcome> {+ self.stage_inner(user, initiator, source, foreign_id, metadata, true)+ .await+ }#[tracing::instrument(skip(self, user, metadata), err)]async fn record_imported(@@ -1339,7 +1333,7 @@ where}}-impl<R, S, C> NotionPageImporter for ImportServiceImpl<R, S, C>+impl<R, S, C, W: SlackWorkspaceSource> NotionPageImporter for ImportServiceImpl<R, S, C, W>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -1474,7 +1468,7 @@ where}}-impl<R, S, C> ImportFinalizer for ImportServiceImpl<R, S, C>+impl<R, S, C, W: SlackWorkspaceSource> ImportFinalizer for ImportServiceImpl<R, S, C, W>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -1557,185 +1551,6 @@ fn is_notion_fetch_tool_name(name: &str) -> bool {)}-/// The mangled name of the connector's channel-search tool. Slack's hosted-/// MCP has shipped several tool-name spellings, so this matches the shape —-/// a search/list over channels — rather than one literal name.-fn slack_channel_search_tool_name(mcp_tools: &impl ToolSet<()>) -> Option<String> {- ToolSet::<()>::request_schemas(mcp_tools)?- .into_iter()- .map(|schema| schema.name)- .find(|name| is_slack_channel_search_tool_name(name))-}--fn is_slack_channel_search_tool_name(name: &str) -> bool {- let Some((_, tool)) = name.rsplit_once("__") else {- return false;- };- let tool = tool.to_ascii_lowercase().replace('-', "_");- let channel_noun = tool.contains("channel") || tool.contains("conversation");- let listing_verb = tool.contains("search") || tool.contains("list");- let other_surface = [- "member", "history", "message", "canvas", "create", "user", "emoji", "file",- ]- .iter()- .any(|word| tool.contains(word));- channel_noun && listing_verb && !other_surface-}--/// One channel parsed out of a Slack channel-search result.-#[derive(Debug, Clone, PartialEq)]-struct SlackChannelCandidate {- id: Option<String>,- name: String,- purpose: Option<String>,- member_count: Option<u64>,- archived: bool,-}--/// One page of channel-search results.-#[derive(Debug, Default, PartialEq)]-struct SlackChannelPage {- channels: Vec<SlackChannelCandidate>,- next_cursor: Option<String>,-}--/// Split a channel-search result into channels plus a pagination cursor.-/// Handles structured MCP output, JSON re-encoded as text, a bare array of-/// channels, and the channel list hiding under common wrapper keys.-fn parse_slack_channel_page(result: serde_json::Value) -> SlackChannelPage {- match result {- serde_json::Value::String(text) => match serde_json::from_str::<serde_json::Value>(&text) {- Ok(structured @ (serde_json::Value::Object(_) | serde_json::Value::Array(_))) => {- parse_slack_channel_page(structured)- }- _ => SlackChannelPage::default(),- },- serde_json::Value::Array(items) => SlackChannelPage {- channels: items.iter().filter_map(parse_slack_channel).collect(),- next_cursor: None,- },- serde_json::Value::Object(map) => {- let next_cursor = slack_next_cursor(&map);- for key in ["channels", "results", "items", "matches", "data"] {- if let Some(value) = map.get(key) {- let inner = parse_slack_channel_page(value.clone());- if !inner.channels.is_empty() {- return SlackChannelPage {- channels: inner.channels,- next_cursor: inner.next_cursor.or(next_cursor),- };- }- }- }- // A single channel object at the top level.- let channels = parse_slack_channel(&serde_json::Value::Object(map))- .into_iter()- .collect();- SlackChannelPage {- channels,- next_cursor,- }- }- _ => SlackChannelPage::default(),- }-}--fn slack_next_cursor(map: &serde_json::Map<String, serde_json::Value>) -> Option<String> {- map.get("next_cursor")- .or_else(|| map.get("nextCursor"))- .or_else(|| map.get("cursor"))- .or_else(|| {- map.get("response_metadata")- .and_then(|value| value.as_object())- .and_then(|metadata| metadata.get("next_cursor"))- })- .and_then(|value| value.as_str())- .map(str::trim)- .filter(|cursor| !cursor.is_empty())- .map(str::to_string)-}--fn parse_slack_channel(value: &serde_json::Value) -> Option<SlackChannelCandidate> {- let map = value.as_object()?;- // DMs and group DMs are not importable channels.- if ["is_im", "is_mpim"]- .iter()- .any(|key| map.get(*key).and_then(serde_json::Value::as_bool) == Some(true))- {- return None;- }- let name = map- .get("name")- .or_else(|| map.get("channel_name"))- .and_then(|value| value.as_str())- .map(|name| name.trim().trim_start_matches('#'))- .filter(|name| !name.is_empty())?- .to_string();- let id = map- .get("id")- .or_else(|| map.get("channel_id"))- .and_then(|value| value.as_str())- .map(str::trim)- .filter(|id| !id.is_empty())- .map(str::to_string);- let purpose = slack_channel_text(map, "purpose")- .or_else(|| slack_channel_text(map, "topic"))- .or_else(|| slack_channel_text(map, "description"));- let member_count = map- .get("member_count")- .or_else(|| map.get("num_members"))- .and_then(serde_json::Value::as_u64);- let archived = map.get("is_archived").and_then(serde_json::Value::as_bool) == Some(true);- Some(SlackChannelCandidate {- id,- name,- purpose,- member_count,- archived,- })-}--/// Slack renders purpose/topic either as a plain string or as the classic-/// API's `{ "value": "…" }` wrapper.-fn slack_channel_text(- map: &serde_json::Map<String, serde_json::Value>,- key: &str,-) -> Option<String> {- let value = map.get(key)?;- value- .as_str()- .or_else(|| value.get("value").and_then(|nested| nested.as_str()))- .map(str::trim)- .filter(|text| !text.is_empty())- .map(str::to_string)-}--/// Rank enumerated channels and keep the strongest `cap`: drop archived-/// channels, dedupe by id (falling back to the name), prefer larger channels-/// (member count is the best activity proxy a listing offers), and keep the-/// listing order among ties.-fn select_slack_candidates(- channels: Vec<SlackChannelCandidate>,- cap: usize,-) -> Vec<SlackChannelCandidate> {- let mut seen = HashSet::new();- let mut candidates: Vec<SlackChannelCandidate> = channels- .into_iter()- .filter(|channel| !channel.archived)- .filter(|channel| {- seen.insert(- channel- .id- .clone()- .unwrap_or_else(|| format!("#{}", channel.name.to_ascii_lowercase())),- )- })- .collect();- candidates.sort_by_key(|channel| std::cmp::Reverse(channel.member_count.unwrap_or(0)));- candidates.truncate(cap);- candidates-}-/// Split a Notion fetch result into its title, Markdown body, and properties./// Structured MCP output and JSON text may use either Notion's `markdown`/// field or the generic MCP fetch `text` field.diff --git a/crates/import/src/domain/service/admission.rs b/crates/import/src/domain/service/admission.rsindex ea6d95e0cf..bf4aa3fe11 100644--- a/crates/import/src/domain/service/admission.rs+++ b/crates/import/src/domain/service/admission.rs@@ -1,13 +1,13 @@//! Admission belongs at AI boundaries, not at deterministic import claims.use super::{ImportServiceImpl, ImportSource, Result, RunStatus};-use crate::domain::ports::ImportRepo;+use crate::domain::ports::{ImportRepo, SlackWorkspaceSource};use ai_billing::{AiAdmissionError, AiAdmissionService};use ai_usage::AiFeature;use macro_user_id::user_id::MacroUserIdStr;use std::sync::Arc;-impl<R, S, C> ImportServiceImpl<R, S, C> {+impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {/// Attach configured shared admission. Production hosts must supply this;/// the source-compatible constructor defaults to disabled enforcement.pub fn with_admission(mut self, admission: Arc<dyn AiAdmissionService>) -> Self {@@ -34,7 +34,7 @@ impl<R, S, C> ImportServiceImpl<R, S, C> {}}-impl<R: ImportRepo, S, C> ImportServiceImpl<R, S, C> {+impl<R: ImportRepo, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {/// Preserve no-op retries and settings before checking new AI spending./// Slack discovery is deterministic until its fallback actually starts.pub(super) async fn prepare_gather(diff --git a/crates/import/src/domain/service/slack.rs b/crates/import/src/domain/service/slack.rsindex c673e94170..5b58e53aa3 100644--- a/crates/import/src/domain/service/slack.rs+++ b/crates/import/src/domain/service/slack.rs@@ -18,6 +18,7 @@ pub(super) async fn ensure_channel(user: &MacroUserIdStr<'static>,row: &ImportEntity,team_id: Uuid,+ participant_emails: &[String],) -> anyhow::Result<Uuid> {let meta: SlackChannelMeta = serde_json::from_value(row.metadata.clone())?;// Never guess a source ID from a channel name. Older normalization prefixed@@ -40,13 +41,8 @@ pub(super) async fn ensure_channel(if target.ready {return Ok(target.channel_id);}- let emails = meta- .participants- .iter()- .filter_map(|participant| participant.email.clone())- .collect::<Vec<_>>();let channel_id = creator- .create_channel(user, &meta.name, &target, &emails)+ .create_channel(user, &meta.name, &target, participant_emails).await?;// Completion validates the persisted ID against the reservation. On retry,// either the same pending UUID is ensured again or a ready mapping is reused.diff --git a/crates/import/src/domain/service/slack/test.rs b/crates/import/src/domain/service/slack/test.rsindex 31c1229dc7..c64dccb096 100644--- a/crates/import/src/domain/service/slack/test.rs+++ b/crates/import/src/domain/service/slack/test.rs@@ -14,6 +14,7 @@ struct State {channels: HashSet<Uuid>,live_creations: usize,creator_calls: usize,+ participant_emails: Vec<String>,fail_before_creation: bool,fail_after_creation: bool,fail_completion: bool,@@ -113,11 +114,12 @@ impl EntityCreator for Harness {_: &MacroUserIdStr<'static>,_: &str,target: &ImportTargetReservation,- _: &[String],+ participant_emails: &[String],) -> anyhow::Result<Uuid> {tokio::task::yield_now().await;let mut state = self.0.lock().unwrap();state.creator_calls += 1;+ state.participant_emails = participant_emails.to_vec();anyhow::ensure!(!std::mem::take(&mut state.fail_before_creation),"interrupted after reservation"@@ -191,7 +193,7 @@ async fn archive_first_onboarding_reuses_without_invites_or_events() {let (user, row, key) = fixture();let archive = h.archive(&key).await;assert_eq!(- ensure_channel(&h, &h, &user, &row, key.team_id)+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.unwrap(),archive@@ -204,7 +206,7 @@ async fn archive_first_onboarding_reuses_without_invites_or_events() {async fn onboarding_first_archive_reuses_target() {let h = Harness::default();let (user, row, key) = fixture();- let onboarding = ensure_channel(&h, &h, &user, &row, key.team_id)+ let onboarding = ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.unwrap();assert_eq!(h.archive(&key).await, onboarding);@@ -216,7 +218,7 @@ async fn concurrent_onboarding_and_archive_reservations_converge() {let h = Harness::default();let (user, row, key) = fixture();let (onboarding, archive) = tokio::join!(- ensure_channel(&h, &h, &user, &row, key.team_id),+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]),h.archive(&key),);assert_eq!(onboarding.unwrap(), archive);@@ -237,13 +239,13 @@ async fn crash_retry_at_each_boundary_keeps_one_channel_and_mapping() {state.fail_completion = boundary == 2;}assert!(- ensure_channel(&h, &h, &user, &row, key.team_id)+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.is_err());let reserved = h.0.lock().unwrap().target.as_ref().unwrap().channel_id;assert_eq!(- ensure_channel(&h, &h, &user, &row, key.team_id)+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.unwrap(),reserved@@ -252,7 +254,7 @@ async fn crash_retry_at_each_boundary_keeps_one_channel_and_mapping() {// importing-row CAS: retry returns the ready target without creating.let calls = h.0.lock().unwrap().creator_calls;assert_eq!(- ensure_channel(&h, &h, &user, &row, key.team_id)+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.unwrap(),reserved@@ -268,26 +270,40 @@ async fn completion_uses_returned_persisted_id_not_candidate() {h.0.lock().unwrap().wrong_result = true;let (user, row, key) = fixture();assert!(- ensure_channel(&h, &h, &user, &row, key.team_id)+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.is_err());assert!(!h.0.lock().unwrap().target.as_ref().unwrap().ready);}+#[tokio::test]+async fn creation_uses_supplied_emails_instead_of_staged_participants() {+ let h = Harness::default();+ let (user, mut row, key) = fixture();+ row.metadata["participants"] = serde_json::json!([+ {"name": "Staged", "email": "[email protected]"}+ ]);+ let emails = vec!["[email protected]".to_string()];+ ensure_channel(&h, &h, &user, &row, key.team_id, &emails)+ .await+ .unwrap();+ assert_eq!(h.0.lock().unwrap().participant_emails, emails);+}+#[tokio::test]async fn ambiguous_or_missing_source_identity_fails_before_reservation() {let h = Harness::default();let (user, mut row, key) = fixture();row.metadata["channel_id"] = serde_json::json!("COTHER");assert!(- ensure_channel(&h, &h, &user, &row, key.team_id)+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.is_err());row.foreign_id = "#engineering".to_string();assert!(- ensure_channel(&h, &h, &user, &row, key.team_id)+ ensure_channel(&h, &h, &user, &row, key.team_id, &[]).await.is_err());diff --git a/crates/import/src/domain/service/slack_discovery.rs b/crates/import/src/domain/service/slack_discovery.rsnew file mode 100644index 0000000000..5a53766869--- /dev/null+++ b/crates/import/src/domain/service/slack_discovery.rs@@ -0,0 +1,413 @@+//! Bounded Slack discovery and roster policy over typed workspace reads.++use super::{GatherMode, ImportServiceImpl, StageOutcome, gather_timeout};+use crate::domain::models::{+ ImportSource, Initiator, SlackChannelMeta, SlackConversation, SlackConversationId,+ SlackConversationKind, SlackConversationPage, SlackMemberPage, SlackParticipant, SlackUser,+ SlackUserId, SlackUserPage,+};+use crate::domain::ports::{+ CanonicalImportRepo, EntityCreator, ImportRepo, SlackSourceError, SlackWorkspaceSession,+ SlackWorkspaceSource,+};+use macro_user_id::user_id::MacroUserIdStr;+use mcp_select::ConnectorSelect;+use std::collections::{HashMap, HashSet};+use std::time::Duration;+use tokio::time::{Instant, timeout_at};+use uuid::Uuid;++#[cfg(test)]+mod test;++const MAX_CONVERSATION_PAGES: usize = 10;+const MAX_USER_PAGES: usize = 10;+const MAX_MEMBER_PAGES: usize = 5;+const MANUAL_MEMBER_BUDGET: usize = 100;+const NOTIFY_EVERY: usize = 10;+const RATE_LIMIT_MAX_SLEEP: Duration = Duration::from_secs(30);+// Finish best-effort work before spawn_gather's hard timeout can fail usable rows.+const ENRICHMENT_TIMEOUT_RESERVE: Duration = Duration::from_secs(10);++/// Default for hosts that have not attached a live Slack workspace reader.+pub struct NoSlackSource;++/// An uninhabited session: the default source never opens successfully.+pub enum NoSlackSession {}++impl SlackWorkspaceSource for NoSlackSource {+ type Session = NoSlackSession;++ async fn open(&self, _: &MacroUserIdStr<'static>) -> Result<Self::Session, SlackSourceError> {+ Err(SlackSourceError::NotConnected)+ }+}++impl SlackWorkspaceSession for NoSlackSession {+ async fn list_conversations(+ &self,+ _: Option<&str>,+ ) -> Result<SlackConversationPage, SlackSourceError> {+ match *self {}+ }++ async fn conversation_members(+ &self,+ _: &SlackConversationId,+ _: Option<&str>,+ ) -> Result<SlackMemberPage, SlackSourceError> {+ match *self {}+ }++ async fn list_users(&self, _: Option<&str>) -> Result<SlackUserPage, SlackSourceError> {+ match *self {}+ }+}++async fn retry_rate_limited<T, F, Fut>(mut read: F) -> Result<T, SlackSourceError>+where+ F: FnMut() -> Fut,+ Fut: Future<Output = Result<T, SlackSourceError>>,+{+ match read().await {+ Err(SlackSourceError::RateLimited { retry_after }) => {+ tokio::time::sleep(+ retry_after+ .unwrap_or(Duration::from_secs(5))+ .min(RATE_LIMIT_MAX_SLEEP),+ )+ .await;+ read().await+ }+ result => result,+ }+}++pub(super) struct SlackDirectory(HashMap<SlackUserId, SlackUser>);++impl SlackDirectory {+ pub(super) async fn load(+ session: &impl SlackWorkspaceSession,+ ) -> Result<Self, SlackSourceError> {+ let mut users = HashMap::new();+ let mut cursor = None;+ for _ in 0..MAX_USER_PAGES {+ let page = retry_rate_limited(|| session.list_users(cursor.as_deref())).await?;+ users.extend(page.users.into_iter().map(|user| (user.id.clone(), user)));+ cursor = page.next_cursor;+ if cursor.is_none() {+ break;+ }+ }+ Ok(Self(users))+ }+}++/// Live membership context shared by the accepted Slack rows in one batch.+pub(super) struct SlackBatch<Sess> {+ state: Option<Option<(Sess, SlackDirectory, Vec<MacroUserIdStr<'static>>)>>,+}++impl<Sess> Default for SlackBatch<Sess> {+ fn default() -> Self {+ Self { state: None }+ }+}++impl<Sess: SlackWorkspaceSession> SlackBatch<Sess> {+ pub(super) async fn resolve_emails<R, S, C, W>(+ &mut self,+ service: &ImportServiceImpl<R, S, C, W>,+ user: &MacroUserIdStr<'static>,+ team_id: Uuid,+ meta: &SlackChannelMeta,+ ) -> Vec<String>+ where+ R: ImportRepo + CanonicalImportRepo + Clone,+ S: ConnectorSelect,+ C: EntityCreator,+ W: SlackWorkspaceSource<Session = Sess>,+ {+ if self.state.is_none() {+ let loaded: anyhow::Result<_> = async {+ let session = service.slack_source.open(user).await?;+ let directory = SlackDirectory::load(&session).await?;+ let roster = service.repo.team_members(team_id).await?;+ Ok((session, directory, roster))+ }+ .await;+ self.state = Some(+ loaded+ .inspect_err(|error| {+ tracing::warn!(error = ?error, "Slack batch membership unavailable");+ })+ .ok(),+ );+ }++ if let Some(Some((session, directory, roster))) = &self.state+ && let Some(channel) = meta+ .channel_id+ .as_deref()+ .and_then(SlackConversationId::new)+ {+ match resolve_members(session, directory, &channel, roster).await {+ Ok(members) => {+ return members+ .participants+ .into_iter()+ .filter_map(|participant| participant.email)+ .collect();+ }+ Err(error) => {+ tracing::warn!(channel = %channel.as_str(), error = ?error, "Slack live membership resolution failed");+ }+ }+ }+ tracing::warn!(channel = ?meta.channel_id, "using staged Slack participants instead of live membership");+ meta.participants+ .iter()+ .filter_map(|participant| participant.email.clone())+ .collect()+ }+}++pub(super) struct ResolvedMembers {+ pub(super) participants: Vec<SlackParticipant>,+ pub(super) member_count: u64,+}++pub(super) async fn resolve_members(+ session: &impl SlackWorkspaceSession,+ directory: &SlackDirectory,+ channel: &SlackConversationId,+ roster: &[MacroUserIdStr<'static>],+) -> Result<ResolvedMembers, SlackSourceError> {+ let mut resolved = ResolvedMembers {+ participants: Vec::new(),+ member_count: 0,+ };+ let mut seen = HashSet::new();+ let mut cursor = None;+ for _ in 0..MAX_MEMBER_PAGES {+ let page =+ retry_rate_limited(|| session.conversation_members(channel, cursor.as_deref())).await?;+ for id in page.members {+ if !seen.insert(id.clone()) {+ continue;+ }+ let Some(user) = directory+ .0+ .get(&id)+ .filter(|user| !user.is_bot && !user.deleted)+ else {+ continue;+ };+ resolved.member_count += 1;+ if let Some(email) = &user.email+ && roster+ .iter()+ .any(|member| member.email_str().eq_ignore_ascii_case(email))+ {+ resolved.participants.push(SlackParticipant {+ name: user.display_name.clone(),+ email: Some(email.clone()),+ });+ }+ }+ cursor = page.next_cursor;+ if cursor.is_none() {+ break;+ }+ }+ Ok(resolved)+}++/// Prefer larger active channels, retaining listing order among ties.+fn select_slack_candidates(+ mut channels: Vec<SlackConversation>,+ cap: usize,+) -> Vec<SlackConversation> {+ channels.retain(|channel| !channel.archived);+ channels.sort_by_key(|channel| std::cmp::Reverse(channel.member_count.unwrap_or(0)));+ channels.truncate(cap);+ channels+}++pub(super) async fn gather_slack<R, S, C, W>(+ service: &ImportServiceImpl<R, S, C, W>,+ user: &MacroUserIdStr<'static>,+ mode: GatherMode,+) -> Result<usize, SlackSourceError>+where+ R: ImportRepo + CanonicalImportRepo + Clone,+ S: ConnectorSelect,+ C: EntityCreator,+ W: SlackWorkspaceSource,+{+ // Anchor before opening/listing: discovery time consumes the enrichment budget.+ // Slack admission before this call does not perform asynchronous work.+ let enrichment_deadline =+ Instant::now() + gather_timeout(ImportSource::Slack, mode) - ENRICHMENT_TIMEOUT_RESERVE;+ let session = service.slack_source.open(user).await?;+ let mut channels = Vec::new();+ let mut seen = HashSet::new();+ let mut cursor = None;+ for _ in 0..MAX_CONVERSATION_PAGES {+ let page = retry_rate_limited(|| session.list_conversations(cursor.as_deref())).await?;+ channels.extend(page.conversations.into_iter().filter(|channel| {+ channel.kind == SlackConversationKind::PublicChannel && seen.insert(channel.id.clone())+ }));+ cursor = page.next_cursor;+ if cursor.is_none() {+ break;+ }+ }+ let initiator = match mode {+ GatherMode::Onboarding => {+ channels = select_slack_candidates(channels, 15);+ Initiator::Onboarding+ }+ GatherMode::Manual => Initiator::Manual,+ };+ let roster = match service+ .repo+ .user_team_id(user)+ .await+ .map_err(|e| SlackSourceError::Other(e.into()))?+ {+ Some(team) => service+ .repo+ .team_members(team)+ .await+ .map_err(|e| SlackSourceError::Other(e.into()))?,+ None => Vec::new(),+ };++ // Make candidates visible before the slower membership reads. Only staged+ // rows are eligible for enrichment; imported/declined rows stay untouched.+ let mut staged = Vec::new();+ for channel in channels {+ let metadata = SlackChannelMeta {+ name: channel.name,+ channel_id: Some(channel.id.as_str().to_string()),+ purpose: channel.purpose,+ participants: Vec::new(),+ member_count: channel.member_count,+ archived: channel.archived,+ members_resolved: false,+ };+ let outcome = service+ .stage_inner(+ user,+ initiator,+ ImportSource::Slack,+ channel.id.as_str(),+ serde_json::to_value(&metadata).map_err(|e| SlackSourceError::Other(e.into()))?,+ false,+ )+ .await;+ match outcome {+ Ok(StageOutcome::Staged(_)) => {+ staged.push((channel.id, metadata));+ if staged.len() % NOTIFY_EVERY == 0 {+ let _ = timeout_at(enrichment_deadline, service.notify(user)).await;+ }+ }+ Ok(_) => {}+ Err(error) => {+ tracing::warn!(channel = %channel.id.as_str(), error = ?error, "failed to stage Slack channel")+ }+ }+ }+ let _ = timeout_at(enrichment_deadline, service.notify(user)).await;+ let count = staged.len();+ if count == 0 || Instant::now() >= enrichment_deadline {+ return Ok(count);+ }+ let budget = match mode {+ GatherMode::Onboarding => count,+ GatherMode::Manual => MANUAL_MEMBER_BUDGET,+ };+ // Dropping this future stops reads/retries and notifications. Each metadata+ // upsert is atomic and only updates staged rows: cancellation leaves either+ // the original or enriched metadata, never an import claim to clean up.+ match timeout_at(+ enrichment_deadline,+ enrich_slack(service, user, &session, &roster, staged, initiator, budget),+ )+ .await+ {+ Ok(Ok(())) => {}+ Ok(Err(error)) => {+ tracing::warn!(error = ?error, "Slack enrichment failed; retaining staged channels");+ }+ Err(_) => {+ tracing::warn!("Slack enrichment deadline reached; retaining staged channels");+ }+ }+ Ok(count)+}++async fn enrich_slack<R, S, C, W>(+ service: &ImportServiceImpl<R, S, C, W>,+ user: &MacroUserIdStr<'static>,+ session: &W::Session,+ roster: &[MacroUserIdStr<'static>],+ mut staged: Vec<(SlackConversationId, SlackChannelMeta)>,+ initiator: Initiator,+ budget: usize,+) -> Result<(), SlackSourceError>+where+ R: ImportRepo + CanonicalImportRepo + Clone,+ S: ConnectorSelect,+ C: EntityCreator,+ W: SlackWorkspaceSource,+{+ let directory = match SlackDirectory::load(session).await {+ Ok(directory) => directory,+ Err(error) => {+ tracing::warn!(error = ?error, "Slack directory unavailable; leaving membership unresolved");+ return Ok(());+ }+ };+ staged.sort_by_key(|(_, metadata)| std::cmp::Reverse(metadata.member_count.unwrap_or(0)));+ let mut enriched = 0;+ for (channel, mut metadata) in staged.into_iter().take(budget) {+ let members = match resolve_members(session, &directory, &channel, roster).await {+ Ok(members) => members,+ Err(error) => {+ tracing::warn!(channel = %channel.as_str(), error = ?error, "Slack membership unavailable");+ continue;+ }+ };+ metadata.participants = members.participants;+ metadata.member_count = Some(members.member_count);+ metadata.members_resolved = true;+ match service+ .stage_inner(+ user,+ initiator,+ ImportSource::Slack,+ channel.as_str(),+ serde_json::to_value(metadata).map_err(|e| SlackSourceError::Other(e.into()))?,+ false,+ )+ .await+ {+ Ok(StageOutcome::Staged(_)) => {+ enriched += 1;+ if enriched % NOTIFY_EVERY == 0 {+ service.notify(user).await;+ }+ }+ // An import or user decision may have won since initial staging.+ Ok(_) => {}+ Err(error) => {+ tracing::warn!(channel = %channel.as_str(), error = ?error, "failed to enrich Slack channel")+ }+ }+ }+ service.notify(user).await;+ Ok(())+}diff --git a/crates/import/src/domain/service/slack_discovery/test.rs b/crates/import/src/domain/service/slack_discovery/test.rsnew file mode 100644index 0000000000..b6eb0f3e87--- /dev/null+++ b/crates/import/src/domain/service/slack_discovery/test.rs@@ -0,0 +1,1024 @@+use super::*;+use crate::domain::models::{ImportStatus, RunStatus};+use crate::domain::ports::ImportError;+use crate::domain::service::test::admission::{Creator, NoConnector, Repo, user};+use crate::domain::service::{ImportService, ImportStager, gather_timeout};+use std::sync::atomic::{AtomicUsize, Ordering};+use std::sync::{Arc, Mutex};++type Service<W = FakeSlackSource> = ImportServiceImpl<Repo, NoConnector, Creator, W>;++#[derive(Default)]+struct Workspace {+ conversations: Vec<SlackConversationPage>,+ users: Vec<SlackUserPage>,+ members: Vec<SlackMemberPage>,+ conversation_calls: Mutex<Vec<Option<String>>>,+ user_calls: Mutex<Vec<Option<String>>>,+ member_calls: Mutex<Vec<(String, Option<String>)>>,+ rate_limits: Mutex<HashMap<String, usize>>,+ open_calls: AtomicUsize,+ not_connected: bool,+ directory_fails: bool,+ listing_fails: bool,+ listing_delay: Duration,+ directory_delay: Duration,+ member_delays: HashMap<String, Duration>,+ observe_staged: Option<Repo>,+ import_during_members: Option<Repo>,+}++#[derive(Clone, Default)]+struct FakeSession(Arc<Workspace>);++struct FakeSlackSource(FakeSession);++impl SlackWorkspaceSource for FakeSlackSource {+ type Session = FakeSession;++ async fn open(&self, _: &MacroUserIdStr<'static>) -> Result<FakeSession, SlackSourceError> {+ self.0.0.open_calls.fetch_add(1, Ordering::SeqCst);+ if self.0.0.not_connected {+ return Err(SlackSourceError::NotConnected);+ }+ Ok(self.0.clone())+ }+}++fn page_index(cursor: Option<&str>) -> usize {+ cursor.map(|cursor| cursor.parse().unwrap()).unwrap_or(0)+}++impl SlackWorkspaceSession for FakeSession {+ async fn list_conversations(+ &self,+ cursor: Option<&str>,+ ) -> Result<SlackConversationPage, SlackSourceError> {+ self.0+ .conversation_calls+ .lock()+ .unwrap()+ .push(cursor.map(str::to_string));+ tokio::time::sleep(self.0.listing_delay).await;+ if self.0.listing_fails {+ return Err(SlackSourceError::MissingScope("channels:read".into()));+ }+ Ok(self+ .0+ .conversations+ .get(page_index(cursor))+ .cloned()+ .unwrap_or(SlackConversationPage {+ conversations: Vec::new(),+ next_cursor: None,+ }))+ }++ async fn list_users(&self, cursor: Option<&str>) -> Result<SlackUserPage, SlackSourceError> {+ self.0+ .user_calls+ .lock()+ .unwrap()+ .push(cursor.map(str::to_string));+ if let Some(repo) = &self.0.observe_staged {+ let rows = repo.list(&user(), None, None).await.unwrap();+ assert!(!rows.is_empty());+ assert!(+ rows.iter()+ .all(|row| row.metadata["members_resolved"] == false)+ );+ }+ tokio::time::sleep(self.0.directory_delay).await;+ if self.0.directory_fails {+ return Err(SlackSourceError::MissingScope("users:read".into()));+ }+ Ok(self+ .0+ .users+ .get(page_index(cursor))+ .cloned()+ .unwrap_or(SlackUserPage {+ users: Vec::new(),+ next_cursor: None,+ }))+ }++ async fn conversation_members(+ &self,+ channel: &SlackConversationId,+ cursor: Option<&str>,+ ) -> Result<SlackMemberPage, SlackSourceError> {+ self.0+ .member_calls+ .lock()+ .unwrap()+ .push((channel.as_str().into(), cursor.map(str::to_string)));+ {+ let mut limits = self.0.rate_limits.lock().unwrap();+ if let Some(remaining) = limits.get_mut(channel.as_str())+ && *remaining > 0+ {+ *remaining -= 1;+ return Err(SlackSourceError::RateLimited {+ retry_after: Some(Duration::ZERO),+ });+ }+ }+ if let Some(delay) = self.0.member_delays.get(channel.as_str()) {+ tokio::time::sleep(*delay).await;+ }+ if let Some(repo) = &self.0.import_during_members {+ let row = repo+ .get_own_by_foreign_id(&user(), ImportSource::Slack, channel.as_str())+ .await+ .unwrap()+ .unwrap();+ repo.mark_importing(&user(), &[row.id]).await.unwrap();+ }+ Ok(self+ .0+ .members+ .get(page_index(cursor))+ .cloned()+ .unwrap_or(SlackMemberPage {+ members: Vec::new(),+ next_cursor: None,+ }))+ }+}++fn channel(index: usize) -> SlackConversation {+ SlackConversation {+ id: SlackConversationId::new(&format!("C{index:010}")).unwrap(),+ name: format!("channel-{index}"),+ kind: SlackConversationKind::PublicChannel,+ archived: false,+ member_count: Some(index as u64),+ purpose: Some("Channel shape only".into()),+ }+}++fn slack_user(index: usize, email: Option<&str>) -> SlackUser {+ SlackUser {+ id: SlackUserId::new(&format!("U{index:010}")).unwrap(),+ display_name: format!("User {index}"),+ email: email.map(str::to_string),+ is_bot: false,+ deleted: false,+ }+}++fn workspace(channels: Vec<SlackConversation>) -> Workspace {+ Workspace {+ conversations: vec![SlackConversationPage {+ conversations: channels,+ next_cursor: None,+ }],+ ..Workspace::default()+ }+}++fn service(repo: Repo, workspace: Workspace) -> Service {+ ImportServiceImpl::new(+ repo,+ Arc::new(NoConnector),+ Arc::new(Creator::default()),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_slack_source(Arc::new(FakeSlackSource(FakeSession(Arc::new(workspace)))))+}++fn cursors(count: usize) -> Vec<Option<String>> {+ (0..count)+ .map(|index| (index > 0).then(|| index.to_string()))+ .collect()+}++#[tokio::test]+async fn pagination_follows_cursors_and_stops_at_each_bound() {+ let workspace = Workspace {+ conversations: (0..12)+ .map(|index| SlackConversationPage {+ conversations: vec![channel(index)],+ next_cursor: Some((index + 1).to_string()),+ })+ .collect(),+ users: (0..12)+ .map(|index| SlackUserPage {+ users: vec![slack_user(index, None)],+ next_cursor: Some((index + 1).to_string()),+ })+ .collect(),+ members: (0..12)+ .map(|index| SlackMemberPage {+ members: vec![slack_user(index, None).id],+ next_cursor: Some((index + 1).to_string()),+ })+ .collect(),+ ..Workspace::default()+ };+ let service = service(Repo::default(), workspace);+ assert_eq!(+ gather_slack(&service, &user(), GatherMode::Manual)+ .await+ .unwrap(),+ MAX_CONVERSATION_PAGES+ );+ let calls = &service.slack_source.0.0;+ assert_eq!(+ *calls.conversation_calls.lock().unwrap(),+ cursors(MAX_CONVERSATION_PAGES)+ );+ assert_eq!(*calls.user_calls.lock().unwrap(), cursors(MAX_USER_PAGES));+ let members = calls.member_calls.lock().unwrap();+ assert_eq!(members.len(), MAX_CONVERSATION_PAGES * MAX_MEMBER_PAGES);+ for reads in members.chunks(MAX_MEMBER_PAGES) {+ assert_eq!(+ reads+ .iter()+ .map(|(_, cursor)| cursor.clone())+ .collect::<Vec<_>>(),+ cursors(MAX_MEMBER_PAGES)+ );+ }+}++#[tokio::test]+async fn onboarding_ranks_caps_and_excludes_archived_and_unsupported_kinds() {+ let mut channels: Vec<_> = (0..20).map(channel).collect();+ channels[19].archived = true;+ channels.push(channel(18)); // duplicate across the listing+ for (index, kind) in [+ SlackConversationKind::PrivateChannel,+ SlackConversationKind::DirectMessage,+ SlackConversationKind::GroupDirectMessage,+ ]+ .into_iter()+ .enumerate()+ {+ let mut unsupported = channel(30 + index);+ unsupported.kind = kind;+ channels.push(unsupported);+ }+ let repo = Repo::default();+ let service = service(+ repo.clone(),+ Workspace {+ observe_staged: Some(repo),+ ..workspace(channels)+ },+ );+ assert_eq!(+ gather_slack(&service, &user(), GatherMode::Onboarding)+ .await+ .unwrap(),+ 15+ );+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 15);+ assert_eq!(rows[0].foreign_id, channel(18).id.as_str());+ assert_eq!(rows[14].foreign_id, channel(4).id.as_str());+ assert!(+ rows.iter()+ .all(|row| row.initiator == Initiator::Onboarding && row.metadata["archived"] == false)+ );+ assert_eq!(+ service.slack_source.0.0.member_calls.lock().unwrap().len(),+ 15+ );+}++#[tokio::test]+async fn manual_stages_all_public_channels_including_archived_but_bounds_enrichment() {+ let mut channels: Vec<_> = (0..102).map(channel).collect();+ channels[101].archived = true;+ channels.push(channel(101));+ for kind in [+ SlackConversationKind::PrivateChannel,+ SlackConversationKind::DirectMessage,+ SlackConversationKind::GroupDirectMessage,+ ] {+ let mut unsupported = channel(200);+ unsupported.kind = kind;+ channels.push(unsupported);+ }+ let service = service(Repo::default(), workspace(channels));+ assert_eq!(+ gather_slack(&service, &user(), GatherMode::Manual)+ .await+ .unwrap(),+ 102+ );+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 102);+ assert!(rows.iter().all(|row| row.initiator == Initiator::Manual));+ assert_eq!(rows[101].metadata["archived"], true);+ assert_eq!(rows[0].metadata["members_resolved"], false);+ assert_eq!(rows[1].metadata["members_resolved"], false);+ assert!(+ rows[2..]+ .iter()+ .all(|row| row.metadata["members_resolved"] == true)+ );+ let calls = service.slack_source.0.0.member_calls.lock().unwrap();+ assert_eq!(calls.len(), MANUAL_MEMBER_BUDGET);+ assert_eq!(calls[0].0, channel(101).id.as_str());+}++#[tokio::test]+async fn resolves_only_roster_matches_and_counts_all_known_humans() {+ let mut users = vec![+ slack_user(0, Some("[email protected]")),+ slack_user(1, Some("[email protected]")),+ slack_user(2, None),+ slack_user(3, Some("[email protected]")),+ slack_user(4, Some("[email protected]")),+ ];+ users[3].is_bot = true;+ users[4].deleted = true;+ let mut members: Vec<_> = users.iter().map(|user| user.id.clone()).collect();+ members.extend([users[0].id.clone(), slack_user(99, None).id]);+ let service = service(+ Repo::with_roster(vec![user()]),+ Workspace {+ users: vec![SlackUserPage {+ users,+ next_cursor: None,+ }],+ members: vec![SlackMemberPage {+ members,+ next_cursor: None,+ }],+ ..workspace(vec![channel(0)])+ },+ );+ gather_slack(&service, &user(), GatherMode::Manual)+ .await+ .unwrap();+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ let metadata: SlackChannelMeta = serde_json::from_value(rows[0].metadata.clone()).unwrap();+ assert_eq!(metadata.member_count, Some(3));+ assert_eq!(+ metadata.participants,+ vec![SlackParticipant {+ name: "User 0".into(),+ email: Some("[email protected]".into())+ }]+ );+ assert!(metadata.members_resolved);+}++#[tokio::test]+async fn rate_limited_channels_retry_once_and_persistent_errors_do_not_fail_gather() {+ let service = service(+ Repo::default(),+ Workspace {+ rate_limits: Mutex::new(HashMap::from([+ (channel(0).id.as_str().into(), 1),+ (channel(1).id.as_str().into(), 3),+ ])),+ ..workspace(vec![channel(0), channel(1), channel(2)])+ },+ );+ assert_eq!(+ gather_slack(&service, &user(), GatherMode::Manual)+ .await+ .unwrap(),+ 3+ );+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows[0].metadata["members_resolved"], true);+ assert_eq!(rows[1].metadata["members_resolved"], false);+ assert_eq!(rows[2].metadata["members_resolved"], true);+ let calls = service.slack_source.0.0.member_calls.lock().unwrap();+ for index in 0..2 {+ assert_eq!(+ calls+ .iter()+ .filter(|(id, _)| id == channel(index).id.as_str())+ .count(),+ 2+ );+ }+}++#[tokio::test]+async fn directory_failure_preserves_unresolved_candidates() {+ let service = service(+ Repo::default(),+ Workspace {+ directory_fails: true,+ ..workspace(vec![channel(0)])+ },+ );+ assert_eq!(+ gather_slack(&service, &user(), GatherMode::Manual)+ .await+ .unwrap(),+ 1+ );+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows[0].metadata["members_resolved"], false);+ assert!(+ service+ .slack_source+ .0+ .0+ .member_calls+ .lock()+ .unwrap()+ .is_empty()+ );+}++#[tokio::test(start_paused = true)]+async fn slow_directory_retains_staged_rows_before_both_outer_timeouts() {+ for mode in [GatherMode::Onboarding, GatherMode::Manual] {+ let outer_timeout = gather_timeout(ImportSource::Slack, mode);+ let repo = Repo::default();+ let service = service(+ repo.clone(),+ Workspace {+ listing_delay: Duration::from_secs(30),+ directory_delay: outer_timeout,+ observe_staged: Some(repo),+ ..workspace(vec![channel(0)])+ },+ );+ let start = Instant::now();+ assert_eq!(+ tokio::time::timeout(outer_timeout, gather_slack(&service, &user(), mode))+ .await+ .expect("best-effort directory must not hit the outer timeout")+ .unwrap(),+ 1+ );+ assert_eq!(start.elapsed(), outer_timeout - ENRICHMENT_TIMEOUT_RESERVE);+ assert!(start.elapsed() < outer_timeout);+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 1);+ assert_eq!(rows[0].status, ImportStatus::Staged);+ assert_eq!(rows[0].metadata["members_resolved"], false);+ let calls = &service.slack_source.0.0;+ assert_eq!(calls.user_calls.lock().unwrap().len(), 1);+ assert!(calls.member_calls.lock().unwrap().is_empty());+ }+}++#[tokio::test(start_paused = true)]+async fn listing_and_directory_share_member_deadline_and_preserve_completed_enrichment() {+ for mode in [GatherMode::Onboarding, GatherMode::Manual] {+ let outer_timeout = gather_timeout(ImportSource::Slack, mode);+ let service = service(+ Repo::default(),+ Workspace {+ listing_delay: Duration::from_secs(30),+ directory_delay: Duration::from_secs(40),+ // Channel 1 is enriched first; channel 0 never finishes.+ member_delays: HashMap::from([(channel(0).id.as_str().into(), outer_timeout)]),+ ..workspace(vec![channel(0), channel(1)])+ },+ );+ let start = Instant::now();+ assert_eq!(+ tokio::time::timeout(outer_timeout, gather_slack(&service, &user(), mode))+ .await+ .expect("best-effort membership must not hit the outer timeout")+ .unwrap(),+ 2+ );+ assert_eq!(start.elapsed(), outer_timeout - ENRICHMENT_TIMEOUT_RESERVE);+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 2);+ for row in rows {+ assert_eq!(row.status, ImportStatus::Staged);+ assert_eq!(+ row.metadata["members_resolved"],+ row.foreign_id == channel(1).id.as_str()+ );+ }+ assert_eq!(+ service.slack_source.0.0.member_calls.lock().unwrap().len(),+ 2+ );+ }+}++#[tokio::test(start_paused = true)]+async fn exhausted_listing_budget_skips_enrichment_without_failing_staged_rows() {+ let mode = GatherMode::Manual;+ let outer_timeout = gather_timeout(ImportSource::Slack, mode);+ let service = service(+ Repo::default(),+ Workspace {+ listing_delay: outer_timeout - Duration::from_secs(5),+ ..workspace(vec![channel(0)])+ },+ );+ assert_eq!(gather_slack(&service, &user(), mode).await.unwrap(), 1);+ let calls = &service.slack_source.0.0;+ assert!(calls.user_calls.lock().unwrap().is_empty());+ assert!(calls.member_calls.lock().unwrap().is_empty());+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows[0].metadata["members_resolved"], false);+}++#[tokio::test(start_paused = true)]+async fn enrichment_notifications_cannot_extend_the_deadline() {+ for notify_at in [1, 2, 3] {+ let calls = Arc::new(AtomicUsize::new(0));+ let mode = GatherMode::Manual;+ let outer_timeout = gather_timeout(ImportSource::Slack, mode);+ let service = service(Repo::default(), workspace((0..10).map(channel).collect()))+ .with_notifier(Arc::new({+ let calls = calls.clone();+ move |_| {+ let call = calls.fetch_add(1, Ordering::SeqCst);+ Box::pin(async move {+ // Calls 0/1 are staging, 2/3 are enrichment batch/final nudges.+ if call == notify_at {+ tokio::time::sleep(outer_timeout).await;+ }+ })+ }+ }));+ let start = Instant::now();+ assert_eq!(+ tokio::time::timeout(outer_timeout, gather_slack(&service, &user(), mode))+ .await+ .expect("notifications must not hit the outer timeout")+ .unwrap(),+ 10+ );+ assert_eq!(start.elapsed(), outer_timeout - ENRICHMENT_TIMEOUT_RESERVE);+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 10);+ assert!(rows.iter().all(|row| row.status == ImportStatus::Staged));+ }+}++#[tokio::test]+async fn listing_errors_still_propagate_before_staging() {+ let service = service(+ Repo::default(),+ Workspace {+ listing_fails: true,+ ..workspace(vec![channel(0)])+ },+ );+ for mode in [GatherMode::Onboarding, GatherMode::Manual] {+ assert!(matches!(+ gather_slack(&service, &user(), mode).await,+ Err(SlackSourceError::MissingScope(scope)) if scope == "channels:read"+ ));+ }+ assert!(+ service+ .repo+ .list(&user(), None, None)+ .await+ .unwrap()+ .is_empty()+ );+ assert!(+ service+ .slack_source+ .0+ .0+ .user_calls+ .lock()+ .unwrap()+ .is_empty()+ );+}++#[tokio::test]+async fn enrichment_does_not_overwrite_a_concurrent_import() {+ let repo = Repo::default();+ let service = service(+ repo.clone(),+ Workspace {+ import_during_members: Some(repo),+ ..workspace(vec![channel(0)])+ },+ );+ assert_eq!(+ gather_slack(&service, &user(), GatherMode::Manual)+ .await+ .unwrap(),+ 1+ );+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows[0].status, ImportStatus::Importing);+ assert_eq!(rows[0].metadata["members_resolved"], false);+}++#[tokio::test]+async fn silent_staging_and_discovery_notifications_are_batched() {+ let calls = Arc::new(AtomicUsize::new(0));+ let service = service(Repo::default(), workspace((0..21).map(channel).collect()))+ .with_notifier(Arc::new({+ let calls = calls.clone();+ move |_| {+ calls.fetch_add(1, Ordering::SeqCst);+ Box::pin(async {})+ }+ }));+ service+ .stage_inner(+ &user(),+ Initiator::Manual,+ ImportSource::Slack,+ channel(0).id.as_str(),+ serde_json::json!({"name": "general"}),+ false,+ )+ .await+ .unwrap();+ assert_eq!(calls.load(Ordering::SeqCst), 0);+ service+ .stage(+ &user(),+ Initiator::Manual,+ ImportSource::Slack,+ channel(0).id.as_str(),+ serde_json::json!({"name": "general"}),+ )+ .await+ .unwrap();+ assert_eq!(calls.swap(0, Ordering::SeqCst), 1);+ gather_slack(&service, &user(), GatherMode::Manual)+ .await+ .unwrap();+ // Two batches and a final notification for each of staging and enrichment.+ assert_eq!(calls.load(Ordering::SeqCst), 6);+}++async fn finished_run<W: SlackWorkspaceSource>(+ service: &Service<W>,+) -> crate::domain::models::ImportRun {+ tokio::time::timeout(Duration::from_secs(2), async {+ loop {+ let run = service+ .repo+ .list_runs(&user())+ .await+ .unwrap()+ .pop()+ .unwrap();+ if run.status != RunStatus::Running {+ return run;+ }+ tokio::task::yield_now().await;+ }+ })+ .await+ .unwrap()+}++#[tokio::test(start_paused = true)]+async fn spawn_gather_marks_slow_enrichment_ready_for_onboarding_and_manual() {+ for mode in [GatherMode::Onboarding, GatherMode::Manual] {+ for slow_directory in [true, false] {+ let outer_timeout = gather_timeout(ImportSource::Slack, mode);+ let mut workspace = workspace(vec![channel(0)]);+ if slow_directory {+ workspace.directory_delay = outer_timeout;+ } else {+ workspace.member_delays =+ HashMap::from([(channel(0).id.as_str().into(), outer_timeout)]);+ }+ let service = service(Repo::default(), workspace);+ let started = match mode {+ GatherMode::Onboarding => {+ service+ .start_gather(user(), ImportSource::Slack, false)+ .await+ }+ GatherMode::Manual => service.start_discovery(user(), ImportSource::Slack).await,+ };+ assert!(started.unwrap());+ // Let the spawned gather start, then advance virtual (not wall) time.+ tokio::task::yield_now().await;+ tokio::time::sleep(outer_timeout - ENRICHMENT_TIMEOUT_RESERVE).await;+ let run = finished_run(&service).await;+ assert_eq!(run.status, RunStatus::Ready);+ assert!(run.error.is_none());+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 1);+ assert_eq!(rows[0].status, ImportStatus::Staged);+ assert_eq!(rows[0].metadata["members_resolved"], false);+ }+ }+}++#[tokio::test]+async fn manual_without_connection_fails_readably_without_loading_connector_tools() {+ let service =+ service(Repo::default(), Workspace::default()).with_slack_source(Arc::new(NoSlackSource));+ assert!(+ service+ .start_discovery(user(), ImportSource::Slack)+ .await+ .unwrap()+ );+ let run = finished_run(&service).await;+ assert_eq!(run.status, RunStatus::Failed);+ assert_eq!(run.error.as_deref(), Some("Slack is not connected"));+ assert!(!run.auto_import);+}++struct MissingTools;+impl SlackWorkspaceSource for MissingTools {+ type Session = NoSlackSession;+ async fn open(&self, _: &MacroUserIdStr<'static>) -> Result<NoSlackSession, SlackSourceError> {+ Err(SlackSourceError::ToolsUnavailable("members"))+ }+}++#[tokio::test]+async fn manual_without_tools_fails_readably() {+ let service =+ service(Repo::default(), Workspace::default()).with_slack_source(Arc::new(MissingTools));+ assert!(+ service+ .start_discovery(user(), ImportSource::Slack)+ .await+ .unwrap()+ );+ let run = finished_run(&service).await;+ assert_eq!(run.status, RunStatus::Failed);+ assert_eq!(+ run.error.as_deref(),+ Some("Your Slack connection does not expose channel tools")+ );+}++#[tokio::test]+async fn manual_rejects_other_sources_and_does_not_restart_running_runs() {+ let service = service(Repo::default(), Workspace::default());+ let error = service+ .start_discovery(user(), ImportSource::Linear)+ .await+ .unwrap_err();+ assert!(matches!(+ error,+ ImportError::UnsupportedDiscovery(ImportSource::Linear)+ ));+ assert_eq!(+ error.to_string(),+ "manual discovery is not supported for linear"+ );+ service+ .repo+ .start_run(&user(), ImportSource::Slack, &[], true)+ .await+ .unwrap();+ assert!(+ !service+ .start_discovery(user(), ImportSource::Slack)+ .await+ .unwrap()+ );+ assert!(service.repo.list_runs(&user()).await.unwrap()[0].auto_import);+ assert!(+ service+ .slack_source+ .0+ .0+ .conversation_calls+ .lock()+ .unwrap()+ .is_empty()+ );+}++#[tokio::test]+async fn manual_restarts_terminal_runs_without_auto_import() {+ for status in [+ RunStatus::Ready,+ RunStatus::Completed,+ RunStatus::Failed,+ RunStatus::Dismissed,+ ] {+ let service = service(Repo::default(), workspace(vec![channel(0)]));+ service+ .repo+ .start_run(&user(), ImportSource::Slack, &[], true)+ .await+ .unwrap();+ service+ .repo+ .finish_run(&user(), ImportSource::Slack, status, None)+ .await+ .unwrap();+ assert!(+ service+ .start_discovery(user(), ImportSource::Slack)+ .await+ .unwrap()+ );+ let run = finished_run(&service).await;+ assert_eq!(run.status, RunStatus::Ready);+ assert!(!run.auto_import);+ }+}++fn staged_metadata(index: usize) -> SlackChannelMeta {+ SlackChannelMeta {+ name: format!("channel-{index}"),+ channel_id: Some(channel(index).id.as_str().into()),+ purpose: None,+ participants: vec![+ SlackParticipant {+ name: "Staged member".into(),+ email: Some("[email protected]".into()),+ },+ SlackParticipant {+ name: "No email".into(),+ email: None,+ },+ ],+ member_count: Some(2),+ archived: false,+ members_resolved: true,+ }+}++fn live_membership_workspace() -> Workspace {+ Workspace {+ users: vec![SlackUserPage {+ users: vec![slack_user(0, Some("[email protected]"))],+ next_cursor: None,+ }],+ members: vec![SlackMemberPage {+ members: vec![slack_user(0, None).id],+ next_cursor: None,+ }],+ ..Workspace::default()+ }+}++#[tokio::test]+async fn batch_lazily_opens_one_session_and_loads_directory_once_for_three_rows() {+ let service = service(Repo::with_roster(vec![user()]), live_membership_workspace());+ let team_id = service.repo.user_team_id(&user()).await.unwrap().unwrap();+ let mut batch = SlackBatch::default();+ let calls = &service.slack_source.0.0;+ assert_eq!(calls.open_calls.load(Ordering::SeqCst), 0);+ assert!(calls.user_calls.lock().unwrap().is_empty());++ for index in 0..3 {+ let emails = batch+ .resolve_emails(&service, &user(), team_id, &staged_metadata(index))+ .await;+ assert_eq!(emails, ["[email protected]"]);+ }++ assert_eq!(calls.open_calls.load(Ordering::SeqCst), 1);+ assert_eq!(*calls.user_calls.lock().unwrap(), [None]);+ assert_eq!(+ *calls.member_calls.lock().unwrap(),+ (0..3)+ .map(|index| (channel(index).id.as_str().into(), None))+ .collect::<Vec<_>>()+ );+ assert!(calls.conversation_calls.lock().unwrap().is_empty());+}++#[tokio::test]+async fn batch_live_emails_replace_staged_participants_even_when_no_members_match() {+ for roster in [vec![user()], Vec::new()] {+ let expected: Vec<_> = roster+ .iter()+ .map(|user| user.email_str().to_string())+ .collect();+ let service = service(Repo::with_roster(roster), live_membership_workspace());+ let team_id = service.repo.user_team_id(&user()).await.unwrap().unwrap();+ let emails = SlackBatch::default()+ .resolve_emails(&service, &user(), team_id, &staged_metadata(0))+ .await;+ assert_eq!(emails, expected);+ }+}++#[tokio::test]+async fn batch_not_connected_falls_back_without_reopening_for_later_rows() {+ let service = service(+ Repo::with_roster(vec![user()]),+ Workspace {+ not_connected: true,+ ..live_membership_workspace()+ },+ );+ let team_id = service.repo.user_team_id(&user()).await.unwrap().unwrap();+ let mut batch = SlackBatch::default();+ for index in 0..3 {+ assert_eq!(+ batch+ .resolve_emails(&service, &user(), team_id, &staged_metadata(index))+ .await,+ ["[email protected]"]+ );+ }+ let calls = &service.slack_source.0.0;+ assert_eq!(calls.open_calls.load(Ordering::SeqCst), 1);+ assert!(calls.user_calls.lock().unwrap().is_empty());+ assert!(calls.member_calls.lock().unwrap().is_empty());+}++#[tokio::test]+async fn batch_directory_failure_is_cached_for_later_rows() {+ let service = service(+ Repo::with_roster(vec![user()]),+ Workspace {+ directory_fails: true,+ ..live_membership_workspace()+ },+ );+ let team_id = service.repo.user_team_id(&user()).await.unwrap().unwrap();+ let mut batch = SlackBatch::default();+ for index in 0..2 {+ assert_eq!(+ batch+ .resolve_emails(&service, &user(), team_id, &staged_metadata(index))+ .await,+ ["[email protected]"]+ );+ }+ let calls = &service.slack_source.0.0;+ assert_eq!(calls.open_calls.load(Ordering::SeqCst), 1);+ assert_eq!(*calls.user_calls.lock().unwrap(), [None]);+ assert!(calls.member_calls.lock().unwrap().is_empty());+}++#[tokio::test]+async fn batch_member_error_falls_back_but_next_row_uses_live_session() {+ let service = service(+ Repo::with_roster(vec![user()]),+ Workspace {+ rate_limits: Mutex::new(HashMap::from([(channel(0).id.as_str().into(), 2)])),+ ..live_membership_workspace()+ },+ );+ let team_id = service.repo.user_team_id(&user()).await.unwrap().unwrap();+ let mut batch = SlackBatch::default();+ assert_eq!(+ batch+ .resolve_emails(&service, &user(), team_id, &staged_metadata(0))+ .await,+ ["[email protected]"]+ );+ assert_eq!(+ batch+ .resolve_emails(&service, &user(), team_id, &staged_metadata(1))+ .await,+ ["[email protected]"]+ );+ let calls = &service.slack_source.0.0;+ assert_eq!(calls.open_calls.load(Ordering::SeqCst), 1);+ assert_eq!(*calls.user_calls.lock().unwrap(), [None]);+ assert_eq!(calls.member_calls.lock().unwrap().len(), 3);+}++#[tokio::test]+async fn batch_missing_or_invalid_channel_id_falls_back_without_poisoning_session() {+ let service = service(Repo::with_roster(vec![user()]), live_membership_workspace());+ let team_id = service.repo.user_team_id(&user()).await.unwrap().unwrap();+ let mut batch = SlackBatch::default();+ for channel_id in [None, Some("#general".into())] {+ let mut metadata = staged_metadata(0);+ metadata.channel_id = channel_id;+ assert_eq!(+ batch+ .resolve_emails(&service, &user(), team_id, &metadata)+ .await,+ ["[email protected]"]+ );+ }+ let calls = &service.slack_source.0.0;+ assert!(calls.member_calls.lock().unwrap().is_empty());+ assert_eq!(+ batch+ .resolve_emails(&service, &user(), team_id, &staged_metadata(1))+ .await,+ ["[email protected]"]+ );+ assert_eq!(calls.open_calls.load(Ordering::SeqCst), 1);+}++#[test]+fn gather_deadlines_depend_on_source_and_mode() {+ assert_eq!(+ gather_timeout(ImportSource::Slack, GatherMode::Onboarding),+ Duration::from_secs(180)+ );+ assert_eq!(+ gather_timeout(ImportSource::Slack, GatherMode::Manual),+ Duration::from_secs(360)+ );+ assert_eq!(+ gather_timeout(ImportSource::Notion, GatherMode::Onboarding),+ Duration::from_secs(90)+ );+}diff --git a/crates/import/src/domain/service/test.rs b/crates/import/src/domain/service/test.rsindex b7b7c95d04..9b71aa3815 100644--- a/crates/import/src/domain/service/test.rs+++ b/crates/import/src/domain/service/test.rs@@ -1,6 +1,6 @@use super::*;-mod admission;+pub(super) mod admission;fn meta(identifier: Option<&str>,@@ -126,175 +126,6 @@ fn slack_gather_lists_channels_with_an_explicit_empty_query() {assert!(prompt.contains("Participant details are optional"));}-#[test]-fn slack_channel_search_tool_names_match_by_shape() {- assert!(is_slack_channel_search_tool_name(- "mcp__Slack__search_channels"- ));- assert!(is_slack_channel_search_tool_name(- "mcp__Slack__slack_search_channels"- ));- assert!(is_slack_channel_search_tool_name(- "mcp__Slack__search-channels"- ));- assert!(is_slack_channel_search_tool_name(- "mcp__Slack__list_channels"- ));- assert!(is_slack_channel_search_tool_name(- "mcp__Slack__conversations_list"- ));-- // Other channel-adjacent surfaces must not be mistaken for the listing.- assert!(!is_slack_channel_search_tool_name(- "mcp__Slack__list_channel_members"- ));- assert!(!is_slack_channel_search_tool_name(- "mcp__Slack__search_messages_and_files"- ));- assert!(!is_slack_channel_search_tool_name(- "mcp__Slack__get_channel_history"- ));- assert!(!is_slack_channel_search_tool_name(- "mcp__Slack__create_channel"- ));- assert!(!is_slack_channel_search_tool_name(- "mcp__Slack__search_users"- ));- assert!(!is_slack_channel_search_tool_name("search_channels")); // unmangled-}--#[test]-fn slack_channel_page_parses_structured_results_with_cursors() {- let page = parse_slack_channel_page(serde_json::json!({- "channels": [- {- "id": "C0123456789",- "name": "engineering",- "purpose": { "value": "Build the thing" },- "member_count": 42,- "is_archived": false,- },- {- "id": "C0000000001",- "name": "#design",- "topic": "Make it pretty",- "num_members": 7,- },- { "id": "D0123456789", "name": "dm", "is_im": true },- { "id": "C0000000002", "name": "old-stuff", "is_archived": true },- ],- "response_metadata": { "next_cursor": "cursor-2" },- }));-- assert_eq!(page.next_cursor.as_deref(), Some("cursor-2"));- assert_eq!(- page.channels,- vec![- SlackChannelCandidate {- id: Some("C0123456789".into()),- name: "engineering".into(),- purpose: Some("Build the thing".into()),- member_count: Some(42),- archived: false,- },- SlackChannelCandidate {- id: Some("C0000000001".into()),- name: "design".into(),- purpose: Some("Make it pretty".into()),- member_count: Some(7),- archived: false,- },- SlackChannelCandidate {- id: Some("C0000000002".into()),- name: "old-stuff".into(),- purpose: None,- member_count: None,- archived: true,- },- ]- );-}--#[test]-fn slack_channel_page_parses_json_text_and_bare_arrays() {- // Results without structured content arrive as JSON re-encoded as text.- let page = parse_slack_channel_page(serde_json::Value::String(- r#"{"results": [{"id": "C1", "name": "general"}], "next_cursor": "abc"}"#.to_string(),- ));- assert_eq!(page.channels.len(), 1);- assert_eq!(page.channels[0].name, "general");- assert_eq!(page.next_cursor.as_deref(), Some("abc"));-- let page = parse_slack_channel_page(serde_json::json!([- { "id": "C1", "name": "general" },- { "id": "C2", "name": "random" },- ]));- assert_eq!(page.channels.len(), 2);- assert_eq!(page.next_cursor, None);-- // Plain prose is not a channel list.- let page = parse_slack_channel_page(serde_json::Value::String("No channels found.".into()));- assert_eq!(page, SlackChannelPage::default());-- // An empty cursor string means "no more pages", not a page named "".- let page = parse_slack_channel_page(serde_json::json!({- "channels": [{ "id": "C1", "name": "general" }],- "response_metadata": { "next_cursor": "" },- }));- assert_eq!(page.next_cursor, None);-}--#[test]-fn slack_candidates_rank_by_size_dedupe_and_cap() {- let channel = |id: &str, name: &str, members: Option<u64>| SlackChannelCandidate {- id: Some(id.to_string()),- name: name.to_string(),- purpose: None,- member_count: members,- archived: false,- };-- let mut channels = vec![- channel("C1", "tiny", Some(2)),- channel("C2", "big", Some(50)),- channel("C2", "big", Some(50)), // duplicate id collapses- channel("C3", "medium", Some(10)),- SlackChannelCandidate {- archived: true,- ..channel("C4", "archived", Some(99))- },- // No id: deduped by name instead.- SlackChannelCandidate {- id: None,- ..channel("", "no-id", None)- },- SlackChannelCandidate {- id: None,- ..channel("", "no-id", None)- },- ];- channels- .extend((0..20).map(|i| channel(&format!("C{}", 100 + i), &format!("filler-{i}"), None)));-- let selected = select_slack_candidates(channels, SLACK_GATHER_MAX_CHANNELS);- assert_eq!(selected.len(), SLACK_GATHER_MAX_CHANNELS);- // Largest first; archived and duplicates gone.- assert_eq!(selected[0].name, "big");- assert_eq!(selected[1].name, "medium");- assert_eq!(selected[2].name, "tiny");- assert!(selected.iter().all(|channel| !channel.archived));- assert_eq!(- selected- .iter()- .filter(|channel| channel.name == "no-id")- .count(),- 1- );- // Ties keep listing order.- assert_eq!(selected[3].name, "no-id");- assert_eq!(selected[4].name, "filler-0");-}-#[test]fn notion_fetch_text_parses_the_fetch_document_shape() {let parsed = parse_notion_fetch_result(serde_json::Value::String(diff --git a/crates/import/src/domain/service/test/admission.rs b/crates/import/src/domain/service/test/admission.rsindex cebf1c4f25..e4e81a4d45 100644--- a/crates/import/src/domain/service/test/admission.rs+++ b/crates/import/src/domain/service/test/admission.rs@@ -3,7 +3,7 @@ use ai_billing::{AiAdmissionError, AiAdmissionService, DenyReason};use std::sync::Mutex;use std::sync::atomic::{AtomicUsize, Ordering};-fn user() -> MacroUserIdStr<'static> {+pub(in crate::domain::service) fn user() -> MacroUserIdStr<'static> {MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()}@@ -47,17 +47,26 @@ impl AiAdmissionService for Admission {}#[derive(Default, Clone)]-struct Repo(Arc<Mutex<Ledger>>);+pub(in crate::domain::service) struct Repo(Arc<Mutex<Ledger>>);#[derive(Default)]struct Ledger {rows: Vec<ImportEntity>,runs: Vec<ImportRun>,team_id: Option<Uuid>,+ roster: Vec<MacroUserIdStr<'static>>,target: Option<ImportTargetReservation>,}impl Repo {+ pub(in crate::domain::service) fn with_roster(roster: Vec<MacroUserIdStr<'static>>) -> Self {+ Self(Arc::new(Mutex::new(Ledger {+ team_id: Some(Uuid::now_v7()),+ roster,+ ..Ledger::default()+ })))+ }+fn seed(&self, source: ImportSource) -> ImportEntity {let row = ImportEntity {id: Uuid::now_v7(),@@ -168,6 +177,25 @@ impl ImportRepo for Repo {}Ok(true)}+ async fn start_manual_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ ) -> Result<bool> {+ let won = self.start_run(user, source, from, false).await?;+ if won {+ self.0+ .lock()+ .unwrap()+ .runs+ .iter_mut()+ .find(|run| run.source == source)+ .unwrap()+ .auto_import = false;+ }+ Ok(won)+ }async fn finish_run(&self,_: &MacroUserIdStr<'static>,@@ -290,6 +318,11 @@ impl ImportRepo for Repo {async fn user_team_id(&self, _: &MacroUserIdStr<'static>) -> Result<Option<Uuid>> {Ok(self.0.lock().unwrap().team_id)}+ async fn team_members(&self, team: Uuid) -> Result<Vec<MacroUserIdStr<'static>>> {+ let ledger = self.0.lock().unwrap();+ assert_eq!(Some(team), ledger.team_id);+ Ok(ledger.roster.clone())+ }async fn fail_stale_importing(&self, _: &MacroUserIdStr<'static>, _: i64) -> Result<u64> {Ok(0)}@@ -330,6 +363,20 @@ impl ImportRepo for Repo {foreign_id: &str,metadata: &serde_json::Value,) -> Result<Option<ImportEntity>> {+ {+ let mut ledger = self.0.lock().unwrap();+ if let Some(row) = ledger+ .rows+ .iter_mut()+ .find(|row| row.source == source && row.foreign_id == foreign_id)+ {+ if row.status != ImportStatus::Staged {+ return Ok(None);+ }+ row.metadata = metadata.clone();+ return Ok(Some(row.clone()));+ }+ }let mut row = self.seed(source);row.foreign_id = foreign_id.into();row.initiator = initiator;@@ -369,7 +416,7 @@ impl ImportRepo for Repo {_: &MacroUserIdStr<'static>,_: ImportSource,) -> Result<Option<Vec<ImportEntity>>> {- unreachable!()+ Ok(None)}async fn finish_auto_import(&self,@@ -381,7 +428,7 @@ impl ImportRepo for Repo {}}-struct NoConnector;+pub(in crate::domain::service) struct NoConnector;impl ConnectorSelect for NoConnector {async fn user_toolset(&self, _: &MacroUserIdStr<'static>) -> UserMcpTools {panic!("unexpected connector call")@@ -403,7 +450,7 @@ impl ConnectorSelect for NoConnector {}#[derive(Default)]-struct Creator(AtomicUsize);+pub(in crate::domain::service) struct Creator(AtomicUsize);impl EntityCreator for Creator {async fn create_task(&self,@@ -437,7 +484,7 @@ impl EntityCreator for Creator {}}-type Service = ImportServiceImpl<Repo, NoConnector, Creator>;+type Service<W = NoSlackSource> = ImportServiceImpl<Repo, NoConnector, Creator, W>;fn service(admission: Arc<Admission>) -> Service {ImportServiceImpl::new(Repo::default(),@@ -664,7 +711,7 @@ async fn default_constructor_keeps_admission_disabled() {}#[tokio::test]-async fn slack_direct_gather_bypasses_admission_but_fallback_does_not() {+async fn slack_agent_fallback_still_requires_admission() {let admission = Admission::refusing(denied());let service = service(admission.clone());assert!(@@ -673,33 +720,19 @@ async fn slack_direct_gather_bypasses_admission_but_fallback_does_not() {.await.unwrap());- let tools = Arc::new(Tools {- name: "mcp__Slack__search_channels",- result: serde_json::json!({"channels": [{"id": "C0123456789", "name": "general"}]}),- calls: AtomicUsize::new(0),- });- service- .gather_with_tools(&user(), ImportSource::Slack, tools.clone())- .await- .unwrap();- assert_eq!(tools.calls.load(Ordering::SeqCst), 1);- assert_eq!(service.repo.0.lock().unwrap().rows.len(), 1);assert_eq!(admission.calls.load(Ordering::SeqCst), 0);- let empty = Arc::new(Tools {- name: "mcp__Slack__search_channels",- result: serde_json::json!({"channels": []}),+ let tools = Arc::new(Tools {+ name: "unused",+ result: serde_json::json!({}),calls: AtomicUsize::new(0),});let error = service- .gather_with_tools(&user(), ImportSource::Slack, empty)+ .gather_agent_session(&user(), ImportSource::Slack, GATHER_MODEL, tools.clone()).await.unwrap_err();assert_eq!(error.downcast_ref::<AiAdmissionError>(), Some(&denied()));- assert_eq!(- admission.calls.load(Ordering::SeqCst),- 1,- "no alternate model after refusal"- );+ assert_eq!(admission.calls.load(Ordering::SeqCst), 1);+ assert_eq!(tools.calls.load(Ordering::SeqCst), 0);}#[tokio::test]diff --git a/crates/import/src/inbound/axum_router.rs b/crates/import/src/inbound/axum_router.rsindex 2e8e7f8873..84080399f7 100644--- a/crates/import/src/inbound/axum_router.rs+++ b/crates/import/src/inbound/axum_router.rs@@ -79,6 +79,10 @@ where"/import/runs/{source}/retry",post(retry_gather_handler::<T, Auth>),)+ .route(+ "/import/runs/{source}/discover",+ post(discover_handler::<T, Auth>),+ ).route("/import/runs/{source}/dismiss",post(dismiss_run_handler::<T, Auth>),@@ -186,6 +190,39 @@ pub async fn retry_gather_handler<T: ImportService, Auth: MacroAuthorizationServ}}+/// Discover candidates for user selection without auto-importing them.+#[utoipa::path(+ post,+ path = "/import/runs/{source}/discover",+ params(("source" = String, Path, description = "Import source")),+ responses(+ (status = 204, description = "Discovery accepted (idempotent)"),+ (status = 400, description = "Unknown import source or discovery not supported"),+ (status = 500, description = "Internal server error"),+ ),+ tag = "import"+)]+#[tracing::instrument(skip(service, user), fields(user_id = %user.authorization.user.macro_user_id))]+pub async fn discover_handler<T: ImportService, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth, UserOrInternal>,+ Path(source): Path<String>,+) -> Response {+ let Ok(source) = ImportSource::from_str(&source) else {+ return unknown_source_response(&source);+ };+ match service+ .start_discovery(user.authorization.user.macro_user_id, source)+ .await+ {+ Ok(_) => StatusCode::NO_CONTENT.into_response(),+ Err(e @ ImportError::UnsupportedDiscovery(_)) => {+ (StatusCode::BAD_REQUEST, e.to_string()).into_response()+ }+ Err(e) => error_response(e),+ }+}+/// Dismiss one source's import section.#[utoipa::path(post,diff --git a/crates/import/src/inbound/axum_router/test.rs b/crates/import/src/inbound/axum_router/test.rsindex d106c68c1c..bd318d2662 100644--- a/crates/import/src/inbound/axum_router/test.rs+++ b/crates/import/src/inbound/axum_router/test.rs@@ -1,5 +1,226 @@use super::*;use ai_billing::{AiAdmissionError, DenyReason};+use axum::{body::Body, http::Request};+use macro_authorization::{InternalIdentityClaims, MacroAuthorizationError};+use macro_user_id::user_id::MacroUserIdStr;+use model_user::UserContext;+use rootcause::Report;+use std::sync::Mutex;+use tower::ServiceExt;++use crate::domain::{models::Initiator, ports::Result};++const TEST_USER: &str = "macro|[email protected]";++#[derive(Clone)]+struct FakeAuthorization;++impl MacroAuthorizationService for FakeAuthorization {+ async fn authorize(+ &self,+ jwt: &str,+ ) -> std::result::Result<UserContext, Report<MacroAuthorizationError>> {+ if jwt != "valid" {+ return Err(Report::new(MacroAuthorizationError::InvalidCredentials));+ }+ Ok(UserContext {+ user_id: TEST_USER.to_string(),+ fusion_user_id: "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb".to_string(),+ permissions: None,+ organization_id: None,+ })+ }++ async fn authorize_internal(+ &self,+ _provided_key: &str,+ _claims: InternalIdentityClaims,+ ) -> std::result::Result<Option<UserContext>, Report<MacroAuthorizationError>> {+ Err(Report::new(MacroAuthorizationError::InvalidCredentials))+ }+}++struct FakeImportService {+ calls: Mutex<Vec<(MacroUserIdStr<'static>, ImportSource)>>,+ outcome: Mutex<Option<Result<bool>>>,+}++impl ImportService for FakeImportService {+ async fn start_discovery(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> Result<bool> {+ self.calls.lock().unwrap().push((user, source));+ self.outcome.lock().unwrap().take().unwrap()+ }++ async fn state(&self, _user: MacroUserIdStr<'static>) -> Result<ImportState> {+ unimplemented!("discovery must not read state")+ }++ async fn start_gather(+ &self,+ _user: MacroUserIdStr<'static>,+ _source: ImportSource,+ _auto_import: bool,+ ) -> Result<bool> {+ unimplemented!("discovery must not start an onboarding gather")+ }++ async fn retry_gather(+ &self,+ _user: MacroUserIdStr<'static>,+ _source: ImportSource,+ ) -> Result<bool> {+ unimplemented!("discovery must not retry a gather")+ }++ async fn dismiss_run(+ &self,+ _user: MacroUserIdStr<'static>,+ _source: ImportSource,+ ) -> Result<()> {+ unimplemented!()+ }++ async fn run_import(+ &self,+ _user: MacroUserIdStr<'static>,+ _import_ids: Vec<Uuid>,+ _discard_ids: Vec<Uuid>,+ ) -> Result<RunImportOutcome> {+ unimplemented!("discovery must not import candidates")+ }++ async fn discard_staged_by_initiator(+ &self,+ _user: MacroUserIdStr<'static>,+ _initiator: Initiator,+ ) -> Result<u64> {+ unimplemented!()+ }++ async fn delete_staged_by_initiator(+ &self,+ _user: MacroUserIdStr<'static>,+ _initiator: Initiator,+ ) -> Result<u64> {+ unimplemented!()+ }+}++fn discovery_router(outcome: Result<bool>) -> (Router, Arc<FakeImportService>) {+ let service = Arc::new(FakeImportService {+ calls: Mutex::new(Vec::new()),+ outcome: Mutex::new(Some(outcome)),+ });+ let router = import_router(ImportRouterState {+ service: service.clone(),+ authorization_state: MacroAuthorizationState::new(Arc::new(FakeAuthorization)),+ });+ (router, service)+}++fn discovery_request(source: &str, authenticated: bool) -> Request<Body> {+ let mut request = Request::builder()+ .method("POST")+ .uri(format!("/import/runs/{source}/discover"));+ if authenticated {+ request = request.header("authorization", "Bearer valid");+ }+ request.body(Body::empty()).unwrap()+}++#[tokio::test]+async fn discovery_rejects_unknown_source_without_calling_service() {+ let (router, service) = discovery_router(Ok(true));+ let response = router+ .oneshot(discovery_request("unknown", true))+ .await+ .unwrap();++ assert_eq!(response.status(), StatusCode::BAD_REQUEST);+ assert!(service.calls.lock().unwrap().is_empty());+ let body = axum::body::to_bytes(response.into_body(), 4096)+ .await+ .unwrap();+ assert_eq!(body, "unknown import source: unknown");+}++#[tokio::test]+async fn discovery_returns_unsupported_source_message() {+ let (router, service) =+ discovery_router(Err(ImportError::UnsupportedDiscovery(ImportSource::Linear)));+ let response = router+ .oneshot(discovery_request("linear", true))+ .await+ .unwrap();++ assert_eq!(response.status(), StatusCode::BAD_REQUEST);+ let body = axum::body::to_bytes(response.into_body(), 4096)+ .await+ .unwrap();+ assert!(+ std::str::from_utf8(&body)+ .unwrap()+ .contains("not supported")+ );+ assert_eq!(service.calls.lock().unwrap()[0].1, ImportSource::Linear);+}++#[tokio::test]+async fn discovery_accepts_slack_idempotently_and_forwards_identity() {+ for started in [true, false] {+ let (router, service) = discovery_router(Ok(started));+ let response = router+ .oneshot(discovery_request("slack", true))+ .await+ .unwrap();++ assert_eq!(response.status(), StatusCode::NO_CONTENT);+ let body = axum::body::to_bytes(response.into_body(), 4096)+ .await+ .unwrap();+ assert!(body.is_empty());+ assert_eq!(+ *service.calls.lock().unwrap(),+ vec![(+ MacroUserIdStr::parse_from_str(TEST_USER).unwrap(),+ ImportSource::Slack+ )]+ );+ }+}++#[tokio::test]+async fn discovery_requires_authentication() {+ let (router, service) = discovery_router(Ok(true));+ let response = router+ .oneshot(discovery_request("slack", false))+ .await+ .unwrap();++ assert_eq!(response.status(), StatusCode::UNAUTHORIZED);+ assert!(service.calls.lock().unwrap().is_empty());+}++#[tokio::test]+async fn discovery_hides_internal_error_details() {+ let (router, _) = discovery_router(Err(ImportError::Other(anyhow::anyhow!(+ "private diagnostic"+ ))));+ let response = router+ .oneshot(discovery_request("slack", true))+ .await+ .unwrap();++ assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);+ let body = axum::body::to_bytes(response.into_body(), 4096)+ .await+ .unwrap();+ assert!(body.is_empty());+}#[tokio::test]async fn admission_failures_use_shared_http_contract() {diff --git a/crates/import/src/outbound/mcp_slack_source.rs b/crates/import/src/outbound/mcp_slack_source.rsnew file mode 100644index 0000000000..8e61a9ea3e--- /dev/null+++ b/crates/import/src/outbound/mcp_slack_source.rs@@ -0,0 +1,288 @@+//! Live Slack workspace reads through the selected user's MCP connector.++use std::collections::HashMap;+use std::sync::{Arc, LazyLock};+use std::time::Duration;++use ai_toolset::{RequestContext, ToolSet};+use macro_user_id::user_id::MacroUserIdStr;+use mcp_select::{ConnectorSelect, UserMcpTools};+use serde_json::{Value, json};++use crate::domain::models::{+ ImportSource, SlackConversationId, SlackConversationPage, SlackMemberPage, SlackUserPage,+};+use crate::domain::ports::{SlackSourceError, SlackWorkspaceSession, SlackWorkspaceSource};++mod parse;+#[cfg(test)]+mod test;++/// Opens Slack readers without depending on either connector stack's adapters.+pub struct McpSlackSource<S: ConnectorSelect> {+ select: Arc<S>,+}++impl<S: ConnectorSelect> McpSlackSource<S> {+ /// Use the host's connector selector.+ pub fn new(select: Arc<S>) -> Self {+ Self { select }+ }+}++impl<S: ConnectorSelect> SlackWorkspaceSource for McpSlackSource<S> {+ type Session = McpSlackSession;++ async fn open(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> Result<Self::Session, SlackSourceError> {+ let tools = self+ .select+ .connector_toolset(user, ImportSource::Slack.connector_ref())+ .await?+ .filter(|tools| !tools.is_empty())+ .ok_or(SlackSourceError::NotConnected)?;+ let schemas = tool_schemas(&tools);+ let channels_tool = resolve_tool(&schemas, Listing::Channels)?;+ let members_tool = resolve_tool(&schemas, Listing::Members).ok();+ let users_tool = resolve_tool(&schemas, Listing::Users).ok();+ Ok(McpSlackSession {+ tools: Arc::new(tools),+ user: user.clone(),+ channels_tool,+ members_tool,+ users_tool,+ schemas,+ })+ }+}++/// Connector tools and schemas loaded once for a gather or import batch.+pub struct McpSlackSession {+ tools: Arc<UserMcpTools>,+ user: MacroUserIdStr<'static>,+ channels_tool: String,+ members_tool: Option<String>,+ users_tool: Option<String>,+ schemas: HashMap<String, Value>,+}++impl SlackWorkspaceSession for McpSlackSession {+ async fn list_conversations(+ &self,+ cursor: Option<&str>,+ ) -> Result<SlackConversationPage, SlackSourceError> {+ let arguments = listing_arguments(&self.schemas[&self.channels_tool], cursor, None, true);+ let result = call_channels(+ self.tools.as_ref(),+ &self.user,+ &self.channels_tool,+ &arguments,+ cursor.is_none(),+ )+ .await?;+ parse::parse_slack_channel_page(result)+ }++ async fn conversation_members(+ &self,+ conversation: &SlackConversationId,+ cursor: Option<&str>,+ ) -> Result<SlackMemberPage, SlackSourceError> {+ let tool = self+ .members_tool+ .as_ref()+ .ok_or(SlackSourceError::ToolsUnavailable("members"))?;+ let schema = &self.schemas[tool];+ if !["channel", "conversation", "channel_id"]+ .iter()+ .any(|key| schema["properties"].get(key).is_some())+ {+ return Err(SlackSourceError::ToolsUnavailable("members"));+ }+ let arguments = listing_arguments(schema, cursor, Some(conversation), false);+ parse::parse_member_page(+ call_tool(self.tools.as_ref(), &self.user, tool, &arguments).await?,+ )+ }++ async fn list_users(&self, cursor: Option<&str>) -> Result<SlackUserPage, SlackSourceError> {+ let tool = self+ .users_tool+ .as_ref()+ .ok_or(SlackSourceError::ToolsUnavailable("users"))?;+ let arguments = listing_arguments(&self.schemas[tool], cursor, None, false);+ parse::parse_user_page(call_tool(self.tools.as_ref(), &self.user, tool, &arguments).await?)+ }+}++fn tool_schemas(tools: &impl ToolSet<()>) -> HashMap<String, Value> {+ ToolSet::<()>::request_schemas(tools)+ .unwrap_or_default()+ .into_iter()+ .map(|schema| (schema.name, schema.schema.to_value()))+ .collect()+}++#[derive(Clone, Copy)]+enum Listing {+ Channels,+ Members,+ Users,+}++fn resolve_tool(+ schemas: &HashMap<String, Value>,+ listing: Listing,+) -> Result<String, SlackSourceError> {+ let (action, capability) = match listing {+ Listing::Channels => ("list-channels", "channel listing"),+ Listing::Members => ("list-members-in-channel", "members"),+ Listing::Users => ("list-users", "users"),+ };+ // Sorting makes fallback selection stable across HashMap iteration order.+ let mut names: Vec<_> = schemas.keys().collect();+ names.sort();+ for prefix in ["slack_v2", "slack"] {+ let exact = format!("{prefix}-{action}");+ if let Some(name) = names.iter().find(|name| tool_suffix(name) == exact) {+ return Ok((*name).clone());+ }+ }+ names+ .into_iter()+ .find(|name| {+ let tool = tool_suffix(name).to_ascii_lowercase().replace('-', "_");+ match listing {+ Listing::Channels => is_slack_channel_search_tool_name(name),+ Listing::Members => {+ tool.contains("member")+ && (tool.contains("channel") || tool.contains("conversation"))+ && (tool.contains("list") || tool.contains("get"))+ }+ Listing::Users => {+ tool.contains("user")+ && tool.contains("list")+ && !["channel", "conversation", "email", "find", "group"]+ .iter()+ .any(|word| tool.contains(word))+ }+ }+ })+ .cloned()+ .ok_or(SlackSourceError::ToolsUnavailable(capability))+}++fn tool_suffix(name: &str) -> &str {+ name.rsplit_once("__").map_or(name, |(_, tool)| tool)+}++fn is_slack_channel_search_tool_name(name: &str) -> bool {+ let tool = tool_suffix(name).to_ascii_lowercase().replace('-', "_");+ let channel_noun = tool.contains("channel") || tool.contains("conversation");+ let listing_verb = tool.contains("search") || tool.contains("list");+ let other_surface = [+ "member", "history", "message", "canvas", "create", "user", "emoji", "file",+ ]+ .iter()+ .any(|word| tool.contains(word));+ channel_noun && listing_verb && !other_surface+}++fn listing_arguments(+ schema: &Value,+ cursor: Option<&str>,+ conversation: Option<&SlackConversationId>,+ channels: bool,+) -> Value {+ let properties = &schema["properties"];+ let mut arguments = json!({});+ if let Some(cursor) = cursor.filter(|_| properties.get("cursor").is_some()) {+ arguments["cursor"] = json!(cursor);+ }+ if properties.get("limit").is_some() {+ arguments["limit"] = json!(200);+ }+ if channels {+ if properties.get("query").is_some() {+ arguments["query"] = json!("");+ }+ if let Some(types) = properties.get("types") {+ // The provisional fixture declares a string; other tools accept arrays.+ arguments["types"] = if types["type"] == "string" {+ json!("public_channel")+ } else {+ json!(["public_channel"])+ };+ }+ if properties.get("exclude_archived").is_some() {+ arguments["exclude_archived"] = json!(false);+ }+ }+ if let Some(conversation) = conversation+ && let Some(key) = ["channel", "conversation", "channel_id"]+ .into_iter()+ .find(|key| properties.get(key).is_some())+ {+ arguments[key] = json!(conversation.as_str());+ }+ arguments+}++async fn call_channels(+ tools: &impl ToolSet<()>,+ user: &MacroUserIdStr<'static>,+ name: &str,+ arguments: &Value,+ first_page: bool,+) -> Result<Value, SlackSourceError> {+ match call_tool(tools, user, name, arguments).await {+ // Some search tools reject the explicit empty query. Do not retry scope+ // failures or rate limits, or discard a continuation cursor on retry.+ Err(SlackSourceError::Other(_)) if first_page && arguments != &json!({}) => {+ call_tool(tools, user, name, &json!({})).await+ }+ result => result,+ }+}++async fn call_tool(+ tools: &impl ToolSet<()>,+ user: &MacroUserIdStr<'static>,+ name: &str,+ arguments: &Value,+) -> Result<Value, SlackSourceError> {+ ToolSet::<()>::try_tool_call(+ tools,+ (),+ RequestContext::new(user.clone()),+ name,+ arguments,+ )+ .await+ .map_err(|error| source_error(error.to_string()))?+ .map_err(|error| source_error(error.description))+}++fn source_error(text: String) -> SlackSourceError {+ let lower = text.to_ascii_lowercase();+ static RATE_LIMIT_STATUS: LazyLock<regex::Regex> =+ LazyLock::new(|| regex::Regex::new(r"\b429\b").unwrap());+ if lower.contains("ratelimited")+ || lower.contains("rate_limited")+ || RATE_LIMIT_STATUS.is_match(&lower)+ {+ static RETRY_AFTER: LazyLock<regex::Regex> =+ LazyLock::new(|| regex::Regex::new(r"retry[\s_-]+after\s*:?\s*(\d+)").unwrap());+ let retry_after = RETRY_AFTER+ .captures(&lower)+ .and_then(|capture| capture[1].parse::<u64>().ok())+ .map(Duration::from_secs);+ SlackSourceError::RateLimited { retry_after }+ } else if lower.contains("missing_scope") {+ SlackSourceError::MissingScope(text)+ } else {+ SlackSourceError::Other(anyhow::anyhow!(text))+ }+}diff --git a/crates/import/src/outbound/mcp_slack_source/fixtures/list_channels.json b/crates/import/src/outbound/mcp_slack_source/fixtures/list_channels.jsonnew file mode 100644index 0000000000..749594c94e--- /dev/null+++ b/crates/import/src/outbound/mcp_slack_source/fixtures/list_channels.json@@ -0,0 +1,9 @@+{+ "content": [+ {+ "type": "text",+ "text": "{\"ok\":true,\"channels\":[{\"id\":\"C0000000001\",\"name\":\"example-public\",\"is_channel\":true,\"is_private\":false,\"is_im\":false,\"is_mpim\":false,\"is_archived\":false,\"is_member\":true,\"num_members\":3,\"context_team_id\":\"T0000000001\",\"purpose\":{\"value\":\"Synthetic public channel\"}},{\"id\":\"C0000000002\",\"name\":\"example-private\",\"is_channel\":true,\"is_private\":true,\"is_im\":false,\"is_mpim\":false,\"is_archived\":false,\"is_member\":true,\"num_members\":1,\"context_team_id\":\"T0000000001\",\"purpose\":{\"value\":\"\"}}],\"response_metadata\":{\"next_cursor\":\"\"}}"+ }+ ],+ "isError": false+}diff --git a/crates/import/src/outbound/mcp_slack_source/fixtures/list_members.json b/crates/import/src/outbound/mcp_slack_source/fixtures/list_members.jsonnew file mode 100644index 0000000000..f675c72f93--- /dev/null+++ b/crates/import/src/outbound/mcp_slack_source/fixtures/list_members.json@@ -0,0 +1,9 @@+{+ "content": [+ {+ "type": "text",+ "text": "{\"ok\":true,\"members\":[\"U0000000001\",\"U0000000002\"],\"response_metadata\":{\"next_cursor\":\"synthetic-next-page\"}}"+ }+ ],+ "isError": false+}diff --git a/crates/import/src/outbound/mcp_slack_source/fixtures/list_users.json b/crates/import/src/outbound/mcp_slack_source/fixtures/list_users.jsonnew file mode 100644index 0000000000..30f271a7a5--- /dev/null+++ b/crates/import/src/outbound/mcp_slack_source/fixtures/list_users.json@@ -0,0 +1,9 @@+{+ "content": [+ {+ "type": "text",+ "text": "{\"ok\":true,\"members\":[{\"id\":\"U0000000001\",\"team_id\":\"T0000000001\",\"name\":\"example-human\",\"real_name\":\"Example Human\",\"deleted\":false,\"is_bot\":false,\"profile\":{\"display_name\":\"Example\",\"real_name\":\"Example Human\",\"email\":\"[email protected]\"}},{\"id\":\"U0000000002\",\"team_id\":\"T0000000001\",\"name\":\"example-bot\",\"real_name\":\"Example Bot\",\"deleted\":false,\"is_bot\":true,\"profile\":{\"display_name\":\"Example Bot\"}},{\"id\":\"U0000000003\",\"team_id\":\"T0000000001\",\"name\":\"example-deleted\",\"real_name\":\"Example Deleted\",\"deleted\":true,\"is_bot\":false,\"profile\":{\"display_name\":\"\",\"email\":\"[email protected]\"}}],\"response_metadata\":{\"next_cursor\":\"\"}}"+ }+ ],+ "isError": false+}diff --git a/crates/import/src/outbound/mcp_slack_source/fixtures/tools_list.json b/crates/import/src/outbound/mcp_slack_source/fixtures/tools_list.jsonnew file mode 100644index 0000000000..ce14bc5dd8--- /dev/null+++ b/crates/import/src/outbound/mcp_slack_source/fixtures/tools_list.json@@ -0,0 +1,41 @@+{+ "tools": [+ {+ "name": "slack_v2-list-channels",+ "description": "Synthetic fallback schema; not verified against Pipedream. List Slack conversations.",+ "inputSchema": {+ "type": "object",+ "properties": {+ "cursor": { "type": "string" },+ "limit": { "type": "integer" },+ "types": { "type": "string" },+ "exclude_archived": { "type": "boolean" }+ }+ }+ },+ {+ "name": "slack_v2-list-members-in-channel",+ "description": "Synthetic fallback schema; not verified against Pipedream. List conversation member IDs.",+ "inputSchema": {+ "type": "object",+ "properties": {+ "channel": { "type": "string" },+ "cursor": { "type": "string" },+ "limit": { "type": "integer" }+ },+ "required": ["channel"]+ }+ },+ {+ "name": "slack_v2-list-users",+ "description": "Synthetic fallback schema; not verified against Pipedream. List workspace users.",+ "inputSchema": {+ "type": "object",+ "properties": {+ "cursor": { "type": "string" },+ "limit": { "type": "integer" }+ }+ }+ }+ ]+}diff --git a/crates/import/src/outbound/mcp_slack_source/parse.rs b/crates/import/src/outbound/mcp_slack_source/parse.rsnew file mode 100644index 0000000000..efda104a56--- /dev/null+++ b/crates/import/src/outbound/mcp_slack_source/parse.rs@@ -0,0 +1,206 @@+//! Translate Slack response facts, without choosing import candidates.++use serde_json::{Map, Value};++use super::source_error;+use crate::domain::models::{+ SlackConversation, SlackConversationId, SlackConversationKind, SlackConversationPage,+ SlackMemberPage, SlackUser, SlackUserId, SlackUserPage,+};+use crate::domain::ports::SlackSourceError;++pub(super) fn parse_slack_channel_page(+ result: Value,+) -> Result<SlackConversationPage, SlackSourceError> {+ let (conversations, next_cursor) = parse_page(+ result,+ &["channels", "results", "items", "matches", "data", "ret"],+ parse_slack_channel,+ 0,+ )?;+ Ok(SlackConversationPage {+ conversations,+ next_cursor,+ })+}++pub(super) fn parse_member_page(result: Value) -> Result<SlackMemberPage, SlackSourceError> {+ let (members, next_cursor) = parse_page(+ result,+ &["members", "users", "data", "ret"],+ |value| {+ value+ .as_str()+ .or_else(|| value.get("id").and_then(Value::as_str))+ .or_else(|| value.get("user").and_then(Value::as_str))+ .and_then(SlackUserId::new)+ },+ 0,+ )?;+ Ok(SlackMemberPage {+ members,+ next_cursor,+ })+}++pub(super) fn parse_user_page(result: Value) -> Result<SlackUserPage, SlackSourceError> {+ let (users, next_cursor) = parse_page(+ result,+ &["members", "users", "results", "items", "data", "ret"],+ parse_user,+ 0,+ )?;+ Ok(SlackUserPage { users, next_cursor })+}++/// Preserve valid empty pages and reject unknown/malformed responses rather than+/// reporting that a failed directory lookup found no members.+fn parse_page<T>(+ result: Value,+ wrappers: &[&str],+ parse_row: fn(&Value) -> Option<T>,+ depth: u8,+) -> Result<(Vec<T>, Option<String>), SlackSourceError> {+ if depth >= 32 {+ return Err(source_error("Slack result nesting limit exceeded".into()));+ }+ match result {+ Value::String(text) => {+ let value = serde_json::from_str(&text)+ .map_err(|_| source_error("Slack tool returned invalid JSON".into()))?;+ parse_page(value, wrappers, parse_row, depth + 1)+ }+ Value::Array(items) => Ok((items.iter().filter_map(parse_row).collect(), None)),+ Value::Object(map) => {+ if map.get("ok") == Some(&Value::Bool(false)) {+ return Err(source_error(Value::Object(map).to_string()));+ }+ if map.get("truncated") == Some(&Value::Bool(true)) {+ return Err(source_error("Slack tool returned truncated data".into()));+ }+ let next_cursor = slack_next_cursor(&map);+ for key in wrappers {+ if let Some(value) = map.get(*key) {+ let (rows, inner_cursor) =+ parse_page(value.clone(), wrappers, parse_row, depth + 1)?;+ return Ok((rows, inner_cursor.or(next_cursor)));+ }+ }+ if let Some(row) = parse_row(&Value::Object(map)) {+ return Ok((vec![row], next_cursor));+ }+ Err(source_error("Unrecognized Slack response object".into()))+ }+ _ => Err(source_error("Unrecognized Slack response value".into())),+ }+}++fn slack_next_cursor(map: &Map<String, Value>) -> Option<String> {+ [+ map.get("next_cursor"),+ map.get("nextCursor"),+ map.get("cursor"),+ map.get("response_metadata")+ .and_then(|metadata| metadata.get("next_cursor")),+ ]+ .into_iter()+ .flatten()+ .filter_map(Value::as_str)+ .map(str::trim)+ .find(|cursor| !cursor.is_empty())+ .map(str::to_string)+}++fn parse_slack_channel(value: &Value) -> Option<SlackConversation> {+ let map = value.as_object()?;+ let id = map+ .get("id")+ .or_else(|| map.get("channel_id"))+ .and_then(Value::as_str)+ .and_then(SlackConversationId::new)?;+ let kind = if map.get("is_im") == Some(&Value::Bool(true)) {+ SlackConversationKind::DirectMessage+ } else if map.get("is_mpim") == Some(&Value::Bool(true)) {+ SlackConversationKind::GroupDirectMessage+ } else if ["is_private", "is_group"]+ .iter()+ .any(|key| map.get(*key) == Some(&Value::Bool(true)))+ {+ SlackConversationKind::PrivateChannel+ } else if map.get("is_private") == Some(&Value::Bool(false)) {+ SlackConversationKind::PublicChannel+ } else {+ // Unknown visibility must never be promoted to a public channel.+ return None;+ };+ let name = map+ .get("name")+ .or_else(|| map.get("channel_name"))+ .and_then(Value::as_str)+ .map(|name| name.trim().trim_start_matches('#'))+ .filter(|name| !name.is_empty())+ // IMs may not have names. Keep their identity/kind for the domain.+ .unwrap_or(id.as_str())+ .to_owned();+ let purpose = slack_channel_text(map, "purpose")+ .or_else(|| slack_channel_text(map, "topic"))+ .or_else(|| slack_channel_text(map, "description"));+ let member_count = map+ .get("num_members")+ .or_else(|| map.get("member_count"))+ .and_then(Value::as_u64);+ let archived = map.get("is_archived") == Some(&Value::Bool(true));+ Some(SlackConversation {+ id,+ name,+ kind,+ archived,+ member_count,+ purpose,+ })+}++fn slack_channel_text(map: &Map<String, Value>, key: &str) -> Option<String> {+ let value = map.get(key)?;+ value+ .as_str()+ .or_else(|| value.get("value").and_then(Value::as_str))+ .and_then(nonempty_text)+ .map(str::to_string)+}++fn nonempty_text(text: &str) -> Option<&str> {+ let text = text.trim();+ (!text.is_empty()).then_some(text)+}++fn parse_user(value: &Value) -> Option<SlackUser> {+ let id = value+ .get("id")+ .and_then(Value::as_str)+ .and_then(SlackUserId::new)?;+ let profile = &value["profile"];+ let display_name = [+ profile.get("display_name"),+ value.get("real_name"),+ value.get("name"),+ ]+ .into_iter()+ .flatten()+ .filter_map(Value::as_str)+ .find_map(nonempty_text)+ .unwrap_or(id.as_str())+ .to_owned();+ let email = profile+ .get("email")+ .and_then(Value::as_str)+ .and_then(nonempty_text)+ .map(str::to_owned);+ Some(SlackUser {+ id,+ display_name,+ email,+ is_bot: value.get("is_bot") == Some(&Value::Bool(true)),+ deleted: value.get("deleted") == Some(&Value::Bool(true)),+ })+}diff --git a/crates/import/src/outbound/mcp_slack_source/test.rs b/crates/import/src/outbound/mcp_slack_source/test.rsnew file mode 100644index 0000000000..36066b7ab0--- /dev/null+++ b/crates/import/src/outbound/mcp_slack_source/test.rs@@ -0,0 +1,603 @@+use super::*;+use crate::domain::models::SlackConversationKind;+use ai_toolset::{RequestSchema, ToolCallError, ToolResult, ToolSetError};+use mcp_select::ConnectorRef;+use std::collections::VecDeque;+use std::pin::Pin;+use std::sync::Mutex;++fn user() -> MacroUserIdStr<'static> {+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()+}++#[derive(Default)]+struct Tools {+ schemas: HashMap<String, Value>,+ responses: Mutex<VecDeque<ToolResult<Value>>>,+ calls: Mutex<Vec<(String, Value)>>,+}++impl ToolSet<()> for Tools {+ fn dispatch_tool_call<'a>(+ &'a self,+ _: (),+ caller: RequestContext,+ name: &'a str,+ arguments: &'a Value,+ ) -> Pin<Box<dyn Future<Output = Result<ToolResult<Value>, ToolSetError>> + Send + 'a>> {+ assert_eq!(caller.user_id, user());+ self.calls+ .lock()+ .unwrap()+ .push((name.into(), arguments.clone()));+ let result = self+ .responses+ .lock()+ .unwrap()+ .pop_front()+ .expect("unexpected call");+ Box::pin(async { Ok(result) })+ }++ fn request_schemas(&self) -> Option<Vec<RequestSchema>> {+ Some(+ self.schemas+ .iter()+ .map(|(name, schema)| RequestSchema {+ name: name.clone(),+ schema: serde_json::from_value(schema.clone()).unwrap(),+ })+ .collect(),+ )+ }+}++fn fixture_tools() -> Tools {+ let fixture: Value = serde_json::from_str(include_str!("fixtures/tools_list.json")).unwrap();+ Tools {+ schemas: fixture["tools"]+ .as_array()+ .unwrap()+ .iter()+ .map(|tool| {+ (+ format!("mcp__Slack__{}", tool["name"].as_str().unwrap()),+ tool["inputSchema"].clone(),+ )+ })+ .collect(),+ ..Tools::default()+ }+}++// Match the MCP toolset's conversion: structuredContent if supplied, otherwise+// text content. T01 fixtures are synthetic, not verified live recordings.+fn fixture_result(fixture: &str) -> Value {+ let fixture: Value = serde_json::from_str(fixture).unwrap();+ assert_eq!(fixture["isError"], false);+ fixture+ .get("structuredContent")+ .cloned()+ .unwrap_or_else(|| {+ json!(+ fixture["content"]+ .as_array()+ .unwrap()+ .iter()+ .map(|content| content["text"].as_str().unwrap())+ .collect::<Vec<_>>()+ .join("\n")+ )+ })+}++#[tokio::test]+async fn fixture_tools_return_channels_members_and_users() {+ let tools = fixture_tools();+ tools.responses.lock().unwrap().extend([+ Ok(fixture_result(include_str!("fixtures/list_channels.json"))),+ Ok(fixture_result(include_str!("fixtures/list_members.json"))),+ Ok(fixture_result(include_str!("fixtures/list_users.json"))),+ ]);+ let schemas = tool_schemas(&tools);+ let channels_tool = resolve_tool(&schemas, Listing::Channels).unwrap();+ let arguments = listing_arguments(&schemas[&channels_tool], None, None, true);+ assert_eq!(+ arguments,+ json!({+ "limit": 200, "types": "public_channel", "exclude_archived": false+ })+ );+ let channels = parse::parse_slack_channel_page(+ call_channels(&tools, &user(), &channels_tool, &arguments, true)+ .await+ .unwrap(),+ )+ .unwrap();+ assert_eq!(channels.conversations.len(), 2);+ assert_eq!(+ channels.conversations[0].kind,+ SlackConversationKind::PublicChannel+ );+ assert_eq!(channels.conversations[0].name, "example-public");+ assert_eq!(+ channels.conversations[0].purpose.as_deref(),+ Some("Synthetic public channel")+ );+ assert_eq!(channels.conversations[0].member_count, Some(3));+ assert_eq!(+ channels.conversations[1].kind,+ SlackConversationKind::PrivateChannel+ );+ assert_eq!(channels.next_cursor, None);++ let members_tool = resolve_tool(&schemas, Listing::Members).unwrap();+ let arguments = listing_arguments(+ &schemas[&members_tool],+ Some("page-2"),+ Some(&channels.conversations[0].id),+ false,+ );+ assert_eq!(+ arguments,+ json!({"channel": "C0000000001", "limit": 200, "cursor": "page-2"})+ );+ let members = parse::parse_member_page(+ call_tool(&tools, &user(), &members_tool, &arguments)+ .await+ .unwrap(),+ )+ .unwrap();+ assert_eq!(+ members+ .members+ .iter()+ .map(|id| id.as_str())+ .collect::<Vec<_>>(),+ ["U0000000001", "U0000000002"]+ );+ assert_eq!(members.next_cursor.as_deref(), Some("synthetic-next-page"));++ let users_tool = resolve_tool(&schemas, Listing::Users).unwrap();+ let arguments = listing_arguments(&schemas[&users_tool], None, None, false);+ let users = parse::parse_user_page(+ call_tool(&tools, &user(), &users_tool, &arguments)+ .await+ .unwrap(),+ )+ .unwrap();+ assert_eq!(users.users.len(), 3);+ assert_eq!(users.users[0].display_name, "Example");+ assert_eq!(users.users[0].email.as_deref(), Some("[email protected]"));+ assert!(users.users[1].is_bot);+ assert_eq!(users.users[1].email, None);+ assert!(users.users[2].deleted);+ assert_eq!(users.users[2].display_name, "Example Deleted");+ assert_eq!(users.next_cursor, None);+ assert_eq!(tools.calls.lock().unwrap().len(), 3);+}++#[test]+fn exact_tool_names_take_precedence_over_shapes_and_legacy_names() {+ let mut schemas = tool_schemas(&fixture_tools());+ for (listing, action, shape) in [+ (+ Listing::Channels,+ "list-channels",+ "search-all-conversations",+ ),+ (+ Listing::Members,+ "list-members-in-channel",+ "get-conversation-members",+ ),+ (Listing::Users, "list-users", "list-workspace-users"),+ ] {+ let legacy = format!("mcp__Slack__slack-{action}");+ let exact = format!("mcp__Slack__slack_v2-{action}");+ schemas.insert(legacy.clone(), json!({}));+ schemas.insert(shape.into(), json!({}));+ assert_eq!(resolve_tool(&schemas, listing).unwrap(), exact);+ schemas.remove(&exact);+ assert_eq!(resolve_tool(&schemas, listing).unwrap(), legacy);+ schemas.remove(&legacy);+ assert_eq!(resolve_tool(&schemas, listing).unwrap(), shape);+ }+}++#[test]+fn resolution_rejects_unrelated_tools_and_reports_missing_capabilities() {+ let schemas = [+ "list-channel-messages",+ "list-channel-history",+ "create-channel",+ "find-user-by-email",+ "list-user-groups",+ "list-channel-users",+ ]+ .into_iter()+ .map(|name| (format!("mcp__Slack__{name}"), json!({})))+ .collect();+ for (listing, expected) in [+ (Listing::Channels, "channel listing"),+ (Listing::Members, "members"),+ (Listing::Users, "users"),+ ] {+ assert!(+ matches!(resolve_tool(&schemas, listing), Err(SlackSourceError::ToolsUnavailable(capability)) if capability == expected)+ );+ }+ assert!(is_slack_channel_search_tool_name(+ "mcp__Slack__SEARCH-ALL-CONVERSATIONS"+ ));+ assert!(!is_slack_channel_search_tool_name(+ "mcp__list_channels__unrelated"+ ));+}++#[test]+fn arguments_use_only_declared_properties_and_channel_alias() {+ let conversation = SlackConversationId::new("C123").unwrap();+ assert_eq!(+ listing_arguments(&json!({}), Some("next"), Some(&conversation), true),+ json!({})+ );+ for key in ["channel", "conversation", "channel_id"] {+ let schema = json!({"properties": {key: {"type": "string"}, "limit": {}}});+ assert_eq!(+ listing_arguments(&schema, Some("next"), Some(&conversation), false),+ json!({key: "C123", "limit": 200})+ );+ }+ let schema = json!({"properties": {"cursor": {}, "types": {"type": "array"}, "query": {}, "exclude_archived": {}}});+ assert_eq!(+ listing_arguments(&schema, Some("next"), None, true),+ json!({+ "cursor": "next", "types": ["public_channel"], "query": "", "exclude_archived": false+ })+ );+ assert!(+ listing_arguments(&schema, None, None, true)+ .get("cursor")+ .is_none()+ );+}++#[test]+fn channels_preserve_kinds_archive_and_counts_and_drop_invalid_ids() {+ let page = parse::parse_slack_channel_page(json!({"ret": {"data": [+ {"id": "C123", "name": " #general ", "is_private": false, "is_archived": true, "member_count": 42, "topic": {"value": " topic "}},+ {"id": "C124", "name": "private", "is_private": true},+ {"channel_id": "G123", "channel_name": "group", "is_group": true, "description": "description"},+ {"id": "D123", "is_im": true},+ {"id": "G124", "is_mpim": true},+ {"id": "invalid", "name": "bad"}, {"name": "missing"}, {"id": " C125", "name": "bad"}+ ]}, "response_metadata": {"next_cursor": " next "}})).unwrap();+ assert_eq!(page.conversations.len(), 5);+ assert_eq!(page.next_cursor.as_deref(), Some("next"));+ let first = &page.conversations[0];+ assert_eq!(first.name, "general");+ assert!(first.archived);+ assert_eq!(first.member_count, Some(42));+ assert_eq!(first.purpose.as_deref(), Some("topic"));+ assert_eq!(+ page.conversations+ .iter()+ .map(|channel| &channel.kind)+ .collect::<Vec<_>>(),+ vec![+ &SlackConversationKind::PublicChannel,+ &SlackConversationKind::PrivateChannel,+ &SlackConversationKind::PrivateChannel,+ &SlackConversationKind::DirectMessage,+ &SlackConversationKind::GroupDirectMessage+ ]+ );+ assert_eq!(page.conversations[3].name, "D123");+}++#[test]+fn channels_require_explicit_visibility_without_relying_on_is_channel() {+ for is_channel in [None, Some(json!(true)), Some(json!(false))] {+ for visibility in [+ None,+ Some(Value::Null),+ Some(json!("false")),+ Some(json!("true")),+ Some(json!(0)),+ Some(json!(1)),+ Some(json!([])),+ Some(json!({})),+ Some(json!(false)),+ Some(json!(true)),+ ] {+ let mut channel = json!({"id": "C123", "name": "channel"});+ if let Some(is_channel) = &is_channel {+ channel["is_channel"] = is_channel.clone();+ }+ if let Some(visibility) = &visibility {+ channel["is_private"] = visibility.clone();+ }+ let page = parse::parse_slack_channel_page(json!([channel])).unwrap();+ let expected = match visibility.and_then(|value| value.as_bool()) {+ Some(false) => vec![SlackConversationKind::PublicChannel],+ Some(true) => vec![SlackConversationKind::PrivateChannel],+ None => vec![],+ };+ assert_eq!(+ page.conversations+ .into_iter()+ .map(|channel| channel.kind)+ .collect::<Vec<_>>(),+ expected,+ "channel: {channel}"+ );+ }+ }+}++#[test]+fn explicit_private_and_dm_kinds_take_precedence_over_public_visibility() {+ for (flag, expected) in [+ ("is_group", SlackConversationKind::PrivateChannel),+ ("is_im", SlackConversationKind::DirectMessage),+ ("is_mpim", SlackConversationKind::GroupDirectMessage),+ ] {+ let page = parse::parse_slack_channel_page(json!([+ {"id": "C123", "is_private": false, "is_channel": true, flag: true}+ ]))+ .unwrap();+ assert_eq!(page.conversations.len(), 1);+ assert_eq!(page.conversations[0].kind, expected);+ }+}++#[test]+fn channel_wrappers_and_cursor_aliases_include_empty_pages() {+ for wrapper in ["channels", "results", "items", "matches", "data", "ret"] {+ for cursor_key in ["next_cursor", "nextCursor", "cursor"] {+ let page = parse::parse_slack_channel_page(json!({wrapper: [], cursor_key: " more "}))+ .unwrap();+ assert!(page.conversations.is_empty());+ assert_eq!(page.next_cursor.as_deref(), Some("more"));+ }+ }+ let page = parse::parse_slack_channel_page(+ json!({"channels": {"items": [], "next_cursor": "inner"}, "next_cursor": "outer"}),+ )+ .unwrap();+ assert_eq!(page.next_cursor.as_deref(), Some("inner"));+ let page = parse::parse_slack_channel_page(+ json!({"id": "C123", "name": "single", "is_private": false, "next_cursor": " "}),+ )+ .unwrap();+ assert_eq!(page.conversations.len(), 1);+ assert_eq!(page.next_cursor, None);+}++#[test]+fn members_accept_ids_objects_text_arrays_and_wrappers() {+ let rows = json!(["U123", {"id": "W123"}, {"user": "U124"}, "bad", {"id": "C123"}, null]);+ for wrapper in ["members", "users", "data", "ret"] {+ let page =+ parse::parse_member_page(json!({wrapper: rows.to_string(), "next_cursor": "more"}))+ .unwrap();+ assert_eq!(+ page.members+ .iter()+ .map(|id| id.as_str())+ .collect::<Vec<_>>(),+ ["U123", "W123", "U124"]+ );+ assert_eq!(page.next_cursor.as_deref(), Some("more"));+ }+ assert_eq!(parse::parse_member_page(rows).unwrap().members.len(), 3);+}++#[test]+fn users_apply_display_name_fallbacks_without_filtering_bots_or_deleted() {+ let rows = json!([+ {"id": "U123", "name": "handle", "real_name": "Real", "profile": {"display_name": " Display ", "email": " [email protected] "}},+ {"id": "U124", "name": "handle", "real_name": "Real", "profile": {"display_name": " "}, "deleted": true},+ {"id": "U125", "name": "handle", "profile": null, "is_bot": true},+ {"id": "W123"}, {"id": "bad"}, {"name": "missing"}+ ]);+ for wrapper in ["members", "users", "data", "ret"] {+ let page = parse::parse_user_page(json!({wrapper: rows.to_string()})).unwrap();+ assert_eq!(+ page.users+ .iter()+ .map(|user| user.display_name.as_str())+ .collect::<Vec<_>>(),+ ["Display", "Real", "handle", "W123"]+ );+ assert_eq!(page.users[0].email.as_deref(), Some("[email protected]"));+ assert!(page.users[1].deleted);+ assert!(page.users[2].is_bot);+ assert_eq!(page.users[3].email, None);+ }+ assert_eq!(parse::parse_user_page(rows).unwrap().users.len(), 4);+}++#[test]+fn malformed_unknown_truncated_and_action_errors_are_not_empty_pages() {+ for value in [+ json!("not JSON"),+ json!("{\"members\":["),+ json!({"unexpected": []}),+ json!(null),+ json!({"members": [], "truncated": true}),+ ] {+ assert!(parse::parse_member_page(value.clone()).is_err());+ assert!(parse::parse_user_page(value.clone()).is_err());+ assert!(parse::parse_slack_channel_page(value).is_err());+ }+ assert!(matches!(+ parse::parse_user_page(json!({"ret": {"ok": false, "error": "missing_scope"}})),+ Err(SlackSourceError::MissingScope(_))+ ));+ assert!(matches!(+ parse::parse_member_page(json!({"ok": false, "error": "ratelimited"})),+ Err(SlackSourceError::RateLimited { retry_after: None })+ ));+}++fn tool_error(description: &str) -> ToolResult<Value> {+ Err(ToolCallError {+ internal_error: anyhow::anyhow!("synthetic tool failure"),+ description: description.into(),+ })+}++#[tokio::test]+async fn retries_first_channel_page_without_arguments_only_once() {+ let tools = Tools::default();+ tools+ .responses+ .lock()+ .unwrap()+ .extend([tool_error("empty query rejected"), Ok(json!([]))]);+ assert_eq!(+ call_channels(&tools, &user(), "channels", &json!({"query": ""}), true)+ .await+ .unwrap(),+ json!([])+ );+ assert_eq!(+ *tools.calls.lock().unwrap(),+ vec![+ ("channels".into(), json!({"query": ""})),+ ("channels".into(), json!({}))+ ]+ );+ for (arguments, first_page) in [(json!({}), true), (json!({"cursor": "next"}), false)] {+ let tools = Tools::default();+ tools+ .responses+ .lock()+ .unwrap()+ .push_back(tool_error("failure"));+ assert!(+ call_channels(&tools, &user(), "channels", &arguments, first_page)+ .await+ .is_err()+ );+ assert_eq!(tools.calls.lock().unwrap().len(), 1);+ }+}++#[tokio::test]+async fn tool_errors_map_rate_limits_and_scopes_without_retrying() {+ for message in [+ "ratelimited: retry after 12 seconds",+ "RATE_LIMITED: Retry-After: 12",+ "HTTP 429 retry after 12",+ ] {+ let tools = Tools::default();+ tools+ .responses+ .lock()+ .unwrap()+ .push_back(tool_error(message));+ assert!(+ matches!(call_channels(&tools, &user(), "channels", &json!({"limit": 200}), true).await,+ Err(SlackSourceError::RateLimited { retry_after: Some(delay) }) if delay == Duration::from_secs(12))+ );+ assert_eq!(tools.calls.lock().unwrap().len(), 1);+ }+ for message in [+ "429",+ "ratelimited retry after unknown",+ "rate_limited retry after 999999999999999999999999999",+ ] {+ assert!(matches!(+ source_error(message.into()),+ SlackSourceError::RateLimited { retry_after: None }+ ));+ }+ let tools = Tools::default();+ tools+ .responses+ .lock()+ .unwrap()+ .push_back(tool_error("missing_scope users:read.email"));+ assert!(+ matches!(call_channels(&tools, &user(), "channels", &json!({"limit": 200}), true).await, Err(SlackSourceError::MissingScope(text)) if text.contains("users:read.email"))+ );+ assert_eq!(tools.calls.lock().unwrap().len(), 1);+ assert!(matches!(+ source_error("other failure".into()),+ SlackSourceError::Other(_)+ ));+}++#[test]+fn rate_limit_status_requires_a_standalone_token() {+ for message in [+ "429",+ "HTTP 429 Too Many Requests",+ "status=429; retry later",+ "HTTP error (429)",+ r#"{"status":429}"#,+ "429: rate limit",+ ] {+ assert!(+ matches!(+ source_error(message.into()),+ SlackSourceError::RateLimited { retry_after: None }+ ),+ "message: {message}"+ );+ }+ for token in [+ "C429ABC", "C429", "429ABC", "1429", "4290", "14290", "id_429",+ ] {+ let message = format!("request failed for {token}");+ assert!(+ matches!(source_error(message), SlackSourceError::Other(_)),+ "token: {token}"+ );+ let message = format!("missing_scope for {token}");+ assert!(+ matches!(source_error(message), SlackSourceError::MissingScope(_)),+ "token: {token}"+ );+ }+}++struct NoConnector;+impl ConnectorSelect for NoConnector {+ async fn user_toolset(&self, _: &MacroUserIdStr<'static>) -> UserMcpTools {+ panic!("must request only the Slack connector")+ }++ async fn connector_toolset(+ &self,+ caller: &MacroUserIdStr<'static>,+ connector: ConnectorRef<'_>,+ ) -> anyhow::Result<Option<UserMcpTools>> {+ assert_eq!(caller, &user());+ let slack = ImportSource::Slack.connector_ref();+ assert_eq!(connector.pipedream_app_slugs, slack.pipedream_app_slugs);+ assert_eq!(connector.native_server_url, slack.native_server_url);+ Ok(None)+ }++ async fn connector_connected(+ &self,+ _: &MacroUserIdStr<'static>,+ _: ConnectorRef<'_>,+ ) -> anyhow::Result<bool> {+ panic!("opening a session must load its toolset directly")+ }+}++#[tokio::test]+async fn no_connector_is_not_connected() {+ assert!(matches!(+ McpSlackSource::new(Arc::new(NoConnector))+ .open(&user())+ .await,+ Err(SlackSourceError::NotConnected)+ ));+}diff --git a/crates/import/src/outbound/mod.rs b/crates/import/src/outbound/mod.rsindex 56ed58c990..b5957428e9 100644--- a/crates/import/src/outbound/mod.rs+++ b/crates/import/src/outbound/mod.rs@@ -2,4 +2,5 @@pub mod document_properties;pub mod gateway_notifier;+pub mod mcp_slack_source;pub mod pg_import_repo;diff --git a/crates/import/src/outbound/pg_import_repo.rs b/crates/import/src/outbound/pg_import_repo.rsindex 6fa8eb9425..8a3497470d 100644--- a/crates/import/src/outbound/pg_import_repo.rs+++ b/crates/import/src/outbound/pg_import_repo.rs@@ -9,6 +9,7 @@ use crate::domain::models::{};use crate::domain::ports::{ImportError, ImportRepo, Result};use chrono::{DateTime, Utc};+use macro_user_id::cowlike::CowLike;use macro_user_id::user_id::MacroUserIdStr;use sqlx::PgPool;use std::collections::HashSet;@@ -511,6 +512,23 @@ impl ImportRepo for PgImportRepo {Ok(team_id)}+ #[tracing::instrument(skip(self), err)]+ async fn team_members(&self, team_id: Uuid) -> Result<Vec<MacroUserIdStr<'static>>> {+ let rows = sqlx::query!("SELECT user_id FROM team_user WHERE team_id = $1", team_id,)+ .fetch_all(&self.pool)+ .await?;+ let mut members = Vec::with_capacity(rows.len());+ for row in rows {+ match MacroUserIdStr::parse_from_str(&row.user_id).map(|id| id.into_owned()) {+ Ok(id) => members.push(id),+ Err(error) => {+ tracing::warn!(?error, %team_id, "skipping unparseable team member id");+ }+ }+ }+ Ok(members)+ }+#[tracing::instrument(skip(self), err)]async fn list_runs(&self, user: &MacroUserIdStr<'static>) -> Result<Vec<ImportRun>> {let rows = sqlx::query_as!(@@ -554,6 +572,31 @@ impl ImportRepo for PgImportRepo {Ok(result.rows_affected() > 0)}+ #[tracing::instrument(skip(self), err)]+ async fn start_manual_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ ) -> Result<bool> {+ let from = run_status_strings(from);+ let result = sqlx::query!(+ r#"+ INSERT INTO import_run (user_id, source, status, auto_import)+ VALUES ($1, $2, 'running', false)+ ON CONFLICT (user_id, source) DO UPDATE+ SET status = 'running', auto_import = false, error = NULL, updated_at = NOW()+ WHERE import_run.status::text = ANY($3::text[])+ "#,+ user.as_ref(),+ source.as_ref(),+ &from,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }+#[tracing::instrument(skip(self), err)]async fn finish_run(&self,diff --git a/crates/import/src/outbound/pg_import_repo/test.rs b/crates/import/src/outbound/pg_import_repo/test.rsindex a0548ea944..a30ea65f35 100644--- a/crates/import/src/outbound/pg_import_repo/test.rs+++ b/crates/import/src/outbound/pg_import_repo/test.rs@@ -7,6 +7,147 @@ use macro_db_migrator::MACRO_DB_MIGRATIONS;use macro_user_id::cowlike::CowLike;use sqlx::{Pool, Postgres};+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn team_members_returns_only_the_requested_roster(pool: PgPool) {+ let owner = ledger_actor(&pool, "roster-owner").await;+ let peer = ledger_actor(&pool, "roster-peer").await;+ let outsider = ledger_actor(&pool, "roster-outsider").await;+ let team = ledger_team(&pool, &owner).await;+ join_ledger_team(&pool, &peer, team).await;+ ledger_team(&pool, &outsider).await;+ let repo = PgImportRepo::new(pool);++ let members = repo.team_members(team).await.unwrap();+ assert_eq!(members.len(), 2);+ assert!(members.contains(&owner));+ assert!(members.contains(&peer));+ assert!(repo.team_members(Uuid::now_v7()).await.unwrap().is_empty());+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn manual_run_inserts_without_auto_import(pool: PgPool) {+ let actor = ledger_actor(&pool, "manual-insert").await;+ let repo = PgImportRepo::new(pool);+ assert!(repo.list_runs(&actor).await.unwrap().is_empty());+ assert!(+ repo.start_manual_run(&actor, ImportSource::Slack, &[])+ .await+ .unwrap()+ );++ let runs = repo.list_runs(&actor).await.unwrap();+ assert_eq!(runs.len(), 1);+ assert_eq!(runs[0].source, ImportSource::Slack);+ assert_eq!(runs[0].status, RunStatus::Running);+ assert!(!runs[0].auto_import);+ assert!(runs[0].error.is_none());+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn manual_run_clears_auto_import_and_error_only_when_claimed(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let from = [+ RunStatus::Ready,+ RunStatus::Completed,+ RunStatus::Failed,+ RunStatus::Dismissed,+ ];+ for status in from {+ let actor = ledger_actor(&pool, status.as_ref()).await;+ assert!(+ repo.start_run(&actor, ImportSource::Slack, &[], true)+ .await+ .unwrap()+ );+ assert!(+ repo.finish_run(&actor, ImportSource::Slack, status, Some("previous error"))+ .await+ .unwrap()+ );+ assert!(repo.list_runs(&actor).await.unwrap()[0].auto_import);++ assert!(+ repo.start_manual_run(&actor, ImportSource::Slack, &from)+ .await+ .unwrap()+ );+ let runs = repo.list_runs(&actor).await.unwrap();+ assert_eq!(runs.len(), 1);+ assert_eq!(runs[0].status, RunStatus::Running);+ assert!(!runs[0].auto_import);+ assert!(runs[0].error.is_none());++ // A subsequent initial gather must not re-enable automatic import.+ assert!(+ repo.start_run(&actor, ImportSource::Slack, &[RunStatus::Running], true)+ .await+ .unwrap()+ );+ assert!(!repo.list_runs(&actor).await.unwrap()[0].auto_import);+ }+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn manual_run_does_not_take_over_active_runs(pool: PgPool) {+ let actor = ledger_actor(&pool, "manual-active").await;+ let repo = PgImportRepo::new(pool);+ let from = [+ RunStatus::Ready,+ RunStatus::Completed,+ RunStatus::Failed,+ RunStatus::Dismissed,+ ];+ assert!(+ repo.start_run(&actor, ImportSource::Slack, &[], true)+ .await+ .unwrap()+ );+ for status in [RunStatus::Running, RunStatus::Importing] {+ if status == RunStatus::Importing {+ assert!(+ repo.transition_run(&actor, ImportSource::Slack, &[RunStatus::Running], status)+ .await+ .unwrap()+ );+ }+ let before = repo.list_runs(&actor).await.unwrap().pop().unwrap();+ assert!(+ !repo+ .start_manual_run(&actor, ImportSource::Slack, &from)+ .await+ .unwrap()+ );+ let after = repo.list_runs(&actor).await.unwrap().pop().unwrap();+ assert_eq!(after.status, status);+ assert!(after.auto_import);+ assert_eq!(after.error, before.error);+ assert_eq!(after.updated_at, before.updated_at);+ }+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn staged_manual_entity_round_trips(pool: PgPool) {+ let actor = ledger_actor(&pool, "manual-entity").await;+ let repo = PgImportRepo::new(pool);+ let metadata = serde_json::json!({"name": "general", "channel_id": "C123"});+ let row = repo+ .upsert_staged(+ &actor,+ ImportSource::Slack,+ Initiator::Manual,+ "C123",+ &metadata,+ )+ .await+ .unwrap()+ .unwrap();+ assert_eq!(row.initiator, Initiator::Manual);+ assert_eq!(row.status, ImportStatus::Staged);+ let stored = repo.get(&actor, row.id).await.unwrap().unwrap();+ assert_eq!(stored.initiator, Initiator::Manual);+ assert_eq!(stored.metadata, metadata);+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn read_only_targets_are_exact_scoped_and_never_reserve(pool: PgPool) {use crate::domain::{models::ImportTargetLookup, ports::ImportTargetReader};diff --git a/crates/macro_db_client/migrations/20261005141244_import_entity_initiator_manual.sql b/crates/macro_db_client/migrations/20261005141244_import_entity_initiator_manual.sqlnew file mode 100644index 0000000000..e762801802--- /dev/null+++ b/crates/macro_db_client/migrations/20261005141244_import_entity_initiator_manual.sql@@ -0,0 +1,3 @@+ALTER TABLE import_entity DROP CONSTRAINT import_entity_initiator_check;+ALTER TABLE import_entity ADD CONSTRAINT import_entity_initiator_check+ CHECK (initiator IN ('onboarding', 'chat', 'archive', 'manual'));diff --git a/crates/mcp_select/Cargo.toml b/crates/mcp_select/Cargo.tomlindex 3b43394cab..c823fb6bf5 100644--- a/crates/mcp_select/Cargo.toml+++ b/crates/mcp_select/Cargo.toml@@ -13,3 +13,6 @@ pipedream_mcp = { path = "../pipedream_mcp" }serde_json.workspace = truetracing.workspace = trueworkspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+tokio.workspace = truediff --git a/crates/mcp_select/src/lib.rs b/crates/mcp_select/src/lib.rsindex b7a8140acc..401eb3008f 100644--- a/crates/mcp_select/src/lib.rs+++ b/crates/mcp_select/src/lib.rs@@ -23,20 +23,30 @@ use pipedream_mcp::domain::service::PipedreamToolSet;use std::pin::Pin;use std::sync::Arc;+#[cfg(test)]+mod test;+/// Mangled MCP tool names start with this prefix on both stacks.const MANGLED_PREFIX: &str = "mcp__";-/// A connector identified in both stacks: the Pipedream app slug and the+/// A connector identified in both stacks: the Pipedream app slugs and the/// native server URL that back the same product (e.g. `linear` //// `https://mcp.linear.app/mcp`).#[derive(Clone, Copy, Debug)]pub struct ConnectorRef<'a> {- /// Pipedream app name slug, e.g. `linear`.- pub pipedream_app_slug: &'a str,+ /// Pipedream app name slugs in preference order, e.g. `slack`, `slack_v2`.+ pub pipedream_app_slugs: &'a [&'a str],/// The native stack's MCP server URL for the same product.pub native_server_url: &'a str,}+impl ConnectorRef<'_> {+ /// Whether this connector recognizes the Pipedream app slug.+ pub fn matches_pipedream_slug(&self, slug: &str) -> bool {+ self.pipedream_app_slugs.contains(&slug)+ }+}+/// The MCP tools loaded for a user — from exactly one stack, per the/// selection rule above.pub enum UserMcpTools {@@ -225,15 +235,26 @@ whereif !pipedream.is_empty() {// The user is on the Pipedream stack: native connectors are// ignored even if this particular app isn't connected there.- let matching: Vec<_> = pipedream- .into_iter()- .filter(|c| c.app_slug == connector.pipedream_app_slug)- .collect();- if matching.is_empty() {+ // Prefer one alias by slug order: two Slack sessions would+ // duplicate tool names when both `slack` and `slack_v2` are connected.+ let matching = pipedream+ .iter()+ .filter(|c| connector.matches_pipedream_slug(&c.app_slug))+ .min_by_key(|c| {+ connector+ .pipedream_app_slugs+ .iter()+ .position(|slug| *slug == c.app_slug)+ });+ let Some(matching) = matching else {return Ok(None);- }+ };return Ok(Some(UserMcpTools::Pipedream(- PipedreamToolSet::new(&matching, self.pipedream_connection.clone()).await,+ PipedreamToolSet::new(+ std::slice::from_ref(matching),+ self.pipedream_connection.clone(),+ )+ .await,)));}@@ -263,7 +284,7 @@ whereif !pipedream.is_empty() {return Ok(pipedream.iter()- .any(|c| c.app_slug == connector.pipedream_app_slug));+ .any(|c| connector.matches_pipedream_slug(&c.app_slug)));}let native = selfdiff --git a/crates/mcp_select/src/test.rs b/crates/mcp_select/src/test.rsnew file mode 100644index 0000000000..41aa19aae7--- /dev/null+++ b/crates/mcp_select/src/test.rs@@ -0,0 +1,211 @@+use super::*;+use mcp_client::domain::models::McpServerRecord;+use pipedream_mcp::domain::models::{McpServer, PipedreamConnection};+use std::sync::Mutex;++const SLACK: ConnectorRef<'static> = ConnectorRef {+ pipedream_app_slugs: &["slack", "slack_v2"],+ native_server_url: "https://mcp.slack.com/mcp",+};++fn user() -> MacroUserIdStr<'static> {+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()+}++struct FakeStore(Vec<PipedreamConnection>);++impl ConnectionStore for FakeStore {+ type Err = ();++ async fn save(&self, _: &PipedreamConnection) -> Result<(), Self::Err> {+ panic!("selection must not save connections")+ }++ async fn load(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &str,+ ) -> Result<Option<PipedreamConnection>, Self::Err> {+ panic!("selection must list connections")+ }++ async fn delete(&self, _: &MacroUserIdStr<'static>, _: &str) -> Result<(), Self::Err> {+ panic!("selection must not delete connections")+ }++ async fn list(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> Result<Vec<PipedreamConnection>, Self::Err> {+ assert_eq!(user, &self::user());+ Ok(self.0.clone())+ }+}++impl McpServerStore for FakeStore {+ type Err = ();++ async fn save(&self, _: &McpServerRecord) -> Result<(), Self::Err> {+ panic!("selection must not save native servers")+ }++ async fn load(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &str,+ ) -> Result<Option<McpServerRecord>, Self::Err> {+ panic!("selection must list native servers")+ }++ async fn delete(&self, _: &MacroUserIdStr<'static>, _: &str) -> Result<(), Self::Err> {+ panic!("selection must not delete native servers")+ }++ async fn list(&self, _: &MacroUserIdStr<'static>) -> Result<Vec<McpServerRecord>, Self::Err> {+ assert!(+ self.0.is_empty(),+ "Pipedream connections exclude the native stack"+ );+ Ok(vec![])+ }+}++#[derive(Default)]+struct FakeConnection {+ attempts: Mutex<Vec<String>>,+}++impl McpConnection for FakeConnection {+ async fn connect(&self, record: &PipedreamConnection) -> anyhow::Result<McpServer> {+ self.attempts.lock().unwrap().push(record.app_slug.clone());+ // Exercise selection without a live MCP transport. Failed sessions+ // are skipped by PipedreamToolSet, but must not try another alias.+ anyhow::bail!("synthetic connection failure")+ }+}++fn selector(slugs: &[&str]) -> McpToolSelector<FakeStore, FakeStore, FakeConnection> {+ let records = slugs+ .iter()+ .map(|slug| PipedreamConnection {+ user_id: user(),+ app_slug: (*slug).to_string(),+ server_name: "Slack".to_string(),+ account_id: format!("apn_{slug}"),+ enabled: true,+ })+ .collect();+ let store = Arc::new(FakeStore(records));+ McpToolSelector::new(store.clone(), store, Arc::new(FakeConnection::default()))+}++#[test]+fn matches_only_declared_slugs() {+ assert!(SLACK.matches_pipedream_slug("slack"));+ assert!(SLACK.matches_pipedream_slug("slack_v2"));+ for slug in ["slack_bot", "Slack", "slack_v", "linear", ""] {+ assert!(!SLACK.matches_pipedream_slug(slug), "{slug}");+ }+ assert!(+ !ConnectorRef {+ pipedream_app_slugs: &[],+ ..SLACK+ }+ .matches_pipedream_slug("slack")+ );+}++#[tokio::test]+async fn either_slack_alias_is_connected_and_selected() {+ for slug in SLACK.pipedream_app_slugs {+ let selector = selector(&["linear", slug]);+ assert!(selector.connector_connected(&user(), SLACK).await.unwrap());+ let tools = selector.connector_toolset(&user(), SLACK).await.unwrap();+ assert!(matches!(tools, Some(UserMcpTools::Pipedream(_))));+ assert_eq!(+ *selector.pipedream_connection.attempts.lock().unwrap(),+ [*slug]+ );+ }+}++#[tokio::test]+async fn selects_only_first_preferred_alias_regardless_of_store_order() {+ for slugs in [["slack", "slack_v2"], ["slack_v2", "slack"]] {+ for preference in [["slack", "slack_v2"], ["slack_v2", "slack"]] {+ let selector = selector(&slugs);+ let connector = ConnectorRef {+ pipedream_app_slugs: &preference,+ ..SLACK+ };+ assert!(+ selector+ .connector_connected(&user(), connector)+ .await+ .unwrap()+ );+ let tools = selector+ .connector_toolset(&user(), connector)+ .await+ .unwrap();+ assert!(matches!(tools, Some(UserMcpTools::Pipedream(_))));+ assert_eq!(+ *selector.pipedream_connection.attempts.lock().unwrap(),+ [preference[0]],+ );+ }+ }+}++#[tokio::test]+async fn unrelated_or_missing_connections_do_not_match() {+ for slugs in [vec!["linear", "slack_bot"], vec![]] {+ let selector = selector(&slugs);+ assert!(!selector.connector_connected(&user(), SLACK).await.unwrap());+ assert!(+ selector+ .connector_toolset(&user(), SLACK)+ .await+ .unwrap()+ .is_none()+ );+ assert!(+ selector+ .pipedream_connection+ .attempts+ .lock()+ .unwrap()+ .is_empty()+ );+ }+}++#[tokio::test]+async fn empty_alias_list_does_not_match() {+ let selector = selector(&["slack", "slack_v2"]);+ let connector = ConnectorRef {+ pipedream_app_slugs: &[],+ ..SLACK+ };+ assert!(+ !selector+ .connector_connected(&user(), connector)+ .await+ .unwrap()+ );+ assert!(+ selector+ .connector_toolset(&user(), connector)+ .await+ .unwrap()+ .is_none()+ );+ assert!(+ selector+ .pipedream_connection+ .attempts+ .lock()+ .unwrap()+ .is_empty()+ );+}diff --git a/crates/onboarding/src/domain/service/test.rs b/crates/onboarding/src/domain/service/test.rsindex 6b75cd6d4d..7b22632770 100644--- a/crates/onboarding/src/domain/service/test.rs+++ b/crates/onboarding/src/domain/service/test.rs@@ -135,6 +135,14 @@ impl ImportService for MockImport {Ok(true)}+ async fn start_discovery(+ &self,+ _user: MacroUserIdStr<'static>,+ _source: ImportSource,+ ) -> ImportResult<bool> {+ Ok(false)+ }+async fn retry_gather(&self,_user: MacroUserIdStr<'static>,diff --git a/crates/slack_integration/src/outbound/import_ledger.rs b/crates/slack_integration/src/outbound/import_ledger.rsindex 2649d0ff00..3bc0dd2f58 100644--- a/crates/slack_integration/src/outbound/import_ledger.rs+++ b/crates/slack_integration/src/outbound/import_ledger.rs@@ -173,9 +173,11 @@ fn map_error(error: import::domain::ports::ImportError) -> rootcause::Report<ImpLedgerError::SourceConfirmationRequired => ImportError::InvalidInput,LedgerError::TargetConflict | LedgerError::TargetNotReserved => ImportError::Conflict,LedgerError::Db(_) | LedgerError::Other(_) => ImportError::Retryable,- // Canonical ledger operations do not perform AI work. An admission- // error here is an unexpected adapter failure, not permission to proceed.- LedgerError::Admission(_) | LedgerError::Metadata(_) => ImportError::Internal,+ // Canonical ledger operations do not perform discovery or AI work.+ // These errors are unexpected adapter failures, not permission to proceed.+ LedgerError::Admission(_)+ | LedgerError::Metadata(_)+ | LedgerError::UnsupportedDiscovery(_) => ImportError::Internal,};rootcause::Report::new(error).context(code)}diff --git a/crates/slack_integration/src/outbound/import_ledger/test.rs b/crates/slack_integration/src/outbound/import_ledger/test.rsindex c15976f024..5c97fa2c71 100644--- a/crates/slack_integration/src/outbound/import_ledger/test.rs+++ b/crates/slack_integration/src/outbound/import_ledger/test.rs@@ -151,6 +151,17 @@ async fn reservation_passes_requester_explicit_team_and_authorized_target_for_ev}}+#[test]+fn unexpected_discovery_error_fails_closed() {+ assert_eq!(+ map_error(LedgerError::UnsupportedDiscovery(+ ledger::ImportSource::Slack+ ))+ .into_current_context(),+ ImportError::Internal+ );+}+#[test]fn unexpected_ai_admission_error_fails_closed() {let admission = serde_json::from_str(r#""unavailable""#).unwrap();diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.mdindex bb614703ba..055875eed5 100644--- a/docs/AGENT_GUIDE/ai-chat.md+++ b/docs/AGENT_GUIDE/ai-chat.md@@ -169,6 +169,30 @@ the shimmer.Home’s **Connect your tools** and agent replies’ **Connect app** chips open thisConnections page. Personal Gmail/GitHub account links remain under Settings →Integrations.++ To recreate Slack channels, open **Settings → Connections → Slack → Import+ channels** (or **Connections → Slack** from the Agents workspace). The card+ appears only for a connected, enabled Pipedream Slack account. Click **Find+ channels** (or **Refresh** on subsequent visits), then wait for discovery and+ member checks to update live. Search by name, purpose, or Slack channel id;+ archived channels stay hidden until **Show archived** is checked. Select+ individual channels or **Select all visible**, then click **Import 1 channel**+ or **Import N channels**.+ Filtering does not clear selections. Rows progress from **Importing…** to an+ **Imported** link; channels already imported by another team member show+ **by a teammate** and cannot be selected again. Imported channels appear under+ **Channels**, with the Slack name and matched Macro teammates. Member counts+ show how many Slack members are on your team; unchecked membership is labeled+ separately. Unmatched members are not invited. Messages, files, private+ channels, and direct messages are not imported, and there is no ongoing sync.+ A failed discovery shows its error and **Retry**; an empty completed discovery+ shows **No public channels found**. If member checks reach their time budget,+ discovered channels remain available with unchecked membership rather than a+ discovery failure. Conversations with unknown visibility are not listed.+ If both Slack connector aliases are connected, the Slack page manages `slack`+ first; `slack_v2` remains a separate row in Connections where it can be disabled+ or disconnected.+**New conversation** opens the composer. **Conversations** is a mixed listof chats and coding sessions, newest first, with one search across both.Chat rows use a chat icon; coding rows use `</>` (the PR status icon when adiff --git a/docs/SLACK_LIVE_CHANNEL_IMPORT.md b/docs/SLACK_LIVE_CHANNEL_IMPORT.mdnew file mode 100644index 0000000000..47dd0844de--- /dev/null+++ b/docs/SLACK_LIVE_CHANNEL_IMPORT.md@@ -0,0 +1,262 @@+# Live Slack channel import: MCP contract investigation++## Purpose and evidence status++This contract supports importing the names and matched participants of selected+public Slack channels through the existing Pipedream connection. Messages, files,+ongoing sync, private channels, DMs, and the separate ZIP-archive importer are out+of scope. The selected read mechanism is Pipedream remote MCP, not Connect Proxy.++**Dev access unavailable.** The local Doppler CLI could not retrieve+`PIPEDREAM_CLIENT_ID`, `PIPEDREAM_CLIENT_SECRET`, and `PIPEDREAM_PROJECT_ID` from+project `document-cognition`, config `dev` (exit status 1; access-token diagnostic).+No credentials were loaded or persisted. No Pipedream token was minted, no live+Slack tool was called, and no dev database was queried. A dev external user and+their connected Slack workspace could not be established. Raw CLI output was+withheld to avoid exposing credentials.++The files below are **synthetic fallback fixtures, not recordings**. They let+adapter tests start without dev access; passing those tests does not establish+Pipedream compatibility. Live verification remains a release prerequisite.++## Slug decision++Canonical slug: `slack` (unverified fallback; retain until dev evidence supports `slack_v2`).++| Evidence | `slack` | `slack_v2` |+| --- | --- | --- |+| `GET /v1/connect/apps/{slug}` existence | Not checked | Not checked |+| Catalog `auth_type` | Unknown | Unknown |+| Hosted Connect UI account slug | Unknown | Unknown |+| Dev `pipedream_mcp_connections.app_slug` rows | Not queried | Not queried |++Do not migrate existing rows or change the new-connection slug based on the+synthetic tool names. Treat both slugs as potential Slack aliases. Switch the+canonical slug to `slack_v2` only after verifying that it exists and that accounts+created by hosted Connect use it. Account app slugs and action-name prefixes are+separate facts; a `slack_v2-*` tool name alone is not that evidence.++## Tool contract: provisional, not observed++`tools_list.json` contains a synthetic MCP `ListToolsResult` with the three+candidate action names from the implementation plan. Its `inputSchema` objects+model Slack Web API arguments; they are **not exact Pipedream schemas**.++| Candidate tool | Synthetic arguments | Still requires verification |+| --- | --- | --- |+| `slack_v2-list-channels` | Optional string `cursor`, integer `limit`, comma-separated string `types`, boolean `exclude_archived` | Exact name, supported properties, defaults, string vs array types |+| `slack_v2-list-members-in-channel` | Required string `channel`; optional `cursor`, `limit` | Channel argument name/shape and whether pagination is exposed |+| `slack_v2-list-users` | Optional `cursor`, `limit` | Exact schema, pagination, email scope, output completeness |++Resolve tools from the actual session's `tools/list`; do not blindly send these+provisional arguments or invent an unsupported cursor. An action may paginate+internally or expose an aggregate-result limit rather than a Slack page size.++### Envelopes, pagination, and directory data++The three `list_*.json` files are synthetic MCP `CallToolResult` objects, without+the outer JSON-RPC `id`/`result` wrapper. Each has `isError: false` and one+`content` entry whose `type` is `text` and whose `text` is JSON:++- Channels: `{ "ok": true, "channels": [...], "response_metadata": { "next_cursor": "" } }`.+ Includes one public and one private channel to exercise filtering, not as a+ claim about the defaults of the live action.+- Members: `{ "ok": true, "members": ["U..."], "response_metadata": { "next_cursor": "synthetic-next-page" } }`.+ The nonempty cursor deliberately represents an incomplete page; it is not a+ usable upstream token or evidence that Pipedream returns cursors.+- Users: `{ "ok": true, "members": [...], "response_metadata": { "next_cursor": "" } }`.+ Includes an active human with `profile.email`, a bot without an email, and a+ deleted human. IDs are consistent across these fixtures; all emails use+ `example.com`.++Live text versus `structuredContent`, wrappers such as `ret`, bare arrays,+`response_metadata.next_cursor`, and internal auto-pagination are **unknown**.+The synthetic empty cursors do not prove complete live results. No real+`list-users` call was possible, so output truncation and maximum workspace size+are untested. `profile.email`, `is_bot`, and `deleted` are plausible Slack Web API+fields, not confirmed Pipedream output. In particular, email availability depends+on the connected account's permissions (including `users:read.email`).++| Measurement | Result |+| --- | --- |+| Per-call latency (channels / members / users) | Not measured; no live calls |+| Rate-limit error text / HTTP 429 / `Retry-After` | Not observed; no live calls |+| Directory truncation | Not tested on a real workspace |++## What the adapter must tolerate++- Both connection slugs, without assuming the action-name prefix equals the slug.+- Missing or renamed tools and changed argument schemas: report unsupported+ capability rather than treating it as an empty workspace.+- JSON text content and structured results; recognized wrapper objects or arrays+ only. Unknown envelopes, malformed JSON, truncation, and non-JSON prose must+ not silently become successful empty lists.+- MCP `isError`, JSON-RPC errors, HTTP failures, and Slack `ok: false` separately+ from valid empty data. Missing scopes and expired connections need actionable+ errors, not empty membership.+- Cursor pagination where exposed, with bounded pages, repeated-cursor detection,+ and deduplication. An omitted cursor from an internally paginated action is not+ proof of completeness until that action's contract is verified. Flag incomplete+ membership instead of claiming everyone was resolved.+- Rate limits surfaced at either transport or action level. Honor `Retry-After`+ when available, bound retries, and preserve partial-resolution status rather+ than retrying indefinitely. No exact upstream error text is asserted here.+- Missing/null profile or email, bots, deleted users, duplicate user IDs, and+ member IDs absent from the directory. Do not manufacture email addresses or+ invite unmatched users; use the existing Macro team-roster matcher.+- Public/private/IM/MPIM flags and archived channels. Never stage unsupported+ kinds simply because their ID begins with `C`; private channels may use it too.+- Large directories and long-running actions: enforce time/output budgets and+ distinguish truncation from complete results. Preserve total member counts+ independently of any stored-participant cap.++These are adapter requirements, not claims that every variant was observed or+that production handling is implemented by this documentation-only task.++## Read-only live verification procedure++1. Authenticate Doppler outside repository files. Load only the three named+ Pipedream credentials from DCS `document-cognition` / `dev` into the probe's+ process environment. Disable shell tracing; never print or persist secrets,+ authorization headers, account credentials, or raw user data.+2. Match `PipedreamClient::access_token` in+ `crates/pipedream_mcp/src/outbound/api.rs`: POST form-encoded+ `grant_type=client_credentials`, `client_id`, and `client_secret` to+ `https://api.pipedream.com/v1/oauth/token`. Keep the bearer only in memory.+3. GET `/v1/connect/apps/slack` and `/v1/connect/apps/slack_v2` with that bearer;+ record HTTP status, `name_slug`, and `auth_type`, not the whole response.+4. For an authorized dev user with an existing Slack connection, GET+ `/v1/connect/{project_id}/accounts?external_user_id=<dev user>` with+ `x-pd-environment: development`. Record only app slugs and whether the account+ is known to originate in hosted Connect. Existing accounts alone do not prove+ what the current UI creates. Do not create accounts during this read-only+ investigation. If reachable, use a read-only dev DB transaction to select+ distinct `app_slug` values from `pipedream_mcp_connections`, limited to+ `slack` and `slack_v2`; do not export user/account IDs.+5. Speak Streamable HTTP MCP to `https://remote.mcp.pipedream.net`. Match+ `McpUpstream::upstream` with `Authorization: Bearer <token>`,+ `x-pd-project-id`, `x-pd-environment: development`, `x-pd-external-user-id`,+ `x-pd-app-slug` (the verified connected slug), and `x-pd-tool-mode: tools-only`.+ Use `Content-Type: application/json` and accept both `application/json` and+ `text/event-stream`. Send `initialize`, retain any returned session ID and+ negotiated protocol version for subsequent requests, then send+ `notifications/initialized` and `tools/list`. Follow tool-list pagination.+6. Capture exact schemas for the three read-only listing actions. Call only+ those tools with schema-supported arguments, using a channel returned by the+ channel listing. Record per-call elapsed time and actual envelope keys. Test+ small page sizes/cursors if supported and run `list-users` on a real workspace+ to check complete JSON, output caps, and email/bot/deleted fields. Do not+ deliberately provoke rate limits; record any naturally encountered errors.+7. Replace the synthetic fixtures with redacted live results under 50 KB each.+ Replace all names, emails, workspace/user/channel IDs, cursors, URLs, purpose+ text, and other user content with synthetic values while preserving structure+ and cross-fixture identity relationships. If arrays must be sampled to meet+ the size budget, document sampling separately from upstream truncation.+ Update the evidence tables, exact tool schemas, and canonical-slug decision.++## Verified behaviour++### Automated integration verification (2026-10-05)++These results validate the integrated code, **not the live Pipedream contract**.+Tests used synthetic MCP fixtures, fake domain ports, and the existing local+Postgres database, which already included the manual-initiator migration. No+hosted database was queried or reset. Rust commands used the installed toolchain+and cached dependencies (`CARGO_NET_OFFLINE=true`); Nix was unavailable.+`SQLX_OFFLINE` was unset for every test command. Database tests used the local URL+from `tooling/just/database.just` via `DATABASE_URL`.++| Check | Result |+| --- | --- |+| `cargo test -p import -p mcp_select -p onboarding -p ai_tools -p channels -p pipedream_mcp` | Passed: 455 tests (114 import, 5 mcp_select, 6 onboarding, 47 ai_tools, 267 channels, 16 pipedream_mcp). Initial attempt lacked `DATABASE_URL`; rerun against local Postgres passed. |+| `cargo test -p slack_integration --features ledger` | Passed: 100 tests, including the archive adapter's new fail-closed error-mapping regression. |+| `just rust-check` | Passed after fixing the archive ledger's exhaustive match for the new `UnsupportedDiscovery` error. Archive import policy is unchanged. |+| `just check full` / `just check` | Wrapper cannot discover changes in this non-colocated jj workspace: its Git commands fail and it exits 0 having checked nothing. Not counted as a pass. Component checks below were run directly. |+| `cargo fmt --check`; `just clippy` | Passed; Clippy covers the workspace and the separate sync-service invocation. |+| Biome CI, oxlint, ast-grep on files changed since the task base, selected with `jj diff` | Passed (exit 0). Warnings: picker metadata-parser complexity; pre-existing domain-to-inbound toolset import in `import/src/domain/service.rs`. Neither was expanded by the integration fix. |+| `bun run check` in `apps/web` | Passed with `NODE_OPTIONS=--max-old-space-size=12288`; the default 4 GB Node heap exhausted memory. Includes schema checks, TypeScript, and Biome. |+| `bun run test` in `apps/web` | Passed with `LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 TZ=UTC`: 1,408 files, 12,539 tests, 1 todo, 168.53 s. Initial host-locale run had 32 failures in reminder/date/calendar tests; locale/timezone normalization required no code changes. |+| Focused Slack connection, slug, picker-model, and picker-component tests | Passed: 4 files, 38 tests, 1.39 s. |+| `bun run gen-api --check` | All 14 services generated successfully; final Git diff step failed in the jj-only workspace (script exit 1). Independent `jj diff` confirmed no generated-file changes, including cognition OpenAPI, client, metadata, and tool schemas. |++The automated coverage exercises public-only discovery, onboarding top-15+selection, manual discovery including archived candidates, bounded pagination,+100-channel enrichment, roster matching, retry/fallback behavior, manual-run+concurrency, canonical target reuse, import-time member refresh, endpoint+validation, both Slack slug aliases, picker selection, and progress rendering.+It does not prove that a real Pipedream action has the fixture's schema or returns+complete data.++The inherited ancestry contains exactly one nonempty commit for each task+T01–T12, without conflicts. The requested bookmark is absent locally, and this+jj version uses `bookmarks(...)`, not `bookmark(...)`; verification used+`jj log -r 'c55d9ccc::@'` instead. Bookmark placement and integration remain owned+by the planner runner; no rebase, merge, or bookmark mutation was performed here.++### Browser and latency evidence: still blocked++Nix is not installed, so the prescribed local-stack preflight could not run.+The DCS dev Doppler credential probe still exits 1 (secret output suppressed).+No authenticated dev Slack workspace or full local app stack was available for+this verification. Browser E2E was **not performed**. The following remain release+gates, not successful observations:++1. Connect Slack through Pipedream development; verify the live slug, tool+ schemas, email availability, and pagination using the procedure above.+2. Settings → Connections → Slack → Import channels → Find channels: compare+ displayed match counts with the team roster, select two channels, import,+ then open both links and verify names, Team kind, and matched participants.+3. Find channels again: verify no duplicate rows, refreshed counts on staged+ rows, and already-imported rows remaining nonselectable. Check archived+ filtering, empty/error states, and exclusion of private/DM conversations.+4. Onboard a fresh user with Slack connected: verify auto-imported channels+ include matched teammates, without changing onboarding to manual selection.+5. Record channel-list, directory, per-channel member, complete discovery, and+ two-channel import latency with workspace/channel sizes. **No live latency+ was measured**; test durations above are not service latency measurements.++### Known limitations++- `/import/state` returns staged channel metadata together; payload size grows+ with staged channel count. There is no paginated picker-state endpoint.+- Manual discovery enriches members for at most 100 channels, preferring larger+ channels. The remainder stays unresolved until selected for import. Import+ batches attempt fresh resolution with one shared session/directory; failures+ fall back to staged participants, potentially leaving only the importer.+- Best-effort membership enrichment shares a deadline measured from the start+ of discovery: 170 seconds for onboarding, 350 seconds for manual discovery,+ leaving 10 seconds before the outer timeout. Directory/member reads, metadata+ enrichment, and notifications are bounded; expiry keeps staged channels ready+ with unresolved membership instead of failing an otherwise usable discovery.+ Initial listing or staging can still fail or reach the outer timeout.+- Stored matched participants are capped at 100; `member_count` is separate.+ After enrichment it counts known, non-bot, non-deleted directory members, not+ necessarily Slack's displayed total. Unmatched people are not invited.+- Listing/directory/member reads stop at 10/10/5 pages respectively. Exhausting+ those bounds can leave partial data; current service code does not surface+ cursor exhaustion as an explicit incomplete-result state. Repeated cursors+ are bounded by those same limits, not diagnosed separately. Large-workspace+ completeness needs follow-up in discovery/adapter work before broad rollout.+- Private channels (including private `C` IDs), DMs, and group DMs are unsupported.+ Public classification requires explicit `is_private: false`; missing/null or+ malformed visibility is omitted. Where supported, listing tools are requested+ with only the `public_channel` filter.+ Public Slack channels become Macro Team channels, not public Macro channels.+ Any connected team member may import; the importer owns the new channel.+- This is channel shape only: no messages, files, ongoing membership sync, or+ invitation flow. The ZIP-archive feature's authorization and behavior remain+ separate. MCP compatibility and canonical new-connection slug remain+ provisional until the live checks above succeed.++## Fixture checks++```sh+ls crates/import/src/outbound/mcp_slack_source/fixtures/+jq . crates/import/src/outbound/mcp_slack_source/fixtures/*.json+grep -n "Canonical slug" docs/SLACK_LIVE_CHANNEL_IMPORT.md+grep -rn "@" crates/import/src/outbound/mcp_slack_source/fixtures/ | grep -v example.com+```++The final pipeline should emit nothing (exit status 1 means no matching lines).+Also parse each text content entry as JSON, check individual file sizes, and+review all string values; the email grep alone is not a redaction audit.diff --git a/packages/sdk/generated/cognition/index.ts b/packages/sdk/generated/cognition/index.tsindex 4ebbff5e9f..ef043f9f47 100644--- a/packages/sdk/generated/cognition/index.ts+++ b/packages/sdk/generated/cognition/index.ts@@ -1,4 +1,4 @@// This file is auto-generated by @hey-api/openapi-tsexport { type Options, Sdk } from './sdk.gen';-export type { AccessLevel, AddMcpServerData, AddMcpServerError, AddMcpServerErrors, AddMcpServerResponse, AddMcpServerResponses, AddServerRequest, AiAdmissionErrorBody, AiFeature, AssistantMessagePart, AttachmentMetadata, AttachmentType, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogError, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponse, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolError, CallToolErrors, CallToolRequest, CallToolResponse, CallToolResponse2, CallToolResponses, ChannelSharePermission, ChannelType, Chat, ChatAttachment, ChatHistory, ChatHistoryBatchMessagesRequest, ChatMessage, ChatMessageContent, ChatMessageError, ChatMessageWithAttachments, ChatPreview, ChatPreviewData, ChatResponse, ChatStream, ClientOptions, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponse, CompleteHandlerResponses, CompleteOnboardingRequest, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionError, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponse, CompletePipedreamMcpConnectionResponses, CompletionUsage, ConnectedServer, ConversationRecord, CopyChatData, CopyChatError, CopyChatErrors, CopyChatResponse, CopyChatResponses, CreateChatData, CreateChatError, CreateChatErrors, CreateChatRequest, CreateChatResponse, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenError, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponse, CreatePipedreamMcpTokenResponses, DatabaseChange, DeleteChatData, DeleteChatError, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerError, DeleteMcpServerErrors, DeleteMcpServerResponse, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionError, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponse, DeletePipedreamMcpConnectionResponses, DismissRunHandlerData, DismissRunHandlerErrors, DismissRunHandlerResponse, DismissRunHandlerResponses, DocumentCognitionServiceApiVersion, DocumentReference, DocumentTextPart, DynamicSchema, Entity, EntityType, ErrorBody, ErrorResponse, Expiry, FeatureUsage, FileType, GenericErrorResponse, GetBatchPreviewData, GetBatchPreviewError, GetBatchPreviewErrors, GetBatchPreviewRequest, GetBatchPreviewResponse, GetBatchPreviewResponse2, GetBatchPreviewResponses, GetChatData, GetChatError, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerError, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponse, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerError, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponse, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsError, GetChatPermissionsErrors, GetChatPermissionsResponse, GetChatPermissionsResponse2, GetChatPermissionsResponses, GetChatResponse, GetChatResponse2, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerError, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponse, GetChatsForAttachmentHandlerResponses, GetChatsForAttachmentResponse, GetCitationHandlerData, GetCitationHandlerError, GetCitationHandlerErrors, GetCitationHandlerResponse, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerError, GetMemoryHandlerErrors, GetMemoryHandlerResponse, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Response, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponse, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerError, GetUsageHandlerErrors, GetUsageHandlerResponse, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, HttpSendChatMessageRequest, ImportEntity, ImportRun, ImportSource, ImportState, ImportStatus, Initiator, JwtPayload, LinearIssueMeta, LinkShare, ListMcpServersData, ListMcpServersError, ListMcpServersErrors, ListMcpServersResponse, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsError, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponse, ListPipedreamMcpConnectionsResponses, MacroUserIdStr, McpAuthCallbackData, McpAuthCallbackError, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, MemoryErrorBody, MemoryResponse, MessageWithAttachments, NewAttachment, NewChatMessage, NewMessageAttachment, NotionDocMeta, OnboardingRow, OnboardingState, OnboardingStatus, PatchChatData, PatchChatError, PatchChatErrors, PatchChatRequest, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatError, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, PipedreamCatalogEntryResponse, PipedreamCatalogResponse, PipedreamCompleteRequest, PipedreamConnectionResponse, PipedreamTokenResponse, PipedreamUpdateRequest, Price, ProjectionStateResponse, ProjectionStatus, RefreshCadence, RejectToolCallData, RejectToolCallError, RejectToolCallErrors, RejectToolCallRequest, RejectToolCallResponses, RetryGatherHandlerData, RetryGatherHandlerError, RetryGatherHandlerErrors, RetryGatherHandlerResponse, RetryGatherHandlerResponses, RevertDeleteChatData, RevertDeleteChatError, RevertDeleteChatErrors, RevertDeleteChatResponses, Role, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponse, RunImportHandlerResponses, RunImportOutcome, RunImportRequest, RunStatus, SendChatMessageData, SendChatMessageError, SendChatMessageErrors, SendChatMessagePayload, SendChatMessageResponse, SendChatMessageResponse2, SendChatMessageResponses, ServerResponse, SetPricingHandlerData, SetPricingHandlerError, SetPricingHandlerErrors, SetPricingHandlerResponses, SetPricingRequest, SharePermissionV2, SlackChannelMeta, SlackParticipant, StartAuthRequest, StartAuthResponse, StartMcpAuthData, StartMcpAuthError, StartMcpAuthErrors, StartMcpAuthResponse, StartMcpAuthResponses, StopChatStreamData, StopChatStreamError, StopChatStreamError2, StopChatStreamErrors, StopChatStreamRequest, StopChatStreamResponse, StopChatStreamResponse2, StopChatStreamResponses, StreamError, StringIdResponse, StructuredCompletionData, StructuredCompletionError, StructuredCompletionError2, StructuredCompletionErrors, StructuredCompletionOutcome, StructuredCompletionRequest, StructuredCompletionResponse, StructuredCompletionResponse2, StructuredCompletionResponses, StructuredToolActivity, TargetType, ToolOutcome, ToolSet, UpdateChannelSharePermission, UpdateMcpServerData, UpdateMcpServerError, UpdateMcpServerErrors, UpdateMcpServerResponse, UpdateMcpServerResponses, UpdateOperation, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionError, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponse, UpdatePipedreamMcpConnectionResponses, UpdateServerRequest, UpdateSharePermissionRequestV2, UpdateToolCallData, UpdateToolCallError, UpdateToolCallErrors, UpdateToolCallRequest, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseError, UpdateToolResponseErrors, UpdateToolResponseRequest, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionError, UpsertAiProjectionErrors, UpsertAiProjectionResponse, UpsertAiProjectionResponses, UpsertProjectionRequest, Usage, UsageRequest, UsageSummary, UserPdfRect, UserToolResponseValue, WebCitation, WithChatId } from './types.gen';+export type { AccessLevel, AddMcpServerData, AddMcpServerError, AddMcpServerErrors, AddMcpServerResponse, AddMcpServerResponses, AddServerRequest, AiAdmissionErrorBody, AiFeature, AssistantMessagePart, AttachmentMetadata, AttachmentType, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogError, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponse, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolError, CallToolErrors, CallToolRequest, CallToolResponse, CallToolResponse2, CallToolResponses, ChannelSharePermission, ChannelType, Chat, ChatAttachment, ChatHistory, ChatHistoryBatchMessagesRequest, ChatMessage, ChatMessageContent, ChatMessageError, ChatMessageWithAttachments, ChatPreview, ChatPreviewData, ChatResponse, ChatStream, ClientOptions, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponse, CompleteHandlerResponses, CompleteOnboardingRequest, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionError, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponse, CompletePipedreamMcpConnectionResponses, CompletionUsage, ConnectedServer, ConversationRecord, CopyChatData, CopyChatError, CopyChatErrors, CopyChatResponse, CopyChatResponses, CreateChatData, CreateChatError, CreateChatErrors, CreateChatRequest, CreateChatResponse, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenError, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponse, CreatePipedreamMcpTokenResponses, DatabaseChange, DeleteChatData, DeleteChatError, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerError, DeleteMcpServerErrors, DeleteMcpServerResponse, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionError, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponse, DeletePipedreamMcpConnectionResponses, DiscoverHandlerData, DiscoverHandlerErrors, DiscoverHandlerResponse, DiscoverHandlerResponses, DismissRunHandlerData, DismissRunHandlerErrors, DismissRunHandlerResponse, DismissRunHandlerResponses, DocumentCognitionServiceApiVersion, DocumentReference, DocumentTextPart, DynamicSchema, Entity, EntityType, ErrorBody, ErrorResponse, Expiry, FeatureUsage, FileType, GenericErrorResponse, GetBatchPreviewData, GetBatchPreviewError, GetBatchPreviewErrors, GetBatchPreviewRequest, GetBatchPreviewResponse, GetBatchPreviewResponse2, GetBatchPreviewResponses, GetChatData, GetChatError, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerError, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponse, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerError, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponse, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsError, GetChatPermissionsErrors, GetChatPermissionsResponse, GetChatPermissionsResponse2, GetChatPermissionsResponses, GetChatResponse, GetChatResponse2, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerError, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponse, GetChatsForAttachmentHandlerResponses, GetChatsForAttachmentResponse, GetCitationHandlerData, GetCitationHandlerError, GetCitationHandlerErrors, GetCitationHandlerResponse, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerError, GetMemoryHandlerErrors, GetMemoryHandlerResponse, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Response, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponse, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerError, GetUsageHandlerErrors, GetUsageHandlerResponse, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, HttpSendChatMessageRequest, ImportEntity, ImportRun, ImportSource, ImportState, ImportStatus, Initiator, JwtPayload, LinearIssueMeta, LinkShare, ListMcpServersData, ListMcpServersError, ListMcpServersErrors, ListMcpServersResponse, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsError, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponse, ListPipedreamMcpConnectionsResponses, MacroUserIdStr, McpAuthCallbackData, McpAuthCallbackError, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, MemoryErrorBody, MemoryResponse, MessageWithAttachments, NewAttachment, NewChatMessage, NewMessageAttachment, NotionDocMeta, OnboardingRow, OnboardingState, OnboardingStatus, PatchChatData, PatchChatError, PatchChatErrors, PatchChatRequest, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatError, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, PipedreamCatalogEntryResponse, PipedreamCatalogResponse, PipedreamCompleteRequest, PipedreamConnectionResponse, PipedreamTokenResponse, PipedreamUpdateRequest, Price, ProjectionStateResponse, ProjectionStatus, RefreshCadence, RejectToolCallData, RejectToolCallError, RejectToolCallErrors, RejectToolCallRequest, RejectToolCallResponses, RetryGatherHandlerData, RetryGatherHandlerError, RetryGatherHandlerErrors, RetryGatherHandlerResponse, RetryGatherHandlerResponses, RevertDeleteChatData, RevertDeleteChatError, RevertDeleteChatErrors, RevertDeleteChatResponses, Role, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponse, RunImportHandlerResponses, RunImportOutcome, RunImportRequest, RunStatus, SendChatMessageData, SendChatMessageError, SendChatMessageErrors, SendChatMessagePayload, SendChatMessageResponse, SendChatMessageResponse2, SendChatMessageResponses, ServerResponse, SetPricingHandlerData, SetPricingHandlerError, SetPricingHandlerErrors, SetPricingHandlerResponses, SetPricingRequest, SharePermissionV2, SlackChannelMeta, SlackParticipant, StartAuthRequest, StartAuthResponse, StartMcpAuthData, StartMcpAuthError, StartMcpAuthErrors, StartMcpAuthResponse, StartMcpAuthResponses, StopChatStreamData, StopChatStreamError, StopChatStreamError2, StopChatStreamErrors, StopChatStreamRequest, StopChatStreamResponse, StopChatStreamResponse2, StopChatStreamResponses, StreamError, StringIdResponse, StructuredCompletionData, StructuredCompletionError, StructuredCompletionError2, StructuredCompletionErrors, StructuredCompletionOutcome, StructuredCompletionRequest, StructuredCompletionResponse, StructuredCompletionResponse2, StructuredCompletionResponses, StructuredToolActivity, TargetType, ToolOutcome, ToolSet, UpdateChannelSharePermission, UpdateMcpServerData, UpdateMcpServerError, UpdateMcpServerErrors, UpdateMcpServerResponse, UpdateMcpServerResponses, UpdateOperation, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionError, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponse, UpdatePipedreamMcpConnectionResponses, UpdateServerRequest, UpdateSharePermissionRequestV2, UpdateToolCallData, UpdateToolCallError, UpdateToolCallErrors, UpdateToolCallRequest, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseError, UpdateToolResponseErrors, UpdateToolResponseRequest, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionError, UpsertAiProjectionErrors, UpsertAiProjectionResponse, UpsertAiProjectionResponses, UpsertProjectionRequest, Usage, UsageRequest, UsageSummary, UserPdfRect, UserToolResponseValue, WebCitation, WithChatId } from './types.gen';diff --git a/packages/sdk/generated/cognition/sdk.gen.ts b/packages/sdk/generated/cognition/sdk.gen.tsindex ef0938a549..77cd0aa7e8 100644--- a/packages/sdk/generated/cognition/sdk.gen.ts+++ b/packages/sdk/generated/cognition/sdk.gen.ts@@ -2,7 +2,7 @@import type { Client, ClientMeta, Options as Options2, RequestResult, TDataShape } from './client';import { client } from './client.gen';-import type { AddMcpServerData, AddMcpServerErrors, AddMcpServerResponses, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolErrors, CallToolResponses, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponses, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponses, CopyChatData, CopyChatErrors, CopyChatResponses, CreateChatData, CreateChatErrors, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponses, DeleteChatData, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerErrors, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponses, DismissRunHandlerData, DismissRunHandlerErrors, DismissRunHandlerResponses, GetBatchPreviewData, GetBatchPreviewErrors, GetBatchPreviewResponses, GetChatData, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsErrors, GetChatPermissionsResponses, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponses, GetCitationHandlerData, GetCitationHandlerErrors, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerErrors, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerErrors, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponses, ListMcpServersData, ListMcpServersErrors, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponses, McpAuthCallbackData, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, PatchChatData, PatchChatErrors, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, RejectToolCallData, RejectToolCallErrors, RejectToolCallResponses, RetryGatherHandlerData, RetryGatherHandlerErrors, RetryGatherHandlerResponses, RevertDeleteChatData, RevertDeleteChatErrors, RevertDeleteChatResponses, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponses, SendChatMessageData, SendChatMessageErrors, SendChatMessageResponses, SetPricingHandlerData, SetPricingHandlerErrors, SetPricingHandlerResponses, StartMcpAuthData, StartMcpAuthErrors, StartMcpAuthResponses, StopChatStreamData, StopChatStreamErrors, StopChatStreamResponses, StructuredCompletionData, StructuredCompletionErrors, StructuredCompletionResponses, UpdateMcpServerData, UpdateMcpServerErrors, UpdateMcpServerResponses, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponses, UpdateToolCallData, UpdateToolCallErrors, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseErrors, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionErrors, UpsertAiProjectionResponses } from './types.gen';+import type { AddMcpServerData, AddMcpServerErrors, AddMcpServerResponses, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolErrors, CallToolResponses, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponses, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponses, CopyChatData, CopyChatErrors, CopyChatResponses, CreateChatData, CreateChatErrors, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponses, DeleteChatData, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerErrors, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponses, DiscoverHandlerData, DiscoverHandlerErrors, DiscoverHandlerResponses, DismissRunHandlerData, DismissRunHandlerErrors, DismissRunHandlerResponses, GetBatchPreviewData, GetBatchPreviewErrors, GetBatchPreviewResponses, GetChatData, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsErrors, GetChatPermissionsResponses, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponses, GetCitationHandlerData, GetCitationHandlerErrors, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerErrors, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerErrors, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponses, ListMcpServersData, ListMcpServersErrors, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponses, McpAuthCallbackData, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, PatchChatData, PatchChatErrors, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, RejectToolCallData, RejectToolCallErrors, RejectToolCallResponses, RetryGatherHandlerData, RetryGatherHandlerErrors, RetryGatherHandlerResponses, RevertDeleteChatData, RevertDeleteChatErrors, RevertDeleteChatResponses, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponses, SendChatMessageData, SendChatMessageErrors, SendChatMessageResponses, SetPricingHandlerData, SetPricingHandlerErrors, SetPricingHandlerResponses, StartMcpAuthData, StartMcpAuthErrors, StartMcpAuthResponses, StopChatStreamData, StopChatStreamErrors, StopChatStreamResponses, StructuredCompletionData, StructuredCompletionErrors, StructuredCompletionResponses, UpdateMcpServerData, UpdateMcpServerErrors, UpdateMcpServerResponses, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponses, UpdateToolCallData, UpdateToolCallErrors, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseErrors, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionErrors, UpsertAiProjectionResponses } from './types.gen';export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown> = Options2<TData, ThrowOnError, TResponse> & {/**@@ -276,6 +276,13 @@ export class Sdk extends HeyApiClient {});}+ /**+ * Discover candidates for user selection without auto-importing them.+ */+ public discoverHandler<ThrowOnError extends boolean = false>(options: Options<DiscoverHandlerData, ThrowOnError>): RequestResult<DiscoverHandlerResponses, DiscoverHandlerErrors, ThrowOnError> {+ return (options.client ?? this.client).post<DiscoverHandlerResponses, DiscoverHandlerErrors, ThrowOnError>({ url: '/import/runs/{source}/discover', ...options });+ }+/*** Dismiss one source's import section.*/diff --git a/packages/sdk/generated/cognition/types.gen.ts b/packages/sdk/generated/cognition/types.gen.tsindex 8932d32bf4..bd20b33c32 100644--- a/packages/sdk/generated/cognition/types.gen.ts+++ b/packages/sdk/generated/cognition/types.gen.ts@@ -799,7 +799,7 @@ export type ImportStatus = 'staged' | 'importing' | 'imported' | 'discarded';* Where an import entity was first staged from. Provenance only — never a* visibility filter.*/-export type Initiator = 'onboarding' | 'chat' | 'archive';+export type Initiator = 'onboarding' | 'chat' | 'archive' | 'manual';export type JwtPayload = {token: string;@@ -1332,16 +1332,28 @@ export type SharePermissionV2 = {* Metadata for one staged Slack channel.*/export type SlackChannelMeta = {+ /**+ * Whether Slack has archived this channel.+ */+ archived?: boolean;/*** Slack's channel id (e.g. `C0123456789`), stable across renames.*/channel_id?: string | null;+ /**+ * Total human members in the Slack channel.+ */+ member_count?: number | null;+ /**+ * Whether `participants` reflects a live membership read.+ */+ members_resolved?: boolean;/*** Channel name without the leading `#`.*/name: string;/**- * The channel's most relevant members, when discoverable.+ * Slack members matched to the Macro team roster when discovery resolved membership.*/participants?: Array<SlackParticipant>;/**@@ -2298,6 +2310,38 @@ export type RunImportHandlerResponses = {export type RunImportHandlerResponse = RunImportHandlerResponses[keyof RunImportHandlerResponses];+export type DiscoverHandlerData = {+ body?: never;+ path: {+ /**+ * Import source+ */+ source: string;+ };+ query?: never;+ url: '/import/runs/{source}/discover';+};++export type DiscoverHandlerErrors = {+ /**+ * Unknown import source or discovery not supported+ */+ 400: unknown;+ /**+ * Internal server error+ */+ 500: unknown;+};++export type DiscoverHandlerResponses = {+ /**+ * Discovery accepted (idempotent)+ */+ 204: void;+};++export type DiscoverHandlerResponse = DiscoverHandlerResponses[keyof DiscoverHandlerResponses];+export type DismissRunHandlerData = {body?: never;path: {diff --git a/packages/sdk/specs/cognition.json b/packages/sdk/specs/cognition.jsonindex 2b2f6d6fe8..404f2e14b7 100644--- a/packages/sdk/specs/cognition.json+++ b/packages/sdk/specs/cognition.json@@ -1097,6 +1097,35 @@}}},+ "/import/runs/{source}/discover": {+ "post": {+ "tags": ["import"],+ "summary": "Discover candidates for user selection without auto-importing them.",+ "operationId": "discover_handler",+ "parameters": [+ {+ "name": "source",+ "in": "path",+ "description": "Import source",+ "required": true,+ "schema": {+ "type": "string"+ }+ }+ ],+ "responses": {+ "204": {+ "description": "Discovery accepted (idempotent)"+ },+ "400": {+ "description": "Unknown import source or discovery not supported"+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },"/import/runs/{source}/dismiss": {"post": {"tags": ["import"],@@ -3866,7 +3895,7 @@"Initiator": {"type": "string","description": "Where an import entity was first staged from. Provenance only — never a\nvisibility filter.",- "enum": ["onboarding", "chat", "archive"]+ "enum": ["onboarding", "chat", "archive", "manual"]},"JwtPayload": {"type": "object",@@ -4555,10 +4584,24 @@"description": "Metadata for one staged Slack channel.","required": ["name"],"properties": {+ "archived": {+ "type": "boolean",+ "description": "Whether Slack has archived this channel."+ },"channel_id": {"type": ["string", "null"],"description": "Slack's channel id (e.g. `C0123456789`), stable across renames."},+ "member_count": {+ "type": ["integer", "null"],+ "format": "int64",+ "description": "Total human members in the Slack channel.",+ "minimum": 0+ },+ "members_resolved": {+ "type": "boolean",+ "description": "Whether `participants` reflects a live membership read."+ },"name": {"type": "string","description": "Channel name without the leading `#`."@@ -4568,7 +4611,7 @@"items": {"$ref": "#/components/schemas/SlackParticipant"},- "description": "The channel's most relevant members, when discoverable."+ "description": "Slack members matched to the Macro team roster when discovery resolved membership."},"purpose": {"type": ["string", "null"],diff --git a/packages/sdk/src/coverage/skipped.ts b/packages/sdk/src/coverage/skipped.tsindex fe90b6fef5..8c4ab0e14e 100644--- a/packages/sdk/src/coverage/skipped.ts+++ b/packages/sdk/src/coverage/skipped.ts@@ -159,6 +159,8 @@ export const cognitionExcluded = ['mcpAuthCallback','mcpOauthClientMetadata','rejectToolCall',+ // Channel discovery shares the app-only import workflow below.+ 'discoverHandler','runImportHandler','dismissRunHandler','retryGatherHandler',diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rsindex 9d2bf1d983..3d2387012c 100644--- a/services/document_cognition_service/src/api/context/test.rs+++ b/services/document_cognition_service/src/api/context/test.rs@@ -559,6 +559,9 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::ArArc::new(creator),recorder.clone(),)+ .with_slack_source(Arc::new(+ import::outbound::mcp_slack_source::McpSlackSource::new(mcp_selector.clone()),+ )).with_admission(admission.clone()),);let onboarding_service = Arc::new(onboarding::domain::service::OnboardingServiceImpl::new(diff --git a/services/document_cognition_service/src/api/swagger.rs b/services/document_cognition_service/src/api/swagger.rsindex 223f49e20d..1613cc290c 100644--- a/services/document_cognition_service/src/api/swagger.rs+++ b/services/document_cognition_service/src/api/swagger.rs@@ -94,6 +94,7 @@ use utoipa::OpenApi;import_api::get_state_handler,import_api::run_import_handler,import_api::retry_gather_handler,+ import_api::discover_handler,import_api::dismiss_run_handler,onboarding_api::get_state_handler,onboarding_api::complete_handler,diff --git a/services/document_cognition_service/src/main.rs b/services/document_cognition_service/src/main.rsindex 34482ec1a0..b8a102b9bb 100644--- a/services/document_cognition_service/src/main.rs+++ b/services/document_cognition_service/src/main.rs@@ -619,6 +619,7 @@ async fn main() -> anyhow::Result<()> {// The one sanctioned meeting point of the two MCP stacks: agents load// tools through this selector, which prefers a user's Pipedream// connectors and falls back to the native ones (see `mcp_select`).+ // The import service's live Slack source shares this selector.let mcp_selector: Arc<ai_tools::ToolMcpSelector> = Arc::new(mcp_select::McpToolSelector::new(Arc::new(mcp_server_repo.clone()),Arc::new(pipedream_repo.clone()),@@ -647,6 +648,9 @@ async fn main() -> anyhow::Result<()> {Arc::new(entity_creator),recorder.clone(),)+ .with_slack_source(Arc::new(+ import::outbound::mcp_slack_source::McpSlackSource::new(mcp_selector.clone()),+ )).with_admission(admission.clone()).with_notifier(import_notify),);