@cursor/july 0.1.24 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/dist/bin/agent-serve.js +0 -0
  2. package/dist/channels/github/instrument.d.ts +20 -0
  3. package/dist/channels/github/instrument.d.ts.map +1 -0
  4. package/dist/docs/404.html +1 -1
  5. package/dist/docs/ab.html +2 -2
  6. package/dist/docs/assets/{app.CvSGaAxk.js → app.DYcC9FY-.js} +1 -1
  7. package/dist/docs/assets/chunks/@localSearchIndexroot.BQTzJjR_.js +1 -0
  8. package/dist/docs/assets/chunks/{VPLocalSearchBox.J1jJbCvs.js → VPLocalSearchBox.o4N_knTV.js} +1 -1
  9. package/dist/docs/assets/chunks/{theme.C7vfzr7h.js → theme.DQ-njyo0.js} +2 -2
  10. package/dist/docs/building-with-agents.html +2 -2
  11. package/dist/docs/concepts.html +2 -2
  12. package/dist/docs/deployment.html +2 -2
  13. package/dist/docs/evals.html +2 -2
  14. package/dist/docs/example-agents/approval-buddy.html +2 -2
  15. package/dist/docs/example-agents/benny.html +2 -2
  16. package/dist/docs/example-agents/bugbot.html +2 -2
  17. package/dist/docs/example-agents/codebase-wiki.html +2 -2
  18. package/dist/docs/example-agents/codeowners-review.html +2 -2
  19. package/dist/docs/example-agents/concierge.html +2 -2
  20. package/dist/docs/example-agents/fsd.html +2 -2
  21. package/dist/docs/example-agents/index.html +2 -2
  22. package/dist/docs/example-agents/knowledge-base.html +2 -2
  23. package/dist/docs/example-agents/oncall.html +2 -2
  24. package/dist/docs/example-agents/security-reviewer.html +2 -2
  25. package/dist/docs/example-agents/slack-agent.html +2 -2
  26. package/dist/docs/example-agents/weather-agent.html +2 -2
  27. package/dist/docs/guides/agent-to-agent.html +2 -2
  28. package/dist/docs/guides/cloud-runtime.html +2 -2
  29. package/dist/docs/guides/github.html +2 -2
  30. package/dist/docs/guides/human-in-the-loop.html +2 -2
  31. package/dist/docs/guides/mcp-oauth.html +2 -2
  32. package/dist/docs/guides/slack.html +2 -2
  33. package/dist/docs/guides/webhooks.html +2 -2
  34. package/dist/docs/hillclimbing.html +2 -2
  35. package/dist/docs/index.html +2 -2
  36. package/dist/docs/quickstart.html +2 -2
  37. package/dist/docs/reference/agent-config.html +2 -2
  38. package/dist/docs/reference/channels.html +2 -2
  39. package/dist/docs/reference/cli.html +2 -2
  40. package/dist/docs/reference/connections.html +2 -2
  41. package/dist/docs/reference/hooks.html +2 -2
  42. package/dist/docs/reference/http-api.html +2 -2
  43. package/dist/docs/reference/instructions.html +2 -2
  44. package/dist/docs/reference/playground.html +2 -2
  45. package/dist/docs/reference/project-layout.html +2 -2
  46. package/dist/docs/reference/prompt.html +2 -2
  47. package/dist/docs/reference/schedules.html +2 -2
  48. package/dist/docs/reference/sessions.html +2 -2
  49. package/dist/docs/reference/skills.html +2 -2
  50. package/dist/docs/reference/subagents.html +2 -2
  51. package/dist/docs/reference/tools.html +2 -2
  52. package/dist/docs/scaffolding-agents.html +2 -2
  53. package/dist/docs/storage.html +2 -2
  54. package/dist/docs/troubleshooting.html +2 -2
  55. package/dist/internal/json-dir-store.d.ts +32 -0
  56. package/dist/internal/json-dir-store.d.ts.map +1 -0
  57. package/dist/internal/json-dir-store.js +100 -0
  58. package/dist/playground/assets/index-CjOQ4hN9.css +1 -0
  59. package/dist/playground/assets/{index-DTG9OsPV.js → index-DqXdAFGa.js} +41 -41
  60. package/dist/playground/index.html +2 -2
  61. package/package.json +31 -24
  62. package/src/artifacts.ts +78 -0
  63. package/src/bin/agent-serve.version.test.ts +64 -0
  64. package/src/channels/github/api.test.ts +64 -0
  65. package/src/channels/github/auth.test.ts +105 -0
  66. package/src/channels/github/cursor-account.test.ts +204 -0
  67. package/src/channels/github/forward.test.ts +457 -0
  68. package/src/channels/github/github-channel.ts +19 -2
  69. package/src/channels/github/github.test.ts +985 -0
  70. package/src/channels/github/replay.test.ts +179 -0
  71. package/src/channels/github/types.ts +6 -0
  72. package/src/channels/slack/api.post-message.test.ts +148 -0
  73. package/src/channels/slack/api.ts +3 -1
  74. package/src/channels/slack/approvals.test.ts +328 -0
  75. package/src/channels/slack/block-actions.test.ts +469 -0
  76. package/src/channels/slack/bot-mentions.test.ts +267 -0
  77. package/src/channels/slack/channel-watch.test.ts +363 -0
  78. package/src/channels/slack/cursor-account.test.ts +301 -0
  79. package/src/channels/slack/cursor-account.ts +3 -1
  80. package/src/channels/slack/defaults.final-post.test.ts +182 -0
  81. package/src/channels/slack/dispatch.test.ts +875 -0
  82. package/src/channels/slack/dispatch.ts +12 -1
  83. package/src/channels/slack/eval-directive.test.ts +273 -0
  84. package/src/channels/slack/interactive.ts +3 -0
  85. package/src/channels/slack/message-body.test.ts +54 -0
  86. package/src/channels/slack/nudge-store.test.ts +143 -0
  87. package/src/channels/slack/slack-channel.ts +6 -0
  88. package/src/channels/slack/slack.test.ts +391 -0
  89. package/src/channels/slack/stop.test.ts +23 -0
  90. package/src/channels/slack/thread-context.test.ts +202 -0
  91. package/src/channels/slack/types.ts +6 -0
  92. package/src/evals/assertions.test.ts +617 -0
  93. package/src/evals/assertions.ts +37 -1
  94. package/src/evals/expect.test.ts +144 -0
  95. package/src/evals/judge.test.ts +181 -0
  96. package/src/evals/loaders.test.ts +132 -0
  97. package/src/evals/matchers.test.ts +95 -0
  98. package/src/evals/reporters.test.ts +303 -0
  99. package/src/evals/run-facts.test.ts +259 -0
  100. package/src/evals.ts +1 -0
  101. package/src/index.ts +1 -0
  102. package/src/internal/ab-snapshot.test.ts +325 -0
  103. package/src/internal/approval-gate.test.ts +49 -0
  104. package/src/internal/approvals.integration.test.ts +383 -0
  105. package/src/internal/artifacts-store.test.ts +283 -0
  106. package/src/internal/artifacts-store.ts +290 -0
  107. package/src/internal/authored-loaders.test.ts +31 -0
  108. package/src/internal/builtin-tools/artifacts.test.ts +247 -0
  109. package/src/internal/builtin-tools/artifacts.ts +74 -0
  110. package/src/internal/builtin-tools/index.ts +21 -0
  111. package/src/internal/builtin-tools/reminders.test.ts +201 -0
  112. package/src/internal/channel-route-schema.test.ts +294 -0
  113. package/src/internal/chat-attach.test.ts +262 -0
  114. package/src/internal/cli-deploy.test.ts +1991 -0
  115. package/src/internal/cli-docs.test.ts +161 -0
  116. package/src/internal/cli-mcp.test.ts +789 -0
  117. package/src/internal/cli-skills.test.ts +133 -0
  118. package/src/internal/cli-slack.test.ts +1654 -0
  119. package/src/internal/cli-slack.ts +13 -4
  120. package/src/internal/cloud-merge.test.ts +74 -0
  121. package/src/internal/cron.test.ts +22 -0
  122. package/src/internal/cursor/account-mcp.test.ts +807 -0
  123. package/src/internal/cursor/backend-client.test.ts +591 -0
  124. package/src/internal/cursor/credentials.test.ts +351 -0
  125. package/src/internal/cursor/github-credentials.test.ts +136 -0
  126. package/src/internal/cursor-account-mcp-auth.test.ts +310 -0
  127. package/src/internal/cursor-account.integration.test.ts +441 -0
  128. package/src/internal/cursor-event-relay.test.ts +746 -0
  129. package/src/internal/cursor-github-credentials.integration.test.ts +271 -0
  130. package/src/internal/cursor-slack-relay.test.ts +525 -0
  131. package/src/internal/deploy-source.test.ts +111 -0
  132. package/src/internal/discovery.artifact-tool.test.ts +136 -0
  133. package/src/internal/discovery.artifacts.test.ts +119 -0
  134. package/src/internal/discovery.builtin-tools.test.ts +94 -0
  135. package/src/internal/discovery.concurrency.test.ts +60 -0
  136. package/src/internal/discovery.cursor-account.test.ts +133 -0
  137. package/src/internal/discovery.cwd.test.ts +83 -0
  138. package/src/internal/discovery.hosting.test.ts +80 -0
  139. package/src/internal/discovery.identity.test.ts +44 -0
  140. package/src/internal/discovery.ts +102 -1
  141. package/src/internal/distribution.ts +1 -0
  142. package/src/internal/docs-site.test.ts +66 -0
  143. package/src/internal/duration.test.ts +29 -0
  144. package/src/internal/eval-judge-model.test.ts +187 -0
  145. package/src/internal/eval-run-store.cancel.test.ts +142 -0
  146. package/src/internal/eval-run-store.storage.test.ts +211 -0
  147. package/src/internal/eval-runner.http.test.ts +403 -0
  148. package/src/internal/eval-runner.run.test.ts +928 -0
  149. package/src/internal/evals-client.test.ts +307 -0
  150. package/src/internal/event-mapper.test.ts +243 -0
  151. package/src/internal/github-fanout.test.ts +213 -0
  152. package/src/internal/handleAgentServeTrigger.test.ts +185 -0
  153. package/src/internal/host-kv.test.ts +82 -0
  154. package/src/internal/host-platforms.test.ts +126 -0
  155. package/src/internal/http-channel.test.ts +402 -0
  156. package/src/internal/init-project.test.ts +270 -0
  157. package/src/internal/install-cursor-skills.test.ts +262 -0
  158. package/src/internal/local-env.test.ts +120 -0
  159. package/src/internal/log-ring.test.ts +31 -0
  160. package/src/internal/logs-client.test.ts +350 -0
  161. package/src/internal/mcp-endpoint.test.ts +436 -0
  162. package/src/internal/mcp-host.test.ts +298 -0
  163. package/src/internal/mcp-oauth.test.ts +148 -0
  164. package/src/internal/net.test.ts +17 -0
  165. package/src/internal/peer-connections.test.ts +128 -0
  166. package/src/internal/peer-mcp.integration.test.ts +289 -0
  167. package/src/internal/playground/toolchain.test.ts +53 -0
  168. package/src/internal/playground-cli.test.ts +187 -0
  169. package/src/internal/playground-proxy.test.ts +376 -0
  170. package/src/internal/prompt-context.integration.test.ts +232 -0
  171. package/src/internal/prompt-context.test.ts +127 -0
  172. package/src/internal/reminder-runner.test.ts +432 -0
  173. package/src/internal/reminder-runner.ts +1 -0
  174. package/src/internal/reminder-store.test.ts +53 -0
  175. package/src/internal/request-headers.test.ts +27 -0
  176. package/src/internal/resolve-prod-target.test.ts +787 -0
  177. package/src/internal/resolved-connections.test.ts +295 -0
  178. package/src/internal/router.test.ts +57 -0
  179. package/src/internal/schedule-runner.ts +1 -0
  180. package/src/internal/sdk-runner.test.ts +290 -0
  181. package/src/internal/server.artifacts.test.ts +334 -0
  182. package/src/internal/server.ts +45 -0
  183. package/src/internal/session-engine.artifacts.test.ts +293 -0
  184. package/src/internal/session-engine.coalesce.test.ts +169 -0
  185. package/src/internal/session-engine.concurrency.test.ts +250 -0
  186. package/src/internal/session-engine.host-oauth-mcp.test.ts +110 -0
  187. package/src/internal/session-engine.interrupt.test.ts +577 -0
  188. package/src/internal/session-engine.storage.test.ts +547 -0
  189. package/src/internal/session-engine.ts +129 -1
  190. package/src/internal/session-urls.test.ts +28 -0
  191. package/src/internal/sessions-client.test.ts +518 -0
  192. package/src/internal/storage-coordinator.test.ts +517 -0
  193. package/src/internal/storage-coordinator.ts +62 -0
  194. package/src/internal/tool-call.test.ts +458 -0
  195. package/src/internal/tool-result.test.ts +52 -0
  196. package/src/internal/trajectory.approvals.test.ts +83 -0
  197. package/src/internal/trajectory.subagents.test.ts +198 -0
  198. package/src/internal/turn-governor.test.ts +137 -0
  199. package/src/internal/update-check.test.ts +485 -0
  200. package/src/internal/workspace.test.ts +207 -0
  201. package/src/storage-backends/cursor-hosted.test.ts +121 -0
  202. package/src/storage.ts +6 -0
  203. package/src/types.ts +152 -2
  204. package/dist/docs/assets/chunks/@localSearchIndexroot.CL2Y0Zmh.js +0 -1
  205. package/dist/playground/assets/index-CidizGZv.css +0 -1
