-
Notifications
You must be signed in to change notification settings - Fork 0
Fix Slack integration event context reads #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -376,6 +376,18 @@ function dedupeStrings(values: string[]): string[] { | |
| return Array.from(new Set(values.map((value) => value.trim()).filter(Boolean))).sort() | ||
| } | ||
|
|
||
| function dedupeStringsInOrder(values: string[]): string[] { | ||
| const seen = new Set<string>() | ||
| const deduped: string[] = [] | ||
| for (const value of values) { | ||
| const trimmed = value.trim() | ||
| if (!trimmed || seen.has(trimmed)) continue | ||
| seen.add(trimmed) | ||
| deduped.push(trimmed) | ||
| } | ||
| return deduped | ||
| } | ||
|
|
||
| function sameStringList(left: string[], right: string[]): boolean { | ||
| return left.length === right.length && left.every((value, index) => value === right[index]) | ||
| } | ||
|
|
@@ -400,7 +412,7 @@ function scopeBooleanDefault(scope: Record<string, unknown>, keys: string[], def | |
|
|
||
| function slackListenDms(integration: ConnectedIntegration): boolean { | ||
| if (!isSlackProvider(integration.provider)) return false | ||
| return scopeBooleanDefault(integration.scope, ['listenDms', 'listenDirectMessages', 'directMessages'], true) | ||
| return scopeBooleanDefault(integration.scope, ['listenDms', 'listenDirectMessages', 'directMessages'], false) | ||
| } | ||
|
Comment on lines
412
to
416
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚩 Slack DM listening default change is a breaking behavioral change The (Refers to lines 403-416) Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| function pathSegments(path: string): string[] { | ||
|
|
@@ -1708,6 +1720,32 @@ function slackEventContextPath(path: string): boolean { | |
| return /^\/slack\/(?:channels|dms|users)\/[^/]+\/(?:messages|threads)\/.+\.json$/u.test(path) | ||
| } | ||
|
|
||
| function slackContextReadCandidatePaths(path: string, specs: SubscriptionSpec[]): string[] { | ||
| const normalizedPath = path.startsWith('/') ? path : `/${path}` | ||
| if (!slackEventContextPath(normalizedPath)) return [normalizedPath] | ||
|
|
||
| const match = normalizedPath.match(/^\/slack\/channels\/([^/]+)(\/(?:messages|threads)\/.+\.json)$/u) | ||
| if (!match?.[1] || !match[2]) return [normalizedPath] | ||
|
|
||
| const currentChannel = match[1] | ||
| const channelId = canonicalSlackChannelSegment(currentChannel) | ||
| const tail = match[2] | ||
| const candidates = [normalizedPath] | ||
|
|
||
| for (const spec of specs) { | ||
| for (const mountPath of spec.mountPaths) { | ||
| const mountMatch = mountPath.match(/^\/slack\/channels\/([^/]+)(?:\/|$)/u) | ||
| const mountedChannel = mountMatch?.[1] | ||
| if (!mountedChannel || canonicalSlackChannelSegment(mountedChannel) !== channelId) { | ||
| continue | ||
| } | ||
| candidates.push(`/slack/channels/${mountedChannel}${tail}`) | ||
| } | ||
| } | ||
|
|
||
| return dedupeStringsInOrder(candidates) | ||
| } | ||
|
|
||
| function slackScopeLabel(path: string): string | undefined { | ||
| const segments = pathSegments(path) | ||
| const channelIndex = segments.indexOf('channels') | ||
|
|
@@ -1730,8 +1768,9 @@ function formatSlackIntegrationEventMessage( | |
| const relayfilePath = eventSummaryValue(resource.path) | ||
| if (provider !== 'slack' || !relayfilePath || !slackEventContextPath(relayfilePath)) return null | ||
|
|
||
| const projectPath = projectIntegrationPathForRelayfilePath(relayfilePath) | ||
| const scopeLabel = slackScopeLabel(relayfilePath) | ||
| const contextPath = contextPreview?.path || relayfilePath | ||
| const projectPath = projectIntegrationPathForRelayfilePath(contextPath) | ||
| const scopeLabel = slackScopeLabel(contextPath) | ||
| const messageText = slackPreviewText(contextPreview) | ||
| const author = slackPreviewAuthor(contextPreview) | ||
| const lines = [ | ||
|
|
@@ -2271,7 +2310,11 @@ export class IntegrationEventBridge { | |
| ) | ||
| } | ||
|
|
||
| private async readEventContextPreview(projectId: string, event: ChangeEvent): Promise<EventContextPreview | undefined> { | ||
| private async readEventContextPreview( | ||
| projectId: string, | ||
| event: ChangeEvent, | ||
| matchedSpecs: SubscriptionSpec[] | ||
| ): Promise<EventContextPreview | undefined> { | ||
| if (event.type === 'file.deleted' || event.type === 'relayfile.changed.summary') return undefined | ||
| const path = eventSummaryValue(event.resource.path) | ||
| if (!path) return undefined | ||
|
|
@@ -2281,14 +2324,19 @@ export class IntegrationEventBridge { | |
| const readDelays = slackEventContextPath(path) ? [0, ...EVENT_CONTEXT_READ_RETRY_DELAYS_MS] : [0] | ||
| const handle = await this.getWorkspaceHandle() | ||
| const client = handle.client() | ||
| const candidatePaths = slackEventContextPath(path) | ||
| ? slackContextReadCandidatePaths(path, matchedSpecs) | ||
| : [path] | ||
| if (typeof client.readFile === 'function') { | ||
| for (const [index, delayMs] of readDelays.entries()) { | ||
| if (delayMs > 0) await delay(delayMs) | ||
| try { | ||
| return eventContextPreviewFromFile(await client.readFile(handle.workspaceId, path)) | ||
| } catch (error) { | ||
| readFileError = error | ||
| if (index === readDelays.length - 1) break | ||
| for (const candidatePath of candidatePaths) { | ||
| try { | ||
| return eventContextPreviewFromFile(await client.readFile(handle.workspaceId, candidatePath)) | ||
| } catch (error) { | ||
| readFileError = error | ||
| if (isUnauthorizedError(error)) throw error | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
2331
to
2341
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a candidate path fails with a permanent error like let activeCandidates = [...candidatePaths]
for (const [index, delayMs] of readDelays.entries()) {
if (activeCandidates.length === 0) break
if (delayMs > 0) await delay(delayMs)
const nextCandidates: string[] = []
for (const candidatePath of activeCandidates) {
try {
return eventContextPreviewFromFile(await client.readFile(handle.workspaceId, candidatePath))
} catch (error) {
readFileError = error
if (!isUnauthorizedError(error)) {
nextCandidates.push(candidatePath)
}
}
}
activeCandidates = nextCandidates
} |
||
| } | ||
|
|
@@ -2401,7 +2449,7 @@ export class IntegrationEventBridge { | |
| } | ||
|
|
||
| const eventMetadata = integrationEventMetadata(event) | ||
| const contextPreview = await this.readEventContextPreview(projectId, event) | ||
| const contextPreview = await this.readEventContextPreview(projectId, event, matchedSpecs) | ||
| const usesConcreteAgentTargets = uniqueRecipients.every((recipient) => !recipient.startsWith('#')) | ||
| const canTrackInjectedDelivery = usesConcreteAgentTargets && typeof bridge.sendMessageAndWaitForInjected === 'function' | ||
| const shouldTrackDedupe = canTrackInjectedDelivery | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,6 +276,24 @@ describe('IntegrationMountManager', () => { | |
| }) | ||
| }) | ||
|
|
||
| it('mounts Slack thread context roots in mirror mode', async () => { | ||
| const manager = new IntegrationMountManager() | ||
|
|
||
| await manager.ensureMounted([ | ||
| { | ||
| provider: 'slack', | ||
| mountPaths: ['/slack/channels/C123/threads'] | ||
| } | ||
| ]) | ||
|
|
||
| expect(mock.mountInputs[0]).toMatchObject({ | ||
| localDir: '/tmp/pear-home/.agentworkforce/pear/relayfile/workspaces/account-workspace-id/slack/channels/C123/threads', | ||
| remotePath: '/slack/channels/C123/threads', | ||
| localLayout: 'exact', | ||
| syncMode: 'mirror' | ||
| }) | ||
| }) | ||
|
Comment on lines
+279
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win Add a test case for mounting both messages and threads for the same channel. The coding guideline requires regression tests when touching integration notifications, not just happy-path cases. Based on Suggested test case+ it('mounts both messages and threads for the same channel as separate mounts', async () => {
+ const manager = new IntegrationMountManager()
+
+ await manager.ensureMounted([
+ {
+ provider: 'slack',
+ mountPaths: ['/slack/channels/C123/messages', '/slack/channels/C123/threads']
+ }
+ ])
+
+ expect(mock.mountInputs).toHaveLength(2)
+ expect(mock.mountInputs[0]).toMatchObject({
+ localDir: '/tmp/pear-home/.agentworkforce/pear/relayfile/workspaces/account-workspace-id/slack/channels/C123/messages',
+ remotePath: '/slack/channels/C123/messages',
+ localLayout: 'exact',
+ syncMode: 'write-only'
+ })
+ expect(mock.mountInputs[1]).toMatchObject({
+ localDir: '/tmp/pear-home/.agentworkforce/pear/relayfile/workspaces/account-workspace-id/slack/channels/C123/threads',
+ remotePath: '/slack/channels/C123/threads',
+ localLayout: 'exact',
+ syncMode: 'mirror'
+ })
+ })🤖 Prompt for AI AgentsSource: Coding guidelines |
||
|
|
||
| it('rejects Slack command roots with traversal segments', async () => { | ||
| const manager = new IntegrationMountManager() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align DM defaults across UI and bridge to keep DM listening truly opt-in.
Line 415 now defaults unset DM scope to
false, but the provided UI snippets still defaultlistenDmstotruewhen unset. That mismatch can silently keep DM subscriptions enabled and weakens the PR’s intended privacy posture.🤖 Prompt for AI Agents