Multiplayer agent harness for work.
projects: refresh the Slack directory on demand when a channel link misses (#487)
Regan Bellf32ec42ae883
diff --git a/cli/templates/slack-manifest.json b/cli/templates/slack-manifest.jsonindex fd38af0b3..dcd8fbcca 100644--- a/cli/templates/slack-manifest.json+++ b/cli/templates/slack-manifest.json@@ -53,6 +53,9 @@"message.mpim","member_joined_channel","member_left_channel",+ "channel_created",+ "channel_rename",+ "channel_unarchive","reaction_added","reaction_removed"]diff --git a/src/api/app-helpers.ts b/src/api/app-helpers.tsindex 670681dc1..6c2b93a70 100644--- a/src/api/app-helpers.ts+++ b/src/api/app-helpers.ts@@ -54,6 +54,11 @@ export function createAppHelpers(deps: AppDeps, app: App) {adminBase ? `${adminBase}/admin/history?session=${encodeURIComponent(sessionId)}` : undefined;const surfaceContext = createSurfaceContextPuller(app);+ const directoryRefresher = createSurfaceContextPuller(app, { waitMs: 4_000 });++ async function refreshSurfaceDirectory(): Promise<void> {+ await directoryRefresher.pull("slack", { syncDirectory: true, count: 1 });+ }const reachDir: ReachDirectory = {resolveRecipient: (q) => deps.directory.resolve(q),resolveChannel: (q) => deps.directory.resolveChannel(q),@@ -603,6 +608,7 @@ export function createAppHelpers(deps: AppDeps, app: App) {effectiveDeploymentPermission,principalCanReadDeployment,principalGitPermission,+ refreshSurfaceDirectory,reconcileProjectMember,syncProjectChannelRoster,syncLinkedProjectRosters,diff --git a/src/api/app-sessions.ts b/src/api/app-sessions.tsindex 4e11833cd..9b95e4a45 100644--- a/src/api/app-sessions.ts+++ b/src/api/app-sessions.ts@@ -344,10 +344,20 @@ export function createSessionMethods(if (channel !== null) {const wanted = channel.trim().replace(/^#/, "");if (!wanted) return { status: "invalid_channel" };- const reachable = await deps.directory.listChannelsFor(principalId).catch(() => []);- const match =- reachable.find((c) => c.channelId === wanted) ??- reachable.find((c) => c.name.toLowerCase() === wanted.toLowerCase());+ const findChannel = async () => {+ const reachable = await deps.directory.listChannelsFor(principalId).catch(() => []);+ return (+ reachable.find((c) => c.channelId === wanted) ??+ reachable.find((c) => c.name.toLowerCase() === wanted.toLowerCase())+ );+ };+ let match = await findChannel();+ if (!match) {+ // The synced directory may not have caught up with a just-created channel;+ // ask the surface for a fresh sync and look once more before giving up.+ await h.refreshSurfaceDirectory().catch(() => undefined);+ match = await findChannel();+ }if (!match) return { status: "invalid_channel" };const channelScope = scopeId("channel", match.channelId);const inUse = (await deps.sessions.listAll()).some((session) => session.scopeId === channelScope);diff --git a/src/slack/events.ts b/src/slack/events.tsindex b964e3f79..3deffafa1 100644--- a/src/slack/events.ts+++ b/src/slack/events.ts@@ -193,6 +193,16 @@ export function registerSlackEvents(}});+ for (const evt of ["channel_created", "channel_rename", "channel_unarchive"] as const) {+ app.event(evt, async ({ event, body, client }: any) => {+ const e = event as { channel?: { id?: string } | string; event_ts?: string };+ const channel = typeof e.channel === "string" ? e.channel : e.channel?.id;+ if (deduper.seen(dedupeKey({ event_id: (body as { event_id?: string })?.event_id, channel, ts: e.event_ts })))+ return;+ await forceDirectorySync(client);+ });+ }+app.event("member_left_channel", async ({ event, body, client }: any) => {const e = event as { channel?: string; event_ts?: string };if (diff --git a/src/slack/manifest.json b/src/slack/manifest.jsonindex 9e0c02936..f222b2a0e 100644--- a/src/slack/manifest.json+++ b/src/slack/manifest.json@@ -53,6 +53,9 @@"message.mpim","member_joined_channel","member_left_channel",+ "channel_created",+ "channel_rename",+ "channel_unarchive","reaction_added","reaction_removed"]diff --git a/src/slack/surface-context.ts b/src/slack/surface-context.tsindex 7d5c1a8a6..ac0efdd52 100644--- a/src/slack/surface-context.ts+++ b/src/slack/surface-context.ts@@ -190,6 +190,10 @@ export function createSurfaceContextFulfiller(deps: {};try {const q = r.query ?? {};+ if (q.syncDirectory) {+ await directory.forceDirectorySync(client).catch(swallowAs("slack: on-demand directory sync", undefined));+ return await post({ messages: [] });+ }if (q.openGroup) return await post(await openGroupDm(client, q.openGroup.participants ?? []));if (typeof q.searchAll === "string" && q.searchAll) {return fulfillLiveSearch(diff --git a/src/types.ts b/src/types.tsindex 664b3d55e..1506643a8 100644--- a/src/types.ts+++ b/src/types.ts@@ -269,6 +269,7 @@ export interface SurfaceContextQuery {viewerToken?: string;file?: { ts: string; threadTs?: string; name?: string };openGroup?: { participants: string[] };+ syncDirectory?: boolean;}export interface SurfaceContextResult {diff --git a/test/projects.test.ts b/test/projects.test.tsindex 800ca2cdd..b92160133 100644--- a/test/projects.test.ts+++ b/test/projects.test.ts@@ -837,6 +837,40 @@ test("leaving a Project still cuts off everything after the member left", asyncassert.equal(await built.app.getSessionForViewer(session.id, "member"), null);assert.deepEqual(await built.app.listSessions("member"), []);});+test("linking a just-created channel refreshes the surface directory and retries", async () => {+ const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "projects-fresh-chan-")) }));+ await built.app.upsertDirectory([{ principalId: "owner", displayName: "Owner", type: "internal" }]);+ await built.app.upsertChannels(+ [{ channelId: "C-OLD", name: "old", isPrivate: false }],+ [{ channelId: "C-OLD", principalId: "owner" }],+ );+ const project = await built.app.createProject("owner", "Fresh");+ assert.ok(project);+ // Fulfil the on-demand sync the way the Slack surface would: push the new channel.+ const unlisten = built.app.onContextRequestCreated((r) => {+ if (!r.query.syncDirectory) return;+ void built.app+ .upsertChannels(+ [+ { channelId: "C-OLD", name: "old", isPrivate: false },+ { channelId: "C-NEW", name: "brand-new", isPrivate: false },+ ],+ [+ { channelId: "C-OLD", principalId: "owner" },+ { channelId: "C-NEW", principalId: "owner" },+ ],+ )+ .then(() => built.app.fulfillContextRequest(r.id, { result: { messages: [] } }));+ });+ try {+ const linked = await built.app.setProjectSlackChannel(project!.id, "owner", "#brand-new");+ assert.equal(linked.status, "ok");+ assert.equal(linked.status === "ok" && linked.project.slackChannel?.channelId, "C-NEW");+ } finally {+ unlisten();+ }+});+test("Project slack-channel routes gate on visibility and workspace use, and sync the channel roster", async (t) => {const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "projects-slack-")) }));const server = createInsecureTestServer(built.app, {});