@@ -0,0 +1,267 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ botMentionsFromEnv,
4
+ buildDelegationMetadata,
5
+ normalizeBotMentionAllowlist,
6
+ parseDelegationMetadata,
7
+ SLACK_DELEGATION_METADATA_EVENT_TYPE,
8
+ SlackBotMentionGate,
9
+ } from "./bot-mentions.js";
10
+ import { parseAppMentionEvent } from "./inbound.js";
11
+ import type { SlackMessage } from "./types.js";
12
+
13
+ function botMention(patch: Partial<SlackMessage> = {}): SlackMessage {
14
+ return {
15
+ text: "<@ULASSO> please deploy #123",
16
+ body: "<@ULASSO> please deploy #123",
17
+ markdown: "@ULASSO please deploy #123",
18
+ ts: "2.0",
19
+ threadTs: "1.0",
20
+ channelId: "C9",
21
+ author: { userId: "ULASSO", isBot: true, botId: "BLASSO" },
22
+ ...patch,
23
+ };
24
+ }
25
+
26
+ function gate(
27
+ options: Partial<ConstructorParameters<typeof SlackBotMentionGate>[0]> = {},
28
+ identity: { botId?: string; botUserId?: string } = {
29
+ botId: "BSELF",
30
+ botUserId: "USELF",
31
+ }
32
+ ): SlackBotMentionGate {
33
+ return new SlackBotMentionGate(
34
+ { allowFrom: ["BLASSO"], ...options },
35
+ identity,
36
+ { log: { info() {}, warn() {}, error() {}, debug() {}, write() {} } }
37
+ );
38
+ }
39
+
40
+ describe("delegation metadata", () => {
41
+ it("round-trips build → parse", () => {
42
+ const metadata = buildDelegationMetadata({
43
+ depth: 2,
44
+ fromAgent: "lasso",
45
+ originSessionId: "sess-1",
46
+ });
47
+ expect(metadata.event_type).toBe(SLACK_DELEGATION_METADATA_EVENT_TYPE);
48
+ const parsed = parseDelegationMetadata({ metadata });
49
+ expect(parsed).toEqual({
50
+ depth: 2,
51
+ fromAgent: "lasso",
52
+ originSessionId: "sess-1",
53
+ });
54
+ });
55
+
56
+ it("ignores foreign and malformed metadata", () => {
57
+ expect(parseDelegationMetadata({})).toBeUndefined();
58
+ expect(
59
+ parseDelegationMetadata({
60
+ metadata: { event_type: "other", event_payload: { depth: 1 } },
61
+ })
62
+ ).toBeUndefined();
63
+ expect(
64
+ parseDelegationMetadata({
65
+ metadata: {
66
+ event_type: SLACK_DELEGATION_METADATA_EVENT_TYPE,
67
+ event_payload: { depth: 0 },
68
+ },
69
+ })
70
+ ).toBeUndefined();
71
+ expect(
72
+ parseDelegationMetadata({
73
+ metadata: {
74
+ event_type: SLACK_DELEGATION_METADATA_EVENT_TYPE,
75
+ event_payload: { depth: "two" },
76
+ },
77
+ })
78
+ ).toBeUndefined();
79
+ });
80
+ });
81
+
82
+ describe("botMentionsFromEnv", () => {
83
+ it("returns undefined when unset and options when set", () => {
84
+ const envVar = "BOT_MENTIONS_TEST_VAR";
85
+ delete process.env[envVar];
86
+ expect(botMentionsFromEnv(envVar)).toBeUndefined();
87
+ process.env[envVar] = " B1 ,,U2 ";
88
+ expect(botMentionsFromEnv(envVar)).toEqual({ allowFrom: ["B1", "U2"] });
89
+ delete process.env[envVar];
90
+ });
91
+ });
92
+
93
+ describe("parseAppMentionEvent bot-authored", () => {
94
+ const body = {
95
+ type: "event_callback",
96
+ team_id: "T1",
97
+ event: {
98
+ type: "app_mention",
99
+ user: "ULASSO",
100
+ bot_id: "BLASSO",
101
+ text: "<@UTARGET> look into this",
102
+ ts: "2.0",
103
+ thread_ts: "1.0",
104
+ channel: "C9",
105
+ metadata: buildDelegationMetadata({ depth: 1, fromAgent: "lasso" }),
106
+ },
107
+ };
108
+
109
+ it("drops bot mentions by default", () => {
110
+ expect(parseAppMentionEvent(body)).toBeNull();
111
+ });
112
+
113
+ it("keeps bot mentions with delegation info when opted in", () => {
114
+ const message = parseAppMentionEvent(body, { includeBotAuthored: true });
115
+ expect(message).not.toBeNull();
116
+ expect(message?.author?.isBot).toBe(true);
117
+ expect(message?.author?.botId).toBe("BLASSO");
118
+ expect(message?.delegation).toEqual({ depth: 1, fromAgent: "lasso" });
119
+ });
120
+
121
+ it("still parses human mentions when opted in", () => {
122
+ const message = parseAppMentionEvent(
123
+ {
124
+ type: "event_callback",
125
+ event: {
126
+ type: "app_mention",
127
+ user: "U1",
128
+ text: "<@UTARGET> hi",
129
+ ts: "3.0",
130
+ channel: "C9",
131
+ },
132
+ },
133
+ { includeBotAuthored: true }
134
+ );
135
+ expect(message?.author?.isBot).toBe(false);
136
+ expect(message?.delegation).toBeUndefined();
137
+ });
138
+ });
139
+
140
+ describe("normalizeBotMentionAllowlist", () => {
141
+ it("trims entries and rejects empty lists", () => {
142
+ expect(normalizeBotMentionAllowlist([" B1 ", "U2"])).toEqual(["B1", "U2"]);
143
+ expect(() => normalizeBotMentionAllowlist([" "])).toThrow(/allowFrom/);
144
+ });
145
+ });
146
+
147
+ describe("SlackBotMentionGate", () => {
148
+ it("requires its own bot identity", () => {
149
+ expect(() => gate({}, {})).toThrow(/identity/);
150
+ });
151
+
152
+ it("admits an allowlisted bot mention", () => {
153
+ const verdict = gate().evaluate(botMention());
154
+ expect(verdict).toEqual({ allow: true, depth: 1 });
155
+ });
156
+
157
+ it("matches on bot user id as well as bot_id", () => {
158
+ const verdict = gate({ allowFrom: ["ULASSO"] }).evaluate(botMention());
159
+ expect(verdict.allow).toBe(true);
160
+ });
161
+
162
+ it("matches compound botId:username entries case-insensitively", () => {
163
+ const g = gate({ allowFrom: ["BCURSOR:Lasso"] });
164
+ const fromCursorAsLasso = botMention({
165
+ author: {
166
+ userId: "UCURSOR",
167
+ isBot: true,
168
+ botId: "BCURSOR",
169
+ userName: "lasso",
170
+ },
171
+ });
172
+ expect(g.evaluate(fromCursorAsLasso).allow).toBe(true);
173
+ });
174
+
175
+ it("compound entries deny other usernames and missing usernames", () => {
176
+ const g = gate({ allowFrom: ["BCURSOR:Lasso"] });
177
+ expect(
178
+ g.evaluate(
179
+ botMention({
180
+ author: {
181
+ userId: "UCURSOR",
182
+ isBot: true,
183
+ botId: "BCURSOR",
184
+ userName: "SomeOtherAgent",
185
+ },
186
+ })
187
+ )
188
+ ).toMatchObject({ allow: false });
189
+ // No username on the event → fail closed.
190
+ expect(
191
+ g.evaluate(
192
+ botMention({
193
+ author: { userId: "UCURSOR", isBot: true, botId: "BCURSOR" },
194
+ })
195
+ )
196
+ ).toMatchObject({ allow: false });
197
+ // Bare bot_id was not allowlisted, only the compound form.
198
+ expect(
199
+ g.evaluate(
200
+ botMention({
201
+ author: {
202
+ userId: "UCURSOR",
203
+ isBot: true,
204
+ botId: "BCURSOR",
205
+ userName: undefined,
206
+ },
207
+ })
208
+ )
209
+ ).toMatchObject({ allow: false });
210
+ });
211
+
212
+ it("denies bots not in the allowlist", () => {
213
+ const verdict = gate().evaluate(
214
+ botMention({ author: { userId: "UOTHER", isBot: true, botId: "BOTHER" } })
215
+ );
216
+ expect(verdict).toMatchObject({ allow: false });
217
+ });
218
+
219
+ it("denies its own posts even when allowlisted", () => {
220
+ const verdict = gate({ allowFrom: ["BSELF"] }).evaluate(
221
+ botMention({ author: { userId: "USELF", isBot: true, botId: "BSELF" } })
222
+ );
223
+ expect(verdict).toMatchObject({ allow: false, reason: /own post/ });
224
+ });
225
+
226
+ it("enforces the depth budget", () => {
227
+ const g = gate();
228
+ expect(g.evaluate(botMention({ delegation: { depth: 2 } })).allow).toBe(
229
+ true
230
+ );
231
+ expect(
232
+ g.evaluate(botMention({ ts: "3.0", delegation: { depth: 3 } }))
233
+ ).toMatchObject({ allow: false, reason: /depth/ });
234
+ });
235
+
236
+ it("enforces the rate budget with a sliding window", () => {
237
+ const g = gate({ maxPerThreadPerHour: 2 });
238
+ expect(g.evaluate(botMention(), 1000).allow).toBe(true);
239
+ expect(g.evaluate(botMention(), 2000).allow).toBe(true);
240
+ expect(g.evaluate(botMention(), 3000)).toMatchObject({
241
+ allow: false,
242
+ reason: /rate budget/,
243
+ });
244
+ // Window slides: an hour later the thread has rate budget again.
245
+ const later = 3000 + 60 * 60 * 1000;
246
+ expect(g.evaluate(botMention(), later).allow).toBe(true);
247
+ // Other threads are unaffected.
248
+ expect(g.evaluate(botMention({ threadTs: "9.0" }), 3000).allow).toBe(true);
249
+ });
250
+
251
+ it("terminates ping-pong with the lifetime thread cap", () => {
252
+ const g = gate({ maxPerThreadPerHour: 100, maxPerThreadTotal: 3 });
253
+ expect(g.evaluate(botMention(), 1000).allow).toBe(true);
254
+ expect(g.evaluate(botMention(), 2000).allow).toBe(true);
255
+ expect(g.evaluate(botMention(), 3000).allow).toBe(true);
256
+ expect(g.evaluate(botMention(), 4000)).toMatchObject({
257
+ allow: false,
258
+ reason: /lifetime/,
259
+ });
260
+ // Sliding the window does NOT refill the lifetime cap.
261
+ const muchLater = 4000 + 5 * 60 * 60 * 1000;
262
+ expect(g.evaluate(botMention(), muchLater)).toMatchObject({
263
+ allow: false,
264
+ reason: /lifetime/,
265
+ });
266
+ });
267
+ });
@@ -0,0 +1,363 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import {
3
+ createChannelPostWatcher,
4
+ SlackChannelAllowlist,
5
+ } from "./channel-watch.js";
6
+ import {
7
+ parseChannelMessageEvent,
8
+ type SlackChannelMessageEvent,
9
+ } from "./inbound.js";
10
+ import type { SlackMessage } from "./types.js";
11
+
12
+ function channelPostBody(args: {
13
+ channel?: string;
14
+ ts?: string;
15
+ threadTs?: string;
16
+ text?: string;
17
+ subtype?: string;
18
+ botId?: string;
19
+ channelType?: string;
20
+ user?: string | null;
21
+ }): unknown {
22
+ return {
23
+ type: "event_callback",
24
+ event_id: "Ev1",
25
+ team_id: "T1",
26
+ event: {
27
+ type: "message",
28
+ channel: args.channel ?? "C123",
29
+ channel_type: args.channelType ?? "channel",
30
+ ts: args.ts ?? "100.1",
31
+ ...(args.threadTs === undefined ? {} : { thread_ts: args.threadTs }),
32
+ text: args.text ?? "new bug report",
33
+ ...(args.user === null ? {} : { user: args.user ?? "U1" }),
34
+ ...(args.subtype === undefined ? {} : { subtype: args.subtype }),
35
+ ...(args.botId === undefined ? {} : { bot_id: args.botId }),
36
+ },
37
+ };
38
+ }
39
+
40
+ describe("parseChannelMessageEvent", () => {
41
+ it("parses a top-level channel post", () => {
42
+ const parsed = parseChannelMessageEvent(channelPostBody({}));
43
+ expect(parsed).not.toBeNull();
44
+ expect(parsed?.kind).toBe("post");
45
+ if (parsed?.kind === "post") {
46
+ expect(parsed.message.channelId).toBe("C123");
47
+ expect(parsed.message.ts).toBe("100.1");
48
+ expect(parsed.message.threadTs).toBe("100.1");
49
+ }
50
+ });
51
+
52
+ it("parses private-channel (group) posts", () => {
53
+ const parsed = parseChannelMessageEvent(
54
+ channelPostBody({ channel: "G9", channelType: "group" })
55
+ );
56
+ expect(parsed?.kind).toBe("post");
57
+ });
58
+
59
+ it("drops bot-authored posts", () => {
60
+ expect(
61
+ parseChannelMessageEvent(channelPostBody({ botId: "B77" }))
62
+ ).toBeNull();
63
+ });
64
+
65
+ it("keeps bot-authored posts when includeBotAuthored is set", () => {
66
+ const parsed = parseChannelMessageEvent(channelPostBody({ botId: "B77" }), {
67
+ includeBotAuthored: true,
68
+ });
69
+ expect(parsed?.kind).toBe("post");
70
+ if (parsed?.kind === "post") {
71
+ expect(parsed.message.author?.isBot).toBe(true);
72
+ expect(parsed.message.author?.botId).toBe("B77");
73
+ }
74
+ });
75
+
76
+ it("keeps userless bot_message webhook posts when includeBotAuthored is set", () => {
77
+ const body = channelPostBody({
78
+ botId: "B77",
79
+ subtype: "bot_message",
80
+ user: null,
81
+ text: "PagerDuty: high error rate on api",
82
+ });
83
+ expect(parseChannelMessageEvent(body)).toBeNull();
84
+ const parsed = parseChannelMessageEvent(body, {
85
+ includeBotAuthored: true,
86
+ });
87
+ expect(parsed?.kind).toBe("post");
88
+ if (parsed?.kind === "post") {
89
+ expect(parsed.message.author).toEqual({
90
+ userId: "",
91
+ isBot: true,
92
+ botId: "B77",
93
+ });
94
+ }
95
+ });
96
+
97
+ it("recovers alert content from attachments when top-level text is empty", () => {
98
+ const parsed = parseChannelMessageEvent(
99
+ {
100
+ type: "event_callback",
101
+ team_id: "T1",
102
+ event: {
103
+ type: "message",
104
+ subtype: "bot_message",
105
+ channel: "C123",
106
+ channel_type: "channel",
107
+ ts: "100.1",
108
+ bot_id: "B77",
109
+ username: "Datadog",
110
+ text: "",
111
+ attachments: [
112
+ {
113
+ title:
114
+ "Triggered: [service-monitor prod:temporal-bugbot] High cache error rate",
115
+ title_link: "https://us5.datadoghq.com/monitors/19610160",
116
+ text: "Service temporal-bugbot has a high cache error rate.",
117
+ },
118
+ ],
119
+ },
120
+ },
121
+ { includeBotAuthored: true }
122
+ );
123
+ expect(parsed?.kind).toBe("post");
124
+ if (parsed?.kind === "post") {
125
+ expect(parsed.message.text).toBe("");
126
+ expect(parsed.message.body).toContain("temporal-bugbot");
127
+ // Raw body keeps the monitor link; markdown is the model-facing view.
128
+ expect(parsed.message.body).toContain(
129
+ "https://us5.datadoghq.com/monitors/19610160"
130
+ );
131
+ expect(parsed.message.markdown).toContain("High cache error rate");
132
+ }
133
+ });
134
+
135
+ it("drops DMs and unknown subtypes", () => {
136
+ expect(
137
+ parseChannelMessageEvent(
138
+ channelPostBody({ channel: "D5", channelType: "im" })
139
+ )
140
+ ).toBeNull();
141
+ expect(
142
+ parseChannelMessageEvent(channelPostBody({ subtype: "channel_join" }))
143
+ ).toBeNull();
144
+ });
145
+
146
+ it("parses message_changed into an edit with the new text", () => {
147
+ const parsed = parseChannelMessageEvent({
148
+ type: "event_callback",
149
+ event: {
150
+ type: "message",
151
+ subtype: "message_changed",
152
+ channel: "C123",
153
+ channel_type: "channel",
154
+ ts: "101.0",
155
+ message: { type: "message", ts: "100.1", text: "edited!", user: "U1" },
156
+ },
157
+ });
158
+ expect(parsed?.kind).toBe("edited");
159
+ if (parsed?.kind === "edited") {
160
+ expect(parsed.message.ts).toBe("100.1");
161
+ expect(parsed.message.text).toBe("edited!");
162
+ }
163
+ });
164
+
165
+ it("parses message_deleted into a deletion", () => {
166
+ const parsed = parseChannelMessageEvent({
167
+ type: "event_callback",
168
+ event: {
169
+ type: "message",
170
+ subtype: "message_deleted",
171
+ channel: "C123",
172
+ channel_type: "channel",
173
+ ts: "101.0",
174
+ deleted_ts: "100.1",
175
+ },
176
+ });
177
+ expect(parsed).toEqual({ kind: "deleted", channelId: "C123", ts: "100.1" });
178
+ });
179
+ });
180
+
181
+ describe("SlackChannelAllowlist", () => {
182
+ it("matches ids directly and names after resolution", () => {
183
+ const allowlist = new SlackChannelAllowlist(["C123", "#issues-glass"]);
184
+ expect(allowlist.matches("C123")).toBe(true);
185
+ expect(allowlist.matches("C999")).toBe(false);
186
+ expect(allowlist.unresolvedNames()).toEqual(["issues-glass"]);
187
+ allowlist.applyResolvedNames(new Map([["issues-glass", "C999"]]));
188
+ expect(allowlist.matches("C999")).toBe(true);
189
+ });
190
+
191
+ it("treats bare names as names, not ids", () => {
192
+ const allowlist = new SlackChannelAllowlist(["issues-glass"]);
193
+ expect(allowlist.unresolvedNames()).toEqual(["issues-glass"]);
194
+ });
195
+ });
196
+
197
+ describe("createChannelPostWatcher", () => {
198
+ const dispatched: Array<{ message: SlackMessage; eventId?: string }> = [];
199
+
200
+ function watcher(args?: {
201
+ posts?: "top-level" | "all";
202
+ debounceMs?: number;
203
+ includeBotPosts?: boolean;
204
+ botUserId?: string;
205
+ botId?: string;
206
+ allow?: string[];
207
+ }) {
208
+ return createChannelPostWatcher({
209
+ options: {
210
+ allow: args?.allow ?? ["C123"],
211
+ ...(args?.posts === undefined ? {} : { posts: args.posts }),
212
+ ...(args?.debounceMs === undefined
213
+ ? {}
214
+ : { debounceMs: args.debounceMs }),
215
+ ...(args?.includeBotPosts === undefined
216
+ ? {}
217
+ : { includeBotPosts: args.includeBotPosts }),
218
+ },
219
+ allowlist: new SlackChannelAllowlist(args?.allow ?? ["C123"]),
220
+ getBotUserId: () => args?.botUserId,
221
+ getBotId: () => args?.botId,
222
+ dispatch: (message, eventId) =>
223
+ dispatched.push(
224
+ eventId === undefined ? { message } : { message, eventId }
225
+ ),
226
+ });
227
+ }
228
+
229
+ function post(args: {
230
+ channel?: string;
231
+ ts?: string;
232
+ threadTs?: string;
233
+ text?: string;
234
+ subtype?: string;
235
+ botId?: string;
236
+ user?: string | null;
237
+ }): SlackChannelMessageEvent {
238
+ const parsed = parseChannelMessageEvent(channelPostBody(args), {
239
+ includeBotAuthored: true,
240
+ });
241
+ if (parsed === null) {
242
+ throw new Error("fixture did not parse");
243
+ }
244
+ return parsed;
245
+ }
246
+
247
+ beforeEach(() => {
248
+ dispatched.length = 0;
249
+ });
250
+ afterEach(() => {
251
+ vi.useRealTimers();
252
+ });
253
+
254
+ it("dispatches allowlisted top-level posts immediately by default", () => {
255
+ const w = watcher();
256
+ expect(w.handle(post({}), "Ev1")).toBe(true);
257
+ expect(dispatched).toHaveLength(1);
258
+ expect(dispatched[0]?.eventId).toBe("Ev1");
259
+ });
260
+
261
+ it("skips channels outside the allowlist", () => {
262
+ const w = watcher();
263
+ w.handle(post({ channel: "C999" }));
264
+ expect(dispatched).toHaveLength(0);
265
+ });
266
+
267
+ it("skips thread replies unless posts is 'all'", () => {
268
+ const w = watcher();
269
+ w.handle(post({ ts: "100.2", threadTs: "100.1" }));
270
+ expect(dispatched).toHaveLength(0);
271
+
272
+ const all = watcher({ posts: "all" });
273
+ all.handle(post({ ts: "100.2", threadTs: "100.1" }));
274
+ expect(dispatched).toHaveLength(1);
275
+ });
276
+
277
+ it("skips posts that mention the bot (app_mention path owns them)", () => {
278
+ const w = watcher({ botUserId: "UBOT" });
279
+ w.handle(post({ text: "<@UBOT> please triage" }));
280
+ expect(dispatched).toHaveLength(0);
281
+ });
282
+
283
+ it("skips bot-authored posts unless includeBotPosts is set", () => {
284
+ const w = watcher();
285
+ w.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
286
+ expect(dispatched).toHaveLength(0);
287
+
288
+ const withBots = watcher({
289
+ includeBotPosts: true,
290
+ botId: "BSELF",
291
+ botUserId: "UBOT",
292
+ });
293
+ withBots.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
294
+ expect(dispatched).toHaveLength(1);
295
+ expect(dispatched[0]?.message.author?.botId).toBe("B77");
296
+ });
297
+
298
+ it("skips bot-authored posts when includeBotPosts is on but bot identity is unknown", () => {
299
+ const w = watcher({ includeBotPosts: true });
300
+ w.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
301
+ expect(dispatched).toHaveLength(0);
302
+ });
303
+
304
+ it("always skips the watching bot's own posts (loop safety)", () => {
305
+ const w = watcher({
306
+ includeBotPosts: true,
307
+ botUserId: "UBOT",
308
+ botId: "BSELF",
309
+ });
310
+ w.handle(post({ botId: "BSELF", user: null, subtype: "bot_message" }));
311
+ expect(dispatched).toHaveLength(0);
312
+
313
+ // Same app posting with its bot user id attached.
314
+ w.handle(post({ botId: "B99", user: "UBOT" }));
315
+ expect(dispatched).toHaveLength(0);
316
+
317
+ // A different bot still dispatches.
318
+ w.handle(post({ botId: "B77", user: null, subtype: "bot_message" }));
319
+ expect(dispatched).toHaveLength(1);
320
+ });
321
+
322
+ it("debounces: edits update the pending text, deletes cancel", () => {
323
+ vi.useFakeTimers();
324
+ const w = watcher({ debounceMs: 1_000 });
325
+
326
+ w.handle(post({ ts: "100.1", text: "first" }));
327
+ expect(dispatched).toHaveLength(0);
328
+ const edit = parseChannelMessageEvent({
329
+ type: "event_callback",
330
+ event: {
331
+ type: "message",
332
+ subtype: "message_changed",
333
+ channel: "C123",
334
+ channel_type: "channel",
335
+ ts: "101.0",
336
+ message: { type: "message", ts: "100.1", text: "edited", user: "U1" },
337
+ },
338
+ });
339
+ if (edit === null) {
340
+ throw new Error("edit fixture did not parse");
341
+ }
342
+ w.handle(edit);
343
+ vi.advanceTimersByTime(1_000);
344
+ expect(dispatched).toHaveLength(1);
345
+ expect(dispatched[0]?.message.text).toBe("edited");
346
+
347
+ w.handle(post({ ts: "200.1", text: "will be deleted" }));
348
+ w.handle({ kind: "deleted", channelId: "C123", ts: "200.1" });
349
+ vi.advanceTimersByTime(5_000);
350
+ expect(dispatched).toHaveLength(1);
351
+
352
+ w.stop();
353
+ });
354
+
355
+ it("stop() cancels pending dispatches", () => {
356
+ vi.useFakeTimers();
357
+ const w = watcher({ debounceMs: 1_000 });
358
+ w.handle(post({}));
359
+ w.stop();
360
+ vi.advanceTimersByTime(5_000);
361
+ expect(dispatched).toHaveLength(0);
362
+ });
363
+ });