Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(channel-reader): terse Telegram notice and a global cap on unresolved-sender notices

Jose Ramirez9f4ca2953310
diff --git a/channel-reader/package-lock.json b/channel-reader/package-lock.json
index 54673b589..0602cd6a9 100644
--- a/channel-reader/package-lock.json
+++ b/channel-reader/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-channel-reader",
- "version": "0.1.95",
+ "version": "0.1.96",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-channel-reader",
- "version": "0.1.95",
+ "version": "0.1.96",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@kubernetes/client-node": "^1.0.0",
diff --git a/channel-reader/package.json b/channel-reader/package.json
index fcb143b85..6a3698267 100644
--- a/channel-reader/package.json
+++ b/channel-reader/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-channel-reader",
- "version": "0.1.95",
+ "version": "0.1.96",
"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.ts
index e412cf38d..30fbe0c19 100644
--- a/channel-reader/src/main.ts
+++ b/channel-reader/src/main.ts
@@ -105,6 +105,16 @@ const UNRESOLVED_NOTICE_COPY =
const UNRESOLVED_NOTICE_COPY_TEAMS =
"I can't accept messages from this Teams account. If you haven't linked it yet, " +
'do that in your evenfire profile. If you think you should already have access, contact your admin.'
+// Telegram deliberately gets no Profile UI link and no product name: unlike Teams
+// and Slack, anyone who discovers the bot handle can message it, so a reply must
+// not confirm what the bot belongs to.
+const UNRESOLVED_NOTICE_COPY_TELEGRAM =
+ "I can't accept messages from this account. If you think that is wrong, contact your administrator."
+/**
+ * Ceiling on live notice records, so a misconfiguration cannot turn the bot
+ * into a flooder. Exported so tests assert against the real value.
+ */
+export const UNRESOLVED_NOTICE_GLOBAL_CAP = 50
const SLACK_VERIFICATION_SCAN_INTERVAL_MS = 60 * 1000
const TOOL_APPROVAL_ACTION_RE = /^tool:([ald]):([A-Za-z0-9_-]{16})$/
/**
@@ -1553,16 +1563,28 @@ export class ChannelReader {
}
/**
- * Tell an unresolved Slack or Teams sender why the agent is ignoring them, at
- * most once per user per conversation per UNRESOLVED_NOTICE_TTL_MS.
+ * Tell an unresolved Slack, Teams, or Telegram sender why the agent is ignoring
+ * them, at most once per user per conversation per UNRESOLVED_NOTICE_TTL_MS, and
+ * never past UNRESOLVED_NOTICE_GLOBAL_CAP live notice records in total.
*/
private async sendUnresolvedSenderNotice(msg: Message): Promise<void> {
- if (msg.channelType !== 'slack' && msg.channelType !== 'teams') return
+ if (
+ msg.channelType !== 'slack' &&
+ msg.channelType !== 'teams' &&
+ msg.channelType !== 'telegram'
+ )
+ return
const identity = msg.providerIdentity
const userId = identity?.providerUserId?.trim()
const workspaceId = identity?.providerWorkspaceId?.trim()
const channelId = msg.channelId?.trim()
- if (!userId || !workspaceId || !channelId) return
+ if (!userId || !channelId) return
+ // Slack and Teams identities are scoped to a workspace/tenant an admin
+ // controls, and the limiter key below relies on that scope. Telegram has no
+ // such concept: every real Telegram message carries providerWorkspaceId: null,
+ // so requiring one here would silently drop the notice for every Telegram
+ // sender.
+ if (msg.channelType !== 'telegram' && !workspaceId) return
// Scope the limiter by provider identity, the same way pendingApprovalChannelScope
// does. The SEND still targets msg.channelId, which is what replyChannelId hands
@@ -1570,22 +1592,32 @@ export class ChannelReader {
const conversationId = identity?.providerChannelId?.trim() || channelId
const key = `${workspaceId}:${userId}:${conversationId}`
if (this.unresolvedNoticesSent.has(key)) return
+ if (this.unresolvedNoticesSent.size >= UNRESOLVED_NOTICE_GLOBAL_CAP) return
// Record on ATTEMPT, not on success: a conversation Slack keeps rejecting
// must not produce one outbound call per inbound message.
this.unresolvedNoticesSent.set(key, { seenAt: Date.now() })
const adapter = this.adapterForMessage(msg)
if (!adapter) return
- const profileUrl = config.profileUiUrl?.trim()
- const baseCopy =
- msg.channelType === 'teams' ? UNRESOLVED_NOTICE_COPY_TEAMS : UNRESOLVED_NOTICE_COPY
- const content = profileUrl ? `${baseCopy} ${profileUrl}` : baseCopy
+ let content: string
+ if (msg.channelType === 'telegram') {
+ content = UNRESOLVED_NOTICE_COPY_TELEGRAM
+ } else {
+ const profileUrl = config.profileUiUrl?.trim()
+ const baseCopy =
+ msg.channelType === 'teams' ? UNRESOLVED_NOTICE_COPY_TEAMS : UNRESOLVED_NOTICE_COPY
+ content = profileUrl ? `${baseCopy} ${profileUrl}` : baseCopy
+ }
try {
if (msg.channelType === 'teams') {
// TeamsAdapter has no ephemeral concept, so the notice is a normal message.
// Thread it under the triggering message so an unconnected user does not
// produce a top-level post in a shared channel.
await adapter.sendMessage(channelId, content, msg.messageId)
+ } else if (msg.channelType === 'telegram') {
+ // No reply id: unlike Teams, Telegram has no channel-wide audience for a
+ // top-level post to leak this notice to.
+ await adapter.sendMessage(channelId, content)
} else {
if (!adapter.sendEphemeral) return
await adapter.sendEphemeral(channelId, userId, content)
diff --git a/channel-reader/test/unresolvedSenderNotice.test.ts b/channel-reader/test/unresolvedSenderNotice.test.ts
index 3af46105a..cefb6b66d 100644
--- a/channel-reader/test/unresolvedSenderNotice.test.ts
+++ b/channel-reader/test/unresolvedSenderNotice.test.ts
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
-import { ChannelReader, type ChannelReaderOptions } from '../src/main'
+import { ChannelReader, type ChannelReaderOptions, UNRESOLVED_NOTICE_GLOBAL_CAP } from '../src/main'
import { RPCClient } from '../src/rpcClient'
import type { ChannelAdapter, Message } from '../src/types'
@@ -387,6 +387,61 @@ describe('unresolved sender notice on Teams', () => {
})
})
+describe('unresolved sender notice on Telegram', () => {
+ it('sends a terse Telegram notice with no product or profile details', async () => {
+ const { deliver, sendMessage } = buildReader({
+ medium: 'telegram',
+ profileUiUrl: 'https://profile.test',
+ })
+
+ await deliver()
+
+ const content = sendMessage.mock.calls[0][1] as string
+ expect(content).not.toMatch(/evenfire/i)
+ expect(content).not.toMatch(/profile/i)
+ expect(content).not.toMatch(/https?:\/\//)
+ })
+
+ it('delivers the Telegram notice with no reply id, unlike Teams', async () => {
+ const { deliver, sendMessage } = buildReader({ medium: 'telegram' })
+
+ await deliver()
+
+ expect(sendMessage).toHaveBeenCalledTimes(1)
+ // Teams threads under the triggering message via a third argument; Telegram
+ // must not, since Telegram has no channel-wide audience to protect against.
+ expect(sendMessage.mock.calls[0]).toEqual(['C1', expect.any(String)])
+ })
+
+ // Real Telegram messages always carry providerWorkspaceId: null (Telegram has no
+ // workspace/tenant concept), unlike Slack and Teams, which require one. A guard
+ // that keeps requiring a workspace id for every provider would silently drop
+ // this notice for every real Telegram sender while still passing a test harness
+ // that defaults workspaceId to a non-empty string.
+ it('sends the Telegram notice even though Telegram messages carry no workspace id', async () => {
+ const { deliver, sendMessage } = buildReader({ medium: 'telegram' })
+
+ await deliver('C1', 'U1', '')
+
+ expect(sendMessage).toHaveBeenCalledTimes(1)
+ })
+})
+
+describe('unresolved notice global cap', () => {
+ it('stops sending notices once the global cap is reached', async () => {
+ const { deliverBatch, sendMessage, buildMessage } = buildReader({ medium: 'teams' })
+
+ // Distinct users so the per-conversation dedup never fires and only the
+ // global cap can be what stops the sends.
+ const messages = Array.from({ length: UNRESOLVED_NOTICE_GLOBAL_CAP + 5 }, (_, i) =>
+ buildMessage('C1', `U${i}`, 'T1')
+ )
+ await deliverBatch(messages)
+
+ expect(sendMessage).toHaveBeenCalledTimes(UNRESOLVED_NOTICE_GLOBAL_CAP)
+ })
+})
+
describe('unresolved notice link', () => {
it('appends the profile URL when configured', async () => {
const { deliver, sendEphemeral } = buildReader({ profileUiUrl: 'https://profile.example.com' })