Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix: address review findings on the unresolved-sender notice
Jose Ramirezb93c20fa44e8
diff --git a/channel-reader/package-lock.json b/channel-reader/package-lock.jsonindex 6fc4b35a5..4861e3118 100644--- a/channel-reader/package-lock.json+++ b/channel-reader/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-channel-reader",- "version": "0.1.91",+ "version": "0.1.92","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-channel-reader",- "version": "0.1.91",+ "version": "0.1.92","license": "SEE LICENSE IN LICENSE","dependencies": {"@kubernetes/client-node": "^1.0.0",diff --git a/channel-reader/package.json b/channel-reader/package.jsonindex d3d1885d0..40ec9029f 100644--- a/channel-reader/package.json+++ b/channel-reader/package.json@@ -1,6 +1,6 @@{"name": "clerum-channel-reader",- "version": "0.1.91",+ "version": "0.1.92","description": "Watches CommunicationChannel CRDs and fetches messages from Telegram, Email, and Slack","main": "dist/main.js","engines": {diff --git a/channel-reader/src/main.ts b/channel-reader/src/main.tsindex 3b335a34c..90185e047 100644--- a/channel-reader/src/main.ts+++ b/channel-reader/src/main.ts@@ -1561,7 +1561,11 @@ export class ChannelReader {const channelId = msg.channelId?.trim()if (!userId || !workspaceId || !channelId) return- const key = `${workspaceId}:${userId}:${channelId}`+ // Scope the limiter by provider identity, the same way pendingApprovalChannelScope+ // does. The SEND still targets msg.channelId, which is what replyChannelId hands+ // every other outbound Slack call; only the dedupe identity is provider-scoped.+ const conversationId = identity?.providerChannelId?.trim() || channelId+ const key = `${workspaceId}:${userId}:${conversationId}`if (this.unresolvedNoticesSent.has(key)) return// Record on ATTEMPT, not on success: a conversation Slack keeps rejecting// must not produce one outbound call per inbound message.@@ -1571,7 +1575,19 @@ export class ChannelReader {if (!adapter?.sendEphemeral) returnconst profileUrl = config.profileUiUrl?.trim()const content = profileUrl ? `${UNRESOLVED_NOTICE_COPY} ${profileUrl}` : UNRESOLVED_NOTICE_COPY- await adapter.sendEphemeral(channelId, userId, content)+ try {+ await adapter.sendEphemeral(channelId, userId, content)+ } catch (error) {+ // Contain the failure HERE. SlackAdapter swallows its own errors today, but+ // the ChannelAdapter interface promises nothing, and this runs inside the+ // per-message loop in handleMessages: an adapter that ever throws would+ // abort the rest of the batch over a notice that is best-effort by design.+ console.warn(+ `[Main] Could not send unresolved-sender notice to ${userId} in ${channelId}: ${+ error instanceof Error ? error.message : String(error)+ }`+ )+ }}private pendingApprovalChannelScope(msg: Message): string {diff --git a/channel-reader/test/unresolvedSenderNotice.test.ts b/channel-reader/test/unresolvedSenderNotice.test.tsindex 2d2eab4d5..1906b5cc3 100644--- a/channel-reader/test/unresolvedSenderNotice.test.ts+++ b/channel-reader/test/unresolvedSenderNotice.test.ts@@ -159,9 +159,18 @@ function buildReader(options: BuildReaderOptions = {}) {})let eventSeq = 0- const deliver = async (channelId: string, userId: string, workspaceId = 'T1'): Promise<void> => {+ /**+ * `providerChannelId` defaults to `channelId` because that is what Slack does+ * today; the tests that pin WHICH id scopes the limiter pass a different value.+ */+ const buildMessage = (+ channelId: string,+ userId: string,+ workspaceId = 'T1',+ providerChannelId = channelId+ ): Message => {eventSeq += 1- const message: Message = {+ return {channelType: medium,channelId,sender: userId,@@ -172,24 +181,41 @@ function buildReader(options: BuildReaderOptions = {}) {medium,providerUserId: userId,providerWorkspaceId: workspaceId,- providerChannelId: channelId,+ providerChannelId,// Unique per delivery on purpose. A repeated providerEventId is dropped by// the provider-event dedupe *before* authorization runs, which would make// every "sent only once" assertion below pass with no rate limiter at all.providerEventId: `evt-${eventSeq}`,},}- // The real Slack adapter never throws (channels/slack.ts swallows Slack- // errors); the ephemeralThrows stub throws only to prove the limiter key is- // recorded before the send, so a rejection here is expected in that one case.- await reader.handleMessages([message]).catch(() => undefined)}+ // Deliberately NOT wrapped in `.catch()`: sendUnresolvedSenderNotice has to+ // contain a throwing adapter itself, so an escaped throw must surface here as a+ // rejected promise rather than being absorbed by the harness.+ const deliver = async (+ channelId: string,+ userId: string,+ workspaceId = 'T1',+ providerChannelId = channelId+ ): Promise<void> =>+ reader.handleMessages([buildMessage(channelId, userId, workspaceId, providerChannelId)])++ const deliverBatch = async (messages: Message[]): Promise<void> => reader.handleMessages(messages)+const runSweep = (): void => {;(reader as unknown as { cleanupStaleApprovals(): void }).cleanupStaleApprovals()}- return { deliver, runSweep, sendEphemeral, adapter, authorizeProviderMessage }+ return {+ buildMessage,+ deliver,+ deliverBatch,+ runSweep,+ sendEphemeral,+ adapter,+ authorizeProviderMessage,+ }}describe('unresolved sender notice', () => {@@ -246,6 +272,25 @@ describe('unresolved sender notice', () => {expect(sendEphemeral).toHaveBeenCalledTimes(2)})+ // The two ids are identical for Slack today, so these two tests hold the limiter+ // to the PROVIDER id from both directions: a differing transport channelId must+ // not split the key, and a differing providerChannelId must.+ it('scopes the limiter by the provider channel id, not the transport channel id', async () => {+ vi.useFakeTimers()+ const { deliver, sendEphemeral } = buildReader()+ await deliver('C-transport', 'U1', 'T1', 'C1')+ await deliver('C-transport', 'U1', 'T1', 'C2')+ expect(sendEphemeral).toHaveBeenCalledTimes(2)+ })++ it('does not re-notify when only the transport channel id changes', async () => {+ vi.useFakeTimers()+ const { deliver, sendEphemeral } = buildReader()+ await deliver('C-transport-a', 'U1', 'T1', 'C1')+ await deliver('C-transport-b', 'U1', 'T1', 'C1')+ expect(sendEphemeral).toHaveBeenCalledTimes(1)+ })+it('treats the same user and channel in a second workspace as its own conversation', async () => {vi.useFakeTimers()const { deliver, sendEphemeral } = buildReader()@@ -257,11 +302,28 @@ describe('unresolved sender notice', () => {it('records the key even when sendEphemeral throws, so one failure is not retried forever', async () => {vi.useFakeTimers()const { deliver, sendEphemeral } = buildReader({ ephemeralThrows: true })- await deliver('C1', 'U1')+ // Nothing catches for us: an uncontained throw rejects this and fails the test.+ await expect(deliver('C1', 'U1')).resolves.toBeUndefined()await deliver('C1', 'U1')expect(sendEphemeral).toHaveBeenCalledTimes(1)})+ // The notice is best-effort; the batch is not. SlackAdapter swallows its own+ // errors today, but ChannelAdapter.sendEphemeral does not promise that, and this+ // runs inside the per-message loop in handleMessages.+ it('keeps processing the rest of the batch when sendEphemeral throws', async () => {+ const { buildMessage, deliverBatch, sendEphemeral, authorizeProviderMessage } = buildReader({+ ephemeralThrows: true,+ })+ await expect(+ deliverBatch([buildMessage('C1', 'U1'), buildMessage('C2', 'U2')])+ ).resolves.toBeUndefined()+ // Both messages reached authorization, and both got their own notice attempt:+ // a throw escaping the first would have aborted the loop at one apiece.+ expect(authorizeProviderMessage).toHaveBeenCalledTimes(2)+ expect(sendEphemeral).toHaveBeenCalledTimes(2)+ })+it('never sends on reason error', async () => {const { deliver, sendEphemeral } = buildReader({ reason: 'error' })await deliver('C1', 'U1')diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex db7f63476..6b495a027 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.236",+ "version": "0.1.237","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.236",+ "version": "0.1.237","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex e3f15e2b6..c4d1823eb 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.236",+ "version": "0.1.237","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/__tests__/providerMessageAuthorization.reason.test.ts b/mcp-host/src/__tests__/providerMessageAuthorization.reason.test.tsindex 79a1bb541..f47763680 100644--- a/mcp-host/src/__tests__/providerMessageAuthorization.reason.test.ts+++ b/mcp-host/src/__tests__/providerMessageAuthorization.reason.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it, vi } from 'vitest'+import { describe, expect, it } from 'vitest'import { WorkflowBrokerRequestError } from '../core/tools/workflowBrokerClient'import { classifyAuthorizationFailure } from '../main'@@ -51,4 +51,18 @@ describe('classifyAuthorizationFailure', () => {)expect(classifyAuthorizationFailure(err)).toBe('error')})++ // Each code is bound to its ONE status. Both statuses below are themselves+ // whitelisted, and both codes are themselves whitelisted, so only the+ // code-to-status BINDING can reject these pairs. A plain set of codes checked+ // against `status === 404 || status === 403` classifies both 'unresolved'.+ it('maps a 403 medium_account_not_found to error', () => {+ const err = new WorkflowBrokerRequestError(403, 'medium_account_not_found', 'denied')+ expect(classifyAuthorizationFailure(err)).toBe('error')+ })++ it('maps a 404 communication_channel_access_denied to error', () => {+ const err = new WorkflowBrokerRequestError(404, 'communication_channel_access_denied', 'nope')+ expect(classifyAuthorizationFailure(err)).toBe('error')+ })})diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.tsindex 0ff552f5e..fee344a05 100644--- a/mcp-host/src/main.ts+++ b/mcp-host/src/main.ts@@ -1788,9 +1788,15 @@ async function handleProviderWorkflowApprovalDecision(return submitProviderWorkflowApprovalDecision(decision, runtimeAuth)}-const UNRESOLVED_BROKER_CODES = new Set([- 'medium_account_not_found',- 'communication_channel_access_denied',+/**+ * Each code is bound to the ONE status control-api emits it with. A pair that+ * does not match exactly (403 medium_account_not_found, 404+ * communication_channel_access_denied) is 'error', so a code reused later under a+ * different status with a different meaning cannot silently widen this whitelist.+ */+const UNRESOLVED_BROKER_CODE_STATUS = new Map<string, number>([+ ['medium_account_not_found', 404],+ ['communication_channel_access_denied', 403],])/**@@ -1801,9 +1807,13 @@ const UNRESOLVED_BROKER_CODES = new Set([* unlinked.*/export function classifyAuthorizationFailure(error: unknown): 'unresolved' | 'error' {- if (error instanceof WorkflowBrokerRequestError) {- if ((error.status === 404 || error.status === 403) && error.code) {- return UNRESOLVED_BROKER_CODES.has(error.code) ? 'unresolved' : 'error'+ if (error instanceof WorkflowBrokerRequestError && error.code) {+ const expectedStatus = UNRESOLVED_BROKER_CODE_STATUS.get(error.code)+ // `expectedStatus !== undefined` first: an unrecognized code lookup and a+ // missing status would otherwise compare undefined === undefined and classify+ // 'unresolved'.+ if (expectedStatus !== undefined && expectedStatus === error.status) {+ return 'unresolved'}}return 'error'