Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Keep Slack thread context across directory warmup (#1769)

Regan Bell17d89a2b30f6
diff --git a/src/api/app-messaging.ts b/src/api/app-messaging.ts
index 3182668ed..aa1d4c86c 100644
--- a/src/api/app-messaging.ts
+++ b/src/api/app-messaging.ts
@@ -478,13 +478,14 @@ export function createMessagingMethods(
}
return true;
},
- async upsertChannels(channels, channelMembers, syncedAt, channelRosterIds, revocations) {
+ async upsertChannels(channels, channelMembers, syncedAt, channelRosterIds, revocations, partial) {
const applied = await deps.directory.replaceChannels(
channels,
channelMembers,
syncedAt,
channelRosterIds,
revocations,
+ partial,
);
await h.syncLinkedProjectRosters();
return applied;
diff --git a/src/api/app-types.ts b/src/api/app-types.ts
index c8726b79b..59320b8c5 100644
--- a/src/api/app-types.ts
+++ b/src/api/app-types.ts
@@ -504,6 +504,7 @@ export interface App {
syncedAt?: number,
channelRosterIds?: string[],
revocations?: ChannelMembership[],
+ partial?: boolean,
): Promise<boolean>;
upsertGroups(
groupMembers: GroupMembership[],
diff --git a/src/api/routes/directory.ts b/src/api/routes/directory.ts
index 4cb87718e..29a421201 100644
--- a/src/api/routes/directory.ts
+++ b/src/api/routes/directory.ts
@@ -49,6 +49,7 @@ async function pushDirectory(ctx: ApiCtx): Promise<void> {
workspaceUrl?: unknown;
membersSyncedAt?: unknown;
channelsSyncedAt?: unknown;
+ partialChannels?: unknown;
groupsSyncedAt?: unknown;
};
if (!Array.isArray(b.members) && !Array.isArray(b.channels) && !Array.isArray(b.groupMembers)) {
@@ -57,6 +58,19 @@ async function pushDirectory(ctx: ApiCtx): Promise<void> {
message: "members[], channels[], and/or groupMembers[] required",
});
}
+ if (
+ b.partialChannels !== undefined &&
+ (typeof b.partialChannels !== "boolean" ||
+ (b.partialChannels &&
+ (!Array.isArray(b.channels) ||
+ !Array.isArray(b.channelMembers) ||
+ numOrUndef(b.channelsSyncedAt) === undefined)))
+ ) {
+ return sendJson(res, 400, {
+ error: "bad_request",
+ message: "partial channels require channels[], channelMembers[] and channelsSyncedAt",
+ });
+ }
if (Array.isArray(b.members) && b.members.some((m) => isObj(m) && !isPrincipalType(m.type))) {
return sendJson(res, 400, {
error: "bad_request",
@@ -103,13 +117,15 @@ async function pushDirectory(ctx: ApiCtx): Promise<void> {
isObj(m) && typeof m.channelId === "string" && typeof m.principalId === "string",
)
: undefined;
- await app.upsertChannels(
+ const applied = await app.upsertChannels(
channels,
channelMembers,
numOrUndef(b.channelsSyncedAt),
channelRosterIds,
channelRevocations,
+ b.partialChannels === true,
);
+ if (b.partialChannels && !applied) return sendJson(res, 409, { error: "stale_directory_observation" });
channelCount = channels.length;
}
let groupMemberCount: number | undefined;
diff --git a/src/api/slack-core-client.ts b/src/api/slack-core-client.ts
index f94d4b441..1f03603a5 100644
--- a/src/api/slack-core-client.ts
+++ b/src/api/slack-core-client.ts
@@ -96,6 +96,7 @@ interface DirectoryPush {
workspaceUrl?: string;
membersSyncedAt?: number;
channelsSyncedAt?: number;
+ partialChannels?: boolean;
groupsSyncedAt?: number;
}
@@ -521,6 +522,7 @@ export function createSlackCoreClient(deps: SlackCoreClientDeps): SlackCoreClien
body.channelsSyncedAt,
body.channelRosterIds,
body.channelRevocations,
+ body.partialChannels,
)) && applied;
}
if (body.groupMembers) {
diff --git a/src/directory/directory-store.ts b/src/directory/directory-store.ts
index 83443ea57..95fbe7a3d 100644
--- a/src/directory/directory-store.ts
+++ b/src/directory/directory-store.ts
@@ -49,6 +49,7 @@ export interface DirectoryStore {
syncedAt?: number,
channelRosterIds?: string[],
revocations?: ChannelMembership[],
+ partial?: boolean,
): Promise<boolean>;
list(): Promise<DirectoryMember[]>;
listChannels(): Promise<DirectoryChannel[]>;
@@ -110,6 +111,7 @@ export function createDirectoryStore(): DirectoryStore {
let groupsSynced = false;
let workspaceUrl: string | undefined;
const syncedAts = new Map<string, number>();
+ const channelObservedAts = new Map<string, number>();
function acceptSync(section: string, syncedAt: number | undefined): boolean {
if (syncedAt === undefined) return true;
@@ -134,15 +136,45 @@ export function createDirectoryStore(): DirectoryStore {
members = next.filter((m) => m.principalId && m.type === "internal");
return true;
},
- async replaceChannels(nextChannels, nextChannelMembers, syncedAt, nextChannelRosterIds, revocations = []) {
- if (!acceptSync("channels", syncedAt)) return false;
- channels = nextChannels.filter((c) => c.channelId && c.name);
+ async replaceChannels(
+ nextChannels,
+ nextChannelMembers,
+ syncedAt,
+ nextChannelRosterIds,
+ revocations = [],
+ partial = false,
+ ) {
+ if (partial && (syncedAt === undefined || !Number.isFinite(syncedAt) || nextChannelMembers === undefined))
+ return false;
+ if (partial && (syncedAts.get("channels") ?? 0) >= syncedAt!) return false;
+ const protectedIds = new Set(
+ [...channelObservedAts]
+ .filter(([, at]) => syncedAt !== undefined && (partial ? at >= syncedAt : at > syncedAt))
+ .map(([id]) => id),
+ );
+ if (partial && nextChannels.some((channel) => protectedIds.has(channel.channelId))) return false;
+ if (!partial && !acceptSync("channels", syncedAt)) return false;
+ const updatedIds = new Set(nextChannels.map((channel) => channel.channelId));
+ channels = [
+ ...channels.filter((channel) =>
+ partial ? !updatedIds.has(channel.channelId) : protectedIds.has(channel.channelId),
+ ),
+ ...nextChannels.filter((c) => c.channelId && c.name && !protectedIds.has(c.channelId)),
+ ];
+ for (const id of updatedIds) {
+ if (partial) channelObservedAts.set(id, syncedAt!);
+ else if (!protectedIds.has(id)) channelObservedAts.delete(id);
+ }
const listed = new Set(channels.map((channel) => channel.channelId));
knownChannelRosters = knownChannelRosters
? new Set([...knownChannelRosters].filter((channelId) => listed.has(channelId)))
: undefined;
if (nextChannelMembers !== undefined) {
- const rosterIds = new Set(nextChannelRosterIds ?? channels.map((channel) => channel.channelId));
+ const rosterIds = new Set(
+ (nextChannelRosterIds ?? nextChannels.map((channel) => channel.channelId)).filter(
+ (id) => updatedIds.has(id) && !protectedIds.has(id),
+ ),
+ );
const byChannel = new Map(
[...(channelMembers ?? [])].filter(([channelId]) => listed.has(channelId) && !rosterIds.has(channelId)),
);
@@ -156,9 +188,10 @@ export function createDirectoryStore(): DirectoryStore {
for (const channelId of rosterIds) if (listed.has(channelId)) knownChannelRosters.add(channelId);
}
for (const member of revocations) {
- if (listed.has(member.channelId) && member.principalId)
+ if (updatedIds.has(member.channelId) && !protectedIds.has(member.channelId) && member.principalId)
channelMembers?.get(member.channelId)?.delete(member.principalId);
}
+ for (const id of channelObservedAts.keys()) if (!listed.has(id)) channelObservedAts.delete(id);
return true;
},
async channelMember(channelId, principalId) {
diff --git a/src/directory/postgres-directory-store.ts b/src/directory/postgres-directory-store.ts
index 347d9d467..7e39f33d6 100644
--- a/src/directory/postgres-directory-store.ts
+++ b/src/directory/postgres-directory-store.ts
@@ -182,6 +182,10 @@ export function createPostgresDirectoryStore(connectionString: string): Director
},
{ id: "directory/store/0002", statements: SYNC_STAMP_SCHEMA },
{ id: "directory/store/0003", statements: EXTERNAL_ROSTER_SCHEMA },
+ {
+ id: "directory/store/0004",
+ statements: ["ALTER TABLE directory_channels ADD COLUMN IF NOT EXISTS observed_at BIGINT"],
+ },
]);
async function pick<T>(
@@ -226,7 +230,8 @@ export function createPostgresDirectoryStore(connectionString: string): Director
hashCol: string,
hash: string,
syncedAt: number | undefined,
- write: (client: PoolClient) => Promise<void>,
+ write: (client: PoolClient) => Promise<boolean | void>,
+ partial = false,
): Promise<boolean> {
const syncedAtCol = hashCol.replace(/_hash$/, "_synced_at");
return withPgTransaction(await pool(), async (client) => {
@@ -235,13 +240,17 @@ export function createPostgresDirectoryStore(connectionString: string): Director
orgId,
]);
const row = sync.rows[0];
- if (syncedAt !== undefined && row?.[syncedAtCol] != null && Number(row[syncedAtCol]) > syncedAt) {
+ if (
+ syncedAt !== undefined &&
+ row?.[syncedAtCol] != null &&
+ (partial ? Number(row[syncedAtCol]) >= syncedAt : Number(row[syncedAtCol]) > syncedAt)
+ ) {
console.warn(
`[directory] refused stale ${hashCol.replace(/_hash$/, "")} swap for ${orgId}: stamped ${Number(row[syncedAtCol]) - syncedAt}ms behind`,
);
return false;
}
- if (row?.[hashCol] === hash) {
+ if (!partial && row?.[hashCol] === hash) {
if (syncedAt !== undefined) {
await client.query(
`UPDATE directory_sync SET ${syncedAtCol} = GREATEST(COALESCE(${syncedAtCol}, 0), $2) WHERE org_id = $1`,
@@ -250,13 +259,14 @@ export function createPostgresDirectoryStore(connectionString: string): Director
}
return true;
}
- await write(client);
+ const complete = await write(client);
+ if (partial && complete === false) return false;
await client.query(
`INSERT INTO directory_sync (org_id, ${hashCol}, ${syncedAtCol}, updated_at) VALUES ($1, $2, $3, $4)
ON CONFLICT (org_id) DO UPDATE SET ${hashCol} = EXCLUDED.${hashCol},
${syncedAtCol} = COALESCE(EXCLUDED.${syncedAtCol}, directory_sync.${syncedAtCol}),
updated_at = EXCLUDED.updated_at`,
- [orgId, hash, syncedAt ?? null, Date.now()],
+ [orgId, partial || complete === false ? null : hash, partial ? null : (syncedAt ?? null), Date.now()],
);
return true;
});
@@ -289,21 +299,23 @@ export function createPostgresDirectoryStore(connectionString: string): Director
});
},
- async replaceChannels(channels, channelMembers, syncedAt, channelRosterIds, revocations = []) {
+ async replaceChannels(channels, channelMembers, syncedAt, channelRosterIds, revocations = [], partial = false) {
+ if (partial && (syncedAt === undefined || !Number.isFinite(syncedAt) || channelMembers === undefined))
+ return false;
const byId = new Map<string, DirectoryChannel>();
for (const c of channels) if (c.channelId && c.name) byId.set(c.channelId, c);
- const list = [...byId.values()];
+ let list = [...byId.values()];
const listedIds = new Set(list.map((channel) => channel.channelId));
- const rosterIds =
+ let rosterIds =
channelMembers === undefined
? undefined
: new Set((channelRosterIds ?? [...listedIds]).filter((channelId) => listedIds.has(channelId)));
- const membershipRows =
+ let membershipRows =
channelMembers === undefined
? undefined
: dedupMemberships(channelMembers).filter((member) => rosterIds!.has(member.channelId));
- const revokedRows = dedupMemberships(revocations).filter((member) => listedIds.has(member.channelId));
+ let revokedRows = dedupMemberships(revocations).filter((member) => listedIds.has(member.channelId));
const channelsPart = list.map((c) => `${c.channelId}|${c.name}|${c.isPrivate ? 1 : 0}|${c.isExternal ? 1 : 0}`);
const membersPart =
membershipRows === undefined
@@ -315,60 +327,82 @@ export function createPostgresDirectoryStore(connectionString: string): Director
];
const hash = hashRoster([...channelsPart, ...membersPart]);
- const applied = await swapIfChanged("channels_hash", hash, syncedAt, async (client) => {
- const channelIds = [...listedIds];
- await client.query("DELETE FROM directory_channels WHERE org_id = $1 AND NOT (channel_id = ANY($2::text[]))", [
- orgId,
- channelIds,
- ]);
- await client.query(
- "DELETE FROM directory_channel_members WHERE org_id = $1 AND NOT (channel_id = ANY($2::text[]))",
- [orgId, channelIds],
- );
- if (list.length) {
- await client.query(
- `INSERT INTO directory_channels (org_id, channel_id, name, name_lc, is_private, is_external, roster_known)
- SELECT $1, * FROM unnest($2::text[], $3::text[], $4::text[], $5::boolean[], $6::boolean[], $7::boolean[])
+ const applied = await swapIfChanged(
+ "channels_hash",
+ hash,
+ syncedAt,
+ async (client) => {
+ const protectedRows = await client.query(
+ "SELECT channel_id FROM directory_channels WHERE org_id = $1 AND (observed_at > $2 OR ($3 AND observed_at = $2))",
+ [orgId, syncedAt ?? null, partial],
+ );
+ const protectedIds = new Set(protectedRows.rows.map((row) => row.channel_id as string));
+ if (partial && list.some((channel) => protectedIds.has(channel.channelId))) return false;
+ list = list.filter((channel) => !protectedIds.has(channel.channelId));
+ rosterIds = rosterIds && new Set([...rosterIds].filter((id) => !protectedIds.has(id)));
+ membershipRows = membershipRows?.filter((member) => !protectedIds.has(member.channelId));
+ revokedRows = revokedRows.filter((member) => !protectedIds.has(member.channelId));
+ const retainedIds = [...listedIds, ...protectedIds];
+ const channelIds = list.map((channel) => channel.channelId);
+ if (!partial) {
+ await client.query(
+ "DELETE FROM directory_channels WHERE org_id = $1 AND NOT (channel_id = ANY($2::text[]))",
+ [orgId, retainedIds],
+ );
+ await client.query(
+ "DELETE FROM directory_channel_members WHERE org_id = $1 AND NOT (channel_id = ANY($2::text[]))",
+ [orgId, retainedIds],
+ );
+ }
+ if (list.length) {
+ await client.query(
+ `INSERT INTO directory_channels (org_id, channel_id, name, name_lc, is_private, is_external, roster_known, observed_at)
+ SELECT $1, rows.*, $8::bigint FROM unnest($2::text[], $3::text[], $4::text[], $5::boolean[], $6::boolean[], $7::boolean[]) AS rows
ON CONFLICT (org_id, channel_id) DO UPDATE SET
name = EXCLUDED.name,
name_lc = EXCLUDED.name_lc,
is_private = EXCLUDED.is_private,
is_external = EXCLUDED.is_external,
- roster_known = directory_channels.roster_known OR EXCLUDED.roster_known`,
- [
- orgId,
- channelIds,
- list.map((c) => c.name),
- list.map((c) => normDirectoryQuery(c.name)),
- list.map((c) => !!c.isPrivate),
- list.map((c) => !!c.isExternal),
- list.map((c) => rosterIds?.has(c.channelId) ?? false),
- ],
- );
- }
- if (membershipRows !== undefined) {
- await client.query(
- "DELETE FROM directory_channel_members WHERE org_id = $1 AND channel_id = ANY($2::text[])",
- [orgId, [...rosterIds!]],
- );
- if (membershipRows.length) {
+ roster_known = directory_channels.roster_known OR EXCLUDED.roster_known,
+ observed_at = EXCLUDED.observed_at`,
+ [
+ orgId,
+ channelIds,
+ list.map((c) => c.name),
+ list.map((c) => normDirectoryQuery(c.name)),
+ list.map((c) => !!c.isPrivate),
+ list.map((c) => !!c.isExternal),
+ list.map((c) => rosterIds?.has(c.channelId) ?? false),
+ partial ? syncedAt : null,
+ ],
+ );
+ }
+ if (membershipRows !== undefined) {
await client.query(
- `INSERT INTO directory_channel_members (org_id, channel_id, principal_id)
- SELECT $1, * FROM unnest($2::text[], $3::text[])`,
- [orgId, membershipRows.map((m) => m.channelId), membershipRows.map((m) => m.principalId)],
+ "DELETE FROM directory_channel_members WHERE org_id = $1 AND channel_id = ANY($2::text[])",
+ [orgId, [...rosterIds!]],
);
+ if (membershipRows.length) {
+ await client.query(
+ `INSERT INTO directory_channel_members (org_id, channel_id, principal_id)
+ SELECT $1, * FROM unnest($2::text[], $3::text[])`,
+ [orgId, membershipRows.map((m) => m.channelId), membershipRows.map((m) => m.principalId)],
+ );
+ }
}
- }
- if (revokedRows.length) {
- await client.query(
- `DELETE FROM directory_channel_members member
+ if (revokedRows.length) {
+ await client.query(
+ `DELETE FROM directory_channel_members member
USING unnest($2::text[], $3::text[]) AS revoked(channel_id, principal_id)
WHERE member.org_id = $1 AND member.channel_id = revoked.channel_id
AND member.principal_id = revoked.principal_id`,
- [orgId, revokedRows.map((m) => m.channelId), revokedRows.map((m) => m.principalId)],
- );
- }
- });
+ [orgId, revokedRows.map((m) => m.channelId), revokedRows.map((m) => m.principalId)],
+ );
+ }
+ return partial || protectedIds.size === 0;
+ },
+ partial,
+ );
if (applied && membershipRows !== undefined) {
await q("UPDATE directory_sync SET channel_members_synced = TRUE WHERE org_id = $1", [orgId]);
}
diff --git a/src/slack/directory.ts b/src/slack/directory.ts
index fa703a35b..6b70d78b8 100644
--- a/src/slack/directory.ts
+++ b/src/slack/directory.ts
@@ -88,6 +88,7 @@ export interface Directory {
actor: ActorAssertion,
actorSlackId: string | undefined,
info: ChannelMeta | undefined,
+ observedAt?: number,
): Promise<{
audience: ActorAssertion[];
publishMembers?: ActorAssertion[];
@@ -293,18 +294,11 @@ export function createDirectory(deps: {
privateChannels: ChannelRef[],
invalidations: ChannelInvalidations,
): Promise<{
- channels: ChannelRow[];
channelMembers: ChannelMembershipRow[];
channelRosterIds: string[];
channelRevocations: ChannelMembershipRow[];
}> {
const refs = [...publicChannels, ...privateChannels];
- const channels = refs.map((channel) => ({
- channelId: channel.id,
- name: channel.name,
- ...(channel.info.is_private ? { isPrivate: true } : {}),
- ...(isExternallyShared(channel.info) ? { isExternal: true } : {}),
- }));
const channelMembers: ChannelMembershipRow[] = [];
const channelRosterIds: string[] = [];
const channelRevocations = [...invalidations].flatMap(([channelId, principalIds]) =>
@@ -326,7 +320,7 @@ export function createDirectory(deps: {
if (!revoked?.has(principalId)) channelMembers.push({ channelId: channel.id, principalId });
}
}
- return { channels, channelMembers, channelRosterIds, channelRevocations };
+ return { channelMembers, channelRosterIds, channelRevocations };
}
async function listBotGroupDms(client: any): Promise<string[]> {
@@ -361,19 +355,7 @@ export function createDirectory(deps: {
return { groupMembers, groupRosterIds: [...rosters.keys()] };
}
- let privateChannelsCache:
- | {
- channels: ChannelRow[];
- channelMembers: ChannelMembershipRow[];
- channelRosterIds: string[];
- channelRevocations: ChannelMembershipRow[];
- groupMembers?: GroupMembershipRow[];
- groupIds?: string[];
- groupRosterIds?: string[];
- fetchedAt: number;
- groupsFetchedAt?: number;
- }
- | undefined;
+ let lastFullRosterFetchAt: number | undefined;
const seenGroupIds = new Set<string>();
async function fetchChannels(
@@ -390,6 +372,7 @@ export function createDirectory(deps: {
groupRosterIds?: string[];
fetchedAt: number;
groupsFetchedAt?: number;
+ fullRosterFetchedAt?: number;
} | null> {
const fetchedAt = Date.now();
let listed: { publicChannels: ChannelRef[]; privateChannels: ChannelRef[] };
@@ -399,95 +382,40 @@ export function createDirectory(deps: {
console.error("[slack-plugin] channel list failed:", (err as Error).message);
return null;
}
- const listedChannels = [...listed.publicChannels, ...listed.privateChannels];
- if (targetChannelIds?.size) {
- const computed = await computeChannelMembership(
- client,
- listed.publicChannels.filter((channel) => targetChannelIds.has(channel.id)),
- listed.privateChannels.filter((channel) => targetChannelIds.has(channel.id)),
- invalidations,
- );
- const channels = listedChannels.map((channel) => ({
- channelId: channel.id,
- name: channel.name,
- ...(channel.info.is_private ? { isPrivate: true } : {}),
- ...(isExternallyShared(channel.info) ? { isExternal: true } : {}),
- }));
- if (privateChannelsCache) {
- const refreshed = new Set(computed.channelRosterIds);
- privateChannelsCache.channels = channels;
- privateChannelsCache.channelMembers = [
- ...privateChannelsCache.channelMembers.filter(
- (member) =>
- !refreshed.has(member.channelId) && !invalidations.get(member.channelId)?.has(member.principalId),
- ),
- ...computed.channelMembers,
- ];
- privateChannelsCache.channelRosterIds = [
- ...new Set([...privateChannelsCache.channelRosterIds, ...computed.channelRosterIds]),
- ];
- }
- return { ...computed, channels, fetchedAt };
- }
- const fresh = privateChannelsCache && Date.now() - privateChannelsCache.fetchedAt < CHANNEL_MEMBERS_TTL_MS;
- let includeGroups = true;
- if (!fresh) {
- const computed = await computeChannelMembership(
- client,
- listed.publicChannels,
- listed.privateChannels,
- invalidations,
- );
- let groupMembers: GroupMembershipRow[] | undefined;
- let groupIds: string[] | undefined;
- let groupRosterIds: string[] | undefined;
- let groupsFetchedAt: number | undefined;
+ const channels = [...listed.publicChannels, ...listed.privateChannels].map((channel) => ({
+ channelId: channel.id,
+ name: channel.name,
+ ...(channel.info.is_private ? { isPrivate: true } : {}),
+ ...(isExternallyShared(channel.info) ? { isExternal: true } : {}),
+ }));
+ const fullRefresh =
+ !targetChannelIds?.size &&
+ (lastFullRosterFetchAt === undefined || fetchedAt - lastFullRosterFetchAt >= CHANNEL_MEMBERS_TTL_MS);
+ const computed = await computeChannelMembership(
+ client,
+ listed.publicChannels.filter((channel) => fullRefresh || targetChannelIds?.has(channel.id)),
+ listed.privateChannels.filter((channel) => fullRefresh || targetChannelIds?.has(channel.id)),
+ invalidations,
+ );
+ let groups:
+ | {
+ groupMembers: GroupMembershipRow[];
+ groupIds: string[];
+ groupRosterIds: string[];
+ groupsFetchedAt: number;
+ }
+ | undefined;
+ if (fullRefresh) {
try {
- groupsFetchedAt = Date.now();
- groupIds = await listBotGroupDms(client);
+ const groupsFetchedAt = Date.now();
+ const groupIds = await listBotGroupDms(client);
for (const id of groupIds) seenGroupIds.add(id);
- const computedGroups = await computeGroupMembership(client, groupIds);
- groupMembers = computedGroups.groupMembers;
- groupRosterIds = computedGroups.groupRosterIds;
+ groups = { ...(await computeGroupMembership(client, groupIds)), groupIds, groupsFetchedAt };
} catch (err) {
console.error("[slack-plugin] group-DM list failed:", (err as Error).message);
- includeGroups = false;
- groupMembers = privateChannelsCache?.groupMembers;
- groupIds = privateChannelsCache?.groupIds;
- groupRosterIds = privateChannelsCache?.groupRosterIds;
- groupsFetchedAt = privateChannelsCache?.groupsFetchedAt;
}
- privateChannelsCache = {
- ...computed,
- groupMembers,
- groupIds,
- groupRosterIds,
- groupsFetchedAt,
- fetchedAt,
- };
}
- const priv = privateChannelsCache ?? {
- channels: [],
- channelMembers: [],
- channelRosterIds: [],
- channelRevocations: [],
- fetchedAt: 0,
- };
- return {
- channels: priv.channels,
- channelMembers: priv.channelMembers,
- channelRosterIds: priv.channelRosterIds,
- channelRevocations: priv.channelRevocations,
- fetchedAt: priv.fetchedAt,
- ...(includeGroups && priv.groupMembers
- ? {
- groupMembers: priv.groupMembers,
- groupIds: priv.groupIds,
- groupRosterIds: priv.groupRosterIds,
- groupsFetchedAt: priv.groupsFetchedAt,
- }
- : {}),
- };
+ return { channels, ...computed, fetchedAt, ...groups, fullRosterFetchedAt: fullRefresh ? fetchedAt : undefined };
}
async function pushDirectory(
@@ -564,6 +492,7 @@ export function createDirectory(deps: {
: {}),
...(ids.ownWorkspaceUrl ? { workspaceUrl: ids.ownWorkspaceUrl } : {}),
});
+ if (applied && fetched?.fullRosterFetchedAt !== undefined) lastFullRosterFetchAt = fetched.fullRosterFetchedAt;
if (!applied) console.warn("[slack-plugin] directory push refused by the store (stale stamp) — will retry");
return fetched !== null && applied;
} catch (err) {
@@ -614,13 +543,12 @@ export function createDirectory(deps: {
fullDirectorySyncRequested = false;
const scheduledTargets = new Set(targetedChannelIds);
const targets = fullSync || !scheduledTargets.size ? undefined : scheduledTargets;
- if (!targets && privateChannelsCache) privateChannelsCache.fetchedAt = 0;
+ if (!targets) lastFullRosterFetchAt = undefined;
const snap = userSnapshot ?? (await getUserSnapshot(directorySyncClient));
const pendingInvalidations = new Map(
[...invalidatedChannelMembers].map(([channelId, principalIds]) => [channelId, new Set(principalIds)]),
);
if (snap && (await pushDirectory(snap, directorySyncClient, pendingInvalidations, targets))) {
- if (privateChannelsCache) privateChannelsCache.channelRevocations = [];
for (const channelId of scheduledTargets) targetedChannelIds.delete(channelId);
for (const [channelId, principalIds] of pendingInvalidations) {
const current = invalidatedChannelMembers.get(channelId);
@@ -697,14 +625,16 @@ export function createDirectory(deps: {
actor: ActorAssertion,
actorSlackId: string | undefined,
info: ChannelMeta | undefined,
+ observedAt?: number,
): Promise<{
audience: ActorAssertion[];
publishMembers?: ActorAssertion[];
slackIdsByPrincipal?: Map<string, string>;
}> {
+ let membership: Awaited<ReturnType<typeof resolveChannelMembership>>;
try {
const memberIds = await fetchChannelMemberIds(client, channel);
- return await resolveChannelMembership({
+ membership = await resolveChannelMembership({
memberIds,
actor,
actorSlackId,
@@ -714,6 +644,24 @@ export function createDirectory(deps: {
} catch {
return { audience: [actor, externalMarker()] };
}
+ if (CORE_SINGLETON && !deps.externalAccess && !actor.isBot && !info?.is_mpim) {
+ if (!info?.name || typeof info.is_private !== "boolean" || info.is_member === false || observedAt === undefined)
+ return { audience: [actor, externalMarker()] };
+ if (membership.publishMembers) {
+ const applied = await core.pushDirectory({
+ channels: [{ channelId: channel, name: info.name, isPrivate: info.is_private }],
+ channelMembers: membership.publishMembers.map((member) => ({
+ channelId: channel,
+ principalId: member.externalId,
+ })),
+ channelRosterIds: [channel],
+ channelsSyncedAt: observedAt,
+ partialChannels: true,
+ });
+ if (!applied) throw new Error("Slack channel membership was not persisted before admission");
+ }
+ }
+ return membership;
}
async function resolveAutoIdentityMode(client: any): Promise<SlackIdentityMode> {
diff --git a/src/slack/turn-handler.ts b/src/slack/turn-handler.ts
index 41b2d103d..82bd37ec9 100644
--- a/src/slack/turn-handler.ts
+++ b/src/slack/turn-handler.ts
@@ -243,6 +243,7 @@ export function createTurnHandler(deps: {
let isMpimChannel: boolean | undefined;
let publishMembers: ActorAssertion[] | undefined;
let channelInfo: ChannelMeta | undefined;
+ let channelObservedAt: number | undefined;
let slackIdsByPrincipal: Map<string, string> | undefined;
let conversationKind: SlackConversationKind = inc.kind;
let allowedTs: Set<string> = new Set();
@@ -286,6 +287,7 @@ export function createTurnHandler(deps: {
deps.ensureHeader?.(client, inc.channel, `personal:${actor.externalId}`, "dm");
} else {
channelRef = inc.channel;
+ channelObservedAt = Date.now();
const info = inc.prefetched ? inc.prefetched.info : await getChannelInfo(client, inc.channel);
channelInfo = info;
isPrivate = info?.is_private;
@@ -311,7 +313,7 @@ export function createTurnHandler(deps: {
publishMembers: inc.prefetched.publishMembers,
slackIdsByPrincipal: inc.prefetched.slackIdsByPrincipal,
}
- : await channelMembership(client, inc.channel, actor, inc.userId, channelInfo);
+ : await channelMembership(client, inc.channel, actor, inc.userId, channelInfo, channelObservedAt);
audience = membership.audience;
publishMembers = membership.publishMembers;
slackIdsByPrincipal = membership.slackIdsByPrincipal;
@@ -937,8 +939,9 @@ export function createTurnHandler(deps: {
if (deps.allowActor && !deps.allowActor(reactor)) return;
let prefetched: Incoming["prefetched"];
if (!isDM) {
+ const observedAt = Date.now();
const info = await getChannelInfo(client, channel);
- const membership = await channelMembership(client, channel, reactor, reactorId, info);
+ const membership = await channelMembership(client, channel, reactor, reactorId, info, observedAt);
if (membership.audience.some((a) => a.isExternalGuest) && !(await externalParticipantsEnabled())) return;
prefetched = {
actor: reactor,
diff --git a/test/directory-resolve.test.ts b/test/directory-resolve.test.ts
index 0c52b6f3b..77ca34771 100644
--- a/test/directory-resolve.test.ts
+++ b/test/directory-resolve.test.ts
@@ -126,6 +126,36 @@ describe("GET /v1/directory/resolve (agent looks up a teammate's mention id)", a
const res = await fetch(`${base}/v1/directory/resolve?q=carol`);
assert.equal(res.status, 401);
});
+ it("source pushes persist one observed room and reject malformed or stale partial writes", async () => {
+ const post = (body: object) => {
+ const raw = JSON.stringify(body);
+ return fetch(`${base}/v1/directory`, {
+ method: "POST",
+ headers: {
+ ...signedRequestHeaders(SECRET, "POST", "/v1/directory", raw),
+ "content-type": "application/json",
+ },
+ body: raw,
+ });
+ };
+ await built.app.upsertChannels([{ channelId: "C_OTHER", name: "other" }], [], 10);
+ const body = {
+ channels: [{ channelId: "C_LIVE", name: "live", isPrivate: true }],
+ channelMembers: [{ channelId: "C_LIVE", principalId: "U1" }],
+ channelsSyncedAt: 30,
+ partialChannels: true,
+ };
+ assert.equal((await post(body)).status, 200);
+ assert.equal(await built.directory.channelPrivacy("C_OTHER"), false);
+ assert.equal(await built.directory.channelMember("C_LIVE", "U1"), true);
+ assert.equal((await post({ ...body, channelsSyncedAt: 20 })).status, 409);
+ for (const invalid of [
+ { ...body, channelsSyncedAt: undefined },
+ { ...body, channelMembers: undefined },
+ { ...body, partialChannels: "true" },
+ ])
+ assert.equal((await post(invalid)).status, 400);
+ });
});
describe("a deployment without the Slack surface (the directory store is never populated)", async () => {
diff --git a/test/directory-store.test.ts b/test/directory-store.test.ts
index 8e8c92b34..1d493a61b 100644
--- a/test/directory-store.test.ts
+++ b/test/directory-store.test.ts
@@ -383,3 +383,8 @@ describe("private-channel membership (authorizes private-channel sends, §10)",
assert.deepEqual(await d.listChannelsFor("U-member"), []);
});
});
+
+it("live channel observations survive older snapshots but yield to later removal", async () => {
+ const { directoryObservationCases } = await import("./support/directory-observation-cases.ts");
+ await directoryObservationCases(createDirectoryStore());
+});
diff --git a/test/postgres-directory-store.test.ts b/test/postgres-directory-store.test.ts
index ff5892f3f..0fd5688e0 100644
--- a/test/postgres-directory-store.test.ts
+++ b/test/postgres-directory-store.test.ts
@@ -470,3 +470,14 @@ test(
assert.equal(await store.groupMember("G-idem", "U-alice"), true);
},
);
+
+test("pg live channel observations survive older snapshots but yield to later removal", { skip }, async () => {
+ const { directoryObservationCases } = await import("./support/directory-observation-cases.ts");
+ const store = createPostgresDirectoryStore(URL!);
+ await store.replaceChannels([], [], Date.now());
+ const pg = (await import("pg")).default;
+ const pool = new pg.Pool({ connectionString: URL! });
+ await pool.query("UPDATE directory_sync SET channels_synced_at = NULL, channels_hash = NULL");
+ await pool.end();
+ await directoryObservationCases(store);
+});
diff --git a/test/slack-directory-freshness.test.ts b/test/slack-directory-freshness.test.ts
new file mode 100644
index 000000000..c79058c82
--- /dev/null
+++ b/test/slack-directory-freshness.test.ts
@@ -0,0 +1,209 @@
+import assert from "node:assert/strict";
+import { setImmediate } from "node:timers/promises";
+import { test, type TestContext } from "node:test";
+import { createDirectoryStore } from "../src/directory/directory-store.ts";
+import { createDirectory } from "../src/slack/directory.ts";
+import type { SlackCoreClient } from "../src/api/slack-core-client.ts";
+
+const channel = (id: string, isPrivate = false) => ({
+ id,
+ name: id.toLowerCase(),
+ is_member: true,
+ is_private: isPrivate,
+});
+
+function fixture(t: TestContext) {
+ let now = 1_000_000;
+ t.mock.method(Date, "now", () => now);
+ const advance = (ms = 301_000) => {
+ now += ms;
+ };
+ const store = createDirectoryStore();
+ function core(channels = [channel("C_ONE")]) {
+ const state = {
+ channels,
+ memberIds: ["U_ONE"],
+ failMembers: false,
+ failList: false,
+ memberCalls: 0,
+ rejectPush: false,
+ };
+ const pushes: Parameters<SlackCoreClient["pushDirectory"]>[0][] = [];
+ const user = { id: "U_ONE", team_id: "T_TEST" };
+ const client = {
+ async *paginate(method: string, args: { types?: string }) {
+ if (method === "users.list") yield { members: [user] };
+ else if (method === "conversations.list") {
+ if (state.failList) throw Error("list unavailable");
+ yield { channels: args.types === "mpim" ? [] : state.channels };
+ } else if (method === "conversations.members") {
+ state.memberCalls++;
+ if (state.failMembers) throw Error("members unavailable");
+ yield { members: state.memberIds };
+ }
+ },
+ users: { info: async () => ({ user }) },
+ };
+ const directory = createDirectory({
+ core: {
+ holdDirectorySync: async (fn: (lost: Promise<void>) => Promise<boolean>) => fn(new Promise(() => {})),
+ pushDirectory: async (body: Parameters<SlackCoreClient["pushDirectory"]>[0]) => {
+ if (state.rejectPush) {
+ pushes.push(body);
+ return false;
+ }
+ const members = await store.replace(body.members ?? [], body.membersSyncedAt);
+ const channels = body.channels
+ ? await store.replaceChannels(
+ body.channels,
+ body.channelMembers,
+ body.channelsSyncedAt,
+ body.channelRosterIds,
+ body.channelRevocations,
+ )
+ : true;
+ const groups = body.groupMembers
+ ? await store.replaceGroups(body.groupMembers, body.groupsSyncedAt, body.groupIds, body.groupRosterIds)
+ : true;
+ pushes.push(body);
+ return members && channels && groups;
+ },
+ } as SlackCoreClient,
+ ids: {
+ ownTeamId: "T_TEST",
+ botUserId: "U_BOT",
+ ownBotId: "B_BOT",
+ botHandle: "bot",
+ ownWorkspaceUrl: "",
+ identityMode: "slack-id",
+ },
+ });
+ async function refresh() {
+ const count = pushes.length;
+ await directory.getUserSnapshot(client);
+ for (let i = 0; i < 100 && pushes.length === count; i++) await setImmediate();
+ assert.equal(pushes.length, count + 1);
+ await setImmediate();
+ return pushes.at(-1)!;
+ }
+ return { state, directory, client, refresh, pushes };
+ }
+ return { store, core, advance };
+}
+
+test("a fresh listing discovers channels without replaying cached rosters", async (t) => {
+ const { core, advance, store } = fixture(t);
+ const a = core();
+ await a.refresh();
+ advance();
+ a.state.channels.push(channel("C_NEW"));
+ const pushed = await a.refresh();
+ assert.equal(await store.channelPrivacy("C_NEW"), false);
+ assert.equal(pushed.channelsSyncedAt, Date.now());
+ assert.deepEqual(pushed.channelRosterIds, []);
+ assert.deepEqual(pushed.channelMembers, []);
+ assert.equal(pushed.groupMembers, undefined);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), true);
+ assert.equal(a.state.memberCalls, 1);
+});
+
+test("fresh listings update names and privacy while roster refresh is throttled", async (t) => {
+ const { core, advance, store } = fixture(t);
+ const a = core();
+ await a.refresh();
+ advance();
+ a.state.channels = [{ ...channel("C_ONE", true), name: "renamed" }];
+ const pushed = await a.refresh();
+ assert.equal(await store.channelPrivacy("C_ONE"), true);
+ assert.equal(pushed.channels?.[0]?.name, "renamed");
+ advance();
+ a.state.channels = [channel("C_ONE")];
+ await a.refresh();
+ assert.equal(await store.channelPrivacy("C_ONE"), false);
+});
+
+test("two cores recover a successful omission through healthy periodic listings", async (t) => {
+ const { core, advance, store } = fixture(t);
+ const a = core();
+ await a.refresh();
+ advance(1);
+ const b = core([channel("C_OTHER")]);
+ await b.refresh();
+ assert.equal(await store.channelPrivacy("C_ONE"), undefined);
+ b.state.channels = [channel("C_ONE")];
+ for (const node of [a, b, a, b]) {
+ advance();
+ await node.refresh();
+ assert.equal(await store.channelPrivacy("C_ONE"), false);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), false);
+ }
+});
+
+test("periodic and unrelated targeted refreshes cannot resurrect another core's revocation", async (t) => {
+ const { core, advance, store } = fixture(t);
+ const a = core([channel("C_ONE", true), channel("C_OTHER")]);
+ await a.refresh();
+ advance(1);
+ const b = core([channel("C_ONE", true), channel("C_OTHER")]);
+ await b.refresh();
+ advance(1);
+ await b.directory.forceDirectorySync(b.client, "C_ONE", "U_ONE");
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), false);
+ advance(1);
+ await a.directory.forceDirectorySync(a.client, "C_OTHER");
+ advance();
+ await a.refresh();
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), false);
+ assert.equal(await store.channelMember("C_OTHER", "U_ONE"), true);
+});
+
+test("targeted refresh does not postpone the next full roster refresh", async (t) => {
+ const { core, advance, store } = fixture(t);
+ const a = core([channel("C_ONE"), channel("C_OTHER")]);
+ await a.refresh();
+ advance(1_500_000);
+ await a.refresh();
+ advance(299_000);
+ await a.directory.forceDirectorySync(a.client, "C_OTHER");
+ a.state.memberIds = [];
+ advance(2_000);
+ const pushed = await a.refresh();
+ assert.deepEqual(pushed.channelRosterIds?.sort(), ["C_ONE", "C_OTHER"]);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), false);
+});
+
+test("API errors preserve stored rosters and metadata but successful omissions remove them", async (t) => {
+ const { core, advance, store } = fixture(t);
+ const a = core();
+ await a.refresh();
+ advance();
+ a.state.failMembers = true;
+ a.state.channels = [channel("C_ONE", true)];
+ await a.directory.forceDirectorySync(a.client, "C_ONE");
+ assert.equal(await store.channelPrivacy("C_ONE"), true);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), true);
+ advance();
+ a.state.failList = true;
+ await a.refresh();
+ assert.equal(await store.channelPrivacy("C_ONE"), true);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), true);
+ advance();
+ a.state.failList = false;
+ a.state.channels = [];
+ await a.refresh();
+ assert.equal(await store.channelPrivacy("C_ONE"), undefined);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), false);
+});
+
+test("a rejected full crawl is retried instead of throttling unpublished rosters", async (t) => {
+ const { core, advance, store } = fixture(t);
+ const a = core();
+ a.state.rejectPush = true;
+ await a.refresh();
+ assert.equal(await store.channelPrivacy("C_ONE"), undefined);
+ a.state.rejectPush = false;
+ advance();
+ await a.refresh();
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), true);
+ assert.equal(a.state.memberCalls, 2);
+});
diff --git a/test/slack-verified-channel.test.ts b/test/slack-verified-channel.test.ts
new file mode 100644
index 000000000..f58a4e7d0
--- /dev/null
+++ b/test/slack-verified-channel.test.ts
@@ -0,0 +1,265 @@
+import { createHmac } from "node:crypto";
+import type { AddressInfo } from "node:net";
+import type { SocketModeClient } from "@slack/socket-mode";
+import type { App as BoltApp, ReceiverEvent } from "@slack/bolt";
+import { createMemoryMap } from "../src/persistence/durable-map.ts";
+import { createEnvelopeStaging, type StagedEnvelope } from "../src/slack/envelope-staging.ts";
+import { createDeferredAckReceiver, type AckGate } from "../src/slack/deferred-ack.ts";
+import { createHttpEventsReceiver } from "../src/slack/http-events.ts";
+import { createDeduper, dedupedRun } from "../src/slack/message-gating.ts";
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { createDirectoryStore } from "../src/directory/directory-store.ts";
+import { createDirectory } from "../src/slack/directory.ts";
+import type { SlackCoreClient } from "../src/api/slack-core-client.ts";
+
+function fixture(options: { secondary?: boolean; failWrite?: boolean; throwWrite?: boolean } = {}) {
+ const store = createDirectoryStore();
+ const state = { memberIds: ["U_ONE"], failMembers: false, failUser: false, writes: 0 };
+ const client = {
+ async *paginate(method: string) {
+ if (method === "conversations.members") {
+ if (state.failMembers) throw Error("unavailable");
+ yield { members: state.memberIds };
+ } else if (method === "users.list") yield { members: [] };
+ else yield { channels: [] };
+ },
+ users: {
+ info: async ({ user }: { user: string }) => {
+ if (state.failUser) throw Error("unavailable");
+ return { user: { id: user, team_id: "T_TEST" } };
+ },
+ },
+ };
+ const directory = createDirectory({
+ core: {
+ holdDirectorySync: async () => null,
+ pushDirectory: async (body: Parameters<SlackCoreClient["pushDirectory"]>[0]) => {
+ state.writes++;
+ if (options.throwWrite) throw new Error("directory transport unavailable");
+ if (options.failWrite) return false;
+ return store.replaceChannels(
+ body.channels!,
+ body.channelMembers,
+ body.channelsSyncedAt,
+ body.channelRosterIds,
+ body.channelRevocations,
+ body.partialChannels,
+ );
+ },
+ } as unknown as SlackCoreClient,
+ coreSingleton: !options.secondary,
+ ids: {
+ ownTeamId: "T_TEST",
+ botUserId: "U_BOT",
+ ownBotId: "B_BOT",
+ botHandle: "bot",
+ ownWorkspaceUrl: "",
+ identityMode: "slack-id",
+ },
+ });
+ const observe = (
+ info: import("../src/slack/identity.ts").ChannelMeta | undefined = { name: "room", is_private: true },
+ ) => directory.channelMembership(client, "C_ONE", { externalId: "U_ONE" }, "U_ONE", info, Date.now());
+ return { store, state, observe };
+}
+
+test("verified channel membership persists before admission without deleting another room", async () => {
+ const { store, observe } = fixture();
+ await store.replaceChannels([{ channelId: "C_OTHER", name: "other" }], [], 1);
+ const result = await observe();
+ assert.ok(result.publishMembers);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), true);
+ assert.equal(await store.channelPrivacy("C_ONE"), true);
+ assert.equal(await store.channelPrivacy("C_OTHER"), false);
+});
+
+test("live observation repairs stale privacy", async () => {
+ const { store, observe } = fixture();
+ await store.replaceChannels([{ channelId: "C_ONE", name: "old", isPrivate: true }], [], 1);
+ await observe({ name: "room", is_private: false });
+ assert.equal(await store.channelPrivacy("C_ONE"), false);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), true);
+});
+
+test("secondary accounts never publish live channels", async () => {
+ const { state, observe } = fixture({ secondary: true });
+ assert.ok((await observe()).publishMembers);
+ assert.equal(state.writes, 0);
+});
+
+test("failed persistence propagates to envelope recovery instead of declaring an external audience", async () => {
+ for (const options of [{ failWrite: true }, { throwWrite: true }]) {
+ const { observe } = fixture(options);
+ await assert.rejects(observe(), /not persisted before admission|directory transport unavailable/);
+ }
+});
+
+test("removed speakers and failed member classification do not publish", async () => {
+ for (const failure of ["removed", "members", "user"]) {
+ const { state, observe } = fixture();
+ if (failure === "removed") state.memberIds = [];
+ if (failure === "members") state.failMembers = true;
+ if (failure === "user") state.failUser = true;
+ assert.equal((await observe()).publishMembers, undefined);
+ assert.equal(state.writes, 0);
+ }
+});
+
+test("unknown, external and group metadata never publish an ordinary channel", async () => {
+ for (const info of [
+ {},
+ { name: "room" },
+ { name: "room", is_private: true, is_member: false },
+ { name: "room", is_private: true, is_ext_shared: true },
+ { name: "room", is_private: true, is_mpim: true },
+ ]) {
+ const { state, observe } = fixture();
+ await observe(info);
+ assert.equal(state.writes, 0);
+ }
+});
+
+test("newer removal rejects an older in-flight live observation", async () => {
+ const { store, observe } = fixture();
+ await store.replaceChannels([], [], Date.now() + 60_000);
+ await assert.rejects(observe(), /not persisted before admission/);
+ assert.equal(await store.channelPrivacy("C_ONE"), undefined);
+});
+
+test("equal observation stamps trigger recovery without changing the internal roster", async (t) => {
+ t.mock.method(Date, "now", () => 100_000);
+ const { store, observe } = fixture();
+ assert.ok((await observe()).publishMembers);
+ await assert.rejects(observe(), /not persisted before admission/);
+ assert.equal(await store.channelMember("C_ONE", "U_ONE"), true);
+});
+
+for (const mode of ["socket", "http"] as const)
+ for (const afterCap of [false, true])
+ test(`${mode} roster failure ${afterCap ? "after" : "before"} ack cap recovers without false external admission`, async (t) => {
+ const options = { failWrite: true };
+ const { observe } = fixture(options);
+ const deduper = createDeduper();
+ const map = createMemoryMap<StagedEnvelope>();
+ const staging = createEnvelopeStaging(map, { account: "test", staleAfterMs: 0 });
+ let release!: () => void;
+ const blocked = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ let complete!: () => void;
+ const completed = new Promise<void>((resolve) => {
+ complete = resolve;
+ });
+ let eventDone: (() => void) | undefined;
+ let attempts = 0;
+ let admitted = 0;
+ let externalRefusals = 0;
+ let acks = 0;
+ const retries: Array<number | undefined> = [];
+ const processEvent = async (event: ReceiverEvent) => {
+ await event.ack();
+ retries.push(event.retryNum);
+ const gate = event.customProperties!.ackGate as AckGate;
+ await dedupedRun(
+ deduper,
+ "message-key",
+ async () => {
+ if (++attempts === 1 && afterCap) await blocked;
+ const membership = await observe();
+ if (!membership.publishMembers) {
+ externalRefusals++;
+ gate.persisted();
+ return;
+ }
+ admitted++;
+ gate.persisted();
+ },
+ (error) => gate.failed(String(error)),
+ );
+ eventDone?.();
+ eventDone = undefined;
+ complete();
+ };
+ const receiver =
+ mode === "socket"
+ ? createDeferredAckReceiver({ appToken: "xapp-test", capMs: afterCap ? 1 : 5000, staging })
+ : createHttpEventsReceiver({ signingSecret: "test-secret", port: 0, capMs: afterCap ? 1 : 5000, staging });
+ receiver.init?.({ processEvent } as unknown as BoltApp);
+ const body = {
+ type: "event_callback",
+ event_id: "EvTest",
+ event: { type: "app_mention", channel: "C_ONE", ts: "1.1" },
+ };
+ let acked!: () => void;
+ const acknowledgement = new Promise<void>((resolve) => {
+ acked = resolve;
+ });
+ const deliver = async (retryNum = 0): Promise<number | undefined> => {
+ if ("client" in receiver) {
+ const processed = new Promise<void>((resolve) => {
+ eventDone = resolve;
+ });
+ (receiver.client as SocketModeClient).emit("slack_event", {
+ body,
+ retry_num: retryNum,
+ ack: async () => {
+ acks++;
+ acked();
+ },
+ });
+ if (afterCap && retryNum === 0) await acknowledgement;
+ else await processed;
+ return undefined;
+ }
+ const address = (receiver as ReturnType<typeof createHttpEventsReceiver>).server.address() as AddressInfo;
+ const timestamp = String(Math.floor(Date.now() / 1000));
+ const raw = JSON.stringify(body);
+ const signature = `v0=${createHmac("sha256", "test-secret").update(`v0:${timestamp}:${raw}`).digest("hex")}`;
+ const response = await fetch(`http://127.0.0.1:${address.port}/slack/events`, {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ "x-slack-signature": signature,
+ "x-slack-request-timestamp": timestamp,
+ "x-slack-retry-num": String(retryNum),
+ },
+ body: raw,
+ });
+ if (response.status === 200) acks++;
+ else assert.deepEqual(await response.json(), { error: "not_persisted" });
+ return response.status;
+ };
+ if ("server" in receiver) {
+ await receiver.start(0 as never);
+ t.after(() => receiver.stop(0 as never));
+ }
+ const status = await deliver();
+ if (mode === "http") assert.equal(status, afterCap ? 200 : 503);
+ assert.equal(acks, afterCap ? 1 : 0);
+ release();
+ await completed;
+ assert.equal(admitted, 0);
+ assert.equal(externalRefusals, 0);
+ assert.equal(deduper.seen("message-key"), false);
+ deduper.forget("message-key");
+ options.failWrite = false;
+ if (afterCap) {
+ assert.equal((await map.entries()).length, 1);
+ assert.equal(
+ await staging.sweep((replayed, ackGate) =>
+ processEvent({ body: replayed, ack: async () => {}, customProperties: { ackGate } }),
+ ),
+ 1,
+ );
+ } else {
+ assert.equal((await map.entries()).length, 0);
+ const retryStatus = await deliver(1);
+ if (mode === "http") assert.equal(retryStatus, 200);
+ assert.equal(retries.at(-1), 1);
+ }
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ assert.equal(admitted, 1);
+ assert.equal(attempts, 2);
+ assert.equal((await map.entries()).length, 0);
+ });
diff --git a/test/support/directory-observation-cases.ts b/test/support/directory-observation-cases.ts
new file mode 100644
index 000000000..d00b04769
--- /dev/null
+++ b/test/support/directory-observation-cases.ts
@@ -0,0 +1,48 @@
+import assert from "node:assert/strict";
+import type { DirectoryStore } from "../../src/directory/directory-store.ts";
+
+export async function directoryObservationCases(store: DirectoryStore): Promise<void> {
+ const room = { channelId: "C_OBS", name: "room", isPrivate: true };
+ const other = { channelId: "C_OTHER", name: "other", isPrivate: true };
+ const member = { channelId: room.channelId, principalId: "U_ONE" };
+ const patch = (at: number) => store.replaceChannels([room], [member], at, [room.channelId], [], true);
+ await store.replaceChannels([other], [], 100);
+ assert.equal(await patch(300), true);
+ assert.equal(await store.channelMembership(room.channelId, member.principalId), true);
+ assert.equal(await store.channelPrivacy(other.channelId), true);
+ assert.equal(await store.replaceChannels([other], [], 200), true);
+ assert.equal(await store.channelMembership(room.channelId, member.principalId), true);
+ assert.equal(await store.replaceChannels([other], [], 400), true);
+ assert.equal(await store.channelPrivacy(room.channelId), undefined);
+ assert.equal(await patch(350), false);
+ assert.equal(await patch(500), true);
+ assert.equal(await patch(450), false);
+ assert.equal(await store.replaceChannels([room, other], [], 475, [], [member]), true);
+ assert.equal(await store.channelMembership(room.channelId, member.principalId), true);
+ assert.equal(await store.replaceChannels([room, other], [], 600, [], [member]), true);
+ assert.equal(await store.channelMembership(room.channelId, member.principalId), false);
+ assert.equal(await patch(550), false);
+ assert.equal(await patch(600), false);
+ assert.equal(await store.channelMembership(room.channelId, member.principalId), false);
+ assert.equal(await patch(650), true);
+ assert.equal(await patch(650), false);
+ assert.equal(await store.replaceChannels([room, other], [], 650, [], [member]), true);
+ assert.equal(await store.channelMember(room.channelId, member.principalId), false);
+ assert.equal(await patch(650), false);
+ assert.equal(await patch(675), true);
+ assert.equal(await store.replaceChannels([other], [], 660), true);
+ assert.equal(await store.channelMember(room.channelId, member.principalId), true);
+ assert.equal(await store.replaceChannels([other], [], 700), true);
+ assert.equal(await store.channelPrivacy(room.channelId), undefined);
+ assert.equal(await patch(800), true);
+ assert.equal(await store.replaceChannels([room, other], []), true);
+ assert.equal(await store.channelMembership(room.channelId, member.principalId), false);
+ assert.equal(await patch(900), true);
+ assert.equal(await store.replaceChannels([], []), true);
+ assert.equal(await store.channelPrivacy(room.channelId), undefined);
+ await store.replaceChannels([room, other], [member], 1000);
+ await store.replaceChannels([other], [], 1100, [room.channelId], [member], true);
+ assert.equal(await store.channelMembership(room.channelId, member.principalId), true);
+ await store.replaceChannels([other], [{ ...member, principalId: "U_TWO" }], 1200, [room.channelId], [], true);
+ assert.equal(await store.channelMembership(room.channelId, "U_TWO"), false);
+}